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
map_user_to_ids(nfs41_idmapper * idmapper,uid_t * uid,gid_t * gid)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
thread_main(void * args)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
ServiceStop()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
check_for_files()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 eprintf("GetSystemDirectoryA failed with error %ld\n", GetLastError());
181 return FALSE;
182 }
183
184 fd = fopen(config_path, "r");
185 #else
186
187 fd = fopen(FILE_NETCONFIG, "r");
188 #endif
189 if (fd == NULL) {
190 #ifdef __REACTOS__
191 fprintf(stderr,"nfsd() failed to open file '%s'\n", config_path);
192 #else
193 fprintf(stderr,"nfsd() failed to open file '%s'\n", FILE_NETCONFIG);
194 #endif
195 return FALSE;
196 }
197 fclose(fd);
198 return TRUE;
199 }
200
PrintUsage()201 static void PrintUsage()
202 {
203 fprintf(stderr, "Usage: nfsd.exe -d <debug_level> --noldap "
204 "--uid <non-zero value> --gid\n");
205 }
parse_cmdlineargs(int argc,TCHAR * argv[],nfsd_args * out)206 static bool_t parse_cmdlineargs(int argc, TCHAR *argv[], nfsd_args *out)
207 {
208 int i;
209
210 /* set defaults. */
211 out->debug_level = 1;
212 out->ldap_enable = TRUE;
213
214 /* parse command line */
215 for (i = 1; i < argc; i++) {
216 if (argv[i][0] == TEXT('-')) {
217 if (_tcscmp(argv[i], TEXT("-h")) == 0) { /* help */
218 PrintUsage();
219 return FALSE;
220 }
221 else if (_tcscmp(argv[i], TEXT("-d")) == 0) { /* debug level */
222 ++i;
223 if (i >= argc) {
224 fprintf(stderr, "Missing debug level value\n");
225 PrintUsage();
226 return FALSE;
227 }
228 out->debug_level = _ttoi(argv[i]);
229 }
230 else if (_tcscmp(argv[i], TEXT("--noldap")) == 0) { /* no LDAP */
231 out->ldap_enable = FALSE;
232 }
233 else if (_tcscmp(argv[i], TEXT("--uid")) == 0) { /* no LDAP, setting default uid */
234 ++i;
235 if (i >= argc) {
236 fprintf(stderr, "Missing uid value\n");
237 PrintUsage();
238 return FALSE;
239 }
240 default_uid = _ttoi(argv[i]);
241 if (!default_uid) {
242 fprintf(stderr, "Invalid (or missing) anonymous uid value of %d\n",
243 default_uid);
244 return FALSE;
245 }
246 }
247 else if (_tcscmp(argv[i], TEXT("--gid")) == 0) { /* no LDAP, setting default gid */
248 ++i;
249 if (i >= argc) {
250 fprintf(stderr, "Missing gid value\n");
251 PrintUsage();
252 return FALSE;
253 }
254 default_gid = _ttoi(argv[i]);
255 }
256 else
257 fprintf(stderr, "Unrecognized option '%s', disregarding.\n", argv[i]);
258 }
259 }
260 fprintf(stdout, "parse_cmdlineargs: debug_level %d ldap is %d\n",
261 out->debug_level, out->ldap_enable);
262 return TRUE;
263 }
264
print_getaddrinfo(struct addrinfo * ptr)265 static void print_getaddrinfo(struct addrinfo *ptr)
266 {
267 char ipstringbuffer[46];
268 DWORD ipbufferlength = 46;
269
270 dprintf(1, "getaddrinfo response flags: 0x%x\n", ptr->ai_flags);
271 switch (ptr->ai_family) {
272 case AF_UNSPEC: dprintf(1, "Family: Unspecified\n"); break;
273 case AF_INET:
274 dprintf(1, "Family: AF_INET IPv4 address %s\n",
275 inet_ntoa(((struct sockaddr_in *)ptr->ai_addr)->sin_addr));
276 break;
277 case AF_INET6:
278 if (WSAAddressToString((LPSOCKADDR)ptr->ai_addr, (DWORD)ptr->ai_addrlen,
279 NULL, ipstringbuffer, &ipbufferlength))
280 dprintf(1, "WSAAddressToString failed with %u\n", WSAGetLastError());
281 else
282 dprintf(1, "Family: AF_INET6 IPv6 address %s\n", ipstringbuffer);
283 break;
284 case AF_NETBIOS: dprintf(1, "AF_NETBIOS (NetBIOS)\n"); break;
285 default: dprintf(1, "Other %ld\n", ptr->ai_family); break;
286 }
287 dprintf(1, "Canonical name: %s\n", ptr->ai_canonname);
288 }
289
getdomainname()290 static int getdomainname()
291 {
292 int status = 0;
293 PFIXED_INFO net_info = NULL;
294 DWORD size = 0;
295 BOOLEAN flag = FALSE;
296
297 status = GetNetworkParams(net_info, &size);
298 if (status != ERROR_BUFFER_OVERFLOW) {
299 eprintf("getdomainname: GetNetworkParams returned %d\n", status);
300 goto out;
301 }
302 net_info = calloc(1, size);
303 if (net_info == NULL) {
304 status = GetLastError();
305 goto out;
306 }
307 status = GetNetworkParams(net_info, &size);
308 if (status) {
309 eprintf("getdomainname: GetNetworkParams returned %d\n", status);
310 goto out_free;
311 }
312
313 if (net_info->DomainName[0] == '\0') {
314 struct addrinfo *result = NULL, *ptr = NULL, hints = { 0 };
315 char hostname[NI_MAXHOST], servInfo[NI_MAXSERV];
316
317 hints.ai_socktype = SOCK_STREAM;
318 hints.ai_protocol = IPPROTO_TCP;
319
320 status = getaddrinfo(net_info->HostName, NULL, &hints, &result);
321 if (status) {
322 status = WSAGetLastError();
323 eprintf("getdomainname: getaddrinfo failed with %d\n", status);
324 goto out_free;
325 }
326
327 for (ptr=result; ptr != NULL; ptr=ptr->ai_next) {
328 print_getaddrinfo(ptr);
329
330 switch (ptr->ai_family) {
331 case AF_INET6:
332 case AF_INET:
333 status = getnameinfo((struct sockaddr *)ptr->ai_addr,
334 (socklen_t)ptr->ai_addrlen, hostname, NI_MAXHOST,
335 servInfo, NI_MAXSERV, NI_NAMEREQD);
336 if (status)
337 dprintf(1, "getnameinfo failed %d\n", WSAGetLastError());
338 else {
339 size_t i, len = strlen(hostname);
340 char *p = hostname;
341 dprintf(1, "getdomainname: hostname %s %d\n", hostname, len);
342 for (i = 0; i < len; i++)
343 if (p[i] == '.')
344 break;
345 if (i == len)
346 break;
347 flag = TRUE;
348 memcpy(localdomain_name, &hostname[i+1], len-i);
349 dprintf(1, "getdomainname: domainname %s %d\n",
350 localdomain_name, strlen(localdomain_name));
351 goto out_loop;
352 }
353 break;
354 default:
355 break;
356 }
357 }
358 out_loop:
359 if (!flag) {
360 status = ERROR_INTERNAL_ERROR;
361 eprintf("getdomainname: unable to get a domain name. "
362 "Set this machine's domain name:\n"
363 "System > ComputerName > Change > More > mydomain\n");
364 }
365 freeaddrinfo(result);
366 } else {
367 dprintf(1, "domain name is %s\n", net_info->DomainName);
368 memcpy(localdomain_name, net_info->DomainName,
369 strlen(net_info->DomainName));
370 localdomain_name[strlen(net_info->DomainName)] = '\0';
371 }
372 out_free:
373 free(net_info);
374 out:
375 return status;
376 }
377
378 #ifdef STANDALONE_NFSD
_tmain(int argc,TCHAR * argv[])379 void __cdecl _tmain(int argc, TCHAR *argv[])
380 #else
381 VOID ServiceStart(DWORD argc, LPTSTR *argv)
382 #endif
383 {
384 DWORD status = 0, len;
385 // handle to our drivers
386 HANDLE pipe;
387 nfs41_process_thread tids[MAX_NUM_THREADS];
388 nfs41_idmapper *idmapper = NULL;
389 int i;
390 nfsd_args cmd_args;
391
392 if (!check_for_files())
393 exit(0);
394 if (!parse_cmdlineargs(argc, argv, &cmd_args))
395 exit(0);
396 set_debug_level(cmd_args.debug_level);
397 open_log_files();
398
399 #ifdef __REACTOS__
400 /* Start the kernel part */
401 {
402 HANDLE hSvcMan = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
403 if (hSvcMan)
404 {
405 HANDLE hSvc = OpenService(hSvcMan, "nfs41_driver", SERVICE_ALL_ACCESS);
406 if (hSvc)
407 {
408 SERVICE_STATUS SvcSt;
409 QueryServiceStatus(hSvc, &SvcSt);
410 if (SvcSt.dwCurrentState != SERVICE_RUNNING)
411 {
412 if (StartService(hSvc, 0, NULL))
413 {
414 dprintf(1, "NFS41 driver started\n");
415 }
416 else
417 {
418 eprintf("Driver failed to start: %d\n", GetLastError());
419 }
420 }
421 else
422 {
423 eprintf("Driver in state: %x\n", SvcSt.dwCurrentState);
424 }
425
426 CloseServiceHandle(hSvc);
427 }
428 else
429 {
430 eprintf("Failed to open service: %d\n", GetLastError());
431 }
432
433 CloseServiceHandle(hSvcMan);
434 }
435 else
436 {
437 eprintf("Failed to open service manager: %d\n", GetLastError());
438 }
439 }
440 #endif
441
442 #ifdef _DEBUG
443 /* dump memory leaks to stderr on exit; this requires the debug heap,
444 /* available only when built in debug mode under visual studio -cbodley */
445 _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
446 _CrtSetReportMode(_CRT_WARN, _CRTDBG_MODE_FILE);
447 #pragma warning (push)
448 #pragma warning (disable : 4306) /* conversion from 'int' to '_HFILE' of greater size */
449 _CrtSetReportFile(_CRT_WARN, _CRTDBG_FILE_STDERR);
450 #pragma warning (pop)
451 dprintf(1, "debug mode. dumping memory leaks to stderr on exit.\n");
452 #endif
453 /* acquire and store in global memory current dns domain name.
454 * needed for acls */
455 if (getdomainname())
456 exit(0);
457
458 nfs41_server_list_init();
459
460 if (cmd_args.ldap_enable) {
461 status = nfs41_idmap_create(&idmapper);
462 if (status) {
463 eprintf("id mapping initialization failed with %d\n", status);
464 goto out_logs;
465 }
466 }
467
468 NFS41D_VERSION = GetTickCount();
469 dprintf(1, "NFS41 Daemon starting: version %d\n", NFS41D_VERSION);
470
471 pipe = CreateFile(NFS41_USER_DEVICE_NAME_A, GENERIC_READ | GENERIC_WRITE,
472 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING,
473 0, NULL);
474 if (pipe == INVALID_HANDLE_VALUE)
475 {
476 eprintf("Unable to open upcall pipe %d\n", GetLastError());
477 goto out_idmap;
478 }
479
480 dprintf(1, "starting nfs41 mini redirector\n");
481 status = DeviceIoControl(pipe, IOCTL_NFS41_START,
482 &NFS41D_VERSION, sizeof(DWORD), NULL, 0, (LPDWORD)&len, NULL);
483 if (!status) {
484 eprintf("IOCTL_NFS41_START failed with %d\n",
485 GetLastError());
486 goto out_pipe;
487 }
488
489 #ifndef STANDALONE_NFSD
490 stop_event = CreateEvent(NULL, TRUE, FALSE, NULL);
491 if (stop_event == NULL)
492 goto out_pipe;
493 #endif
494
495 for (i = 0; i < MAX_NUM_THREADS; i++) {
496 tids[i].handle = (HANDLE)_beginthreadex(NULL, 0, thread_main,
497 idmapper, 0, &tids[i].tid);
498 if (tids[i].handle == INVALID_HANDLE_VALUE) {
499 status = GetLastError();
500 eprintf("_beginthreadex failed %d\n", status);
501 goto out_pipe;
502 }
503 }
504 #ifndef STANDALONE_NFSD
505 // report the status to the service control manager.
506 if (!ReportStatusToSCMgr(SERVICE_RUNNING, NO_ERROR, 0))
507 goto out_pipe;
508 WaitForSingleObject(stop_event, INFINITE);
509 #else
510 //This can be changed to waiting on an array of handles and using waitformultipleobjects
511 dprintf(1, "Parent waiting for children threads\n");
512 for (i = 0; i < MAX_NUM_THREADS; i++)
513 WaitForSingleObject(tids[i].handle, INFINITE );
514 #endif
515 dprintf(1, "Parent woke up!!!!\n");
516
517 out_pipe:
518 CloseHandle(pipe);
519 out_idmap:
520 if (idmapper) nfs41_idmap_free(idmapper);
521 out_logs:
522 #ifndef STANDALONE_NFSD
523 close_log_files();
524 #endif
525 return;
526 }
527