libreplace: Fix the build on Solaris
[sfrench/samba-autobuild/.git] / nsswitch / wbinfo.c
index 8b178598edc66d0e708e080dbfcc0bbaff4f8f14..f45d7b583351cab5d7da0d2ff03dbd9390ff4035 100644 (file)
 #include "includes.h"
 #include "winbind_client.h"
 #include "libwbclient/wbclient.h"
-#include "lib/popt/popt.h"
 #include "../libcli/auth/libcli_auth.h"
-#if (_SAMBA_BUILD_) >= 4
 #include "lib/cmdline/popt_common.h"
-#else
-#include "popt_common.h"
-#endif
+#include "lib/afs/afs_settoken.h"
 
 #ifdef DBGC_CLASS
 #undef DBGC_CLASS
@@ -692,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;
@@ -756,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)) {
@@ -815,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)) {
@@ -830,21 +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;
        }
@@ -1226,8 +1232,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);
@@ -1328,13 +1338,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++) {
@@ -1395,11 +1406,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);
@@ -1556,8 +1584,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);
        }
@@ -1728,7 +1757,11 @@ 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)\n"
+                        "error message was: %s\n",
+                        name_domain,
+                        winbind_separator(),
+                        name_user,
                         err->nt_string,
                         err->nt_status,
                         err->display_string);
@@ -1745,10 +1778,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;
@@ -1793,7 +1827,45 @@ 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;
+
+               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);
+               }
+
+               wbcFreeMemory(info);
+               info = NULL;
+       }
 
        wbcFreeMemory(params.blobs);
 
@@ -1802,7 +1874,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);
@@ -1880,7 +1954,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 */
@@ -1930,9 +2004,16 @@ static bool print_domain_users(const char *domain)
 
        /* Send request to winbind daemon */
 
-       /* '.' is the special sign for our own domain */
-       if (domain && strcmp(domain, ".") == 0) {
+       if (domain == NULL) {
                domain = get_winbind_domain();
+       } else {
+               /* '.' is the special sign for our own domain */
+               if ((domain[0] == '\0') || strcmp(domain, ".") == 0) {
+                       domain = get_winbind_domain();
+               /* '*' is the special sign for all domains */
+               } else if (strcmp(domain, "*") == 0) {
+                       domain = NULL;
+               }
        }
 
        wbc_status = wbcListUsers(domain, &num_users, &users);
@@ -1960,9 +2041,16 @@ static bool print_domain_groups(const char *domain)
 
        /* Send request to winbind daemon */
 
-       /* '.' is the special sign for our own domain */
-       if (domain && strcmp(domain, ".") == 0) {
+       if (domain == NULL) {
                domain = get_winbind_domain();
+       } else {
+               /* '.' is the special sign for our own domain */
+               if ((domain[0] == '\0') || strcmp(domain, ".") == 0) {
+                       domain = get_winbind_domain();
+               /* '*' is the special sign for all domains */
+               } else if (strcmp(domain, "*") == 0) {
+                       domain = NULL;
+               }
        }
 
        wbc_status = wbcListGroups(domain, &num_groups, &groups);
@@ -2068,10 +2156,11 @@ enum {
        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();
@@ -2087,6 +2176,7 @@ int main(int argc, char **argv, char **envp)
        bool use_lanman = false;
        char *logoff_user = getenv("USER");
        int logoff_uid = geteuid();
+       const char *opt_krb5ccname = "FILE";
 
        struct poptOption long_options[] = {
                POPT_AUTOHELP
@@ -2168,6 +2258,7 @@ 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, 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 },
@@ -2179,12 +2270,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 */
@@ -2388,7 +2479,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;
@@ -2514,7 +2605,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;
@@ -2537,13 +2628,13 @@ int main(int argc, char **argv, char **envp)
                                                 WBFLAG_PAM_INFO3_TEXT |
                                                 WBFLAG_PAM_CONTACT_TRUSTDOM;
 
-                               if (!wbinfo_auth_krb5(string_arg, "FILE",
+                               if (!wbinfo_auth_krb5(string_arg, opt_krb5ccname,
                                                      flags)) {
                                        d_fprintf(stderr,
                                                "Could not authenticate user "
                                                "[%s] with Kerberos "
                                                "(ccache: %s)\n", string_arg,
-                                               "FILE");
+                                               opt_krb5ccname);
                                        goto done;
                                }
                                break;
@@ -2623,6 +2714,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");