From d829da1423d10f4c6fa72d6cdad23da4eb03e455 Mon Sep 17 00:00:00 2001 From: Corinna Vinschen Date: Wed, 21 Sep 2005 22:26:08 +0000 Subject: [PATCH] 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. --- winsup/utils/ChangeLog | 17 ++++++ winsup/utils/mkgroup.c | 130 ++++++++++++++++++++++++---------------- winsup/utils/mkpasswd.c | 61 +++++++++++++++---- 3 files changed, 145 insertions(+), 63 deletions(-) diff --git a/winsup/utils/ChangeLog b/winsup/utils/ChangeLog index da4cc028d..8287dd2fb 100644 --- a/winsup/utils/ChangeLog +++ b/winsup/utils/ChangeLog @@ -1,3 +1,20 @@ +2005-09-22 Corinna Vinschen + + 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 * regtool.cc: Extend copyright-years. diff --git a/winsup/utils/mkgroup.c b/winsup/utils/mkgroup.c index 35e64777d..338fb2491 100644 --- a/winsup/utils/mkgroup.c +++ b/winsup/utils/mkgroup.c @@ -20,8 +20,22 @@ #include #include +#define print_win_error(x) _print_win_error(x, __LINE__) + 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_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 (*lsafreememory)(PVOID); +NET_API_STATUS WINAPI (*dsgetdcname)(LPWSTR,LPWSTR,GUID*,LPWSTR,ULONG,PDOMAIN_CONTROLLER_INFOW*); + #ifndef min #define min(a,b) (((a)<(b))?(a):(b)) #endif @@ -68,6 +84,8 @@ load_netapi () if (!(netgetdcname = (void *) GetProcAddress (h, "NetGetDCName"))) return FALSE; + dsgetdcname = (void *) GetProcAddress (h, "DsGetDcNameW"); + if (!(h = LoadLibrary ("advapi32.dll"))) return FALSE; @@ -133,6 +151,22 @@ uni2ansi (LPWSTR wcs, char *mbs, int size) *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 enum_local_users (LPWSTR groupname) { @@ -190,7 +224,7 @@ enum_local_groups (int print_sids, int print_users, char *disp_groupname) switch (rc) { case ERROR_ACCESS_DENIED: - fprintf (stderr, "Access denied\n"); + print_win_error(rc); exit (1); case ERROR_MORE_DATA: @@ -198,7 +232,7 @@ enum_local_groups (int print_sids, int print_users, char *disp_groupname) break; default: - fprintf (stderr, "NetLocalGroupEnum() failed with %ld\n", rc); + print_win_error(rc); exit (1); } @@ -218,8 +252,8 @@ enum_local_groups (int print_sids, int print_users, char *disp_groupname) &sid_length, domain_name, &domname_len, &acc_type)) { - fprintf (stderr, "LookupAccountName(%s) failed with %ld\n", - localgroup_name, GetLastError ()); + print_win_error(rc); + fprintf(stderr, " (%s)\n", localgroup_name); continue; } 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, &acc_type)) { - fprintf (stderr, - "LookupAccountName(%s) failed with error %ld\n", - localgroup_name, GetLastError ()); + print_win_error(rc); + fprintf(stderr, " (%s)\n", domname); continue; } } @@ -322,7 +355,7 @@ enum_groups (LPWSTR servername, int print_sids, int print_users, int id_offset, switch (rc) { case ERROR_ACCESS_DENIED: - fprintf (stderr, "Access denied\n"); + print_win_error(rc); exit (1); case ERROR_MORE_DATA: @@ -330,7 +363,7 @@ enum_groups (LPWSTR servername, int print_sids, int print_users, int id_offset, break; default: - fprintf (stderr, "NetGroupEnum() failed with %ld\n", rc); + print_win_error(rc); exit (1); } @@ -354,11 +387,8 @@ enum_groups (LPWSTR servername, int print_sids, int print_users, int id_offset, domain_name, &domname_len, &acc_type)) { - fprintf (stderr, - "LookupAccountName (%s, %s) failed with error %ld\n", - servername ? ansi_srvname : "NULL", - groupname, - GetLastError ()); + print_win_error(rc); + fprintf(stderr, " (%s)\n", groupname); continue; } 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, &acc_type)) { - fprintf (stderr, - "LookupAccountName(%s,%s) failed with error %ld\n", - servername ? ansi_srvname : "NULL", - domname, - GetLastError ()); + print_win_error(rc); + fprintf(stderr, " (%s)\n", domname); 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 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) { print_win_error (GetLastError ()); - fprintf(stderr, " on line %d\n", errpos); } return; } @@ -662,8 +672,7 @@ main (int argc, char **argv) } if (!load_netapi ()) { - fprintf (stderr, "Failed loading symbols from netapi32.dll " - "with error %lu\n", GetLastError ()); + print_win_error(GetLastError ()); return 1; } @@ -741,22 +750,43 @@ main (int argc, char **argv) if (print_domain) do { - if (domain_specified) - { - mbstowcs (domain_name, argv[optind], (strlen (argv[optind]) + 1)); - rc = netgetdcname (NULL, domain_name, (void *) &servername); + PDOMAIN_CONTROLLER_INFOW pdci = NULL; + + if (dsgetdcname) + { + 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 - rc = netgetdcname (NULL, NULL, (void *) &servername); - - if (rc != ERROR_SUCCESS) { - fprintf (stderr, "Cannot get PDC, code = %ld\n", rc); - return 1; + rc = netgetdcname (NULL, NULL, (void *) &servername); + 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++, disp_groupname); - netapibufferfree (servername); + enum_groups (servername, print_sids, print_users, id_offset * i++, + disp_groupname); + netapibufferfree (pdci ? (PVOID) pdci : (PVOID) servername); } while (++optind < argc); diff --git a/winsup/utils/mkpasswd.c b/winsup/utils/mkpasswd.c index 30e53cf33..96fec7ad1 100644 --- a/winsup/utils/mkpasswd.c +++ b/winsup/utils/mkpasswd.c @@ -30,11 +30,24 @@ static const char version[] = "$Revision$"; SID_IDENTIFIER_AUTHORITY sid_world_auth = {SECURITY_WORLD_SID_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 (*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*); +NET_API_STATUS WINAPI (*dsgetdcname)(LPWSTR,LPWSTR,GUID*,LPWSTR,ULONG,PDOMAIN_CONTROLLER_INFOW*); #ifndef min #define min(a,b) (((a)<(b))?(a):(b)) @@ -59,6 +72,8 @@ load_netapi () if (!(netusergetinfo = (void *) GetProcAddress (h, "NetUserGetInfo"))) return FALSE; + dsgetdcname = (void *) GetProcAddress (h, "DsGetDcNameW"); + return TRUE; } @@ -561,7 +576,7 @@ main (int argc, char **argv) int print_current = 0; int print_domain = 0; int print_local_groups = 0; - int domain_name_specified = 0; + int domain_specified = 0; int print_sids = 1; int print_cygpath = 1; int id_offset = 10000; @@ -675,7 +690,7 @@ main (int argc, char **argv) "when `-d' is given.\n", argv[0]); return 1; } - domain_name_specified = 1; + domain_specified = 1; } if (!load_netapi ()) { @@ -718,23 +733,43 @@ main (int argc, char **argv) if (print_domain) do { - if (domain_name_specified) + PDOMAIN_CONTROLLER_INFOW pdci = NULL; + + if (dsgetdcname) { - mbstowcs (domain_name, argv[optind], (strlen (argv[optind]) + 1)); - 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 - rc = netgetdcname (NULL, NULL, (void *) &servername); - - if (rc != ERROR_SUCCESS) { - print_win_error(rc); - return 1; - } - + rc = netgetdcname (NULL, NULL, (void *) &servername); + 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, id_offset * i++, disp_username); - netapibufferfree (servername); + netapibufferfree (pdci ? (PVOID) pdci : (PVOID) servername); } while (++optind < argc);