Fix spelling s/ouput/output/
[samba.git] / nsswitch / wbinfo.c
index cba469e469a4b7dde6f08f3b372b4788f7f3454f..1b58c73602a1d75fadf206fad6f4589534bdf5c3 100644 (file)
@@ -24,9 +24,9 @@
 #include "includes.h"
 #include "winbind_client.h"
 #include "libwbclient/wbclient.h"
-#include "lib/popt/popt.h"
 #include "../libcli/auth/libcli_auth.h"
 #include "lib/cmdline/popt_common.h"
+#include "lib/afs/afs_settoken.h"
 
 #ifdef DBGC_CLASS
 #undef DBGC_CLASS
@@ -536,7 +536,26 @@ static bool wbinfo_list_domains(bool list_all_domains, bool verbose)
 
                switch(domain_list[i].trust_type) {
                case WBC_DOMINFO_TRUSTTYPE_NONE:
-                       d_printf("None        ");
+                       if (domain_list[i].trust_routing != NULL) {
+                               d_printf("%s\n", domain_list[i].trust_routing);
+                       } else {
+                               d_printf("None\n");
+                       }
+                       continue;
+               case WBC_DOMINFO_TRUSTTYPE_LOCAL:
+                       d_printf("Local\n");
+                       continue;
+               case WBC_DOMINFO_TRUSTTYPE_RWDC:
+                       d_printf("RWDC\n");
+                       continue;
+               case WBC_DOMINFO_TRUSTTYPE_RODC:
+                       d_printf("RODC\n");
+                       continue;
+               case WBC_DOMINFO_TRUSTTYPE_PDC:
+                       d_printf("PDC\n");
+                       continue;
+               case WBC_DOMINFO_TRUSTTYPE_WKSTA:
+                       d_printf("Workstation ");
                        break;
                case WBC_DOMINFO_TRUSTTYPE_FOREST:
                        d_printf("Forest      ");
@@ -621,7 +640,7 @@ static bool wbinfo_show_onlinestatus(const char *domain)
 
                d_printf("%s : %s\n",
                         domain_list[i].short_name,
-                        is_offline ? "offline" : "online" );
+                        is_offline ? "no active connection" : "active connection" );
        }
 
        wbcFreeMemory(domain_list);
@@ -688,7 +707,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 +771,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 +830,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,21 +845,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;
+
+       if (domain) {
+               domain_name = domain;
+       } else {
+               domain_name = get_winbind_domain();
+       }
+
+       wbc_status = wbcPingDc2(domain_name, &error, &dcname);
 
-       d_printf("checking the NETLOGON dc connection to \"%s\" %s\n",
+       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;
        }
@@ -1036,6 +1065,78 @@ 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];
+               struct wbcDomainSid null_sid = { 0 };
+
+               if (memcmp(&null_sid, &sids[i], sizeof(struct wbcDomainSid)) == 0) {
+                       d_printf("NOT MAPPED\n");
+                       continue;
+               }
+               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;
@@ -1222,8 +1323,12 @@ static bool wbinfo_lookupsid(const char *sid_str)
 
        /* Display response */
 
-       d_printf("%s%c%s %d\n",
-                domain, winbind_separator(), name, type);
+       if (type == WBC_SID_NAME_DOMAIN) {
+               d_printf("%s %d\n", domain, type);
+       } else {
+               d_printf("%s%c%s %d\n",
+                        domain, winbind_separator(), name, type);
+       }
 
        wbcFreeMemory(domain);
        wbcFreeMemory(name);
@@ -1324,13 +1429,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++) {
@@ -1391,11 +1497,28 @@ static bool wbinfo_lookup_sids(const char *arg)
        }
 
        for (i=0; i<num_sids; i++) {
+               const char *domain = NULL;
+
                wbcSidToStringBuf(&sids[i], sidstr, sizeof(sidstr));
 
-               d_printf("%s -> %s\\%s %d\n", sidstr,
-                        domains[names[i].domain_index].short_name,
-                        names[i].name, names[i].type);
+               if (names[i].domain_index >= num_domains) {
+                       domain = "<none>";
+               } else if (names[i].domain_index < 0) {
+                       domain = "<none>";
+               } else {
+                       domain = domains[names[i].domain_index].short_name;
+               }
+
+               if (names[i].type == WBC_SID_NAME_DOMAIN) {
+                       d_printf("%s -> %s %d\n", sidstr,
+                                domain,
+                                names[i].type);
+               } else {
+                       d_printf("%s -> %s%c%s %d\n", sidstr,
+                                domain,
+                                winbind_separator(),
+                                names[i].name, names[i].type);
+               }
        }
        wbcFreeMemory(names);
        wbcFreeMemory(domains);
@@ -1552,8 +1675,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);
        }
@@ -1674,15 +1798,25 @@ static bool wbinfo_auth_crap(char *username, bool use_ntlmv2, bool use_lanman)
        if (use_ntlmv2) {
                DATA_BLOB server_chal;
                DATA_BLOB names_blob;
+               const char *netbios_name = NULL;
+               const char *domain = NULL;
+
+               netbios_name = get_winbind_netbios_name(),
+               domain = get_winbind_domain();
+               if (domain == NULL) {
+                       d_fprintf(stderr, "Failed to get domain from winbindd\n");
+                       return false;
+               }
 
                server_chal = data_blob(params.password.response.challenge, 8);
 
                /* Pretend this is a login to 'us', for blob purposes */
                names_blob = NTLMv2_generate_names_blob(NULL,
-                                               get_winbind_netbios_name(),
-                                               get_winbind_domain());
+                                                       netbios_name,
+                                                       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)) {
@@ -1724,9 +1858,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)) {
@@ -1741,10 +1881,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;
@@ -1789,7 +1930,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);
 
@@ -1798,7 +1987,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);
@@ -1876,7 +2067,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 */
@@ -2061,6 +2252,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,
@@ -2073,15 +2265,17 @@ enum {
        OPT_CHANGE_USER_PASSWORD,
        OPT_CCACHE_SAVE,
        OPT_SID_TO_FULLNAME,
+       OPT_NTLMV1,
        OPT_NTLMV2,
        OPT_PAM_LOGON,
        OPT_LOGOFF,
        OPT_LOGOFF_USER,
        OPT_LOGOFF_UID,
-       OPT_LANMAN
+       OPT_LANMAN,
+       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();
@@ -2093,7 +2287,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();
@@ -2131,6 +2325,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',
@@ -2139,7 +2336,7 @@ int main(int argc, char **argv, char **envp)
                { "all-domains", 0, POPT_ARG_NONE, 0, OPT_LIST_ALL_DOMAINS, "List all domains (trusted and own domain)" },
                { "own-domain", 0, POPT_ARG_NONE, 0, OPT_LIST_OWN_DOMAIN, "List own domain" },
                { "sequence", 0, POPT_ARG_NONE, 0, OPT_SEQUENCE, "Deprecated command, see --online-status" },
-               { "online-status", 0, POPT_ARG_NONE, 0, OPT_ONLINESTATUS, "Show whether domains are marked as online or offline"},
+               { "online-status", 0, POPT_ARG_NONE, 0, OPT_ONLINESTATUS, "Show whether domains maintain an active connection"},
                { "domain-info", 'D', POPT_ARG_STRING, &string_arg, 'D', "Show most of the info we have about the domain" },
                { "user-info", 'i', POPT_ARG_STRING, &string_arg, 'i', "Get user info", "USER" },
                { "uid-info", 0, POPT_ARG_INT, &int_arg, OPT_UID_INFO, "Get user info from uid", "UID" },
@@ -2179,11 +2376,13 @@ int main(int argc, char **argv, char **envp)
                { "krb5auth", 'K', POPT_ARG_STRING, &string_arg, 'K', "authenticate user using Kerberos", "user%password" },
                        /* destroys wbinfo --help output */
                        /* "user%password,DOM\\user%password,user@EXAMPLE.COM,EXAMPLE.COM\\user%password" }, */
-               { "krb5ccname", 0, POPT_ARG_STRING, &opt_krb5ccname, '0', "authenticate user using Kerberos and specific credential cache type", "krb5ccname" },
+               { "krb5ccname", 0, POPT_ARG_STRING, &opt_krb5ccname, OPT_KRB5CCNAME, "authenticate user using Kerberos and specific credential cache type", "krb5ccname" },
 #endif
                { "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
@@ -2191,12 +2390,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 */
@@ -2212,8 +2411,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;
@@ -2387,6 +2586,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");
@@ -2400,7 +2606,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;
@@ -2526,7 +2732,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;
@@ -2635,6 +2841,7 @@ int main(int argc, char **argv, char **envp)
                case OPT_LANMAN:
                case OPT_LOGOFF_USER:
                case OPT_LOGOFF_UID:
+               case OPT_KRB5CCNAME:
                        break;
                default:
                        d_fprintf(stderr, "Invalid option\n");