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