2 Unix SMB/CIFS implementation.
4 Copyright (C) Volker Lendecke 2004
5 Copyright (C) Stefan Metzmacher 2004
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.
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.
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.
25 close the socket and shutdown a server_context
27 void ldapsrv_terminate_connection(struct ldapsrv_connection *ldap_conn, const char *reason)
29 server_terminate_connection(ldap_conn->connection, reason);
33 add a socket address to the list of events, one event per port
35 static void add_socket(struct server_service *service,
36 const struct model_ops *model_ops,
39 struct server_socket *srv_sock;
41 char *ip_str = talloc_strdup(service->mem_ctx, inet_ntoa(*ifip));
43 srv_sock = service_setup_socket(service, model_ops, ip_str, &port);
48 /****************************************************************************
49 Open the socket communication.
50 ****************************************************************************/
51 static void ldapsrv_init(struct server_service *service,
52 const struct model_ops *model_ops)
54 DEBUG(1,("ldapsrv_init\n"));
56 if (lp_interfaces() && lp_bind_interfaces_only()) {
57 int num_interfaces = iface_count();
60 /* We have been given an interfaces line, and been
61 told to only bind to those interfaces. Create a
62 socket per interface and bind to only these.
64 for(i = 0; i < num_interfaces; i++) {
65 struct in_addr *ifip = iface_n_ip(i);
68 DEBUG(0,("ldapsrv_init: interface %d has NULL "
69 "IP address !\n", i));
73 add_socket(service, model_ops, ifip);
77 TALLOC_CTX *mem_ctx = talloc_init("ldapsrv_init");
80 smb_panic("No memory");
83 /* Just bind to lp_socket_address() (usually 0.0.0.0) */
84 ifip = interpret_addr2(mem_ctx, lp_socket_address());
85 add_socket(service, model_ops, ifip);
87 talloc_destroy(mem_ctx);
91 /* This rw-buf api is made to avoid memcpy. For now do that like mad... The
92 idea is to write into a circular list of buffers where the ideal case is
93 that a read(2) holds a complete request that is then thrown away
96 static void consumed_from_buf(struct rw_buffer *buf,
99 memcpy(buf->data, buf->data+length, buf->length-length);
100 buf->length -= length;
103 static BOOL append_to_buf(struct rw_buffer *buf, uint8_t *data, size_t length)
105 buf->data = realloc(buf->data, buf->length+length);
107 if (buf->data == NULL)
110 memcpy(buf->data+buf->length, data, length);
112 buf->length += length;
116 static BOOL read_into_buf(struct socket_context *sock, struct rw_buffer *buf)
122 status = socket_recv(sock, sock, &tmp_blob, 1024, 0);
123 if (!NT_STATUS_IS_OK(status)) {
124 DEBUG(0,("socket_recv: %s\n",nt_errstr(status)));
128 ret = append_to_buf(buf, tmp_blob.data, tmp_blob.length);
130 talloc_free(tmp_blob.data);
135 static BOOL write_from_buf(struct socket_context *sock, struct rw_buffer *buf)
141 tmp_blob.data = buf->data;
142 tmp_blob.length = buf->length;
144 status = socket_send(sock, sock, &tmp_blob, &sendlen, 0);
145 if (!NT_STATUS_IS_OK(status)) {
146 DEBUG(0,("socket_send() %s\n",nt_errstr(status)));
150 consumed_from_buf(buf, sendlen);
155 static void peek_into_read_buf(struct rw_buffer *buf, uint8_t **out,
159 *out_length = buf->length;
162 static BOOL ldap_append_to_buf(struct ldap_message *msg, struct rw_buffer *buf)
167 if (!ldap_encode(msg, &blob))
170 res = append_to_buf(buf, blob.data, blob.length);
172 data_blob_free(&blob);
176 struct ldapsrv_reply *ldapsrv_init_reply(struct ldapsrv_call *call, enum ldap_request_tag type)
178 struct ldapsrv_reply *reply;
180 reply = talloc_p(call, struct ldapsrv_reply);
185 reply->prev = reply->next = NULL;
186 reply->state = LDAPSRV_REPLY_STATE_NEW;
187 reply->msg.messageid = call->request.messageid;
188 reply->msg.type = type;
189 reply->msg.mem_ctx = reply;
194 void ldapsrv_queue_reply(struct ldapsrv_call *call, struct ldapsrv_reply *reply)
196 DLIST_ADD_END(call->replies, reply, struct ldapsrv_reply *);
199 struct ldapsrv_partition *ldapsrv_get_partition(struct ldapsrv_connection *conn, const char *dn)
201 static struct ldapsrv_partition null_part;
203 null_part.ops = ldapsrv_get_sldb_partition_ops();
208 void ldapsrv_unwilling(struct ldapsrv_call *call, int error)
210 struct ldapsrv_reply *reply;
211 struct ldap_ExtendedResponse *r;
213 DEBUG(0,("Unwilling type[%d] id[%d]\n", call->request.type, call->request.messageid));
215 reply = ldapsrv_init_reply(call, LDAP_TAG_ExtendedResponse);
217 ldapsrv_terminate_connection(call->conn, "ldapsrv_init_reply() failed");
221 r = &reply->msg.r.ExtendedResponse;
222 r->response.resultcode = error;
223 r->response.dn = NULL;
224 r->response.errormessage = NULL;
225 r->response.referral = NULL;
227 r->value.data = NULL;
230 ldapsrv_queue_reply(call, reply);
233 static void ldapsrv_BindRequest(struct ldapsrv_call *call)
235 struct ldap_BindRequest *req = &call->request.r.BindRequest;
236 struct ldapsrv_reply *reply;
237 struct ldap_BindResponse *resp;
239 DEBUG(5, ("BindRequest dn: %s\n",req->dn));
241 reply = ldapsrv_init_reply(call, LDAP_TAG_BindResponse);
243 ldapsrv_terminate_connection(call->conn, "ldapsrv_init_reply() failed");
247 resp = &reply->msg.r.BindResponse;
248 resp->response.resultcode = 0;
249 resp->response.dn = NULL;
250 resp->response.errormessage = NULL;
251 resp->response.referral = NULL;
252 resp->SASL.secblob = data_blob(NULL, 0);
254 ldapsrv_queue_reply(call, reply);
257 static void ldapsrv_UnbindRequest(struct ldapsrv_call *call)
259 /* struct ldap_UnbindRequest *req = &call->request->r.UnbindRequest;*/
260 DEBUG(10, ("UnbindRequest\n"));
263 static void ldapsrv_SearchRequest(struct ldapsrv_call *call)
265 struct ldap_SearchRequest *req = &call->request.r.SearchRequest;
266 struct ldapsrv_partition *part;
268 DEBUG(10, ("SearchRequest"));
269 DEBUGADD(10, (" basedn: %s", req->basedn));
270 DEBUGADD(10, (" filter: %s\n", req->filter));
272 if (strcasecmp("", req->basedn) == 0) {
273 ldapsrv_RootDSE_Search(call, req);
277 part = ldapsrv_get_partition(call->conn, req->basedn);
279 if (!part->ops->Search) {
280 struct ldap_Result *done;
281 struct ldapsrv_reply *done_r;
283 done_r = ldapsrv_init_reply(call, LDAP_TAG_SearchResultDone);
285 ldapsrv_terminate_connection(call->conn, "ldapsrv_init_reply() failed");
289 done = &done_r->msg.r.SearchResultDone;
290 done->resultcode = 53;
292 done->errormessage = NULL;
293 done->referral = NULL;
295 ldapsrv_queue_reply(call, done_r);
299 part->ops->Search(part, call, req);
302 static void ldapsrv_ModifyRequest(struct ldapsrv_call *call)
304 struct ldap_ModifyRequest *req = &call->request.r.ModifyRequest;
305 struct ldapsrv_partition *part;
307 DEBUG(10, ("ModifyRequest"));
308 DEBUGADD(10, (" dn: %s", req->dn));
310 part = ldapsrv_get_partition(call->conn, req->dn);
312 if (!part->ops->Modify) {
313 ldapsrv_unwilling(call, 53);
317 part->ops->Modify(part, call, req);
320 static void ldapsrv_AddRequest(struct ldapsrv_call *call)
322 struct ldap_AddRequest *req = &call->request.r.AddRequest;
323 struct ldapsrv_partition *part;
325 DEBUG(10, ("AddRequest"));
326 DEBUGADD(10, (" dn: %s", req->dn));
328 part = ldapsrv_get_partition(call->conn, req->dn);
330 if (!part->ops->Add) {
331 ldapsrv_unwilling(call, 53);
335 part->ops->Add(part, call, req);
338 static void ldapsrv_DelRequest(struct ldapsrv_call *call)
340 struct ldap_DelRequest *req = &call->request.r.DelRequest;
341 struct ldapsrv_partition *part;
343 DEBUG(10, ("DelRequest"));
344 DEBUGADD(10, (" dn: %s", req->dn));
346 part = ldapsrv_get_partition(call->conn, req->dn);
348 if (!part->ops->Del) {
349 ldapsrv_unwilling(call, 53);
353 part->ops->Del(part, call, req);
356 static void ldapsrv_ModifyDNRequest(struct ldapsrv_call *call)
358 struct ldap_ModifyDNRequest *req = &call->request.r.ModifyDNRequest;
359 struct ldapsrv_partition *part;
361 DEBUG(10, ("ModifyDNRequrest"));
362 DEBUGADD(10, (" dn: %s", req->dn));
363 DEBUGADD(10, (" newrdn: %s", req->newrdn));
365 part = ldapsrv_get_partition(call->conn, req->dn);
367 if (!part->ops->ModifyDN) {
368 ldapsrv_unwilling(call, 53);
372 part->ops->ModifyDN(part, call, req);
375 static void ldapsrv_CompareRequest(struct ldapsrv_call *call)
377 struct ldap_CompareRequest *req = &call->request.r.CompareRequest;
378 struct ldapsrv_partition *part;
380 DEBUG(10, ("CompareRequest"));
381 DEBUGADD(10, (" dn: %s", req->dn));
383 part = ldapsrv_get_partition(call->conn, req->dn);
385 if (!part->ops->Compare) {
386 ldapsrv_unwilling(call, 53);
390 part->ops->Compare(part, call, req);
393 static void ldapsrv_AbandonRequest(struct ldapsrv_call *call)
395 /* struct ldap_AbandonRequest *req = &call->request.r.AbandonRequest;*/
396 DEBUG(10, ("AbandonRequest\n"));
399 static void ldapsrv_ExtendedRequest(struct ldapsrv_call *call)
401 /* struct ldap_ExtendedRequest *req = &call->request.r.ExtendedRequest;*/
402 struct ldapsrv_reply *reply;
404 DEBUG(10, ("Extended\n"));
406 reply = ldapsrv_init_reply(call, LDAP_TAG_ExtendedResponse);
408 ldapsrv_terminate_connection(call->conn, "ldapsrv_init_reply() failed");
412 ZERO_STRUCT(reply->msg.r);
414 ldapsrv_queue_reply(call, reply);
417 static void ldapsrv_do_call(struct ldapsrv_call *call)
419 switch(call->request.type) {
420 case LDAP_TAG_BindRequest:
421 ldapsrv_BindRequest(call);
423 case LDAP_TAG_UnbindRequest:
424 ldapsrv_UnbindRequest(call);
426 case LDAP_TAG_SearchRequest:
427 ldapsrv_SearchRequest(call);
429 case LDAP_TAG_ModifyRequest:
430 ldapsrv_ModifyRequest(call);
432 case LDAP_TAG_AddRequest:
433 ldapsrv_AddRequest(call);
435 case LDAP_TAG_DelRequest:
436 ldapsrv_DelRequest(call);
438 case LDAP_TAG_ModifyDNRequest:
439 ldapsrv_ModifyDNRequest(call);
441 case LDAP_TAG_CompareRequest:
442 ldapsrv_CompareRequest(call);
444 case LDAP_TAG_AbandonRequest:
445 ldapsrv_AbandonRequest(call);
447 case LDAP_TAG_ExtendedRequest:
448 ldapsrv_ExtendedRequest(call);
451 ldapsrv_unwilling(call, 2);
456 static void ldapsrv_do_responses(struct ldapsrv_connection *conn)
458 struct ldapsrv_call *call, *next_call = NULL;
459 struct ldapsrv_reply *reply, *next_reply = NULL;
461 for (call=conn->calls; call; call=next_call) {
462 for (reply=call->replies; reply; reply=next_reply) {
463 if (!ldap_append_to_buf(&reply->msg, &conn->out_buffer)) {
464 ldapsrv_terminate_connection(conn, "append_to_buf() failed");
467 next_reply = reply->next;
468 DLIST_REMOVE(call->replies, reply);
469 reply->state = LDAPSRV_REPLY_STATE_SEND;
472 next_call = call->next;
473 DLIST_REMOVE(conn->calls, call);
474 call->state = LDAPSRV_CALL_STATE_COMPLETE;
480 called when a LDAP socket becomes readable
482 static void ldapsrv_recv(struct server_connection *conn, time_t t,
485 struct ldapsrv_connection *ldap_conn = conn->private_data;
487 int buf_length, msg_length;
490 struct ldapsrv_call *call;
492 DEBUG(10,("ldapsrv_recv\n"));
494 if (!read_into_buf(conn->socket, &ldap_conn->in_buffer)) {
495 ldapsrv_terminate_connection(ldap_conn, "read_into_buf() failed");
499 peek_into_read_buf(&ldap_conn->in_buffer, &buf, &buf_length);
501 while (buf_length > 0) {
503 peek_into_read_buf(&ldap_conn->in_buffer, &buf, &buf_length);
504 /* LDAP Messages are always SEQUENCES */
506 if (!asn1_object_length(buf, buf_length, ASN1_SEQUENCE(0),
508 ldapsrv_terminate_connection(ldap_conn, "asn1_object_length() failed");
512 if (buf_length < msg_length) {
517 /* We've got a complete LDAP request in the in-buffer, convert
518 * that to a ldap_message and put it into the incoming
522 blob.length = msg_length;
524 if (!asn1_load(&data, blob)) {
525 ldapsrv_terminate_connection(ldap_conn, "asn1_load() failed");
529 call = talloc_p(ldap_conn, struct ldapsrv_call);
531 ldapsrv_terminate_connection(ldap_conn, "no memory");
536 call->state = LDAPSRV_CALL_STATE_NEW;
537 call->conn = ldap_conn;
538 call->request.mem_ctx = call;
540 if (!ldap_decode(&data, &call->request)) {
541 dump_data(0,buf, msg_length);
542 ldapsrv_terminate_connection(ldap_conn, "ldap_decode() failed");
546 DLIST_ADD_END(ldap_conn->calls, call,
547 struct ldapsrv_call *);
549 consumed_from_buf(&ldap_conn->in_buffer, msg_length);
551 ldapsrv_do_call(call);
553 peek_into_read_buf(&ldap_conn->in_buffer, &buf, &buf_length);
556 ldapsrv_do_responses(ldap_conn);
558 if (ldap_conn->out_buffer.length > 0) {
559 conn->event.fde->flags |= EVENT_FD_WRITE;
566 called when a LDAP socket becomes writable
568 static void ldapsrv_send(struct server_connection *conn, time_t t,
571 struct ldapsrv_connection *ldap_conn = conn->private_data;
573 DEBUG(10,("ldapsrv_send\n"));
575 if (!write_from_buf(conn->socket, &ldap_conn->out_buffer)) {
576 ldapsrv_terminate_connection(ldap_conn, "write_from_buf() failed");
580 if (ldap_conn->out_buffer.length == 0) {
581 conn->event.fde->flags &= ~EVENT_FD_WRITE;
588 called when connection is idle
590 static void ldapsrv_idle(struct server_connection *conn, time_t t)
592 DEBUG(10,("ldapsrv_idle: not implemented!\n"));
596 static void ldapsrv_close(struct server_connection *conn, const char *reason)
598 struct ldapsrv_connection *ldap_conn = conn->private_data;
600 talloc_free(ldap_conn);
606 initialise a server_context from a open socket and register a event handler
607 for reading from that socket
609 static void ldapsrv_accept(struct server_connection *conn)
611 struct ldapsrv_connection *ldap_conn;
613 DEBUG(5, ("ldapsrv_accept\n"));
615 ldap_conn = talloc_p(conn, struct ldapsrv_connection);
617 if (ldap_conn == NULL)
620 ZERO_STRUCTP(ldap_conn);
621 ldap_conn->connection = conn;
623 conn->private_data = ldap_conn;
629 called on a fatal error that should cause this server to terminate
631 static void ldapsrv_exit(struct server_service *service, const char *reason)
633 DEBUG(1,("ldapsrv_exit\n"));
637 static const struct server_service_ops ldap_server_ops = {
639 .service_init = ldapsrv_init,
640 .accept_connection = ldapsrv_accept,
641 .recv_handler = ldapsrv_recv,
642 .send_handler = ldapsrv_send,
643 .idle_handler = ldapsrv_idle,
644 .close_connection = ldapsrv_close,
645 .service_exit = ldapsrv_exit,
648 const struct server_service_ops *ldapsrv_get_ops(void)
650 return &ldap_server_ops;
653 NTSTATUS server_service_ldap_init(void)