Convert async_domain_request to wb_domain_request_send
[Samba.git] / source3 / winbindd / winbindd_dual.c
blob85286588321101659cefd8f85163aef2070bcecf
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 void 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 state->finished = True;
55 return;
58 if (state->request->extra_len == 0) {
59 state->request->extra_data.data = NULL;
60 return;
63 DEBUG(10, ("Need to read %d extra bytes\n", (int)state->request->extra_len));
65 state->request->extra_data.data =
66 SMB_MALLOC_ARRAY(char, state->request->extra_len + 1);
68 if (state->request->extra_data.data == NULL) {
69 DEBUG(0, ("malloc failed\n"));
70 state->finished = True;
71 return;
74 /* Ensure null termination */
75 state->request->extra_data.data[state->request->extra_len] = '\0';
77 status= read_data(state->sock, state->request->extra_data.data,
78 state->request->extra_len);
80 if (!NT_STATUS_IS_OK(status)) {
81 DEBUG(0, ("Could not read extra data: %s\n",
82 nt_errstr(status)));
83 state->finished = True;
84 return;
89 * Do winbind child async request. This is not simply wb_simple_trans. We have
90 * to do the queueing ourselves because while a request is queued, the child
91 * might have crashed, and we have to re-fork it in the _trigger function.
94 struct wb_child_request_state {
95 struct tevent_context *ev;
96 struct winbindd_child *child;
97 struct winbindd_request *request;
98 struct winbindd_response *response;
101 static bool fork_domain_child(struct winbindd_child *child);
103 static void wb_child_request_trigger(struct tevent_req *req,
104 void *private_data);
105 static void wb_child_request_done(struct tevent_req *subreq);
107 struct tevent_req *wb_child_request_send(TALLOC_CTX *mem_ctx,
108 struct tevent_context *ev,
109 struct winbindd_child *child,
110 struct winbindd_request *request)
112 struct tevent_req *req;
113 struct wb_child_request_state *state;
115 req = tevent_req_create(mem_ctx, &state,
116 struct wb_child_request_state);
117 if (req == NULL) {
118 return NULL;
121 state->ev = ev;
122 state->child = child;
123 state->request = request;
125 if (!tevent_queue_add(child->queue, ev, req,
126 wb_child_request_trigger, NULL)) {
127 tevent_req_nomem(NULL, req);
128 return tevent_req_post(req, ev);
130 return req;
133 static void wb_child_request_trigger(struct tevent_req *req,
134 void *private_data)
136 struct wb_child_request_state *state = tevent_req_data(
137 req, struct wb_child_request_state);
138 struct tevent_req *subreq;
140 if ((state->child->pid == 0) && (!fork_domain_child(state->child))) {
141 tevent_req_error(req, errno);
142 return;
145 subreq = wb_simple_trans_send(state, winbind_event_context(), NULL,
146 state->child->sock, state->request);
147 if (tevent_req_nomem(subreq, req)) {
148 return;
150 tevent_req_set_callback(subreq, wb_child_request_done, req);
152 if (!tevent_req_set_endtime(req, state->ev,
153 timeval_current_ofs(300, 0))) {
154 tevent_req_nomem(NULL, req);
155 return;
159 static void wb_child_request_done(struct tevent_req *subreq)
161 struct tevent_req *req = tevent_req_callback_data(
162 subreq, struct tevent_req);
163 struct wb_child_request_state *state = tevent_req_data(
164 req, struct wb_child_request_state);
165 int ret, err;
167 ret = wb_simple_trans_recv(subreq, state, &state->response, &err);
168 TALLOC_FREE(subreq);
169 if (ret == -1) {
170 tevent_req_error(req, err);
171 return;
173 tevent_req_done(req);
176 int wb_child_request_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
177 struct winbindd_response **presponse, int *err)
179 struct wb_child_request_state *state = tevent_req_data(
180 req, struct wb_child_request_state);
182 if (tevent_req_is_unix_error(req, err)) {
183 return -1;
185 if (state->response->result != WINBINDD_OK) {
186 *err = EIO; /* EIO doesn't fit, but what would be better? */
187 return -1;
189 *presponse = talloc_move(mem_ctx, &state->response);
190 return 0;
193 struct wb_domain_request_state {
194 struct tevent_context *ev;
195 struct winbindd_domain *domain;
196 struct winbindd_request *request;
197 struct winbindd_request *init_req;
198 struct winbindd_response *response;
201 static void wb_domain_request_gotdc(struct tevent_req *subreq);
202 static void wb_domain_request_initialized(struct tevent_req *subreq);
203 static void wb_domain_request_done(struct tevent_req *subreq);
205 struct tevent_req *wb_domain_request_send(TALLOC_CTX *mem_ctx,
206 struct tevent_context *ev,
207 struct winbindd_domain *domain,
208 struct winbindd_request *request)
210 struct tevent_req *req, *subreq;
211 struct wb_domain_request_state *state;
213 req = tevent_req_create(mem_ctx, &state,
214 struct wb_domain_request_state);
215 if (req == NULL) {
216 return NULL;
219 if (domain->initialized) {
220 subreq = wb_child_request_send(state, ev, &domain->child,
221 request);
222 if (tevent_req_nomem(subreq, req)) {
223 return tevent_req_post(req, ev);
225 tevent_req_set_callback(subreq, wb_domain_request_done, req);
226 return req;
229 state->domain = domain;
230 state->ev = ev;
231 state->request = request;
233 state->init_req = talloc_zero(state, struct winbindd_request);
234 if (tevent_req_nomem(state->init_req, req)) {
235 return tevent_req_post(req, ev);
238 if (IS_DC || domain->primary || domain->internal) {
239 /* The primary domain has to find the DC name itself */
240 state->init_req->cmd = WINBINDD_INIT_CONNECTION;
241 fstrcpy(state->init_req->domain_name, domain->name);
242 state->init_req->data.init_conn.is_primary =
243 domain->primary ? true : false;
244 fstrcpy(state->init_req->data.init_conn.dcname, "");
246 subreq = wb_child_request_send(state, ev, &domain->child,
247 state->init_req);
248 if (tevent_req_nomem(subreq, req)) {
249 return tevent_req_post(req, ev);
251 tevent_req_set_callback(subreq, wb_domain_request_initialized,
252 req);
253 return req;
257 * Ask our DC for a DC name
259 domain = find_our_domain();
261 /* This is *not* the primary domain, let's ask our DC about a DC
262 * name */
264 state->init_req->cmd = WINBINDD_GETDCNAME;
265 fstrcpy(state->init_req->domain_name, domain->name);
267 subreq = wb_child_request_send(state, ev, &domain->child, request);
268 if (tevent_req_nomem(subreq, req)) {
269 return tevent_req_post(req, ev);
271 tevent_req_set_callback(subreq, wb_domain_request_gotdc, req);
272 return req;
275 static void wb_domain_request_gotdc(struct tevent_req *subreq)
277 struct tevent_req *req = tevent_req_callback_data(
278 subreq, struct tevent_req);
279 struct wb_domain_request_state *state = tevent_req_data(
280 req, struct wb_domain_request_state);
281 struct winbindd_response *response;
282 int ret, err;
284 ret = wb_child_request_recv(subreq, talloc_tos(), &response, &err);
285 TALLOC_FREE(subreq);
286 if (ret == -1) {
287 tevent_req_error(req, err);
288 return;
290 state->init_req->cmd = WINBINDD_INIT_CONNECTION;
291 fstrcpy(state->init_req->domain_name, state->domain->name);
292 state->init_req->data.init_conn.is_primary = False;
293 fstrcpy(state->init_req->data.init_conn.dcname,
294 response->data.dc_name);
296 TALLOC_FREE(response);
298 subreq = wb_child_request_send(state, state->ev, &state->domain->child,
299 state->init_req);
300 if (tevent_req_nomem(subreq, req)) {
301 return;
303 tevent_req_set_callback(subreq, wb_domain_request_initialized, req);
306 static void wb_domain_request_initialized(struct tevent_req *subreq)
308 struct tevent_req *req = tevent_req_callback_data(
309 subreq, struct tevent_req);
310 struct wb_domain_request_state *state = tevent_req_data(
311 req, struct wb_domain_request_state);
312 struct winbindd_response *response;
313 int ret, err;
315 ret = wb_child_request_recv(subreq, talloc_tos(), &response, &err);
316 TALLOC_FREE(subreq);
317 if (ret == -1) {
318 tevent_req_error(req, err);
319 return;
322 if (!string_to_sid(&state->domain->sid,
323 response->data.domain_info.sid)) {
324 DEBUG(1,("init_child_recv: Could not convert sid %s "
325 "from string\n", response->data.domain_info.sid));
326 tevent_req_error(req, EINVAL);
327 return;
329 fstrcpy(state->domain->name, response->data.domain_info.name);
330 fstrcpy(state->domain->alt_name, response->data.domain_info.alt_name);
331 state->domain->native_mode = response->data.domain_info.native_mode;
332 state->domain->active_directory =
333 response->data.domain_info.active_directory;
334 state->domain->initialized = true;
336 TALLOC_FREE(response);
338 subreq = wb_child_request_send(state, state->ev, &state->domain->child,
339 state->request);
340 if (tevent_req_nomem(subreq, req)) {
341 return;
343 tevent_req_set_callback(subreq, wb_domain_request_done, req);
346 static void wb_domain_request_done(struct tevent_req *subreq)
348 struct tevent_req *req = tevent_req_callback_data(
349 subreq, struct tevent_req);
350 struct wb_domain_request_state *state = tevent_req_data(
351 req, struct wb_domain_request_state);
352 int ret, err;
354 ret = wb_child_request_recv(subreq, talloc_tos(), &state->response,
355 &err);
356 TALLOC_FREE(subreq);
357 if (ret == -1) {
358 tevent_req_error(req, err);
359 return;
361 tevent_req_done(req);
364 int wb_domain_request_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
365 struct winbindd_response **presponse, int *err)
367 struct wb_domain_request_state *state = tevent_req_data(
368 req, struct wb_domain_request_state);
370 if (tevent_req_is_unix_error(req, err)) {
371 return -1;
373 *presponse = talloc_move(mem_ctx, &state->response);
374 return 0;
378 * Machinery for async requests sent to children. You set up a
379 * winbindd_request, select a child to query, and issue a async_request
380 * call. When the request is completed, the callback function you specified is
381 * called back with the private pointer you gave to async_request.
384 struct winbindd_async_request {
385 struct winbindd_async_request *next, *prev;
386 TALLOC_CTX *mem_ctx;
387 struct winbindd_child *child;
388 struct winbindd_response *response;
389 void (*continuation)(void *private_data, bool success);
390 struct timed_event *reply_timeout_event;
391 pid_t child_pid; /* pid of the child we're waiting on. Used to detect
392 a restart of the child (child->pid != child_pid). */
393 void *private_data;
396 static bool fork_domain_child(struct winbindd_child *child);
397 static void async_request_done(struct tevent_req *req);
399 void async_request(TALLOC_CTX *mem_ctx, struct winbindd_child *child,
400 struct winbindd_request *request,
401 struct winbindd_response *response,
402 void (*continuation)(void *private_data, bool success),
403 void *private_data)
405 struct winbindd_async_request *state;
406 struct tevent_req *req;
408 DEBUG(10, ("Sending request to child pid %d (domain=%s)\n",
409 (int)child->pid,
410 (child->domain != NULL) ? child->domain->name : "''"));
412 state = talloc(mem_ctx, struct winbindd_async_request);
413 if (state == NULL) {
414 DEBUG(0, ("talloc failed\n"));
415 continuation(private_data, False);
416 return;
419 state->mem_ctx = mem_ctx;
420 state->child = child;
421 state->reply_timeout_event = NULL;
422 state->response = response;
423 state->continuation = continuation;
424 state->private_data = private_data;
426 request->pid = child->pid;
428 req = wb_child_request_send(state, winbind_event_context(),
429 child, request);
430 if (req == NULL) {
431 DEBUG(0, ("wb_child_request_send failed\n"));
432 continuation(private_data, false);
433 return;
435 tevent_req_set_callback(req, async_request_done, state);
438 static void async_request_done(struct tevent_req *req)
440 struct winbindd_async_request *state = tevent_req_callback_data(
441 req, struct winbindd_async_request);
442 struct winbindd_response *response;
443 int ret, err;
445 ret = wb_child_request_recv(req, state, &response, &err);
446 TALLOC_FREE(req);
447 if (ret == -1) {
448 DEBUG(2, ("wb_child_request_recv failed: %s\n",
449 strerror(err)));
450 state->continuation(state->private_data, false);
451 return;
453 *state->response = *response;
454 state->continuation(state->private_data, true);
457 struct domain_request_state {
458 struct winbindd_domain *domain;
459 struct winbindd_request *request;
460 struct winbindd_response *response;
461 void (*continuation)(void *private_data_data, bool success);
462 void *private_data_data;
465 static void async_domain_request_done(struct tevent_req *req);
467 void async_domain_request(TALLOC_CTX *mem_ctx,
468 struct winbindd_domain *domain,
469 struct winbindd_request *request,
470 struct winbindd_response *response,
471 void (*continuation)(void *private_data_data, bool success),
472 void *private_data_data)
474 struct tevent_req *subreq;
475 struct domain_request_state *state;
477 state = TALLOC_P(mem_ctx, struct domain_request_state);
478 if (state == NULL) {
479 DEBUG(0, ("talloc failed\n"));
480 continuation(private_data_data, False);
481 return;
484 state->domain = domain;
485 state->request = request;
486 state->response = response;
487 state->continuation = continuation;
488 state->private_data_data = private_data_data;
490 subreq = wb_domain_request_send(state, winbind_event_context(),
491 domain, request);
492 if (subreq == NULL) {
493 DEBUG(5, ("wb_domain_request_send failed\n"));
494 continuation(private_data_data, false);
495 return;
497 tevent_req_set_callback(subreq, async_domain_request_done, state);
500 static void async_domain_request_done(struct tevent_req *req)
502 struct domain_request_state *state = tevent_req_callback_data(
503 req, struct domain_request_state);
504 struct winbindd_response *response;
505 int ret, err;
507 ret = wb_domain_request_recv(req, state, &response, &err);
508 TALLOC_FREE(req);
509 if (ret == -1) {
510 DEBUG(5, ("wb_domain_request returned %s\n", strerror(errno)));
511 state->continuation(state->private_data_data, false);
512 return;
514 *(state->response) = *response;
515 state->continuation(state->private_data_data, true);
518 static void recvfrom_child(void *private_data_data, bool success)
520 struct winbindd_cli_state *state =
521 talloc_get_type_abort(private_data_data, struct winbindd_cli_state);
522 enum winbindd_result result = state->response.result;
524 /* This is an optimization: The child has written directly to the
525 * response buffer. The request itself is still in pending state,
526 * state that in the result code. */
528 state->response.result = WINBINDD_PENDING;
530 if ((!success) || (result != WINBINDD_OK)) {
531 request_error(state);
532 return;
535 request_ok(state);
538 void sendto_child(struct winbindd_cli_state *state,
539 struct winbindd_child *child)
541 async_request(state->mem_ctx, child, state->request,
542 &state->response, recvfrom_child, state);
545 void sendto_domain(struct winbindd_cli_state *state,
546 struct winbindd_domain *domain)
548 async_domain_request(state->mem_ctx, domain,
549 state->request, &state->response,
550 recvfrom_child, state);
553 static void child_process_request(struct winbindd_child *child,
554 struct winbindd_cli_state *state)
556 struct winbindd_domain *domain = child->domain;
557 const struct winbindd_child_dispatch_table *table = child->table;
559 /* Free response data - we may be interrupted and receive another
560 command before being able to send this data off. */
562 state->response.result = WINBINDD_ERROR;
563 state->response.length = sizeof(struct winbindd_response);
565 /* as all requests in the child are sync, we can use talloc_tos() */
566 state->mem_ctx = talloc_tos();
568 /* Process command */
570 for (; table->name; table++) {
571 if (state->request->cmd == table->struct_cmd) {
572 DEBUG(10,("child_process_request: request fn %s\n",
573 table->name));
574 state->response.result = table->struct_fn(domain, state);
575 return;
579 DEBUG(1 ,("child_process_request: unknown request fn number %d\n",
580 (int)state->request->cmd));
581 state->response.result = WINBINDD_ERROR;
584 void setup_child(struct winbindd_child *child,
585 const struct winbindd_child_dispatch_table *table,
586 const char *logprefix,
587 const char *logname)
589 if (logprefix && logname) {
590 if (asprintf(&child->logfilename, "%s/%s-%s",
591 get_dyn_LOGFILEBASE(), logprefix, logname) < 0) {
592 smb_panic("Internal error: asprintf failed");
594 } else {
595 smb_panic("Internal error: logprefix == NULL && "
596 "logname == NULL");
599 child->domain = NULL;
600 child->table = table;
601 child->queue = tevent_queue_create(NULL, "winbind_child");
602 SMB_ASSERT(child->queue != NULL);
605 struct winbindd_child *children = NULL;
607 void winbind_child_died(pid_t pid)
609 struct winbindd_child *child;
611 for (child = children; child != NULL; child = child->next) {
612 if (child->pid == pid) {
613 break;
617 if (child == NULL) {
618 DEBUG(5, ("Already reaped child %u died\n", (unsigned int)pid));
619 return;
622 /* This will be re-added in fork_domain_child() */
624 DLIST_REMOVE(children, child);
626 close(child->sock);
627 child->sock = -1;
628 child->pid = 0;
631 /* Ensure any negative cache entries with the netbios or realm names are removed. */
633 void winbindd_flush_negative_conn_cache(struct winbindd_domain *domain)
635 flush_negative_conn_cache_for_domain(domain->name);
636 if (*domain->alt_name) {
637 flush_negative_conn_cache_for_domain(domain->alt_name);
642 * Parent winbindd process sets its own debug level first and then
643 * sends a message to all the winbindd children to adjust their debug
644 * level to that of parents.
647 void winbind_msg_debug(struct messaging_context *msg_ctx,
648 void *private_data,
649 uint32_t msg_type,
650 struct server_id server_id,
651 DATA_BLOB *data)
653 struct winbindd_child *child;
655 DEBUG(10,("winbind_msg_debug: got debug message.\n"));
657 debug_message(msg_ctx, private_data, MSG_DEBUG, server_id, data);
659 for (child = children; child != NULL; child = child->next) {
661 DEBUG(10,("winbind_msg_debug: sending message to pid %u.\n",
662 (unsigned int)child->pid));
664 messaging_send_buf(msg_ctx, pid_to_procid(child->pid),
665 MSG_DEBUG,
666 data->data,
667 strlen((char *) data->data) + 1);
671 /* Set our domains as offline and forward the offline message to our children. */
673 void winbind_msg_offline(struct messaging_context *msg_ctx,
674 void *private_data,
675 uint32_t msg_type,
676 struct server_id server_id,
677 DATA_BLOB *data)
679 struct winbindd_child *child;
680 struct winbindd_domain *domain;
682 DEBUG(10,("winbind_msg_offline: got offline message.\n"));
684 if (!lp_winbind_offline_logon()) {
685 DEBUG(10,("winbind_msg_offline: rejecting offline message.\n"));
686 return;
689 /* Set our global state as offline. */
690 if (!set_global_winbindd_state_offline()) {
691 DEBUG(10,("winbind_msg_offline: offline request failed.\n"));
692 return;
695 /* Set all our domains as offline. */
696 for (domain = domain_list(); domain; domain = domain->next) {
697 if (domain->internal) {
698 continue;
700 DEBUG(5,("winbind_msg_offline: marking %s offline.\n", domain->name));
701 set_domain_offline(domain);
704 for (child = children; child != NULL; child = child->next) {
705 /* Don't send message to internal childs. We've already
706 done so above. */
707 if (!child->domain || winbindd_internal_child(child)) {
708 continue;
711 /* Or internal domains (this should not be possible....) */
712 if (child->domain->internal) {
713 continue;
716 /* Each winbindd child should only process requests for one domain - make sure
717 we only set it online / offline for that domain. */
719 DEBUG(10,("winbind_msg_offline: sending message to pid %u for domain %s.\n",
720 (unsigned int)child->pid, domain->name ));
722 messaging_send_buf(msg_ctx, pid_to_procid(child->pid),
723 MSG_WINBIND_OFFLINE,
724 (uint8 *)child->domain->name,
725 strlen(child->domain->name)+1);
729 /* Set our domains as online and forward the online message to our children. */
731 void winbind_msg_online(struct messaging_context *msg_ctx,
732 void *private_data,
733 uint32_t msg_type,
734 struct server_id server_id,
735 DATA_BLOB *data)
737 struct winbindd_child *child;
738 struct winbindd_domain *domain;
740 DEBUG(10,("winbind_msg_online: got online message.\n"));
742 if (!lp_winbind_offline_logon()) {
743 DEBUG(10,("winbind_msg_online: rejecting online message.\n"));
744 return;
747 /* Set our global state as online. */
748 set_global_winbindd_state_online();
750 smb_nscd_flush_user_cache();
751 smb_nscd_flush_group_cache();
753 /* Set all our domains as online. */
754 for (domain = domain_list(); domain; domain = domain->next) {
755 if (domain->internal) {
756 continue;
758 DEBUG(5,("winbind_msg_online: requesting %s to go online.\n", domain->name));
760 winbindd_flush_negative_conn_cache(domain);
761 set_domain_online_request(domain);
763 /* Send an online message to the idmap child when our
764 primary domain comes back online */
766 if ( domain->primary ) {
767 struct winbindd_child *idmap = idmap_child();
769 if ( idmap->pid != 0 ) {
770 messaging_send_buf(msg_ctx,
771 pid_to_procid(idmap->pid),
772 MSG_WINBIND_ONLINE,
773 (uint8 *)domain->name,
774 strlen(domain->name)+1);
779 for (child = children; child != NULL; child = child->next) {
780 /* Don't send message to internal childs. */
781 if (!child->domain || winbindd_internal_child(child)) {
782 continue;
785 /* Or internal domains (this should not be possible....) */
786 if (child->domain->internal) {
787 continue;
790 /* Each winbindd child should only process requests for one domain - make sure
791 we only set it online / offline for that domain. */
793 DEBUG(10,("winbind_msg_online: sending message to pid %u for domain %s.\n",
794 (unsigned int)child->pid, child->domain->name ));
796 messaging_send_buf(msg_ctx, pid_to_procid(child->pid),
797 MSG_WINBIND_ONLINE,
798 (uint8 *)child->domain->name,
799 strlen(child->domain->name)+1);
803 static const char *collect_onlinestatus(TALLOC_CTX *mem_ctx)
805 struct winbindd_domain *domain;
806 char *buf = NULL;
808 if ((buf = talloc_asprintf(mem_ctx, "global:%s ",
809 get_global_winbindd_state_offline() ?
810 "Offline":"Online")) == NULL) {
811 return NULL;
814 for (domain = domain_list(); domain; domain = domain->next) {
815 if ((buf = talloc_asprintf_append_buffer(buf, "%s:%s ",
816 domain->name,
817 domain->online ?
818 "Online":"Offline")) == NULL) {
819 return NULL;
823 buf = talloc_asprintf_append_buffer(buf, "\n");
825 DEBUG(5,("collect_onlinestatus: %s", buf));
827 return buf;
830 void winbind_msg_onlinestatus(struct messaging_context *msg_ctx,
831 void *private_data,
832 uint32_t msg_type,
833 struct server_id server_id,
834 DATA_BLOB *data)
836 TALLOC_CTX *mem_ctx;
837 const char *message;
838 struct server_id *sender;
840 DEBUG(5,("winbind_msg_onlinestatus received.\n"));
842 if (!data->data) {
843 return;
846 sender = (struct server_id *)data->data;
848 mem_ctx = talloc_init("winbind_msg_onlinestatus");
849 if (mem_ctx == NULL) {
850 return;
853 message = collect_onlinestatus(mem_ctx);
854 if (message == NULL) {
855 talloc_destroy(mem_ctx);
856 return;
859 messaging_send_buf(msg_ctx, *sender, MSG_WINBIND_ONLINESTATUS,
860 (uint8 *)message, strlen(message) + 1);
862 talloc_destroy(mem_ctx);
865 void winbind_msg_dump_event_list(struct messaging_context *msg_ctx,
866 void *private_data,
867 uint32_t msg_type,
868 struct server_id server_id,
869 DATA_BLOB *data)
871 struct winbindd_child *child;
873 DEBUG(10,("winbind_msg_dump_event_list received\n"));
875 dump_event_list(winbind_event_context());
877 for (child = children; child != NULL; child = child->next) {
879 DEBUG(10,("winbind_msg_dump_event_list: sending message to pid %u\n",
880 (unsigned int)child->pid));
882 messaging_send_buf(msg_ctx, pid_to_procid(child->pid),
883 MSG_DUMP_EVENT_LIST,
884 NULL, 0);
889 void winbind_msg_dump_domain_list(struct messaging_context *msg_ctx,
890 void *private_data,
891 uint32_t msg_type,
892 struct server_id server_id,
893 DATA_BLOB *data)
895 TALLOC_CTX *mem_ctx;
896 const char *message = NULL;
897 struct server_id *sender = NULL;
898 const char *domain = NULL;
899 char *s = NULL;
900 NTSTATUS status;
901 struct winbindd_domain *dom = NULL;
903 DEBUG(5,("winbind_msg_dump_domain_list received.\n"));
905 if (!data || !data->data) {
906 return;
909 if (data->length < sizeof(struct server_id)) {
910 return;
913 mem_ctx = talloc_init("winbind_msg_dump_domain_list");
914 if (!mem_ctx) {
915 return;
918 sender = (struct server_id *)data->data;
919 if (data->length > sizeof(struct server_id)) {
920 domain = (const char *)data->data+sizeof(struct server_id);
923 if (domain) {
925 DEBUG(5,("winbind_msg_dump_domain_list for domain: %s\n",
926 domain));
928 message = NDR_PRINT_STRUCT_STRING(mem_ctx, winbindd_domain,
929 find_domain_from_name_noinit(domain));
930 if (!message) {
931 talloc_destroy(mem_ctx);
932 return;
935 messaging_send_buf(msg_ctx, *sender,
936 MSG_WINBIND_DUMP_DOMAIN_LIST,
937 (uint8_t *)message, strlen(message) + 1);
939 talloc_destroy(mem_ctx);
941 return;
944 DEBUG(5,("winbind_msg_dump_domain_list all domains\n"));
946 for (dom = domain_list(); dom; dom=dom->next) {
947 message = NDR_PRINT_STRUCT_STRING(mem_ctx, winbindd_domain, dom);
948 if (!message) {
949 talloc_destroy(mem_ctx);
950 return;
953 s = talloc_asprintf_append(s, "%s\n", message);
954 if (!s) {
955 talloc_destroy(mem_ctx);
956 return;
960 status = messaging_send_buf(msg_ctx, *sender,
961 MSG_WINBIND_DUMP_DOMAIN_LIST,
962 (uint8_t *)s, strlen(s) + 1);
963 if (!NT_STATUS_IS_OK(status)) {
964 DEBUG(0,("failed to send message: %s\n",
965 nt_errstr(status)));
968 talloc_destroy(mem_ctx);
971 static void account_lockout_policy_handler(struct event_context *ctx,
972 struct timed_event *te,
973 struct timeval now,
974 void *private_data)
976 struct winbindd_child *child =
977 (struct winbindd_child *)private_data;
978 TALLOC_CTX *mem_ctx = NULL;
979 struct winbindd_methods *methods;
980 struct samr_DomInfo12 lockout_policy;
981 NTSTATUS result;
983 DEBUG(10,("account_lockout_policy_handler called\n"));
985 TALLOC_FREE(child->lockout_policy_event);
987 if ( !winbindd_can_contact_domain( child->domain ) ) {
988 DEBUG(10,("account_lockout_policy_handler: Removing myself since I "
989 "do not have an incoming trust to domain %s\n",
990 child->domain->name));
992 return;
995 methods = child->domain->methods;
997 mem_ctx = talloc_init("account_lockout_policy_handler ctx");
998 if (!mem_ctx) {
999 result = NT_STATUS_NO_MEMORY;
1000 } else {
1001 result = methods->lockout_policy(child->domain, mem_ctx, &lockout_policy);
1003 TALLOC_FREE(mem_ctx);
1005 if (!NT_STATUS_IS_OK(result)) {
1006 DEBUG(10,("account_lockout_policy_handler: lockout_policy failed error %s\n",
1007 nt_errstr(result)));
1010 child->lockout_policy_event = event_add_timed(winbind_event_context(), NULL,
1011 timeval_current_ofs(3600, 0),
1012 account_lockout_policy_handler,
1013 child);
1016 static time_t get_machine_password_timeout(void)
1018 /* until we have gpo support use lp setting */
1019 return lp_machine_password_timeout();
1022 static bool calculate_next_machine_pwd_change(const char *domain,
1023 struct timeval *t)
1025 time_t pass_last_set_time;
1026 time_t timeout;
1027 time_t next_change;
1028 char *pw;
1030 pw = secrets_fetch_machine_password(domain,
1031 &pass_last_set_time,
1032 NULL);
1034 if (pw == NULL) {
1035 DEBUG(0,("cannot fetch own machine password ????"));
1036 return false;
1039 SAFE_FREE(pw);
1041 timeout = get_machine_password_timeout();
1042 if (timeout == 0) {
1043 DEBUG(10,("machine password never expires\n"));
1044 return false;
1047 if (time(NULL) < (pass_last_set_time + timeout)) {
1048 next_change = pass_last_set_time + timeout;
1049 DEBUG(10,("machine password still valid until: %s\n",
1050 http_timestring(talloc_tos(), next_change)));
1051 *t = timeval_set(next_change, 0);
1052 return true;
1055 DEBUG(10,("machine password expired, needs immediate change\n"));
1057 *t = timeval_zero();
1059 return true;
1062 static void machine_password_change_handler(struct event_context *ctx,
1063 struct timed_event *te,
1064 struct timeval now,
1065 void *private_data)
1067 struct winbindd_child *child =
1068 (struct winbindd_child *)private_data;
1069 struct rpc_pipe_client *netlogon_pipe = NULL;
1070 TALLOC_CTX *frame;
1071 NTSTATUS result;
1072 struct timeval next_change;
1074 DEBUG(10,("machine_password_change_handler called\n"));
1076 TALLOC_FREE(child->machine_password_change_event);
1078 if (!calculate_next_machine_pwd_change(child->domain->name,
1079 &next_change)) {
1080 return;
1083 if (!winbindd_can_contact_domain(child->domain)) {
1084 DEBUG(10,("machine_password_change_handler: Removing myself since I "
1085 "do not have an incoming trust to domain %s\n",
1086 child->domain->name));
1087 return;
1090 result = cm_connect_netlogon(child->domain, &netlogon_pipe);
1091 if (!NT_STATUS_IS_OK(result)) {
1092 DEBUG(10,("machine_password_change_handler: "
1093 "failed to connect netlogon pipe: %s\n",
1094 nt_errstr(result)));
1095 return;
1098 frame = talloc_stackframe();
1100 result = trust_pw_find_change_and_store_it(netlogon_pipe,
1101 frame,
1102 child->domain->name);
1103 TALLOC_FREE(frame);
1105 if (!NT_STATUS_IS_OK(result)) {
1106 DEBUG(10,("machine_password_change_handler: "
1107 "failed to change machine password: %s\n",
1108 nt_errstr(result)));
1109 } else {
1110 DEBUG(10,("machine_password_change_handler: "
1111 "successfully changed machine password\n"));
1114 child->machine_password_change_event = event_add_timed(winbind_event_context(), NULL,
1115 next_change,
1116 machine_password_change_handler,
1117 child);
1120 /* Deal with a request to go offline. */
1122 static void child_msg_offline(struct messaging_context *msg,
1123 void *private_data,
1124 uint32_t msg_type,
1125 struct server_id server_id,
1126 DATA_BLOB *data)
1128 struct winbindd_domain *domain;
1129 struct winbindd_domain *primary_domain = NULL;
1130 const char *domainname = (const char *)data->data;
1132 if (data->data == NULL || data->length == 0) {
1133 return;
1136 DEBUG(5,("child_msg_offline received for domain %s.\n", domainname));
1138 if (!lp_winbind_offline_logon()) {
1139 DEBUG(10,("child_msg_offline: rejecting offline message.\n"));
1140 return;
1143 primary_domain = find_our_domain();
1145 /* Mark the requested domain offline. */
1147 for (domain = domain_list(); domain; domain = domain->next) {
1148 if (domain->internal) {
1149 continue;
1151 if (strequal(domain->name, domainname)) {
1152 DEBUG(5,("child_msg_offline: marking %s offline.\n", domain->name));
1153 set_domain_offline(domain);
1154 /* we are in the trusted domain, set the primary domain
1155 * offline too */
1156 if (domain != primary_domain) {
1157 set_domain_offline(primary_domain);
1163 /* Deal with a request to go online. */
1165 static void child_msg_online(struct messaging_context *msg,
1166 void *private_data,
1167 uint32_t msg_type,
1168 struct server_id server_id,
1169 DATA_BLOB *data)
1171 struct winbindd_domain *domain;
1172 struct winbindd_domain *primary_domain = NULL;
1173 const char *domainname = (const char *)data->data;
1175 if (data->data == NULL || data->length == 0) {
1176 return;
1179 DEBUG(5,("child_msg_online received for domain %s.\n", domainname));
1181 if (!lp_winbind_offline_logon()) {
1182 DEBUG(10,("child_msg_online: rejecting online message.\n"));
1183 return;
1186 primary_domain = find_our_domain();
1188 /* Set our global state as online. */
1189 set_global_winbindd_state_online();
1191 /* Try and mark everything online - delete any negative cache entries
1192 to force a reconnect now. */
1194 for (domain = domain_list(); domain; domain = domain->next) {
1195 if (domain->internal) {
1196 continue;
1198 if (strequal(domain->name, domainname)) {
1199 DEBUG(5,("child_msg_online: requesting %s to go online.\n", domain->name));
1200 winbindd_flush_negative_conn_cache(domain);
1201 set_domain_online_request(domain);
1203 /* we can be in trusted domain, which will contact primary domain
1204 * we have to bring primary domain online in trusted domain process
1205 * see, winbindd_dual_pam_auth() --> winbindd_dual_pam_auth_samlogon()
1206 * --> contact_domain = find_our_domain()
1207 * */
1208 if (domain != primary_domain) {
1209 winbindd_flush_negative_conn_cache(primary_domain);
1210 set_domain_online_request(primary_domain);
1216 static void child_msg_dump_event_list(struct messaging_context *msg,
1217 void *private_data,
1218 uint32_t msg_type,
1219 struct server_id server_id,
1220 DATA_BLOB *data)
1222 DEBUG(5,("child_msg_dump_event_list received\n"));
1224 dump_event_list(winbind_event_context());
1227 bool winbindd_reinit_after_fork(const char *logfilename)
1229 struct winbindd_domain *domain;
1230 struct winbindd_child *cl;
1232 if (!NT_STATUS_IS_OK(reinit_after_fork(winbind_messaging_context(),
1233 winbind_event_context(),
1234 true))) {
1235 DEBUG(0,("reinit_after_fork() failed\n"));
1236 return false;
1239 close_conns_after_fork();
1241 if (!override_logfile && logfilename) {
1242 lp_set_logfile(logfilename);
1243 reopen_logs();
1246 if (!winbindd_setup_sig_term_handler(false))
1247 return false;
1248 if (!winbindd_setup_sig_hup_handler(override_logfile ? NULL :
1249 logfilename))
1250 return false;
1252 /* Don't handle the same messages as our parent. */
1253 messaging_deregister(winbind_messaging_context(),
1254 MSG_SMB_CONF_UPDATED, NULL);
1255 messaging_deregister(winbind_messaging_context(),
1256 MSG_SHUTDOWN, NULL);
1257 messaging_deregister(winbind_messaging_context(),
1258 MSG_WINBIND_OFFLINE, NULL);
1259 messaging_deregister(winbind_messaging_context(),
1260 MSG_WINBIND_ONLINE, NULL);
1261 messaging_deregister(winbind_messaging_context(),
1262 MSG_WINBIND_ONLINESTATUS, NULL);
1263 messaging_deregister(winbind_messaging_context(),
1264 MSG_DUMP_EVENT_LIST, NULL);
1265 messaging_deregister(winbind_messaging_context(),
1266 MSG_WINBIND_DUMP_DOMAIN_LIST, NULL);
1267 messaging_deregister(winbind_messaging_context(),
1268 MSG_DEBUG, NULL);
1270 /* We have destroyed all events in the winbindd_event_context
1271 * in reinit_after_fork(), so clean out all possible pending
1272 * event pointers. */
1274 /* Deal with check_online_events. */
1276 for (domain = domain_list(); domain; domain = domain->next) {
1277 TALLOC_FREE(domain->check_online_event);
1280 /* Ensure we're not handling a credential cache event inherited
1281 * from our parent. */
1283 ccache_remove_all_after_fork();
1285 /* Destroy all possible events in child list. */
1286 for (cl = children; cl != NULL; cl = cl->next) {
1287 TALLOC_FREE(cl->lockout_policy_event);
1288 TALLOC_FREE(cl->machine_password_change_event);
1290 /* Children should never be able to send
1291 * each other messages, all messages must
1292 * go through the parent.
1294 cl->pid = (pid_t)0;
1297 * This is a little tricky, children must not
1298 * send an MSG_WINBIND_ONLINE message to idmap_child().
1299 * If we are in a child of our primary domain or
1300 * in the process created by fork_child_dc_connect(),
1301 * and the primary domain cannot go online,
1302 * fork_child_dc_connection() sends MSG_WINBIND_ONLINE
1303 * periodically to idmap_child().
1305 * The sequence is, fork_child_dc_connect() ---> getdcs() --->
1306 * get_dc_name_via_netlogon() ---> cm_connect_netlogon()
1307 * ---> init_dc_connection() ---> cm_open_connection --->
1308 * set_domain_online(), sends MSG_WINBIND_ONLINE to
1309 * idmap_child(). Disallow children sending messages
1310 * to each other, all messages must go through the parent.
1312 cl = idmap_child();
1313 cl->pid = (pid_t)0;
1315 return true;
1318 static bool fork_domain_child(struct winbindd_child *child)
1320 int fdpair[2];
1321 struct winbindd_cli_state state;
1322 struct winbindd_domain *primary_domain = NULL;
1324 if (child->domain) {
1325 DEBUG(10, ("fork_domain_child called for domain '%s'\n",
1326 child->domain->name));
1327 } else {
1328 DEBUG(10, ("fork_domain_child called without domain.\n"));
1331 if (socketpair(AF_UNIX, SOCK_STREAM, 0, fdpair) != 0) {
1332 DEBUG(0, ("Could not open child pipe: %s\n",
1333 strerror(errno)));
1334 return False;
1337 ZERO_STRUCT(state);
1338 state.pid = sys_getpid();
1339 state.request = &state._request;
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;
1465 if (run_events(winbind_event_context(), 0, NULL, NULL)) {
1466 TALLOC_FREE(frame);
1467 continue;
1470 GetTimeOfDay(&now);
1472 if (child->domain && child->domain->startup &&
1473 (now.tv_sec > child->domain->startup_time + 30)) {
1474 /* No longer in "startup" mode. */
1475 DEBUG(10,("fork_domain_child: domain %s no longer in 'startup' mode.\n",
1476 child->domain->name ));
1477 child->domain->startup = False;
1480 FD_ZERO(&r_fds);
1481 FD_ZERO(&w_fds);
1482 FD_SET(state.sock, &r_fds);
1483 maxfd = state.sock;
1485 event_add_to_select_args(winbind_event_context(), &now,
1486 &r_fds, &w_fds, &t, &maxfd);
1487 tp = get_timed_events_timeout(winbind_event_context(), &t);
1488 if (tp) {
1489 DEBUG(11,("select will use timeout of %u.%u seconds\n",
1490 (unsigned int)tp->tv_sec, (unsigned int)tp->tv_usec ));
1493 ret = sys_select(maxfd + 1, &r_fds, &w_fds, NULL, tp);
1495 if (run_events(winbind_event_context(), ret, &r_fds, &w_fds)) {
1496 /* We got a signal - continue. */
1497 TALLOC_FREE(frame);
1498 continue;
1501 if (ret == 0) {
1502 DEBUG(11,("nothing is ready yet, continue\n"));
1503 TALLOC_FREE(frame);
1504 continue;
1507 if (ret == -1 && errno == EINTR) {
1508 /* We got a signal - continue. */
1509 TALLOC_FREE(frame);
1510 continue;
1513 if (ret == -1 && errno != EINTR) {
1514 DEBUG(0,("select error occured\n"));
1515 TALLOC_FREE(frame);
1516 perror("select");
1517 _exit(1);
1520 /* fetch a request from the main daemon */
1521 child_read_request(&state);
1523 if (state.finished) {
1524 /* we lost contact with our parent */
1525 _exit(0);
1528 DEBUG(4,("child daemon request %d\n", (int)state.request->cmd));
1530 ZERO_STRUCT(state.response);
1531 state.request->null_term = '\0';
1532 child_process_request(child, &state);
1534 DEBUG(4, ("Finished processing child request %d\n",
1535 (int)state.request->cmd));
1537 SAFE_FREE(state.request->extra_data.data);
1539 iov[0].iov_base = (void *)&state.response;
1540 iov[0].iov_len = sizeof(struct winbindd_response);
1541 iov_count = 1;
1543 if (state.response.length > sizeof(struct winbindd_response)) {
1544 iov[1].iov_base =
1545 (void *)state.response.extra_data.data;
1546 iov[1].iov_len = state.response.length-iov[0].iov_len;
1547 iov_count = 2;
1550 DEBUG(10, ("Writing %d bytes to parent\n",
1551 (int)state.response.length));
1553 if (write_data_iov(state.sock, iov, iov_count) !=
1554 state.response.length) {
1555 DEBUG(0, ("Could not write result\n"));
1556 exit(1);
1558 TALLOC_FREE(frame);