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"
39 #include "lib/ldb/include/ldb.h"
40 #include "lib/ldb/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 DEBUG(2,("ldapsrv_terminate_connection: %s - disconnecting\n",
69 tevent_queue_stop(conn->sockets.send_queue);
70 if (conn->active_call) {
71 tevent_req_cancel(conn->active_call);
72 conn->active_call = NULL;
75 conn->limits.reason = talloc_strdup(conn, reason);
76 if (conn->limits.reason == NULL) {
77 TALLOC_FREE(conn->sockets.tls);
78 TALLOC_FREE(conn->sockets.sasl);
79 TALLOC_FREE(conn->sockets.raw);
80 stream_terminate_connection(conn->connection, reason);
84 subreq = tstream_disconnect_send(conn,
85 conn->connection->event.ctx,
86 conn->sockets.active);
88 TALLOC_FREE(conn->sockets.tls);
89 TALLOC_FREE(conn->sockets.sasl);
90 TALLOC_FREE(conn->sockets.raw);
91 stream_terminate_connection(conn->connection, reason);
94 tevent_req_set_endtime(subreq,
95 conn->connection->event.ctx,
96 conn->limits.endtime);
97 tevent_req_set_callback(subreq, ldapsrv_terminate_connection_done, conn);
100 static void ldapsrv_terminate_connection_done(struct tevent_req *subreq)
102 struct ldapsrv_connection *conn =
103 tevent_req_callback_data(subreq,
104 struct ldapsrv_connection);
108 ret = tstream_disconnect_recv(subreq, &sys_errno);
111 if (conn->sockets.active == conn->sockets.raw) {
112 TALLOC_FREE(conn->sockets.tls);
113 TALLOC_FREE(conn->sockets.sasl);
114 TALLOC_FREE(conn->sockets.raw);
115 stream_terminate_connection(conn->connection,
116 conn->limits.reason);
120 TALLOC_FREE(conn->sockets.tls);
121 TALLOC_FREE(conn->sockets.sasl);
122 conn->sockets.active = conn->sockets.raw;
124 subreq = tstream_disconnect_send(conn,
125 conn->connection->event.ctx,
126 conn->sockets.active);
127 if (subreq == NULL) {
128 TALLOC_FREE(conn->sockets.raw);
129 stream_terminate_connection(conn->connection,
130 conn->limits.reason);
133 tevent_req_set_endtime(subreq,
134 conn->connection->event.ctx,
135 conn->limits.endtime);
136 tevent_req_set_callback(subreq, ldapsrv_terminate_connection_done, conn);
140 called when a LDAP socket becomes readable
142 void ldapsrv_recv(struct stream_connection *c, uint16_t flags)
144 smb_panic(__location__);
148 called when a LDAP socket becomes writable
150 static void ldapsrv_send(struct stream_connection *c, uint16_t flags)
152 smb_panic(__location__);
155 static int ldapsrv_load_limits(struct ldapsrv_connection *conn)
158 const char *attrs[] = { "configurationNamingContext", NULL };
159 const char *attrs2[] = { "lDAPAdminLimits", NULL };
160 struct ldb_message_element *el;
161 struct ldb_result *res = NULL;
162 struct ldb_dn *basedn;
163 struct ldb_dn *conf_dn;
164 struct ldb_dn *policy_dn;
168 /* set defaults limits in case of failure */
169 conn->limits.initial_timeout = 120;
170 conn->limits.conn_idle_time = 900;
171 conn->limits.max_page_size = 1000;
172 conn->limits.search_timeout = 120;
175 tmp_ctx = talloc_new(conn);
176 if (tmp_ctx == NULL) {
180 basedn = ldb_dn_new(tmp_ctx, conn->ldb, NULL);
181 if ( ! ldb_dn_validate(basedn)) {
185 ret = ldb_search(conn->ldb, tmp_ctx, &res, basedn, LDB_SCOPE_BASE, attrs, NULL);
186 if (ret != LDB_SUCCESS) {
190 if (res->count != 1) {
194 conf_dn = ldb_msg_find_attr_as_dn(conn->ldb, tmp_ctx, res->msgs[0], "configurationNamingContext");
195 if (conf_dn == NULL) {
199 policy_dn = ldb_dn_copy(tmp_ctx, conf_dn);
200 ldb_dn_add_child_fmt(policy_dn, "CN=Default Query Policy,CN=Query-Policies,CN=Directory Service,CN=Windows NT,CN=Services");
201 if (policy_dn == NULL) {
205 ret = ldb_search(conn->ldb, tmp_ctx, &res, policy_dn, LDB_SCOPE_BASE, attrs2, NULL);
206 if (ret != LDB_SUCCESS) {
210 if (res->count != 1) {
214 el = ldb_msg_find_element(res->msgs[0], "lDAPAdminLimits");
219 for (i = 0; i < el->num_values; i++) {
220 char policy_name[256];
223 s = sscanf((const char *)el->values[i].data, "%255[^=]=%d", policy_name, &policy_value);
224 if (ret != 2 || policy_value == 0)
227 if (strcasecmp("InitRecvTimeout", policy_name) == 0) {
228 conn->limits.initial_timeout = policy_value;
231 if (strcasecmp("MaxConnIdleTime", policy_name) == 0) {
232 conn->limits.conn_idle_time = policy_value;
235 if (strcasecmp("MaxPageSize", policy_name) == 0) {
236 conn->limits.max_page_size = policy_value;
239 if (strcasecmp("MaxQueryDuration", policy_name) == 0) {
240 conn->limits.search_timeout = policy_value;
248 DEBUG(0, ("Failed to load ldap server query policies\n"));
249 talloc_free(tmp_ctx);
253 static struct tevent_req *ldapsrv_process_call_send(TALLOC_CTX *mem_ctx,
254 struct tevent_context *ev,
255 struct tevent_queue *call_queue,
256 struct ldapsrv_call *call);
257 static NTSTATUS ldapsrv_process_call_recv(struct tevent_req *req);
259 static bool ldapsrv_call_read_next(struct ldapsrv_connection *conn);
260 static void ldapsrv_accept_tls_done(struct tevent_req *subreq);
263 initialise a server_context from a open socket and register a event handler
264 for reading from that socket
266 static void ldapsrv_accept(struct stream_connection *c,
267 struct auth_session_info *session_info)
269 struct ldapsrv_service *ldapsrv_service =
270 talloc_get_type(c->private_data, struct ldapsrv_service);
271 struct ldapsrv_connection *conn;
272 struct cli_credentials *server_credentials;
273 struct socket_address *socket_address;
277 struct tevent_req *subreq;
278 struct timeval endtime;
280 conn = talloc_zero(c, struct ldapsrv_connection);
282 stream_terminate_connection(c, "ldapsrv_accept: out of memory");
286 conn->sockets.send_queue = tevent_queue_create(conn, "ldapsev send queue");
287 if (conn->sockets.send_queue == NULL) {
288 stream_terminate_connection(c,
289 "ldapsrv_accept: tevent_queue_create failed");
293 TALLOC_FREE(c->event.fde);
295 ret = tstream_bsd_existing_socket(conn,
296 socket_get_fd(c->socket),
299 stream_terminate_connection(c,
300 "ldapsrv_accept: out of memory");
303 socket_set_flags(c->socket, SOCKET_FLAG_NOCLOSE);
305 conn->connection = c;
306 conn->service = ldapsrv_service;
307 conn->lp_ctx = ldapsrv_service->task->lp_ctx;
309 c->private_data = conn;
311 socket_address = socket_get_my_addr(c->socket, conn);
312 if (!socket_address) {
313 ldapsrv_terminate_connection(conn, "ldapsrv_accept: failed to obtain local socket address!");
316 port = socket_address->port;
317 talloc_free(socket_address);
318 if (port == 3268) /* Global catalog */ {
319 conn->global_catalog = true;
322 server_credentials = cli_credentials_init(conn);
323 if (!server_credentials) {
324 stream_terminate_connection(c, "Failed to init server credentials\n");
328 cli_credentials_set_conf(server_credentials, conn->lp_ctx);
329 status = cli_credentials_set_machine_account(server_credentials, conn->lp_ctx);
330 if (!NT_STATUS_IS_OK(status)) {
331 stream_terminate_connection(c, talloc_asprintf(conn, "Failed to obtain server credentials, perhaps a standalone server?: %s\n", nt_errstr(status)));
334 conn->server_credentials = server_credentials;
336 conn->session_info = talloc_move(conn, &session_info);
338 if (!NT_STATUS_IS_OK(ldapsrv_backend_Init(conn))) {
339 ldapsrv_terminate_connection(conn, "backend Init failed");
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 conn->sockets.active = conn->sockets.raw;
352 ldapsrv_call_read_next(conn);
356 endtime = timeval_current_ofs(conn->limits.conn_idle_time, 0);
358 subreq = tstream_tls_accept_send(conn,
359 conn->connection->event.ctx,
361 conn->service->tls_params);
362 if (subreq == NULL) {
363 ldapsrv_terminate_connection(conn, "ldapsrv_accept: "
364 "no memory for tstream_tls_accept_send");
367 tevent_req_set_endtime(subreq,
368 conn->connection->event.ctx,
370 tevent_req_set_callback(subreq, ldapsrv_accept_tls_done, conn);
373 static void ldapsrv_accept_tls_done(struct tevent_req *subreq)
375 struct ldapsrv_connection *conn =
376 tevent_req_callback_data(subreq,
377 struct ldapsrv_connection);
381 ret = tstream_tls_accept_recv(subreq, &sys_errno,
382 conn, &conn->sockets.tls);
387 reason = talloc_asprintf(conn, "ldapsrv_accept_tls_loop: "
388 "tstream_tls_accept_recv() - %d:%s",
389 sys_errno, strerror(sys_errno));
391 reason = "ldapsrv_accept_tls_loop: "
392 "tstream_tls_accept_recv() - failed";
395 ldapsrv_terminate_connection(conn, reason);
399 conn->sockets.active = conn->sockets.tls;
400 ldapsrv_call_read_next(conn);
403 static void ldapsrv_call_read_done(struct tevent_req *subreq);
405 static bool ldapsrv_call_read_next(struct ldapsrv_connection *conn)
407 struct tevent_req *subreq;
409 if (timeval_is_zero(&conn->limits.endtime)) {
410 conn->limits.endtime =
411 timeval_current_ofs(conn->limits.initial_timeout, 0);
413 conn->limits.endtime =
414 timeval_current_ofs(conn->limits.conn_idle_time, 0);
418 * The minimun size of a LDAP pdu is 7 bytes
420 * dumpasn1 -hh ldap-unbind-min.dat
422 * <30 05 02 01 09 42 00>
427 * 5 0: [APPLICATION 2]
428 * : Error: Object has zero length.
431 * dumpasn1 -hh ldap-unbind-windows.dat
433 * <30 84 00 00 00 05 02 01 09 42 00>
438 * 9 0: [APPLICATION 2]
439 * : Error: Object has zero length.
442 * This means using an initial read size
445 subreq = tstream_read_pdu_blob_send(conn,
446 conn->connection->event.ctx,
447 conn->sockets.active,
448 7, /* initial_read_size */
451 if (subreq == NULL) {
452 ldapsrv_terminate_connection(conn, "ldapsrv_call_read_next: "
453 "no memory for tstream_read_pdu_blob_send");
456 tevent_req_set_endtime(subreq,
457 conn->connection->event.ctx,
458 conn->limits.endtime);
459 tevent_req_set_callback(subreq, ldapsrv_call_read_done, conn);
463 static void ldapsrv_call_process_done(struct tevent_req *subreq);
465 static void ldapsrv_call_read_done(struct tevent_req *subreq)
467 struct ldapsrv_connection *conn =
468 tevent_req_callback_data(subreq,
469 struct ldapsrv_connection);
471 struct ldapsrv_call *call;
472 struct asn1_data *asn1;
475 call = talloc_zero(conn, struct ldapsrv_call);
477 ldapsrv_terminate_connection(conn, "no memory");
483 status = tstream_read_pdu_blob_recv(subreq,
487 if (!NT_STATUS_IS_OK(status)) {
490 reason = talloc_asprintf(call, "ldapsrv_call_loop: "
491 "tstream_read_pdu_blob_recv() - %s",
494 reason = nt_errstr(status);
497 ldapsrv_terminate_connection(conn, reason);
501 asn1 = asn1_init(call);
503 ldapsrv_terminate_connection(conn, "no memory");
507 call->request = talloc(call, struct ldap_message);
508 if (call->request == NULL) {
509 ldapsrv_terminate_connection(conn, "no memory");
513 if (!asn1_load(asn1, blob)) {
514 ldapsrv_terminate_connection(conn, "asn1_load failed");
518 status = ldap_decode(asn1, samba_ldap_control_handlers(),
520 if (!NT_STATUS_IS_OK(status)) {
521 ldapsrv_terminate_connection(conn, nt_errstr(status));
525 data_blob_free(&blob);
528 /* queue the call in the global queue */
529 subreq = ldapsrv_process_call_send(call,
530 conn->connection->event.ctx,
531 conn->service->call_queue,
533 if (subreq == NULL) {
534 ldapsrv_terminate_connection(conn, "ldapsrv_process_call_send failed");
537 tevent_req_set_callback(subreq, ldapsrv_call_process_done, call);
538 conn->active_call = subreq;
541 static void ldapsrv_call_writev_done(struct tevent_req *subreq);
543 static void ldapsrv_call_process_done(struct tevent_req *subreq)
545 struct ldapsrv_call *call =
546 tevent_req_callback_data(subreq,
547 struct ldapsrv_call);
548 struct ldapsrv_connection *conn = call->conn;
550 DATA_BLOB blob = data_blob_null;
552 conn->active_call = NULL;
554 status = ldapsrv_process_call_recv(subreq);
556 if (!NT_STATUS_IS_OK(status)) {
557 ldapsrv_terminate_connection(conn, nt_errstr(status));
561 /* build all the replies into a single blob */
562 while (call->replies) {
566 if (!ldap_encode(call->replies->msg, samba_ldap_control_handlers(), &b, call)) {
567 DEBUG(0,("Failed to encode ldap reply of type %d\n",
568 call->replies->msg->type));
569 ldapsrv_terminate_connection(conn, "ldap_encode failed");
573 ret = data_blob_append(call, &blob, b.data, b.length);
576 talloc_set_name_const(blob.data, "Outgoing, encoded LDAP packet");
579 ldapsrv_terminate_connection(conn, "data_blob_append failed");
583 DLIST_REMOVE(call->replies, call->replies);
586 if (blob.length == 0) {
589 ldapsrv_call_read_next(conn);
593 call->out_iov.iov_base = blob.data;
594 call->out_iov.iov_len = blob.length;
596 subreq = tstream_writev_queue_send(call,
597 conn->connection->event.ctx,
598 conn->sockets.active,
599 conn->sockets.send_queue,
601 if (subreq == NULL) {
602 ldapsrv_terminate_connection(conn, "stream_writev_queue_send failed");
605 tevent_req_set_callback(subreq, ldapsrv_call_writev_done, call);
608 static void ldapsrv_call_postprocess_done(struct tevent_req *subreq);
610 static void ldapsrv_call_writev_done(struct tevent_req *subreq)
612 struct ldapsrv_call *call =
613 tevent_req_callback_data(subreq,
614 struct ldapsrv_call);
615 struct ldapsrv_connection *conn = call->conn;
619 rc = tstream_writev_queue_recv(subreq, &sys_errno);
624 reason = talloc_asprintf(call, "ldapsrv_call_writev_done: "
625 "tstream_writev_queue_recv() - %d:%s",
626 sys_errno, strerror(sys_errno));
627 if (reason == NULL) {
628 reason = "ldapsrv_call_writev_done: "
629 "tstream_writev_queue_recv() failed";
632 ldapsrv_terminate_connection(conn, reason);
636 if (call->postprocess_send) {
637 subreq = call->postprocess_send(call,
638 conn->connection->event.ctx,
639 call->postprocess_private);
640 if (subreq == NULL) {
641 ldapsrv_terminate_connection(conn, "ldapsrv_call_writev_done: "
642 "call->postprocess_send - no memory");
645 tevent_req_set_callback(subreq,
646 ldapsrv_call_postprocess_done,
653 ldapsrv_call_read_next(conn);
656 static void ldapsrv_call_postprocess_done(struct tevent_req *subreq)
658 struct ldapsrv_call *call =
659 tevent_req_callback_data(subreq,
660 struct ldapsrv_call);
661 struct ldapsrv_connection *conn = call->conn;
664 status = call->postprocess_recv(subreq);
666 if (!NT_STATUS_IS_OK(status)) {
669 reason = talloc_asprintf(call, "ldapsrv_call_postprocess_done: "
670 "call->postprocess_recv() - %s",
672 if (reason == NULL) {
673 reason = nt_errstr(status);
676 ldapsrv_terminate_connection(conn, reason);
682 ldapsrv_call_read_next(conn);
685 struct ldapsrv_process_call_state {
686 struct ldapsrv_call *call;
689 static void ldapsrv_process_call_trigger(struct tevent_req *req,
692 static struct tevent_req *ldapsrv_process_call_send(TALLOC_CTX *mem_ctx,
693 struct tevent_context *ev,
694 struct tevent_queue *call_queue,
695 struct ldapsrv_call *call)
697 struct tevent_req *req;
698 struct ldapsrv_process_call_state *state;
701 req = tevent_req_create(mem_ctx, &state,
702 struct ldapsrv_process_call_state);
709 ok = tevent_queue_add(call_queue, ev, req,
710 ldapsrv_process_call_trigger, NULL);
712 tevent_req_nomem(NULL, req);
713 return tevent_req_post(req, ev);
719 static void ldapsrv_process_call_trigger(struct tevent_req *req,
722 struct ldapsrv_process_call_state *state =
724 struct ldapsrv_process_call_state);
728 status = ldapsrv_do_call(state->call);
729 if (!NT_STATUS_IS_OK(status)) {
730 tevent_req_nterror(req, status);
734 tevent_req_done(req);
737 static NTSTATUS ldapsrv_process_call_recv(struct tevent_req *req)
741 if (tevent_req_is_nterror(req, &status)) {
742 tevent_req_received(req);
746 tevent_req_received(req);
750 static void ldapsrv_accept_nonpriv(struct stream_connection *c)
752 struct ldapsrv_service *ldapsrv_service = talloc_get_type_abort(
753 c->private_data, struct ldapsrv_service);
754 struct auth_session_info *session_info;
757 status = auth_anonymous_session_info(
758 c, ldapsrv_service->task->lp_ctx, &session_info);
759 if (!NT_STATUS_IS_OK(status)) {
760 stream_terminate_connection(c, "failed to setup anonymous "
764 ldapsrv_accept(c, session_info);
767 static const struct stream_server_ops ldap_stream_nonpriv_ops = {
769 .accept_connection = ldapsrv_accept_nonpriv,
770 .recv_handler = ldapsrv_recv,
771 .send_handler = ldapsrv_send,
774 /* The feature removed behind an #ifdef until we can do it properly
775 * with an EXTERNAL bind. */
777 #define WITH_LDAPI_PRIV_SOCKET
779 #ifdef WITH_LDAPI_PRIV_SOCKET
780 static void ldapsrv_accept_priv(struct stream_connection *c)
782 struct ldapsrv_service *ldapsrv_service = talloc_get_type_abort(
783 c->private_data, struct ldapsrv_service);
784 struct auth_session_info *session_info;
786 session_info = system_session(ldapsrv_service->task->lp_ctx);
788 stream_terminate_connection(c, "failed to setup system "
792 ldapsrv_accept(c, session_info);
795 static const struct stream_server_ops ldap_stream_priv_ops = {
797 .accept_connection = ldapsrv_accept_priv,
798 .recv_handler = ldapsrv_recv,
799 .send_handler = ldapsrv_send,
804 add a socket address to the list of events, one event per port
806 static NTSTATUS add_socket(struct tevent_context *event_context,
807 struct loadparm_context *lp_ctx,
808 const struct model_ops *model_ops,
809 const char *address, struct ldapsrv_service *ldap_service)
813 struct ldb_context *ldb;
815 status = stream_setup_socket(event_context, lp_ctx,
816 model_ops, &ldap_stream_nonpriv_ops,
817 "ipv4", address, &port,
818 lpcfg_socket_options(lp_ctx),
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)));
826 if (tstream_tls_params_enabled(ldap_service->tls_params)) {
827 /* add ldaps server */
829 status = stream_setup_socket(event_context, lp_ctx,
831 &ldap_stream_nonpriv_ops,
832 "ipv4", address, &port,
833 lpcfg_socket_options(lp_ctx),
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)));
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);
846 return NT_STATUS_INTERNAL_DB_CORRUPTION;
849 if (samdb_is_gc(ldb)) {
851 status = stream_setup_socket(event_context, lp_ctx,
853 &ldap_stream_nonpriv_ops,
854 "ipv4", address, &port,
855 lpcfg_socket_options(lp_ctx),
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)));
864 /* And once we are bound, free the tempoary ldb, it will
865 * connect again on each incoming LDAP connection */
866 talloc_unlink(ldap_service, ldb);
872 open the ldap server sockets
874 static void ldapsrv_task_init(struct task_server *task)
877 #ifdef WITH_LDAPI_PRIV_SOCKET
880 const char *dns_host_name;
881 struct ldapsrv_service *ldap_service;
883 const struct model_ops *model_ops;
885 switch (lpcfg_server_role(task->lp_ctx)) {
886 case ROLE_STANDALONE:
887 task_server_terminate(task, "ldap_server: no LDAP server required in standalone configuration",
890 case ROLE_DOMAIN_MEMBER:
891 task_server_terminate(task, "ldap_server: no LDAP server required in member server configuration",
894 case ROLE_DOMAIN_CONTROLLER:
895 /* Yes, we want an LDAP server */
899 task_server_set_title(task, "task[ldapsrv]");
901 /* run the ldap server as a single process */
902 model_ops = process_model_startup(task->event_ctx, "single");
903 if (!model_ops) goto failed;
905 ldap_service = talloc_zero(task, struct ldapsrv_service);
906 if (ldap_service == NULL) goto failed;
908 ldap_service->task = task;
910 dns_host_name = talloc_asprintf(ldap_service, "%s.%s",
911 lpcfg_netbios_name(task->lp_ctx),
912 lpcfg_dnsdomain(task->lp_ctx));
913 if (dns_host_name == NULL) goto failed;
915 status = tstream_tls_params_server(ldap_service,
917 lpcfg_tls_enabled(task->lp_ctx),
918 lpcfg_tls_keyfile(ldap_service, task->lp_ctx),
919 lpcfg_tls_certfile(ldap_service, task->lp_ctx),
920 lpcfg_tls_cafile(ldap_service, task->lp_ctx),
921 lpcfg_tls_crlfile(ldap_service, task->lp_ctx),
922 lpcfg_tls_dhpfile(ldap_service, task->lp_ctx),
923 &ldap_service->tls_params);
924 if (!NT_STATUS_IS_OK(status)) {
925 DEBUG(0,("ldapsrv failed tstream_tls_patams_server - %s\n",
930 ldap_service->call_queue = tevent_queue_create(ldap_service, "ldapsrv_call_queue");
931 if (ldap_service->call_queue == NULL) goto failed;
933 if (lpcfg_interfaces(task->lp_ctx) && lpcfg_bind_interfaces_only(task->lp_ctx)) {
934 struct interface *ifaces;
938 load_interfaces(task, lpcfg_interfaces(task->lp_ctx), &ifaces);
939 num_interfaces = iface_count(ifaces);
941 /* We have been given an interfaces line, and been
942 told to only bind to those interfaces. Create a
943 socket per interface and bind to only these.
945 for(i = 0; i < num_interfaces; i++) {
946 const char *address = iface_n_ip(ifaces, i);
947 status = add_socket(task->event_ctx, task->lp_ctx, model_ops, address, ldap_service);
948 if (!NT_STATUS_IS_OK(status)) goto failed;
951 status = add_socket(task->event_ctx, task->lp_ctx, model_ops,
952 lpcfg_socket_address(task->lp_ctx), ldap_service);
953 if (!NT_STATUS_IS_OK(status)) goto failed;
956 ldapi_path = private_path(ldap_service, task->lp_ctx, "ldapi");
961 status = stream_setup_socket(task->event_ctx, task->lp_ctx,
962 model_ops, &ldap_stream_nonpriv_ops,
963 "unix", ldapi_path, NULL,
964 lpcfg_socket_options(task->lp_ctx),
966 talloc_free(ldapi_path);
967 if (!NT_STATUS_IS_OK(status)) {
968 DEBUG(0,("ldapsrv failed to bind to %s - %s\n",
969 ldapi_path, nt_errstr(status)));
972 #ifdef WITH_LDAPI_PRIV_SOCKET
973 priv_dir = private_path(ldap_service, task->lp_ctx, "ldap_priv");
974 if (priv_dir == NULL) {
978 * Make sure the directory for the privileged ldapi socket exists, and
979 * is of the correct permissions
981 if (!directory_create_or_exist(priv_dir, geteuid(), 0750)) {
982 task_server_terminate(task, "Cannot create ldap "
983 "privileged ldapi directory", true);
986 ldapi_path = talloc_asprintf(ldap_service, "%s/ldapi", priv_dir);
987 talloc_free(priv_dir);
988 if (ldapi_path == NULL) {
992 status = stream_setup_socket(task->event_ctx, task->lp_ctx,
993 model_ops, &ldap_stream_priv_ops,
994 "unix", ldapi_path, NULL,
995 lpcfg_socket_options(task->lp_ctx),
997 talloc_free(ldapi_path);
998 if (!NT_STATUS_IS_OK(status)) {
999 DEBUG(0,("ldapsrv failed to bind to %s - %s\n",
1000 ldapi_path, nt_errstr(status)));
1007 task_server_terminate(task, "Failed to startup ldap server task", true);
1011 NTSTATUS server_service_ldap_init(void)
1013 return register_server_service("ldap", ldapsrv_task_init);