From d829da1423d10f4c6fa72d6cdad23da4eb03e455 Mon Sep 17 00:00:00 2001 From: Corinna Vinschen Date: Wed, 21 Sep 2005 22:26:08 +0000 Subject: 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/mkgroup.c | 130 ++++++++++++++++++++++++++++++------------------- 1 file changed, 80 insertions(+), 50 deletions(-) (limited to 'winsup/utils/mkgroup.c') 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); -- cgit v1.2.3