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
);
105 ret
= tstream_disconnect_recv(subreq
, &sys_errno
);
108 if (conn
->sockets
.active
== conn
->sockets
.raw
) {
109 TALLOC_FREE(conn
->sockets
.tls
);
110 TALLOC_FREE(conn
->sockets
.sasl
);
111 TALLOC_FREE(conn
->sockets
.raw
);
112 stream_terminate_connection(conn
->connection
,
113 conn
->limits
.reason
);
117 TALLOC_FREE(conn
->sockets
.tls
);
118 TALLOC_FREE(conn
->sockets
.sasl
);
119 conn
->sockets
.active
= conn
->sockets
.raw
;
121 subreq
= tstream_disconnect_send(conn
,
122 conn
->connection
->event
.ctx
,
123 conn
->sockets
.active
);
124 if (subreq
== NULL
) {
125 TALLOC_FREE(conn
->sockets
.raw
);
126 stream_terminate_connection(conn
->connection
,
127 conn
->limits
.reason
);
130 tevent_req_set_endtime(subreq
,
131 conn
->connection
->event
.ctx
,
132 conn
->limits
.endtime
);
133 tevent_req_set_callback(subreq
, ldapsrv_terminate_connection_done
, conn
);
137 called when a LDAP socket becomes readable
139 void ldapsrv_recv(struct stream_connection
*c
, uint16_t flags
)
141 smb_panic(__location__
);
145 called when a LDAP socket becomes writable
147 static void ldapsrv_send(struct stream_connection
*c
, uint16_t flags
)
149 smb_panic(__location__
);
152 static int ldapsrv_load_limits(struct ldapsrv_connection
*conn
)
155 const char *attrs
[] = { "configurationNamingContext", NULL
};
156 const char *attrs2
[] = { "lDAPAdminLimits", NULL
};
157 struct ldb_message_element
*el
;
158 struct ldb_result
*res
= NULL
;
159 struct ldb_dn
*basedn
;
160 struct ldb_dn
*conf_dn
;
161 struct ldb_dn
*policy_dn
;
165 /* set defaults limits in case of failure */
166 conn
->limits
.initial_timeout
= 120;
167 conn
->limits
.conn_idle_time
= 900;
168 conn
->limits
.max_page_size
= 1000;
169 conn
->limits
.search_timeout
= 120;
172 tmp_ctx
= talloc_new(conn
);
173 if (tmp_ctx
== NULL
) {
177 basedn
= ldb_dn_new(tmp_ctx
, conn
->ldb
, NULL
);
178 if (basedn
== NULL
) {
182 ret
= ldb_search(conn
->ldb
, tmp_ctx
, &res
, basedn
, LDB_SCOPE_BASE
, attrs
, NULL
);
183 if (ret
!= LDB_SUCCESS
) {
187 if (res
->count
!= 1) {
191 conf_dn
= ldb_msg_find_attr_as_dn(conn
->ldb
, tmp_ctx
, res
->msgs
[0], "configurationNamingContext");
192 if (conf_dn
== NULL
) {
196 policy_dn
= ldb_dn_copy(tmp_ctx
, conf_dn
);
197 ldb_dn_add_child_fmt(policy_dn
, "CN=Default Query Policy,CN=Query-Policies,CN=Directory Service,CN=Windows NT,CN=Services");
198 if (policy_dn
== NULL
) {
202 ret
= ldb_search(conn
->ldb
, tmp_ctx
, &res
, policy_dn
, LDB_SCOPE_BASE
, attrs2
, NULL
);
203 if (ret
!= LDB_SUCCESS
) {
207 if (res
->count
!= 1) {
211 el
= ldb_msg_find_element(res
->msgs
[0], "lDAPAdminLimits");
216 for (i
= 0; i
< el
->num_values
; i
++) {
217 char policy_name
[256];
220 s
= sscanf((const char *)el
->values
[i
].data
, "%255[^=]=%d", policy_name
, &policy_value
);
221 if (ret
!= 2 || policy_value
== 0)
224 if (strcasecmp("InitRecvTimeout", policy_name
) == 0) {
225 conn
->limits
.initial_timeout
= policy_value
;
228 if (strcasecmp("MaxConnIdleTime", policy_name
) == 0) {
229 conn
->limits
.conn_idle_time
= policy_value
;
232 if (strcasecmp("MaxPageSize", policy_name
) == 0) {
233 conn
->limits
.max_page_size
= policy_value
;
236 if (strcasecmp("MaxQueryDuration", policy_name
) == 0) {
237 conn
->limits
.search_timeout
= policy_value
;
245 DEBUG(0, ("Failed to load ldap server query policies\n"));
246 talloc_free(tmp_ctx
);
250 static struct tevent_req
*ldapsrv_process_call_send(TALLOC_CTX
*mem_ctx
,
251 struct tevent_context
*ev
,
252 struct tevent_queue
*call_queue
,
253 struct ldapsrv_call
*call
);
254 static NTSTATUS
ldapsrv_process_call_recv(struct tevent_req
*req
);
256 static bool ldapsrv_call_read_next(struct ldapsrv_connection
*conn
);
257 static void ldapsrv_accept_tls_done(struct tevent_req
*subreq
);
260 initialise a server_context from a open socket and register a event handler
261 for reading from that socket
263 static void ldapsrv_accept(struct stream_connection
*c
,
264 struct auth_session_info
*session_info
,
267 struct ldapsrv_service
*ldapsrv_service
=
268 talloc_get_type(c
->private_data
, struct ldapsrv_service
);
269 struct ldapsrv_connection
*conn
;
270 struct cli_credentials
*server_credentials
;
271 struct socket_address
*socket_address
;
275 struct tevent_req
*subreq
;
276 struct timeval endtime
;
278 conn
= talloc_zero(c
, struct ldapsrv_connection
);
280 stream_terminate_connection(c
, "ldapsrv_accept: out of memory");
283 conn
->is_privileged
= is_privileged
;
285 conn
->sockets
.send_queue
= tevent_queue_create(conn
, "ldapsev send queue");
286 if (conn
->sockets
.send_queue
== NULL
) {
287 stream_terminate_connection(c
,
288 "ldapsrv_accept: tevent_queue_create failed");
292 TALLOC_FREE(c
->event
.fde
);
294 ret
= tstream_bsd_existing_socket(conn
,
295 socket_get_fd(c
->socket
),
298 stream_terminate_connection(c
,
299 "ldapsrv_accept: out of memory");
302 socket_set_flags(c
->socket
, SOCKET_FLAG_NOCLOSE
);
304 conn
->connection
= c
;
305 conn
->service
= ldapsrv_service
;
306 conn
->lp_ctx
= ldapsrv_service
->task
->lp_ctx
;
308 c
->private_data
= conn
;
310 socket_address
= socket_get_my_addr(c
->socket
, conn
);
311 if (!socket_address
) {
312 ldapsrv_terminate_connection(conn
, "ldapsrv_accept: failed to obtain local socket address!");
315 port
= socket_address
->port
;
316 talloc_free(socket_address
);
317 if (port
== 3268 || port
== 3269) /* Global catalog */ {
318 conn
->global_catalog
= true;
321 server_credentials
= cli_credentials_init(conn
);
322 if (!server_credentials
) {
323 stream_terminate_connection(c
, "Failed to init server credentials\n");
327 cli_credentials_set_conf(server_credentials
, conn
->lp_ctx
);
328 status
= cli_credentials_set_machine_account(server_credentials
, conn
->lp_ctx
);
329 if (!NT_STATUS_IS_OK(status
)) {
330 stream_terminate_connection(c
, talloc_asprintf(conn
, "Failed to obtain server credentials, perhaps a standalone server?: %s\n", nt_errstr(status
)));
333 conn
->server_credentials
= server_credentials
;
335 conn
->session_info
= session_info
;
337 conn
->sockets
.active
= conn
->sockets
.raw
;
339 if (!NT_STATUS_IS_OK(ldapsrv_backend_Init(conn
))) {
340 ldapsrv_terminate_connection(conn
, "backend Init failed");
344 /* load limits from the conf partition */
345 ldapsrv_load_limits(conn
); /* should we fail on error ? */
347 /* register the server */
348 irpc_add_name(c
->msg_ctx
, "ldap_server");
350 if (port
!= 636 && port
!= 3269) {
351 ldapsrv_call_read_next(conn
);
355 endtime
= timeval_current_ofs(conn
->limits
.conn_idle_time
, 0);
357 subreq
= tstream_tls_accept_send(conn
,
358 conn
->connection
->event
.ctx
,
360 conn
->service
->tls_params
);
361 if (subreq
== NULL
) {
362 ldapsrv_terminate_connection(conn
, "ldapsrv_accept: "
363 "no memory for tstream_tls_accept_send");
366 tevent_req_set_endtime(subreq
,
367 conn
->connection
->event
.ctx
,
369 tevent_req_set_callback(subreq
, ldapsrv_accept_tls_done
, conn
);
372 static void ldapsrv_accept_tls_done(struct tevent_req
*subreq
)
374 struct ldapsrv_connection
*conn
=
375 tevent_req_callback_data(subreq
,
376 struct ldapsrv_connection
);
380 ret
= tstream_tls_accept_recv(subreq
, &sys_errno
,
381 conn
, &conn
->sockets
.tls
);
386 reason
= talloc_asprintf(conn
, "ldapsrv_accept_tls_loop: "
387 "tstream_tls_accept_recv() - %d:%s",
388 sys_errno
, strerror(sys_errno
));
390 reason
= "ldapsrv_accept_tls_loop: "
391 "tstream_tls_accept_recv() - failed";
394 ldapsrv_terminate_connection(conn
, reason
);
398 conn
->sockets
.active
= conn
->sockets
.tls
;
399 ldapsrv_call_read_next(conn
);
402 static void ldapsrv_call_read_done(struct tevent_req
*subreq
);
404 static bool ldapsrv_call_read_next(struct ldapsrv_connection
*conn
)
406 struct tevent_req
*subreq
;
408 if (timeval_is_zero(&conn
->limits
.endtime
)) {
409 conn
->limits
.endtime
=
410 timeval_current_ofs(conn
->limits
.initial_timeout
, 0);
412 conn
->limits
.endtime
=
413 timeval_current_ofs(conn
->limits
.conn_idle_time
, 0);
417 * The minimun size of a LDAP pdu is 7 bytes
419 * dumpasn1 -hh ldap-unbind-min.dat
421 * <30 05 02 01 09 42 00>
426 * 5 0: [APPLICATION 2]
427 * : Error: Object has zero length.
430 * dumpasn1 -hh ldap-unbind-windows.dat
432 * <30 84 00 00 00 05 02 01 09 42 00>
437 * 9 0: [APPLICATION 2]
438 * : Error: Object has zero length.
441 * This means using an initial read size
444 subreq
= tstream_read_pdu_blob_send(conn
,
445 conn
->connection
->event
.ctx
,
446 conn
->sockets
.active
,
447 7, /* initial_read_size */
450 if (subreq
== NULL
) {
451 ldapsrv_terminate_connection(conn
, "ldapsrv_call_read_next: "
452 "no memory for tstream_read_pdu_blob_send");
455 tevent_req_set_endtime(subreq
,
456 conn
->connection
->event
.ctx
,
457 conn
->limits
.endtime
);
458 tevent_req_set_callback(subreq
, ldapsrv_call_read_done
, conn
);
462 static void ldapsrv_call_process_done(struct tevent_req
*subreq
);
464 static void ldapsrv_call_read_done(struct tevent_req
*subreq
)
466 struct ldapsrv_connection
*conn
=
467 tevent_req_callback_data(subreq
,
468 struct ldapsrv_connection
);
470 struct ldapsrv_call
*call
;
471 struct asn1_data
*asn1
;
474 call
= talloc_zero(conn
, struct ldapsrv_call
);
476 ldapsrv_terminate_connection(conn
, "no memory");
482 status
= tstream_read_pdu_blob_recv(subreq
,
486 if (!NT_STATUS_IS_OK(status
)) {
489 reason
= talloc_asprintf(call
, "ldapsrv_call_loop: "
490 "tstream_read_pdu_blob_recv() - %s",
493 reason
= nt_errstr(status
);
496 ldapsrv_terminate_connection(conn
, reason
);
500 asn1
= asn1_init(call
);
502 ldapsrv_terminate_connection(conn
, "no memory");
506 call
->request
= talloc(call
, struct ldap_message
);
507 if (call
->request
== NULL
) {
508 ldapsrv_terminate_connection(conn
, "no memory");
512 if (!asn1_load(asn1
, blob
)) {
513 ldapsrv_terminate_connection(conn
, "asn1_load failed");
517 status
= ldap_decode(asn1
, samba_ldap_control_handlers(),
519 if (!NT_STATUS_IS_OK(status
)) {
520 ldapsrv_terminate_connection(conn
, nt_errstr(status
));
524 data_blob_free(&blob
);
527 /* queue the call in the global queue */
528 subreq
= ldapsrv_process_call_send(call
,
529 conn
->connection
->event
.ctx
,
530 conn
->service
->call_queue
,
532 if (subreq
== NULL
) {
533 ldapsrv_terminate_connection(conn
, "ldapsrv_process_call_send failed");
536 tevent_req_set_callback(subreq
, ldapsrv_call_process_done
, call
);
537 conn
->active_call
= subreq
;
540 static void ldapsrv_call_writev_done(struct tevent_req
*subreq
);
542 static void ldapsrv_call_process_done(struct tevent_req
*subreq
)
544 struct ldapsrv_call
*call
=
545 tevent_req_callback_data(subreq
,
546 struct ldapsrv_call
);
547 struct ldapsrv_connection
*conn
= call
->conn
;
549 DATA_BLOB blob
= data_blob_null
;
551 conn
->active_call
= NULL
;
553 status
= ldapsrv_process_call_recv(subreq
);
555 if (!NT_STATUS_IS_OK(status
)) {
556 ldapsrv_terminate_connection(conn
, nt_errstr(status
));
560 /* build all the replies into a single blob */
561 while (call
->replies
) {
565 if (!ldap_encode(call
->replies
->msg
, samba_ldap_control_handlers(), &b
, call
)) {
566 DEBUG(0,("Failed to encode ldap reply of type %d\n",
567 call
->replies
->msg
->type
));
568 ldapsrv_terminate_connection(conn
, "ldap_encode failed");
572 ret
= data_blob_append(call
, &blob
, b
.data
, b
.length
);
575 talloc_set_name_const(blob
.data
, "Outgoing, encoded LDAP packet");
578 ldapsrv_terminate_connection(conn
, "data_blob_append failed");
582 DLIST_REMOVE(call
->replies
, call
->replies
);
585 if (blob
.length
== 0) {
588 ldapsrv_call_read_next(conn
);
592 call
->out_iov
.iov_base
= blob
.data
;
593 call
->out_iov
.iov_len
= blob
.length
;
595 subreq
= tstream_writev_queue_send(call
,
596 conn
->connection
->event
.ctx
,
597 conn
->sockets
.active
,
598 conn
->sockets
.send_queue
,
600 if (subreq
== NULL
) {
601 ldapsrv_terminate_connection(conn
, "stream_writev_queue_send failed");
604 tevent_req_set_callback(subreq
, ldapsrv_call_writev_done
, call
);
607 static void ldapsrv_call_postprocess_done(struct tevent_req
*subreq
);
609 static void ldapsrv_call_writev_done(struct tevent_req
*subreq
)
611 struct ldapsrv_call
*call
=
612 tevent_req_callback_data(subreq
,
613 struct ldapsrv_call
);
614 struct ldapsrv_connection
*conn
= call
->conn
;
618 rc
= tstream_writev_queue_recv(subreq
, &sys_errno
);
623 reason
= talloc_asprintf(call
, "ldapsrv_call_writev_done: "
624 "tstream_writev_queue_recv() - %d:%s",
625 sys_errno
, strerror(sys_errno
));
626 if (reason
== NULL
) {
627 reason
= "ldapsrv_call_writev_done: "
628 "tstream_writev_queue_recv() failed";
631 ldapsrv_terminate_connection(conn
, reason
);
635 if (call
->postprocess_send
) {
636 subreq
= call
->postprocess_send(call
,
637 conn
->connection
->event
.ctx
,
638 call
->postprocess_private
);
639 if (subreq
== NULL
) {
640 ldapsrv_terminate_connection(conn
, "ldapsrv_call_writev_done: "
641 "call->postprocess_send - no memory");
644 tevent_req_set_callback(subreq
,
645 ldapsrv_call_postprocess_done
,
652 ldapsrv_call_read_next(conn
);
655 static void ldapsrv_call_postprocess_done(struct tevent_req
*subreq
)
657 struct ldapsrv_call
*call
=
658 tevent_req_callback_data(subreq
,
659 struct ldapsrv_call
);
660 struct ldapsrv_connection
*conn
= call
->conn
;
663 status
= call
->postprocess_recv(subreq
);
665 if (!NT_STATUS_IS_OK(status
)) {
668 reason
= talloc_asprintf(call
, "ldapsrv_call_postprocess_done: "
669 "call->postprocess_recv() - %s",
671 if (reason
== NULL
) {
672 reason
= nt_errstr(status
);
675 ldapsrv_terminate_connection(conn
, reason
);
681 ldapsrv_call_read_next(conn
);
684 struct ldapsrv_process_call_state
{
685 struct ldapsrv_call
*call
;
688 static void ldapsrv_process_call_trigger(struct tevent_req
*req
,
691 static struct tevent_req
*ldapsrv_process_call_send(TALLOC_CTX
*mem_ctx
,
692 struct tevent_context
*ev
,
693 struct tevent_queue
*call_queue
,
694 struct ldapsrv_call
*call
)
696 struct tevent_req
*req
;
697 struct ldapsrv_process_call_state
*state
;
700 req
= tevent_req_create(mem_ctx
, &state
,
701 struct ldapsrv_process_call_state
);
708 ok
= tevent_queue_add(call_queue
, ev
, req
,
709 ldapsrv_process_call_trigger
, NULL
);
712 return tevent_req_post(req
, ev
);
718 static void ldapsrv_process_call_trigger(struct tevent_req
*req
,
721 struct ldapsrv_process_call_state
*state
=
723 struct ldapsrv_process_call_state
);
727 status
= ldapsrv_do_call(state
->call
);
728 if (!NT_STATUS_IS_OK(status
)) {
729 tevent_req_nterror(req
, status
);
733 tevent_req_done(req
);
736 static NTSTATUS
ldapsrv_process_call_recv(struct tevent_req
*req
)
740 if (tevent_req_is_nterror(req
, &status
)) {
741 tevent_req_received(req
);
745 tevent_req_received(req
);
749 static void ldapsrv_accept_nonpriv(struct stream_connection
*c
)
751 struct ldapsrv_service
*ldapsrv_service
= talloc_get_type_abort(
752 c
->private_data
, struct ldapsrv_service
);
753 struct auth_session_info
*session_info
;
756 status
= auth_anonymous_session_info(
757 c
, ldapsrv_service
->task
->lp_ctx
, &session_info
);
758 if (!NT_STATUS_IS_OK(status
)) {
759 stream_terminate_connection(c
, "failed to setup anonymous "
763 ldapsrv_accept(c
, session_info
, false);
766 static const struct stream_server_ops ldap_stream_nonpriv_ops
= {
768 .accept_connection
= ldapsrv_accept_nonpriv
,
769 .recv_handler
= ldapsrv_recv
,
770 .send_handler
= ldapsrv_send
,
773 /* The feature removed behind an #ifdef until we can do it properly
774 * with an EXTERNAL bind. */
776 #define WITH_LDAPI_PRIV_SOCKET
778 #ifdef WITH_LDAPI_PRIV_SOCKET
779 static void ldapsrv_accept_priv(struct stream_connection
*c
)
781 struct ldapsrv_service
*ldapsrv_service
= talloc_get_type_abort(
782 c
->private_data
, struct ldapsrv_service
);
783 struct auth_session_info
*session_info
;
785 session_info
= system_session(ldapsrv_service
->task
->lp_ctx
);
787 stream_terminate_connection(c
, "failed to setup system "
791 ldapsrv_accept(c
, session_info
, true);
794 static const struct stream_server_ops ldap_stream_priv_ops
= {
796 .accept_connection
= ldapsrv_accept_priv
,
797 .recv_handler
= ldapsrv_recv
,
798 .send_handler
= ldapsrv_send
,
805 add a socket address to the list of events, one event per port
807 static NTSTATUS
add_socket(struct task_server
*task
,
808 struct loadparm_context
*lp_ctx
,
809 const struct model_ops
*model_ops
,
810 const char *address
, struct ldapsrv_service
*ldap_service
)
814 struct ldb_context
*ldb
;
816 status
= stream_setup_socket(task
, task
->event_ctx
, lp_ctx
,
817 model_ops
, &ldap_stream_nonpriv_ops
,
818 "ip", address
, &port
,
819 lpcfg_socket_options(lp_ctx
),
821 if (!NT_STATUS_IS_OK(status
)) {
822 DEBUG(0,("ldapsrv failed to bind to %s:%u - %s\n",
823 address
, port
, nt_errstr(status
)));
827 if (tstream_tls_params_enabled(ldap_service
->tls_params
)) {
828 /* add ldaps server */
830 status
= stream_setup_socket(task
, task
->event_ctx
, lp_ctx
,
832 &ldap_stream_nonpriv_ops
,
833 "ip", address
, &port
,
834 lpcfg_socket_options(lp_ctx
),
836 if (!NT_STATUS_IS_OK(status
)) {
837 DEBUG(0,("ldapsrv failed to bind to %s:%u - %s\n",
838 address
, port
, nt_errstr(status
)));
843 /* Load LDAP database, but only to read our settings */
844 ldb
= samdb_connect(ldap_service
, ldap_service
->task
->event_ctx
,
845 lp_ctx
, system_session(lp_ctx
), 0);
847 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
850 if (samdb_is_gc(ldb
)) {
852 status
= stream_setup_socket(task
, task
->event_ctx
, lp_ctx
,
854 &ldap_stream_nonpriv_ops
,
855 "ip", address
, &port
,
856 lpcfg_socket_options(lp_ctx
),
858 if (!NT_STATUS_IS_OK(status
)) {
859 DEBUG(0,("ldapsrv failed to bind to %s:%u - %s\n",
860 address
, port
, nt_errstr(status
)));
863 if (tstream_tls_params_enabled(ldap_service
->tls_params
)) {
864 /* add ldaps server for the global catalog */
866 status
= stream_setup_socket(task
, task
->event_ctx
, lp_ctx
,
868 &ldap_stream_nonpriv_ops
,
869 "ip", address
, &port
,
870 lpcfg_socket_options(lp_ctx
),
872 if (!NT_STATUS_IS_OK(status
)) {
873 DEBUG(0,("ldapsrv failed to bind to %s:%u - %s\n",
874 address
, port
, nt_errstr(status
)));
880 /* And once we are bound, free the temporary ldb, it will
881 * connect again on each incoming LDAP connection */
882 talloc_unlink(ldap_service
, ldb
);
888 open the ldap server sockets
890 static void ldapsrv_task_init(struct task_server
*task
)
893 #ifdef WITH_LDAPI_PRIV_SOCKET
896 const char *dns_host_name
;
897 struct ldapsrv_service
*ldap_service
;
899 const struct model_ops
*model_ops
;
901 switch (lpcfg_server_role(task
->lp_ctx
)) {
902 case ROLE_STANDALONE
:
903 task_server_terminate(task
, "ldap_server: no LDAP server required in standalone configuration",
906 case ROLE_DOMAIN_MEMBER
:
907 task_server_terminate(task
, "ldap_server: no LDAP server required in member server configuration",
910 case ROLE_ACTIVE_DIRECTORY_DC
:
911 /* Yes, we want an LDAP server */
915 task_server_set_title(task
, "task[ldapsrv]");
917 /* run the ldap server as a single process */
918 model_ops
= process_model_startup("single");
919 if (!model_ops
) goto failed
;
921 ldap_service
= talloc_zero(task
, struct ldapsrv_service
);
922 if (ldap_service
== NULL
) goto failed
;
924 ldap_service
->task
= task
;
926 dns_host_name
= talloc_asprintf(ldap_service
, "%s.%s",
927 lpcfg_netbios_name(task
->lp_ctx
),
928 lpcfg_dnsdomain(task
->lp_ctx
));
929 if (dns_host_name
== NULL
) goto failed
;
931 status
= tstream_tls_params_server(ldap_service
,
933 lpcfg_tls_enabled(task
->lp_ctx
),
934 lpcfg_tls_keyfile(ldap_service
, task
->lp_ctx
),
935 lpcfg_tls_certfile(ldap_service
, task
->lp_ctx
),
936 lpcfg_tls_cafile(ldap_service
, task
->lp_ctx
),
937 lpcfg_tls_crlfile(ldap_service
, task
->lp_ctx
),
938 lpcfg_tls_dhpfile(ldap_service
, task
->lp_ctx
),
939 &ldap_service
->tls_params
);
940 if (!NT_STATUS_IS_OK(status
)) {
941 DEBUG(0,("ldapsrv failed tstream_tls_params_server - %s\n",
946 ldap_service
->call_queue
= tevent_queue_create(ldap_service
, "ldapsrv_call_queue");
947 if (ldap_service
->call_queue
== NULL
) goto failed
;
949 if (lpcfg_interfaces(task
->lp_ctx
) && lpcfg_bind_interfaces_only(task
->lp_ctx
)) {
950 struct interface
*ifaces
;
954 load_interface_list(task
, task
->lp_ctx
, &ifaces
);
955 num_interfaces
= iface_list_count(ifaces
);
957 /* We have been given an interfaces line, and been
958 told to only bind to those interfaces. Create a
959 socket per interface and bind to only these.
961 for(i
= 0; i
< num_interfaces
; i
++) {
962 const char *address
= iface_list_n_ip(ifaces
, i
);
963 status
= add_socket(task
, task
->lp_ctx
, model_ops
, address
, ldap_service
);
964 if (!NT_STATUS_IS_OK(status
)) goto failed
;
969 wcard
= iface_list_wildcard(task
, task
->lp_ctx
);
971 DEBUG(0,("No wildcard addresses available\n"));
974 for (i
=0; wcard
[i
]; i
++) {
975 status
= add_socket(task
, task
->lp_ctx
, model_ops
, wcard
[i
], ldap_service
);
976 if (!NT_STATUS_IS_OK(status
)) goto failed
;
981 ldapi_path
= lpcfg_private_path(ldap_service
, task
->lp_ctx
, "ldapi");
986 status
= stream_setup_socket(task
, task
->event_ctx
, task
->lp_ctx
,
987 model_ops
, &ldap_stream_nonpriv_ops
,
988 "unix", ldapi_path
, NULL
,
989 lpcfg_socket_options(task
->lp_ctx
),
991 talloc_free(ldapi_path
);
992 if (!NT_STATUS_IS_OK(status
)) {
993 DEBUG(0,("ldapsrv failed to bind to %s - %s\n",
994 ldapi_path
, nt_errstr(status
)));
997 #ifdef WITH_LDAPI_PRIV_SOCKET
998 priv_dir
= lpcfg_private_path(ldap_service
, task
->lp_ctx
, "ldap_priv");
999 if (priv_dir
== NULL
) {
1003 * Make sure the directory for the privileged ldapi socket exists, and
1004 * is of the correct permissions
1006 if (!directory_create_or_exist(priv_dir
, geteuid(), 0750)) {
1007 task_server_terminate(task
, "Cannot create ldap "
1008 "privileged ldapi directory", true);
1011 ldapi_path
= talloc_asprintf(ldap_service
, "%s/ldapi", priv_dir
);
1012 talloc_free(priv_dir
);
1013 if (ldapi_path
== NULL
) {
1017 status
= stream_setup_socket(task
, task
->event_ctx
, task
->lp_ctx
,
1018 model_ops
, &ldap_stream_priv_ops
,
1019 "unix", ldapi_path
, NULL
,
1020 lpcfg_socket_options(task
->lp_ctx
),
1022 talloc_free(ldapi_path
);
1023 if (!NT_STATUS_IS_OK(status
)) {
1024 DEBUG(0,("ldapsrv failed to bind to %s - %s\n",
1025 ldapi_path
, nt_errstr(status
)));
1032 task_server_terminate(task
, "Failed to startup ldap server task", true);
1036 NTSTATUS
server_service_ldap_init(void)
1038 return register_server_service("ldap", ldapsrv_task_init
);