2 Unix SMB/CIFS implementation.
4 NBT datagram netlogon server
6 Copyright (C) Andrew Tridgell 2005
7 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2008
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
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"
29 #include "../lib/util/util_ldb.h"
30 #include "param/param.h"
31 #include "smbd/service_task.h"
32 #include "cldap_server/cldap_server.h"
33 #include "libcli/security/security.h"
34 #include "nbt_server/dgram/proto.h"
37 reply to a GETDC request
39 static void nbtd_netlogon_getdc(struct dgram_mailslot_handler *dgmslot,
40 struct nbtd_interface *iface,
41 struct nbt_dgram_packet *packet,
42 const struct socket_address *src,
43 struct nbt_netlogon_packet *netlogon)
45 struct nbt_name *name = &packet->data.msg.dest_name;
46 struct nbtd_interface *reply_iface = nbtd_find_reply_iface(iface, src->addr, false);
47 struct nbt_netlogon_response_from_pdc *pdc;
48 const char *ref_attrs[] = {"nETBIOSName", NULL};
49 struct ldb_message **ref_res;
50 struct ldb_context *samctx;
51 struct ldb_dn *partitions_basedn;
52 struct nbt_netlogon_response netlogon_response;
55 /* only answer getdc requests on the PDC or LOGON names */
56 if (name->type != NBT_NAME_PDC && name->type != NBT_NAME_LOGON) {
60 samctx = iface->nbtsrv->sam_ctx;
62 if (!samdb_is_pdc(samctx)) {
63 DEBUG(2, ("Not a PDC, so not processing LOGON_PRIMARY_QUERY\n"));
67 partitions_basedn = samdb_partitions_dn(samctx, packet);
69 ret = gendb_search(samctx, packet, partitions_basedn, &ref_res, ref_attrs,
70 "(&(&(nETBIOSName=%s)(objectclass=crossRef))(ncName=*))",
74 DEBUG(2,("Unable to find domain reference '%s' in sam\n", name->name));
78 /* setup a GETDC reply */
79 ZERO_STRUCT(netlogon_response);
80 netlogon_response.response_type = NETLOGON_GET_PDC;
81 pdc = &netlogon_response.data.get_pdc;
83 pdc->command = NETLOGON_RESPONSE_FROM_PDC;
84 pdc->pdc_name = lp_netbios_name(iface->nbtsrv->task->lp_ctx);
85 pdc->unicode_pdc_name = pdc->pdc_name;
86 pdc->domain_name = samdb_result_string(ref_res[0], "nETBIOSName", name->name);;
88 pdc->lmnt_token = 0xFFFF;
89 pdc->lm20_token = 0xFFFF;
91 dgram_mailslot_netlogon_reply(reply_iface->dgmsock,
93 lp_netbios_name(iface->nbtsrv->task->lp_ctx),
94 netlogon->req.pdc.mailslot_name,
100 reply to a ADS style GETDC request
102 static void nbtd_netlogon_samlogon(struct dgram_mailslot_handler *dgmslot,
103 struct nbtd_interface *iface,
104 struct nbt_dgram_packet *packet,
105 const struct socket_address *src,
106 struct nbt_netlogon_packet *netlogon)
108 struct nbt_name *name = &packet->data.msg.dest_name;
109 struct nbtd_interface *reply_iface = nbtd_find_reply_iface(iface, src->addr, false);
110 struct ldb_context *samctx;
111 const char *my_ip = reply_iface->ip_address;
113 struct nbt_netlogon_response netlogon_response;
117 DEBUG(0, ("Could not obtain own IP address for datagram socket\n"));
121 /* only answer getdc requests on the PDC or LOGON names */
122 if (name->type != NBT_NAME_PDC && name->type != NBT_NAME_LOGON) {
126 samctx = iface->nbtsrv->sam_ctx;
128 if (netlogon->req.logon.sid_size) {
129 sid = &netlogon->req.logon.sid;
134 status = fill_netlogon_samlogon_response(samctx, packet, NULL, name->name, sid, NULL,
135 netlogon->req.logon.user_name, netlogon->req.logon.acct_control, src->addr,
136 netlogon->req.logon.nt_version, iface->nbtsrv->task->lp_ctx, &netlogon_response.data.samlogon);
137 if (!NT_STATUS_IS_OK(status)) {
138 DEBUG(2,("NBT netlogon query failed domain=%s sid=%s version=%d - %s\n",
139 name->name, dom_sid_string(packet, sid), netlogon->req.logon.nt_version, nt_errstr(status)));
143 netlogon_response.response_type = NETLOGON_SAMLOGON;
145 packet->data.msg.dest_name.type = 0;
147 dgram_mailslot_netlogon_reply(reply_iface->dgmsock,
149 lp_netbios_name(iface->nbtsrv->task->lp_ctx),
150 netlogon->req.logon.mailslot_name,
156 handle incoming netlogon mailslot requests
158 void nbtd_mailslot_netlogon_handler(struct dgram_mailslot_handler *dgmslot,
159 struct nbt_dgram_packet *packet,
160 struct socket_address *src)
162 NTSTATUS status = NT_STATUS_NO_MEMORY;
163 struct nbtd_interface *iface =
164 talloc_get_type(dgmslot->private, struct nbtd_interface);
165 struct nbt_netlogon_packet *netlogon =
166 talloc(dgmslot, struct nbt_netlogon_packet);
167 struct nbtd_iface_name *iname;
168 struct nbt_name *name = &packet->data.msg.dest_name;
170 if (netlogon == NULL) goto failed;
173 see if the we are listening on the destination netbios name
175 iname = nbtd_find_iname(iface, name, 0);
177 status = NT_STATUS_BAD_NETWORK_NAME;
181 DEBUG(2,("netlogon request to %s from %s:%d\n",
182 nbt_name_string(netlogon, name), src->addr, src->port));
183 status = dgram_mailslot_netlogon_parse_request(dgmslot, netlogon, packet, netlogon);
184 if (!NT_STATUS_IS_OK(status)) goto failed;
186 switch (netlogon->command) {
187 case LOGON_PRIMARY_QUERY:
188 nbtd_netlogon_getdc(dgmslot, iface, packet,
191 case LOGON_SAM_LOGON_REQUEST:
192 nbtd_netlogon_samlogon(dgmslot, iface, packet,
196 DEBUG(2,("unknown netlogon op %d from %s:%d\n",
197 netlogon->command, src->addr, src->port));
198 NDR_PRINT_DEBUG(nbt_netlogon_packet, netlogon);
202 talloc_free(netlogon);
206 DEBUG(2,("nbtd netlogon handler failed from %s:%d to %s - %s\n",
207 src->addr, src->port, nbt_name_string(netlogon, name),
209 talloc_free(netlogon);