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