r10353: Fix typo
[ira/wip.git] / source / 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
37 /*
38   close the socket and shutdown a server_context
39 */
40 static void ldapsrv_terminate_connection(struct ldapsrv_connection *conn, 
41                                          const char *reason)
42 {
43         if (conn->tls) {
44                 talloc_free(conn->tls);
45                 conn->tls = NULL;
46         }
47         stream_terminate_connection(conn->connection, reason);
48 }
49
50 /*
51   process a decoded ldap message
52 */
53 static void ldapsrv_process_message(struct ldapsrv_connection *conn,
54                                     struct ldap_message *msg)
55 {
56         struct ldapsrv_call *call;
57         NTSTATUS status;
58         DATA_BLOB blob;
59         struct data_blob_list_item *q;
60         BOOL enable_wrap = conn->enable_wrap;
61
62         call = talloc(conn, struct ldapsrv_call);
63         if (!call) {
64                 ldapsrv_terminate_connection(conn, "no memory");
65                 return;         
66         }
67         
68         call->request = talloc_steal(call, msg);
69         call->conn = conn;
70         call->replies = NULL;
71
72         /* make the call */
73         status = ldapsrv_do_call(call);
74         if (!NT_STATUS_IS_OK(status)) {
75                 goto failed;
76         }
77         
78         blob = data_blob(NULL, 0);
79
80         if (call->replies == NULL) {
81                 talloc_free(call);
82                 return;
83         }
84
85         /* build all the replies into a single blob */
86         while (call->replies) {
87                 DATA_BLOB b;
88
89                 msg = call->replies->msg;
90                 if (!ldap_encode(msg, &b, call)) {
91                         DEBUG(0,("Failed to encode ldap reply of type %d\n", msg->type));
92                         goto failed;
93                 }
94
95                 status = data_blob_append(call, &blob, b.data, b.length);
96                 data_blob_free(&b);
97
98                 if (!NT_STATUS_IS_OK(status)) goto failed;
99
100                 DLIST_REMOVE(call->replies, call->replies);
101         }
102
103         /* possibly encrypt/sign the reply */
104         if (enable_wrap) {
105                 DATA_BLOB wrapped;
106
107                 status = gensec_wrap(conn->gensec, call, &blob, &wrapped);
108                 if (!NT_STATUS_IS_OK(status)) {
109                         goto failed;
110                 }
111                 data_blob_free(&blob);
112                 blob = data_blob_talloc(call, NULL, wrapped.length + 4);
113                 if (blob.data == NULL) {
114                         goto failed;
115                 }
116                 RSIVAL(blob.data, 0, wrapped.length);
117                 memcpy(blob.data+4, wrapped.data, wrapped.length);
118                 data_blob_free(&wrapped);
119         }
120
121         q = talloc(conn, struct data_blob_list_item);
122         if (q == NULL) goto failed;
123
124         q->blob = blob;
125         talloc_steal(q, blob.data);
126         
127         if (conn->send_queue == NULL) {
128                 EVENT_FD_WRITEABLE(conn->connection->event.fde);
129         }
130         DLIST_ADD_END(conn->send_queue, q, struct data_blob_list_item *);
131         talloc_free(call);
132         return;
133
134 failed:
135         talloc_free(call);
136 }
137
138
139 /*
140   try and decode the partial input buffer
141 */
142 static void ldapsrv_try_decode_plain(struct ldapsrv_connection *conn)
143 {
144         struct asn1_data asn1;
145
146         if (!asn1_load(&asn1, conn->partial)) {
147                 ldapsrv_terminate_connection(conn, "out of memory");
148                 return;
149         }
150
151         /* try and decode - this will fail if we don't have a full packet yet */
152         while (asn1.ofs < asn1.length) {
153                 struct ldap_message *msg = talloc(conn, struct ldap_message);
154                 off_t saved_ofs = asn1.ofs;
155                         
156                 if (msg == NULL) {
157                         ldapsrv_terminate_connection(conn, "out of memory");
158                         return;
159                 }
160
161                 if (ldap_decode(&asn1, msg)) {
162                         ldapsrv_process_message(conn, msg);
163                 } else {
164                         if (asn1.ofs < asn1.length) {
165                                 ldapsrv_terminate_connection(conn, "ldap_decode failed");
166                                 return;
167                         }
168                         asn1.ofs = saved_ofs;
169                         talloc_free(msg);
170                         break;
171                 }
172         }
173
174         /* keep any remaining data in conn->partial */
175         data_blob_free(&conn->partial);
176         if (asn1.ofs != asn1.length) {
177                 conn->partial = data_blob_talloc(conn, 
178                                                  asn1.data + asn1.ofs, 
179                                                  asn1.length - asn1.ofs);
180         }
181         asn1_free(&asn1);
182 }
183
184
185 /*
186   try and decode/process wrapped data
187 */
188 static void ldapsrv_try_decode_wrapped(struct ldapsrv_connection *conn)
189 {
190         uint32_t len;
191
192         /* keep decoding while we have a full wrapped packet */
193         while (conn->partial.length >= 4 &&
194                (len=RIVAL(conn->partial.data, 0)) <= conn->partial.length-4) {
195                 DATA_BLOB wrapped, unwrapped;
196                 struct asn1_data asn1;
197                 struct ldap_message *msg = talloc(conn, struct ldap_message);
198                 NTSTATUS status;
199
200                 if (msg == NULL) {
201                         ldapsrv_terminate_connection(conn, "out of memory");
202                         return;
203                 }
204
205                 wrapped.data   = conn->partial.data+4;
206                 wrapped.length = len;
207
208                 status = gensec_unwrap(conn->gensec, msg, &wrapped, &unwrapped);
209                 if (!NT_STATUS_IS_OK(status)) {
210                         ldapsrv_terminate_connection(conn, "gensec unwrap failed");
211                         return;
212                 }
213
214                 if (!asn1_load(&asn1, unwrapped)) {
215                         ldapsrv_terminate_connection(conn, "out of memory");
216                         return;
217                 }
218
219                 while (ldap_decode(&asn1, msg)) {
220                         ldapsrv_process_message(conn, msg);
221                         msg = talloc(conn, struct ldap_message);
222                 }
223
224                 if (asn1.ofs < asn1.length) {
225                         ldapsrv_terminate_connection(conn, "ldap_decode failed");
226                         return;
227                 }
228                 
229                 talloc_free(msg);
230                 asn1_free(&asn1);
231
232                 if (conn->partial.length == len + 4) {
233                         data_blob_free(&conn->partial);
234                 } else {
235                         memmove(conn->partial.data, conn->partial.data+len+4,
236                                 conn->partial.length - (len+4));
237                         conn->partial.length -= len + 4;
238                 }
239         }
240 }
241
242
243 /*
244   called when a LDAP socket becomes readable
245 */
246 static void ldapsrv_recv(struct stream_connection *c, uint16_t flags)
247 {
248         struct ldapsrv_connection *conn = 
249                 talloc_get_type(c->private, struct ldapsrv_connection);
250         NTSTATUS status;
251         size_t npending, nread;
252
253         if (conn->processing) {
254                 EVENT_FD_NOT_READABLE(c->event.fde);
255                 return;
256         }
257
258         /* work out how much data is pending */
259         status = tls_socket_pending(conn->tls, &npending);
260         if (!NT_STATUS_IS_OK(status)) {
261                 ldapsrv_terminate_connection(conn, "socket_pending() failed");
262                 return;
263         }
264         if (npending == 0) {
265                 ldapsrv_terminate_connection(conn, "EOF from client");
266                 return;
267         }
268
269         conn->partial.data = talloc_realloc_size(conn, conn->partial.data, 
270                                                  conn->partial.length + npending);
271         if (conn->partial.data == NULL) {
272                 ldapsrv_terminate_connection(conn, "out of memory");
273                 return;
274         }
275
276         /* receive the data */
277         status = tls_socket_recv(conn->tls, conn->partial.data + conn->partial.length,
278                                  npending, &nread);
279         if (NT_STATUS_IS_ERR(status)) {
280                 ldapsrv_terminate_connection(conn, "socket_recv() failed");
281                 return;
282         }
283         if (!NT_STATUS_IS_OK(status)) {
284                 return;
285         }
286         if (nread == 0) {
287                 ldapsrv_terminate_connection(conn, "EOF from client");
288                 return;
289         }
290         conn->partial.length += nread;
291
292         conn->processing = True;
293         /* see if we can decode what we have */
294         if (conn->enable_wrap) {
295                 ldapsrv_try_decode_wrapped(conn);
296         } else {
297                 ldapsrv_try_decode_plain(conn);
298         }
299         conn->processing = False;
300
301         EVENT_FD_READABLE(c->event.fde);
302 }
303         
304 /*
305   called when a LDAP socket becomes writable
306 */
307 static void ldapsrv_send(struct stream_connection *c, uint16_t flags)
308 {
309         struct ldapsrv_connection *conn = 
310                 talloc_get_type(c->private, struct ldapsrv_connection);
311         while (conn->send_queue) {
312                 struct data_blob_list_item *q = conn->send_queue;
313                 size_t nsent;
314                 NTSTATUS status;
315
316                 status = tls_socket_send(conn->tls, &q->blob, &nsent);
317                 if (NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES)) {
318                         break;
319                 }
320                 if (!NT_STATUS_IS_OK(status)) {
321                         ldapsrv_terminate_connection(conn, "socket_send error");
322                         return;
323                 }
324
325                 q->blob.data += nsent;
326                 q->blob.length -= nsent;
327                 if (q->blob.length == 0) {
328                         DLIST_REMOVE(conn->send_queue, q);
329                 }
330         }
331         if (conn->send_queue == NULL) {
332                 EVENT_FD_NOT_WRITEABLE(c->event.fde);
333         }
334 }
335
336 /*
337   initialise a server_context from a open socket and register a event handler
338   for reading from that socket
339 */
340 static void ldapsrv_accept(struct stream_connection *c)
341 {
342         struct ldapsrv_service *ldapsrv_service = 
343                 talloc_get_type(c->private, struct ldapsrv_service);
344         struct ldapsrv_connection *conn;
345         int port;
346
347         conn = talloc_zero(c, struct ldapsrv_connection);
348         if (!conn) {
349                 stream_terminate_connection(c, "ldapsrv_accept: out of memory");
350                 return;
351         }
352
353         conn->enable_wrap = False;
354         conn->partial     = data_blob(NULL, 0);
355         conn->send_queue  = NULL;
356         conn->connection  = c;
357         conn->service     = ldapsrv_service;
358         conn->processing  = False;
359         c->private        = conn;
360
361         port = socket_get_my_port(c->socket);
362
363         /* note that '0' is a ASN1_SEQUENCE(0), which is the first byte on
364            any ldap connection */
365         conn->tls = tls_init_server(ldapsrv_service->tls_params, c->socket, 
366                                     c->event.fde, NULL, port != 389);
367         if (!conn->tls) {
368                 ldapsrv_terminate_connection(conn, "ldapsrv_accept: tls_init_server() failed");
369                 return;
370         }
371
372         irpc_add_name(c->msg_ctx, "ldap_server");
373 }
374
375 static const struct stream_server_ops ldap_stream_ops = {
376         .name                   = "ldap",
377         .accept_connection      = ldapsrv_accept,
378         .recv_handler           = ldapsrv_recv,
379         .send_handler           = ldapsrv_send,
380 };
381
382 /*
383   add a socket address to the list of events, one event per port
384 */
385 static NTSTATUS add_socket(struct event_context *event_context,
386                            const struct model_ops *model_ops,
387                            const char *address, struct ldapsrv_service *ldap_service)
388 {
389         uint16_t port = 389;
390         NTSTATUS status;
391
392         status = stream_setup_socket(event_context, model_ops, &ldap_stream_ops, 
393                                      "ipv4", address, &port, ldap_service);
394         if (!NT_STATUS_IS_OK(status)) {
395                 DEBUG(0,("ldapsrv failed to bind to %s:%u - %s\n",
396                          address, port, nt_errstr(status)));
397         }
398
399         if (tls_support(ldap_service->tls_params)) {
400                 /* add ldaps server */
401                 port = 636;
402                 status = stream_setup_socket(event_context, model_ops, &ldap_stream_ops, 
403                                              "ipv4", address, &port, ldap_service);
404                 if (!NT_STATUS_IS_OK(status)) {
405                         DEBUG(0,("ldapsrv failed to bind to %s:%u - %s\n",
406                                  address, port, nt_errstr(status)));
407                 }
408         }
409
410         return status;
411 }
412
413 /*
414   open the ldap server sockets
415 */
416 static void ldapsrv_task_init(struct task_server *task)
417 {       
418         struct ldapsrv_service *ldap_service;
419         struct ldapsrv_partition *rootDSE_part;
420         struct ldapsrv_partition *part;
421         NTSTATUS status;
422
423         ldap_service = talloc_zero(task, struct ldapsrv_service);
424         if (ldap_service == NULL) goto failed;
425
426         ldap_service->tls_params = tls_initialise(ldap_service);
427         if (ldap_service->tls_params == NULL) goto failed;
428
429         rootDSE_part = talloc(ldap_service, struct ldapsrv_partition);
430         if (rootDSE_part == NULL) goto failed;
431
432         rootDSE_part->base_dn = ""; /* RootDSE */
433         rootDSE_part->ops = ldapsrv_get_rootdse_partition_ops();
434
435         ldap_service->rootDSE = rootDSE_part;
436         DLIST_ADD_END(ldap_service->partitions, rootDSE_part, struct ldapsrv_partition *);
437
438         part = talloc(ldap_service, struct ldapsrv_partition);
439         if (part == NULL) goto failed;
440
441         part->base_dn = "*"; /* default partition */
442         if (lp_parm_bool(-1, "ldapsrv", "hacked", False)) {
443                 part->ops = ldapsrv_get_hldb_partition_ops();
444         } else {
445                 part->ops = ldapsrv_get_sldb_partition_ops();
446         }
447
448         ldap_service->default_partition = part;
449         DLIST_ADD_END(ldap_service->partitions, part, struct ldapsrv_partition *);
450
451         if (lp_interfaces() && lp_bind_interfaces_only()) {
452                 int num_interfaces = iface_count();
453                 int i;
454
455                 /* We have been given an interfaces line, and been 
456                    told to only bind to those interfaces. Create a
457                    socket per interface and bind to only these.
458                 */
459                 for(i = 0; i < num_interfaces; i++) {
460                         const char *address = iface_n_ip(i);
461                         status = add_socket(task->event_ctx, task->model_ops, address, ldap_service);
462                         if (!NT_STATUS_IS_OK(status)) goto failed;
463                 }
464         } else {
465                 status = add_socket(task->event_ctx, task->model_ops, lp_socket_address(), ldap_service);
466                 if (!NT_STATUS_IS_OK(status)) goto failed;
467         }
468
469         return;
470
471 failed:
472         task_server_terminate(task, "Failed to startup ldap server task");      
473 }
474
475 /*
476   called on startup of the web server service It's job is to start
477   listening on all configured sockets
478 */
479 static NTSTATUS ldapsrv_init(struct event_context *event_context, 
480                              const struct model_ops *model_ops)
481 {       
482         return task_server_startup(event_context, model_ops, ldapsrv_task_init);
483 }
484
485
486 NTSTATUS server_service_ldap_init(void)
487 {
488         return register_server_service("ldap", ldapsrv_init);
489 }