r11094: Connect to SAM, implement getdcname
[ira/wip.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 2 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, write to the Free Software
18    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20 /*
21   a composite API for finding a DC and its name
22 */
23
24 #include "includes.h"
25 #include "libcli/composite/composite.h"
26 #include "libcli/smb_composite/smb_composite.h"
27 #include "winbind/wb_async_helpers.h"
28 #include "winbind/wb_server.h"
29 #include "smbd/service_stream.h"
30
31 #include "librpc/gen_ndr/nbt.h"
32 #include "librpc/gen_ndr/samr.h"
33 #include "lib/messaging/irpc.h"
34 #include "librpc/gen_ndr/irpc.h"
35 #include "librpc/gen_ndr/ndr_irpc.h"
36 #include "libcli/raw/libcliraw.h"
37 #include "librpc/gen_ndr/ndr_netlogon.h"
38 #include "librpc/gen_ndr/ndr_lsa.h"
39 #include "libcli/auth/credentials.h"
40
41 struct finddcs_state {
42         struct composite_context *ctx;
43         struct messaging_context *msg_ctx;
44
45         const char *domain_name;
46         const struct dom_sid *domain_sid;
47
48         struct nbtd_getdcname r;
49
50         int num_dcs;
51         struct nbt_dc_name *dcs;
52 };
53
54 static void finddcs_resolve(struct composite_context *ctx);
55 static void finddcs_getdc(struct irpc_request *ireq);
56
57 struct composite_context *wb_finddcs_send(const char *domain_name,
58                                           const struct dom_sid *domain_sid,
59                                           struct event_context *event_ctx,
60                                           struct messaging_context *msg_ctx)
61 {
62         struct composite_context *result, *ctx;
63         struct finddcs_state *state;
64         struct nbt_name name;
65
66         result = talloc_zero(NULL, struct composite_context);
67         if (result == NULL) goto failed;
68         result->state = COMPOSITE_STATE_IN_PROGRESS;
69         result->async.fn = NULL;
70         result->event_ctx = event_ctx;
71
72         state = talloc(result, struct finddcs_state);
73         if (state == NULL) goto failed;
74         state->ctx = result;
75         result->private_data = state;
76
77         state->domain_name = talloc_strdup(state, domain_name);
78         if (state->domain_name == NULL) goto failed;
79         state->domain_sid = dom_sid_dup(state, domain_sid);
80         if (state->domain_sid == NULL) goto failed;
81         state->msg_ctx = msg_ctx;
82
83         make_nbt_name(&name, state->domain_name, 0x1c);
84         ctx = resolve_name_send(&name, result->event_ctx,
85                                 lp_name_resolve_order());
86
87         if (ctx == NULL) goto failed;
88         ctx->async.fn = finddcs_resolve;
89         ctx->async.private_data = state;
90
91         return result;
92
93 failed:
94         talloc_free(result);
95         return NULL;
96 }
97
98 static void finddcs_resolve(struct composite_context *ctx)
99 {
100         struct finddcs_state *state =
101                 talloc_get_type(ctx->async.private_data, struct finddcs_state);
102         struct irpc_request *ireq;
103         uint32_t *nbt_servers;
104         const char *address;
105
106         state->ctx->status = resolve_name_recv(ctx, state, &address);
107         if (!composite_is_ok(state->ctx)) return;
108
109         state->num_dcs = 1;
110         state->dcs = talloc_array(state, struct nbt_dc_name, state->num_dcs);
111         if (composite_nomem(state->dcs, state->ctx)) return;
112
113         state->dcs[0].address = talloc_steal(state->dcs, address);
114
115         nbt_servers = irpc_servers_byname(state->msg_ctx, "nbt_server");
116         if ((nbt_servers == NULL) || (nbt_servers[0] == 0)) {
117                 composite_error(state->ctx, NT_STATUS_NO_LOGON_SERVERS);
118                 return;
119         }
120
121         state->r.in.domainname = state->domain_name;
122         state->r.in.ip_address = state->dcs[0].address;
123         state->r.in.my_computername = lp_netbios_name();
124         state->r.in.my_accountname = talloc_asprintf(state, "%s$",
125                                                      lp_netbios_name());
126         if (composite_nomem(state->r.in.my_accountname, state->ctx)) return;
127         state->r.in.account_control = ACB_WSTRUST;
128         state->r.in.domain_sid = dom_sid_dup(state, state->domain_sid);
129         if (composite_nomem(state->r.in.domain_sid, state->ctx)) return;
130
131         ireq = irpc_call_send(state->msg_ctx, nbt_servers[0],
132                               &dcerpc_table_irpc, DCERPC_NBTD_GETDCNAME,
133                               &state->r, state);
134         composite_continue_irpc(state->ctx, ireq, finddcs_getdc, state);
135 }
136
137 static void finddcs_getdc(struct irpc_request *ireq)
138 {
139         struct finddcs_state *state =
140                 talloc_get_type(ireq->async.private, struct finddcs_state);
141
142         state->ctx->status = irpc_call_recv(ireq);
143         talloc_free(ireq);
144         if (!composite_is_ok(state->ctx)) return;
145
146         state->dcs[0].name = talloc_steal(state->dcs, state->r.out.dcname);
147         composite_done(state->ctx);
148 }
149
150 NTSTATUS wb_finddcs_recv(struct composite_context *c, TALLOC_CTX *mem_ctx,
151                          int *num_dcs, struct nbt_dc_name **dcs)
152 {
153         NTSTATUS status =composite_wait(c);
154         if (NT_STATUS_IS_OK(status)) {
155                 struct finddcs_state *state =
156                         talloc_get_type(c->private_data, struct finddcs_state);
157                 *num_dcs = state->num_dcs;
158                 *dcs = talloc_steal(mem_ctx, state->dcs);
159         }
160         talloc_free(c);
161         return status;
162 }
163
164 NTSTATUS wb_finddcs(const char *domain_name, const struct dom_sid *domain_sid,
165                     struct event_context *event_ctx,
166                     struct messaging_context *msg_ctx,
167                     TALLOC_CTX *mem_ctx,
168                     int *num_dcs, struct nbt_dc_name **dcs)
169 {
170         struct composite_context *c = wb_finddcs_send(domain_name, domain_sid,
171                                                       event_ctx, msg_ctx);
172         return wb_finddcs_recv(c, mem_ctx, num_dcs, dcs);
173 }
174
175 struct get_schannel_creds_state {
176         struct composite_context *ctx;
177         struct cli_credentials *wks_creds;
178         struct dcerpc_pipe *p;
179         struct netr_ServerReqChallenge r;
180
181         struct creds_CredentialState *creds_state;
182         struct netr_Credential netr_cred;
183         uint32_t negotiate_flags;
184         struct netr_ServerAuthenticate2 a;
185 };
186
187 static void get_schannel_creds_recv_auth(struct rpc_request *req);
188 static void get_schannel_creds_recv_chal(struct rpc_request *req);
189 static void get_schannel_creds_recv_pipe(struct composite_context *ctx);
190
191 struct composite_context *wb_get_schannel_creds_send(struct cli_credentials *wks_creds,
192                                                      struct smbcli_tree *tree,
193                                                      struct event_context *ev)
194 {
195         struct composite_context *result, *ctx;
196         struct get_schannel_creds_state *state;
197
198         result = talloc_zero(NULL, struct composite_context);
199         if (result == NULL) goto failed;
200         result->state = COMPOSITE_STATE_IN_PROGRESS;
201         result->async.fn = NULL;
202         result->event_ctx = ev;
203
204         state = talloc(result, struct get_schannel_creds_state);
205         if (state == NULL) goto failed;
206         result->private_data = state;
207         state->ctx = result;
208
209         state->wks_creds = wks_creds;
210
211         state->p = dcerpc_pipe_init(state, ev);
212         if (state->p == NULL) goto failed;
213
214         ctx = dcerpc_pipe_open_smb_send(state->p->conn, tree, "\\netlogon");
215         if (ctx == NULL) goto failed;
216
217         ctx->async.fn = get_schannel_creds_recv_pipe;
218         ctx->async.private_data = state;
219         return result;
220
221  failed:
222         talloc_free(result);
223         return NULL;
224 }
225
226 static void get_schannel_creds_recv_pipe(struct composite_context *ctx)
227 {
228         struct get_schannel_creds_state *state =
229                 talloc_get_type(ctx->async.private_data,
230                                 struct get_schannel_creds_state);
231         struct rpc_request *req;
232
233         state->ctx->status = dcerpc_pipe_open_smb_recv(ctx);
234         if (!composite_is_ok(state->ctx)) return;
235
236         state->ctx->status = dcerpc_bind_auth_none(state->p,
237                                                    DCERPC_NETLOGON_UUID,
238                                                    DCERPC_NETLOGON_VERSION);
239         if (!composite_is_ok(state->ctx)) return;
240
241         state->r.in.computer_name =
242                 cli_credentials_get_workstation(state->wks_creds);
243         state->r.in.server_name =
244                 talloc_asprintf(state, "\\\\%s",
245                                 dcerpc_server_name(state->p));
246         if (composite_nomem(state->r.in.server_name, state->ctx)) return;
247
248         state->r.in.credentials = talloc(state, struct netr_Credential);
249         if (composite_nomem(state->r.in.credentials, state->ctx)) return;
250
251         state->r.out.credentials = talloc(state, struct netr_Credential);
252         if (composite_nomem(state->r.out.credentials, state->ctx)) return;
253
254         generate_random_buffer(state->r.in.credentials->data,
255                                sizeof(state->r.in.credentials->data));
256
257         req = dcerpc_netr_ServerReqChallenge_send(state->p, state, &state->r);
258         composite_continue_rpc(state->ctx, req,
259                                get_schannel_creds_recv_chal, state);
260 }
261
262 static void get_schannel_creds_recv_chal(struct rpc_request *req)
263 {
264         struct get_schannel_creds_state *state =
265                 talloc_get_type(req->async.private,
266                                 struct get_schannel_creds_state);
267         const struct samr_Password *mach_pwd;
268
269         state->ctx->status = dcerpc_ndr_request_recv(req);
270         if (!composite_is_ok(state->ctx)) return;
271         state->ctx->status = state->r.out.result;
272         if (!composite_is_ok(state->ctx)) return;
273
274         state->creds_state = talloc(state, struct creds_CredentialState);
275         if (composite_nomem(state->creds_state, state->ctx)) return;
276
277         mach_pwd = cli_credentials_get_nt_hash(state->wks_creds, state);
278         if (composite_nomem(mach_pwd, state->ctx)) return;
279
280         state->negotiate_flags = NETLOGON_NEG_AUTH2_FLAGS;
281
282         creds_client_init(state->creds_state, state->r.in.credentials,
283                           state->r.out.credentials, mach_pwd,
284                           &state->netr_cred, state->negotiate_flags);
285
286         state->a.in.server_name =
287                 talloc_reference(state, state->r.in.server_name);
288         state->a.in.account_name =
289                 cli_credentials_get_username(state->wks_creds);
290         state->a.in.secure_channel_type =
291                 cli_credentials_get_secure_channel_type(state->wks_creds);
292         state->a.in.computer_name =
293                 cli_credentials_get_workstation(state->wks_creds);
294         state->a.in.negotiate_flags = &state->negotiate_flags;
295         state->a.out.negotiate_flags = &state->negotiate_flags;
296         state->a.in.credentials = &state->netr_cred;
297         state->a.out.credentials = &state->netr_cred;
298
299         req = dcerpc_netr_ServerAuthenticate2_send(state->p, state, &state->a);
300         composite_continue_rpc(state->ctx, req,
301                                get_schannel_creds_recv_auth, state);
302 }
303
304 static void get_schannel_creds_recv_auth(struct rpc_request *req)
305 {
306         struct get_schannel_creds_state *state =
307                 talloc_get_type(req->async.private,
308                                 struct get_schannel_creds_state);
309
310         state->ctx->status = dcerpc_ndr_request_recv(req);
311         if (!NT_STATUS_IS_OK(state->ctx->status)) goto done;
312         state->ctx->status = state->a.out.result;
313         if (!NT_STATUS_IS_OK(state->ctx->status)) goto done;
314
315         if (!creds_client_check(state->creds_state,
316                                 state->a.out.credentials)) {
317                 DEBUG(5, ("Server got us invalid creds\n"));
318                 state->ctx->status = NT_STATUS_UNSUCCESSFUL;
319                 goto done;
320         }
321
322         cli_credentials_set_netlogon_creds(state->wks_creds,
323                                            state->creds_state);
324
325         state->ctx->state = COMPOSITE_STATE_DONE;
326
327  done:
328         if (!NT_STATUS_IS_OK(state->ctx->status)) {
329                 state->ctx->state = COMPOSITE_STATE_ERROR;
330         }
331         if ((state->ctx->state >= COMPOSITE_STATE_DONE) &&
332             (state->ctx->async.fn != NULL)) {
333                 state->ctx->async.fn(state->ctx);
334         }
335 }
336
337 NTSTATUS wb_get_schannel_creds_recv(struct composite_context *c,
338                                     TALLOC_CTX *mem_ctx,
339                                     struct dcerpc_pipe **netlogon_pipe)
340 {
341         NTSTATUS status = composite_wait(c);
342         if (NT_STATUS_IS_OK(status)) {
343                 struct get_schannel_creds_state *state =
344                         talloc_get_type(c->private_data,
345                                         struct get_schannel_creds_state);
346                 *netlogon_pipe = talloc_steal(mem_ctx, state->p);
347         }
348         talloc_free(c);
349         return status;
350 }
351
352 NTSTATUS wb_get_schannel_creds(struct cli_credentials *wks_creds,
353                                struct smbcli_tree *tree,
354                                struct event_context *event_ctx,
355                                TALLOC_CTX *mem_ctx,
356                                struct dcerpc_pipe **netlogon_pipe)
357 {
358         struct composite_context *c =
359                 wb_get_schannel_creds_send(wks_creds, tree, event_ctx);
360         return wb_get_schannel_creds_recv(c, mem_ctx, netlogon_pipe);
361 }
362
363 struct lsa_lookupnames_state {
364         struct composite_context *ctx;
365         uint32_t num_names;
366         struct lsa_LookupNames r;
367         struct lsa_TransSidArray sids;
368         uint32_t count;
369         struct wb_sid_object **result;
370 };
371
372 static void lsa_lookupnames_recv_sids(struct rpc_request *req);
373
374 struct composite_context *wb_lsa_lookupnames_send(struct dcerpc_pipe *lsa_pipe,
375                                                   struct policy_handle *handle,
376                                                   int num_names,
377                                                   const char **names)
378 {
379         struct composite_context *result;
380         struct rpc_request *req;
381         struct lsa_lookupnames_state *state;
382
383         struct lsa_String *lsa_names;
384         int i;
385
386         result = talloc_zero(NULL, struct composite_context);
387         if (result == NULL) goto failed;
388         result->state = COMPOSITE_STATE_IN_PROGRESS;
389         result->async.fn = NULL;
390         result->event_ctx = lsa_pipe->conn->event_ctx;
391
392         state = talloc(result, struct lsa_lookupnames_state);
393         if (state == NULL) goto failed;
394         result->private_data = state;
395         state->ctx = result;
396
397         state->sids.count = 0;
398         state->sids.sids = NULL;
399         state->num_names = num_names;
400         state->count = 0;
401
402         lsa_names = talloc_array(state, struct lsa_String, num_names);
403         if (lsa_names == NULL) goto failed;
404
405         for (i=0; i<num_names; i++) {
406                 lsa_names[i].string = names[i];
407         }
408
409         state->r.in.handle = handle;
410         state->r.in.num_names = num_names;
411         state->r.in.names = lsa_names;
412         state->r.in.sids = &state->sids;
413         state->r.in.level = 1;
414         state->r.in.count = &state->count;
415         state->r.out.count = &state->count;
416         state->r.out.sids = &state->sids;
417
418         req = dcerpc_lsa_LookupNames_send(lsa_pipe, state, &state->r);
419         if (req == NULL) goto failed;
420
421         req->async.callback = lsa_lookupnames_recv_sids;
422         req->async.private = state;
423         return result;
424
425  failed:
426         talloc_free(result);
427         return NULL;
428 }
429
430 static void lsa_lookupnames_recv_sids(struct rpc_request *req)
431 {
432         struct lsa_lookupnames_state *state =
433                 talloc_get_type(req->async.private,
434                                 struct lsa_lookupnames_state);
435         int i;
436
437         state->ctx->status = dcerpc_ndr_request_recv(req);
438         if (!composite_is_ok(state->ctx)) return;
439         state->ctx->status = state->r.out.result;
440         if (!NT_STATUS_IS_OK(state->ctx->status) &&
441             !NT_STATUS_EQUAL(state->ctx->status, STATUS_SOME_UNMAPPED)) {
442                 composite_error(state->ctx, state->ctx->status);
443                 return;
444         }
445
446         state->result = talloc_array(state, struct wb_sid_object *,
447                                      state->num_names);
448         if (composite_nomem(state->result, state->ctx)) return;
449
450         for (i=0; i<state->num_names; i++) {
451                 struct lsa_TranslatedSid *sid = &state->r.out.sids->sids[i];
452                 struct lsa_TrustInformation *dom;
453
454                 state->result[i] = talloc_zero(state->result,
455                                                struct wb_sid_object);
456                 if (composite_nomem(state->result[i], state->ctx)) return;
457
458                 state->result[i]->type = sid->sid_type;
459                 if (state->result[i]->type == SID_NAME_UNKNOWN) {
460                         continue;
461                 }
462
463                 if (sid->sid_index >= state->r.out.domains->count) {
464                         composite_error(state->ctx,
465                                         NT_STATUS_INVALID_PARAMETER);
466                         return;
467                 }
468
469                 dom = &state->r.out.domains->domains[sid->sid_index];
470
471                 state->result[i]->sid = dom_sid_add_rid(state->result[i],
472                                                         dom->sid, sid->rid);
473         }
474
475         composite_done(state->ctx);
476 }
477
478 NTSTATUS wb_lsa_lookupnames_recv(struct composite_context *c,
479                                  TALLOC_CTX *mem_ctx,
480                                  struct wb_sid_object ***sids)
481 {
482         NTSTATUS status = composite_wait(c);
483         if (NT_STATUS_IS_OK(status)) {
484                 struct lsa_lookupnames_state *state =
485                         talloc_get_type(c->private_data,
486                                         struct lsa_lookupnames_state);
487                 *sids = talloc_steal(mem_ctx, state->result);
488         }
489         talloc_free(c);
490         return status;
491 }
492
493 NTSTATUS wb_lsa_lookupnames(struct dcerpc_pipe *lsa_pipe, 
494                             struct policy_handle *handle,
495                             int num_names, const char **names,
496                             TALLOC_CTX *mem_ctx,
497                             struct wb_sid_object ***sids)
498 {
499         struct composite_context *c =
500                 wb_lsa_lookupnames_send(lsa_pipe, handle, num_names, names);
501         return wb_lsa_lookupnames_recv(c, mem_ctx, sids);
502 }
503
504 struct cmd_checkmachacc_state {
505         struct composite_context *ctx;
506         struct wbsrv_call *call;
507         struct wbsrv_domain *domain;
508 };
509
510 static void cmd_checkmachacc_recv_init(struct composite_context *ctx);
511
512 struct composite_context *wb_cmd_checkmachacc_send(struct wbsrv_call *call)
513 {
514         struct composite_context *result, *ctx;
515         struct cmd_checkmachacc_state *state;
516         struct wbsrv_service *service = call->wbconn->listen_socket->service;
517
518         result = talloc(call, struct composite_context);
519         if (result == NULL) goto failed;
520         result->state = COMPOSITE_STATE_IN_PROGRESS;
521         result->async.fn = NULL;
522         result->event_ctx = call->event_ctx;
523
524         state = talloc(result, struct cmd_checkmachacc_state);
525         if (state == NULL) goto failed;
526         state->ctx = result;
527         result->private_data = state;
528         state->call = call;
529
530         state->domain = service->domains;
531
532         ctx = wb_init_domain_send(state->domain, result->event_ctx, 
533                                   call->wbconn->conn->msg_ctx);
534         if (ctx == NULL) goto failed;
535         ctx->async.fn = cmd_checkmachacc_recv_init;
536         ctx->async.private_data = state;
537
538         return result;
539
540  failed:
541         talloc_free(result);
542         return NULL;
543 }
544
545 static void cmd_checkmachacc_recv_init(struct composite_context *ctx)
546 {
547         struct cmd_checkmachacc_state *state =
548                 talloc_get_type(ctx->async.private_data,
549                                 struct cmd_checkmachacc_state);
550
551         state->ctx->status = wb_init_domain_recv(ctx);
552         if (!composite_is_ok(state->ctx)) return;
553
554         composite_done(state->ctx);
555 }
556
557 NTSTATUS wb_cmd_checkmachacc_recv(struct composite_context *c)
558 {
559         NTSTATUS status = composite_wait(c);
560         talloc_free(c);
561         return status;
562 }
563
564 NTSTATUS wb_cmd_checkmachacc(struct wbsrv_call *call)
565 {
566         struct composite_context *c = wb_cmd_checkmachacc_send(call);
567         return wb_cmd_checkmachacc_recv(c);
568 }
569
570 static void composite_netr_LogonSamLogon_recv_rpc(struct rpc_request *req);
571
572 struct composite_context *composite_netr_LogonSamLogon_send(struct dcerpc_pipe *p,
573                                                             TALLOC_CTX *mem_ctx,
574                                                             struct netr_LogonSamLogon *r)
575 {
576         struct composite_context *result;
577         struct rpc_request *req;
578
579         result = talloc(mem_ctx, struct composite_context);
580         if (result == NULL) goto failed;
581         result->state = COMPOSITE_STATE_IN_PROGRESS;
582         result->async.fn = NULL;
583         result->event_ctx = p->conn->event_ctx;
584
585         req = dcerpc_netr_LogonSamLogon_send(p, mem_ctx, r);
586         if (req == NULL) goto failed;
587         req->async.callback = composite_netr_LogonSamLogon_recv_rpc;
588         req->async.private = result;
589         return result;
590
591  failed:
592         talloc_free(result);
593         return NULL;
594 }
595
596 static void composite_netr_LogonSamLogon_recv_rpc(struct rpc_request *req)
597 {
598         struct composite_context *ctx =
599                 talloc_get_type(req->async.private, struct composite_context);
600
601         ctx->status = dcerpc_ndr_request_recv(req);
602         if (!composite_is_ok(ctx)) return;
603         composite_done(ctx);
604 }
605
606 NTSTATUS composite_netr_LogonSamLogon_recv(struct composite_context *ctx)
607 {
608         NTSTATUS status = composite_wait(ctx);
609         talloc_free(ctx);
610         return status;
611 }
612