s3-netapi: Fix Coverity #776 (REVERSE_INULL).
[ira/wip.git] / source3 / lib / netapi / group.c
index 2e0c4dc48efce311c24e0bda03d6aad15bf6ac1e..c09632a8570b08c925e5e506d0e850ecd3b7b82d 100644 (file)
 WERROR NetGroupAdd_r(struct libnetapi_ctx *ctx,
                     struct NetGroupAdd *r)
 {
-       struct cli_state *cli = NULL;
        struct rpc_pipe_client *pipe_cli = NULL;
        NTSTATUS status;
        WERROR werr;
-       POLICY_HND connect_handle, domain_handle, group_handle;
+       struct policy_handle connect_handle, domain_handle, group_handle;
        struct lsa_String lsa_group_name;
        struct dom_sid2 *domain_sid = NULL;
        uint32_t rid = 0;
@@ -73,7 +72,6 @@ WERROR NetGroupAdd_r(struct libnetapi_ctx *ctx,
 
        werr = libnetapi_open_pipe(ctx, r->in.server_name,
                                   &ndr_table_samr.syntax_id,
-                                  &cli,
                                   &pipe_cli);
        if (!W_ERROR_IS_OK(werr)) {
                goto done;
@@ -195,10 +193,6 @@ WERROR NetGroupAdd_r(struct libnetapi_ctx *ctx,
                                      &group_handle);
 
  done:
-       if (!cli) {
-               return werr;
-       }
-
        if (is_valid_policy_hnd(&group_handle)) {
                rpccli_samr_Close(pipe_cli, ctx, &group_handle);
        }
@@ -226,11 +220,10 @@ WERROR NetGroupAdd_l(struct libnetapi_ctx *ctx,
 WERROR NetGroupDel_r(struct libnetapi_ctx *ctx,
                     struct NetGroupDel *r)
 {
-       struct cli_state *cli = NULL;
        struct rpc_pipe_client *pipe_cli = NULL;
        NTSTATUS status;
        WERROR werr;
-       POLICY_HND connect_handle, domain_handle, group_handle;
+       struct policy_handle connect_handle, domain_handle, group_handle;
        struct lsa_String lsa_group_name;
        struct dom_sid2 *domain_sid = NULL;
        int i = 0;
@@ -250,7 +243,6 @@ WERROR NetGroupDel_r(struct libnetapi_ctx *ctx,
 
        werr = libnetapi_open_pipe(ctx, r->in.server_name,
                                   &ndr_table_samr.syntax_id,
-                                  &cli,
                                   &pipe_cli);
        if (!W_ERROR_IS_OK(werr)) {
                goto done;
@@ -362,10 +354,6 @@ WERROR NetGroupDel_r(struct libnetapi_ctx *ctx,
        werr = WERR_OK;
 
  done:
-       if (!cli) {
-               return werr;
-       }
-
        if (is_valid_policy_hnd(&group_handle)) {
                rpccli_samr_Close(pipe_cli, ctx, &group_handle);
        }
@@ -393,11 +381,10 @@ WERROR NetGroupDel_l(struct libnetapi_ctx *ctx,
 WERROR NetGroupSetInfo_r(struct libnetapi_ctx *ctx,
                         struct NetGroupSetInfo *r)
 {
-       struct cli_state *cli = NULL;
        struct rpc_pipe_client *pipe_cli = NULL;
        NTSTATUS status;
        WERROR werr;
-       POLICY_HND connect_handle, domain_handle, group_handle;
+       struct policy_handle connect_handle, domain_handle, group_handle;
        struct lsa_String lsa_group_name;
        struct dom_sid2 *domain_sid = NULL;
 
@@ -421,7 +408,6 @@ WERROR NetGroupSetInfo_r(struct libnetapi_ctx *ctx,
 
        werr = libnetapi_open_pipe(ctx, r->in.server_name,
                                   &ndr_table_samr.syntax_id,
-                                  &cli,
                                   &pipe_cli);
        if (!W_ERROR_IS_OK(werr)) {
                goto done;
@@ -547,10 +533,6 @@ WERROR NetGroupSetInfo_r(struct libnetapi_ctx *ctx,
        werr = WERR_OK;
 
  done:
-       if (!cli) {
-               return werr;
-       }
-
        if (is_valid_policy_hnd(&group_handle)) {
                rpccli_samr_Close(pipe_cli, ctx, &group_handle);
        }
@@ -639,11 +621,10 @@ static WERROR map_group_info_to_buffer(TALLOC_CTX *mem_ctx,
 WERROR NetGroupGetInfo_r(struct libnetapi_ctx *ctx,
                         struct NetGroupGetInfo *r)
 {
-       struct cli_state *cli = NULL;
        struct rpc_pipe_client *pipe_cli = NULL;
        NTSTATUS status;
        WERROR werr;
-       POLICY_HND connect_handle, domain_handle, group_handle;
+       struct policy_handle connect_handle, domain_handle, group_handle;
        struct lsa_String lsa_group_name;
        struct dom_sid2 *domain_sid = NULL;
 
@@ -662,7 +643,6 @@ WERROR NetGroupGetInfo_r(struct libnetapi_ctx *ctx,
 
        werr = libnetapi_open_pipe(ctx, r->in.server_name,
                                   &ndr_table_samr.syntax_id,
-                                  &cli,
                                   &pipe_cli);
        if (!W_ERROR_IS_OK(werr)) {
                goto done;
@@ -732,10 +712,6 @@ WERROR NetGroupGetInfo_r(struct libnetapi_ctx *ctx,
                goto done;
        }
  done:
-       if (!cli) {
-               return werr;
-       }
-
        if (is_valid_policy_hnd(&group_handle)) {
                rpccli_samr_Close(pipe_cli, ctx, &group_handle);
        }
@@ -763,11 +739,10 @@ WERROR NetGroupGetInfo_l(struct libnetapi_ctx *ctx,
 WERROR NetGroupAddUser_r(struct libnetapi_ctx *ctx,
                         struct NetGroupAddUser *r)
 {
-       struct cli_state *cli = NULL;
        struct rpc_pipe_client *pipe_cli = NULL;
        NTSTATUS status;
        WERROR werr;
-       POLICY_HND connect_handle, domain_handle, group_handle;
+       struct policy_handle connect_handle, domain_handle, group_handle;
        struct lsa_String lsa_group_name, lsa_user_name;
        struct dom_sid2 *domain_sid = NULL;
 
@@ -784,7 +759,6 @@ WERROR NetGroupAddUser_r(struct libnetapi_ctx *ctx,
 
        werr = libnetapi_open_pipe(ctx, r->in.server_name,
                                   &ndr_table_samr.syntax_id,
-                                  &cli,
                                   &pipe_cli);
        if (!W_ERROR_IS_OK(werr)) {
                goto done;
@@ -859,10 +833,6 @@ WERROR NetGroupAddUser_r(struct libnetapi_ctx *ctx,
        werr = WERR_OK;
 
  done:
-       if (!cli) {
-               return werr;
-       }
-
        if (is_valid_policy_hnd(&group_handle)) {
                rpccli_samr_Close(pipe_cli, ctx, &group_handle);
        }
@@ -890,11 +860,10 @@ WERROR NetGroupAddUser_l(struct libnetapi_ctx *ctx,
 WERROR NetGroupDelUser_r(struct libnetapi_ctx *ctx,
                         struct NetGroupDelUser *r)
 {
-       struct cli_state *cli = NULL;
        struct rpc_pipe_client *pipe_cli = NULL;
        NTSTATUS status;
        WERROR werr;
-       POLICY_HND connect_handle, domain_handle, group_handle;
+       struct policy_handle connect_handle, domain_handle, group_handle;
        struct lsa_String lsa_group_name, lsa_user_name;
        struct dom_sid2 *domain_sid = NULL;
 
@@ -911,7 +880,6 @@ WERROR NetGroupDelUser_r(struct libnetapi_ctx *ctx,
 
        werr = libnetapi_open_pipe(ctx, r->in.server_name,
                                   &ndr_table_samr.syntax_id,
-                                  &cli,
                                   &pipe_cli);
        if (!W_ERROR_IS_OK(werr)) {
                goto done;
@@ -985,10 +953,6 @@ WERROR NetGroupDelUser_r(struct libnetapi_ctx *ctx,
        werr = WERR_OK;
 
  done:
-       if (!cli) {
-               return werr;
-       }
-
        if (is_valid_policy_hnd(&group_handle)) {
                rpccli_samr_Close(pipe_cli, ctx, &group_handle);
        }
@@ -1166,7 +1130,6 @@ static WERROR convert_samr_disp_groups_to_GROUP_INFO_buffer(TALLOC_CTX *mem_ctx,
 WERROR NetGroupEnum_r(struct libnetapi_ctx *ctx,
                      struct NetGroupEnum *r)
 {
-       struct cli_state *cli = NULL;
        struct rpc_pipe_client *pipe_cli = NULL;
        struct policy_handle connect_handle;
        struct dom_sid2 *domain_sid = NULL;
@@ -1195,7 +1158,6 @@ WERROR NetGroupEnum_r(struct libnetapi_ctx *ctx,
 
        werr = libnetapi_open_pipe(ctx, r->in.server_name,
                                   &ndr_table_samr.syntax_id,
-                                  &cli,
                                   &pipe_cli);
        if (!W_ERROR_IS_OK(werr)) {
                goto done;
@@ -1224,7 +1186,7 @@ WERROR NetGroupEnum_r(struct libnetapi_ctx *ctx,
        }
 
        if (r->out.total_entries) {
-               *r->out.total_entries = domain_info->info2.num_groups;
+               *r->out.total_entries = domain_info->general.num_groups;
        }
 
        status = rpccli_samr_QueryDisplayInfo2(pipe_cli,
@@ -1243,7 +1205,7 @@ WERROR NetGroupEnum_r(struct libnetapi_ctx *ctx,
                goto done;
        }
 
-       if (r->out.resume_handle) {
+       if (r->out.resume_handle && info.info3.count > 0) {
                *r->out.resume_handle =
                        info.info3.entries[info.info3.count-1].idx;
        }
@@ -1260,10 +1222,6 @@ WERROR NetGroupEnum_r(struct libnetapi_ctx *ctx,
        }
 
  done:
-       if (!cli) {
-               return werr;
-       }
-
        /* if last query */
        if (NT_STATUS_IS_OK(status) ||
            NT_STATUS_IS_ERR(status)) {
@@ -1294,7 +1252,6 @@ WERROR NetGroupGetUsers_r(struct libnetapi_ctx *ctx,
 {
        /* FIXME: this call needs to cope with large replies */
 
-       struct cli_state *cli = NULL;
        struct rpc_pipe_client *pipe_cli = NULL;
        struct policy_handle connect_handle, domain_handle, group_handle;
        struct lsa_String lsa_account_name;
@@ -1319,6 +1276,7 @@ WERROR NetGroupGetUsers_r(struct libnetapi_ctx *ctx,
 
        *r->out.buffer = NULL;
        *r->out.entries_read = 0;
+       *r->out.total_entries = 0;
 
        switch (r->in.level) {
                case 0:
@@ -1331,7 +1289,6 @@ WERROR NetGroupGetUsers_r(struct libnetapi_ctx *ctx,
 
        werr = libnetapi_open_pipe(ctx, r->in.server_name,
                                   &ndr_table_samr.syntax_id,
-                                  &cli,
                                   &pipe_cli);
        if (!W_ERROR_IS_OK(werr)) {
                goto done;
@@ -1391,10 +1348,15 @@ WERROR NetGroupGetUsers_r(struct libnetapi_ctx *ctx,
        }
 
        for (i=0; i < names.count; i++) {
+
+               if (member_types.ids[i] != SID_NAME_USER) {
+                       continue;
+               }
+
                status = add_GROUP_USERS_INFO_X_buffer(ctx,
                                                       r->in.level,
                                                       names.names[i].string,
-                                                      member_types.ids[i],
+                                                      7,
                                                       r->out.buffer,
                                                       &entries_read);
                if (!NT_STATUS_IS_OK(status)) {
@@ -1403,21 +1365,12 @@ WERROR NetGroupGetUsers_r(struct libnetapi_ctx *ctx,
                }
        }
 
-       if (r->out.entries_read) {
-               *r->out.entries_read = entries_read;
-       }
-
-       if (r->out.total_entries) {
-               *r->out.total_entries = entries_read;
-       }
+       *r->out.entries_read = entries_read;
+       *r->out.total_entries = entries_read;
 
        werr = WERR_OK;
 
  done:
-       if (!cli) {
-               return werr;
-       }
-
        if (is_valid_policy_hnd(&group_handle)) {
                rpccli_samr_Close(pipe_cli, ctx, &group_handle);
        }
@@ -1445,7 +1398,6 @@ WERROR NetGroupGetUsers_l(struct libnetapi_ctx *ctx,
 WERROR NetGroupSetUsers_r(struct libnetapi_ctx *ctx,
                          struct NetGroupSetUsers *r)
 {
-       struct cli_state *cli = NULL;
        struct rpc_pipe_client *pipe_cli = NULL;
        struct policy_handle connect_handle, domain_handle, group_handle;
        struct lsa_String lsa_account_name;
@@ -1454,6 +1406,7 @@ WERROR NetGroupSetUsers_r(struct libnetapi_ctx *ctx,
        struct samr_Ids user_rids, name_types;
        struct samr_Ids group_rids, group_types;
        struct samr_RidTypeArray *rid_array = NULL;
+       struct lsa_String *lsa_names = NULL;
 
        uint32_t *add_rids = NULL;
        uint32_t *del_rids = NULL;
@@ -1488,7 +1441,6 @@ WERROR NetGroupSetUsers_r(struct libnetapi_ctx *ctx,
 
        werr = libnetapi_open_pipe(ctx, r->in.server_name,
                                   &ndr_table_samr.syntax_id,
-                                  &cli,
                                   &pipe_cli);
        if (!W_ERROR_IS_OK(werr)) {
                goto done;
@@ -1549,39 +1501,40 @@ WERROR NetGroupSetUsers_r(struct libnetapi_ctx *ctx,
                        break;
        }
 
+       lsa_names = talloc_array(ctx, struct lsa_String, r->in.num_entries);
+       if (!lsa_names) {
+               werr = WERR_NOMEM;
+               goto done;
+       }
+
        for (i=0; i < r->in.num_entries; i++) {
 
                switch (r->in.level) {
                        case 0:
-                               init_lsa_String(&lsa_account_name, i0->grui0_name);
+                               init_lsa_String(&lsa_names[i], i0->grui0_name);
                                i0++;
                                break;
                        case 1:
-                               init_lsa_String(&lsa_account_name, i1->grui1_name);
+                               init_lsa_String(&lsa_names[i], i1->grui1_name);
                                i1++;
                                break;
                }
+       }
 
-               status = rpccli_samr_LookupNames(pipe_cli, ctx,
-                                                &domain_handle,
-                                                1,
-                                                &lsa_account_name,
-                                                &user_rids,
-                                                &name_types);
-               if (!NT_STATUS_IS_OK(status)) {
-                       werr = ntstatus_to_werror(status);
-                       goto done;
-               }
-
-               if (!add_rid_to_array_unique(ctx,
-                                            user_rids.ids[0],
-                                            &member_rids,
-                                            &num_member_rids)) {
-                       werr = WERR_GENERAL_FAILURE;
-                       goto done;
-               }
+       status = rpccli_samr_LookupNames(pipe_cli, ctx,
+                                        &domain_handle,
+                                        r->in.num_entries,
+                                        lsa_names,
+                                        &user_rids,
+                                        &name_types);
+       if (!NT_STATUS_IS_OK(status)) {
+               werr = ntstatus_to_werror(status);
+               goto done;
        }
 
+       member_rids = user_rids.ids;
+       num_member_rids = user_rids.count;
+
        status = rpccli_samr_QueryGroupMember(pipe_cli, ctx,
                                              &group_handle,
                                              &rid_array);
@@ -1658,10 +1611,6 @@ WERROR NetGroupSetUsers_r(struct libnetapi_ctx *ctx,
        werr = WERR_OK;
 
  done:
-       if (!cli) {
-               return werr;
-       }
-
        if (is_valid_policy_hnd(&group_handle)) {
                rpccli_samr_Close(pipe_cli, ctx, &group_handle);
        }