s4:dsdb: Move cldap netlogon functions into samdb/ldb_modules
[Samba.git] / source4 / nbt_server / dgram / netlogon.c
blob0e5294cb94c42c13ef38d70885303998c75a7a39
1 /*
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/>.
23 #include "includes.h"
24 #include "nbt_server/nbt_server.h"
25 #include "lib/socket/socket.h"
26 #include <ldb.h>
27 #include "dsdb/samdb/samdb.h"
28 #include "auth/auth.h"
29 #include "param/param.h"
30 #include "smbd/service_task.h"
31 #include "dsdb/samdb/ldb_modules/util.h"
32 #include "libcli/security/security.h"
33 #include "nbt_server/dgram/proto.h"
36 reply to a GETDC request
38 static void nbtd_netlogon_getdc(struct dgram_mailslot_handler *dgmslot,
39 struct nbtd_interface *iface,
40 struct nbt_dgram_packet *packet,
41 const struct socket_address *src,
42 struct nbt_netlogon_packet *netlogon)
44 struct nbt_name *name = &packet->data.msg.dest_name;
45 struct nbtd_interface *reply_iface = nbtd_find_reply_iface(iface, src->addr, false);
46 struct nbt_netlogon_response_from_pdc *pdc;
47 struct ldb_context *samctx;
48 struct nbt_netlogon_response netlogon_response;
50 /* only answer getdc requests on the PDC or LOGON names */
51 if (name->type != NBT_NAME_PDC && name->type != NBT_NAME_LOGON) {
52 return;
55 samctx = iface->nbtsrv->sam_ctx;
57 if (lpcfg_server_role(iface->nbtsrv->task->lp_ctx) != ROLE_ACTIVE_DIRECTORY_DC
58 || !samdb_is_pdc(samctx)) {
59 DEBUG(2, ("Not a PDC, so not processing LOGON_PRIMARY_QUERY\n"));
60 return;
63 if (strcasecmp_m(name->name, lpcfg_workgroup(iface->nbtsrv->task->lp_ctx)) != 0) {
64 DEBUG(5,("GetDC requested for a domian %s that we don't host\n", name->name));
65 return;
68 /* setup a GETDC reply */
69 ZERO_STRUCT(netlogon_response);
70 netlogon_response.response_type = NETLOGON_GET_PDC;
71 pdc = &netlogon_response.data.get_pdc;
73 pdc->command = NETLOGON_RESPONSE_FROM_PDC;
74 pdc->pdc_name = lpcfg_netbios_name(iface->nbtsrv->task->lp_ctx);
75 pdc->unicode_pdc_name = pdc->pdc_name;
76 pdc->domain_name = lpcfg_workgroup(iface->nbtsrv->task->lp_ctx);
77 pdc->nt_version = 1;
78 pdc->lmnt_token = 0xFFFF;
79 pdc->lm20_token = 0xFFFF;
81 dgram_mailslot_netlogon_reply(reply_iface->dgmsock,
82 packet,
83 lpcfg_netbios_name(iface->nbtsrv->task->lp_ctx),
84 netlogon->req.pdc.mailslot_name,
85 &netlogon_response);
90 reply to a ADS style GETDC request
92 static void nbtd_netlogon_samlogon(struct dgram_mailslot_handler *dgmslot,
93 struct nbtd_interface *iface,
94 struct nbt_dgram_packet *packet,
95 const struct socket_address *src,
96 struct nbt_netlogon_packet *netlogon)
98 struct nbt_name *name = &packet->data.msg.dest_name;
99 struct nbtd_interface *reply_iface = nbtd_find_reply_iface(iface, src->addr, false);
100 struct ldb_context *samctx;
101 const char *my_ip = reply_iface->ip_address;
102 struct dom_sid *sid;
103 struct nbt_netlogon_response netlogon_response;
104 NTSTATUS status;
106 if (!my_ip) {
107 DEBUG(0, ("Could not obtain own IP address for datagram socket\n"));
108 return;
111 /* only answer getdc requests on the PDC or LOGON names */
112 if (name->type != NBT_NAME_PDC && name->type != NBT_NAME_LOGON) {
113 return;
116 samctx = iface->nbtsrv->sam_ctx;
118 if (netlogon->req.logon.sid_size) {
119 sid = &netlogon->req.logon.sid;
120 } else {
121 sid = NULL;
124 status = fill_netlogon_samlogon_response(samctx, packet, NULL, name->name, sid, NULL,
125 netlogon->req.logon.user_name, netlogon->req.logon.acct_control, src->addr,
126 netlogon->req.logon.nt_version, iface->nbtsrv->task->lp_ctx, &netlogon_response.data.samlogon, false);
127 if (!NT_STATUS_IS_OK(status)) {
128 DEBUG(2,("NBT netlogon query failed domain=%s sid=%s version=%d - %s\n",
129 name->name, dom_sid_string(packet, sid), netlogon->req.logon.nt_version, nt_errstr(status)));
130 return;
133 netlogon_response.response_type = NETLOGON_SAMLOGON;
135 packet->data.msg.dest_name.type = 0;
137 dgram_mailslot_netlogon_reply(reply_iface->dgmsock,
138 packet,
139 lpcfg_netbios_name(iface->nbtsrv->task->lp_ctx),
140 netlogon->req.logon.mailslot_name,
141 &netlogon_response);
146 handle incoming netlogon mailslot requests
148 void nbtd_mailslot_netlogon_handler(struct dgram_mailslot_handler *dgmslot,
149 struct nbt_dgram_packet *packet,
150 struct socket_address *src)
152 NTSTATUS status = NT_STATUS_NO_MEMORY;
153 struct nbtd_interface *iface =
154 talloc_get_type(dgmslot->private_data, struct nbtd_interface);
155 struct nbt_netlogon_packet *netlogon =
156 talloc(dgmslot, struct nbt_netlogon_packet);
157 struct nbtd_iface_name *iname;
158 struct nbt_name *name = &packet->data.msg.dest_name;
160 if (netlogon == NULL) goto failed;
163 see if the we are listening on the destination netbios name
165 iname = nbtd_find_iname(iface, name, 0);
166 if (iname == NULL) {
167 status = NT_STATUS_BAD_NETWORK_NAME;
168 goto failed;
171 DEBUG(5,("netlogon request to %s from %s:%d\n",
172 nbt_name_string(netlogon, name), src->addr, src->port));
173 status = dgram_mailslot_netlogon_parse_request(dgmslot, netlogon, packet, netlogon);
174 if (!NT_STATUS_IS_OK(status)) goto failed;
176 switch (netlogon->command) {
177 case LOGON_PRIMARY_QUERY:
178 nbtd_netlogon_getdc(dgmslot, iface, packet,
179 src, netlogon);
180 break;
181 case LOGON_SAM_LOGON_REQUEST:
182 nbtd_netlogon_samlogon(dgmslot, iface, packet,
183 src, netlogon);
184 break;
185 default:
186 DEBUG(2,("unknown netlogon op %d from %s:%d\n",
187 netlogon->command, src->addr, src->port));
188 NDR_PRINT_DEBUG(nbt_netlogon_packet, netlogon);
189 break;
192 talloc_free(netlogon);
193 return;
195 failed:
196 DEBUG(2,("nbtd netlogon handler failed from %s:%d to %s - %s\n",
197 src->addr, src->port, nbt_name_string(netlogon, name),
198 nt_errstr(status)));
199 talloc_free(netlogon);