r3481: split out client.h and events.h
[jelmer/samba4-debian.git] / source / 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         memcpy(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
177         ret = ldapsrv_append_to_buf(buf, tmp_blob.data, tmp_blob.length);
178
179         talloc_free(tmp_blob.data);
180
181         return ret;
182 }
183
184 static BOOL ldapsrv_read_buf(struct ldapsrv_connection *conn)
185 {
186         NTSTATUS status;
187         DATA_BLOB tmp_blob;
188         DATA_BLOB creds;
189         BOOL ret;
190         uint8_t *buf;
191         int buf_length, sasl_length;
192         struct socket_context *sock = conn->connection->socket;
193         TALLOC_CTX *mem_ctx;
194         size_t nread;
195
196         if (!conn->gensec || !conn->session_info ||
197            !(gensec_have_feature(conn->gensec, GENSEC_WANT_SIGN) &&
198              gensec_have_feature(conn->gensec, GENSEC_WANT_SEAL))) {
199                 return read_into_buf(sock, &conn->in_buffer);
200         }
201
202         mem_ctx = talloc(conn, 0);
203         if (!mem_ctx) {
204                 DEBUG(0,("no memory\n"));
205                 return False;
206         }
207
208         tmp_blob = data_blob_talloc(mem_ctx, NULL, 1024);
209         if (tmp_blob.data == NULL) {
210                 talloc_free(mem_ctx);
211                 return False;
212         }
213
214         status = socket_recv(sock, tmp_blob.data, tmp_blob.length, &nread, 0);
215         if (NT_STATUS_IS_ERR(status)) {
216                 DEBUG(10,("socket_recv: %s\n",nt_errstr(status)));
217                 talloc_free(mem_ctx);
218                 return False;
219         }
220         tmp_blob.length = nread;
221
222         ret = ldapsrv_append_to_buf(&conn->sasl_in_buffer, tmp_blob.data, tmp_blob.length);
223         if (!ret) {
224                 talloc_free(mem_ctx);
225                 return False;
226         }
227
228         peek_into_read_buf(&conn->sasl_in_buffer, &buf, &buf_length);
229
230         if (buf_length < 4) {
231                 /* not enough yet */
232                 talloc_free(mem_ctx);
233                 return True;
234         }
235
236         sasl_length = RIVAL(buf, 0);
237
238         if (buf_length < (4 + sasl_length)) {
239                 /* not enough yet */
240                 talloc_free(mem_ctx);
241                 return True;
242         }
243
244         creds.data = buf + 4;
245         creds.length = gensec_sig_size(conn->gensec);
246
247         if (creds.length > sasl_length) {
248                 /* invalid packet? */
249                 talloc_free(mem_ctx);
250                 return False;
251         }
252
253         tmp_blob.data = buf + (4 + creds.length);
254         tmp_blob.length = (4 + sasl_length) - (4 + creds.length);
255
256         if (gensec_have_feature(conn->gensec, GENSEC_WANT_SEAL)) {
257                 status = gensec_unseal_packet(conn->gensec, mem_ctx,
258                                               tmp_blob.data, tmp_blob.length,
259                                               tmp_blob.data, tmp_blob.length,
260                                               &creds);
261                 if (!NT_STATUS_IS_OK(status)) {
262                         DEBUG(0,("gensec_unseal_packet: %s\n",nt_errstr(status)));
263                         talloc_free(mem_ctx);
264                         return False;
265                 }
266         } else {
267                 status = gensec_check_packet(conn->gensec, mem_ctx,
268                                               tmp_blob.data, tmp_blob.length,
269                                               tmp_blob.data, tmp_blob.length,
270                                               &creds);
271                 if (!NT_STATUS_IS_OK(status)) {
272                         DEBUG(0,("gensec_check_packet: %s\n",nt_errstr(status)));
273                         talloc_free(mem_ctx);
274                         return False;
275                 }
276         }
277
278         ret = ldapsrv_append_to_buf(&conn->in_buffer, tmp_blob.data, tmp_blob.length);
279         if (!ret) {
280                 talloc_free(mem_ctx);
281                 return False;
282         }
283
284         ldapsrv_consumed_from_buf(&conn->sasl_in_buffer, 4 + sasl_length);
285
286         talloc_free(mem_ctx);
287         return ret;
288 }
289
290 static BOOL write_from_buf(struct socket_context *sock, struct rw_buffer *buf)
291 {
292         NTSTATUS status;
293         DATA_BLOB tmp_blob;
294         size_t sendlen;
295
296         tmp_blob.data = buf->data;
297         tmp_blob.length = buf->length;
298
299         status = socket_send(sock, &tmp_blob, &sendlen, 0);
300         if (!NT_STATUS_IS_OK(status)) {
301                 DEBUG(10,("socket_send() %s\n",nt_errstr(status)));
302                 return False;
303         }
304
305         ldapsrv_consumed_from_buf(buf, sendlen);
306
307         return True;
308 }
309
310 static BOOL ldapsrv_write_buf(struct ldapsrv_connection *conn)
311 {
312         NTSTATUS status;
313         DATA_BLOB tmp_blob;
314         DATA_BLOB creds;
315         DATA_BLOB sasl;
316         size_t sendlen;
317         BOOL ret;
318         struct socket_context *sock = conn->connection->socket;
319         TALLOC_CTX *mem_ctx;
320
321         if (!conn->gensec || !conn->session_info ||
322            !(gensec_have_feature(conn->gensec, GENSEC_WANT_SIGN) &&
323              gensec_have_feature(conn->gensec, GENSEC_WANT_SEAL))) {
324                 return write_from_buf(sock, &conn->out_buffer);
325         }
326
327         mem_ctx = talloc(conn, 0);
328         if (!mem_ctx) {
329                 DEBUG(0,("no memory\n"));
330                 return False;
331         }
332
333         tmp_blob.data = conn->out_buffer.data;
334         tmp_blob.length = conn->out_buffer.length;
335
336         if (tmp_blob.length == 0) {
337                 goto nodata;
338         }
339
340         if (gensec_have_feature(conn->gensec, GENSEC_WANT_SEAL)) {
341                 status = gensec_seal_packet(conn->gensec, mem_ctx,
342                                             tmp_blob.data, tmp_blob.length,
343                                             tmp_blob.data, tmp_blob.length,
344                                             &creds);
345                 if (!NT_STATUS_IS_OK(status)) {
346                         DEBUG(0,("gensec_seal_packet: %s\n",nt_errstr(status)));
347                         talloc_free(mem_ctx);
348                         return False;
349                 }
350         } else {
351                 status = gensec_sign_packet(conn->gensec, mem_ctx,
352                                             tmp_blob.data, tmp_blob.length,
353                                             tmp_blob.data, tmp_blob.length,
354                                             &creds);
355                 if (!NT_STATUS_IS_OK(status)) {
356                         DEBUG(0,("gensec_sign_packet: %s\n",nt_errstr(status)));
357                         talloc_free(mem_ctx);
358                         return False;
359                 }               
360         }
361
362         sasl = data_blob_talloc(mem_ctx, NULL, 4 + creds.length + tmp_blob.length);
363         if (!sasl.data) {
364                 DEBUG(0,("no memory\n"));
365                 talloc_free(mem_ctx);
366                 return False;
367         }
368
369         RSIVAL(sasl.data, 0, creds.length + tmp_blob.length);
370         memcpy(sasl.data + 4, creds.data, creds.length);
371         memcpy(sasl.data + 4 + creds.length, tmp_blob.data, tmp_blob.length);
372
373         ret = ldapsrv_append_to_buf(&conn->sasl_out_buffer, sasl.data, sasl.length);
374         if (!ret) {
375                 talloc_free(mem_ctx);
376                 return False;
377         }
378         ldapsrv_consumed_from_buf(&conn->out_buffer, tmp_blob.length);
379 nodata:
380         tmp_blob.data = conn->sasl_out_buffer.data;
381         tmp_blob.length = conn->sasl_out_buffer.length;
382
383         status = socket_send(sock, &tmp_blob, &sendlen, 0);
384         if (!NT_STATUS_IS_OK(status)) {
385                 DEBUG(10,("socket_send() %s\n",nt_errstr(status)));
386                 talloc_free(mem_ctx);
387                 return False;
388         }
389
390         ldapsrv_consumed_from_buf(&conn->sasl_out_buffer, sendlen);
391
392         talloc_free(mem_ctx);
393
394         return True;
395 }
396
397 static BOOL ldap_encode_to_buf(struct ldap_message *msg, struct rw_buffer *buf)
398 {
399         DATA_BLOB blob;
400         BOOL res;
401
402         if (!ldap_encode(msg, &blob))
403                 return False;
404
405         res = ldapsrv_append_to_buf(buf, blob.data, blob.length);
406
407         data_blob_free(&blob);
408         return res;
409 }
410
411 NTSTATUS ldapsrv_do_responses(struct ldapsrv_connection *conn)
412 {
413         struct ldapsrv_call *call, *next_call = NULL;
414         struct ldapsrv_reply *reply, *next_reply = NULL;
415
416         for (call=conn->calls; call; call=next_call) {
417                 for (reply=call->replies; reply; reply=next_reply) {
418                         if (!ldap_encode_to_buf(&reply->msg, &conn->out_buffer)) {
419                                 return NT_STATUS_FOOBAR;
420                         }
421                         next_reply = reply->next;
422                         DLIST_REMOVE(call->replies, reply);
423                         reply->state = LDAPSRV_REPLY_STATE_SEND;
424                         talloc_free(reply);
425                 }
426                 next_call = call->next;
427                 DLIST_REMOVE(conn->calls, call);
428                 call->state = LDAPSRV_CALL_STATE_COMPLETE;
429                 talloc_free(call);
430         }
431
432         return NT_STATUS_OK;
433 }
434
435 NTSTATUS ldapsrv_flush_responses(struct ldapsrv_connection *conn)
436 {
437         return NT_STATUS_OK;
438 }
439
440 /*
441   called when a LDAP socket becomes readable
442 */
443 static void ldapsrv_recv(struct server_connection *conn, time_t t,
444                          uint16_t flags)
445 {
446         struct ldapsrv_connection *ldap_conn = conn->private_data;
447         uint8_t *buf;
448         int buf_length, msg_length;
449         DATA_BLOB blob;
450         struct asn1_data data;
451         struct ldapsrv_call *call;
452         NTSTATUS status;
453
454         DEBUG(10,("ldapsrv_recv\n"));
455
456         if (!ldapsrv_read_buf(ldap_conn)) {
457                 ldapsrv_terminate_connection(ldap_conn, "ldapsrv_read_buf() failed");
458                 return;
459         }
460
461         peek_into_read_buf(&ldap_conn->in_buffer, &buf, &buf_length);
462
463         while (buf_length > 0) {
464                 /* LDAP Messages are always SEQUENCES */
465
466                 if (!asn1_object_length(buf, buf_length, ASN1_SEQUENCE(0),
467                                         &msg_length)) {
468                         ldapsrv_terminate_connection(ldap_conn, "asn1_object_length() failed");
469                         return;
470                 }
471
472                 if (buf_length < msg_length) {
473                         /* Not enough yet */
474                         break;
475                 }
476
477                 /* We've got a complete LDAP request in the in-buffer, convert
478                  * that to a ldap_message and put it into the incoming
479                  * queue. */
480
481                 blob.data = buf;
482                 blob.length = msg_length;
483
484                 if (!asn1_load(&data, blob)) {
485                         ldapsrv_terminate_connection(ldap_conn, "asn1_load() failed");
486                         return;
487                 }
488
489                 call = talloc_p(ldap_conn, struct ldapsrv_call);
490                 if (!call) {
491                         ldapsrv_terminate_connection(ldap_conn, "no memory");
492                         return;         
493                 }
494
495                 ZERO_STRUCTP(call);
496                 call->state = LDAPSRV_CALL_STATE_NEW;
497                 call->conn = ldap_conn;
498                 call->request.mem_ctx = call;
499
500                 if (!ldap_decode(&data, &call->request)) {
501                         dump_data(0,buf, msg_length);
502                         asn1_free(&data);
503                         ldapsrv_terminate_connection(ldap_conn, "ldap_decode() failed");
504                         return;
505                 }
506
507                 asn1_free(&data);
508
509                 DLIST_ADD_END(ldap_conn->calls, call,
510                               struct ldapsrv_call *);
511
512                 ldapsrv_consumed_from_buf(&ldap_conn->in_buffer, msg_length);
513
514                 status = ldapsrv_do_call(call);
515                 if (!NT_STATUS_IS_OK(status)) {
516                         ldapsrv_terminate_connection(ldap_conn, "ldapsrv_do_call() failed");
517                         return;
518                 }
519
520                 peek_into_read_buf(&ldap_conn->in_buffer, &buf, &buf_length);
521         }
522
523         status = ldapsrv_do_responses(ldap_conn);
524         if (!NT_STATUS_IS_OK(status)) {
525                 ldapsrv_terminate_connection(ldap_conn, "ldapsrv_do_responses() failed");
526                 return;
527         }
528
529         if ((ldap_conn->out_buffer.length > 0)||(ldap_conn->sasl_out_buffer.length > 0)) {
530                 conn->event.fde->flags |= EVENT_FD_WRITE;
531         }
532
533         return;
534 }
535         
536 /*
537   called when a LDAP socket becomes writable
538 */
539 static void ldapsrv_send(struct server_connection *conn, time_t t,
540                          uint16_t flags)
541 {
542         struct ldapsrv_connection *ldap_conn = conn->private_data;
543
544         DEBUG(10,("ldapsrv_send\n"));
545
546         if (!ldapsrv_write_buf(ldap_conn)) {
547                 ldapsrv_terminate_connection(ldap_conn, "ldapsrv_write_buf() failed");
548                 return;
549         }
550
551         if (ldap_conn->out_buffer.length == 0 && ldap_conn->sasl_out_buffer.length == 0) {
552                 conn->event.fde->flags &= ~EVENT_FD_WRITE;
553         }
554
555         return;
556 }
557
558 /*
559   called when connection is idle
560 */
561 static void ldapsrv_idle(struct server_connection *conn, time_t t)
562 {
563         DEBUG(10,("ldapsrv_idle: not implemented!\n"));
564         return;
565 }
566
567 static void ldapsrv_close(struct server_connection *conn, const char *reason)
568 {
569         return;
570 }
571
572 /*
573   initialise a server_context from a open socket and register a event handler
574   for reading from that socket
575 */
576 static void ldapsrv_accept(struct server_connection *conn)
577 {
578         struct ldapsrv_connection *ldap_conn;
579
580         DEBUG(10, ("ldapsrv_accept\n"));
581
582         ldap_conn = talloc_p(conn, struct ldapsrv_connection);
583
584         if (ldap_conn == NULL)
585                 return;
586
587         ZERO_STRUCTP(ldap_conn);
588         ldap_conn->connection = conn;
589         ldap_conn->service = talloc_reference(ldap_conn, conn->service->private_data);
590
591         conn->private_data = ldap_conn;
592
593         return;
594 }
595
596 /*
597   called on a fatal error that should cause this server to terminate
598 */
599 static void ldapsrv_exit(struct server_service *service, const char *reason)
600 {
601         DEBUG(10,("ldapsrv_exit\n"));
602         return;
603 }
604
605 static const struct server_service_ops ldap_server_ops = {
606         .name                   = "ldap",
607         .service_init           = ldapsrv_init,
608         .accept_connection      = ldapsrv_accept,
609         .recv_handler           = ldapsrv_recv,
610         .send_handler           = ldapsrv_send,
611         .idle_handler           = ldapsrv_idle,
612         .close_connection       = ldapsrv_close,
613         .service_exit           = ldapsrv_exit, 
614 };
615
616 const struct server_service_ops *ldapsrv_get_ops(void)
617 {
618         return &ldap_server_ops;
619 }
620
621 NTSTATUS server_service_ldap_init(void)
622 {
623         return NT_STATUS_OK;    
624 }