This is the mail archive of the
cygwin-patches@cygwin.com
mailing list for the Cygwin project.
RE: patch to mkpasswd.c - allows selection of specific user
- To: "'cygwin-patches at cygwin dot com'" <cygwin-patches at cygwin dot com>
- Subject: RE: patch to mkpasswd.c - allows selection of specific user
- From: Mark Bradshaw <bradshaw at staff dot crosswalk dot com>
- Date: Sat, 10 Nov 2001 23:07:21 -0500
Well, after a little thought I decided not to split things up. Easier that
way. Scrap that last patch. Here's the final patch. This patch adds a -u
option that allows for specifying that only info on a particular user should
be displayed. All calls to enum_users now includes an additional username
parameter. If the username is NULL enum_users functions as it had in the
past. If it's not NULL (a user has been specified) then instead of calling
NetUserEnum it calls NetUserGetInfo, which targets just a particular user
and displays that user's info. Also, if -u is specified then the display of
the three special users/groups are suppressed.
Since only one lookup is done when a user is supplied via -u, this should
make it very fast for those with LARGE domains. This patch was tested on
Win2k Pro (not in a domain), Win2k Server (in a domain), and NT 4.0 (BDC).
It worked fine on all three. I've mailed my assignment, so it should be
getting to you....sometime. Do I need to resubmit the patch at a later
date, i.e. after the assignment's been processed?
==============================
2001-11-10 Mark Bradshaw <bradshaw@staff.crosswalk.com>
* mkpasswd.c (main): New -u option to allow specifying a
specific user. If specified, groups aren't displayed and
output is limited to only the specified user.
(enum_users): If specific user is specified, via -u option,
display only that user's record. With -u use NetUserGetInfo
instead of NetUserEnum.
(load_netapi): Added netusergetinfo.
===============================
--- mkpasswd.c.orig Sat Nov 10 22:50:08 2001
+++ mkpasswd.c Sat Nov 10 22:52:45 2001
@@ -27,6 +27,7 @@ NET_API_STATUS WINAPI (*netapibufferfree
NET_API_STATUS WINAPI
(*netuserenum)(LPWSTR,DWORD,DWORD,PBYTE*,DWORD,PDWORD,PDWORD,PDWORD);
NET_API_STATUS WINAPI
(*netlocalgroupenum)(LPWSTR,DWORD,PBYTE*,DWORD,PDWORD,PDWORD,PDWORD);
NET_API_STATUS WINAPI (*netgetdcname)(LPWSTR,LPWSTR,PBYTE*);
+NET_API_STATUS WINAPI (*netusergetinfo)(LPWSTR,LPWSTR,DWORD,PBYTE*);
#ifndef min
#define min(a,b) (((a)<(b))?(a):(b))
@@ -48,6 +49,8 @@ load_netapi ()
return FALSE;
if (!(netgetdcname = (void *) GetProcAddress (h, "NetGetDCName")))
return FALSE;
+ if (!(netusergetinfo = (void *) GetProcAddress (h, "NetUserGetInfo")))
+ return FALSE;
return TRUE;
}
@@ -104,7 +107,7 @@ uni2ansi (LPWSTR wcs, char *mbs, int siz
int
enum_users (LPWSTR servername, int print_sids, int print_cygpath,
- const char * passed_home_path, int id_offset)
+ const char * passed_home_path, int id_offset, char
*disp_username)
{
USER_INFO_3 *buffer;
DWORD entriesread = 0;
@@ -112,6 +115,7 @@ enum_users (LPWSTR servername, int print
DWORD resume_handle = 0;
DWORD rc;
char ansi_srvname[256];
+ WCHAR uni_name[512];
if (servername)
uni2ansi (servername, ansi_srvname, sizeof (ansi_srvname));
@@ -120,6 +124,12 @@ enum_users (LPWSTR servername, int print
{
DWORD i;
+ if (disp_username != NULL) {
+ MultiByteToWideChar (CP_ACP, 0, disp_username, -1, uni_name, 512 );
+ rc = netusergetinfo(servername, (LPWSTR) & uni_name, 3, (LPBYTE *)
&buffer );
+ entriesread=1;
+ }
+ else
rc = netuserenum (servername, 3, FILTER_NORMAL_ACCOUNT,
(LPBYTE *) & buffer, 1024,
&entriesread, &totalentries, &resume_handle);
@@ -135,6 +145,7 @@ enum_users (LPWSTR servername, int print
default:
fprintf (stderr, "NetUserEnum() failed with %ld\n", rc);
+ if ( rc == 2221 ) printf("That user doesn't appear to exist.\n");
exit (1);
}
@@ -381,6 +392,7 @@ usage ()
fprintf (stderr, " (this affects ntsec)\n");
fprintf (stderr, " -p,--path-to-home path if user account has no home
dir, use\n");
fprintf (stderr, " path instead of /home/\n");
+ fprintf (stderr, " -u,--username username only return information for
the specified user\n");
fprintf (stderr, " -?,--help displays this message\n\n");
fprintf (stderr, "One of `-l', `-d' or `-g' must be given on NT/W2K.\n");
return 1;
@@ -394,11 +406,12 @@ struct option longopts[] = {
{"no-mount", no_argument, NULL, 'm'},
{"no-sids", no_argument, NULL, 's'},
{"path-to-home",required_argument, NULL, 'p'},
+ {"username",required_argument, NULL, 'u'},
{"help", no_argument, NULL, 'h'},
{0, no_argument, NULL, 0}
};
-char opts[] = "ldo:gsmhp:";
+char opts[] = "ldo:gsmhpu:";
int
main (int argc, char **argv)
@@ -414,6 +427,7 @@ main (int argc, char **argv)
int print_cygpath = 1;
int id_offset = 10000;
int i;
+ char *disp_username = NULL;
char name[256], passed_home_path[MAX_PATH];
DWORD len;
@@ -459,6 +473,9 @@ main (int argc, char **argv)
if (optarg[strlen (optarg)-1] != '/')
strcat (passed_home_path, "/");
break;
+ case 'u':
+ disp_username = optarg;
+ break;
case 'h':
return usage ();
default:
@@ -513,19 +530,21 @@ main (int argc, char **argv)
/*
* Get `Everyone' group
*/
- print_special (print_sids, &sid_world_auth, 1, SECURITY_WORLD_RID, 0, 0,
0, 0, 0, 0, 0);
- /*
- * Get `system' group
- */
- print_special (print_sids, &sid_nt_auth, 1, SECURITY_LOCAL_SYSTEM_RID, 0,
0, 0, 0, 0, 0, 0);
- /*
- * Get `administrators' group
- */
- if (!print_local_groups)
- print_special (print_sids, &sid_nt_auth, 2,
SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_ADMINS, 0, 0, 0, 0, 0, 0);
-
- if (print_local_groups)
- enum_local_groups (print_sids);
+ if ( disp_username == NULL ) {
+ print_special (print_sids, &sid_world_auth, 1, SECURITY_WORLD_RID, 0,
0, 0, 0, 0, 0, 0);
+ /*
+ * Get `system' group
+ */
+ print_special (print_sids, &sid_nt_auth, 1, SECURITY_LOCAL_SYSTEM_RID,
0, 0, 0, 0, 0, 0, 0);
+ /*
+ * Get `administrators' group
+ */
+ if (!print_local_groups)
+ print_special (print_sids, &sid_nt_auth, 2,
SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_ADMINS, 0, 0, 0, 0, 0, 0);
+
+ if (print_local_groups)
+ enum_local_groups (print_sids);
+ }
if (print_domain)
{
@@ -541,11 +560,11 @@ main (int argc, char **argv)
exit (1);
}
- enum_users (servername, print_sids, print_cygpath, passed_home_path,
id_offset);
+ enum_users (servername, print_sids, print_cygpath, passed_home_path,
id_offset, disp_username);
}
if (print_local)
- enum_users (NULL, print_sids, print_cygpath, passed_home_path, 0);
+ enum_users (NULL, print_sids, print_cygpath, passed_home_path, 0,
disp_username);
if (servername)
netapibufferfree (servername);
mkpasswd.c.diff