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"
36 #include "lib/stream/packet.h"
39 close the socket and shutdown a server_context
41 static void ldapsrv_terminate_connection(struct ldapsrv_connection *conn,
44 /* we don't actually do the stream termination here as the
45 recv/send functions dereference the connection after the
46 packet processing callbacks. Instead we mark it for
47 termination and do the real termination in the send/recv
49 conn->terminate = reason;
55 static void ldapsrv_error_handler(void *private, NTSTATUS status)
57 struct ldapsrv_connection *conn = talloc_get_type(private,
58 struct ldapsrv_connection);
59 ldapsrv_terminate_connection(conn, nt_errstr(status));
63 process a decoded ldap message
65 static void ldapsrv_process_message(struct ldapsrv_connection *conn,
66 struct ldap_message *msg)
68 struct ldapsrv_call *call;
71 BOOL enable_wrap = conn->enable_wrap;
73 call = talloc(conn, struct ldapsrv_call);
75 ldapsrv_terminate_connection(conn, "no memory");
79 call->request = talloc_steal(call, msg);
84 status = ldapsrv_do_call(call);
85 if (!NT_STATUS_IS_OK(status)) {
89 blob = data_blob(NULL, 0);
91 if (call->replies == NULL) {
96 /* build all the replies into a single blob */
97 while (call->replies) {
100 msg = call->replies->msg;
101 if (!ldap_encode(msg, &b, call)) {
102 DEBUG(0,("Failed to encode ldap reply of type %d\n", msg->type));
106 status = data_blob_append(call, &blob, b.data, b.length);
109 if (!NT_STATUS_IS_OK(status)) goto failed;
111 DLIST_REMOVE(call->replies, call->replies);
114 /* possibly encrypt/sign the reply */
118 status = gensec_wrap(conn->gensec, call, &blob, &wrapped);
119 if (!NT_STATUS_IS_OK(status)) {
122 data_blob_free(&blob);
123 blob = data_blob_talloc(call, NULL, wrapped.length + 4);
124 if (blob.data == NULL) {
127 RSIVAL(blob.data, 0, wrapped.length);
128 memcpy(blob.data+4, wrapped.data, wrapped.length);
129 data_blob_free(&wrapped);
132 packet_send(conn->packet, blob);
142 decode the input buffer
144 static NTSTATUS ldapsrv_decode_plain(struct ldapsrv_connection *conn, DATA_BLOB blob)
146 struct asn1_data asn1;
147 struct ldap_message *msg = talloc(conn, struct ldap_message);
150 return NT_STATUS_NO_MEMORY;
153 if (!asn1_load(&asn1, blob)) {
154 return NT_STATUS_NO_MEMORY;
157 if (!ldap_decode(&asn1, msg)) {
159 return NT_STATUS_LDAP(LDAP_PROTOCOL_ERROR);
162 data_blob_free(&blob);
163 ldapsrv_process_message(conn, msg);
170 decode/process wrapped data
172 static NTSTATUS ldapsrv_decode_wrapped(struct ldapsrv_connection *conn,
175 DATA_BLOB wrapped, unwrapped;
176 struct asn1_data asn1;
177 struct ldap_message *msg = talloc(conn, struct ldap_message);
181 return NT_STATUS_NO_MEMORY;
184 wrapped = data_blob_const(blob.data+4, blob.length-4);
186 status = gensec_unwrap(conn->gensec, msg, &wrapped, &unwrapped);
187 if (!NT_STATUS_IS_OK(status)) {
188 return NT_STATUS_LDAP(LDAP_PROTOCOL_ERROR);
191 data_blob_free(&blob);
193 if (!asn1_load(&asn1, unwrapped)) {
194 return NT_STATUS_LDAP(LDAP_PROTOCOL_ERROR);
197 while (ldap_decode(&asn1, msg)) {
198 ldapsrv_process_message(conn, msg);
199 msg = talloc(conn, struct ldap_message);
202 if (asn1.ofs < asn1.length) {
203 return NT_STATUS_LDAP(LDAP_PROTOCOL_ERROR);
215 static NTSTATUS ldapsrv_decode(void *private, DATA_BLOB blob)
217 struct ldapsrv_connection *conn = talloc_get_type(private,
218 struct ldapsrv_connection);
219 if (conn->enable_wrap) {
220 return ldapsrv_decode_wrapped(conn, blob);
222 return ldapsrv_decode_plain(conn, blob);
226 called when a LDAP socket becomes readable
228 static void ldapsrv_recv(struct stream_connection *c, uint16_t flags)
230 struct ldapsrv_connection *conn =
231 talloc_get_type(c->private, struct ldapsrv_connection);
233 packet_recv(conn->packet);
235 if (conn->terminate) {
237 talloc_free(conn->tls);
240 stream_terminate_connection(conn->connection, conn->terminate);
245 check if a blob is a complete ldap packet
246 handle wrapper or unwrapped connections
248 NTSTATUS ldapsrv_complete_packet(void *private, DATA_BLOB blob, size_t *size)
250 struct ldapsrv_connection *conn = talloc_get_type(private,
251 struct ldapsrv_connection);
252 if (conn->enable_wrap) {
253 return packet_full_request_u32(private, blob, size);
255 return ldap_full_packet(private, blob, size);
259 called when a LDAP socket becomes writable
261 static void ldapsrv_send(struct stream_connection *c, uint16_t flags)
263 struct ldapsrv_connection *conn =
264 talloc_get_type(c->private, struct ldapsrv_connection);
266 packet_queue_run(conn->packet);
268 if (conn->terminate) {
270 talloc_free(conn->tls);
273 stream_terminate_connection(conn->connection, conn->terminate);
278 initialise a server_context from a open socket and register a event handler
279 for reading from that socket
281 static void ldapsrv_accept(struct stream_connection *c)
283 struct ldapsrv_partition *part;
284 struct ldapsrv_service *ldapsrv_service =
285 talloc_get_type(c->private, struct ldapsrv_service);
286 struct ldapsrv_connection *conn;
289 conn = talloc_zero(c, struct ldapsrv_connection);
291 stream_terminate_connection(c, "ldapsrv_accept: out of memory");
295 conn->enable_wrap = False;
297 conn->connection = c;
298 conn->service = ldapsrv_service;
301 port = socket_get_my_port(c->socket);
303 conn->tls = tls_init_server(ldapsrv_service->tls_params, c->socket,
304 c->event.fde, NULL, port != 389);
306 ldapsrv_terminate_connection(conn, "ldapsrv_accept: tls_init_server() failed");
310 conn->packet = packet_init(conn);
311 if (conn->packet == NULL) {
312 ldapsrv_terminate_connection(conn, "out of memory");
315 packet_set_private(conn->packet, conn);
316 packet_set_tls(conn->packet, conn->tls);
317 packet_set_callback(conn->packet, ldapsrv_decode);
318 packet_set_full_request(conn->packet, ldapsrv_complete_packet);
319 packet_set_error_handler(conn->packet, ldapsrv_error_handler);
320 packet_set_event_context(conn->packet, c->event.ctx);
321 packet_set_fde(conn->packet, c->event.fde);
322 packet_set_serialise(conn->packet);
324 /* Connections start out anonymous */
325 if (!NT_STATUS_IS_OK(auth_anonymous_session_info(conn, &conn->session_info))) {
326 ldapsrv_terminate_connection(conn, "failed to setup anonymous session info");
330 part = talloc(conn, struct ldapsrv_partition);
332 ldapsrv_terminate_connection(conn, "talloc failed");
336 part->base_dn = "*"; /* default partition */
337 part->ops = ldapsrv_get_sldb_partition_ops();
338 if (!NT_STATUS_IS_OK(part->ops->Init(part, conn))) {
339 ldapsrv_terminate_connection(conn, "default partition Init failed");
343 conn->default_partition = part;
344 DLIST_ADD_END(conn->partitions, part, struct ldapsrv_partition *);
346 irpc_add_name(c->msg_ctx, "ldap_server");
349 if (conn->terminate) {
351 talloc_free(conn->tls);
354 stream_terminate_connection(conn->connection, conn->terminate);
358 static const struct stream_server_ops ldap_stream_ops = {
360 .accept_connection = ldapsrv_accept,
361 .recv_handler = ldapsrv_recv,
362 .send_handler = ldapsrv_send,
366 add a socket address to the list of events, one event per port
368 static NTSTATUS add_socket(struct event_context *event_context,
369 const struct model_ops *model_ops,
370 const char *address, struct ldapsrv_service *ldap_service)
375 status = stream_setup_socket(event_context, model_ops, &ldap_stream_ops,
376 "ipv4", address, &port, ldap_service);
377 if (!NT_STATUS_IS_OK(status)) {
378 DEBUG(0,("ldapsrv failed to bind to %s:%u - %s\n",
379 address, port, nt_errstr(status)));
382 if (tls_support(ldap_service->tls_params)) {
383 /* add ldaps server */
385 status = stream_setup_socket(event_context, model_ops, &ldap_stream_ops,
386 "ipv4", address, &port, ldap_service);
387 if (!NT_STATUS_IS_OK(status)) {
388 DEBUG(0,("ldapsrv failed to bind to %s:%u - %s\n",
389 address, port, nt_errstr(status)));
393 /* if we are a PDC, then also enable the global catalog server port, 3268 */
394 if (lp_server_role() == ROLE_DOMAIN_PDC) {
396 status = stream_setup_socket(event_context, model_ops, &ldap_stream_ops,
397 "ipv4", address, &port, ldap_service);
398 if (!NT_STATUS_IS_OK(status)) {
399 DEBUG(0,("ldapsrv failed to bind to %s:%u - %s\n",
400 address, port, nt_errstr(status)));
408 open the ldap server sockets
410 static void ldapsrv_task_init(struct task_server *task)
412 struct ldapsrv_service *ldap_service;
415 ldap_service = talloc_zero(task, struct ldapsrv_service);
416 if (ldap_service == NULL) goto failed;
418 ldap_service->tls_params = tls_initialise(ldap_service);
419 if (ldap_service->tls_params == NULL) goto failed;
421 if (lp_interfaces() && lp_bind_interfaces_only()) {
422 int num_interfaces = iface_count();
425 /* We have been given an interfaces line, and been
426 told to only bind to those interfaces. Create a
427 socket per interface and bind to only these.
429 for(i = 0; i < num_interfaces; i++) {
430 const char *address = iface_n_ip(i);
431 status = add_socket(task->event_ctx, task->model_ops, address, ldap_service);
432 if (!NT_STATUS_IS_OK(status)) goto failed;
435 status = add_socket(task->event_ctx, task->model_ops, lp_socket_address(), ldap_service);
436 if (!NT_STATUS_IS_OK(status)) goto failed;
442 task_server_terminate(task, "Failed to startup ldap server task");
446 called on startup of the web server service It's job is to start
447 listening on all configured sockets
449 static NTSTATUS ldapsrv_init(struct event_context *event_context,
450 const struct model_ops *model_ops)
452 return task_server_startup(event_context, model_ops, ldapsrv_task_init);
456 NTSTATUS server_service_ldap_init(void)
458 return register_server_service("ldap", ldapsrv_init);