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