2 Unix SMB/CIFS implementation.
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.
32 #include "../../nsswitch/libwbclient/wbc_async.h"
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)
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",
57 if (state->request->extra_len == 0) {
58 state->request->extra_data.data = NULL;
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",
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,
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);
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);
130 static void wb_child_request_trigger(struct tevent_req *req,
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);
142 subreq = wb_simple_trans_send(state, winbind_event_context(), NULL,
143 state->child->sock, state->request);
144 if (tevent_req_nomem(subreq, req)) {
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);
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);
164 ret = wb_simple_trans_recv(subreq, state, &state->response, &err);
167 tevent_req_error(req, err);
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)) {
182 *presponse = talloc_move(mem_ctx, &state->response);
186 struct wb_domain_request_state {
187 struct tevent_context *ev;
188 struct winbindd_domain *domain;
189 struct winbindd_request *request;
190 struct winbindd_request *init_req;
191 struct winbindd_response *response;
194 static void wb_domain_request_gotdc(struct tevent_req *subreq);
195 static void wb_domain_request_initialized(struct tevent_req *subreq);
196 static void wb_domain_request_done(struct tevent_req *subreq);
198 struct tevent_req *wb_domain_request_send(TALLOC_CTX *mem_ctx,
199 struct tevent_context *ev,
200 struct winbindd_domain *domain,
201 struct winbindd_request *request)
203 struct tevent_req *req, *subreq;
204 struct wb_domain_request_state *state;
206 req = tevent_req_create(mem_ctx, &state,
207 struct wb_domain_request_state);
212 if (domain->initialized) {
213 subreq = wb_child_request_send(state, ev, &domain->child,
215 if (tevent_req_nomem(subreq, req)) {
216 return tevent_req_post(req, ev);
218 tevent_req_set_callback(subreq, wb_domain_request_done, req);
222 state->domain = domain;
224 state->request = request;
226 state->init_req = talloc_zero(state, struct winbindd_request);
227 if (tevent_req_nomem(state->init_req, req)) {
228 return tevent_req_post(req, ev);
231 if (IS_DC || domain->primary || domain->internal) {
232 /* The primary domain has to find the DC name itself */
233 state->init_req->cmd = WINBINDD_INIT_CONNECTION;
234 fstrcpy(state->init_req->domain_name, domain->name);
235 state->init_req->data.init_conn.is_primary =
236 domain->primary ? true : false;
237 fstrcpy(state->init_req->data.init_conn.dcname, "");
239 subreq = wb_child_request_send(state, ev, &domain->child,
241 if (tevent_req_nomem(subreq, req)) {
242 return tevent_req_post(req, ev);
244 tevent_req_set_callback(subreq, wb_domain_request_initialized,
250 * Ask our DC for a DC name
252 domain = find_our_domain();
254 /* This is *not* the primary domain, let's ask our DC about a DC
257 state->init_req->cmd = WINBINDD_GETDCNAME;
258 fstrcpy(state->init_req->domain_name, domain->name);
260 subreq = wb_child_request_send(state, ev, &domain->child, request);
261 if (tevent_req_nomem(subreq, req)) {
262 return tevent_req_post(req, ev);
264 tevent_req_set_callback(subreq, wb_domain_request_gotdc, req);
268 static void wb_domain_request_gotdc(struct tevent_req *subreq)
270 struct tevent_req *req = tevent_req_callback_data(
271 subreq, struct tevent_req);
272 struct wb_domain_request_state *state = tevent_req_data(
273 req, struct wb_domain_request_state);
274 struct winbindd_response *response;
277 ret = wb_child_request_recv(subreq, talloc_tos(), &response, &err);
280 tevent_req_error(req, err);
283 state->init_req->cmd = WINBINDD_INIT_CONNECTION;
284 fstrcpy(state->init_req->domain_name, state->domain->name);
285 state->init_req->data.init_conn.is_primary = False;
286 fstrcpy(state->init_req->data.init_conn.dcname,
287 response->data.dc_name);
289 TALLOC_FREE(response);
291 subreq = wb_child_request_send(state, state->ev, &state->domain->child,
293 if (tevent_req_nomem(subreq, req)) {
296 tevent_req_set_callback(subreq, wb_domain_request_initialized, req);
299 static void wb_domain_request_initialized(struct tevent_req *subreq)
301 struct tevent_req *req = tevent_req_callback_data(
302 subreq, struct tevent_req);
303 struct wb_domain_request_state *state = tevent_req_data(
304 req, struct wb_domain_request_state);
305 struct winbindd_response *response;
308 ret = wb_child_request_recv(subreq, talloc_tos(), &response, &err);
311 tevent_req_error(req, err);
315 if (!string_to_sid(&state->domain->sid,
316 response->data.domain_info.sid)) {
317 DEBUG(1,("init_child_recv: Could not convert sid %s "
318 "from string\n", response->data.domain_info.sid));
319 tevent_req_error(req, EINVAL);
322 fstrcpy(state->domain->name, response->data.domain_info.name);
323 fstrcpy(state->domain->alt_name, response->data.domain_info.alt_name);
324 state->domain->native_mode = response->data.domain_info.native_mode;
325 state->domain->active_directory =
326 response->data.domain_info.active_directory;
327 state->domain->initialized = true;
329 TALLOC_FREE(response);
331 subreq = wb_child_request_send(state, state->ev, &state->domain->child,
333 if (tevent_req_nomem(subreq, req)) {
336 tevent_req_set_callback(subreq, wb_domain_request_done, req);
339 static void wb_domain_request_done(struct tevent_req *subreq)
341 struct tevent_req *req = tevent_req_callback_data(
342 subreq, struct tevent_req);
343 struct wb_domain_request_state *state = tevent_req_data(
344 req, struct wb_domain_request_state);
347 ret = wb_child_request_recv(subreq, talloc_tos(), &state->response,
351 tevent_req_error(req, err);
354 tevent_req_done(req);
357 int wb_domain_request_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
358 struct winbindd_response **presponse, int *err)
360 struct wb_domain_request_state *state = tevent_req_data(
361 req, struct wb_domain_request_state);
363 if (tevent_req_is_unix_error(req, err)) {
366 *presponse = talloc_move(mem_ctx, &state->response);
371 * Machinery for async requests sent to children. You set up a
372 * winbindd_request, select a child to query, and issue a async_request
373 * call. When the request is completed, the callback function you specified is
374 * called back with the private pointer you gave to async_request.
377 struct winbindd_async_request {
378 struct winbindd_async_request *next, *prev;
380 struct winbindd_child *child;
381 struct winbindd_response *response;
382 void (*continuation)(void *private_data, bool success);
383 struct timed_event *reply_timeout_event;
384 pid_t child_pid; /* pid of the child we're waiting on. Used to detect
385 a restart of the child (child->pid != child_pid). */
389 static bool fork_domain_child(struct winbindd_child *child);
390 static void async_request_done(struct tevent_req *req);
392 void async_request(TALLOC_CTX *mem_ctx, struct winbindd_child *child,
393 struct winbindd_request *request,
394 struct winbindd_response *response,
395 void (*continuation)(void *private_data, bool success),
398 struct winbindd_async_request *state;
399 struct tevent_req *req;
401 DEBUG(10, ("Sending request to child pid %d (domain=%s)\n",
403 (child->domain != NULL) ? child->domain->name : "''"));
405 state = talloc(mem_ctx, struct winbindd_async_request);
407 DEBUG(0, ("talloc failed\n"));
408 continuation(private_data, False);
412 state->mem_ctx = mem_ctx;
413 state->child = child;
414 state->reply_timeout_event = NULL;
415 state->response = response;
416 state->continuation = continuation;
417 state->private_data = private_data;
419 request->pid = child->pid;
421 req = wb_child_request_send(state, winbind_event_context(),
424 DEBUG(0, ("wb_child_request_send failed\n"));
425 continuation(private_data, false);
428 tevent_req_set_callback(req, async_request_done, state);
431 static void async_request_done(struct tevent_req *req)
433 struct winbindd_async_request *state = tevent_req_callback_data(
434 req, struct winbindd_async_request);
435 struct winbindd_response *response;
438 ret = wb_child_request_recv(req, state, &response, &err);
441 DEBUG(2, ("wb_child_request_recv failed: %s\n",
443 state->continuation(state->private_data, false);
446 *state->response = *response;
447 state->continuation(state->private_data, true);
450 struct domain_request_state {
451 struct winbindd_domain *domain;
452 struct winbindd_request *request;
453 struct winbindd_response *response;
454 void (*continuation)(void *private_data_data, bool success);
455 void *private_data_data;
458 static void async_domain_request_done(struct tevent_req *req);
460 void async_domain_request(TALLOC_CTX *mem_ctx,
461 struct winbindd_domain *domain,
462 struct winbindd_request *request,
463 struct winbindd_response *response,
464 void (*continuation)(void *private_data_data, bool success),
465 void *private_data_data)
467 struct tevent_req *subreq;
468 struct domain_request_state *state;
470 state = TALLOC_P(mem_ctx, struct domain_request_state);
472 DEBUG(0, ("talloc failed\n"));
473 continuation(private_data_data, False);
477 state->domain = domain;
478 state->request = request;
479 state->response = response;
480 state->continuation = continuation;
481 state->private_data_data = private_data_data;
483 subreq = wb_domain_request_send(state, winbind_event_context(),
485 if (subreq == NULL) {
486 DEBUG(5, ("wb_domain_request_send failed\n"));
487 continuation(private_data_data, false);
490 tevent_req_set_callback(subreq, async_domain_request_done, state);
493 static void async_domain_request_done(struct tevent_req *req)
495 struct domain_request_state *state = tevent_req_callback_data(
496 req, struct domain_request_state);
497 struct winbindd_response *response;
500 ret = wb_domain_request_recv(req, state, &response, &err);
503 DEBUG(5, ("wb_domain_request returned %s\n", strerror(err)));
504 state->continuation(state->private_data_data, false);
507 *(state->response) = *response;
508 state->continuation(state->private_data_data, true);
511 static void recvfrom_child(void *private_data_data, bool success)
513 struct winbindd_cli_state *state =
514 talloc_get_type_abort(private_data_data, struct winbindd_cli_state);
515 enum winbindd_result result = state->response->result;
517 /* This is an optimization: The child has written directly to the
518 * response buffer. The request itself is still in pending state,
519 * state that in the result code. */
521 state->response->result = WINBINDD_PENDING;
523 if ((!success) || (result != WINBINDD_OK)) {
524 request_error(state);
531 void sendto_child(struct winbindd_cli_state *state,
532 struct winbindd_child *child)
534 async_request(state->mem_ctx, child, state->request,
535 state->response, recvfrom_child, state);
538 void sendto_domain(struct winbindd_cli_state *state,
539 struct winbindd_domain *domain)
541 async_domain_request(state->mem_ctx, domain,
542 state->request, state->response,
543 recvfrom_child, state);
546 static void child_process_request(struct winbindd_child *child,
547 struct winbindd_cli_state *state)
549 struct winbindd_domain *domain = child->domain;
550 const struct winbindd_child_dispatch_table *table = child->table;
552 /* Free response data - we may be interrupted and receive another
553 command before being able to send this data off. */
555 state->response->result = WINBINDD_ERROR;
556 state->response->length = sizeof(struct winbindd_response);
558 /* as all requests in the child are sync, we can use talloc_tos() */
559 state->mem_ctx = talloc_tos();
561 /* Process command */
563 for (; table->name; table++) {
564 if (state->request->cmd == table->struct_cmd) {
565 DEBUG(10,("child_process_request: request fn %s\n",
567 state->response->result = table->struct_fn(domain, state);
572 DEBUG(1 ,("child_process_request: unknown request fn number %d\n",
573 (int)state->request->cmd));
574 state->response->result = WINBINDD_ERROR;
577 void setup_child(struct winbindd_child *child,
578 const struct winbindd_child_dispatch_table *table,
579 const char *logprefix,
582 if (logprefix && logname) {
583 if (asprintf(&child->logfilename, "%s/%s-%s",
584 get_dyn_LOGFILEBASE(), logprefix, logname) < 0) {
585 smb_panic("Internal error: asprintf failed");
588 smb_panic("Internal error: logprefix == NULL && "
592 child->domain = NULL;
593 child->table = table;
594 child->queue = tevent_queue_create(NULL, "winbind_child");
595 SMB_ASSERT(child->queue != NULL);
598 struct winbindd_child *children = NULL;
600 void winbind_child_died(pid_t pid)
602 struct winbindd_child *child;
604 for (child = children; child != NULL; child = child->next) {
605 if (child->pid == pid) {
611 DEBUG(5, ("Already reaped child %u died\n", (unsigned int)pid));
615 /* This will be re-added in fork_domain_child() */
617 DLIST_REMOVE(children, child);
624 /* Ensure any negative cache entries with the netbios or realm names are removed. */
626 void winbindd_flush_negative_conn_cache(struct winbindd_domain *domain)
628 flush_negative_conn_cache_for_domain(domain->name);
629 if (*domain->alt_name) {
630 flush_negative_conn_cache_for_domain(domain->alt_name);
635 * Parent winbindd process sets its own debug level first and then
636 * sends a message to all the winbindd children to adjust their debug
637 * level to that of parents.
640 void winbind_msg_debug(struct messaging_context *msg_ctx,
643 struct server_id server_id,
646 struct winbindd_child *child;
648 DEBUG(10,("winbind_msg_debug: got debug message.\n"));
650 debug_message(msg_ctx, private_data, MSG_DEBUG, server_id, data);
652 for (child = children; child != NULL; child = child->next) {
654 DEBUG(10,("winbind_msg_debug: sending message to pid %u.\n",
655 (unsigned int)child->pid));
657 messaging_send_buf(msg_ctx, pid_to_procid(child->pid),
660 strlen((char *) data->data) + 1);
664 /* Set our domains as offline and forward the offline message to our children. */
666 void winbind_msg_offline(struct messaging_context *msg_ctx,
669 struct server_id server_id,
672 struct winbindd_child *child;
673 struct winbindd_domain *domain;
675 DEBUG(10,("winbind_msg_offline: got offline message.\n"));
677 if (!lp_winbind_offline_logon()) {
678 DEBUG(10,("winbind_msg_offline: rejecting offline message.\n"));
682 /* Set our global state as offline. */
683 if (!set_global_winbindd_state_offline()) {
684 DEBUG(10,("winbind_msg_offline: offline request failed.\n"));
688 /* Set all our domains as offline. */
689 for (domain = domain_list(); domain; domain = domain->next) {
690 if (domain->internal) {
693 DEBUG(5,("winbind_msg_offline: marking %s offline.\n", domain->name));
694 set_domain_offline(domain);
697 for (child = children; child != NULL; child = child->next) {
698 /* Don't send message to internal childs. We've already
700 if (!child->domain || winbindd_internal_child(child)) {
704 /* Or internal domains (this should not be possible....) */
705 if (child->domain->internal) {
709 /* Each winbindd child should only process requests for one domain - make sure
710 we only set it online / offline for that domain. */
712 DEBUG(10,("winbind_msg_offline: sending message to pid %u for domain %s.\n",
713 (unsigned int)child->pid, domain->name ));
715 messaging_send_buf(msg_ctx, pid_to_procid(child->pid),
717 (uint8 *)child->domain->name,
718 strlen(child->domain->name)+1);
722 /* Set our domains as online and forward the online message to our children. */
724 void winbind_msg_online(struct messaging_context *msg_ctx,
727 struct server_id server_id,
730 struct winbindd_child *child;
731 struct winbindd_domain *domain;
733 DEBUG(10,("winbind_msg_online: got online message.\n"));
735 if (!lp_winbind_offline_logon()) {
736 DEBUG(10,("winbind_msg_online: rejecting online message.\n"));
740 /* Set our global state as online. */
741 set_global_winbindd_state_online();
743 smb_nscd_flush_user_cache();
744 smb_nscd_flush_group_cache();
746 /* Set all our domains as online. */
747 for (domain = domain_list(); domain; domain = domain->next) {
748 if (domain->internal) {
751 DEBUG(5,("winbind_msg_online: requesting %s to go online.\n", domain->name));
753 winbindd_flush_negative_conn_cache(domain);
754 set_domain_online_request(domain);
756 /* Send an online message to the idmap child when our
757 primary domain comes back online */
759 if ( domain->primary ) {
760 struct winbindd_child *idmap = idmap_child();
762 if ( idmap->pid != 0 ) {
763 messaging_send_buf(msg_ctx,
764 pid_to_procid(idmap->pid),
766 (uint8 *)domain->name,
767 strlen(domain->name)+1);
772 for (child = children; child != NULL; child = child->next) {
773 /* Don't send message to internal childs. */
774 if (!child->domain || winbindd_internal_child(child)) {
778 /* Or internal domains (this should not be possible....) */
779 if (child->domain->internal) {
783 /* Each winbindd child should only process requests for one domain - make sure
784 we only set it online / offline for that domain. */
786 DEBUG(10,("winbind_msg_online: sending message to pid %u for domain %s.\n",
787 (unsigned int)child->pid, child->domain->name ));
789 messaging_send_buf(msg_ctx, pid_to_procid(child->pid),
791 (uint8 *)child->domain->name,
792 strlen(child->domain->name)+1);
796 static const char *collect_onlinestatus(TALLOC_CTX *mem_ctx)
798 struct winbindd_domain *domain;
801 if ((buf = talloc_asprintf(mem_ctx, "global:%s ",
802 get_global_winbindd_state_offline() ?
803 "Offline":"Online")) == NULL) {
807 for (domain = domain_list(); domain; domain = domain->next) {
808 if ((buf = talloc_asprintf_append_buffer(buf, "%s:%s ",
811 "Online":"Offline")) == NULL) {
816 buf = talloc_asprintf_append_buffer(buf, "\n");
818 DEBUG(5,("collect_onlinestatus: %s", buf));
823 void winbind_msg_onlinestatus(struct messaging_context *msg_ctx,
826 struct server_id server_id,
831 struct server_id *sender;
833 DEBUG(5,("winbind_msg_onlinestatus received.\n"));
839 sender = (struct server_id *)data->data;
841 mem_ctx = talloc_init("winbind_msg_onlinestatus");
842 if (mem_ctx == NULL) {
846 message = collect_onlinestatus(mem_ctx);
847 if (message == NULL) {
848 talloc_destroy(mem_ctx);
852 messaging_send_buf(msg_ctx, *sender, MSG_WINBIND_ONLINESTATUS,
853 (uint8 *)message, strlen(message) + 1);
855 talloc_destroy(mem_ctx);
858 void winbind_msg_dump_event_list(struct messaging_context *msg_ctx,
861 struct server_id server_id,
864 struct winbindd_child *child;
866 DEBUG(10,("winbind_msg_dump_event_list received\n"));
868 dump_event_list(winbind_event_context());
870 for (child = children; child != NULL; child = child->next) {
872 DEBUG(10,("winbind_msg_dump_event_list: sending message to pid %u\n",
873 (unsigned int)child->pid));
875 messaging_send_buf(msg_ctx, pid_to_procid(child->pid),
882 void winbind_msg_dump_domain_list(struct messaging_context *msg_ctx,
885 struct server_id server_id,
889 const char *message = NULL;
890 struct server_id *sender = NULL;
891 const char *domain = NULL;
894 struct winbindd_domain *dom = NULL;
896 DEBUG(5,("winbind_msg_dump_domain_list received.\n"));
898 if (!data || !data->data) {
902 if (data->length < sizeof(struct server_id)) {
906 mem_ctx = talloc_init("winbind_msg_dump_domain_list");
911 sender = (struct server_id *)data->data;
912 if (data->length > sizeof(struct server_id)) {
913 domain = (const char *)data->data+sizeof(struct server_id);
918 DEBUG(5,("winbind_msg_dump_domain_list for domain: %s\n",
921 message = NDR_PRINT_STRUCT_STRING(mem_ctx, winbindd_domain,
922 find_domain_from_name_noinit(domain));
924 talloc_destroy(mem_ctx);
928 messaging_send_buf(msg_ctx, *sender,
929 MSG_WINBIND_DUMP_DOMAIN_LIST,
930 (uint8_t *)message, strlen(message) + 1);
932 talloc_destroy(mem_ctx);
937 DEBUG(5,("winbind_msg_dump_domain_list all domains\n"));
939 for (dom = domain_list(); dom; dom=dom->next) {
940 message = NDR_PRINT_STRUCT_STRING(mem_ctx, winbindd_domain, dom);
942 talloc_destroy(mem_ctx);
946 s = talloc_asprintf_append(s, "%s\n", message);
948 talloc_destroy(mem_ctx);
953 status = messaging_send_buf(msg_ctx, *sender,
954 MSG_WINBIND_DUMP_DOMAIN_LIST,
955 (uint8_t *)s, strlen(s) + 1);
956 if (!NT_STATUS_IS_OK(status)) {
957 DEBUG(0,("failed to send message: %s\n",
961 talloc_destroy(mem_ctx);
964 static void account_lockout_policy_handler(struct event_context *ctx,
965 struct timed_event *te,
969 struct winbindd_child *child =
970 (struct winbindd_child *)private_data;
971 TALLOC_CTX *mem_ctx = NULL;
972 struct winbindd_methods *methods;
973 struct samr_DomInfo12 lockout_policy;
976 DEBUG(10,("account_lockout_policy_handler called\n"));
978 TALLOC_FREE(child->lockout_policy_event);
980 if ( !winbindd_can_contact_domain( child->domain ) ) {
981 DEBUG(10,("account_lockout_policy_handler: Removing myself since I "
982 "do not have an incoming trust to domain %s\n",
983 child->domain->name));
988 methods = child->domain->methods;
990 mem_ctx = talloc_init("account_lockout_policy_handler ctx");
992 result = NT_STATUS_NO_MEMORY;
994 result = methods->lockout_policy(child->domain, mem_ctx, &lockout_policy);
996 TALLOC_FREE(mem_ctx);
998 if (!NT_STATUS_IS_OK(result)) {
999 DEBUG(10,("account_lockout_policy_handler: lockout_policy failed error %s\n",
1000 nt_errstr(result)));
1003 child->lockout_policy_event = event_add_timed(winbind_event_context(), NULL,
1004 timeval_current_ofs(3600, 0),
1005 account_lockout_policy_handler,
1009 static time_t get_machine_password_timeout(void)
1011 /* until we have gpo support use lp setting */
1012 return lp_machine_password_timeout();
1015 static bool calculate_next_machine_pwd_change(const char *domain,
1018 time_t pass_last_set_time;
1023 pw = secrets_fetch_machine_password(domain,
1024 &pass_last_set_time,
1028 DEBUG(0,("cannot fetch own machine password ????"));
1034 timeout = get_machine_password_timeout();
1036 DEBUG(10,("machine password never expires\n"));
1040 if (time(NULL) < (pass_last_set_time + timeout)) {
1041 next_change = pass_last_set_time + timeout;
1042 DEBUG(10,("machine password still valid until: %s\n",
1043 http_timestring(talloc_tos(), next_change)));
1044 *t = timeval_set(next_change, 0);
1048 DEBUG(10,("machine password expired, needs immediate change\n"));
1050 *t = timeval_zero();
1055 static void machine_password_change_handler(struct event_context *ctx,
1056 struct timed_event *te,
1060 struct winbindd_child *child =
1061 (struct winbindd_child *)private_data;
1062 struct rpc_pipe_client *netlogon_pipe = NULL;
1065 struct timeval next_change;
1067 DEBUG(10,("machine_password_change_handler called\n"));
1069 TALLOC_FREE(child->machine_password_change_event);
1071 if (!calculate_next_machine_pwd_change(child->domain->name,
1076 if (!winbindd_can_contact_domain(child->domain)) {
1077 DEBUG(10,("machine_password_change_handler: Removing myself since I "
1078 "do not have an incoming trust to domain %s\n",
1079 child->domain->name));
1083 result = cm_connect_netlogon(child->domain, &netlogon_pipe);
1084 if (!NT_STATUS_IS_OK(result)) {
1085 DEBUG(10,("machine_password_change_handler: "
1086 "failed to connect netlogon pipe: %s\n",
1087 nt_errstr(result)));
1091 frame = talloc_stackframe();
1093 result = trust_pw_find_change_and_store_it(netlogon_pipe,
1095 child->domain->name);
1098 if (!NT_STATUS_IS_OK(result)) {
1099 DEBUG(10,("machine_password_change_handler: "
1100 "failed to change machine password: %s\n",
1101 nt_errstr(result)));
1103 DEBUG(10,("machine_password_change_handler: "
1104 "successfully changed machine password\n"));
1107 child->machine_password_change_event = event_add_timed(winbind_event_context(), NULL,
1109 machine_password_change_handler,
1113 /* Deal with a request to go offline. */
1115 static void child_msg_offline(struct messaging_context *msg,
1118 struct server_id server_id,
1121 struct winbindd_domain *domain;
1122 struct winbindd_domain *primary_domain = NULL;
1123 const char *domainname = (const char *)data->data;
1125 if (data->data == NULL || data->length == 0) {
1129 DEBUG(5,("child_msg_offline received for domain %s.\n", domainname));
1131 if (!lp_winbind_offline_logon()) {
1132 DEBUG(10,("child_msg_offline: rejecting offline message.\n"));
1136 primary_domain = find_our_domain();
1138 /* Mark the requested domain offline. */
1140 for (domain = domain_list(); domain; domain = domain->next) {
1141 if (domain->internal) {
1144 if (strequal(domain->name, domainname)) {
1145 DEBUG(5,("child_msg_offline: marking %s offline.\n", domain->name));
1146 set_domain_offline(domain);
1147 /* we are in the trusted domain, set the primary domain
1149 if (domain != primary_domain) {
1150 set_domain_offline(primary_domain);
1156 /* Deal with a request to go online. */
1158 static void child_msg_online(struct messaging_context *msg,
1161 struct server_id server_id,
1164 struct winbindd_domain *domain;
1165 struct winbindd_domain *primary_domain = NULL;
1166 const char *domainname = (const char *)data->data;
1168 if (data->data == NULL || data->length == 0) {
1172 DEBUG(5,("child_msg_online received for domain %s.\n", domainname));
1174 if (!lp_winbind_offline_logon()) {
1175 DEBUG(10,("child_msg_online: rejecting online message.\n"));
1179 primary_domain = find_our_domain();
1181 /* Set our global state as online. */
1182 set_global_winbindd_state_online();
1184 /* Try and mark everything online - delete any negative cache entries
1185 to force a reconnect now. */
1187 for (domain = domain_list(); domain; domain = domain->next) {
1188 if (domain->internal) {
1191 if (strequal(domain->name, domainname)) {
1192 DEBUG(5,("child_msg_online: requesting %s to go online.\n", domain->name));
1193 winbindd_flush_negative_conn_cache(domain);
1194 set_domain_online_request(domain);
1196 /* we can be in trusted domain, which will contact primary domain
1197 * we have to bring primary domain online in trusted domain process
1198 * see, winbindd_dual_pam_auth() --> winbindd_dual_pam_auth_samlogon()
1199 * --> contact_domain = find_our_domain()
1201 if (domain != primary_domain) {
1202 winbindd_flush_negative_conn_cache(primary_domain);
1203 set_domain_online_request(primary_domain);
1209 static void child_msg_dump_event_list(struct messaging_context *msg,
1212 struct server_id server_id,
1215 DEBUG(5,("child_msg_dump_event_list received\n"));
1217 dump_event_list(winbind_event_context());
1220 bool winbindd_reinit_after_fork(const char *logfilename)
1222 struct winbindd_domain *domain;
1223 struct winbindd_child *cl;
1225 if (!NT_STATUS_IS_OK(reinit_after_fork(winbind_messaging_context(),
1226 winbind_event_context(),
1228 DEBUG(0,("reinit_after_fork() failed\n"));
1232 close_conns_after_fork();
1234 if (!override_logfile && logfilename) {
1235 lp_set_logfile(logfilename);
1239 if (!winbindd_setup_sig_term_handler(false))
1241 if (!winbindd_setup_sig_hup_handler(override_logfile ? NULL :
1245 /* Don't handle the same messages as our parent. */
1246 messaging_deregister(winbind_messaging_context(),
1247 MSG_SMB_CONF_UPDATED, NULL);
1248 messaging_deregister(winbind_messaging_context(),
1249 MSG_SHUTDOWN, NULL);
1250 messaging_deregister(winbind_messaging_context(),
1251 MSG_WINBIND_OFFLINE, NULL);
1252 messaging_deregister(winbind_messaging_context(),
1253 MSG_WINBIND_ONLINE, NULL);
1254 messaging_deregister(winbind_messaging_context(),
1255 MSG_WINBIND_ONLINESTATUS, NULL);
1256 messaging_deregister(winbind_messaging_context(),
1257 MSG_DUMP_EVENT_LIST, NULL);
1258 messaging_deregister(winbind_messaging_context(),
1259 MSG_WINBIND_DUMP_DOMAIN_LIST, NULL);
1260 messaging_deregister(winbind_messaging_context(),
1263 /* We have destroyed all events in the winbindd_event_context
1264 * in reinit_after_fork(), so clean out all possible pending
1265 * event pointers. */
1267 /* Deal with check_online_events. */
1269 for (domain = domain_list(); domain; domain = domain->next) {
1270 TALLOC_FREE(domain->check_online_event);
1273 /* Ensure we're not handling a credential cache event inherited
1274 * from our parent. */
1276 ccache_remove_all_after_fork();
1278 /* Destroy all possible events in child list. */
1279 for (cl = children; cl != NULL; cl = cl->next) {
1280 TALLOC_FREE(cl->lockout_policy_event);
1281 TALLOC_FREE(cl->machine_password_change_event);
1283 /* Children should never be able to send
1284 * each other messages, all messages must
1285 * go through the parent.
1290 * This is a little tricky, children must not
1291 * send an MSG_WINBIND_ONLINE message to idmap_child().
1292 * If we are in a child of our primary domain or
1293 * in the process created by fork_child_dc_connect(),
1294 * and the primary domain cannot go online,
1295 * fork_child_dc_connection() sends MSG_WINBIND_ONLINE
1296 * periodically to idmap_child().
1298 * The sequence is, fork_child_dc_connect() ---> getdcs() --->
1299 * get_dc_name_via_netlogon() ---> cm_connect_netlogon()
1300 * ---> init_dc_connection() ---> cm_open_connection --->
1301 * set_domain_online(), sends MSG_WINBIND_ONLINE to
1302 * idmap_child(). Disallow children sending messages
1303 * to each other, all messages must go through the parent.
1311 static bool fork_domain_child(struct winbindd_child *child)
1314 struct winbindd_cli_state state;
1315 struct winbindd_request request;
1316 struct winbindd_response response;
1317 struct winbindd_domain *primary_domain = NULL;
1319 if (child->domain) {
1320 DEBUG(10, ("fork_domain_child called for domain '%s'\n",
1321 child->domain->name));
1323 DEBUG(10, ("fork_domain_child called without domain.\n"));
1326 if (socketpair(AF_UNIX, SOCK_STREAM, 0, fdpair) != 0) {
1327 DEBUG(0, ("Could not open child pipe: %s\n",
1333 state.pid = sys_getpid();
1334 state.request = &request;
1335 state.response = &response;
1337 child->pid = sys_fork();
1339 if (child->pid == -1) {
1340 DEBUG(0, ("Could not fork: %s\n", strerror(errno)));
1344 if (child->pid != 0) {
1347 child->next = child->prev = NULL;
1348 DLIST_ADD(children, child);
1349 child->sock = fdpair[1];
1355 DEBUG(10, ("Child process %d\n", (int)sys_getpid()));
1357 /* Stop zombies in children */
1360 state.sock = fdpair[0];
1363 if (!winbindd_reinit_after_fork(child->logfilename)) {
1367 /* Handle online/offline messages. */
1368 messaging_register(winbind_messaging_context(), NULL,
1369 MSG_WINBIND_OFFLINE, child_msg_offline);
1370 messaging_register(winbind_messaging_context(), NULL,
1371 MSG_WINBIND_ONLINE, child_msg_online);
1372 messaging_register(winbind_messaging_context(), NULL,
1373 MSG_DUMP_EVENT_LIST, child_msg_dump_event_list);
1374 messaging_register(winbind_messaging_context(), NULL,
1375 MSG_DEBUG, debug_message);
1377 primary_domain = find_our_domain();
1379 if (primary_domain == NULL) {
1380 smb_panic("no primary domain found");
1383 /* It doesn't matter if we allow cache login,
1384 * try to bring domain online after fork. */
1385 if ( child->domain ) {
1386 child->domain->startup = True;
1387 child->domain->startup_time = time(NULL);
1388 /* we can be in primary domain or in trusted domain
1389 * If we are in trusted domain, set the primary domain
1390 * in start-up mode */
1391 if (!(child->domain->internal)) {
1392 set_domain_online_request(child->domain);
1393 if (!(child->domain->primary)) {
1394 primary_domain->startup = True;
1395 primary_domain->startup_time = time(NULL);
1396 set_domain_online_request(primary_domain);
1402 * We are in idmap child, make sure that we set the
1403 * check_online_event to bring primary domain online.
1405 if (child == idmap_child()) {
1406 set_domain_online_request(primary_domain);
1409 /* We might be in the idmap child...*/
1410 if (child->domain && !(child->domain->internal) &&
1411 lp_winbind_offline_logon()) {
1413 set_domain_online_request(child->domain);
1415 if (primary_domain && (primary_domain != child->domain)) {
1416 /* We need to talk to the primary
1417 * domain as well as the trusted
1418 * domain inside a trusted domain
1421 * set_dc_type_and_flags_trustinfo()
1424 set_domain_online_request(primary_domain);
1427 child->lockout_policy_event = event_add_timed(
1428 winbind_event_context(), NULL, timeval_zero(),
1429 account_lockout_policy_handler,
1433 if (child->domain && child->domain->primary &&
1434 !USE_KERBEROS_KEYTAB &&
1435 lp_server_role() == ROLE_DOMAIN_MEMBER) {
1437 struct timeval next_change;
1439 if (calculate_next_machine_pwd_change(child->domain->name,
1441 child->machine_password_change_event = event_add_timed(
1442 winbind_event_context(), NULL, next_change,
1443 machine_password_change_handler,
1457 TALLOC_CTX *frame = talloc_stackframe();
1458 struct iovec iov[2];
1462 if (run_events(winbind_event_context(), 0, NULL, NULL)) {
1469 if (child->domain && child->domain->startup &&
1470 (now.tv_sec > child->domain->startup_time + 30)) {
1471 /* No longer in "startup" mode. */
1472 DEBUG(10,("fork_domain_child: domain %s no longer in 'startup' mode.\n",
1473 child->domain->name ));
1474 child->domain->startup = False;
1479 FD_SET(state.sock, &r_fds);
1482 event_add_to_select_args(winbind_event_context(), &now,
1483 &r_fds, &w_fds, &t, &maxfd);
1484 tp = get_timed_events_timeout(winbind_event_context(), &t);
1486 DEBUG(11,("select will use timeout of %u.%u seconds\n",
1487 (unsigned int)tp->tv_sec, (unsigned int)tp->tv_usec ));
1490 ret = sys_select(maxfd + 1, &r_fds, &w_fds, NULL, tp);
1492 if (run_events(winbind_event_context(), ret, &r_fds, &w_fds)) {
1493 /* We got a signal - continue. */
1499 DEBUG(11,("nothing is ready yet, continue\n"));
1504 if (ret == -1 && errno == EINTR) {
1505 /* We got a signal - continue. */
1510 if (ret == -1 && errno != EINTR) {
1511 DEBUG(0,("select error occured\n"));
1517 /* fetch a request from the main daemon */
1518 status = child_read_request(&state);
1520 if (!NT_STATUS_IS_OK(status)) {
1521 /* we lost contact with our parent */
1525 DEBUG(4,("child daemon request %d\n", (int)state.request->cmd));
1527 ZERO_STRUCTP(state.response);
1528 state.request->null_term = '\0';
1529 child_process_request(child, &state);
1531 DEBUG(4, ("Finished processing child request %d\n",
1532 (int)state.request->cmd));
1534 SAFE_FREE(state.request->extra_data.data);
1536 iov[0].iov_base = (void *)state.response;
1537 iov[0].iov_len = sizeof(struct winbindd_response);
1540 if (state.response->length > sizeof(struct winbindd_response)) {
1542 (void *)state.response->extra_data.data;
1543 iov[1].iov_len = state.response->length-iov[0].iov_len;
1547 DEBUG(10, ("Writing %d bytes to parent\n",
1548 (int)state.response->length));
1550 if (write_data_iov(state.sock, iov, iov_count) !=
1551 state.response->length) {
1552 DEBUG(0, ("Could not write result\n"));