Revert "s4:ldap_server: rewrite to socket layer to use tstream"
[garming/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 3 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, see <http://www.gnu.org/licenses/>.
22 */
23
24 #include "includes.h"
25 #include "lib/events/events.h"
26 #include "auth/auth.h"
27 #include "auth/credentials/credentials.h"
28 #include "librpc/gen_ndr/ndr_samr.h"
29 #include "../lib/util/dlinklist.h"
30 #include "../lib/util/asn1.h"
31 #include "ldap_server/ldap_server.h"
32 #include "smbd/service_task.h"
33 #include "smbd/service_stream.h"
34 #include "smbd/service.h"
35 #include "smbd/process_model.h"
36 #include "lib/tls/tls.h"
37 #include "lib/messaging/irpc.h"
38 #include "lib/ldb/include/ldb.h"
39 #include "lib/ldb/include/ldb_errors.h"
40 #include "libcli/ldap/ldap_proto.h"
41 #include "system/network.h"
42 #include "lib/socket/netif.h"
43 #include "dsdb/samdb/samdb.h"
44 #include "param/param.h"
45 /*
46   close the socket and shutdown a server_context
47 */
48 void ldapsrv_terminate_connection(struct ldapsrv_connection *conn,
49                                          const char *reason)
50 {
51         packet_recv_disable(conn->packet);
52         TALLOC_FREE(conn->packet);
53         TALLOC_FREE(conn->sockets.tls);
54         stream_terminate_connection(conn->connection, reason);
55 }
56
57 /*
58   handle packet errors
59 */
60 static void ldapsrv_error_handler(void *private_data, NTSTATUS status)
61 {
62         struct ldapsrv_connection *conn = talloc_get_type(private_data,
63                                                           struct ldapsrv_connection);
64         ldapsrv_terminate_connection(conn, nt_errstr(status));
65 }
66
67 /*
68   process a decoded ldap message
69 */
70 static void ldapsrv_process_message(struct ldapsrv_connection *conn,
71                                     struct ldap_message *msg)
72 {
73         struct ldapsrv_call *call;
74         NTSTATUS status;
75         DATA_BLOB blob;
76
77         call = talloc(conn, struct ldapsrv_call);
78         if (!call) {
79                 ldapsrv_terminate_connection(conn, "no memory");
80                 return;
81         }
82
83         call->request = talloc_steal(call, msg);
84         call->conn = conn;
85         call->replies = NULL;
86         call->send_callback = NULL;
87         call->send_private = NULL;
88
89         /* make the call */
90         status = ldapsrv_do_call(call);
91         if (!NT_STATUS_IS_OK(status)) {
92                 talloc_free(call);
93                 return;
94         }
95
96         blob = data_blob(NULL, 0);
97
98         if (call->replies == NULL) {
99                 talloc_free(call);
100                 return;
101         }
102
103         /* build all the replies into a single blob */
104         while (call->replies) {
105                 DATA_BLOB b;
106                 bool ret;
107
108                 msg = call->replies->msg;
109                 if (!ldap_encode(msg, samba_ldap_control_handlers(), &b, call)) {
110                         DEBUG(0,("Failed to encode ldap reply of type %d\n", msg->type));
111                         talloc_free(call);
112                         return;
113                 }
114
115                 ret = data_blob_append(call, &blob, b.data, b.length);
116                 data_blob_free(&b);
117
118                 talloc_set_name_const(blob.data, "Outgoing, encoded LDAP packet");
119
120                 if (!ret) {
121                         talloc_free(call);
122                         return;
123                 }
124
125                 DLIST_REMOVE(call->replies, call->replies);
126         }
127
128         packet_send_callback(conn->packet, blob,
129                              call->send_callback, call->send_private);
130         talloc_free(call);
131         return;
132 }
133
134 /*
135   disable packets on other sockets while processing this one
136  */
137 static void ldapsrv_disable_recv(struct ldapsrv_connection *conn)
138 {
139         struct ldapsrv_packet_interfaces *p;
140         for (p=conn->service->packet_interfaces; p; p=p->next) {
141                 if (p->packet != conn->packet) {
142                         packet_recv_disable(p->packet);
143                 }
144         }
145 }
146
147 /*
148   disable packets on other sockets while processing this one
149  */
150 static void ldapsrv_enable_recv(struct ldapsrv_connection *conn)
151 {
152         struct ldapsrv_packet_interfaces *p;
153         for (p=conn->service->packet_interfaces; p; p=p->next) {
154                 if (p->packet != conn->packet) {
155                         packet_recv_enable(p->packet);
156                 }
157         }
158 }
159
160 /*
161   decode/process data
162 */
163 static NTSTATUS ldapsrv_decode(void *private_data, DATA_BLOB blob)
164 {
165         NTSTATUS status;
166         struct ldapsrv_connection *conn = talloc_get_type(private_data,
167                                                           struct ldapsrv_connection);
168         struct asn1_data *asn1 = asn1_init(conn);
169         struct ldap_message *msg = talloc(conn, struct ldap_message);
170
171         if (asn1 == NULL || msg == NULL) {
172                 return NT_STATUS_NO_MEMORY;
173         }
174
175         if (!asn1_load(asn1, blob)) {
176                 talloc_free(msg);
177                 talloc_free(asn1);
178                 return NT_STATUS_NO_MEMORY;
179         }
180
181         status = ldap_decode(asn1, samba_ldap_control_handlers(), msg);
182         if (!NT_STATUS_IS_OK(status)) {
183                 asn1_free(asn1);
184                 return status;
185         }
186
187         data_blob_free(&blob);
188         talloc_steal(conn, msg);
189         asn1_free(asn1);
190
191         /* disable messages on other sockets while processing this one */
192         ldapsrv_disable_recv(conn);
193
194         ldapsrv_process_message(conn, msg);
195
196         ldapsrv_enable_recv(conn);
197
198         return NT_STATUS_OK;
199 }
200
201 /*
202  Idle timeout handler
203 */
204 static void ldapsrv_conn_idle_timeout(struct tevent_context *ev,
205                                       struct tevent_timer *te,
206                                       struct timeval t,
207                                       void *private_data)
208 {
209         struct ldapsrv_connection *conn = talloc_get_type(private_data, struct ldapsrv_connection);
210
211         ldapsrv_terminate_connection(conn, "Timeout. No requests after bind");
212 }
213
214 /*
215   called when a LDAP socket becomes readable
216 */
217 void ldapsrv_recv(struct stream_connection *c, uint16_t flags)
218 {
219         struct ldapsrv_connection *conn =
220                 talloc_get_type(c->private_data, struct ldapsrv_connection);
221
222         if (conn->limits.ite) { /* clean initial timeout if any */
223                 talloc_free(conn->limits.ite);
224                 conn->limits.ite = NULL;
225         }
226
227         if (conn->limits.te) { /* clean idle timeout if any */
228                 talloc_free(conn->limits.te);
229                 conn->limits.te = NULL;
230         }
231
232         packet_recv(conn->packet);
233
234         /* set idle timeout */
235         conn->limits.te = event_add_timed(c->event.ctx, conn,
236                                            timeval_current_ofs(conn->limits.conn_idle_time, 0),
237                                            ldapsrv_conn_idle_timeout, conn);
238 }
239
240 /*
241   called when a LDAP socket becomes writable
242 */
243 static void ldapsrv_send(struct stream_connection *c, uint16_t flags)
244 {
245         struct ldapsrv_connection *conn =
246                 talloc_get_type(c->private_data, struct ldapsrv_connection);
247
248         packet_queue_run(conn->packet);
249 }
250
251 static void ldapsrv_conn_init_timeout(struct tevent_context *ev,
252                                       struct tevent_timer *te,
253                                       struct timeval t,
254                                       void *private_data)
255 {
256         struct ldapsrv_connection *conn = talloc_get_type(private_data, struct ldapsrv_connection);
257
258         ldapsrv_terminate_connection(conn, "Timeout. No requests after initial connection");
259 }
260
261 static int ldapsrv_load_limits(struct ldapsrv_connection *conn)
262 {
263         TALLOC_CTX *tmp_ctx;
264         const char *attrs[] = { "configurationNamingContext", NULL };
265         const char *attrs2[] = { "lDAPAdminLimits", NULL };
266         struct ldb_message_element *el;
267         struct ldb_result *res = NULL;
268         struct ldb_dn *basedn;
269         struct ldb_dn *conf_dn;
270         struct ldb_dn *policy_dn;
271         unsigned int i;
272         int ret;
273
274         /* set defaults limits in case of failure */
275         conn->limits.initial_timeout = 120;
276         conn->limits.conn_idle_time = 900;
277         conn->limits.max_page_size = 1000;
278         conn->limits.search_timeout = 120;
279
280
281         tmp_ctx = talloc_new(conn);
282         if (tmp_ctx == NULL) {
283                 return -1;
284         }
285
286         basedn = ldb_dn_new(tmp_ctx, conn->ldb, NULL);
287         if ( ! ldb_dn_validate(basedn)) {
288                 goto failed;
289         }
290
291         ret = ldb_search(conn->ldb, tmp_ctx, &res, basedn, LDB_SCOPE_BASE, attrs, NULL);
292         if (ret != LDB_SUCCESS) {
293                 goto failed;
294         }
295
296         if (res->count != 1) {
297                 goto failed;
298         }
299
300         conf_dn = ldb_msg_find_attr_as_dn(conn->ldb, tmp_ctx, res->msgs[0], "configurationNamingContext");
301         if (conf_dn == NULL) {
302                 goto failed;
303         }
304
305         policy_dn = ldb_dn_copy(tmp_ctx, conf_dn);
306         ldb_dn_add_child_fmt(policy_dn, "CN=Default Query Policy,CN=Query-Policies,CN=Directory Service,CN=Windows NT,CN=Services");
307         if (policy_dn == NULL) {
308                 goto failed;
309         }
310
311         ret = ldb_search(conn->ldb, tmp_ctx, &res, policy_dn, LDB_SCOPE_BASE, attrs2, NULL);
312         if (ret != LDB_SUCCESS) {
313                 goto failed;
314         }
315
316         if (res->count != 1) {
317                 goto failed;
318         }
319
320         el = ldb_msg_find_element(res->msgs[0], "lDAPAdminLimits");
321         if (el == NULL) {
322                 goto failed;
323         }
324
325         for (i = 0; i < el->num_values; i++) {
326                 char policy_name[256];
327                 int policy_value, s;
328
329                 s = sscanf((const char *)el->values[i].data, "%255[^=]=%d", policy_name, &policy_value);
330                 if (ret != 2 || policy_value == 0)
331                         continue;
332
333                 if (strcasecmp("InitRecvTimeout", policy_name) == 0) {
334                         conn->limits.initial_timeout = policy_value;
335                         continue;
336                 }
337                 if (strcasecmp("MaxConnIdleTime", policy_name) == 0) {
338                         conn->limits.conn_idle_time = policy_value;
339                         continue;
340                 }
341                 if (strcasecmp("MaxPageSize", policy_name) == 0) {
342                         conn->limits.max_page_size = policy_value;
343                         continue;
344                 }
345                 if (strcasecmp("MaxQueryDuration", policy_name) == 0) {
346                         conn->limits.search_timeout = policy_value;
347                         continue;
348                 }
349         }
350
351         return 0;
352
353 failed:
354         DEBUG(0, ("Failed to load ldap server query policies\n"));
355         talloc_free(tmp_ctx);
356         return -1;
357 }
358
359 /*
360   remove a packet interface from the service level list
361  */
362 static int packet_interface_destructor(struct ldapsrv_packet_interfaces *packet_interface)
363 {
364         DLIST_REMOVE(packet_interface->service->packet_interfaces, packet_interface);
365         return 0;
366 }
367
368 /*
369   initialise a server_context from a open socket and register a event handler
370   for reading from that socket
371 */
372 static void ldapsrv_accept(struct stream_connection *c,
373                            struct auth_session_info *session_info)
374 {
375         struct ldapsrv_service *ldapsrv_service = 
376                 talloc_get_type(c->private_data, struct ldapsrv_service);
377         struct ldapsrv_connection *conn;
378         struct cli_credentials *server_credentials;
379         struct socket_address *socket_address;
380         NTSTATUS status;
381         int port;
382
383         conn = talloc_zero(c, struct ldapsrv_connection);
384         if (!conn) {
385                 stream_terminate_connection(c, "ldapsrv_accept: out of memory");
386                 return;
387         }
388
389         conn->packet      = NULL;
390         conn->connection  = c;
391         conn->service     = ldapsrv_service;
392         conn->sockets.raw = c->socket;
393         conn->lp_ctx      = ldapsrv_service->task->lp_ctx;
394
395         c->private_data   = conn;
396
397         socket_address = socket_get_my_addr(c->socket, conn);
398         if (!socket_address) {
399                 ldapsrv_terminate_connection(conn, "ldapsrv_accept: failed to obtain local socket address!");
400                 return;
401         }
402         port = socket_address->port;
403         talloc_free(socket_address);
404
405         if (port == 636) {
406                 struct socket_context *tls_socket = tls_init_server(ldapsrv_service->tls_params, c->socket,
407                                                                     c->event.fde, NULL);
408                 if (!tls_socket) {
409                         ldapsrv_terminate_connection(conn, "ldapsrv_accept: tls_init_server() failed");
410                         return;
411                 }
412                 talloc_steal(c, tls_socket);
413                 c->socket = tls_socket;
414                 conn->sockets.tls = tls_socket;
415
416         } else if (port == 3268) /* Global catalog */ {
417                 conn->global_catalog = true;
418         }
419         conn->packet = packet_init(conn);
420         if (conn->packet == NULL) {
421                 ldapsrv_terminate_connection(conn, "out of memory");
422                 return;
423         }
424
425         packet_set_private(conn->packet, conn);
426         packet_set_socket(conn->packet, c->socket);
427         packet_set_callback(conn->packet, ldapsrv_decode);
428         packet_set_full_request(conn->packet, ldap_full_packet);
429         packet_set_error_handler(conn->packet, ldapsrv_error_handler);
430         packet_set_event_context(conn->packet, c->event.ctx);
431         packet_set_fde(conn->packet, c->event.fde);
432         packet_set_serialise(conn->packet);
433
434         if (conn->sockets.tls) {
435                 packet_set_unreliable_select(conn->packet);
436         }
437
438         /* Ensure we don't get packets until the database is ready below */
439         packet_recv_disable(conn->packet);
440
441         /* add to the service level list of packet interfaces, to
442          * allow us to serialise between connections
443          */
444         conn->packet_interface = talloc(conn, struct ldapsrv_packet_interfaces);
445         if (conn->packet_interface == NULL) {
446                 ldapsrv_terminate_connection(conn, "out of memory");
447         }
448         conn->packet_interface->service = ldapsrv_service;
449         conn->packet_interface->packet = conn->packet;
450         DLIST_ADD(conn->service->packet_interfaces, conn->packet_interface);
451         talloc_set_destructor(conn->packet_interface, packet_interface_destructor);
452
453         server_credentials = cli_credentials_init(conn);
454         if (!server_credentials) {
455                 stream_terminate_connection(c, "Failed to init server credentials\n");
456                 return;
457         }
458
459         cli_credentials_set_conf(server_credentials, conn->lp_ctx);
460         status = cli_credentials_set_machine_account(server_credentials, conn->lp_ctx);
461         if (!NT_STATUS_IS_OK(status)) {
462                 stream_terminate_connection(c, talloc_asprintf(conn, "Failed to obtain server credentials, perhaps a standalone server?: %s\n", nt_errstr(status)));
463                 return;
464         }
465         conn->server_credentials = server_credentials;
466
467         conn->session_info = talloc_move(conn, &session_info);
468
469         if (!NT_STATUS_IS_OK(ldapsrv_backend_Init(conn))) {
470                 ldapsrv_terminate_connection(conn, "backend Init failed");
471                 return;
472         }
473
474         /* load limits from the conf partition */
475         ldapsrv_load_limits(conn); /* should we fail on error ? */
476
477         /* register the server */       
478         irpc_add_name(c->msg_ctx, "ldap_server");
479
480         /* set connections limits */
481         conn->limits.ite = event_add_timed(c->event.ctx, conn,
482                                            timeval_current_ofs(conn->limits.initial_timeout, 0),
483                                            ldapsrv_conn_init_timeout, conn);
484
485         packet_recv_enable(conn->packet);
486
487 }
488
489 static void ldapsrv_accept_nonpriv(struct stream_connection *c)
490 {
491         struct ldapsrv_service *ldapsrv_service = talloc_get_type_abort(
492                 c->private_data, struct ldapsrv_service);
493         struct auth_session_info *session_info;
494         NTSTATUS status;
495
496         status = auth_anonymous_session_info(
497                 c, ldapsrv_service->task->lp_ctx, &session_info);
498         if (!NT_STATUS_IS_OK(status)) {
499                 stream_terminate_connection(c, "failed to setup anonymous "
500                                             "session info");
501                 return;
502         }
503         ldapsrv_accept(c, session_info);
504 }
505
506 static const struct stream_server_ops ldap_stream_nonpriv_ops = {
507         .name                   = "ldap",
508         .accept_connection      = ldapsrv_accept_nonpriv,
509         .recv_handler           = ldapsrv_recv,
510         .send_handler           = ldapsrv_send,
511 };
512
513 /* The feature removed behind an #ifdef until we can do it properly
514  * with an EXTERNAL bind. */
515
516 #define WITH_LDAPI_PRIV_SOCKET
517
518 #ifdef WITH_LDAPI_PRIV_SOCKET
519 static void ldapsrv_accept_priv(struct stream_connection *c)
520 {
521         struct ldapsrv_service *ldapsrv_service = talloc_get_type_abort(
522                 c->private_data, struct ldapsrv_service);
523         struct auth_session_info *session_info;
524
525         session_info = system_session(ldapsrv_service->task->lp_ctx);
526         if (!session_info) {
527                 stream_terminate_connection(c, "failed to setup system "
528                                             "session info");
529                 return;
530         }
531         ldapsrv_accept(c, session_info);
532 }
533
534 static const struct stream_server_ops ldap_stream_priv_ops = {
535         .name                   = "ldap",
536         .accept_connection      = ldapsrv_accept_priv,
537         .recv_handler           = ldapsrv_recv,
538         .send_handler           = ldapsrv_send,
539 };
540
541 #endif
542 /*
543   add a socket address to the list of events, one event per port
544 */
545 static NTSTATUS add_socket(struct tevent_context *event_context,
546                            struct loadparm_context *lp_ctx,
547                            const struct model_ops *model_ops,
548                            const char *address, struct ldapsrv_service *ldap_service)
549 {
550         uint16_t port = 389;
551         NTSTATUS status;
552         struct ldb_context *ldb;
553
554         status = stream_setup_socket(event_context, lp_ctx,
555                                      model_ops, &ldap_stream_nonpriv_ops,
556                                      "ipv4", address, &port, 
557                                      lpcfg_socket_options(lp_ctx),
558                                      ldap_service);
559         if (!NT_STATUS_IS_OK(status)) {
560                 DEBUG(0,("ldapsrv failed to bind to %s:%u - %s\n",
561                          address, port, nt_errstr(status)));
562                 return status;
563         }
564
565         if (tls_support(ldap_service->tls_params)) {
566                 /* add ldaps server */
567                 port = 636;
568                 status = stream_setup_socket(event_context, lp_ctx,
569                                              model_ops,
570                                              &ldap_stream_nonpriv_ops,
571                                              "ipv4", address, &port, 
572                                              lpcfg_socket_options(lp_ctx),
573                                              ldap_service);
574                 if (!NT_STATUS_IS_OK(status)) {
575                         DEBUG(0,("ldapsrv failed to bind to %s:%u - %s\n",
576                                  address, port, nt_errstr(status)));
577                         return status;
578                 }
579         }
580
581         /* Load LDAP database, but only to read our settings */
582         ldb = samdb_connect(ldap_service, ldap_service->task->event_ctx, 
583                             lp_ctx, system_session(lp_ctx));
584         if (!ldb) {
585                 return NT_STATUS_INTERNAL_DB_CORRUPTION;
586         }
587
588         if (samdb_is_gc(ldb)) {
589                 port = 3268;
590                 status = stream_setup_socket(event_context, lp_ctx,
591                                              model_ops,
592                                              &ldap_stream_nonpriv_ops,
593                                              "ipv4", address, &port, 
594                                              lpcfg_socket_options(lp_ctx),
595                                              ldap_service);
596                 if (!NT_STATUS_IS_OK(status)) {
597                         DEBUG(0,("ldapsrv failed to bind to %s:%u - %s\n",
598                                  address, port, nt_errstr(status)));
599                         return status;
600                 }
601         }
602
603         /* And once we are bound, free the tempoary ldb, it will
604          * connect again on each incoming LDAP connection */
605         talloc_unlink(ldap_service, ldb);
606
607         return NT_STATUS_OK;
608 }
609
610 /*
611   open the ldap server sockets
612 */
613 static void ldapsrv_task_init(struct task_server *task)
614 {       
615         char *ldapi_path;
616 #ifdef WITH_LDAPI_PRIV_SOCKET
617         char *priv_dir;
618 #endif
619         struct ldapsrv_service *ldap_service;
620         NTSTATUS status;
621         const struct model_ops *model_ops;
622
623         switch (lpcfg_server_role(task->lp_ctx)) {
624         case ROLE_STANDALONE:
625                 task_server_terminate(task, "ldap_server: no LDAP server required in standalone configuration", 
626                                       false);
627                 return;
628         case ROLE_DOMAIN_MEMBER:
629                 task_server_terminate(task, "ldap_server: no LDAP server required in member server configuration", 
630                                       false);
631                 return;
632         case ROLE_DOMAIN_CONTROLLER:
633                 /* Yes, we want an LDAP server */
634                 break;
635         }
636
637         task_server_set_title(task, "task[ldapsrv]");
638
639         /* run the ldap server as a single process */
640         model_ops = process_model_startup(task->event_ctx, "single");
641         if (!model_ops) goto failed;
642
643         ldap_service = talloc_zero(task, struct ldapsrv_service);
644         if (ldap_service == NULL) goto failed;
645
646         ldap_service->task = task;
647
648         ldap_service->tls_params = tls_initialise(ldap_service, task->lp_ctx);
649         if (ldap_service->tls_params == NULL) goto failed;
650
651         if (lpcfg_interfaces(task->lp_ctx) && lpcfg_bind_interfaces_only(task->lp_ctx)) {
652                 struct interface *ifaces;
653                 int num_interfaces;
654                 int i;
655
656                 load_interfaces(task, lpcfg_interfaces(task->lp_ctx), &ifaces);
657                 num_interfaces = iface_count(ifaces);
658
659                 /* We have been given an interfaces line, and been 
660                    told to only bind to those interfaces. Create a
661                    socket per interface and bind to only these.
662                 */
663                 for(i = 0; i < num_interfaces; i++) {
664                         const char *address = iface_n_ip(ifaces, i);
665                         status = add_socket(task->event_ctx, task->lp_ctx, model_ops, address, ldap_service);
666                         if (!NT_STATUS_IS_OK(status)) goto failed;
667                 }
668         } else {
669                 status = add_socket(task->event_ctx, task->lp_ctx, model_ops,
670                                     lpcfg_socket_address(task->lp_ctx), ldap_service);
671                 if (!NT_STATUS_IS_OK(status)) goto failed;
672         }
673
674         ldapi_path = private_path(ldap_service, task->lp_ctx, "ldapi");
675         if (!ldapi_path) {
676                 goto failed;
677         }
678
679         status = stream_setup_socket(task->event_ctx, task->lp_ctx,
680                                      model_ops, &ldap_stream_nonpriv_ops,
681                                      "unix", ldapi_path, NULL, 
682                                      lpcfg_socket_options(task->lp_ctx),
683                                      ldap_service);
684         talloc_free(ldapi_path);
685         if (!NT_STATUS_IS_OK(status)) {
686                 DEBUG(0,("ldapsrv failed to bind to %s - %s\n",
687                          ldapi_path, nt_errstr(status)));
688         }
689
690 #ifdef WITH_LDAPI_PRIV_SOCKET
691         priv_dir = private_path(ldap_service, task->lp_ctx, "ldap_priv");
692         if (priv_dir == NULL) {
693                 goto failed;
694         }
695         /*
696          * Make sure the directory for the privileged ldapi socket exists, and
697          * is of the correct permissions
698          */
699         if (!directory_create_or_exist(priv_dir, geteuid(), 0750)) {
700                 task_server_terminate(task, "Cannot create ldap "
701                                       "privileged ldapi directory", true);
702                 return;
703         }
704         ldapi_path = talloc_asprintf(ldap_service, "%s/ldapi", priv_dir);
705         talloc_free(priv_dir);
706         if (ldapi_path == NULL) {
707                 goto failed;
708         }
709
710         status = stream_setup_socket(task->event_ctx, task->lp_ctx,
711                                      model_ops, &ldap_stream_priv_ops,
712                                      "unix", ldapi_path, NULL,
713                                      lpcfg_socket_options(task->lp_ctx),
714                                      ldap_service);
715         talloc_free(ldapi_path);
716         if (!NT_STATUS_IS_OK(status)) {
717                 DEBUG(0,("ldapsrv failed to bind to %s - %s\n",
718                          ldapi_path, nt_errstr(status)));
719         }
720
721 #endif
722         return;
723
724 failed:
725         task_server_terminate(task, "Failed to startup ldap server task", true);
726 }
727
728
729 NTSTATUS server_service_ldap_init(void)
730 {
731         return register_server_service("ldap", ldapsrv_task_init);
732 }