summaryrefslogtreecommitdiff
path: root/source4/nbt_server/dgram/netlogon.c
blob: 7fae6bc1f6c67d8db04e82b9c62a0f01a1b5697c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/* 
   Unix SMB/CIFS implementation.

   NBT datagram netlogon server

   Copyright (C) Andrew Tridgell	2005
   
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3 of the License, or
   (at your option) any later version.
   
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
   
   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "includes.h"
#include "nbt_server/nbt_server.h"
#include "lib/socket/socket.h"
#include "lib/ldb/include/ldb.h"
#include "dsdb/samdb/samdb.h"
#include "auth/auth.h"
#include "util/util_ldb.h"
#include "librpc/gen_ndr/ndr_nbt.h"
#include "param/param.h"
#include "smbd/service_task.h"

/*
  reply to a GETDC request
 */
static void nbtd_netlogon_getdc(struct dgram_mailslot_handler *dgmslot, 
				struct nbtd_interface *iface,
				struct nbt_dgram_packet *packet, 
				const struct socket_address *src,
				struct nbt_netlogon_packet *netlogon)
{
	struct nbt_name *name = &packet->data.msg.dest_name;
	struct nbtd_interface *reply_iface = nbtd_find_reply_iface(iface, src->addr, false);
	struct nbt_netlogon_packet reply;
	struct nbt_netlogon_response_from_pdc *pdc;
	const char *ref_attrs[] = {"nETBIOSName", NULL};
	struct ldb_message **ref_res;
	struct ldb_context *samctx;
	struct ldb_dn *partitions_basedn;
	int ret;

	/* only answer getdc requests on the PDC or LOGON names */
	if (name->type != NBT_NAME_PDC && name->type != NBT_NAME_LOGON) {
		return;
	}

	samctx = samdb_connect(packet, iface->nbtsrv->task->event_ctx, iface->nbtsrv->task->lp_ctx, anonymous_session(packet, iface->nbtsrv->task->event_ctx, iface->nbtsrv->task->lp_ctx));
	if (samctx == NULL) {
		DEBUG(2,("Unable to open sam in getdc reply\n"));
		return;
	}

	partitions_basedn = samdb_partitions_dn(samctx, packet);

	ret = gendb_search(samctx, packet, partitions_basedn, &ref_res, ref_attrs,
			   "(&(&(nETBIOSName=%s)(objectclass=crossRef))(ncName=*))", 
			   name->name);
	
	if (ret != 1) {
		DEBUG(2,("Unable to find domain reference '%s' in sam\n", name->name));
		return;
	}

	/* setup a GETDC reply */
	ZERO_STRUCT(reply);
	reply.command = NETLOGON_RESPONSE_FROM_PDC;
	pdc = &reply.req.response;

	pdc->pdc_name         = lp_netbios_name(iface->nbtsrv->task->lp_ctx);
	pdc->unicode_pdc_name = pdc->pdc_name;
	pdc->domain_name      = samdb_result_string(ref_res[0], "nETBIOSName", name->name);;
	pdc->nt_version       = 1;
	pdc->lmnt_token       = 0xFFFF;
	pdc->lm20_token       = 0xFFFF;


	packet->data.msg.dest_name.type = 0;

	dgram_mailslot_netlogon_reply(reply_iface->dgmsock, 
				      packet, 
				      lp_netbios_name(iface->nbtsrv->task->lp_ctx),
				      netlogon->req.pdc.mailslot_name,
				      &reply);
}


/*
  reply to a ADS style GETDC request
 */
static void nbtd_netlogon_getdc2(struct dgram_mailslot_handler *dgmslot,
				 struct nbtd_interface *iface,
				 struct nbt_dgram_packet *packet, 
				 const struct socket_address *src,
				 struct nbt_netlogon_packet *netlogon)
{
	struct nbt_name *name = &packet->data.msg.dest_name;
	struct nbtd_interface *reply_iface = nbtd_find_reply_iface(iface, src->addr, false);
	struct nbt_netlogon_packet reply;
	struct nbt_netlogon_response_from_pdc2 *pdc;
	struct ldb_context *samctx;
	const char *ref_attrs[] = {"nETBIOSName", "dnsRoot", "ncName", NULL};
	const char *dom_attrs[] = {"objectGUID", NULL};
	struct ldb_message **ref_res, **dom_res;
	int ret;
	const char **services = lp_server_services(iface->nbtsrv->task->lp_ctx);
	const char *my_ip = reply_iface->ip_address; 
	struct ldb_dn *partitions_basedn;
	if (!my_ip) {
		DEBUG(0, ("Could not obtain own IP address for datagram socket\n"));
		return;
	}

	/* only answer getdc requests on the PDC or LOGON names */
	if (name->type != NBT_NAME_PDC && name->type != NBT_NAME_LOGON) {
		return;
	}

	samctx = samdb_connect(packet, iface->nbtsrv->task->event_ctx, iface->nbtsrv->task->lp_ctx, anonymous_session(packet, iface->nbtsrv->task->event_ctx, iface->nbtsrv->task->lp_ctx));
	if (samctx == NULL) {
		DEBUG(2,("Unable to open sam in getdc reply\n"));
		return;
	}

	partitions_basedn = samdb_partitions_dn(samctx, packet);

	ret = gendb_search(samctx, packet, partitions_basedn, &ref_res, ref_attrs,
				  "(&(&(nETBIOSName=%s)(objectclass=crossRef))(ncName=*))", 
				  name->name);
	
	if (ret != 1) {
		DEBUG(2,("Unable to find domain reference '%s' in sam\n", name->name));
		return;
	}

	/* try and find the domain */
	ret = gendb_search_dn(samctx, packet, 
			      samdb_result_dn(samctx, samctx, ref_res[0], "ncName", NULL), 
			      &dom_res, dom_attrs);
	if (ret != 1) {
		DEBUG(2,("Unable to find domain from reference '%s' in sam\n",
			 ldb_dn_get_linearized(ref_res[0]->dn)));
		return;
	}

	/* setup a GETDC reply */
	ZERO_STRUCT(reply);
	reply.command = NETLOGON_RESPONSE_FROM_PDC2;

#if 0
	/* newer testing shows that the reply command type is not
	   changed based on whether a username is given in the
	   reply. This was what was causing the w2k join to be so
	   slow */
	if (netlogon->req.pdc2.user_name[0]) {
		reply.command = NETLOGON_RESPONSE_FROM_PDC_USER;
	}
#endif

	pdc = &reply.req.response2;

	/* TODO: accurately depict which services we are running */
	pdc->server_type      = 
		NBT_SERVER_PDC | NBT_SERVER_GC | 
		NBT_SERVER_DS | NBT_SERVER_TIMESERV |
		NBT_SERVER_CLOSEST | NBT_SERVER_WRITABLE | 
		NBT_SERVER_GOOD_TIMESERV;

	/* hmm, probably a better way to do this */
	if (str_list_check(services, "ldap")) {
		pdc->server_type |= NBT_SERVER_LDAP;
	}

	if (str_list_check(services, "kdc")) {
		pdc->server_type |= NBT_SERVER_KDC;
	}

	pdc->domain_uuid      = samdb_result_guid(dom_res[0], "objectGUID");
	pdc->forest           = samdb_result_string(ref_res[0], "dnsRoot", 
						    lp_realm(iface->nbtsrv->task->lp_ctx));
	pdc->dns_domain       = samdb_result_string(ref_res[0], "dnsRoot", 
						    lp_realm(iface->nbtsrv->task->lp_ctx));

	/* TODO: get our full DNS name from somewhere else */
	pdc->pdc_dns_name     = talloc_asprintf(packet, "%s.%s", 
						strlower_talloc(packet, 
								lp_netbios_name(iface->nbtsrv->task->lp_ctx)), 
						pdc->dns_domain);
	pdc->domain           = samdb_result_string(ref_res[0], "nETBIOSName", name->name);;
	pdc->pdc_name         = lp_netbios_name(iface->nbtsrv->task->lp_ctx);
	pdc->user_name        = netlogon->req.pdc2.user_name;
	/* TODO: we need to make sure these are in our DNS zone */
	pdc->server_site      = "Default-First-Site-Name";
	pdc->client_site      = "Default-First-Site-Name";
	pdc->unknown          = 0x10; /* what is this? */
	pdc->unknown2         = 2; /* and this ... */
	pdc->pdc_ip           = my_ip;
	pdc->nt_version       = 13;
	pdc->lmnt_token       = 0xFFFF;
	pdc->lm20_token       = 0xFFFF;

	packet->data.msg.dest_name.type = 0;

	dgram_mailslot_netlogon_reply(reply_iface->dgmsock, 
				      packet, 
				      lp_netbios_name(iface->nbtsrv->task->lp_ctx),
				      netlogon->req.pdc2.mailslot_name,
				      &reply);
}


/*
  handle incoming netlogon mailslot requests
*/
void nbtd_mailslot_netlogon_handler(struct dgram_mailslot_handler *dgmslot, 
				    struct nbt_dgram_packet *packet, 
				    struct socket_address *src)
{
	NTSTATUS status = NT_STATUS_NO_MEMORY;
	struct nbtd_interface *iface = 
		talloc_get_type(dgmslot->private, struct nbtd_interface);
	struct nbt_netlogon_packet *netlogon = 
		talloc(dgmslot, struct nbt_netlogon_packet);
	struct nbtd_iface_name *iname;
	struct nbt_name *name = &packet->data.msg.dest_name;

	if (netlogon == NULL) goto failed;

	/*
	  see if the we are listening on the destination netbios name
	*/
	iname = nbtd_find_iname(iface, name, 0);
	if (iname == NULL) {
		status = NT_STATUS_BAD_NETWORK_NAME;
		goto failed;
	}

	DEBUG(2,("netlogon request to %s from %s:%d\n", 
		 nbt_name_string(netlogon, name), src->addr, src->port));
	status = dgram_mailslot_netlogon_parse(dgmslot, netlogon, packet, netlogon);
	if (!NT_STATUS_IS_OK(status)) goto failed;

	switch (netlogon->command) {
	case NETLOGON_QUERY_FOR_PDC:
		nbtd_netlogon_getdc(dgmslot, iface, packet, src, netlogon);
		break;
	case NETLOGON_QUERY_FOR_PDC2:
		nbtd_netlogon_getdc2(dgmslot, iface, packet, src, netlogon);
		break;
	default:
		DEBUG(2,("unknown netlogon op %d from %s:%d\n", 
			 netlogon->command, src->addr, src->port));
		NDR_PRINT_DEBUG(nbt_netlogon_packet, netlogon);
		break;
	}

	talloc_free(netlogon);
	return;

failed:
	DEBUG(2,("nbtd netlogon handler failed from %s:%d to %s - %s\n",
		 src->addr, src->port, nbt_name_string(netlogon, name),
		 nt_errstr(status)));
	talloc_free(netlogon);
}