2004-09-13 14:36:59 +04:00
/*
Unix SMB / CIFS implementation .
LDAP server
Copyright ( C ) Volker Lendecke 2004
Copyright ( C ) Stefan Metzmacher 2004
This program is free software ; you can redistribute it and / or modify
it under the terms of the GNU General Public License as published by
2007-07-10 06:07:03 +04:00
the Free Software Foundation ; either version 3 of the License , or
2004-09-13 14:36:59 +04:00
( at your option ) any later version .
This program is distributed in the hope that it will be useful ,
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU General Public License for more details .
You should have received a copy of the GNU General Public License
2007-07-10 06:07:03 +04:00
along with this program . If not , see < http : //www.gnu.org/licenses/>.
2004-09-13 14:36:59 +04:00
*/
2010-05-21 11:39:15 +04:00
# include "libcli/ldap/libcli_ldap.h"
r17197: This patch moves the encryption of bulk data on SASL negotiated security
contexts from the application layer into the socket layer.
This improves a number of correctness aspects, as we now allow LDAP
packets to cross multiple SASL packets. It should also make it much
easier to write async LDAP tests from windows clients, as they use SASL
by default. It is also vital to allowing OpenLDAP clients to use GSSAPI
against Samba4, as it negotiates a rather small SASL buffer size.
This patch mirrors the earlier work done to move TLS into the socket
layer.
Unusual in this pstch is the extra read callback argument I take. As
SASL is a layer on top of a socket, it is entirely possible for the
SASL layer to drain a socket dry, but for the caller not to have read
all the decrypted data. This would leave the system without an event
to restart the read (as the socket is dry).
As such, I re-invoke the read handler from a timed callback, which
should trigger on the next running of the event loop. I believe that
the TLS code does require a similar callback.
In trying to understand why this is required, imagine a SASL-encrypted
LDAP packet in the following formation:
+-----------------+---------------------+
| SASL Packet #1 | SASL Packet #2 |
----------------------------------------+
| LDAP Packet #1 | LDAP Packet #2 |
----------------------------------------+
In the old code, this was illegal, but it is perfectly standard
SASL-encrypted LDAP. Without the callback, we would read and process
the first LDAP packet, and the SASL code would have read the second SASL
packet (to decrypt enough data for the LDAP packet), and no data would
remain on the socket.
Without data on the socket, read events stop. That is why I add timed
events, until the SASL buffer is drained.
Another approach would be to add a hack to the event system, to have it
pretend there remained data to read off the network (but that is ugly).
In improving the code, to handle more real-world cases, I've been able
to remove almost all the special-cases in the testnonblock code. The
only special case is that we must use a deterministic partial packet
when calling send, rather than a random length. (1 + n/2). This is
needed because of the way the SASL and TLS code works, and the 'resend
on failure' requirements.
Andrew Bartlett
(This used to be commit 5d7c9c12cb2b39673172a357092b80cd814850b0)
2006-07-23 06:50:08 +04:00
# include "lib/socket/socket.h"
# include "lib/stream/packet.h"
2010-09-22 16:24:03 +04:00
# include "system/network.h"
2015-08-28 13:19:37 +03:00
# include "lib/param/loadparm.h"
2005-02-10 10:08:40 +03:00
2019-05-21 04:17:22 +03:00
enum ldap_server_referral_scheme {
LDAP_REFERRAL_SCHEME_LDAP ,
LDAP_REFERRAL_SCHEME_LDAPS
} ;
2005-06-19 13:31:34 +04:00
struct ldapsrv_connection {
2015-07-23 13:08:42 +03:00
struct ldapsrv_connection * next , * prev ;
2007-12-03 23:25:06 +03:00
struct loadparm_context * lp_ctx ;
2005-06-19 13:31:34 +04:00
struct stream_connection * connection ;
struct gensec_security * gensec ;
struct auth_session_info * session_info ;
struct ldapsrv_service * service ;
2006-01-03 03:10:15 +03:00
struct cli_credentials * server_credentials ;
2006-01-13 03:38:35 +03:00
struct ldb_context * ldb ;
2004-09-13 14:36:59 +04:00
2006-07-25 23:20:04 +04:00
struct {
2010-09-22 16:24:03 +04:00
struct tevent_queue * send_queue ;
2015-07-23 13:06:11 +03:00
struct tevent_req * read_req ;
2010-09-22 16:24:03 +04:00
struct tstream_context * raw ;
struct tstream_context * tls ;
struct tstream_context * sasl ;
struct tstream_context * active ;
2006-07-25 23:20:04 +04:00
} sockets ;
2007-08-27 22:10:19 +04:00
bool global_catalog ;
2010-12-01 14:18:21 +03:00
bool is_privileged ;
2015-08-28 13:19:37 +03:00
enum ldap_server_require_strong_auth require_strong_auth ;
2017-03-03 02:53:06 +03:00
bool authz_logged ;
2019-05-21 04:17:22 +03:00
enum ldap_server_referral_scheme referral_scheme ;
2006-07-12 08:59:41 +04:00
2006-01-13 03:38:35 +03:00
struct {
int initial_timeout ;
int conn_idle_time ;
int max_page_size ;
2015-07-23 13:08:42 +03:00
int max_notifications ;
2006-01-13 03:38:35 +03:00
int search_timeout ;
2010-09-22 16:24:03 +04:00
struct timeval endtime ;
const char * reason ;
2006-01-13 03:38:35 +03:00
} limits ;
2010-09-07 05:57:44 +04:00
2010-09-22 16:24:03 +04:00
struct tevent_req * active_call ;
2015-07-23 13:08:42 +03:00
struct ldapsrv_call * pending_calls ;
2005-06-19 13:31:34 +04:00
} ;
2004-09-22 14:48:32 +04:00
struct ldapsrv_call {
2015-07-23 13:08:42 +03:00
struct ldapsrv_call * prev , * next ;
2004-09-22 14:48:32 +04:00
struct ldapsrv_connection * conn ;
2005-06-15 04:27:51 +04:00
struct ldap_message * request ;
2004-09-22 14:48:32 +04:00
struct ldapsrv_reply {
2005-06-19 13:31:34 +04:00
struct ldapsrv_reply * prev , * next ;
2005-06-15 04:27:51 +04:00
struct ldap_message * msg ;
2019-04-04 06:52:17 +03:00
DATA_BLOB blob ;
2004-09-22 14:48:32 +04:00
} * replies ;
2019-04-04 07:25:30 +03:00
struct iovec * out_iov ;
size_t iov_count ;
2019-05-08 05:03:50 +03:00
size_t reply_size ;
2010-09-22 16:24:03 +04:00
2017-05-11 17:51:15 +03:00
struct tevent_req * ( * wait_send ) ( TALLOC_CTX * mem_ctx ,
struct tevent_context * ev ,
void * private_data ) ;
NTSTATUS ( * wait_recv ) ( struct tevent_req * req ) ;
void * wait_private ;
2010-09-22 16:24:03 +04:00
struct tevent_req * ( * postprocess_send ) ( TALLOC_CTX * mem_ctx ,
struct tevent_context * ev ,
void * private_data ) ;
NTSTATUS ( * postprocess_recv ) ( struct tevent_req * req ) ;
void * postprocess_private ;
2015-07-23 13:08:42 +03:00
struct {
bool busy ;
uint64_t generation ;
} notification ;
2004-09-22 14:48:32 +04:00
} ;
2019-05-08 05:03:50 +03:00
/*
* This matches the previous implicit size limit via talloc ' s maximum
* allocation size
*/
# define LDAP_SERVER_MAX_REPLY_SIZE ((size_t)(256 * 1024 * 1024))
2019-05-14 03:08:03 +03:00
/*
* Start writing to the network before we hit this size
*/
# define LDAP_SERVER_MAX_CHUNK_SIZE ((size_t)(25 * 1024 * 1024))
2004-09-22 17:01:00 +04:00
struct ldapsrv_service {
2010-09-22 16:24:03 +04:00
struct tstream_tls_params * tls_params ;
2008-01-06 00:36:33 +03:00
struct task_server * task ;
2010-09-22 16:24:03 +04:00
struct tevent_queue * call_queue ;
2015-07-23 13:08:42 +03:00
struct ldapsrv_connection * connections ;
struct {
uint64_t generation ;
struct tevent_req * retry ;
} notification ;
2019-05-08 03:40:48 +03:00
struct ldb_context * sam_ctx ;
2004-09-13 14:36:59 +04:00
} ;
2006-03-07 14:07:23 +03:00
# include "ldap_server/proto.h"