r4459: GENSEC refinements:
[kamenim/samba.git] / source4 / ldap_server / ldap_server.c
1 /* 
2    Unix SMB/CIFS implementation.
3    LDAP server
4    Copyright (C) Volker Lendecke 2004
5    Copyright (C) Stefan Metzmacher 2004
6    
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11    
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16    
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22 #include "includes.h"
23 #include "events.h"
24 #include "auth/auth.h"
25 #include "dlinklist.h"
26 #include "asn_1.h"
27 #include "ldap_server/ldap_server.h"
28
29 /*
30   close the socket and shutdown a server_context
31 */
32 static void ldapsrv_terminate_connection(struct ldapsrv_connection *ldap_conn, const char *reason)
33 {
34         server_terminate_connection(ldap_conn->connection, reason);
35 }
36
37 /*
38   add a socket address to the list of events, one event per port
39 */
40 static void add_socket(struct server_service *service, 
41                        const struct model_ops *model_ops, 
42                        struct ipv4_addr *ifip)
43 {
44         struct server_socket *srv_sock;
45         uint16_t port = 389;
46         char *ip_str = talloc_strdup(service, sys_inet_ntoa(*ifip));
47
48         srv_sock = service_setup_socket(service, model_ops, "ipv4", ip_str, &port);
49
50         port = 3268;
51         srv_sock = service_setup_socket(service, model_ops, "ipv4", ip_str, &port);
52
53         talloc_free(ip_str);
54 }
55
56 /****************************************************************************
57  Open the socket communication.
58 ****************************************************************************/
59 static void ldapsrv_init(struct server_service *service,
60                          const struct model_ops *model_ops)
61 {       
62         struct ldapsrv_service *ldap_service;
63         struct ldapsrv_partition *part;
64
65         DEBUG(10,("ldapsrv_init\n"));
66
67         ldap_service = talloc_p(service, struct ldapsrv_service);
68         if (!ldap_service) {
69                 DEBUG(0,("talloc_p(service, struct ldapsrv_service) failed\n"));
70                 return;
71         }
72         ZERO_STRUCTP(ldap_service);
73
74         part = talloc_p(ldap_service, struct ldapsrv_partition);
75         if (!ldap_service) {
76                 DEBUG(0,("talloc_p(ldap_service, struct ldapsrv_partition) failed\n"));
77                 return;
78         }
79         part->base_dn = ""; /* RootDSE */
80         part->ops = ldapsrv_get_rootdse_partition_ops();
81
82         ldap_service->rootDSE = part;
83         DLIST_ADD_END(ldap_service->partitions, part, struct ldapsrv_partition *);
84
85         part = talloc_p(ldap_service, struct ldapsrv_partition);
86         if (!ldap_service) {
87                 DEBUG(0,("talloc_p(ldap_service, struct ldapsrv_partition) failed\n"));
88                 return;
89         }
90         part->base_dn = "*"; /* default partition */
91         part->ops = ldapsrv_get_sldb_partition_ops();
92
93         ldap_service->default_partition = part;
94         DLIST_ADD_END(ldap_service->partitions, part, struct ldapsrv_partition *);
95
96         service->private_data = ldap_service;
97
98         if (lp_interfaces() && lp_bind_interfaces_only()) {
99                 int num_interfaces = iface_count();
100                 int i;
101
102                 /* We have been given an interfaces line, and been 
103                    told to only bind to those interfaces. Create a
104                    socket per interface and bind to only these.
105                 */
106                 for(i = 0; i < num_interfaces; i++) {
107                         struct ipv4_addr *ifip = iface_n_ip(i);
108
109                         if (ifip == NULL) {
110                                 DEBUG(0,("ldapsrv_init: interface %d has NULL "
111                                          "IP address !\n", i));
112                                 continue;
113                         }
114
115                         add_socket(service, model_ops, ifip);
116                 }
117         } else {
118                 struct ipv4_addr ifip;
119
120                 /* Just bind to lp_socket_address() (usually 0.0.0.0) */
121                 ifip = interpret_addr2(lp_socket_address());
122                 add_socket(service, model_ops, &ifip);
123         }
124 }
125
126 /* This rw-buf api is made to avoid memcpy. For now do that like mad...  The
127    idea is to write into a circular list of buffers where the ideal case is
128    that a read(2) holds a complete request that is then thrown away
129    completely. */
130
131 void ldapsrv_consumed_from_buf(struct rw_buffer *buf,
132                                    size_t length)
133 {
134         memmove(buf->data, buf->data+length, buf->length-length);
135         buf->length -= length;
136 }
137
138 static void peek_into_read_buf(struct rw_buffer *buf, uint8_t **out,
139                                size_t *out_length)
140 {
141         *out = buf->data;
142         *out_length = buf->length;
143 }
144
145 BOOL ldapsrv_append_to_buf(struct rw_buffer *buf, uint8_t *data, size_t length)
146 {
147         buf->data = realloc(buf->data, buf->length+length);
148
149         if (buf->data == NULL)
150                 return False;
151
152         memcpy(buf->data+buf->length, data, length);
153
154         buf->length += length;
155         return True;
156 }
157
158 static BOOL read_into_buf(struct socket_context *sock, struct rw_buffer *buf)
159 {
160         NTSTATUS status;
161         DATA_BLOB tmp_blob;
162         BOOL ret;
163         size_t nread;
164
165         tmp_blob = data_blob_talloc(sock, NULL, 1024);
166         if (tmp_blob.data == NULL) {
167                 return False;
168         }
169
170         status = socket_recv(sock, tmp_blob.data, tmp_blob.length, &nread, 0);
171         if (NT_STATUS_IS_ERR(status)) {
172                 DEBUG(10,("socket_recv: %s\n",nt_errstr(status)));
173                 talloc_free(tmp_blob.data);
174                 return False;
175         }
176         tmp_blob.length = nread;
177
178         ret = ldapsrv_append_to_buf(buf, tmp_blob.data, tmp_blob.length);
179
180         talloc_free(tmp_blob.data);
181
182         return ret;
183 }
184
185 static BOOL ldapsrv_read_buf(struct ldapsrv_connection *conn)
186 {
187         NTSTATUS status;
188         DATA_BLOB tmp_blob;
189         DATA_BLOB wrapped;
190         DATA_BLOB unwrapped;
191         BOOL ret;
192         uint8_t *buf;
193         size_t buf_length, sasl_length;
194         struct socket_context *sock = conn->connection->socket;
195         TALLOC_CTX *mem_ctx;
196         size_t nread;
197
198         if (!conn->gensec) {
199                 return read_into_buf(sock, &conn->in_buffer);
200         }
201         if (!conn->session_info) {
202                 return read_into_buf(sock, &conn->in_buffer);
203         }
204         if (!(gensec_have_feature(conn->gensec, GENSEC_FEATURE_SIGN) ||
205               gensec_have_feature(conn->gensec, GENSEC_FEATURE_SEAL))) {
206                 return read_into_buf(sock, &conn->in_buffer);
207         }
208
209         mem_ctx = talloc(conn, 0);
210         if (!mem_ctx) {
211                 DEBUG(0,("no memory\n"));
212                 return False;
213         }
214
215         tmp_blob = data_blob_talloc(mem_ctx, NULL, 1024);
216         if (tmp_blob.data == NULL) {
217                 talloc_free(mem_ctx);
218                 return False;
219         }
220
221         status = socket_recv(sock, tmp_blob.data, tmp_blob.length, &nread, 0);
222         if (NT_STATUS_IS_ERR(status)) {
223                 DEBUG(10,("socket_recv: %s\n",nt_errstr(status)));
224                 talloc_free(mem_ctx);
225                 return False;
226         }
227         tmp_blob.length = nread;
228
229         ret = ldapsrv_append_to_buf(&conn->sasl_in_buffer, tmp_blob.data, tmp_blob.length);
230         if (!ret) {
231                 talloc_free(mem_ctx);
232                 return False;
233         }
234
235         peek_into_read_buf(&conn->sasl_in_buffer, &buf, &buf_length);
236
237         if (buf_length < 4) {
238                 /* not enough yet */
239                 talloc_free(mem_ctx);
240                 return True;
241         }
242
243         sasl_length = RIVAL(buf, 0);
244
245         if ((buf_length - 4) < sasl_length) {
246                 /* not enough yet */
247                 talloc_free(mem_ctx);
248                 return True;
249         }
250
251         wrapped.data = buf + 4;
252         wrapped.length = sasl_length;
253
254         status = gensec_unwrap(conn->gensec, mem_ctx,
255                                &wrapped, 
256                                &unwrapped);
257         if (!NT_STATUS_IS_OK(status)) {
258                 DEBUG(0,("gensec_unwrap: %s\n",nt_errstr(status)));
259                 talloc_free(mem_ctx);
260                 return False;
261         }
262
263         ret = ldapsrv_append_to_buf(&conn->in_buffer, unwrapped.data, unwrapped.length);
264         if (!ret) {
265                 talloc_free(mem_ctx);
266                 return False;
267         }
268
269         ldapsrv_consumed_from_buf(&conn->sasl_in_buffer, 4 + sasl_length);
270
271         talloc_free(mem_ctx);
272         return ret;
273 }
274
275 static BOOL write_from_buf(struct socket_context *sock, struct rw_buffer *buf)
276 {
277         NTSTATUS status;
278         DATA_BLOB tmp_blob;
279         size_t sendlen;
280
281         tmp_blob.data = buf->data;
282         tmp_blob.length = buf->length;
283
284         status = socket_send(sock, &tmp_blob, &sendlen, 0);
285         if (!NT_STATUS_IS_OK(status)) {
286                 DEBUG(10,("socket_send() %s\n",nt_errstr(status)));
287                 return False;
288         }
289
290         ldapsrv_consumed_from_buf(buf, sendlen);
291
292         return True;
293 }
294
295 static BOOL ldapsrv_write_buf(struct ldapsrv_connection *conn)
296 {
297         NTSTATUS status;
298         DATA_BLOB wrapped;
299         DATA_BLOB tmp_blob;
300         DATA_BLOB sasl;
301         size_t sendlen;
302         BOOL ret;
303         struct socket_context *sock = conn->connection->socket;
304         TALLOC_CTX *mem_ctx;
305
306
307         if (!conn->gensec) {
308                 return write_from_buf(sock, &conn->out_buffer);
309         }
310         if (!conn->session_info) {
311                 return write_from_buf(sock, &conn->out_buffer);
312         }
313         if (!(gensec_have_feature(conn->gensec, GENSEC_FEATURE_SIGN) ||
314               gensec_have_feature(conn->gensec, GENSEC_FEATURE_SEAL))) {
315                 return write_from_buf(sock, &conn->out_buffer);
316         }
317
318         mem_ctx = talloc(conn, 0);
319         if (!mem_ctx) {
320                 DEBUG(0,("no memory\n"));
321                 return False;
322         }
323
324         if (conn->out_buffer.length == 0) {
325                 goto nodata;
326         }
327
328         tmp_blob.data = conn->out_buffer.data;
329         tmp_blob.length = conn->out_buffer.length;
330         status = gensec_wrap(conn->gensec, mem_ctx,
331                              &tmp_blob,
332                              &wrapped);
333         if (!NT_STATUS_IS_OK(status)) {
334                 DEBUG(0,("gensec_wrap: %s\n",nt_errstr(status)));
335                 talloc_free(mem_ctx);
336                 return False;
337         }
338
339         sasl = data_blob_talloc(mem_ctx, NULL, 4 + wrapped.length);
340         if (!sasl.data) {
341                 DEBUG(0,("no memory\n"));
342                 talloc_free(mem_ctx);
343                 return False;
344         }
345
346         RSIVAL(sasl.data, 0, wrapped.length);
347         memcpy(sasl.data + 4, wrapped.data, wrapped.length);
348
349         ret = ldapsrv_append_to_buf(&conn->sasl_out_buffer, sasl.data, sasl.length);
350         if (!ret) {
351                 talloc_free(mem_ctx);
352                 return False;
353         }
354         ldapsrv_consumed_from_buf(&conn->out_buffer, conn->out_buffer.length);
355 nodata:
356         tmp_blob.data = conn->sasl_out_buffer.data;
357         tmp_blob.length = conn->sasl_out_buffer.length;
358
359         status = socket_send(sock, &tmp_blob, &sendlen, 0);
360         if (!NT_STATUS_IS_OK(status)) {
361                 DEBUG(10,("socket_send() %s\n",nt_errstr(status)));
362                 talloc_free(mem_ctx);
363                 return False;
364         }
365
366         ldapsrv_consumed_from_buf(&conn->sasl_out_buffer, sendlen);
367
368         talloc_free(mem_ctx);
369
370         return True;
371 }
372
373 static BOOL ldap_encode_to_buf(struct ldap_message *msg, struct rw_buffer *buf)
374 {
375         DATA_BLOB blob;
376         BOOL res;
377
378         if (!ldap_encode(msg, &blob))
379                 return False;
380
381         res = ldapsrv_append_to_buf(buf, blob.data, blob.length);
382
383         data_blob_free(&blob);
384         return res;
385 }
386
387 NTSTATUS ldapsrv_do_responses(struct ldapsrv_connection *conn)
388 {
389         struct ldapsrv_call *call, *next_call = NULL;
390         struct ldapsrv_reply *reply, *next_reply = NULL;
391
392         for (call=conn->calls; call; call=next_call) {
393                 for (reply=call->replies; reply; reply=next_reply) {
394                         if (!ldap_encode_to_buf(&reply->msg, &conn->out_buffer)) {
395                                 return NT_STATUS_FOOBAR;
396                         }
397                         next_reply = reply->next;
398                         DLIST_REMOVE(call->replies, reply);
399                         reply->state = LDAPSRV_REPLY_STATE_SEND;
400                         talloc_free(reply);
401                 }
402                 next_call = call->next;
403                 DLIST_REMOVE(conn->calls, call);
404                 call->state = LDAPSRV_CALL_STATE_COMPLETE;
405                 talloc_free(call);
406         }
407
408         return NT_STATUS_OK;
409 }
410
411 NTSTATUS ldapsrv_flush_responses(struct ldapsrv_connection *conn)
412 {
413         return NT_STATUS_OK;
414 }
415
416 /*
417   called when a LDAP socket becomes readable
418 */
419 static void ldapsrv_recv(struct server_connection *conn, struct timeval t,
420                          uint16_t flags)
421 {
422         struct ldapsrv_connection *ldap_conn = conn->private_data;
423         uint8_t *buf;
424         size_t buf_length, msg_length;
425         DATA_BLOB blob;
426         struct asn1_data data;
427         struct ldapsrv_call *call;
428         NTSTATUS status;
429
430         DEBUG(10,("ldapsrv_recv\n"));
431
432         if (!ldapsrv_read_buf(ldap_conn)) {
433                 ldapsrv_terminate_connection(ldap_conn, "ldapsrv_read_buf() failed");
434                 return;
435         }
436
437         peek_into_read_buf(&ldap_conn->in_buffer, &buf, &buf_length);
438
439         while (buf_length > 0) {
440                 /* LDAP Messages are always SEQUENCES */
441
442                 if (!asn1_object_length(buf, buf_length, ASN1_SEQUENCE(0),
443                                         &msg_length)) {
444                         ldapsrv_terminate_connection(ldap_conn, "asn1_object_length() failed");
445                         return;
446                 }
447
448                 if (buf_length < msg_length) {
449                         /* Not enough yet */
450                         break;
451                 }
452
453                 /* We've got a complete LDAP request in the in-buffer, convert
454                  * that to a ldap_message and put it into the incoming
455                  * queue. */
456
457                 blob.data = buf;
458                 blob.length = msg_length;
459
460                 if (!asn1_load(&data, blob)) {
461                         ldapsrv_terminate_connection(ldap_conn, "asn1_load() failed");
462                         return;
463                 }
464
465                 call = talloc_p(ldap_conn, struct ldapsrv_call);
466                 if (!call) {
467                         ldapsrv_terminate_connection(ldap_conn, "no memory");
468                         return;         
469                 }
470
471                 ZERO_STRUCTP(call);
472                 call->state = LDAPSRV_CALL_STATE_NEW;
473                 call->conn = ldap_conn;
474                 call->request.mem_ctx = call;
475
476                 if (!ldap_decode(&data, &call->request)) {
477                         dump_data(0,buf, msg_length);
478                         asn1_free(&data);
479                         ldapsrv_terminate_connection(ldap_conn, "ldap_decode() failed");
480                         return;
481                 }
482
483                 asn1_free(&data);
484
485                 DLIST_ADD_END(ldap_conn->calls, call,
486                               struct ldapsrv_call *);
487
488                 ldapsrv_consumed_from_buf(&ldap_conn->in_buffer, msg_length);
489
490                 status = ldapsrv_do_call(call);
491                 if (!NT_STATUS_IS_OK(status)) {
492                         ldapsrv_terminate_connection(ldap_conn, "ldapsrv_do_call() failed");
493                         return;
494                 }
495
496                 peek_into_read_buf(&ldap_conn->in_buffer, &buf, &buf_length);
497         }
498
499         status = ldapsrv_do_responses(ldap_conn);
500         if (!NT_STATUS_IS_OK(status)) {
501                 ldapsrv_terminate_connection(ldap_conn, "ldapsrv_do_responses() failed");
502                 return;
503         }
504
505         if ((ldap_conn->out_buffer.length > 0)||(ldap_conn->sasl_out_buffer.length > 0)) {
506                 conn->event.fde->flags |= EVENT_FD_WRITE;
507         }
508
509         return;
510 }
511         
512 /*
513   called when a LDAP socket becomes writable
514 */
515 static void ldapsrv_send(struct server_connection *conn, struct timeval t,
516                          uint16_t flags)
517 {
518         struct ldapsrv_connection *ldap_conn = conn->private_data;
519
520         DEBUG(10,("ldapsrv_send\n"));
521
522         if (!ldapsrv_write_buf(ldap_conn)) {
523                 ldapsrv_terminate_connection(ldap_conn, "ldapsrv_write_buf() failed");
524                 return;
525         }
526
527         if (ldap_conn->out_buffer.length == 0 && ldap_conn->sasl_out_buffer.length == 0) {
528                 conn->event.fde->flags &= ~EVENT_FD_WRITE;
529         }
530
531         return;
532 }
533
534 /*
535   called when connection is idle
536 */
537 static void ldapsrv_idle(struct server_connection *conn, struct timeval t)
538 {
539         DEBUG(10,("ldapsrv_idle: not implemented!\n"));
540         return;
541 }
542
543 static void ldapsrv_close(struct server_connection *conn, const char *reason)
544 {
545         return;
546 }
547
548 /*
549   initialise a server_context from a open socket and register a event handler
550   for reading from that socket
551 */
552 static void ldapsrv_accept(struct server_connection *conn)
553 {
554         struct ldapsrv_connection *ldap_conn;
555
556         DEBUG(10, ("ldapsrv_accept\n"));
557
558         ldap_conn = talloc_p(conn, struct ldapsrv_connection);
559
560         if (ldap_conn == NULL)
561                 return;
562
563         ZERO_STRUCTP(ldap_conn);
564         ldap_conn->connection = conn;
565         ldap_conn->service = talloc_reference(ldap_conn, conn->service->private_data);
566
567         conn->private_data = ldap_conn;
568
569         return;
570 }
571
572 /*
573   called on a fatal error that should cause this server to terminate
574 */
575 static void ldapsrv_exit(struct server_service *service, const char *reason)
576 {
577         DEBUG(10,("ldapsrv_exit\n"));
578         return;
579 }
580
581 static const struct server_service_ops ldap_server_ops = {
582         .name                   = "ldap",
583         .service_init           = ldapsrv_init,
584         .accept_connection      = ldapsrv_accept,
585         .recv_handler           = ldapsrv_recv,
586         .send_handler           = ldapsrv_send,
587         .idle_handler           = ldapsrv_idle,
588         .close_connection       = ldapsrv_close,
589         .service_exit           = ldapsrv_exit, 
590 };
591
592 const struct server_service_ops *ldapsrv_get_ops(void)
593 {
594         return &ldap_server_ops;
595 }
596
597 NTSTATUS server_service_ldap_init(void)
598 {
599         return NT_STATUS_OK;    
600 }