r23792: convert Samba4 to GPLv3
[abartlet/samba.git/.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 "librpc/gen_ndr/ndr_irpc.h"
30 #include "librpc/gen_ndr/samr.h"
31 #include "lib/messaging/irpc.h"
32 #include "libcli/finddcs.h"
33
34 struct get_dom_info_state {
35         struct composite_context *ctx;
36         struct wb_dom_info *info;
37 };
38
39 static void get_dom_info_recv_addrs(struct composite_context *ctx);
40
41 struct composite_context *wb_get_dom_info_send(TALLOC_CTX *mem_ctx,
42                                                struct wbsrv_service *service,
43                                                const char *domain_name,
44                                                const struct dom_sid *sid)
45 {
46         struct composite_context *result, *ctx;
47         struct get_dom_info_state *state;
48         struct dom_sid *dup_sid;
49         result = composite_create(mem_ctx, service->task->event_ctx);
50         if (result == NULL) goto failed;
51
52         state = talloc(result, struct get_dom_info_state);
53         if (state == NULL) goto failed;
54         state->ctx = result;
55         result->private_data = state;
56
57         state->info = talloc_zero(state, struct wb_dom_info);
58         if (state->info == NULL) goto failed;
59
60         dup_sid = dom_sid_dup(state, sid);
61         if (dup_sid == NULL) goto failed;
62
63         ctx = finddcs_send(mem_ctx, domain_name, NBT_NAME_LOGON, 
64                            dup_sid, lp_name_resolve_order(), service->task->event_ctx, 
65                            service->task->msg_ctx);
66         if (ctx == NULL) goto failed;
67
68         composite_continue(state->ctx, ctx, get_dom_info_recv_addrs, state);
69         return result;
70
71  failed:
72         talloc_free(result);
73         return NULL;
74 }
75
76 static void get_dom_info_recv_addrs(struct composite_context *ctx)
77 {
78         struct get_dom_info_state *state =
79                 talloc_get_type(ctx->async.private_data,
80                                 struct get_dom_info_state);
81
82         state->ctx->status = finddcs_recv(ctx, state->info,
83                                           &state->info->num_dcs,
84                                           &state->info->dcs);
85         if (!composite_is_ok(state->ctx)) return;
86
87         composite_done(state->ctx);
88 }
89
90 NTSTATUS wb_get_dom_info_recv(struct composite_context *ctx,
91                               TALLOC_CTX *mem_ctx,
92                               struct wb_dom_info **result)
93 {
94         NTSTATUS status = composite_wait(ctx);
95         if (NT_STATUS_IS_OK(status)) {
96                 struct get_dom_info_state *state =
97                         talloc_get_type(ctx->private_data,
98                                         struct get_dom_info_state);
99                 *result = talloc_steal(mem_ctx, state->info);
100         }
101         talloc_free(ctx);
102         return status;
103 }
104
105 NTSTATUS wb_get_dom_info(TALLOC_CTX *mem_ctx,
106                          struct wbsrv_service *service,
107                          const char *domain_name,
108                          const struct dom_sid *sid,
109                          struct wb_dom_info **result)
110 {
111         struct composite_context *ctx =
112                 wb_get_dom_info_send(mem_ctx, service, domain_name, sid);
113         return wb_get_dom_info_recv(ctx, mem_ctx, result);
114 }