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 2 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, write to the Free Software
22 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 #include "lib/events/events.h"
27 #include "auth/auth.h"
28 #include "dlinklist.h"
29 #include "libcli/util/asn_1.h"
30 #include "ldap_server/ldap_server.h"
31 #include "smbd/service_task.h"
32 #include "smbd/service_stream.h"
33 #include "lib/socket/socket.h"
34 #include "lib/tls/tls.h"
35 #include "lib/messaging/irpc.h"
36 #include "lib/stream/packet.h"
37 #include "lib/ldb/include/ldb.h"
38 #include "lib/ldb/include/ldb_errors.h"
41 close the socket and shutdown a server_context
43 static void ldapsrv_terminate_connection(struct ldapsrv_connection *conn,
47 talloc_free(conn->tls);
50 stream_terminate_connection(conn->connection, reason);
56 static void ldapsrv_error_handler(void *private, NTSTATUS status)
58 struct ldapsrv_connection *conn = talloc_get_type(private,
59 struct ldapsrv_connection);
60 ldapsrv_terminate_connection(conn, nt_errstr(status));
64 process a decoded ldap message
66 static void ldapsrv_process_message(struct ldapsrv_connection *conn,
67 struct ldap_message *msg)
69 struct ldapsrv_call *call;
72 BOOL enable_wrap = conn->enable_wrap;
74 call = talloc(conn, struct ldapsrv_call);
76 ldapsrv_terminate_connection(conn, "no memory");
80 call->request = talloc_steal(call, msg);
85 status = ldapsrv_do_call(call);
86 if (!NT_STATUS_IS_OK(status)) {
90 blob = data_blob(NULL, 0);
92 if (call->replies == NULL) {
97 /* build all the replies into a single blob */
98 while (call->replies) {
101 msg = call->replies->msg;
102 if (!ldap_encode(msg, &b, call)) {
103 DEBUG(0,("Failed to encode ldap reply of type %d\n", msg->type));
107 status = data_blob_append(call, &blob, b.data, b.length);
110 if (!NT_STATUS_IS_OK(status)) goto failed;
112 DLIST_REMOVE(call->replies, call->replies);
115 /* possibly encrypt/sign the reply */
119 status = gensec_wrap(conn->gensec, call, &blob, &wrapped);
120 if (!NT_STATUS_IS_OK(status)) {
123 data_blob_free(&blob);
124 blob = data_blob_talloc(call, NULL, wrapped.length + 4);
125 if (blob.data == NULL) {
128 RSIVAL(blob.data, 0, wrapped.length);
129 memcpy(blob.data+4, wrapped.data, wrapped.length);
130 data_blob_free(&wrapped);
133 packet_send(conn->packet, blob);
143 decode the input buffer
145 static NTSTATUS ldapsrv_decode_plain(struct ldapsrv_connection *conn, DATA_BLOB blob)
147 struct asn1_data asn1;
148 struct ldap_message *msg = talloc(conn, struct ldap_message);
151 return NT_STATUS_NO_MEMORY;
154 if (!asn1_load(&asn1, blob)) {
155 return NT_STATUS_NO_MEMORY;
158 if (!ldap_decode(&asn1, msg)) {
160 return NT_STATUS_LDAP(LDAP_PROTOCOL_ERROR);
163 data_blob_free(&blob);
164 ldapsrv_process_message(conn, msg);
171 decode/process wrapped data
173 static NTSTATUS ldapsrv_decode_wrapped(struct ldapsrv_connection *conn,
176 DATA_BLOB wrapped, unwrapped;
177 struct asn1_data asn1;
178 struct ldap_message *msg = talloc(conn, struct ldap_message);
182 return NT_STATUS_NO_MEMORY;
185 wrapped = data_blob_const(blob.data+4, blob.length-4);
187 status = gensec_unwrap(conn->gensec, msg, &wrapped, &unwrapped);
188 if (!NT_STATUS_IS_OK(status)) {
189 return NT_STATUS_LDAP(LDAP_PROTOCOL_ERROR);
192 data_blob_free(&blob);
194 if (!asn1_load(&asn1, unwrapped)) {
195 return NT_STATUS_LDAP(LDAP_PROTOCOL_ERROR);
198 while (ldap_decode(&asn1, msg)) {
199 ldapsrv_process_message(conn, msg);
200 msg = talloc(conn, struct ldap_message);
203 if (asn1.ofs < asn1.length) {
204 return NT_STATUS_LDAP(LDAP_PROTOCOL_ERROR);
216 static NTSTATUS ldapsrv_decode(void *private, DATA_BLOB blob)
218 struct ldapsrv_connection *conn = talloc_get_type(private,
219 struct ldapsrv_connection);
220 if (conn->enable_wrap) {
221 return ldapsrv_decode_wrapped(conn, blob);
223 return ldapsrv_decode_plain(conn, blob);
227 called when a LDAP socket becomes readable
229 static void ldapsrv_recv(struct stream_connection *c, uint16_t flags)
231 struct ldapsrv_connection *conn =
232 talloc_get_type(c->private, struct ldapsrv_connection);
234 if (conn->limits.ite) {
235 talloc_free(conn->limits.ite);
236 conn->limits.ite = NULL;
239 packet_recv(conn->packet);
243 check if a blob is a complete ldap packet
244 handle wrapper or unwrapped connections
246 NTSTATUS ldapsrv_complete_packet(void *private, DATA_BLOB blob, size_t *size)
248 struct ldapsrv_connection *conn = talloc_get_type(private,
249 struct ldapsrv_connection);
250 if (conn->enable_wrap) {
251 return packet_full_request_u32(private, blob, size);
253 return ldap_full_packet(private, blob, size);
257 called when a LDAP socket becomes writable
259 static void ldapsrv_send(struct stream_connection *c, uint16_t flags)
261 struct ldapsrv_connection *conn =
262 talloc_get_type(c->private, struct ldapsrv_connection);
264 packet_queue_run(conn->packet);
267 static void ldapsrv_conn_init_timeout(struct event_context *ev,
268 struct timed_event *te,
272 struct ldapsrv_connection *conn = talloc_get_type(private, struct ldapsrv_connection);
274 ldapsrv_terminate_connection(conn, "Timeout. No requests after initial connection");
277 static int ldapsrv_load_limits(struct ldapsrv_connection *conn)
280 const char *attrs[] = { "configurationNamingContext", NULL };
281 const char *attrs2[] = { "lDAPAdminLimits", NULL };
282 const char *conf_dn_s;
283 struct ldb_message_element *el;
284 struct ldb_result *res = NULL;
285 struct ldb_dn *basedn;
286 struct ldb_dn *conf_dn;
287 struct ldb_dn *policy_dn;
290 /* set defaults limits in case of failure */
291 conn->limits.initial_timeout = 120;
292 conn->limits.conn_idle_time = 900;
293 conn->limits.max_page_size = 1000;
294 conn->limits.search_timeout = 120;
297 tmp_ctx = talloc_new(conn);
298 if (tmp_ctx == NULL) {
302 basedn = ldb_dn_explode(tmp_ctx, "");
303 if (basedn == NULL) {
307 ret = ldb_search(conn->ldb, basedn, LDB_SCOPE_BASE, NULL, attrs, &res);
308 talloc_steal(tmp_ctx, res);
309 if (ret != LDB_SUCCESS || res->count != 1) {
313 conf_dn_s = ldb_msg_find_string(res->msgs[0], "configurationNamingContext", NULL);
314 if (conf_dn_s == NULL) {
317 conf_dn = ldb_dn_explode(tmp_ctx, conf_dn_s);
318 if (conf_dn == NULL) {
322 policy_dn = ldb_dn_string_compose(tmp_ctx, conf_dn, "CN=Default Query Policy,CN=Query-Policies,CN=Directory Service,CN=Windows NT,CN=Services");
323 if (policy_dn == NULL) {
327 ret = ldb_search(conn->ldb, policy_dn, LDB_SCOPE_BASE, NULL, attrs2, &res);
328 talloc_steal(tmp_ctx, res);
329 if (ret != LDB_SUCCESS || res->count != 1) {
333 el = ldb_msg_find_element(res->msgs[0], "lDAPAdminLimits");
338 for (i = 0; i < el->num_values; i++) {
339 char policy_name[256];
342 s = sscanf(el->values[i].data, "%255[^=]=%d", policy_name, &policy_value);
343 if (ret != 2 || policy_value == 0)
346 if (strcasecmp("InitRecvTimeout", policy_name) == 0) {
347 conn->limits.initial_timeout = policy_value;
350 if (strcasecmp("MaxConnIdleTime", policy_name) == 0) {
351 conn->limits.conn_idle_time = policy_value;
354 if (strcasecmp("MaxPageSize", policy_name) == 0) {
355 conn->limits.max_page_size = policy_value;
358 if (strcasecmp("MaxQueryDuration", policy_name) == 0) {
359 conn->limits.search_timeout = policy_value;
367 DEBUG(0, ("Failed to load ldap server query policies\n"));
368 talloc_free(tmp_ctx);
373 initialise a server_context from a open socket and register a event handler
374 for reading from that socket
376 static void ldapsrv_accept(struct stream_connection *c)
378 struct ldapsrv_service *ldapsrv_service =
379 talloc_get_type(c->private, struct ldapsrv_service);
380 struct ldapsrv_connection *conn;
381 struct cli_credentials *server_credentials;
382 struct socket_address *socket_address;
386 conn = talloc_zero(c, struct ldapsrv_connection);
388 stream_terminate_connection(c, "ldapsrv_accept: out of memory");
392 conn->enable_wrap = False;
394 conn->connection = c;
395 conn->service = ldapsrv_service;
398 = cli_credentials_init(conn);
399 if (!server_credentials) {
400 stream_terminate_connection(c, "Failed to init server credentials\n");
405 cli_credentials_set_conf(server_credentials);
406 status = cli_credentials_set_machine_account(server_credentials);
407 if (!NT_STATUS_IS_OK(status)) {
408 stream_terminate_connection(c, talloc_asprintf(conn, "Failed to obtain server credentials, perhaps a standalone server?: %s\n", nt_errstr(status)));
412 conn->server_credentials = server_credentials;
416 socket_address = socket_get_my_addr(c->socket, conn);
417 if (!socket_address) {
418 ldapsrv_terminate_connection(conn, "ldapsrv_accept: failed to obtain local socket address!");
421 port = socket_address->port;
422 talloc_free(socket_address);
424 conn->tls = tls_init_server(ldapsrv_service->tls_params, c->socket,
425 c->event.fde, NULL, port != 389);
427 ldapsrv_terminate_connection(conn, "ldapsrv_accept: tls_init_server() failed");
431 conn->packet = packet_init(conn);
432 if (conn->packet == NULL) {
433 ldapsrv_terminate_connection(conn, "out of memory");
436 packet_set_private(conn->packet, conn);
437 packet_set_tls(conn->packet, conn->tls);
438 packet_set_callback(conn->packet, ldapsrv_decode);
439 packet_set_full_request(conn->packet, ldapsrv_complete_packet);
440 packet_set_error_handler(conn->packet, ldapsrv_error_handler);
441 packet_set_event_context(conn->packet, c->event.ctx);
442 packet_set_fde(conn->packet, c->event.fde);
443 packet_set_serialise(conn->packet);
445 /* Connections start out anonymous */
446 if (!NT_STATUS_IS_OK(auth_anonymous_session_info(conn, &conn->session_info))) {
447 ldapsrv_terminate_connection(conn, "failed to setup anonymous session info");
451 if (!NT_STATUS_IS_OK(ldapsrv_backend_Init(conn))) {
452 ldapsrv_terminate_connection(conn, "backend Init failed");
456 /* load limits from the conf partition */
457 ldapsrv_load_limits(conn); /* should we fail on error ? */
459 /* register the server */
460 irpc_add_name(c->msg_ctx, "ldap_server");
462 /* set connections limits */
463 conn->limits.ite = event_add_timed(c->event.ctx, conn,
464 timeval_current_ofs(conn->limits.initial_timeout, 0),
465 ldapsrv_conn_init_timeout, conn);
468 static const struct stream_server_ops ldap_stream_ops = {
470 .accept_connection = ldapsrv_accept,
471 .recv_handler = ldapsrv_recv,
472 .send_handler = ldapsrv_send,
476 add a socket address to the list of events, one event per port
478 static NTSTATUS add_socket(struct event_context *event_context,
479 const struct model_ops *model_ops,
480 const char *address, struct ldapsrv_service *ldap_service)
485 status = stream_setup_socket(event_context, model_ops, &ldap_stream_ops,
486 "ipv4", address, &port, ldap_service);
487 if (!NT_STATUS_IS_OK(status)) {
488 DEBUG(0,("ldapsrv failed to bind to %s:%u - %s\n",
489 address, port, nt_errstr(status)));
492 if (tls_support(ldap_service->tls_params)) {
493 /* add ldaps server */
495 status = stream_setup_socket(event_context, model_ops, &ldap_stream_ops,
496 "ipv4", address, &port, ldap_service);
497 if (!NT_STATUS_IS_OK(status)) {
498 DEBUG(0,("ldapsrv failed to bind to %s:%u - %s\n",
499 address, port, nt_errstr(status)));
503 /* if we are a PDC, then also enable the global catalog server port, 3268 */
504 if (lp_server_role() == ROLE_DOMAIN_PDC) {
506 status = stream_setup_socket(event_context, model_ops, &ldap_stream_ops,
507 "ipv4", address, &port, ldap_service);
508 if (!NT_STATUS_IS_OK(status)) {
509 DEBUG(0,("ldapsrv failed to bind to %s:%u - %s\n",
510 address, port, nt_errstr(status)));
518 open the ldap server sockets
520 static void ldapsrv_task_init(struct task_server *task)
522 struct ldapsrv_service *ldap_service;
525 ldap_service = talloc_zero(task, struct ldapsrv_service);
526 if (ldap_service == NULL) goto failed;
528 ldap_service->tls_params = tls_initialise(ldap_service);
529 if (ldap_service->tls_params == NULL) goto failed;
531 if (lp_interfaces() && lp_bind_interfaces_only()) {
532 int num_interfaces = iface_count();
535 /* We have been given an interfaces line, and been
536 told to only bind to those interfaces. Create a
537 socket per interface and bind to only these.
539 for(i = 0; i < num_interfaces; i++) {
540 const char *address = iface_n_ip(i);
541 status = add_socket(task->event_ctx, task->model_ops, address, ldap_service);
542 if (!NT_STATUS_IS_OK(status)) goto failed;
545 status = add_socket(task->event_ctx, task->model_ops, lp_socket_address(), ldap_service);
546 if (!NT_STATUS_IS_OK(status)) goto failed;
552 task_server_terminate(task, "Failed to startup ldap server task");
556 called on startup of the web server service It's job is to start
557 listening on all configured sockets
559 static NTSTATUS ldapsrv_init(struct event_context *event_context,
560 const struct model_ops *model_ops)
562 return task_server_startup(event_context, model_ops, ldapsrv_task_init);
566 NTSTATUS server_service_ldap_init(void)
568 return register_server_service("ldap", ldapsrv_init);