Specify event_context to ldb_wrap_connect explicitly.
[kai/samba-autobuild/.git] / source4 / nbt_server / dgram / netlogon.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    NBT datagram netlogon server
5
6    Copyright (C) Andrew Tridgell        2005
7    
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include "includes.h"
23 #include "nbt_server/nbt_server.h"
24 #include "lib/socket/socket.h"
25 #include "lib/ldb/include/ldb.h"
26 #include "dsdb/samdb/samdb.h"
27 #include "auth/auth.h"
28 #include "util/util_ldb.h"
29 #include "librpc/gen_ndr/ndr_nbt.h"
30 #include "param/param.h"
31 #include "smbd/service_task.h"
32
33 /*
34   reply to a GETDC request
35  */
36 static void nbtd_netlogon_getdc(struct dgram_mailslot_handler *dgmslot, 
37                                 struct nbtd_interface *iface,
38                                 struct nbt_dgram_packet *packet, 
39                                 const struct socket_address *src,
40                                 struct nbt_netlogon_packet *netlogon)
41 {
42         struct nbt_name *name = &packet->data.msg.dest_name;
43         struct nbtd_interface *reply_iface = nbtd_find_reply_iface(iface, src->addr, false);
44         struct nbt_netlogon_packet reply;
45         struct nbt_netlogon_response_from_pdc *pdc;
46         const char *ref_attrs[] = {"nETBIOSName", NULL};
47         struct ldb_message **ref_res;
48         struct ldb_context *samctx;
49         struct ldb_dn *partitions_basedn;
50         int ret;
51
52         /* only answer getdc requests on the PDC or LOGON names */
53         if (name->type != NBT_NAME_PDC && name->type != NBT_NAME_LOGON) {
54                 return;
55         }
56
57         samctx = samdb_connect(packet, iface->nbtsrv->task->event_ctx, iface->nbtsrv->task->lp_ctx, anonymous_session(packet, iface->nbtsrv->task->event_ctx, iface->nbtsrv->task->lp_ctx));
58         if (samctx == NULL) {
59                 DEBUG(2,("Unable to open sam in getdc reply\n"));
60                 return;
61         }
62
63         partitions_basedn = samdb_partitions_dn(samctx, packet);
64
65         ret = gendb_search(samctx, packet, partitions_basedn, &ref_res, ref_attrs,
66                            "(&(&(nETBIOSName=%s)(objectclass=crossRef))(ncName=*))", 
67                            name->name);
68         
69         if (ret != 1) {
70                 DEBUG(2,("Unable to find domain reference '%s' in sam\n", name->name));
71                 return;
72         }
73
74         /* setup a GETDC reply */
75         ZERO_STRUCT(reply);
76         reply.command = NETLOGON_RESPONSE_FROM_PDC;
77         pdc = &reply.req.response;
78
79         pdc->pdc_name         = lp_netbios_name(iface->nbtsrv->task->lp_ctx);
80         pdc->unicode_pdc_name = pdc->pdc_name;
81         pdc->domain_name      = samdb_result_string(ref_res[0], "nETBIOSName", name->name);;
82         pdc->nt_version       = 1;
83         pdc->lmnt_token       = 0xFFFF;
84         pdc->lm20_token       = 0xFFFF;
85
86
87         packet->data.msg.dest_name.type = 0;
88
89         dgram_mailslot_netlogon_reply(reply_iface->dgmsock, 
90                                       packet, 
91                                       lp_netbios_name(iface->nbtsrv->task->lp_ctx),
92                                       netlogon->req.pdc.mailslot_name,
93                                       &reply);
94 }
95
96
97 /*
98   reply to a ADS style GETDC request
99  */
100 static void nbtd_netlogon_getdc2(struct dgram_mailslot_handler *dgmslot,
101                                  struct nbtd_interface *iface,
102                                  struct nbt_dgram_packet *packet, 
103                                  const struct socket_address *src,
104                                  struct nbt_netlogon_packet *netlogon)
105 {
106         struct nbt_name *name = &packet->data.msg.dest_name;
107         struct nbtd_interface *reply_iface = nbtd_find_reply_iface(iface, src->addr, false);
108         struct nbt_netlogon_packet reply;
109         struct nbt_netlogon_response_from_pdc2 *pdc;
110         struct ldb_context *samctx;
111         const char *ref_attrs[] = {"nETBIOSName", "dnsRoot", "ncName", NULL};
112         const char *dom_attrs[] = {"objectGUID", NULL};
113         struct ldb_message **ref_res, **dom_res;
114         int ret;
115         const char **services = lp_server_services(iface->nbtsrv->task->lp_ctx);
116         const char *my_ip = reply_iface->ip_address; 
117         struct ldb_dn *partitions_basedn;
118         if (!my_ip) {
119                 DEBUG(0, ("Could not obtain own IP address for datagram socket\n"));
120                 return;
121         }
122
123         /* only answer getdc requests on the PDC or LOGON names */
124         if (name->type != NBT_NAME_PDC && name->type != NBT_NAME_LOGON) {
125                 return;
126         }
127
128         samctx = samdb_connect(packet, iface->nbtsrv->task->event_ctx, iface->nbtsrv->task->lp_ctx, anonymous_session(packet, iface->nbtsrv->task->event_ctx, iface->nbtsrv->task->lp_ctx));
129         if (samctx == NULL) {
130                 DEBUG(2,("Unable to open sam in getdc reply\n"));
131                 return;
132         }
133
134         partitions_basedn = samdb_partitions_dn(samctx, packet);
135
136         ret = gendb_search(samctx, packet, partitions_basedn, &ref_res, ref_attrs,
137                                   "(&(&(nETBIOSName=%s)(objectclass=crossRef))(ncName=*))", 
138                                   name->name);
139         
140         if (ret != 1) {
141                 DEBUG(2,("Unable to find domain reference '%s' in sam\n", name->name));
142                 return;
143         }
144
145         /* try and find the domain */
146         ret = gendb_search_dn(samctx, packet, 
147                               samdb_result_dn(samctx, samctx, ref_res[0], "ncName", NULL), 
148                               &dom_res, dom_attrs);
149         if (ret != 1) {
150                 DEBUG(2,("Unable to find domain from reference '%s' in sam\n",
151                          ldb_dn_get_linearized(ref_res[0]->dn)));
152                 return;
153         }
154
155         /* setup a GETDC reply */
156         ZERO_STRUCT(reply);
157         reply.command = NETLOGON_RESPONSE_FROM_PDC2;
158
159 #if 0
160         /* newer testing shows that the reply command type is not
161            changed based on whether a username is given in the
162            reply. This was what was causing the w2k join to be so
163            slow */
164         if (netlogon->req.pdc2.user_name[0]) {
165                 reply.command = NETLOGON_RESPONSE_FROM_PDC_USER;
166         }
167 #endif
168
169         pdc = &reply.req.response2;
170
171         /* TODO: accurately depict which services we are running */
172         pdc->server_type      = 
173                 NBT_SERVER_PDC | NBT_SERVER_GC | 
174                 NBT_SERVER_DS | NBT_SERVER_TIMESERV |
175                 NBT_SERVER_CLOSEST | NBT_SERVER_WRITABLE | 
176                 NBT_SERVER_GOOD_TIMESERV;
177
178         /* hmm, probably a better way to do this */
179         if (str_list_check(services, "ldap")) {
180                 pdc->server_type |= NBT_SERVER_LDAP;
181         }
182
183         if (str_list_check(services, "kdc")) {
184                 pdc->server_type |= NBT_SERVER_KDC;
185         }
186
187         pdc->domain_uuid      = samdb_result_guid(dom_res[0], "objectGUID");
188         pdc->forest           = samdb_result_string(ref_res[0], "dnsRoot", 
189                                                     lp_realm(iface->nbtsrv->task->lp_ctx));
190         pdc->dns_domain       = samdb_result_string(ref_res[0], "dnsRoot", 
191                                                     lp_realm(iface->nbtsrv->task->lp_ctx));
192
193         /* TODO: get our full DNS name from somewhere else */
194         pdc->pdc_dns_name     = talloc_asprintf(packet, "%s.%s", 
195                                                 strlower_talloc(packet, 
196                                                                 lp_netbios_name(iface->nbtsrv->task->lp_ctx)), 
197                                                 pdc->dns_domain);
198         pdc->domain           = samdb_result_string(ref_res[0], "nETBIOSName", name->name);;
199         pdc->pdc_name         = lp_netbios_name(iface->nbtsrv->task->lp_ctx);
200         pdc->user_name        = netlogon->req.pdc2.user_name;
201         /* TODO: we need to make sure these are in our DNS zone */
202         pdc->server_site      = "Default-First-Site-Name";
203         pdc->client_site      = "Default-First-Site-Name";
204         pdc->unknown          = 0x10; /* what is this? */
205         pdc->unknown2         = 2; /* and this ... */
206         pdc->pdc_ip           = my_ip;
207         pdc->nt_version       = 13;
208         pdc->lmnt_token       = 0xFFFF;
209         pdc->lm20_token       = 0xFFFF;
210
211         packet->data.msg.dest_name.type = 0;
212
213         dgram_mailslot_netlogon_reply(reply_iface->dgmsock, 
214                                       packet, 
215                                       lp_netbios_name(iface->nbtsrv->task->lp_ctx),
216                                       netlogon->req.pdc2.mailslot_name,
217                                       &reply);
218 }
219
220
221 /*
222   handle incoming netlogon mailslot requests
223 */
224 void nbtd_mailslot_netlogon_handler(struct dgram_mailslot_handler *dgmslot, 
225                                     struct nbt_dgram_packet *packet, 
226                                     struct socket_address *src)
227 {
228         NTSTATUS status = NT_STATUS_NO_MEMORY;
229         struct nbtd_interface *iface = 
230                 talloc_get_type(dgmslot->private, struct nbtd_interface);
231         struct nbt_netlogon_packet *netlogon = 
232                 talloc(dgmslot, struct nbt_netlogon_packet);
233         struct nbtd_iface_name *iname;
234         struct nbt_name *name = &packet->data.msg.dest_name;
235
236         if (netlogon == NULL) goto failed;
237
238         /*
239           see if the we are listening on the destination netbios name
240         */
241         iname = nbtd_find_iname(iface, name, 0);
242         if (iname == NULL) {
243                 status = NT_STATUS_BAD_NETWORK_NAME;
244                 goto failed;
245         }
246
247         DEBUG(2,("netlogon request to %s from %s:%d\n", 
248                  nbt_name_string(netlogon, name), src->addr, src->port));
249         status = dgram_mailslot_netlogon_parse(dgmslot, netlogon, packet, netlogon);
250         if (!NT_STATUS_IS_OK(status)) goto failed;
251
252         switch (netlogon->command) {
253         case NETLOGON_QUERY_FOR_PDC:
254                 nbtd_netlogon_getdc(dgmslot, iface, packet, src, netlogon);
255                 break;
256         case NETLOGON_QUERY_FOR_PDC2:
257                 nbtd_netlogon_getdc2(dgmslot, iface, packet, src, netlogon);
258                 break;
259         default:
260                 DEBUG(2,("unknown netlogon op %d from %s:%d\n", 
261                          netlogon->command, src->addr, src->port));
262                 NDR_PRINT_DEBUG(nbt_netlogon_packet, netlogon);
263                 break;
264         }
265
266         talloc_free(netlogon);
267         return;
268
269 failed:
270         DEBUG(2,("nbtd netlogon handler failed from %s:%d to %s - %s\n",
271                  src->addr, src->port, nbt_name_string(netlogon, name),
272                  nt_errstr(status)));
273         talloc_free(netlogon);
274 }