This commit was manufactured by cvs2svn to create branch 'SAMBA_3_0'.(This used to...
[ira/wip.git] / source3 / rpc_client / cli_samr.c
index f0035ca22ea6e727c05bf9e399972688f3d0a713..4fe8fba7b0b63e6efc320a441fc587b7af255739 100644 (file)
@@ -561,8 +561,8 @@ NTSTATUS cli_samr_enum_dom_users(struct cli_state *cli, TALLOC_CTX *mem_ctx,
                                  uint32 size, char ***dom_users, uint32 **rids,
                                  uint32 *num_dom_users)
 {
-       prs_struct qdata;
-       prs_struct rdata;
+       prs_struct qbuf;
+       prs_struct rbuf;
        SAMR_Q_ENUM_DOM_USERS q;
        SAMR_R_ENUM_DOM_USERS r;
        NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
@@ -571,51 +571,36 @@ NTSTATUS cli_samr_enum_dom_users(struct cli_state *cli, TALLOC_CTX *mem_ctx,
        ZERO_STRUCT(q);
        ZERO_STRUCT(r);
        
-       if (cli == NULL || pol == NULL)
-               return result;
+       /* always init this */
+       *num_dom_users = 0;
        
-       /* initialise parse structures */
-       prs_init(&qdata, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
-       prs_init(&rdata, 0, mem_ctx, UNMARSHALL);
-       
-       DEBUG(4, ("SAMR Enum Domain Users. start_idx: %d, acb: %d, size: %d\n",
-                       *start_idx, acb_mask, size));
+       /* Initialise parse structures */
+
+       prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
+       prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
        
-       /* fill query structure with parameters */
+       /* Fill query structure with parameters */
+
        init_samr_q_enum_dom_users(&q, pol, *start_idx, acb_mask, 0, size);
        
-       /* prepare query stream */
-       if (!samr_io_q_enum_dom_users("", &q, &qdata, 0)) {
-               prs_mem_free(&qdata);
-               prs_mem_free(&rdata);
-               return result;
-       }
-       
-       /* send rpc call over the pipe */
-       if (!rpc_api_pipe_req(cli, SAMR_ENUM_DOM_USERS, &qdata, &rdata)) {
-               prs_mem_free(&qdata);
-               prs_mem_free(&rdata);
-               return result;
+       if (!samr_io_q_enum_dom_users("", &q, &qbuf, 0) ||
+           !rpc_api_pipe_req(cli, SAMR_ENUM_DOM_USERS, &qbuf, &rbuf)) {
+               goto done;
        }
-               
+
        /* unpack received stream */
-       if(!samr_io_r_enum_dom_users("", &r, &rdata, 0)) {
-               prs_mem_free(&qdata);
-               prs_mem_free(&rdata);
-               result = r.status;
-               return result;
-       }
+
+       if(!samr_io_r_enum_dom_users("", &r, &rbuf, 0))
+               goto done;
        
-       /* return the data obtained in response */
-       if (!NT_STATUS_IS_OK(r.status) &&
-               (NT_STATUS_EQUAL(r.status, STATUS_MORE_ENTRIES) ||
-               NT_STATUS_EQUAL(r.status, NT_STATUS_NO_MORE_ENTRIES))) {
-               return r.status;
-       }
+       result = r.status;
+
+       if (!NT_STATUS_IS_OK(result) &&
+           NT_STATUS_V(result) != NT_STATUS_V(STATUS_MORE_ENTRIES))
+               goto done;
        
        *start_idx = r.next_idx;
        *num_dom_users = r.num_entries2;
-       result = r.status;
 
        if (r.num_entries2) {
                /* allocate memory needed to return received data */    
@@ -641,13 +626,13 @@ NTSTATUS cli_samr_enum_dom_users(struct cli_state *cli, TALLOC_CTX *mem_ctx,
                }
        }
        
-       prs_mem_free(&qdata);
-       prs_mem_free(&rdata);
+done:
+       prs_mem_free(&qbuf);
+       prs_mem_free(&rbuf);
        
        return result;
 };
 
-
 /* Enumerate domain groups */
 
 NTSTATUS cli_samr_enum_dom_groups(struct cli_state *cli, TALLOC_CTX *mem_ctx, 
@@ -692,13 +677,16 @@ NTSTATUS cli_samr_enum_dom_groups(struct cli_state *cli, TALLOC_CTX *mem_ctx,
 
        *num_dom_groups = r.num_entries2;
 
+       if (*num_dom_groups == 0)
+               goto done;
+
        if (!((*dom_groups) = (struct acct_info *)
              talloc(mem_ctx, sizeof(struct acct_info) * *num_dom_groups))) {
-               result = NT_STATUS_UNSUCCESSFUL;
+               result = NT_STATUS_NO_MEMORY;
                goto done;
        }
 
-       memset(*dom_groups, 0, sizeof(struct acct_info) * *num_dom_groups);
+       memset(*dom_groups, 0, sizeof(struct acct_info) * (*num_dom_groups));
 
        name_idx = 0;
 
@@ -727,8 +715,8 @@ NTSTATUS cli_samr_enum_dom_groups(struct cli_state *cli, TALLOC_CTX *mem_ctx,
 
 NTSTATUS cli_samr_enum_als_groups(struct cli_state *cli, TALLOC_CTX *mem_ctx, 
                                   POLICY_HND *pol, uint32 *start_idx, 
-                                  uint32 size, struct acct_info **dom_groups,
-                                  uint32 *num_dom_groups)
+                                  uint32 size, struct acct_info **dom_aliases,
+                                  uint32 *num_dom_aliases)
 {
        prs_struct qbuf, rbuf;
        SAMR_Q_ENUM_DOM_ALIASES q;
@@ -768,24 +756,27 @@ NTSTATUS cli_samr_enum_als_groups(struct cli_state *cli, TALLOC_CTX *mem_ctx,
                goto done;
        }
 
-       *num_dom_groups = r.num_entries2;
+       *num_dom_aliases = r.num_entries2;
 
-       if (!((*dom_groups) = (struct acct_info *)
-             talloc(mem_ctx, sizeof(struct acct_info) * *num_dom_groups))) {
-               result = NT_STATUS_UNSUCCESSFUL;
+       if (*num_dom_aliases == 0)
+               goto done;
+
+       if (!((*dom_aliases) = (struct acct_info *)
+             talloc(mem_ctx, sizeof(struct acct_info) * *num_dom_aliases))) {
+               result = NT_STATUS_NO_MEMORY;
                goto done;
        }
 
-       memset(*dom_groups, 0, sizeof(struct acct_info) * *num_dom_groups);
+       memset(*dom_aliases, 0, sizeof(struct acct_info) * *num_dom_aliases);
 
        name_idx = 0;
 
-       for (i = 0; i < *num_dom_groups; i++) {
+       for (i = 0; i < *num_dom_aliases; i++) {
 
-               (*dom_groups)[i].rid = r.sam[i].rid;
+               (*dom_aliases)[i].rid = r.sam[i].rid;
 
                if (r.sam[i].hdr_name.buffer) {
-                       unistr2_to_ascii((*dom_groups)[i].acct_name,
+                       unistr2_to_ascii((*dom_aliases)[i].acct_name,
                                         &r.uni_grp_name[name_idx],
                                         sizeof(fstring) - 1);
                        name_idx++;
@@ -1009,6 +1000,8 @@ NTSTATUS cli_samr_query_dispinfo(struct cli_state *cli, TALLOC_CTX *mem_ctx,
        ZERO_STRUCT(q);
        ZERO_STRUCT(r);
 
+       *num_entries = 0;
+
        /* Initialise parse structures */
 
        prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
@@ -1482,3 +1475,49 @@ NTSTATUS cli_samr_get_dom_pwinfo(struct cli_state *cli, TALLOC_CTX *mem_ctx,
 
        return result;
 }
+
+/* Lookup Domain Name */
+
+NTSTATUS cli_samr_lookup_domain(struct cli_state *cli, TALLOC_CTX *mem_ctx,
+                               POLICY_HND *user_pol, char *domain_name, 
+                               DOM_SID *sid)
+{
+       prs_struct qbuf, rbuf;
+       SAMR_Q_LOOKUP_DOMAIN q;
+       SAMR_R_LOOKUP_DOMAIN r;
+       NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
+
+       ZERO_STRUCT(q);
+       ZERO_STRUCT(r);
+
+       /* Initialise parse structures */
+
+       prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
+       prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
+
+       /* Marshall data and send request */
+
+       init_samr_q_lookup_domain(&q, user_pol, domain_name);
+
+       if (!samr_io_q_lookup_domain("", &q, &qbuf, 0) ||
+           !rpc_api_pipe_req(cli, SAMR_LOOKUP_DOMAIN, &qbuf, &rbuf))
+               goto done;
+
+       /* Unmarshall response */
+
+       if (!samr_io_r_lookup_domain("", &r, &rbuf, 0))
+               goto done;
+
+       /* Return output parameters */
+
+       result = r.status;
+
+       if (NT_STATUS_IS_OK(result))
+               sid_copy(sid, &r.dom_sid.sid);
+
+ done:
+       prs_mem_free(&qbuf);
+       prs_mem_free(&rbuf);
+
+       return result;
+}