r14709: allways use the unicast socket of the interface, when reply to DGRAM
[sfrench/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 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         if (!my_ip) {
113                 DEBUG(0, ("Could not obtain own IP address for datagram socket\n"));
114                 return;
115         }
116
117         /* only answer getdc requests on the PDC or LOGON names */
118         if (name->type != NBT_NAME_PDC && name->type != NBT_NAME_LOGON) {
119                 return;
120         }
121
122         samctx = samdb_connect(packet, anonymous_session(packet));
123         if (samctx == NULL) {
124                 DEBUG(2,("Unable to open sam in getdc reply\n"));
125                 return;
126         }
127
128         ret = gendb_search(samctx, samctx, NULL, &ref_res, ref_attrs,
129                                   "(&(&(nETBIOSName=%s)(objectclass=crossRef))(ncName=*))", 
130                                   name->name);
131         
132         if (ret != 1) {
133                 DEBUG(2,("Unable to find domain reference '%s' in sam\n", name->name));
134                 return;
135         }
136
137         /* try and find the domain */
138         ret = gendb_search_dn(samctx, samctx, 
139                               samdb_result_dn(samctx, ref_res[0], "ncName", NULL), 
140                               &dom_res, dom_attrs);
141         if (ret != 1) {
142                 DEBUG(2,("Unable to find domain from reference '%s' in sam\n",
143                          ldb_dn_linearize(samctx, ref_res[0]->dn)));
144                 return;
145         }
146
147         /* setup a GETDC reply */
148         ZERO_STRUCT(reply);
149         reply.command = NETLOGON_RESPONSE_FROM_PDC2;
150
151 #if 0
152         /* newer testing shows that the reply command type is not
153            changed based on whether a username is given in the
154            reply. This was what was causing the w2k join to be so
155            slow */
156         if (netlogon->req.pdc2.user_name[0]) {
157                 reply.command = NETLOGON_RESPONSE_FROM_PDC_USER;
158         }
159 #endif
160
161         pdc = &reply.req.response2;
162
163         /* TODO: accurately depict which services we are running */
164         pdc->server_type      = 
165                 NBT_SERVER_PDC | NBT_SERVER_GC | 
166                 NBT_SERVER_DS | NBT_SERVER_TIMESERV |
167                 NBT_SERVER_CLOSEST | NBT_SERVER_WRITABLE | 
168                 NBT_SERVER_GOOD_TIMESERV;
169
170         /* hmm, probably a better way to do this */
171         if (str_list_check(services, "ldap")) {
172                 pdc->server_type |= NBT_SERVER_LDAP;
173         }
174
175         if (str_list_check(services, "kdc")) {
176                 pdc->server_type |= NBT_SERVER_KDC;
177         }
178
179         pdc->domain_uuid      = samdb_result_guid(dom_res[0], "objectGUID");
180         pdc->forest           = samdb_result_string(dom_res[0], "dnsDomain", lp_realm());
181         pdc->dns_domain       = samdb_result_string(dom_res[0], "dnsDomain", lp_realm());
182
183         /* TODO: get our full DNS name from somewhere else */
184         pdc->pdc_dns_name     = talloc_asprintf(packet, "%s.%s", 
185                                                 strlower_talloc(packet, lp_netbios_name()), 
186                                                 pdc->dns_domain);
187         pdc->domain           = samdb_result_string(dom_res[0], "nETBIOSName", name->name);;
188         pdc->pdc_name         = lp_netbios_name();
189         pdc->user_name        = netlogon->req.pdc2.user_name;
190         /* TODO: we need to make sure these are in our DNS zone */
191         pdc->site_name        = "Default-First-Site-Name";
192         pdc->site_name2       = "Default-First-Site-Name";
193         pdc->unknown          = 0x10; /* what is this? */
194         pdc->unknown2         = 2; /* and this ... */
195         pdc->pdc_ip           = my_ip;
196         pdc->nt_version       = 13;
197         pdc->lmnt_token       = 0xFFFF;
198         pdc->lm20_token       = 0xFFFF;
199
200         packet->data.msg.dest_name.type = 0;
201
202         dgram_mailslot_netlogon_reply(reply_iface->dgmsock, 
203                                       packet, 
204                                       netlogon->req.pdc2.mailslot_name,
205                                       &reply);
206 }
207
208
209 /*
210   handle incoming netlogon mailslot requests
211 */
212 void nbtd_mailslot_netlogon_handler(struct dgram_mailslot_handler *dgmslot, 
213                                     struct nbt_dgram_packet *packet, 
214                                     struct socket_address *src)
215 {
216         NTSTATUS status = NT_STATUS_NO_MEMORY;
217         struct nbtd_interface *iface = 
218                 talloc_get_type(dgmslot->private, struct nbtd_interface);
219         struct nbt_netlogon_packet *netlogon = 
220                 talloc(dgmslot, struct nbt_netlogon_packet);
221         struct nbtd_iface_name *iname;
222         struct nbt_name *name = &packet->data.msg.dest_name;
223
224         if (netlogon == NULL) goto failed;
225
226         /*
227           see if the we are listening on the destination netbios name
228         */
229         iname = nbtd_find_iname(iface, name, 0);
230         if (iname == NULL) {
231                 status = NT_STATUS_BAD_NETWORK_NAME;
232                 goto failed;
233         }
234
235         DEBUG(2,("netlogon request to %s from %s:%d\n", 
236                  nbt_name_string(netlogon, name), src->addr, src->port));
237         status = dgram_mailslot_netlogon_parse(dgmslot, netlogon, packet, netlogon);
238         if (!NT_STATUS_IS_OK(status)) goto failed;
239
240         switch (netlogon->command) {
241         case NETLOGON_QUERY_FOR_PDC:
242                 nbtd_netlogon_getdc(dgmslot, iface, packet, src, netlogon);
243                 break;
244         case NETLOGON_QUERY_FOR_PDC2:
245                 nbtd_netlogon_getdc2(dgmslot, iface, packet, src, netlogon);
246                 break;
247         default:
248                 DEBUG(2,("unknown netlogon op %d from %s:%d\n", 
249                          netlogon->command, src->addr, src->port));
250                 NDR_PRINT_DEBUG(nbt_netlogon_packet, netlogon);
251                 break;
252         }
253
254         talloc_free(netlogon);
255         return;
256
257 failed:
258         DEBUG(2,("nbtd netlogon handler failed from %s:%d to %s - %s\n",
259                  src->addr, src->port, nbt_name_string(netlogon, name),
260                  nt_errstr(status)));
261         talloc_free(netlogon);
262 }