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 "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"
30 #include "librpc/gen_ndr/ndr_nbt.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;
50 /* only answer getdc requests on the PDC or LOGON names */
51 if (name->type != NBT_NAME_PDC && name->type != NBT_NAME_LOGON) {
55 samctx = samdb_connect(packet, anonymous_session(packet));
57 DEBUG(2,("Unable to open sam in getdc reply\n"));
61 ret = gendb_search(samctx, samctx, NULL, &ref_res, ref_attrs,
62 "(&(&(nETBIOSName=%s)(objectclass=crossRef))(ncName=*))",
66 DEBUG(2,("Unable to find domain reference '%s' in sam\n", name->name));
70 /* setup a GETDC reply */
72 reply.command = NETLOGON_RESPONSE_FROM_PDC;
73 pdc = &reply.req.response;
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);;
79 pdc->lmnt_token = 0xFFFF;
80 pdc->lm20_token = 0xFFFF;
83 packet->data.msg.dest_name.type = 0;
85 dgram_mailslot_netlogon_reply(reply_iface->dgmsock,
87 netlogon->req.pdc.mailslot_name,
93 reply to a ADS style GETDC request
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)
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;
110 const char **services = lp_server_services();
111 const char *my_ip = reply_iface->ip_address;
113 DEBUG(0, ("Could not obtain own IP address for datagram socket\n"));
117 /* only answer getdc requests on the PDC or LOGON names */
118 if (name->type != NBT_NAME_PDC && name->type != NBT_NAME_LOGON) {
122 samctx = samdb_connect(packet, anonymous_session(packet));
123 if (samctx == NULL) {
124 DEBUG(2,("Unable to open sam in getdc reply\n"));
128 ret = gendb_search(samctx, samctx, NULL, &ref_res, ref_attrs,
129 "(&(&(nETBIOSName=%s)(objectclass=crossRef))(ncName=*))",
133 DEBUG(2,("Unable to find domain reference '%s' in sam\n", name->name));
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);
142 DEBUG(2,("Unable to find domain from reference '%s' in sam\n",
143 ldb_dn_linearize(samctx, ref_res[0]->dn)));
147 /* setup a GETDC reply */
149 reply.command = NETLOGON_RESPONSE_FROM_PDC2;
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
156 if (netlogon->req.pdc2.user_name[0]) {
157 reply.command = NETLOGON_RESPONSE_FROM_PDC_USER;
161 pdc = &reply.req.response2;
163 /* TODO: accurately depict which services we are running */
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;
170 /* hmm, probably a better way to do this */
171 if (str_list_check(services, "ldap")) {
172 pdc->server_type |= NBT_SERVER_LDAP;
175 if (str_list_check(services, "kdc")) {
176 pdc->server_type |= NBT_SERVER_KDC;
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());
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()),
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 ... */
196 pdc->nt_version = 13;
197 pdc->lmnt_token = 0xFFFF;
198 pdc->lm20_token = 0xFFFF;
200 packet->data.msg.dest_name.type = 0;
202 dgram_mailslot_netlogon_reply(reply_iface->dgmsock,
204 netlogon->req.pdc2.mailslot_name,
210 handle incoming netlogon mailslot requests
212 void nbtd_mailslot_netlogon_handler(struct dgram_mailslot_handler *dgmslot,
213 struct nbt_dgram_packet *packet,
214 struct socket_address *src)
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;
224 if (netlogon == NULL) goto failed;
227 see if the we are listening on the destination netbios name
229 iname = nbtd_find_iname(iface, name, 0);
231 status = NT_STATUS_BAD_NETWORK_NAME;
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;
240 switch (netlogon->command) {
241 case NETLOGON_QUERY_FOR_PDC:
242 nbtd_netlogon_getdc(dgmslot, iface, packet, src, netlogon);
244 case NETLOGON_QUERY_FOR_PDC2:
245 nbtd_netlogon_getdc2(dgmslot, iface, packet, src, netlogon);
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);
254 talloc_free(netlogon);
258 DEBUG(2,("nbtd netlogon handler failed from %s:%d to %s - %s\n",
259 src->addr, src->port, nbt_name_string(netlogon, name),
261 talloc_free(netlogon);