r26003: Split up DB_WRAP, as first step in an attempt to sanitize dependencies.
[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
32 /*
33   reply to a GETDC request
34  */
35 static void nbtd_netlogon_getdc(struct dgram_mailslot_handler *dgmslot, 
36                                 struct nbtd_interface *iface,
37                                 struct nbt_dgram_packet *packet, 
38                                 const struct socket_address *src,
39                                 struct nbt_netlogon_packet *netlogon)
40 {
41         struct nbt_name *name = &packet->data.msg.dest_name;
42         struct nbtd_interface *reply_iface = nbtd_find_reply_iface(iface, src->addr, false);
43         struct nbt_netlogon_packet reply;
44         struct nbt_netlogon_response_from_pdc *pdc;
45         const char *ref_attrs[] = {"nETBIOSName", NULL};
46         struct ldb_message **ref_res;
47         struct ldb_context *samctx;
48         struct ldb_dn *partitions_basedn;
49         int ret;
50
51         /* only answer getdc requests on the PDC or LOGON names */
52         if (name->type != NBT_NAME_PDC && name->type != NBT_NAME_LOGON) {
53                 return;
54         }
55
56         samctx = samdb_connect(packet, anonymous_session(packet));
57         if (samctx == NULL) {
58                 DEBUG(2,("Unable to open sam in getdc reply\n"));
59                 return;
60         }
61
62         partitions_basedn = samdb_partitions_dn(samctx, packet);
63
64         ret = gendb_search(samctx, packet, partitions_basedn, &ref_res, ref_attrs,
65                            "(&(&(nETBIOSName=%s)(objectclass=crossRef))(ncName=*))", 
66                            name->name);
67         
68         if (ret != 1) {
69                 DEBUG(2,("Unable to find domain reference '%s' in sam\n", name->name));
70                 return;
71         }
72
73         /* setup a GETDC reply */
74         ZERO_STRUCT(reply);
75         reply.command = NETLOGON_RESPONSE_FROM_PDC;
76         pdc = &reply.req.response;
77
78         pdc->pdc_name         = lp_netbios_name(global_loadparm);
79         pdc->unicode_pdc_name = pdc->pdc_name;
80         pdc->domain_name      = samdb_result_string(ref_res[0], "nETBIOSName", name->name);;
81         pdc->nt_version       = 1;
82         pdc->lmnt_token       = 0xFFFF;
83         pdc->lm20_token       = 0xFFFF;
84
85
86         packet->data.msg.dest_name.type = 0;
87
88         dgram_mailslot_netlogon_reply(reply_iface->dgmsock, 
89                                       packet, 
90                                       netlogon->req.pdc.mailslot_name,
91                                       &reply);
92 }
93
94
95 /*
96   reply to a ADS style GETDC request
97  */
98 static void nbtd_netlogon_getdc2(struct dgram_mailslot_handler *dgmslot,
99                                  struct nbtd_interface *iface,
100                                  struct nbt_dgram_packet *packet, 
101                                  const struct socket_address *src,
102                                  struct nbt_netlogon_packet *netlogon)
103 {
104         struct nbt_name *name = &packet->data.msg.dest_name;
105         struct nbtd_interface *reply_iface = nbtd_find_reply_iface(iface, src->addr, false);
106         struct nbt_netlogon_packet reply;
107         struct nbt_netlogon_response_from_pdc2 *pdc;
108         struct ldb_context *samctx;
109         const char *ref_attrs[] = {"nETBIOSName", "dnsRoot", "ncName", NULL};
110         const char *dom_attrs[] = {"objectGUID", NULL};
111         struct ldb_message **ref_res, **dom_res;
112         int ret;
113         const char **services = lp_server_services(global_loadparm);
114         const char *my_ip = reply_iface->ip_address; 
115         struct ldb_dn *partitions_basedn;
116         if (!my_ip) {
117                 DEBUG(0, ("Could not obtain own IP address for datagram socket\n"));
118                 return;
119         }
120
121         /* only answer getdc requests on the PDC or LOGON names */
122         if (name->type != NBT_NAME_PDC && name->type != NBT_NAME_LOGON) {
123                 return;
124         }
125
126         samctx = samdb_connect(packet, anonymous_session(packet));
127         if (samctx == NULL) {
128                 DEBUG(2,("Unable to open sam in getdc reply\n"));
129                 return;
130         }
131
132         partitions_basedn = samdb_partitions_dn(samctx, packet);
133
134         ret = gendb_search(samctx, packet, partitions_basedn, &ref_res, ref_attrs,
135                                   "(&(&(nETBIOSName=%s)(objectclass=crossRef))(ncName=*))", 
136                                   name->name);
137         
138         if (ret != 1) {
139                 DEBUG(2,("Unable to find domain reference '%s' in sam\n", name->name));
140                 return;
141         }
142
143         /* try and find the domain */
144         ret = gendb_search_dn(samctx, packet, 
145                               samdb_result_dn(samctx, samctx, ref_res[0], "ncName", NULL), 
146                               &dom_res, dom_attrs);
147         if (ret != 1) {
148                 DEBUG(2,("Unable to find domain from reference '%s' in sam\n",
149                          ldb_dn_get_linearized(ref_res[0]->dn)));
150                 return;
151         }
152
153         /* setup a GETDC reply */
154         ZERO_STRUCT(reply);
155         reply.command = NETLOGON_RESPONSE_FROM_PDC2;
156
157 #if 0
158         /* newer testing shows that the reply command type is not
159            changed based on whether a username is given in the
160            reply. This was what was causing the w2k join to be so
161            slow */
162         if (netlogon->req.pdc2.user_name[0]) {
163                 reply.command = NETLOGON_RESPONSE_FROM_PDC_USER;
164         }
165 #endif
166
167         pdc = &reply.req.response2;
168
169         /* TODO: accurately depict which services we are running */
170         pdc->server_type      = 
171                 NBT_SERVER_PDC | NBT_SERVER_GC | 
172                 NBT_SERVER_DS | NBT_SERVER_TIMESERV |
173                 NBT_SERVER_CLOSEST | NBT_SERVER_WRITABLE | 
174                 NBT_SERVER_GOOD_TIMESERV;
175
176         /* hmm, probably a better way to do this */
177         if (str_list_check(services, "ldap")) {
178                 pdc->server_type |= NBT_SERVER_LDAP;
179         }
180
181         if (str_list_check(services, "kdc")) {
182                 pdc->server_type |= NBT_SERVER_KDC;
183         }
184
185         pdc->domain_uuid      = samdb_result_guid(dom_res[0], "objectGUID");
186         pdc->forest           = samdb_result_string(ref_res[0], "dnsRoot", 
187                                                     lp_realm(global_loadparm));
188         pdc->dns_domain       = samdb_result_string(ref_res[0], "dnsRoot", 
189                                                     lp_realm(global_loadparm));
190
191         /* TODO: get our full DNS name from somewhere else */
192         pdc->pdc_dns_name     = talloc_asprintf(packet, "%s.%s", 
193                                                 strlower_talloc(packet, 
194                                                                 lp_netbios_name(global_loadparm)), 
195                                                 pdc->dns_domain);
196         pdc->domain           = samdb_result_string(ref_res[0], "nETBIOSName", name->name);;
197         pdc->pdc_name         = lp_netbios_name(global_loadparm);
198         pdc->user_name        = netlogon->req.pdc2.user_name;
199         /* TODO: we need to make sure these are in our DNS zone */
200         pdc->server_site      = "Default-First-Site-Name";
201         pdc->client_site      = "Default-First-Site-Name";
202         pdc->unknown          = 0x10; /* what is this? */
203         pdc->unknown2         = 2; /* and this ... */
204         pdc->pdc_ip           = my_ip;
205         pdc->nt_version       = 13;
206         pdc->lmnt_token       = 0xFFFF;
207         pdc->lm20_token       = 0xFFFF;
208
209         packet->data.msg.dest_name.type = 0;
210
211         dgram_mailslot_netlogon_reply(reply_iface->dgmsock, 
212                                       packet, 
213                                       netlogon->req.pdc2.mailslot_name,
214                                       &reply);
215 }
216
217
218 /*
219   handle incoming netlogon mailslot requests
220 */
221 void nbtd_mailslot_netlogon_handler(struct dgram_mailslot_handler *dgmslot, 
222                                     struct nbt_dgram_packet *packet, 
223                                     struct socket_address *src)
224 {
225         NTSTATUS status = NT_STATUS_NO_MEMORY;
226         struct nbtd_interface *iface = 
227                 talloc_get_type(dgmslot->private, struct nbtd_interface);
228         struct nbt_netlogon_packet *netlogon = 
229                 talloc(dgmslot, struct nbt_netlogon_packet);
230         struct nbtd_iface_name *iname;
231         struct nbt_name *name = &packet->data.msg.dest_name;
232
233         if (netlogon == NULL) goto failed;
234
235         /*
236           see if the we are listening on the destination netbios name
237         */
238         iname = nbtd_find_iname(iface, name, 0);
239         if (iname == NULL) {
240                 status = NT_STATUS_BAD_NETWORK_NAME;
241                 goto failed;
242         }
243
244         DEBUG(2,("netlogon request to %s from %s:%d\n", 
245                  nbt_name_string(netlogon, name), src->addr, src->port));
246         status = dgram_mailslot_netlogon_parse(dgmslot, netlogon, packet, netlogon);
247         if (!NT_STATUS_IS_OK(status)) goto failed;
248
249         switch (netlogon->command) {
250         case NETLOGON_QUERY_FOR_PDC:
251                 nbtd_netlogon_getdc(dgmslot, iface, packet, src, netlogon);
252                 break;
253         case NETLOGON_QUERY_FOR_PDC2:
254                 nbtd_netlogon_getdc2(dgmslot, iface, packet, src, netlogon);
255                 break;
256         default:
257                 DEBUG(2,("unknown netlogon op %d from %s:%d\n", 
258                          netlogon->command, src->addr, src->port));
259                 NDR_PRINT_DEBUG(nbt_netlogon_packet, netlogon);
260                 break;
261         }
262
263         talloc_free(netlogon);
264         return;
265
266 failed:
267         DEBUG(2,("nbtd netlogon handler failed from %s:%d to %s - %s\n",
268                  src->addr, src->port, nbt_name_string(netlogon, name),
269                  nt_errstr(status)));
270         talloc_free(netlogon);
271 }