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