libnet: Add NULL checks to py_net_finddc
[Samba.git] / source3 / winbindd / wb_query_user_list.c
blob3c18080e8475c539ad9764f805f9424c2aa2d26e
1 /*
2 Unix SMB/CIFS implementation.
3 async query_user_list
4 Copyright (C) Volker Lendecke 2009
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include "includes.h"
21 #include "winbindd.h"
22 #include "librpc/gen_ndr/ndr_winbind_c.h"
23 #include "lib/util/strv.h"
25 struct wb_query_user_list_state {
26 struct tevent_context *ev;
27 struct winbindd_domain *domain;
28 struct wbint_RidArray rids;
29 const char *domain_name;
30 struct wbint_Principals names;
31 char *users;
34 static void wb_query_user_list_gotrids(struct tevent_req *subreq);
35 static void wb_query_user_list_done(struct tevent_req *subreq);
37 struct tevent_req *wb_query_user_list_send(TALLOC_CTX *mem_ctx,
38 struct tevent_context *ev,
39 struct winbindd_domain *domain)
41 struct tevent_req *req, *subreq;
42 struct wb_query_user_list_state *state;
44 req = tevent_req_create(mem_ctx, &state,
45 struct wb_query_user_list_state);
46 if (req == NULL) {
47 return NULL;
49 state->ev = ev;
50 state->domain = domain;
52 subreq = dcerpc_wbint_QueryUserRidList_send(
53 state, ev, dom_child_handle(domain), &state->rids);
54 if (tevent_req_nomem(subreq, req)) {
55 return tevent_req_post(req, ev);
57 tevent_req_set_callback(subreq, wb_query_user_list_gotrids, req);
58 return req;
61 static void wb_query_user_list_gotrids(struct tevent_req *subreq)
63 struct tevent_req *req = tevent_req_callback_data(
64 subreq, struct tevent_req);
65 struct wb_query_user_list_state *state = tevent_req_data(
66 req, struct wb_query_user_list_state);
67 NTSTATUS status, result;
69 status = dcerpc_wbint_QueryUserRidList_recv(subreq, state, &result);
70 TALLOC_FREE(subreq);
71 if (any_nt_status_not_ok(status, result, &status)) {
72 tevent_req_nterror(req, status);
73 return;
76 DEBUG(10, ("dcerpc_wbint_QueryUserList returned %d users\n",
77 state->rids.num_rids));
79 subreq = dcerpc_wbint_LookupRids_send(
80 state, state->ev, dom_child_handle(state->domain),
81 &state->domain->sid, &state->rids,
82 &state->domain_name, &state->names);
83 if (tevent_req_nomem(subreq, req)) {
84 return;
86 tevent_req_set_callback(subreq, wb_query_user_list_done, req);
89 static void wb_query_user_list_done(struct tevent_req *subreq)
91 struct tevent_req *req = tevent_req_callback_data(
92 subreq, struct tevent_req);
93 struct wb_query_user_list_state *state = tevent_req_data(
94 req, struct wb_query_user_list_state);
95 NTSTATUS status, result;
96 int i;
98 status = dcerpc_wbint_LookupRids_recv(subreq, state, &result);
99 TALLOC_FREE(subreq);
100 if (any_nt_status_not_ok(status, result, &status)) {
101 tevent_req_nterror(req, status);
102 return;
105 for (i=0; i<state->names.num_principals; i++) {
106 struct wbint_Principal *p = &state->names.principals[i];
107 fstring name;
108 int ret;
110 fill_domain_username(name, state->domain_name, p->name, true);
112 ret = strv_add(state, &state->users, name);
113 if (ret != 0) {
114 tevent_req_nterror(req, map_nt_error_from_unix(ret));
115 return;
119 tevent_req_done(req);
122 NTSTATUS wb_query_user_list_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
123 char **users)
125 struct wb_query_user_list_state *state = tevent_req_data(
126 req, struct wb_query_user_list_state);
127 NTSTATUS status;
129 if (tevent_req_is_nterror(req, &status)) {
130 return status;
133 *users = talloc_move(mem_ctx, &state->users);
135 return NT_STATUS_OK;