2 Unix SMB/CIFS implementation.
6 Copyright (C) Andrew Tridgell 2005
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 see RFC1798 for details of CLDAP
27 - carried over UDP on port 389
28 - request and response matched by message ID
29 - request consists of only a single searchRequest element
30 - response can be in one of two forms
31 - a single searchResponse, followed by a searchResult
32 - a single searchResult
36 #include "lib/events/events.h"
37 #include "dlinklist.h"
38 #include "libcli/ldap/ldap.h"
39 #include "libcli/cldap/cldap.h"
40 #include "lib/socket/socket.h"
41 #include "include/asn_1.h"
44 destroy a pending request
46 static int cldap_request_destructor(void *ptr)
48 struct cldap_request *req = talloc_get_type(ptr, struct cldap_request);
49 if (req->state == CLDAP_REQUEST_SEND) {
50 DLIST_REMOVE(req->cldap->send_queue, req);
52 if (!req->is_reply && req->message_id != 0) {
53 idr_remove(req->cldap->idr, req->message_id);
60 handle recv events on a cldap socket
62 static void cldap_socket_recv(struct cldap_socket *cldap)
64 TALLOC_CTX *tmp_ctx = talloc_new(cldap);
70 struct asn1_data asn1;
71 struct ldap_message ldap_msg;
72 struct cldap_request *req;
74 status = socket_pending(cldap->sock, &dsize);
75 if (!NT_STATUS_IS_OK(status)) {
80 blob = data_blob_talloc(tmp_ctx, NULL, dsize);
81 if (blob.data == NULL) {
86 status = socket_recvfrom(cldap->sock, blob.data, blob.length, &nread, 0,
87 &src_addr, &src_port);
88 if (!NT_STATUS_IS_OK(status)) {
92 talloc_steal(tmp_ctx, src_addr);
95 DEBUG(2,("Received cldap packet of length %d from %s:%d\n",
96 blob.length, src_addr, src_port));
98 if (!asn1_load(&asn1, blob)) {
99 DEBUG(2,("Failed to setup for asn.1 decode\n"));
100 talloc_free(tmp_ctx);
103 talloc_steal(tmp_ctx, asn1.data);
105 ZERO_STRUCT(ldap_msg);
106 ldap_msg.mem_ctx = tmp_ctx;
108 /* this initial decode is used to find the message id */
109 if (!ldap_decode(&asn1, &ldap_msg)) {
110 DEBUG(2,("Failed to decode ldap message\n"));
111 talloc_free(tmp_ctx);
115 /* find the pending request */
116 req = idr_find(cldap->idr, ldap_msg.messageid);
118 if (cldap->incoming.handler) {
119 cldap->incoming.handler(cldap, &ldap_msg, src_addr, src_port);
121 DEBUG(2,("Mismatched cldap reply %u from %s:%d\n",
122 ldap_msg.messageid, src_addr, src_port));
124 talloc_free(tmp_ctx);
129 talloc_steal(req, asn1.data);
132 req->state = CLDAP_REQUEST_DONE;
133 talloc_free(req->te);
135 talloc_free(tmp_ctx);
143 handle request timeouts
145 static void cldap_request_timeout(struct event_context *event_ctx,
146 struct timed_event *te, struct timeval t,
149 struct cldap_request *req = talloc_get_type(private, struct cldap_request);
151 /* possibly try again */
152 if (req->num_retries != 0) {
153 size_t len = req->encoded.length;
157 socket_sendto(req->cldap->sock, &req->encoded, &len, 0,
158 req->dest_addr, req->dest_port);
160 req->te = event_add_timed(req->cldap->event_ctx, req,
161 timeval_current_ofs(req->timeout, 0),
162 cldap_request_timeout, req);
166 req->state = CLDAP_REQUEST_TIMEOUT;
173 handle send events on a cldap socket
175 static void cldap_socket_send(struct cldap_socket *cldap)
177 struct cldap_request *req;
180 while ((req = cldap->send_queue)) {
183 len = req->encoded.length;
184 status = socket_sendto(cldap->sock, &req->encoded, &len, 0,
185 req->dest_addr, req->dest_port);
186 if (NT_STATUS_IS_ERR(status)) {
187 DEBUG(3,("Failed to send cldap request of length %u to %s:%d\n",
188 req->encoded.length, req->dest_addr, req->dest_port));
189 DLIST_REMOVE(cldap->send_queue, req);
194 if (!NT_STATUS_IS_OK(status)) return;
196 DLIST_REMOVE(cldap->send_queue, req);
201 req->state = CLDAP_REQUEST_WAIT;
203 req->te = event_add_timed(cldap->event_ctx, req,
204 timeval_current_ofs(req->timeout, 0),
205 cldap_request_timeout, req);
207 EVENT_FD_READABLE(cldap->fde);
211 EVENT_FD_NOT_WRITEABLE(cldap->fde);
217 handle fd events on a cldap_socket
219 static void cldap_socket_handler(struct event_context *ev, struct fd_event *fde,
220 uint16_t flags, void *private)
222 struct cldap_socket *cldap = talloc_get_type(private, struct cldap_socket);
223 if (flags & EVENT_FD_WRITE) {
224 cldap_socket_send(cldap);
226 if (flags & EVENT_FD_READ) {
227 cldap_socket_recv(cldap);
232 initialise a cldap_socket. The event_ctx is optional, if provided
233 then operations will use that event context
235 struct cldap_socket *cldap_socket_init(TALLOC_CTX *mem_ctx,
236 struct event_context *event_ctx)
238 struct cldap_socket *cldap;
241 cldap = talloc(mem_ctx, struct cldap_socket);
242 if (cldap == NULL) goto failed;
244 if (event_ctx == NULL) {
245 cldap->event_ctx = event_context_init(cldap);
247 cldap->event_ctx = talloc_reference(cldap, event_ctx);
249 if (cldap->event_ctx == NULL) goto failed;
251 cldap->idr = idr_init(cldap);
252 if (cldap->idr == NULL) goto failed;
254 status = socket_create("ip", SOCKET_TYPE_DGRAM, &cldap->sock, 0);
255 if (!NT_STATUS_IS_OK(status)) goto failed;
257 talloc_steal(cldap, cldap->sock);
259 cldap->fde = event_add_fd(cldap->event_ctx, cldap,
260 socket_get_fd(cldap->sock), 0,
261 cldap_socket_handler, cldap);
263 cldap->send_queue = NULL;
264 cldap->incoming.handler = NULL;
275 setup a handler for incoming requests
277 NTSTATUS cldap_set_incoming_handler(struct cldap_socket *cldap,
278 void (*handler)(struct cldap_socket *, struct ldap_message *,
282 cldap->incoming.handler = handler;
283 cldap->incoming.private = private;
284 EVENT_FD_READABLE(cldap->fde);
289 queue a cldap request for send
291 struct cldap_request *cldap_search_send(struct cldap_socket *cldap,
292 struct cldap_search *io)
294 struct ldap_message msg;
295 struct cldap_request *req;
296 struct ldap_SearchRequest *search;
298 req = talloc_zero(cldap, struct cldap_request);
299 if (req == NULL) goto failed;
302 req->state = CLDAP_REQUEST_SEND;
303 req->timeout = io->in.timeout;
304 req->num_retries = io->in.retries;
305 req->is_reply = False;
307 req->dest_addr = talloc_strdup(req, io->in.dest_address);
308 if (req->dest_addr == NULL) goto failed;
309 req->dest_port = lp_cldap_port();
311 req->message_id = idr_get_new_random(cldap->idr, req, UINT16_MAX);
312 if (req->message_id == -1) goto failed;
314 talloc_set_destructor(req, cldap_request_destructor);
317 msg.messageid = req->message_id;
318 msg.type = LDAP_TAG_SearchRequest;
319 msg.num_controls = 0;
321 search = &msg.r.SearchRequest;
324 search->scope = LDAP_SEARCH_SCOPE_BASE;
325 search->deref = LDAP_DEREFERENCE_NEVER;
326 search->timelimit = 0;
327 search->sizelimit = 0;
328 search->attributesonly = False;
329 search->num_attributes = str_list_length(io->in.attributes);
330 search->attributes = io->in.attributes;
331 search->filter = io->in.filter;
333 if (!ldap_encode(&msg, &req->encoded)) {
334 DEBUG(0,("Failed to encode cldap message to %s:%d\n",
335 req->dest_addr, req->dest_port));
338 talloc_steal(req, req->encoded.data);
340 DLIST_ADD_END(cldap->send_queue, req, struct cldap_request *);
342 EVENT_FD_WRITEABLE(cldap->fde);
353 queue a cldap reply for send
355 NTSTATUS cldap_reply_send(struct cldap_socket *cldap, struct cldap_reply *io)
357 struct ldap_message msg;
358 struct cldap_request *req;
359 DATA_BLOB blob1, blob2;
360 NTSTATUS status = NT_STATUS_NO_MEMORY;
362 req = talloc_zero(cldap, struct cldap_request);
363 if (req == NULL) goto failed;
366 req->state = CLDAP_REQUEST_SEND;
367 req->is_reply = True;
369 req->dest_addr = talloc_strdup(req, io->dest_address);
370 if (req->dest_addr == NULL) goto failed;
371 req->dest_port = io->dest_port;
373 talloc_set_destructor(req, cldap_request_destructor);
376 msg.messageid = io->messageid;
377 msg.num_controls = 0;
381 msg.type = LDAP_TAG_SearchResultEntry;
382 msg.r.SearchResultEntry = *io->response;
384 if (!ldap_encode(&msg, &blob1)) {
385 DEBUG(0,("Failed to encode cldap message to %s:%d\n",
386 req->dest_addr, req->dest_port));
387 status = NT_STATUS_INVALID_PARAMETER;
390 talloc_steal(req, blob1.data);
392 blob1 = data_blob(NULL, 0);
395 msg.type = LDAP_TAG_SearchResultDone;
396 msg.r.SearchResultDone = *io->result;
398 if (!ldap_encode(&msg, &blob2)) {
399 DEBUG(0,("Failed to encode cldap message to %s:%d\n",
400 req->dest_addr, req->dest_port));
401 status = NT_STATUS_INVALID_PARAMETER;
404 talloc_steal(req, blob2.data);
406 req->encoded = data_blob_talloc(req, NULL, blob1.length + blob2.length);
407 if (req->encoded.data == NULL) goto failed;
409 memcpy(req->encoded.data, blob1.data, blob1.length);
410 memcpy(req->encoded.data+blob1.length, blob2.data, blob2.length);
412 DLIST_ADD_END(cldap->send_queue, req, struct cldap_request *);
414 EVENT_FD_WRITEABLE(cldap->fde);
424 receive a cldap reply
426 NTSTATUS cldap_search_recv(struct cldap_request *req,
428 struct cldap_search *io)
430 struct ldap_message ldap_msg;
433 return NT_STATUS_NO_MEMORY;
436 while (req->state < CLDAP_REQUEST_DONE) {
437 if (event_loop_once(req->cldap->event_ctx) != 0) {
439 return NT_STATUS_UNEXPECTED_NETWORK_ERROR;
443 if (req->state == CLDAP_REQUEST_TIMEOUT) {
445 return NT_STATUS_IO_TIMEOUT;
448 ZERO_STRUCT(ldap_msg);
449 ldap_msg.mem_ctx = mem_ctx;
451 if (!ldap_decode(&req->asn1, &ldap_msg)) {
453 return NT_STATUS_INVALID_PARAMETER;
456 ZERO_STRUCT(io->out);
458 /* the first possible form has a search result in first place */
459 if (ldap_msg.type == LDAP_TAG_SearchResultEntry) {
460 io->out.response = talloc(mem_ctx, struct ldap_SearchResEntry);
461 NT_STATUS_HAVE_NO_MEMORY(io->out.response);
462 *io->out.response = ldap_msg.r.SearchResultEntry;
464 /* decode the 2nd part */
465 if (!ldap_decode(&req->asn1, &ldap_msg)) {
467 return NT_STATUS_INVALID_PARAMETER;
471 if (ldap_msg.type != LDAP_TAG_SearchResultDone) {
473 return NT_STATUS_UNEXPECTED_NETWORK_ERROR;
476 io->out.result = talloc(mem_ctx, struct ldap_Result);
477 NT_STATUS_HAVE_NO_MEMORY(io->out.result);
478 *io->out.result = ldap_msg.r.SearchResultDone;
486 synchronous cldap search
488 NTSTATUS cldap_search(struct cldap_socket *cldap,
490 struct cldap_search *io)
492 struct cldap_request *req = cldap_search_send(cldap, io);
493 return cldap_search_recv(req, mem_ctx, io);
499 queue a cldap netlogon for send
501 struct cldap_request *cldap_netlogon_send(struct cldap_socket *cldap,
502 struct cldap_netlogon *io)
504 struct cldap_search search;
506 struct cldap_request *req;
507 const char *attr[] = { "NetLogon", NULL };
508 TALLOC_CTX *tmp_ctx = talloc_new(cldap);
510 filter = talloc_asprintf(tmp_ctx, "(&(NtVer=%s)",
511 ldap_encode_ndr_uint32(tmp_ctx, io->in.version));
512 if (filter == NULL) goto failed;
514 filter = talloc_asprintf_append(filter, "(User=%s)", io->in.user);
515 if (filter == NULL) goto failed;
518 filter = talloc_asprintf_append(filter, "(Host=%s)", io->in.host);
519 if (filter == NULL) goto failed;
522 filter = talloc_asprintf_append(filter, "(DnsDomain=%s)", io->in.realm);
523 if (filter == NULL) goto failed;
525 if (io->in.acct_control != -1) {
526 filter = talloc_asprintf_append(filter, "(AAC=%s)",
527 ldap_encode_ndr_uint32(tmp_ctx, io->in.acct_control));
528 if (filter == NULL) goto failed;
530 if (io->in.domain_sid) {
531 struct dom_sid *sid = dom_sid_parse_talloc(tmp_ctx, io->in.domain_sid);
532 if (sid == NULL) goto failed;
533 filter = talloc_asprintf_append(filter, "(domainSid=%s)",
534 ldap_encode_ndr_dom_sid(tmp_ctx, sid));
535 if (filter == NULL) goto failed;
537 if (io->in.domain_guid) {
540 status = GUID_from_string(io->in.domain_guid, &guid);
541 if (!NT_STATUS_IS_OK(status)) goto failed;
542 filter = talloc_asprintf_append(filter, "(DomainGuid=%s)",
543 ldap_encode_ndr_GUID(tmp_ctx, &guid));
544 if (filter == NULL) goto failed;
546 filter = talloc_asprintf_append(filter, ")");
547 if (filter == NULL) goto failed;
549 search.in.dest_address = io->in.dest_address;
550 search.in.filter = filter;
551 search.in.attributes = attr;
552 search.in.timeout = 2;
553 search.in.retries = 2;
555 req = cldap_search_send(cldap, &search);
557 talloc_free(tmp_ctx);
560 talloc_free(tmp_ctx);
566 receive a cldap netlogon reply
568 NTSTATUS cldap_netlogon_recv(struct cldap_request *req,
570 struct cldap_netlogon *io)
573 struct cldap_search search;
576 status = cldap_search_recv(req, mem_ctx, &search);
577 if (!NT_STATUS_IS_OK(status)) {
580 if (search.out.response == NULL) {
581 return NT_STATUS_NOT_FOUND;
584 if (search.out.response->num_attributes != 1 ||
585 strcasecmp(search.out.response->attributes[0].name, "netlogon") != 0 ||
586 search.out.response->attributes[0].num_values != 1 ||
587 search.out.response->attributes[0].values->length < 2) {
588 return NT_STATUS_UNEXPECTED_NETWORK_ERROR;
590 data = search.out.response->attributes[0].values;
592 status = ndr_pull_union_blob(data, mem_ctx, &io->out.netlogon,
593 io->in.version & 0xF,
594 (ndr_pull_flags_fn_t)ndr_pull_nbt_cldap_netlogon);
595 if (!NT_STATUS_IS_OK(status)) {
596 DEBUG(2,("cldap failed to parse netlogon response of type 0x%02x\n",
597 SVAL(data->data, 0)));
598 dump_data(10, data->data, data->length);
605 sync cldap netlogon search
607 NTSTATUS cldap_netlogon(struct cldap_socket *cldap,
608 TALLOC_CTX *mem_ctx, struct cldap_netlogon *io)
610 struct cldap_request *req = cldap_netlogon_send(cldap, io);
611 return cldap_netlogon_recv(req, mem_ctx, io);
616 send an empty reply (used on any error, so the client doesn't keep waiting
617 or send the bad request again)
619 NTSTATUS cldap_empty_reply(struct cldap_socket *cldap,
621 const char *src_address, int src_port)
624 struct cldap_reply reply;
625 struct ldap_Result result;
627 reply.messageid = message_id;
628 reply.dest_address = src_address;
629 reply.dest_port = src_port;
630 reply.response = NULL;
631 reply.result = &result;
635 status = cldap_reply_send(cldap, &reply);
642 send a netlogon reply
644 NTSTATUS cldap_netlogon_reply(struct cldap_socket *cldap,
646 const char *src_address, int src_port,
648 union nbt_cldap_netlogon *netlogon)
651 struct cldap_reply reply;
652 struct ldap_SearchResEntry response;
653 struct ldap_Result result;
654 TALLOC_CTX *tmp_ctx = talloc_new(cldap);
657 status = ndr_push_union_blob(&blob, tmp_ctx, netlogon, version & 0xF,
658 (ndr_push_flags_fn_t)ndr_push_nbt_cldap_netlogon);
659 if (!NT_STATUS_IS_OK(status)) {
660 talloc_free(tmp_ctx);
664 reply.messageid = message_id;
665 reply.dest_address = src_address;
666 reply.dest_port = src_port;
667 reply.response = &response;
668 reply.result = &result;
673 response.num_attributes = 1;
674 response.attributes = talloc(tmp_ctx, struct ldap_attribute);
675 NT_STATUS_HAVE_NO_MEMORY(response.attributes);
676 response.attributes->name = "netlogon";
677 response.attributes->num_values = 1;
678 response.attributes->values = &blob;
680 status = cldap_reply_send(cldap, &reply);
682 talloc_free(tmp_ctx);