* Makefile.in (ALL_LDFLAGS): Add --enable-auto-import option to quiet ld

warnings.
* mkgroup.c: Change all global variables and most functions to static.
(enum_local_groups): Add a print_current parameter to control whether entries
for everything should be printed or just the current user.
(enum_groups): Ditto.
(main): Move call fetch_current_pgrp_sid earlier to avoid a NULL deference when
no command line arguments are specified.  Accommodate argument changes to above
functions.  Avoid printing SYSTEM account when print_current is specified.
Issue error when extra stuff is found on the command line.
* mkpasswd.c: Change all global variables and most functions to static.
(enum_users): Add a print_current parameter to control whether entries for
everything should be printed or just the current user.
(main): Move call fetch_current_user_sid earlier to avoid a NULL deference when
no command line arguments are specified.  Accommodate argument change to above
function.  Avoid printing standard accounts when print_current is specified.
Issue error when extra stuff is found on the command line.
This commit is contained in:
Christopher Faylor 2008-08-17 16:45:44 +00:00
parent f609b6715a
commit 6510edf4bc
4 changed files with 161 additions and 110 deletions

View File

@ -1,3 +1,25 @@
2008-08-17 Christopher Faylor <me+cygwin@cgf.cx>
* Makefile.in (ALL_LDFLAGS): Add --enable-auto-import option to quiet
ld warnings.
* mkgroup.c: Change all global variables and most functions to static.
(enum_local_groups): Add a print_current parameter to control whether
entries for everything should be printed or just the current user.
(enum_groups): Ditto.
(main): Move call fetch_current_pgrp_sid earlier to avoid a NULL
deference when no command line arguments are specified. Accommodate
argument changes to above functions. Avoid printing SYSTEM account
when print_current is specified. Issue error when extra stuff is found
on the command line.
* mkpasswd.c: Change all global variables and most functions to static.
(enum_users): Add a print_current parameter to control whether entries
for everything should be printed or just the current user.
(main): Move call fetch_current_user_sid earlier to avoid a NULL
deference when no command line arguments are specified. Accommodate
argument change to above function. Avoid printing standard accounts
when print_current is specified. Issue error when extra stuff is found
on the command line.
2008-08-15 Corinna Vinschen <corinna@vinschen.de> 2008-08-15 Corinna Vinschen <corinna@vinschen.de>
* mkgroup.c (fetch_current_pgrp_sid): New function to fetch primary * mkgroup.c (fetch_current_pgrp_sid): New function to fetch primary

View File

@ -41,7 +41,7 @@ include $(srcdir)/../Makefile.common
.PHONY: all install clean realclean warn_dumper warn_cygcheck_zlib .PHONY: all install clean realclean warn_dumper warn_cygcheck_zlib
ALL_LDLIBS := -lnetapi32 -ladvapi32 ALL_LDLIBS := -lnetapi32 -ladvapi32
ALL_LDFLAGS := -B$(newlib_build)/libc -B$(w32api_lib) $(LDFLAGS) $(ALL_LDLIBS) ALL_LDFLAGS := -Wl,--enable-auto-import -B$(newlib_build)/libc -B$(w32api_lib) $(LDFLAGS) $(ALL_LDLIBS)
ALL_DEP_LDLIBS := $(cygwin_build)/libcygwin.a ${patsubst -l%,\ ALL_DEP_LDLIBS := $(cygwin_build)/libcygwin.a ${patsubst -l%,\
$(w32api_lib)/lib%.a,$(ALL_LDLIBS) -lkernel32 -luser32} $(w32api_lib)/lib%.a,$(ALL_LDLIBS) -lkernel32 -luser32}

View File

@ -53,7 +53,7 @@ typedef struct
BOOL with_dom; BOOL with_dom;
} domlist_t; } domlist_t;
void static void
_print_win_error (DWORD code, int line) _print_win_error (DWORD code, int line)
{ {
char buf[4096]; char buf[4096];
@ -69,7 +69,7 @@ _print_win_error (DWORD code, int line)
fprintf (stderr, "mkgroup (%d): error %lu", line, code); fprintf (stderr, "mkgroup (%d): error %lu", line, code);
} }
void static void
load_dsgetdcname () load_dsgetdcname ()
{ {
HANDLE h = LoadLibrary ("netapi32.dll"); HANDLE h = LoadLibrary ("netapi32.dll");
@ -124,7 +124,7 @@ get_dcname (char *domain)
return server; return server;
} }
char * static char *
put_sid (PSID psid) put_sid (PSID psid)
{ {
static char s[512]; static char s[512];
@ -158,10 +158,10 @@ typedef struct {
int buffer[10]; int buffer[10];
} sidbuf; } sidbuf;
sidbuf curr_pgrp; static sidbuf curr_pgrp;
BOOL got_curr_pgrp = FALSE; static BOOL got_curr_pgrp = FALSE;
void static void
fetch_current_pgrp_sid () fetch_current_pgrp_sid ()
{ {
DWORD len; DWORD len;
@ -177,7 +177,7 @@ fetch_current_pgrp_sid ()
} }
} }
void static void
current_group (const char *sep, DWORD id_offset) current_group (const char *sep, DWORD id_offset)
{ {
WCHAR grp[GNLEN + 1]; WCHAR grp[GNLEN + 1];
@ -204,7 +204,7 @@ current_group (const char *sep, DWORD id_offset)
id_offset + gid); id_offset + gid);
} }
void static void
enum_unix_groups (domlist_t *dom_or_machine, const char *sep, DWORD id_offset, enum_unix_groups (domlist_t *dom_or_machine, const char *sep, DWORD id_offset,
char *unix_grp_list) char *unix_grp_list)
{ {
@ -307,9 +307,10 @@ enum_unix_groups (domlist_t *dom_or_machine, const char *sep, DWORD id_offset,
FreeSid (psid); FreeSid (psid);
} }
int static int
enum_local_groups (BOOL domain, domlist_t *dom_or_machine, const char *sep, enum_local_groups (BOOL domain, domlist_t *dom_or_machine, const char *sep,
DWORD id_offset, char *disp_groupname, int print_builtin) DWORD id_offset, char *disp_groupname, int print_builtin,
int print_current)
{ {
WCHAR machine[INTERNET_MAX_HOST_NAME_LENGTH + 1]; WCHAR machine[INTERNET_MAX_HOST_NAME_LENGTH + 1];
PWCHAR servername = NULL; PWCHAR servername = NULL;
@ -332,7 +333,7 @@ enum_local_groups (BOOL domain, domlist_t *dom_or_machine, const char *sep,
{ {
int ret = mbstowcs (machine, d_or_m, INTERNET_MAX_HOST_NAME_LENGTH + 1); int ret = mbstowcs (machine, d_or_m, INTERNET_MAX_HOST_NAME_LENGTH + 1);
if (ret < 1 || ret >= INTERNET_MAX_HOST_NAME_LENGTH + 1) if (ret < 1 || ret >= INTERNET_MAX_HOST_NAME_LENGTH + 1)
{ {
fprintf (stderr, "%s: Invalid machine name '%s'. Skipping...\n", fprintf (stderr, "%s: Invalid machine name '%s'. Skipping...\n",
__progname, d_or_m); __progname, d_or_m);
return 1; return 1;
@ -352,7 +353,7 @@ enum_local_groups (BOOL domain, domlist_t *dom_or_machine, const char *sep,
buffer[0].lgrpi0_name = gname; buffer[0].lgrpi0_name = gname;
entriesread = 1; entriesread = 1;
} }
else else
rc = NetLocalGroupEnum (servername, 0, (void *) &buffer, rc = NetLocalGroupEnum (servername, 0, (void *) &buffer,
MAX_PREFERRED_LENGTH, &entriesread, MAX_PREFERRED_LENGTH, &entriesread,
&totalentries, &resume_handle); &totalentries, &resume_handle);
@ -391,25 +392,25 @@ enum_local_groups (BOOL domain, domlist_t *dom_or_machine, const char *sep,
fprintf (stderr, " (%ls)\n", buffer[i].lgrpi0_name); fprintf (stderr, " (%ls)\n", buffer[i].lgrpi0_name);
continue; continue;
} }
else if (acc_type == SidTypeDomain) else if (acc_type == SidTypeDomain)
{ {
WCHAR domname[MAX_DOMAIN_NAME_LEN + GNLEN + 2]; WCHAR domname[MAX_DOMAIN_NAME_LEN + GNLEN + 2];
wcscpy (domname, domain_name); wcscpy (domname, domain_name);
wcscat (domname, L"\\"); wcscat (domname, L"\\");
wcscat (domname, buffer[i].lgrpi0_name); wcscat (domname, buffer[i].lgrpi0_name);
sid_length = MAX_SID_LEN; sid_length = MAX_SID_LEN;
domname_len = MAX_DOMAIN_NAME_LEN + 1; domname_len = MAX_DOMAIN_NAME_LEN + 1;
if (!LookupAccountNameW (servername, domname, if (!LookupAccountNameW (servername, domname,
psid, &sid_length, psid, &sid_length,
domain_name, &domname_len, domain_name, &domname_len,
&acc_type)) &acc_type))
{ {
print_win_error (rc); print_win_error (rc);
fprintf(stderr, " (%ls)\n", domname); fprintf(stderr, " (%ls)\n", domname);
continue; continue;
} }
} }
/* Store all local SIDs with prefix "S-1-5-32-" and check if it /* Store all local SIDs with prefix "S-1-5-32-" and check if it
has been printed already. This allows to get all builtin has been printed already. This allows to get all builtin
@ -421,7 +422,7 @@ enum_local_groups (BOOL domain, domlist_t *dom_or_machine, const char *sep,
int b; int b;
if (!print_builtin) if (!print_builtin)
goto skip_group; goto skip_group;
is_builtin = TRUE; is_builtin = TRUE;
if (builtin_sid_cnt) if (builtin_sid_cnt)
for (b = 0; b < builtin_sid_cnt; b++) for (b = 0; b < builtin_sid_cnt; b++)
@ -431,8 +432,12 @@ enum_local_groups (BOOL domain, domlist_t *dom_or_machine, const char *sep,
CopySid (sizeof (DBGSID), &builtin_sid_list[builtin_sid_cnt++], CopySid (sizeof (DBGSID), &builtin_sid_list[builtin_sid_cnt++],
psid); psid);
} }
if (EqualSid (curr_pgrp.psid, psid)) if (!print_current)
/* fall through */;
else if (EqualSid (curr_pgrp.psid, psid))
got_curr_pgrp = TRUE; got_curr_pgrp = TRUE;
else
continue;
gid = *GetSidSubAuthority (psid, *GetSidSubAuthorityCount(psid) - 1); gid = *GetSidSubAuthority (psid, *GetSidSubAuthorityCount(psid) - 1);
printf ("%ls%s%ls:%s:%ld:\n", printf ("%ls%s%ls:%s:%ld:\n",
with_dom && !is_builtin ? domain_name : L"", with_dom && !is_builtin ? domain_name : L"",
@ -452,9 +457,9 @@ skip_group:
return 0; return 0;
} }
void static void
enum_groups (BOOL domain, domlist_t *dom_or_machine, const char *sep, enum_groups (BOOL domain, domlist_t *dom_or_machine, const char *sep,
DWORD id_offset, char *disp_groupname) DWORD id_offset, char *disp_groupname, int print_current)
{ {
WCHAR machine[INTERNET_MAX_HOST_NAME_LENGTH + 1]; WCHAR machine[INTERNET_MAX_HOST_NAME_LENGTH + 1];
PWCHAR servername = NULL; PWCHAR servername = NULL;
@ -477,7 +482,7 @@ enum_groups (BOOL domain, domlist_t *dom_or_machine, const char *sep,
{ {
int ret = mbstowcs (machine, d_or_m, INTERNET_MAX_HOST_NAME_LENGTH + 1); int ret = mbstowcs (machine, d_or_m, INTERNET_MAX_HOST_NAME_LENGTH + 1);
if (ret < 1 || ret >= INTERNET_MAX_HOST_NAME_LENGTH + 1) if (ret < 1 || ret >= INTERNET_MAX_HOST_NAME_LENGTH + 1)
{ {
fprintf (stderr, "%s: Invalid machine name '%s'. Skipping...\n", fprintf (stderr, "%s: Invalid machine name '%s'. Skipping...\n",
__progname, d_or_m); __progname, d_or_m);
return; return;
@ -496,7 +501,7 @@ enum_groups (BOOL domain, domlist_t *dom_or_machine, const char *sep,
(void *) &buffer); (void *) &buffer);
entriesread=1; entriesread=1;
} }
else else
rc = NetGroupEnum (servername, 2, (void *) & buffer, rc = NetGroupEnum (servername, 2, (void *) & buffer,
MAX_PREFERRED_LENGTH, &entriesread, &totalentries, MAX_PREFERRED_LENGTH, &entriesread, &totalentries,
&resume_handle); &resume_handle);
@ -554,8 +559,12 @@ enum_groups (BOOL domain, domlist_t *dom_or_machine, const char *sep,
continue; continue;
} }
} }
if (EqualSid (curr_pgrp.psid, psid)) if (!print_current)
/* fall through */;
else if (EqualSid (curr_pgrp.psid, psid))
got_curr_pgrp = TRUE; got_curr_pgrp = TRUE;
else
continue;
printf ("%ls%s%ls:%s:%lu:\n", printf ("%ls%s%ls:%s:%lu:\n",
with_dom ? domain_name : L"", with_dom ? domain_name : L"",
with_dom ? sep : "", with_dom ? sep : "",
@ -570,7 +579,7 @@ enum_groups (BOOL domain, domlist_t *dom_or_machine, const char *sep,
while (rc == ERROR_MORE_DATA); while (rc == ERROR_MORE_DATA);
} }
void static void
print_special (PSID_IDENTIFIER_AUTHORITY auth, BYTE cnt, print_special (PSID_IDENTIFIER_AUTHORITY auth, BYTE cnt,
DWORD sub1, DWORD sub2, DWORD sub3, DWORD sub4, DWORD sub1, DWORD sub2, DWORD sub3, DWORD sub4,
DWORD sub5, DWORD sub6, DWORD sub7, DWORD sub8) DWORD sub5, DWORD sub6, DWORD sub7, DWORD sub8)
@ -581,7 +590,7 @@ print_special (PSID_IDENTIFIER_AUTHORITY auth, BYTE cnt,
SID_NAME_USE acc_type; SID_NAME_USE acc_type;
if (AllocateAndInitializeSid (auth, cnt, sub1, sub2, sub3, sub4, if (AllocateAndInitializeSid (auth, cnt, sub1, sub2, sub3, sub4,
sub5, sub6, sub7, sub8, &psid)) sub5, sub6, sub7, sub8, &psid))
{ {
if (LookupAccountSidW (NULL, psid, if (LookupAccountSidW (NULL, psid,
grp, (glen = GNLEN + 1, &glen), grp, (glen = GNLEN + 1, &glen),
@ -605,12 +614,12 @@ print_special (PSID_IDENTIFIER_AUTHORITY auth, BYTE cnt,
else else
rid = sub1; rid = sub1;
printf ("%ls:%s:%lu:\n", grp, put_sid (psid), rid); printf ("%ls:%s:%lu:\n", grp, put_sid (psid), rid);
} }
FreeSid (psid); FreeSid (psid);
} }
} }
int static int
usage (FILE * stream) usage (FILE * stream)
{ {
fprintf (stream, fprintf (stream,
@ -672,9 +681,9 @@ struct option longopts[] = {
{0, no_argument, NULL, 0} {0, no_argument, NULL, 0}
}; };
char opts[] = "bcCd::D::g:hl::L::o:sS:uU:v"; static char opts[] = "bcCd::D::g:hl::L::o:sS:uU:v";
void static void
print_version () print_version ()
{ {
const char *v = strchr (version, ':'); const char *v = strchr (version, ':');
@ -712,7 +721,7 @@ fetch_primary_domain ()
if (!NT_SUCCESS (status)) if (!NT_SUCCESS (status))
return FALSE; return FALSE;
status = LsaQueryInformationPolicy (lsa, PolicyPrimaryDomainInformation, status = LsaQueryInformationPolicy (lsa, PolicyPrimaryDomainInformation,
(PVOID *) &p_dom); (PVOID *) ((void *) &p_dom));
LsaClose (lsa); LsaClose (lsa);
if (!NT_SUCCESS (status)) if (!NT_SUCCESS (status))
return FALSE; return FALSE;
@ -741,6 +750,8 @@ main (int argc, char **argv)
load_dsgetdcname (); load_dsgetdcname ();
in_domain = fetch_primary_domain (); in_domain = fetch_primary_domain ();
fetch_current_pgrp_sid ();
if (argc == 1) if (argc == 1)
{ {
print_special (&sid_nt_auth, 1, SECURITY_LOCAL_SYSTEM_RID, print_special (&sid_nt_auth, 1, SECURITY_LOCAL_SYSTEM_RID,
@ -748,12 +759,12 @@ main (int argc, char **argv)
if (in_domain) if (in_domain)
{ {
if (!enum_local_groups (TRUE, NULL, sep_char, id_offset, if (!enum_local_groups (TRUE, NULL, sep_char, id_offset,
disp_groupname, print_builtin)) disp_groupname, print_builtin, 0))
enum_groups (TRUE, NULL, sep_char, id_offset, disp_groupname); enum_groups (TRUE, NULL, sep_char, id_offset, disp_groupname, 0);
} }
else if (!enum_local_groups (FALSE, NULL, sep_char, 0, disp_groupname, else if (!enum_local_groups (FALSE, NULL, sep_char, 0, disp_groupname,
print_builtin)) print_builtin, 0))
enum_groups (FALSE, NULL, sep_char, 0, disp_groupname); enum_groups (FALSE, NULL, sep_char, 0, disp_groupname, 0);
return 0; return 0;
} }
@ -792,13 +803,13 @@ main (int argc, char **argv)
if (opt && (p = strchr (opt, ','))) if (opt && (p = strchr (opt, ',')))
{ {
if (p == opt if (p == opt
|| !isdigit (p[1]) || !isdigit (p[1])
|| (domlist[print_domlist].id_offset = strtol (p + 1, &ep, 10) || (domlist[print_domlist].id_offset = strtol (p + 1, &ep, 10)
, *ep)) , *ep))
{ {
fprintf (stderr, "%s: Malformed machine,offset string '%s'. " fprintf (stderr, "%s: Malformed machine,offset string '%s'. "
"Skipping...\n", __progname, opt); "Skipping...\n", __progname, opt);
break; break;
} }
*p = '\0'; *p = '\0';
} }
@ -819,12 +830,12 @@ skip:
"character.\n", __progname); "character.\n", __progname);
return 1; return 1;
} }
break; break;
case 'U': case 'U':
print_unix = optarg; print_unix = optarg;
break; break;
case 'c': case 'c':
sep_char = NULL; sep_char = NULL;
/*FALLTHRU*/ /*FALLTHRU*/
case 'C': case 'C':
print_current = 1; print_current = 1;
@ -833,7 +844,7 @@ skip:
id_offset = strtol (optarg, NULL, 10); id_offset = strtol (optarg, NULL, 10);
break; break;
case 'b': case 'b':
print_builtin = 0; print_builtin = 0;
break; break;
case 's': case 's':
break; break;
@ -853,13 +864,19 @@ skip:
return 1; return 1;
} }
if (argv[optind])
{
fprintf (stderr,
"mkgroup: non-option command line argument `%s' is not allowed.\n"
"Try `mkgroup --help' for more information.\n", argv[optind]);
exit (1);
}
/* Get 'system' group */ /* Get 'system' group */
if (!disp_groupname && print_system && print_builtin) if (!disp_groupname && print_system && print_builtin && !print_current)
print_special (&sid_nt_auth, 1, SECURITY_LOCAL_SYSTEM_RID, print_special (&sid_nt_auth, 1, SECURITY_LOCAL_SYSTEM_RID,
0, 0, 0, 0, 0, 0, 0); 0, 0, 0, 0, 0, 0, 0);
fetch_current_pgrp_sid ();
off = id_offset; off = id_offset;
for (i = 0; i < print_domlist; ++i) for (i = 0; i < print_domlist; ++i)
{ {
@ -867,12 +884,12 @@ skip:
? domlist[i].id_offset != ULONG_MAX ? domlist[i].id_offset != ULONG_MAX
? domlist[i].id_offset : off : 0; ? domlist[i].id_offset : off : 0;
if (!enum_local_groups (domlist[i].domain, domlist + i, sep_char, if (!enum_local_groups (domlist[i].domain, domlist + i, sep_char,
my_off, disp_groupname, print_builtin)) my_off, disp_groupname, print_builtin, print_current))
{ {
if (!domlist[i].domain && domlist[i].str && print_unix) if (!domlist[i].domain && domlist[i].str && print_unix)
enum_unix_groups (domlist + i, sep_char, my_off, print_unix); enum_unix_groups (domlist + i, sep_char, my_off, print_unix);
enum_groups (domlist[i].domain, domlist + i, sep_char, my_off, enum_groups (domlist[i].domain, domlist + i, sep_char, my_off,
disp_groupname); disp_groupname, print_current);
if (my_off) if (my_off)
off += id_offset; off += id_offset;
} }

View File

@ -45,7 +45,7 @@ NET_API_STATUS WINAPI (*dsgetdcname)(LPWSTR,LPWSTR,GUID*,LPWSTR,ULONG,PDOMAIN_CO
#define min(a,b) (((a)<(b))?(a):(b)) #define min(a,b) (((a)<(b))?(a):(b))
#endif #endif
typedef struct typedef struct
{ {
char *str; char *str;
DWORD id_offset; DWORD id_offset;
@ -53,7 +53,7 @@ typedef struct
BOOL with_dom; BOOL with_dom;
} domlist_t; } domlist_t;
void static void
_print_win_error(DWORD code, int line) _print_win_error(DWORD code, int line)
{ {
char buf[4096]; char buf[4096];
@ -69,7 +69,7 @@ _print_win_error(DWORD code, int line)
fprintf (stderr, "mkpasswd (%d): error %lu", line, code); fprintf (stderr, "mkpasswd (%d): error %lu", line, code);
} }
void static void
load_dsgetdcname () load_dsgetdcname ()
{ {
HANDLE h = LoadLibrary ("netapi32.dll"); HANDLE h = LoadLibrary ("netapi32.dll");
@ -125,7 +125,7 @@ get_dcname (char *domain)
return server; return server;
} }
char * static char *
put_sid (PSID sid) put_sid (PSID sid)
{ {
static char s[512]; static char s[512];
@ -143,7 +143,7 @@ put_sid (PSID sid)
return s; return s;
} }
void static void
psx_dir (char *in, char *out) psx_dir (char *in, char *out)
{ {
if (isalpha (in[0]) && in[1] == ':') if (isalpha (in[0]) && in[1] == ':')
@ -166,7 +166,7 @@ psx_dir (char *in, char *out)
*out = '\0'; *out = '\0';
} }
void static void
uni2ansi (LPWSTR wcs, char *mbs, int size) uni2ansi (LPWSTR wcs, char *mbs, int size)
{ {
if (wcs) if (wcs)
@ -180,11 +180,11 @@ typedef struct {
int buffer[10]; int buffer[10];
} sidbuf; } sidbuf;
sidbuf curr_user; static sidbuf curr_user;
sidbuf curr_pgrp; static sidbuf curr_pgrp;
BOOL got_curr_user = FALSE; static BOOL got_curr_user = FALSE;
void static void
fetch_current_user_sid () fetch_current_user_sid ()
{ {
DWORD len; DWORD len;
@ -202,7 +202,7 @@ fetch_current_user_sid ()
} }
} }
void static void
current_user (int print_cygpath, const char *sep, const char *passed_home_path, current_user (int print_cygpath, const char *sep, const char *passed_home_path,
DWORD id_offset, const char *disp_username) DWORD id_offset, const char *disp_username)
{ {
@ -233,7 +233,7 @@ current_user (int print_cygpath, const char *sep, const char *passed_home_path,
char *envhomepath = getenv ("HOMEPATH"); char *envhomepath = getenv ("HOMEPATH");
if (envhome && envhome[0]) if (envhome && envhome[0])
{ {
if (print_cygpath) if (print_cygpath)
cygwin_conv_path (CCP_WIN_A_TO_POSIX | CCP_ABSOLUTE, envhome, cygwin_conv_path (CCP_WIN_A_TO_POSIX | CCP_ABSOLUTE, envhome,
homedir_psx, PATH_MAX); homedir_psx, PATH_MAX);
@ -241,7 +241,7 @@ current_user (int print_cygpath, const char *sep, const char *passed_home_path,
psx_dir (envhome, homedir_psx); psx_dir (envhome, homedir_psx);
} }
else if (envhomepath && envhomepath[0]) else if (envhomepath && envhomepath[0])
{ {
if (envhomedrive) if (envhomedrive)
strlcpy (homedir_w32, envhomedrive, sizeof (homedir_w32)); strlcpy (homedir_w32, envhomedrive, sizeof (homedir_w32));
if (envhomepath[0] != '\\') if (envhomepath[0] != '\\')
@ -254,7 +254,7 @@ current_user (int print_cygpath, const char *sep, const char *passed_home_path,
psx_dir (homedir_w32, homedir_psx); psx_dir (homedir_w32, homedir_psx);
} }
else else
{ {
wcstombs (stpncpy (homedir_psx, "/home/", sizeof (homedir_psx)), wcstombs (stpncpy (homedir_psx, "/home/", sizeof (homedir_psx)),
user, sizeof (homedir_psx) - 6); user, sizeof (homedir_psx) - 6);
homedir_psx[PATH_MAX - 1] = '\0'; homedir_psx[PATH_MAX - 1] = '\0';
@ -279,7 +279,7 @@ current_user (int print_cygpath, const char *sep, const char *passed_home_path,
homedir_psx); homedir_psx);
} }
void static void
enum_unix_users (domlist_t *dom_or_machine, const char *sep, DWORD id_offset, enum_unix_users (domlist_t *dom_or_machine, const char *sep, DWORD id_offset,
char *unix_user_list) char *unix_user_list)
{ {
@ -303,7 +303,7 @@ enum_unix_users (domlist_t *dom_or_machine, const char *sep, DWORD id_offset,
if (ret < 1 || ret >= INTERNET_MAX_HOST_NAME_LENGTH + 1) if (ret < 1 || ret >= INTERNET_MAX_HOST_NAME_LENGTH + 1)
{ {
fprintf (stderr, "%s: Invalid machine name '%s'. Skipping...\n", fprintf (stderr, "%s: Invalid machine name '%s'. Skipping...\n",
__progname, d_or_m); __progname, d_or_m);
return; return;
} }
servername = machine; servername = machine;
@ -320,7 +320,7 @@ enum_unix_users (domlist_t *dom_or_machine, const char *sep, DWORD id_offset,
for (ustr = strtok (user_list, ","); ustr; ustr = strtok (NULL, ",")) for (ustr = strtok (user_list, ","); ustr; ustr = strtok (NULL, ","))
{ {
if (!isdigit (ustr[0]) && ustr[0] != '-') if (!isdigit (ustr[0]) && ustr[0] != '-')
{ {
PWCHAR p = wcpcpy (user, L"Unix User\\"); PWCHAR p = wcpcpy (user, L"Unix User\\");
ret = mbstowcs (p, ustr, UNLEN + 1); ret = mbstowcs (p, ustr, UNLEN + 1);
if (ret < 1 || ret >= UNLEN + 1) if (ret < 1 || ret >= UNLEN + 1)
@ -332,14 +332,14 @@ enum_unix_users (domlist_t *dom_or_machine, const char *sep, DWORD id_offset,
dom, dom,
(dlen = MAX_DOMAIN_NAME_LEN + 1, &dlen), (dlen = MAX_DOMAIN_NAME_LEN + 1, &dlen),
&acc_type)) &acc_type))
printf ("%s%s%ls:unused:%lu:99999:,%s::\n", printf ("%s%s%ls:unused:%lu:99999:,%s::\n",
with_dom ? "Unix User" : "", with_dom ? "Unix User" : "",
with_dom ? sep : "", with_dom ? sep : "",
user + 10, user + 10,
id_offset + id_offset +
*GetSidSubAuthority (psid, *GetSidSubAuthority (psid,
*GetSidSubAuthorityCount(psid) - 1), *GetSidSubAuthorityCount(psid) - 1),
put_sid (psid)); put_sid (psid));
} }
else else
{ {
@ -382,10 +382,10 @@ enum_unix_users (domlist_t *dom_or_machine, const char *sep, DWORD id_offset,
FreeSid (psid); FreeSid (psid);
} }
int static int
enum_users (BOOL domain, domlist_t *dom_or_machine, const char *sep, enum_users (BOOL domain, domlist_t *dom_or_machine, const char *sep,
int print_cygpath, const char *passed_home_path, DWORD id_offset, int print_cygpath, const char *passed_home_path, DWORD id_offset,
char *disp_username) char *disp_username, int print_current)
{ {
WCHAR machine[INTERNET_MAX_HOST_NAME_LENGTH + 1]; WCHAR machine[INTERNET_MAX_HOST_NAME_LENGTH + 1];
PWCHAR servername = NULL; PWCHAR servername = NULL;
@ -397,12 +397,11 @@ enum_users (BOOL domain, domlist_t *dom_or_machine, const char *sep,
DWORD resume_handle = 0; DWORD resume_handle = 0;
DWORD rc; DWORD rc;
WCHAR uni_name[UNLEN + 1]; WCHAR uni_name[UNLEN + 1];
if (domain) if (domain)
{ {
servername = get_dcname (d_or_m); servername = get_dcname (d_or_m);
if (servername == (PWCHAR) -1) if (servername == (PWCHAR) -1)
return 1; return 1;
} }
else if (d_or_m) else if (d_or_m)
{ {
@ -427,7 +426,7 @@ enum_users (BOOL domain, domlist_t *dom_or_machine, const char *sep,
(void *) &buffer); (void *) &buffer);
entriesread = 1; entriesread = 1;
} }
else else
rc = NetUserEnum (servername, 3, FILTER_NORMAL_ACCOUNT, rc = NetUserEnum (servername, 3, FILTER_NORMAL_ACCOUNT,
(void *) &buffer, MAX_PREFERRED_LENGTH, (void *) &buffer, MAX_PREFERRED_LENGTH,
&entriesread, &totalentries, &resume_handle); &entriesread, &totalentries, &resume_handle);
@ -508,16 +507,21 @@ enum_users (BOOL domain, domlist_t *dom_or_machine, const char *sep,
continue; continue;
} }
} }
if (EqualSid (curr_user.psid, psid)) if (!print_current)
/* fall through */;
else if (EqualSid (curr_user.psid, psid))
got_curr_user = TRUE; got_curr_user = TRUE;
else
continue;
printf ("%ls%s%ls:unused:%lu:%lu:%ls%sU-%ls\\%ls,%s:%s:/bin/bash\n", printf ("%ls%s%ls:unused:%lu:%lu:%ls%sU-%ls\\%ls,%s:%s:/bin/bash\n",
with_dom ? domain_name : L"", with_dom ? domain_name : L"",
with_dom ? sep : "", with_dom ? sep : "",
buffer[i].usri3_name, buffer[i].usri3_name,
id_offset + uid, id_offset + uid,
id_offset + gid, id_offset + gid,
buffer[i].usri3_full_name ?: L"", buffer[i].usri3_full_name ?: L"",
buffer[i].usri3_full_name buffer[i].usri3_full_name
&& buffer[i].usri3_full_name[0] ? "," : "", && buffer[i].usri3_full_name[0] ? "," : "",
domain_name, domain_name,
buffer[i].usri3_name, buffer[i].usri3_name,
@ -533,7 +537,7 @@ enum_users (BOOL domain, domlist_t *dom_or_machine, const char *sep,
return 0; return 0;
} }
void static void
print_special (PSID_IDENTIFIER_AUTHORITY auth, BYTE cnt, print_special (PSID_IDENTIFIER_AUTHORITY auth, BYTE cnt,
DWORD sub1, DWORD sub2, DWORD sub3, DWORD sub4, DWORD sub1, DWORD sub2, DWORD sub3, DWORD sub4,
DWORD sub5, DWORD sub6, DWORD sub7, DWORD sub8) DWORD sub5, DWORD sub6, DWORD sub7, DWORD sub8)
@ -544,7 +548,7 @@ print_special (PSID_IDENTIFIER_AUTHORITY auth, BYTE cnt,
SID_NAME_USE acc_type; SID_NAME_USE acc_type;
if (AllocateAndInitializeSid (auth, cnt, sub1, sub2, sub3, sub4, if (AllocateAndInitializeSid (auth, cnt, sub1, sub2, sub3, sub4,
sub5, sub6, sub7, sub8, &sid)) sub5, sub6, sub7, sub8, &sid))
{ {
if (LookupAccountSidW (NULL, sid, if (LookupAccountSidW (NULL, sid,
user, (len = UNLEN + 1, &len), user, (len = UNLEN + 1, &len),
@ -570,12 +574,12 @@ print_special (PSID_IDENTIFIER_AUTHORITY auth, BYTE cnt,
printf ("%ls:*:%lu:%lu:,%s::\n", printf ("%ls:*:%lu:%lu:,%s::\n",
user, rid, rid == 18 ? 544 : rid, /* SYSTEM hack */ user, rid, rid == 18 ? 544 : rid, /* SYSTEM hack */
put_sid (sid)); put_sid (sid));
} }
FreeSid (sid); FreeSid (sid);
} }
} }
int static int
usage (FILE * stream) usage (FILE * stream)
{ {
fprintf (stream, fprintf (stream,
@ -605,7 +609,7 @@ usage (FILE * stream)
" -p,--path-to-home path use specified path instead of user account home dir\n" " -p,--path-to-home path use specified path instead of user account home dir\n"
" or /home prefix\n" " or /home prefix\n"
" -m,--no-mount don't use mount points for home dir\n" " -m,--no-mount don't use mount points for home dir\n"
" -U,--unix userlist additionally print UNIX users when using -l or -L\n" " -U,--unix userlist additionally print UNIX users when using -l or -L\n"
" on a UNIX Samba server\n" " on a UNIX Samba server\n"
" userlist is a comma-separated list of usernames\n" " userlist is a comma-separated list of usernames\n"
" or uid ranges (root,-25,50-100).\n" " or uid ranges (root,-25,50-100).\n"
@ -620,7 +624,7 @@ usage (FILE * stream)
return 1; return 1;
} }
struct option longopts[] = { static struct option longopts[] = {
{"current", no_argument, NULL, 'c'}, {"current", no_argument, NULL, 'c'},
{"Current", no_argument, NULL, 'C'}, {"Current", no_argument, NULL, 'C'},
{"domain", optional_argument, NULL, 'd'}, {"domain", optional_argument, NULL, 'd'},
@ -640,7 +644,7 @@ struct option longopts[] = {
{0, no_argument, NULL, 0} {0, no_argument, NULL, 0}
}; };
char opts[] = "cCd::D::ghl::L::mo:sS:p:u:U:v"; static char opts[] = "cCd::D::ghl::L::mo:sS:p:u:U:v";
static void static void
print_version () print_version ()
@ -692,7 +696,7 @@ fetch_primary_domain ()
if (!NT_SUCCESS (status)) if (!NT_SUCCESS (status))
return FALSE; return FALSE;
status = LsaQueryInformationPolicy (lsa, PolicyPrimaryDomainInformation, status = LsaQueryInformationPolicy (lsa, PolicyPrimaryDomainInformation,
(PVOID *) &p_dom); (PVOID *) ((void *) &p_dom));
LsaClose (lsa); LsaClose (lsa);
if (!NT_SUCCESS (status)) if (!NT_SUCCESS (status))
return FALSE; return FALSE;
@ -722,15 +726,17 @@ main (int argc, char **argv)
load_dsgetdcname (); load_dsgetdcname ();
in_domain = fetch_primary_domain (); in_domain = fetch_primary_domain ();
fetch_current_user_sid ();
if (argc == 1) if (argc == 1)
{ {
enum_std_accounts (); enum_std_accounts ();
if (in_domain) if (in_domain)
enum_users (TRUE, NULL, sep_char, print_cygpath, passed_home_path, enum_users (TRUE, NULL, sep_char, print_cygpath, passed_home_path,
10000, disp_username); 10000, disp_username, 0);
else else
enum_users (FALSE, NULL, sep_char, print_cygpath, passed_home_path, 0, enum_users (FALSE, NULL, sep_char, print_cygpath, passed_home_path, 0,
disp_username); disp_username, 0);
return 0; return 0;
} }
@ -768,13 +774,13 @@ main (int argc, char **argv)
if (opt && (p = strchr (opt, ','))) if (opt && (p = strchr (opt, ',')))
{ {
if (p == opt if (p == opt
|| !isdigit (p[1]) || !isdigit (p[1])
|| (domlist[print_domlist].id_offset = strtol (p + 1, &ep, 10) || (domlist[print_domlist].id_offset = strtol (p + 1, &ep, 10)
, *ep)) , *ep))
{ {
fprintf (stderr, "%s: Malformed domain,offset string '%s'. " fprintf (stderr, "%s: Malformed domain,offset string '%s'. "
"Skipping...\n", __progname, opt); "Skipping...\n", __progname, opt);
break; break;
} }
*p = '\0'; *p = '\0';
} }
@ -795,9 +801,9 @@ skip:
"character.\n", __progname); "character.\n", __progname);
return 1; return 1;
} }
break; break;
case 'U': case 'U':
print_unix = optarg; print_unix = optarg;
break; break;
case 'c': case 'c':
sep_char = NULL; sep_char = NULL;
@ -846,7 +852,13 @@ skip:
return 1; return 1;
} }
fetch_current_user_sid (); if (argv[optind])
{
fprintf (stderr,
"mkpasswd: non-option command line argument `%s' is not allowed.\n"
"Try `mkpasswd --help' for more information.\n", argv[optind]);
exit (1);
}
off = id_offset; off = id_offset;
for (i = 0; i < print_domlist; ++i) for (i = 0; i < print_domlist; ++i)
@ -856,12 +868,12 @@ skip:
? domlist[i].id_offset : off : 0; ? domlist[i].id_offset : off : 0;
if (!domlist[i].domain && domlist[i].str && print_unix) if (!domlist[i].domain && domlist[i].str && print_unix)
enum_unix_users (domlist + i, sep_char, my_off, print_unix); enum_unix_users (domlist + i, sep_char, my_off, print_unix);
if (!my_off) if (!my_off && !print_current)
enum_std_accounts (); enum_std_accounts ();
enum_users (domlist[i].domain, domlist + i, sep_char, print_cygpath, enum_users (domlist[i].domain, domlist + i, sep_char, print_cygpath,
passed_home_path, my_off, disp_username); passed_home_path, my_off, disp_username, print_current);
if (my_off) if (my_off)
off += id_offset; off += id_offset;
} }
if (print_current && !got_curr_user) if (print_current && !got_curr_user)