s3-net: Check return value of string_to_sid().
[nivanova/samba-autobuild/.git] / source3 / utils / net_rpc.c
index 37f088dae8b33254179ca60a6088ff70435db2fb..2ccc92876bc2a43c1102d991d6cf71ad2b401ac0 100644 (file)
@@ -43,7 +43,7 @@
 #include "libsmb/clirap.h"
 #include "nsswitch/libwbclient/wbclient.h"
 #include "passdb.h"
-#include "libcli/smb/smbXcli_base.h"
+#include "../libcli/smb/smbXcli_base.h"
 
 static int net_mode_share;
 static NTSTATUS sync_files(struct copy_clistate *cp_clistate, const char *mask);
@@ -886,13 +886,20 @@ static int rpc_user_password(struct net_context *c, int argc, const char **argv)
        if (argv[1]) {
                u1003.usri1003_password = argv[1];
        } else {
+               char pwd[256] = {0};
                ret = asprintf(&prompt, _("Enter new password for %s:"),
                               argv[0]);
                if (ret == -1) {
                        return -1;
                }
-               u1003.usri1003_password = talloc_strdup(c, getpass(prompt));
+
+               ret = samba_getpass(prompt, pwd, sizeof(pwd), false, false);
                SAFE_FREE(prompt);
+               if (ret < 0) {
+                       return -1;
+               }
+
+               u1003.usri1003_password = talloc_strdup(c, pwd);
                if (u1003.usri1003_password == NULL) {
                        return -1;
                }
@@ -4675,6 +4682,7 @@ static bool get_user_sids(const char *domain, const char *user, struct security_
        for (i = 0; i < num_groups; i++) {
                gid_t gid = groups[i];
                struct dom_sid sid;
+               bool ok;
 
                wbc_status = wbcGidToSid(gid, &wsid);
                if (!WBC_ERROR_IS_OK(wbc_status)) {
@@ -4688,7 +4696,12 @@ static bool get_user_sids(const char *domain, const char *user, struct security_
 
                DEBUG(3, (" %s\n", sid_str));
 
-               string_to_sid(&sid, sid_str);
+               ok = string_to_sid(&sid, sid_str);
+               if (!ok) {
+                       DEBUG(1, ("Failed to convert string to SID\n"));
+                       wbcFreeMemory(groups);
+                       return false;
+               }
                add_sid_to_token(token, &sid);
        }
        wbcFreeMemory(groups);
@@ -4750,7 +4763,11 @@ static bool get_user_tokens(struct net_context *c, int *num_tokens,
                } else {
                        *p++ = '\0';
                        fstrcpy(domain, users[i]);
-                       strupper_m(domain);
+                       if (!strupper_m(domain)) {
+                               DEBUG(1, ("strupper_m %s failed\n", domain));
+                               wbcFreeMemory(users);
+                               return false;
+                       }
                        fstrcpy(user, p);
                }
 
@@ -4913,28 +4930,6 @@ static void show_userlist(struct rpc_pipe_client *pipe_hnd,
        return;
 }
 
-struct share_list {
-       int num_shares;
-       char **shares;
-};
-
-static void collect_share(const char *name, uint32 m,
-                         const char *comment, void *state)
-{
-       struct share_list *share_list = (struct share_list *)state;
-
-       if (m != STYPE_DISKTREE)
-               return;
-
-       share_list->num_shares += 1;
-       share_list->shares = SMB_REALLOC_ARRAY(share_list->shares, char *, share_list->num_shares);
-       if (!share_list->shares) {
-               share_list->num_shares = 0;
-               return;
-       }
-       share_list->shares[share_list->num_shares-1] = SMB_STRDUP(name);
-}
-
 /**
  * List shares on a remote RPC server, including the security descriptors.
  *
@@ -4960,16 +4955,21 @@ static NTSTATUS rpc_share_allowedusers_internals(struct net_context *c,
                                                int argc,
                                                const char **argv)
 {
-       int ret;
        bool r;
-       uint32 i;
        FILE *f;
+       NTSTATUS nt_status = NT_STATUS_OK;
+       uint32_t total_entries = 0;
+       uint32_t resume_handle = 0;
+       uint32_t preferred_len = 0xffffffff;
+       uint32_t i;
+       struct dcerpc_binding_handle *b = NULL;
+       struct srvsvc_NetShareInfoCtr info_ctr;
+       struct srvsvc_NetShareCtr1 ctr1;
+       WERROR result;
 
        struct user_token *tokens = NULL;
        int num_tokens = 0;
 
-       struct share_list share_list;
-
        if (argc == 0) {
                f = stdin;
        } else {
@@ -4994,22 +4994,47 @@ static NTSTATUS rpc_share_allowedusers_internals(struct net_context *c,
        for (i=0; i<num_tokens; i++)
                collect_alias_memberships(&tokens[i].token);
 
-       share_list.num_shares = 0;
-       share_list.shares = NULL;
+       ZERO_STRUCT(info_ctr);
+       ZERO_STRUCT(ctr1);
+
+       info_ctr.level = 1;
+       info_ctr.ctr.ctr1 = &ctr1;
+
+       b = pipe_hnd->binding_handle;
+
+       /* Issue the NetShareEnum RPC call and retrieve the response */
+       nt_status = dcerpc_srvsvc_NetShareEnumAll(b,
+                                       talloc_tos(),
+                                       pipe_hnd->desthost,
+                                       &info_ctr,
+                                       preferred_len,
+                                       &total_entries,
+                                       &resume_handle,
+                                       &result);
 
-       ret = cli_RNetShareEnum(cli, collect_share, &share_list);
+       /* Was it successful? */
+       if (!NT_STATUS_IS_OK(nt_status)) {
+               /*  Nope.  Go clean up. */
+               goto done;
+       }
 
-       if (ret == -1) {
-               DEBUG(0, ("Error returning browse list: %s\n",
-                         cli_errstr(cli)));
+       if (!W_ERROR_IS_OK(result)) {
+               /*  Nope.  Go clean up. */
+               nt_status = werror_to_ntstatus(result);
                goto done;
        }
 
-       for (i = 0; i < share_list.num_shares; i++) {
-               char *netname = share_list.shares[i];
+       if (total_entries == 0) {
+               goto done;
+       }
 
-               if (netname[strlen(netname)-1] == '$')
+        /* For each returned entry... */
+       for (i = 0; i < info_ctr.ctr.ctr1->count; i++) {
+               const char *netname = info_ctr.ctr.ctr1->array[i].name;
+
+               if (info_ctr.ctr.ctr1->array[i].type != STYPE_DISKTREE) {
                        continue;
+               }
 
                d_printf("%s\n", netname);
 
@@ -5021,9 +5046,8 @@ static NTSTATUS rpc_share_allowedusers_internals(struct net_context *c,
                free_user_token(&tokens[i].token);
        }
        SAFE_FREE(tokens);
-       SAFE_FREE(share_list.shares);
 
-       return NT_STATUS_OK;
+       return nt_status;
 }
 
 static int rpc_share_allowedusers(struct net_context *c, int argc,
@@ -5776,6 +5800,7 @@ static NTSTATUS rpc_trustdom_add_internals(struct net_context *c,
        union samr_UserInfo info;
        unsigned int orig_timeout;
        struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
+       DATA_BLOB session_key = data_blob_null;
 
        if (argc != 2) {
                d_printf("%s\n%s",
@@ -5793,10 +5818,20 @@ static NTSTATUS rpc_trustdom_add_internals(struct net_context *c,
                return NT_STATUS_NO_MEMORY;
        }
 
-       strupper_m(acct_name);
+       if (!strupper_m(acct_name)) {
+               SAFE_FREE(acct_name);
+               return NT_STATUS_INVALID_PARAMETER;
+       }
 
        init_lsa_String(&lsa_acct_name, acct_name);
 
+       status = cli_get_session_key(mem_ctx, pipe_hnd, &session_key);
+       if (!NT_STATUS_IS_OK(status)) {
+               DEBUG(0,("Error getting session_key of SAM pipe. Error was %s\n",
+                       nt_errstr(status)));
+               goto done;
+       }
+
        /* Get samr policy handle */
        status = dcerpc_samr_Connect2(b, mem_ctx,
                                      pipe_hnd->desthost,
@@ -5867,7 +5902,7 @@ static NTSTATUS rpc_trustdom_add_internals(struct net_context *c,
                ZERO_STRUCT(info.info23);
 
                init_samr_CryptPassword(argv[1],
-                                       &cli->user_session_key,
+                                       &session_key,
                                        &crypt_pwd);
 
                info.info23.info.fields_present = SAMR_FIELD_ACCT_FLAGS |
@@ -5894,6 +5929,7 @@ static NTSTATUS rpc_trustdom_add_internals(struct net_context *c,
 
  done:
        SAFE_FREE(acct_name);
+       data_blob_clear_free(&session_key);
        return status;
 }
 
@@ -5969,7 +6005,10 @@ static NTSTATUS rpc_trustdom_del_internals(struct net_context *c,
        if (acct_name == NULL)
                return NT_STATUS_NO_MEMORY;
 
-       strupper_m(acct_name);
+       if (!strupper_m(acct_name)) {
+               TALLOC_FREE(acct_name);
+               return NT_STATUS_INVALID_PARAMETER;
+       }
 
        /* Get samr policy handle */
        status = dcerpc_samr_Connect2(b, mem_ctx,
@@ -6215,13 +6254,20 @@ static int rpc_trustdom_establish(struct net_context *c, int argc,
        }
 
        domain_name = smb_xstrdup(argv[0]);
-       strupper_m(domain_name);
+       if (!strupper_m(domain_name)) {
+               SAFE_FREE(domain_name);
+               return -1;
+       }
 
        /* account name used at first is our domain's name with '$' */
        if (asprintf(&acct_name, "%s$", lp_workgroup()) == -1) {
                return -1;
        }
-       strupper_m(acct_name);
+       if (!strupper_m(acct_name)) {
+               SAFE_FREE(domain_name);
+               SAFE_FREE(acct_name);
+               return -1;
+       }
 
        /*
         * opt_workgroup will be used by connection functions further,
@@ -6393,7 +6439,10 @@ static int rpc_trustdom_revoke(struct net_context *c, int argc,
 
        /* generate upper cased domain name */
        domain_name = smb_xstrdup(argv[0]);
-       strupper_m(domain_name);
+       if (!strupper_m(domain_name)) {
+               SAFE_FREE(domain_name);
+               return -1;
+       }
 
        /* delete password of the trust */
        if (!pdb_del_trusteddom_pw(domain_name)) {
@@ -6962,7 +7011,11 @@ static int rpc_trustdom_list(struct net_context *c, int argc, const char **argv)
                                str[ascii_dom_name_len - 1] = '\0';
 
                        /* set opt_* variables to remote domain */
-                       strupper_m(str);
+                       if (!strupper_m(str)) {
+                               cli_shutdown(cli);
+                               talloc_destroy(mem_ctx);
+                               return -1;
+                       }
                        c->opt_workgroup = talloc_strdup(mem_ctx, str);
                        c->opt_target_workgroup = c->opt_workgroup;
 
@@ -7105,10 +7158,11 @@ bool net_rpc_check(struct net_context *c, unsigned flags)
        if (!NT_STATUS_IS_OK(status)) {
                return false;
        }
-       status = cli_negprot(cli, PROTOCOL_NT1);
+       status = smbXcli_negprot(cli->conn, cli->timeout, PROTOCOL_CORE,
+                                PROTOCOL_NT1);
        if (!NT_STATUS_IS_OK(status))
                goto done;
-       if (cli_state_protocol(cli) < PROTOCOL_NT1)
+       if (smbXcli_conn_protocol(cli->conn) < PROTOCOL_NT1)
                goto done;
 
        ret = true;