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