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 2 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, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 * We fork a child per domain to be able to act non-blocking in the main
26 * winbind daemon. A domain controller thousands of miles away being being
27 * slow replying with a 10.000 user list should not hold up netlogon calls
28 * that can be handled locally.
35 #define DBGC_CLASS DBGC_WINBIND
37 /* Read some data from a client connection */
39 static void child_read_request(struct winbindd_cli_state *state)
45 len = read_data(state->sock, (char *)&state->request,
46 sizeof(state->request));
48 if (len != sizeof(state->request)) {
49 DEBUG(0, ("Got invalid request length: %d\n", (int)len));
50 state->finished = True;
54 if (state->request.extra_len == 0) {
55 state->request.extra_data.data = NULL;
59 DEBUG(10, ("Need to read %d extra bytes\n", (int)state->request.extra_len));
61 state->request.extra_data.data =
62 SMB_MALLOC_ARRAY(char, state->request.extra_len + 1);
64 if (state->request.extra_data.data == NULL) {
65 DEBUG(0, ("malloc failed\n"));
66 state->finished = True;
70 /* Ensure null termination */
71 state->request.extra_data.data[state->request.extra_len] = '\0';
73 len = read_data(state->sock, state->request.extra_data.data,
74 state->request.extra_len);
76 if (len != state->request.extra_len) {
77 DEBUG(0, ("Could not read extra data\n"));
78 state->finished = True;
84 * Machinery for async requests sent to children. You set up a
85 * winbindd_request, select a child to query, and issue a async_request
86 * call. When the request is completed, the callback function you specified is
87 * called back with the private pointer you gave to async_request.
90 struct winbindd_async_request {
91 struct winbindd_async_request *next, *prev;
93 struct winbindd_child *child;
94 struct winbindd_request *request;
95 struct winbindd_response *response;
96 void (*continuation)(void *private_data, BOOL success);
100 static void async_main_request_sent(void *private_data, BOOL success);
101 static void async_request_sent(void *private_data, BOOL success);
102 static void async_reply_recv(void *private_data, BOOL success);
103 static void schedule_async_request(struct winbindd_child *child);
105 void async_request(TALLOC_CTX *mem_ctx, struct winbindd_child *child,
106 struct winbindd_request *request,
107 struct winbindd_response *response,
108 void (*continuation)(void *private_data, BOOL success),
111 struct winbindd_async_request *state, *tmp;
113 SMB_ASSERT(continuation != NULL);
115 state = TALLOC_P(mem_ctx, struct winbindd_async_request);
118 DEBUG(0, ("talloc failed\n"));
119 continuation(private_data, False);
123 state->mem_ctx = mem_ctx;
124 state->child = child;
125 state->request = request;
126 state->response = response;
127 state->continuation = continuation;
128 state->private_data = private_data;
130 DLIST_ADD_END(child->requests, state, tmp);
132 schedule_async_request(child);
137 static void async_main_request_sent(void *private_data, BOOL success)
139 struct winbindd_async_request *state =
140 talloc_get_type_abort(private_data, struct winbindd_async_request);
143 DEBUG(5, ("Could not send async request\n"));
145 state->response->length = sizeof(struct winbindd_response);
146 state->response->result = WINBINDD_ERROR;
147 state->continuation(state->private_data, False);
151 if (state->request->extra_len == 0) {
152 async_request_sent(private_data, True);
156 setup_async_write(&state->child->event, state->request->extra_data.data,
157 state->request->extra_len,
158 async_request_sent, state);
161 static void async_request_sent(void *private_data_data, BOOL success)
163 struct winbindd_async_request *state =
164 talloc_get_type_abort(private_data_data, struct winbindd_async_request);
167 DEBUG(5, ("Could not send async request\n"));
169 state->response->length = sizeof(struct winbindd_response);
170 state->response->result = WINBINDD_ERROR;
171 state->continuation(state->private_data, False);
175 /* Request successfully sent to the child, setup the wait for reply */
177 setup_async_read(&state->child->event,
178 &state->response->result,
179 sizeof(state->response->result),
180 async_reply_recv, state);
183 static void async_reply_recv(void *private_data, BOOL success)
185 struct winbindd_async_request *state =
186 talloc_get_type_abort(private_data, struct winbindd_async_request);
187 struct winbindd_child *child = state->child;
189 state->response->length = sizeof(struct winbindd_response);
192 DEBUG(5, ("Could not receive async reply\n"));
193 state->response->result = WINBINDD_ERROR;
197 SMB_ASSERT(cache_retrieve_response(child->pid,
200 cache_cleanup_response(child->pid);
202 DLIST_REMOVE(child->requests, state);
204 schedule_async_request(child);
206 state->continuation(state->private_data, True);
209 static BOOL fork_domain_child(struct winbindd_child *child);
211 static void schedule_async_request(struct winbindd_child *child)
213 struct winbindd_async_request *request = child->requests;
215 if (request == NULL) {
219 if (child->event.flags != 0) {
223 if ((child->pid == 0) && (!fork_domain_child(child))) {
224 /* Cancel all outstanding requests */
226 while (request != NULL) {
227 /* request might be free'd in the continuation */
228 struct winbindd_async_request *next = request->next;
229 request->continuation(request->private_data, False);
235 setup_async_write(&child->event, request->request,
236 sizeof(*request->request),
237 async_main_request_sent, request);
239 talloc_destroy(child->mem_ctx);
243 struct domain_request_state {
245 struct winbindd_domain *domain;
246 struct winbindd_request *request;
247 struct winbindd_response *response;
248 void (*continuation)(void *private_data_data, BOOL success);
249 void *private_data_data;
252 static void domain_init_recv(void *private_data_data, BOOL success);
254 void async_domain_request(TALLOC_CTX *mem_ctx,
255 struct winbindd_domain *domain,
256 struct winbindd_request *request,
257 struct winbindd_response *response,
258 void (*continuation)(void *private_data_data, BOOL success),
259 void *private_data_data)
261 struct domain_request_state *state;
263 if (domain->initialized) {
264 async_request(mem_ctx, &domain->child, request, response,
265 continuation, private_data_data);
269 state = TALLOC_P(mem_ctx, struct domain_request_state);
271 DEBUG(0, ("talloc failed\n"));
272 continuation(private_data_data, False);
276 state->mem_ctx = mem_ctx;
277 state->domain = domain;
278 state->request = request;
279 state->response = response;
280 state->continuation = continuation;
281 state->private_data_data = private_data_data;
283 init_child_connection(domain, domain_init_recv, state);
286 static void recvfrom_child(void *private_data_data, BOOL success)
288 struct winbindd_cli_state *state =
289 talloc_get_type_abort(private_data_data, struct winbindd_cli_state);
290 enum winbindd_result result = state->response.result;
292 /* This is an optimization: The child has written directly to the
293 * response buffer. The request itself is still in pending state,
294 * state that in the result code. */
296 state->response.result = WINBINDD_PENDING;
298 if ((!success) || (result != WINBINDD_OK)) {
299 request_error(state);
306 void sendto_child(struct winbindd_cli_state *state,
307 struct winbindd_child *child)
309 async_request(state->mem_ctx, child, &state->request,
310 &state->response, recvfrom_child, state);
313 void sendto_domain(struct winbindd_cli_state *state,
314 struct winbindd_domain *domain)
316 async_domain_request(state->mem_ctx, domain,
317 &state->request, &state->response,
318 recvfrom_child, state);
321 static void domain_init_recv(void *private_data_data, BOOL success)
323 struct domain_request_state *state =
324 talloc_get_type_abort(private_data_data, struct domain_request_state);
327 DEBUG(5, ("Domain init returned an error\n"));
328 state->continuation(state->private_data_data, False);
332 async_request(state->mem_ctx, &state->domain->child,
333 state->request, state->response,
334 state->continuation, state->private_data_data);
337 struct winbindd_child_dispatch_table {
338 enum winbindd_cmd cmd;
339 enum winbindd_result (*fn)(struct winbindd_domain *domain,
340 struct winbindd_cli_state *state);
341 const char *winbindd_cmd_name;
344 static struct winbindd_child_dispatch_table child_dispatch_table[] = {
346 { WINBINDD_LOOKUPSID, winbindd_dual_lookupsid, "LOOKUPSID" },
347 { WINBINDD_LOOKUPNAME, winbindd_dual_lookupname, "LOOKUPNAME" },
348 { WINBINDD_LOOKUPRIDS, winbindd_dual_lookuprids, "LOOKUPRIDS" },
349 { WINBINDD_LIST_TRUSTDOM, winbindd_dual_list_trusted_domains, "LIST_TRUSTDOM" },
350 { WINBINDD_INIT_CONNECTION, winbindd_dual_init_connection, "INIT_CONNECTION" },
351 { WINBINDD_GETDCNAME, winbindd_dual_getdcname, "GETDCNAME" },
352 { WINBINDD_SHOW_SEQUENCE, winbindd_dual_show_sequence, "SHOW_SEQUENCE" },
353 { WINBINDD_PAM_AUTH, winbindd_dual_pam_auth, "PAM_AUTH" },
354 { WINBINDD_PAM_AUTH_CRAP, winbindd_dual_pam_auth_crap, "AUTH_CRAP" },
355 { WINBINDD_PAM_LOGOFF, winbindd_dual_pam_logoff, "PAM_LOGOFF" },
356 { WINBINDD_PAM_CHNG_PSWD_AUTH_CRAP,winbindd_dual_pam_chng_pswd_auth_crap,"CHNG_PSWD_AUTH_CRAP" },
357 { WINBINDD_CHECK_MACHACC, winbindd_dual_check_machine_acct, "CHECK_MACHACC" },
358 { WINBINDD_DUAL_SID2UID, winbindd_dual_sid2uid, "DUAL_SID2UID" },
359 { WINBINDD_DUAL_SID2GID, winbindd_dual_sid2gid, "DUAL_SID2GID" },
360 { WINBINDD_DUAL_UID2SID, winbindd_dual_uid2sid, "DUAL_UID2SID" },
361 { WINBINDD_DUAL_GID2SID, winbindd_dual_gid2sid, "DUAL_GID2SID" },
362 { WINBINDD_DUAL_UID2NAME, winbindd_dual_uid2name, "DUAL_UID2NAME" },
363 { WINBINDD_DUAL_NAME2UID, winbindd_dual_name2uid, "DUAL_NAME2UID" },
364 { WINBINDD_DUAL_GID2NAME, winbindd_dual_gid2name, "DUAL_GID2NAME" },
365 { WINBINDD_DUAL_NAME2GID, winbindd_dual_name2gid, "DUAL_NAME2GID" },
366 { WINBINDD_DUAL_IDMAPSET, winbindd_dual_idmapset, "DUAL_IDMAPSET" },
367 { WINBINDD_DUAL_USERINFO, winbindd_dual_userinfo, "DUAL_USERINFO" },
368 { WINBINDD_ALLOCATE_UID, winbindd_dual_allocate_uid, "ALLOCATE_UID" },
369 { WINBINDD_ALLOCATE_GID, winbindd_dual_allocate_gid, "ALLOCATE_GID" },
370 { WINBINDD_GETUSERDOMGROUPS, winbindd_dual_getuserdomgroups, "GETUSERDOMGROUPS" },
371 { WINBINDD_DUAL_GETSIDALIASES, winbindd_dual_getsidaliases, "GETSIDALIASES" },
374 { WINBINDD_NUM_CMDS, NULL, "NONE" }
377 static void child_process_request(struct winbindd_domain *domain,
378 struct winbindd_cli_state *state)
380 struct winbindd_child_dispatch_table *table;
382 /* Free response data - we may be interrupted and receive another
383 command before being able to send this data off. */
385 state->response.result = WINBINDD_ERROR;
386 state->response.length = sizeof(struct winbindd_response);
388 state->mem_ctx = talloc_init("winbind request");
389 if (state->mem_ctx == NULL)
392 /* Process command */
394 for (table = child_dispatch_table; table->fn; table++) {
395 if (state->request.cmd == table->cmd) {
396 DEBUG(10,("process_request: request fn %s\n",
397 table->winbindd_cmd_name ));
398 state->response.result = table->fn(domain, state);
404 DEBUG(10,("process_request: unknown request fn number %d\n",
405 (int)state->request.cmd ));
406 state->response.result = WINBINDD_ERROR;
409 talloc_destroy(state->mem_ctx);
412 void setup_domain_child(struct winbindd_domain *domain,
413 struct winbindd_child *child,
414 const char *explicit_logfile)
416 if (explicit_logfile != NULL) {
417 pstr_sprintf(child->logfilename, "%s/log.winbindd-%s",
418 dyn_LOGFILEBASE, explicit_logfile);
419 } else if (domain != NULL) {
420 pstr_sprintf(child->logfilename, "%s/log.wb-%s",
421 dyn_LOGFILEBASE, domain->name);
423 smb_panic("Internal error: domain == NULL && "
424 "explicit_logfile == NULL");
427 child->domain = domain;
430 struct winbindd_child *children = NULL;
432 void winbind_child_died(pid_t pid)
434 struct winbindd_child *child;
436 for (child = children; child != NULL; child = child->next) {
437 if (child->pid == pid) {
443 DEBUG(0, ("Unknown child %d died!\n", pid));
447 remove_fd_event(&child->event);
448 close(child->event.fd);
450 child->event.flags = 0;
453 schedule_async_request(child);
456 /* Forward the online/offline messages to our children. */
457 void winbind_msg_offline(int msg_type, struct process_id src, void *buf, size_t len)
459 struct winbindd_child *child;
461 DEBUG(10,("winbind_msg_offline: got offline message.\n"));
463 if (!lp_winbind_offline_logon()) {
464 DEBUG(10,("winbind_msg_offline: rejecting offline message.\n"));
468 /* Set our global state as offline. */
469 if (!set_global_winbindd_state_offline()) {
470 DEBUG(10,("winbind_msg_offline: offline request failed.\n"));
474 for (child = children; child != NULL; child = child->next) {
475 DEBUG(10,("winbind_msg_offline: sending message to pid %u.\n",
476 (unsigned int)child->pid ));
477 message_send_pid(pid_to_procid(child->pid), MSG_WINBIND_OFFLINE, NULL, 0, False);
481 /* Forward the online/offline messages to our children. */
482 void winbind_msg_online(int msg_type, struct process_id src, void *buf, size_t len)
484 struct winbindd_child *child;
486 DEBUG(10,("winbind_msg_online: got online message.\n"));
488 if (!lp_winbind_offline_logon()) {
489 DEBUG(10,("winbind_msg_online: rejecting online message.\n"));
493 /* Set our global state as online. */
494 set_global_winbindd_state_online();
496 for (child = children; child != NULL; child = child->next) {
497 DEBUG(10,("winbind_msg_online: sending message to pid %u.\n",
498 (unsigned int)child->pid ));
499 message_send_pid(pid_to_procid(child->pid), MSG_WINBIND_ONLINE, NULL, 0, False);
503 /* Forward the online/offline messages to our children. */
504 void winbind_msg_onlinestatus(int msg_type, struct process_id src, void *buf, size_t len)
506 struct winbindd_child *child;
508 DEBUG(10,("winbind_msg_onlinestatus: got onlinestatus message.\n"));
510 for (child = children; child != NULL; child = child->next) {
511 if (child->domain && child->domain->primary) {
512 DEBUG(10,("winbind_msg_onlinestatus: "
513 "sending message to pid %u of primary domain.\n",
514 (unsigned int)child->pid));
515 message_send_pid(pid_to_procid(child->pid),
516 MSG_WINBIND_ONLINESTATUS, buf, len, False);
523 static void account_lockout_policy_handler(struct timed_event *te,
524 const struct timeval *now,
527 struct winbindd_child *child =
528 (struct winbindd_child *)private_data;
530 struct winbindd_methods *methods;
531 SAM_UNK_INFO_12 lockout_policy;
534 DEBUG(10,("account_lockout_policy_handler called\n"));
536 if (child->lockout_policy_event) {
537 TALLOC_FREE(child->lockout_policy_event);
540 methods = child->domain->methods;
542 result = methods->lockout_policy(child->domain, child->mem_ctx, &lockout_policy);
543 if (!NT_STATUS_IS_OK(result)) {
544 DEBUG(10,("account_lockout_policy_handler: failed to call lockout_policy\n"));
548 child->lockout_policy_event = add_timed_event(child->mem_ctx,
549 timeval_current_ofs(3600, 0),
550 "account_lockout_policy_handler",
551 account_lockout_policy_handler,
555 /* Deal with a request to go offline. */
557 static void child_msg_offline(int msg_type, struct process_id src, void *buf, size_t len)
559 struct winbindd_domain *domain;
561 DEBUG(5,("child_msg_offline received.\n"));
563 if (!lp_winbind_offline_logon()) {
564 DEBUG(10,("child_msg_offline: rejecting offline message.\n"));
568 /* Set our global state as offline. */
569 if (!set_global_winbindd_state_offline()) {
570 DEBUG(10,("child_msg_offline: offline request failed.\n"));
574 /* Mark all our domains as offline. */
576 for (domain = domain_list(); domain; domain = domain->next) {
577 DEBUG(5,("child_msg_offline: marking %s offline.\n", domain->name));
578 domain->online = False;
582 /* Deal with a request to go online. */
584 static void child_msg_online(int msg_type, struct process_id src, void *buf, size_t len)
586 struct winbindd_domain *domain;
588 DEBUG(5,("child_msg_online received.\n"));
590 if (!lp_winbind_offline_logon()) {
591 DEBUG(10,("child_msg_online: rejecting online message.\n"));
595 /* Set our global state as online. */
596 set_global_winbindd_state_online();
598 winbindd_flush_nscd_cache();
600 /* Mark everything online - delete any negative cache entries
601 to force an immediate reconnect. */
603 for (domain = domain_list(); domain; domain = domain->next) {
604 DEBUG(5,("child_msg_online: marking %s online.\n", domain->name));
605 domain->online = True;
606 check_negative_conn_cache_timeout(domain->name, domain->dcname, 0);
610 static const char *collect_onlinestatus(TALLOC_CTX *mem_ctx)
612 struct winbindd_domain *domain;
615 if ((buf = talloc_asprintf(mem_ctx, "global:%s ",
616 get_global_winbindd_state_online() ?
617 "Offline":"Online")) == NULL) {
621 for (domain = domain_list(); domain; domain = domain->next) {
622 if ((buf = talloc_asprintf_append(buf, "%s:%s ",
625 "Online":"Offline")) == NULL) {
630 buf = talloc_asprintf_append(buf, "\n");
632 DEBUG(5,("collect_onlinestatus: %s", buf));
637 static void child_msg_onlinestatus(int msg_type, struct process_id src, void *buf, size_t len)
641 struct process_id *sender;
643 DEBUG(5,("winbind_msg_onlinestatus received.\n"));
649 sender = (struct process_id *)buf;
651 mem_ctx = talloc_init("winbind_msg_onlinestatus");
652 if (mem_ctx == NULL) {
656 message = collect_onlinestatus(mem_ctx);
657 if (message == NULL) {
658 talloc_destroy(mem_ctx);
662 message_send_pid(*sender, MSG_WINBIND_ONLINESTATUS,
663 message, strlen(message) + 1, True);
665 talloc_destroy(mem_ctx);
668 static BOOL fork_domain_child(struct winbindd_child *child)
671 struct winbindd_cli_state state;
672 extern BOOL override_logfile;
674 if (socketpair(AF_UNIX, SOCK_STREAM, 0, fdpair) != 0) {
675 DEBUG(0, ("Could not open child pipe: %s\n",
681 state.pid = getpid();
683 /* Ensure we don't process messages whilst we're
684 changing the disposition for the child. */
687 child->pid = sys_fork();
689 if (child->pid == -1) {
690 DEBUG(0, ("Could not fork: %s\n", strerror(errno)));
695 if (child->pid != 0) {
698 child->next = child->prev = NULL;
699 DLIST_ADD(children, child);
700 child->event.fd = fdpair[1];
701 child->event.flags = 0;
702 child->requests = NULL;
703 add_fd_event(&child->event);
704 /* We're ok with online/offline messages now. */
711 state.sock = fdpair[0];
714 /* tdb needs special fork handling */
715 if (tdb_reopen_all(1) == -1) {
716 DEBUG(0,("tdb_reopen_all failed.\n"));
720 close_conns_after_fork();
722 if (!override_logfile) {
723 lp_set_logfile(child->logfilename);
727 /* Don't handle the same messages as our parent. */
728 message_deregister(MSG_SMB_CONF_UPDATED);
729 message_deregister(MSG_SHUTDOWN);
730 message_deregister(MSG_WINBIND_OFFLINE);
731 message_deregister(MSG_WINBIND_ONLINE);
732 message_deregister(MSG_WINBIND_ONLINESTATUS);
734 /* The child is ok with online/offline messages now. */
737 child->mem_ctx = talloc_init("child_mem_ctx");
738 if (child->mem_ctx == NULL) {
742 if (child->domain != NULL && lp_winbind_offline_logon()) {
743 /* We might be in the idmap child...*/
744 child->lockout_policy_event = add_timed_event(
745 child->mem_ctx, timeval_zero(),
746 "account_lockout_policy_handler",
747 account_lockout_policy_handler,
751 /* Handle online/offline messages. */
752 message_register(MSG_WINBIND_OFFLINE,child_msg_offline);
753 message_register(MSG_WINBIND_ONLINE,child_msg_online);
754 message_register(MSG_WINBIND_ONLINESTATUS,child_msg_onlinestatus);
764 /* free up any talloc memory */
766 main_loop_TALLOC_FREE();
772 tp = get_timed_events_timeout(&t);
774 DEBUG(11,("select will use timeout of %u.%u seconds\n",
775 (unsigned int)tp->tv_sec, (unsigned int)tp->tv_usec ));
778 /* Handle messages */
783 FD_SET(state.sock, &read_fds);
785 ret = sys_select(state.sock + 1, &read_fds, NULL, NULL, tp);
788 DEBUG(11,("nothing is ready yet, continue\n"));
792 if (ret == -1 && errno == EINTR) {
793 /* We got a signal - continue. */
797 if (ret == -1 && errno != EINTR) {
798 DEBUG(0,("select error occured\n"));
803 /* fetch a request from the main daemon */
804 child_read_request(&state);
806 if (state.finished) {
807 /* we lost contact with our parent */
811 DEBUG(4,("child daemon request %d\n", (int)state.request.cmd));
813 ZERO_STRUCT(state.response);
814 state.request.null_term = '\0';
815 child_process_request(child->domain, &state);
817 SAFE_FREE(state.request.extra_data.data);
819 cache_store_response(sys_getpid(), &state.response);
821 SAFE_FREE(state.response.extra_data.data);
823 /* We just send the result code back, the result
824 * structure needs to be fetched via the
825 * winbindd_cache. Hmm. That needs fixing... */
827 if (write_data(state.sock,
828 (const char *)&state.response.result,
829 sizeof(state.response.result)) !=
830 sizeof(state.response.result)) {
831 DEBUG(0, ("Could not write result\n"));