summaryrefslogtreecommitdiff
path: root/source3/libads/cldap.c
blob: c44201ab8b58e968cc741a1724762f67ff41b193 (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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
/* 
   Samba Unix/Linux SMB client library 
   net ads cldap functions 
   Copyright (C) 2001 Andrew Tridgell (tridge@samba.org)
   Copyright (C) 2003 Jim McDonough (jmcd@us.ibm.com)
   Copyright (C) 2008 Guenther Deschner (gd@samba.org)
   Copyright (C) 2009 Stefan Metzmacher (metze@samba.org)

   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 "../libcli/cldap/cldap.h"
#include "../librpc/gen_ndr/ndr_netlogon.h"
#include "../lib/tsocket/tsocket.h"
#include "../lib/util/tevent_ntstatus.h"
#include "libads/cldap.h"

struct cldap_multi_netlogon_state {
	struct tevent_context *ev;
	const struct tsocket_address * const *servers;
	int num_servers;
	const char *domain;
	const char *hostname;
	unsigned ntversion;
	int min_servers;

	struct cldap_socket **cldap;
	struct tevent_req **subreqs;
	int num_sent;
	int num_received;
	int num_good_received;
	struct cldap_netlogon *ios;
	struct netlogon_samlogon_response **responses;
};

static void cldap_multi_netlogon_done(struct tevent_req *subreq);
static void cldap_multi_netlogon_next(struct tevent_req *subreq);

/****************************************************************
****************************************************************/

#define RETURN_ON_FALSE(x) if (!(x)) return false;

bool check_cldap_reply_required_flags(uint32_t ret_flags,
				      uint32_t req_flags)
{
	if (req_flags == 0) {
		return true;
	}

	if (req_flags & DS_PDC_REQUIRED)
		RETURN_ON_FALSE(ret_flags & NBT_SERVER_PDC);

	if (req_flags & DS_GC_SERVER_REQUIRED)
		RETURN_ON_FALSE(ret_flags & NBT_SERVER_GC);

	if (req_flags & DS_ONLY_LDAP_NEEDED)
		RETURN_ON_FALSE(ret_flags & NBT_SERVER_LDAP);

	if ((req_flags & DS_DIRECTORY_SERVICE_REQUIRED) ||
	    (req_flags & DS_DIRECTORY_SERVICE_PREFERRED))
		RETURN_ON_FALSE(ret_flags & NBT_SERVER_DS);

	if (req_flags & DS_KDC_REQUIRED)
		RETURN_ON_FALSE(ret_flags & NBT_SERVER_KDC);

	if (req_flags & DS_TIMESERV_REQUIRED)
		RETURN_ON_FALSE(ret_flags & NBT_SERVER_TIMESERV);

	if (req_flags & DS_WRITABLE_REQUIRED)
		RETURN_ON_FALSE(ret_flags & NBT_SERVER_WRITABLE);

	return true;
}

/*
 * Do a parallel cldap ping to the servers. The first "min_servers"
 * are fired directly, the remaining ones in 100msec intervals. If
 * "min_servers" responses came in successfully, we immediately reply,
 * not waiting for the remaining ones.
 */

struct tevent_req *cldap_multi_netlogon_send(
	TALLOC_CTX *mem_ctx, struct tevent_context *ev,
	const struct tsocket_address * const *servers, int num_servers,
	const char *domain, const char *hostname, unsigned ntversion,
	int min_servers)
{
	struct tevent_req *req, *subreq;
	struct cldap_multi_netlogon_state *state;
	int i;

	req = tevent_req_create(mem_ctx, &state,
				struct cldap_multi_netlogon_state);
	if (req == NULL) {
		return NULL;
	}
	state->ev = ev;
	state->servers = servers;
	state->num_servers = num_servers;
	state->domain = domain;
	state->hostname = hostname;
	state->ntversion = ntversion;
	state->min_servers = min_servers;

	if (min_servers > num_servers) {
		tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
		return tevent_req_post(req, ev);
	}

	state->subreqs = talloc_zero_array(state,
					   struct tevent_req *,
					   num_servers);
	if (tevent_req_nomem(state->subreqs, req)) {
		return tevent_req_post(req, ev);
	}

	state->cldap = talloc_zero_array(state,
					 struct cldap_socket *,
					 num_servers);
	if (tevent_req_nomem(state->cldap, req)) {
		return tevent_req_post(req, ev);
	}

	state->responses = talloc_zero_array(state,
				struct netlogon_samlogon_response *,
				num_servers);
	if (tevent_req_nomem(state->responses, req)) {
		return tevent_req_post(req, ev);
	}

	state->ios = talloc_zero_array(state->responses,
				       struct cldap_netlogon,
				       num_servers);
	if (tevent_req_nomem(state->ios, req)) {
		return tevent_req_post(req, ev);
	}

	for (i=0; i<num_servers; i++) {
		NTSTATUS status;

		status = cldap_socket_init(state->cldap,
					   NULL, /* local_addr */
					   state->servers[i],
					   &state->cldap[i]);
		if (!NT_STATUS_IS_OK(status)) {
			/*
			 * Don't error out all sends just
			 * because one cldap_socket_init() failed.
			 * Log it here, and the cldap_netlogon_send()
			 * will catch it (with in.dest_address == NULL)
			 * and correctly error out in
			 * cldap_multi_netlogon_done(). This still allows
			 * the other requests to be concurrently sent.
			 */
			DBG_NOTICE("cldap_socket_init failed for %s "
				" error %s\n",
				tsocket_address_string(state->servers[i],
					req),
				nt_errstr(status));
		}

		state->ios[i].in.dest_address	= NULL;
		state->ios[i].in.dest_port	= 0;
		state->ios[i].in.realm		= domain;
		state->ios[i].in.host		= NULL;
		state->ios[i].in.user		= NULL;
		state->ios[i].in.domain_guid	= NULL;
		state->ios[i].in.domain_sid	= NULL;
		state->ios[i].in.acct_control	= 0;
		state->ios[i].in.version	= ntversion;
		state->ios[i].in.map_response	= false;
	}

	for (i=0; i<min_servers; i++) {
		state->subreqs[i] = cldap_netlogon_send(state->subreqs,
							state->ev,
							state->cldap[i],
							&state->ios[i]);
		if (tevent_req_nomem(state->subreqs[i], req)) {
			return tevent_req_post(req, ev);
		}
		tevent_req_set_callback(
			state->subreqs[i], cldap_multi_netlogon_done, req);
	}
	state->num_sent = min_servers;

	if (state->num_sent < state->num_servers) {
		/*
		 * After 100 milliseconds fire the next one
		 */
		subreq = tevent_wakeup_send(state, state->ev,
					    timeval_current_ofs(0, 100000));
		if (tevent_req_nomem(subreq, req)) {
			return tevent_req_post(req, ev);
		}
		tevent_req_set_callback(subreq, cldap_multi_netlogon_next,
					req);
	}

	return req;
}

static void cldap_multi_netlogon_done(struct tevent_req *subreq)
{
	struct tevent_req *req = tevent_req_callback_data(
		subreq, struct tevent_req);
	struct cldap_multi_netlogon_state *state = tevent_req_data(
		req, struct cldap_multi_netlogon_state);
	NTSTATUS status;
	struct netlogon_samlogon_response *response;
	int i;

	for (i=0; i<state->num_sent; i++) {
		if (state->subreqs[i] == subreq) {
			break;
		}
	}
	if (i == state->num_sent) {
		/*
		 * Got a response we did not fire...
		 */
		tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
		return;
	}
	state->subreqs[i] = NULL;

	response = talloc_zero(state, struct netlogon_samlogon_response);
	if (tevent_req_nomem(response, req)) {
		return;
	}

	status = cldap_netlogon_recv(subreq, response,
				     &state->ios[i]);
	TALLOC_FREE(subreq);
	state->num_received += 1;

	if (NT_STATUS_IS_OK(status)) {
		*response = state->ios[i].out.netlogon;
		state->responses[i] = talloc_move(state->responses,
						  &response);
		state->num_good_received += 1;
	}

	if ((state->num_received == state->num_servers) ||
	    (state->num_good_received >= state->min_servers)) {
		tevent_req_done(req);
		return;
	}
}

static void cldap_multi_netlogon_next(struct tevent_req *subreq)
{
	struct tevent_req *req = tevent_req_callback_data(
		subreq, struct tevent_req);
	struct cldap_multi_netlogon_state *state = tevent_req_data(
		req, struct cldap_multi_netlogon_state);
	bool ret;

	ret = tevent_wakeup_recv(subreq);
	TALLOC_FREE(subreq);
	if (!ret) {
		tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
		return;
	}

	subreq = cldap_netlogon_send(state->subreqs,
				     state->ev,
				     state->cldap[state->num_sent],
				     &state->ios[state->num_sent]);
	if (tevent_req_nomem(subreq, req)) {
		return;
	}
	tevent_req_set_callback(subreq, cldap_multi_netlogon_done, req);
	state->subreqs[state->num_sent] = subreq;
	state->num_sent += 1;

	if (state->num_sent < state->num_servers) {
		/*
		 * After 100 milliseconds fire the next one
		 */
		subreq = tevent_wakeup_send(state, state->ev,
					    timeval_current_ofs(0, 100000));
		if (tevent_req_nomem(subreq, req)) {
			return;
		}
		tevent_req_set_callback(subreq, cldap_multi_netlogon_next,
					req);
	}
}

NTSTATUS cldap_multi_netlogon_recv(
	struct tevent_req *req, TALLOC_CTX *mem_ctx,
	struct netlogon_samlogon_response ***responses)
{
	struct cldap_multi_netlogon_state *state = tevent_req_data(
		req, struct cldap_multi_netlogon_state);
	NTSTATUS status;

	if (tevent_req_is_nterror(req, &status) &&
	    !NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
		return status;
	}
	/*
	 * If we timeout, give back what we have so far
	 */
	*responses = talloc_move(mem_ctx, &state->responses);
	return NT_STATUS_OK;
}

NTSTATUS cldap_multi_netlogon(
	TALLOC_CTX *mem_ctx,
	const struct tsocket_address * const *servers,
	int num_servers,
	const char *domain, const char *hostname, unsigned ntversion,
	int min_servers, struct timeval timeout,
	struct netlogon_samlogon_response ***responses)
{
	struct tevent_context *ev;
	struct tevent_req *req;
	NTSTATUS status = NT_STATUS_NO_MEMORY;

	ev = samba_tevent_context_init(talloc_tos());
	if (ev == NULL) {
		goto fail;
	}
	req = cldap_multi_netlogon_send(
		ev, ev, servers, num_servers, domain, hostname, ntversion,
		min_servers);
	if (req == NULL) {
		goto fail;
	}
	if (!tevent_req_set_endtime(req, ev, timeout)) {
		goto fail;
	}
	if (!tevent_req_poll_ntstatus(req, ev, &status)) {
		goto fail;
	}
	status = cldap_multi_netlogon_recv(req, mem_ctx, responses);
fail:
	TALLOC_FREE(ev);
	return status;
}

/*******************************************************************
  do a cldap netlogon query.  Always 389/udp
*******************************************************************/

bool ads_cldap_netlogon(TALLOC_CTX *mem_ctx,
			struct sockaddr_storage *ss,
			const char *realm,
			uint32_t nt_version,
			struct netlogon_samlogon_response **_reply)
{
	NTSTATUS status;
	char addrstr[INET6_ADDRSTRLEN];
	const char *dest_str;
	struct tsocket_address *dest_addr;
	const struct tsocket_address * const *dest_addrs;
	struct netlogon_samlogon_response **responses = NULL;
	int ret;

	dest_str = print_sockaddr(addrstr, sizeof(addrstr), ss);

	ret = tsocket_address_inet_from_strings(mem_ctx, "ip",
						dest_str, LDAP_PORT,
						&dest_addr);
	if (ret != 0) {
		status = map_nt_error_from_unix(errno);
		DEBUG(2,("Failed to create cldap tsocket_address for %s - %s\n",
			 dest_str, nt_errstr(status)));
		return false;
	}

	dest_addrs = (const struct tsocket_address * const *)&dest_addr;

	status = cldap_multi_netlogon(talloc_tos(),
				dest_addrs, 1,
				realm, NULL,
				nt_version, 1,
				timeval_current_ofs(MAX(3,lp_ldap_timeout()/2), 0),
				&responses);
	if (!NT_STATUS_IS_OK(status)) {
		DEBUG(2, ("ads_cldap_netlogon: cldap_multi_netlogon "
			  "failed: %s\n", nt_errstr(status)));
		return false;
	}
	if (responses == NULL || responses[0] == NULL) {
		DEBUG(2, ("ads_cldap_netlogon: did not get a reply\n"));
		TALLOC_FREE(responses);
		return false;
	}
	*_reply = talloc_move(mem_ctx, &responses[0]);

	return true;
}

/*******************************************************************
  do a cldap netlogon query.  Always 389/udp
*******************************************************************/

bool ads_cldap_netlogon_5(TALLOC_CTX *mem_ctx,
			  struct sockaddr_storage *ss,
			  const char *realm,
			  struct NETLOGON_SAM_LOGON_RESPONSE_EX *reply5)
{
	uint32_t nt_version = NETLOGON_NT_VERSION_5 | NETLOGON_NT_VERSION_5EX;
	struct netlogon_samlogon_response *reply = NULL;
	bool ret;

	ret = ads_cldap_netlogon(mem_ctx, ss, realm, nt_version, &reply);
	if (!ret) {
		return false;
	}

	if (reply->ntver != NETLOGON_NT_VERSION_5EX) {
		DEBUG(0,("ads_cldap_netlogon_5: nt_version mismatch: 0x%08x\n",
			reply->ntver));
		return false;
	}

	*reply5 = reply->data.nt5_ex;

	return true;
}