r11955: got rid of the old rootDSE code in the ldap server.
[bbaumbach/samba-autobuild/.git] / source4 / ldap_server / ldap_server.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    LDAP server
5
6    Copyright (C) Andrew Tridgell 2005
7    Copyright (C) Volker Lendecke 2004
8    Copyright (C) Stefan Metzmacher 2004
9    
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.
14    
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.
19    
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.
23 */
24
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"
36 #include "lib/stream/packet.h"
37
38 /*
39   close the socket and shutdown a server_context
40 */
41 static void ldapsrv_terminate_connection(struct ldapsrv_connection *conn, 
42                                          const char *reason)
43 {
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
48            functions */
49         conn->terminate = reason;
50 }
51
52 /*
53   handle packet errors
54 */
55 static void ldapsrv_error_handler(void *private, NTSTATUS status)
56 {
57         struct ldapsrv_connection *conn = talloc_get_type(private, 
58                                                           struct ldapsrv_connection);
59         ldapsrv_terminate_connection(conn, nt_errstr(status));
60 }
61
62 /*
63   process a decoded ldap message
64 */
65 static void ldapsrv_process_message(struct ldapsrv_connection *conn,
66                                     struct ldap_message *msg)
67 {
68         struct ldapsrv_call *call;
69         NTSTATUS status;
70         DATA_BLOB blob;
71         BOOL enable_wrap = conn->enable_wrap;
72
73         call = talloc(conn, struct ldapsrv_call);
74         if (!call) {
75                 ldapsrv_terminate_connection(conn, "no memory");
76                 return;         
77         }
78         
79         call->request = talloc_steal(call, msg);
80         call->conn = conn;
81         call->replies = NULL;
82
83         /* make the call */
84         status = ldapsrv_do_call(call);
85         if (!NT_STATUS_IS_OK(status)) {
86                 goto failed;
87         }
88         
89         blob = data_blob(NULL, 0);
90
91         if (call->replies == NULL) {
92                 talloc_free(call);
93                 return;
94         }
95
96         /* build all the replies into a single blob */
97         while (call->replies) {
98                 DATA_BLOB b;
99
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));
103                         goto failed;
104                 }
105
106                 status = data_blob_append(call, &blob, b.data, b.length);
107                 data_blob_free(&b);
108
109                 if (!NT_STATUS_IS_OK(status)) goto failed;
110
111                 DLIST_REMOVE(call->replies, call->replies);
112         }
113
114         /* possibly encrypt/sign the reply */
115         if (enable_wrap) {
116                 DATA_BLOB wrapped;
117
118                 status = gensec_wrap(conn->gensec, call, &blob, &wrapped);
119                 if (!NT_STATUS_IS_OK(status)) {
120                         goto failed;
121                 }
122                 data_blob_free(&blob);
123                 blob = data_blob_talloc(call, NULL, wrapped.length + 4);
124                 if (blob.data == NULL) {
125                         goto failed;
126                 }
127                 RSIVAL(blob.data, 0, wrapped.length);
128                 memcpy(blob.data+4, wrapped.data, wrapped.length);
129                 data_blob_free(&wrapped);
130         }
131
132         packet_send(conn->packet, blob);
133         talloc_free(call);
134         return;
135
136 failed:
137         talloc_free(call);
138 }
139
140
141 /*
142   decode the input buffer
143 */
144 static NTSTATUS ldapsrv_decode_plain(struct ldapsrv_connection *conn, DATA_BLOB blob)
145 {
146         struct asn1_data asn1;
147         struct ldap_message *msg = talloc(conn, struct ldap_message);
148
149         if (msg == NULL) {
150                 return NT_STATUS_NO_MEMORY;
151         }
152
153         if (!asn1_load(&asn1, blob)) {
154                 return NT_STATUS_NO_MEMORY;
155         }
156
157         if (!ldap_decode(&asn1, msg)) {
158                 asn1_free(&asn1);
159                 return NT_STATUS_LDAP(LDAP_PROTOCOL_ERROR);
160         }
161
162         data_blob_free(&blob);
163         ldapsrv_process_message(conn, msg);
164         asn1_free(&asn1);
165         return NT_STATUS_OK;
166 }
167
168
169 /*
170   decode/process wrapped data
171 */
172 static NTSTATUS ldapsrv_decode_wrapped(struct ldapsrv_connection *conn, 
173                                        DATA_BLOB blob)
174 {
175         DATA_BLOB wrapped, unwrapped;
176         struct asn1_data asn1;
177         struct ldap_message *msg = talloc(conn, struct ldap_message);
178         NTSTATUS status;
179
180         if (msg == NULL) {
181                 return NT_STATUS_NO_MEMORY;
182         }
183
184         wrapped = data_blob_const(blob.data+4, blob.length-4);
185
186         status = gensec_unwrap(conn->gensec, msg, &wrapped, &unwrapped);
187         if (!NT_STATUS_IS_OK(status)) {
188                 return NT_STATUS_LDAP(LDAP_PROTOCOL_ERROR);
189         }
190
191         data_blob_free(&blob);
192
193         if (!asn1_load(&asn1, unwrapped)) {
194                 return NT_STATUS_LDAP(LDAP_PROTOCOL_ERROR);
195         }
196
197         while (ldap_decode(&asn1, msg)) {
198                 ldapsrv_process_message(conn, msg);
199                 msg = talloc(conn, struct ldap_message);
200         }
201
202         if (asn1.ofs < asn1.length) {
203                 return NT_STATUS_LDAP(LDAP_PROTOCOL_ERROR);
204         }
205                 
206         talloc_free(msg);
207         asn1_free(&asn1);
208
209         return NT_STATUS_OK;
210 }
211
212 /*
213   decode/process data
214 */
215 static NTSTATUS ldapsrv_decode(void *private, DATA_BLOB blob)
216 {
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);
221         }
222         return ldapsrv_decode_plain(conn, blob);
223 }
224
225 /*
226   called when a LDAP socket becomes readable
227 */
228 static void ldapsrv_recv(struct stream_connection *c, uint16_t flags)
229 {
230         struct ldapsrv_connection *conn = 
231                 talloc_get_type(c->private, struct ldapsrv_connection);
232         
233         packet_recv(conn->packet);
234
235         if (conn->terminate) {
236                 if (conn->tls) {
237                         talloc_free(conn->tls);
238                         conn->tls = NULL;
239                 }
240                 stream_terminate_connection(conn->connection, conn->terminate);
241         }
242 }
243
244 /*
245   check if a blob is a complete ldap packet
246   handle wrapper or unwrapped connections
247 */
248 NTSTATUS ldapsrv_complete_packet(void *private, DATA_BLOB blob, size_t *size)
249 {
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);
254         }
255         return ldap_full_packet(private, blob, size);
256 }
257         
258 /*
259   called when a LDAP socket becomes writable
260 */
261 static void ldapsrv_send(struct stream_connection *c, uint16_t flags)
262 {
263         struct ldapsrv_connection *conn = 
264                 talloc_get_type(c->private, struct ldapsrv_connection);
265         
266         packet_queue_run(conn->packet);
267
268         if (conn->terminate) {
269                 if (conn->tls) {
270                         talloc_free(conn->tls);
271                         conn->tls = NULL;
272                 }
273                 stream_terminate_connection(conn->connection, conn->terminate);
274         }
275 }
276
277 /*
278   initialise a server_context from a open socket and register a event handler
279   for reading from that socket
280 */
281 static void ldapsrv_accept(struct stream_connection *c)
282 {
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;
287         int port;
288
289         conn = talloc_zero(c, struct ldapsrv_connection);
290         if (!conn) {
291                 stream_terminate_connection(c, "ldapsrv_accept: out of memory");
292                 return;
293         }
294
295         conn->enable_wrap = False;
296         conn->packet      = NULL;
297         conn->connection  = c;
298         conn->service     = ldapsrv_service;
299         c->private        = conn;
300
301         port = socket_get_my_port(c->socket);
302
303         conn->tls = tls_init_server(ldapsrv_service->tls_params, c->socket, 
304                                     c->event.fde, NULL, port != 389);
305         if (!conn->tls) {
306                 ldapsrv_terminate_connection(conn, "ldapsrv_accept: tls_init_server() failed");
307                 goto done;
308         }
309
310         conn->packet = packet_init(conn);
311         if (conn->packet == NULL) {
312                 ldapsrv_terminate_connection(conn, "out of memory");
313                 goto done;
314         }
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);
323
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");
327                 goto done;
328         }
329
330         part = talloc(conn, struct ldapsrv_partition);
331         if (part == NULL) {
332                 ldapsrv_terminate_connection(conn, "talloc failed");
333                 goto done;
334         }
335
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");
340                 goto done;
341         }
342
343         conn->default_partition = part;
344         DLIST_ADD_END(conn->partitions, part, struct ldapsrv_partition *);
345
346         irpc_add_name(c->msg_ctx, "ldap_server");
347
348 done:
349         if (conn->terminate) {
350                 if (conn->tls) {
351                         talloc_free(conn->tls);
352                         conn->tls = NULL;
353                 }
354                 stream_terminate_connection(conn->connection, conn->terminate);
355         }
356 }
357
358 static const struct stream_server_ops ldap_stream_ops = {
359         .name                   = "ldap",
360         .accept_connection      = ldapsrv_accept,
361         .recv_handler           = ldapsrv_recv,
362         .send_handler           = ldapsrv_send,
363 };
364
365 /*
366   add a socket address to the list of events, one event per port
367 */
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)
371 {
372         uint16_t port = 389;
373         NTSTATUS status;
374
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)));
380         }
381
382         if (tls_support(ldap_service->tls_params)) {
383                 /* add ldaps server */
384                 port = 636;
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)));
390                 }
391         }
392
393         /* if we are a PDC, then also enable the global catalog server port, 3268 */
394         if (lp_server_role() == ROLE_DOMAIN_PDC) {
395                 port = 3268;
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)));
401                 }
402         }
403
404         return status;
405 }
406
407 /*
408   open the ldap server sockets
409 */
410 static void ldapsrv_task_init(struct task_server *task)
411 {       
412         struct ldapsrv_service *ldap_service;
413         NTSTATUS status;
414
415         ldap_service = talloc_zero(task, struct ldapsrv_service);
416         if (ldap_service == NULL) goto failed;
417
418         ldap_service->tls_params = tls_initialise(ldap_service);
419         if (ldap_service->tls_params == NULL) goto failed;
420
421         if (lp_interfaces() && lp_bind_interfaces_only()) {
422                 int num_interfaces = iface_count();
423                 int i;
424
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.
428                 */
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;
433                 }
434         } else {
435                 status = add_socket(task->event_ctx, task->model_ops, lp_socket_address(), ldap_service);
436                 if (!NT_STATUS_IS_OK(status)) goto failed;
437         }
438
439         return;
440
441 failed:
442         task_server_terminate(task, "Failed to startup ldap server task");      
443 }
444
445 /*
446   called on startup of the web server service It's job is to start
447   listening on all configured sockets
448 */
449 static NTSTATUS ldapsrv_init(struct event_context *event_context, 
450                              const struct model_ops *model_ops)
451 {       
452         return task_server_startup(event_context, model_ops, ldapsrv_task_init);
453 }
454
455
456 NTSTATUS server_service_ldap_init(void)
457 {
458         return register_server_service("ldap", ldapsrv_init);
459 }