[OOPS] That was not meant to be pushed/commited
[reactos.git] / base / services / nfsd / nfs41_daemon.c
1 /* NFSv4.1 client for Windows
2 * Copyright © 2012 The Regents of the University of Michigan
3 *
4 * Olga Kornievskaia <aglo@umich.edu>
5 * Casey Bodley <cbodley@umich.edu>
6 *
7 * This library is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as published by
9 * the Free Software Foundation; either version 2.1 of the License, or (at
10 * your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful, but
13 * without any warranty; without even the implied warranty of merchantability
14 * or fitness for a particular purpose. See the GNU Lesser General Public
15 * License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with this library; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 */
21
22 #include <windows.h>
23 #include <process.h>
24 #include <tchar.h>
25 #include <stdio.h>
26 #ifdef __REACTOS__
27 #include <strsafe.h>
28 #endif
29
30 #include <devioctl.h>
31 #include <lmcons.h> /* UNLEN for GetUserName() */
32 #include <iphlpapi.h> /* for GetNetworkParam() */
33 #include "nfs41_driver.h" /* for NFS41_USER_DEVICE_NAME_A */
34 #include "nfs41_np.h" /* for NFS41NP_SHARED_MEMORY */
35
36 #include "idmap.h"
37 #include "daemon_debug.h"
38 #include "upcall.h"
39 #include "util.h"
40
41 #define MAX_NUM_THREADS 128
42 DWORD NFS41D_VERSION = 0;
43
44 #ifndef __REACTOS__
45 static const char FILE_NETCONFIG[] = "C:\\ReactOS\\System32\\drivers\\etc\\netconfig";
46 #endif
47
48 /* Globals */
49 char localdomain_name[NFS41_HOSTNAME_LEN];
50 int default_uid = 666;
51 int default_gid = 777;
52
53 #ifndef STANDALONE_NFSD //make sure to define it in "sources" not here
54 #include "service.h"
55 HANDLE stop_event = NULL;
56 #endif
57 typedef struct _nfs41_process_thread {
58 HANDLE handle;
59 uint32_t tid;
60 } nfs41_process_thread;
61
62 static int map_user_to_ids(nfs41_idmapper *idmapper, uid_t *uid, gid_t *gid)
63 {
64 char username[UNLEN + 1];
65 DWORD len = UNLEN + 1;
66 int status = NO_ERROR;
67
68 if (!GetUserNameA(username, &len)) {
69 status = GetLastError();
70 eprintf("GetUserName() failed with %d\n", status);
71 goto out;
72 }
73 dprintf(1, "map_user_to_ids: mapping user %s\n", username);
74
75 if (nfs41_idmap_name_to_ids(idmapper, username, uid, gid)) {
76 /* instead of failing for auth_sys, fall back to 'nobody' uid/gid */
77 *uid = default_uid;
78 *gid = default_gid;
79 }
80 out:
81 return status;
82 }
83
84 static unsigned int WINAPI thread_main(void *args)
85 {
86 nfs41_idmapper *idmapper = (nfs41_idmapper*)args;
87 DWORD status = 0;
88 HANDLE pipe;
89 // buffer used to process upcall, assumed to be fixed size.
90 // if we ever need to handle non-cached IO, need to make it dynamic
91 unsigned char outbuf[UPCALL_BUF_SIZE], inbuf[UPCALL_BUF_SIZE];
92 DWORD inbuf_len = UPCALL_BUF_SIZE, outbuf_len;
93 nfs41_upcall upcall;
94
95 pipe = CreateFile(NFS41_USER_DEVICE_NAME_A, GENERIC_READ | GENERIC_WRITE,
96 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING,
97 0, NULL);
98 if (pipe == INVALID_HANDLE_VALUE)
99 {
100 eprintf("Unable to open upcall pipe %d\n", GetLastError());
101 return GetLastError();
102 }
103
104 while(1) {
105 status = DeviceIoControl(pipe, IOCTL_NFS41_READ, NULL, 0,
106 outbuf, UPCALL_BUF_SIZE, (LPDWORD)&outbuf_len, NULL);
107 if (!status) {
108 eprintf("IOCTL_NFS41_READ failed %d\n", GetLastError());
109 continue;
110 }
111
112 status = upcall_parse(outbuf, (uint32_t)outbuf_len, &upcall);
113 if (status) {
114 upcall.status = status;
115 goto write_downcall;
116 }
117
118 /* map username to uid/gid */
119 status = map_user_to_ids(idmapper, &upcall.uid, &upcall.gid);
120 if (status) {
121 upcall.status = status;
122 goto write_downcall;
123 }
124
125 if (upcall.opcode == NFS41_SHUTDOWN) {
126 printf("Shutting down..\n");
127 exit(0);
128 }
129
130 status = upcall_handle(&upcall);
131
132 write_downcall:
133 dprintf(1, "writing downcall: xid=%lld opcode=%s status=%d "
134 "get_last_error=%d\n", upcall.xid, opcode2string(upcall.opcode),
135 upcall.status, upcall.last_error);
136
137 upcall_marshall(&upcall, inbuf, (uint32_t)inbuf_len, (uint32_t*)&outbuf_len);
138
139 dprintf(2, "making a downcall: outbuf_len %ld\n\n", outbuf_len);
140 status = DeviceIoControl(pipe, IOCTL_NFS41_WRITE,
141 inbuf, inbuf_len, NULL, 0, (LPDWORD)&outbuf_len, NULL);
142 if (!status) {
143 eprintf("IOCTL_NFS41_WRITE failed with %d xid=%lld opcode=%s\n",
144 GetLastError(), upcall.xid, opcode2string(upcall.opcode));
145 upcall_cancel(&upcall);
146 }
147 if (upcall.status != NFSD_VERSION_MISMATCH)
148 upcall_cleanup(&upcall);
149 }
150 CloseHandle(pipe);
151
152 return GetLastError();
153 }
154
155 #ifndef STANDALONE_NFSD
156 VOID ServiceStop()
157 {
158 if (stop_event)
159 SetEvent(stop_event);
160 }
161 #endif
162
163 typedef struct _nfsd_args {
164 bool_t ldap_enable;
165 int debug_level;
166 } nfsd_args;
167
168 static bool_t check_for_files()
169 {
170 FILE *fd;
171 #ifdef __REACTOS__
172 char config_path[MAX_PATH];
173
174 if (GetSystemDirectoryA(config_path, ARRAYSIZE(config_path)))
175 {
176 StringCchCatA(config_path, ARRAYSIZE(config_path), "\\drivers\\etc\\netconfig");
177 }
178 else
179 {
180 StringCchCopyA(config_path, ARRAYSIZE(config_path), "C:\\ReactOS\\system32\\drivers\\etc\\netconfig");
181 }
182
183 fd = fopen(config_path, "r");
184 #else
185
186 fd = fopen(FILE_NETCONFIG, "r");
187 #endif
188 if (fd == NULL) {
189 #ifdef __REACTOS__
190 fprintf(stderr,"nfsd() failed to open file '%s'\n", config_path);
191 #else
192 fprintf(stderr,"nfsd() failed to open file '%s'\n", FILE_NETCONFIG);
193 #endif
194 return FALSE;
195 }
196 fclose(fd);
197 return TRUE;
198 }
199
200 static void PrintUsage()
201 {
202 fprintf(stderr, "Usage: nfsd.exe -d <debug_level> --noldap "
203 "--uid <non-zero value> --gid\n");
204 }
205 static bool_t parse_cmdlineargs(int argc, TCHAR *argv[], nfsd_args *out)
206 {
207 int i;
208
209 /* set defaults. */
210 out->debug_level = 1;
211 out->ldap_enable = TRUE;
212
213 /* parse command line */
214 for (i = 1; i < argc; i++) {
215 if (argv[i][0] == TEXT('-')) {
216 if (_tcscmp(argv[i], TEXT("-h")) == 0) { /* help */
217 PrintUsage();
218 return FALSE;
219 }
220 else if (_tcscmp(argv[i], TEXT("-d")) == 0) { /* debug level */
221 ++i;
222 if (i >= argc) {
223 fprintf(stderr, "Missing debug level value\n");
224 PrintUsage();
225 return FALSE;
226 }
227 out->debug_level = _ttoi(argv[i]);
228 }
229 else if (_tcscmp(argv[i], TEXT("--noldap")) == 0) { /* no LDAP */
230 out->ldap_enable = FALSE;
231 }
232 else if (_tcscmp(argv[i], TEXT("--uid")) == 0) { /* no LDAP, setting default uid */
233 ++i;
234 if (i >= argc) {
235 fprintf(stderr, "Missing uid value\n");
236 PrintUsage();
237 return FALSE;
238 }
239 default_uid = _ttoi(argv[i]);
240 if (!default_uid) {
241 fprintf(stderr, "Invalid (or missing) anonymous uid value of %d\n",
242 default_uid);
243 return FALSE;
244 }
245 }
246 else if (_tcscmp(argv[i], TEXT("--gid")) == 0) { /* no LDAP, setting default gid */
247 ++i;
248 if (i >= argc) {
249 fprintf(stderr, "Missing gid value\n");
250 PrintUsage();
251 return FALSE;
252 }
253 default_gid = _ttoi(argv[i]);
254 }
255 else
256 fprintf(stderr, "Unrecognized option '%s', disregarding.\n", argv[i]);
257 }
258 }
259 fprintf(stdout, "parse_cmdlineargs: debug_level %d ldap is %d\n",
260 out->debug_level, out->ldap_enable);
261 return TRUE;
262 }
263
264 static void print_getaddrinfo(struct addrinfo *ptr)
265 {
266 char ipstringbuffer[46];
267 DWORD ipbufferlength = 46;
268
269 dprintf(1, "getaddrinfo response flags: 0x%x\n", ptr->ai_flags);
270 switch (ptr->ai_family) {
271 case AF_UNSPEC: dprintf(1, "Family: Unspecified\n"); break;
272 case AF_INET:
273 dprintf(1, "Family: AF_INET IPv4 address %s\n",
274 inet_ntoa(((struct sockaddr_in *)ptr->ai_addr)->sin_addr));
275 break;
276 case AF_INET6:
277 if (WSAAddressToString((LPSOCKADDR)ptr->ai_addr, (DWORD)ptr->ai_addrlen,
278 NULL, ipstringbuffer, &ipbufferlength))
279 dprintf(1, "WSAAddressToString failed with %u\n", WSAGetLastError());
280 else
281 dprintf(1, "Family: AF_INET6 IPv6 address %s\n", ipstringbuffer);
282 break;
283 case AF_NETBIOS: dprintf(1, "AF_NETBIOS (NetBIOS)\n"); break;
284 default: dprintf(1, "Other %ld\n", ptr->ai_family); break;
285 }
286 dprintf(1, "Canonical name: %s\n", ptr->ai_canonname);
287 }
288
289 static int getdomainname()
290 {
291 int status = 0;
292 PFIXED_INFO net_info = NULL;
293 DWORD size = 0;
294 BOOLEAN flag = FALSE;
295
296 status = GetNetworkParams(net_info, &size);
297 if (status != ERROR_BUFFER_OVERFLOW) {
298 eprintf("getdomainname: GetNetworkParams returned %d\n", status);
299 goto out;
300 }
301 net_info = calloc(1, size);
302 if (net_info == NULL) {
303 status = GetLastError();
304 goto out;
305 }
306 status = GetNetworkParams(net_info, &size);
307 if (status) {
308 eprintf("getdomainname: GetNetworkParams returned %d\n", status);
309 goto out_free;
310 }
311
312 if (net_info->DomainName[0] == '\0') {
313 struct addrinfo *result = NULL, *ptr = NULL, hints = { 0 };
314 char hostname[NI_MAXHOST], servInfo[NI_MAXSERV];
315
316 hints.ai_socktype = SOCK_STREAM;
317 hints.ai_protocol = IPPROTO_TCP;
318
319 status = getaddrinfo(net_info->HostName, NULL, &hints, &result);
320 if (status) {
321 status = WSAGetLastError();
322 eprintf("getdomainname: getaddrinfo failed with %d\n", status);
323 goto out_free;
324 }
325
326 for (ptr=result; ptr != NULL; ptr=ptr->ai_next) {
327 print_getaddrinfo(ptr);
328
329 switch (ptr->ai_family) {
330 case AF_INET6:
331 case AF_INET:
332 status = getnameinfo((struct sockaddr *)ptr->ai_addr,
333 (socklen_t)ptr->ai_addrlen, hostname, NI_MAXHOST,
334 servInfo, NI_MAXSERV, NI_NAMEREQD);
335 if (status)
336 dprintf(1, "getnameinfo failed %d\n", WSAGetLastError());
337 else {
338 size_t i, len = strlen(hostname);
339 char *p = hostname;
340 dprintf(1, "getdomainname: hostname %s %d\n", hostname, len);
341 for (i = 0; i < len; i++)
342 if (p[i] == '.')
343 break;
344 if (i == len)
345 break;
346 flag = TRUE;
347 memcpy(localdomain_name, &hostname[i+1], len-i);
348 dprintf(1, "getdomainname: domainname %s %d\n",
349 localdomain_name, strlen(localdomain_name));
350 goto out_loop;
351 }
352 break;
353 default:
354 break;
355 }
356 }
357 out_loop:
358 if (!flag) {
359 status = ERROR_INTERNAL_ERROR;
360 eprintf("getdomainname: unable to get a domain name. "
361 "Set this machine's domain name:\n"
362 "System > ComputerName > Change > More > mydomain\n");
363 }
364 freeaddrinfo(result);
365 } else {
366 dprintf(1, "domain name is %s\n", net_info->DomainName);
367 memcpy(localdomain_name, net_info->DomainName,
368 strlen(net_info->DomainName));
369 localdomain_name[strlen(net_info->DomainName)] = '\0';
370 }
371 out_free:
372 free(net_info);
373 out:
374 return status;
375 }
376
377 #ifdef STANDALONE_NFSD
378 void __cdecl _tmain(int argc, TCHAR *argv[])
379 #else
380 VOID ServiceStart(DWORD argc, LPTSTR *argv)
381 #endif
382 {
383 DWORD status = 0, len;
384 // handle to our drivers
385 HANDLE pipe;
386 nfs41_process_thread tids[MAX_NUM_THREADS];
387 nfs41_idmapper *idmapper = NULL;
388 int i;
389 nfsd_args cmd_args;
390
391 if (!check_for_files())
392 exit(0);
393 if (!parse_cmdlineargs(argc, argv, &cmd_args))
394 exit(0);
395 set_debug_level(cmd_args.debug_level);
396 open_log_files();
397
398 #ifdef __REACTOS__
399 /* Start the kernel part */
400 {
401 HANDLE hSvcMan = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
402 if (hSvcMan)
403 {
404 HANDLE hSvc = OpenService(hSvcMan, "nfs41_driver", SERVICE_ALL_ACCESS);
405 if (hSvc)
406 {
407 SERVICE_STATUS SvcSt;
408 QueryServiceStatus(hSvc, &SvcSt);
409 if (SvcSt.dwCurrentState != SERVICE_RUNNING)
410 {
411 if (StartService(hSvc, 0, NULL))
412 {
413 dprintf(1, "NFS41 driver started\n");
414 }
415 else
416 {
417 eprintf("Driver failed to start: %d\n", GetLastError());
418 }
419 }
420 else
421 {
422 eprintf("Driver in state: %x\n", SvcSt.dwCurrentState);
423 }
424
425 CloseServiceHandle(hSvc);
426 }
427 else
428 {
429 eprintf("Failed to open service: %d\n", GetLastError());
430 }
431
432 CloseServiceHandle(hSvcMan);
433 }
434 else
435 {
436 eprintf("Failed to open service manager: %d\n", GetLastError());
437 }
438 }
439 #endif
440
441 #ifdef _DEBUG
442 /* dump memory leaks to stderr on exit; this requires the debug heap,
443 /* available only when built in debug mode under visual studio -cbodley */
444 _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
445 _CrtSetReportMode(_CRT_WARN, _CRTDBG_MODE_FILE);
446 #pragma warning (push)
447 #pragma warning (disable : 4306) /* conversion from 'int' to '_HFILE' of greater size */
448 _CrtSetReportFile(_CRT_WARN, _CRTDBG_FILE_STDERR);
449 #pragma warning (pop)
450 dprintf(1, "debug mode. dumping memory leaks to stderr on exit.\n");
451 #endif
452 /* acquire and store in global memory current dns domain name.
453 * needed for acls */
454 if (getdomainname())
455 exit(0);
456
457 nfs41_server_list_init();
458
459 if (cmd_args.ldap_enable) {
460 status = nfs41_idmap_create(&idmapper);
461 if (status) {
462 eprintf("id mapping initialization failed with %d\n", status);
463 goto out_logs;
464 }
465 }
466
467 NFS41D_VERSION = GetTickCount();
468 dprintf(1, "NFS41 Daemon starting: version %d\n", NFS41D_VERSION);
469
470 pipe = CreateFile(NFS41_USER_DEVICE_NAME_A, GENERIC_READ | GENERIC_WRITE,
471 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING,
472 0, NULL);
473 if (pipe == INVALID_HANDLE_VALUE)
474 {
475 eprintf("Unable to open upcall pipe %d\n", GetLastError());
476 goto out_idmap;
477 }
478
479 dprintf(1, "starting nfs41 mini redirector\n");
480 status = DeviceIoControl(pipe, IOCTL_NFS41_START,
481 &NFS41D_VERSION, sizeof(DWORD), NULL, 0, (LPDWORD)&len, NULL);
482 if (!status) {
483 eprintf("IOCTL_NFS41_START failed with %d\n",
484 GetLastError());
485 goto out_pipe;
486 }
487
488 #ifndef STANDALONE_NFSD
489 stop_event = CreateEvent(NULL, TRUE, FALSE, NULL);
490 if (stop_event == NULL)
491 goto out_pipe;
492 #endif
493
494 for (i = 0; i < MAX_NUM_THREADS; i++) {
495 tids[i].handle = (HANDLE)_beginthreadex(NULL, 0, thread_main,
496 idmapper, 0, &tids[i].tid);
497 if (tids[i].handle == INVALID_HANDLE_VALUE) {
498 status = GetLastError();
499 eprintf("_beginthreadex failed %d\n", status);
500 goto out_pipe;
501 }
502 }
503 #ifndef STANDALONE_NFSD
504 // report the status to the service control manager.
505 if (!ReportStatusToSCMgr(SERVICE_RUNNING, NO_ERROR, 0))
506 goto out_pipe;
507 WaitForSingleObject(stop_event, INFINITE);
508 #else
509 //This can be changed to waiting on an array of handles and using waitformultipleobjects
510 dprintf(1, "Parent waiting for children threads\n");
511 for (i = 0; i < MAX_NUM_THREADS; i++)
512 WaitForSingleObject(tids[i].handle, INFINITE );
513 #endif
514 dprintf(1, "Parent woke up!!!!\n");
515
516 out_pipe:
517 CloseHandle(pipe);
518 out_idmap:
519 if (idmapper) nfs41_idmap_free(idmapper);
520 out_logs:
521 #ifndef STANDALONE_NFSD
522 close_log_files();
523 #endif
524 return;
525 }