2 Unix SMB/CIFS implementation.
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/>.
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"
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,
58 struct tevent_req *subreq;
60 if (conn->limits.reason) {
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);
81 subreq = tstream_disconnect_send(conn,
82 conn->connection->event.ctx,
83 conn->sockets.active);
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);
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);
104 tstream_disconnect_recv(subreq, &sys_errno);
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);
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);
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)
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;
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) {
176 basedn = ldb_dn_new(tmp_ctx, conn->ldb, NULL);
177 if (basedn == NULL) {
181 ret = ldb_search(conn->ldb, tmp_ctx, &res, basedn, LDB_SCOPE_BASE, attrs, NULL);
182 if (ret != LDB_SUCCESS) {
186 if (res->count != 1) {
190 conf_dn = ldb_msg_find_attr_as_dn(conn->ldb, tmp_ctx, res->msgs[0], "configurationNamingContext");
191 if (conf_dn == NULL) {
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) {
201 ret = ldb_search(conn->ldb, tmp_ctx, &res, policy_dn, LDB_SCOPE_BASE, attrs2, NULL);
202 if (ret != LDB_SUCCESS) {
206 if (res->count != 1) {
210 el = ldb_msg_find_element(res->msgs[0], "lDAPAdminLimits");
215 for (i = 0; i < el->num_values; i++) {
216 char policy_name[256];
219 s = sscanf((const char *)el->values[i].data, "%255[^=]=%d", policy_name, &policy_value);
220 if (s != 2 || policy_value == 0)
222 if (strcasecmp("InitRecvTimeout", policy_name) == 0) {
223 conn->limits.initial_timeout = policy_value;
226 if (strcasecmp("MaxConnIdleTime", policy_name) == 0) {
227 conn->limits.conn_idle_time = policy_value;
230 if (strcasecmp("MaxPageSize", policy_name) == 0) {
231 conn->limits.max_page_size = policy_value;
234 if (strcasecmp("MaxQueryDuration", policy_name) == 0) {
235 conn->limits.search_timeout = policy_value;
243 DEBUG(0, ("Failed to load ldap server query policies\n"));
244 talloc_free(tmp_ctx);
248 static struct tevent_req *ldapsrv_process_call_send(TALLOC_CTX *mem_ctx,
249 struct tevent_context *ev,
250 struct tevent_queue *call_queue,
251 struct ldapsrv_call *call);
252 static NTSTATUS ldapsrv_process_call_recv(struct tevent_req *req);
254 static bool ldapsrv_call_read_next(struct ldapsrv_connection *conn);
255 static void ldapsrv_accept_tls_done(struct tevent_req *subreq);
258 initialise a server_context from a open socket and register a event handler
259 for reading from that socket
261 static void ldapsrv_accept(struct stream_connection *c,
262 struct auth_session_info *session_info,
265 struct ldapsrv_service *ldapsrv_service =
266 talloc_get_type(c->private_data, struct ldapsrv_service);
267 struct ldapsrv_connection *conn;
268 struct cli_credentials *server_credentials;
269 struct socket_address *socket_address;
273 struct tevent_req *subreq;
274 struct timeval endtime;
276 conn = talloc_zero(c, struct ldapsrv_connection);
278 stream_terminate_connection(c, "ldapsrv_accept: out of memory");
281 conn->is_privileged = is_privileged;
283 conn->sockets.send_queue = tevent_queue_create(conn, "ldapsev send queue");
284 if (conn->sockets.send_queue == NULL) {
285 stream_terminate_connection(c,
286 "ldapsrv_accept: tevent_queue_create failed");
290 TALLOC_FREE(c->event.fde);
292 ret = tstream_bsd_existing_socket(conn,
293 socket_get_fd(c->socket),
296 stream_terminate_connection(c,
297 "ldapsrv_accept: out of memory");
300 socket_set_flags(c->socket, SOCKET_FLAG_NOCLOSE);
302 conn->connection = c;
303 conn->service = ldapsrv_service;
304 conn->lp_ctx = ldapsrv_service->task->lp_ctx;
306 c->private_data = conn;
308 socket_address = socket_get_my_addr(c->socket, conn);
309 if (!socket_address) {
310 ldapsrv_terminate_connection(conn, "ldapsrv_accept: failed to obtain local socket address!");
313 port = socket_address->port;
314 talloc_free(socket_address);
315 if (port == 3268 || port == 3269) /* Global catalog */ {
316 conn->global_catalog = true;
319 server_credentials = cli_credentials_init(conn);
320 if (!server_credentials) {
321 stream_terminate_connection(c, "Failed to init server credentials\n");
325 cli_credentials_set_conf(server_credentials, conn->lp_ctx);
326 status = cli_credentials_set_machine_account(server_credentials, conn->lp_ctx);
327 if (!NT_STATUS_IS_OK(status)) {
328 stream_terminate_connection(c, talloc_asprintf(conn, "Failed to obtain server credentials, perhaps a standalone server?: %s\n", nt_errstr(status)));
331 conn->server_credentials = server_credentials;
333 conn->session_info = session_info;
335 conn->sockets.active = conn->sockets.raw;
337 if (!NT_STATUS_IS_OK(ldapsrv_backend_Init(conn))) {
338 ldapsrv_terminate_connection(conn, "backend Init failed");
342 /* load limits from the conf partition */
343 ldapsrv_load_limits(conn); /* should we fail on error ? */
345 /* register the server */
346 irpc_add_name(c->msg_ctx, "ldap_server");
348 if (port != 636 && port != 3269) {
349 ldapsrv_call_read_next(conn);
353 endtime = timeval_current_ofs(conn->limits.conn_idle_time, 0);
355 subreq = tstream_tls_accept_send(conn,
356 conn->connection->event.ctx,
358 conn->service->tls_params);
359 if (subreq == NULL) {
360 ldapsrv_terminate_connection(conn, "ldapsrv_accept: "
361 "no memory for tstream_tls_accept_send");
364 tevent_req_set_endtime(subreq,
365 conn->connection->event.ctx,
367 tevent_req_set_callback(subreq, ldapsrv_accept_tls_done, conn);
370 static void ldapsrv_accept_tls_done(struct tevent_req *subreq)
372 struct ldapsrv_connection *conn =
373 tevent_req_callback_data(subreq,
374 struct ldapsrv_connection);
378 ret = tstream_tls_accept_recv(subreq, &sys_errno,
379 conn, &conn->sockets.tls);
384 reason = talloc_asprintf(conn, "ldapsrv_accept_tls_loop: "
385 "tstream_tls_accept_recv() - %d:%s",
386 sys_errno, strerror(sys_errno));
388 reason = "ldapsrv_accept_tls_loop: "
389 "tstream_tls_accept_recv() - failed";
392 ldapsrv_terminate_connection(conn, reason);
396 conn->sockets.active = conn->sockets.tls;
397 ldapsrv_call_read_next(conn);
400 static void ldapsrv_call_read_done(struct tevent_req *subreq);
402 static bool ldapsrv_call_read_next(struct ldapsrv_connection *conn)
404 struct tevent_req *subreq;
406 if (timeval_is_zero(&conn->limits.endtime)) {
407 conn->limits.endtime =
408 timeval_current_ofs(conn->limits.initial_timeout, 0);
410 conn->limits.endtime =
411 timeval_current_ofs(conn->limits.conn_idle_time, 0);
415 * The minimun size of a LDAP pdu is 7 bytes
417 * dumpasn1 -hh ldap-unbind-min.dat
419 * <30 05 02 01 09 42 00>
424 * 5 0: [APPLICATION 2]
425 * : Error: Object has zero length.
428 * dumpasn1 -hh ldap-unbind-windows.dat
430 * <30 84 00 00 00 05 02 01 09 42 00>
435 * 9 0: [APPLICATION 2]
436 * : Error: Object has zero length.
439 * This means using an initial read size
442 subreq = tstream_read_pdu_blob_send(conn,
443 conn->connection->event.ctx,
444 conn->sockets.active,
445 7, /* initial_read_size */
448 if (subreq == NULL) {
449 ldapsrv_terminate_connection(conn, "ldapsrv_call_read_next: "
450 "no memory for tstream_read_pdu_blob_send");
453 tevent_req_set_endtime(subreq,
454 conn->connection->event.ctx,
455 conn->limits.endtime);
456 tevent_req_set_callback(subreq, ldapsrv_call_read_done, conn);
460 static void ldapsrv_call_process_done(struct tevent_req *subreq);
462 static void ldapsrv_call_read_done(struct tevent_req *subreq)
464 struct ldapsrv_connection *conn =
465 tevent_req_callback_data(subreq,
466 struct ldapsrv_connection);
468 struct ldapsrv_call *call;
469 struct asn1_data *asn1;
472 call = talloc_zero(conn, struct ldapsrv_call);
474 ldapsrv_terminate_connection(conn, "no memory");
480 status = tstream_read_pdu_blob_recv(subreq,
484 if (!NT_STATUS_IS_OK(status)) {
487 reason = talloc_asprintf(call, "ldapsrv_call_loop: "
488 "tstream_read_pdu_blob_recv() - %s",
491 reason = nt_errstr(status);
494 ldapsrv_terminate_connection(conn, reason);
498 asn1 = asn1_init(call);
500 ldapsrv_terminate_connection(conn, "no memory");
504 call->request = talloc(call, struct ldap_message);
505 if (call->request == NULL) {
506 ldapsrv_terminate_connection(conn, "no memory");
510 if (!asn1_load(asn1, blob)) {
511 ldapsrv_terminate_connection(conn, "asn1_load failed");
515 status = ldap_decode(asn1, samba_ldap_control_handlers(),
517 if (!NT_STATUS_IS_OK(status)) {
518 ldapsrv_terminate_connection(conn, nt_errstr(status));
522 data_blob_free(&blob);
525 /* queue the call in the global queue */
526 subreq = ldapsrv_process_call_send(call,
527 conn->connection->event.ctx,
528 conn->service->call_queue,
530 if (subreq == NULL) {
531 ldapsrv_terminate_connection(conn, "ldapsrv_process_call_send failed");
534 tevent_req_set_callback(subreq, ldapsrv_call_process_done, call);
535 conn->active_call = subreq;
538 static void ldapsrv_call_writev_done(struct tevent_req *subreq);
540 static void ldapsrv_call_process_done(struct tevent_req *subreq)
542 struct ldapsrv_call *call =
543 tevent_req_callback_data(subreq,
544 struct ldapsrv_call);
545 struct ldapsrv_connection *conn = call->conn;
547 DATA_BLOB blob = data_blob_null;
549 conn->active_call = NULL;
551 status = ldapsrv_process_call_recv(subreq);
553 if (!NT_STATUS_IS_OK(status)) {
554 ldapsrv_terminate_connection(conn, nt_errstr(status));
558 /* build all the replies into a single blob */
559 while (call->replies) {
563 if (!ldap_encode(call->replies->msg, samba_ldap_control_handlers(), &b, call)) {
564 DEBUG(0,("Failed to encode ldap reply of type %d\n",
565 call->replies->msg->type));
566 ldapsrv_terminate_connection(conn, "ldap_encode failed");
570 ret = data_blob_append(call, &blob, b.data, b.length);
573 talloc_set_name_const(blob.data, "Outgoing, encoded LDAP packet");
576 ldapsrv_terminate_connection(conn, "data_blob_append failed");
580 DLIST_REMOVE(call->replies, call->replies);
583 if (blob.length == 0) {
586 ldapsrv_call_read_next(conn);
590 call->out_iov.iov_base = blob.data;
591 call->out_iov.iov_len = blob.length;
593 subreq = tstream_writev_queue_send(call,
594 conn->connection->event.ctx,
595 conn->sockets.active,
596 conn->sockets.send_queue,
598 if (subreq == NULL) {
599 ldapsrv_terminate_connection(conn, "stream_writev_queue_send failed");
602 tevent_req_set_callback(subreq, ldapsrv_call_writev_done, call);
605 static void ldapsrv_call_postprocess_done(struct tevent_req *subreq);
607 static void ldapsrv_call_writev_done(struct tevent_req *subreq)
609 struct ldapsrv_call *call =
610 tevent_req_callback_data(subreq,
611 struct ldapsrv_call);
612 struct ldapsrv_connection *conn = call->conn;
616 rc = tstream_writev_queue_recv(subreq, &sys_errno);
621 reason = talloc_asprintf(call, "ldapsrv_call_writev_done: "
622 "tstream_writev_queue_recv() - %d:%s",
623 sys_errno, strerror(sys_errno));
624 if (reason == NULL) {
625 reason = "ldapsrv_call_writev_done: "
626 "tstream_writev_queue_recv() failed";
629 ldapsrv_terminate_connection(conn, reason);
633 if (call->postprocess_send) {
634 subreq = call->postprocess_send(call,
635 conn->connection->event.ctx,
636 call->postprocess_private);
637 if (subreq == NULL) {
638 ldapsrv_terminate_connection(conn, "ldapsrv_call_writev_done: "
639 "call->postprocess_send - no memory");
642 tevent_req_set_callback(subreq,
643 ldapsrv_call_postprocess_done,
650 ldapsrv_call_read_next(conn);
653 static void ldapsrv_call_postprocess_done(struct tevent_req *subreq)
655 struct ldapsrv_call *call =
656 tevent_req_callback_data(subreq,
657 struct ldapsrv_call);
658 struct ldapsrv_connection *conn = call->conn;
661 status = call->postprocess_recv(subreq);
663 if (!NT_STATUS_IS_OK(status)) {
666 reason = talloc_asprintf(call, "ldapsrv_call_postprocess_done: "
667 "call->postprocess_recv() - %s",
669 if (reason == NULL) {
670 reason = nt_errstr(status);
673 ldapsrv_terminate_connection(conn, reason);
679 ldapsrv_call_read_next(conn);
682 struct ldapsrv_process_call_state {
683 struct ldapsrv_call *call;
686 static void ldapsrv_process_call_trigger(struct tevent_req *req,
689 static struct tevent_req *ldapsrv_process_call_send(TALLOC_CTX *mem_ctx,
690 struct tevent_context *ev,
691 struct tevent_queue *call_queue,
692 struct ldapsrv_call *call)
694 struct tevent_req *req;
695 struct ldapsrv_process_call_state *state;
698 req = tevent_req_create(mem_ctx, &state,
699 struct ldapsrv_process_call_state);
706 ok = tevent_queue_add(call_queue, ev, req,
707 ldapsrv_process_call_trigger, NULL);
710 return tevent_req_post(req, ev);
716 static void ldapsrv_process_call_trigger(struct tevent_req *req,
719 struct ldapsrv_process_call_state *state =
721 struct ldapsrv_process_call_state);
725 status = ldapsrv_do_call(state->call);
726 if (!NT_STATUS_IS_OK(status)) {
727 tevent_req_nterror(req, status);
731 tevent_req_done(req);
734 static NTSTATUS ldapsrv_process_call_recv(struct tevent_req *req)
738 if (tevent_req_is_nterror(req, &status)) {
739 tevent_req_received(req);
743 tevent_req_received(req);
747 static void ldapsrv_accept_nonpriv(struct stream_connection *c)
749 struct ldapsrv_service *ldapsrv_service = talloc_get_type_abort(
750 c->private_data, struct ldapsrv_service);
751 struct auth_session_info *session_info;
754 status = auth_anonymous_session_info(
755 c, ldapsrv_service->task->lp_ctx, &session_info);
756 if (!NT_STATUS_IS_OK(status)) {
757 stream_terminate_connection(c, "failed to setup anonymous "
761 ldapsrv_accept(c, session_info, false);
764 static const struct stream_server_ops ldap_stream_nonpriv_ops = {
766 .accept_connection = ldapsrv_accept_nonpriv,
767 .recv_handler = ldapsrv_recv,
768 .send_handler = ldapsrv_send,
771 /* The feature removed behind an #ifdef until we can do it properly
772 * with an EXTERNAL bind. */
774 #define WITH_LDAPI_PRIV_SOCKET
776 #ifdef WITH_LDAPI_PRIV_SOCKET
777 static void ldapsrv_accept_priv(struct stream_connection *c)
779 struct ldapsrv_service *ldapsrv_service = talloc_get_type_abort(
780 c->private_data, struct ldapsrv_service);
781 struct auth_session_info *session_info;
783 session_info = system_session(ldapsrv_service->task->lp_ctx);
785 stream_terminate_connection(c, "failed to setup system "
789 ldapsrv_accept(c, session_info, true);
792 static const struct stream_server_ops ldap_stream_priv_ops = {
794 .accept_connection = ldapsrv_accept_priv,
795 .recv_handler = ldapsrv_recv,
796 .send_handler = ldapsrv_send,
803 add a socket address to the list of events, one event per port
805 static NTSTATUS add_socket(struct task_server *task,
806 struct loadparm_context *lp_ctx,
807 const struct model_ops *model_ops,
808 const char *address, struct ldapsrv_service *ldap_service)
812 struct ldb_context *ldb;
814 status = stream_setup_socket(task, task->event_ctx, lp_ctx,
815 model_ops, &ldap_stream_nonpriv_ops,
816 "ip", address, &port,
817 lpcfg_socket_options(lp_ctx),
819 if (!NT_STATUS_IS_OK(status)) {
820 DEBUG(0,("ldapsrv failed to bind to %s:%u - %s\n",
821 address, port, nt_errstr(status)));
825 if (tstream_tls_params_enabled(ldap_service->tls_params)) {
826 /* add ldaps server */
828 status = stream_setup_socket(task, task->event_ctx, lp_ctx,
830 &ldap_stream_nonpriv_ops,
831 "ip", address, &port,
832 lpcfg_socket_options(lp_ctx),
834 if (!NT_STATUS_IS_OK(status)) {
835 DEBUG(0,("ldapsrv failed to bind to %s:%u - %s\n",
836 address, port, nt_errstr(status)));
841 /* Load LDAP database, but only to read our settings */
842 ldb = samdb_connect(ldap_service, ldap_service->task->event_ctx,
843 lp_ctx, system_session(lp_ctx), 0);
845 return NT_STATUS_INTERNAL_DB_CORRUPTION;
848 if (samdb_is_gc(ldb)) {
850 status = stream_setup_socket(task, task->event_ctx, lp_ctx,
852 &ldap_stream_nonpriv_ops,
853 "ip", address, &port,
854 lpcfg_socket_options(lp_ctx),
856 if (!NT_STATUS_IS_OK(status)) {
857 DEBUG(0,("ldapsrv failed to bind to %s:%u - %s\n",
858 address, port, nt_errstr(status)));
861 if (tstream_tls_params_enabled(ldap_service->tls_params)) {
862 /* add ldaps server for the global catalog */
864 status = stream_setup_socket(task, task->event_ctx, lp_ctx,
866 &ldap_stream_nonpriv_ops,
867 "ip", address, &port,
868 lpcfg_socket_options(lp_ctx),
870 if (!NT_STATUS_IS_OK(status)) {
871 DEBUG(0,("ldapsrv failed to bind to %s:%u - %s\n",
872 address, port, nt_errstr(status)));
878 /* And once we are bound, free the temporary ldb, it will
879 * connect again on each incoming LDAP connection */
880 talloc_unlink(ldap_service, ldb);
886 open the ldap server sockets
888 static void ldapsrv_task_init(struct task_server *task)
891 #ifdef WITH_LDAPI_PRIV_SOCKET
894 const char *dns_host_name;
895 struct ldapsrv_service *ldap_service;
897 const struct model_ops *model_ops;
899 switch (lpcfg_server_role(task->lp_ctx)) {
900 case ROLE_STANDALONE:
901 task_server_terminate(task, "ldap_server: no LDAP server required in standalone configuration",
904 case ROLE_DOMAIN_MEMBER:
905 task_server_terminate(task, "ldap_server: no LDAP server required in member server configuration",
908 case ROLE_ACTIVE_DIRECTORY_DC:
909 /* Yes, we want an LDAP server */
913 task_server_set_title(task, "task[ldapsrv]");
915 /* run the ldap server as a single process */
916 model_ops = process_model_startup("single");
917 if (!model_ops) goto failed;
919 ldap_service = talloc_zero(task, struct ldapsrv_service);
920 if (ldap_service == NULL) goto failed;
922 ldap_service->task = task;
924 dns_host_name = talloc_asprintf(ldap_service, "%s.%s",
925 lpcfg_netbios_name(task->lp_ctx),
926 lpcfg_dnsdomain(task->lp_ctx));
927 if (dns_host_name == NULL) goto failed;
929 status = tstream_tls_params_server(ldap_service,
931 lpcfg_tls_enabled(task->lp_ctx),
932 lpcfg_tls_keyfile(ldap_service, task->lp_ctx),
933 lpcfg_tls_certfile(ldap_service, task->lp_ctx),
934 lpcfg_tls_cafile(ldap_service, task->lp_ctx),
935 lpcfg_tls_crlfile(ldap_service, task->lp_ctx),
936 lpcfg_tls_dhpfile(ldap_service, task->lp_ctx),
937 &ldap_service->tls_params);
938 if (!NT_STATUS_IS_OK(status)) {
939 DEBUG(0,("ldapsrv failed tstream_tls_params_server - %s\n",
944 ldap_service->call_queue = tevent_queue_create(ldap_service, "ldapsrv_call_queue");
945 if (ldap_service->call_queue == NULL) goto failed;
947 if (lpcfg_interfaces(task->lp_ctx) && lpcfg_bind_interfaces_only(task->lp_ctx)) {
948 struct interface *ifaces;
952 load_interface_list(task, task->lp_ctx, &ifaces);
953 num_interfaces = iface_list_count(ifaces);
955 /* We have been given an interfaces line, and been
956 told to only bind to those interfaces. Create a
957 socket per interface and bind to only these.
959 for(i = 0; i < num_interfaces; i++) {
960 const char *address = iface_list_n_ip(ifaces, i);
961 status = add_socket(task, task->lp_ctx, model_ops, address, ldap_service);
962 if (!NT_STATUS_IS_OK(status)) goto failed;
967 wcard = iface_list_wildcard(task, task->lp_ctx);
969 DEBUG(0,("No wildcard addresses available\n"));
972 for (i=0; wcard[i]; i++) {
973 status = add_socket(task, task->lp_ctx, model_ops, wcard[i], ldap_service);
974 if (!NT_STATUS_IS_OK(status)) goto failed;
979 ldapi_path = lpcfg_private_path(ldap_service, task->lp_ctx, "ldapi");
984 status = stream_setup_socket(task, task->event_ctx, task->lp_ctx,
985 model_ops, &ldap_stream_nonpriv_ops,
986 "unix", ldapi_path, NULL,
987 lpcfg_socket_options(task->lp_ctx),
989 talloc_free(ldapi_path);
990 if (!NT_STATUS_IS_OK(status)) {
991 DEBUG(0,("ldapsrv failed to bind to %s - %s\n",
992 ldapi_path, nt_errstr(status)));
995 #ifdef WITH_LDAPI_PRIV_SOCKET
996 priv_dir = lpcfg_private_path(ldap_service, task->lp_ctx, "ldap_priv");
997 if (priv_dir == NULL) {
1001 * Make sure the directory for the privileged ldapi socket exists, and
1002 * is of the correct permissions
1004 if (!directory_create_or_exist(priv_dir, geteuid(), 0750)) {
1005 task_server_terminate(task, "Cannot create ldap "
1006 "privileged ldapi directory", true);
1009 ldapi_path = talloc_asprintf(ldap_service, "%s/ldapi", priv_dir);
1010 talloc_free(priv_dir);
1011 if (ldapi_path == NULL) {
1015 status = stream_setup_socket(task, task->event_ctx, task->lp_ctx,
1016 model_ops, &ldap_stream_priv_ops,
1017 "unix", ldapi_path, NULL,
1018 lpcfg_socket_options(task->lp_ctx),
1020 talloc_free(ldapi_path);
1021 if (!NT_STATUS_IS_OK(status)) {
1022 DEBUG(0,("ldapsrv failed to bind to %s - %s\n",
1023 ldapi_path, nt_errstr(status)));
1028 /* register the server */
1029 irpc_add_name(task->msg_ctx, "ldap_server");
1033 task_server_terminate(task, "Failed to startup ldap server task", true);
1037 NTSTATUS server_service_ldap_init(void)
1039 return register_server_service("ldap", ldapsrv_task_init);