s3/packaging: pam_winbind has been moved to section 8.
[Samba/gebeck_regimport.git] / source3 / winbindd / winbindd_dual.c
blob92f0d60817e474725b0cfa6fcdac8e44304bbb7f
1 /*
2 Unix SMB/CIFS implementation.
4 Winbind child daemons
6 Copyright (C) Andrew Tridgell 2002
7 Copyright (C) Volker Lendecke 2004,2005
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
24 * We fork a child per domain to be able to act non-blocking in the main
25 * winbind daemon. A domain controller thousands of miles away being being
26 * slow replying with a 10.000 user list should not hold up netlogon calls
27 * that can be handled locally.
30 #include "includes.h"
31 #include "winbindd.h"
32 #include "../../nsswitch/libwbclient/wbc_async.h"
34 #undef DBGC_CLASS
35 #define DBGC_CLASS DBGC_WINBIND
37 extern bool override_logfile;
38 extern struct winbindd_methods cache_methods;
40 /* Read some data from a client connection */
42 static NTSTATUS child_read_request(struct winbindd_cli_state *state)
44 NTSTATUS status;
46 /* Read data */
48 status = read_data(state->sock, (char *)state->request,
49 sizeof(*state->request));
51 if (!NT_STATUS_IS_OK(status)) {
52 DEBUG(3, ("child_read_request: read_data failed: %s\n",
53 nt_errstr(status)));
54 return status;
57 if (state->request->extra_len == 0) {
58 state->request->extra_data.data = NULL;
59 return NT_STATUS_OK;
62 DEBUG(10, ("Need to read %d extra bytes\n", (int)state->request->extra_len));
64 state->request->extra_data.data =
65 SMB_MALLOC_ARRAY(char, state->request->extra_len + 1);
67 if (state->request->extra_data.data == NULL) {
68 DEBUG(0, ("malloc failed\n"));
69 return NT_STATUS_NO_MEMORY;
72 /* Ensure null termination */
73 state->request->extra_data.data[state->request->extra_len] = '\0';
75 status= read_data(state->sock, state->request->extra_data.data,
76 state->request->extra_len);
78 if (!NT_STATUS_IS_OK(status)) {
79 DEBUG(0, ("Could not read extra data: %s\n",
80 nt_errstr(status)));
82 return status;
86 * Do winbind child async request. This is not simply wb_simple_trans. We have
87 * to do the queueing ourselves because while a request is queued, the child
88 * might have crashed, and we have to re-fork it in the _trigger function.
91 struct wb_child_request_state {
92 struct tevent_context *ev;
93 struct winbindd_child *child;
94 struct winbindd_request *request;
95 struct winbindd_response *response;
98 static bool fork_domain_child(struct winbindd_child *child);
100 static void wb_child_request_trigger(struct tevent_req *req,
101 void *private_data);
102 static void wb_child_request_done(struct tevent_req *subreq);
104 struct tevent_req *wb_child_request_send(TALLOC_CTX *mem_ctx,
105 struct tevent_context *ev,
106 struct winbindd_child *child,
107 struct winbindd_request *request)
109 struct tevent_req *req;
110 struct wb_child_request_state *state;
112 req = tevent_req_create(mem_ctx, &state,
113 struct wb_child_request_state);
114 if (req == NULL) {
115 return NULL;
118 state->ev = ev;
119 state->child = child;
120 state->request = request;
122 if (!tevent_queue_add(child->queue, ev, req,
123 wb_child_request_trigger, NULL)) {
124 tevent_req_nomem(NULL, req);
125 return tevent_req_post(req, ev);
127 return req;
130 static void wb_child_request_trigger(struct tevent_req *req,
131 void *private_data)
133 struct wb_child_request_state *state = tevent_req_data(
134 req, struct wb_child_request_state);
135 struct tevent_req *subreq;
137 if ((state->child->pid == 0) && (!fork_domain_child(state->child))) {
138 tevent_req_error(req, errno);
139 return;
142 subreq = wb_simple_trans_send(state, winbind_event_context(), NULL,
143 state->child->sock, state->request);
144 if (tevent_req_nomem(subreq, req)) {
145 return;
147 tevent_req_set_callback(subreq, wb_child_request_done, req);
149 if (!tevent_req_set_endtime(req, state->ev,
150 timeval_current_ofs(300, 0))) {
151 tevent_req_nomem(NULL, req);
152 return;
156 static void wb_child_request_done(struct tevent_req *subreq)
158 struct tevent_req *req = tevent_req_callback_data(
159 subreq, struct tevent_req);
160 struct wb_child_request_state *state = tevent_req_data(
161 req, struct wb_child_request_state);
162 int ret, err;
164 ret = wb_simple_trans_recv(subreq, state, &state->response, &err);
165 TALLOC_FREE(subreq);
166 if (ret == -1) {
167 tevent_req_error(req, err);
168 return;
170 tevent_req_done(req);
173 int wb_child_request_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
174 struct winbindd_response **presponse, int *err)
176 struct wb_child_request_state *state = tevent_req_data(
177 req, struct wb_child_request_state);
179 if (tevent_req_is_unix_error(req, err)) {
180 return -1;
182 if (state->response->result != WINBINDD_OK) {
183 *err = EIO; /* EIO doesn't fit, but what would be better? */
184 return -1;
186 *presponse = talloc_move(mem_ctx, &state->response);
187 return 0;
190 struct wb_domain_request_state {
191 struct tevent_context *ev;
192 struct winbindd_domain *domain;
193 struct winbindd_request *request;
194 struct winbindd_request *init_req;
195 struct winbindd_response *response;
198 static void wb_domain_request_gotdc(struct tevent_req *subreq);
199 static void wb_domain_request_initialized(struct tevent_req *subreq);
200 static void wb_domain_request_done(struct tevent_req *subreq);
202 struct tevent_req *wb_domain_request_send(TALLOC_CTX *mem_ctx,
203 struct tevent_context *ev,
204 struct winbindd_domain *domain,
205 struct winbindd_request *request)
207 struct tevent_req *req, *subreq;
208 struct wb_domain_request_state *state;
210 req = tevent_req_create(mem_ctx, &state,
211 struct wb_domain_request_state);
212 if (req == NULL) {
213 return NULL;
216 if (domain->initialized) {
217 subreq = wb_child_request_send(state, ev, &domain->child,
218 request);
219 if (tevent_req_nomem(subreq, req)) {
220 return tevent_req_post(req, ev);
222 tevent_req_set_callback(subreq, wb_domain_request_done, req);
223 return req;
226 state->domain = domain;
227 state->ev = ev;
228 state->request = request;
230 state->init_req = talloc_zero(state, struct winbindd_request);
231 if (tevent_req_nomem(state->init_req, req)) {
232 return tevent_req_post(req, ev);
235 if (IS_DC || domain->primary || domain->internal) {
236 /* The primary domain has to find the DC name itself */
237 state->init_req->cmd = WINBINDD_INIT_CONNECTION;
238 fstrcpy(state->init_req->domain_name, domain->name);
239 state->init_req->data.init_conn.is_primary =
240 domain->primary ? true : false;
241 fstrcpy(state->init_req->data.init_conn.dcname, "");
243 subreq = wb_child_request_send(state, ev, &domain->child,
244 state->init_req);
245 if (tevent_req_nomem(subreq, req)) {
246 return tevent_req_post(req, ev);
248 tevent_req_set_callback(subreq, wb_domain_request_initialized,
249 req);
250 return req;
254 * Ask our DC for a DC name
256 domain = find_our_domain();
258 /* This is *not* the primary domain, let's ask our DC about a DC
259 * name */
261 state->init_req->cmd = WINBINDD_GETDCNAME;
262 fstrcpy(state->init_req->domain_name, domain->name);
264 subreq = wb_child_request_send(state, ev, &domain->child, request);
265 if (tevent_req_nomem(subreq, req)) {
266 return tevent_req_post(req, ev);
268 tevent_req_set_callback(subreq, wb_domain_request_gotdc, req);
269 return req;
272 static void wb_domain_request_gotdc(struct tevent_req *subreq)
274 struct tevent_req *req = tevent_req_callback_data(
275 subreq, struct tevent_req);
276 struct wb_domain_request_state *state = tevent_req_data(
277 req, struct wb_domain_request_state);
278 struct winbindd_response *response;
279 int ret, err;
281 ret = wb_child_request_recv(subreq, talloc_tos(), &response, &err);
282 TALLOC_FREE(subreq);
283 if (ret == -1) {
284 tevent_req_error(req, err);
285 return;
287 state->init_req->cmd = WINBINDD_INIT_CONNECTION;
288 fstrcpy(state->init_req->domain_name, state->domain->name);
289 state->init_req->data.init_conn.is_primary = False;
290 fstrcpy(state->init_req->data.init_conn.dcname,
291 response->data.dc_name);
293 TALLOC_FREE(response);
295 subreq = wb_child_request_send(state, state->ev, &state->domain->child,
296 state->init_req);
297 if (tevent_req_nomem(subreq, req)) {
298 return;
300 tevent_req_set_callback(subreq, wb_domain_request_initialized, req);
303 static void wb_domain_request_initialized(struct tevent_req *subreq)
305 struct tevent_req *req = tevent_req_callback_data(
306 subreq, struct tevent_req);
307 struct wb_domain_request_state *state = tevent_req_data(
308 req, struct wb_domain_request_state);
309 struct winbindd_response *response;
310 int ret, err;
312 ret = wb_child_request_recv(subreq, talloc_tos(), &response, &err);
313 TALLOC_FREE(subreq);
314 if (ret == -1) {
315 tevent_req_error(req, err);
316 return;
319 if (!string_to_sid(&state->domain->sid,
320 response->data.domain_info.sid)) {
321 DEBUG(1,("init_child_recv: Could not convert sid %s "
322 "from string\n", response->data.domain_info.sid));
323 tevent_req_error(req, EINVAL);
324 return;
326 fstrcpy(state->domain->name, response->data.domain_info.name);
327 fstrcpy(state->domain->alt_name, response->data.domain_info.alt_name);
328 state->domain->native_mode = response->data.domain_info.native_mode;
329 state->domain->active_directory =
330 response->data.domain_info.active_directory;
331 state->domain->initialized = true;
333 TALLOC_FREE(response);
335 subreq = wb_child_request_send(state, state->ev, &state->domain->child,
336 state->request);
337 if (tevent_req_nomem(subreq, req)) {
338 return;
340 tevent_req_set_callback(subreq, wb_domain_request_done, req);
343 static void wb_domain_request_done(struct tevent_req *subreq)
345 struct tevent_req *req = tevent_req_callback_data(
346 subreq, struct tevent_req);
347 struct wb_domain_request_state *state = tevent_req_data(
348 req, struct wb_domain_request_state);
349 int ret, err;
351 ret = wb_child_request_recv(subreq, talloc_tos(), &state->response,
352 &err);
353 TALLOC_FREE(subreq);
354 if (ret == -1) {
355 tevent_req_error(req, err);
356 return;
358 tevent_req_done(req);
361 int wb_domain_request_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
362 struct winbindd_response **presponse, int *err)
364 struct wb_domain_request_state *state = tevent_req_data(
365 req, struct wb_domain_request_state);
367 if (tevent_req_is_unix_error(req, err)) {
368 return -1;
370 *presponse = talloc_move(mem_ctx, &state->response);
371 return 0;
375 * Machinery for async requests sent to children. You set up a
376 * winbindd_request, select a child to query, and issue a async_request
377 * call. When the request is completed, the callback function you specified is
378 * called back with the private pointer you gave to async_request.
381 struct winbindd_async_request {
382 struct winbindd_async_request *next, *prev;
383 TALLOC_CTX *mem_ctx;
384 struct winbindd_child *child;
385 struct winbindd_response *response;
386 void (*continuation)(void *private_data, bool success);
387 struct timed_event *reply_timeout_event;
388 pid_t child_pid; /* pid of the child we're waiting on. Used to detect
389 a restart of the child (child->pid != child_pid). */
390 void *private_data;
393 static bool fork_domain_child(struct winbindd_child *child);
394 static void async_request_done(struct tevent_req *req);
396 void async_request(TALLOC_CTX *mem_ctx, struct winbindd_child *child,
397 struct winbindd_request *request,
398 struct winbindd_response *response,
399 void (*continuation)(void *private_data, bool success),
400 void *private_data)
402 struct winbindd_async_request *state;
403 struct tevent_req *req;
405 DEBUG(10, ("Sending request to child pid %d (domain=%s)\n",
406 (int)child->pid,
407 (child->domain != NULL) ? child->domain->name : "''"));
409 state = talloc(mem_ctx, struct winbindd_async_request);
410 if (state == NULL) {
411 DEBUG(0, ("talloc failed\n"));
412 continuation(private_data, False);
413 return;
416 state->mem_ctx = mem_ctx;
417 state->child = child;
418 state->reply_timeout_event = NULL;
419 state->response = response;
420 state->continuation = continuation;
421 state->private_data = private_data;
423 request->pid = child->pid;
425 req = wb_child_request_send(state, winbind_event_context(),
426 child, request);
427 if (req == NULL) {
428 DEBUG(0, ("wb_child_request_send failed\n"));
429 continuation(private_data, false);
430 return;
432 tevent_req_set_callback(req, async_request_done, state);
435 static void async_request_done(struct tevent_req *req)
437 struct winbindd_async_request *state = tevent_req_callback_data(
438 req, struct winbindd_async_request);
439 struct winbindd_response *response;
440 int ret, err;
442 ret = wb_child_request_recv(req, state, &response, &err);
443 TALLOC_FREE(req);
444 if (ret == -1) {
445 DEBUG(2, ("wb_child_request_recv failed: %s\n",
446 strerror(err)));
447 state->continuation(state->private_data, false);
448 return;
450 *state->response = *response;
451 state->continuation(state->private_data, true);
454 struct domain_request_state {
455 struct winbindd_domain *domain;
456 struct winbindd_request *request;
457 struct winbindd_response *response;
458 void (*continuation)(void *private_data_data, bool success);
459 void *private_data_data;
462 static void async_domain_request_done(struct tevent_req *req);
464 void async_domain_request(TALLOC_CTX *mem_ctx,
465 struct winbindd_domain *domain,
466 struct winbindd_request *request,
467 struct winbindd_response *response,
468 void (*continuation)(void *private_data_data, bool success),
469 void *private_data_data)
471 struct tevent_req *subreq;
472 struct domain_request_state *state;
474 state = TALLOC_P(mem_ctx, struct domain_request_state);
475 if (state == NULL) {
476 DEBUG(0, ("talloc failed\n"));
477 continuation(private_data_data, False);
478 return;
481 state->domain = domain;
482 state->request = request;
483 state->response = response;
484 state->continuation = continuation;
485 state->private_data_data = private_data_data;
487 subreq = wb_domain_request_send(state, winbind_event_context(),
488 domain, request);
489 if (subreq == NULL) {
490 DEBUG(5, ("wb_domain_request_send failed\n"));
491 continuation(private_data_data, false);
492 return;
494 tevent_req_set_callback(subreq, async_domain_request_done, state);
497 static void async_domain_request_done(struct tevent_req *req)
499 struct domain_request_state *state = tevent_req_callback_data(
500 req, struct domain_request_state);
501 struct winbindd_response *response;
502 int ret, err;
504 ret = wb_domain_request_recv(req, state, &response, &err);
505 TALLOC_FREE(req);
506 if (ret == -1) {
507 DEBUG(5, ("wb_domain_request returned %s\n", strerror(err)));
508 state->continuation(state->private_data_data, false);
509 return;
511 *(state->response) = *response;
512 state->continuation(state->private_data_data, true);
515 static void recvfrom_child(void *private_data_data, bool success)
517 struct winbindd_cli_state *state =
518 talloc_get_type_abort(private_data_data, struct winbindd_cli_state);
519 enum winbindd_result result = state->response->result;
521 /* This is an optimization: The child has written directly to the
522 * response buffer. The request itself is still in pending state,
523 * state that in the result code. */
525 state->response->result = WINBINDD_PENDING;
527 if ((!success) || (result != WINBINDD_OK)) {
528 request_error(state);
529 return;
532 request_ok(state);
535 void sendto_child(struct winbindd_cli_state *state,
536 struct winbindd_child *child)
538 async_request(state->mem_ctx, child, state->request,
539 state->response, recvfrom_child, state);
542 void sendto_domain(struct winbindd_cli_state *state,
543 struct winbindd_domain *domain)
545 async_domain_request(state->mem_ctx, domain,
546 state->request, state->response,
547 recvfrom_child, state);
550 static void child_process_request(struct winbindd_child *child,
551 struct winbindd_cli_state *state)
553 struct winbindd_domain *domain = child->domain;
554 const struct winbindd_child_dispatch_table *table = child->table;
556 /* Free response data - we may be interrupted and receive another
557 command before being able to send this data off. */
559 state->response->result = WINBINDD_ERROR;
560 state->response->length = sizeof(struct winbindd_response);
562 /* as all requests in the child are sync, we can use talloc_tos() */
563 state->mem_ctx = talloc_tos();
565 /* Process command */
567 for (; table->name; table++) {
568 if (state->request->cmd == table->struct_cmd) {
569 DEBUG(10,("child_process_request: request fn %s\n",
570 table->name));
571 state->response->result = table->struct_fn(domain, state);
572 return;
576 DEBUG(1 ,("child_process_request: unknown request fn number %d\n",
577 (int)state->request->cmd));
578 state->response->result = WINBINDD_ERROR;
581 void setup_child(struct winbindd_child *child,
582 const struct winbindd_child_dispatch_table *table,
583 const char *logprefix,
584 const char *logname)
586 if (logprefix && logname) {
587 if (asprintf(&child->logfilename, "%s/%s-%s",
588 get_dyn_LOGFILEBASE(), logprefix, logname) < 0) {
589 smb_panic("Internal error: asprintf failed");
591 } else {
592 smb_panic("Internal error: logprefix == NULL && "
593 "logname == NULL");
596 child->domain = NULL;
597 child->table = table;
598 child->queue = tevent_queue_create(NULL, "winbind_child");
599 SMB_ASSERT(child->queue != NULL);
602 struct winbindd_child *children = NULL;
604 void winbind_child_died(pid_t pid)
606 struct winbindd_child *child;
608 for (child = children; child != NULL; child = child->next) {
609 if (child->pid == pid) {
610 break;
614 if (child == NULL) {
615 DEBUG(5, ("Already reaped child %u died\n", (unsigned int)pid));
616 return;
619 /* This will be re-added in fork_domain_child() */
621 DLIST_REMOVE(children, child);
623 close(child->sock);
624 child->sock = -1;
625 child->pid = 0;
628 /* Ensure any negative cache entries with the netbios or realm names are removed. */
630 void winbindd_flush_negative_conn_cache(struct winbindd_domain *domain)
632 flush_negative_conn_cache_for_domain(domain->name);
633 if (*domain->alt_name) {
634 flush_negative_conn_cache_for_domain(domain->alt_name);
639 * Parent winbindd process sets its own debug level first and then
640 * sends a message to all the winbindd children to adjust their debug
641 * level to that of parents.
644 void winbind_msg_debug(struct messaging_context *msg_ctx,
645 void *private_data,
646 uint32_t msg_type,
647 struct server_id server_id,
648 DATA_BLOB *data)
650 struct winbindd_child *child;
652 DEBUG(10,("winbind_msg_debug: got debug message.\n"));
654 debug_message(msg_ctx, private_data, MSG_DEBUG, server_id, data);
656 for (child = children; child != NULL; child = child->next) {
658 DEBUG(10,("winbind_msg_debug: sending message to pid %u.\n",
659 (unsigned int)child->pid));
661 messaging_send_buf(msg_ctx, pid_to_procid(child->pid),
662 MSG_DEBUG,
663 data->data,
664 strlen((char *) data->data) + 1);
668 /* Set our domains as offline and forward the offline message to our children. */
670 void winbind_msg_offline(struct messaging_context *msg_ctx,
671 void *private_data,
672 uint32_t msg_type,
673 struct server_id server_id,
674 DATA_BLOB *data)
676 struct winbindd_child *child;
677 struct winbindd_domain *domain;
679 DEBUG(10,("winbind_msg_offline: got offline message.\n"));
681 if (!lp_winbind_offline_logon()) {
682 DEBUG(10,("winbind_msg_offline: rejecting offline message.\n"));
683 return;
686 /* Set our global state as offline. */
687 if (!set_global_winbindd_state_offline()) {
688 DEBUG(10,("winbind_msg_offline: offline request failed.\n"));
689 return;
692 /* Set all our domains as offline. */
693 for (domain = domain_list(); domain; domain = domain->next) {
694 if (domain->internal) {
695 continue;
697 DEBUG(5,("winbind_msg_offline: marking %s offline.\n", domain->name));
698 set_domain_offline(domain);
701 for (child = children; child != NULL; child = child->next) {
702 /* Don't send message to internal childs. We've already
703 done so above. */
704 if (!child->domain || winbindd_internal_child(child)) {
705 continue;
708 /* Or internal domains (this should not be possible....) */
709 if (child->domain->internal) {
710 continue;
713 /* Each winbindd child should only process requests for one domain - make sure
714 we only set it online / offline for that domain. */
716 DEBUG(10,("winbind_msg_offline: sending message to pid %u for domain %s.\n",
717 (unsigned int)child->pid, domain->name ));
719 messaging_send_buf(msg_ctx, pid_to_procid(child->pid),
720 MSG_WINBIND_OFFLINE,
721 (uint8 *)child->domain->name,
722 strlen(child->domain->name)+1);
726 /* Set our domains as online and forward the online message to our children. */
728 void winbind_msg_online(struct messaging_context *msg_ctx,
729 void *private_data,
730 uint32_t msg_type,
731 struct server_id server_id,
732 DATA_BLOB *data)
734 struct winbindd_child *child;
735 struct winbindd_domain *domain;
737 DEBUG(10,("winbind_msg_online: got online message.\n"));
739 if (!lp_winbind_offline_logon()) {
740 DEBUG(10,("winbind_msg_online: rejecting online message.\n"));
741 return;
744 /* Set our global state as online. */
745 set_global_winbindd_state_online();
747 smb_nscd_flush_user_cache();
748 smb_nscd_flush_group_cache();
750 /* Set all our domains as online. */
751 for (domain = domain_list(); domain; domain = domain->next) {
752 if (domain->internal) {
753 continue;
755 DEBUG(5,("winbind_msg_online: requesting %s to go online.\n", domain->name));
757 winbindd_flush_negative_conn_cache(domain);
758 set_domain_online_request(domain);
760 /* Send an online message to the idmap child when our
761 primary domain comes back online */
763 if ( domain->primary ) {
764 struct winbindd_child *idmap = idmap_child();
766 if ( idmap->pid != 0 ) {
767 messaging_send_buf(msg_ctx,
768 pid_to_procid(idmap->pid),
769 MSG_WINBIND_ONLINE,
770 (uint8 *)domain->name,
771 strlen(domain->name)+1);
776 for (child = children; child != NULL; child = child->next) {
777 /* Don't send message to internal childs. */
778 if (!child->domain || winbindd_internal_child(child)) {
779 continue;
782 /* Or internal domains (this should not be possible....) */
783 if (child->domain->internal) {
784 continue;
787 /* Each winbindd child should only process requests for one domain - make sure
788 we only set it online / offline for that domain. */
790 DEBUG(10,("winbind_msg_online: sending message to pid %u for domain %s.\n",
791 (unsigned int)child->pid, child->domain->name ));
793 messaging_send_buf(msg_ctx, pid_to_procid(child->pid),
794 MSG_WINBIND_ONLINE,
795 (uint8 *)child->domain->name,
796 strlen(child->domain->name)+1);
800 static const char *collect_onlinestatus(TALLOC_CTX *mem_ctx)
802 struct winbindd_domain *domain;
803 char *buf = NULL;
805 if ((buf = talloc_asprintf(mem_ctx, "global:%s ",
806 get_global_winbindd_state_offline() ?
807 "Offline":"Online")) == NULL) {
808 return NULL;
811 for (domain = domain_list(); domain; domain = domain->next) {
812 if ((buf = talloc_asprintf_append_buffer(buf, "%s:%s ",
813 domain->name,
814 domain->online ?
815 "Online":"Offline")) == NULL) {
816 return NULL;
820 buf = talloc_asprintf_append_buffer(buf, "\n");
822 DEBUG(5,("collect_onlinestatus: %s", buf));
824 return buf;
827 void winbind_msg_onlinestatus(struct messaging_context *msg_ctx,
828 void *private_data,
829 uint32_t msg_type,
830 struct server_id server_id,
831 DATA_BLOB *data)
833 TALLOC_CTX *mem_ctx;
834 const char *message;
835 struct server_id *sender;
837 DEBUG(5,("winbind_msg_onlinestatus received.\n"));
839 if (!data->data) {
840 return;
843 sender = (struct server_id *)data->data;
845 mem_ctx = talloc_init("winbind_msg_onlinestatus");
846 if (mem_ctx == NULL) {
847 return;
850 message = collect_onlinestatus(mem_ctx);
851 if (message == NULL) {
852 talloc_destroy(mem_ctx);
853 return;
856 messaging_send_buf(msg_ctx, *sender, MSG_WINBIND_ONLINESTATUS,
857 (uint8 *)message, strlen(message) + 1);
859 talloc_destroy(mem_ctx);
862 void winbind_msg_dump_event_list(struct messaging_context *msg_ctx,
863 void *private_data,
864 uint32_t msg_type,
865 struct server_id server_id,
866 DATA_BLOB *data)
868 struct winbindd_child *child;
870 DEBUG(10,("winbind_msg_dump_event_list received\n"));
872 dump_event_list(winbind_event_context());
874 for (child = children; child != NULL; child = child->next) {
876 DEBUG(10,("winbind_msg_dump_event_list: sending message to pid %u\n",
877 (unsigned int)child->pid));
879 messaging_send_buf(msg_ctx, pid_to_procid(child->pid),
880 MSG_DUMP_EVENT_LIST,
881 NULL, 0);
886 void winbind_msg_dump_domain_list(struct messaging_context *msg_ctx,
887 void *private_data,
888 uint32_t msg_type,
889 struct server_id server_id,
890 DATA_BLOB *data)
892 TALLOC_CTX *mem_ctx;
893 const char *message = NULL;
894 struct server_id *sender = NULL;
895 const char *domain = NULL;
896 char *s = NULL;
897 NTSTATUS status;
898 struct winbindd_domain *dom = NULL;
900 DEBUG(5,("winbind_msg_dump_domain_list received.\n"));
902 if (!data || !data->data) {
903 return;
906 if (data->length < sizeof(struct server_id)) {
907 return;
910 mem_ctx = talloc_init("winbind_msg_dump_domain_list");
911 if (!mem_ctx) {
912 return;
915 sender = (struct server_id *)data->data;
916 if (data->length > sizeof(struct server_id)) {
917 domain = (const char *)data->data+sizeof(struct server_id);
920 if (domain) {
922 DEBUG(5,("winbind_msg_dump_domain_list for domain: %s\n",
923 domain));
925 message = NDR_PRINT_STRUCT_STRING(mem_ctx, winbindd_domain,
926 find_domain_from_name_noinit(domain));
927 if (!message) {
928 talloc_destroy(mem_ctx);
929 return;
932 messaging_send_buf(msg_ctx, *sender,
933 MSG_WINBIND_DUMP_DOMAIN_LIST,
934 (uint8_t *)message, strlen(message) + 1);
936 talloc_destroy(mem_ctx);
938 return;
941 DEBUG(5,("winbind_msg_dump_domain_list all domains\n"));
943 for (dom = domain_list(); dom; dom=dom->next) {
944 message = NDR_PRINT_STRUCT_STRING(mem_ctx, winbindd_domain, dom);
945 if (!message) {
946 talloc_destroy(mem_ctx);
947 return;
950 s = talloc_asprintf_append(s, "%s\n", message);
951 if (!s) {
952 talloc_destroy(mem_ctx);
953 return;
957 status = messaging_send_buf(msg_ctx, *sender,
958 MSG_WINBIND_DUMP_DOMAIN_LIST,
959 (uint8_t *)s, strlen(s) + 1);
960 if (!NT_STATUS_IS_OK(status)) {
961 DEBUG(0,("failed to send message: %s\n",
962 nt_errstr(status)));
965 talloc_destroy(mem_ctx);
968 static void account_lockout_policy_handler(struct event_context *ctx,
969 struct timed_event *te,
970 struct timeval now,
971 void *private_data)
973 struct winbindd_child *child =
974 (struct winbindd_child *)private_data;
975 TALLOC_CTX *mem_ctx = NULL;
976 struct winbindd_methods *methods;
977 struct samr_DomInfo12 lockout_policy;
978 NTSTATUS result;
980 DEBUG(10,("account_lockout_policy_handler called\n"));
982 TALLOC_FREE(child->lockout_policy_event);
984 if ( !winbindd_can_contact_domain( child->domain ) ) {
985 DEBUG(10,("account_lockout_policy_handler: Removing myself since I "
986 "do not have an incoming trust to domain %s\n",
987 child->domain->name));
989 return;
992 methods = child->domain->methods;
994 mem_ctx = talloc_init("account_lockout_policy_handler ctx");
995 if (!mem_ctx) {
996 result = NT_STATUS_NO_MEMORY;
997 } else {
998 result = methods->lockout_policy(child->domain, mem_ctx, &lockout_policy);
1000 TALLOC_FREE(mem_ctx);
1002 if (!NT_STATUS_IS_OK(result)) {
1003 DEBUG(10,("account_lockout_policy_handler: lockout_policy failed error %s\n",
1004 nt_errstr(result)));
1007 child->lockout_policy_event = event_add_timed(winbind_event_context(), NULL,
1008 timeval_current_ofs(3600, 0),
1009 account_lockout_policy_handler,
1010 child);
1013 static time_t get_machine_password_timeout(void)
1015 /* until we have gpo support use lp setting */
1016 return lp_machine_password_timeout();
1019 static bool calculate_next_machine_pwd_change(const char *domain,
1020 struct timeval *t)
1022 time_t pass_last_set_time;
1023 time_t timeout;
1024 time_t next_change;
1025 char *pw;
1027 pw = secrets_fetch_machine_password(domain,
1028 &pass_last_set_time,
1029 NULL);
1031 if (pw == NULL) {
1032 DEBUG(0,("cannot fetch own machine password ????"));
1033 return false;
1036 SAFE_FREE(pw);
1038 timeout = get_machine_password_timeout();
1039 if (timeout == 0) {
1040 DEBUG(10,("machine password never expires\n"));
1041 return false;
1044 if (time(NULL) < (pass_last_set_time + timeout)) {
1045 next_change = pass_last_set_time + timeout;
1046 DEBUG(10,("machine password still valid until: %s\n",
1047 http_timestring(talloc_tos(), next_change)));
1048 *t = timeval_set(next_change, 0);
1049 return true;
1052 DEBUG(10,("machine password expired, needs immediate change\n"));
1054 *t = timeval_zero();
1056 return true;
1059 static void machine_password_change_handler(struct event_context *ctx,
1060 struct timed_event *te,
1061 struct timeval now,
1062 void *private_data)
1064 struct winbindd_child *child =
1065 (struct winbindd_child *)private_data;
1066 struct rpc_pipe_client *netlogon_pipe = NULL;
1067 TALLOC_CTX *frame;
1068 NTSTATUS result;
1069 struct timeval next_change;
1071 DEBUG(10,("machine_password_change_handler called\n"));
1073 TALLOC_FREE(child->machine_password_change_event);
1075 if (!calculate_next_machine_pwd_change(child->domain->name,
1076 &next_change)) {
1077 return;
1080 if (!winbindd_can_contact_domain(child->domain)) {
1081 DEBUG(10,("machine_password_change_handler: Removing myself since I "
1082 "do not have an incoming trust to domain %s\n",
1083 child->domain->name));
1084 return;
1087 result = cm_connect_netlogon(child->domain, &netlogon_pipe);
1088 if (!NT_STATUS_IS_OK(result)) {
1089 DEBUG(10,("machine_password_change_handler: "
1090 "failed to connect netlogon pipe: %s\n",
1091 nt_errstr(result)));
1092 return;
1095 frame = talloc_stackframe();
1097 result = trust_pw_find_change_and_store_it(netlogon_pipe,
1098 frame,
1099 child->domain->name);
1100 TALLOC_FREE(frame);
1102 if (!NT_STATUS_IS_OK(result)) {
1103 DEBUG(10,("machine_password_change_handler: "
1104 "failed to change machine password: %s\n",
1105 nt_errstr(result)));
1106 } else {
1107 DEBUG(10,("machine_password_change_handler: "
1108 "successfully changed machine password\n"));
1111 child->machine_password_change_event = event_add_timed(winbind_event_context(), NULL,
1112 next_change,
1113 machine_password_change_handler,
1114 child);
1117 /* Deal with a request to go offline. */
1119 static void child_msg_offline(struct messaging_context *msg,
1120 void *private_data,
1121 uint32_t msg_type,
1122 struct server_id server_id,
1123 DATA_BLOB *data)
1125 struct winbindd_domain *domain;
1126 struct winbindd_domain *primary_domain = NULL;
1127 const char *domainname = (const char *)data->data;
1129 if (data->data == NULL || data->length == 0) {
1130 return;
1133 DEBUG(5,("child_msg_offline received for domain %s.\n", domainname));
1135 if (!lp_winbind_offline_logon()) {
1136 DEBUG(10,("child_msg_offline: rejecting offline message.\n"));
1137 return;
1140 primary_domain = find_our_domain();
1142 /* Mark the requested domain offline. */
1144 for (domain = domain_list(); domain; domain = domain->next) {
1145 if (domain->internal) {
1146 continue;
1148 if (strequal(domain->name, domainname)) {
1149 DEBUG(5,("child_msg_offline: marking %s offline.\n", domain->name));
1150 set_domain_offline(domain);
1151 /* we are in the trusted domain, set the primary domain
1152 * offline too */
1153 if (domain != primary_domain) {
1154 set_domain_offline(primary_domain);
1160 /* Deal with a request to go online. */
1162 static void child_msg_online(struct messaging_context *msg,
1163 void *private_data,
1164 uint32_t msg_type,
1165 struct server_id server_id,
1166 DATA_BLOB *data)
1168 struct winbindd_domain *domain;
1169 struct winbindd_domain *primary_domain = NULL;
1170 const char *domainname = (const char *)data->data;
1172 if (data->data == NULL || data->length == 0) {
1173 return;
1176 DEBUG(5,("child_msg_online received for domain %s.\n", domainname));
1178 if (!lp_winbind_offline_logon()) {
1179 DEBUG(10,("child_msg_online: rejecting online message.\n"));
1180 return;
1183 primary_domain = find_our_domain();
1185 /* Set our global state as online. */
1186 set_global_winbindd_state_online();
1188 /* Try and mark everything online - delete any negative cache entries
1189 to force a reconnect now. */
1191 for (domain = domain_list(); domain; domain = domain->next) {
1192 if (domain->internal) {
1193 continue;
1195 if (strequal(domain->name, domainname)) {
1196 DEBUG(5,("child_msg_online: requesting %s to go online.\n", domain->name));
1197 winbindd_flush_negative_conn_cache(domain);
1198 set_domain_online_request(domain);
1200 /* we can be in trusted domain, which will contact primary domain
1201 * we have to bring primary domain online in trusted domain process
1202 * see, winbindd_dual_pam_auth() --> winbindd_dual_pam_auth_samlogon()
1203 * --> contact_domain = find_our_domain()
1204 * */
1205 if (domain != primary_domain) {
1206 winbindd_flush_negative_conn_cache(primary_domain);
1207 set_domain_online_request(primary_domain);
1213 static void child_msg_dump_event_list(struct messaging_context *msg,
1214 void *private_data,
1215 uint32_t msg_type,
1216 struct server_id server_id,
1217 DATA_BLOB *data)
1219 DEBUG(5,("child_msg_dump_event_list received\n"));
1221 dump_event_list(winbind_event_context());
1224 bool winbindd_reinit_after_fork(const char *logfilename)
1226 struct winbindd_domain *domain;
1227 struct winbindd_child *cl;
1229 if (!NT_STATUS_IS_OK(reinit_after_fork(winbind_messaging_context(),
1230 winbind_event_context(),
1231 true))) {
1232 DEBUG(0,("reinit_after_fork() failed\n"));
1233 return false;
1236 close_conns_after_fork();
1238 if (!override_logfile && logfilename) {
1239 lp_set_logfile(logfilename);
1240 reopen_logs();
1243 if (!winbindd_setup_sig_term_handler(false))
1244 return false;
1245 if (!winbindd_setup_sig_hup_handler(override_logfile ? NULL :
1246 logfilename))
1247 return false;
1249 /* Don't handle the same messages as our parent. */
1250 messaging_deregister(winbind_messaging_context(),
1251 MSG_SMB_CONF_UPDATED, NULL);
1252 messaging_deregister(winbind_messaging_context(),
1253 MSG_SHUTDOWN, NULL);
1254 messaging_deregister(winbind_messaging_context(),
1255 MSG_WINBIND_OFFLINE, NULL);
1256 messaging_deregister(winbind_messaging_context(),
1257 MSG_WINBIND_ONLINE, NULL);
1258 messaging_deregister(winbind_messaging_context(),
1259 MSG_WINBIND_ONLINESTATUS, NULL);
1260 messaging_deregister(winbind_messaging_context(),
1261 MSG_DUMP_EVENT_LIST, NULL);
1262 messaging_deregister(winbind_messaging_context(),
1263 MSG_WINBIND_DUMP_DOMAIN_LIST, NULL);
1264 messaging_deregister(winbind_messaging_context(),
1265 MSG_DEBUG, NULL);
1267 /* We have destroyed all events in the winbindd_event_context
1268 * in reinit_after_fork(), so clean out all possible pending
1269 * event pointers. */
1271 /* Deal with check_online_events. */
1273 for (domain = domain_list(); domain; domain = domain->next) {
1274 TALLOC_FREE(domain->check_online_event);
1277 /* Ensure we're not handling a credential cache event inherited
1278 * from our parent. */
1280 ccache_remove_all_after_fork();
1282 /* Destroy all possible events in child list. */
1283 for (cl = children; cl != NULL; cl = cl->next) {
1284 TALLOC_FREE(cl->lockout_policy_event);
1285 TALLOC_FREE(cl->machine_password_change_event);
1287 /* Children should never be able to send
1288 * each other messages, all messages must
1289 * go through the parent.
1291 cl->pid = (pid_t)0;
1294 * This is a little tricky, children must not
1295 * send an MSG_WINBIND_ONLINE message to idmap_child().
1296 * If we are in a child of our primary domain or
1297 * in the process created by fork_child_dc_connect(),
1298 * and the primary domain cannot go online,
1299 * fork_child_dc_connection() sends MSG_WINBIND_ONLINE
1300 * periodically to idmap_child().
1302 * The sequence is, fork_child_dc_connect() ---> getdcs() --->
1303 * get_dc_name_via_netlogon() ---> cm_connect_netlogon()
1304 * ---> init_dc_connection() ---> cm_open_connection --->
1305 * set_domain_online(), sends MSG_WINBIND_ONLINE to
1306 * idmap_child(). Disallow children sending messages
1307 * to each other, all messages must go through the parent.
1309 cl = idmap_child();
1310 cl->pid = (pid_t)0;
1312 return true;
1315 static bool fork_domain_child(struct winbindd_child *child)
1317 int fdpair[2];
1318 struct winbindd_cli_state state;
1319 struct winbindd_request request;
1320 struct winbindd_response response;
1321 struct winbindd_domain *primary_domain = NULL;
1323 if (child->domain) {
1324 DEBUG(10, ("fork_domain_child called for domain '%s'\n",
1325 child->domain->name));
1326 } else {
1327 DEBUG(10, ("fork_domain_child called without domain.\n"));
1330 if (socketpair(AF_UNIX, SOCK_STREAM, 0, fdpair) != 0) {
1331 DEBUG(0, ("Could not open child pipe: %s\n",
1332 strerror(errno)));
1333 return False;
1336 ZERO_STRUCT(state);
1337 state.pid = sys_getpid();
1338 state.request = &request;
1339 state.response = &response;
1341 child->pid = sys_fork();
1343 if (child->pid == -1) {
1344 DEBUG(0, ("Could not fork: %s\n", strerror(errno)));
1345 return False;
1348 if (child->pid != 0) {
1349 /* Parent */
1350 close(fdpair[0]);
1351 child->next = child->prev = NULL;
1352 DLIST_ADD(children, child);
1353 child->sock = fdpair[1];
1354 return True;
1357 /* Child */
1359 DEBUG(10, ("Child process %d\n", (int)sys_getpid()));
1361 /* Stop zombies in children */
1362 CatchChild();
1364 state.sock = fdpair[0];
1365 close(fdpair[1]);
1367 if (!winbindd_reinit_after_fork(child->logfilename)) {
1368 _exit(0);
1371 /* Handle online/offline messages. */
1372 messaging_register(winbind_messaging_context(), NULL,
1373 MSG_WINBIND_OFFLINE, child_msg_offline);
1374 messaging_register(winbind_messaging_context(), NULL,
1375 MSG_WINBIND_ONLINE, child_msg_online);
1376 messaging_register(winbind_messaging_context(), NULL,
1377 MSG_DUMP_EVENT_LIST, child_msg_dump_event_list);
1378 messaging_register(winbind_messaging_context(), NULL,
1379 MSG_DEBUG, debug_message);
1381 primary_domain = find_our_domain();
1383 if (primary_domain == NULL) {
1384 smb_panic("no primary domain found");
1387 /* It doesn't matter if we allow cache login,
1388 * try to bring domain online after fork. */
1389 if ( child->domain ) {
1390 child->domain->startup = True;
1391 child->domain->startup_time = time(NULL);
1392 /* we can be in primary domain or in trusted domain
1393 * If we are in trusted domain, set the primary domain
1394 * in start-up mode */
1395 if (!(child->domain->internal)) {
1396 set_domain_online_request(child->domain);
1397 if (!(child->domain->primary)) {
1398 primary_domain->startup = True;
1399 primary_domain->startup_time = time(NULL);
1400 set_domain_online_request(primary_domain);
1406 * We are in idmap child, make sure that we set the
1407 * check_online_event to bring primary domain online.
1409 if (child == idmap_child()) {
1410 set_domain_online_request(primary_domain);
1413 /* We might be in the idmap child...*/
1414 if (child->domain && !(child->domain->internal) &&
1415 lp_winbind_offline_logon()) {
1417 set_domain_online_request(child->domain);
1419 if (primary_domain && (primary_domain != child->domain)) {
1420 /* We need to talk to the primary
1421 * domain as well as the trusted
1422 * domain inside a trusted domain
1423 * child.
1424 * See the code in :
1425 * set_dc_type_and_flags_trustinfo()
1426 * for details.
1428 set_domain_online_request(primary_domain);
1431 child->lockout_policy_event = event_add_timed(
1432 winbind_event_context(), NULL, timeval_zero(),
1433 account_lockout_policy_handler,
1434 child);
1437 if (child->domain && child->domain->primary &&
1438 !USE_KERBEROS_KEYTAB &&
1439 lp_server_role() == ROLE_DOMAIN_MEMBER) {
1441 struct timeval next_change;
1443 if (calculate_next_machine_pwd_change(child->domain->name,
1444 &next_change)) {
1445 child->machine_password_change_event = event_add_timed(
1446 winbind_event_context(), NULL, next_change,
1447 machine_password_change_handler,
1448 child);
1452 while (1) {
1454 int ret;
1455 fd_set r_fds;
1456 fd_set w_fds;
1457 int maxfd;
1458 struct timeval t;
1459 struct timeval *tp;
1460 struct timeval now;
1461 TALLOC_CTX *frame = talloc_stackframe();
1462 struct iovec iov[2];
1463 int iov_count;
1464 NTSTATUS status;
1466 if (run_events(winbind_event_context(), 0, NULL, NULL)) {
1467 TALLOC_FREE(frame);
1468 continue;
1471 GetTimeOfDay(&now);
1473 if (child->domain && child->domain->startup &&
1474 (now.tv_sec > child->domain->startup_time + 30)) {
1475 /* No longer in "startup" mode. */
1476 DEBUG(10,("fork_domain_child: domain %s no longer in 'startup' mode.\n",
1477 child->domain->name ));
1478 child->domain->startup = False;
1481 FD_ZERO(&r_fds);
1482 FD_ZERO(&w_fds);
1483 FD_SET(state.sock, &r_fds);
1484 maxfd = state.sock;
1486 event_add_to_select_args(winbind_event_context(), &now,
1487 &r_fds, &w_fds, &t, &maxfd);
1488 tp = get_timed_events_timeout(winbind_event_context(), &t);
1489 if (tp) {
1490 DEBUG(11,("select will use timeout of %u.%u seconds\n",
1491 (unsigned int)tp->tv_sec, (unsigned int)tp->tv_usec ));
1494 ret = sys_select(maxfd + 1, &r_fds, &w_fds, NULL, tp);
1496 if (run_events(winbind_event_context(), ret, &r_fds, &w_fds)) {
1497 /* We got a signal - continue. */
1498 TALLOC_FREE(frame);
1499 continue;
1502 if (ret == 0) {
1503 DEBUG(11,("nothing is ready yet, continue\n"));
1504 TALLOC_FREE(frame);
1505 continue;
1508 if (ret == -1 && errno == EINTR) {
1509 /* We got a signal - continue. */
1510 TALLOC_FREE(frame);
1511 continue;
1514 if (ret == -1 && errno != EINTR) {
1515 DEBUG(0,("select error occured\n"));
1516 TALLOC_FREE(frame);
1517 perror("select");
1518 _exit(1);
1521 /* fetch a request from the main daemon */
1522 status = child_read_request(&state);
1524 if (!NT_STATUS_IS_OK(status)) {
1525 /* we lost contact with our parent */
1526 _exit(0);
1529 DEBUG(4,("child daemon request %d\n", (int)state.request->cmd));
1531 ZERO_STRUCTP(state.response);
1532 state.request->null_term = '\0';
1533 child_process_request(child, &state);
1535 DEBUG(4, ("Finished processing child request %d\n",
1536 (int)state.request->cmd));
1538 SAFE_FREE(state.request->extra_data.data);
1540 iov[0].iov_base = (void *)state.response;
1541 iov[0].iov_len = sizeof(struct winbindd_response);
1542 iov_count = 1;
1544 if (state.response->length > sizeof(struct winbindd_response)) {
1545 iov[1].iov_base =
1546 (void *)state.response->extra_data.data;
1547 iov[1].iov_len = state.response->length-iov[0].iov_len;
1548 iov_count = 2;
1551 DEBUG(10, ("Writing %d bytes to parent\n",
1552 (int)state.response->length));
1554 if (write_data_iov(state.sock, iov, iov_count) !=
1555 state.response->length) {
1556 DEBUG(0, ("Could not write result\n"));
1557 exit(1);
1559 TALLOC_FREE(frame);