r23141: Use the finddcs() library call rather than a winbind-specific version.
[ira/wip.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 #include "libcli/finddcs.h"
34
35 struct get_dom_info_state {
36         struct composite_context *ctx;
37         struct wb_dom_info *info;
38 };
39
40 static void get_dom_info_recv_addrs(struct composite_context *ctx);
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 dom_sid *dup_sid;
50         result = composite_create(mem_ctx, service->task->event_ctx);
51         if (result == NULL) goto failed;
52
53         state = talloc(result, struct get_dom_info_state);
54         if (state == NULL) goto failed;
55         state->ctx = result;
56         result->private_data = state;
57
58         state->info = talloc_zero(state, struct wb_dom_info);
59         if (state->info == NULL) goto failed;
60
61         dup_sid = dom_sid_dup(state, sid);
62         if (dup_sid == NULL) goto failed;
63
64         ctx = finddcs_send(mem_ctx, domain_name, NBT_NAME_LOGON, 
65                            dup_sid, lp_name_resolve_order(), service->task->event_ctx, 
66                            service->task->msg_ctx);
67         if (ctx == NULL) goto failed;
68
69         composite_continue(state->ctx, ctx, get_dom_info_recv_addrs, state);
70         return result;
71
72  failed:
73         talloc_free(result);
74         return NULL;
75 }
76
77 static void get_dom_info_recv_addrs(struct composite_context *ctx)
78 {
79         struct get_dom_info_state *state =
80                 talloc_get_type(ctx->async.private_data,
81                                 struct get_dom_info_state);
82
83         state->ctx->status = finddcs_recv(ctx, state->info,
84                                           &state->info->num_dcs,
85                                           &state->info->dcs);
86         if (!composite_is_ok(state->ctx)) return;
87
88         composite_done(state->ctx);
89 }
90
91 NTSTATUS wb_get_dom_info_recv(struct composite_context *ctx,
92                               TALLOC_CTX *mem_ctx,
93                               struct wb_dom_info **result)
94 {
95         NTSTATUS status = composite_wait(ctx);
96         if (NT_STATUS_IS_OK(status)) {
97                 struct get_dom_info_state *state =
98                         talloc_get_type(ctx->private_data,
99                                         struct get_dom_info_state);
100                 *result = talloc_steal(mem_ctx, state->info);
101         }
102         talloc_free(ctx);
103         return status;
104 }
105
106 NTSTATUS wb_get_dom_info(TALLOC_CTX *mem_ctx,
107                          struct wbsrv_service *service,
108                          const char *domain_name,
109                          const struct dom_sid *sid,
110                          struct wb_dom_info **result)
111 {
112         struct composite_context *ctx =
113                 wb_get_dom_info_send(mem_ctx, service, domain_name, sid);
114         return wb_get_dom_info_recv(ctx, mem_ctx, result);
115 }