s4:winbind: use ncalrpc for connections to ourself
[samba.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/finddc.h"
30 #include "lib/socket/netif.h"
31 #include "param/param.h"
32
33 struct get_dom_info_state {
34         struct composite_context *ctx;
35         struct wb_dom_info *info;
36 };
37
38 static void get_dom_info_recv_addrs(struct tevent_req *req);
39
40 struct composite_context *wb_get_dom_info_send(TALLOC_CTX *mem_ctx,
41                                                struct wbsrv_service *service,
42                                                const char *domain_name,
43                                                const char *dns_domain_name,
44                                                const struct dom_sid *sid)
45 {
46         struct composite_context *result;
47         struct tevent_req *req;
48         struct get_dom_info_state *state;
49         struct dom_sid *dom_sid;
50         struct finddcs finddcs_io;
51
52         DEBUG(5, ("wb_get_dom_info_send called\n"));
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->info = talloc_zero(state, struct wb_dom_info);
62         if (state->info == NULL) goto failed;
63
64         state->info->name = talloc_strdup(state->info, domain_name);
65         if (state->info->name == NULL) goto failed;
66
67         state->info->sid = dom_sid_dup(state->info, sid);
68         if (state->info->sid == NULL) goto failed;
69
70         if ((lpcfg_server_role(service->task->lp_ctx) != ROLE_DOMAIN_MEMBER) &&
71             dom_sid_equal(sid, service->primary_sid) &&
72             service->sec_channel_type != SEC_CHAN_RODC) {
73                 struct interface *ifaces = NULL;
74
75                 load_interface_list(state, service->task->lp_ctx, &ifaces);
76
77                 state->info->dc = talloc(state->info, struct nbt_dc_name);
78
79                 state->info->dc->address = talloc_strdup(state->info->dc,
80                                                 iface_list_n_ip(ifaces, 0));
81                 state->info->dc->name = talloc_strdup(state->info->dc,
82                                                 lpcfg_netbios_name(service->task->lp_ctx));
83
84                 composite_done(state->ctx);
85                 return result;
86         }
87
88         dom_sid = dom_sid_dup(mem_ctx, sid);
89         if (dom_sid == NULL) goto failed;
90
91         ZERO_STRUCT(finddcs_io);
92         finddcs_io.in.domain_name      = dns_domain_name;
93         finddcs_io.in.domain_sid       = dom_sid;
94         finddcs_io.in.minimum_dc_flags = NBT_SERVER_LDAP | NBT_SERVER_DS;
95         if (service->sec_channel_type == SEC_CHAN_RODC) {
96                 finddcs_io.in.minimum_dc_flags |= NBT_SERVER_WRITABLE;
97         }
98
99         req = finddcs_cldap_send(mem_ctx, &finddcs_io,
100                                  lpcfg_resolve_context(service->task->lp_ctx),
101                                  service->task->event_ctx);
102         if (req == NULL) goto failed;
103
104         tevent_req_set_callback(req, get_dom_info_recv_addrs, state);
105
106         return result;
107
108  failed:
109         talloc_free(result);
110         return NULL;
111 }
112
113 static void get_dom_info_recv_addrs(struct tevent_req *req)
114 {
115         struct get_dom_info_state *state = tevent_req_callback_data(req, struct get_dom_info_state);
116         struct finddcs finddcs_io;
117
118         state->info->dc = talloc(state->info, struct nbt_dc_name);
119
120         state->ctx->status = finddcs_cldap_recv(req, state->info, &finddcs_io);
121         if (!composite_is_ok(state->ctx)) return;
122
123         if (finddcs_io.out.netlogon.ntver != NETLOGON_NT_VERSION_5EX) {
124                 /* the finddcs code should have mapped the response to
125                    the type we want */
126                 DEBUG(0,(__location__ ": unexpected ntver 0x%08x in finddcs response\n",
127                          finddcs_io.out.netlogon.ntver));
128                 state->ctx->status = NT_STATUS_UNEXPECTED_NETWORK_ERROR;
129                 if (!composite_is_ok(state->ctx)) return;
130         }
131
132         state->info->dc->address = finddcs_io.out.address;
133         state->info->dc->name    = finddcs_io.out.netlogon.data.nt5_ex.pdc_dns_name;
134
135         composite_done(state->ctx);
136 }
137
138 NTSTATUS wb_get_dom_info_recv(struct composite_context *ctx,
139                               TALLOC_CTX *mem_ctx,
140                               struct wb_dom_info **result)
141 {
142         NTSTATUS status = composite_wait(ctx);
143         if (NT_STATUS_IS_OK(status)) {
144                 struct get_dom_info_state *state =
145                         talloc_get_type(ctx->private_data,
146                                         struct get_dom_info_state);
147                 *result = talloc_steal(mem_ctx, state->info);
148         }
149         talloc_free(ctx);
150         return status;
151 }
152
153 NTSTATUS wb_get_dom_info(TALLOC_CTX *mem_ctx,
154                          struct wbsrv_service *service,
155                          const char *domain_name,
156                          const char *dns_domain_name,
157                          const struct dom_sid *sid,
158                          struct wb_dom_info **result)
159 {
160         struct composite_context *ctx =
161                 wb_get_dom_info_send(mem_ctx, service, domain_name, dns_domain_name, sid);
162         return wb_get_dom_info_recv(ctx, mem_ctx, result);
163 }