1b327883389072b4dc453a416edf66bbb7a4415c
[bbaumbach/samba-autobuild/.git] / source4 / winbind / wb_dom_info.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    Get a struct wb_dom_info for a domain using DNS, netbios, possibly cldap
5    etc.
6
7    Copyright (C) Volker Lendecke 2005
8    
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 2 of the License, or
12    (at your option) any later version.
13    
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18    
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software
21    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 */
23
24 #include "includes.h"
25 #include "libcli/composite/composite.h"
26 #include "winbind/wb_server.h"
27 #include "smbd/service_task.h"
28 #include "librpc/gen_ndr/ndr_irpc.h"
29 #include "librpc/gen_ndr/samr.h"
30 #include "lib/messaging/irpc.h"
31
32 struct get_dom_info_state {
33         struct composite_context *ctx;
34         struct wbsrv_service *service;
35         struct nbtd_getdcname r;
36         struct wb_dom_info *info;
37 };
38
39 static void get_dom_info_recv_addrs(struct composite_context *ctx);
40 static void get_dom_info_recv_dcname(struct irpc_request *ireq);
41
42 struct composite_context *wb_get_dom_info_send(TALLOC_CTX *mem_ctx,
43                                                struct wbsrv_service *service,
44                                                const char *domain_name,
45                                                const struct dom_sid *sid)
46 {
47         struct composite_context *result, *ctx;
48         struct get_dom_info_state *state;
49         struct nbt_name name;
50
51         result = talloc(mem_ctx, struct composite_context);
52         if (result == NULL) goto failed;
53         result->state = COMPOSITE_STATE_IN_PROGRESS;
54         result->async.fn = NULL;
55         result->event_ctx = service->task->event_ctx;
56
57         state = talloc(result, struct get_dom_info_state);
58         if (state == NULL) goto failed;
59         state->ctx = result;
60         result->private_data = state;
61
62         state->service = service;
63
64         state->info = talloc_zero(state, struct wb_dom_info);
65         if (state->info == NULL) goto failed;
66
67         state->info->name = talloc_strdup(state->info, domain_name);
68         if (state->info->name == NULL) goto failed;
69         state->info->sid = dom_sid_dup(state->info, sid);
70         if (state->info->sid == NULL) goto failed;
71
72         make_nbt_name(&name, state->info->name, NBT_NAME_LOGON);
73
74         ctx = resolve_name_send(&name, result->event_ctx,
75                                 lp_name_resolve_order());
76         if (ctx == NULL) goto failed;
77
78         ctx->async.fn = get_dom_info_recv_addrs;
79         ctx->async.private_data = state;
80         return result;
81
82  failed:
83         talloc_free(result);
84         return NULL;
85 }
86
87 static void get_dom_info_recv_addrs(struct composite_context *ctx)
88 {
89         struct get_dom_info_state *state =
90                 talloc_get_type(ctx->async.private_data,
91                                 struct get_dom_info_state);
92         uint32_t *nbt_servers;
93         struct irpc_request *ireq;
94
95         state->ctx->status = resolve_name_recv(ctx, state->info,
96                                                &state->info->dc_address);
97         if (!composite_is_ok(state->ctx)) return;
98
99         nbt_servers = irpc_servers_byname(state->service->task->msg_ctx,
100                                           "nbt_server");
101         if ((nbt_servers == NULL) || (nbt_servers[0] == 0)) {
102                 composite_error(state->ctx, NT_STATUS_NO_LOGON_SERVERS);
103                 return;
104         }
105
106         state->r.in.domainname = state->info->name;
107         state->r.in.ip_address = state->info->dc_address;
108         state->r.in.my_computername = lp_netbios_name();
109         state->r.in.my_accountname = talloc_asprintf(state, "%s$",
110                                                      lp_netbios_name());
111         if (composite_nomem(state->r.in.my_accountname, state->ctx)) return;
112         state->r.in.account_control = ACB_WSTRUST;
113         state->r.in.domain_sid = dom_sid_dup(state, state->info->sid);
114         if (composite_nomem(state->r.in.domain_sid, state->ctx)) return;
115
116         ireq = irpc_call_send(state->service->task->msg_ctx, nbt_servers[0],
117                               &dcerpc_table_irpc, DCERPC_NBTD_GETDCNAME,
118                               &state->r, state);
119         composite_continue_irpc(state->ctx, ireq, get_dom_info_recv_dcname,
120                                 state);
121 }
122
123 static void get_dom_info_recv_dcname(struct irpc_request *ireq)
124 {
125         struct get_dom_info_state *state =
126                 talloc_get_type(ireq->async.private,
127                                 struct get_dom_info_state);
128
129
130         state->ctx->status = irpc_call_recv(ireq);
131         if (!composite_is_ok(state->ctx)) return;
132
133         state->info->dc_name = talloc_steal(state->info, state->r.out.dcname);
134         composite_done(state->ctx);
135 }
136
137 NTSTATUS wb_get_dom_info_recv(struct composite_context *ctx,
138                               TALLOC_CTX *mem_ctx,
139                               struct wb_dom_info **result)
140 {
141         NTSTATUS status = composite_wait(ctx);
142         if (NT_STATUS_IS_OK(status)) {
143                 struct get_dom_info_state *state =
144                         talloc_get_type(ctx->private_data,
145                                         struct get_dom_info_state);
146                 *result = talloc_steal(mem_ctx, state->info);
147         }
148         talloc_free(ctx);
149         return status;
150 }
151
152 NTSTATUS wb_get_dom_info(TALLOC_CTX *mem_ctx,
153                          struct wbsrv_service *service,
154                          const char *domain_name,
155                          const struct dom_sid *sid,
156                          struct wb_dom_info **result)
157 {
158         struct composite_context *ctx =
159                 wb_get_dom_info_send(mem_ctx, service, domain_name, sid);
160         return wb_get_dom_info_recv(ctx, mem_ctx, result);
161 }