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 void 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",
54 state->finished = True;
58 if (state->request->extra_len == 0) {
59 state->request->extra_data.data = NULL;
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;
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",
83 state->finished = True;
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,
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);
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);
133 static void wb_child_request_trigger(struct tevent_req *req,
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);
145 subreq = wb_simple_trans_send(state, winbind_event_context(), NULL,
146 state->child->sock, state->request);
147 if (tevent_req_nomem(subreq, req)) {
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);
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);
167 ret = wb_simple_trans_recv(subreq, state, &state->response, &err);
170 tevent_req_error(req, err);
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)) {
185 if (state->response->result != WINBINDD_OK) {
186 *err = EIO; /* EIO doesn't fit, but what would be better? */
189 *presponse = talloc_move(mem_ctx, &state->response);
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);
219 if (domain->initialized) {
220 subreq = wb_child_request_send(state, ev, &domain->child,
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);
229 state->domain = domain;
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,
248 if (tevent_req_nomem(subreq, req)) {
249 return tevent_req_post(req, ev);
251 tevent_req_set_callback(subreq, wb_domain_request_initialized,
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
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);
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;
284 ret = wb_child_request_recv(subreq, talloc_tos(), &response, &err);
287 tevent_req_error(req, err);
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,
300 if (tevent_req_nomem(subreq, req)) {
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;
315 ret = wb_child_request_recv(subreq, talloc_tos(), &response, &err);
318 tevent_req_error(req, err);
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);
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,
340 if (tevent_req_nomem(subreq, req)) {
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);
354 ret = wb_child_request_recv(subreq, talloc_tos(), &state->response,
358 tevent_req_error(req, err);
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)) {
373 *presponse = talloc_move(mem_ctx, &state->response);
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;
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). */
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),
405 struct winbindd_async_request *state;
406 struct tevent_req *req;
408 DEBUG(10, ("Sending request to child pid %d (domain=%s)\n",
410 (child->domain != NULL) ? child->domain->name : "''"));
412 state = talloc(mem_ctx, struct winbindd_async_request);
414 DEBUG(0, ("talloc failed\n"));
415 continuation(private_data, False);
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(),
431 DEBUG(0, ("wb_child_request_send failed\n"));
432 continuation(private_data, false);
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;
445 ret = wb_child_request_recv(req, state, &response, &err);
448 DEBUG(2, ("wb_child_request_recv failed: %s\n",
450 state->continuation(state->private_data, false);
453 *state->response = *response;
454 state->continuation(state->private_data, true);
457 struct domain_request_state {
459 struct winbindd_domain *domain;
460 struct winbindd_request *request;
461 struct winbindd_response *response;
462 void (*continuation)(void *private_data_data, bool success);
463 void *private_data_data;
466 static void domain_init_recv(void *private_data_data, bool success);
468 void async_domain_request(TALLOC_CTX *mem_ctx,
469 struct winbindd_domain *domain,
470 struct winbindd_request *request,
471 struct winbindd_response *response,
472 void (*continuation)(void *private_data_data, bool success),
473 void *private_data_data)
475 struct domain_request_state *state;
477 if (domain->initialized) {
478 async_request(mem_ctx, &domain->child, request, response,
479 continuation, private_data_data);
483 state = TALLOC_P(mem_ctx, struct domain_request_state);
485 DEBUG(0, ("talloc failed\n"));
486 continuation(private_data_data, False);
490 state->mem_ctx = mem_ctx;
491 state->domain = domain;
492 state->request = request;
493 state->response = response;
494 state->continuation = continuation;
495 state->private_data_data = private_data_data;
497 init_child_connection(domain, domain_init_recv, state);
500 static void domain_init_recv(void *private_data_data, bool success)
502 struct domain_request_state *state =
503 talloc_get_type_abort(private_data_data, struct domain_request_state);
506 DEBUG(5, ("Domain init returned an error\n"));
507 state->continuation(state->private_data_data, False);
511 async_request(state->mem_ctx, &state->domain->child,
512 state->request, state->response,
513 state->continuation, state->private_data_data);
516 static void recvfrom_child(void *private_data_data, bool success)
518 struct winbindd_cli_state *state =
519 talloc_get_type_abort(private_data_data, struct winbindd_cli_state);
520 enum winbindd_result result = state->response.result;
522 /* This is an optimization: The child has written directly to the
523 * response buffer. The request itself is still in pending state,
524 * state that in the result code. */
526 state->response.result = WINBINDD_PENDING;
528 if ((!success) || (result != WINBINDD_OK)) {
529 request_error(state);
536 void sendto_child(struct winbindd_cli_state *state,
537 struct winbindd_child *child)
539 async_request(state->mem_ctx, child, state->request,
540 &state->response, recvfrom_child, state);
543 void sendto_domain(struct winbindd_cli_state *state,
544 struct winbindd_domain *domain)
546 async_domain_request(state->mem_ctx, domain,
547 state->request, &state->response,
548 recvfrom_child, state);
551 static void child_process_request(struct winbindd_child *child,
552 struct winbindd_cli_state *state)
554 struct winbindd_domain *domain = child->domain;
555 const struct winbindd_child_dispatch_table *table = child->table;
557 /* Free response data - we may be interrupted and receive another
558 command before being able to send this data off. */
560 state->response.result = WINBINDD_ERROR;
561 state->response.length = sizeof(struct winbindd_response);
563 /* as all requests in the child are sync, we can use talloc_tos() */
564 state->mem_ctx = talloc_tos();
566 /* Process command */
568 for (; table->name; table++) {
569 if (state->request->cmd == table->struct_cmd) {
570 DEBUG(10,("child_process_request: request fn %s\n",
572 state->response.result = table->struct_fn(domain, state);
577 DEBUG(1 ,("child_process_request: unknown request fn number %d\n",
578 (int)state->request->cmd));
579 state->response.result = WINBINDD_ERROR;
582 void setup_child(struct winbindd_child *child,
583 const struct winbindd_child_dispatch_table *table,
584 const char *logprefix,
587 if (logprefix && logname) {
588 if (asprintf(&child->logfilename, "%s/%s-%s",
589 get_dyn_LOGFILEBASE(), logprefix, logname) < 0) {
590 smb_panic("Internal error: asprintf failed");
593 smb_panic("Internal error: logprefix == NULL && "
597 child->domain = NULL;
598 child->table = table;
599 child->queue = tevent_queue_create(NULL, "winbind_child");
600 SMB_ASSERT(child->queue != NULL);
603 struct winbindd_child *children = NULL;
605 void winbind_child_died(pid_t pid)
607 struct winbindd_child *child;
609 for (child = children; child != NULL; child = child->next) {
610 if (child->pid == pid) {
616 DEBUG(5, ("Already reaped child %u died\n", (unsigned int)pid));
620 /* This will be re-added in fork_domain_child() */
622 DLIST_REMOVE(children, child);
629 /* Ensure any negative cache entries with the netbios or realm names are removed. */
631 void winbindd_flush_negative_conn_cache(struct winbindd_domain *domain)
633 flush_negative_conn_cache_for_domain(domain->name);
634 if (*domain->alt_name) {
635 flush_negative_conn_cache_for_domain(domain->alt_name);
640 * Parent winbindd process sets its own debug level first and then
641 * sends a message to all the winbindd children to adjust their debug
642 * level to that of parents.
645 void winbind_msg_debug(struct messaging_context *msg_ctx,
648 struct server_id server_id,
651 struct winbindd_child *child;
653 DEBUG(10,("winbind_msg_debug: got debug message.\n"));
655 debug_message(msg_ctx, private_data, MSG_DEBUG, server_id, data);
657 for (child = children; child != NULL; child = child->next) {
659 DEBUG(10,("winbind_msg_debug: sending message to pid %u.\n",
660 (unsigned int)child->pid));
662 messaging_send_buf(msg_ctx, pid_to_procid(child->pid),
665 strlen((char *) data->data) + 1);
669 /* Set our domains as offline and forward the offline message to our children. */
671 void winbind_msg_offline(struct messaging_context *msg_ctx,
674 struct server_id server_id,
677 struct winbindd_child *child;
678 struct winbindd_domain *domain;
680 DEBUG(10,("winbind_msg_offline: got offline message.\n"));
682 if (!lp_winbind_offline_logon()) {
683 DEBUG(10,("winbind_msg_offline: rejecting offline message.\n"));
687 /* Set our global state as offline. */
688 if (!set_global_winbindd_state_offline()) {
689 DEBUG(10,("winbind_msg_offline: offline request failed.\n"));
693 /* Set all our domains as offline. */
694 for (domain = domain_list(); domain; domain = domain->next) {
695 if (domain->internal) {
698 DEBUG(5,("winbind_msg_offline: marking %s offline.\n", domain->name));
699 set_domain_offline(domain);
702 for (child = children; child != NULL; child = child->next) {
703 /* Don't send message to internal childs. We've already
705 if (!child->domain || winbindd_internal_child(child)) {
709 /* Or internal domains (this should not be possible....) */
710 if (child->domain->internal) {
714 /* Each winbindd child should only process requests for one domain - make sure
715 we only set it online / offline for that domain. */
717 DEBUG(10,("winbind_msg_offline: sending message to pid %u for domain %s.\n",
718 (unsigned int)child->pid, domain->name ));
720 messaging_send_buf(msg_ctx, pid_to_procid(child->pid),
722 (uint8 *)child->domain->name,
723 strlen(child->domain->name)+1);
727 /* Set our domains as online and forward the online message to our children. */
729 void winbind_msg_online(struct messaging_context *msg_ctx,
732 struct server_id server_id,
735 struct winbindd_child *child;
736 struct winbindd_domain *domain;
738 DEBUG(10,("winbind_msg_online: got online message.\n"));
740 if (!lp_winbind_offline_logon()) {
741 DEBUG(10,("winbind_msg_online: rejecting online message.\n"));
745 /* Set our global state as online. */
746 set_global_winbindd_state_online();
748 smb_nscd_flush_user_cache();
749 smb_nscd_flush_group_cache();
751 /* Set all our domains as online. */
752 for (domain = domain_list(); domain; domain = domain->next) {
753 if (domain->internal) {
756 DEBUG(5,("winbind_msg_online: requesting %s to go online.\n", domain->name));
758 winbindd_flush_negative_conn_cache(domain);
759 set_domain_online_request(domain);
761 /* Send an online message to the idmap child when our
762 primary domain comes back online */
764 if ( domain->primary ) {
765 struct winbindd_child *idmap = idmap_child();
767 if ( idmap->pid != 0 ) {
768 messaging_send_buf(msg_ctx,
769 pid_to_procid(idmap->pid),
771 (uint8 *)domain->name,
772 strlen(domain->name)+1);
777 for (child = children; child != NULL; child = child->next) {
778 /* Don't send message to internal childs. */
779 if (!child->domain || winbindd_internal_child(child)) {
783 /* Or internal domains (this should not be possible....) */
784 if (child->domain->internal) {
788 /* Each winbindd child should only process requests for one domain - make sure
789 we only set it online / offline for that domain. */
791 DEBUG(10,("winbind_msg_online: sending message to pid %u for domain %s.\n",
792 (unsigned int)child->pid, child->domain->name ));
794 messaging_send_buf(msg_ctx, pid_to_procid(child->pid),
796 (uint8 *)child->domain->name,
797 strlen(child->domain->name)+1);
801 static const char *collect_onlinestatus(TALLOC_CTX *mem_ctx)
803 struct winbindd_domain *domain;
806 if ((buf = talloc_asprintf(mem_ctx, "global:%s ",
807 get_global_winbindd_state_offline() ?
808 "Offline":"Online")) == NULL) {
812 for (domain = domain_list(); domain; domain = domain->next) {
813 if ((buf = talloc_asprintf_append_buffer(buf, "%s:%s ",
816 "Online":"Offline")) == NULL) {
821 buf = talloc_asprintf_append_buffer(buf, "\n");
823 DEBUG(5,("collect_onlinestatus: %s", buf));
828 void winbind_msg_onlinestatus(struct messaging_context *msg_ctx,
831 struct server_id server_id,
836 struct server_id *sender;
838 DEBUG(5,("winbind_msg_onlinestatus received.\n"));
844 sender = (struct server_id *)data->data;
846 mem_ctx = talloc_init("winbind_msg_onlinestatus");
847 if (mem_ctx == NULL) {
851 message = collect_onlinestatus(mem_ctx);
852 if (message == NULL) {
853 talloc_destroy(mem_ctx);
857 messaging_send_buf(msg_ctx, *sender, MSG_WINBIND_ONLINESTATUS,
858 (uint8 *)message, strlen(message) + 1);
860 talloc_destroy(mem_ctx);
863 void winbind_msg_dump_event_list(struct messaging_context *msg_ctx,
866 struct server_id server_id,
869 struct winbindd_child *child;
871 DEBUG(10,("winbind_msg_dump_event_list received\n"));
873 dump_event_list(winbind_event_context());
875 for (child = children; child != NULL; child = child->next) {
877 DEBUG(10,("winbind_msg_dump_event_list: sending message to pid %u\n",
878 (unsigned int)child->pid));
880 messaging_send_buf(msg_ctx, pid_to_procid(child->pid),
887 void winbind_msg_dump_domain_list(struct messaging_context *msg_ctx,
890 struct server_id server_id,
894 const char *message = NULL;
895 struct server_id *sender = NULL;
896 const char *domain = NULL;
899 struct winbindd_domain *dom = NULL;
901 DEBUG(5,("winbind_msg_dump_domain_list received.\n"));
903 if (!data || !data->data) {
907 if (data->length < sizeof(struct server_id)) {
911 mem_ctx = talloc_init("winbind_msg_dump_domain_list");
916 sender = (struct server_id *)data->data;
917 if (data->length > sizeof(struct server_id)) {
918 domain = (const char *)data->data+sizeof(struct server_id);
923 DEBUG(5,("winbind_msg_dump_domain_list for domain: %s\n",
926 message = NDR_PRINT_STRUCT_STRING(mem_ctx, winbindd_domain,
927 find_domain_from_name_noinit(domain));
929 talloc_destroy(mem_ctx);
933 messaging_send_buf(msg_ctx, *sender,
934 MSG_WINBIND_DUMP_DOMAIN_LIST,
935 (uint8_t *)message, strlen(message) + 1);
937 talloc_destroy(mem_ctx);
942 DEBUG(5,("winbind_msg_dump_domain_list all domains\n"));
944 for (dom = domain_list(); dom; dom=dom->next) {
945 message = NDR_PRINT_STRUCT_STRING(mem_ctx, winbindd_domain, dom);
947 talloc_destroy(mem_ctx);
951 s = talloc_asprintf_append(s, "%s\n", message);
953 talloc_destroy(mem_ctx);
958 status = messaging_send_buf(msg_ctx, *sender,
959 MSG_WINBIND_DUMP_DOMAIN_LIST,
960 (uint8_t *)s, strlen(s) + 1);
961 if (!NT_STATUS_IS_OK(status)) {
962 DEBUG(0,("failed to send message: %s\n",
966 talloc_destroy(mem_ctx);
969 static void account_lockout_policy_handler(struct event_context *ctx,
970 struct timed_event *te,
974 struct winbindd_child *child =
975 (struct winbindd_child *)private_data;
976 TALLOC_CTX *mem_ctx = NULL;
977 struct winbindd_methods *methods;
978 struct samr_DomInfo12 lockout_policy;
981 DEBUG(10,("account_lockout_policy_handler called\n"));
983 TALLOC_FREE(child->lockout_policy_event);
985 if ( !winbindd_can_contact_domain( child->domain ) ) {
986 DEBUG(10,("account_lockout_policy_handler: Removing myself since I "
987 "do not have an incoming trust to domain %s\n",
988 child->domain->name));
993 methods = child->domain->methods;
995 mem_ctx = talloc_init("account_lockout_policy_handler ctx");
997 result = NT_STATUS_NO_MEMORY;
999 result = methods->lockout_policy(child->domain, mem_ctx, &lockout_policy);
1001 TALLOC_FREE(mem_ctx);
1003 if (!NT_STATUS_IS_OK(result)) {
1004 DEBUG(10,("account_lockout_policy_handler: lockout_policy failed error %s\n",
1005 nt_errstr(result)));
1008 child->lockout_policy_event = event_add_timed(winbind_event_context(), NULL,
1009 timeval_current_ofs(3600, 0),
1010 account_lockout_policy_handler,
1014 static time_t get_machine_password_timeout(void)
1016 /* until we have gpo support use lp setting */
1017 return lp_machine_password_timeout();
1020 static bool calculate_next_machine_pwd_change(const char *domain,
1023 time_t pass_last_set_time;
1028 pw = secrets_fetch_machine_password(domain,
1029 &pass_last_set_time,
1033 DEBUG(0,("cannot fetch own machine password ????"));
1039 timeout = get_machine_password_timeout();
1041 DEBUG(10,("machine password never expires\n"));
1045 if (time(NULL) < (pass_last_set_time + timeout)) {
1046 next_change = pass_last_set_time + timeout;
1047 DEBUG(10,("machine password still valid until: %s\n",
1048 http_timestring(talloc_tos(), next_change)));
1049 *t = timeval_set(next_change, 0);
1053 DEBUG(10,("machine password expired, needs immediate change\n"));
1055 *t = timeval_zero();
1060 static void machine_password_change_handler(struct event_context *ctx,
1061 struct timed_event *te,
1065 struct winbindd_child *child =
1066 (struct winbindd_child *)private_data;
1067 struct rpc_pipe_client *netlogon_pipe = NULL;
1070 struct timeval next_change;
1072 DEBUG(10,("machine_password_change_handler called\n"));
1074 TALLOC_FREE(child->machine_password_change_event);
1076 if (!calculate_next_machine_pwd_change(child->domain->name,
1081 if (!winbindd_can_contact_domain(child->domain)) {
1082 DEBUG(10,("machine_password_change_handler: Removing myself since I "
1083 "do not have an incoming trust to domain %s\n",
1084 child->domain->name));
1088 result = cm_connect_netlogon(child->domain, &netlogon_pipe);
1089 if (!NT_STATUS_IS_OK(result)) {
1090 DEBUG(10,("machine_password_change_handler: "
1091 "failed to connect netlogon pipe: %s\n",
1092 nt_errstr(result)));
1096 frame = talloc_stackframe();
1098 result = trust_pw_find_change_and_store_it(netlogon_pipe,
1100 child->domain->name);
1103 if (!NT_STATUS_IS_OK(result)) {
1104 DEBUG(10,("machine_password_change_handler: "
1105 "failed to change machine password: %s\n",
1106 nt_errstr(result)));
1108 DEBUG(10,("machine_password_change_handler: "
1109 "successfully changed machine password\n"));
1112 child->machine_password_change_event = event_add_timed(winbind_event_context(), NULL,
1114 machine_password_change_handler,
1118 /* Deal with a request to go offline. */
1120 static void child_msg_offline(struct messaging_context *msg,
1123 struct server_id server_id,
1126 struct winbindd_domain *domain;
1127 struct winbindd_domain *primary_domain = NULL;
1128 const char *domainname = (const char *)data->data;
1130 if (data->data == NULL || data->length == 0) {
1134 DEBUG(5,("child_msg_offline received for domain %s.\n", domainname));
1136 if (!lp_winbind_offline_logon()) {
1137 DEBUG(10,("child_msg_offline: rejecting offline message.\n"));
1141 primary_domain = find_our_domain();
1143 /* Mark the requested domain offline. */
1145 for (domain = domain_list(); domain; domain = domain->next) {
1146 if (domain->internal) {
1149 if (strequal(domain->name, domainname)) {
1150 DEBUG(5,("child_msg_offline: marking %s offline.\n", domain->name));
1151 set_domain_offline(domain);
1152 /* we are in the trusted domain, set the primary domain
1154 if (domain != primary_domain) {
1155 set_domain_offline(primary_domain);
1161 /* Deal with a request to go online. */
1163 static void child_msg_online(struct messaging_context *msg,
1166 struct server_id server_id,
1169 struct winbindd_domain *domain;
1170 struct winbindd_domain *primary_domain = NULL;
1171 const char *domainname = (const char *)data->data;
1173 if (data->data == NULL || data->length == 0) {
1177 DEBUG(5,("child_msg_online received for domain %s.\n", domainname));
1179 if (!lp_winbind_offline_logon()) {
1180 DEBUG(10,("child_msg_online: rejecting online message.\n"));
1184 primary_domain = find_our_domain();
1186 /* Set our global state as online. */
1187 set_global_winbindd_state_online();
1189 /* Try and mark everything online - delete any negative cache entries
1190 to force a reconnect now. */
1192 for (domain = domain_list(); domain; domain = domain->next) {
1193 if (domain->internal) {
1196 if (strequal(domain->name, domainname)) {
1197 DEBUG(5,("child_msg_online: requesting %s to go online.\n", domain->name));
1198 winbindd_flush_negative_conn_cache(domain);
1199 set_domain_online_request(domain);
1201 /* we can be in trusted domain, which will contact primary domain
1202 * we have to bring primary domain online in trusted domain process
1203 * see, winbindd_dual_pam_auth() --> winbindd_dual_pam_auth_samlogon()
1204 * --> contact_domain = find_our_domain()
1206 if (domain != primary_domain) {
1207 winbindd_flush_negative_conn_cache(primary_domain);
1208 set_domain_online_request(primary_domain);
1214 static void child_msg_dump_event_list(struct messaging_context *msg,
1217 struct server_id server_id,
1220 DEBUG(5,("child_msg_dump_event_list received\n"));
1222 dump_event_list(winbind_event_context());
1225 bool winbindd_reinit_after_fork(const char *logfilename)
1227 struct winbindd_domain *domain;
1228 struct winbindd_child *cl;
1230 if (!NT_STATUS_IS_OK(reinit_after_fork(winbind_messaging_context(),
1231 winbind_event_context(),
1233 DEBUG(0,("reinit_after_fork() failed\n"));
1237 close_conns_after_fork();
1239 if (!override_logfile && logfilename) {
1240 lp_set_logfile(logfilename);
1244 if (!winbindd_setup_sig_term_handler(false))
1246 if (!winbindd_setup_sig_hup_handler(override_logfile ? NULL :
1250 /* Don't handle the same messages as our parent. */
1251 messaging_deregister(winbind_messaging_context(),
1252 MSG_SMB_CONF_UPDATED, NULL);
1253 messaging_deregister(winbind_messaging_context(),
1254 MSG_SHUTDOWN, NULL);
1255 messaging_deregister(winbind_messaging_context(),
1256 MSG_WINBIND_OFFLINE, NULL);
1257 messaging_deregister(winbind_messaging_context(),
1258 MSG_WINBIND_ONLINE, NULL);
1259 messaging_deregister(winbind_messaging_context(),
1260 MSG_WINBIND_ONLINESTATUS, NULL);
1261 messaging_deregister(winbind_messaging_context(),
1262 MSG_DUMP_EVENT_LIST, NULL);
1263 messaging_deregister(winbind_messaging_context(),
1264 MSG_WINBIND_DUMP_DOMAIN_LIST, NULL);
1265 messaging_deregister(winbind_messaging_context(),
1268 /* We have destroyed all events in the winbindd_event_context
1269 * in reinit_after_fork(), so clean out all possible pending
1270 * event pointers. */
1272 /* Deal with check_online_events. */
1274 for (domain = domain_list(); domain; domain = domain->next) {
1275 TALLOC_FREE(domain->check_online_event);
1278 /* Ensure we're not handling a credential cache event inherited
1279 * from our parent. */
1281 ccache_remove_all_after_fork();
1283 /* Destroy all possible events in child list. */
1284 for (cl = children; cl != NULL; cl = cl->next) {
1285 TALLOC_FREE(cl->lockout_policy_event);
1286 TALLOC_FREE(cl->machine_password_change_event);
1288 /* Children should never be able to send
1289 * each other messages, all messages must
1290 * go through the parent.
1295 * This is a little tricky, children must not
1296 * send an MSG_WINBIND_ONLINE message to idmap_child().
1297 * If we are in a child of our primary domain or
1298 * in the process created by fork_child_dc_connect(),
1299 * and the primary domain cannot go online,
1300 * fork_child_dc_connection() sends MSG_WINBIND_ONLINE
1301 * periodically to idmap_child().
1303 * The sequence is, fork_child_dc_connect() ---> getdcs() --->
1304 * get_dc_name_via_netlogon() ---> cm_connect_netlogon()
1305 * ---> init_dc_connection() ---> cm_open_connection --->
1306 * set_domain_online(), sends MSG_WINBIND_ONLINE to
1307 * idmap_child(). Disallow children sending messages
1308 * to each other, all messages must go through the parent.
1316 static bool fork_domain_child(struct winbindd_child *child)
1319 struct winbindd_cli_state state;
1320 struct winbindd_domain *primary_domain = NULL;
1322 if (child->domain) {
1323 DEBUG(10, ("fork_domain_child called for domain '%s'\n",
1324 child->domain->name));
1326 DEBUG(10, ("fork_domain_child called without domain.\n"));
1329 if (socketpair(AF_UNIX, SOCK_STREAM, 0, fdpair) != 0) {
1330 DEBUG(0, ("Could not open child pipe: %s\n",
1336 state.pid = sys_getpid();
1337 state.request = &state._request;
1339 child->pid = sys_fork();
1341 if (child->pid == -1) {
1342 DEBUG(0, ("Could not fork: %s\n", strerror(errno)));
1346 if (child->pid != 0) {
1349 child->next = child->prev = NULL;
1350 DLIST_ADD(children, child);
1351 child->sock = fdpair[1];
1357 DEBUG(10, ("Child process %d\n", (int)sys_getpid()));
1359 /* Stop zombies in children */
1362 state.sock = fdpair[0];
1365 if (!winbindd_reinit_after_fork(child->logfilename)) {
1369 /* Handle online/offline messages. */
1370 messaging_register(winbind_messaging_context(), NULL,
1371 MSG_WINBIND_OFFLINE, child_msg_offline);
1372 messaging_register(winbind_messaging_context(), NULL,
1373 MSG_WINBIND_ONLINE, child_msg_online);
1374 messaging_register(winbind_messaging_context(), NULL,
1375 MSG_DUMP_EVENT_LIST, child_msg_dump_event_list);
1376 messaging_register(winbind_messaging_context(), NULL,
1377 MSG_DEBUG, debug_message);
1379 primary_domain = find_our_domain();
1381 if (primary_domain == NULL) {
1382 smb_panic("no primary domain found");
1385 /* It doesn't matter if we allow cache login,
1386 * try to bring domain online after fork. */
1387 if ( child->domain ) {
1388 child->domain->startup = True;
1389 child->domain->startup_time = time(NULL);
1390 /* we can be in primary domain or in trusted domain
1391 * If we are in trusted domain, set the primary domain
1392 * in start-up mode */
1393 if (!(child->domain->internal)) {
1394 set_domain_online_request(child->domain);
1395 if (!(child->domain->primary)) {
1396 primary_domain->startup = True;
1397 primary_domain->startup_time = time(NULL);
1398 set_domain_online_request(primary_domain);
1404 * We are in idmap child, make sure that we set the
1405 * check_online_event to bring primary domain online.
1407 if (child == idmap_child()) {
1408 set_domain_online_request(primary_domain);
1411 /* We might be in the idmap child...*/
1412 if (child->domain && !(child->domain->internal) &&
1413 lp_winbind_offline_logon()) {
1415 set_domain_online_request(child->domain);
1417 if (primary_domain && (primary_domain != child->domain)) {
1418 /* We need to talk to the primary
1419 * domain as well as the trusted
1420 * domain inside a trusted domain
1423 * set_dc_type_and_flags_trustinfo()
1426 set_domain_online_request(primary_domain);
1429 child->lockout_policy_event = event_add_timed(
1430 winbind_event_context(), NULL, timeval_zero(),
1431 account_lockout_policy_handler,
1435 if (child->domain && child->domain->primary &&
1436 !USE_KERBEROS_KEYTAB &&
1437 lp_server_role() == ROLE_DOMAIN_MEMBER) {
1439 struct timeval next_change;
1441 if (calculate_next_machine_pwd_change(child->domain->name,
1443 child->machine_password_change_event = event_add_timed(
1444 winbind_event_context(), NULL, next_change,
1445 machine_password_change_handler,
1459 TALLOC_CTX *frame = talloc_stackframe();
1460 struct iovec iov[2];
1463 if (run_events(winbind_event_context(), 0, NULL, NULL)) {
1470 if (child->domain && child->domain->startup &&
1471 (now.tv_sec > child->domain->startup_time + 30)) {
1472 /* No longer in "startup" mode. */
1473 DEBUG(10,("fork_domain_child: domain %s no longer in 'startup' mode.\n",
1474 child->domain->name ));
1475 child->domain->startup = False;
1480 FD_SET(state.sock, &r_fds);
1483 event_add_to_select_args(winbind_event_context(), &now,
1484 &r_fds, &w_fds, &t, &maxfd);
1485 tp = get_timed_events_timeout(winbind_event_context(), &t);
1487 DEBUG(11,("select will use timeout of %u.%u seconds\n",
1488 (unsigned int)tp->tv_sec, (unsigned int)tp->tv_usec ));
1491 ret = sys_select(maxfd + 1, &r_fds, &w_fds, NULL, tp);
1493 if (run_events(winbind_event_context(), ret, &r_fds, &w_fds)) {
1494 /* We got a signal - continue. */
1500 DEBUG(11,("nothing is ready yet, continue\n"));
1505 if (ret == -1 && errno == EINTR) {
1506 /* We got a signal - continue. */
1511 if (ret == -1 && errno != EINTR) {
1512 DEBUG(0,("select error occured\n"));
1518 /* fetch a request from the main daemon */
1519 child_read_request(&state);
1521 if (state.finished) {
1522 /* we lost contact with our parent */
1526 DEBUG(4,("child daemon request %d\n", (int)state.request->cmd));
1528 ZERO_STRUCT(state.response);
1529 state.request->null_term = '\0';
1530 child_process_request(child, &state);
1532 DEBUG(4, ("Finished processing child request %d\n",
1533 (int)state.request->cmd));
1535 SAFE_FREE(state.request->extra_data.data);
1537 iov[0].iov_base = (void *)&state.response;
1538 iov[0].iov_len = sizeof(struct winbindd_response);
1541 if (state.response.length > sizeof(struct winbindd_response)) {
1543 (void *)state.response.extra_data.data;
1544 iov[1].iov_len = state.response.length-iov[0].iov_len;
1548 DEBUG(10, ("Writing %d bytes to parent\n",
1549 (int)state.response.length));
1551 if (write_data_iov(state.sock, iov, iov_count) !=
1552 state.response.length) {
1553 DEBUG(0, ("Could not write result\n"));