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"
45 #include "lib/global_contexts.h"
48 #define DBGC_CLASS DBGC_WINBIND
50 extern bool override_logfile;
52 static void forall_domain_children(bool (*fn)(struct winbindd_child *c,
56 struct winbindd_domain *d;
58 for (d = domain_list(); d != NULL; d = d->next) {
61 for (i = 0; i < lp_winbind_max_domain_connections(); i++) {
62 struct winbindd_child *c = &d->children[i];
69 ok = fn(c, private_data);
77 static void forall_children(bool (*fn)(struct winbindd_child *c,
81 struct winbindd_child *c;
86 ok = fn(c, private_data);
94 ok = fn(c, private_data);
100 forall_domain_children(fn, private_data);
103 /* Read some data from a client connection */
105 static NTSTATUS child_read_request(int sock, struct winbindd_request *wreq)
109 status = read_data_ntstatus(sock, (char *)wreq, sizeof(*wreq));
110 if (!NT_STATUS_IS_OK(status)) {
111 DEBUG(3, ("child_read_request: read_data failed: %s\n",
116 if (wreq->extra_len == 0) {
117 wreq->extra_data.data = NULL;
121 DEBUG(10, ("Need to read %d extra bytes\n", (int)wreq->extra_len));
123 wreq->extra_data.data = SMB_MALLOC_ARRAY(char, wreq->extra_len + 1);
124 if (wreq->extra_data.data == NULL) {
125 DEBUG(0, ("malloc failed\n"));
126 return NT_STATUS_NO_MEMORY;
129 /* Ensure null termination */
130 wreq->extra_data.data[wreq->extra_len] = '\0';
132 status = read_data_ntstatus(sock, wreq->extra_data.data,
134 if (!NT_STATUS_IS_OK(status)) {
135 DEBUG(0, ("Could not read extra data: %s\n",
141 static NTSTATUS child_write_response(int sock, struct winbindd_response *wrsp)
146 iov[0].iov_base = (void *)wrsp;
147 iov[0].iov_len = sizeof(struct winbindd_response);
150 if (wrsp->length > sizeof(struct winbindd_response)) {
151 iov[1].iov_base = (void *)wrsp->extra_data.data;
152 iov[1].iov_len = wrsp->length-iov[0].iov_len;
156 DEBUG(10, ("Writing %d bytes to parent\n", (int)wrsp->length));
158 if (write_data_iov(sock, iov, iov_count) != wrsp->length) {
159 DEBUG(0, ("Could not write result\n"));
160 return NT_STATUS_INVALID_HANDLE;
167 * Do winbind child async request. This is not simply wb_simple_trans. We have
168 * to do the queueing ourselves because while a request is queued, the child
169 * might have crashed, and we have to re-fork it in the _trigger function.
172 struct wb_child_request_state {
173 struct tevent_context *ev;
174 struct tevent_req *queue_subreq;
175 struct tevent_req *subreq;
176 struct winbindd_child *child;
177 struct winbindd_request *request;
178 struct winbindd_response *response;
181 static bool fork_domain_child(struct winbindd_child *child);
183 static void wb_child_request_waited(struct tevent_req *subreq);
184 static void wb_child_request_done(struct tevent_req *subreq);
185 static void wb_child_request_orphaned(struct tevent_req *subreq);
187 static void wb_child_request_cleanup(struct tevent_req *req,
188 enum tevent_req_state req_state);
190 struct tevent_req *wb_child_request_send(TALLOC_CTX *mem_ctx,
191 struct tevent_context *ev,
192 struct winbindd_child *child,
193 struct winbindd_request *request)
195 struct tevent_req *req;
196 struct wb_child_request_state *state;
197 struct tevent_req *subreq;
199 req = tevent_req_create(mem_ctx, &state,
200 struct wb_child_request_state);
206 state->child = child;
209 * We have to make a copy of "request", because our caller
210 * might drop us via talloc_free().
212 * The talloc_move() magic in wb_child_request_cleanup() keeps
213 * all the requests, but if we are sitting deep within
214 * writev_send() down to the client, we have given it the
215 * pointer to "request". As our caller lost interest, it will
216 * just free "request", while writev_send still references it.
219 state->request = talloc_memdup(state, request, sizeof(*request));
220 if (tevent_req_nomem(state->request, req)) {
221 return tevent_req_post(req, ev);
224 if (request->extra_data.data != NULL) {
225 state->request->extra_data.data = talloc_memdup(
227 request->extra_data.data,
229 if (tevent_req_nomem(state->request->extra_data.data, req)) {
230 return tevent_req_post(req, ev);
234 subreq = tevent_queue_wait_send(state, ev, child->queue);
235 if (tevent_req_nomem(subreq, req)) {
236 return tevent_req_post(req, ev);
238 tevent_req_set_callback(subreq, wb_child_request_waited, req);
239 state->queue_subreq = subreq;
241 tevent_req_set_cleanup_fn(req, wb_child_request_cleanup);
246 static void wb_child_request_waited(struct tevent_req *subreq)
248 struct tevent_req *req = tevent_req_callback_data(
249 subreq, struct tevent_req);
250 struct wb_child_request_state *state = tevent_req_data(
251 req, struct wb_child_request_state);
254 ok = tevent_queue_wait_recv(subreq);
260 * We need to keep state->queue_subreq
261 * in order to block the queue.
265 if ((state->child->sock == -1) && (!fork_domain_child(state->child))) {
266 tevent_req_error(req, errno);
270 tevent_fd_set_flags(state->child->monitor_fde, 0);
272 subreq = wb_simple_trans_send(state, global_event_context(), NULL,
273 state->child->sock, state->request);
274 if (tevent_req_nomem(subreq, req)) {
278 state->subreq = subreq;
279 tevent_req_set_callback(subreq, wb_child_request_done, req);
280 tevent_req_set_endtime(req, state->ev, timeval_current_ofs(300, 0));
283 static void wb_child_request_done(struct tevent_req *subreq)
285 struct tevent_req *req = tevent_req_callback_data(
286 subreq, struct tevent_req);
287 struct wb_child_request_state *state = tevent_req_data(
288 req, struct wb_child_request_state);
291 ret = wb_simple_trans_recv(subreq, state, &state->response, &err);
292 /* Freeing the subrequest is deferred until the cleanup function,
293 * which has to know whether a subrequest exists, and consequently
294 * decide whether to shut down the pipe to the child process.
297 tevent_req_error(req, err);
300 tevent_req_done(req);
303 static void wb_child_request_orphaned(struct tevent_req *subreq)
305 struct winbindd_child *child =
306 (struct winbindd_child *)tevent_req_callback_data_void(subreq);
308 DBG_WARNING("cleanup orphaned subreq[%p]\n", subreq);
311 if (child->domain != NULL) {
313 * If the child is attached to a domain,
314 * we need to make sure the domain queue
315 * can move forward, after the orphaned
318 tevent_queue_start(child->domain->queue);
322 int wb_child_request_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
323 struct winbindd_response **presponse, int *err)
325 struct wb_child_request_state *state = tevent_req_data(
326 req, struct wb_child_request_state);
328 if (tevent_req_is_unix_error(req, err)) {
331 *presponse = talloc_move(mem_ctx, &state->response);
335 static void wb_child_request_cleanup(struct tevent_req *req,
336 enum tevent_req_state req_state)
338 struct wb_child_request_state *state =
339 tevent_req_data(req, struct wb_child_request_state);
341 if (state->subreq == NULL) {
342 /* nothing to cleanup */
346 if (req_state == TEVENT_REQ_RECEIVED) {
347 struct tevent_req *subreq = NULL;
350 * Our caller gave up, but we need to keep
351 * the low level request (wb_simple_trans)
352 * in order to maintain the parent child protocol.
354 * We also need to keep the child queue blocked
355 * until we got the response from the child.
358 subreq = talloc_move(state->child->queue, &state->subreq);
359 talloc_move(subreq, &state->queue_subreq);
360 talloc_move(subreq, &state->request);
361 tevent_req_set_callback(subreq,
362 wb_child_request_orphaned,
365 DBG_WARNING("keep orphaned subreq[%p]\n", subreq);
369 TALLOC_FREE(state->subreq);
370 TALLOC_FREE(state->queue_subreq);
372 tevent_fd_set_flags(state->child->monitor_fde, TEVENT_FD_READ);
374 if (state->child->domain != NULL) {
376 * If the child is attached to a domain,
377 * we need to make sure the domain queue
378 * can move forward, after the request
381 tevent_queue_start(state->child->domain->queue);
384 if (req_state == TEVENT_REQ_DONE) {
385 /* transmitted request and got response */
390 * Failed to transmit and receive response, or request
391 * cancelled while being serviced.
392 * The basic parent/child communication broke, close
395 TALLOC_FREE(state->child->monitor_fde);
396 close(state->child->sock);
397 state->child->sock = -1;
400 static void child_socket_readable(struct tevent_context *ev,
401 struct tevent_fd *fde,
405 struct winbindd_child *child = private_data;
407 if ((flags & TEVENT_FD_READ) == 0) {
411 TALLOC_FREE(child->monitor_fde);
414 * We're only active when there is no outstanding child
415 * request. Arriving here means the child closed its socket,
416 * it died. Do the same here.
419 SMB_ASSERT(child->sock != -1);
425 static struct winbindd_child *choose_domain_child(struct winbindd_domain *domain)
427 struct winbindd_child *shortest = &domain->children[0];
428 struct winbindd_child *current;
431 for (i=0; i<lp_winbind_max_domain_connections(); i++) {
432 size_t shortest_len, current_len;
434 current = &domain->children[i];
435 current_len = tevent_queue_length(current->queue);
437 if (current_len == 0) {
442 shortest_len = tevent_queue_length(shortest->queue);
444 if (current_len < shortest_len) {
452 struct dcerpc_binding_handle *dom_child_handle(struct winbindd_domain *domain)
454 return domain->binding_handle;
457 struct wb_domain_request_state {
458 struct tevent_context *ev;
459 struct tevent_queue_entry *queue_entry;
460 struct winbindd_domain *domain;
461 struct winbindd_child *child;
462 struct winbindd_request *request;
463 struct winbindd_request *init_req;
464 struct winbindd_response *response;
465 struct tevent_req *pending_subreq;
468 static void wb_domain_request_cleanup(struct tevent_req *req,
469 enum tevent_req_state req_state)
471 struct wb_domain_request_state *state = tevent_req_data(
472 req, struct wb_domain_request_state);
475 * If we're completely done or got a failure.
476 * we should remove ourself from the domain queue,
477 * after removing the child subreq from the child queue
478 * and give the next one in the queue the chance
479 * to check for an idle child.
481 TALLOC_FREE(state->pending_subreq);
482 TALLOC_FREE(state->queue_entry);
483 tevent_queue_start(state->domain->queue);
486 static void wb_domain_request_trigger(struct tevent_req *req,
488 static void wb_domain_request_gotdc(struct tevent_req *subreq);
489 static void wb_domain_request_initialized(struct tevent_req *subreq);
490 static void wb_domain_request_done(struct tevent_req *subreq);
492 struct tevent_req *wb_domain_request_send(TALLOC_CTX *mem_ctx,
493 struct tevent_context *ev,
494 struct winbindd_domain *domain,
495 struct winbindd_request *request)
497 struct tevent_req *req;
498 struct wb_domain_request_state *state;
500 req = tevent_req_create(mem_ctx, &state,
501 struct wb_domain_request_state);
506 state->domain = domain;
508 state->request = request;
510 tevent_req_set_cleanup_fn(req, wb_domain_request_cleanup);
512 state->queue_entry = tevent_queue_add_entry(
513 domain->queue, state->ev, req,
514 wb_domain_request_trigger, NULL);
515 if (tevent_req_nomem(state->queue_entry, req)) {
516 return tevent_req_post(req, ev);
522 static void wb_domain_request_trigger(struct tevent_req *req,
525 struct wb_domain_request_state *state = tevent_req_data(
526 req, struct wb_domain_request_state);
527 struct winbindd_domain *domain = state->domain;
528 struct tevent_req *subreq = NULL;
529 size_t shortest_queue_length;
531 state->child = choose_domain_child(domain);
532 shortest_queue_length = tevent_queue_length(state->child->queue);
533 if (shortest_queue_length > 0) {
535 * All children are busy, we need to stop
536 * the queue and untrigger our own queue
537 * entry. Once a pending request
538 * is done it calls tevent_queue_start
539 * and we get retriggered.
542 tevent_queue_stop(state->domain->queue);
543 tevent_queue_entry_untrigger(state->queue_entry);
547 if (domain->initialized) {
548 subreq = wb_child_request_send(state, state->ev, state->child,
550 if (tevent_req_nomem(subreq, req)) {
553 tevent_req_set_callback(subreq, wb_domain_request_done, req);
554 state->pending_subreq = subreq;
557 * Once the domain is initialized and
558 * once we placed our real request into the child queue,
559 * we can remove ourself from the domain queue
560 * and give the next one in the queue the chance
561 * to check for an idle child.
563 TALLOC_FREE(state->queue_entry);
567 state->init_req = talloc_zero(state, struct winbindd_request);
568 if (tevent_req_nomem(state->init_req, req)) {
572 if (IS_DC || domain->primary || domain->internal) {
573 /* The primary domain has to find the DC name itself */
574 state->init_req->cmd = WINBINDD_INIT_CONNECTION;
575 fstrcpy(state->init_req->domain_name, domain->name);
576 state->init_req->data.init_conn.is_primary = domain->primary;
577 fstrcpy(state->init_req->data.init_conn.dcname, "");
579 subreq = wb_child_request_send(state, state->ev, state->child,
581 if (tevent_req_nomem(subreq, req)) {
584 tevent_req_set_callback(subreq, wb_domain_request_initialized,
586 state->pending_subreq = subreq;
591 * This is *not* the primary domain,
592 * let's ask our DC about a DC name.
594 * We prefer getting a dns name in dc_unc,
595 * which is indicated by DS_RETURN_DNS_NAME.
596 * For NT4 domains we still get the netbios name.
598 subreq = wb_dsgetdcname_send(state, state->ev,
600 NULL, /* domain_guid */
601 NULL, /* site_name */
602 DS_RETURN_DNS_NAME); /* flags */
603 if (tevent_req_nomem(subreq, req)) {
606 tevent_req_set_callback(subreq, wb_domain_request_gotdc, req);
607 state->pending_subreq = subreq;
611 static void wb_domain_request_gotdc(struct tevent_req *subreq)
613 struct tevent_req *req = tevent_req_callback_data(
614 subreq, struct tevent_req);
615 struct wb_domain_request_state *state = tevent_req_data(
616 req, struct wb_domain_request_state);
617 struct netr_DsRGetDCNameInfo *dcinfo = NULL;
619 const char *dcname = NULL;
621 state->pending_subreq = NULL;
623 status = wb_dsgetdcname_recv(subreq, state, &dcinfo);
625 if (tevent_req_nterror(req, status)) {
628 dcname = dcinfo->dc_unc;
629 while (dcname != NULL && *dcname == '\\') {
632 state->init_req->cmd = WINBINDD_INIT_CONNECTION;
633 fstrcpy(state->init_req->domain_name, state->domain->name);
634 state->init_req->data.init_conn.is_primary = False;
635 fstrcpy(state->init_req->data.init_conn.dcname,
640 subreq = wb_child_request_send(state, state->ev, state->child,
642 if (tevent_req_nomem(subreq, req)) {
645 tevent_req_set_callback(subreq, wb_domain_request_initialized, req);
646 state->pending_subreq = subreq;
649 static void wb_domain_request_initialized(struct tevent_req *subreq)
651 struct tevent_req *req = tevent_req_callback_data(
652 subreq, struct tevent_req);
653 struct wb_domain_request_state *state = tevent_req_data(
654 req, struct wb_domain_request_state);
655 struct winbindd_response *response;
658 state->pending_subreq = NULL;
660 ret = wb_child_request_recv(subreq, talloc_tos(), &response, &err);
663 tevent_req_error(req, err);
667 if (!string_to_sid(&state->domain->sid,
668 response->data.domain_info.sid)) {
669 DEBUG(1,("init_child_recv: Could not convert sid %s "
670 "from string\n", response->data.domain_info.sid));
671 tevent_req_error(req, EINVAL);
675 talloc_free(state->domain->name);
676 state->domain->name = talloc_strdup(state->domain,
677 response->data.domain_info.name);
678 if (state->domain->name == NULL) {
679 tevent_req_error(req, ENOMEM);
683 if (response->data.domain_info.alt_name[0] != '\0') {
684 talloc_free(state->domain->alt_name);
686 state->domain->alt_name = talloc_strdup(state->domain,
687 response->data.domain_info.alt_name);
688 if (state->domain->alt_name == NULL) {
689 tevent_req_error(req, ENOMEM);
694 state->domain->native_mode = response->data.domain_info.native_mode;
695 state->domain->active_directory =
696 response->data.domain_info.active_directory;
697 state->domain->initialized = true;
699 TALLOC_FREE(response);
701 subreq = wb_child_request_send(state, state->ev, state->child,
703 if (tevent_req_nomem(subreq, req)) {
706 tevent_req_set_callback(subreq, wb_domain_request_done, req);
707 state->pending_subreq = subreq;
710 * Once the domain is initialized and
711 * once we placed our real request into the child queue,
712 * we can remove ourself from the domain queue
713 * and give the next one in the queue the chance
714 * to check for an idle child.
716 TALLOC_FREE(state->queue_entry);
719 static void wb_domain_request_done(struct tevent_req *subreq)
721 struct tevent_req *req = tevent_req_callback_data(
722 subreq, struct tevent_req);
723 struct wb_domain_request_state *state = tevent_req_data(
724 req, struct wb_domain_request_state);
727 state->pending_subreq = NULL;
729 ret = wb_child_request_recv(subreq, talloc_tos(), &state->response,
733 tevent_req_error(req, err);
736 tevent_req_done(req);
739 int wb_domain_request_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
740 struct winbindd_response **presponse, int *err)
742 struct wb_domain_request_state *state = tevent_req_data(
743 req, struct wb_domain_request_state);
745 if (tevent_req_is_unix_error(req, err)) {
748 *presponse = talloc_move(mem_ctx, &state->response);
752 static void child_process_request(struct winbindd_child *child,
753 struct winbindd_cli_state *state)
755 struct winbindd_domain *domain = child->domain;
756 const struct winbindd_child_dispatch_table *table = child->table;
758 /* Free response data - we may be interrupted and receive another
759 command before being able to send this data off. */
761 state->response->result = WINBINDD_ERROR;
762 state->response->length = sizeof(struct winbindd_response);
764 /* as all requests in the child are sync, we can use talloc_tos() */
765 state->mem_ctx = talloc_tos();
767 /* Process command */
769 for (; table->name; table++) {
770 if (state->request->cmd == table->struct_cmd) {
771 DEBUG(10,("child_process_request: request fn %s\n",
773 state->response->result = table->struct_fn(domain, state);
778 DEBUG(1, ("child_process_request: unknown request fn number %d\n",
779 (int)state->request->cmd));
780 state->response->result = WINBINDD_ERROR;
783 void setup_child(struct winbindd_domain *domain, struct winbindd_child *child,
784 const struct winbindd_child_dispatch_table *table,
785 const char *logprefix,
788 const struct loadparm_substitution *lp_sub =
789 loadparm_s3_global_substitution();
791 if (logprefix && logname) {
792 char *logbase = NULL;
794 if (*lp_logfile(talloc_tos(), lp_sub)) {
797 if (asprintf(&logbase, "%s", lp_logfile(talloc_tos(), lp_sub)) < 0) {
798 smb_panic("Internal error: asprintf failed");
801 if ((end = strrchr_m(logbase, '/'))) {
805 if (asprintf(&logbase, "%s", get_dyn_LOGFILEBASE()) < 0) {
806 smb_panic("Internal error: asprintf failed");
810 if (asprintf(&child->logfilename, "%s/%s-%s",
811 logbase, logprefix, logname) < 0) {
813 smb_panic("Internal error: asprintf failed");
818 smb_panic("Internal error: logprefix == NULL && "
824 child->domain = domain;
825 child->table = table;
826 child->queue = tevent_queue_create(NULL, "winbind_child");
827 SMB_ASSERT(child->queue != NULL);
828 if (domain == NULL) {
829 child->binding_handle = wbint_binding_handle(NULL, NULL, child);
830 SMB_ASSERT(child->binding_handle != NULL);
834 struct winbind_child_died_state {
836 struct winbindd_child *child;
839 static bool winbind_child_died_fn(struct winbindd_child *child,
842 struct winbind_child_died_state *state = private_data;
844 if (child->pid == state->pid) {
845 state->child = child;
851 void winbind_child_died(pid_t pid)
853 struct winbind_child_died_state state = { .pid = pid };
855 forall_children(winbind_child_died_fn, &state);
857 if (state.child == NULL) {
858 DEBUG(5, ("Already reaped child %u died\n", (unsigned int)pid));
862 state.child->pid = 0;
865 /* Ensure any negative cache entries with the netbios or realm names are removed. */
867 void winbindd_flush_negative_conn_cache(struct winbindd_domain *domain)
869 flush_negative_conn_cache_for_domain(domain->name);
870 if (domain->alt_name != NULL) {
871 flush_negative_conn_cache_for_domain(domain->alt_name);
876 * Parent winbindd process sets its own debug level first and then
877 * sends a message to all the winbindd children to adjust their debug
878 * level to that of parents.
881 struct winbind_msg_relay_state {
882 struct messaging_context *msg_ctx;
887 static bool winbind_msg_relay_fn(struct winbindd_child *child,
890 struct winbind_msg_relay_state *state = private_data;
892 DBG_DEBUG("sending message to pid %u.\n",
893 (unsigned int)child->pid);
895 messaging_send(state->msg_ctx, pid_to_procid(child->pid),
896 state->msg_type, state->data);
900 void winbind_msg_debug(struct messaging_context *msg_ctx,
903 struct server_id server_id,
906 struct winbind_msg_relay_state state = {
907 .msg_ctx = msg_ctx, .msg_type = msg_type, .data = data
910 DEBUG(10,("winbind_msg_debug: got debug message.\n"));
912 debug_message(msg_ctx, private_data, MSG_DEBUG, server_id, data);
914 forall_children(winbind_msg_relay_fn, &state);
917 void winbind_disconnect_dc_parent(struct messaging_context *msg_ctx,
920 struct server_id server_id,
923 struct winbind_msg_relay_state state = {
924 .msg_ctx = msg_ctx, .msg_type = msg_type, .data = data
927 DBG_DEBUG("Got disconnect_dc message\n");
929 forall_children(winbind_msg_relay_fn, &state);
932 /* Set our domains as offline and forward the offline message to our children. */
934 struct winbind_msg_on_offline_state {
935 struct messaging_context *msg_ctx;
939 static bool winbind_msg_on_offline_fn(struct winbindd_child *child,
942 struct winbind_msg_on_offline_state *state = private_data;
944 if (child->domain->internal) {
949 * Each winbindd child should only process requests for one
950 * domain - make sure we only set it online / offline for that
953 DBG_DEBUG("sending message to pid %u for domain %s.\n",
954 (unsigned int)child->pid, child->domain->name);
956 messaging_send_buf(state->msg_ctx,
957 pid_to_procid(child->pid),
959 (const uint8_t *)child->domain->name,
960 strlen(child->domain->name)+1);
965 void winbind_msg_offline(struct messaging_context *msg_ctx,
968 struct server_id server_id,
971 struct winbind_msg_on_offline_state state = {
973 .msg_type = MSG_WINBIND_OFFLINE,
975 struct winbindd_domain *domain;
977 DEBUG(10,("winbind_msg_offline: got offline message.\n"));
979 if (!lp_winbind_offline_logon()) {
980 DEBUG(10,("winbind_msg_offline: rejecting offline message.\n"));
984 /* Set our global state as offline. */
985 if (!set_global_winbindd_state_offline()) {
986 DEBUG(10,("winbind_msg_offline: offline request failed.\n"));
990 /* Set all our domains as offline. */
991 for (domain = domain_list(); domain; domain = domain->next) {
992 if (domain->internal) {
995 DEBUG(5,("winbind_msg_offline: marking %s offline.\n", domain->name));
996 set_domain_offline(domain);
999 forall_domain_children(winbind_msg_on_offline_fn, &state);
1002 /* Set our domains as online and forward the online message to our children. */
1004 void winbind_msg_online(struct messaging_context *msg_ctx,
1007 struct server_id server_id,
1010 struct winbind_msg_on_offline_state state = {
1012 .msg_type = MSG_WINBIND_ONLINE,
1014 struct winbindd_domain *domain;
1016 DEBUG(10,("winbind_msg_online: got online message.\n"));
1018 if (!lp_winbind_offline_logon()) {
1019 DEBUG(10,("winbind_msg_online: rejecting online message.\n"));
1023 /* Set our global state as online. */
1024 set_global_winbindd_state_online();
1026 smb_nscd_flush_user_cache();
1027 smb_nscd_flush_group_cache();
1029 /* Set all our domains as online. */
1030 for (domain = domain_list(); domain; domain = domain->next) {
1031 if (domain->internal) {
1034 DEBUG(5,("winbind_msg_online: requesting %s to go online.\n", domain->name));
1036 winbindd_flush_negative_conn_cache(domain);
1037 set_domain_online_request(domain);
1039 /* Send an online message to the idmap child when our
1040 primary domain comes back online */
1042 if ( domain->primary ) {
1043 pid_t idmap_pid = idmap_child_pid();
1045 if (idmap_pid != 0) {
1046 messaging_send_buf(msg_ctx,
1047 pid_to_procid(idmap_pid),
1049 (const uint8_t *)domain->name,
1050 strlen(domain->name)+1);
1055 forall_domain_children(winbind_msg_on_offline_fn, &state);
1058 static const char *collect_onlinestatus(TALLOC_CTX *mem_ctx)
1060 struct winbindd_domain *domain;
1063 if ((buf = talloc_asprintf(mem_ctx, "global:%s ",
1064 get_global_winbindd_state_offline() ?
1065 "Offline":"Online")) == NULL) {
1069 for (domain = domain_list(); domain; domain = domain->next) {
1070 if ((buf = talloc_asprintf_append_buffer(buf, "%s:%s ",
1073 "Online":"Offline")) == NULL) {
1078 buf = talloc_asprintf_append_buffer(buf, "\n");
1080 DEBUG(5,("collect_onlinestatus: %s", buf));
1085 void winbind_msg_onlinestatus(struct messaging_context *msg_ctx,
1088 struct server_id server_id,
1091 TALLOC_CTX *mem_ctx;
1092 const char *message;
1094 DEBUG(5,("winbind_msg_onlinestatus received.\n"));
1096 mem_ctx = talloc_init("winbind_msg_onlinestatus");
1097 if (mem_ctx == NULL) {
1101 message = collect_onlinestatus(mem_ctx);
1102 if (message == NULL) {
1103 talloc_destroy(mem_ctx);
1107 messaging_send_buf(msg_ctx, server_id, MSG_WINBIND_ONLINESTATUS,
1108 (const uint8_t *)message, strlen(message) + 1);
1110 talloc_destroy(mem_ctx);
1113 void winbind_msg_dump_domain_list(struct messaging_context *msg_ctx,
1116 struct server_id server_id,
1119 TALLOC_CTX *mem_ctx;
1120 const char *message = NULL;
1121 const char *domain = NULL;
1124 struct winbindd_domain *dom = NULL;
1126 DEBUG(5,("winbind_msg_dump_domain_list received.\n"));
1128 mem_ctx = talloc_init("winbind_msg_dump_domain_list");
1133 if (data->length > 0) {
1134 domain = (const char *)data->data;
1139 DEBUG(5,("winbind_msg_dump_domain_list for domain: %s\n",
1142 message = NDR_PRINT_STRUCT_STRING(mem_ctx, winbindd_domain,
1143 find_domain_from_name_noinit(domain));
1145 talloc_destroy(mem_ctx);
1149 messaging_send_buf(msg_ctx, server_id,
1150 MSG_WINBIND_DUMP_DOMAIN_LIST,
1151 (const uint8_t *)message, strlen(message) + 1);
1153 talloc_destroy(mem_ctx);
1158 DEBUG(5,("winbind_msg_dump_domain_list all domains\n"));
1160 for (dom = domain_list(); dom; dom=dom->next) {
1161 message = NDR_PRINT_STRUCT_STRING(mem_ctx, winbindd_domain, dom);
1163 talloc_destroy(mem_ctx);
1167 s = talloc_asprintf_append(s, "%s\n", message);
1169 talloc_destroy(mem_ctx);
1174 status = messaging_send_buf(msg_ctx, server_id,
1175 MSG_WINBIND_DUMP_DOMAIN_LIST,
1176 (uint8_t *)s, strlen(s) + 1);
1177 if (!NT_STATUS_IS_OK(status)) {
1178 DEBUG(0,("failed to send message: %s\n",
1179 nt_errstr(status)));
1182 talloc_destroy(mem_ctx);
1185 static void account_lockout_policy_handler(struct tevent_context *ctx,
1186 struct tevent_timer *te,
1190 struct winbindd_child *child =
1191 (struct winbindd_child *)private_data;
1192 TALLOC_CTX *mem_ctx = NULL;
1193 struct samr_DomInfo12 lockout_policy;
1196 DEBUG(10,("account_lockout_policy_handler called\n"));
1198 TALLOC_FREE(child->lockout_policy_event);
1200 if ( !winbindd_can_contact_domain( child->domain ) ) {
1201 DEBUG(10,("account_lockout_policy_handler: Removing myself since I "
1202 "do not have an incoming trust to domain %s\n",
1203 child->domain->name));
1208 mem_ctx = talloc_init("account_lockout_policy_handler ctx");
1210 result = NT_STATUS_NO_MEMORY;
1212 result = wb_cache_lockout_policy(child->domain, mem_ctx,
1215 TALLOC_FREE(mem_ctx);
1217 if (!NT_STATUS_IS_OK(result)) {
1218 DEBUG(10,("account_lockout_policy_handler: lockout_policy failed error %s\n",
1219 nt_errstr(result)));
1222 child->lockout_policy_event = tevent_add_timer(global_event_context(), NULL,
1223 timeval_current_ofs(3600, 0),
1224 account_lockout_policy_handler,
1228 static time_t get_machine_password_timeout(void)
1230 /* until we have gpo support use lp setting */
1231 return lp_machine_password_timeout();
1234 static bool calculate_next_machine_pwd_change(const char *domain,
1237 time_t pass_last_set_time;
1243 pw = secrets_fetch_machine_password(domain,
1244 &pass_last_set_time,
1248 DEBUG(0,("cannot fetch own machine password ????"));
1254 timeout = get_machine_password_timeout();
1256 DEBUG(10,("machine password never expires\n"));
1260 tv.tv_sec = pass_last_set_time;
1261 DEBUG(10, ("password last changed %s\n",
1262 timeval_string(talloc_tos(), &tv, false)));
1263 tv.tv_sec += timeout;
1264 DEBUGADD(10, ("password valid until %s\n",
1265 timeval_string(talloc_tos(), &tv, false)));
1267 if (time(NULL) < (pass_last_set_time + timeout)) {
1268 next_change = pass_last_set_time + timeout;
1269 DEBUG(10,("machine password still valid until: %s\n",
1270 http_timestring(talloc_tos(), next_change)));
1271 *t = timeval_set(next_change, 0);
1273 if (lp_clustering()) {
1276 * When having a cluster, we have several
1277 * winbinds racing for the password change. In
1278 * the machine_password_change_handler()
1279 * function we check if someone else was
1280 * faster when the event triggers. We add a
1281 * 255-second random delay here, so that we
1282 * don't run to change the password at the
1283 * exact same moment.
1285 generate_random_buffer(&randbuf, sizeof(randbuf));
1286 DEBUG(10, ("adding %d seconds randomness\n",
1288 t->tv_sec += randbuf;
1293 DEBUG(10,("machine password expired, needs immediate change\n"));
1295 *t = timeval_zero();
1300 static void machine_password_change_handler(struct tevent_context *ctx,
1301 struct tevent_timer *te,
1305 struct messaging_context *msg_ctx = global_messaging_context();
1306 struct winbindd_child *child =
1307 (struct winbindd_child *)private_data;
1308 struct rpc_pipe_client *netlogon_pipe = NULL;
1309 struct netlogon_creds_cli_context *netlogon_creds_ctx = NULL;
1311 struct timeval next_change;
1313 DEBUG(10,("machine_password_change_handler called\n"));
1315 TALLOC_FREE(child->machine_password_change_event);
1317 if (!calculate_next_machine_pwd_change(child->domain->name,
1319 DEBUG(10, ("calculate_next_machine_pwd_change failed\n"));
1323 DEBUG(10, ("calculate_next_machine_pwd_change returned %s\n",
1324 timeval_string(talloc_tos(), &next_change, false)));
1326 if (!timeval_expired(&next_change)) {
1327 DEBUG(10, ("Someone else has already changed the pw\n"));
1331 if (!winbindd_can_contact_domain(child->domain)) {
1332 DEBUG(10,("machine_password_change_handler: Removing myself since I "
1333 "do not have an incoming trust to domain %s\n",
1334 child->domain->name));
1338 result = cm_connect_netlogon_secure(child->domain,
1340 &netlogon_creds_ctx);
1341 if (!NT_STATUS_IS_OK(result)) {
1342 DEBUG(10,("machine_password_change_handler: "
1343 "failed to connect netlogon pipe: %s\n",
1344 nt_errstr(result)));
1348 result = trust_pw_change(netlogon_creds_ctx,
1350 netlogon_pipe->binding_handle,
1351 child->domain->name,
1352 child->domain->dcname,
1355 DEBUG(10, ("machine_password_change_handler: "
1356 "trust_pw_change returned %s\n",
1357 nt_errstr(result)));
1359 if (NT_STATUS_EQUAL(result, NT_STATUS_ACCESS_DENIED) ) {
1360 DEBUG(3,("machine_password_change_handler: password set returned "
1361 "ACCESS_DENIED. Maybe the trust account "
1362 "password was changed and we didn't know it. "
1363 "Killing connections to domain %s\n",
1364 child->domain->name));
1365 invalidate_cm_connection(child->domain);
1368 if (!calculate_next_machine_pwd_change(child->domain->name,
1370 DEBUG(10, ("calculate_next_machine_pwd_change failed\n"));
1374 DEBUG(10, ("calculate_next_machine_pwd_change returned %s\n",
1375 timeval_string(talloc_tos(), &next_change, false)));
1377 if (!NT_STATUS_IS_OK(result)) {
1380 * In case of failure, give the DC a minute to recover
1382 tmp = timeval_current_ofs(60, 0);
1383 next_change = timeval_max(&next_change, &tmp);
1387 child->machine_password_change_event = tevent_add_timer(global_event_context(), NULL,
1389 machine_password_change_handler,
1393 /* Deal with a request to go offline. */
1395 static void child_msg_offline(struct messaging_context *msg,
1398 struct server_id server_id,
1401 struct winbindd_domain *domain;
1402 struct winbindd_domain *primary_domain = NULL;
1403 const char *domainname = (const char *)data->data;
1405 if (data->data == NULL || data->length == 0) {
1409 DEBUG(5,("child_msg_offline received for domain %s.\n", domainname));
1411 if (!lp_winbind_offline_logon()) {
1412 DEBUG(10,("child_msg_offline: rejecting offline message.\n"));
1416 primary_domain = find_our_domain();
1418 /* Mark the requested domain offline. */
1420 for (domain = domain_list(); domain; domain = domain->next) {
1421 if (domain->internal) {
1424 if (strequal(domain->name, domainname)) {
1425 DEBUG(5,("child_msg_offline: marking %s offline.\n", domain->name));
1426 set_domain_offline(domain);
1427 /* we are in the trusted domain, set the primary domain
1429 if (domain != primary_domain) {
1430 set_domain_offline(primary_domain);
1436 /* Deal with a request to go online. */
1438 static void child_msg_online(struct messaging_context *msg,
1441 struct server_id server_id,
1444 struct winbindd_domain *domain;
1445 struct winbindd_domain *primary_domain = NULL;
1446 const char *domainname = (const char *)data->data;
1448 if (data->data == NULL || data->length == 0) {
1452 DEBUG(5,("child_msg_online received for domain %s.\n", domainname));
1454 if (!lp_winbind_offline_logon()) {
1455 DEBUG(10,("child_msg_online: rejecting online message.\n"));
1459 primary_domain = find_our_domain();
1461 /* Set our global state as online. */
1462 set_global_winbindd_state_online();
1464 /* Try and mark everything online - delete any negative cache entries
1465 to force a reconnect now. */
1467 for (domain = domain_list(); domain; domain = domain->next) {
1468 if (domain->internal) {
1471 if (strequal(domain->name, domainname)) {
1472 DEBUG(5,("child_msg_online: requesting %s to go online.\n", domain->name));
1473 winbindd_flush_negative_conn_cache(domain);
1474 set_domain_online_request(domain);
1476 /* we can be in trusted domain, which will contact primary domain
1477 * we have to bring primary domain online in trusted domain process
1478 * see, winbindd_dual_pam_auth() --> winbindd_dual_pam_auth_samlogon()
1479 * --> contact_domain = find_our_domain()
1481 if (domain != primary_domain) {
1482 winbindd_flush_negative_conn_cache(primary_domain);
1483 set_domain_online_request(primary_domain);
1489 struct winbindd_reinit_after_fork_state {
1490 const struct winbindd_child *myself;
1493 static bool winbindd_reinit_after_fork_fn(struct winbindd_child *child,
1496 struct winbindd_reinit_after_fork_state *state = private_data;
1498 if (child == state->myself) {
1502 /* Destroy all possible events in child list. */
1503 TALLOC_FREE(child->lockout_policy_event);
1504 TALLOC_FREE(child->machine_password_change_event);
1507 * Children should never be able to send each other messages,
1508 * all messages must go through the parent.
1510 child->pid = (pid_t)0;
1513 * Close service sockets to all other children
1515 if (child->sock != -1) {
1523 NTSTATUS winbindd_reinit_after_fork(const struct winbindd_child *myself,
1524 const char *logfilename)
1526 struct winbindd_reinit_after_fork_state state = { .myself = myself };
1527 struct winbindd_domain *domain;
1530 status = reinit_after_fork(
1531 global_messaging_context(),
1532 global_event_context(),
1534 if (!NT_STATUS_IS_OK(status)) {
1535 DEBUG(0,("reinit_after_fork() failed\n"));
1538 initialize_password_db(true, global_event_context());
1540 close_conns_after_fork();
1542 if (!override_logfile && logfilename) {
1543 lp_set_logfile(logfilename);
1547 if (!winbindd_setup_sig_term_handler(false))
1548 return NT_STATUS_NO_MEMORY;
1549 if (!winbindd_setup_sig_hup_handler(override_logfile ? NULL :
1551 return NT_STATUS_NO_MEMORY;
1553 /* Stop zombies in children */
1556 /* Don't handle the same messages as our parent. */
1557 messaging_deregister(global_messaging_context(),
1558 MSG_SMB_CONF_UPDATED, NULL);
1559 messaging_deregister(global_messaging_context(),
1560 MSG_SHUTDOWN, NULL);
1561 messaging_deregister(global_messaging_context(),
1562 MSG_WINBIND_OFFLINE, NULL);
1563 messaging_deregister(global_messaging_context(),
1564 MSG_WINBIND_ONLINE, NULL);
1565 messaging_deregister(global_messaging_context(),
1566 MSG_WINBIND_ONLINESTATUS, NULL);
1567 messaging_deregister(global_messaging_context(),
1568 MSG_WINBIND_DUMP_DOMAIN_LIST, NULL);
1569 messaging_deregister(global_messaging_context(),
1572 messaging_deregister(global_messaging_context(),
1573 MSG_WINBIND_DOMAIN_OFFLINE, NULL);
1574 messaging_deregister(global_messaging_context(),
1575 MSG_WINBIND_DOMAIN_ONLINE, NULL);
1577 /* We have destroyed all events in the winbindd_event_context
1578 * in reinit_after_fork(), so clean out all possible pending
1579 * event pointers. */
1581 /* Deal with check_online_events. */
1583 for (domain = domain_list(); domain; domain = domain->next) {
1584 TALLOC_FREE(domain->check_online_event);
1587 /* Ensure we're not handling a credential cache event inherited
1588 * from our parent. */
1590 ccache_remove_all_after_fork();
1592 forall_children(winbindd_reinit_after_fork_fn, &state);
1594 return NT_STATUS_OK;
1598 * In a child there will be only one domain, reference that here.
1600 static struct winbindd_domain *child_domain;
1602 struct winbindd_domain *wb_child_domain(void)
1604 return child_domain;
1607 struct child_handler_state {
1608 struct winbindd_child *child;
1609 struct winbindd_cli_state cli;
1612 static void child_handler(struct tevent_context *ev, struct tevent_fd *fde,
1613 uint16_t flags, void *private_data)
1615 struct child_handler_state *state =
1616 (struct child_handler_state *)private_data;
1619 /* fetch a request from the main daemon */
1620 status = child_read_request(state->cli.sock, state->cli.request);
1622 if (!NT_STATUS_IS_OK(status)) {
1623 /* we lost contact with our parent */
1627 DEBUG(4,("child daemon request %d\n",
1628 (int)state->cli.request->cmd));
1630 ZERO_STRUCTP(state->cli.response);
1631 state->cli.request->null_term = '\0';
1632 state->cli.mem_ctx = talloc_tos();
1633 child_process_request(state->child, &state->cli);
1635 DEBUG(4, ("Finished processing child request %d\n",
1636 (int)state->cli.request->cmd));
1638 SAFE_FREE(state->cli.request->extra_data.data);
1640 status = child_write_response(state->cli.sock, state->cli.response);
1641 if (!NT_STATUS_IS_OK(status)) {
1646 static bool fork_domain_child(struct winbindd_child *child)
1649 struct child_handler_state state;
1650 struct winbindd_request request;
1651 struct winbindd_response response;
1652 struct winbindd_domain *primary_domain = NULL;
1655 struct tevent_fd *fde;
1657 if (child->domain) {
1658 DEBUG(10, ("fork_domain_child called for domain '%s'\n",
1659 child->domain->name));
1661 DEBUG(10, ("fork_domain_child called without domain.\n"));
1664 if (socketpair(AF_UNIX, SOCK_STREAM, 0, fdpair) != 0) {
1665 DEBUG(0, ("Could not open child pipe: %s\n",
1671 state.child = child;
1672 state.cli.pid = getpid();
1673 state.cli.request = &request;
1674 state.cli.response = &response;
1676 child->pid = fork();
1678 if (child->pid == -1) {
1679 DEBUG(0, ("Could not fork: %s\n", strerror(errno)));
1685 if (child->pid != 0) {
1692 nread = sys_read(fdpair[1], &status, sizeof(status));
1693 if (nread != sizeof(status)) {
1694 DEBUG(1, ("fork_domain_child: Could not read child status: "
1695 "nread=%d, error=%s\n", (int)nread,
1700 if (!NT_STATUS_IS_OK(status)) {
1701 DEBUG(1, ("fork_domain_child: Child status is %s\n",
1702 nt_errstr(status)));
1707 child->monitor_fde = tevent_add_fd(global_event_context(),
1708 global_event_context(),
1711 child_socket_readable,
1713 if (child->monitor_fde == NULL) {
1714 DBG_WARNING("tevent_add_fd failed\n");
1719 rc = set_blocking(fdpair[1], false);
1725 child->sock = fdpair[1];
1731 child_domain = child->domain;
1733 DEBUG(10, ("Child process %d\n", (int)getpid()));
1735 state.cli.sock = fdpair[0];
1738 status = winbindd_reinit_after_fork(child, child->logfilename);
1740 nwritten = sys_write(state.cli.sock, &status, sizeof(status));
1741 if (nwritten != sizeof(status)) {
1742 DEBUG(1, ("fork_domain_child: Could not write status: "
1743 "nwritten=%d, error=%s\n", (int)nwritten,
1747 if (!NT_STATUS_IS_OK(status)) {
1748 DEBUG(1, ("winbindd_reinit_after_fork failed: %s\n",
1749 nt_errstr(status)));
1753 if (child_domain != NULL) {
1754 setproctitle("domain child [%s]", child_domain->name);
1755 } else if (is_idmap_child(child)) {
1756 setproctitle("idmap child");
1759 /* Handle online/offline messages. */
1760 messaging_register(global_messaging_context(), NULL,
1761 MSG_WINBIND_OFFLINE, child_msg_offline);
1762 messaging_register(global_messaging_context(), NULL,
1763 MSG_WINBIND_ONLINE, child_msg_online);
1764 messaging_register(global_messaging_context(), NULL,
1765 MSG_DEBUG, debug_message);
1766 messaging_register(global_messaging_context(), NULL,
1767 MSG_WINBIND_IP_DROPPED,
1768 winbind_msg_ip_dropped);
1769 messaging_register(global_messaging_context(), NULL,
1770 MSG_WINBIND_DISCONNECT_DC,
1771 winbind_msg_disconnect_dc);
1773 primary_domain = find_our_domain();
1775 if (primary_domain == NULL) {
1776 smb_panic("no primary domain found");
1779 /* It doesn't matter if we allow cache login,
1780 * try to bring domain online after fork. */
1781 if ( child->domain ) {
1782 child->domain->startup = True;
1783 child->domain->startup_time = time_mono(NULL);
1784 /* we can be in primary domain or in trusted domain
1785 * If we are in trusted domain, set the primary domain
1786 * in start-up mode */
1787 if (!(child->domain->internal)) {
1788 set_domain_online_request(child->domain);
1789 if (!(child->domain->primary)) {
1790 primary_domain->startup = True;
1791 primary_domain->startup_time = time_mono(NULL);
1792 set_domain_online_request(primary_domain);
1798 * We are in idmap child, make sure that we set the
1799 * check_online_event to bring primary domain online.
1801 if (is_idmap_child(child)) {
1802 set_domain_online_request(primary_domain);
1805 /* We might be in the idmap child...*/
1806 if (child->domain && !(child->domain->internal) &&
1807 lp_winbind_offline_logon()) {
1809 set_domain_online_request(child->domain);
1811 if (primary_domain && (primary_domain != child->domain)) {
1812 /* We need to talk to the primary
1813 * domain as well as the trusted
1814 * domain inside a trusted domain
1817 * set_dc_type_and_flags_trustinfo()
1820 set_domain_online_request(primary_domain);
1823 child->lockout_policy_event = tevent_add_timer(
1824 global_event_context(), NULL, timeval_zero(),
1825 account_lockout_policy_handler,
1829 if (child->domain && child->domain->primary &&
1830 !USE_KERBEROS_KEYTAB &&
1831 lp_server_role() == ROLE_DOMAIN_MEMBER) {
1833 struct timeval next_change;
1835 if (calculate_next_machine_pwd_change(child->domain->name,
1837 child->machine_password_change_event = tevent_add_timer(
1838 global_event_context(), NULL, next_change,
1839 machine_password_change_handler,
1844 fde = tevent_add_fd(global_event_context(), NULL, state.cli.sock,
1845 TEVENT_FD_READ, child_handler, &state);
1847 DEBUG(1, ("tevent_add_fd failed\n"));
1854 TALLOC_CTX *frame = talloc_stackframe();
1856 ret = tevent_loop_once(global_event_context());
1858 DEBUG(1, ("tevent_loop_once failed: %s\n",
1863 if (child->domain && child->domain->startup &&
1864 (time_mono(NULL) > child->domain->startup_time + 30)) {
1865 /* No longer in "startup" mode. */
1866 DEBUG(10,("fork_domain_child: domain %s no longer in 'startup' mode.\n",
1867 child->domain->name ));
1868 child->domain->startup = False;
1875 void winbind_msg_ip_dropped_parent(struct messaging_context *msg_ctx,
1878 struct server_id server_id,
1881 struct winbind_msg_relay_state state = {
1883 .msg_type = msg_type,
1887 winbind_msg_ip_dropped(msg_ctx, private_data, msg_type,
1890 forall_children(winbind_msg_relay_fn, &state);