summaryrefslogtreecommitdiff
path: root/source3/rpc_server/lsasd.c
blob: ad184d89c951523dd84d83b267dad6c2c24ac3b0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
/*
 *  Unix SMB/CIFS implementation.
 *
 *  LSA service daemon
 *
 *  Copyright (c) 2011      Andreas Schneider <asn@samba.org>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, see <http://www.gnu.org/licenses/>.
 */

#include "includes.h"
#include "serverid.h"
#include "messages.h"
#include "ntdomain.h"
#include <libgen.h>

#include "lib/id_cache.h"

#include "../lib/tsocket/tsocket.h"
#include "lib/server_prefork.h"
#include "lib/server_prefork_util.h"
#include "librpc/rpc/dcerpc_ep.h"

#include "rpc_server/rpc_server.h"
#include "rpc_server/rpc_ep_register.h"
#include "rpc_server/rpc_sock_helper.h"

#include "librpc/gen_ndr/srv_lsa.h"
#include "librpc/gen_ndr/srv_samr.h"
#include "librpc/gen_ndr/srv_netlogon.h"

#define DAEMON_NAME "lsasd"
#define LSASD_MAX_SOCKETS 64

static struct server_id parent_id;
static struct prefork_pool *lsasd_pool = NULL;
static int lsasd_child_id = 0;

static struct pf_daemon_config default_pf_lsasd_cfg = {
	.prefork_status = PFH_INIT,
	.min_children = 5,
	.max_children = 25,
	.spawn_rate = 5,
	.max_allowed_clients = 100,
	.child_min_life = 60 /* 1 minute minimum life time */
};
static struct pf_daemon_config pf_lsasd_cfg = { 0 };

void start_lsasd(struct tevent_context *ev_ctx,
		 struct messaging_context *msg_ctx);

static void lsasd_reopen_logs(int child_id)
{
	char *lfile = lp_logfile();
	char *extension;
	int rc;

	if (child_id) {
		rc = asprintf(&extension, "%s.%d", DAEMON_NAME, child_id);
	} else {
		rc = asprintf(&extension, "%s", DAEMON_NAME);
	}
	if (rc == -1) {
		return;
	}

	rc = 0;
	if (lfile == NULL || lfile[0] == '\0') {
		rc = asprintf(&lfile, "%s/log.%s",
			      get_dyn_LOGFILEBASE(), extension);
	} else {
		if (strstr(lfile, extension) == NULL) {
			if (child_id) {
				rc = asprintf(&lfile, "%s.%d",
						lp_logfile(),
						child_id);
			} else {
				rc = asprintf(&lfile, "%s.%s",
						lp_logfile(),
						extension);
			}
		}
	}

	if (rc > 0) {
		lp_set_logfile(lfile);
		SAFE_FREE(lfile);
	}

	SAFE_FREE(extension);

	reopen_logs();
}

static void lsasd_smb_conf_updated(struct messaging_context *msg,
				  void *private_data,
				  uint32_t msg_type,
				  struct server_id server_id,
				  DATA_BLOB *data)
{
	struct tevent_context *ev_ctx;

	DEBUG(10, ("Got message saying smb.conf was updated. Reloading.\n"));
	ev_ctx = talloc_get_type_abort(private_data, struct tevent_context);

	change_to_root_user();
	lp_load(get_dyn_CONFIGFILE(), true, false, false, true);

	lsasd_reopen_logs(lsasd_child_id);
	if (lsasd_child_id == 0) {
		pfh_daemon_config(DAEMON_NAME,
				  &pf_lsasd_cfg,
				  &default_pf_lsasd_cfg);
		pfh_manage_pool(ev_ctx, msg, &pf_lsasd_cfg, lsasd_pool);
	}
}

static void lsasd_sig_term_handler(struct tevent_context *ev,
				  struct tevent_signal *se,
				  int signum,
				  int count,
				  void *siginfo,
				  void *private_data)
{
	rpc_netlogon_shutdown();
	rpc_samr_shutdown();
	rpc_lsarpc_shutdown();

	DEBUG(0, ("termination signal\n"));
	exit(0);
}

static void lsasd_setup_sig_term_handler(struct tevent_context *ev_ctx)
{
	struct tevent_signal *se;

	se = tevent_add_signal(ev_ctx,
			       ev_ctx,
			       SIGTERM, 0,
			       lsasd_sig_term_handler,
			       NULL);
	if (!se) {
		DEBUG(0, ("failed to setup SIGTERM handler\n"));
		exit(1);
	}
}

static void lsasd_sig_hup_handler(struct tevent_context *ev,
				    struct tevent_signal *se,
				    int signum,
				    int count,
				    void *siginfo,
				    void *pvt)
{

	change_to_root_user();
	lp_load(get_dyn_CONFIGFILE(), true, false, false, true);

	lsasd_reopen_logs(lsasd_child_id);
	pfh_daemon_config(DAEMON_NAME,
			  &pf_lsasd_cfg,
			  &default_pf_lsasd_cfg);

	/* relay to all children */
	prefork_send_signal_to_all(lsasd_pool, SIGHUP);
}

static void lsasd_setup_sig_hup_handler(struct tevent_context *ev_ctx)
{
	struct tevent_signal *se;

	se = tevent_add_signal(ev_ctx,
			       ev_ctx,
			       SIGHUP, 0,
			       lsasd_sig_hup_handler,
			       NULL);
	if (!se) {
		DEBUG(0, ("failed to setup SIGHUP handler\n"));
		exit(1);
	}
}

/**********************************************************
 * Children
 **********************************************************/

static void lsasd_chld_sig_hup_handler(struct tevent_context *ev,
					 struct tevent_signal *se,
					 int signum,
					 int count,
					 void *siginfo,
					 void *pvt)
{
	change_to_root_user();
	lsasd_reopen_logs(lsasd_child_id);
}

static bool lsasd_setup_chld_hup_handler(struct tevent_context *ev_ctx)
{
	struct tevent_signal *se;

	se = tevent_add_signal(ev_ctx,
			       ev_ctx,
			       SIGHUP, 0,
			       lsasd_chld_sig_hup_handler,
			       NULL);
	if (!se) {
		DEBUG(1, ("failed to setup SIGHUP handler"));
		return false;
	}

	return true;
}

static void parent_ping(struct messaging_context *msg_ctx,
			void *private_data,
			uint32_t msg_type,
			struct server_id server_id,
			DATA_BLOB *data)
{

	/* The fact we received this message is enough to let make the event
	 * loop if it was idle. lsasd_children_main will cycle through
	 * lsasd_next_client at least once. That function will take whatever
	 * action is necessary */

	DEBUG(10, ("Got message that the parent changed status.\n"));
	return;
}

static bool lsasd_child_init(struct tevent_context *ev_ctx,
			     int child_id,
			     struct pf_worker_data *pf)
{
	NTSTATUS status;
	struct messaging_context *msg_ctx = server_messaging_context();
	bool ok;

	status = reinit_after_fork(msg_ctx, ev_ctx,
				   true);
	if (!NT_STATUS_IS_OK(status)) {
		DEBUG(0,("reinit_after_fork() failed\n"));
		smb_panic("reinit_after_fork() failed");
	}

	lsasd_child_id = child_id;
	lsasd_reopen_logs(child_id);

	ok = lsasd_setup_chld_hup_handler(ev_ctx);
	if (!ok) {
		return false;
	}

	if (!serverid_register(procid_self(), FLAG_MSG_GENERAL)) {
		return false;
	}

	messaging_register(msg_ctx, ev_ctx,
			   MSG_SMB_CONF_UPDATED, lsasd_smb_conf_updated);
	messaging_register(msg_ctx, ev_ctx,
			   MSG_PREFORK_PARENT_EVENT, parent_ping);
	id_cache_register_msgs(msg_ctx);

	status = rpc_lsarpc_init(NULL);
	if (!NT_STATUS_IS_OK(status)) {
		DEBUG(0, ("Failed to register lsarpc rpc inteface! (%s)\n",
			  nt_errstr(status)));
		return false;
	}

	status = rpc_samr_init(NULL);
	if (!NT_STATUS_IS_OK(status)) {
		DEBUG(0, ("Failed to register samr rpc inteface! (%s)\n",
			  nt_errstr(status)));
		return false;
	}

	status = rpc_netlogon_init(NULL);
	if (!NT_STATUS_IS_OK(status)) {
		DEBUG(0, ("Failed to register netlogon rpc inteface! (%s)\n",
			  nt_errstr(status)));
		return false;
	}

	return true;
}

struct lsasd_children_data {
	struct tevent_context *ev_ctx;
	struct messaging_context *msg_ctx;
	struct pf_worker_data *pf;
	int listen_fd_size;
	int *listen_fds;
};

static void lsasd_next_client(void *pvt);

static int lsasd_children_main(struct tevent_context *ev_ctx,
			       struct messaging_context *msg_ctx,
			       struct pf_worker_data *pf,
			       int child_id,
			       int listen_fd_size,
			       int *listen_fds,
			       void *private_data)
{
	struct lsasd_children_data *data;
	bool ok;
	int ret = 0;

	ok = lsasd_child_init(ev_ctx, child_id, pf);
	if (!ok) {
		return 1;
	}

	data = talloc(ev_ctx, struct lsasd_children_data);
	if (!data) {
		return 1;
	}
	data->pf = pf;
	data->ev_ctx = ev_ctx;
	data->msg_ctx = msg_ctx;
	data->listen_fd_size = listen_fd_size;
	data->listen_fds = listen_fds;

	/* loop until it is time to exit */
	while (pf->status != PF_WORKER_EXITING) {
		/* try to see if it is time to schedule the next client */
		lsasd_next_client(data);

		ret = tevent_loop_once(ev_ctx);
		if (ret != 0) {
			DEBUG(0, ("tevent_loop_once() exited with %d: %s\n",
				  ret, strerror(errno)));
			pf->status = PF_WORKER_EXITING;
		}
	}

	return ret;
}

static void lsasd_client_terminated(void *pvt)
{
	struct lsasd_children_data *data;

	data = talloc_get_type_abort(pvt, struct lsasd_children_data);

	pfh_client_terminated(data->pf);

	lsasd_next_client(pvt);
}

struct lsasd_new_client {
	struct lsasd_children_data *data;
};

static void lsasd_handle_client(struct tevent_req *req);

static void lsasd_next_client(void *pvt)
{
	struct tevent_req *req;
	struct lsasd_children_data *data;
	struct lsasd_new_client *next;

	data = talloc_get_type_abort(pvt, struct lsasd_children_data);

	if (!pfh_child_allowed_to_accept(data->pf)) {
		/* nothing to do for now we are already listening
		 * or we are not allowed to listen further */
		return;
	}

	next = talloc_zero(data, struct lsasd_new_client);
	if (!next) {
		DEBUG(1, ("Out of memory!?\n"));
		return;
	}
	next->data = data;

	req = prefork_listen_send(next,
				  data->ev_ctx,
				  data->pf,
				  data->listen_fd_size,
				  data->listen_fds);
	if (!req) {
		DEBUG(1, ("Failed to make listening request!?\n"));
		talloc_free(next);
		return;
	}
	tevent_req_set_callback(req, lsasd_handle_client, next);
}

static void lsasd_handle_client(struct tevent_req *req)
{
	struct lsasd_children_data *data;
	struct lsasd_new_client *client;
	const DATA_BLOB ping = data_blob_null;
	int rc;
	int sd;
	TALLOC_CTX *tmp_ctx;
	struct tsocket_address *srv_addr;
	struct tsocket_address *cli_addr;

	client = tevent_req_callback_data(req, struct lsasd_new_client);
	data = client->data;

	tmp_ctx = talloc_stackframe();
	if (tmp_ctx == NULL) {
		DEBUG(1, ("Failed to allocate stackframe!\n"));
		return;
	}

	rc = prefork_listen_recv(req,
				 tmp_ctx,
				 &sd,
				 &srv_addr,
				 &cli_addr);

	/* this will free the request too */
	talloc_free(client);

	if (rc != 0) {
		DEBUG(6, ("No client connection was available after all!\n"));
		goto done;
	}

	/* Warn parent that our status changed */
	messaging_send(data->msg_ctx, parent_id,
			MSG_PREFORK_CHILD_EVENT, &ping);

	DEBUG(2, ("LSASD preforked child %d got client connection!\n",
		  (int)(data->pf->pid)));

	if (tsocket_address_is_inet(srv_addr, "ip")) {
		DEBUG(3, ("Got a tcpip client connection from %s on inteface %s\n",
			   tsocket_address_string(cli_addr, tmp_ctx),
			   tsocket_address_string(srv_addr, tmp_ctx)));

		dcerpc_ncacn_accept(data->ev_ctx,
				    data->msg_ctx,
				    NCACN_IP_TCP,
				    "IP",
				    cli_addr,
				    srv_addr,
				    sd,
				    NULL);
	} else if (tsocket_address_is_unix(srv_addr)) {
		char *p;

		p = tsocket_address_unix_path(srv_addr, tmp_ctx);
		if (p == NULL) {
			talloc_free(tmp_ctx);
			return;
		}

		if (strstr(p, "/np/")) {
			p = basename(p);

			named_pipe_accept_function(data->ev_ctx,
						   data->msg_ctx,
						   p,
						   sd,
						   lsasd_client_terminated,
						   data);
		} else {
			p = basename(p);

			dcerpc_ncacn_accept(data->ev_ctx,
					    data->msg_ctx,
					    NCALRPC,
					    p,
					    cli_addr,
					    srv_addr,
					    sd,
					    NULL);
		}
	} else {
		DEBUG(0, ("ERROR: Unsupported socket!\n"));
	}

done:
	talloc_free(tmp_ctx);
}

/*
 * MAIN
 */

static void child_ping(struct messaging_context *msg_ctx,
			void *private_data,
			uint32_t msg_type,
			struct server_id server_id,
			DATA_BLOB *data)
{
	struct tevent_context *ev_ctx;

	ev_ctx = talloc_get_type_abort(private_data, struct tevent_context);

	DEBUG(10, ("Got message that a child changed status.\n"));
	pfh_manage_pool(ev_ctx, msg_ctx, &pf_lsasd_cfg, lsasd_pool);
}

static bool lsasd_schedule_check(struct tevent_context *ev_ctx,
				 struct messaging_context *msg_ctx,
				 struct timeval current_time);

static void lsasd_check_children(struct tevent_context *ev_ctx,
				    struct tevent_timer *te,
				    struct timeval current_time,
				    void *pvt);

static void lsasd_sigchld_handler(struct tevent_context *ev_ctx,
				  struct prefork_pool *pfp,
				  void *pvt)
{
	struct messaging_context *msg_ctx;

	msg_ctx = talloc_get_type_abort(pvt, struct messaging_context);

	/* run pool management so we can fork/retire or increase
	 * the allowed connections per child based on load */
	pfh_manage_pool(ev_ctx, msg_ctx, &pf_lsasd_cfg, lsasd_pool);
}

static bool lsasd_setup_children_monitor(struct tevent_context *ev_ctx,
					 struct messaging_context *msg_ctx)
{
	bool ok;

	/* add our oun sigchld callback */
	prefork_set_sigchld_callback(lsasd_pool, lsasd_sigchld_handler, msg_ctx);

	ok = lsasd_schedule_check(ev_ctx, msg_ctx, tevent_timeval_current());

	return ok;
}

static bool lsasd_schedule_check(struct tevent_context *ev_ctx,
				 struct messaging_context *msg_ctx,
				 struct timeval current_time)
{
	struct tevent_timer *te;
	struct timeval next_event;

	/* check situation again in 10 seconds */
	next_event = tevent_timeval_current_ofs(10, 0);

	/* TODO: check when the socket becomes readable, so that children
	 * are checked only when there is some activity ? */
	te = tevent_add_timer(ev_ctx, lsasd_pool, next_event,
			      lsasd_check_children, msg_ctx);
	if (!te) {
		DEBUG(2, ("Failed to set up children monitoring!\n"));
		return false;
	}

	return true;
}

static void lsasd_check_children(struct tevent_context *ev_ctx,
				 struct tevent_timer *te,
				 struct timeval current_time,
				 void *pvt)
{
	struct messaging_context *msg_ctx;

	msg_ctx = talloc_get_type_abort(pvt, struct messaging_context);

	pfh_manage_pool(ev_ctx, msg_ctx, &pf_lsasd_cfg, lsasd_pool);

	lsasd_schedule_check(ev_ctx, msg_ctx, current_time);
}

/*
 * start it up
 */

static bool lsasd_create_sockets(struct tevent_context *ev_ctx,
				 struct messaging_context *msg_ctx,
				 int *listen_fd,
				 int *listen_fd_size)
{
	struct dcerpc_binding_vector *v, *v_orig;
	TALLOC_CTX *tmp_ctx;
	NTSTATUS status;
	uint32_t i;
	int fd;
	int rc;
	bool ok = true;

	tmp_ctx = talloc_stackframe();
	if (tmp_ctx == NULL) {
		return false;
	}

	status = dcerpc_binding_vector_new(tmp_ctx, &v_orig);
	if (!NT_STATUS_IS_OK(status)) {
		ok = false;
		goto done;
	}

	/* Create only one tcpip listener for all services */
	status = rpc_create_tcpip_sockets(&ndr_table_lsarpc,
					  v_orig,
					  0,
					  listen_fd,
					  listen_fd_size);
	if (!NT_STATUS_IS_OK(status)) {
		ok = false;
		goto done;
	}

	/* Start to listen on tcpip sockets */
	for (i = 0; i < *listen_fd_size; i++) {
		rc = listen(listen_fd[i], pf_lsasd_cfg.max_allowed_clients);
		if (rc == -1) {
			DEBUG(0, ("Failed to listen on tcpip socket - %s\n",
				  strerror(errno)));
			ok = false;
			goto done;
		}
	}

	/* LSARPC */
	fd = create_named_pipe_socket("lsarpc");
	if (fd < 0) {
		ok = false;
		goto done;
	}
	listen_fd[*listen_fd_size] = fd;
	(*listen_fd_size)++;

	rc = listen(fd, pf_lsasd_cfg.max_allowed_clients);
	if (rc == -1) {
		DEBUG(0, ("Failed to listen on lsarpc pipe - %s\n",
			  strerror(errno)));
		ok = false;
		goto done;
	}

	fd = create_named_pipe_socket("lsass");
	if (fd < 0) {
		ok = false;
		goto done;
	}
	listen_fd[*listen_fd_size] = fd;
	(*listen_fd_size)++;

	rc = listen(fd, pf_lsasd_cfg.max_allowed_clients);
	if (rc == -1) {
		DEBUG(0, ("Failed to listen on lsass pipe - %s\n",
			  strerror(errno)));
		ok = false;
		goto done;
	}

	fd = create_dcerpc_ncalrpc_socket("lsarpc");
	if (fd < 0) {
		ok = false;
		goto done;
	}
	listen_fd[*listen_fd_size] = fd;
	(*listen_fd_size)++;

	rc = listen(fd, pf_lsasd_cfg.max_allowed_clients);
	if (rc == -1) {
		DEBUG(0, ("Failed to listen on lsarpc ncalrpc - %s\n",
			  strerror(errno)));
		ok = false;
		goto done;
	}

	v = dcerpc_binding_vector_dup(tmp_ctx, v_orig);
	if (v == NULL) {
		ok = false;
		goto done;
	}

	status = dcerpc_binding_vector_replace_iface(&ndr_table_lsarpc, v);
	if (!NT_STATUS_IS_OK(status)) {
		return false;
	}

	status = dcerpc_binding_vector_add_np_default(&ndr_table_lsarpc, v);
	if (!NT_STATUS_IS_OK(status)) {
		ok = false;
		goto done;
	}

	status = dcerpc_binding_vector_add_unix(&ndr_table_lsarpc, v, "lsarpc");
	if (!NT_STATUS_IS_OK(status)) {
		ok = false;
		goto done;
	}

	status = rpc_ep_register(ev_ctx, msg_ctx, &ndr_table_lsarpc, v);
	if (!NT_STATUS_IS_OK(status)) {
		ok = false;
		goto done;
	}

	/* SAMR */
	fd = create_named_pipe_socket("samr");
	if (fd < 0) {
		ok = false;
		goto done;
	}

	rc = listen(fd, pf_lsasd_cfg.max_allowed_clients);
	if (rc == -1) {
		DEBUG(0, ("Failed to listen on samr pipe - %s\n",
			  strerror(errno)));
		ok = false;
		goto done;
	}
	listen_fd[*listen_fd_size] = fd;
	(*listen_fd_size)++;

	fd = create_dcerpc_ncalrpc_socket("samr");
	if (fd < 0) {
		ok = false;
		goto done;
	}
	listen_fd[*listen_fd_size] = fd;
	(*listen_fd_size)++;

	rc = listen(fd, pf_lsasd_cfg.max_allowed_clients);
	if (rc == -1) {
		DEBUG(0, ("Failed to listen on samr ncalrpc - %s\n",
			  strerror(errno)));
		ok = false;
		goto done;
	}

	v = dcerpc_binding_vector_dup(tmp_ctx, v_orig);
	if (v == NULL) {
		ok = false;
		goto done;
	}

	status = dcerpc_binding_vector_replace_iface(&ndr_table_samr, v);
	if (!NT_STATUS_IS_OK(status)) {
		return false;
	}

	status = dcerpc_binding_vector_add_np_default(&ndr_table_samr, v);
	if (!NT_STATUS_IS_OK(status)) {
		ok = false;
		goto done;
	}

	status = dcerpc_binding_vector_add_unix(&ndr_table_lsarpc, v, "samr");
	if (!NT_STATUS_IS_OK(status)) {
		ok = false;
		goto done;
	}

	status = rpc_ep_register(ev_ctx, msg_ctx, &ndr_table_samr, v);
	if (!NT_STATUS_IS_OK(status)) {
		ok = false;
		goto done;
	}

	/* NETLOGON */
	fd = create_named_pipe_socket("netlogon");
	if (fd < 0) {
		ok = false;
		goto done;
	}

	rc = listen(fd, pf_lsasd_cfg.max_allowed_clients);
	if (rc == -1) {
		DEBUG(0, ("Failed to listen on samr pipe - %s\n",
			  strerror(errno)));
		ok = false;
		goto done;
	}
	listen_fd[*listen_fd_size] = fd;
	(*listen_fd_size)++;

	fd = create_dcerpc_ncalrpc_socket("netlogon");
	if (fd < 0) {
		ok = false;
		goto done;
	}
	listen_fd[*listen_fd_size] = fd;
	(*listen_fd_size)++;

	rc = listen(fd, pf_lsasd_cfg.max_allowed_clients);
	if (rc == -1) {
		DEBUG(0, ("Failed to listen on netlogon ncalrpc - %s\n",
			  strerror(errno)));
		ok = false;
		goto done;
	}

	v = dcerpc_binding_vector_dup(tmp_ctx, v_orig);
	if (v == NULL) {
		ok = false;
		goto done;
	}

	status = dcerpc_binding_vector_replace_iface(&ndr_table_netlogon, v);
	if (!NT_STATUS_IS_OK(status)) {
		return false;
	}

	status = dcerpc_binding_vector_add_np_default(&ndr_table_netlogon, v);
	if (!NT_STATUS_IS_OK(status)) {
		ok = false;
		goto done;
	}

	status = dcerpc_binding_vector_add_unix(&ndr_table_lsarpc, v, "netlogon");
	if (!NT_STATUS_IS_OK(status)) {
		ok = false;
		goto done;
	}

	status = rpc_ep_register(ev_ctx, msg_ctx, &ndr_table_netlogon, v);
	if (!NT_STATUS_IS_OK(status)) {
		ok = false;
		goto done;
	}

done:
	talloc_free(tmp_ctx);
	return ok;
}

void start_lsasd(struct tevent_context *ev_ctx,
		 struct messaging_context *msg_ctx)
{
	NTSTATUS status;
	int listen_fd[LSASD_MAX_SOCKETS];
	int listen_fd_size = 0;
	pid_t pid;
	int rc;
	bool ok;

	DEBUG(1, ("Forking LSA Service Daemon\n"));

	/*
	 * Block signals before forking child as it will have to
	 * set its own handlers. Child will re-enable SIGHUP as
	 * soon as the handlers are set up.
	 */
	BlockSignals(true, SIGTERM);
	BlockSignals(true, SIGHUP);

	pid = sys_fork();
	if (pid == -1) {
		DEBUG(0, ("Failed to fork LSASD [%s], aborting ...\n",
			   strerror(errno)));
		exit(1);
	}

	/* parent or error */
	if (pid != 0) {

		/* Re-enable SIGHUP before returnig */
		BlockSignals(false, SIGTERM);
		BlockSignals(false, SIGHUP);

		return;
	}

	/* save the parent process id so the children can use it later */
	parent_id = procid_self();

	status = reinit_after_fork(msg_ctx,
				   ev_ctx,
				   true);
	if (!NT_STATUS_IS_OK(status)) {
		DEBUG(0,("reinit_after_fork() failed\n"));
		smb_panic("reinit_after_fork() failed");
	}

	lsasd_reopen_logs(0);
	pfh_daemon_config(DAEMON_NAME,
			  &pf_lsasd_cfg,
			  &default_pf_lsasd_cfg);

	lsasd_setup_sig_term_handler(ev_ctx);
	lsasd_setup_sig_hup_handler(ev_ctx);

	BlockSignals(false, SIGTERM);
	BlockSignals(false, SIGHUP);

	ok = lsasd_create_sockets(ev_ctx, msg_ctx, listen_fd, &listen_fd_size);
	if (!ok) {
		exit(1);
	}

	/* start children before any more initialization is done */
	ok = prefork_create_pool(ev_ctx, /* mem_ctx */
				 ev_ctx,
				 msg_ctx,
				 listen_fd_size,
				 listen_fd,
				 pf_lsasd_cfg.min_children,
				 pf_lsasd_cfg.max_children,
				 &lsasd_children_main,
				 NULL,
				 &lsasd_pool);
	if (!ok) {
		exit(1);
	}

	if (!serverid_register(procid_self(), FLAG_MSG_GENERAL)) {
		exit(1);
	}

	messaging_register(msg_ctx,
			   ev_ctx,
			   MSG_SMB_CONF_UPDATED,
			   lsasd_smb_conf_updated);
	messaging_register(msg_ctx, ev_ctx,
			   MSG_PREFORK_CHILD_EVENT, child_ping);

	status = rpc_lsarpc_init(NULL);
	if (!NT_STATUS_IS_OK(status)) {
		DEBUG(0, ("Failed to register lsarpc rpc inteface in lsasd! (%s)\n",
			  nt_errstr(status)));
		exit(1);
	}

	status = rpc_samr_init(NULL);
	if (!NT_STATUS_IS_OK(status)) {
		DEBUG(0, ("Failed to register samr rpc inteface in lsasd! (%s)\n",
			  nt_errstr(status)));
		exit(1);
	}

	status = rpc_netlogon_init(NULL);
	if (!NT_STATUS_IS_OK(status)) {
		DEBUG(0, ("Failed to register netlogon rpc inteface in lsasd! (%s)\n",
			  nt_errstr(status)));
		exit(1);
	}

	ok = lsasd_setup_children_monitor(ev_ctx, msg_ctx);
	if (!ok) {
		DEBUG(0, ("Failed to setup children monitoring!\n"));
		exit(1);
	}

	DEBUG(1, ("LSASD Daemon Started (%d)\n", getpid()));

	/* loop forever */
	rc = tevent_loop_wait(ev_ctx);

	/* should not be reached */
	DEBUG(0,("lsasd: tevent_loop_wait() exited with %d - %s\n",
		 rc, (rc == 0) ? "out of events" : strerror(errno)));
	exit(1);
}