selftest: Use tree_delete control in idmap_rfc2307 test
[gd/samba-autobuild/.git] / nsswitch / wbinfo.c
index a31fbdf283fb0281d073ca2476ecb30c26771ee4..67a97b5bbc04b3e15e049a2ea7bf80f5c2ba9fce 100644 (file)
@@ -688,7 +688,7 @@ static bool wbinfo_getdcname(const char *domain_name)
 
        /* Send request */
 
-       if (winbindd_request_response(WINBINDD_GETDCNAME, &request,
+       if (winbindd_request_response(NULL, WINBINDD_GETDCNAME, &request,
                                      &response) != NSS_STATUS_SUCCESS) {
                d_fprintf(stderr, "Could not get dc name for %s\n",domain_name);
                return false;
@@ -752,8 +752,8 @@ static bool wbinfo_check_secret(const char *domain)
                WBC_ERROR_IS_OK(wbc_status) ? "succeeded" : "failed");
 
        if (wbc_status == WBC_ERR_AUTH_ERROR) {
-               d_fprintf(stderr, "error code was %s (0x%x)\n",
-                         error->nt_string, error->nt_status);
+               d_fprintf(stderr, "wbcCheckTrustCredentials(%s): error code was %s (0x%x)\n",
+                         domain_name, error->nt_string, error->nt_status);
                wbcFreeMemory(error);
        }
        if (!WBC_ERROR_IS_OK(wbc_status)) {
@@ -811,8 +811,8 @@ static bool wbinfo_change_secret(const char *domain)
                WBC_ERROR_IS_OK(wbc_status) ? "succeeded" : "failed");
 
        if (wbc_status == WBC_ERR_AUTH_ERROR) {
-               d_fprintf(stderr, "error code was %s (0x%x)\n",
-                         error->nt_string, error->nt_status);
+               d_fprintf(stderr, "wbcChangeTrustCredentials(%s): error code was %s (0x%x)\n",
+                         domain_name, error->nt_string, error->nt_status);
                wbcFreeMemory(error);
        }
        if (!WBC_ERROR_IS_OK(wbc_status)) {
@@ -826,22 +826,31 @@ static bool wbinfo_change_secret(const char *domain)
 
 /* Check DC connection */
 
-static bool wbinfo_ping_dc(void)
+static bool wbinfo_ping_dc(const char *domain)
 {
        wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
        struct wbcAuthErrorInfo *error = NULL;
        char *dcname = NULL;
 
-       wbc_status = wbcPingDc2(NULL, &error, &dcname);
+       const char *domain_name;
 
-       d_printf("checking the NETLOGON dc connection to \"%s\" %s\n",
+       if (domain) {
+               domain_name = domain;
+       } else {
+               domain_name = get_winbind_domain();
+       }
+
+       wbc_status = wbcPingDc2(domain_name, &error, &dcname);
+
+       d_printf("checking the NETLOGON for domain[%s] dc connection to \"%s\" %s\n",
+                domain_name ? domain_name : "",
                 dcname ? dcname : "",
                 WBC_ERROR_IS_OK(wbc_status) ? "succeeded" : "failed");
 
        wbcFreeMemory(dcname);
        if (wbc_status == WBC_ERR_AUTH_ERROR) {
-               d_fprintf(stderr, "error code was %s (0x%x)\n",
-                         error->nt_string, error->nt_status);
+               d_fprintf(stderr, "wbcPingDc2(%s): error code was %s (0x%x)\n",
+                         domain_name, error->nt_string, error->nt_status);
                wbcFreeMemory(error);
                return false;
        }
@@ -1037,6 +1046,72 @@ static bool wbinfo_sids_to_unix_ids(const char *arg)
        return true;
 }
 
+static bool wbinfo_xids_to_sids(const char *arg)
+{
+       fstring idstr;
+       struct wbcUnixId *xids = NULL;
+       struct wbcDomainSid *sids;
+       wbcErr wbc_status;
+       int num_xids = 0;
+       const char *p;
+       int i;
+
+       p = arg;
+
+       while (next_token(&p, idstr, LIST_SEP, sizeof(idstr))) {
+               xids = talloc_realloc(talloc_tos(), xids, struct wbcUnixId,
+                                     num_xids+1);
+               if (xids == NULL) {
+                       d_fprintf(stderr, "talloc failed\n");
+                       return false;
+               }
+
+               switch (idstr[0]) {
+               case 'u':
+                       xids[num_xids] = (struct wbcUnixId) {
+                               .type = WBC_ID_TYPE_UID,
+                               .id.uid = atoi(&idstr[1])
+                       };
+                       break;
+               case 'g':
+                       xids[num_xids] = (struct wbcUnixId) {
+                               .type = WBC_ID_TYPE_GID,
+                               .id.gid = atoi(&idstr[1])
+                       };
+                       break;
+               default:
+                       d_fprintf(stderr, "%s is an invalid id\n", idstr);
+                       TALLOC_FREE(xids);
+                       return false;
+               }
+               num_xids += 1;
+       }
+
+       sids = talloc_array(talloc_tos(), struct wbcDomainSid, num_xids);
+       if (sids == NULL) {
+               d_fprintf(stderr, "talloc failed\n");
+               TALLOC_FREE(xids);
+               return false;
+       }
+
+       wbc_status = wbcUnixIdsToSids(xids, num_xids, sids);
+       if (!WBC_ERROR_IS_OK(wbc_status)) {
+               d_fprintf(stderr, "wbcUnixIdsToSids failed: %s\n",
+                         wbcErrorString(wbc_status));
+               TALLOC_FREE(sids);
+               TALLOC_FREE(xids);
+               return false;
+       }
+
+       for (i=0; i<num_xids; i++) {
+               char str[WBC_SID_STRING_BUFLEN];
+               wbcSidToStringBuf(&sids[i], str, sizeof(str));
+               d_printf("%s\n", str);
+       }
+
+       return true;
+}
+
 static bool wbinfo_allocate_uid(void)
 {
        wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
@@ -1329,13 +1404,14 @@ static bool wbinfo_lookuprids(const char *domain, const char *arg)
        }
 
        wbc_status = wbcLookupRids(&dinfo->sid, num_rids, rids,
-                                  (const char **)&domain_name, &names, &types);
+                                  &p, &names, &types);
        if (!WBC_ERROR_IS_OK(wbc_status)) {
                d_printf("winbind_lookup_rids failed: %s\n",
                         wbcErrorString(wbc_status));
                goto done;
        }
 
+       domain_name = discard_const_p(char, p);
        d_printf("Domain: %s\n", domain_name);
 
        for (i=0; i<num_rids; i++) {
@@ -1574,8 +1650,9 @@ static bool wbinfo_auth_krb5(char *username, const char *cctype, uint32_t flags)
 
        if (error) {
                d_fprintf(stderr,
-                        "error code was %s (0x%x)\nerror message was: %s\n",
-                        error->nt_string,
+                        "wbcLogonUser(%s): error code was %s (0x%x)\n"
+                        "error message was: %s\n",
+                        params.username, error->nt_string,
                         error->nt_status,
                         error->display_string);
        }
@@ -1704,7 +1781,8 @@ static bool wbinfo_auth_crap(char *username, bool use_ntlmv2, bool use_lanman)
                                                get_winbind_netbios_name(),
                                                get_winbind_domain());
 
-               if (!SMBNTLMv2encrypt(NULL, name_user, name_domain, pass,
+               if (pass != NULL &&
+                   !SMBNTLMv2encrypt(NULL, name_user, name_domain, pass,
                                      &server_chal,
                                      &names_blob,
                                      &lm, &nt, NULL, NULL)) {
@@ -1746,9 +1824,15 @@ static bool wbinfo_auth_crap(char *username, bool use_ntlmv2, bool use_lanman)
 
        if (wbc_status == WBC_ERR_AUTH_ERROR) {
                d_fprintf(stderr,
-                        "error code was %s (0x%x)\nerror message was: %s\n",
+                        "wbcAuthenticateUserEx(%s%c%s): error code was "
+                         "%s (0x%x, authoritative=%"PRIu8")\n"
+                        "error message was: %s\n",
+                        name_domain,
+                        winbind_separator(),
+                        name_user,
                         err->nt_string,
                         err->nt_status,
+                        err->authoritative,
                         err->display_string);
                wbcFreeMemory(err);
        } else if (WBC_ERROR_IS_OK(wbc_status)) {
@@ -1763,10 +1847,11 @@ static bool wbinfo_auth_crap(char *username, bool use_ntlmv2, bool use_lanman)
 
 /* Authenticate a user with a plaintext password */
 
-static bool wbinfo_pam_logon(char *username)
+static bool wbinfo_pam_logon(char *username, bool verbose)
 {
        wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
        struct wbcLogonUserParams params;
+       struct wbcLogonUserInfo *info = NULL;
        struct wbcAuthErrorInfo *error = NULL;
        char *s = NULL;
        char *p = NULL;
@@ -1811,7 +1896,55 @@ static bool wbinfo_pam_logon(char *username)
                return false;
        }
 
-       wbc_status = wbcLogonUser(&params, NULL, &error, NULL);
+       wbc_status = wbcLogonUser(&params, &info, &error, NULL);
+
+       if (verbose && (info != NULL)) {
+               struct wbcAuthUserInfo *i = info->info;
+               uint32_t j;
+
+               if (i->account_name != NULL) {
+                       d_printf("account_name: %s\n", i->account_name);
+               }
+               if (i->user_principal != NULL) {
+                       d_printf("user_principal: %s\n", i->user_principal);
+               }
+               if (i->full_name != NULL) {
+                       d_printf("full_name: %s\n", i->full_name);
+               }
+               if (i->domain_name != NULL) {
+                       d_printf("domain_name: %s\n", i->domain_name);
+               }
+               if (i->dns_domain_name != NULL) {
+                       d_printf("dns_domain_name: %s\n", i->dns_domain_name);
+               }
+               if (i->logon_server != NULL) {
+                       d_printf("logon_server: %s\n", i->logon_server);
+               }
+               if (i->logon_script != NULL) {
+                       d_printf("logon_script: %s\n", i->logon_script);
+               }
+               if (i->profile_path != NULL) {
+                       d_printf("profile_path: %s\n", i->profile_path);
+               }
+               if (i->home_directory != NULL) {
+                       d_printf("home_directory: %s\n", i->home_directory);
+               }
+               if (i->home_drive != NULL) {
+                       d_printf("home_drive: %s\n", i->home_drive);
+               }
+
+               d_printf("sids:");
+
+               for (j=0; j<i->num_sids; j++) {
+                       char buf[WBC_SID_STRING_BUFLEN];
+                       wbcSidToStringBuf(&i->sids[j].sid, buf, sizeof(buf));
+                       d_printf(" %s", buf);
+               }
+               d_printf("\n");
+
+               wbcFreeMemory(info);
+               info = NULL;
+       }
 
        wbcFreeMemory(params.blobs);
 
@@ -1820,7 +1953,9 @@ static bool wbinfo_pam_logon(char *username)
 
        if (!WBC_ERROR_IS_OK(wbc_status) && (error != NULL)) {
                d_fprintf(stderr,
-                         "error code was %s (0x%x)\nerror message was: %s\n",
+                         "wbcLogonUser(%s): error code was %s (0x%x)\n"
+                         "error message was: %s\n",
+                         params.username,
                          error->nt_string,
                          (int)error->nt_status,
                          error->display_string);
@@ -1898,7 +2033,7 @@ static bool wbinfo_klog(char *username)
 
        request.flags |= WBFLAG_PAM_AFS_TOKEN;
 
-       result = winbindd_request_response(WINBINDD_PAM_AUTH, &request,
+       result = winbindd_request_response(NULL, WINBINDD_PAM_AUTH, &request,
                                           &response);
 
        /* Display response */
@@ -2083,6 +2218,7 @@ enum {
        OPT_REMOVE_UID_MAPPING,
        OPT_REMOVE_GID_MAPPING,
        OPT_SIDS_TO_XIDS,
+       OPT_XIDS_TO_SIDS,
        OPT_SEPARATOR,
        OPT_LIST_ALL_DOMAINS,
        OPT_LIST_OWN_DOMAIN,
@@ -2095,6 +2231,7 @@ enum {
        OPT_CHANGE_USER_PASSWORD,
        OPT_CCACHE_SAVE,
        OPT_SID_TO_FULLNAME,
+       OPT_NTLMV1,
        OPT_NTLMV2,
        OPT_PAM_LOGON,
        OPT_LOGOFF,
@@ -2104,7 +2241,7 @@ enum {
        OPT_KRB5CCNAME
 };
 
-int main(int argc, char **argv, char **envp)
+int main(int argc, const char **argv, char **envp)
 {
        int opt;
        TALLOC_CTX *frame = talloc_stackframe();
@@ -2116,7 +2253,7 @@ int main(int argc, char **argv, char **envp)
        int int_subarg = -1;
        int result = 1;
        bool verbose = false;
-       bool use_ntlmv2 = false;
+       bool use_ntlmv2 = true;
        bool use_lanman = false;
        char *logoff_user = getenv("USER");
        int logoff_uid = geteuid();
@@ -2154,6 +2291,9 @@ int main(int argc, char **argv, char **envp)
                { "remove-gid-mapping", 0, POPT_ARG_STRING, &string_arg, OPT_REMOVE_GID_MAPPING, "Remove gid to sid mapping in idmap", "GID,SID" },
                { "sids-to-unix-ids", 0, POPT_ARG_STRING, &string_arg,
                  OPT_SIDS_TO_XIDS, "Translate SIDs to Unix IDs", "Sid-List" },
+               { "unix-ids-to-sids", 0, POPT_ARG_STRING, &string_arg,
+                 OPT_XIDS_TO_SIDS, "Translate Unix IDs to SIDs",
+                 "ID-List (u<num> g<num>)" },
                { "check-secret", 't', POPT_ARG_NONE, 0, 't', "Check shared secret" },
                { "change-secret", 'c', POPT_ARG_NONE, 0, 'c', "Change shared secret" },
                { "ping-dc", 'P', POPT_ARG_NONE, 0, 'P',
@@ -2207,6 +2347,8 @@ int main(int argc, char **argv, char **envp)
                { "separator", 0, POPT_ARG_NONE, 0, OPT_SEPARATOR, "Get the active winbind separator", NULL },
                { "verbose", 0, POPT_ARG_NONE, 0, OPT_VERBOSE, "Print additional information per command", NULL },
                { "change-user-password", 0, POPT_ARG_STRING, &string_arg, OPT_CHANGE_USER_PASSWORD, "Change the password for a user", NULL },
+               { "ntlmv1", 0, POPT_ARG_NONE, 0, OPT_NTLMV1,
+                 "Use NTLMv1 cryptography for user authentication", NULL},
                { "ntlmv2", 0, POPT_ARG_NONE, 0, OPT_NTLMV2, "Use NTLMv2 cryptography for user authentication", NULL},
                { "lanman", 0, POPT_ARG_NONE, 0, OPT_LANMAN, "Use lanman cryptography for user authentication", NULL},
                POPT_COMMON_VERSION
@@ -2214,12 +2356,12 @@ int main(int argc, char **argv, char **envp)
        };
 
        /* Samba client initialisation */
-       load_case_tables();
+       smb_init_locale();
 
 
        /* Parse options */
 
-       pc = poptGetContext("wbinfo", argc, (const char **)argv,
+       pc = poptGetContext("wbinfo", argc, argv,
                            long_options, 0);
 
        /* Parse command line options */
@@ -2235,8 +2377,8 @@ int main(int argc, char **argv, char **envp)
                case OPT_VERBOSE:
                        verbose = true;
                        break;
-               case OPT_NTLMV2:
-                       use_ntlmv2 = true;
+               case OPT_NTLMV1:
+                       use_ntlmv2 = false;
                        break;
                case OPT_LANMAN:
                        use_lanman = true;
@@ -2410,6 +2552,13 @@ int main(int argc, char **argv, char **envp)
                                goto done;
                        }
                        break;
+               case OPT_XIDS_TO_SIDS:
+                       if (!wbinfo_xids_to_sids(string_arg)) {
+                               d_fprintf(stderr, "wbinfo_xids_to_sids "
+                                         "failed\n");
+                               goto done;
+                       }
+                       break;
                case 't':
                        if (!wbinfo_check_secret(opt_domain_name)) {
                                d_fprintf(stderr, "Could not check secret\n");
@@ -2423,7 +2572,7 @@ int main(int argc, char **argv, char **envp)
                        }
                        break;
                case 'P':
-                       if (!wbinfo_ping_dc()) {
+                       if (!wbinfo_ping_dc(opt_domain_name)) {
                                goto done;
                        }
                        break;
@@ -2549,7 +2698,7 @@ int main(int argc, char **argv, char **envp)
                                break;
                        }
                case OPT_PAM_LOGON:
-                       if (!wbinfo_pam_logon(string_arg)) {
+                       if (!wbinfo_pam_logon(string_arg, verbose)) {
                                d_fprintf(stderr, "pam_logon failed for %s\n",
                                          string_arg);
                                goto done;