nsswitch: add wbinfo -c (change trust account passwords).
[ira/wip.git] / nsswitch / wbinfo.c
index 3cd5a29e60adbbae36a81bd869e0214b10a992c5..219ec24fbaa72f4777695a7022a916b5b03eda23 100644 (file)
@@ -4,7 +4,7 @@
    Winbind status program.
 
    Copyright (C) Tim Potter      2000-2003
-   Copyright (C) Andrew Bartlett 2002
+   Copyright (C) Andrew Bartlett 2002-2007
 
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
 #include "includes.h"
 #include "winbind_client.h"
 #include "libwbclient/wbclient.h"
+#include "lib/popt/popt.h"
 #include "../libcli/auth/libcli_auth.h"
+#if !(_SAMBA_VERSION_) < 4
+#include "lib/cmdline/popt_common.h"
+#endif
 
+#ifdef DBGC_CLASS
 #undef DBGC_CLASS
 #define DBGC_CLASS DBGC_WINBIND
+#endif
 
 static struct wbcInterfaceDetails *init_interface_details(void)
 {
@@ -87,6 +93,20 @@ static const char *get_winbind_domain(void)
        return details->netbios_domain;
 }
 
+static const char *get_winbind_netbios_name(void)
+{
+       static struct wbcInterfaceDetails *details;
+
+       details = init_interface_details();
+
+       if (!details) {
+               d_fprintf(stderr, "could not obtain winbind netbios name!\n");
+               return 0;
+       }
+
+       return details->netbios_name;
+}
+
 /* Copy of parse_domain_user from winbindd_util.c.  Parse a string of the
    form DOMAIN/user into a domain and a user */
 
@@ -704,15 +724,55 @@ static bool wbinfo_dsgetdcname(const char *domain_name, uint32_t flags)
 
 /* Check trust account password */
 
-static bool wbinfo_check_secret(void)
+static bool wbinfo_check_secret(const char *domain)
 {
        wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
        struct wbcAuthErrorInfo *error = NULL;
+       const char *domain_name;
+
+       if (domain) {
+               domain_name = domain;
+       } else {
+               domain_name = get_winbind_domain();
+       }
 
-       wbc_status = wbcCheckTrustCredentials(NULL, &error);
+       wbc_status = wbcCheckTrustCredentials(domain_name, &error);
 
-       d_printf("checking the trust secret via RPC calls %s\n",
-                WBC_ERROR_IS_OK(wbc_status) ? "succeeded" : "failed");
+       d_printf("checking the trust secret for domain %s via RPC calls %s\n",
+               domain_name,
+               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);
+               wbcFreeMemory(error);
+       }
+       if (!WBC_ERROR_IS_OK(wbc_status)) {
+               return false;
+       }
+
+       return true;
+}
+
+/* Change trust account password */
+
+static bool wbinfo_change_secret(const char *domain)
+{
+       wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
+       struct wbcAuthErrorInfo *error = NULL;
+       const char *domain_name;
+
+       if (domain) {
+               domain_name = domain;
+       } else {
+               domain_name = get_winbind_domain();
+       }
+
+       wbc_status = wbcChangeTrustCredentials(domain_name, &error);
+
+       d_printf("changing the trust secret for domain %s via RPC calls %s\n",
+               domain_name,
+               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",
@@ -1101,7 +1161,7 @@ static bool wbinfo_lookuprids(const char *domain, const char *arg)
 
        for (i=0; i<num_rids; i++) {
                d_printf("%8d: %s (%s)\n", rids[i], names[i],
-                        sid_type_lookup(types[i]));
+                        wbcSidTypeString(types[i]));
        }
 
        ret = true;
@@ -1151,20 +1211,21 @@ static bool wbinfo_lookupname(const char *full_name)
 
        /* Display response */
 
-       d_printf("%s %s (%d)\n", sid_str, sid_type_lookup(type), type);
+       d_printf("%s %s (%d)\n", sid_str, wbcSidTypeString(type), type);
 
        wbcFreeMemory(sid_str);
 
        return true;
 }
 
-static char *wbinfo_prompt_pass(const char *prefix,
+static char *wbinfo_prompt_pass(TALLOC_CTX *mem_ctx,
+                               const char *prefix,
                                const char *username)
 {
        char *prompt;
        const char *ret = NULL;
 
-       prompt = talloc_asprintf(talloc_tos(), "Enter %s's ", username);
+       prompt = talloc_asprintf(mem_ctx, "Enter %s's ", username);
        if (!prompt) {
                return NULL;
        }
@@ -1182,7 +1243,7 @@ static char *wbinfo_prompt_pass(const char *prefix,
        ret = getpass(prompt);
        TALLOC_FREE(prompt);
 
-       return SMB_STRDUP(ret);
+       return talloc_strdup(mem_ctx, ret);
 }
 
 /* Authenticate a user with a plaintext password */
@@ -1194,24 +1255,28 @@ static bool wbinfo_auth_krb5(char *username, const char *cctype, uint32_t flags)
        char *p = NULL;
        char *password = NULL;
        char *name = NULL;
+       char *local_cctype = NULL;
        uid_t uid;
        struct wbcLogonUserParams params;
        struct wbcLogonUserInfo *info;
        struct wbcAuthErrorInfo *error;
        struct wbcUserPasswordPolicyInfo *policy;
+       TALLOC_CTX *frame = talloc_tos();
 
-       if ((s = SMB_STRDUP(username)) == NULL) {
+       if ((s = talloc_strdup(frame, username)) == NULL) {
                return false;
        }
 
        if ((p = strchr(s, '%')) != NULL) {
                *p = 0;
                p++;
-               password = SMB_STRDUP(p);
+               password = talloc_strdup(frame, p);
        } else {
-               password = wbinfo_prompt_pass(NULL, username);
+               password = wbinfo_prompt_pass(frame, NULL, username);
        }
 
+       local_cctype = talloc_strdup(frame, cctype);
+
        name = s;
 
        uid = geteuid();
@@ -1245,7 +1310,7 @@ static bool wbinfo_auth_krb5(char *username, const char *cctype, uint32_t flags)
                                     &params.blobs,
                                     "krb5_cc_type",
                                     0,
-                                    (uint8_t *)cctype,
+                                    (uint8_t *)local_cctype,
                                     strlen(cctype)+1);
        if (!WBC_ERROR_IS_OK(wbc_status)) {
                goto done;
@@ -1293,8 +1358,6 @@ static bool wbinfo_auth_krb5(char *username, const char *cctype, uint32_t flags)
        }
  done:
 
-       SAFE_FREE(s);
-       SAFE_FREE(password);
        wbcFreeMemory(params.blobs);
 
        return WBC_ERROR_IS_OK(wbc_status);
@@ -1309,17 +1372,18 @@ static bool wbinfo_auth(char *username)
        char *p = NULL;
        char *password = NULL;
        char *name = NULL;
+       TALLOC_CTX *frame = talloc_tos();
 
-       if ((s = SMB_STRDUP(username)) == NULL) {
+       if ((s = talloc_strdup(frame, username)) == NULL) {
                return false;
        }
 
        if ((p = strchr(s, '%')) != NULL) {
                *p = 0;
                p++;
-               password = SMB_STRDUP(p);
+               password = talloc_strdup(frame, p);
        } else {
-               password = wbinfo_prompt_pass(NULL, username);
+               password = wbinfo_prompt_pass(frame, NULL, username);
        }
 
        name = s;
@@ -1338,9 +1402,6 @@ static bool wbinfo_auth(char *username)
                         response.data.auth.error_string);
 #endif
 
-       SAFE_FREE(s);
-       SAFE_FREE(password);
-
        return WBC_ERROR_IS_OK(wbc_status);
 }
 
@@ -1358,14 +1419,15 @@ static bool wbinfo_auth_crap(char *username, bool use_ntlmv2, bool use_lanman)
        fstring name_domain;
        char *pass;
        char *p;
+       TALLOC_CTX *frame = talloc_tos();
 
        p = strchr(username, '%');
 
        if (p) {
                *p = 0;
-               pass = SMB_STRDUP(p + 1);
+               pass = talloc_strdup(frame, p + 1);
        } else {
-               pass = wbinfo_prompt_pass(NULL, username);
+               pass = wbinfo_prompt_pass(frame, NULL, username);
        }
 
        parse_wbinfo_domain_user(username, name_domain, name_user);
@@ -1389,8 +1451,9 @@ static bool wbinfo_auth_crap(char *username, bool use_ntlmv2, bool use_lanman)
                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, global_myname(),
-                                                       get_winbind_domain());
+               names_blob = NTLMv2_generate_names_blob(NULL,
+                                               get_winbind_netbios_name(),
+                                               get_winbind_domain());
 
                if (!SMBNTLMv2encrypt(NULL, name_user, name_domain, pass,
                                      &server_chal,
@@ -1398,7 +1461,7 @@ static bool wbinfo_auth_crap(char *username, bool use_ntlmv2, bool use_lanman)
                                      &lm, &nt, NULL, NULL)) {
                        data_blob_free(&names_blob);
                        data_blob_free(&server_chal);
-                       SAFE_FREE(pass);
+                       TALLOC_FREE(pass);
                        return false;
                }
                data_blob_free(&names_blob);
@@ -1445,11 +1508,11 @@ static bool wbinfo_auth_crap(char *username, bool use_ntlmv2, bool use_lanman)
 
        data_blob_free(&nt);
        data_blob_free(&lm);
-       SAFE_FREE(pass);
 
        return WBC_ERROR_IS_OK(wbc_status);
 }
 
+#ifdef WITH_FAKE_KASERVER
 /* Authenticate a user with a plaintext password and set a token */
 
 static bool wbinfo_klog(char *username)
@@ -1509,6 +1572,13 @@ static bool wbinfo_klog(char *username)
        d_printf("Successfully created AFS token\n");
        return true;
 }
+#else
+static bool wbinfo_klog(char *username)
+{
+       d_fprintf(stderr, "No AFS support compiled in.\n");
+       return false;
+}
+#endif
 
 /* Print domain users */
 
@@ -1604,9 +1674,10 @@ static bool wbinfo_change_user_password(const char *username)
        wbcErr wbc_status;
        char *old_password = NULL;
        char *new_password = NULL;
+       TALLOC_CTX *frame = talloc_tos();
 
-       old_password = wbinfo_prompt_pass("old", username);
-       new_password = wbinfo_prompt_pass("new", username);
+       old_password = wbinfo_prompt_pass(frame, "old", username);
+       new_password = wbinfo_prompt_pass(frame, "new", username);
 
        wbc_status = wbcChangeUserPassword(username, old_password,new_password);
 
@@ -1615,9 +1686,6 @@ static bool wbinfo_change_user_password(const char *username)
        d_printf("Password change for user %s %s\n", username,
                WBC_ERROR_IS_OK(wbc_status) ? "succeeded" : "failed");
 
-       SAFE_FREE(old_password);
-       SAFE_FREE(new_password);
-
        return WBC_ERROR_IS_OK(wbc_status);
 }
 
@@ -1697,6 +1765,7 @@ int main(int argc, char **argv, char **envp)
                { "remove-uid-mapping", 0, POPT_ARG_STRING, &string_arg, OPT_REMOVE_UID_MAPPING, "Remove uid to sid mapping in idmap", "UID,SID" },
                { "remove-gid-mapping", 0, POPT_ARG_STRING, &string_arg, OPT_REMOVE_GID_MAPPING, "Remove gid to sid mapping in idmap", "GID,SID" },
                { "check-secret", 't', POPT_ARG_NONE, 0, 't', "Check shared secret" },
+               { "change-secret", 'c', POPT_ARG_NONE, 0, 'c', "Change shared secret" },
                { "trusted-domains", 'm', POPT_ARG_NONE, 0, 'm', "List trusted domains" },
                { "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" },
@@ -1734,7 +1803,6 @@ int main(int argc, char **argv, char **envp)
                { "change-user-password", 0, POPT_ARG_STRING, &string_arg, OPT_CHANGE_USER_PASSWORD, "Change the password for a user", 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_CONFIGFILE
                POPT_COMMON_VERSION
                POPT_TABLEEND
        };
@@ -1759,7 +1827,7 @@ int main(int argc, char **argv, char **envp)
                /* get the generic configuration parameters like --domain */
                switch (opt) {
                case OPT_VERBOSE:
-                       verbose = True;
+                       verbose = true;
                        break;
                case OPT_NTLMV2:
                        use_ntlmv2 = true;
@@ -1772,11 +1840,6 @@ int main(int argc, char **argv, char **envp)
 
        poptFreeContext(pc);
 
-       if (!init_names())
-               return 1;
-
-       load_interfaces();
-
        pc = poptGetContext(NULL, argc, (const char **)argv, long_options,
                            POPT_CONTEXT_KEEP_FIRST);
 
@@ -1928,11 +1991,17 @@ int main(int argc, char **argv, char **envp)
                        }
                        break;
                case 't':
-                       if (!wbinfo_check_secret()) {
+                       if (!wbinfo_check_secret(opt_domain_name)) {
                                d_fprintf(stderr, "Could not check secret\n");
                                goto done;
                        }
                        break;
+               case 'c':
+                       if (!wbinfo_change_secret(opt_domain_name)) {
+                               d_fprintf(stderr, "Could not change secret\n");
+                               goto done;
+                       }
+                       break;
                case 'm':
                        if (!wbinfo_list_domains(false, verbose)) {
                                d_fprintf(stderr,
@@ -2151,7 +2220,7 @@ int main(int argc, char **argv, char **envp)
        /* Exit code */
 
  done:
-       talloc_destroy(frame);
+       talloc_free(frame);
 
        poptFreeContext(pc);
        return result;