torture/attr: use security_ace_equal instead of sec_ace_equal
[samba.git] / nsswitch / wbinfo.c
index aee4004e57d25c0814d3030444b69d4e95c5bb7a..bc25a1722f7ed592024e787fc396715d3a426e39 100644 (file)
 #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
 
 #ifdef DBGC_CLASS
 #undef DBGC_CLASS
@@ -121,7 +117,8 @@ static bool parse_wbinfo_domain_user(const char *domuser, fstring domain,
 
        if (!p) {
                /* Maybe it was a UPN? */
-               if ((p = strchr(domuser, '@')) != NULL) {
+               p = strchr(domuser, '@');
+               if (p != NULL) {
                        fstrcpy(domain, "");
                        fstrcpy(user, domuser);
                        return true;
@@ -242,6 +239,8 @@ static bool wbinfo_get_user_sidinfo(const char *sid_str)
                 pwd->pw_dir,
                 pwd->pw_shell);
 
+       wbcFreeMemory(pwd);
+
        return true;
 }
 
@@ -839,6 +838,7 @@ static bool wbinfo_ping_dc(void)
                 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);
@@ -1223,8 +1223,15 @@ 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);
 
        return true;
 }
@@ -1260,6 +1267,9 @@ static bool wbinfo_lookupsid_fullname(const char *sid_str)
        d_printf("%s%c%s %d\n",
                 domain, winbind_separator(), name, type);
 
+       wbcFreeMemory(domain);
+       wbcFreeMemory(name);
+
        return true;
 }
 
@@ -1386,11 +1396,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);
@@ -1435,7 +1462,8 @@ static char *wbinfo_prompt_pass(TALLOC_CTX *mem_ctx,
                                const char *username)
 {
        char *prompt;
-       const char *ret = NULL;
+       char buf[1024] = {0};
+       int rc;
 
        prompt = talloc_asprintf(mem_ctx, "Enter %s's ", username);
        if (!prompt) {
@@ -1452,10 +1480,13 @@ static char *wbinfo_prompt_pass(TALLOC_CTX *mem_ctx,
                return NULL;
        }
 
-       ret = getpass(prompt);
+       rc = samba_getpass(prompt, buf, sizeof(buf), false, false);
        TALLOC_FREE(prompt);
+       if (rc < 0) {
+               return NULL;
+       }
 
-       return talloc_strdup(mem_ctx, ret);
+       return talloc_strdup(mem_ctx, buf);
 }
 
 /* Authenticate a user with a plaintext password */
@@ -1736,7 +1767,7 @@ static bool wbinfo_pam_logon(char *username)
 {
        wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
        struct wbcLogonUserParams params;
-       struct wbcAuthErrorInfo *error;
+       struct wbcAuthErrorInfo *error = NULL;
        char *s = NULL;
        char *p = NULL;
        TALLOC_CTX *frame = talloc_tos();
@@ -1787,16 +1818,15 @@ static bool wbinfo_pam_logon(char *username)
        d_printf("plaintext password authentication %s\n",
                 WBC_ERROR_IS_OK(wbc_status) ? "succeeded" : "failed");
 
-       if (!WBC_ERROR_IS_OK(wbc_status)) {
+       if (!WBC_ERROR_IS_OK(wbc_status) && (error != NULL)) {
                d_fprintf(stderr,
                          "error code was %s (0x%x)\nerror message was: %s\n",
                          error->nt_string,
                          (int)error->nt_status,
                          error->display_string);
                wbcFreeMemory(error);
-               return false;
        }
-       return true;
+       return WBC_ERROR_IS_OK(wbc_status);
 }
 
 /* Save creds with winbind */
@@ -1860,7 +1890,10 @@ static bool wbinfo_klog(char *username)
                *p = '%';
        } else {
                fstrcpy(request.data.auth.user, username);
-               fstrcpy(request.data.auth.pass, getpass("Password: "));
+               (void) samba_getpass("Password: ",
+                                    request.data.auth.pass,
+                                    sizeof(request.data.auth.pass),
+                                    false, false);
        }
 
        request.flags |= WBFLAG_PAM_AFS_TOKEN;
@@ -1915,9 +1948,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);
@@ -1945,9 +1985,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);
@@ -2053,7 +2100,8 @@ enum {
        OPT_LOGOFF,
        OPT_LOGOFF_USER,
        OPT_LOGOFF_UID,
-       OPT_LANMAN
+       OPT_LANMAN,
+       OPT_KRB5CCNAME
 };
 
 int main(int argc, char **argv, char **envp)
@@ -2072,6 +2120,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
@@ -2153,6 +2202,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 },
@@ -2522,13 +2572,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;
@@ -2608,6 +2658,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");