]> sourceware.org Git - newlib-cygwin.git/blob - winsup/utils/mkpasswd.c
* mkgroup.c (main): Keep correctly track of optional arguments.
[newlib-cygwin.git] / winsup / utils / mkpasswd.c
1 /* mkpasswd.c:
2
3 Copyright 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2005, 2006,
4 2008 Red Hat, Inc.
5
6 This file is part of Cygwin.
7
8 This software is a copyrighted work licensed under the terms of the
9 Cygwin license. Please consult the file "CYGWIN_LICENSE" for
10 details. */
11
12 #define _WIN32_WINNT 0x0600
13 #include <ctype.h>
14 #include <stdlib.h>
15 #include <wchar.h>
16 #include <wctype.h>
17 #include <stdio.h>
18 #include <unistd.h>
19 #include <getopt.h>
20 #include <io.h>
21 #include <sys/fcntl.h>
22 #include <sys/cygwin.h>
23 #include <windows.h>
24 #include <lm.h>
25 #include <iptypes.h>
26 #include <wininet.h>
27 #include <ntsecapi.h>
28 #include <dsgetdc.h>
29 #include <ntdef.h>
30
31 #define print_win_error(x) _print_win_error(x, __LINE__)
32
33 #define MAX_SID_LEN 40
34
35 static const char version[] = "$Revision$";
36
37 extern char *__progname;
38
39 SID_IDENTIFIER_AUTHORITY sid_world_auth = {SECURITY_WORLD_SID_AUTHORITY};
40 SID_IDENTIFIER_AUTHORITY sid_nt_auth = {SECURITY_NT_AUTHORITY};
41
42 NET_API_STATUS WINAPI (*dsgetdcname)(LPWSTR,LPWSTR,GUID*,LPWSTR,ULONG,PDOMAIN_CONTROLLER_INFOW*);
43
44 #ifndef min
45 #define min(a,b) (((a)<(b))?(a):(b))
46 #endif
47
48 typedef struct
49 {
50 char *str;
51 DWORD id_offset;
52 BOOL domain;
53 BOOL with_dom;
54 } domlist_t;
55
56 static void
57 _print_win_error(DWORD code, int line)
58 {
59 char buf[4096];
60
61 if (FormatMessage (FORMAT_MESSAGE_FROM_SYSTEM
62 | FORMAT_MESSAGE_IGNORE_INSERTS,
63 NULL,
64 code,
65 MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT),
66 (LPTSTR) buf, sizeof (buf), NULL))
67 fprintf (stderr, "mkpasswd (%d): [%lu] %s", line, code, buf);
68 else
69 fprintf (stderr, "mkpasswd (%d): error %lu", line, code);
70 }
71
72 static void
73 load_dsgetdcname ()
74 {
75 HANDLE h = LoadLibrary ("netapi32.dll");
76
77 if (h)
78 dsgetdcname = (void *) GetProcAddress (h, "DsGetDcNameW");
79 }
80
81 static PWCHAR
82 get_dcname (char *domain)
83 {
84 static WCHAR server[INTERNET_MAX_HOST_NAME_LENGTH + 1];
85 DWORD rc;
86 PWCHAR servername;
87 WCHAR domain_name[MAX_DOMAIN_NAME_LEN + 1];
88 PDOMAIN_CONTROLLER_INFOW pdci = NULL;
89
90 if (dsgetdcname)
91 {
92 if (domain)
93 {
94 mbstowcs (domain_name, domain, strlen (domain) + 1);
95 rc = dsgetdcname (NULL, domain_name, NULL, NULL, 0, &pdci);
96 }
97 else
98 rc = dsgetdcname (NULL, NULL, NULL, NULL, 0, &pdci);
99 if (rc != ERROR_SUCCESS)
100 {
101 print_win_error(rc);
102 return (PWCHAR) -1;
103 }
104 wcscpy (server, pdci->DomainControllerName);
105 NetApiBufferFree (pdci);
106 }
107 else
108 {
109 rc = NetGetDCName (NULL, NULL, (void *) &servername);
110 if (rc == ERROR_SUCCESS && domain)
111 {
112 LPWSTR server = servername;
113 mbstowcs (domain_name, domain, strlen (domain) + 1);
114 rc = NetGetDCName (server, domain_name, (void *) &servername);
115 NetApiBufferFree (server);
116 }
117 if (rc != ERROR_SUCCESS)
118 {
119 print_win_error(rc);
120 return (PWCHAR) -1;
121 }
122 wcscpy (server, servername);
123 NetApiBufferFree ((PVOID) servername);
124 }
125 return server;
126 }
127
128 static char *
129 put_sid (PSID sid)
130 {
131 static char s[512];
132 char t[32];
133 DWORD i;
134
135 strcpy (s, "S-1-");
136 sprintf(t, "%u", GetSidIdentifierAuthority (sid)->Value[5]);
137 strcat (s, t);
138 for (i = 0; i < *GetSidSubAuthorityCount (sid); ++i)
139 {
140 sprintf(t, "-%lu", *GetSidSubAuthority (sid, i));
141 strcat (s, t);
142 }
143 return s;
144 }
145
146 static void
147 psx_dir (char *in, char *out)
148 {
149 if (isalpha (in[0]) && in[1] == ':')
150 {
151 sprintf (out, "/cygdrive/%c", in[0]);
152 in += 2;
153 out += strlen (out);
154 }
155
156 while (*in)
157 {
158 if (*in == '\\')
159 *out = '/';
160 else
161 *out = *in;
162 in++;
163 out++;
164 }
165
166 *out = '\0';
167 }
168
169 static void
170 uni2ansi (LPWSTR wcs, char *mbs, int size)
171 {
172 if (wcs)
173 wcstombs (mbs, wcs, size);
174 else
175 *mbs = '\0';
176 }
177
178 typedef struct {
179 PSID psid;
180 int buffer[10];
181 } sidbuf;
182
183 static sidbuf curr_user;
184 static sidbuf curr_pgrp;
185 static BOOL got_curr_user = FALSE;
186
187 static void
188 fetch_current_user_sid ()
189 {
190 DWORD len;
191 HANDLE ptok;
192
193 if (!OpenProcessToken (GetCurrentProcess (), TOKEN_QUERY, &ptok)
194 || !GetTokenInformation (ptok, TokenUser, &curr_user, sizeof curr_user,
195 &len)
196 || !GetTokenInformation (ptok, TokenPrimaryGroup, &curr_pgrp,
197 sizeof curr_pgrp, &len)
198 || !CloseHandle (ptok))
199 {
200 print_win_error (GetLastError ());
201 return;
202 }
203 }
204
205 static void
206 current_user (int print_cygpath, const char *sep, const char *passed_home_path,
207 DWORD id_offset, const char *disp_username)
208 {
209 WCHAR user[UNLEN + 1];
210 WCHAR dom[MAX_DOMAIN_NAME_LEN + 1];
211 DWORD ulen = UNLEN + 1;
212 DWORD dlen = MAX_DOMAIN_NAME_LEN + 1;
213 SID_NAME_USE acc_type;
214 int uid, gid;
215 char homedir_psx[PATH_MAX] = {0}, homedir_w32[MAX_PATH] = {0};
216
217 if (!curr_user.psid || !curr_pgrp.psid
218 || !LookupAccountSidW (NULL, curr_user.psid, user, &ulen, dom, &dlen,
219 &acc_type))
220 {
221 print_win_error (GetLastError ());
222 return;
223 }
224
225 uid = *GetSidSubAuthority (curr_user.psid,
226 *GetSidSubAuthorityCount(curr_user.psid) - 1);
227 gid = *GetSidSubAuthority (curr_pgrp.psid,
228 *GetSidSubAuthorityCount(curr_pgrp.psid) - 1);
229 if (passed_home_path[0] == '\0')
230 {
231 char *envhome = getenv ("HOME");
232 char *envhomedrive = getenv ("HOMEDRIVE");
233 char *envhomepath = getenv ("HOMEPATH");
234
235 if (envhome && envhome[0])
236 {
237 if (print_cygpath)
238 cygwin_conv_path (CCP_WIN_A_TO_POSIX | CCP_ABSOLUTE, envhome,
239 homedir_psx, PATH_MAX);
240 else
241 psx_dir (envhome, homedir_psx);
242 }
243 else if (envhomepath && envhomepath[0])
244 {
245 if (envhomedrive)
246 strlcpy (homedir_w32, envhomedrive, sizeof (homedir_w32));
247 if (envhomepath[0] != '\\')
248 strlcat (homedir_w32, "\\", sizeof (homedir_w32));
249 strlcat (homedir_w32, envhomepath, sizeof (homedir_w32));
250 if (print_cygpath)
251 cygwin_conv_path (CCP_WIN_A_TO_POSIX | CCP_ABSOLUTE, homedir_w32,
252 homedir_psx, PATH_MAX);
253 else
254 psx_dir (homedir_w32, homedir_psx);
255 }
256 else
257 {
258 wcstombs (stpncpy (homedir_psx, "/home/", sizeof (homedir_psx)),
259 user, sizeof (homedir_psx) - 6);
260 homedir_psx[PATH_MAX - 1] = '\0';
261 }
262 }
263 else
264 {
265 char *p = stpncpy (homedir_psx, passed_home_path, sizeof (homedir_psx));
266 wcstombs (p, user, sizeof (homedir_psx) - (p - homedir_psx));
267 homedir_psx[PATH_MAX - 1] = '\0';
268 }
269
270 printf ("%ls%s%ls:unused:%lu:%lu:U-%ls\\%ls,%s:%s:/bin/bash\n",
271 sep ? dom : L"",
272 sep ?: "",
273 user,
274 id_offset + uid,
275 id_offset + gid,
276 dom,
277 user,
278 put_sid (curr_user.psid),
279 homedir_psx);
280 }
281
282 static void
283 enum_unix_users (domlist_t *dom_or_machine, const char *sep, DWORD id_offset,
284 char *unix_user_list)
285 {
286 WCHAR machine[INTERNET_MAX_HOST_NAME_LENGTH + 1];
287 PWCHAR servername = NULL;
288 char *d_or_m = dom_or_machine ? dom_or_machine->str : NULL;
289 BOOL with_dom = dom_or_machine ? dom_or_machine->with_dom : FALSE;
290 SID_IDENTIFIER_AUTHORITY auth = { { 0, 0, 0, 0, 0, 22 } };
291 char *ustr, *user_list;
292 WCHAR user[UNLEN + sizeof ("Unix User\\") + 1];
293 WCHAR dom[MAX_DOMAIN_NAME_LEN + 1];
294 DWORD ulen, dlen, sidlen;
295 PSID psid;
296 char psid_buffer[MAX_SID_LEN];
297 SID_NAME_USE acc_type;
298
299 if (!d_or_m)
300 return;
301
302 int ret = mbstowcs (machine, d_or_m, INTERNET_MAX_HOST_NAME_LENGTH + 1);
303 if (ret < 1 || ret >= INTERNET_MAX_HOST_NAME_LENGTH + 1)
304 {
305 fprintf (stderr, "%s: Invalid machine name '%s'. Skipping...\n",
306 __progname, d_or_m);
307 return;
308 }
309 servername = machine;
310
311 if (!AllocateAndInitializeSid (&auth, 2, 1, 0, 0, 0, 0, 0, 0, 0, &psid))
312 return;
313
314 if (!(user_list = strdup (unix_user_list)))
315 {
316 FreeSid (psid);
317 return;
318 }
319
320 for (ustr = strtok (user_list, ","); ustr; ustr = strtok (NULL, ","))
321 {
322 if (!isdigit (ustr[0]) && ustr[0] != '-')
323 {
324 PWCHAR p = wcpcpy (user, L"Unix User\\");
325 ret = mbstowcs (p, ustr, UNLEN + 1);
326 if (ret < 1 || ret >= UNLEN + 1)
327 fprintf (stderr, "%s: Invalid user name '%s'. Skipping...\n",
328 __progname, ustr);
329 else if (LookupAccountNameW (servername, user,
330 psid = (PSID) psid_buffer,
331 (sidlen = MAX_SID_LEN, &sidlen),
332 dom,
333 (dlen = MAX_DOMAIN_NAME_LEN + 1, &dlen),
334 &acc_type))
335 printf ("%s%s%ls:unused:%lu:99999:,%s::\n",
336 with_dom ? "Unix User" : "",
337 with_dom ? sep : "",
338 user + 10,
339 id_offset +
340 *GetSidSubAuthority (psid,
341 *GetSidSubAuthorityCount(psid) - 1),
342 put_sid (psid));
343 }
344 else
345 {
346 DWORD start, stop;
347 char *p = ustr;
348 if (*p == '-')
349 start = 0;
350 else
351 start = strtol (p, &p, 10);
352 if (!*p)
353 stop = start;
354 else if (*p++ != '-' || !isdigit (*p)
355 || (stop = strtol (p, &p, 10)) < start || *p)
356 {
357 fprintf (stderr, "%s: Malformed unix user list entry '%s'. "
358 "Skipping...\n", __progname, ustr);
359 continue;
360 }
361 for (; start <= stop; ++ start)
362 {
363 *GetSidSubAuthority (psid, *GetSidSubAuthorityCount(psid) - 1)
364 = start;
365 if (LookupAccountSidW (servername, psid,
366 user, (ulen = GNLEN + 1, &ulen),
367 dom,
368 (dlen = MAX_DOMAIN_NAME_LEN + 1, &dlen),
369 &acc_type)
370 && !iswdigit (user[0]))
371 printf ("%s%s%ls:unused:%lu:99999:,%s::\n",
372 with_dom ? "Unix User" : "",
373 with_dom ? sep : "",
374 user,
375 id_offset + start,
376 put_sid (psid));
377 }
378 }
379 }
380
381 free (user_list);
382 FreeSid (psid);
383 }
384
385 static int
386 enum_users (BOOL domain, domlist_t *dom_or_machine, const char *sep,
387 int print_cygpath, const char *passed_home_path, DWORD id_offset,
388 char *disp_username, int print_current)
389 {
390 WCHAR machine[INTERNET_MAX_HOST_NAME_LENGTH + 1];
391 PWCHAR servername = NULL;
392 char *d_or_m = dom_or_machine ? dom_or_machine->str : NULL;
393 BOOL with_dom = dom_or_machine ? dom_or_machine->with_dom : FALSE;
394 USER_INFO_3 *buffer;
395 DWORD entriesread = 0;
396 DWORD totalentries = 0;
397 DWORD resume_handle = 0;
398 DWORD rc;
399 WCHAR uni_name[UNLEN + 1];
400 if (domain)
401 {
402 servername = get_dcname (d_or_m);
403 if (servername == (PWCHAR) -1)
404 return 1;
405 }
406 else if (d_or_m)
407 {
408 int ret = mbstowcs (machine, d_or_m, INTERNET_MAX_HOST_NAME_LENGTH + 1);
409 if (ret < 1 || ret >= INTERNET_MAX_HOST_NAME_LENGTH + 1)
410 {
411 fprintf (stderr, "%s: Invalid machine name '%s'. Skipping...\n",
412 __progname, d_or_m);
413 return 1;
414 }
415 servername = machine;
416 }
417
418 do
419 {
420 DWORD i;
421
422 if (disp_username != NULL)
423 {
424 mbstowcs (uni_name, disp_username, UNLEN + 1);
425 rc = NetUserGetInfo (servername, (LPWSTR) &uni_name, 3,
426 (void *) &buffer);
427 entriesread = 1;
428 }
429 else
430 rc = NetUserEnum (servername, 3, FILTER_NORMAL_ACCOUNT,
431 (void *) &buffer, MAX_PREFERRED_LENGTH,
432 &entriesread, &totalentries, &resume_handle);
433 switch (rc)
434 {
435 case ERROR_ACCESS_DENIED:
436 print_win_error(rc);
437 return 1;
438
439 case ERROR_MORE_DATA:
440 case ERROR_SUCCESS:
441 break;
442
443 default:
444 print_win_error(rc);
445 return 1;
446 }
447
448 for (i = 0; i < entriesread; i++)
449 {
450 char homedir_psx[PATH_MAX];
451 char homedir_w32[MAX_PATH];
452 WCHAR domain_name[MAX_DOMAIN_NAME_LEN + 1];
453 DWORD domname_len = MAX_DOMAIN_NAME_LEN + 1;
454 char psid_buffer[MAX_SID_LEN];
455 PSID psid = (PSID) psid_buffer;
456 DWORD sid_length = MAX_SID_LEN;
457 SID_NAME_USE acc_type;
458
459 int uid = buffer[i].usri3_user_id;
460 int gid = buffer[i].usri3_primary_group_id;
461 homedir_w32[0] = homedir_psx[0] = '\0';
462 if (passed_home_path[0] == '\0')
463 {
464 uni2ansi (buffer[i].usri3_home_dir, homedir_w32,
465 sizeof (homedir_w32));
466 if (homedir_w32[0] != '\0')
467 {
468 if (print_cygpath)
469 cygwin_conv_path (CCP_WIN_A_TO_POSIX | CCP_ABSOLUTE,
470 homedir_w32, homedir_psx, PATH_MAX);
471 else
472 psx_dir (homedir_w32, homedir_psx);
473 }
474 else
475 uni2ansi (buffer[i].usri3_name,
476 stpcpy (homedir_psx, "/home/"), PATH_MAX - 6);
477 }
478 else
479 uni2ansi (buffer[i].usri3_name,
480 stpcpy (homedir_psx, passed_home_path),
481 PATH_MAX - strlen (passed_home_path));
482
483 if (!LookupAccountNameW (servername, buffer[i].usri3_name,
484 psid, &sid_length, domain_name,
485 &domname_len, &acc_type))
486 {
487 print_win_error(GetLastError ());
488 fprintf(stderr, " (%ls)\n", buffer[i].usri3_name);
489 continue;
490 }
491 else if (acc_type == SidTypeDomain)
492 {
493 WCHAR domname[MAX_DOMAIN_NAME_LEN + UNLEN + 2];
494
495 wcscpy (domname, domain || !servername
496 ? domain_name : servername);
497 wcscat (domname, L"\\");
498 wcscat (domname, buffer[i].usri3_name);
499 sid_length = MAX_SID_LEN;
500 domname_len = sizeof (domname);
501 if (!LookupAccountNameW (servername, domname, psid,
502 &sid_length, domain_name,
503 &domname_len, &acc_type))
504 {
505 print_win_error(GetLastError ());
506 fprintf(stderr, " (%ls)\n", domname);
507 continue;
508 }
509 }
510 if (!print_current)
511 /* fall through */;
512 else if (EqualSid (curr_user.psid, psid))
513 got_curr_user = TRUE;
514 else
515 continue;
516
517 printf ("%ls%s%ls:unused:%lu:%lu:%ls%sU-%ls\\%ls,%s:%s:/bin/bash\n",
518 with_dom ? domain_name : L"",
519 with_dom ? sep : "",
520 buffer[i].usri3_name,
521 id_offset + uid,
522 id_offset + gid,
523 buffer[i].usri3_full_name ?: L"",
524 buffer[i].usri3_full_name
525 && buffer[i].usri3_full_name[0] ? "," : "",
526 domain_name,
527 buffer[i].usri3_name,
528 put_sid (psid),
529 homedir_psx);
530 }
531
532 NetApiBufferFree (buffer);
533
534 }
535 while (rc == ERROR_MORE_DATA);
536
537 return 0;
538 }
539
540 static void
541 print_special (PSID_IDENTIFIER_AUTHORITY auth, BYTE cnt,
542 DWORD sub1, DWORD sub2, DWORD sub3, DWORD sub4,
543 DWORD sub5, DWORD sub6, DWORD sub7, DWORD sub8)
544 {
545 WCHAR user[UNLEN + 1], dom[MAX_DOMAIN_NAME_LEN + 1];
546 DWORD len, len2, rid;
547 PSID sid;
548 SID_NAME_USE acc_type;
549
550 if (AllocateAndInitializeSid (auth, cnt, sub1, sub2, sub3, sub4,
551 sub5, sub6, sub7, sub8, &sid))
552 {
553 if (LookupAccountSidW (NULL, sid,
554 user, (len = UNLEN + 1, &len),
555 dom, (len2 = MAX_DOMAIN_NAME_LEN + 1, &len),
556 &acc_type))
557 {
558 if (sub8)
559 rid = sub8;
560 else if (sub7)
561 rid = sub7;
562 else if (sub6)
563 rid = sub6;
564 else if (sub5)
565 rid = sub5;
566 else if (sub4)
567 rid = sub4;
568 else if (sub3)
569 rid = sub3;
570 else if (sub2)
571 rid = sub2;
572 else
573 rid = sub1;
574 printf ("%ls:*:%lu:%lu:,%s::\n",
575 user, rid, rid == 18 ? 544 : rid, /* SYSTEM hack */
576 put_sid (sid));
577 }
578 FreeSid (sid);
579 }
580 }
581
582 static int
583 usage (FILE * stream)
584 {
585 fprintf (stream,
586 "Usage: mkpasswd [OPTIONS]...\n"
587 "Print /etc/passwd file to stdout\n"
588 "\n"
589 "Options:\n"
590 " -l,--local [machine[,offset]]\n"
591 " print local user accounts with uid offset offset\n"
592 " (from local machine if no machine specified)\n"
593 " -L,--Local [machine[,offset]]\n"
594 " ditto, but generate username with machine prefix\n"
595 " -d,--domain [domain[,offset]]\n"
596 " print domain accounts with uid offset offset\n"
597 " (from current domain if no domain specified)\n"
598 " -D,--Domain [domain[,offset]]\n"
599 " ditto, but generate username with domain prefix\n"
600 " -c,--current print current user\n"
601 " -C,--Current ditto, but generate username with machine or\n"
602 " domain prefix\n"
603 " -S,--separator char for -L, -D, -C use character char as domain\\user\n"
604 " separator in username instead of the default '\\'\n"
605 " -o,--id-offset offset change the default offset (10000) added to uids\n"
606 " in domain or foreign server accounts.\n"
607 " -u,--username username only return information for the specified user\n"
608 " one of -l, -L, -d, -D must be specified, too\n"
609 " -p,--path-to-home path use specified path instead of user account home dir\n"
610 " or /home prefix\n"
611 " -m,--no-mount don't use mount points for home dir\n"
612 " -U,--unix userlist additionally print UNIX users when using -l or -L\n"
613 " on a UNIX Samba server\n"
614 " userlist is a comma-separated list of usernames\n"
615 " or uid ranges (root,-25,50-100).\n"
616 " (enumerating large ranges can take a long time!)\n"
617 " -s,--no-sids (ignored)\n"
618 " -g,--local-groups (ignored)\n"
619 " -h,--help displays this message\n"
620 " -v,--version version information and exit\n"
621 "\n"
622 "Default is to print local accounts on stand-alone machines, domain accounts\n"
623 "on domain controllers and domain member machines.\n");
624 return 1;
625 }
626
627 static struct option longopts[] = {
628 {"current", no_argument, NULL, 'c'},
629 {"Current", no_argument, NULL, 'C'},
630 {"domain", optional_argument, NULL, 'd'},
631 {"Domain", optional_argument, NULL, 'D'},
632 {"local-groups", no_argument, NULL, 'g'},
633 {"help", no_argument, NULL, 'h'},
634 {"local", optional_argument, NULL, 'l'},
635 {"Local", optional_argument, NULL, 'L'},
636 {"no-mount", no_argument, NULL, 'm'},
637 {"id-offset", required_argument, NULL, 'o'},
638 {"path-to-home", required_argument, NULL, 'p'},
639 {"no-sids", no_argument, NULL, 's'},
640 {"separator", required_argument, NULL, 'S'},
641 {"username", required_argument, NULL, 'u'},
642 {"unix", required_argument, NULL, 'U'},
643 {"version", no_argument, NULL, 'v'},
644 {0, no_argument, NULL, 0}
645 };
646
647 static char opts[] = "cCd::D::ghl::L::mo:sS:p:u:U:v";
648
649 static void
650 print_version ()
651 {
652 const char *v = strchr (version, ':');
653 int len;
654 if (!v)
655 {
656 v = "?";
657 len = 1;
658 }
659 else
660 {
661 v += 2;
662 len = strchr (v, ' ') - v;
663 }
664 printf ("\
665 mkpasswd (cygwin) %.*s\n\
666 passwd File Generator\n\
667 Copyright 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2005, 2006, 2008 Red Hat, Inc.\n\
668 Compiled on %s\n\
669 ", len, v, __DATE__);
670 }
671
672 static void
673 enum_std_accounts ()
674 {
675 /* Generate service starter account entries. */
676 printf ("SYSTEM:*:18:544:,S-1-5-18::\n");
677 printf ("LocalService:*:19:544:U-NT AUTHORITY\\LocalService,S-1-5-19::\n");
678 printf ("NetworkService:*:20:544:U-NT AUTHORITY\\NetworkService,S-1-5-20::\n");
679 /* Get 'administrators' group (has localized name). */
680 print_special (&sid_nt_auth, 2, SECURITY_BUILTIN_DOMAIN_RID,
681 DOMAIN_ALIAS_RID_ADMINS, 0, 0, 0, 0, 0, 0);
682 }
683
684 static PPOLICY_PRIMARY_DOMAIN_INFO p_dom;
685
686 static BOOL
687 fetch_primary_domain ()
688 {
689 NTSTATUS status;
690 LSA_OBJECT_ATTRIBUTES oa = { 0, 0, 0, 0, 0, 0 };
691 LSA_HANDLE lsa;
692
693 if (!p_dom)
694 {
695 status = LsaOpenPolicy (NULL, &oa, POLICY_VIEW_LOCAL_INFORMATION, &lsa);
696 if (!NT_SUCCESS (status))
697 return FALSE;
698 status = LsaQueryInformationPolicy (lsa, PolicyPrimaryDomainInformation,
699 (PVOID *) ((void *) &p_dom));
700 LsaClose (lsa);
701 if (!NT_SUCCESS (status))
702 return FALSE;
703 }
704 return !!p_dom->Sid;
705 }
706
707 int
708 main (int argc, char **argv)
709 {
710 int print_domlist = 0;
711 domlist_t domlist[32];
712 char *opt, *p, *ep;
713 int print_cygpath = 1;
714 int print_current = 0;
715 char *print_unix = NULL;
716 const char *sep_char = "\\";
717 DWORD id_offset = 10000, off;
718 int c, i;
719 char *disp_username = NULL;
720 char passed_home_path[PATH_MAX];
721 BOOL in_domain;
722 int optional_args = 0;
723
724 passed_home_path[0] = '\0';
725 if (!isatty (1))
726 setmode (1, O_BINARY);
727
728 load_dsgetdcname ();
729 in_domain = fetch_primary_domain ();
730 fetch_current_user_sid ();
731
732 if (argc == 1)
733 {
734 enum_std_accounts ();
735 if (in_domain)
736 enum_users (TRUE, NULL, sep_char, print_cygpath, passed_home_path,
737 10000, disp_username, 0);
738 else
739 enum_users (FALSE, NULL, sep_char, print_cygpath, passed_home_path, 0,
740 disp_username, 0);
741 return 0;
742 }
743
744 unsetenv ("POSIXLY_CORRECT"); /* To get optional arg processing right. */
745 while ((c = getopt_long (argc, argv, opts, longopts, NULL)) != EOF)
746 switch (c)
747 {
748 case 'd':
749 case 'D':
750 case 'l':
751 case 'L':
752 if (print_domlist >= 32)
753 {
754 fprintf (stderr, "%s: Can not enumerate from more than 32 "
755 "domains and machines.\n", __progname);
756 return 1;
757 }
758 domlist[print_domlist].domain = (c == 'd' || c == 'D');
759 opt = optarg ?:
760 argv[optind] && argv[optind][0] != '-' ? argv[optind] : NULL;
761 if (argv[optind] && opt == argv[optind])
762 ++optional_args;
763 for (i = 0; i < print_domlist; ++i)
764 if (domlist[i].domain == domlist[print_domlist].domain
765 && ((!domlist[i].str && !opt)
766 || (domlist[i].str && opt
767 && (off = strlen (domlist[i].str))
768 && !strncmp (domlist[i].str, opt, off)
769 && (!opt[off] || opt[off] == ','))))
770 {
771 fprintf (stderr, "%s: Duplicate %s '%s'. Skipping...\n",
772 __progname, domlist[i].domain ? "domain" : "machine",
773 domlist[i].str);
774 goto skip;
775 }
776 domlist[print_domlist].str = opt;
777 domlist[print_domlist].id_offset = ULONG_MAX;
778 if (opt && (p = strchr (opt, ',')))
779 {
780 if (p == opt
781 || !isdigit (p[1])
782 || (domlist[print_domlist].id_offset = strtol (p + 1, &ep, 10)
783 , *ep))
784 {
785 fprintf (stderr, "%s: Malformed domain,offset string '%s'. "
786 "Skipping...\n", __progname, opt);
787 break;
788 }
789 *p = '\0';
790 }
791 domlist[print_domlist++].with_dom = (c == 'D' || c == 'L');
792 skip:
793 break;
794 case 'S':
795 sep_char = optarg;
796 if (strlen (sep_char) > 1)
797 {
798 fprintf (stderr, "%s: Only one character allowed as domain\\user "
799 "separator character.\n", __progname);
800 return 1;
801 }
802 if (*sep_char == ':')
803 {
804 fprintf (stderr, "%s: Colon not allowed as domain\\user separator "
805 "character.\n", __progname);
806 return 1;
807 }
808 break;
809 case 'U':
810 print_unix = optarg;
811 break;
812 case 'c':
813 sep_char = NULL;
814 /*FALLTHRU*/
815 case 'C':
816 print_current = 1;
817 break;
818 case 'o':
819 id_offset = strtoul (optarg, &ep, 10);
820 if (*ep)
821 {
822 fprintf (stderr, "%s: Malformed offset '%s'. "
823 "Skipping...\n", __progname, optarg);
824 return 1;
825 }
826 break;
827 case 'g':
828 break;
829 case 's':
830 break;
831 case 'm':
832 print_cygpath = 0;
833 break;
834 case 'p':
835 if (optarg[0] != '/')
836 {
837 fprintf (stderr, "%s: '%s' is not a fully qualified path.\n",
838 __progname, optarg);
839 return 1;
840 }
841 strcpy (passed_home_path, optarg);
842 if (optarg[strlen (optarg)-1] != '/')
843 strcat (passed_home_path, "/");
844 break;
845 case 'u':
846 disp_username = optarg;
847 break;
848 case 'h':
849 usage (stdout);
850 return 0;
851 case 'v':
852 print_version ();
853 return 0;
854 default:
855 fprintf (stderr, "Try '%s --help' for more information.\n", __progname);
856 return 1;
857 }
858
859 optind += optional_args;
860 if (argv[optind])
861 {
862 fprintf (stderr,
863 "mkpasswd: non-option command line argument `%s' is not allowed.\n"
864 "Try `mkpasswd --help' for more information.\n", argv[optind]);
865 exit (1);
866 }
867
868 off = id_offset;
869 for (i = 0; i < print_domlist; ++i)
870 {
871 DWORD my_off = (domlist[i].domain || domlist[i].str)
872 ? domlist[i].id_offset != ULONG_MAX
873 ? domlist[i].id_offset : off : 0;
874 if (!domlist[i].domain && domlist[i].str && print_unix)
875 enum_unix_users (domlist + i, sep_char, my_off, print_unix);
876 if (!my_off && !print_current && !disp_username)
877 enum_std_accounts ();
878 enum_users (domlist[i].domain, domlist + i, sep_char, print_cygpath,
879 passed_home_path, my_off, disp_username, print_current);
880 if (my_off)
881 off += id_offset;
882 }
883
884 if (print_current && !got_curr_user)
885 current_user (print_cygpath, sep_char, passed_home_path, off,
886 disp_username);
887
888 return 0;
889 }
This page took 0.079094 seconds and 6 git commands to generate.