r10810: This adds the hooks required to communicate the current user from the
[Samba/aatanasov.git] / source / ldap_server / ldap_server.c
blobdac7feecfb1416a9c0e4c28668f4e9c3a5067a33
1 /*
2 Unix SMB/CIFS implementation.
4 LDAP server
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.
25 #include "includes.h"
26 #include "lib/events/events.h"
27 #include "auth/auth.h"
28 #include "dlinklist.h"
29 #include "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"
38 close the socket and shutdown a server_context
40 static void ldapsrv_terminate_connection(struct ldapsrv_connection *conn,
41 const char *reason)
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
47 functions */
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;
58 NTSTATUS status;
59 DATA_BLOB blob;
60 struct data_blob_list_item *q;
61 BOOL enable_wrap = conn->enable_wrap;
63 call = talloc(conn, struct ldapsrv_call);
64 if (!call) {
65 ldapsrv_terminate_connection(conn, "no memory");
66 return;
69 call->request = talloc_steal(call, msg);
70 call->conn = conn;
71 call->replies = NULL;
73 /* make the call */
74 status = ldapsrv_do_call(call);
75 if (!NT_STATUS_IS_OK(status)) {
76 goto failed;
79 blob = data_blob(NULL, 0);
81 if (call->replies == NULL) {
82 talloc_free(call);
83 return;
86 /* build all the replies into a single blob */
87 while (call->replies) {
88 DATA_BLOB b;
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));
93 goto failed;
96 status = data_blob_append(call, &blob, b.data, b.length);
97 data_blob_free(&b);
99 if (!NT_STATUS_IS_OK(status)) goto failed;
101 DLIST_REMOVE(call->replies, call->replies);
104 /* possibly encrypt/sign the reply */
105 if (enable_wrap) {
106 DATA_BLOB wrapped;
108 status = gensec_wrap(conn->gensec, call, &blob, &wrapped);
109 if (!NT_STATUS_IS_OK(status)) {
110 goto failed;
112 data_blob_free(&blob);
113 blob = data_blob_talloc(call, NULL, wrapped.length + 4);
114 if (blob.data == NULL) {
115 goto failed;
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;
125 q->blob = blob;
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 *);
132 talloc_free(call);
133 return;
135 failed:
136 talloc_free(call);
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");
149 return;
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;
157 if (msg == NULL) {
158 ldapsrv_terminate_connection(conn, "out of memory");
159 return;
162 if (ldap_decode(&asn1, msg)) {
163 ldapsrv_process_message(conn, msg);
164 } else {
165 if (asn1.ofs < asn1.length) {
166 ldapsrv_terminate_connection(conn, "ldap_decode failed");
167 return;
169 asn1.ofs = saved_ofs;
170 talloc_free(msg);
171 break;
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);
182 asn1_free(&asn1);
187 try and decode/process wrapped data
189 static void ldapsrv_try_decode_wrapped(struct ldapsrv_connection *conn)
191 uint32_t len;
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);
199 NTSTATUS status;
201 if (msg == NULL) {
202 ldapsrv_terminate_connection(conn, "out of memory");
203 return;
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");
212 return;
215 if (!asn1_load(&asn1, unwrapped)) {
216 ldapsrv_terminate_connection(conn, "out of memory");
217 return;
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");
227 return;
230 talloc_free(msg);
231 asn1_free(&asn1);
233 if (conn->partial.length == len + 4) {
234 data_blob_free(&conn->partial);
235 } else {
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);
251 NTSTATUS status;
252 size_t npending, nread;
254 if (conn->processing) {
255 EVENT_FD_NOT_READABLE(c->event.fde);
256 return;
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");
263 return;
265 if (npending == 0) {
266 ldapsrv_terminate_connection(conn, "EOF from client");
267 return;
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");
274 return;
277 /* receive the data */
278 status = tls_socket_recv(conn->tls, conn->partial.data + conn->partial.length,
279 npending, &nread);
280 if (NT_STATUS_IS_ERR(status)) {
281 ldapsrv_terminate_connection(conn, "socket_recv() failed");
282 return;
284 if (!NT_STATUS_IS_OK(status)) {
285 return;
287 if (nread == 0) {
288 ldapsrv_terminate_connection(conn, "EOF from client");
289 return;
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);
297 } else {
298 ldapsrv_try_decode_plain(conn);
300 conn->processing = False;
302 EVENT_FD_READABLE(c->event.fde);
304 if (conn->terminate) {
305 if (conn->tls) {
306 talloc_free(conn->tls);
307 conn->tls = NULL;
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;
322 size_t nsent;
323 NTSTATUS status;
325 status = tls_socket_send(conn->tls, &q->blob, &nsent);
326 if (NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES)) {
327 break;
329 if (!NT_STATUS_IS_OK(status)) {
330 ldapsrv_terminate_connection(conn, "socket_send error");
331 return;
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) {
345 if (conn->tls) {
346 talloc_free(conn->tls);
347 conn->tls = NULL;
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;
364 int port;
366 conn = talloc_zero(c, struct ldapsrv_connection);
367 if (!conn) {
368 stream_terminate_connection(c, "ldapsrv_accept: out of memory");
369 return;
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;
378 c->private = conn;
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);
386 if (!conn->tls) {
387 ldapsrv_terminate_connection(conn, "ldapsrv_accept: tls_init_server() failed");
388 return;
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");
394 return;
397 rootDSE_part = talloc(conn, struct ldapsrv_partition);
398 if (rootDSE_part == NULL) {
399 ldapsrv_terminate_connection(conn, "talloc failed");
400 return;
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);
413 if (part == NULL) {
414 ldapsrv_terminate_connection(conn, "talloc failed");
415 return;
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 = {
431 .name = "ldap",
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)
444 uint16_t port = 389;
445 NTSTATUS status;
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 */
456 port = 636;
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 return status;
469 open the ldap server sockets
471 static void ldapsrv_task_init(struct task_server *task)
473 struct ldapsrv_service *ldap_service;
474 NTSTATUS status;
476 ldap_service = talloc_zero(task, struct ldapsrv_service);
477 if (ldap_service == NULL) goto failed;
479 ldap_service->tls_params = tls_initialise(ldap_service);
480 if (ldap_service->tls_params == NULL) goto failed;
482 if (lp_interfaces() && lp_bind_interfaces_only()) {
483 int num_interfaces = iface_count();
484 int i;
486 /* We have been given an interfaces line, and been
487 told to only bind to those interfaces. Create a
488 socket per interface and bind to only these.
490 for(i = 0; i < num_interfaces; i++) {
491 const char *address = iface_n_ip(i);
492 status = add_socket(task->event_ctx, task->model_ops, address, ldap_service);
493 if (!NT_STATUS_IS_OK(status)) goto failed;
495 } else {
496 status = add_socket(task->event_ctx, task->model_ops, lp_socket_address(), ldap_service);
497 if (!NT_STATUS_IS_OK(status)) goto failed;
500 return;
502 failed:
503 task_server_terminate(task, "Failed to startup ldap server task");
507 called on startup of the web server service It's job is to start
508 listening on all configured sockets
510 static NTSTATUS ldapsrv_init(struct event_context *event_context,
511 const struct model_ops *model_ops)
513 return task_server_startup(event_context, model_ops, ldapsrv_task_init);
517 NTSTATUS server_service_ldap_init(void)
519 return register_server_service("ldap", ldapsrv_init);