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"
30 reply to a GETDC request
32 static void nbtd_netlogon_getdc(struct dgram_mailslot_handler *dgmslot,
33 struct nbt_dgram_packet *packet,
34 const char *src_address, int src_port,
35 struct nbt_netlogon_packet *netlogon)
37 struct nbt_name *name = &packet->data.msg.dest_name;
38 struct nbt_netlogon_packet reply;
39 struct nbt_netlogon_response_from_pdc *pdc;
41 /* only answer getdc requests on the PDC or LOGON names */
42 if (name->type != NBT_NAME_PDC && name->type != NBT_NAME_LOGON) {
46 /* setup a GETDC reply */
48 reply.command = NETLOGON_RESPONSE_FROM_PDC;
49 pdc = &reply.req.response;
51 pdc->pdc_name = lp_netbios_name();
52 pdc->unicode_pdc_name = pdc->pdc_name;
53 pdc->domain_name = lp_workgroup();
55 pdc->lmnt_token = 0xFFFF;
56 pdc->lm20_token = 0xFFFF;
59 packet->data.msg.dest_name.type = 0;
61 dgram_mailslot_netlogon_reply(dgmslot->dgmsock,
63 netlogon->req.pdc.mailslot_name,
69 reply to a ADS style GETDC request
71 static void nbtd_netlogon_getdc2(struct dgram_mailslot_handler *dgmslot,
72 struct nbt_dgram_packet *packet,
73 const char *src_address, int src_port,
74 struct nbt_netlogon_packet *netlogon)
76 struct nbt_name *name = &packet->data.msg.dest_name;
77 struct nbt_netlogon_packet reply;
78 struct nbt_netlogon_response_from_pdc2 *pdc;
79 struct ldb_context *samctx;
80 const char *attrs[] = {"realm", "dnsDomain", "objectGUID", NULL};
81 struct ldb_message **res;
83 const char **services = lp_server_services();
85 /* only answer getdc requests on the PDC or LOGON names */
86 if (name->type != NBT_NAME_PDC && name->type != NBT_NAME_LOGON) {
90 samctx = samdb_connect(packet);
92 DEBUG(2,("Unable to open sam in getdc reply\n"));
96 /* try and find the domain */
97 ret = gendb_search(samctx, samctx, NULL, &res, attrs,
98 "(&(name=%s)(objectClass=domainDNS))", name->name);
100 DEBUG(2,("Unable to find domain '%s' in sam\n", name->name));
104 /* setup a GETDC reply */
106 if (netlogon->req.pdc2.user_name[0]) {
107 reply.command = NETLOGON_RESPONSE_FROM_PDC_USER;
109 reply.command = NETLOGON_RESPONSE_FROM_PDC2;
111 pdc = &reply.req.response2;
113 /* TODO: accurately depict which services we are running */
115 NBT_SERVER_PDC | NBT_SERVER_GC |
116 NBT_SERVER_DS | NBT_SERVER_TIMESERV |
117 NBT_SERVER_CLOSEST | NBT_SERVER_WRITABLE |
118 NBT_SERVER_GOOD_TIMESERV;
120 /* hmm, probably a better way to do this */
121 if (lp_parm_bool(-1, "gensec", "krb5", True)) {
122 pdc->server_type |= NBT_SERVER_KDC;
124 if (str_list_check(services, "ldap")) {
125 pdc->server_type |= NBT_SERVER_LDAP;
128 pdc->domain_uuid = samdb_result_guid(res[0], "objectGUID");
129 pdc->forest = samdb_result_string(res[0], "realm", lp_realm());
130 pdc->dns_domain = samdb_result_string(res[0], "dnsDomain", lp_realm());
132 /* TODO: get our full DNS name from somewhere else */
133 pdc->pdc_dns_name = talloc_asprintf(packet, "%s.%s",
134 lp_netbios_name(), pdc->dns_domain);
135 pdc->domain = name->name;
136 pdc->pdc_name = lp_netbios_name();
137 pdc->user_name = netlogon->req.pdc2.user_name;
138 /* TODO: we need to make sure these are in our DNS zone */
139 pdc->site_name = "Default-First-Site-Name";
140 pdc->site_name2 = "Default-First-Site-Name";
141 pdc->unknown = 0x10; /* what is this? */
142 pdc->unknown2 = 2; /* and this ... */
143 pdc->pdc_ip = socket_get_my_addr(dgmslot->dgmsock->sock, packet);
144 pdc->nt_version = 13;
145 pdc->lmnt_token = 0xFFFF;
146 pdc->lm20_token = 0xFFFF;
148 packet->data.msg.dest_name.type = 0;
150 dgram_mailslot_netlogon_reply(dgmslot->dgmsock,
152 netlogon->req.pdc2.mailslot_name,
158 handle incoming netlogon mailslot requests
160 void nbtd_mailslot_netlogon_handler(struct dgram_mailslot_handler *dgmslot,
161 struct nbt_dgram_packet *packet,
162 const char *src_address, int src_port)
164 NTSTATUS status = NT_STATUS_NO_MEMORY;
165 struct nbtd_interface *iface =
166 talloc_get_type(dgmslot->private, struct nbtd_interface);
167 struct nbt_netlogon_packet *netlogon =
168 talloc(dgmslot, struct nbt_netlogon_packet);
169 struct nbtd_iface_name *iname;
170 struct nbt_name *name = &packet->data.msg.dest_name;
172 if (netlogon == NULL) goto failed;
175 see if the we are listening on the destination netbios name
177 iname = nbtd_find_iname(iface, name, 0);
179 status = NT_STATUS_BAD_NETWORK_NAME;
183 DEBUG(2,("netlogon request to %s from %s:%d\n",
184 nbt_name_string(netlogon, name), src_address, src_port));
185 status = dgram_mailslot_netlogon_parse(dgmslot, netlogon, packet, netlogon);
186 if (!NT_STATUS_IS_OK(status)) goto failed;
188 NDR_PRINT_DEBUG(nbt_netlogon_packet, netlogon);
190 switch (netlogon->command) {
191 case NETLOGON_QUERY_FOR_PDC:
192 nbtd_netlogon_getdc(dgmslot, packet, src_address, src_port, netlogon);
194 case NETLOGON_QUERY_FOR_PDC2:
195 nbtd_netlogon_getdc2(dgmslot, packet, src_address, src_port, netlogon);
198 DEBUG(2,("unknown netlogon op %d from %s:%d\n",
199 netlogon->command, src_address, src_port));
203 talloc_free(netlogon);
207 DEBUG(2,("nbtd netlogon handler failed from %s:%d - %s\n",
208 src_address, src_port, nt_errstr(status)));
209 talloc_free(netlogon);