wbinfo: prompt for password when none has been given.
[samba.git] / source / nsswitch / wbinfo.c
index 80a72903438f70387db3497686a3954bcc172c9e..2fb46c4a2f701f5cf30cd7824d1ec91989069e79 100644 (file)
@@ -297,52 +297,42 @@ static bool wbinfo_get_userdomgroups(const char *user_sid_str)
 
 /* Convert NetBIOS name to IP */
 
-static bool wbinfo_wins_byname(char *name)
+static bool wbinfo_wins_byname(const char *name)
 {
-       struct winbindd_request request;
-       struct winbindd_response response;
-
-       ZERO_STRUCT(request);
-       ZERO_STRUCT(response);
-
-       /* Send request */
-
-       fstrcpy(request.data.winsreq, name);
+       wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
+       char *ip = NULL;
 
-       if (winbindd_request_response(WINBINDD_WINS_BYNAME, &request, &response) !=
-           NSS_STATUS_SUCCESS) {
+       wbc_status = wbcResolveWinsByName(name, &ip);
+       if (!WBC_ERROR_IS_OK(wbc_status)) {
                return false;
        }
 
        /* Display response */
 
-       d_printf("%s\n", response.data.winsresp);
+       d_printf("%s\n", ip);
+
+       wbcFreeMemory(ip);
 
        return true;
 }
 
 /* Convert IP to NetBIOS name */
 
-static bool wbinfo_wins_byip(char *ip)
+static bool wbinfo_wins_byip(const char *ip)
 {
-       struct winbindd_request request;
-       struct winbindd_response response;
-
-       ZERO_STRUCT(request);
-       ZERO_STRUCT(response);
-
-       /* Send request */
-
-       fstrcpy(request.data.winsreq, ip);
+       wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
+       char *name = NULL;
 
-       if (winbindd_request_response(WINBINDD_WINS_BYIP, &request, &response) !=
-           NSS_STATUS_SUCCESS) {
+       wbc_status = wbcResolveWinsByIP(ip, &name);
+       if (!WBC_ERROR_IS_OK(wbc_status)) {
                return false;
        }
 
        /* Display response */
 
-       d_printf("%s\n", response.data.winsresp);
+       d_printf("%s\n", name);
+
+       wbcFreeMemory(name);
 
        return true;
 }
@@ -351,101 +341,67 @@ static bool wbinfo_wins_byip(char *ip)
 
 static bool wbinfo_list_domains(bool list_all_domains, bool verbose)
 {
-       struct winbindd_request request;
-       struct winbindd_response response;
-
+       struct wbcDomainInfo *domain_list = NULL;
+       size_t num_domains;
+       wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
        bool print_all = !list_all_domains && verbose;
+       int i;
 
-       ZERO_STRUCT(request);
-       ZERO_STRUCT(response);
-
-       /* Send request */
-
-       request.data.list_all_domains = list_all_domains;
-
-       if (winbindd_request_response(WINBINDD_LIST_TRUSTDOM, &request, &response) !=
-           NSS_STATUS_SUCCESS)
+       wbc_status = wbcListTrusts(&domain_list, &num_domains);
+       if (!WBC_ERROR_IS_OK(wbc_status)) {
                return false;
+       }
 
-       /* Display response */
+       if (print_all) {
+               d_printf("%-16s%-24s%-12s%-12s%-5s%-5s\n", 
+                        "Domain Name", "DNS Domain", "Trust Type", 
+                        "Transitive", "In", "Out");
+       }
 
-       if (response.extra_data.data) {
-               const char *extra_data = (char *)response.extra_data.data;
-               char *name;
-               char *beg, *end;
-               TALLOC_CTX *frame = talloc_stackframe();
+       for (i=0; i<num_domains; i++) {
+               d_printf("%-16s", domain_list[i].short_name);
 
-               if (print_all) {
-                       d_printf("%-16s%-24s%-12s%-12s%-5s%-5s\n", 
-                                "Domain Name", "DNS Domain", "Trust Type", 
-                                "Transitive", "In", "Out");
+               if (!print_all) {
+                       d_printf("\n"); 
+                       continue;
                }
 
-               while(next_token_talloc(frame,&extra_data,&name,"\n")) {
-                       /* Print Domain Name */
-                       if ((beg = strchr(name, '\\')) == NULL)
-                               goto error;
-                       *beg = 0;
-                       beg++;
-                       if ((end = strchr(beg, '\\')) == NULL)
-                               goto error;
-                       *end = 0;
-
-                       /* Print short name */
-
-                       d_printf("%-16s", name);
+               d_printf("%-24s", domain_list[i].dns_name);
 
-                       if (!print_all) {
-                               d_printf("\n"); 
-                               continue;
-                       }
+               switch(domain_list[i].trust_type) {
+               case WBC_DOMINFO_TRUSTTYPE_NONE:
+                       d_printf("None        ");
+                       break;
+               case WBC_DOMINFO_TRUSTTYPE_FOREST:              
+                       d_printf("Forest      ");
+                       break;
+               case WBC_DOMINFO_TRUSTTYPE_EXTERNAL:            
+                       d_printf("External    ");
+                       break;
+               case WBC_DOMINFO_TRUSTTYPE_IN_FOREST:
+                       d_printf("In-Forest   ");
+                       break;
+               }
 
-                       /* Print DNS domain */
+               if (domain_list[i].trust_flags & WBC_DOMINFO_TRUST_TRANSITIVE) {
+                       d_printf("Yes         ");
+               } else {
+                       d_printf("No          ");
+               }
 
-                       if (beg) {
-                               d_printf("%-24s", beg);
-                       }
+               if (domain_list[i].trust_flags & WBC_DOMINFO_TRUST_INCOMING) {
+                       d_printf("Yes  ");
+               } else {
+                       d_printf("No   ");
+               }
 
-                       /* Skip SID */
-                       beg = ++end;
-                       if ((end = strchr(beg, '\\')) == NULL)
-                               goto error;
-
-                       /* Print Trust Type */
-                       beg = ++end;
-                       if ((end = strchr(beg, '\\')) == NULL)
-                               goto error;
-                       *end = 0;
-                       d_printf("%-12s", beg);
-
-                       /* Print Transitive */
-                       beg = ++end;
-                       if ((end = strchr(beg, '\\')) == NULL)
-                               goto error;
-                       *end = 0;
-                       d_printf("%-12s", beg);
-
-                       /* Print Incoming */
-                       beg = ++end;
-                       if ((end = strchr(beg, '\\')) == NULL)
-                               goto error;
-                       *end = 0;
-                       d_printf("%-5s", beg);
-
-                       /* Print Outgoing */
-                       beg = ++end;
-                       d_printf("%-5s\n", beg);
+               if (domain_list[i].trust_flags & WBC_DOMINFO_TRUST_OUTGOING) {
+                       d_printf("Yes  ");
+               } else {
+                       d_printf("No   ");
                }
-               goto out;
 
-error:
-               d_fprintf(stderr, "Got invalid response: %s\n", extra_data);
-               TALLOC_FREE(frame);
-               SAFE_FREE(response.extra_data.data);
-               return false;
-out:
-               TALLOC_FREE(frame);
-               SAFE_FREE(response.extra_data.data);
+               d_printf("\n");
        }
 
        return true;
@@ -463,39 +419,43 @@ static bool wbinfo_list_own_domain(void)
 /* show sequence numbers */
 static bool wbinfo_show_sequence(const char *domain)
 {
-       struct winbindd_request  request;
-       struct winbindd_response response;
-
-       ZERO_STRUCT(response);
-       ZERO_STRUCT(request);
-
-       if ( domain )
-               fstrcpy( request.domain_name, domain );
+       d_printf("This command has been deprecated.  Please use the --online-status option instead.\n");
+       return false;
+}
 
-       /* Send request */
+/* show sequence numbers */
+static bool wbinfo_show_onlinestatus(const char *domain)
+{
+       struct wbcDomainInfo *domain_list = NULL;
+       size_t num_domains;
+       wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
+       int i;
 
-       if (winbindd_request_response(WINBINDD_SHOW_SEQUENCE, &request, &response) !=
-           NSS_STATUS_SUCCESS)
+       wbc_status = wbcListTrusts(&domain_list, &num_domains);
+       if (!WBC_ERROR_IS_OK(wbc_status)) {
                return false;
+       }
 
-       /* Display response */
+       for (i=0; i<num_domains; i++) {
+               bool is_offline;
 
-       if (domain) {
-               d_printf("%s : ", domain);
-               if (response.data.sequence_number == (uint32_t)-1) {
-                       d_printf("DISCONNECTED\n");
-               } else {
-                       d_printf("%d\n", response.data.sequence_number);
+               if (domain) {
+                       if (!strequal(domain_list[i].short_name, domain)) {
+                               continue;
+                       }
                }
-       } else if (response.extra_data.data) {
-               char *extra_data = (char *)response.extra_data.data;
-               d_printf("%s", extra_data);
-               SAFE_FREE(response.extra_data.data);
+
+               is_offline = (domain_list[i].domain_flags & WBC_DOMINFO_DOMAIN_OFFLINE);
+               
+               d_printf("%s : %s\n", 
+                        domain_list[i].short_name,
+                        is_offline ? "offline" : "online" );
        }
 
        return true;
 }
 
+
 /* Show domain info */
 
 static bool wbinfo_domain_info(const char *domain)
@@ -529,12 +489,12 @@ static bool wbinfo_domain_info(const char *domain)
        d_printf("SID               : %s\n", sid_str);
 
        d_printf("Active Directory  : %s\n",
-                (dinfo->flags & WBC_DOMINFO_AD) ? "Yes" : "No");
+                (dinfo->domain_flags & WBC_DOMINFO_DOMAIN_AD) ? "Yes" : "No");
        d_printf("Native            : %s\n",
-                (dinfo->flags & WBC_DOMINFO_NATIVE) ? "Yes" : "No");
+                (dinfo->domain_flags & WBC_DOMINFO_DOMAIN_NATIVE) ? "Yes" : "No");
 
        d_printf("Primary           : %s\n",
-                (dinfo->flags & WBC_DOMINFO_PRIMARY) ? "Yes" : "No");
+                (dinfo->domain_flags & WBC_DOMINFO_DOMAIN_PRIMARY) ? "Yes" : "No");
 
        wbcFreeMemory(sid_str);
        wbcFreeMemory(dinfo);
@@ -601,22 +561,24 @@ static bool wbinfo_dsgetdcname(const char *domain_name, uint32_t flags)
 
 static bool wbinfo_check_secret(void)
 {
-       struct winbindd_response response;
-       NSS_STATUS result;
-
-       ZERO_STRUCT(response);
+       wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
+       struct wbcAuthErrorInfo *error = NULL;
 
-       result = winbindd_request_response(WINBINDD_CHECK_MACHACC, NULL, &response);
+       wbc_status = wbcCheckTrustCredentials(NULL, &error);
 
        d_printf("checking the trust secret via RPC calls %s\n",
-                (result == NSS_STATUS_SUCCESS) ? "succeeded" : "failed");
+                WBC_ERROR_IS_OK(wbc_status) ? "succeeded" : "failed");
 
-       if (result != NSS_STATUS_SUCCESS)
+       if (wbc_status == WBC_ERR_AUTH_ERROR) {
                d_fprintf(stderr, "error code was %s (0x%x)\n",
-                        response.data.auth.nt_status_string,
-                        response.data.auth.nt_status);
+                         error->nt_string, error->nt_status);
+               wbcFreeMemory(error);
+       }
+       if (!WBC_ERROR_IS_OK(wbc_status)) {
+               return false;
+       }
 
-       return result == NSS_STATUS_SUCCESS;    
+       return true;
 }
 
 /* Convert uid to sid */
@@ -811,7 +773,7 @@ static bool wbinfo_lookuprids(const char *domain, const char *arg)
        uint32 *rids = NULL;
        const char *p;
        char *ridstr;
-       TALLOC_CTX *mem_ctx;
+       TALLOC_CTX *mem_ctx = NULL;
        bool ret = false;
 
        if ((domain == NULL) || (strequal(domain, ".")) || (domain[0] == '\0')) {
@@ -996,7 +958,14 @@ static bool wbinfo_auth(char *username)
                p++;
                password = p;
        } else {
-               password = "";
+               char *prompt;
+               asprintf(&prompt, "Enter %s's password:", username);
+               if (!prompt) {
+                       return false;
+               }
+
+               password = getpass(prompt);
+               SAFE_FREE(prompt);
        }
 
        name = s;
@@ -1039,6 +1008,16 @@ static bool wbinfo_auth_crap(char *username)
        if (p) {
                *p = 0;
                fstrcpy(pass, p + 1);
+       } else {
+               char *prompt;
+               asprintf(&prompt, "Enter %s's password:", username);
+               if (!prompt) {
+                       return false;
+               }
+
+               fstrcpy(pass, getpass(prompt));
+               SAFE_FREE(prompt);
+
        }
                
        parse_wbinfo_domain_user(username, name_domain, name_user);
@@ -1359,7 +1338,8 @@ enum {
        OPT_LIST_OWN_DOMAIN,
        OPT_UID_INFO,
        OPT_GROUP_INFO,
-       OPT_VERBOSE
+       OPT_VERBOSE,
+       OPT_ONLINESTATUS
 };
 
 int main(int argc, char **argv, char **envp)
@@ -1399,6 +1379,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, "Show sequence numbers of all domains" },
+               { "online-status", 0, POPT_ARG_NONE, 0, OPT_ONLINESTATUS, "Show whether domains are marked as online or offline"},
                { "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" },
@@ -1571,6 +1552,12 @@ int main(int argc, char **argv, char **envp)
                                goto done;
                        }
                        break;
+               case OPT_ONLINESTATUS:
+                       if (!wbinfo_show_onlinestatus(opt_domain_name)) {
+                               d_fprintf(stderr, "Could not show online-status\n");
+                               goto done;
+                       }
+                       break;
                case 'D':
                        if (!wbinfo_domain_info(string_arg)) {
                                d_fprintf(stderr, "Could not get domain info\n");