2 Unix SMB/CIFS implementation.
4 NBT datagram netlogon server
6 Copyright (C) Andrew Tridgell 2005
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.
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.
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.
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"
33 reply to a GETDC request
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)
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;
48 /* only answer getdc requests on the PDC or LOGON names */
49 if (name->type != NBT_NAME_PDC && name->type != NBT_NAME_LOGON) {
53 samctx = samdb_connect(packet, anonymous_session(packet));
55 DEBUG(2,("Unable to open sam in getdc reply\n"));
59 ret = gendb_search(samctx, samctx, NULL, &ref_res, ref_attrs,
60 "(&(&(nETBIOSName=%s)(objectclass=crossRef))(ncName=*))",
64 DEBUG(2,("Unable to find domain reference '%s' in sam\n", name->name));
68 /* setup a GETDC reply */
70 reply.command = NETLOGON_RESPONSE_FROM_PDC;
71 pdc = &reply.req.response;
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);;
77 pdc->lmnt_token = 0xFFFF;
78 pdc->lm20_token = 0xFFFF;
81 packet->data.msg.dest_name.type = 0;
83 dgram_mailslot_netlogon_reply(dgmslot->dgmsock,
85 netlogon->req.pdc.mailslot_name,
91 reply to a ADS style GETDC request
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)
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;
106 const char **services = lp_server_services();
108 /* only answer getdc requests on the PDC or LOGON names */
109 if (name->type != NBT_NAME_PDC && name->type != NBT_NAME_LOGON) {
113 samctx = samdb_connect(packet, anonymous_session(packet));
114 if (samctx == NULL) {
115 DEBUG(2,("Unable to open sam in getdc reply\n"));
119 ret = gendb_search(samctx, samctx, NULL, &ref_res, ref_attrs,
120 "(&(&(nETBIOSName=%s)(objectclass=crossRef))(ncName=*))",
124 DEBUG(2,("Unable to find domain reference '%s' in sam\n", name->name));
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);
133 DEBUG(2,("Unable to find domain from reference '%s' in sam\n",
134 ldb_dn_linearize(samctx, ref_res[0]->dn)));
138 /* setup a GETDC reply */
140 reply.command = NETLOGON_RESPONSE_FROM_PDC2;
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
147 if (netlogon->req.pdc2.user_name[0]) {
148 reply.command = NETLOGON_RESPONSE_FROM_PDC_USER;
152 pdc = &reply.req.response2;
154 /* TODO: accurately depict which services we are running */
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;
161 /* hmm, probably a better way to do this */
162 if (str_list_check(services, "ldap")) {
163 pdc->server_type |= NBT_SERVER_LDAP;
166 if (str_list_check(services, "kdc")) {
167 pdc->server_type |= NBT_SERVER_KDC;
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());
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()),
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;
191 packet->data.msg.dest_name.type = 0;
193 dgram_mailslot_netlogon_reply(dgmslot->dgmsock,
195 netlogon->req.pdc2.mailslot_name,
201 handle incoming netlogon mailslot requests
203 void nbtd_mailslot_netlogon_handler(struct dgram_mailslot_handler *dgmslot,
204 struct nbt_dgram_packet *packet,
205 const struct nbt_peer_socket *src)
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;
215 if (netlogon == NULL) goto failed;
218 see if the we are listening on the destination netbios name
220 iname = nbtd_find_iname(iface, name, 0);
222 status = NT_STATUS_BAD_NETWORK_NAME;
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;
231 switch (netlogon->command) {
232 case NETLOGON_QUERY_FOR_PDC:
233 nbtd_netlogon_getdc(dgmslot, packet, src, netlogon);
235 case NETLOGON_QUERY_FOR_PDC2:
236 nbtd_netlogon_getdc2(dgmslot, packet, src, netlogon);
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);
245 talloc_free(netlogon);
249 DEBUG(2,("nbtd netlogon handler failed from %s:%d - %s\n",
250 src->addr, src->port, nt_errstr(status)));
251 talloc_free(netlogon);