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 "samba/service_task.h"
34 #include "samba/service_stream.h"
35 #include "samba/service.h"
36 #include "samba/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"
49 #include "libds/common/roles.h"
50 #include "lib/util/time.h"
51 #include "lib/util/server_id.h"
52 #include "lib/util/server_id_db.h"
53 #include "lib/messaging/messaging_internal.h"
58 #define DBGC_CLASS DBGC_LDAPSRV
60 static void ldapsrv_terminate_connection_done(struct tevent_req
*subreq
);
63 close the socket and shutdown a server_context
65 static void ldapsrv_terminate_connection(struct ldapsrv_connection
*conn
,
68 struct tevent_req
*subreq
;
70 if (conn
->limits
.reason
) {
74 DLIST_REMOVE(conn
->service
->connections
, conn
);
76 conn
->limits
.endtime
= timeval_current_ofs(0, 500);
78 tevent_queue_stop(conn
->sockets
.send_queue
);
79 TALLOC_FREE(conn
->sockets
.read_req
);
80 TALLOC_FREE(conn
->deferred_expire_disconnect
);
81 if (conn
->active_call
) {
82 tevent_req_cancel(conn
->active_call
);
83 conn
->active_call
= NULL
;
86 conn
->limits
.reason
= talloc_strdup(conn
, reason
);
87 if (conn
->limits
.reason
== NULL
) {
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
);
95 subreq
= tstream_disconnect_send(conn
,
96 conn
->connection
->event
.ctx
,
97 conn
->sockets
.active
);
99 TALLOC_FREE(conn
->sockets
.tls
);
100 TALLOC_FREE(conn
->sockets
.sasl
);
101 TALLOC_FREE(conn
->sockets
.raw
);
102 stream_terminate_connection(conn
->connection
, reason
);
105 tevent_req_set_endtime(subreq
,
106 conn
->connection
->event
.ctx
,
107 conn
->limits
.endtime
);
108 tevent_req_set_callback(subreq
, ldapsrv_terminate_connection_done
, conn
);
111 static void ldapsrv_terminate_connection_done(struct tevent_req
*subreq
)
113 struct ldapsrv_connection
*conn
=
114 tevent_req_callback_data(subreq
,
115 struct ldapsrv_connection
);
119 tstream_disconnect_recv(subreq
, &sys_errno
);
122 if (conn
->sockets
.active
== conn
->sockets
.raw
) {
123 TALLOC_FREE(conn
->sockets
.tls
);
124 TALLOC_FREE(conn
->sockets
.sasl
);
125 TALLOC_FREE(conn
->sockets
.raw
);
126 stream_terminate_connection(conn
->connection
,
127 conn
->limits
.reason
);
131 TALLOC_FREE(conn
->sockets
.tls
);
132 TALLOC_FREE(conn
->sockets
.sasl
);
133 conn
->sockets
.active
= conn
->sockets
.raw
;
135 subreq
= tstream_disconnect_send(conn
,
136 conn
->connection
->event
.ctx
,
137 conn
->sockets
.active
);
138 if (subreq
== NULL
) {
139 TALLOC_FREE(conn
->sockets
.raw
);
140 stream_terminate_connection(conn
->connection
,
141 conn
->limits
.reason
);
144 ok
= tevent_req_set_endtime(subreq
,
145 conn
->connection
->event
.ctx
,
146 conn
->limits
.endtime
);
148 TALLOC_FREE(conn
->sockets
.raw
);
149 stream_terminate_connection(conn
->connection
,
150 conn
->limits
.reason
);
153 tevent_req_set_callback(subreq
, ldapsrv_terminate_connection_done
, conn
);
157 called when a LDAP socket becomes readable
159 void ldapsrv_recv(struct stream_connection
*c
, uint16_t flags
)
161 smb_panic(__location__
);
165 called when a LDAP socket becomes writable
167 static void ldapsrv_send(struct stream_connection
*c
, uint16_t flags
)
169 smb_panic(__location__
);
172 static int ldapsrv_load_limits(struct ldapsrv_connection
*conn
)
175 const char *attrs
[] = { "configurationNamingContext", NULL
};
176 const char *attrs2
[] = { "lDAPAdminLimits", NULL
};
177 struct ldb_message_element
*el
;
178 struct ldb_result
*res
= NULL
;
179 struct ldb_dn
*basedn
;
180 struct ldb_dn
*conf_dn
;
181 struct ldb_dn
*policy_dn
;
185 /* set defaults limits in case of failure */
186 conn
->limits
.initial_timeout
= 120;
187 conn
->limits
.conn_idle_time
= 900;
188 conn
->limits
.max_page_size
= 1000;
189 conn
->limits
.max_notifications
= 5;
190 conn
->limits
.search_timeout
= 120;
191 conn
->limits
.expire_time
= (struct timeval
) {
192 .tv_sec
= get_time_t_max(),
196 tmp_ctx
= talloc_new(conn
);
197 if (tmp_ctx
== NULL
) {
201 basedn
= ldb_dn_new(tmp_ctx
, conn
->ldb
, NULL
);
202 if (basedn
== NULL
) {
206 ret
= ldb_search(conn
->ldb
, tmp_ctx
, &res
, basedn
, LDB_SCOPE_BASE
, attrs
, NULL
);
207 if (ret
!= LDB_SUCCESS
) {
211 if (res
->count
!= 1) {
215 conf_dn
= ldb_msg_find_attr_as_dn(conn
->ldb
, tmp_ctx
, res
->msgs
[0], "configurationNamingContext");
216 if (conf_dn
== NULL
) {
220 policy_dn
= ldb_dn_copy(tmp_ctx
, conf_dn
);
221 ldb_dn_add_child_fmt(policy_dn
, "CN=Default Query Policy,CN=Query-Policies,CN=Directory Service,CN=Windows NT,CN=Services");
222 if (policy_dn
== NULL
) {
226 ret
= ldb_search(conn
->ldb
, tmp_ctx
, &res
, policy_dn
, LDB_SCOPE_BASE
, attrs2
, NULL
);
227 if (ret
!= LDB_SUCCESS
) {
231 if (res
->count
!= 1) {
235 el
= ldb_msg_find_element(res
->msgs
[0], "lDAPAdminLimits");
240 for (i
= 0; i
< el
->num_values
; i
++) {
241 char policy_name
[256];
244 s
= sscanf((const char *)el
->values
[i
].data
, "%255[^=]=%d", policy_name
, &policy_value
);
245 if (s
!= 2 || policy_value
== 0)
247 if (strcasecmp("InitRecvTimeout", policy_name
) == 0) {
248 conn
->limits
.initial_timeout
= policy_value
;
251 if (strcasecmp("MaxConnIdleTime", policy_name
) == 0) {
252 conn
->limits
.conn_idle_time
= policy_value
;
255 if (strcasecmp("MaxPageSize", policy_name
) == 0) {
256 conn
->limits
.max_page_size
= policy_value
;
259 if (strcasecmp("MaxNotificationPerConn", policy_name
) == 0) {
260 conn
->limits
.max_notifications
= policy_value
;
263 if (strcasecmp("MaxQueryDuration", policy_name
) == 0) {
264 if (policy_value
> 0) {
265 conn
->limits
.search_timeout
= policy_value
;
274 DBG_ERR("Failed to load ldap server query policies\n");
275 talloc_free(tmp_ctx
);
279 static int ldapsrv_call_destructor(struct ldapsrv_call
*call
)
281 if (call
->conn
== NULL
) {
285 DLIST_REMOVE(call
->conn
->pending_calls
, call
);
291 static struct tevent_req
*ldapsrv_process_call_send(TALLOC_CTX
*mem_ctx
,
292 struct tevent_context
*ev
,
293 struct tevent_queue
*call_queue
,
294 struct ldapsrv_call
*call
);
295 static NTSTATUS
ldapsrv_process_call_recv(struct tevent_req
*req
);
297 static bool ldapsrv_call_read_next(struct ldapsrv_connection
*conn
);
298 static void ldapsrv_accept_tls_done(struct tevent_req
*subreq
);
301 initialise a server_context from a open socket and register a event handler
302 for reading from that socket
304 static void ldapsrv_accept(struct stream_connection
*c
,
305 struct auth_session_info
*session_info
,
309 struct ldapsrv_service
*ldapsrv_service
=
310 talloc_get_type(c
->private_data
, struct ldapsrv_service
);
311 struct ldapsrv_connection
*conn
;
312 struct cli_credentials
*server_credentials
;
313 struct socket_address
*socket_address
;
316 struct tevent_req
*subreq
;
317 struct timeval endtime
;
318 char *errstring
= NULL
;
320 conn
= talloc_zero(c
, struct ldapsrv_connection
);
322 stream_terminate_connection(c
, "ldapsrv_accept: out of memory");
325 conn
->is_privileged
= is_privileged
;
326 conn
->is_ldapi
= is_ldapi
;
328 conn
->sockets
.send_queue
= tevent_queue_create(conn
, "ldapsrv send queue");
329 if (conn
->sockets
.send_queue
== NULL
) {
330 stream_terminate_connection(c
,
331 "ldapsrv_accept: tevent_queue_create failed");
335 TALLOC_FREE(c
->event
.fde
);
337 ret
= tstream_bsd_existing_socket(conn
,
338 socket_get_fd(c
->socket
),
341 stream_terminate_connection(c
,
342 "ldapsrv_accept: out of memory");
345 socket_set_flags(c
->socket
, SOCKET_FLAG_NOCLOSE
);
346 /* as server we want to fail early */
347 tstream_bsd_fail_readv_first_error(conn
->sockets
.raw
, true);
349 conn
->connection
= c
;
350 conn
->service
= ldapsrv_service
;
351 conn
->lp_ctx
= ldapsrv_service
->lp_ctx
;
353 c
->private_data
= conn
;
355 socket_address
= socket_get_my_addr(c
->socket
, conn
);
356 if (!socket_address
) {
357 ldapsrv_terminate_connection(conn
, "ldapsrv_accept: failed to obtain local socket address!");
360 port
= socket_address
->port
;
361 talloc_free(socket_address
);
362 if (port
== 3268 || port
== 3269) /* Global catalog */ {
363 conn
->global_catalog
= true;
366 server_credentials
= cli_credentials_init_server(conn
, conn
->lp_ctx
);
367 if (!server_credentials
) {
368 stream_terminate_connection(c
, "Failed to init server credentials\n");
372 conn
->server_credentials
= server_credentials
;
374 conn
->session_info
= session_info
;
376 conn
->sockets
.active
= conn
->sockets
.raw
;
378 if (conn
->is_privileged
) {
379 conn
->require_strong_auth
= LDAP_SERVER_REQUIRE_STRONG_AUTH_NO
;
381 conn
->require_strong_auth
= lpcfg_ldap_server_require_strong_auth(conn
->lp_ctx
);
384 if (conn
->require_strong_auth
==
385 LDAP_SERVER_REQUIRE_STRONG_AUTH_ALLOW_SASL_OVER_TLS
)
387 D_ERR("WARNING: You have not configured "
388 "'ldap server require strong auth = "
389 "allow_sasl_over_tls'.\n"
390 "Please change to 'yes' (preferred and default) or "
391 "'allow_sasl_without_tls_channel_bindings' "
392 "(if really needed)\n\n");
395 ret
= ldapsrv_backend_Init(conn
, &errstring
);
396 if (ret
!= LDB_SUCCESS
) {
397 char *reason
= talloc_asprintf(conn
,
398 "LDB backend for LDAP Init "
400 errstring
, ldb_strerror(ret
));
401 ldapsrv_terminate_connection(conn
, reason
);
405 /* load limits from the conf partition */
406 ldapsrv_load_limits(conn
); /* should we fail on error ? */
408 /* register the server */
409 irpc_add_name(c
->msg_ctx
, "ldap_server");
411 DLIST_ADD_END(ldapsrv_service
->connections
, conn
);
413 if (port
!= 636 && port
!= 3269) {
414 ldapsrv_call_read_next(conn
);
418 endtime
= timeval_current_ofs(conn
->limits
.conn_idle_time
, 0);
420 subreq
= tstream_tls_accept_send(conn
,
421 conn
->connection
->event
.ctx
,
423 conn
->service
->tls_params
);
424 if (subreq
== NULL
) {
425 ldapsrv_terminate_connection(conn
, "ldapsrv_accept: "
426 "no memory for tstream_tls_accept_send");
429 tevent_req_set_endtime(subreq
,
430 conn
->connection
->event
.ctx
,
432 tevent_req_set_callback(subreq
, ldapsrv_accept_tls_done
, conn
);
435 static void ldapsrv_accept_tls_done(struct tevent_req
*subreq
)
437 struct ldapsrv_connection
*conn
=
438 tevent_req_callback_data(subreq
,
439 struct ldapsrv_connection
);
443 ret
= tstream_tls_accept_recv(subreq
, &sys_errno
,
444 conn
, &conn
->sockets
.tls
);
449 reason
= talloc_asprintf(conn
, "ldapsrv_accept_tls_loop: "
450 "tstream_tls_accept_recv() - %d:%s",
451 sys_errno
, strerror(sys_errno
));
453 reason
= "ldapsrv_accept_tls_loop: "
454 "tstream_tls_accept_recv() - failed";
457 ldapsrv_terminate_connection(conn
, reason
);
461 conn
->sockets
.active
= conn
->sockets
.tls
;
462 conn
->referral_scheme
= LDAP_REFERRAL_SCHEME_LDAPS
;
463 ldapsrv_call_read_next(conn
);
466 static void ldapsrv_call_read_done(struct tevent_req
*subreq
);
467 static NTSTATUS
ldapsrv_packet_check(
468 struct tstream_context
*stream
,
471 size_t *packet_size
);
473 static bool ldapsrv_call_read_next(struct ldapsrv_connection
*conn
)
475 struct tevent_req
*subreq
;
477 if (conn
->pending_calls
!= NULL
) {
478 conn
->limits
.endtime
= timeval_zero();
480 ldapsrv_notification_retry_setup(conn
->service
, false);
481 } else if (timeval_is_zero(&conn
->limits
.endtime
)) {
482 conn
->limits
.endtime
=
483 timeval_current_ofs(conn
->limits
.initial_timeout
, 0);
485 conn
->limits
.endtime
=
486 timeval_current_ofs(conn
->limits
.conn_idle_time
, 0);
489 if (conn
->sockets
.read_req
!= NULL
) {
494 * The minimum size of a LDAP pdu is 7 bytes
496 * dumpasn1 -hh ldap-unbind-min.dat
498 * <30 05 02 01 09 42 00>
503 * 5 0: [APPLICATION 2]
504 * : Error: Object has zero length.
507 * dumpasn1 -hh ldap-unbind-windows.dat
509 * <30 84 00 00 00 05 02 01 09 42 00>
514 * 9 0: [APPLICATION 2]
515 * : Error: Object has zero length.
518 * This means using an initial read size
521 subreq
= tstream_read_pdu_blob_send(conn
,
522 conn
->connection
->event
.ctx
,
523 conn
->sockets
.active
,
524 7, /* initial_read_size */
525 ldapsrv_packet_check
,
527 if (subreq
== NULL
) {
528 ldapsrv_terminate_connection(conn
, "ldapsrv_call_read_next: "
529 "no memory for tstream_read_pdu_blob_send");
532 if (!timeval_is_zero(&conn
->limits
.endtime
)) {
534 ok
= tevent_req_set_endtime(subreq
,
535 conn
->connection
->event
.ctx
,
536 conn
->limits
.endtime
);
538 ldapsrv_terminate_connection(
540 "ldapsrv_call_read_next: "
541 "no memory for tevent_req_set_endtime");
545 tevent_req_set_callback(subreq
, ldapsrv_call_read_done
, conn
);
546 conn
->sockets
.read_req
= subreq
;
550 static void ldapsrv_call_process_done(struct tevent_req
*subreq
);
551 static int ldapsrv_check_packet_size(
552 struct ldapsrv_connection
*conn
,
555 static void ldapsrv_call_read_done(struct tevent_req
*subreq
)
557 struct ldapsrv_connection
*conn
=
558 tevent_req_callback_data(subreq
,
559 struct ldapsrv_connection
);
561 struct ldapsrv_call
*call
;
562 struct asn1_data
*asn1
;
564 int ret
= LDAP_SUCCESS
;
565 struct ldap_request_limits limits
= {0};
567 conn
->sockets
.read_req
= NULL
;
569 call
= talloc_zero(conn
, struct ldapsrv_call
);
571 ldapsrv_terminate_connection(conn
, "no memory");
574 talloc_set_destructor(call
, ldapsrv_call_destructor
);
578 status
= tstream_read_pdu_blob_recv(subreq
,
582 if (!NT_STATUS_IS_OK(status
)) {
585 reason
= talloc_asprintf(call
, "ldapsrv_call_loop: "
586 "tstream_read_pdu_blob_recv() - %s",
589 reason
= nt_errstr(status
);
592 ldapsrv_terminate_connection(conn
, reason
);
596 ret
= ldapsrv_check_packet_size(conn
, blob
.length
);
597 if (ret
!= LDAP_SUCCESS
) {
598 ldapsrv_terminate_connection(
600 "Request packet too large");
604 asn1
= asn1_init(call
, ASN1_MAX_TREE_DEPTH
);
606 ldapsrv_terminate_connection(conn
, "no memory");
610 call
->request
= talloc(call
, struct ldap_message
);
611 if (call
->request
== NULL
) {
612 ldapsrv_terminate_connection(conn
, "no memory");
616 asn1_load_nocopy(asn1
, blob
.data
, blob
.length
);
618 limits
.max_search_size
=
619 lpcfg_ldap_max_search_request_size(conn
->lp_ctx
);
620 status
= ldap_decode(
623 samba_ldap_control_handlers(),
625 if (!NT_STATUS_IS_OK(status
)) {
626 ldapsrv_terminate_connection(conn
, nt_errstr(status
));
630 data_blob_free(&blob
);
634 /* queue the call in the global queue */
635 subreq
= ldapsrv_process_call_send(call
,
636 conn
->connection
->event
.ctx
,
637 conn
->service
->call_queue
,
639 if (subreq
== NULL
) {
640 ldapsrv_terminate_connection(conn
, "ldapsrv_process_call_send failed");
643 tevent_req_set_callback(subreq
, ldapsrv_call_process_done
, call
);
644 conn
->active_call
= subreq
;
647 static void ldapsrv_call_wait_done(struct tevent_req
*subreq
);
648 static void ldapsrv_call_writev_start(struct ldapsrv_call
*call
);
649 static void ldapsrv_call_writev_done(struct tevent_req
*subreq
);
651 static void ldapsrv_call_process_done(struct tevent_req
*subreq
)
653 struct ldapsrv_call
*call
=
654 tevent_req_callback_data(subreq
,
655 struct ldapsrv_call
);
656 struct ldapsrv_connection
*conn
= call
->conn
;
659 conn
->active_call
= NULL
;
661 status
= ldapsrv_process_call_recv(subreq
);
663 if (!NT_STATUS_IS_OK(status
)) {
664 ldapsrv_terminate_connection(conn
, nt_errstr(status
));
668 if (call
->wait_send
!= NULL
) {
669 subreq
= call
->wait_send(call
,
670 conn
->connection
->event
.ctx
,
672 if (subreq
== NULL
) {
673 ldapsrv_terminate_connection(conn
,
674 "ldapsrv_call_process_done: "
675 "call->wait_send - no memory");
678 tevent_req_set_callback(subreq
,
679 ldapsrv_call_wait_done
,
681 conn
->active_call
= subreq
;
685 ldapsrv_call_writev_start(call
);
688 static void ldapsrv_call_wait_done(struct tevent_req
*subreq
)
690 struct ldapsrv_call
*call
=
691 tevent_req_callback_data(subreq
,
692 struct ldapsrv_call
);
693 struct ldapsrv_connection
*conn
= call
->conn
;
696 conn
->active_call
= NULL
;
698 status
= call
->wait_recv(subreq
);
700 if (!NT_STATUS_IS_OK(status
)) {
703 reason
= talloc_asprintf(call
, "ldapsrv_call_wait_done: "
704 "call->wait_recv() - %s",
706 if (reason
== NULL
) {
707 reason
= nt_errstr(status
);
710 ldapsrv_terminate_connection(conn
, reason
);
714 ldapsrv_call_writev_start(call
);
717 static void ldapsrv_call_writev_start(struct ldapsrv_call
*call
)
719 struct ldapsrv_connection
*conn
= call
->conn
;
720 struct ldapsrv_reply
*reply
= NULL
;
721 struct tevent_req
*subreq
= NULL
;
722 struct timeval endtime
;
728 /* build all the replies into an IOV (no copy) */
729 for (reply
= call
->replies
;
731 reply
= reply
->next
) {
733 /* Cap output at 25MB per writev() */
734 if (length
> length
+ reply
->blob
.length
735 || length
+ reply
->blob
.length
> LDAP_SERVER_MAX_CHUNK_SIZE
) {
740 * Overflow is harmless here, just used below to
741 * decide if to read or write, but checked above anyway
743 length
+= reply
->blob
.length
;
746 * At worst an overflow would mean we send less
753 if (!call
->notification
.busy
) {
757 ldapsrv_call_read_next(conn
);
761 /* Cap call->iov_count at IOV_MAX */
762 call
->iov_count
= MIN(call
->iov_count
, IOV_MAX
);
764 call
->out_iov
= talloc_array(call
,
767 if (!call
->out_iov
) {
768 /* This is not ideal */
769 ldapsrv_terminate_connection(conn
,
770 "failed to allocate "
775 /* We may have had to cap the number of replies at IOV_MAX */
777 i
< call
->iov_count
&& call
->replies
!= NULL
;
779 reply
= call
->replies
;
780 call
->out_iov
[i
].iov_base
= reply
->blob
.data
;
781 call
->out_iov
[i
].iov_len
= reply
->blob
.length
;
783 /* Keep only the ASN.1 encoded data */
784 talloc_steal(call
->out_iov
, reply
->blob
.data
);
786 DLIST_REMOVE(call
->replies
, reply
);
790 if (i
> call
->iov_count
) {
791 /* This is not ideal, but also (essentially) impossible */
792 ldapsrv_terminate_connection(conn
,
798 subreq
= tstream_writev_queue_send(call
,
799 conn
->connection
->event
.ctx
,
800 conn
->sockets
.active
,
801 conn
->sockets
.send_queue
,
802 call
->out_iov
, call
->iov_count
);
803 if (subreq
== NULL
) {
804 ldapsrv_terminate_connection(conn
, "stream_writev_queue_send failed");
807 endtime
= timeval_current_ofs(conn
->limits
.conn_idle_time
, 0);
808 tevent_req_set_endtime(subreq
,
809 conn
->connection
->event
.ctx
,
811 tevent_req_set_callback(subreq
, ldapsrv_call_writev_done
, call
);
814 static void ldapsrv_call_postprocess_done(struct tevent_req
*subreq
);
816 static void ldapsrv_call_writev_done(struct tevent_req
*subreq
)
818 struct ldapsrv_call
*call
=
819 tevent_req_callback_data(subreq
,
820 struct ldapsrv_call
);
821 struct ldapsrv_connection
*conn
= call
->conn
;
825 rc
= tstream_writev_queue_recv(subreq
, &sys_errno
);
828 /* This releases the ASN.1 encoded packets from memory */
829 TALLOC_FREE(call
->out_iov
);
833 reason
= talloc_asprintf(call
, "ldapsrv_call_writev_done: "
834 "tstream_writev_queue_recv() - %d:%s",
835 sys_errno
, strerror(sys_errno
));
836 if (reason
== NULL
) {
837 reason
= "ldapsrv_call_writev_done: "
838 "tstream_writev_queue_recv() failed";
841 ldapsrv_terminate_connection(conn
, reason
);
845 if (call
->postprocess_send
) {
846 subreq
= call
->postprocess_send(call
,
847 conn
->connection
->event
.ctx
,
848 call
->postprocess_private
);
849 if (subreq
== NULL
) {
850 ldapsrv_terminate_connection(conn
, "ldapsrv_call_writev_done: "
851 "call->postprocess_send - no memory");
854 tevent_req_set_callback(subreq
,
855 ldapsrv_call_postprocess_done
,
860 /* Perhaps still some more to send */
861 if (call
->replies
!= NULL
) {
862 ldapsrv_call_writev_start(call
);
866 if (!call
->notification
.busy
) {
870 ldapsrv_call_read_next(conn
);
873 static void ldapsrv_call_postprocess_done(struct tevent_req
*subreq
)
875 struct ldapsrv_call
*call
=
876 tevent_req_callback_data(subreq
,
877 struct ldapsrv_call
);
878 struct ldapsrv_connection
*conn
= call
->conn
;
881 status
= call
->postprocess_recv(subreq
);
883 if (!NT_STATUS_IS_OK(status
)) {
886 reason
= talloc_asprintf(call
, "ldapsrv_call_postprocess_done: "
887 "call->postprocess_recv() - %s",
889 if (reason
== NULL
) {
890 reason
= nt_errstr(status
);
893 ldapsrv_terminate_connection(conn
, reason
);
899 ldapsrv_call_read_next(conn
);
902 static void ldapsrv_notification_retry_done(struct tevent_req
*subreq
);
904 void ldapsrv_notification_retry_setup(struct ldapsrv_service
*service
, bool force
)
906 struct ldapsrv_connection
*conn
= NULL
;
907 struct timeval retry
;
908 size_t num_pending
= 0;
909 size_t num_active
= 0;
912 TALLOC_FREE(service
->notification
.retry
);
913 service
->notification
.generation
+= 1;
916 if (service
->notification
.retry
!= NULL
) {
920 for (conn
= service
->connections
; conn
!= NULL
; conn
= conn
->next
) {
921 if (conn
->pending_calls
== NULL
) {
927 if (conn
->pending_calls
->notification
.generation
!=
928 service
->notification
.generation
)
934 if (num_pending
== 0) {
938 if (num_active
!= 0) {
939 retry
= timeval_current_ofs(0, 100);
941 retry
= timeval_current_ofs(5, 0);
944 service
->notification
.retry
= tevent_wakeup_send(service
,
947 if (service
->notification
.retry
== NULL
) {
952 tevent_req_set_callback(service
->notification
.retry
,
953 ldapsrv_notification_retry_done
,
957 static void ldapsrv_notification_retry_done(struct tevent_req
*subreq
)
959 struct ldapsrv_service
*service
=
960 tevent_req_callback_data(subreq
,
961 struct ldapsrv_service
);
962 struct ldapsrv_connection
*conn
= NULL
;
963 struct ldapsrv_connection
*conn_next
= NULL
;
966 service
->notification
.retry
= NULL
;
968 ok
= tevent_wakeup_recv(subreq
);
974 for (conn
= service
->connections
; conn
!= NULL
; conn
= conn_next
) {
975 struct ldapsrv_call
*call
= conn
->pending_calls
;
977 conn_next
= conn
->next
;
979 if (conn
->pending_calls
== NULL
) {
983 if (conn
->active_call
!= NULL
) {
987 DLIST_DEMOTE(conn
->pending_calls
, call
);
988 call
->notification
.generation
=
989 service
->notification
.generation
;
991 /* queue the call in the global queue */
992 subreq
= ldapsrv_process_call_send(call
,
993 conn
->connection
->event
.ctx
,
994 conn
->service
->call_queue
,
996 if (subreq
== NULL
) {
997 ldapsrv_terminate_connection(conn
,
998 "ldapsrv_process_call_send failed");
1001 tevent_req_set_callback(subreq
, ldapsrv_call_process_done
, call
);
1002 conn
->active_call
= subreq
;
1005 ldapsrv_notification_retry_setup(service
, false);
1008 struct ldapsrv_process_call_state
{
1009 struct ldapsrv_call
*call
;
1012 static void ldapsrv_process_call_trigger(struct tevent_req
*req
,
1013 void *private_data
);
1015 static struct tevent_req
*ldapsrv_process_call_send(TALLOC_CTX
*mem_ctx
,
1016 struct tevent_context
*ev
,
1017 struct tevent_queue
*call_queue
,
1018 struct ldapsrv_call
*call
)
1020 struct tevent_req
*req
;
1021 struct ldapsrv_process_call_state
*state
;
1024 req
= tevent_req_create(mem_ctx
, &state
,
1025 struct ldapsrv_process_call_state
);
1032 ok
= tevent_queue_add(call_queue
, ev
, req
,
1033 ldapsrv_process_call_trigger
, NULL
);
1035 tevent_req_oom(req
);
1036 return tevent_req_post(req
, ev
);
1042 static void ldapsrv_disconnect_ticket_expired(struct tevent_req
*subreq
);
1044 static void ldapsrv_process_call_trigger(struct tevent_req
*req
,
1047 struct ldapsrv_process_call_state
*state
=
1048 tevent_req_data(req
,
1049 struct ldapsrv_process_call_state
);
1050 struct ldapsrv_connection
*conn
= state
->call
->conn
;
1053 if (conn
->deferred_expire_disconnect
!= NULL
) {
1055 * Just drop this on the floor
1057 tevent_req_done(req
);
1062 status
= ldapsrv_do_call(state
->call
);
1064 if (NT_STATUS_EQUAL(status
, NT_STATUS_NETWORK_SESSION_EXPIRED
)) {
1066 * For testing purposes, defer the TCP disconnect
1067 * after having sent the msgid 0
1068 * 1.3.6.1.4.1.1466.20036 exop response. LDAP clients
1069 * should not wait for the TCP connection to close but
1070 * handle this packet equivalent to a TCP
1071 * disconnect. This delay enables testing both cases
1072 * in LDAP client libraries.
1075 int defer_msec
= lpcfg_parm_int(
1079 "delay_expire_disconnect",
1082 conn
->deferred_expire_disconnect
= tevent_wakeup_send(
1084 conn
->connection
->event
.ctx
,
1085 timeval_current_ofs_msec(defer_msec
));
1086 if (tevent_req_nomem(conn
->deferred_expire_disconnect
, req
)) {
1089 tevent_req_set_callback(
1090 conn
->deferred_expire_disconnect
,
1091 ldapsrv_disconnect_ticket_expired
,
1094 tevent_req_done(req
);
1098 if (!NT_STATUS_IS_OK(status
)) {
1099 tevent_req_nterror(req
, status
);
1103 tevent_req_done(req
);
1106 static void ldapsrv_disconnect_ticket_expired(struct tevent_req
*subreq
)
1108 struct ldapsrv_connection
*conn
= tevent_req_callback_data(
1109 subreq
, struct ldapsrv_connection
);
1112 ok
= tevent_wakeup_recv(subreq
);
1113 TALLOC_FREE(subreq
);
1115 DBG_WARNING("tevent_wakeup_recv failed\n");
1117 conn
->deferred_expire_disconnect
= NULL
;
1118 ldapsrv_terminate_connection(conn
, "network session expired");
1121 static NTSTATUS
ldapsrv_process_call_recv(struct tevent_req
*req
)
1125 if (tevent_req_is_nterror(req
, &status
)) {
1126 tevent_req_received(req
);
1130 tevent_req_received(req
);
1131 return NT_STATUS_OK
;
1134 static void ldapsrv_accept_nonpriv(struct stream_connection
*c
)
1136 struct ldapsrv_service
*ldapsrv_service
= talloc_get_type_abort(
1137 c
->private_data
, struct ldapsrv_service
);
1138 struct auth_session_info
*session_info
;
1141 status
= auth_anonymous_session_info(
1142 c
, ldapsrv_service
->lp_ctx
, &session_info
);
1143 if (!NT_STATUS_IS_OK(status
)) {
1144 stream_terminate_connection(c
, "failed to setup anonymous "
1148 ldapsrv_accept(c
, session_info
, false, false);
1151 static const struct stream_server_ops ldap_stream_nonpriv_ops
= {
1153 .accept_connection
= ldapsrv_accept_nonpriv
,
1154 .recv_handler
= ldapsrv_recv
,
1155 .send_handler
= ldapsrv_send
,
1158 static void ldapsrv_accept_nonpriv_ldapi(struct stream_connection
*c
)
1160 struct ldapsrv_service
*ldapsrv_service
= talloc_get_type_abort(
1161 c
->private_data
, struct ldapsrv_service
);
1162 struct auth_session_info
*session_info
;
1165 status
= auth_anonymous_session_info(
1166 c
, ldapsrv_service
->lp_ctx
, &session_info
);
1167 if (!NT_STATUS_IS_OK(status
)) {
1168 stream_terminate_connection(c
, "failed to setup anonymous "
1172 ldapsrv_accept(c
, session_info
, false, true);
1175 static const struct stream_server_ops ldapi_stream_nonpriv_ops
= {
1177 .accept_connection
= ldapsrv_accept_nonpriv_ldapi
,
1178 .recv_handler
= ldapsrv_recv
,
1179 .send_handler
= ldapsrv_send
,
1182 /* The feature removed behind an #ifdef until we can do it properly
1183 * with an EXTERNAL bind. */
1185 #define WITH_LDAPI_PRIV_SOCKET
1187 #ifdef WITH_LDAPI_PRIV_SOCKET
1188 static void ldapsrv_accept_priv_ldapi(struct stream_connection
*c
)
1190 struct ldapsrv_service
*ldapsrv_service
= talloc_get_type_abort(
1191 c
->private_data
, struct ldapsrv_service
);
1192 struct auth_session_info
*session_info
;
1194 session_info
= system_session(ldapsrv_service
->lp_ctx
);
1195 if (!session_info
) {
1196 stream_terminate_connection(c
, "failed to setup system "
1200 ldapsrv_accept(c
, session_info
, true, true);
1203 static const struct stream_server_ops ldapi_stream_priv_ops
= {
1205 .accept_connection
= ldapsrv_accept_priv_ldapi
,
1206 .recv_handler
= ldapsrv_recv
,
1207 .send_handler
= ldapsrv_send
,
1214 add a socket address to the list of events, one event per port
1216 static NTSTATUS
add_socket(struct task_server
*task
,
1217 struct loadparm_context
*lp_ctx
,
1218 const struct model_ops
*model_ops
,
1219 const char *address
, struct ldapsrv_service
*ldap_service
)
1221 uint16_t port
= 389;
1223 struct ldb_context
*ldb
;
1225 status
= stream_setup_socket(task
, task
->event_ctx
, lp_ctx
,
1226 model_ops
, &ldap_stream_nonpriv_ops
,
1227 "ip", address
, &port
,
1228 lpcfg_socket_options(lp_ctx
),
1229 ldap_service
, task
->process_context
);
1230 if (!NT_STATUS_IS_OK(status
)) {
1231 DBG_ERR("ldapsrv failed to bind to %s:%u - %s\n",
1232 address
, port
, nt_errstr(status
));
1236 if (tstream_tls_params_enabled(ldap_service
->tls_params
)) {
1237 /* add ldaps server */
1239 status
= stream_setup_socket(task
, task
->event_ctx
, lp_ctx
,
1241 &ldap_stream_nonpriv_ops
,
1242 "ip", address
, &port
,
1243 lpcfg_socket_options(lp_ctx
),
1245 task
->process_context
);
1246 if (!NT_STATUS_IS_OK(status
)) {
1247 DBG_ERR("ldapsrv failed to bind to %s:%u - %s\n",
1248 address
, port
, nt_errstr(status
));
1253 /* Load LDAP database, but only to read our settings */
1254 ldb
= samdb_connect(ldap_service
,
1255 ldap_service
->current_ev
,
1257 system_session(lp_ctx
),
1261 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
1264 if (samdb_is_gc(ldb
)) {
1266 status
= stream_setup_socket(task
, task
->event_ctx
, lp_ctx
,
1268 &ldap_stream_nonpriv_ops
,
1269 "ip", address
, &port
,
1270 lpcfg_socket_options(lp_ctx
),
1272 task
->process_context
);
1273 if (!NT_STATUS_IS_OK(status
)) {
1274 DBG_ERR("ldapsrv failed to bind to %s:%u - %s\n",
1275 address
, port
, nt_errstr(status
));
1278 if (tstream_tls_params_enabled(ldap_service
->tls_params
)) {
1279 /* add ldaps server for the global catalog */
1281 status
= stream_setup_socket(task
, task
->event_ctx
, lp_ctx
,
1283 &ldap_stream_nonpriv_ops
,
1284 "ip", address
, &port
,
1285 lpcfg_socket_options(lp_ctx
),
1287 task
->process_context
);
1288 if (!NT_STATUS_IS_OK(status
)) {
1289 DBG_ERR("ldapsrv failed to bind to %s:%u - %s\n",
1290 address
, port
, nt_errstr(status
));
1296 /* And once we are bound, free the temporary ldb, it will
1297 * connect again on each incoming LDAP connection */
1298 talloc_unlink(ldap_service
, ldb
);
1300 return NT_STATUS_OK
;
1303 static void ldap_reload_certs(struct imessaging_context
*msg_ctx
,
1306 struct server_id server_id
,
1311 TALLOC_CTX
*frame
= talloc_stackframe();
1312 struct ldapsrv_service
*ldap_service
=
1313 talloc_get_type_abort(private_data
,
1314 struct ldapsrv_service
);
1315 int default_children
;
1319 struct server_id ldap_master_id
;
1321 struct tstream_tls_params
*new_tls_params
= NULL
;
1323 SMB_ASSERT(msg_ctx
== ldap_service
->current_msg
);
1325 /* reload certificates */
1326 status
= tstream_tls_params_server(ldap_service
,
1327 ldap_service
->dns_host_name
,
1328 lpcfg_tls_enabled(ldap_service
->lp_ctx
),
1329 lpcfg_tls_keyfile(frame
, ldap_service
->lp_ctx
),
1330 lpcfg_tls_certfile(frame
, ldap_service
->lp_ctx
),
1331 lpcfg_tls_cafile(frame
, ldap_service
->lp_ctx
),
1332 lpcfg_tls_crlfile(frame
, ldap_service
->lp_ctx
),
1333 lpcfg_tls_dhpfile(frame
, ldap_service
->lp_ctx
),
1334 lpcfg_tls_priority(ldap_service
->lp_ctx
),
1336 if (!NT_STATUS_IS_OK(status
)) {
1337 DBG_ERR("ldapsrv failed tstream_tls_params_server - %s\n",
1343 TALLOC_FREE(ldap_service
->tls_params
);
1344 ldap_service
->tls_params
= new_tls_params
;
1346 if (getpid() != ldap_service
->parent_pid
) {
1348 * If we are not the master process we are done
1355 * Check we're running under the prefork model,
1356 * by checking if the prefork-master-ldap name
1359 ok
= server_id_db_lookup_one(msg_ctx
->names
, "prefork-master-ldap", &ldap_master_id
);
1362 * We are done if another process model is in use.
1369 * Now we loop over all possible prefork workers
1370 * in order to notify them about the reload
1372 default_children
= lpcfg_prefork_children(ldap_service
->lp_ctx
);
1373 num_children
= lpcfg_parm_int(ldap_service
->lp_ctx
,
1374 NULL
, "prefork children", "ldap",
1376 for (i
= 0; i
< num_children
; i
++) {
1377 char child_name
[64] = { 0, };
1378 struct server_id ldap_worker_id
;
1380 snprintf(child_name
, sizeof(child_name
), "prefork-worker-ldap-%d", i
);
1381 ok
= server_id_db_lookup_one(msg_ctx
->names
, child_name
, &ldap_worker_id
);
1383 DBG_ERR("server_id_db_lookup_one(%s) - failed\n",
1388 status
= imessaging_send(msg_ctx
, ldap_worker_id
,
1389 MSG_RELOAD_TLS_CERTIFICATES
, NULL
);
1390 if (!NT_STATUS_IS_OK(status
)) {
1391 struct server_id_buf id_buf
;
1392 DBG_ERR("ldapsrv failed imessaging_send(%s, %s) - %s\n",
1394 server_id_str_buf(ldap_worker_id
, &id_buf
),
1404 open the ldap server sockets
1406 static NTSTATUS
ldapsrv_task_init(struct task_server
*task
)
1409 #ifdef WITH_LDAPI_PRIV_SOCKET
1412 struct ldapsrv_service
*ldap_service
;
1415 switch (lpcfg_server_role(task
->lp_ctx
)) {
1416 case ROLE_STANDALONE
:
1417 task_server_terminate(task
, "ldap_server: no LDAP server required in standalone configuration",
1419 return NT_STATUS_INVALID_DOMAIN_ROLE
;
1420 case ROLE_DOMAIN_MEMBER
:
1421 task_server_terminate(task
, "ldap_server: no LDAP server required in member server configuration",
1423 return NT_STATUS_INVALID_DOMAIN_ROLE
;
1424 case ROLE_ACTIVE_DIRECTORY_DC
:
1425 /* Yes, we want an LDAP server */
1429 task_server_set_title(task
, "task[ldapsrv]");
1431 ldap_service
= talloc_zero(task
, struct ldapsrv_service
);
1432 if (ldap_service
== NULL
) {
1433 status
= NT_STATUS_NO_MEMORY
;
1437 ldap_service
->lp_ctx
= task
->lp_ctx
;
1438 ldap_service
->current_ev
= task
->event_ctx
;
1439 ldap_service
->current_msg
= task
->msg_ctx
;
1441 ldap_service
->dns_host_name
= talloc_asprintf(ldap_service
, "%s.%s",
1442 lpcfg_netbios_name(task
->lp_ctx
),
1443 lpcfg_dnsdomain(task
->lp_ctx
));
1444 if (ldap_service
->dns_host_name
== NULL
) {
1445 status
= NT_STATUS_NO_MEMORY
;
1449 ldap_service
->parent_pid
= getpid();
1451 status
= tstream_tls_params_server(ldap_service
,
1452 ldap_service
->dns_host_name
,
1453 lpcfg_tls_enabled(task
->lp_ctx
),
1454 lpcfg_tls_keyfile(ldap_service
, task
->lp_ctx
),
1455 lpcfg_tls_certfile(ldap_service
, task
->lp_ctx
),
1456 lpcfg_tls_cafile(ldap_service
, task
->lp_ctx
),
1457 lpcfg_tls_crlfile(ldap_service
, task
->lp_ctx
),
1458 lpcfg_tls_dhpfile(ldap_service
, task
->lp_ctx
),
1459 lpcfg_tls_priority(task
->lp_ctx
),
1460 &ldap_service
->tls_params
);
1461 if (!NT_STATUS_IS_OK(status
)) {
1462 DBG_ERR("ldapsrv failed tstream_tls_params_server - %s\n",
1467 ldap_service
->call_queue
= tevent_queue_create(ldap_service
, "ldapsrv_call_queue");
1468 if (ldap_service
->call_queue
== NULL
) {
1469 status
= NT_STATUS_NO_MEMORY
;
1473 if (lpcfg_interfaces(task
->lp_ctx
) && lpcfg_bind_interfaces_only(task
->lp_ctx
)) {
1474 struct interface
*ifaces
;
1478 load_interface_list(task
, task
->lp_ctx
, &ifaces
);
1479 num_interfaces
= iface_list_count(ifaces
);
1481 /* We have been given an interfaces line, and been
1482 told to only bind to those interfaces. Create a
1483 socket per interface and bind to only these.
1485 for(i
= 0; i
< num_interfaces
; i
++) {
1486 const char *address
= iface_list_n_ip(ifaces
, i
);
1487 status
= add_socket(task
, task
->lp_ctx
, task
->model_ops
,
1488 address
, ldap_service
);
1489 if (!NT_STATUS_IS_OK(status
)) goto failed
;
1494 size_t num_binds
= 0;
1495 wcard
= iface_list_wildcard(task
);
1496 if (wcard
== NULL
) {
1497 DBG_ERR("No wildcard addresses available\n");
1498 status
= NT_STATUS_UNSUCCESSFUL
;
1501 for (i
=0; wcard
[i
]; i
++) {
1502 status
= add_socket(task
, task
->lp_ctx
, task
->model_ops
,
1503 wcard
[i
], ldap_service
);
1504 if (NT_STATUS_IS_OK(status
)) {
1509 if (num_binds
== 0) {
1510 status
= NT_STATUS_UNSUCCESSFUL
;
1515 ldapi_path
= lpcfg_private_path(ldap_service
, task
->lp_ctx
, "ldapi");
1517 status
= NT_STATUS_UNSUCCESSFUL
;
1521 status
= stream_setup_socket(task
, task
->event_ctx
, task
->lp_ctx
,
1522 task
->model_ops
, &ldapi_stream_nonpriv_ops
,
1523 "unix", ldapi_path
, NULL
,
1524 lpcfg_socket_options(task
->lp_ctx
),
1525 ldap_service
, task
->process_context
);
1526 talloc_free(ldapi_path
);
1527 if (!NT_STATUS_IS_OK(status
)) {
1528 DBG_ERR("ldapsrv failed to bind to %s - %s\n",
1529 ldapi_path
, nt_errstr(status
));
1532 #ifdef WITH_LDAPI_PRIV_SOCKET
1533 priv_dir
= lpcfg_private_path(ldap_service
, task
->lp_ctx
, "ldap_priv");
1534 if (priv_dir
== NULL
) {
1535 status
= NT_STATUS_UNSUCCESSFUL
;
1539 * Make sure the directory for the privileged ldapi socket exists, and
1540 * is of the correct permissions
1542 if (!directory_create_or_exist(priv_dir
, 0750)) {
1543 task_server_terminate(task
, "Cannot create ldap "
1544 "privileged ldapi directory", true);
1545 return NT_STATUS_UNSUCCESSFUL
;
1547 ldapi_path
= talloc_asprintf(ldap_service
, "%s/ldapi", priv_dir
);
1548 talloc_free(priv_dir
);
1549 if (ldapi_path
== NULL
) {
1550 status
= NT_STATUS_NO_MEMORY
;
1554 status
= stream_setup_socket(task
, task
->event_ctx
, task
->lp_ctx
,
1555 task
->model_ops
, &ldapi_stream_priv_ops
,
1556 "unix", ldapi_path
, NULL
,
1557 lpcfg_socket_options(task
->lp_ctx
),
1559 task
->process_context
);
1560 talloc_free(ldapi_path
);
1561 if (!NT_STATUS_IS_OK(status
)) {
1562 DBG_ERR("ldapsrv failed to bind to %s - %s\n",
1563 ldapi_path
, nt_errstr(status
));
1568 /* register the server */
1569 irpc_add_name(task
->msg_ctx
, "ldap_server");
1571 task
->private_data
= ldap_service
;
1573 return NT_STATUS_OK
;
1576 task_server_terminate(task
, "Failed to startup ldap server task", true);
1581 * Open a database to be later used by LDB wrap code (although it should be
1582 * plumbed through correctly eventually).
1584 static void ldapsrv_post_fork(struct task_server
*task
, struct process_details
*pd
)
1586 struct ldapsrv_service
*ldap_service
=
1587 talloc_get_type_abort(task
->private_data
, struct ldapsrv_service
);
1590 * As ldapsrv_before_loop() may changed the values for the parent loop
1591 * we need to adjust the pointers to the correct value in the child
1593 ldap_service
->lp_ctx
= task
->lp_ctx
;
1594 ldap_service
->current_ev
= task
->event_ctx
;
1595 ldap_service
->current_msg
= task
->msg_ctx
;
1597 ldap_service
->sam_ctx
= samdb_connect(ldap_service
,
1598 ldap_service
->current_ev
,
1599 ldap_service
->lp_ctx
,
1600 system_session(ldap_service
->lp_ctx
),
1603 if (ldap_service
->sam_ctx
== NULL
) {
1604 task_server_terminate(task
, "Cannot open system session LDB",
1610 static void ldapsrv_before_loop(struct task_server
*task
)
1612 struct ldapsrv_service
*ldap_service
=
1613 talloc_get_type_abort(task
->private_data
, struct ldapsrv_service
);
1616 if (ldap_service
->sam_ctx
!= NULL
) {
1618 * Make sure the values are still the same
1619 * as set in ldapsrv_post_fork()
1621 SMB_ASSERT(task
->lp_ctx
== ldap_service
->lp_ctx
);
1622 SMB_ASSERT(task
->event_ctx
== ldap_service
->current_ev
);
1623 SMB_ASSERT(task
->msg_ctx
== ldap_service
->current_msg
);
1626 * We need to adjust the pointers to the correct value
1627 * in the parent loop.
1629 ldap_service
->lp_ctx
= task
->lp_ctx
;
1630 ldap_service
->current_ev
= task
->event_ctx
;
1631 ldap_service
->current_msg
= task
->msg_ctx
;
1634 status
= imessaging_register(ldap_service
->current_msg
,
1636 MSG_RELOAD_TLS_CERTIFICATES
,
1638 if (!NT_STATUS_IS_OK(status
)) {
1639 task_server_terminate(task
, "Cannot register ldap_reload_certs",
1646 * Check the size of an ldap request packet.
1648 * For authenticated connections the maximum packet size is controlled by
1649 * the smb.conf parameter "ldap max authenticated request size"
1651 * For anonymous connections the maximum packet size is controlled by
1652 * the smb.conf parameter "ldap max anonymous request size"
1654 static int ldapsrv_check_packet_size(
1655 struct ldapsrv_connection
*conn
,
1658 bool is_anonymous
= false;
1659 size_t max_size
= 0;
1661 max_size
= lpcfg_ldap_max_anonymous_request_size(conn
->lp_ctx
);
1662 if (size
<= max_size
) {
1663 return LDAP_SUCCESS
;
1667 * Request is larger than the maximum unauthenticated request size.
1668 * As this code is called frequently we avoid calling
1669 * security_token_is_anonymous if possible
1671 if (conn
->session_info
!= NULL
&&
1672 conn
->session_info
->security_token
!= NULL
) {
1673 is_anonymous
= security_token_is_anonymous(
1674 conn
->session_info
->security_token
);
1679 "LDAP request size (%zu) exceeds (%zu)\n",
1682 return LDAP_UNWILLING_TO_PERFORM
;
1685 max_size
= lpcfg_ldap_max_authenticated_request_size(conn
->lp_ctx
);
1686 if (size
> max_size
) {
1688 "LDAP request size (%zu) exceeds (%zu)\n",
1691 return LDAP_UNWILLING_TO_PERFORM
;
1693 return LDAP_SUCCESS
;
1698 * Check that the blob contains enough data to be a valid packet
1699 * If there is a packet header check the size to ensure that it does not
1700 * exceed the maximum sizes.
1703 static NTSTATUS
ldapsrv_packet_check(
1704 struct tstream_context
*stream
,
1707 size_t *packet_size
)
1710 struct ldapsrv_connection
*conn
= private_data
;
1711 int result
= LDB_SUCCESS
;
1713 ret
= ldap_full_packet(stream
, private_data
, blob
, packet_size
);
1714 if (!NT_STATUS_IS_OK(ret
)) {
1717 result
= ldapsrv_check_packet_size(conn
, *packet_size
);
1718 if (result
!= LDAP_SUCCESS
) {
1719 return NT_STATUS_LDAP(result
);
1721 return NT_STATUS_OK
;
1724 NTSTATUS
server_service_ldap_init(TALLOC_CTX
*ctx
)
1726 static const struct service_details details
= {
1727 .inhibit_fork_on_accept
= false,
1728 .inhibit_pre_fork
= false,
1729 .task_init
= ldapsrv_task_init
,
1730 .post_fork
= ldapsrv_post_fork
,
1731 .before_loop
= ldapsrv_before_loop
,
1733 return register_server_service(ctx
, "ldap", &details
);