1 | /*
|
---|
2 | * Unix SMB/CIFS implementation.
|
---|
3 | *
|
---|
4 | * LSA service daemon
|
---|
5 | *
|
---|
6 | * Copyright (c) 2011 Andreas Schneider <asn@samba.org>
|
---|
7 | *
|
---|
8 | * This program is free software; you can redistribute it and/or modify
|
---|
9 | * it under the terms of the GNU General Public License as published by
|
---|
10 | * the Free Software Foundation; either version 3 of the License, or
|
---|
11 | * (at your option) any later version.
|
---|
12 | *
|
---|
13 | * This program is distributed in the hope that it will be useful,
|
---|
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
16 | * GNU General Public License for more details.
|
---|
17 | *
|
---|
18 | * You should have received a copy of the GNU General Public License
|
---|
19 | * along with this program; if not, see <http://www.gnu.org/licenses/>.
|
---|
20 | */
|
---|
21 |
|
---|
22 | #include "includes.h"
|
---|
23 | #include "serverid.h"
|
---|
24 | #include "messages.h"
|
---|
25 | #include "ntdomain.h"
|
---|
26 |
|
---|
27 | #include "lib/id_cache.h"
|
---|
28 |
|
---|
29 | #include "../lib/tsocket/tsocket.h"
|
---|
30 | #include "lib/server_prefork.h"
|
---|
31 | #include "lib/server_prefork_util.h"
|
---|
32 | #include "librpc/rpc/dcerpc_ep.h"
|
---|
33 |
|
---|
34 | #include "rpc_server/rpc_server.h"
|
---|
35 | #include "rpc_server/rpc_ep_register.h"
|
---|
36 | #include "rpc_server/rpc_sock_helper.h"
|
---|
37 |
|
---|
38 | #include "librpc/gen_ndr/srv_lsa.h"
|
---|
39 | #include "librpc/gen_ndr/srv_samr.h"
|
---|
40 | #include "librpc/gen_ndr/srv_netlogon.h"
|
---|
41 |
|
---|
42 | #define DAEMON_NAME "lsasd"
|
---|
43 | #define LSASD_MAX_SOCKETS 64
|
---|
44 |
|
---|
45 | static struct server_id parent_id;
|
---|
46 | static struct prefork_pool *lsasd_pool = NULL;
|
---|
47 | static int lsasd_child_id = 0;
|
---|
48 |
|
---|
49 | static struct pf_daemon_config default_pf_lsasd_cfg = {
|
---|
50 | .prefork_status = PFH_INIT,
|
---|
51 | .min_children = 5,
|
---|
52 | .max_children = 25,
|
---|
53 | .spawn_rate = 5,
|
---|
54 | .max_allowed_clients = 100,
|
---|
55 | .child_min_life = 60 /* 1 minute minimum life time */
|
---|
56 | };
|
---|
57 | static struct pf_daemon_config pf_lsasd_cfg = { 0 };
|
---|
58 |
|
---|
59 | void start_lsasd(struct tevent_context *ev_ctx,
|
---|
60 | struct messaging_context *msg_ctx);
|
---|
61 |
|
---|
62 | static void lsasd_reopen_logs(int child_id)
|
---|
63 | {
|
---|
64 | char *lfile = lp_logfile(talloc_tos());
|
---|
65 | char *extension;
|
---|
66 | int rc;
|
---|
67 |
|
---|
68 | if (child_id) {
|
---|
69 | rc = asprintf(&extension, "%s.%d", DAEMON_NAME, child_id);
|
---|
70 | } else {
|
---|
71 | rc = asprintf(&extension, "%s", DAEMON_NAME);
|
---|
72 | }
|
---|
73 | if (rc == -1) {
|
---|
74 | return;
|
---|
75 | }
|
---|
76 |
|
---|
77 | rc = 0;
|
---|
78 | if (lfile == NULL || lfile[0] == '\0') {
|
---|
79 | rc = asprintf(&lfile, "%s/log.%s",
|
---|
80 | get_dyn_LOGFILEBASE(), extension);
|
---|
81 | } else {
|
---|
82 | if (strstr(lfile, extension) == NULL) {
|
---|
83 | if (child_id) {
|
---|
84 | rc = asprintf(&lfile, "%s.%d",
|
---|
85 | lp_logfile(talloc_tos()),
|
---|
86 | child_id);
|
---|
87 | } else {
|
---|
88 | rc = asprintf(&lfile, "%s.%s",
|
---|
89 | lp_logfile(talloc_tos()),
|
---|
90 | extension);
|
---|
91 | }
|
---|
92 | }
|
---|
93 | }
|
---|
94 |
|
---|
95 | if (rc > 0) {
|
---|
96 | lp_set_logfile(lfile);
|
---|
97 | SAFE_FREE(lfile);
|
---|
98 | }
|
---|
99 |
|
---|
100 | SAFE_FREE(extension);
|
---|
101 |
|
---|
102 | reopen_logs();
|
---|
103 | }
|
---|
104 |
|
---|
105 | static void lsasd_smb_conf_updated(struct messaging_context *msg,
|
---|
106 | void *private_data,
|
---|
107 | uint32_t msg_type,
|
---|
108 | struct server_id server_id,
|
---|
109 | DATA_BLOB *data)
|
---|
110 | {
|
---|
111 | struct tevent_context *ev_ctx;
|
---|
112 |
|
---|
113 | DEBUG(10, ("Got message saying smb.conf was updated. Reloading.\n"));
|
---|
114 | ev_ctx = talloc_get_type_abort(private_data, struct tevent_context);
|
---|
115 |
|
---|
116 | change_to_root_user();
|
---|
117 | lp_load_global(get_dyn_CONFIGFILE());
|
---|
118 |
|
---|
119 | lsasd_reopen_logs(lsasd_child_id);
|
---|
120 | if (lsasd_child_id == 0) {
|
---|
121 | pfh_daemon_config(DAEMON_NAME,
|
---|
122 | &pf_lsasd_cfg,
|
---|
123 | &default_pf_lsasd_cfg);
|
---|
124 | pfh_manage_pool(ev_ctx, msg, &pf_lsasd_cfg, lsasd_pool);
|
---|
125 | }
|
---|
126 | }
|
---|
127 |
|
---|
128 | static void lsasd_sig_term_handler(struct tevent_context *ev,
|
---|
129 | struct tevent_signal *se,
|
---|
130 | int signum,
|
---|
131 | int count,
|
---|
132 | void *siginfo,
|
---|
133 | void *private_data)
|
---|
134 | {
|
---|
135 | rpc_netlogon_shutdown();
|
---|
136 | rpc_samr_shutdown();
|
---|
137 | rpc_lsarpc_shutdown();
|
---|
138 |
|
---|
139 | DEBUG(0, ("termination signal\n"));
|
---|
140 | exit(0);
|
---|
141 | }
|
---|
142 |
|
---|
143 | static void lsasd_setup_sig_term_handler(struct tevent_context *ev_ctx)
|
---|
144 | {
|
---|
145 | struct tevent_signal *se;
|
---|
146 |
|
---|
147 | se = tevent_add_signal(ev_ctx,
|
---|
148 | ev_ctx,
|
---|
149 | SIGTERM, 0,
|
---|
150 | lsasd_sig_term_handler,
|
---|
151 | NULL);
|
---|
152 | if (!se) {
|
---|
153 | DEBUG(0, ("failed to setup SIGTERM handler\n"));
|
---|
154 | exit(1);
|
---|
155 | }
|
---|
156 | }
|
---|
157 |
|
---|
158 | static void lsasd_sig_hup_handler(struct tevent_context *ev,
|
---|
159 | struct tevent_signal *se,
|
---|
160 | int signum,
|
---|
161 | int count,
|
---|
162 | void *siginfo,
|
---|
163 | void *pvt)
|
---|
164 | {
|
---|
165 |
|
---|
166 | change_to_root_user();
|
---|
167 | lp_load_global(get_dyn_CONFIGFILE());
|
---|
168 |
|
---|
169 | lsasd_reopen_logs(lsasd_child_id);
|
---|
170 | pfh_daemon_config(DAEMON_NAME,
|
---|
171 | &pf_lsasd_cfg,
|
---|
172 | &default_pf_lsasd_cfg);
|
---|
173 |
|
---|
174 | /* relay to all children */
|
---|
175 | prefork_send_signal_to_all(lsasd_pool, SIGHUP);
|
---|
176 | }
|
---|
177 |
|
---|
178 | static void lsasd_setup_sig_hup_handler(struct tevent_context *ev_ctx)
|
---|
179 | {
|
---|
180 | struct tevent_signal *se;
|
---|
181 |
|
---|
182 | se = tevent_add_signal(ev_ctx,
|
---|
183 | ev_ctx,
|
---|
184 | SIGHUP, 0,
|
---|
185 | lsasd_sig_hup_handler,
|
---|
186 | NULL);
|
---|
187 | if (!se) {
|
---|
188 | DEBUG(0, ("failed to setup SIGHUP handler\n"));
|
---|
189 | exit(1);
|
---|
190 | }
|
---|
191 | }
|
---|
192 |
|
---|
193 | /**********************************************************
|
---|
194 | * Children
|
---|
195 | **********************************************************/
|
---|
196 |
|
---|
197 | static void lsasd_chld_sig_hup_handler(struct tevent_context *ev,
|
---|
198 | struct tevent_signal *se,
|
---|
199 | int signum,
|
---|
200 | int count,
|
---|
201 | void *siginfo,
|
---|
202 | void *pvt)
|
---|
203 | {
|
---|
204 | change_to_root_user();
|
---|
205 | lsasd_reopen_logs(lsasd_child_id);
|
---|
206 | }
|
---|
207 |
|
---|
208 | static bool lsasd_setup_chld_hup_handler(struct tevent_context *ev_ctx)
|
---|
209 | {
|
---|
210 | struct tevent_signal *se;
|
---|
211 |
|
---|
212 | se = tevent_add_signal(ev_ctx,
|
---|
213 | ev_ctx,
|
---|
214 | SIGHUP, 0,
|
---|
215 | lsasd_chld_sig_hup_handler,
|
---|
216 | NULL);
|
---|
217 | if (!se) {
|
---|
218 | DEBUG(1, ("failed to setup SIGHUP handler"));
|
---|
219 | return false;
|
---|
220 | }
|
---|
221 |
|
---|
222 | return true;
|
---|
223 | }
|
---|
224 |
|
---|
225 | static void parent_ping(struct messaging_context *msg_ctx,
|
---|
226 | void *private_data,
|
---|
227 | uint32_t msg_type,
|
---|
228 | struct server_id server_id,
|
---|
229 | DATA_BLOB *data)
|
---|
230 | {
|
---|
231 |
|
---|
232 | /* The fact we received this message is enough to let make the event
|
---|
233 | * loop if it was idle. lsasd_children_main will cycle through
|
---|
234 | * lsasd_next_client at least once. That function will take whatever
|
---|
235 | * action is necessary */
|
---|
236 |
|
---|
237 | DEBUG(10, ("Got message that the parent changed status.\n"));
|
---|
238 | return;
|
---|
239 | }
|
---|
240 |
|
---|
241 | static bool lsasd_child_init(struct tevent_context *ev_ctx,
|
---|
242 | int child_id,
|
---|
243 | struct pf_worker_data *pf)
|
---|
244 | {
|
---|
245 | NTSTATUS status;
|
---|
246 | struct messaging_context *msg_ctx = server_messaging_context();
|
---|
247 | bool ok;
|
---|
248 |
|
---|
249 | status = reinit_after_fork(msg_ctx, ev_ctx,
|
---|
250 | true, "lsasd-child");
|
---|
251 | if (!NT_STATUS_IS_OK(status)) {
|
---|
252 | DEBUG(0,("reinit_after_fork() failed\n"));
|
---|
253 | smb_panic("reinit_after_fork() failed");
|
---|
254 | }
|
---|
255 |
|
---|
256 | lsasd_child_id = child_id;
|
---|
257 | lsasd_reopen_logs(child_id);
|
---|
258 |
|
---|
259 | ok = lsasd_setup_chld_hup_handler(ev_ctx);
|
---|
260 | if (!ok) {
|
---|
261 | return false;
|
---|
262 | }
|
---|
263 |
|
---|
264 | if (!serverid_register(messaging_server_id(msg_ctx),
|
---|
265 | FLAG_MSG_GENERAL)) {
|
---|
266 | return false;
|
---|
267 | }
|
---|
268 |
|
---|
269 | messaging_register(msg_ctx, ev_ctx,
|
---|
270 | MSG_SMB_CONF_UPDATED, lsasd_smb_conf_updated);
|
---|
271 | messaging_register(msg_ctx, ev_ctx,
|
---|
272 | MSG_PREFORK_PARENT_EVENT, parent_ping);
|
---|
273 | id_cache_register_msgs(msg_ctx);
|
---|
274 |
|
---|
275 | status = rpc_lsarpc_init(NULL);
|
---|
276 | if (!NT_STATUS_IS_OK(status)) {
|
---|
277 | DEBUG(0, ("Failed to register lsarpc rpc interface! (%s)\n",
|
---|
278 | nt_errstr(status)));
|
---|
279 | return false;
|
---|
280 | }
|
---|
281 |
|
---|
282 | status = rpc_samr_init(NULL);
|
---|
283 | if (!NT_STATUS_IS_OK(status)) {
|
---|
284 | DEBUG(0, ("Failed to register samr rpc interface! (%s)\n",
|
---|
285 | nt_errstr(status)));
|
---|
286 | return false;
|
---|
287 | }
|
---|
288 |
|
---|
289 | status = rpc_netlogon_init(NULL);
|
---|
290 | if (!NT_STATUS_IS_OK(status)) {
|
---|
291 | DEBUG(0, ("Failed to register netlogon rpc interface! (%s)\n",
|
---|
292 | nt_errstr(status)));
|
---|
293 | return false;
|
---|
294 | }
|
---|
295 |
|
---|
296 | return true;
|
---|
297 | }
|
---|
298 |
|
---|
299 | struct lsasd_children_data {
|
---|
300 | struct tevent_context *ev_ctx;
|
---|
301 | struct messaging_context *msg_ctx;
|
---|
302 | struct pf_worker_data *pf;
|
---|
303 | int listen_fd_size;
|
---|
304 | int *listen_fds;
|
---|
305 | };
|
---|
306 |
|
---|
307 | static void lsasd_next_client(void *pvt);
|
---|
308 |
|
---|
309 | static int lsasd_children_main(struct tevent_context *ev_ctx,
|
---|
310 | struct messaging_context *msg_ctx,
|
---|
311 | struct pf_worker_data *pf,
|
---|
312 | int child_id,
|
---|
313 | int listen_fd_size,
|
---|
314 | int *listen_fds,
|
---|
315 | void *private_data)
|
---|
316 | {
|
---|
317 | struct lsasd_children_data *data;
|
---|
318 | bool ok;
|
---|
319 | int ret = 0;
|
---|
320 |
|
---|
321 | ok = lsasd_child_init(ev_ctx, child_id, pf);
|
---|
322 | if (!ok) {
|
---|
323 | return 1;
|
---|
324 | }
|
---|
325 |
|
---|
326 | data = talloc(ev_ctx, struct lsasd_children_data);
|
---|
327 | if (!data) {
|
---|
328 | return 1;
|
---|
329 | }
|
---|
330 | data->pf = pf;
|
---|
331 | data->ev_ctx = ev_ctx;
|
---|
332 | data->msg_ctx = msg_ctx;
|
---|
333 | data->listen_fd_size = listen_fd_size;
|
---|
334 | data->listen_fds = listen_fds;
|
---|
335 |
|
---|
336 | /* loop until it is time to exit */
|
---|
337 | while (pf->status != PF_WORKER_EXITING) {
|
---|
338 | /* try to see if it is time to schedule the next client */
|
---|
339 | lsasd_next_client(data);
|
---|
340 |
|
---|
341 | ret = tevent_loop_once(ev_ctx);
|
---|
342 | if (ret != 0) {
|
---|
343 | DEBUG(0, ("tevent_loop_once() exited with %d: %s\n",
|
---|
344 | ret, strerror(errno)));
|
---|
345 | pf->status = PF_WORKER_EXITING;
|
---|
346 | }
|
---|
347 | }
|
---|
348 |
|
---|
349 | return ret;
|
---|
350 | }
|
---|
351 |
|
---|
352 | static void lsasd_client_terminated(void *pvt)
|
---|
353 | {
|
---|
354 | struct lsasd_children_data *data;
|
---|
355 |
|
---|
356 | data = talloc_get_type_abort(pvt, struct lsasd_children_data);
|
---|
357 |
|
---|
358 | pfh_client_terminated(data->pf);
|
---|
359 |
|
---|
360 | lsasd_next_client(pvt);
|
---|
361 | }
|
---|
362 |
|
---|
363 | struct lsasd_new_client {
|
---|
364 | struct lsasd_children_data *data;
|
---|
365 | };
|
---|
366 |
|
---|
367 | static void lsasd_handle_client(struct tevent_req *req);
|
---|
368 |
|
---|
369 | static void lsasd_next_client(void *pvt)
|
---|
370 | {
|
---|
371 | struct tevent_req *req;
|
---|
372 | struct lsasd_children_data *data;
|
---|
373 | struct lsasd_new_client *next;
|
---|
374 |
|
---|
375 | data = talloc_get_type_abort(pvt, struct lsasd_children_data);
|
---|
376 |
|
---|
377 | if (!pfh_child_allowed_to_accept(data->pf)) {
|
---|
378 | /* nothing to do for now we are already listening
|
---|
379 | * or we are not allowed to listen further */
|
---|
380 | return;
|
---|
381 | }
|
---|
382 |
|
---|
383 | next = talloc_zero(data, struct lsasd_new_client);
|
---|
384 | if (!next) {
|
---|
385 | DEBUG(1, ("Out of memory!?\n"));
|
---|
386 | return;
|
---|
387 | }
|
---|
388 | next->data = data;
|
---|
389 |
|
---|
390 | req = prefork_listen_send(next,
|
---|
391 | data->ev_ctx,
|
---|
392 | data->pf,
|
---|
393 | data->listen_fd_size,
|
---|
394 | data->listen_fds);
|
---|
395 | if (!req) {
|
---|
396 | DEBUG(1, ("Failed to make listening request!?\n"));
|
---|
397 | talloc_free(next);
|
---|
398 | return;
|
---|
399 | }
|
---|
400 | tevent_req_set_callback(req, lsasd_handle_client, next);
|
---|
401 | }
|
---|
402 |
|
---|
403 | static void lsasd_handle_client(struct tevent_req *req)
|
---|
404 | {
|
---|
405 | struct lsasd_children_data *data;
|
---|
406 | struct lsasd_new_client *client;
|
---|
407 | const DATA_BLOB ping = data_blob_null;
|
---|
408 | int rc;
|
---|
409 | int sd;
|
---|
410 | TALLOC_CTX *tmp_ctx;
|
---|
411 | struct tsocket_address *srv_addr;
|
---|
412 | struct tsocket_address *cli_addr;
|
---|
413 |
|
---|
414 | client = tevent_req_callback_data(req, struct lsasd_new_client);
|
---|
415 | data = client->data;
|
---|
416 |
|
---|
417 | tmp_ctx = talloc_stackframe();
|
---|
418 | if (tmp_ctx == NULL) {
|
---|
419 | DEBUG(1, ("Failed to allocate stackframe!\n"));
|
---|
420 | return;
|
---|
421 | }
|
---|
422 |
|
---|
423 | rc = prefork_listen_recv(req,
|
---|
424 | tmp_ctx,
|
---|
425 | &sd,
|
---|
426 | &srv_addr,
|
---|
427 | &cli_addr);
|
---|
428 |
|
---|
429 | /* this will free the request too */
|
---|
430 | talloc_free(client);
|
---|
431 |
|
---|
432 | if (rc != 0) {
|
---|
433 | DEBUG(6, ("No client connection was available after all!\n"));
|
---|
434 | goto done;
|
---|
435 | }
|
---|
436 |
|
---|
437 | /* Warn parent that our status changed */
|
---|
438 | messaging_send(data->msg_ctx, parent_id,
|
---|
439 | MSG_PREFORK_CHILD_EVENT, &ping);
|
---|
440 |
|
---|
441 | DEBUG(2, ("LSASD preforked child %d got client connection!\n",
|
---|
442 | (int)(data->pf->pid)));
|
---|
443 |
|
---|
444 | if (tsocket_address_is_inet(srv_addr, "ip")) {
|
---|
445 | DEBUG(3, ("Got a tcpip client connection from %s on interface %s\n",
|
---|
446 | tsocket_address_string(cli_addr, tmp_ctx),
|
---|
447 | tsocket_address_string(srv_addr, tmp_ctx)));
|
---|
448 |
|
---|
449 | dcerpc_ncacn_accept(data->ev_ctx,
|
---|
450 | data->msg_ctx,
|
---|
451 | NCACN_IP_TCP,
|
---|
452 | "IP",
|
---|
453 | cli_addr,
|
---|
454 | srv_addr,
|
---|
455 | sd,
|
---|
456 | NULL);
|
---|
457 | } else if (tsocket_address_is_unix(srv_addr)) {
|
---|
458 | const char *p;
|
---|
459 | const char *b;
|
---|
460 |
|
---|
461 | p = tsocket_address_unix_path(srv_addr, tmp_ctx);
|
---|
462 | if (p == NULL) {
|
---|
463 | talloc_free(tmp_ctx);
|
---|
464 | return;
|
---|
465 | }
|
---|
466 |
|
---|
467 | b = strrchr(p, '/');
|
---|
468 | if (b != NULL) {
|
---|
469 | b++;
|
---|
470 | } else {
|
---|
471 | b = p;
|
---|
472 | }
|
---|
473 |
|
---|
474 | if (strstr(p, "/np/")) {
|
---|
475 | named_pipe_accept_function(data->ev_ctx,
|
---|
476 | data->msg_ctx,
|
---|
477 | b,
|
---|
478 | sd,
|
---|
479 | lsasd_client_terminated,
|
---|
480 | data);
|
---|
481 | } else {
|
---|
482 | dcerpc_ncacn_accept(data->ev_ctx,
|
---|
483 | data->msg_ctx,
|
---|
484 | NCALRPC,
|
---|
485 | b,
|
---|
486 | cli_addr,
|
---|
487 | srv_addr,
|
---|
488 | sd,
|
---|
489 | NULL);
|
---|
490 | }
|
---|
491 | } else {
|
---|
492 | DEBUG(0, ("ERROR: Unsupported socket!\n"));
|
---|
493 | }
|
---|
494 |
|
---|
495 | done:
|
---|
496 | talloc_free(tmp_ctx);
|
---|
497 | }
|
---|
498 |
|
---|
499 | /*
|
---|
500 | * MAIN
|
---|
501 | */
|
---|
502 |
|
---|
503 | static void child_ping(struct messaging_context *msg_ctx,
|
---|
504 | void *private_data,
|
---|
505 | uint32_t msg_type,
|
---|
506 | struct server_id server_id,
|
---|
507 | DATA_BLOB *data)
|
---|
508 | {
|
---|
509 | struct tevent_context *ev_ctx;
|
---|
510 |
|
---|
511 | ev_ctx = talloc_get_type_abort(private_data, struct tevent_context);
|
---|
512 |
|
---|
513 | DEBUG(10, ("Got message that a child changed status.\n"));
|
---|
514 | pfh_manage_pool(ev_ctx, msg_ctx, &pf_lsasd_cfg, lsasd_pool);
|
---|
515 | }
|
---|
516 |
|
---|
517 | static bool lsasd_schedule_check(struct tevent_context *ev_ctx,
|
---|
518 | struct messaging_context *msg_ctx,
|
---|
519 | struct timeval current_time);
|
---|
520 |
|
---|
521 | static void lsasd_check_children(struct tevent_context *ev_ctx,
|
---|
522 | struct tevent_timer *te,
|
---|
523 | struct timeval current_time,
|
---|
524 | void *pvt);
|
---|
525 |
|
---|
526 | static void lsasd_sigchld_handler(struct tevent_context *ev_ctx,
|
---|
527 | struct prefork_pool *pfp,
|
---|
528 | void *pvt)
|
---|
529 | {
|
---|
530 | struct messaging_context *msg_ctx;
|
---|
531 |
|
---|
532 | msg_ctx = talloc_get_type_abort(pvt, struct messaging_context);
|
---|
533 |
|
---|
534 | /* run pool management so we can fork/retire or increase
|
---|
535 | * the allowed connections per child based on load */
|
---|
536 | pfh_manage_pool(ev_ctx, msg_ctx, &pf_lsasd_cfg, lsasd_pool);
|
---|
537 | }
|
---|
538 |
|
---|
539 | static bool lsasd_setup_children_monitor(struct tevent_context *ev_ctx,
|
---|
540 | struct messaging_context *msg_ctx)
|
---|
541 | {
|
---|
542 | bool ok;
|
---|
543 |
|
---|
544 | /* add our oun sigchld callback */
|
---|
545 | prefork_set_sigchld_callback(lsasd_pool, lsasd_sigchld_handler, msg_ctx);
|
---|
546 |
|
---|
547 | ok = lsasd_schedule_check(ev_ctx, msg_ctx, tevent_timeval_current());
|
---|
548 |
|
---|
549 | return ok;
|
---|
550 | }
|
---|
551 |
|
---|
552 | static bool lsasd_schedule_check(struct tevent_context *ev_ctx,
|
---|
553 | struct messaging_context *msg_ctx,
|
---|
554 | struct timeval current_time)
|
---|
555 | {
|
---|
556 | struct tevent_timer *te;
|
---|
557 | struct timeval next_event;
|
---|
558 |
|
---|
559 | /* check situation again in 10 seconds */
|
---|
560 | next_event = tevent_timeval_current_ofs(10, 0);
|
---|
561 |
|
---|
562 | /* TODO: check when the socket becomes readable, so that children
|
---|
563 | * are checked only when there is some activity ? */
|
---|
564 | te = tevent_add_timer(ev_ctx, lsasd_pool, next_event,
|
---|
565 | lsasd_check_children, msg_ctx);
|
---|
566 | if (!te) {
|
---|
567 | DEBUG(2, ("Failed to set up children monitoring!\n"));
|
---|
568 | return false;
|
---|
569 | }
|
---|
570 |
|
---|
571 | return true;
|
---|
572 | }
|
---|
573 |
|
---|
574 | static void lsasd_check_children(struct tevent_context *ev_ctx,
|
---|
575 | struct tevent_timer *te,
|
---|
576 | struct timeval current_time,
|
---|
577 | void *pvt)
|
---|
578 | {
|
---|
579 | struct messaging_context *msg_ctx;
|
---|
580 |
|
---|
581 | msg_ctx = talloc_get_type_abort(pvt, struct messaging_context);
|
---|
582 |
|
---|
583 | pfh_manage_pool(ev_ctx, msg_ctx, &pf_lsasd_cfg, lsasd_pool);
|
---|
584 |
|
---|
585 | lsasd_schedule_check(ev_ctx, msg_ctx, current_time);
|
---|
586 | }
|
---|
587 |
|
---|
588 | /*
|
---|
589 | * start it up
|
---|
590 | */
|
---|
591 |
|
---|
592 | static bool lsasd_create_sockets(struct tevent_context *ev_ctx,
|
---|
593 | struct messaging_context *msg_ctx,
|
---|
594 | int *listen_fd,
|
---|
595 | int *listen_fd_size)
|
---|
596 | {
|
---|
597 | struct dcerpc_binding_vector *v, *v_orig;
|
---|
598 | TALLOC_CTX *tmp_ctx;
|
---|
599 | NTSTATUS status;
|
---|
600 | uint32_t i;
|
---|
601 | int fd = -1;
|
---|
602 | int rc;
|
---|
603 | bool ok = false;
|
---|
604 |
|
---|
605 | tmp_ctx = talloc_stackframe();
|
---|
606 | if (tmp_ctx == NULL) {
|
---|
607 | return false;
|
---|
608 | }
|
---|
609 |
|
---|
610 | status = dcerpc_binding_vector_new(tmp_ctx, &v_orig);
|
---|
611 | if (!NT_STATUS_IS_OK(status)) {
|
---|
612 | goto done;
|
---|
613 | }
|
---|
614 |
|
---|
615 | /* Create only one tcpip listener for all services */
|
---|
616 | status = rpc_create_tcpip_sockets(&ndr_table_lsarpc,
|
---|
617 | v_orig,
|
---|
618 | 0,
|
---|
619 | listen_fd,
|
---|
620 | listen_fd_size);
|
---|
621 | if (!NT_STATUS_IS_OK(status)) {
|
---|
622 | goto done;
|
---|
623 | }
|
---|
624 |
|
---|
625 | /* Start to listen on tcpip sockets */
|
---|
626 | for (i = 0; i < *listen_fd_size; i++) {
|
---|
627 | rc = listen(listen_fd[i], pf_lsasd_cfg.max_allowed_clients);
|
---|
628 | if (rc == -1) {
|
---|
629 | DEBUG(0, ("Failed to listen on tcpip socket - %s\n",
|
---|
630 | strerror(errno)));
|
---|
631 | goto done;
|
---|
632 | }
|
---|
633 | }
|
---|
634 |
|
---|
635 | /* LSARPC */
|
---|
636 | fd = create_named_pipe_socket("lsarpc");
|
---|
637 | if (fd < 0) {
|
---|
638 | goto done;
|
---|
639 | }
|
---|
640 |
|
---|
641 | rc = listen(fd, pf_lsasd_cfg.max_allowed_clients);
|
---|
642 | if (rc == -1) {
|
---|
643 | DEBUG(0, ("Failed to listen on lsarpc pipe - %s\n",
|
---|
644 | strerror(errno)));
|
---|
645 | goto done;
|
---|
646 | }
|
---|
647 | listen_fd[*listen_fd_size] = fd;
|
---|
648 | (*listen_fd_size)++;
|
---|
649 |
|
---|
650 | fd = create_named_pipe_socket("lsass");
|
---|
651 | if (fd < 0) {
|
---|
652 | goto done;
|
---|
653 | }
|
---|
654 |
|
---|
655 | rc = listen(fd, pf_lsasd_cfg.max_allowed_clients);
|
---|
656 | if (rc == -1) {
|
---|
657 | DEBUG(0, ("Failed to listen on lsass pipe - %s\n",
|
---|
658 | strerror(errno)));
|
---|
659 | goto done;
|
---|
660 | }
|
---|
661 | listen_fd[*listen_fd_size] = fd;
|
---|
662 | (*listen_fd_size)++;
|
---|
663 |
|
---|
664 | fd = create_dcerpc_ncalrpc_socket("lsarpc");
|
---|
665 | if (fd < 0) {
|
---|
666 | goto done;
|
---|
667 | }
|
---|
668 |
|
---|
669 | rc = listen(fd, pf_lsasd_cfg.max_allowed_clients);
|
---|
670 | if (rc == -1) {
|
---|
671 | DEBUG(0, ("Failed to listen on lsarpc ncalrpc - %s\n",
|
---|
672 | strerror(errno)));
|
---|
673 | goto done;
|
---|
674 | }
|
---|
675 | listen_fd[*listen_fd_size] = fd;
|
---|
676 | (*listen_fd_size)++;
|
---|
677 | fd = -1;
|
---|
678 |
|
---|
679 | v = dcerpc_binding_vector_dup(tmp_ctx, v_orig);
|
---|
680 | if (v == NULL) {
|
---|
681 | goto done;
|
---|
682 | }
|
---|
683 |
|
---|
684 | status = dcerpc_binding_vector_replace_iface(&ndr_table_lsarpc, v);
|
---|
685 | if (!NT_STATUS_IS_OK(status)) {
|
---|
686 | goto done;
|
---|
687 | }
|
---|
688 |
|
---|
689 | status = dcerpc_binding_vector_add_np_default(&ndr_table_lsarpc, v);
|
---|
690 | if (!NT_STATUS_IS_OK(status)) {
|
---|
691 | goto done;
|
---|
692 | }
|
---|
693 |
|
---|
694 | status = dcerpc_binding_vector_add_unix(&ndr_table_lsarpc, v, "lsarpc");
|
---|
695 | if (!NT_STATUS_IS_OK(status)) {
|
---|
696 | goto done;
|
---|
697 | }
|
---|
698 |
|
---|
699 | status = rpc_ep_register(ev_ctx, msg_ctx, &ndr_table_lsarpc, v);
|
---|
700 | if (!NT_STATUS_IS_OK(status)) {
|
---|
701 | goto done;
|
---|
702 | }
|
---|
703 |
|
---|
704 | /* SAMR */
|
---|
705 | fd = create_named_pipe_socket("samr");
|
---|
706 | if (fd < 0) {
|
---|
707 | goto done;
|
---|
708 | }
|
---|
709 |
|
---|
710 | rc = listen(fd, pf_lsasd_cfg.max_allowed_clients);
|
---|
711 | if (rc == -1) {
|
---|
712 | DEBUG(0, ("Failed to listen on samr pipe - %s\n",
|
---|
713 | strerror(errno)));
|
---|
714 | goto done;
|
---|
715 | }
|
---|
716 | listen_fd[*listen_fd_size] = fd;
|
---|
717 | (*listen_fd_size)++;
|
---|
718 |
|
---|
719 | fd = create_dcerpc_ncalrpc_socket("samr");
|
---|
720 | if (fd < 0) {
|
---|
721 | goto done;
|
---|
722 | }
|
---|
723 |
|
---|
724 | rc = listen(fd, pf_lsasd_cfg.max_allowed_clients);
|
---|
725 | if (rc == -1) {
|
---|
726 | DEBUG(0, ("Failed to listen on samr ncalrpc - %s\n",
|
---|
727 | strerror(errno)));
|
---|
728 | goto done;
|
---|
729 | }
|
---|
730 | listen_fd[*listen_fd_size] = fd;
|
---|
731 | (*listen_fd_size)++;
|
---|
732 | fd = -1;
|
---|
733 |
|
---|
734 | v = dcerpc_binding_vector_dup(tmp_ctx, v_orig);
|
---|
735 | if (v == NULL) {
|
---|
736 | goto done;
|
---|
737 | }
|
---|
738 |
|
---|
739 | status = dcerpc_binding_vector_replace_iface(&ndr_table_samr, v);
|
---|
740 | if (!NT_STATUS_IS_OK(status)) {
|
---|
741 | goto done;
|
---|
742 | }
|
---|
743 |
|
---|
744 | status = dcerpc_binding_vector_add_np_default(&ndr_table_samr, v);
|
---|
745 | if (!NT_STATUS_IS_OK(status)) {
|
---|
746 | goto done;
|
---|
747 | }
|
---|
748 |
|
---|
749 | status = dcerpc_binding_vector_add_unix(&ndr_table_lsarpc, v, "samr");
|
---|
750 | if (!NT_STATUS_IS_OK(status)) {
|
---|
751 | goto done;
|
---|
752 | }
|
---|
753 |
|
---|
754 | status = rpc_ep_register(ev_ctx, msg_ctx, &ndr_table_samr, v);
|
---|
755 | if (!NT_STATUS_IS_OK(status)) {
|
---|
756 | goto done;
|
---|
757 | }
|
---|
758 |
|
---|
759 | /* NETLOGON */
|
---|
760 | fd = create_named_pipe_socket("netlogon");
|
---|
761 | if (fd < 0) {
|
---|
762 | goto done;
|
---|
763 | }
|
---|
764 |
|
---|
765 | rc = listen(fd, pf_lsasd_cfg.max_allowed_clients);
|
---|
766 | if (rc == -1) {
|
---|
767 | DEBUG(0, ("Failed to listen on samr pipe - %s\n",
|
---|
768 | strerror(errno)));
|
---|
769 | goto done;
|
---|
770 | }
|
---|
771 | listen_fd[*listen_fd_size] = fd;
|
---|
772 | (*listen_fd_size)++;
|
---|
773 |
|
---|
774 | fd = create_dcerpc_ncalrpc_socket("netlogon");
|
---|
775 | if (fd < 0) {
|
---|
776 | goto done;
|
---|
777 | }
|
---|
778 |
|
---|
779 | rc = listen(fd, pf_lsasd_cfg.max_allowed_clients);
|
---|
780 | if (rc == -1) {
|
---|
781 | DEBUG(0, ("Failed to listen on netlogon ncalrpc - %s\n",
|
---|
782 | strerror(errno)));
|
---|
783 | goto done;
|
---|
784 | }
|
---|
785 | listen_fd[*listen_fd_size] = fd;
|
---|
786 | (*listen_fd_size)++;
|
---|
787 | fd = -1;
|
---|
788 |
|
---|
789 | v = dcerpc_binding_vector_dup(tmp_ctx, v_orig);
|
---|
790 | if (v == NULL) {
|
---|
791 | goto done;
|
---|
792 | }
|
---|
793 |
|
---|
794 | status = dcerpc_binding_vector_replace_iface(&ndr_table_netlogon, v);
|
---|
795 | if (!NT_STATUS_IS_OK(status)) {
|
---|
796 | goto done;
|
---|
797 | }
|
---|
798 |
|
---|
799 | status = dcerpc_binding_vector_add_np_default(&ndr_table_netlogon, v);
|
---|
800 | if (!NT_STATUS_IS_OK(status)) {
|
---|
801 | goto done;
|
---|
802 | }
|
---|
803 |
|
---|
804 | status = dcerpc_binding_vector_add_unix(&ndr_table_lsarpc, v, "netlogon");
|
---|
805 | if (!NT_STATUS_IS_OK(status)) {
|
---|
806 | goto done;
|
---|
807 | }
|
---|
808 |
|
---|
809 | status = rpc_ep_register(ev_ctx, msg_ctx, &ndr_table_netlogon, v);
|
---|
810 | if (!NT_STATUS_IS_OK(status)) {
|
---|
811 | goto done;
|
---|
812 | }
|
---|
813 |
|
---|
814 | ok = true;
|
---|
815 | done:
|
---|
816 | if (fd != -1) {
|
---|
817 | close(fd);
|
---|
818 | }
|
---|
819 | talloc_free(tmp_ctx);
|
---|
820 | return ok;
|
---|
821 | }
|
---|
822 |
|
---|
823 | void start_lsasd(struct tevent_context *ev_ctx,
|
---|
824 | struct messaging_context *msg_ctx)
|
---|
825 | {
|
---|
826 | NTSTATUS status;
|
---|
827 | int listen_fd[LSASD_MAX_SOCKETS];
|
---|
828 | int listen_fd_size = 0;
|
---|
829 | pid_t pid;
|
---|
830 | int rc;
|
---|
831 | bool ok;
|
---|
832 |
|
---|
833 | DEBUG(1, ("Forking LSA Service Daemon\n"));
|
---|
834 |
|
---|
835 | /*
|
---|
836 | * Block signals before forking child as it will have to
|
---|
837 | * set its own handlers. Child will re-enable SIGHUP as
|
---|
838 | * soon as the handlers are set up.
|
---|
839 | */
|
---|
840 | BlockSignals(true, SIGTERM);
|
---|
841 | BlockSignals(true, SIGHUP);
|
---|
842 |
|
---|
843 | pid = fork();
|
---|
844 | if (pid == -1) {
|
---|
845 | DEBUG(0, ("Failed to fork LSASD [%s], aborting ...\n",
|
---|
846 | strerror(errno)));
|
---|
847 | exit(1);
|
---|
848 | }
|
---|
849 |
|
---|
850 | /* parent or error */
|
---|
851 | if (pid != 0) {
|
---|
852 |
|
---|
853 | /* Re-enable SIGHUP before returnig */
|
---|
854 | BlockSignals(false, SIGTERM);
|
---|
855 | BlockSignals(false, SIGHUP);
|
---|
856 |
|
---|
857 | return;
|
---|
858 | }
|
---|
859 |
|
---|
860 | status = smbd_reinit_after_fork(msg_ctx, ev_ctx, true, "lsasd-master");
|
---|
861 | if (!NT_STATUS_IS_OK(status)) {
|
---|
862 | DEBUG(0,("reinit_after_fork() failed\n"));
|
---|
863 | smb_panic("reinit_after_fork() failed");
|
---|
864 | }
|
---|
865 |
|
---|
866 | /* save the parent process id so the children can use it later */
|
---|
867 | parent_id = messaging_server_id(msg_ctx);
|
---|
868 |
|
---|
869 | lsasd_reopen_logs(0);
|
---|
870 | pfh_daemon_config(DAEMON_NAME,
|
---|
871 | &pf_lsasd_cfg,
|
---|
872 | &default_pf_lsasd_cfg);
|
---|
873 |
|
---|
874 | lsasd_setup_sig_term_handler(ev_ctx);
|
---|
875 | lsasd_setup_sig_hup_handler(ev_ctx);
|
---|
876 |
|
---|
877 | BlockSignals(false, SIGTERM);
|
---|
878 | BlockSignals(false, SIGHUP);
|
---|
879 |
|
---|
880 | ok = lsasd_create_sockets(ev_ctx, msg_ctx, listen_fd, &listen_fd_size);
|
---|
881 | if (!ok) {
|
---|
882 | exit(1);
|
---|
883 | }
|
---|
884 |
|
---|
885 | /* start children before any more initialization is done */
|
---|
886 | ok = prefork_create_pool(ev_ctx, /* mem_ctx */
|
---|
887 | ev_ctx,
|
---|
888 | msg_ctx,
|
---|
889 | listen_fd_size,
|
---|
890 | listen_fd,
|
---|
891 | pf_lsasd_cfg.min_children,
|
---|
892 | pf_lsasd_cfg.max_children,
|
---|
893 | &lsasd_children_main,
|
---|
894 | NULL,
|
---|
895 | &lsasd_pool);
|
---|
896 | if (!ok) {
|
---|
897 | exit(1);
|
---|
898 | }
|
---|
899 |
|
---|
900 | if (!serverid_register(messaging_server_id(msg_ctx),
|
---|
901 | FLAG_MSG_GENERAL)) {
|
---|
902 | exit(1);
|
---|
903 | }
|
---|
904 |
|
---|
905 | messaging_register(msg_ctx,
|
---|
906 | ev_ctx,
|
---|
907 | MSG_SMB_CONF_UPDATED,
|
---|
908 | lsasd_smb_conf_updated);
|
---|
909 | messaging_register(msg_ctx, ev_ctx,
|
---|
910 | MSG_PREFORK_CHILD_EVENT, child_ping);
|
---|
911 |
|
---|
912 | status = rpc_lsarpc_init(NULL);
|
---|
913 | if (!NT_STATUS_IS_OK(status)) {
|
---|
914 | DEBUG(0, ("Failed to register lsarpc rpc interface in lsasd! (%s)\n",
|
---|
915 | nt_errstr(status)));
|
---|
916 | exit(1);
|
---|
917 | }
|
---|
918 |
|
---|
919 | status = rpc_samr_init(NULL);
|
---|
920 | if (!NT_STATUS_IS_OK(status)) {
|
---|
921 | DEBUG(0, ("Failed to register samr rpc interface in lsasd! (%s)\n",
|
---|
922 | nt_errstr(status)));
|
---|
923 | exit(1);
|
---|
924 | }
|
---|
925 |
|
---|
926 | status = rpc_netlogon_init(NULL);
|
---|
927 | if (!NT_STATUS_IS_OK(status)) {
|
---|
928 | DEBUG(0, ("Failed to register netlogon rpc interface in lsasd! (%s)\n",
|
---|
929 | nt_errstr(status)));
|
---|
930 | exit(1);
|
---|
931 | }
|
---|
932 |
|
---|
933 | ok = lsasd_setup_children_monitor(ev_ctx, msg_ctx);
|
---|
934 | if (!ok) {
|
---|
935 | DEBUG(0, ("Failed to setup children monitoring!\n"));
|
---|
936 | exit(1);
|
---|
937 | }
|
---|
938 |
|
---|
939 | DEBUG(1, ("LSASD Daemon Started (%u)\n", (unsigned int)getpid()));
|
---|
940 |
|
---|
941 | /* loop forever */
|
---|
942 | rc = tevent_loop_wait(ev_ctx);
|
---|
943 |
|
---|
944 | /* should not be reached */
|
---|
945 | DEBUG(0,("lsasd: tevent_loop_wait() exited with %d - %s\n",
|
---|
946 | rc, (rc == 0) ? "out of events" : strerror(errno)));
|
---|
947 | exit(1);
|
---|
948 | }
|
---|