]> sourceware.org Git - newlib-cygwin.git/blob - winsup/utils/mkpasswd.c
* mkgroup.c (fetch_current_pgrp_sid): New function to fetch primary
[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 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 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 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 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 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 sidbuf curr_user;
184 sidbuf curr_pgrp;
185 BOOL got_curr_user = FALSE;
186
187 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 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 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 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)
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
401 if (domain)
402 {
403 servername = get_dcname (d_or_m);
404 if (servername == (PWCHAR) -1)
405 return 1;
406 }
407 else if (d_or_m)
408 {
409 int ret = mbstowcs (machine, d_or_m, INTERNET_MAX_HOST_NAME_LENGTH + 1);
410 if (ret < 1 || ret >= INTERNET_MAX_HOST_NAME_LENGTH + 1)
411 {
412 fprintf (stderr, "%s: Invalid machine name '%s'. Skipping...\n",
413 __progname, d_or_m);
414 return 1;
415 }
416 servername = machine;
417 }
418
419 do
420 {
421 DWORD i;
422
423 if (disp_username != NULL)
424 {
425 mbstowcs (uni_name, disp_username, UNLEN + 1);
426 rc = NetUserGetInfo (servername, (LPWSTR) &uni_name, 3,
427 (void *) &buffer);
428 entriesread = 1;
429 }
430 else
431 rc = NetUserEnum (servername, 3, FILTER_NORMAL_ACCOUNT,
432 (void *) &buffer, MAX_PREFERRED_LENGTH,
433 &entriesread, &totalentries, &resume_handle);
434 switch (rc)
435 {
436 case ERROR_ACCESS_DENIED:
437 print_win_error(rc);
438 return 1;
439
440 case ERROR_MORE_DATA:
441 case ERROR_SUCCESS:
442 break;
443
444 default:
445 print_win_error(rc);
446 return 1;
447 }
448
449 for (i = 0; i < entriesread; i++)
450 {
451 char homedir_psx[PATH_MAX];
452 char homedir_w32[MAX_PATH];
453 WCHAR domain_name[MAX_DOMAIN_NAME_LEN + 1];
454 DWORD domname_len = MAX_DOMAIN_NAME_LEN + 1;
455 char psid_buffer[MAX_SID_LEN];
456 PSID psid = (PSID) psid_buffer;
457 DWORD sid_length = MAX_SID_LEN;
458 SID_NAME_USE acc_type;
459
460 int uid = buffer[i].usri3_user_id;
461 int gid = buffer[i].usri3_primary_group_id;
462 homedir_w32[0] = homedir_psx[0] = '\0';
463 if (passed_home_path[0] == '\0')
464 {
465 uni2ansi (buffer[i].usri3_home_dir, homedir_w32,
466 sizeof (homedir_w32));
467 if (homedir_w32[0] != '\0')
468 {
469 if (print_cygpath)
470 cygwin_conv_path (CCP_WIN_A_TO_POSIX | CCP_ABSOLUTE,
471 homedir_w32, homedir_psx, PATH_MAX);
472 else
473 psx_dir (homedir_w32, homedir_psx);
474 }
475 else
476 uni2ansi (buffer[i].usri3_name,
477 stpcpy (homedir_psx, "/home/"), PATH_MAX - 6);
478 }
479 else
480 uni2ansi (buffer[i].usri3_name,
481 stpcpy (homedir_psx, passed_home_path),
482 PATH_MAX - strlen (passed_home_path));
483
484 if (!LookupAccountNameW (servername, buffer[i].usri3_name,
485 psid, &sid_length, domain_name,
486 &domname_len, &acc_type))
487 {
488 print_win_error(GetLastError ());
489 fprintf(stderr, " (%ls)\n", buffer[i].usri3_name);
490 continue;
491 }
492 else if (acc_type == SidTypeDomain)
493 {
494 WCHAR domname[MAX_DOMAIN_NAME_LEN + UNLEN + 2];
495
496 wcscpy (domname, domain || !servername
497 ? domain_name : servername);
498 wcscat (domname, L"\\");
499 wcscat (domname, buffer[i].usri3_name);
500 sid_length = MAX_SID_LEN;
501 domname_len = sizeof (domname);
502 if (!LookupAccountNameW (servername, domname, psid,
503 &sid_length, domain_name,
504 &domname_len, &acc_type))
505 {
506 print_win_error(GetLastError ());
507 fprintf(stderr, " (%ls)\n", domname);
508 continue;
509 }
510 }
511 if (EqualSid (curr_user.psid, psid))
512 got_curr_user = TRUE;
513 printf ("%ls%s%ls:unused:%lu:%lu:%ls%sU-%ls\\%ls,%s:%s:/bin/bash\n",
514 with_dom ? domain_name : L"",
515 with_dom ? sep : "",
516 buffer[i].usri3_name,
517 id_offset + uid,
518 id_offset + gid,
519 buffer[i].usri3_full_name ?: L"",
520 buffer[i].usri3_full_name
521 && buffer[i].usri3_full_name[0] ? "," : "",
522 domain_name,
523 buffer[i].usri3_name,
524 put_sid (psid),
525 homedir_psx);
526 }
527
528 NetApiBufferFree (buffer);
529
530 }
531 while (rc == ERROR_MORE_DATA);
532
533 return 0;
534 }
535
536 void
537 print_special (PSID_IDENTIFIER_AUTHORITY auth, BYTE cnt,
538 DWORD sub1, DWORD sub2, DWORD sub3, DWORD sub4,
539 DWORD sub5, DWORD sub6, DWORD sub7, DWORD sub8)
540 {
541 WCHAR user[UNLEN + 1], dom[MAX_DOMAIN_NAME_LEN + 1];
542 DWORD len, len2, rid;
543 PSID sid;
544 SID_NAME_USE acc_type;
545
546 if (AllocateAndInitializeSid (auth, cnt, sub1, sub2, sub3, sub4,
547 sub5, sub6, sub7, sub8, &sid))
548 {
549 if (LookupAccountSidW (NULL, sid,
550 user, (len = UNLEN + 1, &len),
551 dom, (len2 = MAX_DOMAIN_NAME_LEN + 1, &len),
552 &acc_type))
553 {
554 if (sub8)
555 rid = sub8;
556 else if (sub7)
557 rid = sub7;
558 else if (sub6)
559 rid = sub6;
560 else if (sub5)
561 rid = sub5;
562 else if (sub4)
563 rid = sub4;
564 else if (sub3)
565 rid = sub3;
566 else if (sub2)
567 rid = sub2;
568 else
569 rid = sub1;
570 printf ("%ls:*:%lu:%lu:,%s::\n",
571 user, rid, rid == 18 ? 544 : rid, /* SYSTEM hack */
572 put_sid (sid));
573 }
574 FreeSid (sid);
575 }
576 }
577
578 int
579 usage (FILE * stream)
580 {
581 fprintf (stream,
582 "Usage: mkpasswd [OPTIONS]...\n"
583 "Print /etc/passwd file to stdout\n"
584 "\n"
585 "Options:\n"
586 " -l,--local [machine[,offset]]\n"
587 " print local user accounts with uid offset offset\n"
588 " (from local machine if no machine specified)\n"
589 " -L,--Local [machine[,offset]]\n"
590 " ditto, but generate username with machine prefix\n"
591 " -d,--domain [domain[,offset]]\n"
592 " print domain accounts with uid offset offset\n"
593 " (from current domain if no domain specified)\n"
594 " -D,--Domain [domain[,offset]]\n"
595 " ditto, but generate username with domain prefix\n"
596 " -c,--current print current user\n"
597 " -C,--Current ditto, but generate username with machine or\n"
598 " domain prefix\n"
599 " -S,--separator char for -L, -D, -C use character char as domain\\user\n"
600 " separator in username instead of the default '\\'\n"
601 " -o,--id-offset offset change the default offset (10000) added to uids\n"
602 " in domain or foreign server accounts.\n"
603 " -u,--username username only return information for the specified user\n"
604 " one of -l, -L, -d, -D must be specified, too\n"
605 " -p,--path-to-home path use specified path instead of user account home dir\n"
606 " or /home prefix\n"
607 " -m,--no-mount don't use mount points for home dir\n"
608 " -U,--unix userlist additionally print UNIX users when using -l or -L\n"
609 " on a UNIX Samba server\n"
610 " userlist is a comma-separated list of usernames\n"
611 " or uid ranges (root,-25,50-100).\n"
612 " (enumerating large ranges can take a long time!)\n"
613 " -s,--no-sids (ignored)\n"
614 " -g,--local-groups (ignored)\n"
615 " -h,--help displays this message\n"
616 " -v,--version version information and exit\n"
617 "\n"
618 "Default is to print local accounts on stand-alone machines, domain accounts\n"
619 "on domain controllers and domain member machines.\n");
620 return 1;
621 }
622
623 struct option longopts[] = {
624 {"current", no_argument, NULL, 'c'},
625 {"Current", no_argument, NULL, 'C'},
626 {"domain", optional_argument, NULL, 'd'},
627 {"Domain", optional_argument, NULL, 'D'},
628 {"local-groups", no_argument, NULL, 'g'},
629 {"help", no_argument, NULL, 'h'},
630 {"local", optional_argument, NULL, 'l'},
631 {"Local", optional_argument, NULL, 'L'},
632 {"no-mount", no_argument, NULL, 'm'},
633 {"id-offset", required_argument, NULL, 'o'},
634 {"path-to-home", required_argument, NULL, 'p'},
635 {"no-sids", no_argument, NULL, 's'},
636 {"separator", required_argument, NULL, 'S'},
637 {"username", required_argument, NULL, 'u'},
638 {"unix", required_argument, NULL, 'U'},
639 {"version", no_argument, NULL, 'v'},
640 {0, no_argument, NULL, 0}
641 };
642
643 char opts[] = "cCd::D::ghl::L::mo:sS:p:u:U:v";
644
645 static void
646 print_version ()
647 {
648 const char *v = strchr (version, ':');
649 int len;
650 if (!v)
651 {
652 v = "?";
653 len = 1;
654 }
655 else
656 {
657 v += 2;
658 len = strchr (v, ' ') - v;
659 }
660 printf ("\
661 mkpasswd (cygwin) %.*s\n\
662 passwd File Generator\n\
663 Copyright 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2005, 2006, 2008 Red Hat, Inc.\n\
664 Compiled on %s\n\
665 ", len, v, __DATE__);
666 }
667
668 static void
669 enum_std_accounts ()
670 {
671 /* Generate service starter account entries. */
672 printf ("SYSTEM:*:18:544:,S-1-5-18::\n");
673 printf ("LocalService:*:19:544:U-NT AUTHORITY\\LocalService,S-1-5-19::\n");
674 printf ("NetworkService:*:20:544:U-NT AUTHORITY\\NetworkService,S-1-5-20::\n");
675 /* Get 'administrators' group (has localized name). */
676 print_special (&sid_nt_auth, 2, SECURITY_BUILTIN_DOMAIN_RID,
677 DOMAIN_ALIAS_RID_ADMINS, 0, 0, 0, 0, 0, 0);
678 }
679
680 static PPOLICY_PRIMARY_DOMAIN_INFO p_dom;
681
682 static BOOL
683 fetch_primary_domain ()
684 {
685 NTSTATUS status;
686 LSA_OBJECT_ATTRIBUTES oa = { 0, 0, 0, 0, 0, 0 };
687 LSA_HANDLE lsa;
688
689 if (!p_dom)
690 {
691 status = LsaOpenPolicy (NULL, &oa, POLICY_VIEW_LOCAL_INFORMATION, &lsa);
692 if (!NT_SUCCESS (status))
693 return FALSE;
694 status = LsaQueryInformationPolicy (lsa, PolicyPrimaryDomainInformation,
695 (PVOID *) &p_dom);
696 LsaClose (lsa);
697 if (!NT_SUCCESS (status))
698 return FALSE;
699 }
700 return !!p_dom->Sid;
701 }
702
703 int
704 main (int argc, char **argv)
705 {
706 int print_domlist = 0;
707 domlist_t domlist[32];
708 char *opt, *p, *ep;
709 int print_cygpath = 1;
710 int print_current = 0;
711 char *print_unix = NULL;
712 const char *sep_char = "\\";
713 DWORD id_offset = 10000, off;
714 int c, i;
715 char *disp_username = NULL;
716 char passed_home_path[PATH_MAX];
717 BOOL in_domain;
718
719 passed_home_path[0] = '\0';
720 if (!isatty (1))
721 setmode (1, O_BINARY);
722
723 load_dsgetdcname ();
724 in_domain = fetch_primary_domain ();
725 if (argc == 1)
726 {
727 enum_std_accounts ();
728 if (in_domain)
729 enum_users (TRUE, NULL, sep_char, print_cygpath, passed_home_path,
730 10000, disp_username);
731 else
732 enum_users (FALSE, NULL, sep_char, print_cygpath, passed_home_path, 0,
733 disp_username);
734 return 0;
735 }
736
737 while ((c = getopt_long (argc, argv, opts, longopts, NULL)) != EOF)
738 switch (c)
739 {
740 case 'd':
741 case 'D':
742 case 'l':
743 case 'L':
744 if (print_domlist >= 32)
745 {
746 fprintf (stderr, "%s: Can not enumerate from more than 32 "
747 "domains and machines.\n", __progname);
748 return 1;
749 }
750 domlist[print_domlist].domain = (c == 'd' || c == 'D');
751 opt = optarg ?:
752 argv[optind] && argv[optind][0] != '-' ? argv[optind] : NULL;
753 for (i = 0; i < print_domlist; ++i)
754 if (domlist[i].domain == domlist[print_domlist].domain
755 && ((!domlist[i].str && !opt)
756 || (domlist[i].str && opt
757 && (off = strlen (domlist[i].str))
758 && !strncmp (domlist[i].str, opt, off)
759 && (!opt[off] || opt[off] == ','))))
760 {
761 fprintf (stderr, "%s: Duplicate %s '%s'. Skipping...\n",
762 __progname, domlist[i].domain ? "domain" : "machine",
763 domlist[i].str);
764 goto skip;
765 }
766 domlist[print_domlist].str = opt;
767 domlist[print_domlist].id_offset = ULONG_MAX;
768 if (opt && (p = strchr (opt, ',')))
769 {
770 if (p == opt
771 || !isdigit (p[1])
772 || (domlist[print_domlist].id_offset = strtol (p + 1, &ep, 10)
773 , *ep))
774 {
775 fprintf (stderr, "%s: Malformed domain,offset string '%s'. "
776 "Skipping...\n", __progname, opt);
777 break;
778 }
779 *p = '\0';
780 }
781 domlist[print_domlist++].with_dom = (c == 'D' || c == 'L');
782 skip:
783 break;
784 case 'S':
785 sep_char = optarg;
786 if (strlen (sep_char) > 1)
787 {
788 fprintf (stderr, "%s: Only one character allowed as domain\\user "
789 "separator character.\n", __progname);
790 return 1;
791 }
792 if (*sep_char == ':')
793 {
794 fprintf (stderr, "%s: Colon not allowed as domain\\user separator "
795 "character.\n", __progname);
796 return 1;
797 }
798 break;
799 case 'U':
800 print_unix = optarg;
801 break;
802 case 'c':
803 sep_char = NULL;
804 /*FALLTHRU*/
805 case 'C':
806 print_current = 1;
807 break;
808 case 'o':
809 id_offset = strtoul (optarg, &ep, 10);
810 if (*ep)
811 {
812 fprintf (stderr, "%s: Malformed offset '%s'. "
813 "Skipping...\n", __progname, optarg);
814 return 1;
815 }
816 break;
817 case 'g':
818 break;
819 case 's':
820 break;
821 case 'm':
822 print_cygpath = 0;
823 break;
824 case 'p':
825 if (optarg[0] != '/')
826 {
827 fprintf (stderr, "%s: '%s' is not a fully qualified path.\n",
828 __progname, optarg);
829 return 1;
830 }
831 strcpy (passed_home_path, optarg);
832 if (optarg[strlen (optarg)-1] != '/')
833 strcat (passed_home_path, "/");
834 break;
835 case 'u':
836 disp_username = optarg;
837 break;
838 case 'h':
839 usage (stdout);
840 return 0;
841 case 'v':
842 print_version ();
843 return 0;
844 default:
845 fprintf (stderr, "Try '%s --help' for more information.\n", __progname);
846 return 1;
847 }
848
849 fetch_current_user_sid ();
850
851 off = id_offset;
852 for (i = 0; i < print_domlist; ++i)
853 {
854 DWORD my_off = (domlist[i].domain || domlist[i].str)
855 ? domlist[i].id_offset != ULONG_MAX
856 ? domlist[i].id_offset : off : 0;
857 if (!domlist[i].domain && domlist[i].str && print_unix)
858 enum_unix_users (domlist + i, sep_char, my_off, print_unix);
859 if (!my_off)
860 enum_std_accounts ();
861 enum_users (domlist[i].domain, domlist + i, sep_char, print_cygpath,
862 passed_home_path, my_off, disp_username);
863 if (my_off)
864 off += id_offset;
865 }
866
867 if (print_current && !got_curr_user)
868 current_user (print_cygpath, sep_char, passed_home_path, off,
869 disp_username);
870
871 return 0;
872 }
This page took 0.078303 seconds and 6 git commands to generate.