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 "lib/events/events.h"
26 #include "auth/auth.h"
27 #include "auth/credentials/credentials.h"
28 #include "librpc/gen_ndr/ndr_samr.h"
29 #include "lib/util/dlinklist.h"
30 #include "libcli/util/asn_1.h"
31 #include "ldap_server/ldap_server.h"
32 #include "smbd/service_task.h"
33 #include "smbd/service_stream.h"
34 #include "smbd/service.h"
35 #include "smbd/process_model.h"
36 #include "lib/tls/tls.h"
37 #include "lib/messaging/irpc.h"
38 #include "lib/ldb/include/ldb.h"
39 #include "lib/ldb/include/ldb_errors.h"
40 #include "system/network.h"
41 #include "lib/socket/netif.h"
42 #include "dsdb/samdb/samdb.h"
43 #include "param/param.h"
45 close the socket and shutdown a server_context
47 void ldapsrv_terminate_connection(struct ldapsrv_connection *conn,
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;
73 call = talloc(conn, struct ldapsrv_call);
75 ldapsrv_terminate_connection(conn, "no memory");
79 call->request = talloc_steal(call, msg);
82 call->send_callback = NULL;
83 call->send_private = NULL;
86 status = ldapsrv_do_call(call);
87 if (!NT_STATUS_IS_OK(status)) {
92 blob = data_blob(NULL, 0);
94 if (call->replies == NULL) {
99 /* build all the replies into a single blob */
100 while (call->replies) {
104 msg = call->replies->msg;
105 if (!ldap_encode(msg, &b, call)) {
106 DEBUG(0,("Failed to encode ldap reply of type %d\n", msg->type));
111 ret = data_blob_append(call, &blob, b.data, b.length);
114 talloc_set_name_const(blob.data, "Outgoing, encoded LDAP packet");
121 DLIST_REMOVE(call->replies, call->replies);
124 packet_send_callback(conn->packet, blob,
125 call->send_callback, call->send_private);
133 static NTSTATUS ldapsrv_decode(void *private, DATA_BLOB blob)
136 struct ldapsrv_connection *conn = talloc_get_type(private,
137 struct ldapsrv_connection);
138 struct asn1_data *asn1 = asn1_init(conn);
139 struct ldap_message *msg = talloc(conn, struct ldap_message);
141 if (asn1 == NULL || msg == NULL) {
142 return NT_STATUS_NO_MEMORY;
145 if (!asn1_load(asn1, blob)) {
148 return NT_STATUS_NO_MEMORY;
151 status = ldap_decode(asn1, msg);
152 if (!NT_STATUS_IS_OK(status)) {
157 data_blob_free(&blob);
158 ldapsrv_process_message(conn, msg);
166 static void ldapsrv_conn_idle_timeout(struct event_context *ev,
167 struct timed_event *te,
171 struct ldapsrv_connection *conn = talloc_get_type(private, struct ldapsrv_connection);
173 ldapsrv_terminate_connection(conn, "Timeout. No requests after bind");
177 called when a LDAP socket becomes readable
179 void ldapsrv_recv(struct stream_connection *c, uint16_t flags)
181 struct ldapsrv_connection *conn =
182 talloc_get_type(c->private, struct ldapsrv_connection);
184 if (conn->limits.ite) { /* clean initial timeout if any */
185 talloc_free(conn->limits.ite);
186 conn->limits.ite = NULL;
189 if (conn->limits.te) { /* clean idle timeout if any */
190 talloc_free(conn->limits.te);
191 conn->limits.te = NULL;
194 packet_recv(conn->packet);
196 /* set idle timeout */
197 conn->limits.te = event_add_timed(c->event.ctx, conn,
198 timeval_current_ofs(conn->limits.conn_idle_time, 0),
199 ldapsrv_conn_idle_timeout, conn);
203 called when a LDAP socket becomes writable
205 static void ldapsrv_send(struct stream_connection *c, uint16_t flags)
207 struct ldapsrv_connection *conn =
208 talloc_get_type(c->private, struct ldapsrv_connection);
210 packet_queue_run(conn->packet);
213 static void ldapsrv_conn_init_timeout(struct event_context *ev,
214 struct timed_event *te,
218 struct ldapsrv_connection *conn = talloc_get_type(private, struct ldapsrv_connection);
220 ldapsrv_terminate_connection(conn, "Timeout. No requests after initial connection");
223 static int ldapsrv_load_limits(struct ldapsrv_connection *conn)
226 const char *attrs[] = { "configurationNamingContext", NULL };
227 const char *attrs2[] = { "lDAPAdminLimits", NULL };
228 struct ldb_message_element *el;
229 struct ldb_result *res = NULL;
230 struct ldb_dn *basedn;
231 struct ldb_dn *conf_dn;
232 struct ldb_dn *policy_dn;
235 /* set defaults limits in case of failure */
236 conn->limits.initial_timeout = 120;
237 conn->limits.conn_idle_time = 900;
238 conn->limits.max_page_size = 1000;
239 conn->limits.search_timeout = 120;
242 tmp_ctx = talloc_new(conn);
243 if (tmp_ctx == NULL) {
247 basedn = ldb_dn_new(tmp_ctx, conn->ldb, NULL);
248 if ( ! ldb_dn_validate(basedn)) {
252 ret = ldb_search(conn->ldb, basedn, LDB_SCOPE_BASE, NULL, attrs, &res);
253 if (ret != LDB_SUCCESS) {
257 talloc_steal(tmp_ctx, res);
259 if (res->count != 1) {
263 conf_dn = ldb_msg_find_attr_as_dn(conn->ldb, tmp_ctx, res->msgs[0], "configurationNamingContext");
264 if (conf_dn == NULL) {
268 policy_dn = ldb_dn_copy(tmp_ctx, conf_dn);
269 ldb_dn_add_child_fmt(policy_dn, "CN=Default Query Policy,CN=Query-Policies,CN=Directory Service,CN=Windows NT,CN=Services");
270 if (policy_dn == NULL) {
274 ret = ldb_search(conn->ldb, policy_dn, LDB_SCOPE_BASE, NULL, attrs2, &res);
275 if (ret != LDB_SUCCESS) {
279 talloc_steal(tmp_ctx, res);
281 if (res->count != 1) {
285 el = ldb_msg_find_element(res->msgs[0], "lDAPAdminLimits");
290 for (i = 0; i < el->num_values; i++) {
291 char policy_name[256];
294 s = sscanf((const char *)el->values[i].data, "%255[^=]=%d", policy_name, &policy_value);
295 if (ret != 2 || policy_value == 0)
298 if (strcasecmp("InitRecvTimeout", policy_name) == 0) {
299 conn->limits.initial_timeout = policy_value;
302 if (strcasecmp("MaxConnIdleTime", policy_name) == 0) {
303 conn->limits.conn_idle_time = policy_value;
306 if (strcasecmp("MaxPageSize", policy_name) == 0) {
307 conn->limits.max_page_size = policy_value;
310 if (strcasecmp("MaxQueryDuration", policy_name) == 0) {
311 conn->limits.search_timeout = policy_value;
319 DEBUG(0, ("Failed to load ldap server query policies\n"));
320 talloc_free(tmp_ctx);
325 initialise a server_context from a open socket and register a event handler
326 for reading from that socket
328 static void ldapsrv_accept(struct stream_connection *c)
330 struct ldapsrv_service *ldapsrv_service =
331 talloc_get_type(c->private, struct ldapsrv_service);
332 struct ldapsrv_connection *conn;
333 struct cli_credentials *server_credentials;
334 struct socket_address *socket_address;
338 conn = talloc_zero(c, struct ldapsrv_connection);
340 stream_terminate_connection(c, "ldapsrv_accept: out of memory");
345 conn->connection = c;
346 conn->service = ldapsrv_service;
347 conn->sockets.raw = c->socket;
348 conn->lp_ctx = ldapsrv_service->task->lp_ctx;
352 socket_address = socket_get_my_addr(c->socket, conn);
353 if (!socket_address) {
354 ldapsrv_terminate_connection(conn, "ldapsrv_accept: failed to obtain local socket address!");
357 port = socket_address->port;
358 talloc_free(socket_address);
361 struct socket_context *tls_socket = tls_init_server(ldapsrv_service->tls_params, c->socket,
364 ldapsrv_terminate_connection(conn, "ldapsrv_accept: tls_init_server() failed");
367 talloc_unlink(c, c->socket);
368 talloc_steal(c, tls_socket);
369 c->socket = tls_socket;
370 conn->sockets.tls = tls_socket;
372 } else if (port == 3268) /* Global catalog */ {
373 conn->global_catalog = true;
375 conn->packet = packet_init(conn);
376 if (conn->packet == NULL) {
377 ldapsrv_terminate_connection(conn, "out of memory");
381 packet_set_private(conn->packet, conn);
382 packet_set_socket(conn->packet, c->socket);
383 packet_set_callback(conn->packet, ldapsrv_decode);
384 packet_set_full_request(conn->packet, ldap_full_packet);
385 packet_set_error_handler(conn->packet, ldapsrv_error_handler);
386 packet_set_event_context(conn->packet, c->event.ctx);
387 packet_set_fde(conn->packet, c->event.fde);
388 packet_set_serialise(conn->packet);
390 /* Ensure we don't get packets until the database is ready below */
391 packet_recv_disable(conn->packet);
393 server_credentials = cli_credentials_init(conn);
394 if (!server_credentials) {
395 stream_terminate_connection(c, "Failed to init server credentials\n");
399 cli_credentials_set_conf(server_credentials, conn->lp_ctx);
400 status = cli_credentials_set_machine_account(server_credentials, conn->lp_ctx);
401 if (!NT_STATUS_IS_OK(status)) {
402 stream_terminate_connection(c, talloc_asprintf(conn, "Failed to obtain server credentials, perhaps a standalone server?: %s\n", nt_errstr(status)));
405 conn->server_credentials = server_credentials;
407 /* Connections start out anonymous */
408 if (!NT_STATUS_IS_OK(auth_anonymous_session_info(conn, conn->lp_ctx, &conn->session_info))) {
409 ldapsrv_terminate_connection(conn, "failed to setup anonymous session info");
413 if (!NT_STATUS_IS_OK(ldapsrv_backend_Init(conn))) {
414 ldapsrv_terminate_connection(conn, "backend Init failed");
418 /* load limits from the conf partition */
419 ldapsrv_load_limits(conn); /* should we fail on error ? */
421 /* register the server */
422 irpc_add_name(c->msg_ctx, "ldap_server");
424 /* set connections limits */
425 conn->limits.ite = event_add_timed(c->event.ctx, conn,
426 timeval_current_ofs(conn->limits.initial_timeout, 0),
427 ldapsrv_conn_init_timeout, conn);
429 packet_recv_enable(conn->packet);
433 static const struct stream_server_ops ldap_stream_ops = {
435 .accept_connection = ldapsrv_accept,
436 .recv_handler = ldapsrv_recv,
437 .send_handler = ldapsrv_send,
441 add a socket address to the list of events, one event per port
443 static NTSTATUS add_socket(struct event_context *event_context,
444 struct loadparm_context *lp_ctx,
445 const struct model_ops *model_ops,
446 const char *address, struct ldapsrv_service *ldap_service)
450 struct ldb_context *ldb;
452 status = stream_setup_socket(event_context, model_ops, &ldap_stream_ops,
453 "ipv4", address, &port,
454 lp_socket_options(lp_ctx),
456 if (!NT_STATUS_IS_OK(status)) {
457 DEBUG(0,("ldapsrv failed to bind to %s:%u - %s\n",
458 address, port, nt_errstr(status)));
461 if (tls_support(ldap_service->tls_params)) {
462 /* add ldaps server */
464 status = stream_setup_socket(event_context, model_ops, &ldap_stream_ops,
465 "ipv4", address, &port,
466 lp_socket_options(lp_ctx),
468 if (!NT_STATUS_IS_OK(status)) {
469 DEBUG(0,("ldapsrv failed to bind to %s:%u - %s\n",
470 address, port, nt_errstr(status)));
474 /* Load LDAP database */
475 ldb = samdb_connect(ldap_service, lp_ctx, system_session(ldap_service, lp_ctx));
477 return NT_STATUS_INTERNAL_DB_CORRUPTION;
480 if (samdb_is_gc(ldb)) {
482 status = stream_setup_socket(event_context, model_ops, &ldap_stream_ops,
483 "ipv4", address, &port,
484 lp_socket_options(lp_ctx),
486 if (!NT_STATUS_IS_OK(status)) {
487 DEBUG(0,("ldapsrv failed to bind to %s:%u - %s\n",
488 address, port, nt_errstr(status)));
496 open the ldap server sockets
498 static void ldapsrv_task_init(struct task_server *task)
501 struct ldapsrv_service *ldap_service;
503 const struct model_ops *model_ops;
505 switch (lp_server_role(task->lp_ctx)) {
506 case ROLE_STANDALONE:
507 task_server_terminate(task, "ldap_server: no LDAP server required in standalone configuration");
509 case ROLE_DOMAIN_MEMBER:
510 task_server_terminate(task, "ldap_server: no LDAP server required in member server configuration");
512 case ROLE_DOMAIN_CONTROLLER:
513 /* Yes, we want an LDAP server */
517 task_server_set_title(task, "task[ldapsrv]");
519 /* run the ldap server as a single process */
520 model_ops = process_model_byname("single");
521 if (!model_ops) goto failed;
523 ldap_service = talloc_zero(task, struct ldapsrv_service);
524 if (ldap_service == NULL) goto failed;
526 ldap_service->task = task;
528 ldap_service->tls_params = tls_initialise(ldap_service, task->lp_ctx);
529 if (ldap_service->tls_params == NULL) goto failed;
531 if (lp_interfaces(task->lp_ctx) && lp_bind_interfaces_only(task->lp_ctx)) {
532 struct interface *ifaces;
536 load_interfaces(task, lp_interfaces(task->lp_ctx), &ifaces);
537 num_interfaces = iface_count(ifaces);
539 /* We have been given an interfaces line, and been
540 told to only bind to those interfaces. Create a
541 socket per interface and bind to only these.
543 for(i = 0; i < num_interfaces; i++) {
544 const char *address = iface_n_ip(ifaces, i);
545 status = add_socket(task->event_ctx, task->lp_ctx, model_ops, address, ldap_service);
546 if (!NT_STATUS_IS_OK(status)) goto failed;
549 status = add_socket(task->event_ctx, task->lp_ctx, model_ops,
550 lp_socket_address(task->lp_ctx), ldap_service);
551 if (!NT_STATUS_IS_OK(status)) goto failed;
554 ldapi_path = private_path(ldap_service, task->lp_ctx, "ldapi");
559 status = stream_setup_socket(task->event_ctx, model_ops, &ldap_stream_ops,
560 "unix", ldapi_path, NULL,
561 lp_socket_options(task->lp_ctx),
563 talloc_free(ldapi_path);
564 if (!NT_STATUS_IS_OK(status)) {
565 DEBUG(0,("ldapsrv failed to bind to %s - %s\n",
566 ldapi_path, nt_errstr(status)));
572 task_server_terminate(task, "Failed to startup ldap server task");
576 called on startup of the web server service It's job is to start
577 listening on all configured sockets
579 static NTSTATUS ldapsrv_init(struct event_context *event_context,
580 struct loadparm_context *lp_ctx,
581 const struct model_ops *model_ops)
583 return task_server_startup(event_context, model_ops, ldapsrv_task_init);
587 NTSTATUS server_service_ldap_init(void)
589 return register_server_service("ldap", ldapsrv_init);