2 Unix SMB/CIFS implementation.
4 Copyright (C) Volker Lendecke 2004
5 Copyright (C) Stefan Metzmacher 2004
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 close the socket and shutdown a server_context
27 static void ldapsrv_terminate_connection(struct ldapsrv_connection *ldap_conn, const char *reason)
29 server_terminate_connection(ldap_conn->connection, reason);
33 add a socket address to the list of events, one event per port
35 static void add_socket(struct server_service *service,
36 const struct model_ops *model_ops,
39 struct server_socket *srv_sock;
41 char *ip_str = talloc_strdup(service, inet_ntoa(*ifip));
43 srv_sock = service_setup_socket(service, model_ops, ip_str, &port);
48 /****************************************************************************
49 Open the socket communication.
50 ****************************************************************************/
51 static void ldapsrv_init(struct server_service *service,
52 const struct model_ops *model_ops)
54 struct ldapsrv_service *ldap_service;
55 struct ldapsrv_partition *part;
57 DEBUG(10,("ldapsrv_init\n"));
59 ldap_service = talloc_p(service, struct ldapsrv_service);
61 DEBUG(0,("talloc_p(service, struct ldapsrv_service) failed\n"));
64 ZERO_STRUCTP(ldap_service);
66 part = talloc_p(ldap_service, struct ldapsrv_partition);
68 DEBUG(0,("talloc_p(ldap_service, struct ldapsrv_partition) failed\n"));
71 part->base_dn = ""; /* RootDSE */
72 part->ops = ldapsrv_get_rootdse_partition_ops();
74 ldap_service->rootDSE = part;
75 DLIST_ADD_END(ldap_service->partitions, part, struct ldapsrv_partition *);
77 part = talloc_p(ldap_service, struct ldapsrv_partition);
79 DEBUG(0,("talloc_p(ldap_service, struct ldapsrv_partition) failed\n"));
82 part->base_dn = "*"; /* default partition */
83 part->ops = ldapsrv_get_sldb_partition_ops();
85 ldap_service->default_partition = part;
86 DLIST_ADD_END(ldap_service->partitions, part, struct ldapsrv_partition *);
88 service->private_data = ldap_service;
90 if (lp_interfaces() && lp_bind_interfaces_only()) {
91 int num_interfaces = iface_count();
94 /* We have been given an interfaces line, and been
95 told to only bind to those interfaces. Create a
96 socket per interface and bind to only these.
98 for(i = 0; i < num_interfaces; i++) {
99 struct in_addr *ifip = iface_n_ip(i);
102 DEBUG(0,("ldapsrv_init: interface %d has NULL "
103 "IP address !\n", i));
107 add_socket(service, model_ops, ifip);
110 struct in_addr *ifip;
112 /* Just bind to lp_socket_address() (usually 0.0.0.0) */
113 ifip = interpret_addr2(service, lp_socket_address());
114 add_socket(service, model_ops, ifip);
116 talloc_destroy(ifip);
120 /* This rw-buf api is made to avoid memcpy. For now do that like mad... The
121 idea is to write into a circular list of buffers where the ideal case is
122 that a read(2) holds a complete request that is then thrown away
125 static void consumed_from_buf(struct rw_buffer *buf,
128 memcpy(buf->data, buf->data+length, buf->length-length);
129 buf->length -= length;
132 static void peek_into_read_buf(struct rw_buffer *buf, uint8_t **out,
136 *out_length = buf->length;
139 static BOOL append_to_buf(struct rw_buffer *buf, uint8_t *data, size_t length)
141 buf->data = realloc(buf->data, buf->length+length);
143 if (buf->data == NULL)
146 memcpy(buf->data+buf->length, data, length);
148 buf->length += length;
152 static BOOL read_into_buf(struct socket_context *sock, struct rw_buffer *buf)
158 status = socket_recv(sock, sock, &tmp_blob, 1024, 0);
159 if (!NT_STATUS_IS_OK(status)) {
160 DEBUG(10,("socket_recv: %s\n",nt_errstr(status)));
164 ret = append_to_buf(buf, tmp_blob.data, tmp_blob.length);
166 talloc_free(tmp_blob.data);
171 static BOOL ldapsrv_read_buf(struct ldapsrv_connection *conn)
178 int buf_length, sasl_length;
179 struct socket_context *sock = conn->connection->socket;
183 !(gensec_have_feature(conn->gensec, GENSEC_WANT_SIGN) &&
184 gensec_have_feature(conn->gensec, GENSEC_WANT_SEAL))) {
185 return read_into_buf(sock, &conn->in_buffer);
188 mem_ctx = talloc(conn, 0);
190 DEBUG(0,("no memory\n"));
194 status = socket_recv(sock, mem_ctx, &tmp_blob, 1024, 0);
195 if (!NT_STATUS_IS_OK(status)) {
196 DEBUG(10,("socket_recv: %s\n",nt_errstr(status)));
197 talloc_free(mem_ctx);
201 ret = append_to_buf(&conn->sasl_in_buffer, tmp_blob.data, tmp_blob.length);
203 talloc_free(mem_ctx);
207 peek_into_read_buf(&conn->sasl_in_buffer, &buf, &buf_length);
209 if (buf_length < 4) {
211 talloc_free(mem_ctx);
215 sasl_length = RIVAL(buf, 0);
217 if (buf_length < (4 + sasl_length)) {
219 talloc_free(mem_ctx);
223 creds.data = buf + 4;
224 creds.length = gensec_sig_size(conn->gensec);
226 if (creds.length > sasl_length) {
227 /* invalid packet? */
228 talloc_free(mem_ctx);
232 tmp_blob.data = buf + (4 + creds.length);
233 tmp_blob.length = (4 + sasl_length) - (4 + creds.length);
235 if (gensec_have_feature(conn->gensec, GENSEC_WANT_SEAL)) {
236 status = gensec_unseal_packet(conn->gensec, mem_ctx,
237 tmp_blob.data, tmp_blob.length,
238 tmp_blob.data, tmp_blob.length,
240 if (!NT_STATUS_IS_OK(status)) {
241 DEBUG(0,("gensec_unseal_packet: %s\n",nt_errstr(status)));
242 talloc_free(mem_ctx);
246 status = gensec_check_packet(conn->gensec, mem_ctx,
247 tmp_blob.data, tmp_blob.length,
248 tmp_blob.data, tmp_blob.length,
250 if (!NT_STATUS_IS_OK(status)) {
251 DEBUG(0,("gensec_check_packet: %s\n",nt_errstr(status)));
252 talloc_free(mem_ctx);
257 ret = append_to_buf(&conn->in_buffer, tmp_blob.data, tmp_blob.length);
259 talloc_free(mem_ctx);
263 consumed_from_buf(&conn->sasl_in_buffer, 4 + sasl_length);
265 talloc_free(mem_ctx);
269 static BOOL write_from_buf(struct socket_context *sock, struct rw_buffer *buf)
275 tmp_blob.data = buf->data;
276 tmp_blob.length = buf->length;
278 status = socket_send(sock, sock, &tmp_blob, &sendlen, 0);
279 if (!NT_STATUS_IS_OK(status)) {
280 DEBUG(10,("socket_send() %s\n",nt_errstr(status)));
284 consumed_from_buf(buf, sendlen);
289 static BOOL ldapsrv_write_buf(struct ldapsrv_connection *conn)
297 struct socket_context *sock = conn->connection->socket;
301 !(gensec_have_feature(conn->gensec, GENSEC_WANT_SIGN) &&
302 gensec_have_feature(conn->gensec, GENSEC_WANT_SEAL))) {
303 return write_from_buf(sock, &conn->out_buffer);
306 mem_ctx = talloc(conn, 0);
308 DEBUG(0,("no memory\n"));
312 tmp_blob.data = conn->out_buffer.data;
313 tmp_blob.length = conn->out_buffer.length;
315 if (gensec_have_feature(conn->gensec, GENSEC_WANT_SEAL)) {
316 status = gensec_seal_packet(conn->gensec, mem_ctx,
317 tmp_blob.data, tmp_blob.length,
318 tmp_blob.data, tmp_blob.length,
320 if (!NT_STATUS_IS_OK(status)) {
321 DEBUG(0,("gensec_seal_packet: %s\n",nt_errstr(status)));
322 talloc_free(mem_ctx);
326 status = gensec_sign_packet(conn->gensec, mem_ctx,
327 tmp_blob.data, tmp_blob.length,
328 tmp_blob.data, tmp_blob.length,
330 if (!NT_STATUS_IS_OK(status)) {
331 DEBUG(0,("gensec_sign_packet: %s\n",nt_errstr(status)));
332 talloc_free(mem_ctx);
337 sasl = data_blob_talloc(mem_ctx, NULL, 4 + creds.length + tmp_blob.length);
339 DEBUG(0,("no memory\n"));
340 talloc_free(mem_ctx);
344 RSIVAL(sasl.data, 0, creds.length + tmp_blob.length);
345 memcpy(sasl.data + 4, creds.data, creds.length);
346 memcpy(sasl.data + 4 + creds.length, tmp_blob.data, tmp_blob.length);
348 ret = append_to_buf(&conn->sasl_out_buffer, sasl.data, sasl.length);
350 talloc_free(mem_ctx);
353 consumed_from_buf(&conn->out_buffer, tmp_blob.length);
355 status = socket_send(sock, mem_ctx, &tmp_blob, &sendlen, 0);
356 if (!NT_STATUS_IS_OK(status)) {
357 DEBUG(10,("socket_send() %s\n",nt_errstr(status)));
358 talloc_free(mem_ctx);
362 consumed_from_buf(&conn->sasl_out_buffer, sendlen);
364 talloc_free(mem_ctx);
369 static BOOL ldap_append_to_buf(struct ldap_message *msg, struct rw_buffer *buf)
374 if (!ldap_encode(msg, &blob))
377 res = append_to_buf(buf, blob.data, blob.length);
379 data_blob_free(&blob);
383 struct ldapsrv_reply *ldapsrv_init_reply(struct ldapsrv_call *call, enum ldap_request_tag type)
385 struct ldapsrv_reply *reply;
387 reply = talloc_p(call, struct ldapsrv_reply);
392 reply->prev = reply->next = NULL;
393 reply->state = LDAPSRV_REPLY_STATE_NEW;
394 reply->msg.messageid = call->request.messageid;
395 reply->msg.type = type;
396 reply->msg.mem_ctx = reply;
401 NTSTATUS ldapsrv_queue_reply(struct ldapsrv_call *call, struct ldapsrv_reply *reply)
403 DLIST_ADD_END(call->replies, reply, struct ldapsrv_reply *);
407 struct ldapsrv_partition *ldapsrv_get_partition(struct ldapsrv_connection *conn, const char *dn)
409 if (strcasecmp("", dn) == 0) {
410 return conn->service->rootDSE;
413 return conn->service->default_partition;
416 NTSTATUS ldapsrv_unwilling(struct ldapsrv_call *call, int error)
418 struct ldapsrv_reply *reply;
419 struct ldap_ExtendedResponse *r;
421 DEBUG(10,("Unwilling type[%d] id[%d]\n", call->request.type, call->request.messageid));
423 reply = ldapsrv_init_reply(call, LDAP_TAG_ExtendedResponse);
425 return NT_STATUS_NO_MEMORY;
428 r = &reply->msg.r.ExtendedResponse;
429 r->response.resultcode = error;
430 r->response.dn = NULL;
431 r->response.errormessage = NULL;
432 r->response.referral = NULL;
434 r->value.data = NULL;
437 return ldapsrv_queue_reply(call, reply);
440 static NTSTATUS ldapsrv_BindRequest(struct ldapsrv_call *call)
442 struct ldap_BindRequest *req = &call->request.r.BindRequest;
443 struct ldapsrv_reply *reply;
444 struct ldap_BindResponse *resp;
446 DEBUG(10, ("BindRequest dn: %s\n",req->dn));
448 reply = ldapsrv_init_reply(call, LDAP_TAG_BindResponse);
450 return NT_STATUS_NO_MEMORY;
453 resp = &reply->msg.r.BindResponse;
454 resp->response.resultcode = 0;
455 resp->response.dn = NULL;
456 resp->response.errormessage = NULL;
457 resp->response.referral = NULL;
458 resp->SASL.secblob = data_blob(NULL, 0);
460 return ldapsrv_queue_reply(call, reply);
463 static NTSTATUS ldapsrv_UnbindRequest(struct ldapsrv_call *call)
465 /* struct ldap_UnbindRequest *req = &call->request->r.UnbindRequest;*/
466 DEBUG(10, ("UnbindRequest\n"));
470 static NTSTATUS ldapsrv_SearchRequest(struct ldapsrv_call *call)
472 struct ldap_SearchRequest *req = &call->request.r.SearchRequest;
473 struct ldapsrv_partition *part;
475 DEBUG(10, ("SearchRequest"));
476 DEBUGADD(10, (" basedn: %s", req->basedn));
477 DEBUGADD(10, (" filter: %s\n", req->filter));
479 part = ldapsrv_get_partition(call->conn, req->basedn);
481 if (!part->ops->Search) {
482 struct ldap_Result *done;
483 struct ldapsrv_reply *done_r;
485 done_r = ldapsrv_init_reply(call, LDAP_TAG_SearchResultDone);
487 return NT_STATUS_NO_MEMORY;
490 done = &done_r->msg.r.SearchResultDone;
491 done->resultcode = 53;
493 done->errormessage = NULL;
494 done->referral = NULL;
496 return ldapsrv_queue_reply(call, done_r);
499 return part->ops->Search(part, call, req);
502 static NTSTATUS ldapsrv_ModifyRequest(struct ldapsrv_call *call)
504 struct ldap_ModifyRequest *req = &call->request.r.ModifyRequest;
505 struct ldapsrv_partition *part;
507 DEBUG(10, ("ModifyRequest"));
508 DEBUGADD(10, (" dn: %s", req->dn));
510 part = ldapsrv_get_partition(call->conn, req->dn);
512 if (!part->ops->Modify) {
513 return ldapsrv_unwilling(call, 53);
516 return part->ops->Modify(part, call, req);
519 static NTSTATUS ldapsrv_AddRequest(struct ldapsrv_call *call)
521 struct ldap_AddRequest *req = &call->request.r.AddRequest;
522 struct ldapsrv_partition *part;
524 DEBUG(10, ("AddRequest"));
525 DEBUGADD(10, (" dn: %s", req->dn));
527 part = ldapsrv_get_partition(call->conn, req->dn);
529 if (!part->ops->Add) {
530 return ldapsrv_unwilling(call, 53);
533 return part->ops->Add(part, call, req);
536 static NTSTATUS ldapsrv_DelRequest(struct ldapsrv_call *call)
538 struct ldap_DelRequest *req = &call->request.r.DelRequest;
539 struct ldapsrv_partition *part;
541 DEBUG(10, ("DelRequest"));
542 DEBUGADD(10, (" dn: %s", req->dn));
544 part = ldapsrv_get_partition(call->conn, req->dn);
546 if (!part->ops->Del) {
547 return ldapsrv_unwilling(call, 53);
550 return part->ops->Del(part, call, req);
553 static NTSTATUS ldapsrv_ModifyDNRequest(struct ldapsrv_call *call)
555 struct ldap_ModifyDNRequest *req = &call->request.r.ModifyDNRequest;
556 struct ldapsrv_partition *part;
558 DEBUG(10, ("ModifyDNRequrest"));
559 DEBUGADD(10, (" dn: %s", req->dn));
560 DEBUGADD(10, (" newrdn: %s", req->newrdn));
562 part = ldapsrv_get_partition(call->conn, req->dn);
564 if (!part->ops->ModifyDN) {
565 return ldapsrv_unwilling(call, 53);
568 return part->ops->ModifyDN(part, call, req);
571 static NTSTATUS ldapsrv_CompareRequest(struct ldapsrv_call *call)
573 struct ldap_CompareRequest *req = &call->request.r.CompareRequest;
574 struct ldapsrv_partition *part;
576 DEBUG(10, ("CompareRequest"));
577 DEBUGADD(10, (" dn: %s", req->dn));
579 part = ldapsrv_get_partition(call->conn, req->dn);
581 if (!part->ops->Compare) {
582 return ldapsrv_unwilling(call, 53);
585 return part->ops->Compare(part, call, req);
588 static NTSTATUS ldapsrv_AbandonRequest(struct ldapsrv_call *call)
590 /* struct ldap_AbandonRequest *req = &call->request.r.AbandonRequest;*/
591 DEBUG(10, ("AbandonRequest\n"));
595 static NTSTATUS ldapsrv_ExtendedRequest(struct ldapsrv_call *call)
597 /* struct ldap_ExtendedRequest *req = &call->request.r.ExtendedRequest;*/
598 struct ldapsrv_reply *reply;
600 DEBUG(10, ("Extended\n"));
602 reply = ldapsrv_init_reply(call, LDAP_TAG_ExtendedResponse);
604 return NT_STATUS_NO_MEMORY;
607 ZERO_STRUCT(reply->msg.r);
609 return ldapsrv_queue_reply(call, reply);
612 static NTSTATUS ldapsrv_do_call(struct ldapsrv_call *call)
614 switch(call->request.type) {
615 case LDAP_TAG_BindRequest:
616 return ldapsrv_BindRequest(call);
617 case LDAP_TAG_UnbindRequest:
618 return ldapsrv_UnbindRequest(call);
619 case LDAP_TAG_SearchRequest:
620 return ldapsrv_SearchRequest(call);
621 case LDAP_TAG_ModifyRequest:
622 return ldapsrv_ModifyRequest(call);
623 case LDAP_TAG_AddRequest:
624 return ldapsrv_AddRequest(call);
625 case LDAP_TAG_DelRequest:
626 return ldapsrv_DelRequest(call);
627 case LDAP_TAG_ModifyDNRequest:
628 return ldapsrv_ModifyDNRequest(call);
629 case LDAP_TAG_CompareRequest:
630 return ldapsrv_CompareRequest(call);
631 case LDAP_TAG_AbandonRequest:
632 return ldapsrv_AbandonRequest(call);
633 case LDAP_TAG_ExtendedRequest:
634 return ldapsrv_ExtendedRequest(call);
636 return ldapsrv_unwilling(call, 2);
640 static NTSTATUS ldapsrv_do_responses(struct ldapsrv_connection *conn)
642 struct ldapsrv_call *call, *next_call = NULL;
643 struct ldapsrv_reply *reply, *next_reply = NULL;
645 for (call=conn->calls; call; call=next_call) {
646 for (reply=call->replies; reply; reply=next_reply) {
647 if (!ldap_append_to_buf(&reply->msg, &conn->out_buffer)) {
648 return NT_STATUS_FOOBAR;
650 next_reply = reply->next;
651 DLIST_REMOVE(call->replies, reply);
652 reply->state = LDAPSRV_REPLY_STATE_SEND;
655 next_call = call->next;
656 DLIST_REMOVE(conn->calls, call);
657 call->state = LDAPSRV_CALL_STATE_COMPLETE;
665 called when a LDAP socket becomes readable
667 static void ldapsrv_recv(struct server_connection *conn, time_t t,
670 struct ldapsrv_connection *ldap_conn = conn->private_data;
672 int buf_length, msg_length;
675 struct ldapsrv_call *call;
678 DEBUG(10,("ldapsrv_recv\n"));
680 if (!ldapsrv_read_buf(ldap_conn)) {
681 ldapsrv_terminate_connection(ldap_conn, "ldapsrv_read_buf() failed");
685 peek_into_read_buf(&ldap_conn->in_buffer, &buf, &buf_length);
687 while (buf_length > 0) {
688 /* LDAP Messages are always SEQUENCES */
690 if (!asn1_object_length(buf, buf_length, ASN1_SEQUENCE(0),
692 ldapsrv_terminate_connection(ldap_conn, "asn1_object_length() failed");
696 if (buf_length < msg_length) {
701 /* We've got a complete LDAP request in the in-buffer, convert
702 * that to a ldap_message and put it into the incoming
706 blob.length = msg_length;
708 if (!asn1_load(&data, blob)) {
709 ldapsrv_terminate_connection(ldap_conn, "asn1_load() failed");
713 call = talloc_p(ldap_conn, struct ldapsrv_call);
715 ldapsrv_terminate_connection(ldap_conn, "no memory");
720 call->state = LDAPSRV_CALL_STATE_NEW;
721 call->conn = ldap_conn;
722 /* TODO: we should use talloc_reference() here */
723 call->session_info = ldap_conn->session_info;
724 call->request.mem_ctx = call;
726 if (!ldap_decode(&data, &call->request)) {
727 dump_data(0,buf, msg_length);
729 ldapsrv_terminate_connection(ldap_conn, "ldap_decode() failed");
735 DLIST_ADD_END(ldap_conn->calls, call,
736 struct ldapsrv_call *);
738 consumed_from_buf(&ldap_conn->in_buffer, msg_length);
740 status = ldapsrv_do_call(call);
741 if (!NT_STATUS_IS_OK(status)) {
742 ldapsrv_terminate_connection(ldap_conn, "ldapsrv_do_call() failed");
746 peek_into_read_buf(&ldap_conn->in_buffer, &buf, &buf_length);
749 status = ldapsrv_do_responses(ldap_conn);
750 if (!NT_STATUS_IS_OK(status)) {
751 ldapsrv_terminate_connection(ldap_conn, "ldapsrv_do_responses() failed");
755 if ((ldap_conn->out_buffer.length > 0)||(ldap_conn->sasl_out_buffer.length > 0)) {
756 conn->event.fde->flags |= EVENT_FD_WRITE;
763 called when a LDAP socket becomes writable
765 static void ldapsrv_send(struct server_connection *conn, time_t t,
768 struct ldapsrv_connection *ldap_conn = conn->private_data;
770 DEBUG(10,("ldapsrv_send\n"));
772 if (!ldapsrv_write_buf(ldap_conn)) {
773 ldapsrv_terminate_connection(ldap_conn, "ldapsrv_write_buf() failed");
777 if (ldap_conn->out_buffer.length == 0 && ldap_conn->sasl_out_buffer.length == 0) {
778 conn->event.fde->flags &= ~EVENT_FD_WRITE;
785 called when connection is idle
787 static void ldapsrv_idle(struct server_connection *conn, time_t t)
789 DEBUG(10,("ldapsrv_idle: not implemented!\n"));
793 static void ldapsrv_close(struct server_connection *conn, const char *reason)
799 initialise a server_context from a open socket and register a event handler
800 for reading from that socket
802 static void ldapsrv_accept(struct server_connection *conn)
804 struct ldapsrv_connection *ldap_conn;
806 DEBUG(10, ("ldapsrv_accept\n"));
808 ldap_conn = talloc_p(conn, struct ldapsrv_connection);
810 if (ldap_conn == NULL)
813 ZERO_STRUCTP(ldap_conn);
814 ldap_conn->connection = conn;
815 ldap_conn->service = talloc_reference(ldap_conn, conn->service->private_data);
817 conn->private_data = ldap_conn;
823 called on a fatal error that should cause this server to terminate
825 static void ldapsrv_exit(struct server_service *service, const char *reason)
827 DEBUG(10,("ldapsrv_exit\n"));
831 static const struct server_service_ops ldap_server_ops = {
833 .service_init = ldapsrv_init,
834 .accept_connection = ldapsrv_accept,
835 .recv_handler = ldapsrv_recv,
836 .send_handler = ldapsrv_send,
837 .idle_handler = ldapsrv_idle,
838 .close_connection = ldapsrv_close,
839 .service_exit = ldapsrv_exit,
842 const struct server_service_ops *ldapsrv_get_ops(void)
844 return &ldap_server_ops;
847 NTSTATUS server_service_ldap_init(void)