summaryrefslogtreecommitdiffstats
path: root/libcli/cldap/cldap.c
blob: 7de72b5e8996d424023a52186afd5fa995f4262e (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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
/* 
   Unix SMB/CIFS implementation.

   cldap client library

   Copyright (C) Andrew Tridgell 2005
   Copyright (C) Stefan Metzmacher 2009
   
   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/>.
*/

/*
  see RFC1798 for details of CLDAP

  basic properties
    - carried over UDP on port 389
    - request and response matched by message ID
    - request consists of only a single searchRequest element
    - response can be in one of two forms
       - a single searchResponse, followed by a searchResult
       - a single searchResult
*/

#include "includes.h"
#include <tevent.h>
#include "../lib/util/dlinklist.h"
#include "../libcli/ldap/ldap_message.h"
#include "../libcli/ldap/ldap_ndr.h"
#include "../libcli/cldap/cldap.h"
#include "../lib/tsocket/tsocket.h"
#include "../libcli/security/dom_sid.h"
#include "../librpc/gen_ndr/ndr_nbt.h"
#include "../lib/util/asn1.h"
#include "../lib/util/tevent_ntstatus.h"

#undef strcasecmp

/*
  context structure for operations on cldap packets
*/
struct cldap_socket {
	/* the low level socket */
	struct tdgram_context *sock;

	/*
	 * Are we in connected mode, which means
	 * we get ICMP errors back instead of timing
	 * out requests. And we can only send requests
	 * to the connected peer.
	 */
	bool connected;

	/* the queue for outgoing dgrams */
	struct tevent_queue *send_queue;

	/* do we have an async tsocket_recvfrom request pending */
	struct tevent_req *recv_subreq;

	struct {
		/* a queue of pending search requests */
		struct cldap_search_state *list;

		/* mapping from message_id to pending request */
		struct idr_context *idr;
	} searches;

	/* what to do with incoming request packets */
	struct {
		struct tevent_context *ev;
		void (*handler)(struct cldap_socket *,
				void *private_data,
				struct cldap_incoming *);
		void *private_data;
	} incoming;
};

struct cldap_search_state {
	struct cldap_search_state *prev, *next;

	struct {
		struct tevent_context *ev;
		struct cldap_socket *cldap;
	} caller;

	int message_id;

	struct {
		uint32_t idx;
		uint32_t delay;
		uint32_t count;
		struct tsocket_address *dest;
		DATA_BLOB blob;
	} request;

	struct {
		struct cldap_incoming *in;
		struct asn1_data *asn1;
	} response;

	struct tevent_req *req;
};

/*
 * For CLDAP we limit the maximum search request size to 4kb
 */
#define MAX_SEARCH_REQUEST 4096

static int cldap_socket_destructor(struct cldap_socket *c)
{
	while (c->searches.list) {
		struct cldap_search_state *s = c->searches.list;
		DLIST_REMOVE(c->searches.list, s);
		ZERO_STRUCT(s->caller);
	}

	talloc_free(c->recv_subreq);
	talloc_free(c->send_queue);
	talloc_free(c->sock);
	return 0;
}

static void cldap_recvfrom_done(struct tevent_req *subreq);

static bool cldap_recvfrom_setup(struct cldap_socket *c)
{
	struct tevent_context *ev;

	if (c->recv_subreq) {
		return true;
	}

	if (!c->searches.list && !c->incoming.handler) {
		return true;
	}

	ev = c->incoming.ev;
	if (ev == NULL) {
		/* this shouldn't happen but should be protected against */
		if (c->searches.list == NULL) {
			return false;
		}
		ev = c->searches.list->caller.ev;
	}

	c->recv_subreq = tdgram_recvfrom_send(c, ev, c->sock);
	if (!c->recv_subreq) {
		return false;
	}
	tevent_req_set_callback(c->recv_subreq, cldap_recvfrom_done, c);

	return true;
}

static void cldap_recvfrom_stop(struct cldap_socket *c)
{
	if (!c->recv_subreq) {
		return;
	}

	if (c->searches.list || c->incoming.handler) {
		return;
	}

	talloc_free(c->recv_subreq);
	c->recv_subreq = NULL;
}

static bool cldap_socket_recv_dgram(struct cldap_socket *c,
				    struct cldap_incoming *in);

static void cldap_recvfrom_done(struct tevent_req *subreq)
{
	struct cldap_socket *c = tevent_req_callback_data(subreq,
				 struct cldap_socket);
	struct cldap_incoming *in = NULL;
	ssize_t ret;
	bool setup_done;

	c->recv_subreq = NULL;

	in = talloc_zero(c, struct cldap_incoming);
	if (!in) {
		goto nomem;
	}

	ret = tdgram_recvfrom_recv(subreq,
				   &in->recv_errno,
				   in,
				   &in->buf,
				   &in->src);
	talloc_free(subreq);
	subreq = NULL;
	if (ret >= 0) {
		in->len = ret;
	}
	if (ret == -1 && in->recv_errno == 0) {
		in->recv_errno = EIO;
	}

	/* this function should free or steal 'in' */
	setup_done = cldap_socket_recv_dgram(c, in);
	in = NULL;

	if (!setup_done && !cldap_recvfrom_setup(c)) {
		goto nomem;
	}

	return;

nomem:
	talloc_free(subreq);
	talloc_free(in);
}

/*
  handle recv events on a cldap socket
*/
static bool cldap_socket_recv_dgram(struct cldap_socket *c,
				    struct cldap_incoming *in)
{
	struct asn1_data *asn1;
	void *p;
	struct cldap_search_state *search;
	NTSTATUS status;
	struct ldap_request_limits limits = {
		.max_search_size = MAX_SEARCH_REQUEST
	};

	if (in->recv_errno != 0) {
		goto error;
	}

	asn1 = asn1_init(in, ASN1_MAX_TREE_DEPTH);
	if (!asn1) {
		goto nomem;
	}

	asn1_load_nocopy(asn1, in->buf, in->len);

	in->ldap_msg = talloc(in, struct ldap_message);
	if (in->ldap_msg == NULL) {
		goto nomem;
	}

	/* this initial decode is used to find the message id */
	status = ldap_decode(asn1, &limits, NULL, in->ldap_msg);
	if (!NT_STATUS_IS_OK(status)) {
		goto nterror;
	}

	/* find the pending request */
	p = idr_find(c->searches.idr, in->ldap_msg->messageid);
	if (p == NULL) {
		if (!c->incoming.handler) {
			TALLOC_FREE(in);
			return true;
		}

		/* this function should free or steal 'in' */
		c->incoming.handler(c, c->incoming.private_data, in);
		return false;
	}

	search = talloc_get_type_abort(p, struct cldap_search_state);
	search->response.in = talloc_move(search, &in);

	search->response.asn1 = asn1;

	asn1_load_nocopy(search->response.asn1,
			 search->response.in->buf, search->response.in->len);

	DLIST_REMOVE(c->searches.list, search);

	if (cldap_recvfrom_setup(c)) {
		tevent_req_done(search->req);
		return true;
	}

	/*
	 * This request was ok, just defer the notify of the caller
	 * and then just fail the next request if needed
	 */
	tevent_req_defer_callback(search->req, search->caller.ev);
	tevent_req_done(search->req);

	status = NT_STATUS_NO_MEMORY;
	/* in is NULL it this point */
	goto nterror;
nomem:
	in->recv_errno = ENOMEM;
error:
	status = map_nt_error_from_unix_common(in->recv_errno);
nterror:
	TALLOC_FREE(in);
	/* in connected mode the first pending search gets the error */
	if (!c->connected) {
		/* otherwise we just ignore the error */
		return false;
	}
	if (!c->searches.list) {
		return false;
	}
	/*
	 * We might called tevent_req_done() for a successful
	 * search before, so we better deliver the failure
	 * after the success, that is why we better also
	 * use tevent_req_defer_callback() here.
	 */
	tevent_req_defer_callback(c->searches.list->req,
				  c->searches.list->caller.ev);
	tevent_req_nterror(c->searches.list->req, status);
	return false;
}

/*
  initialise a cldap_sock
*/
NTSTATUS cldap_socket_init(TALLOC_CTX *mem_ctx,
			   const struct tsocket_address *local_addr,
			   const struct tsocket_address *remote_addr,
			   struct cldap_socket **_cldap)
{
	struct cldap_socket *c = NULL;
	struct tsocket_address *any = NULL;
	NTSTATUS status;
	int ret;
	const char *fam = NULL;

	if (local_addr == NULL && remote_addr == NULL) {
		return NT_STATUS_INVALID_PARAMETER_MIX;
	}

	if (remote_addr) {
		bool is_ipv4;
		bool is_ipv6;

		is_ipv4 = tsocket_address_is_inet(remote_addr, "ipv4");
		is_ipv6 = tsocket_address_is_inet(remote_addr, "ipv6");

		if (is_ipv4) {
			fam = "ipv4";
		} else if (is_ipv6) {
			fam = "ipv6";
		} else {
			return NT_STATUS_INVALID_ADDRESS;
		}
	}

	c = talloc_zero(mem_ctx, struct cldap_socket);
	if (!c) {
		goto nomem;
	}

	if (!local_addr) {
		/*
		 * Here we know the address family of the remote address.
		 */
		if (fam == NULL) {
			return NT_STATUS_INVALID_PARAMETER_MIX;
		}

		ret = tsocket_address_inet_from_strings(c, fam,
							NULL, 0,
							&any);
		if (ret != 0) {
			status = map_nt_error_from_unix_common(errno);
			goto nterror;
		}
		local_addr = any;
	}

	c->searches.idr = idr_init(c);
	if (!c->searches.idr) {
		goto nomem;
	}

	ret = tdgram_inet_udp_socket(local_addr, remote_addr,
				     c, &c->sock);
	if (ret != 0) {
		status = map_nt_error_from_unix_common(errno);
		goto nterror;
	}
	talloc_free(any);

	if (remote_addr) {
		c->connected = true;
	}

	c->send_queue = tevent_queue_create(c, "cldap_send_queue");
	if (!c->send_queue) {
		goto nomem;
	}

	talloc_set_destructor(c, cldap_socket_destructor);

	*_cldap = c;
	return NT_STATUS_OK;

nomem:
	status = NT_STATUS_NO_MEMORY;
nterror:
	talloc_free(c);
	return status;
}

/*
  setup a handler for incoming requests
*/
NTSTATUS cldap_set_incoming_handler(struct cldap_socket *c,
				    struct tevent_context *ev,
				    void (*handler)(struct cldap_socket *,
						    void *private_data,
						    struct cldap_incoming *),
				    void *private_data)
{
	if (c->connected) {
		return NT_STATUS_PIPE_CONNECTED;
	}

	c->incoming.ev = ev;
	c->incoming.handler = handler;
	c->incoming.private_data = private_data;

	if (!cldap_recvfrom_setup(c)) {
		ZERO_STRUCT(c->incoming);
		return NT_STATUS_NO_MEMORY;
	}

	return NT_STATUS_OK;
}

struct cldap_reply_state {
	struct tsocket_address *dest;
	DATA_BLOB blob;
};

static void cldap_reply_state_destroy(struct tevent_req *subreq);

/*
  queue a cldap reply for send
*/
NTSTATUS cldap_reply_send(struct cldap_socket *cldap, struct cldap_reply *io)
{
	struct cldap_reply_state *state = NULL;
	struct ldap_message *msg;
	DATA_BLOB blob1, blob2;
	NTSTATUS status;
	struct tevent_req *subreq;

	if (cldap->connected) {
		return NT_STATUS_PIPE_CONNECTED;
	}

	if (cldap->incoming.ev == NULL) {
		return NT_STATUS_INVALID_PIPE_STATE;
	}

	if (!io->dest) {
		return NT_STATUS_INVALID_ADDRESS;
	}

	state = talloc(cldap, struct cldap_reply_state);
	NT_STATUS_HAVE_NO_MEMORY(state);

	state->dest = tsocket_address_copy(io->dest, state);
	if (!state->dest) {
		goto nomem;
	}

	msg = talloc(state, struct ldap_message);
	if (!msg) {
		goto nomem;
	}

	msg->messageid       = io->messageid;
	msg->controls        = NULL;

	if (io->response) {
		msg->type = LDAP_TAG_SearchResultEntry;
		msg->r.SearchResultEntry = *io->response;

		if (!ldap_encode(msg, NULL, &blob1, state)) {
			status = NT_STATUS_INVALID_PARAMETER;
			goto failed;
		}
	} else {
		blob1 = data_blob(NULL, 0);
	}

	msg->type = LDAP_TAG_SearchResultDone;
	msg->r.SearchResultDone = *io->result;

	if (!ldap_encode(msg, NULL, &blob2, state)) {
		status = NT_STATUS_INVALID_PARAMETER;
		goto failed;
	}
	talloc_free(msg);

	state->blob = data_blob_talloc(state, NULL, blob1.length + blob2.length);
	if (!state->blob.data) {
		goto nomem;
	}

	memcpy(state->blob.data, blob1.data, blob1.length);
	memcpy(state->blob.data+blob1.length, blob2.data, blob2.length);
	data_blob_free(&blob1);
	data_blob_free(&blob2);

	subreq = tdgram_sendto_queue_send(state,
					  cldap->incoming.ev,
					  cldap->sock,
					  cldap->send_queue,
					  state->blob.data,
					  state->blob.length,
					  state->dest);
	if (!subreq) {
		goto nomem;
	}
	/* the callback will just free the state, as we don't need a result */
	tevent_req_set_callback(subreq, cldap_reply_state_destroy, state);

	return NT_STATUS_OK;

nomem:
	status = NT_STATUS_NO_MEMORY;
failed:
	talloc_free(state);
	return status;
}

static void cldap_reply_state_destroy(struct tevent_req *subreq)
{
	struct cldap_reply_state *state = tevent_req_callback_data(subreq,
					  struct cldap_reply_state);

	/* we don't want to know the result here, we just free the state */
	talloc_free(subreq);
	talloc_free(state);
}

static int cldap_search_state_destructor(struct cldap_search_state *s)
{
	if (s->caller.cldap) {
		if (s->message_id != -1) {
			idr_remove(s->caller.cldap->searches.idr, s->message_id);
			s->message_id = -1;
		}
		DLIST_REMOVE(s->caller.cldap->searches.list, s);
		cldap_recvfrom_stop(s->caller.cldap);
		ZERO_STRUCT(s->caller);
	}

	return 0;
}

static void cldap_search_state_queue_done(struct tevent_req *subreq);
static void cldap_search_state_wakeup_done(struct tevent_req *subreq);

/*
  queue a cldap reply for send
*/
struct tevent_req *cldap_search_send(TALLOC_CTX *mem_ctx,
				     struct tevent_context *ev,
				     struct cldap_socket *cldap,
				     const struct cldap_search *io)
{
	struct tevent_req *req, *subreq;
	struct cldap_search_state *state = NULL;
	struct ldap_message *msg;
	struct ldap_SearchRequest *search;
	struct timeval now;
	struct timeval end;
	uint32_t i;
	int ret;

	req = tevent_req_create(mem_ctx, &state,
				struct cldap_search_state);
	if (!req) {
		return NULL;
	}
	state->caller.ev = ev;
	state->req = req;
	state->caller.cldap = cldap;
	state->message_id = -1;

	talloc_set_destructor(state, cldap_search_state_destructor);

	if (state->caller.cldap == NULL) {
		tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
		goto post;
	}

	if (io->in.dest_address) {
		if (cldap->connected) {
			tevent_req_nterror(req, NT_STATUS_PIPE_CONNECTED);
			goto post;
		}
		ret = tsocket_address_inet_from_strings(state,
						        "ip",
						        io->in.dest_address,
						        io->in.dest_port,
							&state->request.dest);
		if (ret != 0) {
			tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
			goto post;
		}
	} else {
		if (!cldap->connected) {
			tevent_req_nterror(req, NT_STATUS_INVALID_ADDRESS);
			goto post;
		}
		state->request.dest = NULL;
	}

	state->message_id = idr_get_new_random(cldap->searches.idr,
					       state, UINT16_MAX);
	if (state->message_id == -1) {
		tevent_req_nterror(req, NT_STATUS_INSUFFICIENT_RESOURCES);
		goto post;
	}

	msg = talloc(state, struct ldap_message);
	if (tevent_req_nomem(msg, req)) {
		goto post;
	}

	msg->messageid	= state->message_id;
	msg->type	= LDAP_TAG_SearchRequest;
	msg->controls	= NULL;
	search = &msg->r.SearchRequest;

	search->basedn		= "";
	search->scope		= LDAP_SEARCH_SCOPE_BASE;
	search->deref		= LDAP_DEREFERENCE_NEVER;
	search->timelimit	= 0;
	search->sizelimit	= 0;
	search->attributesonly	= false;
	search->num_attributes	= str_list_length(io->in.attributes);
	search->attributes	= io->in.attributes;
	search->tree		= ldb_parse_tree(msg, io->in.filter);
	if (tevent_req_nomem(search->tree, req)) {
		goto post;
	}

	if (!ldap_encode(msg, NULL, &state->request.blob, state)) {
		tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
		goto post;
	}
	talloc_free(msg);

	state->request.idx = 0;
	state->request.delay = 10*1000*1000;
	state->request.count = 3;
	if (io->in.timeout > 0) {
		state->request.delay = io->in.timeout * 1000 * 1000;
		state->request.count = io->in.retries + 1;
	}

	now = tevent_timeval_current();
	end = now;
	for (i = 0; i < state->request.count; i++) {
		end = tevent_timeval_add(&end, state->request.delay / 1000000,
					 state->request.delay % 1000000);
	}

	if (!tevent_req_set_endtime(req, state->caller.ev, end)) {
		goto post;
	}

	subreq = tdgram_sendto_queue_send(state,
					  state->caller.ev,
					  state->caller.cldap->sock,
					  state->caller.cldap->send_queue,
					  state->request.blob.data,
					  state->request.blob.length,
					  state->request.dest);
	if (tevent_req_nomem(subreq, req)) {
		goto post;
	}
	tevent_req_set_callback(subreq, cldap_search_state_queue_done, req);

	DLIST_ADD_END(cldap->searches.list, state);

	return req;

 post:
	return tevent_req_post(req, state->caller.ev);
}

static void cldap_search_state_queue_done(struct tevent_req *subreq)
{
	struct tevent_req *req = tevent_req_callback_data(subreq,
				 struct tevent_req);
	struct cldap_search_state *state = tevent_req_data(req,
					   struct cldap_search_state);
	ssize_t ret;
	int sys_errno = 0;
	struct timeval next;

	ret = tdgram_sendto_queue_recv(subreq, &sys_errno);
	talloc_free(subreq);
	if (ret == -1) {
		NTSTATUS status;
		status = map_nt_error_from_unix_common(sys_errno);
		DLIST_REMOVE(state->caller.cldap->searches.list, state);
		ZERO_STRUCT(state->caller.cldap);
		tevent_req_nterror(req, status);
		return;
	}

	state->request.idx++;

	/* wait for incoming traffic */
	if (!cldap_recvfrom_setup(state->caller.cldap)) {
		tevent_req_oom(req);
		return;
	}

	if (state->request.idx > state->request.count) {
		/* we just wait for the response or a timeout */
		return;
	}

	next = tevent_timeval_current_ofs(state->request.delay / 1000000,
					  state->request.delay % 1000000);
	subreq = tevent_wakeup_send(state,
				    state->caller.ev,
				    next);
	if (tevent_req_nomem(subreq, req)) {
		return;
	}
	tevent_req_set_callback(subreq, cldap_search_state_wakeup_done, req);
}

static void cldap_search_state_wakeup_done(struct tevent_req *subreq)
{
	struct tevent_req *req = tevent_req_callback_data(subreq,
				 struct tevent_req);
	struct cldap_search_state *state = tevent_req_data(req,
					   struct cldap_search_state);
	bool ok;

	ok = tevent_wakeup_recv(subreq);
	talloc_free(subreq);
	if (!ok) {
		tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
		return;
	}

	subreq = tdgram_sendto_queue_send(state,
					  state->caller.ev,
					  state->caller.cldap->sock,
					  state->caller.cldap->send_queue,
					  state->request.blob.data,
					  state->request.blob.length,
					  state->request.dest);
	if (tevent_req_nomem(subreq, req)) {
		return;
	}
	tevent_req_set_callback(subreq, cldap_search_state_queue_done, req);
}

/*
  receive a cldap reply
*/
NTSTATUS cldap_search_recv(struct tevent_req *req,
			   TALLOC_CTX *mem_ctx,
			   struct cldap_search *io)
{
	struct cldap_search_state *state = tevent_req_data(req,
					   struct cldap_search_state);
	struct ldap_message *ldap_msg;
	NTSTATUS status;
	struct ldap_request_limits limits = {
		.max_search_size = MAX_SEARCH_REQUEST
	};

	if (tevent_req_is_nterror(req, &status)) {
		goto failed;
	}

	ldap_msg = talloc(mem_ctx, struct ldap_message);
	if (!ldap_msg) {
		goto nomem;
	}

	status = ldap_decode(state->response.asn1, &limits, NULL, ldap_msg);
	if (!NT_STATUS_IS_OK(status)) {
		goto failed;
	}

	ZERO_STRUCT(io->out);

	/* the first possible form has a search result in first place */
	if (ldap_msg->type == LDAP_TAG_SearchResultEntry) {
		io->out.response = talloc(mem_ctx, struct ldap_SearchResEntry);
		if (!io->out.response) {
			goto nomem;
		}
		*io->out.response = ldap_msg->r.SearchResultEntry;

		/* decode the 2nd part */
		status = ldap_decode(
			state->response.asn1, &limits, NULL, ldap_msg);
		if (!NT_STATUS_IS_OK(status)) {
			goto failed;
		}
	}

	if (ldap_msg->type != LDAP_TAG_SearchResultDone) {
		status = NT_STATUS_LDAP(LDAP_PROTOCOL_ERROR);
		goto failed;
	}

	io->out.result = talloc(mem_ctx, struct ldap_Result);
	if (!io->out.result) {
		goto nomem;
	}
	*io->out.result = ldap_msg->r.SearchResultDone;

	if (io->out.result->resultcode != LDAP_SUCCESS) {
		status = NT_STATUS_LDAP(io->out.result->resultcode);
		goto failed;
	}

	tevent_req_received(req);
	return NT_STATUS_OK;

nomem:
	status = NT_STATUS_NO_MEMORY;
failed:
	tevent_req_received(req);
	return status;
}


/*
  synchronous cldap search
*/
NTSTATUS cldap_search(struct cldap_socket *cldap,
		      TALLOC_CTX *mem_ctx,
		      struct cldap_search *io)
{
	TALLOC_CTX *frame;
	struct tevent_req *req;
	struct tevent_context *ev;
	NTSTATUS status;

	if (cldap->searches.list) {
		return NT_STATUS_PIPE_BUSY;
	}

	if (cldap->incoming.handler) {
		return NT_STATUS_INVALID_PIPE_STATE;
	}

	frame = talloc_stackframe();

	ev = samba_tevent_context_init(frame);
	if (ev == NULL) {
		TALLOC_FREE(frame);
		return NT_STATUS_NO_MEMORY;
	}

	req = cldap_search_send(mem_ctx, ev, cldap, io);
	if (req == NULL) {
		TALLOC_FREE(frame);
		return NT_STATUS_NO_MEMORY;
	}

	if (!tevent_req_poll(req, ev)) {
		status = map_nt_error_from_unix_common(errno);
		TALLOC_FREE(frame);
		return status;
	}

	status = cldap_search_recv(req, mem_ctx, io);
	if (!NT_STATUS_IS_OK(status)) {
		TALLOC_FREE(frame);
		return status;
	}

	TALLOC_FREE(frame);
	return NT_STATUS_OK;
}

struct cldap_netlogon_state {
	struct cldap_search search;
};

char *cldap_netlogon_create_filter(TALLOC_CTX *mem_ctx,
				   const struct cldap_netlogon *io)
{
	char *filter;

	filter = talloc_asprintf(mem_ctx, "(&(NtVer=%s)",
				 ldap_encode_ndr_uint32(mem_ctx, io->in.version));
	if (filter == NULL)
		return NULL;

	if (io->in.user) {
		filter = talloc_asprintf_append_buffer(filter, "(User=%s)", io->in.user);
		if (filter == NULL) {
			return NULL;
		}
	}
	if (io->in.host) {
		filter = talloc_asprintf_append_buffer(filter, "(Host=%s)", io->in.host);
		if (filter == NULL) {
			return NULL;
		}
	}
	if (io->in.realm) {
		filter = talloc_asprintf_append_buffer(filter, "(DnsDomain=%s)", io->in.realm);
		if (filter == NULL) {
			return NULL;
		}
	}
	if (io->in.acct_control != -1) {
		filter = talloc_asprintf_append_buffer(filter, "(AAC=%s)", 
						ldap_encode_ndr_uint32(mem_ctx, io->in.acct_control));
		if (filter == NULL) {
			return NULL;
		}
	}
	if (io->in.domain_sid) {
		struct dom_sid *sid = dom_sid_parse_talloc(mem_ctx, io->in.domain_sid);

		filter = talloc_asprintf_append_buffer(filter, "(domainSid=%s)",
						ldap_encode_ndr_dom_sid(mem_ctx, sid));
		if (filter == NULL) {
			return NULL;
		}
	}
	if (io->in.domain_guid) {
		struct GUID guid;
		GUID_from_string(io->in.domain_guid, &guid);

		filter = talloc_asprintf_append_buffer(filter, "(DomainGuid=%s)",
						ldap_encode_ndr_GUID(mem_ctx, &guid));
		if (filter == NULL) {
			return NULL;
		}
	}
	filter = talloc_asprintf_append_buffer(filter, ")");

	return filter;
}

static void cldap_netlogon_state_done(struct tevent_req *subreq);
/*
  queue a cldap netlogon for send
*/
struct tevent_req *cldap_netlogon_send(TALLOC_CTX *mem_ctx,
				       struct tevent_context *ev,
				       struct cldap_socket *cldap,
				       const struct cldap_netlogon *io)
{
	struct tevent_req *req, *subreq;
	struct cldap_netlogon_state *state;
	char *filter;
	static const char * const attr[] = { "NetLogon", NULL };

	req = tevent_req_create(mem_ctx, &state,
				struct cldap_netlogon_state);
	if (!req) {
		return NULL;
	}

	filter = cldap_netlogon_create_filter(state, io);
	if (tevent_req_nomem(filter, req)) {
		goto post;
	}

	if (io->in.dest_address) {
		state->search.in.dest_address = talloc_strdup(state,
						io->in.dest_address);
		if (tevent_req_nomem(state->search.in.dest_address, req)) {
			goto post;
		}
		state->search.in.dest_port = io->in.dest_port;
	} else {
		state->search.in.dest_address	= NULL;
		state->search.in.dest_port	= 0;
	}
	state->search.in.filter		= filter;
	state->search.in.attributes	= attr;
	state->search.in.timeout	= 2;
	state->search.in.retries	= 2;

	subreq = cldap_search_send(state, ev, cldap, &state->search);
	if (tevent_req_nomem(subreq, req)) {
		goto post;
	}
	tevent_req_set_callback(subreq, cldap_netlogon_state_done, req);

	return req;
post:
	return tevent_req_post(req, ev);
}

static void cldap_netlogon_state_done(struct tevent_req *subreq)
{
	struct tevent_req *req = tevent_req_callback_data(subreq,
				 struct tevent_req);
	struct cldap_netlogon_state *state = tevent_req_data(req,
					     struct cldap_netlogon_state);
	NTSTATUS status;

	status = cldap_search_recv(subreq, state, &state->search);
	talloc_free(subreq);

	if (tevent_req_nterror(req, status)) {
		return;
	}

	tevent_req_done(req);
}

/*
  receive a cldap netlogon reply
*/
NTSTATUS cldap_netlogon_recv(struct tevent_req *req,
			     TALLOC_CTX *mem_ctx,
			     struct cldap_netlogon *io)
{
	struct cldap_netlogon_state *state = tevent_req_data(req,
					     struct cldap_netlogon_state);
	NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
	DATA_BLOB *data;

	if (tevent_req_is_nterror(req, &status)) {
		goto failed;
	}

	if (state->search.out.response == NULL) {
		status = NT_STATUS_NOT_FOUND;
		goto failed;
	}

	if (state->search.out.response->num_attributes != 1 ||
	    strcasecmp(state->search.out.response->attributes[0].name, "netlogon") != 0 ||
	    state->search.out.response->attributes[0].num_values != 1 ||
	    state->search.out.response->attributes[0].values->length < 2) {
		status = NT_STATUS_UNEXPECTED_NETWORK_ERROR;
		goto failed;
	}
	data = state->search.out.response->attributes[0].values;

	status = pull_netlogon_samlogon_response(data, mem_ctx,
						 &io->out.netlogon);
	if (!NT_STATUS_IS_OK(status)) {
		goto failed;
	}

	if (io->in.map_response) {
		map_netlogon_samlogon_response(&io->out.netlogon);
	}

	status =  NT_STATUS_OK;
failed:
	tevent_req_received(req);
	return status;
}

/*
  sync cldap netlogon search
*/
NTSTATUS cldap_netlogon(struct cldap_socket *cldap,
			TALLOC_CTX *mem_ctx,
			struct cldap_netlogon *io)
{
	TALLOC_CTX *frame;
	struct tevent_req *req;
	struct tevent_context *ev;
	NTSTATUS status;

	if (cldap->searches.list) {
		return NT_STATUS_PIPE_BUSY;
	}

	if (cldap->incoming.handler) {
		return NT_STATUS_INVALID_PIPE_STATE;
	}

	frame = talloc_stackframe();

	ev = samba_tevent_context_init(frame);
	if (ev == NULL) {
		TALLOC_FREE(frame);
		return NT_STATUS_NO_MEMORY;
	}

	req = cldap_netlogon_send(mem_ctx, ev, cldap, io);
	if (req == NULL) {
		TALLOC_FREE(frame);
		return NT_STATUS_NO_MEMORY;
	}

	if (!tevent_req_poll(req, ev)) {
		status = map_nt_error_from_unix_common(errno);
		TALLOC_FREE(frame);
		return status;
	}

	status = cldap_netlogon_recv(req, mem_ctx, io);
	if (!NT_STATUS_IS_OK(status)) {
		TALLOC_FREE(frame);
		return status;
	}

	TALLOC_FREE(frame);
	return NT_STATUS_OK;
}


/*
  send an empty reply (used on any error, so the client doesn't keep waiting
  or send the bad request again)
*/
NTSTATUS cldap_empty_reply(struct cldap_socket *cldap,
			   uint32_t message_id,
			   struct tsocket_address *dest)
{
	NTSTATUS status;
	struct cldap_reply reply;
	struct ldap_Result result;

	reply.messageid    = message_id;
	reply.dest         = dest;
	reply.response     = NULL;
	reply.result       = &result;

	ZERO_STRUCT(result);

	status = cldap_reply_send(cldap, &reply);

	return status;
}

/*
  send an error reply (used on any error, so the client doesn't keep waiting
  or send the bad request again)
*/
NTSTATUS cldap_error_reply(struct cldap_socket *cldap,
			   uint32_t message_id,
			   struct tsocket_address *dest,
			   int resultcode,
			   const char *errormessage)
{
	NTSTATUS status;
	struct cldap_reply reply;
	struct ldap_Result result;

	reply.messageid    = message_id;
	reply.dest         = dest;
	reply.response     = NULL;
	reply.result       = &result;

	ZERO_STRUCT(result);
	result.resultcode	= resultcode;
	result.errormessage	= errormessage;

	status = cldap_reply_send(cldap, &reply);

	return status;
}


/*
  send a netlogon reply 
*/
NTSTATUS cldap_netlogon_reply(struct cldap_socket *cldap,
			      uint32_t message_id,
			      struct tsocket_address *dest,
			      uint32_t version,
			      struct netlogon_samlogon_response *netlogon)
{
	NTSTATUS status;
	struct cldap_reply reply;
	struct ldap_SearchResEntry response;
	struct ldap_Result result;
	TALLOC_CTX *tmp_ctx = talloc_new(cldap);
	DATA_BLOB blob;

	status = push_netlogon_samlogon_response(&blob, tmp_ctx,
						 netlogon);
	if (!NT_STATUS_IS_OK(status)) {
		talloc_free(tmp_ctx);
		return status;
	}
	reply.messageid    = message_id;
	reply.dest         = dest;
	reply.response     = &response;
	reply.result       = &result;

	ZERO_STRUCT(result);

	response.dn = "";
	response.num_attributes = 1;
	response.attributes = talloc(tmp_ctx, struct ldb_message_element);
	NT_STATUS_HAVE_NO_MEMORY(response.attributes);
	response.attributes->name = "netlogon";
	response.attributes->num_values = 1;
	response.attributes->values = &blob;

	status = cldap_reply_send(cldap, &reply);

	talloc_free(tmp_ctx);

	return status;
}