Register types rather than constructors, display structs as classes.
[jelmer/samba4-debian.git] / source / winbind / wb_async_helpers.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    Copyright (C) Volker Lendecke 2005
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   a composite API for finding a DC and its name
21 */
22
23 #include "includes.h"
24 #include "libcli/composite/composite.h"
25 #include "winbind/wb_async_helpers.h"
26
27 #include "lib/messaging/irpc.h"
28 #include "librpc/gen_ndr/irpc.h"
29 #include "auth/credentials/credentials.h"
30 #include "libcli/security/security.h"
31 #include "libcli/auth/libcli_auth.h"
32 #include "librpc/gen_ndr/ndr_netlogon_c.h"
33 #include "librpc/gen_ndr/ndr_lsa_c.h"
34 #include "librpc/gen_ndr/ndr_samr_c.h"
35
36 #include "winbind/wb_helper.h"
37
38 struct lsa_lookupsids_state {
39         struct composite_context *ctx;
40         int num_sids;
41         struct lsa_LookupSids r;
42         struct lsa_SidArray sids;
43         struct lsa_TransNameArray names;
44         uint32_t count;
45         struct wb_sid_object **result;
46 };
47
48 static void lsa_lookupsids_recv_names(struct rpc_request *req);
49
50 struct composite_context *wb_lsa_lookupsids_send(TALLOC_CTX *mem_ctx,
51                                                  struct dcerpc_pipe *lsa_pipe,
52                                                  struct policy_handle *handle,
53                                                  int num_sids,
54                                                  const struct dom_sid **sids)
55 {
56         struct composite_context *result;
57         struct rpc_request *req;
58         struct lsa_lookupsids_state *state;
59         int i;
60
61         result = composite_create(mem_ctx, lsa_pipe->conn->event_ctx);
62         if (result == NULL) goto failed;
63
64         state = talloc(result, struct lsa_lookupsids_state);
65         if (state == NULL) goto failed;
66         result->private_data = state;
67         state->ctx = result;
68
69         state->sids.num_sids = num_sids;
70         state->sids.sids = talloc_array(state, struct lsa_SidPtr, num_sids);
71         if (state->sids.sids == NULL) goto failed;
72
73         for (i=0; i<num_sids; i++) {
74                 state->sids.sids[i].sid = dom_sid_dup(state->sids.sids,
75                                                       sids[i]);
76                 if (state->sids.sids[i].sid == NULL) goto failed;
77         }
78
79         state->count = 0;
80         state->num_sids = num_sids;
81         state->names.count = 0;
82         state->names.names = NULL;
83
84         state->r.in.handle = handle;
85         state->r.in.sids = &state->sids;
86         state->r.in.names = &state->names;
87         state->r.in.level = 1;
88         state->r.in.count = &state->count;
89         state->r.out.names = &state->names;
90         state->r.out.count = &state->count;
91
92         req = dcerpc_lsa_LookupSids_send(lsa_pipe, state, &state->r);
93         if (req == NULL) goto failed;
94
95         req->async.callback = lsa_lookupsids_recv_names;
96         req->async.private_data = state;
97         return result;
98
99  failed:
100         talloc_free(result);
101         return NULL;
102 }
103
104 static void lsa_lookupsids_recv_names(struct rpc_request *req)
105 {
106         struct lsa_lookupsids_state *state =
107                 talloc_get_type(req->async.private_data,
108                                 struct lsa_lookupsids_state);
109         int i;
110
111         state->ctx->status = dcerpc_ndr_request_recv(req);
112         if (!composite_is_ok(state->ctx)) return;
113         state->ctx->status = state->r.out.result;
114         if (!NT_STATUS_IS_OK(state->ctx->status) &&
115             !NT_STATUS_EQUAL(state->ctx->status, STATUS_SOME_UNMAPPED)) {
116                 composite_error(state->ctx, state->ctx->status);
117                 return;
118         }
119
120         state->result = talloc_array(state, struct wb_sid_object *,
121                                      state->num_sids);
122         if (composite_nomem(state->result, state->ctx)) return;
123
124         for (i=0; i<state->num_sids; i++) {
125                 struct lsa_TranslatedName *name =
126                         &state->r.out.names->names[i];
127                 struct lsa_DomainInfo *dom;
128
129                 state->result[i] = talloc_zero(state->result,
130                                                struct wb_sid_object);
131                 if (composite_nomem(state->result[i], state->ctx)) return;
132
133                 state->result[i]->type = name->sid_type;
134                 if (state->result[i]->type == SID_NAME_UNKNOWN) {
135                         continue;
136                 }
137
138                 if (name->sid_index >= state->r.out.domains->count) {
139                         composite_error(state->ctx,
140                                         NT_STATUS_INVALID_PARAMETER);
141                         return;
142                 }
143
144                 dom = &state->r.out.domains->domains[name->sid_index];
145                 state->result[i]->domain = talloc_reference(state->result[i],
146                                                             dom->name.string);
147                 if ((name->sid_type == SID_NAME_DOMAIN) ||
148                     (name->name.string == NULL)) {
149                         state->result[i]->name =
150                                 talloc_strdup(state->result[i], "");
151                 } else {
152                         state->result[i]->name =
153                                 talloc_steal(state->result[i],
154                                              name->name.string);
155                 }
156
157                 if (composite_nomem(state->result[i]->name, state->ctx)) {
158                         return;
159                 }
160         }
161
162         composite_done(state->ctx);
163 }
164
165 NTSTATUS wb_lsa_lookupsids_recv(struct composite_context *c,
166                                 TALLOC_CTX *mem_ctx,
167                                 struct wb_sid_object ***names)
168 {
169         NTSTATUS status = composite_wait(c);
170         if (NT_STATUS_IS_OK(status)) {
171                 struct lsa_lookupsids_state *state =
172                         talloc_get_type(c->private_data,
173                                         struct lsa_lookupsids_state);
174                 *names = talloc_steal(mem_ctx, state->result);
175         }
176         talloc_free(c);
177         return status;
178 }
179
180
181 struct lsa_lookupnames_state {
182         struct composite_context *ctx;
183         uint32_t num_names;
184         struct lsa_LookupNames r;
185         struct lsa_TransSidArray sids;
186         uint32_t count;
187         struct wb_sid_object **result;
188 };
189
190 static void lsa_lookupnames_recv_sids(struct rpc_request *req);
191
192 struct composite_context *wb_lsa_lookupnames_send(TALLOC_CTX *mem_ctx,
193                                                   struct dcerpc_pipe *lsa_pipe,
194                                                   struct policy_handle *handle,
195                                                   int num_names,
196                                                   const char **names)
197 {
198         struct composite_context *result;
199         struct rpc_request *req;
200         struct lsa_lookupnames_state *state;
201
202         struct lsa_String *lsa_names;
203         int i;
204
205         result = composite_create(mem_ctx, lsa_pipe->conn->event_ctx);
206         if (result == NULL) goto failed;
207
208         state = talloc(result, struct lsa_lookupnames_state);
209         if (state == NULL) goto failed;
210         result->private_data = state;
211         state->ctx = result;
212
213         state->sids.count = 0;
214         state->sids.sids = NULL;
215         state->num_names = num_names;
216         state->count = 0;
217
218         lsa_names = talloc_array(state, struct lsa_String, num_names);
219         if (lsa_names == NULL) goto failed;
220
221         for (i=0; i<num_names; i++) {
222                 lsa_names[i].string = names[i];
223         }
224
225         state->r.in.handle = handle;
226         state->r.in.num_names = num_names;
227         state->r.in.names = lsa_names;
228         state->r.in.sids = &state->sids;
229         state->r.in.level = 1;
230         state->r.in.count = &state->count;
231         state->r.out.count = &state->count;
232         state->r.out.sids = &state->sids;
233
234         req = dcerpc_lsa_LookupNames_send(lsa_pipe, state, &state->r);
235         if (req == NULL) goto failed;
236
237         req->async.callback = lsa_lookupnames_recv_sids;
238         req->async.private_data = state;
239         return result;
240
241  failed:
242         talloc_free(result);
243         return NULL;
244 }
245
246 static void lsa_lookupnames_recv_sids(struct rpc_request *req)
247 {
248         struct lsa_lookupnames_state *state =
249                 talloc_get_type(req->async.private_data,
250                                 struct lsa_lookupnames_state);
251         int i;
252
253         state->ctx->status = dcerpc_ndr_request_recv(req);
254         if (!composite_is_ok(state->ctx)) return;
255         state->ctx->status = state->r.out.result;
256         if (!NT_STATUS_IS_OK(state->ctx->status) &&
257             !NT_STATUS_EQUAL(state->ctx->status, STATUS_SOME_UNMAPPED)) {
258                 composite_error(state->ctx, state->ctx->status);
259                 return;
260         }
261
262         state->result = talloc_array(state, struct wb_sid_object *,
263                                      state->num_names);
264         if (composite_nomem(state->result, state->ctx)) return;
265
266         for (i=0; i<state->num_names; i++) {
267                 struct lsa_TranslatedSid *sid = &state->r.out.sids->sids[i];
268                 struct lsa_DomainInfo *dom;
269
270                 state->result[i] = talloc_zero(state->result,
271                                                struct wb_sid_object);
272                 if (composite_nomem(state->result[i], state->ctx)) return;
273
274                 state->result[i]->type = sid->sid_type;
275                 if (state->result[i]->type == SID_NAME_UNKNOWN) {
276                         continue;
277                 }
278
279                 if (sid->sid_index >= state->r.out.domains->count) {
280                         composite_error(state->ctx,
281                                         NT_STATUS_INVALID_PARAMETER);
282                         return;
283                 }
284
285                 dom = &state->r.out.domains->domains[sid->sid_index];
286
287                 state->result[i]->sid = dom_sid_add_rid(state->result[i],
288                                                         dom->sid, sid->rid);
289         }
290
291         composite_done(state->ctx);
292 }
293
294 NTSTATUS wb_lsa_lookupnames_recv(struct composite_context *c,
295                                  TALLOC_CTX *mem_ctx,
296                                  struct wb_sid_object ***sids)
297 {
298         NTSTATUS status = composite_wait(c);
299         if (NT_STATUS_IS_OK(status)) {
300                 struct lsa_lookupnames_state *state =
301                         talloc_get_type(c->private_data,
302                                         struct lsa_lookupnames_state);
303                 *sids = talloc_steal(mem_ctx, state->result);
304         }
305         talloc_free(c);
306         return status;
307 }
308
309
310 #if 0
311
312 struct cmd_checkmachacc_state {
313         struct composite_context *ctx;
314         struct wbsrv_call *call;
315         struct wbsrv_domain *domain;
316 };
317
318 static void cmd_checkmachacc_recv_init(struct composite_context *ctx);
319
320  struct composite_context *wb_cmd_checkmachacc_send(struct wbsrv_call *call)
321 {
322         struct composite_context *result, *ctx;
323         struct cmd_checkmachacc_state *state;
324         struct wbsrv_service *service = call->wbconn->listen_socket->service;
325
326         result = composite_create(mem_ctx, call->event_ctx;
327         if (result == NULL) goto failed;
328
329         state = talloc(result, struct cmd_checkmachacc_state);
330         if (state == NULL) goto failed;
331         state->ctx = result;
332         result->private_data = state;
333         state->call = call;
334
335         state->domain = service->domains;
336
337         ctx = wb_init_domain_send(service, state->domain);
338         if (ctx == NULL) goto failed;
339         ctx->async.fn = cmd_checkmachacc_recv_init;
340         ctx->async.private_data = state;
341
342         return result;
343
344  failed:
345         talloc_free(result);
346         return NULL;
347 }
348
349 static void cmd_checkmachacc_recv_init(struct composite_context *ctx)
350 {
351         struct cmd_checkmachacc_state *state =
352                 talloc_get_type(ctx->async.private_data,
353                                 struct cmd_checkmachacc_state);
354
355         state->ctx->status = wb_init_domain_recv(ctx);
356         if (!composite_is_ok(state->ctx)) return;
357
358         composite_done(state->ctx);
359 }
360
361  NTSTATUS wb_cmd_checkmachacc_recv(struct composite_context *c)
362 {
363         NTSTATUS status = composite_wait(c);
364         talloc_free(c);
365         return status;
366 }
367
368  NTSTATUS wb_cmd_checkmachacc(struct wbsrv_call *call)
369 {
370         struct composite_context *c = wb_cmd_checkmachacc_send(call);
371         return wb_cmd_checkmachacc_recv(c);
372 }
373 #endif
374
375 struct samr_getuserdomgroups_state {
376         struct composite_context *ctx;
377         struct dcerpc_pipe *samr_pipe;
378
379         int num_rids;
380         uint32_t *rids;
381
382         struct policy_handle *user_handle;
383         struct samr_OpenUser o;
384         struct samr_GetGroupsForUser g;
385         struct samr_Close c;
386 };
387
388 static void samr_usergroups_recv_open(struct rpc_request *req);
389 static void samr_usergroups_recv_groups(struct rpc_request *req);
390 static void samr_usergroups_recv_close(struct rpc_request *req);
391
392 struct composite_context *wb_samr_userdomgroups_send(TALLOC_CTX *mem_ctx,
393                                                      struct dcerpc_pipe *samr_pipe,
394                                                      struct policy_handle *domain_handle,
395                                                      uint32_t rid)
396 {
397         struct composite_context *result;
398         struct rpc_request *req;
399         struct samr_getuserdomgroups_state *state;
400
401         result = composite_create(mem_ctx, samr_pipe->conn->event_ctx);
402         if (result == NULL) goto failed;
403
404         state = talloc(result, struct samr_getuserdomgroups_state);
405         if (state == NULL) goto failed;
406         result->private_data = state;
407         state->ctx = result;
408
409         state->samr_pipe = samr_pipe;
410
411         state->user_handle = talloc(state, struct policy_handle);
412         if (state->user_handle == NULL) goto failed;
413
414         state->o.in.domain_handle = domain_handle;
415         state->o.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
416         state->o.in.rid = rid;
417         state->o.out.user_handle = state->user_handle;
418
419         req = dcerpc_samr_OpenUser_send(state->samr_pipe, state, &state->o);
420         if (req == NULL) goto failed;
421
422         req->async.callback = samr_usergroups_recv_open;
423         req->async.private_data = state;
424         return result;
425
426  failed:
427         talloc_free(result);
428         return NULL;
429 }
430                                               
431 static void samr_usergroups_recv_open(struct rpc_request *req)
432 {
433         struct samr_getuserdomgroups_state *state =
434                 talloc_get_type(req->async.private_data,
435                                 struct samr_getuserdomgroups_state);
436
437         state->ctx->status = dcerpc_ndr_request_recv(req);
438         if (!composite_is_ok(state->ctx)) return;
439         state->ctx->status = state->o.out.result;
440         if (!composite_is_ok(state->ctx)) return;
441
442         state->g.in.user_handle = state->user_handle;
443
444         req = dcerpc_samr_GetGroupsForUser_send(state->samr_pipe, state,
445                                                 &state->g);
446         composite_continue_rpc(state->ctx, req, samr_usergroups_recv_groups,
447                                state);
448 }
449
450 static void samr_usergroups_recv_groups(struct rpc_request *req)
451 {
452         struct samr_getuserdomgroups_state *state =
453                 talloc_get_type(req->async.private_data,
454                                 struct samr_getuserdomgroups_state);
455
456         state->ctx->status = dcerpc_ndr_request_recv(req);
457         if (!composite_is_ok(state->ctx)) return;
458         state->ctx->status = state->g.out.result;
459         if (!composite_is_ok(state->ctx)) return;
460
461         state->c.in.handle = state->user_handle;
462         state->c.out.handle = state->user_handle;
463
464         req = dcerpc_samr_Close_send(state->samr_pipe, state, &state->c);
465         composite_continue_rpc(state->ctx, req, samr_usergroups_recv_close,
466                                state);
467 }
468
469 static void samr_usergroups_recv_close(struct rpc_request *req)
470 {
471         struct samr_getuserdomgroups_state *state =
472                 talloc_get_type(req->async.private_data,
473                                 struct samr_getuserdomgroups_state);
474
475         state->ctx->status = dcerpc_ndr_request_recv(req);
476         if (!composite_is_ok(state->ctx)) return;
477         state->ctx->status = state->c.out.result;
478         if (!composite_is_ok(state->ctx)) return;
479
480         composite_done(state->ctx);
481 }
482
483 NTSTATUS wb_samr_userdomgroups_recv(struct composite_context *ctx,
484                                     TALLOC_CTX *mem_ctx,
485                                     int *num_rids, uint32_t **rids)
486 {
487         struct samr_getuserdomgroups_state *state =
488                 talloc_get_type(ctx->private_data,
489                                 struct samr_getuserdomgroups_state);
490
491         int i;
492         NTSTATUS status = composite_wait(ctx);
493         if (!NT_STATUS_IS_OK(status)) goto done;
494
495         *num_rids = state->g.out.rids->count;
496         *rids = talloc_array(mem_ctx, uint32_t, *num_rids);
497         if (*rids == NULL) {
498                 status = NT_STATUS_NO_MEMORY;
499                 goto done;
500         }
501
502         for (i=0; i<*num_rids; i++) {
503                 (*rids)[i] = state->g.out.rids->rids[i].rid;
504         }
505
506  done:
507         talloc_free(ctx);
508         return status;
509 }