s4:dcerpc_netlogon - unify the two workstation object lookups (DNS hostname and suppo...
[ira/wip.git] / nsswitch / wbinfo.c
index 415d0591df5e4268189f57054781f456e0331509..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;
 
-       wbc_status = wbcCheckTrustCredentials(NULL, &error);
+       if (domain) {
+               domain_name = domain;
+       } else {
+               domain_name = get_winbind_domain();
+       }
 
-       d_printf("checking the trust secret via RPC calls %s\n",
-                WBC_ERROR_IS_OK(wbc_status) ? "succeeded" : "failed");
+       wbc_status = wbcCheckTrustCredentials(domain_name, &error);
+
+       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",
@@ -1045,7 +1105,7 @@ static bool wbinfo_lookuprids(const char *domain, const char *arg)
        enum wbcSidType *types = NULL;
        size_t i;
        int num_rids;
-       uint32 *rids = NULL;
+       uint32_t *rids = NULL;
        const char *p;
        char *ridstr;
        TALLOC_CTX *mem_ctx = NULL;
@@ -1075,8 +1135,13 @@ static bool wbinfo_lookuprids(const char *domain, const char *arg)
        p = arg;
 
        while (next_token_talloc(mem_ctx, &p, &ridstr, " ,\n")) {
-               uint32 rid = strtoul(ridstr, NULL, 10);
-               ADD_TO_ARRAY(mem_ctx, uint32, rid, &rids, &num_rids);
+               uint32_t rid = strtoul(ridstr, NULL, 10);
+               rids = talloc_realloc(mem_ctx, rids, uint32_t, num_rids + 1);
+               if (rids == NULL) {
+                       d_printf("talloc_realloc failed\n");
+               }
+               rids[num_rids] = rid;
+               num_rids += 1;
        }
 
        if (rids == NULL) {
@@ -1096,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;
@@ -1146,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;
        }
@@ -1177,36 +1243,40 @@ 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 */
 
-static bool wbinfo_auth_krb5(char *username, const char *cctype, uint32 flags)
+static bool wbinfo_auth_krb5(char *username, const char *cctype, uint32_t flags)
 {
        wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
        char *s = NULL;
        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();
@@ -1240,7 +1310,7 @@ static bool wbinfo_auth_krb5(char *username, const char *cctype, uint32 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;
@@ -1288,8 +1358,6 @@ static bool wbinfo_auth_krb5(char *username, const char *cctype, uint32 flags)
        }
  done:
 
-       SAFE_FREE(s);
-       SAFE_FREE(password);
        wbcFreeMemory(params.blobs);
 
        return WBC_ERROR_IS_OK(wbc_status);
@@ -1304,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;
@@ -1333,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);
 }
 
@@ -1353,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);
@@ -1384,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,
@@ -1393,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);
@@ -1440,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)
@@ -1504,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 */
 
@@ -1569,99 +1644,15 @@ static bool print_domain_groups(const char *domain)
 
 static bool wbinfo_set_auth_user(char *username)
 {
-       const char *password;
-       char *p;
-       fstring user, domain;
-
-       /* Separate into user and password */
-
-       parse_wbinfo_domain_user(username, domain, user);
-
-       p = strchr(user, '%');
-
-       if (p != NULL) {
-               *p = 0;
-               password = p+1;
-       } else {
-               char *thepass = getpass("Password: ");
-               if (thepass) {
-                       password = thepass;
-               } else
-                       password = "";
-       }
-
-       /* Store or remove DOMAIN\username%password in secrets.tdb */
-
-       secrets_init();
-
-       if (user[0]) {
-
-               if (!secrets_store(SECRETS_AUTH_USER, user,
-                                  strlen(user) + 1)) {
-                       d_fprintf(stderr, "error storing username\n");
-                       return false;
-               }
-
-               /* We always have a domain name added by the
-                  parse_wbinfo_domain_user() function. */
-
-               if (!secrets_store(SECRETS_AUTH_DOMAIN, domain,
-                                  strlen(domain) + 1)) {
-                       d_fprintf(stderr, "error storing domain name\n");
-                       return false;
-               }
-
-       } else {
-               secrets_delete(SECRETS_AUTH_USER);
-               secrets_delete(SECRETS_AUTH_DOMAIN);
-       }
-
-       if (password[0]) {
-
-               if (!secrets_store(SECRETS_AUTH_PASSWORD, password,
-                                  strlen(password) + 1)) {
-                       d_fprintf(stderr, "error storing password\n");
-                       return false;
-               }
-
-       } else
-               secrets_delete(SECRETS_AUTH_PASSWORD);
-
-       return true;
+       d_fprintf(stderr, "This functionality was moved to the 'net' utility.\n"
+                         "See 'net help setauthuser' for details.\n");
+       return false;
 }
 
 static void wbinfo_get_auth_user(void)
 {
-       char *user, *domain, *password;
-       char separator[] = {'\0', '\0'};
-
-       /* Lift data from secrets file */
-
-       secrets_fetch_ipc_userpass(&user, &domain, &password);
-
-       if ((!user || !*user) && (!domain || !*domain ) &&
-           (!password || !*password)){
-
-               SAFE_FREE(user);
-               SAFE_FREE(domain);
-               SAFE_FREE(password);
-               d_printf("No authorised user configured\n");
-               return;
-       }
-
-       /* Pretty print authorised user info */
-
-       if (domain) {
-               separator[0] = winbind_separator();
-       }
-
-       d_printf("%s%s%s%s%s\n", domain ? domain : "",
-                separator, user,
-                password ? "%" : "", password ? password : "");
-
-       SAFE_FREE(user);
-       SAFE_FREE(domain);
-       SAFE_FREE(password);
+       d_fprintf(stderr, "This functionality was moved to the 'net' utility.\n"
+                         "See 'net help getauthuser' for details.\n");
 }
 
 static bool wbinfo_ping(void)
@@ -1683,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);
 
@@ -1694,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);
 }
 
@@ -1776,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" },
@@ -1813,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
        };
@@ -1838,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;
@@ -1851,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);
 
@@ -2007,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,
@@ -2134,11 +2124,11 @@ int main(int argc, char **argv, char **envp)
                                break;
                        }
                case 'K': {
-                               uint32 flags =  WBFLAG_PAM_KRB5 |
-                                               WBFLAG_PAM_CACHED_LOGIN |
+                               uint32_t flags = WBFLAG_PAM_KRB5 |
+                                                WBFLAG_PAM_CACHED_LOGIN |
                                                WBFLAG_PAM_FALLBACK_AFTER_KRB5 |
-                                               WBFLAG_PAM_INFO3_TEXT |
-                                               WBFLAG_PAM_CONTACT_TRUSTDOM;
+                                                WBFLAG_PAM_INFO3_TEXT |
+                                                WBFLAG_PAM_CONTACT_TRUSTDOM;
 
                                if (!wbinfo_auth_krb5(string_arg, "FILE",
                                                      flags)) {
@@ -2170,6 +2160,7 @@ int main(int argc, char **argv, char **envp)
                        break;
                case OPT_GET_AUTH_USER:
                        wbinfo_get_auth_user();
+                       goto done;
                        break;
                case OPT_GETDCNAME:
                        if (!wbinfo_getdcname(string_arg)) {
@@ -2229,7 +2220,7 @@ int main(int argc, char **argv, char **envp)
        /* Exit code */
 
  done:
-       talloc_destroy(frame);
+       talloc_free(frame);
 
        poptFreeContext(pc);
        return result;