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 3 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, see <http://www.gnu.org/licenses/>.
23 #include "nbt_server/nbt_server.h"
24 #include "lib/socket/socket.h"
25 #include "lib/ldb/include/ldb.h"
26 #include "dsdb/samdb/samdb.h"
27 #include "auth/auth.h"
28 #include "util/util_ldb.h"
29 #include "librpc/gen_ndr/ndr_nbt.h"
30 #include "param/param.h"
33 reply to a GETDC request
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)
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 struct ldb_dn *partitions_basedn;
51 /* only answer getdc requests on the PDC or LOGON names */
52 if (name->type != NBT_NAME_PDC && name->type != NBT_NAME_LOGON) {
56 samctx = samdb_connect(packet, global_loadparm, anonymous_session(packet, global_loadparm));
58 DEBUG(2,("Unable to open sam in getdc reply\n"));
62 partitions_basedn = samdb_partitions_dn(samctx, packet);
64 ret = gendb_search(samctx, packet, partitions_basedn, &ref_res, ref_attrs,
65 "(&(&(nETBIOSName=%s)(objectclass=crossRef))(ncName=*))",
69 DEBUG(2,("Unable to find domain reference '%s' in sam\n", name->name));
73 /* setup a GETDC reply */
75 reply.command = NETLOGON_RESPONSE_FROM_PDC;
76 pdc = &reply.req.response;
78 pdc->pdc_name = lp_netbios_name(global_loadparm);
79 pdc->unicode_pdc_name = pdc->pdc_name;
80 pdc->domain_name = samdb_result_string(ref_res[0], "nETBIOSName", name->name);;
82 pdc->lmnt_token = 0xFFFF;
83 pdc->lm20_token = 0xFFFF;
86 packet->data.msg.dest_name.type = 0;
88 dgram_mailslot_netlogon_reply(reply_iface->dgmsock,
90 lp_netbios_name(global_loadparm),
91 netlogon->req.pdc.mailslot_name,
97 reply to a ADS style GETDC request
99 static void nbtd_netlogon_getdc2(struct dgram_mailslot_handler *dgmslot,
100 struct nbtd_interface *iface,
101 struct nbt_dgram_packet *packet,
102 const struct socket_address *src,
103 struct nbt_netlogon_packet *netlogon)
105 struct nbt_name *name = &packet->data.msg.dest_name;
106 struct nbtd_interface *reply_iface = nbtd_find_reply_iface(iface, src->addr, false);
107 struct nbt_netlogon_packet reply;
108 struct nbt_netlogon_response_from_pdc2 *pdc;
109 struct ldb_context *samctx;
110 const char *ref_attrs[] = {"nETBIOSName", "dnsRoot", "ncName", NULL};
111 const char *dom_attrs[] = {"objectGUID", NULL};
112 struct ldb_message **ref_res, **dom_res;
114 const char **services = lp_server_services(global_loadparm);
115 const char *my_ip = reply_iface->ip_address;
116 struct ldb_dn *partitions_basedn;
118 DEBUG(0, ("Could not obtain own IP address for datagram socket\n"));
122 /* only answer getdc requests on the PDC or LOGON names */
123 if (name->type != NBT_NAME_PDC && name->type != NBT_NAME_LOGON) {
127 samctx = samdb_connect(packet, global_loadparm, anonymous_session(packet, global_loadparm));
128 if (samctx == NULL) {
129 DEBUG(2,("Unable to open sam in getdc reply\n"));
133 partitions_basedn = samdb_partitions_dn(samctx, packet);
135 ret = gendb_search(samctx, packet, partitions_basedn, &ref_res, ref_attrs,
136 "(&(&(nETBIOSName=%s)(objectclass=crossRef))(ncName=*))",
140 DEBUG(2,("Unable to find domain reference '%s' in sam\n", name->name));
144 /* try and find the domain */
145 ret = gendb_search_dn(samctx, packet,
146 samdb_result_dn(samctx, samctx, ref_res[0], "ncName", NULL),
147 &dom_res, dom_attrs);
149 DEBUG(2,("Unable to find domain from reference '%s' in sam\n",
150 ldb_dn_get_linearized(ref_res[0]->dn)));
154 /* setup a GETDC reply */
156 reply.command = NETLOGON_RESPONSE_FROM_PDC2;
159 /* newer testing shows that the reply command type is not
160 changed based on whether a username is given in the
161 reply. This was what was causing the w2k join to be so
163 if (netlogon->req.pdc2.user_name[0]) {
164 reply.command = NETLOGON_RESPONSE_FROM_PDC_USER;
168 pdc = &reply.req.response2;
170 /* TODO: accurately depict which services we are running */
172 NBT_SERVER_PDC | NBT_SERVER_GC |
173 NBT_SERVER_DS | NBT_SERVER_TIMESERV |
174 NBT_SERVER_CLOSEST | NBT_SERVER_WRITABLE |
175 NBT_SERVER_GOOD_TIMESERV;
177 /* hmm, probably a better way to do this */
178 if (str_list_check(services, "ldap")) {
179 pdc->server_type |= NBT_SERVER_LDAP;
182 if (str_list_check(services, "kdc")) {
183 pdc->server_type |= NBT_SERVER_KDC;
186 pdc->domain_uuid = samdb_result_guid(dom_res[0], "objectGUID");
187 pdc->forest = samdb_result_string(ref_res[0], "dnsRoot",
188 lp_realm(global_loadparm));
189 pdc->dns_domain = samdb_result_string(ref_res[0], "dnsRoot",
190 lp_realm(global_loadparm));
192 /* TODO: get our full DNS name from somewhere else */
193 pdc->pdc_dns_name = talloc_asprintf(packet, "%s.%s",
194 strlower_talloc(packet,
195 lp_netbios_name(global_loadparm)),
197 pdc->domain = samdb_result_string(ref_res[0], "nETBIOSName", name->name);;
198 pdc->pdc_name = lp_netbios_name(global_loadparm);
199 pdc->user_name = netlogon->req.pdc2.user_name;
200 /* TODO: we need to make sure these are in our DNS zone */
201 pdc->server_site = "Default-First-Site-Name";
202 pdc->client_site = "Default-First-Site-Name";
203 pdc->unknown = 0x10; /* what is this? */
204 pdc->unknown2 = 2; /* and this ... */
206 pdc->nt_version = 13;
207 pdc->lmnt_token = 0xFFFF;
208 pdc->lm20_token = 0xFFFF;
210 packet->data.msg.dest_name.type = 0;
212 dgram_mailslot_netlogon_reply(reply_iface->dgmsock,
214 lp_netbios_name(global_loadparm),
215 netlogon->req.pdc2.mailslot_name,
221 handle incoming netlogon mailslot requests
223 void nbtd_mailslot_netlogon_handler(struct dgram_mailslot_handler *dgmslot,
224 struct nbt_dgram_packet *packet,
225 struct socket_address *src)
227 NTSTATUS status = NT_STATUS_NO_MEMORY;
228 struct nbtd_interface *iface =
229 talloc_get_type(dgmslot->private, struct nbtd_interface);
230 struct nbt_netlogon_packet *netlogon =
231 talloc(dgmslot, struct nbt_netlogon_packet);
232 struct nbtd_iface_name *iname;
233 struct nbt_name *name = &packet->data.msg.dest_name;
235 if (netlogon == NULL) goto failed;
238 see if the we are listening on the destination netbios name
240 iname = nbtd_find_iname(iface, name, 0);
242 status = NT_STATUS_BAD_NETWORK_NAME;
246 DEBUG(2,("netlogon request to %s from %s:%d\n",
247 nbt_name_string(netlogon, name), src->addr, src->port));
248 status = dgram_mailslot_netlogon_parse(dgmslot, netlogon, packet, netlogon);
249 if (!NT_STATUS_IS_OK(status)) goto failed;
251 switch (netlogon->command) {
252 case NETLOGON_QUERY_FOR_PDC:
253 nbtd_netlogon_getdc(dgmslot, iface, packet, src, netlogon);
255 case NETLOGON_QUERY_FOR_PDC2:
256 nbtd_netlogon_getdc2(dgmslot, iface, packet, src, netlogon);
259 DEBUG(2,("unknown netlogon op %d from %s:%d\n",
260 netlogon->command, src->addr, src->port));
261 NDR_PRINT_DEBUG(nbt_netlogon_packet, netlogon);
265 talloc_free(netlogon);
269 DEBUG(2,("nbtd netlogon handler failed from %s:%d to %s - %s\n",
270 src->addr, src->port, nbt_name_string(netlogon, name),
272 talloc_free(netlogon);