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 3 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, see <http://www.gnu.org/licenses/>.
23 see RFC1798 for details of CLDAP
26 - carried over UDP on port 389
27 - request and response matched by message ID
28 - request consists of only a single searchRequest element
29 - response can be in one of two forms
30 - a single searchResponse, followed by a searchResult
31 - a single searchResult
35 #include "lib/events/events.h"
36 #include "lib/util/dlinklist.h"
37 #include "libcli/ldap/ldap.h"
38 #include "libcli/cldap/cldap.h"
39 #include "lib/socket/socket.h"
40 #include "libcli/security/security.h"
41 #include "librpc/gen_ndr/ndr_nbt.h"
44 destroy a pending request
46 static int cldap_request_destructor(struct cldap_request *req)
48 if (req->state == CLDAP_REQUEST_SEND) {
49 DLIST_REMOVE(req->cldap->send_queue, req);
51 if (!req->is_reply && req->message_id != 0) {
52 idr_remove(req->cldap->idr, req->message_id);
59 handle recv events on a cldap socket
61 static void cldap_socket_recv(struct cldap_socket *cldap)
63 TALLOC_CTX *tmp_ctx = talloc_new(cldap);
65 struct socket_address *src;
68 struct asn1_data *asn1 = asn1_init(tmp_ctx);
69 struct ldap_message *ldap_msg;
70 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,
88 if (!NT_STATUS_IS_OK(status)) {
94 DEBUG(2,("Received cldap packet of length %d from %s:%d\n",
95 (int)blob.length, src->addr, src->port));
97 if (!asn1_load(asn1, blob)) {
98 DEBUG(2,("Failed to setup for asn.1 decode\n"));
103 ldap_msg = talloc(tmp_ctx, struct ldap_message);
104 if (ldap_msg == NULL) {
105 talloc_free(tmp_ctx);
109 /* this initial decode is used to find the message id */
110 status = ldap_decode(asn1, ldap_msg);
111 if (!NT_STATUS_IS_OK(status)) {
112 DEBUG(2,("Failed to decode ldap message: %s\n", nt_errstr(status)));
113 talloc_free(tmp_ctx);
117 /* find the pending request */
118 req = idr_find(cldap->idr, ldap_msg->messageid);
120 if (cldap->incoming.handler) {
121 cldap->incoming.handler(cldap, ldap_msg, src);
123 DEBUG(2,("Mismatched cldap reply %u from %s:%d\n",
124 ldap_msg->messageid, src->addr, src->port));
126 talloc_free(tmp_ctx);
130 req->asn1 = talloc_steal(req, asn1);
133 req->state = CLDAP_REQUEST_DONE;
134 talloc_free(req->te);
136 talloc_free(tmp_ctx);
144 handle request timeouts
146 static void cldap_request_timeout(struct event_context *event_ctx,
147 struct timed_event *te, struct timeval t,
150 struct cldap_request *req = talloc_get_type(private, struct cldap_request);
152 /* possibly try again */
153 if (req->num_retries != 0) {
154 size_t len = req->encoded.length;
158 socket_sendto(req->cldap->sock, &req->encoded, &len,
161 req->te = event_add_timed(req->cldap->event_ctx, req,
162 timeval_current_ofs(req->timeout, 0),
163 cldap_request_timeout, req);
167 req->state = CLDAP_REQUEST_ERROR;
168 req->status = NT_STATUS_IO_TIMEOUT;
175 handle send events on a cldap socket
177 static void cldap_socket_send(struct cldap_socket *cldap)
179 struct cldap_request *req;
182 while ((req = cldap->send_queue)) {
185 len = req->encoded.length;
186 status = socket_sendto(cldap->sock, &req->encoded, &len,
188 if (NT_STATUS_IS_ERR(status)) {
189 DEBUG(0,("Failed to send cldap request of length %u to %s:%d\n",
190 (unsigned)req->encoded.length, req->dest->addr, req->dest->port));
191 DLIST_REMOVE(cldap->send_queue, req);
192 req->state = CLDAP_REQUEST_ERROR;
193 req->status = status;
200 if (!NT_STATUS_IS_OK(status)) return;
202 DLIST_REMOVE(cldap->send_queue, req);
207 req->state = CLDAP_REQUEST_WAIT;
209 req->te = event_add_timed(cldap->event_ctx, req,
210 timeval_current_ofs(req->timeout, 0),
211 cldap_request_timeout, req);
213 EVENT_FD_READABLE(cldap->fde);
217 EVENT_FD_NOT_WRITEABLE(cldap->fde);
223 handle fd events on a cldap_socket
225 static void cldap_socket_handler(struct event_context *ev, struct fd_event *fde,
226 uint16_t flags, void *private)
228 struct cldap_socket *cldap = talloc_get_type(private, struct cldap_socket);
229 if (flags & EVENT_FD_WRITE) {
230 cldap_socket_send(cldap);
232 if (flags & EVENT_FD_READ) {
233 cldap_socket_recv(cldap);
238 initialise a cldap_socket. The event_ctx is optional, if provided
239 then operations will use that event context
241 struct cldap_socket *cldap_socket_init(TALLOC_CTX *mem_ctx,
242 struct event_context *event_ctx)
244 struct cldap_socket *cldap;
247 cldap = talloc(mem_ctx, struct cldap_socket);
248 if (cldap == NULL) goto failed;
250 if (event_ctx == NULL) {
251 cldap->event_ctx = event_context_init(cldap);
253 cldap->event_ctx = talloc_reference(cldap, event_ctx);
255 if (cldap->event_ctx == NULL) goto failed;
257 cldap->idr = idr_init(cldap);
258 if (cldap->idr == NULL) goto failed;
260 status = socket_create("ip", SOCKET_TYPE_DGRAM, &cldap->sock, 0);
261 if (!NT_STATUS_IS_OK(status)) goto failed;
263 talloc_steal(cldap, cldap->sock);
265 cldap->fde = event_add_fd(cldap->event_ctx, cldap,
266 socket_get_fd(cldap->sock), 0,
267 cldap_socket_handler, cldap);
269 cldap->send_queue = NULL;
270 cldap->incoming.handler = NULL;
281 setup a handler for incoming requests
283 NTSTATUS cldap_set_incoming_handler(struct cldap_socket *cldap,
284 void (*handler)(struct cldap_socket *, struct ldap_message *,
285 struct socket_address *),
288 cldap->incoming.handler = handler;
289 cldap->incoming.private = private;
290 EVENT_FD_READABLE(cldap->fde);
295 queue a cldap request for send
297 struct cldap_request *cldap_search_send(struct cldap_socket *cldap,
298 struct cldap_search *io)
300 struct ldap_message *msg;
301 struct cldap_request *req;
302 struct ldap_SearchRequest *search;
304 req = talloc_zero(cldap, struct cldap_request);
305 if (req == NULL) goto failed;
308 req->state = CLDAP_REQUEST_SEND;
309 req->timeout = io->in.timeout;
310 req->num_retries = io->in.retries;
311 req->is_reply = False;
312 req->asn1 = asn1_init(req);
317 req->dest = socket_address_from_strings(req, cldap->sock->backend_name,
318 io->in.dest_address, lp_cldap_port());
319 if (!req->dest) goto failed;
321 req->message_id = idr_get_new_random(cldap->idr, req, UINT16_MAX);
322 if (req->message_id == -1) goto failed;
324 talloc_set_destructor(req, cldap_request_destructor);
326 msg = talloc(req, struct ldap_message);
327 if (msg == NULL) goto failed;
328 msg->messageid = req->message_id;
329 msg->type = LDAP_TAG_SearchRequest;
330 msg->controls = NULL;
331 search = &msg->r.SearchRequest;
334 search->scope = LDAP_SEARCH_SCOPE_BASE;
335 search->deref = LDAP_DEREFERENCE_NEVER;
336 search->timelimit = 0;
337 search->sizelimit = 0;
338 search->attributesonly = False;
339 search->num_attributes = str_list_length(io->in.attributes);
340 search->attributes = io->in.attributes;
341 search->tree = ldb_parse_tree(req, io->in.filter);
342 if (search->tree == NULL) {
346 if (!ldap_encode(msg, &req->encoded, req)) {
347 DEBUG(0,("Failed to encode cldap message to %s:%d\n",
348 req->dest->addr, req->dest->port));
352 DLIST_ADD_END(cldap->send_queue, req, struct cldap_request *);
354 EVENT_FD_WRITEABLE(cldap->fde);
365 queue a cldap reply for send
367 NTSTATUS cldap_reply_send(struct cldap_socket *cldap, struct cldap_reply *io)
369 struct ldap_message *msg;
370 struct cldap_request *req;
371 DATA_BLOB blob1, blob2;
372 NTSTATUS status = NT_STATUS_NO_MEMORY;
374 req = talloc_zero(cldap, struct cldap_request);
375 if (req == NULL) goto failed;
378 req->state = CLDAP_REQUEST_SEND;
379 req->is_reply = True;
380 req->asn1 = asn1_init(req);
385 req->dest = io->dest;
386 if (talloc_reference(req, io->dest) == NULL) goto failed;
388 talloc_set_destructor(req, cldap_request_destructor);
390 msg = talloc(req, struct ldap_message);
391 if (msg == NULL) goto failed;
392 msg->messageid = io->messageid;
393 msg->controls = NULL;
396 msg->type = LDAP_TAG_SearchResultEntry;
397 msg->r.SearchResultEntry = *io->response;
399 if (!ldap_encode(msg, &blob1, req)) {
400 DEBUG(0,("Failed to encode cldap message to %s:%d\n",
401 req->dest->addr, req->dest->port));
402 status = NT_STATUS_INVALID_PARAMETER;
406 blob1 = data_blob(NULL, 0);
409 msg->type = LDAP_TAG_SearchResultDone;
410 msg->r.SearchResultDone = *io->result;
412 if (!ldap_encode(msg, &blob2, req)) {
413 DEBUG(0,("Failed to encode cldap message to %s:%d\n",
414 req->dest->addr, req->dest->port));
415 status = NT_STATUS_INVALID_PARAMETER;
419 req->encoded = data_blob_talloc(req, NULL, blob1.length + blob2.length);
420 if (req->encoded.data == NULL) goto failed;
422 memcpy(req->encoded.data, blob1.data, blob1.length);
423 memcpy(req->encoded.data+blob1.length, blob2.data, blob2.length);
425 DLIST_ADD_END(cldap->send_queue, req, struct cldap_request *);
427 EVENT_FD_WRITEABLE(cldap->fde);
437 receive a cldap reply
439 NTSTATUS cldap_search_recv(struct cldap_request *req,
441 struct cldap_search *io)
443 struct ldap_message *ldap_msg;
447 return NT_STATUS_NO_MEMORY;
450 while (req->state < CLDAP_REQUEST_DONE) {
451 if (event_loop_once(req->cldap->event_ctx) != 0) {
453 return NT_STATUS_UNEXPECTED_NETWORK_ERROR;
457 if (req->state == CLDAP_REQUEST_ERROR) {
458 status = req->status;
463 ldap_msg = talloc(mem_ctx, struct ldap_message);
464 NT_STATUS_HAVE_NO_MEMORY(ldap_msg);
466 status = ldap_decode(req->asn1, ldap_msg);
467 if (!NT_STATUS_IS_OK(status)) {
468 DEBUG(2,("Failed to decode cldap search reply: %s\n", nt_errstr(status)));
473 ZERO_STRUCT(io->out);
475 /* the first possible form has a search result in first place */
476 if (ldap_msg->type == LDAP_TAG_SearchResultEntry) {
477 io->out.response = talloc(mem_ctx, struct ldap_SearchResEntry);
478 NT_STATUS_HAVE_NO_MEMORY(io->out.response);
479 *io->out.response = ldap_msg->r.SearchResultEntry;
481 /* decode the 2nd part */
482 status = ldap_decode(req->asn1, ldap_msg);
483 if (!NT_STATUS_IS_OK(status)) {
484 DEBUG(2,("Failed to decode cldap search result entry: %s\n", nt_errstr(status)));
490 if (ldap_msg->type != LDAP_TAG_SearchResultDone) {
492 return NT_STATUS_LDAP(LDAP_PROTOCOL_ERROR);
495 io->out.result = talloc(mem_ctx, struct ldap_Result);
496 NT_STATUS_HAVE_NO_MEMORY(io->out.result);
497 *io->out.result = ldap_msg->r.SearchResultDone;
501 if (io->out.result->resultcode != LDAP_SUCCESS) {
502 return NT_STATUS_LDAP(io->out.result->resultcode);
509 synchronous cldap search
511 NTSTATUS cldap_search(struct cldap_socket *cldap,
513 struct cldap_search *io)
515 struct cldap_request *req = cldap_search_send(cldap, io);
516 return cldap_search_recv(req, mem_ctx, io);
522 queue a cldap netlogon for send
524 struct cldap_request *cldap_netlogon_send(struct cldap_socket *cldap,
525 struct cldap_netlogon *io)
527 struct cldap_search search;
529 struct cldap_request *req;
530 const char *attr[] = { "NetLogon", NULL };
531 TALLOC_CTX *tmp_ctx = talloc_new(cldap);
533 filter = talloc_asprintf(tmp_ctx, "(&(NtVer=%s)",
534 ldap_encode_ndr_uint32(tmp_ctx, io->in.version));
535 if (filter == NULL) goto failed;
537 filter = talloc_asprintf_append(filter, "(User=%s)", io->in.user);
538 if (filter == NULL) goto failed;
541 filter = talloc_asprintf_append(filter, "(Host=%s)", io->in.host);
542 if (filter == NULL) goto failed;
545 filter = talloc_asprintf_append(filter, "(DnsDomain=%s)", io->in.realm);
546 if (filter == NULL) goto failed;
548 if (io->in.acct_control != -1) {
549 filter = talloc_asprintf_append(filter, "(AAC=%s)",
550 ldap_encode_ndr_uint32(tmp_ctx, io->in.acct_control));
551 if (filter == NULL) goto failed;
553 if (io->in.domain_sid) {
554 struct dom_sid *sid = dom_sid_parse_talloc(tmp_ctx, io->in.domain_sid);
555 if (sid == NULL) goto failed;
556 filter = talloc_asprintf_append(filter, "(domainSid=%s)",
557 ldap_encode_ndr_dom_sid(tmp_ctx, sid));
558 if (filter == NULL) goto failed;
560 if (io->in.domain_guid) {
563 status = GUID_from_string(io->in.domain_guid, &guid);
564 if (!NT_STATUS_IS_OK(status)) goto failed;
565 filter = talloc_asprintf_append(filter, "(DomainGuid=%s)",
566 ldap_encode_ndr_GUID(tmp_ctx, &guid));
567 if (filter == NULL) goto failed;
569 filter = talloc_asprintf_append(filter, ")");
570 if (filter == NULL) goto failed;
572 search.in.dest_address = io->in.dest_address;
573 search.in.filter = filter;
574 search.in.attributes = attr;
575 search.in.timeout = 2;
576 search.in.retries = 2;
578 req = cldap_search_send(cldap, &search);
580 talloc_free(tmp_ctx);
583 talloc_free(tmp_ctx);
589 receive a cldap netlogon reply
591 NTSTATUS cldap_netlogon_recv(struct cldap_request *req,
593 struct cldap_netlogon *io)
596 struct cldap_search search;
599 status = cldap_search_recv(req, mem_ctx, &search);
600 if (!NT_STATUS_IS_OK(status)) {
603 if (search.out.response == NULL) {
604 return NT_STATUS_NOT_FOUND;
607 if (search.out.response->num_attributes != 1 ||
608 strcasecmp(search.out.response->attributes[0].name, "netlogon") != 0 ||
609 search.out.response->attributes[0].num_values != 1 ||
610 search.out.response->attributes[0].values->length < 2) {
611 return NT_STATUS_UNEXPECTED_NETWORK_ERROR;
613 data = search.out.response->attributes[0].values;
615 status = ndr_pull_union_blob(data, mem_ctx, &io->out.netlogon,
616 io->in.version & 0xF,
617 (ndr_pull_flags_fn_t)ndr_pull_nbt_cldap_netlogon);
618 if (!NT_STATUS_IS_OK(status)) {
619 DEBUG(2,("cldap failed to parse netlogon response of type 0x%02x\n",
620 SVAL(data->data, 0)));
621 dump_data(10, data->data, data->length);
628 sync cldap netlogon search
630 NTSTATUS cldap_netlogon(struct cldap_socket *cldap,
631 TALLOC_CTX *mem_ctx, struct cldap_netlogon *io)
633 struct cldap_request *req = cldap_netlogon_send(cldap, io);
634 return cldap_netlogon_recv(req, mem_ctx, io);
639 send an empty reply (used on any error, so the client doesn't keep waiting
640 or send the bad request again)
642 NTSTATUS cldap_empty_reply(struct cldap_socket *cldap,
644 struct socket_address *src)
647 struct cldap_reply reply;
648 struct ldap_Result result;
650 reply.messageid = message_id;
652 reply.response = NULL;
653 reply.result = &result;
657 status = cldap_reply_send(cldap, &reply);
663 send an error reply (used on any error, so the client doesn't keep waiting
664 or send the bad request again)
666 NTSTATUS cldap_error_reply(struct cldap_socket *cldap,
668 struct socket_address *src,
670 const char *errormessage)
673 struct cldap_reply reply;
674 struct ldap_Result result;
676 reply.messageid = message_id;
678 reply.response = NULL;
679 reply.result = &result;
682 result.resultcode = resultcode;
683 result.errormessage = errormessage;
685 status = cldap_reply_send(cldap, &reply);
692 send a netlogon reply
694 NTSTATUS cldap_netlogon_reply(struct cldap_socket *cldap,
696 struct socket_address *src,
698 union nbt_cldap_netlogon *netlogon)
701 struct cldap_reply reply;
702 struct ldap_SearchResEntry response;
703 struct ldap_Result result;
704 TALLOC_CTX *tmp_ctx = talloc_new(cldap);
707 status = ndr_push_union_blob(&blob, tmp_ctx, netlogon, version & 0xF,
708 (ndr_push_flags_fn_t)ndr_push_nbt_cldap_netlogon);
709 if (!NT_STATUS_IS_OK(status)) {
710 talloc_free(tmp_ctx);
714 reply.messageid = message_id;
716 reply.response = &response;
717 reply.result = &result;
722 response.num_attributes = 1;
723 response.attributes = talloc(tmp_ctx, struct ldb_message_element);
724 NT_STATUS_HAVE_NO_MEMORY(response.attributes);
725 response.attributes->name = "netlogon";
726 response.attributes->num_values = 1;
727 response.attributes->values = &blob;
729 status = cldap_reply_send(cldap, &reply);
731 talloc_free(tmp_ctx);