ldap-server: remove warning for the ret not being used
[Samba.git] / source4 / ldap_server / ldap_server.c
blob2221976ccae8075ce2c8512e9c7fa62c37df4b08
1 /*
2 Unix SMB/CIFS implementation.
4 LDAP server
6 Copyright (C) Andrew Tridgell 2005
7 Copyright (C) Volker Lendecke 2004
8 Copyright (C) Stefan Metzmacher 2004
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>.
24 #include "includes.h"
25 #include "system/network.h"
26 #include "lib/events/events.h"
27 #include "auth/auth.h"
28 #include "auth/credentials/credentials.h"
29 #include "librpc/gen_ndr/ndr_samr.h"
30 #include "../lib/util/dlinklist.h"
31 #include "../lib/util/asn1.h"
32 #include "ldap_server/ldap_server.h"
33 #include "smbd/service_task.h"
34 #include "smbd/service_stream.h"
35 #include "smbd/service.h"
36 #include "smbd/process_model.h"
37 #include "lib/tls/tls.h"
38 #include "lib/messaging/irpc.h"
39 #include <ldb.h>
40 #include <ldb_errors.h>
41 #include "libcli/ldap/ldap_proto.h"
42 #include "system/network.h"
43 #include "lib/socket/netif.h"
44 #include "dsdb/samdb/samdb.h"
45 #include "param/param.h"
46 #include "../lib/tsocket/tsocket.h"
47 #include "../lib/util/tevent_ntstatus.h"
48 #include "../libcli/util/tstream.h"
50 static void ldapsrv_terminate_connection_done(struct tevent_req *subreq);
53 close the socket and shutdown a server_context
55 static void ldapsrv_terminate_connection(struct ldapsrv_connection *conn,
56 const char *reason)
58 struct tevent_req *subreq;
60 if (conn->limits.reason) {
61 return;
64 conn->limits.endtime = timeval_current_ofs(0, 500);
66 tevent_queue_stop(conn->sockets.send_queue);
67 if (conn->active_call) {
68 tevent_req_cancel(conn->active_call);
69 conn->active_call = NULL;
72 conn->limits.reason = talloc_strdup(conn, reason);
73 if (conn->limits.reason == NULL) {
74 TALLOC_FREE(conn->sockets.tls);
75 TALLOC_FREE(conn->sockets.sasl);
76 TALLOC_FREE(conn->sockets.raw);
77 stream_terminate_connection(conn->connection, reason);
78 return;
81 subreq = tstream_disconnect_send(conn,
82 conn->connection->event.ctx,
83 conn->sockets.active);
84 if (subreq == NULL) {
85 TALLOC_FREE(conn->sockets.tls);
86 TALLOC_FREE(conn->sockets.sasl);
87 TALLOC_FREE(conn->sockets.raw);
88 stream_terminate_connection(conn->connection, reason);
89 return;
91 tevent_req_set_endtime(subreq,
92 conn->connection->event.ctx,
93 conn->limits.endtime);
94 tevent_req_set_callback(subreq, ldapsrv_terminate_connection_done, conn);
97 static void ldapsrv_terminate_connection_done(struct tevent_req *subreq)
99 struct ldapsrv_connection *conn =
100 tevent_req_callback_data(subreq,
101 struct ldapsrv_connection);
102 int sys_errno;
104 tstream_disconnect_recv(subreq, &sys_errno);
105 TALLOC_FREE(subreq);
107 if (conn->sockets.active == conn->sockets.raw) {
108 TALLOC_FREE(conn->sockets.tls);
109 TALLOC_FREE(conn->sockets.sasl);
110 TALLOC_FREE(conn->sockets.raw);
111 stream_terminate_connection(conn->connection,
112 conn->limits.reason);
113 return;
116 TALLOC_FREE(conn->sockets.tls);
117 TALLOC_FREE(conn->sockets.sasl);
118 conn->sockets.active = conn->sockets.raw;
120 subreq = tstream_disconnect_send(conn,
121 conn->connection->event.ctx,
122 conn->sockets.active);
123 if (subreq == NULL) {
124 TALLOC_FREE(conn->sockets.raw);
125 stream_terminate_connection(conn->connection,
126 conn->limits.reason);
127 return;
129 tevent_req_set_endtime(subreq,
130 conn->connection->event.ctx,
131 conn->limits.endtime);
132 tevent_req_set_callback(subreq, ldapsrv_terminate_connection_done, conn);
136 called when a LDAP socket becomes readable
138 void ldapsrv_recv(struct stream_connection *c, uint16_t flags)
140 smb_panic(__location__);
144 called when a LDAP socket becomes writable
146 static void ldapsrv_send(struct stream_connection *c, uint16_t flags)
148 smb_panic(__location__);
151 static int ldapsrv_load_limits(struct ldapsrv_connection *conn)
153 TALLOC_CTX *tmp_ctx;
154 const char *attrs[] = { "configurationNamingContext", NULL };
155 const char *attrs2[] = { "lDAPAdminLimits", NULL };
156 struct ldb_message_element *el;
157 struct ldb_result *res = NULL;
158 struct ldb_dn *basedn;
159 struct ldb_dn *conf_dn;
160 struct ldb_dn *policy_dn;
161 unsigned int i;
162 int ret;
164 /* set defaults limits in case of failure */
165 conn->limits.initial_timeout = 120;
166 conn->limits.conn_idle_time = 900;
167 conn->limits.max_page_size = 1000;
168 conn->limits.search_timeout = 120;
171 tmp_ctx = talloc_new(conn);
172 if (tmp_ctx == NULL) {
173 return -1;
176 basedn = ldb_dn_new(tmp_ctx, conn->ldb, NULL);
177 if (basedn == NULL) {
178 goto failed;
181 ret = ldb_search(conn->ldb, tmp_ctx, &res, basedn, LDB_SCOPE_BASE, attrs, NULL);
182 if (ret != LDB_SUCCESS) {
183 goto failed;
186 if (res->count != 1) {
187 goto failed;
190 conf_dn = ldb_msg_find_attr_as_dn(conn->ldb, tmp_ctx, res->msgs[0], "configurationNamingContext");
191 if (conf_dn == NULL) {
192 goto failed;
195 policy_dn = ldb_dn_copy(tmp_ctx, conf_dn);
196 ldb_dn_add_child_fmt(policy_dn, "CN=Default Query Policy,CN=Query-Policies,CN=Directory Service,CN=Windows NT,CN=Services");
197 if (policy_dn == NULL) {
198 goto failed;
201 ret = ldb_search(conn->ldb, tmp_ctx, &res, policy_dn, LDB_SCOPE_BASE, attrs2, NULL);
202 if (ret != LDB_SUCCESS) {
203 goto failed;
206 if (res->count != 1) {
207 goto failed;
210 el = ldb_msg_find_element(res->msgs[0], "lDAPAdminLimits");
211 if (el == NULL) {
212 goto failed;
215 for (i = 0; i < el->num_values; i++) {
216 char policy_name[256];
217 int policy_value, s;
219 s = sscanf((const char *)el->values[i].data, "%255[^=]=%d", policy_name, &policy_value);
220 if (ret != 2 || policy_value == 0)
221 continue;
223 if (strcasecmp("InitRecvTimeout", policy_name) == 0) {
224 conn->limits.initial_timeout = policy_value;
225 continue;
227 if (strcasecmp("MaxConnIdleTime", policy_name) == 0) {
228 conn->limits.conn_idle_time = policy_value;
229 continue;
231 if (strcasecmp("MaxPageSize", policy_name) == 0) {
232 conn->limits.max_page_size = policy_value;
233 continue;
235 if (strcasecmp("MaxQueryDuration", policy_name) == 0) {
236 conn->limits.search_timeout = policy_value;
237 continue;
241 return 0;
243 failed:
244 DEBUG(0, ("Failed to load ldap server query policies\n"));
245 talloc_free(tmp_ctx);
246 return -1;
249 static struct tevent_req *ldapsrv_process_call_send(TALLOC_CTX *mem_ctx,
250 struct tevent_context *ev,
251 struct tevent_queue *call_queue,
252 struct ldapsrv_call *call);
253 static NTSTATUS ldapsrv_process_call_recv(struct tevent_req *req);
255 static bool ldapsrv_call_read_next(struct ldapsrv_connection *conn);
256 static void ldapsrv_accept_tls_done(struct tevent_req *subreq);
259 initialise a server_context from a open socket and register a event handler
260 for reading from that socket
262 static void ldapsrv_accept(struct stream_connection *c,
263 struct auth_session_info *session_info,
264 bool is_privileged)
266 struct ldapsrv_service *ldapsrv_service =
267 talloc_get_type(c->private_data, struct ldapsrv_service);
268 struct ldapsrv_connection *conn;
269 struct cli_credentials *server_credentials;
270 struct socket_address *socket_address;
271 NTSTATUS status;
272 int port;
273 int ret;
274 struct tevent_req *subreq;
275 struct timeval endtime;
277 conn = talloc_zero(c, struct ldapsrv_connection);
278 if (!conn) {
279 stream_terminate_connection(c, "ldapsrv_accept: out of memory");
280 return;
282 conn->is_privileged = is_privileged;
284 conn->sockets.send_queue = tevent_queue_create(conn, "ldapsev send queue");
285 if (conn->sockets.send_queue == NULL) {
286 stream_terminate_connection(c,
287 "ldapsrv_accept: tevent_queue_create failed");
288 return;
291 TALLOC_FREE(c->event.fde);
293 ret = tstream_bsd_existing_socket(conn,
294 socket_get_fd(c->socket),
295 &conn->sockets.raw);
296 if (ret == -1) {
297 stream_terminate_connection(c,
298 "ldapsrv_accept: out of memory");
299 return;
301 socket_set_flags(c->socket, SOCKET_FLAG_NOCLOSE);
303 conn->connection = c;
304 conn->service = ldapsrv_service;
305 conn->lp_ctx = ldapsrv_service->task->lp_ctx;
307 c->private_data = conn;
309 socket_address = socket_get_my_addr(c->socket, conn);
310 if (!socket_address) {
311 ldapsrv_terminate_connection(conn, "ldapsrv_accept: failed to obtain local socket address!");
312 return;
314 port = socket_address->port;
315 talloc_free(socket_address);
316 if (port == 3268 || port == 3269) /* Global catalog */ {
317 conn->global_catalog = true;
320 server_credentials = cli_credentials_init(conn);
321 if (!server_credentials) {
322 stream_terminate_connection(c, "Failed to init server credentials\n");
323 return;
326 cli_credentials_set_conf(server_credentials, conn->lp_ctx);
327 status = cli_credentials_set_machine_account(server_credentials, conn->lp_ctx);
328 if (!NT_STATUS_IS_OK(status)) {
329 stream_terminate_connection(c, talloc_asprintf(conn, "Failed to obtain server credentials, perhaps a standalone server?: %s\n", nt_errstr(status)));
330 return;
332 conn->server_credentials = server_credentials;
334 conn->session_info = session_info;
336 conn->sockets.active = conn->sockets.raw;
338 if (!NT_STATUS_IS_OK(ldapsrv_backend_Init(conn))) {
339 ldapsrv_terminate_connection(conn, "backend Init failed");
340 return;
343 /* load limits from the conf partition */
344 ldapsrv_load_limits(conn); /* should we fail on error ? */
346 /* register the server */
347 irpc_add_name(c->msg_ctx, "ldap_server");
349 if (port != 636 && port != 3269) {
350 ldapsrv_call_read_next(conn);
351 return;
354 endtime = timeval_current_ofs(conn->limits.conn_idle_time, 0);
356 subreq = tstream_tls_accept_send(conn,
357 conn->connection->event.ctx,
358 conn->sockets.raw,
359 conn->service->tls_params);
360 if (subreq == NULL) {
361 ldapsrv_terminate_connection(conn, "ldapsrv_accept: "
362 "no memory for tstream_tls_accept_send");
363 return;
365 tevent_req_set_endtime(subreq,
366 conn->connection->event.ctx,
367 endtime);
368 tevent_req_set_callback(subreq, ldapsrv_accept_tls_done, conn);
371 static void ldapsrv_accept_tls_done(struct tevent_req *subreq)
373 struct ldapsrv_connection *conn =
374 tevent_req_callback_data(subreq,
375 struct ldapsrv_connection);
376 int ret;
377 int sys_errno;
379 ret = tstream_tls_accept_recv(subreq, &sys_errno,
380 conn, &conn->sockets.tls);
381 TALLOC_FREE(subreq);
382 if (ret == -1) {
383 const char *reason;
385 reason = talloc_asprintf(conn, "ldapsrv_accept_tls_loop: "
386 "tstream_tls_accept_recv() - %d:%s",
387 sys_errno, strerror(sys_errno));
388 if (!reason) {
389 reason = "ldapsrv_accept_tls_loop: "
390 "tstream_tls_accept_recv() - failed";
393 ldapsrv_terminate_connection(conn, reason);
394 return;
397 conn->sockets.active = conn->sockets.tls;
398 ldapsrv_call_read_next(conn);
401 static void ldapsrv_call_read_done(struct tevent_req *subreq);
403 static bool ldapsrv_call_read_next(struct ldapsrv_connection *conn)
405 struct tevent_req *subreq;
407 if (timeval_is_zero(&conn->limits.endtime)) {
408 conn->limits.endtime =
409 timeval_current_ofs(conn->limits.initial_timeout, 0);
410 } else {
411 conn->limits.endtime =
412 timeval_current_ofs(conn->limits.conn_idle_time, 0);
416 * The minimun size of a LDAP pdu is 7 bytes
418 * dumpasn1 -hh ldap-unbind-min.dat
420 * <30 05 02 01 09 42 00>
421 * 0 5: SEQUENCE {
422 * <02 01 09>
423 * 2 1: INTEGER 9
424 * <42 00>
425 * 5 0: [APPLICATION 2]
426 * : Error: Object has zero length.
427 * : }
429 * dumpasn1 -hh ldap-unbind-windows.dat
431 * <30 84 00 00 00 05 02 01 09 42 00>
432 * 0 5: SEQUENCE {
433 * <02 01 09>
434 * 6 1: INTEGER 9
435 * <42 00>
436 * 9 0: [APPLICATION 2]
437 * : Error: Object has zero length.
438 * : }
440 * This means using an initial read size
441 * of 7 is ok.
443 subreq = tstream_read_pdu_blob_send(conn,
444 conn->connection->event.ctx,
445 conn->sockets.active,
446 7, /* initial_read_size */
447 ldap_full_packet,
448 conn);
449 if (subreq == NULL) {
450 ldapsrv_terminate_connection(conn, "ldapsrv_call_read_next: "
451 "no memory for tstream_read_pdu_blob_send");
452 return false;
454 tevent_req_set_endtime(subreq,
455 conn->connection->event.ctx,
456 conn->limits.endtime);
457 tevent_req_set_callback(subreq, ldapsrv_call_read_done, conn);
458 return true;
461 static void ldapsrv_call_process_done(struct tevent_req *subreq);
463 static void ldapsrv_call_read_done(struct tevent_req *subreq)
465 struct ldapsrv_connection *conn =
466 tevent_req_callback_data(subreq,
467 struct ldapsrv_connection);
468 NTSTATUS status;
469 struct ldapsrv_call *call;
470 struct asn1_data *asn1;
471 DATA_BLOB blob;
473 call = talloc_zero(conn, struct ldapsrv_call);
474 if (!call) {
475 ldapsrv_terminate_connection(conn, "no memory");
476 return;
479 call->conn = conn;
481 status = tstream_read_pdu_blob_recv(subreq,
482 call,
483 &blob);
484 TALLOC_FREE(subreq);
485 if (!NT_STATUS_IS_OK(status)) {
486 const char *reason;
488 reason = talloc_asprintf(call, "ldapsrv_call_loop: "
489 "tstream_read_pdu_blob_recv() - %s",
490 nt_errstr(status));
491 if (!reason) {
492 reason = nt_errstr(status);
495 ldapsrv_terminate_connection(conn, reason);
496 return;
499 asn1 = asn1_init(call);
500 if (asn1 == NULL) {
501 ldapsrv_terminate_connection(conn, "no memory");
502 return;
505 call->request = talloc(call, struct ldap_message);
506 if (call->request == NULL) {
507 ldapsrv_terminate_connection(conn, "no memory");
508 return;
511 if (!asn1_load(asn1, blob)) {
512 ldapsrv_terminate_connection(conn, "asn1_load failed");
513 return;
516 status = ldap_decode(asn1, samba_ldap_control_handlers(),
517 call->request);
518 if (!NT_STATUS_IS_OK(status)) {
519 ldapsrv_terminate_connection(conn, nt_errstr(status));
520 return;
523 data_blob_free(&blob);
526 /* queue the call in the global queue */
527 subreq = ldapsrv_process_call_send(call,
528 conn->connection->event.ctx,
529 conn->service->call_queue,
530 call);
531 if (subreq == NULL) {
532 ldapsrv_terminate_connection(conn, "ldapsrv_process_call_send failed");
533 return;
535 tevent_req_set_callback(subreq, ldapsrv_call_process_done, call);
536 conn->active_call = subreq;
539 static void ldapsrv_call_writev_done(struct tevent_req *subreq);
541 static void ldapsrv_call_process_done(struct tevent_req *subreq)
543 struct ldapsrv_call *call =
544 tevent_req_callback_data(subreq,
545 struct ldapsrv_call);
546 struct ldapsrv_connection *conn = call->conn;
547 NTSTATUS status;
548 DATA_BLOB blob = data_blob_null;
550 conn->active_call = NULL;
552 status = ldapsrv_process_call_recv(subreq);
553 TALLOC_FREE(subreq);
554 if (!NT_STATUS_IS_OK(status)) {
555 ldapsrv_terminate_connection(conn, nt_errstr(status));
556 return;
559 /* build all the replies into a single blob */
560 while (call->replies) {
561 DATA_BLOB b;
562 bool ret;
564 if (!ldap_encode(call->replies->msg, samba_ldap_control_handlers(), &b, call)) {
565 DEBUG(0,("Failed to encode ldap reply of type %d\n",
566 call->replies->msg->type));
567 ldapsrv_terminate_connection(conn, "ldap_encode failed");
568 return;
571 ret = data_blob_append(call, &blob, b.data, b.length);
572 data_blob_free(&b);
574 talloc_set_name_const(blob.data, "Outgoing, encoded LDAP packet");
576 if (!ret) {
577 ldapsrv_terminate_connection(conn, "data_blob_append failed");
578 return;
581 DLIST_REMOVE(call->replies, call->replies);
584 if (blob.length == 0) {
585 TALLOC_FREE(call);
587 ldapsrv_call_read_next(conn);
588 return;
591 call->out_iov.iov_base = blob.data;
592 call->out_iov.iov_len = blob.length;
594 subreq = tstream_writev_queue_send(call,
595 conn->connection->event.ctx,
596 conn->sockets.active,
597 conn->sockets.send_queue,
598 &call->out_iov, 1);
599 if (subreq == NULL) {
600 ldapsrv_terminate_connection(conn, "stream_writev_queue_send failed");
601 return;
603 tevent_req_set_callback(subreq, ldapsrv_call_writev_done, call);
606 static void ldapsrv_call_postprocess_done(struct tevent_req *subreq);
608 static void ldapsrv_call_writev_done(struct tevent_req *subreq)
610 struct ldapsrv_call *call =
611 tevent_req_callback_data(subreq,
612 struct ldapsrv_call);
613 struct ldapsrv_connection *conn = call->conn;
614 int sys_errno;
615 int rc;
617 rc = tstream_writev_queue_recv(subreq, &sys_errno);
618 TALLOC_FREE(subreq);
619 if (rc == -1) {
620 const char *reason;
622 reason = talloc_asprintf(call, "ldapsrv_call_writev_done: "
623 "tstream_writev_queue_recv() - %d:%s",
624 sys_errno, strerror(sys_errno));
625 if (reason == NULL) {
626 reason = "ldapsrv_call_writev_done: "
627 "tstream_writev_queue_recv() failed";
630 ldapsrv_terminate_connection(conn, reason);
631 return;
634 if (call->postprocess_send) {
635 subreq = call->postprocess_send(call,
636 conn->connection->event.ctx,
637 call->postprocess_private);
638 if (subreq == NULL) {
639 ldapsrv_terminate_connection(conn, "ldapsrv_call_writev_done: "
640 "call->postprocess_send - no memory");
641 return;
643 tevent_req_set_callback(subreq,
644 ldapsrv_call_postprocess_done,
645 call);
646 return;
649 TALLOC_FREE(call);
651 ldapsrv_call_read_next(conn);
654 static void ldapsrv_call_postprocess_done(struct tevent_req *subreq)
656 struct ldapsrv_call *call =
657 tevent_req_callback_data(subreq,
658 struct ldapsrv_call);
659 struct ldapsrv_connection *conn = call->conn;
660 NTSTATUS status;
662 status = call->postprocess_recv(subreq);
663 TALLOC_FREE(subreq);
664 if (!NT_STATUS_IS_OK(status)) {
665 const char *reason;
667 reason = talloc_asprintf(call, "ldapsrv_call_postprocess_done: "
668 "call->postprocess_recv() - %s",
669 nt_errstr(status));
670 if (reason == NULL) {
671 reason = nt_errstr(status);
674 ldapsrv_terminate_connection(conn, reason);
675 return;
678 TALLOC_FREE(call);
680 ldapsrv_call_read_next(conn);
683 struct ldapsrv_process_call_state {
684 struct ldapsrv_call *call;
687 static void ldapsrv_process_call_trigger(struct tevent_req *req,
688 void *private_data);
690 static struct tevent_req *ldapsrv_process_call_send(TALLOC_CTX *mem_ctx,
691 struct tevent_context *ev,
692 struct tevent_queue *call_queue,
693 struct ldapsrv_call *call)
695 struct tevent_req *req;
696 struct ldapsrv_process_call_state *state;
697 bool ok;
699 req = tevent_req_create(mem_ctx, &state,
700 struct ldapsrv_process_call_state);
701 if (req == NULL) {
702 return req;
705 state->call = call;
707 ok = tevent_queue_add(call_queue, ev, req,
708 ldapsrv_process_call_trigger, NULL);
709 if (!ok) {
710 tevent_req_oom(req);
711 return tevent_req_post(req, ev);
714 return req;
717 static void ldapsrv_process_call_trigger(struct tevent_req *req,
718 void *private_data)
720 struct ldapsrv_process_call_state *state =
721 tevent_req_data(req,
722 struct ldapsrv_process_call_state);
723 NTSTATUS status;
725 /* make the call */
726 status = ldapsrv_do_call(state->call);
727 if (!NT_STATUS_IS_OK(status)) {
728 tevent_req_nterror(req, status);
729 return;
732 tevent_req_done(req);
735 static NTSTATUS ldapsrv_process_call_recv(struct tevent_req *req)
737 NTSTATUS status;
739 if (tevent_req_is_nterror(req, &status)) {
740 tevent_req_received(req);
741 return status;
744 tevent_req_received(req);
745 return NT_STATUS_OK;
748 static void ldapsrv_accept_nonpriv(struct stream_connection *c)
750 struct ldapsrv_service *ldapsrv_service = talloc_get_type_abort(
751 c->private_data, struct ldapsrv_service);
752 struct auth_session_info *session_info;
753 NTSTATUS status;
755 status = auth_anonymous_session_info(
756 c, ldapsrv_service->task->lp_ctx, &session_info);
757 if (!NT_STATUS_IS_OK(status)) {
758 stream_terminate_connection(c, "failed to setup anonymous "
759 "session info");
760 return;
762 ldapsrv_accept(c, session_info, false);
765 static const struct stream_server_ops ldap_stream_nonpriv_ops = {
766 .name = "ldap",
767 .accept_connection = ldapsrv_accept_nonpriv,
768 .recv_handler = ldapsrv_recv,
769 .send_handler = ldapsrv_send,
772 /* The feature removed behind an #ifdef until we can do it properly
773 * with an EXTERNAL bind. */
775 #define WITH_LDAPI_PRIV_SOCKET
777 #ifdef WITH_LDAPI_PRIV_SOCKET
778 static void ldapsrv_accept_priv(struct stream_connection *c)
780 struct ldapsrv_service *ldapsrv_service = talloc_get_type_abort(
781 c->private_data, struct ldapsrv_service);
782 struct auth_session_info *session_info;
784 session_info = system_session(ldapsrv_service->task->lp_ctx);
785 if (!session_info) {
786 stream_terminate_connection(c, "failed to setup system "
787 "session info");
788 return;
790 ldapsrv_accept(c, session_info, true);
793 static const struct stream_server_ops ldap_stream_priv_ops = {
794 .name = "ldap",
795 .accept_connection = ldapsrv_accept_priv,
796 .recv_handler = ldapsrv_recv,
797 .send_handler = ldapsrv_send,
800 #endif
804 add a socket address to the list of events, one event per port
806 static NTSTATUS add_socket(struct task_server *task,
807 struct loadparm_context *lp_ctx,
808 const struct model_ops *model_ops,
809 const char *address, struct ldapsrv_service *ldap_service)
811 uint16_t port = 389;
812 NTSTATUS status;
813 struct ldb_context *ldb;
815 status = stream_setup_socket(task, task->event_ctx, lp_ctx,
816 model_ops, &ldap_stream_nonpriv_ops,
817 "ip", address, &port,
818 lpcfg_socket_options(lp_ctx),
819 ldap_service);
820 if (!NT_STATUS_IS_OK(status)) {
821 DEBUG(0,("ldapsrv failed to bind to %s:%u - %s\n",
822 address, port, nt_errstr(status)));
823 return status;
826 if (tstream_tls_params_enabled(ldap_service->tls_params)) {
827 /* add ldaps server */
828 port = 636;
829 status = stream_setup_socket(task, task->event_ctx, lp_ctx,
830 model_ops,
831 &ldap_stream_nonpriv_ops,
832 "ip", address, &port,
833 lpcfg_socket_options(lp_ctx),
834 ldap_service);
835 if (!NT_STATUS_IS_OK(status)) {
836 DEBUG(0,("ldapsrv failed to bind to %s:%u - %s\n",
837 address, port, nt_errstr(status)));
838 return status;
842 /* Load LDAP database, but only to read our settings */
843 ldb = samdb_connect(ldap_service, ldap_service->task->event_ctx,
844 lp_ctx, system_session(lp_ctx), 0);
845 if (!ldb) {
846 return NT_STATUS_INTERNAL_DB_CORRUPTION;
849 if (samdb_is_gc(ldb)) {
850 port = 3268;
851 status = stream_setup_socket(task, task->event_ctx, lp_ctx,
852 model_ops,
853 &ldap_stream_nonpriv_ops,
854 "ip", address, &port,
855 lpcfg_socket_options(lp_ctx),
856 ldap_service);
857 if (!NT_STATUS_IS_OK(status)) {
858 DEBUG(0,("ldapsrv failed to bind to %s:%u - %s\n",
859 address, port, nt_errstr(status)));
860 return status;
862 if (tstream_tls_params_enabled(ldap_service->tls_params)) {
863 /* add ldaps server for the global catalog */
864 port = 3269;
865 status = stream_setup_socket(task, task->event_ctx, lp_ctx,
866 model_ops,
867 &ldap_stream_nonpriv_ops,
868 "ip", address, &port,
869 lpcfg_socket_options(lp_ctx),
870 ldap_service);
871 if (!NT_STATUS_IS_OK(status)) {
872 DEBUG(0,("ldapsrv failed to bind to %s:%u - %s\n",
873 address, port, nt_errstr(status)));
874 return status;
879 /* And once we are bound, free the temporary ldb, it will
880 * connect again on each incoming LDAP connection */
881 talloc_unlink(ldap_service, ldb);
883 return NT_STATUS_OK;
887 open the ldap server sockets
889 static void ldapsrv_task_init(struct task_server *task)
891 char *ldapi_path;
892 #ifdef WITH_LDAPI_PRIV_SOCKET
893 char *priv_dir;
894 #endif
895 const char *dns_host_name;
896 struct ldapsrv_service *ldap_service;
897 NTSTATUS status;
898 const struct model_ops *model_ops;
900 switch (lpcfg_server_role(task->lp_ctx)) {
901 case ROLE_STANDALONE:
902 task_server_terminate(task, "ldap_server: no LDAP server required in standalone configuration",
903 false);
904 return;
905 case ROLE_DOMAIN_MEMBER:
906 task_server_terminate(task, "ldap_server: no LDAP server required in member server configuration",
907 false);
908 return;
909 case ROLE_ACTIVE_DIRECTORY_DC:
910 /* Yes, we want an LDAP server */
911 break;
914 task_server_set_title(task, "task[ldapsrv]");
916 /* run the ldap server as a single process */
917 model_ops = process_model_startup("single");
918 if (!model_ops) goto failed;
920 ldap_service = talloc_zero(task, struct ldapsrv_service);
921 if (ldap_service == NULL) goto failed;
923 ldap_service->task = task;
925 dns_host_name = talloc_asprintf(ldap_service, "%s.%s",
926 lpcfg_netbios_name(task->lp_ctx),
927 lpcfg_dnsdomain(task->lp_ctx));
928 if (dns_host_name == NULL) goto failed;
930 status = tstream_tls_params_server(ldap_service,
931 dns_host_name,
932 lpcfg_tls_enabled(task->lp_ctx),
933 lpcfg_tls_keyfile(ldap_service, task->lp_ctx),
934 lpcfg_tls_certfile(ldap_service, task->lp_ctx),
935 lpcfg_tls_cafile(ldap_service, task->lp_ctx),
936 lpcfg_tls_crlfile(ldap_service, task->lp_ctx),
937 lpcfg_tls_dhpfile(ldap_service, task->lp_ctx),
938 &ldap_service->tls_params);
939 if (!NT_STATUS_IS_OK(status)) {
940 DEBUG(0,("ldapsrv failed tstream_tls_params_server - %s\n",
941 nt_errstr(status)));
942 goto failed;
945 ldap_service->call_queue = tevent_queue_create(ldap_service, "ldapsrv_call_queue");
946 if (ldap_service->call_queue == NULL) goto failed;
948 if (lpcfg_interfaces(task->lp_ctx) && lpcfg_bind_interfaces_only(task->lp_ctx)) {
949 struct interface *ifaces;
950 int num_interfaces;
951 int i;
953 load_interface_list(task, task->lp_ctx, &ifaces);
954 num_interfaces = iface_list_count(ifaces);
956 /* We have been given an interfaces line, and been
957 told to only bind to those interfaces. Create a
958 socket per interface and bind to only these.
960 for(i = 0; i < num_interfaces; i++) {
961 const char *address = iface_list_n_ip(ifaces, i);
962 status = add_socket(task, task->lp_ctx, model_ops, address, ldap_service);
963 if (!NT_STATUS_IS_OK(status)) goto failed;
965 } else {
966 const char **wcard;
967 int i;
968 wcard = iface_list_wildcard(task, task->lp_ctx);
969 if (wcard == NULL) {
970 DEBUG(0,("No wildcard addresses available\n"));
971 goto failed;
973 for (i=0; wcard[i]; i++) {
974 status = add_socket(task, task->lp_ctx, model_ops, wcard[i], ldap_service);
975 if (!NT_STATUS_IS_OK(status)) goto failed;
977 talloc_free(wcard);
980 ldapi_path = lpcfg_private_path(ldap_service, task->lp_ctx, "ldapi");
981 if (!ldapi_path) {
982 goto failed;
985 status = stream_setup_socket(task, task->event_ctx, task->lp_ctx,
986 model_ops, &ldap_stream_nonpriv_ops,
987 "unix", ldapi_path, NULL,
988 lpcfg_socket_options(task->lp_ctx),
989 ldap_service);
990 talloc_free(ldapi_path);
991 if (!NT_STATUS_IS_OK(status)) {
992 DEBUG(0,("ldapsrv failed to bind to %s - %s\n",
993 ldapi_path, nt_errstr(status)));
996 #ifdef WITH_LDAPI_PRIV_SOCKET
997 priv_dir = lpcfg_private_path(ldap_service, task->lp_ctx, "ldap_priv");
998 if (priv_dir == NULL) {
999 goto failed;
1002 * Make sure the directory for the privileged ldapi socket exists, and
1003 * is of the correct permissions
1005 if (!directory_create_or_exist(priv_dir, geteuid(), 0750)) {
1006 task_server_terminate(task, "Cannot create ldap "
1007 "privileged ldapi directory", true);
1008 return;
1010 ldapi_path = talloc_asprintf(ldap_service, "%s/ldapi", priv_dir);
1011 talloc_free(priv_dir);
1012 if (ldapi_path == NULL) {
1013 goto failed;
1016 status = stream_setup_socket(task, task->event_ctx, task->lp_ctx,
1017 model_ops, &ldap_stream_priv_ops,
1018 "unix", ldapi_path, NULL,
1019 lpcfg_socket_options(task->lp_ctx),
1020 ldap_service);
1021 talloc_free(ldapi_path);
1022 if (!NT_STATUS_IS_OK(status)) {
1023 DEBUG(0,("ldapsrv failed to bind to %s - %s\n",
1024 ldapi_path, nt_errstr(status)));
1027 #endif
1028 return;
1030 failed:
1031 task_server_terminate(task, "Failed to startup ldap server task", true);
1035 NTSTATUS server_service_ldap_init(void)
1037 return register_server_service("ldap", ldapsrv_task_init);