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"
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"
38 close the socket and shutdown a server_context
40 static void ldapsrv_terminate_connection(struct ldapsrv_connection *conn,
43 /* we don't actually do the stream termination here as the
44 recv/send functions dereference the connection after the
45 packet processing callbacks. Instead we mark it for
46 termination and do the real termination in the send/recv
48 conn->terminate = reason;
52 process a decoded ldap message
54 static void ldapsrv_process_message(struct ldapsrv_connection *conn,
55 struct ldap_message *msg)
57 struct ldapsrv_call *call;
60 struct data_blob_list_item *q;
61 BOOL enable_wrap = conn->enable_wrap;
63 call = talloc(conn, struct ldapsrv_call);
65 ldapsrv_terminate_connection(conn, "no memory");
69 call->request = talloc_steal(call, msg);
74 status = ldapsrv_do_call(call);
75 if (!NT_STATUS_IS_OK(status)) {
79 blob = data_blob(NULL, 0);
81 if (call->replies == NULL) {
86 /* build all the replies into a single blob */
87 while (call->replies) {
90 msg = call->replies->msg;
91 if (!ldap_encode(msg, &b, call)) {
92 DEBUG(0,("Failed to encode ldap reply of type %d\n", msg->type));
96 status = data_blob_append(call, &blob, b.data, b.length);
99 if (!NT_STATUS_IS_OK(status)) goto failed;
101 DLIST_REMOVE(call->replies, call->replies);
104 /* possibly encrypt/sign the reply */
108 status = gensec_wrap(conn->gensec, call, &blob, &wrapped);
109 if (!NT_STATUS_IS_OK(status)) {
112 data_blob_free(&blob);
113 blob = data_blob_talloc(call, NULL, wrapped.length + 4);
114 if (blob.data == NULL) {
117 RSIVAL(blob.data, 0, wrapped.length);
118 memcpy(blob.data+4, wrapped.data, wrapped.length);
119 data_blob_free(&wrapped);
122 q = talloc(conn, struct data_blob_list_item);
123 if (q == NULL) goto failed;
126 talloc_steal(q, blob.data);
128 if (conn->send_queue == NULL) {
129 EVENT_FD_WRITEABLE(conn->connection->event.fde);
131 DLIST_ADD_END(conn->send_queue, q, struct data_blob_list_item *);
141 try and decode the partial input buffer
143 static void ldapsrv_try_decode_plain(struct ldapsrv_connection *conn)
145 struct asn1_data asn1;
147 if (!asn1_load(&asn1, conn->partial)) {
148 ldapsrv_terminate_connection(conn, "out of memory");
152 /* try and decode - this will fail if we don't have a full packet yet */
153 while (asn1.ofs < asn1.length) {
154 struct ldap_message *msg = talloc(conn, struct ldap_message);
155 off_t saved_ofs = asn1.ofs;
158 ldapsrv_terminate_connection(conn, "out of memory");
162 if (ldap_decode(&asn1, msg)) {
163 ldapsrv_process_message(conn, msg);
165 if (asn1.ofs < asn1.length) {
166 ldapsrv_terminate_connection(conn, "ldap_decode failed");
169 asn1.ofs = saved_ofs;
175 /* keep any remaining data in conn->partial */
176 data_blob_free(&conn->partial);
177 if (asn1.ofs != asn1.length) {
178 conn->partial = data_blob_talloc(conn,
179 asn1.data + asn1.ofs,
180 asn1.length - asn1.ofs);
187 try and decode/process wrapped data
189 static void ldapsrv_try_decode_wrapped(struct ldapsrv_connection *conn)
193 /* keep decoding while we have a full wrapped packet */
194 while (conn->partial.length >= 4 &&
195 (len=RIVAL(conn->partial.data, 0)) <= conn->partial.length-4) {
196 DATA_BLOB wrapped, unwrapped;
197 struct asn1_data asn1;
198 struct ldap_message *msg = talloc(conn, struct ldap_message);
202 ldapsrv_terminate_connection(conn, "out of memory");
206 wrapped.data = conn->partial.data+4;
207 wrapped.length = len;
209 status = gensec_unwrap(conn->gensec, msg, &wrapped, &unwrapped);
210 if (!NT_STATUS_IS_OK(status)) {
211 ldapsrv_terminate_connection(conn, "gensec unwrap failed");
215 if (!asn1_load(&asn1, unwrapped)) {
216 ldapsrv_terminate_connection(conn, "out of memory");
220 while (ldap_decode(&asn1, msg)) {
221 ldapsrv_process_message(conn, msg);
222 msg = talloc(conn, struct ldap_message);
225 if (asn1.ofs < asn1.length) {
226 ldapsrv_terminate_connection(conn, "ldap_decode failed");
233 if (conn->partial.length == len + 4) {
234 data_blob_free(&conn->partial);
236 memmove(conn->partial.data, conn->partial.data+len+4,
237 conn->partial.length - (len+4));
238 conn->partial.length -= len + 4;
245 called when a LDAP socket becomes readable
247 static void ldapsrv_recv(struct stream_connection *c, uint16_t flags)
249 struct ldapsrv_connection *conn =
250 talloc_get_type(c->private, struct ldapsrv_connection);
252 size_t npending, nread;
254 if (conn->processing) {
255 EVENT_FD_NOT_READABLE(c->event.fde);
259 /* work out how much data is pending */
260 status = tls_socket_pending(conn->tls, &npending);
261 if (!NT_STATUS_IS_OK(status)) {
262 ldapsrv_terminate_connection(conn, "socket_pending() failed");
266 ldapsrv_terminate_connection(conn, "EOF from client");
270 conn->partial.data = talloc_realloc_size(conn, conn->partial.data,
271 conn->partial.length + npending);
272 if (conn->partial.data == NULL) {
273 ldapsrv_terminate_connection(conn, "out of memory");
277 /* receive the data */
278 status = tls_socket_recv(conn->tls, conn->partial.data + conn->partial.length,
280 if (NT_STATUS_IS_ERR(status)) {
281 ldapsrv_terminate_connection(conn, "socket_recv() failed");
284 if (!NT_STATUS_IS_OK(status)) {
288 ldapsrv_terminate_connection(conn, "EOF from client");
291 conn->partial.length += nread;
293 conn->processing = True;
294 /* see if we can decode what we have */
295 if (conn->enable_wrap) {
296 ldapsrv_try_decode_wrapped(conn);
298 ldapsrv_try_decode_plain(conn);
300 conn->processing = False;
302 EVENT_FD_READABLE(c->event.fde);
304 if (conn->terminate) {
306 talloc_free(conn->tls);
309 stream_terminate_connection(conn->connection, conn->terminate);
314 called when a LDAP socket becomes writable
316 static void ldapsrv_send(struct stream_connection *c, uint16_t flags)
318 struct ldapsrv_connection *conn =
319 talloc_get_type(c->private, struct ldapsrv_connection);
320 while (conn->send_queue) {
321 struct data_blob_list_item *q = conn->send_queue;
325 status = tls_socket_send(conn->tls, &q->blob, &nsent);
326 if (NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES)) {
329 if (!NT_STATUS_IS_OK(status)) {
330 ldapsrv_terminate_connection(conn, "socket_send error");
334 q->blob.data += nsent;
335 q->blob.length -= nsent;
336 if (q->blob.length == 0) {
337 DLIST_REMOVE(conn->send_queue, q);
340 if (conn->send_queue == NULL) {
341 EVENT_FD_NOT_WRITEABLE(c->event.fde);
344 if (conn->terminate) {
346 talloc_free(conn->tls);
349 stream_terminate_connection(conn->connection, conn->terminate);
354 initialise a server_context from a open socket and register a event handler
355 for reading from that socket
357 static void ldapsrv_accept(struct stream_connection *c)
359 struct ldapsrv_partition *rootDSE_part;
360 struct ldapsrv_partition *part;
361 struct ldapsrv_service *ldapsrv_service =
362 talloc_get_type(c->private, struct ldapsrv_service);
363 struct ldapsrv_connection *conn;
366 conn = talloc_zero(c, struct ldapsrv_connection);
368 stream_terminate_connection(c, "ldapsrv_accept: out of memory");
372 conn->enable_wrap = False;
373 conn->partial = data_blob(NULL, 0);
374 conn->send_queue = NULL;
375 conn->connection = c;
376 conn->service = ldapsrv_service;
377 conn->processing = False;
380 port = socket_get_my_port(c->socket);
382 /* note that '0' is a ASN1_SEQUENCE(0), which is the first byte on
383 any ldap connection */
384 conn->tls = tls_init_server(ldapsrv_service->tls_params, c->socket,
385 c->event.fde, NULL, port != 389);
387 ldapsrv_terminate_connection(conn, "ldapsrv_accept: tls_init_server() failed");
391 /* Connections start out anonymous */
392 if (!NT_STATUS_IS_OK(auth_anonymous_session_info(conn, &conn->session_info))) {
393 ldapsrv_terminate_connection(conn, "failed to setup anonymous session info");
397 rootDSE_part = talloc(conn, struct ldapsrv_partition);
398 if (rootDSE_part == NULL) {
399 ldapsrv_terminate_connection(conn, "talloc failed");
403 rootDSE_part->base_dn = ""; /* RootDSE */
404 rootDSE_part->ops = ldapsrv_get_rootdse_partition_ops();
405 if (!NT_STATUS_IS_OK(rootDSE_part->ops->Init(rootDSE_part, conn))) {
406 ldapsrv_terminate_connection(conn, "rootDSE Init failed");
409 conn->rootDSE = rootDSE_part;
410 DLIST_ADD_END(conn->partitions, rootDSE_part, struct ldapsrv_partition *);
412 part = talloc(conn, struct ldapsrv_partition);
414 ldapsrv_terminate_connection(conn, "talloc failed");
418 part->base_dn = "*"; /* default partition */
419 part->ops = ldapsrv_get_sldb_partition_ops();
420 if (!NT_STATUS_IS_OK(part->ops->Init(part, conn))) {
421 ldapsrv_terminate_connection(conn, "default partition Init failed");
424 conn->default_partition = part;
425 DLIST_ADD_END(conn->partitions, part, struct ldapsrv_partition *);
427 irpc_add_name(c->msg_ctx, "ldap_server");
430 static const struct stream_server_ops ldap_stream_ops = {
432 .accept_connection = ldapsrv_accept,
433 .recv_handler = ldapsrv_recv,
434 .send_handler = ldapsrv_send,
438 add a socket address to the list of events, one event per port
440 static NTSTATUS add_socket(struct event_context *event_context,
441 const struct model_ops *model_ops,
442 const char *address, struct ldapsrv_service *ldap_service)
447 status = stream_setup_socket(event_context, model_ops, &ldap_stream_ops,
448 "ipv4", address, &port, ldap_service);
449 if (!NT_STATUS_IS_OK(status)) {
450 DEBUG(0,("ldapsrv failed to bind to %s:%u - %s\n",
451 address, port, nt_errstr(status)));
454 if (tls_support(ldap_service->tls_params)) {
455 /* add ldaps server */
457 status = stream_setup_socket(event_context, model_ops, &ldap_stream_ops,
458 "ipv4", address, &port, ldap_service);
459 if (!NT_STATUS_IS_OK(status)) {
460 DEBUG(0,("ldapsrv failed to bind to %s:%u - %s\n",
461 address, port, nt_errstr(status)));
465 /* if we are a PDC, then also enable the global catalog server port, 3268 */
466 if (lp_server_role() == ROLE_DOMAIN_PDC) {
468 status = stream_setup_socket(event_context, model_ops, &ldap_stream_ops,
469 "ipv4", address, &port, ldap_service);
470 if (!NT_STATUS_IS_OK(status)) {
471 DEBUG(0,("ldapsrv failed to bind to %s:%u - %s\n",
472 address, port, nt_errstr(status)));
480 open the ldap server sockets
482 static void ldapsrv_task_init(struct task_server *task)
484 struct ldapsrv_service *ldap_service;
487 ldap_service = talloc_zero(task, struct ldapsrv_service);
488 if (ldap_service == NULL) goto failed;
490 ldap_service->tls_params = tls_initialise(ldap_service);
491 if (ldap_service->tls_params == NULL) goto failed;
493 if (lp_interfaces() && lp_bind_interfaces_only()) {
494 int num_interfaces = iface_count();
497 /* We have been given an interfaces line, and been
498 told to only bind to those interfaces. Create a
499 socket per interface and bind to only these.
501 for(i = 0; i < num_interfaces; i++) {
502 const char *address = iface_n_ip(i);
503 status = add_socket(task->event_ctx, task->model_ops, address, ldap_service);
504 if (!NT_STATUS_IS_OK(status)) goto failed;
507 status = add_socket(task->event_ctx, task->model_ops, lp_socket_address(), ldap_service);
508 if (!NT_STATUS_IS_OK(status)) goto failed;
514 task_server_terminate(task, "Failed to startup ldap server task");
518 called on startup of the web server service It's job is to start
519 listening on all configured sockets
521 static NTSTATUS ldapsrv_init(struct event_context *event_context,
522 const struct model_ops *model_ops)
524 return task_server_startup(event_context, model_ops, ldapsrv_task_init);
528 NTSTATUS server_service_ldap_init(void)
530 return register_server_service("ldap", ldapsrv_init);