build: commit all the waf build files in the tree
[nivanova/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 3 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, see <http://www.gnu.org/licenses/>.
21 */
22
23 #include "includes.h"
24 #include "libcli/composite/composite.h"
25 #include "libcli/resolve/resolve.h"
26 #include "libcli/security/security.h"
27 #include "winbind/wb_server.h"
28 #include "smbd/service_task.h"
29 #include "libcli/finddcs.h"
30 #include "param/param.h"
31
32 struct get_dom_info_state {
33         struct composite_context *ctx;
34         struct wb_dom_info *info;
35 };
36
37 static void get_dom_info_recv_addrs(struct composite_context *ctx);
38
39 struct composite_context *wb_get_dom_info_send(TALLOC_CTX *mem_ctx,
40                                                struct wbsrv_service *service,
41                                                const char *domain_name,
42                                                const struct dom_sid *sid)
43 {
44         struct composite_context *result, *ctx;
45         struct get_dom_info_state *state;
46         struct dom_sid *dom_sid;
47         result = composite_create(mem_ctx, service->task->event_ctx);
48         if (result == NULL) goto failed;
49
50         state = talloc(result, struct get_dom_info_state);
51         if (state == NULL) goto failed;
52         state->ctx = result;
53         result->private_data = state;
54
55         state->info = talloc_zero(state, struct wb_dom_info);
56         if (state->info == NULL) goto failed;
57
58         state->info->name = talloc_strdup(state->info, domain_name);
59         if (state->info->name == NULL) goto failed;
60
61         state->info->sid = dom_sid_dup(state->info, sid);
62         if (state->info->sid == NULL) goto failed;
63
64         dom_sid = dom_sid_dup(mem_ctx, sid);
65         if (dom_sid == NULL) goto failed;
66
67         ctx = finddcs_send(mem_ctx, lp_netbios_name(service->task->lp_ctx),
68                            lp_nbt_port(service->task->lp_ctx),
69                            domain_name, NBT_NAME_LOGON, 
70                            dom_sid, 
71                            lp_iconv_convenience(service->task->lp_ctx),
72                            lp_resolve_context(service->task->lp_ctx), 
73                            service->task->event_ctx, 
74                            service->task->msg_ctx);
75         if (ctx == NULL) goto failed;
76
77         composite_continue(state->ctx, ctx, get_dom_info_recv_addrs, state);
78         return result;
79
80  failed:
81         talloc_free(result);
82         return NULL;
83 }
84
85 static void get_dom_info_recv_addrs(struct composite_context *ctx)
86 {
87         struct get_dom_info_state *state =
88                 talloc_get_type(ctx->async.private_data,
89                                 struct get_dom_info_state);
90
91         state->ctx->status = finddcs_recv(ctx, state->info,
92                                           &state->info->num_dcs,
93                                           &state->info->dcs);
94         if (!composite_is_ok(state->ctx)) return;
95
96         composite_done(state->ctx);
97 }
98
99 NTSTATUS wb_get_dom_info_recv(struct composite_context *ctx,
100                               TALLOC_CTX *mem_ctx,
101                               struct wb_dom_info **result)
102 {
103         NTSTATUS status = composite_wait(ctx);
104         if (NT_STATUS_IS_OK(status)) {
105                 struct get_dom_info_state *state =
106                         talloc_get_type(ctx->private_data,
107                                         struct get_dom_info_state);
108                 *result = talloc_steal(mem_ctx, state->info);
109         }
110         talloc_free(ctx);
111         return status;
112 }
113
114 NTSTATUS wb_get_dom_info(TALLOC_CTX *mem_ctx,
115                          struct wbsrv_service *service,
116                          const char *domain_name,
117                          const struct dom_sid *sid,
118                          struct wb_dom_info **result)
119 {
120         struct composite_context *ctx =
121                 wb_get_dom_info_send(mem_ctx, service, domain_name, sid);
122         return wb_get_dom_info_recv(ctx, mem_ctx, result);
123 }