winbindd: allow wbinfo -a REALM\\user to work on a DC
[amitay/samba.git] / source3 / winbindd / wb_query_user_list.c
1 /*
2    Unix SMB/CIFS implementation.
3    async query_user_list
4    Copyright (C) Volker Lendecke 2009
5
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.
10
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.
15
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/>.
18 */
19
20 #include "includes.h"
21 #include "winbindd.h"
22 #include "librpc/gen_ndr/ndr_winbind_c.h"
23 #include "lib/util/strv.h"
24
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;
32 };
33
34 static void wb_query_user_list_gotrids(struct tevent_req *subreq);
35 static void wb_query_user_list_done(struct tevent_req *subreq);
36
37 struct tevent_req *wb_query_user_list_send(TALLOC_CTX *mem_ctx,
38                                            struct tevent_context *ev,
39                                            struct winbindd_domain *domain)
40 {
41         struct tevent_req *req, *subreq;
42         struct wb_query_user_list_state *state;
43
44         req = tevent_req_create(mem_ctx, &state,
45                                 struct wb_query_user_list_state);
46         if (req == NULL) {
47                 return NULL;
48         }
49         state->ev = ev;
50         state->domain = domain;
51
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);
56         }
57         tevent_req_set_callback(subreq, wb_query_user_list_gotrids, req);
58         return req;
59 }
60
61 static void wb_query_user_list_gotrids(struct tevent_req *subreq)
62 {
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;
68
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;
74         }
75
76         DEBUG(10, ("dcerpc_wbint_QueryUserList returned %d users\n",
77                    state->rids.num_rids));
78
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;
85         }
86         tevent_req_set_callback(subreq, wb_query_user_list_done, req);
87 }
88
89 static void wb_query_user_list_done(struct tevent_req *subreq)
90 {
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;
97
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;
103         }
104
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;
109
110                 fill_domain_username(name, state->domain_name, p->name, true);
111
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;
116                 }
117         }
118
119         tevent_req_done(req);
120 }
121
122 NTSTATUS wb_query_user_list_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
123                                  char **users)
124 {
125         struct wb_query_user_list_state *state = tevent_req_data(
126                 req, struct wb_query_user_list_state);
127         NTSTATUS status;
128
129         if (tevent_req_is_nterror(req, &status)) {
130                 return status;
131         }
132
133         *users = talloc_move(mem_ctx, &state->users);
134
135         return NT_STATUS_OK;
136 }