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"
31 reply to a GETDC request
33 static void nbtd_netlogon_getdc(struct dgram_mailslot_handler *dgmslot,
34 struct nbt_dgram_packet *packet,
35 const struct nbt_peer_socket *src,
36 struct nbt_netlogon_packet *netlogon)
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;
46 /* only answer getdc requests on the PDC or LOGON names */
47 if (name->type != NBT_NAME_PDC && name->type != NBT_NAME_LOGON) {
51 samctx = samdb_connect(packet, anonymous_session(packet));
53 DEBUG(2,("Unable to open sam in getdc reply\n"));
57 ret = gendb_search(samctx, samctx, NULL, &ref_res, ref_attrs,
58 "(&(&(nETBIOSName=%s)(objectclass=crossRef))(ncName=*))",
62 DEBUG(2,("Unable to find domain reference '%s' in sam\n", name->name));
66 /* setup a GETDC reply */
68 reply.command = NETLOGON_RESPONSE_FROM_PDC;
69 pdc = &reply.req.response;
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);;
75 pdc->lmnt_token = 0xFFFF;
76 pdc->lm20_token = 0xFFFF;
79 packet->data.msg.dest_name.type = 0;
81 dgram_mailslot_netlogon_reply(dgmslot->dgmsock,
83 netlogon->req.pdc.mailslot_name,
89 reply to a ADS style GETDC request
91 static void nbtd_netlogon_getdc2(struct dgram_mailslot_handler *dgmslot,
92 struct nbt_dgram_packet *packet,
93 const struct nbt_peer_socket *src,
94 struct nbt_netlogon_packet *netlogon)
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;
104 const char **services = lp_server_services();
106 /* only answer getdc requests on the PDC or LOGON names */
107 if (name->type != NBT_NAME_PDC && name->type != NBT_NAME_LOGON) {
111 samctx = samdb_connect(packet, anonymous_session(packet));
112 if (samctx == NULL) {
113 DEBUG(2,("Unable to open sam in getdc reply\n"));
117 ret = gendb_search(samctx, samctx, NULL, &ref_res, ref_attrs,
118 "(&(&(nETBIOSName=%s)(objectclass=crossRef))(ncName=*))",
122 DEBUG(2,("Unable to find domain reference '%s' in sam\n", name->name));
126 /* try and find the domain */
127 ret = gendb_search_dn(samctx, samctx,
128 samdb_result_dn(samctx, ref_res[0], "ncName", NULL),
129 &dom_res, dom_attrs);
131 DEBUG(2,("Unable to find domain from reference '%s' in sam\n",
132 ldb_dn_linearize(samctx, ref_res[0]->dn)));
136 /* setup a GETDC reply */
138 reply.command = NETLOGON_RESPONSE_FROM_PDC2;
141 /* newer testing shows that the reply command type is not
142 changed based on whether a username is given in the
143 reply. This was what was causing the w2k join to be so
145 if (netlogon->req.pdc2.user_name[0]) {
146 reply.command = NETLOGON_RESPONSE_FROM_PDC_USER;
150 pdc = &reply.req.response2;
152 /* TODO: accurately depict which services we are running */
154 NBT_SERVER_PDC | NBT_SERVER_GC |
155 NBT_SERVER_DS | NBT_SERVER_TIMESERV |
156 NBT_SERVER_CLOSEST | NBT_SERVER_WRITABLE |
157 NBT_SERVER_GOOD_TIMESERV;
159 /* hmm, probably a better way to do this */
160 if (str_list_check(services, "ldap")) {
161 pdc->server_type |= NBT_SERVER_LDAP;
164 if (str_list_check(services, "kdc")) {
165 pdc->server_type |= NBT_SERVER_KDC;
168 pdc->domain_uuid = samdb_result_guid(dom_res[0], "objectGUID");
169 pdc->forest = samdb_result_string(dom_res[0], "dnsDomain", lp_realm());
170 pdc->dns_domain = samdb_result_string(dom_res[0], "dnsDomain", lp_realm());
172 /* TODO: get our full DNS name from somewhere else */
173 pdc->pdc_dns_name = talloc_asprintf(packet, "%s.%s",
174 strlower_talloc(packet, lp_netbios_name()),
176 pdc->domain = samdb_result_string(dom_res[0], "nETBIOSName", name->name);;
177 pdc->pdc_name = lp_netbios_name();
178 pdc->user_name = netlogon->req.pdc2.user_name;
179 /* TODO: we need to make sure these are in our DNS zone */
180 pdc->site_name = "Default-First-Site-Name";
181 pdc->site_name2 = "Default-First-Site-Name";
182 pdc->unknown = 0x10; /* what is this? */
183 pdc->unknown2 = 2; /* and this ... */
184 pdc->pdc_ip = socket_get_my_addr(dgmslot->dgmsock->sock, packet);
185 pdc->nt_version = 13;
186 pdc->lmnt_token = 0xFFFF;
187 pdc->lm20_token = 0xFFFF;
189 packet->data.msg.dest_name.type = 0;
191 dgram_mailslot_netlogon_reply(dgmslot->dgmsock,
193 netlogon->req.pdc2.mailslot_name,
199 handle incoming netlogon mailslot requests
201 void nbtd_mailslot_netlogon_handler(struct dgram_mailslot_handler *dgmslot,
202 struct nbt_dgram_packet *packet,
203 const struct nbt_peer_socket *src)
205 NTSTATUS status = NT_STATUS_NO_MEMORY;
206 struct nbtd_interface *iface =
207 talloc_get_type(dgmslot->private, struct nbtd_interface);
208 struct nbt_netlogon_packet *netlogon =
209 talloc(dgmslot, struct nbt_netlogon_packet);
210 struct nbtd_iface_name *iname;
211 struct nbt_name *name = &packet->data.msg.dest_name;
213 if (netlogon == NULL) goto failed;
216 see if the we are listening on the destination netbios name
218 iname = nbtd_find_iname(iface, name, 0);
220 status = NT_STATUS_BAD_NETWORK_NAME;
224 DEBUG(2,("netlogon request to %s from %s:%d\n",
225 nbt_name_string(netlogon, name), src->addr, src->port));
226 status = dgram_mailslot_netlogon_parse(dgmslot, netlogon, packet, netlogon);
227 if (!NT_STATUS_IS_OK(status)) goto failed;
229 switch (netlogon->command) {
230 case NETLOGON_QUERY_FOR_PDC:
231 nbtd_netlogon_getdc(dgmslot, packet, src, netlogon);
233 case NETLOGON_QUERY_FOR_PDC2:
234 nbtd_netlogon_getdc2(dgmslot, packet, src, netlogon);
237 DEBUG(2,("unknown netlogon op %d from %s:%d\n",
238 netlogon->command, src->addr, src->port));
239 NDR_PRINT_DEBUG(nbt_netlogon_packet, netlogon);
243 talloc_free(netlogon);
247 DEBUG(2,("nbtd netlogon handler failed from %s:%d - %s\n",
248 src->addr, src->port, nt_errstr(status)));
249 talloc_free(netlogon);