]> sourceware.org Git - newlib-cygwin.git/blob - winsup/utils/mkpasswd.c
* mkpasswd.cc (enum_users): Shorten "unused" passwd field.
[newlib-cygwin.git] / winsup / utils / mkpasswd.c
1 /* mkpasswd.c:
2
3 Copyright 1997, 1998, 1999, 2000 Cygnus Solutions.
4
5 This file is part of Cygwin.
6
7 This software is a copyrighted work licensed under the terms of the
8 Cygwin license. Please consult the file "CYGWIN_LICENSE" for
9 details. */
10
11 #include <ctype.h>
12 #include <stdlib.h>
13 #include <wchar.h>
14 #include <stdio.h>
15 #include <windows.h>
16 #include <sys/cygwin.h>
17 #include <getopt.h>
18 #include <lmaccess.h>
19 #include <lmapibuf.h>
20 #include <sys/fcntl.h>
21
22 SID_IDENTIFIER_AUTHORITY sid_world_auth = {SECURITY_WORLD_SID_AUTHORITY};
23 SID_IDENTIFIER_AUTHORITY sid_nt_auth = {SECURITY_NT_AUTHORITY};
24
25 NET_API_STATUS WINAPI (*netapibufferfree)(PVOID);
26 NET_API_STATUS WINAPI (*netuserenum)(LPWSTR,DWORD,DWORD,PBYTE*,DWORD,PDWORD,PDWORD,PDWORD);
27 NET_API_STATUS WINAPI (*netlocalgroupenum)(LPWSTR,DWORD,PBYTE*,DWORD,PDWORD,PDWORD,PDWORD);
28 NET_API_STATUS WINAPI (*netgetdcname)(LPWSTR,LPWSTR,PBYTE*);
29
30 #ifndef min
31 #define min(a,b) (((a)<(b))?(a):(b))
32 #endif
33
34 BOOL
35 load_netapi ()
36 {
37 HANDLE h = LoadLibrary ("netapi32.dll");
38
39 if (!h)
40 return FALSE;
41
42 if (!(netapibufferfree = GetProcAddress (h, "NetApiBufferFree")))
43 return FALSE;
44 if (!(netuserenum = GetProcAddress (h, "NetUserEnum")))
45 return FALSE;
46 if (!(netlocalgroupenum = GetProcAddress (h, "NetLocalGroupEnum")))
47 return FALSE;
48 if (!(netgetdcname = GetProcAddress (h, "NetGetDCName")))
49 return FALSE;
50
51 return TRUE;
52 }
53
54 char *
55 put_sid (PSID sid)
56 {
57 static char s[512];
58 char t[32];
59 DWORD i;
60
61 strcpy (s, "S-1-");
62 sprintf(t, "%u", GetSidIdentifierAuthority (sid)->Value[5]);
63 strcat (s, t);
64 for (i = 0; i < *GetSidSubAuthorityCount (sid); ++i)
65 {
66 sprintf(t, "-%lu", *GetSidSubAuthority (sid, i));
67 strcat (s, t);
68 }
69 return s;
70 }
71
72 void
73 psx_dir (char *in, char *out)
74 {
75 if (isalpha (in[0]) && in[1] == ':')
76 {
77 sprintf (out, "/cygdrive/%c", in[0]);
78 in += 2;
79 out += strlen (out);
80 }
81
82 while (*in)
83 {
84 if (*in == '\\')
85 *out = '/';
86 else
87 *out = *in;
88 in++;
89 out++;
90 }
91
92 *out = '\0';
93 }
94
95 void
96 uni2ansi (LPWSTR wcs, char *mbs, int size)
97 {
98 if (wcs)
99 WideCharToMultiByte (CP_ACP, 0, wcs, -1, mbs, size, NULL, NULL);
100 else
101 *mbs = '\0';
102 }
103
104 int
105 enum_users (LPWSTR servername, int print_sids, int print_cygpath,
106 const char * passed_home_path, int id_offset)
107 {
108 USER_INFO_3 *buffer;
109 DWORD entriesread = 0;
110 DWORD totalentries = 0;
111 DWORD resume_handle = 0;
112 DWORD rc;
113 char ansi_srvname[256];
114
115 if (servername)
116 uni2ansi (servername, ansi_srvname, sizeof (ansi_srvname));
117
118 do
119 {
120 DWORD i;
121
122 rc = netuserenum (servername, 3, FILTER_NORMAL_ACCOUNT,
123 (LPBYTE *) & buffer, 1024,
124 &entriesread, &totalentries, &resume_handle);
125 switch (rc)
126 {
127 case ERROR_ACCESS_DENIED:
128 fprintf (stderr, "Access denied\n");
129 exit (1);
130
131 case ERROR_MORE_DATA:
132 case ERROR_SUCCESS:
133 break;
134
135 default:
136 fprintf (stderr, "NetUserEnum() failed with %ld\n", rc);
137 exit (1);
138 }
139
140 for (i = 0; i < entriesread; i++)
141 {
142 char username[100];
143 char fullname[100];
144 char homedir_psx[MAX_PATH];
145 char homedir_w32[MAX_PATH];
146 char domain_name[100];
147 DWORD domname_len = 100;
148 char psid_buffer[1024];
149 PSID psid = (PSID) psid_buffer;
150 DWORD sid_length = 1024;
151 SID_NAME_USE acc_type;
152
153 int uid = buffer[i].usri3_user_id;
154 int gid = buffer[i].usri3_primary_group_id;
155 uni2ansi (buffer[i].usri3_name, username, sizeof (username));
156 uni2ansi (buffer[i].usri3_full_name, fullname, sizeof (fullname));
157 homedir_w32[0] = homedir_psx[0] = '\0';
158 uni2ansi (buffer[i].usri3_home_dir, homedir_w32, sizeof (homedir_w32));
159 if (print_cygpath)
160 cygwin_conv_to_posix_path (homedir_w32, homedir_psx);
161 else
162 psx_dir (homedir_w32, homedir_psx);
163
164 if (homedir_psx[0] == '\0')
165 {
166 strcat (homedir_psx, passed_home_path);
167 strcat (homedir_psx, username);
168 }
169
170 if (print_sids)
171 {
172 if (!LookupAccountName (servername ? ansi_srvname : NULL,
173 username,
174 psid, &sid_length,
175 domain_name, &domname_len,
176 &acc_type))
177 {
178 fprintf (stderr,
179 "LookupAccountName(%s,%s) failed with error %ld\n",
180 servername ? ansi_srvname : "NULL",
181 username,
182 GetLastError ());
183 continue;
184 }
185 else if (acc_type == SidTypeDomain)
186 {
187 char domname[356];
188
189 strcpy (domname, domain_name);
190 strcat (domname, "\\");
191 strcat (domname, username);
192 sid_length = 1024;
193 domname_len = 100;
194 if (!LookupAccountName (servername ? ansi_srvname : NULL,
195 domname,
196 psid, &sid_length,
197 domain_name, &domname_len,
198 &acc_type))
199 {
200 fprintf (stderr,
201 "LookupAccountName(%s,%s) failed with error %ld\n",
202 servername ? ansi_srvname : "NULL",
203 domname,
204 GetLastError ());
205 continue;
206 }
207 }
208 }
209 printf ("%s:unused_by_nt/2000/xp:%d:%d:%s%s%s:%s:/bin/bash\n", username,
210 uid + id_offset,
211 gid + id_offset,
212 fullname,
213 print_sids ? "," : "",
214 print_sids ? put_sid (psid) : "",
215 homedir_psx);
216 }
217
218 netapibufferfree (buffer);
219
220 }
221 while (rc == ERROR_MORE_DATA);
222
223 if (servername)
224 netapibufferfree (servername);
225
226 return 0;
227 }
228
229 int
230 enum_local_groups (int print_sids)
231 {
232 LOCALGROUP_INFO_0 *buffer;
233 DWORD entriesread = 0;
234 DWORD totalentries = 0;
235 DWORD resume_handle = 0;
236 DWORD rc ;
237
238 do
239 {
240 DWORD i;
241
242 rc = netlocalgroupenum (NULL, 0, (LPBYTE *) & buffer, 1024,
243 &entriesread, &totalentries, &resume_handle);
244 switch (rc)
245 {
246 case ERROR_ACCESS_DENIED:
247 fprintf (stderr, "Access denied\n");
248 exit (1);
249
250 case ERROR_MORE_DATA:
251 case ERROR_SUCCESS:
252 break;
253
254 default:
255 fprintf (stderr, "NetLocalGroupEnum() failed with %ld\n", rc);
256 exit (1);
257 }
258
259 for (i = 0; i < entriesread; i++)
260 {
261 char localgroup_name[100];
262 char domain_name[100];
263 DWORD domname_len = 100;
264 char psid_buffer[1024];
265 PSID psid = (PSID) psid_buffer;
266 DWORD sid_length = 1024;
267 DWORD gid;
268 SID_NAME_USE acc_type;
269 uni2ansi (buffer[i].lgrpi0_name, localgroup_name, sizeof (localgroup_name));
270
271 if (!LookupAccountName (NULL, localgroup_name, psid,
272 &sid_length, domain_name, &domname_len,
273 &acc_type))
274 {
275 fprintf (stderr, "LookupAccountName(%s) failed with %ld\n",
276 localgroup_name, GetLastError ());
277 continue;
278 }
279 else if (acc_type == SidTypeDomain)
280 {
281 char domname[356];
282
283 strcpy (domname, domain_name);
284 strcat (domname, "\\");
285 strcat (domname, localgroup_name);
286 sid_length = 1024;
287 domname_len = 100;
288 if (!LookupAccountName (NULL, domname,
289 psid, &sid_length,
290 domain_name, &domname_len,
291 &acc_type))
292 {
293 fprintf (stderr,
294 "LookupAccountName(%s) failed with error %ld\n",
295 localgroup_name, GetLastError ());
296 continue;
297 }
298 }
299
300 gid = *GetSidSubAuthority (psid, *GetSidSubAuthorityCount(psid) - 1);
301
302 printf ("%s:*:%ld:%ld:%s%s::\n", localgroup_name, gid, gid,
303 print_sids ? "," : "",
304 print_sids ? put_sid (psid) : "");
305 }
306
307 netapibufferfree (buffer);
308
309 }
310 while (rc == ERROR_MORE_DATA);
311
312 return 0;
313 }
314
315 int
316 usage ()
317 {
318 fprintf (stderr, "Usage: mkpasswd [OPTION]... [domain]\n\n");
319 fprintf (stderr, "This program prints a /etc/passwd file to stdout\n\n");
320 fprintf (stderr, "Options:\n");
321 fprintf (stderr, " -l,--local print local user accounts\n");
322 fprintf (stderr, " -d,--domain print domain accounts (from current domain\n");
323 fprintf (stderr, " if no domain specified)\n");
324 fprintf (stderr, " -o,--id-offset offset change the default offset (10000) added to uids\n");
325 fprintf (stderr, " in domain accounts.\n");
326 fprintf (stderr, " -g,--local-groups print local group information too\n");
327 fprintf (stderr, " if no domain specified\n");
328 fprintf (stderr, " -m,--no-mount don't use mount points for home dir\n");
329 fprintf (stderr, " -s,--no-sids don't print SIDs in GCOS field\n");
330 fprintf (stderr, " (this affects ntsec)\n");
331 fprintf (stderr, " -p,--path-to-home path if user account has no home dir, use\n");
332 fprintf (stderr, " path instead of /home/\n");
333 fprintf (stderr, " -?,--help displays this message\n\n");
334 fprintf (stderr, "One of `-l', `-d' or `-g' must be given on NT/W2K.\n");
335 return 1;
336 }
337
338 struct option longopts[] = {
339 {"local", no_argument, NULL, 'l'},
340 {"domain", no_argument, NULL, 'd'},
341 {"id-offset", required_argument, NULL, 'o'},
342 {"local-groups", no_argument, NULL, 'g'},
343 {"no-mount", no_argument, NULL, 'm'},
344 {"no-sids", no_argument, NULL, 's'},
345 {"path-to-home",required_argument, NULL, 'p'},
346 {"help", no_argument, NULL, 'h'},
347 {0, no_argument, NULL, 0}
348 };
349
350 char opts[] = "ldo:gsmhp:";
351
352 int
353 main (int argc, char **argv)
354 {
355 LPWSTR servername = NULL;
356 DWORD rc = ERROR_SUCCESS;
357 WCHAR domain_name[200];
358 int print_local = 0;
359 int print_domain = 0;
360 int print_local_groups = 0;
361 int domain_name_specified = 0;
362 int print_sids = 1;
363 int print_cygpath = 1;
364 int id_offset = 10000;
365 int i;
366
367 char name[256], dom[256], passed_home_path[MAX_PATH];
368 DWORD len, len2;
369 PSID sid;
370 SID_NAME_USE use;
371
372 passed_home_path[0] = '\0';
373 setmode (1, O_BINARY);
374
375 if (GetVersion () < 0x80000000)
376 if (argc == 1)
377 return usage ();
378 else
379 {
380 while ((i = getopt_long (argc, argv, opts, longopts, NULL)) != EOF)
381 switch (i)
382 {
383 case 'l':
384 print_local = 1;
385 break;
386 case 'd':
387 print_domain = 1;
388 break;
389 case 'o':
390 id_offset = strtol (optarg, NULL, 10);
391 break;
392 case 'g':
393 print_local_groups = 1;
394 break;
395 case 's':
396 print_sids = 0;
397 break;
398 case 'm':
399 print_cygpath = 0;
400 break;
401 case 'p':
402 if (optarg[0] != '/')
403 {
404 fprintf (stderr, "%s: `%s' is not a fully qualified path.\n",
405 argv[0], optarg);
406 return 1;
407 }
408 strcpy (passed_home_path, optarg);
409 if (optarg[strlen (optarg)-1] != '/')
410 strcat (passed_home_path, "/");
411 break;
412 case 'h':
413 return usage ();
414 default:
415 fprintf (stderr, "Try `%s --help' for more information.\n", argv[0]);
416 return 1;
417 }
418 if (!print_local && !print_domain && !print_local_groups)
419 {
420 fprintf (stderr, "%s: Specify one of `-l', `-d' or `-g'\n", argv[0]);
421 return 1;
422 }
423 if (optind < argc)
424 {
425 if (!print_domain)
426 {
427 fprintf (stderr, "%s: A domain name is only accepted "
428 "when `-d' is given.\n", argv[0]);
429 return 1;
430 }
431 mbstowcs (domain_name, argv[optind], (strlen (argv[optind]) + 1));
432 domain_name_specified = 1;
433 }
434 }
435
436 if (passed_home_path[0] == '\0')
437 strcpy (passed_home_path, "/home/");
438
439 /* This takes Windows 9x/ME into account. */
440 if (GetVersion () >= 0x80000000)
441 {
442 /* Same behaviour as in cygwin/uinfo.cc (internal_getlogin). */
443 if (!GetUserName (name, (len = 256, &len)))
444 strcpy (name, "unknown");
445
446 printf ("%s::%ld:%ld::%s%s:/bin/bash\n", name,
447 DOMAIN_USER_RID_ADMIN,
448 DOMAIN_ALIAS_RID_ADMINS,
449 passed_home_path,
450 name);
451
452 return 0;
453 }
454
455 if (!load_netapi ())
456 {
457 fprintf (stderr, "Failed loading symbols from netapi32.dll "
458 "with error %lu\n", GetLastError ());
459 return 1;
460 }
461
462 /*
463 * Get `Everyone' group
464 */
465 if (AllocateAndInitializeSid (&sid_world_auth, 1, SECURITY_WORLD_RID,
466 0, 0, 0, 0, 0, 0, 0, &sid))
467 {
468 if (LookupAccountSid (NULL, sid,
469 name, (len = 256, &len),
470 dom, (len2 = 256, &len),
471 &use))
472 printf ("%s:*:%d:%d:%s%s::\n", name,
473 SECURITY_WORLD_RID,
474 SECURITY_WORLD_RID,
475 print_sids ? "," : "",
476 print_sids ? put_sid (sid) : "");
477 FreeSid (sid);
478 }
479
480 /*
481 * Get `system' group
482 */
483 if (AllocateAndInitializeSid (&sid_nt_auth, 1, SECURITY_LOCAL_SYSTEM_RID,
484 0, 0, 0, 0, 0, 0, 0, &sid))
485 {
486 if (LookupAccountSid (NULL, sid,
487 name, (len = 256, &len),
488 dom, (len2 = 256, &len),
489 &use))
490 printf ("%s:*:%d:%d:%s%s::\n", name,
491 SECURITY_LOCAL_SYSTEM_RID,
492 SECURITY_LOCAL_SYSTEM_RID,
493 print_sids ? "," : "",
494 print_sids ? put_sid (sid) : "");
495 FreeSid (sid);
496 }
497
498 /*
499 * Get `administrators' group
500 */
501 if (!print_local_groups
502 && AllocateAndInitializeSid (&sid_nt_auth, 2,
503 SECURITY_BUILTIN_DOMAIN_RID,
504 DOMAIN_ALIAS_RID_ADMINS,
505 0, 0, 0, 0, 0, 0, &sid))
506 {
507 if (LookupAccountSid (NULL, sid,
508 name, (len = 256, &len),
509 dom, (len2 = 256, &len),
510 &use))
511 printf ("%s:*:%ld:%ld:%s%s::\n", name,
512 DOMAIN_ALIAS_RID_ADMINS,
513 DOMAIN_ALIAS_RID_ADMINS,
514 print_sids ? "," : "",
515 print_sids ? put_sid (sid) : "");
516 FreeSid (sid);
517 }
518
519 if (print_local_groups)
520 enum_local_groups (print_sids);
521
522 if (print_domain)
523 {
524 if (domain_name_specified)
525 rc = netgetdcname (NULL, domain_name, (LPBYTE *) & servername);
526
527 else
528 rc = netgetdcname (NULL, NULL, (LPBYTE *) & servername);
529
530 if (rc != ERROR_SUCCESS)
531 {
532 fprintf (stderr, "Cannot get DC, code = %ld\n", rc);
533 exit (1);
534 }
535
536 enum_users (servername, print_sids, print_cygpath, passed_home_path, id_offset);
537 }
538
539 if (print_local)
540 enum_users (NULL, print_sids, print_cygpath, passed_home_path, 0);
541
542 if (servername)
543 netapibufferfree (servername);
544
545 return 0;
546 }
This page took 0.067054 seconds and 6 git commands to generate.