Align error message handling to mkpasswd's error messages throughout.

* mkgroup.c (print_win_error): Create macro calling ...
	(_print_win_error): ... this function created from former
	print_win_error.  Move up in source.
	(PDOMAIN_CONTROLLER_INFOW): Define return type of DcGetDcNameW.
	(dsgetdcname): New function pointer for DcGetDcNameW.
	(load_netapi): Get DcGetDcNameW address.
	(main): If DcGetDcNameW is available, use it.
	* mkpasswd.c (PDOMAIN_CONTROLLER_INFOW): Define return type of
	DcGetDcNameW.
	(dsgetdcname): New function pointer for DcGetDcNameW.
	(load_netapi): Get DcGetDcNameW address.
	(main): If DcGetDcNameW is available, use it.  Rename local variable
	domain_name_specified to domain_specified as in mkgroup.c.
This commit is contained in:
Corinna Vinschen 2005-09-21 22:26:08 +00:00
parent faf4210542
commit d829da1423
3 changed files with 145 additions and 63 deletions

View File

@ -1,3 +1,20 @@
2005-09-22 Corinna Vinschen <corinna@vinschen.de>
Align error message handling to mkpasswd's error messages throughout.
* mkgroup.c (print_win_error): Create macro calling ...
(_print_win_error): ... this function created from former
print_win_error. Move up in source.
(PDOMAIN_CONTROLLER_INFOW): Define return type of DcGetDcNameW.
(dsgetdcname): New function pointer for DcGetDcNameW.
(load_netapi): Get DcGetDcNameW address.
(main): If DcGetDcNameW is available, use it.
* mkpasswd.c (PDOMAIN_CONTROLLER_INFOW): Define return type of
DcGetDcNameW.
(dsgetdcname): New function pointer for DcGetDcNameW.
(load_netapi): Get DcGetDcNameW address.
(main): If DcGetDcNameW is available, use it. Rename local variable
domain_name_specified to domain_specified as in mkgroup.c.
2005-09-08 Bas van Gompel <cygwin-patch.buzz@bavag.tmfweb.nl> 2005-09-08 Bas van Gompel <cygwin-patch.buzz@bavag.tmfweb.nl>
* regtool.cc: Extend copyright-years. * regtool.cc: Extend copyright-years.

View File

@ -20,8 +20,22 @@
#include <ntsecapi.h> #include <ntsecapi.h>
#include <ntdef.h> #include <ntdef.h>
#define print_win_error(x) _print_win_error(x, __LINE__)
static const char version[] = "$Revision$"; static const char version[] = "$Revision$";
typedef struct {
LPWSTR DomainControllerName;
LPWSTR DomainControllerAddress;
ULONG DomainControllerAddressType;
GUID DomainGuid;
LPWSTR DomainName;
LPWSTR DnsForestName;
ULONG Flags;
LPWSTR DcSiteName;
LPWSTR ClientSiteName;
} *PDOMAIN_CONTROLLER_INFOW;
SID_IDENTIFIER_AUTHORITY sid_world_auth = {SECURITY_WORLD_SID_AUTHORITY}; SID_IDENTIFIER_AUTHORITY sid_world_auth = {SECURITY_WORLD_SID_AUTHORITY};
SID_IDENTIFIER_AUTHORITY sid_nt_auth = {SECURITY_NT_AUTHORITY}; SID_IDENTIFIER_AUTHORITY sid_nt_auth = {SECURITY_NT_AUTHORITY};
@ -39,6 +53,8 @@ NTSTATUS NTAPI (*lsaopenpolicy)(PLSA_UNICODE_STRING,PLSA_OBJECT_ATTRIBUTES,ACCES
NTSTATUS NTAPI (*lsaqueryinformationpolicy)(LSA_HANDLE,POLICY_INFORMATION_CLASS,PVOID*); NTSTATUS NTAPI (*lsaqueryinformationpolicy)(LSA_HANDLE,POLICY_INFORMATION_CLASS,PVOID*);
NTSTATUS NTAPI (*lsafreememory)(PVOID); NTSTATUS NTAPI (*lsafreememory)(PVOID);
NET_API_STATUS WINAPI (*dsgetdcname)(LPWSTR,LPWSTR,GUID*,LPWSTR,ULONG,PDOMAIN_CONTROLLER_INFOW*);
#ifndef min #ifndef min
#define min(a,b) (((a)<(b))?(a):(b)) #define min(a,b) (((a)<(b))?(a):(b))
#endif #endif
@ -68,6 +84,8 @@ load_netapi ()
if (!(netgetdcname = (void *) GetProcAddress (h, "NetGetDCName"))) if (!(netgetdcname = (void *) GetProcAddress (h, "NetGetDCName")))
return FALSE; return FALSE;
dsgetdcname = (void *) GetProcAddress (h, "DsGetDcNameW");
if (!(h = LoadLibrary ("advapi32.dll"))) if (!(h = LoadLibrary ("advapi32.dll")))
return FALSE; return FALSE;
@ -133,6 +151,22 @@ uni2ansi (LPWSTR wcs, char *mbs, int size)
*mbs = '\0'; *mbs = '\0';
} }
void
_print_win_error(DWORD code, int line)
{
char buf[4096];
if (FormatMessage (FORMAT_MESSAGE_FROM_SYSTEM
| FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
code,
MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR) buf, sizeof (buf), NULL))
fprintf (stderr, "mkgroup (%d): [%lu] %s", line, code, buf);
else
fprintf (stderr, "mkgroup (%d): error %lu", line, code);
}
void void
enum_local_users (LPWSTR groupname) enum_local_users (LPWSTR groupname)
{ {
@ -190,7 +224,7 @@ enum_local_groups (int print_sids, int print_users, char *disp_groupname)
switch (rc) switch (rc)
{ {
case ERROR_ACCESS_DENIED: case ERROR_ACCESS_DENIED:
fprintf (stderr, "Access denied\n"); print_win_error(rc);
exit (1); exit (1);
case ERROR_MORE_DATA: case ERROR_MORE_DATA:
@ -198,7 +232,7 @@ enum_local_groups (int print_sids, int print_users, char *disp_groupname)
break; break;
default: default:
fprintf (stderr, "NetLocalGroupEnum() failed with %ld\n", rc); print_win_error(rc);
exit (1); exit (1);
} }
@ -218,8 +252,8 @@ enum_local_groups (int print_sids, int print_users, char *disp_groupname)
&sid_length, domain_name, &domname_len, &sid_length, domain_name, &domname_len,
&acc_type)) &acc_type))
{ {
fprintf (stderr, "LookupAccountName(%s) failed with %ld\n", print_win_error(rc);
localgroup_name, GetLastError ()); fprintf(stderr, " (%s)\n", localgroup_name);
continue; continue;
} }
else if (acc_type == SidTypeDomain) else if (acc_type == SidTypeDomain)
@ -236,9 +270,8 @@ enum_local_groups (int print_sids, int print_users, char *disp_groupname)
domain_name, &domname_len, domain_name, &domname_len,
&acc_type)) &acc_type))
{ {
fprintf (stderr, print_win_error(rc);
"LookupAccountName(%s) failed with error %ld\n", fprintf(stderr, " (%s)\n", domname);
localgroup_name, GetLastError ());
continue; continue;
} }
} }
@ -322,7 +355,7 @@ enum_groups (LPWSTR servername, int print_sids, int print_users, int id_offset,
switch (rc) switch (rc)
{ {
case ERROR_ACCESS_DENIED: case ERROR_ACCESS_DENIED:
fprintf (stderr, "Access denied\n"); print_win_error(rc);
exit (1); exit (1);
case ERROR_MORE_DATA: case ERROR_MORE_DATA:
@ -330,7 +363,7 @@ enum_groups (LPWSTR servername, int print_sids, int print_users, int id_offset,
break; break;
default: default:
fprintf (stderr, "NetGroupEnum() failed with %ld\n", rc); print_win_error(rc);
exit (1); exit (1);
} }
@ -354,11 +387,8 @@ enum_groups (LPWSTR servername, int print_sids, int print_users, int id_offset,
domain_name, &domname_len, domain_name, &domname_len,
&acc_type)) &acc_type))
{ {
fprintf (stderr, print_win_error(rc);
"LookupAccountName (%s, %s) failed with error %ld\n", fprintf(stderr, " (%s)\n", groupname);
servername ? ansi_srvname : "NULL",
groupname,
GetLastError ());
continue; continue;
} }
else if (acc_type == SidTypeDomain) else if (acc_type == SidTypeDomain)
@ -376,11 +406,8 @@ enum_groups (LPWSTR servername, int print_sids, int print_users, int id_offset,
domain_name, &domname_len, domain_name, &domname_len,
&acc_type)) &acc_type))
{ {
fprintf (stderr, print_win_error(rc);
"LookupAccountName(%s,%s) failed with error %ld\n", fprintf(stderr, " (%s)\n", domname);
servername ? ansi_srvname : "NULL",
domname,
GetLastError ());
continue; continue;
} }
} }
@ -442,22 +469,6 @@ print_special (int print_sids,
} }
} }
void
print_win_error(DWORD code)
{
char buf[4096];
if (FormatMessage (FORMAT_MESSAGE_FROM_SYSTEM
| FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
code,
MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR) buf, sizeof (buf), NULL))
fprintf (stderr, "mkgroup: [%lu] %s", code, buf);
else
fprintf (stderr, "mkgroup: error %lu", code);
}
void void
current_group (int print_sids, int print_users, int id_offset) current_group (int print_sids, int print_users, int id_offset)
{ {
@ -489,7 +500,6 @@ current_group (int print_sids, int print_users, int id_offset)
if (errpos) if (errpos)
{ {
print_win_error (GetLastError ()); print_win_error (GetLastError ());
fprintf(stderr, " on line %d\n", errpos);
} }
return; return;
} }
@ -662,8 +672,7 @@ main (int argc, char **argv)
} }
if (!load_netapi ()) if (!load_netapi ())
{ {
fprintf (stderr, "Failed loading symbols from netapi32.dll " print_win_error(GetLastError ());
"with error %lu\n", GetLastError ());
return 1; return 1;
} }
@ -741,22 +750,43 @@ main (int argc, char **argv)
if (print_domain) if (print_domain)
do do
{ {
if (domain_specified) PDOMAIN_CONTROLLER_INFOW pdci = NULL;
{
mbstowcs (domain_name, argv[optind], (strlen (argv[optind]) + 1)); if (dsgetdcname)
rc = netgetdcname (NULL, domain_name, (void *) &servername); {
if (domain_specified)
{
mbstowcs (domain_name, argv[optind], strlen (argv[optind]) + 1);
rc = dsgetdcname (NULL, domain_name, NULL, NULL, 0, &pdci);
}
else
rc = dsgetdcname (NULL, NULL, NULL, NULL, 0, &pdci);
if (rc != ERROR_SUCCESS)
{
print_win_error(rc);
return 1;
}
servername = pdci->DomainControllerName;
} }
else else
rc = netgetdcname (NULL, NULL, (void *) &servername);
if (rc != ERROR_SUCCESS)
{ {
fprintf (stderr, "Cannot get PDC, code = %ld\n", rc); rc = netgetdcname (NULL, NULL, (void *) &servername);
return 1; if (rc == ERROR_SUCCESS && domain_specified)
{
LPWSTR server = servername;
mbstowcs (domain_name, argv[optind], strlen (argv[optind]) + 1);
rc = netgetdcname (NULL, domain_name, (void *) &servername);
netapibufferfree (server);
}
if (rc != ERROR_SUCCESS)
{
print_win_error(rc);
return 1;
}
} }
enum_groups (servername, print_sids, print_users, id_offset * i++,
enum_groups (servername, print_sids, print_users, id_offset * i++, disp_groupname); disp_groupname);
netapibufferfree (servername); netapibufferfree (pdci ? (PVOID) pdci : (PVOID) servername);
} }
while (++optind < argc); while (++optind < argc);

View File

@ -30,11 +30,24 @@ static const char version[] = "$Revision$";
SID_IDENTIFIER_AUTHORITY sid_world_auth = {SECURITY_WORLD_SID_AUTHORITY}; SID_IDENTIFIER_AUTHORITY sid_world_auth = {SECURITY_WORLD_SID_AUTHORITY};
SID_IDENTIFIER_AUTHORITY sid_nt_auth = {SECURITY_NT_AUTHORITY}; SID_IDENTIFIER_AUTHORITY sid_nt_auth = {SECURITY_NT_AUTHORITY};
typedef struct {
LPWSTR DomainControllerName;
LPWSTR DomainControllerAddress;
ULONG DomainControllerAddressType;
GUID DomainGuid;
LPWSTR DomainName;
LPWSTR DnsForestName;
ULONG Flags;
LPWSTR DcSiteName;
LPWSTR ClientSiteName;
} *PDOMAIN_CONTROLLER_INFOW;
NET_API_STATUS WINAPI (*netapibufferfree)(PVOID); NET_API_STATUS WINAPI (*netapibufferfree)(PVOID);
NET_API_STATUS WINAPI (*netuserenum)(LPWSTR,DWORD,DWORD,PBYTE*,DWORD,PDWORD,PDWORD,PDWORD); 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 (*netlocalgroupenum)(LPWSTR,DWORD,PBYTE*,DWORD,PDWORD,PDWORD,PDWORD);
NET_API_STATUS WINAPI (*netgetdcname)(LPWSTR,LPWSTR,PBYTE*); NET_API_STATUS WINAPI (*netgetdcname)(LPWSTR,LPWSTR,PBYTE*);
NET_API_STATUS WINAPI (*netusergetinfo)(LPWSTR,LPWSTR,DWORD,PBYTE*); NET_API_STATUS WINAPI (*netusergetinfo)(LPWSTR,LPWSTR,DWORD,PBYTE*);
NET_API_STATUS WINAPI (*dsgetdcname)(LPWSTR,LPWSTR,GUID*,LPWSTR,ULONG,PDOMAIN_CONTROLLER_INFOW*);
#ifndef min #ifndef min
#define min(a,b) (((a)<(b))?(a):(b)) #define min(a,b) (((a)<(b))?(a):(b))
@ -59,6 +72,8 @@ load_netapi ()
if (!(netusergetinfo = (void *) GetProcAddress (h, "NetUserGetInfo"))) if (!(netusergetinfo = (void *) GetProcAddress (h, "NetUserGetInfo")))
return FALSE; return FALSE;
dsgetdcname = (void *) GetProcAddress (h, "DsGetDcNameW");
return TRUE; return TRUE;
} }
@ -561,7 +576,7 @@ main (int argc, char **argv)
int print_current = 0; int print_current = 0;
int print_domain = 0; int print_domain = 0;
int print_local_groups = 0; int print_local_groups = 0;
int domain_name_specified = 0; int domain_specified = 0;
int print_sids = 1; int print_sids = 1;
int print_cygpath = 1; int print_cygpath = 1;
int id_offset = 10000; int id_offset = 10000;
@ -675,7 +690,7 @@ main (int argc, char **argv)
"when `-d' is given.\n", argv[0]); "when `-d' is given.\n", argv[0]);
return 1; return 1;
} }
domain_name_specified = 1; domain_specified = 1;
} }
if (!load_netapi ()) if (!load_netapi ())
{ {
@ -718,23 +733,43 @@ main (int argc, char **argv)
if (print_domain) if (print_domain)
do do
{ {
if (domain_name_specified) PDOMAIN_CONTROLLER_INFOW pdci = NULL;
if (dsgetdcname)
{ {
mbstowcs (domain_name, argv[optind], (strlen (argv[optind]) + 1)); if (domain_specified)
rc = netgetdcname (NULL, domain_name, (void *) &servername); {
mbstowcs (domain_name, argv[optind], strlen (argv[optind]) + 1);
rc = dsgetdcname (NULL, domain_name, NULL, NULL, 0, &pdci);
}
else
rc = dsgetdcname (NULL, NULL, NULL, NULL, 0, &pdci);
if (rc != ERROR_SUCCESS)
{
print_win_error(rc);
return 1;
}
servername = pdci->DomainControllerName;
} }
else else
rc = netgetdcname (NULL, NULL, (void *) &servername);
if (rc != ERROR_SUCCESS)
{ {
print_win_error(rc); rc = netgetdcname (NULL, NULL, (void *) &servername);
return 1; if (rc == ERROR_SUCCESS && domain_specified)
} {
LPWSTR server = servername;
mbstowcs (domain_name, argv[optind], strlen (argv[optind]) + 1);
rc = netgetdcname (server, domain_name, (void *) &servername);
netapibufferfree (server);
}
if (rc != ERROR_SUCCESS)
{
print_win_error(rc);
return 1;
}
}
enum_users (servername, print_sids, print_cygpath, passed_home_path, enum_users (servername, print_sids, print_cygpath, passed_home_path,
id_offset * i++, disp_username); id_offset * i++, disp_username);
netapibufferfree (servername); netapibufferfree (pdci ? (PVOID) pdci : (PVOID) servername);
} }
while (++optind < argc); while (++optind < argc);