s3:winbindd_lookuprids: use wbint_LookupSids instead of wbint_LookupRids for most...
[metze/samba/wip.git] / source3 / winbindd / winbindd_lookuprids.c
1 /*
2    Unix SMB/CIFS implementation.
3    async implementation of WINBINDD_LOOKUPRIDS
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_wbint_c.h"
23 #include "../libcli/security/security.h"
24 #include "passdb/machine_sid.h"
25
26 struct winbindd_lookuprids_state {
27         struct tevent_context *ev;
28         const char *domain_name;
29         struct wbint_RidArray rids;
30         struct wbint_Principals names;
31
32         struct lsa_SidArray tmp_sids;
33         struct lsa_RefDomainList tmp_domains;
34         struct lsa_TransNameArray tmp_names;
35 };
36
37 static bool parse_ridlist(TALLOC_CTX *mem_ctx, char *ridstr,
38                           uint32_t **prids, uint32_t *pnum_rids);
39
40 static void winbindd_lookuprids_rids_done(struct tevent_req *subreq);
41 static void winbindd_lookuprids_sids_done(struct tevent_req *subreq);
42
43 struct tevent_req *winbindd_lookuprids_send(TALLOC_CTX *mem_ctx,
44                                             struct tevent_context *ev,
45                                             struct winbindd_cli_state *cli,
46                                             struct winbindd_request *request)
47 {
48         struct tevent_req *req, *subreq;
49         struct winbindd_lookuprids_state *state;
50         struct winbindd_domain *domain;
51         struct dom_sid sid;
52         uint32_t i;
53
54         req = tevent_req_create(mem_ctx, &state,
55                                 struct winbindd_lookuprids_state);
56         if (req == NULL) {
57                 return NULL;
58         }
59         state->ev = ev;
60
61         /* Ensure null termination */
62         request->data.sid[sizeof(request->data.sid)-1]='\0';
63
64         DEBUG(3, ("lookuprids (%s)\n", request->data.sid));
65
66         if (!string_to_sid(&sid, request->data.sid)) {
67                 DEBUG(5, ("%s not a SID\n", request->data.sid));
68                 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
69                 return tevent_req_post(req, ev);
70         }
71
72         domain = find_lookup_domain_from_sid(&sid);
73         if (domain == NULL) {
74                 DEBUG(5, ("Domain for sid %s not found\n",
75                           sid_string_dbg(&sid)));
76                 tevent_req_nterror(req, NT_STATUS_NO_SUCH_DOMAIN);
77                 return tevent_req_post(req, ev);
78         }
79
80         if (request->extra_data.data[request->extra_len-1] != '\0') {
81                 DEBUG(5, ("extra_data not 0-terminated\n"));
82                 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
83                 return tevent_req_post(req, ev);
84         }
85
86         if (!parse_ridlist(state, request->extra_data.data,
87                            &state->rids.rids, &state->rids.num_rids)) {
88                 DEBUG(5, ("parse_ridlist failed\n"));
89                 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
90                 return tevent_req_post(req, ev);
91         }
92
93         if (sid_check_is_domain(&sid)) {
94                 subreq = dcerpc_wbint_LookupRids_send(
95                         state, ev, dom_child_handle(domain), &state->rids,
96                         &state->domain_name, &state->names);
97                 if (tevent_req_nomem(subreq, req)) {
98                         return tevent_req_post(req, ev);
99                 }
100                 tevent_req_set_callback(subreq, winbindd_lookuprids_rids_done, req);
101                 return req;
102         }
103
104         state->tmp_sids.num_sids = state->rids.num_rids;
105         state->tmp_sids.sids = talloc_array(state, struct lsa_SidPtr,
106                                             state->tmp_sids.num_sids);
107         if (tevent_req_nomem(state->tmp_sids.sids, req)) {
108                 return tevent_req_post(req, ev);
109         }
110
111         for (i=0; i<state->rids.num_rids; i++) {
112                 struct lsa_SidPtr *cur = &state->tmp_sids.sids[i];
113                 cur->sid = dom_sid_add_rid(state->tmp_sids.sids,
114                                            &sid, state->rids.rids[i]);
115                 if (tevent_req_nomem(cur->sid, req)) {
116                         return tevent_req_post(req, ev);
117                 }
118         }
119
120         state->names.num_principals = state->rids.num_rids;
121         state->names.principals = talloc_array(state,
122                                                struct wbint_Principal,
123                                                state->names.num_principals);
124         if (tevent_req_nomem(state->names.principals, req)) {
125                 return tevent_req_post(req, ev);
126         }
127
128         subreq = dcerpc_wbint_LookupSids_send(
129                 state, state->ev, dom_child_handle(domain),
130                 &state->tmp_sids, &state->tmp_domains, &state->tmp_names);
131         if (tevent_req_nomem(subreq, req)) {
132                 return tevent_req_post(req, ev);
133         }
134         tevent_req_set_callback(subreq, winbindd_lookuprids_sids_done, req);
135         return req;
136 }
137
138 static void winbindd_lookuprids_rids_done(struct tevent_req *subreq)
139 {
140         struct tevent_req *req = tevent_req_callback_data(
141                 subreq, struct tevent_req);
142         struct winbindd_lookuprids_state *state = tevent_req_data(
143                 req, struct winbindd_lookuprids_state);
144         NTSTATUS status, result;
145
146         status = dcerpc_wbint_LookupRids_recv(subreq, state, &result);
147         TALLOC_FREE(subreq);
148         if (any_nt_status_not_ok(status, result, &status)) {
149                 tevent_req_nterror(req, status);
150                 return;
151         }
152         tevent_req_done(req);
153 }
154
155 static void winbindd_lookuprids_sids_done(struct tevent_req *subreq)
156 {
157         struct tevent_req *req = tevent_req_callback_data(
158                 subreq, struct tevent_req);
159         struct winbindd_lookuprids_state *state = tevent_req_data(
160                 req, struct winbindd_lookuprids_state);
161         NTSTATUS status, result;
162         uint32_t i;
163
164         status = dcerpc_wbint_LookupSids_recv(subreq, state, &result);
165         TALLOC_FREE(subreq);
166         if (any_nt_status_not_ok(status, result, &status)) {
167                 tevent_req_nterror(req, status);
168                 return;
169         }
170
171         if (state->tmp_names.count != state->names.num_principals) {
172                 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
173                 return;
174         }
175
176         if (state->tmp_domains.count != 1) {
177                 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
178                 return;
179         }
180
181         state->domain_name = talloc_move(state,
182                                          &state->tmp_domains.domains[0].name.string);
183
184         for (i=0; i < state->tmp_names.count; i++) {
185                 struct lsa_TranslatedName *n = &state->tmp_names.names[i];
186                 struct wbint_Principal *p = &state->names.principals[i];
187
188                 if (n->sid_index != 0) {
189                         tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
190                         return;
191                 }
192
193                 p->name = talloc_move(state->names.principals, &n->name.string);
194                 p->sid = *state->tmp_sids.sids[i].sid;
195                 p->type = n->sid_type;
196         }
197
198         tevent_req_done(req);
199 }
200
201 NTSTATUS winbindd_lookuprids_recv(struct tevent_req *req,
202                                   struct winbindd_response *response)
203 {
204         struct winbindd_lookuprids_state *state = tevent_req_data(
205                 req, struct winbindd_lookuprids_state);
206         NTSTATUS status;
207         char *result;
208         int i;
209
210         if (tevent_req_is_nterror(req, &status)) {
211                 DEBUG(5, ("Lookuprids failed: %s\n",nt_errstr(status)));
212                 return status;
213         }
214
215         result = talloc_strdup(response, "");
216         if (result == NULL) {
217                 return NT_STATUS_NO_MEMORY;
218         }
219
220         for (i=0; i<state->names.num_principals; i++) {
221                 struct wbint_Principal *p = &state->names.principals[i];
222
223                 result = talloc_asprintf_append_buffer(
224                         result, "%d %s\n", (int)p->type, p->name);
225                 if (result == NULL) {
226                         return NT_STATUS_NO_MEMORY;
227                 }
228         }
229
230         fstrcpy(response->data.domain_name, state->domain_name);
231         response->extra_data.data = result;
232         response->length += talloc_get_size(result);
233         return NT_STATUS_OK;
234 }
235
236 static bool parse_ridlist(TALLOC_CTX *mem_ctx, char *ridstr,
237                           uint32_t **prids, uint32_t *pnum_rids)
238 {
239         uint32_t i, num_rids;
240         uint32_t *rids;
241         char *p;
242
243         if (ridstr == NULL) {
244                 return false;
245         }
246
247         p = ridstr;
248         num_rids = 0;
249
250         /* count rids */
251
252         while ((p = strchr(p, '\n')) != NULL) {
253                 p += 1;
254                 num_rids += 1;
255         }
256
257         if (num_rids == 0) {
258                 *pnum_rids = 0;
259                 *prids = NULL;
260                 return true;
261         }
262
263         rids = talloc_array(mem_ctx, uint32_t, num_rids);
264         if (rids == NULL) {
265                 return false;
266         }
267
268         p = ridstr;
269
270         for (i=0; i<num_rids; i++) {
271                 char *q;
272                 rids[i] = strtoul(p, &q, 10);
273                 if (*q != '\n') {
274                         DEBUG(0, ("Got invalid ridstr: %s\n", p));
275                         return false;
276                 }
277                 p = q+1;
278         }
279
280         *pnum_rids = num_rids;
281         *prids = rids;
282         return true;
283 }