fix memleaks by passing an mem_ctx to
[samba-svnmirror.git] / source / 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 "libcli/resolve/resolve.h"
27 #include "libcli/security/security.h"
28 #include "winbind/wb_server.h"
29 #include "smbd/service_task.h"
30 #include "librpc/gen_ndr/ndr_irpc.h"
31 #include "librpc/gen_ndr/samr.h"
32 #include "lib/messaging/irpc.h"
33
34 struct get_dom_info_state {
35         struct composite_context *ctx;
36         struct wbsrv_service *service;
37         struct nbtd_getdcname r;
38         struct wb_dom_info *info;
39 };
40
41 static void get_dom_info_recv_addrs(struct composite_context *ctx);
42 static void get_dom_info_recv_dcname(struct irpc_request *ireq);
43
44 struct composite_context *wb_get_dom_info_send(TALLOC_CTX *mem_ctx,
45                                                struct wbsrv_service *service,
46                                                const char *domain_name,
47                                                const struct dom_sid *sid)
48 {
49         struct composite_context *result, *ctx;
50         struct get_dom_info_state *state;
51         struct nbt_name name;
52
53         result = composite_create(mem_ctx, service->task->event_ctx);
54         if (result == NULL) goto failed;
55
56         state = talloc(result, struct get_dom_info_state);
57         if (state == NULL) goto failed;
58         state->ctx = result;
59         result->private_data = state;
60
61         state->service = service;
62
63         state->info = talloc_zero(state, struct wb_dom_info);
64         if (state->info == NULL) goto failed;
65
66         state->info->name = talloc_strdup(state->info, domain_name);
67         if (state->info->name == NULL) goto failed;
68         state->info->sid = dom_sid_dup(state->info, sid);
69         if (state->info->sid == NULL) goto failed;
70
71         make_nbt_name(&name, state->info->name, NBT_NAME_LOGON);
72
73         ctx = resolve_name_send(&name, result->event_ctx,
74                                 lp_name_resolve_order());
75         if (ctx == NULL) goto failed;
76
77         ctx->async.fn = get_dom_info_recv_addrs;
78         ctx->async.private_data = state;
79         return result;
80
81  failed:
82         talloc_free(result);
83         return NULL;
84 }
85
86 static void get_dom_info_recv_addrs(struct composite_context *ctx)
87 {
88         struct get_dom_info_state *state =
89                 talloc_get_type(ctx->async.private_data,
90                                 struct get_dom_info_state);
91         struct server_id *nbt_servers;
92         struct irpc_request *ireq;
93
94         state->ctx->status = resolve_name_recv(ctx, state->info,
95                                                &state->info->dc_address);
96         if (!composite_is_ok(state->ctx)) return;
97
98         nbt_servers = irpc_servers_byname(state->service->task->msg_ctx,
99                                           state, "nbt_server");
100         if ((nbt_servers == NULL) || (nbt_servers[0].id == 0)) {
101                 composite_error(state->ctx, NT_STATUS_NO_LOGON_SERVERS);
102                 return;
103         }
104
105         state->r.in.domainname = state->info->name;
106         state->r.in.ip_address = state->info->dc_address;
107         state->r.in.my_computername = lp_netbios_name();
108         state->r.in.my_accountname = talloc_asprintf(state, "%s$",
109                                                      lp_netbios_name());
110         if (composite_nomem(state->r.in.my_accountname, state->ctx)) return;
111         state->r.in.account_control = ACB_WSTRUST;
112         state->r.in.domain_sid = dom_sid_dup(state, state->info->sid);
113         if (composite_nomem(state->r.in.domain_sid, state->ctx)) return;
114
115         ireq = irpc_call_send(state->service->task->msg_ctx, nbt_servers[0],
116                               &dcerpc_table_irpc, DCERPC_NBTD_GETDCNAME,
117                               &state->r, state);
118         composite_continue_irpc(state->ctx, ireq, get_dom_info_recv_dcname,
119                                 state);
120 }
121
122 static void get_dom_info_recv_dcname(struct irpc_request *ireq)
123 {
124         struct get_dom_info_state *state =
125                 talloc_get_type(ireq->async.private,
126                                 struct get_dom_info_state);
127
128
129         state->ctx->status = irpc_call_recv(ireq);
130         if (!composite_is_ok(state->ctx)) return;
131
132         state->info->dc_name = talloc_steal(state->info, state->r.out.dcname);
133         composite_done(state->ctx);
134 }
135
136 NTSTATUS wb_get_dom_info_recv(struct composite_context *ctx,
137                               TALLOC_CTX *mem_ctx,
138                               struct wb_dom_info **result)
139 {
140         NTSTATUS status = composite_wait(ctx);
141         if (NT_STATUS_IS_OK(status)) {
142                 struct get_dom_info_state *state =
143                         talloc_get_type(ctx->private_data,
144                                         struct get_dom_info_state);
145                 *result = talloc_steal(mem_ctx, state->info);
146         }
147         talloc_free(ctx);
148         return status;
149 }
150
151 NTSTATUS wb_get_dom_info(TALLOC_CTX *mem_ctx,
152                          struct wbsrv_service *service,
153                          const char *domain_name,
154                          const struct dom_sid *sid,
155                          struct wb_dom_info **result)
156 {
157         struct composite_context *ctx =
158                 wb_get_dom_info_send(mem_ctx, service, domain_name, sid);
159         return wb_get_dom_info_recv(ctx, mem_ctx, result);
160 }