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 "rpc_client/rpc_client.h"
33 #include "nsswitch/wb_reqtrans.h"
35 #include "../lib/util/select.h"
36 #include "../libcli/security/security.h"
37 #include "system/select.h"
39 #include "../lib/util/tevent_unix.h"
40 #include "lib/param/loadparm.h"
41 #include "lib/util/sys_rw.h"
42 #include "lib/util/sys_rw_data.h"
44 #include "lib/util/string_wrappers.h"
47 #define DBGC_CLASS DBGC_WINBIND
49 extern bool override_logfile;
51 static void forall_domain_children(bool (*fn)(struct winbindd_child *c,
55 struct winbindd_domain *d;
57 for (d = domain_list(); d != NULL; d = d->next) {
60 for (i = 0; i < lp_winbind_max_domain_connections(); i++) {
61 struct winbindd_child *c = &d->children[i];
68 ok = fn(c, private_data);
76 static void forall_children(bool (*fn)(struct winbindd_child *c,
80 struct winbindd_child *c;
85 ok = fn(c, private_data);
93 ok = fn(c, private_data);
99 forall_domain_children(fn, private_data);
102 /* Read some data from a client connection */
104 static NTSTATUS child_read_request(int sock, struct winbindd_request *wreq)
108 status = read_data_ntstatus(sock, (char *)wreq, sizeof(*wreq));
109 if (!NT_STATUS_IS_OK(status)) {
110 DEBUG(3, ("child_read_request: read_data failed: %s\n",
115 if (wreq->extra_len == 0) {
116 wreq->extra_data.data = NULL;
120 DEBUG(10, ("Need to read %d extra bytes\n", (int)wreq->extra_len));
122 wreq->extra_data.data = SMB_MALLOC_ARRAY(char, wreq->extra_len + 1);
123 if (wreq->extra_data.data == NULL) {
124 DEBUG(0, ("malloc failed\n"));
125 return NT_STATUS_NO_MEMORY;
128 /* Ensure null termination */
129 wreq->extra_data.data[wreq->extra_len] = '\0';
131 status = read_data_ntstatus(sock, wreq->extra_data.data,
133 if (!NT_STATUS_IS_OK(status)) {
134 DEBUG(0, ("Could not read extra data: %s\n",
140 static NTSTATUS child_write_response(int sock, struct winbindd_response *wrsp)
145 iov[0].iov_base = (void *)wrsp;
146 iov[0].iov_len = sizeof(struct winbindd_response);
149 if (wrsp->length > sizeof(struct winbindd_response)) {
150 iov[1].iov_base = (void *)wrsp->extra_data.data;
151 iov[1].iov_len = wrsp->length-iov[0].iov_len;
155 DEBUG(10, ("Writing %d bytes to parent\n", (int)wrsp->length));
157 if (write_data_iov(sock, iov, iov_count) != wrsp->length) {
158 DEBUG(0, ("Could not write result\n"));
159 return NT_STATUS_INVALID_HANDLE;
166 * Do winbind child async request. This is not simply wb_simple_trans. We have
167 * to do the queueing ourselves because while a request is queued, the child
168 * might have crashed, and we have to re-fork it in the _trigger function.
171 struct wb_child_request_state {
172 struct tevent_context *ev;
173 struct tevent_req *queue_subreq;
174 struct tevent_req *subreq;
175 struct winbindd_child *child;
176 struct winbindd_request *request;
177 struct winbindd_response *response;
180 static bool fork_domain_child(struct winbindd_child *child);
182 static void wb_child_request_waited(struct tevent_req *subreq);
183 static void wb_child_request_done(struct tevent_req *subreq);
184 static void wb_child_request_orphaned(struct tevent_req *subreq);
186 static void wb_child_request_cleanup(struct tevent_req *req,
187 enum tevent_req_state req_state);
189 struct tevent_req *wb_child_request_send(TALLOC_CTX *mem_ctx,
190 struct tevent_context *ev,
191 struct winbindd_child *child,
192 struct winbindd_request *request)
194 struct tevent_req *req;
195 struct wb_child_request_state *state;
196 struct tevent_req *subreq;
198 req = tevent_req_create(mem_ctx, &state,
199 struct wb_child_request_state);
205 state->child = child;
208 * We have to make a copy of "request", because our caller
209 * might drop us via talloc_free().
211 * The talloc_move() magic in wb_child_request_cleanup() keeps
212 * all the requests, but if we are sitting deep within
213 * writev_send() down to the client, we have given it the
214 * pointer to "request". As our caller lost interest, it will
215 * just free "request", while writev_send still references it.
218 state->request = talloc_memdup(state, request, sizeof(*request));
219 if (tevent_req_nomem(state->request, req)) {
220 return tevent_req_post(req, ev);
223 if (request->extra_data.data != NULL) {
224 state->request->extra_data.data = talloc_memdup(
226 request->extra_data.data,
228 if (tevent_req_nomem(state->request->extra_data.data, req)) {
229 return tevent_req_post(req, ev);
233 subreq = tevent_queue_wait_send(state, ev, child->queue);
234 if (tevent_req_nomem(subreq, req)) {
235 return tevent_req_post(req, ev);
237 tevent_req_set_callback(subreq, wb_child_request_waited, req);
238 state->queue_subreq = subreq;
240 tevent_req_set_cleanup_fn(req, wb_child_request_cleanup);
245 static void wb_child_request_waited(struct tevent_req *subreq)
247 struct tevent_req *req = tevent_req_callback_data(
248 subreq, struct tevent_req);
249 struct wb_child_request_state *state = tevent_req_data(
250 req, struct wb_child_request_state);
253 ok = tevent_queue_wait_recv(subreq);
259 * We need to keep state->queue_subreq
260 * in order to block the queue.
264 if ((state->child->sock == -1) && (!fork_domain_child(state->child))) {
265 tevent_req_error(req, errno);
269 tevent_fd_set_flags(state->child->monitor_fde, 0);
271 subreq = wb_simple_trans_send(state, global_event_context(), NULL,
272 state->child->sock, state->request);
273 if (tevent_req_nomem(subreq, req)) {
277 state->subreq = subreq;
278 tevent_req_set_callback(subreq, wb_child_request_done, req);
279 tevent_req_set_endtime(req, state->ev, timeval_current_ofs(300, 0));
282 static void wb_child_request_done(struct tevent_req *subreq)
284 struct tevent_req *req = tevent_req_callback_data(
285 subreq, struct tevent_req);
286 struct wb_child_request_state *state = tevent_req_data(
287 req, struct wb_child_request_state);
290 ret = wb_simple_trans_recv(subreq, state, &state->response, &err);
291 /* Freeing the subrequest is deferred until the cleanup function,
292 * which has to know whether a subrequest exists, and consequently
293 * decide whether to shut down the pipe to the child process.
296 tevent_req_error(req, err);
299 tevent_req_done(req);
302 static void wb_child_request_orphaned(struct tevent_req *subreq)
304 struct winbindd_child *child =
305 (struct winbindd_child *)tevent_req_callback_data_void(subreq);
307 DBG_WARNING("cleanup orphaned subreq[%p]\n", subreq);
310 if (child->domain != NULL) {
312 * If the child is attached to a domain,
313 * we need to make sure the domain queue
314 * can move forward, after the orphaned
317 tevent_queue_start(child->domain->queue);
321 int wb_child_request_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
322 struct winbindd_response **presponse, int *err)
324 struct wb_child_request_state *state = tevent_req_data(
325 req, struct wb_child_request_state);
327 if (tevent_req_is_unix_error(req, err)) {
330 *presponse = talloc_move(mem_ctx, &state->response);
334 static void wb_child_request_cleanup(struct tevent_req *req,
335 enum tevent_req_state req_state)
337 struct wb_child_request_state *state =
338 tevent_req_data(req, struct wb_child_request_state);
340 if (state->subreq == NULL) {
341 /* nothing to cleanup */
345 if (req_state == TEVENT_REQ_RECEIVED) {
346 struct tevent_req *subreq = NULL;
349 * Our caller gave up, but we need to keep
350 * the low level request (wb_simple_trans)
351 * in order to maintain the parent child protocol.
353 * We also need to keep the child queue blocked
354 * until we got the response from the child.
357 subreq = talloc_move(state->child->queue, &state->subreq);
358 talloc_move(subreq, &state->queue_subreq);
359 talloc_move(subreq, &state->request);
360 tevent_req_set_callback(subreq,
361 wb_child_request_orphaned,
364 DBG_WARNING("keep orphaned subreq[%p]\n", subreq);
368 TALLOC_FREE(state->subreq);
369 TALLOC_FREE(state->queue_subreq);
371 tevent_fd_set_flags(state->child->monitor_fde, TEVENT_FD_READ);
373 if (state->child->domain != NULL) {
375 * If the child is attached to a domain,
376 * we need to make sure the domain queue
377 * can move forward, after the request
380 tevent_queue_start(state->child->domain->queue);
383 if (req_state == TEVENT_REQ_DONE) {
384 /* transmitted request and got response */
389 * Failed to transmit and receive response, or request
390 * cancelled while being serviced.
391 * The basic parent/child communication broke, close
394 TALLOC_FREE(state->child->monitor_fde);
395 close(state->child->sock);
396 state->child->sock = -1;
399 static void child_socket_readable(struct tevent_context *ev,
400 struct tevent_fd *fde,
404 struct winbindd_child *child = private_data;
406 if ((flags & TEVENT_FD_READ) == 0) {
410 TALLOC_FREE(child->monitor_fde);
413 * We're only active when there is no outstanding child
414 * request. Arriving here means the child closed its socket,
415 * it died. Do the same here.
418 SMB_ASSERT(child->sock != -1);
424 static struct winbindd_child *choose_domain_child(struct winbindd_domain *domain)
426 struct winbindd_child *shortest = &domain->children[0];
427 struct winbindd_child *current;
430 for (i=0; i<lp_winbind_max_domain_connections(); i++) {
431 size_t shortest_len, current_len;
433 current = &domain->children[i];
434 current_len = tevent_queue_length(current->queue);
436 if (current_len == 0) {
441 shortest_len = tevent_queue_length(shortest->queue);
443 if (current_len < shortest_len) {
451 struct dcerpc_binding_handle *dom_child_handle(struct winbindd_domain *domain)
453 return domain->binding_handle;
456 struct wb_domain_request_state {
457 struct tevent_context *ev;
458 struct tevent_queue_entry *queue_entry;
459 struct winbindd_domain *domain;
460 struct winbindd_child *child;
461 struct winbindd_request *request;
462 struct winbindd_request *init_req;
463 struct winbindd_response *response;
464 struct tevent_req *pending_subreq;
467 static void wb_domain_request_cleanup(struct tevent_req *req,
468 enum tevent_req_state req_state)
470 struct wb_domain_request_state *state = tevent_req_data(
471 req, struct wb_domain_request_state);
474 * If we're completely done or got a failure.
475 * we should remove ourself from the domain queue,
476 * after removing the child subreq from the child queue
477 * and give the next one in the queue the chance
478 * to check for an idle child.
480 TALLOC_FREE(state->pending_subreq);
481 TALLOC_FREE(state->queue_entry);
482 tevent_queue_start(state->domain->queue);
485 static void wb_domain_request_trigger(struct tevent_req *req,
487 static void wb_domain_request_gotdc(struct tevent_req *subreq);
488 static void wb_domain_request_initialized(struct tevent_req *subreq);
489 static void wb_domain_request_done(struct tevent_req *subreq);
491 struct tevent_req *wb_domain_request_send(TALLOC_CTX *mem_ctx,
492 struct tevent_context *ev,
493 struct winbindd_domain *domain,
494 struct winbindd_request *request)
496 struct tevent_req *req;
497 struct wb_domain_request_state *state;
499 req = tevent_req_create(mem_ctx, &state,
500 struct wb_domain_request_state);
505 state->domain = domain;
507 state->request = request;
509 tevent_req_set_cleanup_fn(req, wb_domain_request_cleanup);
511 state->queue_entry = tevent_queue_add_entry(
512 domain->queue, state->ev, req,
513 wb_domain_request_trigger, NULL);
514 if (tevent_req_nomem(state->queue_entry, req)) {
515 return tevent_req_post(req, ev);
521 static void wb_domain_request_trigger(struct tevent_req *req,
524 struct wb_domain_request_state *state = tevent_req_data(
525 req, struct wb_domain_request_state);
526 struct winbindd_domain *domain = state->domain;
527 struct tevent_req *subreq = NULL;
528 size_t shortest_queue_length;
530 state->child = choose_domain_child(domain);
531 shortest_queue_length = tevent_queue_length(state->child->queue);
532 if (shortest_queue_length > 0) {
534 * All children are busy, we need to stop
535 * the queue and untrigger our own queue
536 * entry. Once a pending request
537 * is done it calls tevent_queue_start
538 * and we get retriggered.
541 tevent_queue_stop(state->domain->queue);
542 tevent_queue_entry_untrigger(state->queue_entry);
546 if (domain->initialized) {
547 subreq = wb_child_request_send(state, state->ev, state->child,
549 if (tevent_req_nomem(subreq, req)) {
552 tevent_req_set_callback(subreq, wb_domain_request_done, req);
553 state->pending_subreq = subreq;
556 * Once the domain is initialized and
557 * once we placed our real request into the child queue,
558 * we can remove ourself from the domain queue
559 * and give the next one in the queue the chance
560 * to check for an idle child.
562 TALLOC_FREE(state->queue_entry);
566 state->init_req = talloc_zero(state, struct winbindd_request);
567 if (tevent_req_nomem(state->init_req, req)) {
571 if (IS_DC || domain->primary || domain->internal) {
572 /* The primary domain has to find the DC name itself */
573 state->init_req->cmd = WINBINDD_INIT_CONNECTION;
574 fstrcpy(state->init_req->domain_name, domain->name);
575 state->init_req->data.init_conn.is_primary = domain->primary;
576 fstrcpy(state->init_req->data.init_conn.dcname, "");
578 subreq = wb_child_request_send(state, state->ev, state->child,
580 if (tevent_req_nomem(subreq, req)) {
583 tevent_req_set_callback(subreq, wb_domain_request_initialized,
585 state->pending_subreq = subreq;
590 * This is *not* the primary domain,
591 * let's ask our DC about a DC name.
593 * We prefer getting a dns name in dc_unc,
594 * which is indicated by DS_RETURN_DNS_NAME.
595 * For NT4 domains we still get the netbios name.
597 subreq = wb_dsgetdcname_send(state, state->ev,
599 NULL, /* domain_guid */
600 NULL, /* site_name */
601 DS_RETURN_DNS_NAME); /* flags */
602 if (tevent_req_nomem(subreq, req)) {
605 tevent_req_set_callback(subreq, wb_domain_request_gotdc, req);
606 state->pending_subreq = subreq;
610 static void wb_domain_request_gotdc(struct tevent_req *subreq)
612 struct tevent_req *req = tevent_req_callback_data(
613 subreq, struct tevent_req);
614 struct wb_domain_request_state *state = tevent_req_data(
615 req, struct wb_domain_request_state);
616 struct netr_DsRGetDCNameInfo *dcinfo = NULL;
618 const char *dcname = NULL;
620 state->pending_subreq = NULL;
622 status = wb_dsgetdcname_recv(subreq, state, &dcinfo);
624 if (tevent_req_nterror(req, status)) {
627 dcname = dcinfo->dc_unc;
628 while (dcname != NULL && *dcname == '\\') {
631 state->init_req->cmd = WINBINDD_INIT_CONNECTION;
632 fstrcpy(state->init_req->domain_name, state->domain->name);
633 state->init_req->data.init_conn.is_primary = False;
634 fstrcpy(state->init_req->data.init_conn.dcname,
639 subreq = wb_child_request_send(state, state->ev, state->child,
641 if (tevent_req_nomem(subreq, req)) {
644 tevent_req_set_callback(subreq, wb_domain_request_initialized, req);
645 state->pending_subreq = subreq;
648 static void wb_domain_request_initialized(struct tevent_req *subreq)
650 struct tevent_req *req = tevent_req_callback_data(
651 subreq, struct tevent_req);
652 struct wb_domain_request_state *state = tevent_req_data(
653 req, struct wb_domain_request_state);
654 struct winbindd_response *response;
657 state->pending_subreq = NULL;
659 ret = wb_child_request_recv(subreq, talloc_tos(), &response, &err);
662 tevent_req_error(req, err);
666 if (!string_to_sid(&state->domain->sid,
667 response->data.domain_info.sid)) {
668 DEBUG(1,("init_child_recv: Could not convert sid %s "
669 "from string\n", response->data.domain_info.sid));
670 tevent_req_error(req, EINVAL);
674 talloc_free(state->domain->name);
675 state->domain->name = talloc_strdup(state->domain,
676 response->data.domain_info.name);
677 if (state->domain->name == NULL) {
678 tevent_req_error(req, ENOMEM);
682 if (response->data.domain_info.alt_name[0] != '\0') {
683 talloc_free(state->domain->alt_name);
685 state->domain->alt_name = talloc_strdup(state->domain,
686 response->data.domain_info.alt_name);
687 if (state->domain->alt_name == NULL) {
688 tevent_req_error(req, ENOMEM);
693 state->domain->native_mode = response->data.domain_info.native_mode;
694 state->domain->active_directory =
695 response->data.domain_info.active_directory;
696 state->domain->initialized = true;
698 TALLOC_FREE(response);
700 subreq = wb_child_request_send(state, state->ev, state->child,
702 if (tevent_req_nomem(subreq, req)) {
705 tevent_req_set_callback(subreq, wb_domain_request_done, req);
706 state->pending_subreq = subreq;
709 * Once the domain is initialized and
710 * once we placed our real request into the child queue,
711 * we can remove ourself from the domain queue
712 * and give the next one in the queue the chance
713 * to check for an idle child.
715 TALLOC_FREE(state->queue_entry);
718 static void wb_domain_request_done(struct tevent_req *subreq)
720 struct tevent_req *req = tevent_req_callback_data(
721 subreq, struct tevent_req);
722 struct wb_domain_request_state *state = tevent_req_data(
723 req, struct wb_domain_request_state);
726 state->pending_subreq = NULL;
728 ret = wb_child_request_recv(subreq, talloc_tos(), &state->response,
732 tevent_req_error(req, err);
735 tevent_req_done(req);
738 int wb_domain_request_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
739 struct winbindd_response **presponse, int *err)
741 struct wb_domain_request_state *state = tevent_req_data(
742 req, struct wb_domain_request_state);
744 if (tevent_req_is_unix_error(req, err)) {
747 *presponse = talloc_move(mem_ctx, &state->response);
751 static void child_process_request(struct winbindd_child *child,
752 struct winbindd_cli_state *state)
754 struct winbindd_domain *domain = child->domain;
755 const struct winbindd_child_dispatch_table *table = child->table;
757 /* Free response data - we may be interrupted and receive another
758 command before being able to send this data off. */
760 state->response->result = WINBINDD_ERROR;
761 state->response->length = sizeof(struct winbindd_response);
763 /* as all requests in the child are sync, we can use talloc_tos() */
764 state->mem_ctx = talloc_tos();
766 /* Process command */
768 for (; table->name; table++) {
769 if (state->request->cmd == table->struct_cmd) {
770 DEBUG(10,("child_process_request: request fn %s\n",
772 state->response->result = table->struct_fn(domain, state);
777 DEBUG(1, ("child_process_request: unknown request fn number %d\n",
778 (int)state->request->cmd));
779 state->response->result = WINBINDD_ERROR;
782 void setup_child(struct winbindd_domain *domain, struct winbindd_child *child,
783 const struct winbindd_child_dispatch_table *table,
784 const char *logprefix,
787 const struct loadparm_substitution *lp_sub =
788 loadparm_s3_global_substitution();
790 if (logprefix && logname) {
791 char *logbase = NULL;
793 if (*lp_logfile(talloc_tos(), lp_sub)) {
796 if (asprintf(&logbase, "%s", lp_logfile(talloc_tos(), lp_sub)) < 0) {
797 smb_panic("Internal error: asprintf failed");
800 if ((end = strrchr_m(logbase, '/'))) {
804 if (asprintf(&logbase, "%s", get_dyn_LOGFILEBASE()) < 0) {
805 smb_panic("Internal error: asprintf failed");
809 if (asprintf(&child->logfilename, "%s/%s-%s",
810 logbase, logprefix, logname) < 0) {
812 smb_panic("Internal error: asprintf failed");
817 smb_panic("Internal error: logprefix == NULL && "
823 child->domain = domain;
824 child->table = table;
825 child->queue = tevent_queue_create(NULL, "winbind_child");
826 SMB_ASSERT(child->queue != NULL);
827 if (domain == NULL) {
828 child->binding_handle = wbint_binding_handle(NULL, NULL, child);
829 SMB_ASSERT(child->binding_handle != NULL);
833 struct winbind_child_died_state {
835 struct winbindd_child *child;
838 static bool winbind_child_died_fn(struct winbindd_child *child,
841 struct winbind_child_died_state *state = private_data;
843 if (child->pid == state->pid) {
844 state->child = child;
850 void winbind_child_died(pid_t pid)
852 struct winbind_child_died_state state = { .pid = pid };
854 forall_children(winbind_child_died_fn, &state);
856 if (state.child == NULL) {
857 DEBUG(5, ("Already reaped child %u died\n", (unsigned int)pid));
861 state.child->pid = 0;
864 /* Ensure any negative cache entries with the netbios or realm names are removed. */
866 void winbindd_flush_negative_conn_cache(struct winbindd_domain *domain)
868 flush_negative_conn_cache_for_domain(domain->name);
869 if (domain->alt_name != NULL) {
870 flush_negative_conn_cache_for_domain(domain->alt_name);
875 * Parent winbindd process sets its own debug level first and then
876 * sends a message to all the winbindd children to adjust their debug
877 * level to that of parents.
880 struct winbind_msg_relay_state {
881 struct messaging_context *msg_ctx;
886 static bool winbind_msg_relay_fn(struct winbindd_child *child,
889 struct winbind_msg_relay_state *state = private_data;
891 DBG_DEBUG("sending message to pid %u.\n",
892 (unsigned int)child->pid);
894 messaging_send(state->msg_ctx, pid_to_procid(child->pid),
895 state->msg_type, state->data);
899 void winbind_msg_debug(struct messaging_context *msg_ctx,
902 struct server_id server_id,
905 struct winbind_msg_relay_state state = {
906 .msg_ctx = msg_ctx, .msg_type = msg_type, .data = data
909 DEBUG(10,("winbind_msg_debug: got debug message.\n"));
911 debug_message(msg_ctx, private_data, MSG_DEBUG, server_id, data);
913 forall_children(winbind_msg_relay_fn, &state);
916 void winbind_disconnect_dc_parent(struct messaging_context *msg_ctx,
919 struct server_id server_id,
922 struct winbind_msg_relay_state state = {
923 .msg_ctx = msg_ctx, .msg_type = msg_type, .data = data
926 DBG_DEBUG("Got disconnect_dc message\n");
928 forall_children(winbind_msg_relay_fn, &state);
931 /* Set our domains as offline and forward the offline message to our children. */
933 struct winbind_msg_on_offline_state {
934 struct messaging_context *msg_ctx;
938 static bool winbind_msg_on_offline_fn(struct winbindd_child *child,
941 struct winbind_msg_on_offline_state *state = private_data;
943 if (child->domain->internal) {
948 * Each winbindd child should only process requests for one
949 * domain - make sure we only set it online / offline for that
952 DBG_DEBUG("sending message to pid %u for domain %s.\n",
953 (unsigned int)child->pid, child->domain->name);
955 messaging_send_buf(state->msg_ctx,
956 pid_to_procid(child->pid),
958 (const uint8_t *)child->domain->name,
959 strlen(child->domain->name)+1);
964 void winbind_msg_offline(struct messaging_context *msg_ctx,
967 struct server_id server_id,
970 struct winbind_msg_on_offline_state state = {
972 .msg_type = MSG_WINBIND_OFFLINE,
974 struct winbindd_domain *domain;
976 DEBUG(10,("winbind_msg_offline: got offline message.\n"));
978 if (!lp_winbind_offline_logon()) {
979 DEBUG(10,("winbind_msg_offline: rejecting offline message.\n"));
983 /* Set our global state as offline. */
984 if (!set_global_winbindd_state_offline()) {
985 DEBUG(10,("winbind_msg_offline: offline request failed.\n"));
989 /* Set all our domains as offline. */
990 for (domain = domain_list(); domain; domain = domain->next) {
991 if (domain->internal) {
994 DEBUG(5,("winbind_msg_offline: marking %s offline.\n", domain->name));
995 set_domain_offline(domain);
998 forall_domain_children(winbind_msg_on_offline_fn, &state);
1001 /* Set our domains as online and forward the online message to our children. */
1003 void winbind_msg_online(struct messaging_context *msg_ctx,
1006 struct server_id server_id,
1009 struct winbind_msg_on_offline_state state = {
1011 .msg_type = MSG_WINBIND_ONLINE,
1013 struct winbindd_domain *domain;
1015 DEBUG(10,("winbind_msg_online: got online message.\n"));
1017 if (!lp_winbind_offline_logon()) {
1018 DEBUG(10,("winbind_msg_online: rejecting online message.\n"));
1022 /* Set our global state as online. */
1023 set_global_winbindd_state_online();
1025 smb_nscd_flush_user_cache();
1026 smb_nscd_flush_group_cache();
1028 /* Set all our domains as online. */
1029 for (domain = domain_list(); domain; domain = domain->next) {
1030 if (domain->internal) {
1033 DEBUG(5,("winbind_msg_online: requesting %s to go online.\n", domain->name));
1035 winbindd_flush_negative_conn_cache(domain);
1036 set_domain_online_request(domain);
1038 /* Send an online message to the idmap child when our
1039 primary domain comes back online */
1041 if ( domain->primary ) {
1042 struct winbindd_child *idmap = idmap_child();
1044 if ( idmap->pid != 0 ) {
1045 messaging_send_buf(msg_ctx,
1046 pid_to_procid(idmap->pid),
1048 (const uint8_t *)domain->name,
1049 strlen(domain->name)+1);
1054 forall_domain_children(winbind_msg_on_offline_fn, &state);
1057 static const char *collect_onlinestatus(TALLOC_CTX *mem_ctx)
1059 struct winbindd_domain *domain;
1062 if ((buf = talloc_asprintf(mem_ctx, "global:%s ",
1063 get_global_winbindd_state_offline() ?
1064 "Offline":"Online")) == NULL) {
1068 for (domain = domain_list(); domain; domain = domain->next) {
1069 if ((buf = talloc_asprintf_append_buffer(buf, "%s:%s ",
1072 "Online":"Offline")) == NULL) {
1077 buf = talloc_asprintf_append_buffer(buf, "\n");
1079 DEBUG(5,("collect_onlinestatus: %s", buf));
1084 void winbind_msg_onlinestatus(struct messaging_context *msg_ctx,
1087 struct server_id server_id,
1090 TALLOC_CTX *mem_ctx;
1091 const char *message;
1093 DEBUG(5,("winbind_msg_onlinestatus received.\n"));
1095 mem_ctx = talloc_init("winbind_msg_onlinestatus");
1096 if (mem_ctx == NULL) {
1100 message = collect_onlinestatus(mem_ctx);
1101 if (message == NULL) {
1102 talloc_destroy(mem_ctx);
1106 messaging_send_buf(msg_ctx, server_id, MSG_WINBIND_ONLINESTATUS,
1107 (const uint8_t *)message, strlen(message) + 1);
1109 talloc_destroy(mem_ctx);
1112 void winbind_msg_dump_domain_list(struct messaging_context *msg_ctx,
1115 struct server_id server_id,
1118 TALLOC_CTX *mem_ctx;
1119 const char *message = NULL;
1120 const char *domain = NULL;
1123 struct winbindd_domain *dom = NULL;
1125 DEBUG(5,("winbind_msg_dump_domain_list received.\n"));
1127 mem_ctx = talloc_init("winbind_msg_dump_domain_list");
1132 if (data->length > 0) {
1133 domain = (const char *)data->data;
1138 DEBUG(5,("winbind_msg_dump_domain_list for domain: %s\n",
1141 message = NDR_PRINT_STRUCT_STRING(mem_ctx, winbindd_domain,
1142 find_domain_from_name_noinit(domain));
1144 talloc_destroy(mem_ctx);
1148 messaging_send_buf(msg_ctx, server_id,
1149 MSG_WINBIND_DUMP_DOMAIN_LIST,
1150 (const uint8_t *)message, strlen(message) + 1);
1152 talloc_destroy(mem_ctx);
1157 DEBUG(5,("winbind_msg_dump_domain_list all domains\n"));
1159 for (dom = domain_list(); dom; dom=dom->next) {
1160 message = NDR_PRINT_STRUCT_STRING(mem_ctx, winbindd_domain, dom);
1162 talloc_destroy(mem_ctx);
1166 s = talloc_asprintf_append(s, "%s\n", message);
1168 talloc_destroy(mem_ctx);
1173 status = messaging_send_buf(msg_ctx, server_id,
1174 MSG_WINBIND_DUMP_DOMAIN_LIST,
1175 (uint8_t *)s, strlen(s) + 1);
1176 if (!NT_STATUS_IS_OK(status)) {
1177 DEBUG(0,("failed to send message: %s\n",
1178 nt_errstr(status)));
1181 talloc_destroy(mem_ctx);
1184 static void account_lockout_policy_handler(struct tevent_context *ctx,
1185 struct tevent_timer *te,
1189 struct winbindd_child *child =
1190 (struct winbindd_child *)private_data;
1191 TALLOC_CTX *mem_ctx = NULL;
1192 struct samr_DomInfo12 lockout_policy;
1195 DEBUG(10,("account_lockout_policy_handler called\n"));
1197 TALLOC_FREE(child->lockout_policy_event);
1199 if ( !winbindd_can_contact_domain( child->domain ) ) {
1200 DEBUG(10,("account_lockout_policy_handler: Removing myself since I "
1201 "do not have an incoming trust to domain %s\n",
1202 child->domain->name));
1207 mem_ctx = talloc_init("account_lockout_policy_handler ctx");
1209 result = NT_STATUS_NO_MEMORY;
1211 result = wb_cache_lockout_policy(child->domain, mem_ctx,
1214 TALLOC_FREE(mem_ctx);
1216 if (!NT_STATUS_IS_OK(result)) {
1217 DEBUG(10,("account_lockout_policy_handler: lockout_policy failed error %s\n",
1218 nt_errstr(result)));
1221 child->lockout_policy_event = tevent_add_timer(global_event_context(), NULL,
1222 timeval_current_ofs(3600, 0),
1223 account_lockout_policy_handler,
1227 static time_t get_machine_password_timeout(void)
1229 /* until we have gpo support use lp setting */
1230 return lp_machine_password_timeout();
1233 static bool calculate_next_machine_pwd_change(const char *domain,
1236 time_t pass_last_set_time;
1242 pw = secrets_fetch_machine_password(domain,
1243 &pass_last_set_time,
1247 DEBUG(0,("cannot fetch own machine password ????"));
1253 timeout = get_machine_password_timeout();
1255 DEBUG(10,("machine password never expires\n"));
1259 tv.tv_sec = pass_last_set_time;
1260 DEBUG(10, ("password last changed %s\n",
1261 timeval_string(talloc_tos(), &tv, false)));
1262 tv.tv_sec += timeout;
1263 DEBUGADD(10, ("password valid until %s\n",
1264 timeval_string(talloc_tos(), &tv, false)));
1266 if (time(NULL) < (pass_last_set_time + timeout)) {
1267 next_change = pass_last_set_time + timeout;
1268 DEBUG(10,("machine password still valid until: %s\n",
1269 http_timestring(talloc_tos(), next_change)));
1270 *t = timeval_set(next_change, 0);
1272 if (lp_clustering()) {
1275 * When having a cluster, we have several
1276 * winbinds racing for the password change. In
1277 * the machine_password_change_handler()
1278 * function we check if someone else was
1279 * faster when the event triggers. We add a
1280 * 255-second random delay here, so that we
1281 * don't run to change the password at the
1282 * exact same moment.
1284 generate_random_buffer(&randbuf, sizeof(randbuf));
1285 DEBUG(10, ("adding %d seconds randomness\n",
1287 t->tv_sec += randbuf;
1292 DEBUG(10,("machine password expired, needs immediate change\n"));
1294 *t = timeval_zero();
1299 static void machine_password_change_handler(struct tevent_context *ctx,
1300 struct tevent_timer *te,
1304 struct messaging_context *msg_ctx = global_messaging_context();
1305 struct winbindd_child *child =
1306 (struct winbindd_child *)private_data;
1307 struct rpc_pipe_client *netlogon_pipe = NULL;
1308 struct netlogon_creds_cli_context *netlogon_creds_ctx = NULL;
1310 struct timeval next_change;
1312 DEBUG(10,("machine_password_change_handler called\n"));
1314 TALLOC_FREE(child->machine_password_change_event);
1316 if (!calculate_next_machine_pwd_change(child->domain->name,
1318 DEBUG(10, ("calculate_next_machine_pwd_change failed\n"));
1322 DEBUG(10, ("calculate_next_machine_pwd_change returned %s\n",
1323 timeval_string(talloc_tos(), &next_change, false)));
1325 if (!timeval_expired(&next_change)) {
1326 DEBUG(10, ("Someone else has already changed the pw\n"));
1330 if (!winbindd_can_contact_domain(child->domain)) {
1331 DEBUG(10,("machine_password_change_handler: Removing myself since I "
1332 "do not have an incoming trust to domain %s\n",
1333 child->domain->name));
1337 result = cm_connect_netlogon_secure(child->domain,
1339 &netlogon_creds_ctx);
1340 if (!NT_STATUS_IS_OK(result)) {
1341 DEBUG(10,("machine_password_change_handler: "
1342 "failed to connect netlogon pipe: %s\n",
1343 nt_errstr(result)));
1347 result = trust_pw_change(netlogon_creds_ctx,
1349 netlogon_pipe->binding_handle,
1350 child->domain->name,
1351 child->domain->dcname,
1354 DEBUG(10, ("machine_password_change_handler: "
1355 "trust_pw_change returned %s\n",
1356 nt_errstr(result)));
1358 if (NT_STATUS_EQUAL(result, NT_STATUS_ACCESS_DENIED) ) {
1359 DEBUG(3,("machine_password_change_handler: password set returned "
1360 "ACCESS_DENIED. Maybe the trust account "
1361 "password was changed and we didn't know it. "
1362 "Killing connections to domain %s\n",
1363 child->domain->name));
1364 invalidate_cm_connection(child->domain);
1367 if (!calculate_next_machine_pwd_change(child->domain->name,
1369 DEBUG(10, ("calculate_next_machine_pwd_change failed\n"));
1373 DEBUG(10, ("calculate_next_machine_pwd_change returned %s\n",
1374 timeval_string(talloc_tos(), &next_change, false)));
1376 if (!NT_STATUS_IS_OK(result)) {
1379 * In case of failure, give the DC a minute to recover
1381 tmp = timeval_current_ofs(60, 0);
1382 next_change = timeval_max(&next_change, &tmp);
1386 child->machine_password_change_event = tevent_add_timer(global_event_context(), NULL,
1388 machine_password_change_handler,
1392 /* Deal with a request to go offline. */
1394 static void child_msg_offline(struct messaging_context *msg,
1397 struct server_id server_id,
1400 struct winbindd_domain *domain;
1401 struct winbindd_domain *primary_domain = NULL;
1402 const char *domainname = (const char *)data->data;
1404 if (data->data == NULL || data->length == 0) {
1408 DEBUG(5,("child_msg_offline received for domain %s.\n", domainname));
1410 if (!lp_winbind_offline_logon()) {
1411 DEBUG(10,("child_msg_offline: rejecting offline message.\n"));
1415 primary_domain = find_our_domain();
1417 /* Mark the requested domain offline. */
1419 for (domain = domain_list(); domain; domain = domain->next) {
1420 if (domain->internal) {
1423 if (strequal(domain->name, domainname)) {
1424 DEBUG(5,("child_msg_offline: marking %s offline.\n", domain->name));
1425 set_domain_offline(domain);
1426 /* we are in the trusted domain, set the primary domain
1428 if (domain != primary_domain) {
1429 set_domain_offline(primary_domain);
1435 /* Deal with a request to go online. */
1437 static void child_msg_online(struct messaging_context *msg,
1440 struct server_id server_id,
1443 struct winbindd_domain *domain;
1444 struct winbindd_domain *primary_domain = NULL;
1445 const char *domainname = (const char *)data->data;
1447 if (data->data == NULL || data->length == 0) {
1451 DEBUG(5,("child_msg_online received for domain %s.\n", domainname));
1453 if (!lp_winbind_offline_logon()) {
1454 DEBUG(10,("child_msg_online: rejecting online message.\n"));
1458 primary_domain = find_our_domain();
1460 /* Set our global state as online. */
1461 set_global_winbindd_state_online();
1463 /* Try and mark everything online - delete any negative cache entries
1464 to force a reconnect now. */
1466 for (domain = domain_list(); domain; domain = domain->next) {
1467 if (domain->internal) {
1470 if (strequal(domain->name, domainname)) {
1471 DEBUG(5,("child_msg_online: requesting %s to go online.\n", domain->name));
1472 winbindd_flush_negative_conn_cache(domain);
1473 set_domain_online_request(domain);
1475 /* we can be in trusted domain, which will contact primary domain
1476 * we have to bring primary domain online in trusted domain process
1477 * see, winbindd_dual_pam_auth() --> winbindd_dual_pam_auth_samlogon()
1478 * --> contact_domain = find_our_domain()
1480 if (domain != primary_domain) {
1481 winbindd_flush_negative_conn_cache(primary_domain);
1482 set_domain_online_request(primary_domain);
1488 struct winbindd_reinit_after_fork_state {
1489 const struct winbindd_child *myself;
1492 static bool winbindd_reinit_after_fork_fn(struct winbindd_child *child,
1495 struct winbindd_reinit_after_fork_state *state = private_data;
1497 if (child == state->myself) {
1501 /* Destroy all possible events in child list. */
1502 TALLOC_FREE(child->lockout_policy_event);
1503 TALLOC_FREE(child->machine_password_change_event);
1506 * Children should never be able to send each other messages,
1507 * all messages must go through the parent.
1509 child->pid = (pid_t)0;
1512 * Close service sockets to all other children
1514 if (child->sock != -1) {
1522 NTSTATUS winbindd_reinit_after_fork(const struct winbindd_child *myself,
1523 const char *logfilename)
1525 struct winbindd_reinit_after_fork_state state = { .myself = myself };
1526 struct winbindd_domain *domain;
1529 status = reinit_after_fork(
1530 global_messaging_context(),
1531 global_event_context(),
1533 if (!NT_STATUS_IS_OK(status)) {
1534 DEBUG(0,("reinit_after_fork() failed\n"));
1537 initialize_password_db(true, global_event_context());
1539 close_conns_after_fork();
1541 if (!override_logfile && logfilename) {
1542 lp_set_logfile(logfilename);
1546 if (!winbindd_setup_sig_term_handler(false))
1547 return NT_STATUS_NO_MEMORY;
1548 if (!winbindd_setup_sig_hup_handler(override_logfile ? NULL :
1550 return NT_STATUS_NO_MEMORY;
1552 /* Stop zombies in children */
1555 /* Don't handle the same messages as our parent. */
1556 messaging_deregister(global_messaging_context(),
1557 MSG_SMB_CONF_UPDATED, NULL);
1558 messaging_deregister(global_messaging_context(),
1559 MSG_SHUTDOWN, NULL);
1560 messaging_deregister(global_messaging_context(),
1561 MSG_WINBIND_OFFLINE, NULL);
1562 messaging_deregister(global_messaging_context(),
1563 MSG_WINBIND_ONLINE, NULL);
1564 messaging_deregister(global_messaging_context(),
1565 MSG_WINBIND_ONLINESTATUS, NULL);
1566 messaging_deregister(global_messaging_context(),
1567 MSG_WINBIND_DUMP_DOMAIN_LIST, NULL);
1568 messaging_deregister(global_messaging_context(),
1571 messaging_deregister(global_messaging_context(),
1572 MSG_WINBIND_DOMAIN_OFFLINE, NULL);
1573 messaging_deregister(global_messaging_context(),
1574 MSG_WINBIND_DOMAIN_ONLINE, NULL);
1576 /* We have destroyed all events in the winbindd_event_context
1577 * in reinit_after_fork(), so clean out all possible pending
1578 * event pointers. */
1580 /* Deal with check_online_events. */
1582 for (domain = domain_list(); domain; domain = domain->next) {
1583 TALLOC_FREE(domain->check_online_event);
1586 /* Ensure we're not handling a credential cache event inherited
1587 * from our parent. */
1589 ccache_remove_all_after_fork();
1591 forall_children(winbindd_reinit_after_fork_fn, &state);
1593 return NT_STATUS_OK;
1597 * In a child there will be only one domain, reference that here.
1599 static struct winbindd_domain *child_domain;
1601 struct winbindd_domain *wb_child_domain(void)
1603 return child_domain;
1606 struct child_handler_state {
1607 struct winbindd_child *child;
1608 struct winbindd_cli_state cli;
1611 static void child_handler(struct tevent_context *ev, struct tevent_fd *fde,
1612 uint16_t flags, void *private_data)
1614 struct child_handler_state *state =
1615 (struct child_handler_state *)private_data;
1618 /* fetch a request from the main daemon */
1619 status = child_read_request(state->cli.sock, state->cli.request);
1621 if (!NT_STATUS_IS_OK(status)) {
1622 /* we lost contact with our parent */
1626 DEBUG(4,("child daemon request %d\n",
1627 (int)state->cli.request->cmd));
1629 ZERO_STRUCTP(state->cli.response);
1630 state->cli.request->null_term = '\0';
1631 state->cli.mem_ctx = talloc_tos();
1632 child_process_request(state->child, &state->cli);
1634 DEBUG(4, ("Finished processing child request %d\n",
1635 (int)state->cli.request->cmd));
1637 SAFE_FREE(state->cli.request->extra_data.data);
1639 status = child_write_response(state->cli.sock, state->cli.response);
1640 if (!NT_STATUS_IS_OK(status)) {
1645 static bool fork_domain_child(struct winbindd_child *child)
1648 struct child_handler_state state;
1649 struct winbindd_request request;
1650 struct winbindd_response response;
1651 struct winbindd_domain *primary_domain = NULL;
1654 struct tevent_fd *fde;
1656 if (child->domain) {
1657 DEBUG(10, ("fork_domain_child called for domain '%s'\n",
1658 child->domain->name));
1660 DEBUG(10, ("fork_domain_child called without domain.\n"));
1663 if (socketpair(AF_UNIX, SOCK_STREAM, 0, fdpair) != 0) {
1664 DEBUG(0, ("Could not open child pipe: %s\n",
1670 state.child = child;
1671 state.cli.pid = getpid();
1672 state.cli.request = &request;
1673 state.cli.response = &response;
1675 child->pid = fork();
1677 if (child->pid == -1) {
1678 DEBUG(0, ("Could not fork: %s\n", strerror(errno)));
1684 if (child->pid != 0) {
1690 nread = sys_read(fdpair[1], &status, sizeof(status));
1691 if (nread != sizeof(status)) {
1692 DEBUG(1, ("fork_domain_child: Could not read child status: "
1693 "nread=%d, error=%s\n", (int)nread,
1698 if (!NT_STATUS_IS_OK(status)) {
1699 DEBUG(1, ("fork_domain_child: Child status is %s\n",
1700 nt_errstr(status)));
1705 child->monitor_fde = tevent_add_fd(global_event_context(),
1706 global_event_context(),
1709 child_socket_readable,
1711 if (child->monitor_fde == NULL) {
1712 DBG_WARNING("tevent_add_fd failed\n");
1717 child->sock = fdpair[1];
1718 set_blocking(child->sock, false);
1723 child_domain = child->domain;
1725 DEBUG(10, ("Child process %d\n", (int)getpid()));
1727 state.cli.sock = fdpair[0];
1730 status = winbindd_reinit_after_fork(child, child->logfilename);
1732 nwritten = sys_write(state.cli.sock, &status, sizeof(status));
1733 if (nwritten != sizeof(status)) {
1734 DEBUG(1, ("fork_domain_child: Could not write status: "
1735 "nwritten=%d, error=%s\n", (int)nwritten,
1739 if (!NT_STATUS_IS_OK(status)) {
1740 DEBUG(1, ("winbindd_reinit_after_fork failed: %s\n",
1741 nt_errstr(status)));
1745 if (child_domain != NULL) {
1746 setproctitle("domain child [%s]", child_domain->name);
1747 } else if (child == idmap_child()) {
1748 setproctitle("idmap child");
1751 /* Handle online/offline messages. */
1752 messaging_register(global_messaging_context(), NULL,
1753 MSG_WINBIND_OFFLINE, child_msg_offline);
1754 messaging_register(global_messaging_context(), NULL,
1755 MSG_WINBIND_ONLINE, child_msg_online);
1756 messaging_register(global_messaging_context(), NULL,
1757 MSG_DEBUG, debug_message);
1758 messaging_register(global_messaging_context(), NULL,
1759 MSG_WINBIND_IP_DROPPED,
1760 winbind_msg_ip_dropped);
1761 messaging_register(global_messaging_context(), NULL,
1762 MSG_WINBIND_DISCONNECT_DC,
1763 winbind_msg_disconnect_dc);
1765 primary_domain = find_our_domain();
1767 if (primary_domain == NULL) {
1768 smb_panic("no primary domain found");
1771 /* It doesn't matter if we allow cache login,
1772 * try to bring domain online after fork. */
1773 if ( child->domain ) {
1774 child->domain->startup = True;
1775 child->domain->startup_time = time_mono(NULL);
1776 /* we can be in primary domain or in trusted domain
1777 * If we are in trusted domain, set the primary domain
1778 * in start-up mode */
1779 if (!(child->domain->internal)) {
1780 set_domain_online_request(child->domain);
1781 if (!(child->domain->primary)) {
1782 primary_domain->startup = True;
1783 primary_domain->startup_time = time_mono(NULL);
1784 set_domain_online_request(primary_domain);
1790 * We are in idmap child, make sure that we set the
1791 * check_online_event to bring primary domain online.
1793 if (child == idmap_child()) {
1794 set_domain_online_request(primary_domain);
1797 /* We might be in the idmap child...*/
1798 if (child->domain && !(child->domain->internal) &&
1799 lp_winbind_offline_logon()) {
1801 set_domain_online_request(child->domain);
1803 if (primary_domain && (primary_domain != child->domain)) {
1804 /* We need to talk to the primary
1805 * domain as well as the trusted
1806 * domain inside a trusted domain
1809 * set_dc_type_and_flags_trustinfo()
1812 set_domain_online_request(primary_domain);
1815 child->lockout_policy_event = tevent_add_timer(
1816 global_event_context(), NULL, timeval_zero(),
1817 account_lockout_policy_handler,
1821 if (child->domain && child->domain->primary &&
1822 !USE_KERBEROS_KEYTAB &&
1823 lp_server_role() == ROLE_DOMAIN_MEMBER) {
1825 struct timeval next_change;
1827 if (calculate_next_machine_pwd_change(child->domain->name,
1829 child->machine_password_change_event = tevent_add_timer(
1830 global_event_context(), NULL, next_change,
1831 machine_password_change_handler,
1836 fde = tevent_add_fd(global_event_context(), NULL, state.cli.sock,
1837 TEVENT_FD_READ, child_handler, &state);
1839 DEBUG(1, ("tevent_add_fd failed\n"));
1846 TALLOC_CTX *frame = talloc_stackframe();
1848 ret = tevent_loop_once(global_event_context());
1850 DEBUG(1, ("tevent_loop_once failed: %s\n",
1855 if (child->domain && child->domain->startup &&
1856 (time_mono(NULL) > child->domain->startup_time + 30)) {
1857 /* No longer in "startup" mode. */
1858 DEBUG(10,("fork_domain_child: domain %s no longer in 'startup' mode.\n",
1859 child->domain->name ));
1860 child->domain->startup = False;
1867 void winbind_msg_ip_dropped_parent(struct messaging_context *msg_ctx,
1870 struct server_id server_id,
1873 struct winbind_msg_relay_state state = {
1875 .msg_type = msg_type,
1879 winbind_msg_ip_dropped(msg_ctx, private_data, msg_type,
1882 forall_children(winbind_msg_relay_fn, &state);