2 Unix SMB/CIFS implementation.
4 Winbind daemon for ntdom nss module
6 Copyright (C) by Tim Potter 2000-2002
7 Copyright (C) Andrew Tridgell 2002
8 Copyright (C) Jelmer Vernooij 2003
9 Copyright (C) Volker Lendecke 2004
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 3 of the License, or
14 (at your option) any later version.
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with this program. If not, see <http://www.gnu.org/licenses/>.
29 #define DBGC_CLASS DBGC_WINBIND
31 static bool opt_nocache
= False
;
32 static bool interactive
= False
;
34 extern bool override_logfile
;
36 struct event_context
*winbind_event_context(void)
38 static struct event_context
*ctx
;
40 if (!ctx
&& !(ctx
= event_context_init(NULL
))) {
41 smb_panic("Could not init winbind event context");
46 struct messaging_context
*winbind_messaging_context(void)
48 static struct messaging_context
*ctx
;
51 ctx
= messaging_init(NULL
, server_id_self(),
52 winbind_event_context());
55 DEBUG(0, ("Could not init winbind messaging context.\n"));
60 /* Reload configuration */
62 static bool reload_services_file(const char *lfile
)
67 const char *fname
= lp_configfile();
69 if (file_exist(fname
) && !strcsequal(fname
,get_dyn_CONFIGFILE())) {
70 set_dyn_CONFIGFILE(fname
);
74 /* if this is a child, restore the logfile to the special
75 name - <domain>, idmap, etc. */
76 if (lfile
&& *lfile
) {
77 lp_set_logfile(lfile
);
81 ret
= lp_load(get_dyn_CONFIGFILE(),False
,False
,True
,True
);
90 /**************************************************************************** **
92 **************************************************************************** */
94 static void fault_quit(void)
99 static void winbindd_status(void)
101 struct winbindd_cli_state
*tmp
;
103 DEBUG(0, ("winbindd status:\n"));
105 /* Print client state information */
107 DEBUG(0, ("\t%d clients currently active\n", winbindd_num_clients()));
109 if (DEBUGLEVEL
>= 2 && winbindd_num_clients()) {
110 DEBUG(2, ("\tclient list:\n"));
111 for(tmp
= winbindd_client_list(); tmp
; tmp
= tmp
->next
) {
112 DEBUGADD(2, ("\t\tpid %lu, sock %d\n",
113 (unsigned long)tmp
->pid
, tmp
->sock
));
118 /* Print winbindd status to log file */
120 static void print_winbindd_status(void)
125 /* Flush client cache */
127 static void flush_caches(void)
129 /* We need to invalidate cached user list entries on a SIGHUP
130 otherwise cached access denied errors due to restrict anonymous
131 hang around until the sequence number changes. */
133 if (!wcache_invalidate_cache()) {
134 DEBUG(0, ("invalidating the cache failed; revalidate the cache\n"));
135 if (!winbindd_cache_validate_and_initialize()) {
141 /* Handle the signal by unlinking socket and exiting */
143 static void terminate(bool is_parent
)
146 /* When parent goes away we should
147 * remove the socket file. Not so
148 * when children terminate.
152 if (asprintf(&path
, "%s/%s",
153 get_winbind_pipe_dir(), WINBINDD_SOCKET_NAME
) > 0) {
161 trustdom_cache_shutdown();
165 TALLOC_CTX
*mem_ctx
= talloc_init("end_description");
166 char *description
= talloc_describe_all(mem_ctx
);
168 DEBUG(3, ("tallocs left:\n%s\n", description
));
169 talloc_destroy(mem_ctx
);
176 static void winbindd_sig_term_handler(struct tevent_context
*ev
,
177 struct tevent_signal
*se
,
183 bool *is_parent
= talloc_get_type_abort(private_data
, bool);
185 DEBUG(0,("Got sig[%d] terminate (is_parent=%d)\n",
186 signum
, (int)*is_parent
));
187 terminate(*is_parent
);
190 bool winbindd_setup_sig_term_handler(bool parent
)
192 struct tevent_signal
*se
;
195 is_parent
= talloc(winbind_event_context(), bool);
202 se
= tevent_add_signal(winbind_event_context(),
205 winbindd_sig_term_handler
,
208 DEBUG(0,("failed to setup SIGTERM handler"));
209 talloc_free(is_parent
);
213 se
= tevent_add_signal(winbind_event_context(),
216 winbindd_sig_term_handler
,
219 DEBUG(0,("failed to setup SIGINT handler"));
220 talloc_free(is_parent
);
224 se
= tevent_add_signal(winbind_event_context(),
227 winbindd_sig_term_handler
,
230 DEBUG(0,("failed to setup SIGINT handler"));
231 talloc_free(is_parent
);
238 static void winbindd_sig_hup_handler(struct tevent_context
*ev
,
239 struct tevent_signal
*se
,
245 const char *file
= (const char *)private_data
;
247 DEBUG(1,("Reloading services after SIGHUP\n"));
249 reload_services_file(file
);
252 bool winbindd_setup_sig_hup_handler(const char *lfile
)
254 struct tevent_signal
*se
;
258 file
= talloc_strdup(winbind_event_context(),
265 se
= tevent_add_signal(winbind_event_context(),
266 winbind_event_context(),
268 winbindd_sig_hup_handler
,
277 static void winbindd_sig_chld_handler(struct tevent_context
*ev
,
278 struct tevent_signal
*se
,
286 while ((pid
= sys_waitpid(-1, NULL
, WNOHANG
)) > 0) {
287 winbind_child_died(pid
);
291 static bool winbindd_setup_sig_chld_handler(void)
293 struct tevent_signal
*se
;
295 se
= tevent_add_signal(winbind_event_context(),
296 winbind_event_context(),
298 winbindd_sig_chld_handler
,
307 static void winbindd_sig_usr2_handler(struct tevent_context
*ev
,
308 struct tevent_signal
*se
,
314 print_winbindd_status();
317 static bool winbindd_setup_sig_usr2_handler(void)
319 struct tevent_signal
*se
;
321 se
= tevent_add_signal(winbind_event_context(),
322 winbind_event_context(),
324 winbindd_sig_usr2_handler
,
333 /* React on 'smbcontrol winbindd reload-config' in the same way as on SIGHUP*/
334 static void msg_reload_services(struct messaging_context
*msg
,
337 struct server_id server_id
,
340 /* Flush various caches */
342 reload_services_file((const char *) private_data
);
345 /* React on 'smbcontrol winbindd shutdown' in the same way as on SIGTERM*/
346 static void msg_shutdown(struct messaging_context
*msg
,
349 struct server_id server_id
,
352 /* only the parent waits for this message */
353 DEBUG(0,("Got shutdown message\n"));
358 static void winbind_msg_validate_cache(struct messaging_context
*msg_ctx
,
361 struct server_id server_id
,
366 struct sigaction act
;
367 struct sigaction oldact
;
369 DEBUG(10, ("winbindd_msg_validate_cache: got validate-cache "
373 * call the validation code from a child:
374 * so we don't block the main winbindd and the validation
375 * code can safely use fork/waitpid...
378 child_pid
= sys_fork();
380 if (child_pid
== -1) {
381 DEBUG(1, ("winbind_msg_validate_cache: Could not fork: %s\n",
386 if (child_pid
!= 0) {
388 DEBUG(5, ("winbind_msg_validate_cache: child created with "
389 "pid %d.\n", (int)child_pid
));
395 /* install default SIGCHLD handler: validation code uses fork/waitpid */
397 act
.sa_handler
= SIG_DFL
;
399 /* We *want* SIGALRM to interrupt a system call. */
400 act
.sa_flags
= SA_RESTART
;
402 sigemptyset(&act
.sa_mask
);
403 sigaddset(&act
.sa_mask
,SIGCHLD
);
404 sigaction(SIGCHLD
,&act
,&oldact
);
406 ret
= (uint8
)winbindd_validate_cache_nobackup();
407 DEBUG(10, ("winbindd_msg_validata_cache: got return value %d\n", ret
));
408 messaging_send_buf(msg_ctx
, server_id
, MSG_WINBIND_VALIDATE_CACHE
, &ret
,
413 static struct winbindd_dispatch_table
{
414 enum winbindd_cmd cmd
;
415 void (*fn
)(struct winbindd_cli_state
*state
);
416 const char *winbindd_cmd_name
;
417 } dispatch_table
[] = {
421 { WINBINDD_GETPWNAM
, winbindd_getpwnam
, "GETPWNAM" },
422 { WINBINDD_GETPWUID
, winbindd_getpwuid
, "GETPWUID" },
423 { WINBINDD_GETPWSID
, winbindd_getpwsid
, "GETPWSID" },
425 { WINBINDD_SETPWENT
, winbindd_setpwent
, "SETPWENT" },
426 { WINBINDD_ENDPWENT
, winbindd_endpwent
, "ENDPWENT" },
427 { WINBINDD_GETPWENT
, winbindd_getpwent
, "GETPWENT" },
429 { WINBINDD_GETGROUPS
, winbindd_getgroups
, "GETGROUPS" },
430 { WINBINDD_GETUSERSIDS
, winbindd_getusersids
, "GETUSERSIDS" },
431 { WINBINDD_GETUSERDOMGROUPS
, winbindd_getuserdomgroups
,
432 "GETUSERDOMGROUPS" },
433 { WINBINDD_GETSIDALIASES
, winbindd_getsidaliases
,
434 "LOOKUPUSERALIASES" },
436 /* Group functions */
438 { WINBINDD_GETGRNAM
, winbindd_getgrnam
, "GETGRNAM" },
439 { WINBINDD_GETGRGID
, winbindd_getgrgid
, "GETGRGID" },
440 { WINBINDD_SETGRENT
, winbindd_setgrent
, "SETGRENT" },
441 { WINBINDD_ENDGRENT
, winbindd_endgrent
, "ENDGRENT" },
442 { WINBINDD_GETGRENT
, winbindd_getgrent
, "GETGRENT" },
443 { WINBINDD_GETGRLST
, winbindd_getgrent
, "GETGRLST" },
445 /* PAM auth functions */
447 { WINBINDD_PAM_AUTH
, winbindd_pam_auth
, "PAM_AUTH" },
448 { WINBINDD_PAM_AUTH_CRAP
, winbindd_pam_auth_crap
, "AUTH_CRAP" },
449 { WINBINDD_PAM_CHAUTHTOK
, winbindd_pam_chauthtok
, "CHAUTHTOK" },
450 { WINBINDD_PAM_LOGOFF
, winbindd_pam_logoff
, "PAM_LOGOFF" },
451 { WINBINDD_PAM_CHNG_PSWD_AUTH_CRAP
, winbindd_pam_chng_pswd_auth_crap
, "CHNG_PSWD_AUTH_CRAP" },
453 /* Enumeration functions */
455 { WINBINDD_LIST_USERS
, winbindd_list_users
, "LIST_USERS" },
456 { WINBINDD_LIST_GROUPS
, winbindd_list_groups
, "LIST_GROUPS" },
457 { WINBINDD_LIST_TRUSTDOM
, winbindd_list_trusted_domains
,
459 { WINBINDD_SHOW_SEQUENCE
, winbindd_show_sequence
, "SHOW_SEQUENCE" },
461 /* SID related functions */
463 { WINBINDD_LOOKUPSID
, winbindd_lookupsid
, "LOOKUPSID" },
464 { WINBINDD_LOOKUPNAME
, winbindd_lookupname
, "LOOKUPNAME" },
465 { WINBINDD_LOOKUPRIDS
, winbindd_lookuprids
, "LOOKUPRIDS" },
467 /* Lookup related functions */
469 { WINBINDD_SID_TO_UID
, winbindd_sid_to_uid
, "SID_TO_UID" },
470 { WINBINDD_SID_TO_GID
, winbindd_sid_to_gid
, "SID_TO_GID" },
471 { WINBINDD_UID_TO_SID
, winbindd_uid_to_sid
, "UID_TO_SID" },
472 { WINBINDD_GID_TO_SID
, winbindd_gid_to_sid
, "GID_TO_SID" },
473 { WINBINDD_ALLOCATE_UID
, winbindd_allocate_uid
, "ALLOCATE_UID" },
474 { WINBINDD_ALLOCATE_GID
, winbindd_allocate_gid
, "ALLOCATE_GID" },
475 { WINBINDD_SET_MAPPING
, winbindd_set_mapping
, "SET_MAPPING" },
476 { WINBINDD_REMOVE_MAPPING
, winbindd_remove_mapping
, "REMOVE_MAPPING" },
477 { WINBINDD_SET_HWM
, winbindd_set_hwm
, "SET_HWMS" },
481 { WINBINDD_CHECK_MACHACC
, winbindd_check_machine_acct
, "CHECK_MACHACC" },
482 { WINBINDD_PING
, winbindd_ping
, "PING" },
483 { WINBINDD_INFO
, winbindd_info
, "INFO" },
484 { WINBINDD_INTERFACE_VERSION
, winbindd_interface_version
,
485 "INTERFACE_VERSION" },
486 { WINBINDD_DOMAIN_NAME
, winbindd_domain_name
, "DOMAIN_NAME" },
487 { WINBINDD_DOMAIN_INFO
, winbindd_domain_info
, "DOMAIN_INFO" },
488 { WINBINDD_NETBIOS_NAME
, winbindd_netbios_name
, "NETBIOS_NAME" },
489 { WINBINDD_PRIV_PIPE_DIR
, winbindd_priv_pipe_dir
,
490 "WINBINDD_PRIV_PIPE_DIR" },
491 { WINBINDD_GETDCNAME
, winbindd_getdcname
, "GETDCNAME" },
492 { WINBINDD_DSGETDCNAME
, winbindd_dsgetdcname
, "DSGETDCNAME" },
494 /* Credential cache access */
495 { WINBINDD_CCACHE_NTLMAUTH
, winbindd_ccache_ntlm_auth
, "NTLMAUTH" },
499 { WINBINDD_WINS_BYNAME
, winbindd_wins_byname
, "WINS_BYNAME" },
500 { WINBINDD_WINS_BYIP
, winbindd_wins_byip
, "WINS_BYIP" },
504 { WINBINDD_NUM_CMDS
, NULL
, "NONE" }
507 static void process_request(struct winbindd_cli_state
*state
)
509 struct winbindd_dispatch_table
*table
= dispatch_table
;
511 ZERO_STRUCT(state
->response
);
513 state
->response
.result
= WINBINDD_PENDING
;
514 state
->response
.length
= sizeof(struct winbindd_response
);
516 state
->mem_ctx
= talloc_init("winbind request");
517 if (state
->mem_ctx
== NULL
)
520 /* Remember who asked us. */
521 state
->pid
= state
->request
.pid
;
523 /* Process command */
525 for (table
= dispatch_table
; table
->fn
; table
++) {
526 if (state
->request
.cmd
== table
->cmd
) {
527 DEBUG(10,("process_request: request fn %s\n",
528 table
->winbindd_cmd_name
));
535 DEBUG(10,("process_request: unknown request fn number %d\n",
536 (int)state
->request
.cmd
));
537 request_error(state
);
542 * A list of file descriptors being monitored by select in the main processing
543 * loop. winbindd_fd_event->handler is called whenever the socket is readable/writable.
546 static struct winbindd_fd_event
*fd_events
= NULL
;
548 void add_fd_event(struct winbindd_fd_event
*ev
)
550 struct winbindd_fd_event
*match
;
552 /* only add unique winbindd_fd_event structs */
554 for (match
=fd_events
; match
; match
=match
->next
) {
556 SMB_ASSERT( match
!= ev
);
563 DLIST_ADD(fd_events
, ev
);
566 void remove_fd_event(struct winbindd_fd_event
*ev
)
568 DLIST_REMOVE(fd_events
, ev
);
572 * Handler for winbindd_fd_events to complete a read/write request, set up by
573 * setup_async_read/setup_async_write.
576 static void rw_callback(struct winbindd_fd_event
*event
, int flags
)
581 todo
= event
->length
- event
->done
;
583 if (event
->flags
& EVENT_FD_WRITE
) {
584 SMB_ASSERT(flags
== EVENT_FD_WRITE
);
585 done
= sys_write(event
->fd
,
586 &((char *)event
->data
)[event
->done
],
591 event
->finished(event
->private_data
, False
);
596 if (event
->flags
& EVENT_FD_READ
) {
597 SMB_ASSERT(flags
== EVENT_FD_READ
);
598 done
= sys_read(event
->fd
, &((char *)event
->data
)[event
->done
],
603 event
->finished(event
->private_data
, False
);
610 if (event
->done
== event
->length
) {
612 event
->finished(event
->private_data
, True
);
617 * Request an async read/write on a winbindd_fd_event structure. (*finished) is called
618 * when the request is completed or an error had occurred.
621 void setup_async_read(struct winbindd_fd_event
*event
, void *data
, size_t length
,
622 void (*finished
)(void *private_data
, bool success
),
625 SMB_ASSERT(event
->flags
== 0);
627 event
->length
= length
;
629 event
->handler
= rw_callback
;
630 event
->finished
= finished
;
631 event
->private_data
= private_data
;
632 event
->flags
= EVENT_FD_READ
;
635 void setup_async_write(struct winbindd_fd_event
*event
, void *data
, size_t length
,
636 void (*finished
)(void *private_data
, bool success
),
639 SMB_ASSERT(event
->flags
== 0);
641 event
->length
= length
;
643 event
->handler
= rw_callback
;
644 event
->finished
= finished
;
645 event
->private_data
= private_data
;
646 event
->flags
= EVENT_FD_WRITE
;
650 * This is the main event loop of winbind requests. It goes through a
651 * state-machine of 3 read/write requests, 4 if you have extra data to send.
653 * An idle winbind client has a read request of 4 bytes outstanding,
654 * finalizing function is request_len_recv, checking the length. request_recv
655 * then processes the packet. The processing function then at some point has
656 * to call request_finished which schedules sending the response.
659 static void request_len_recv(void *private_data
, bool success
);
660 static void request_recv(void *private_data
, bool success
);
661 static void request_main_recv(void *private_data
, bool success
);
662 static void request_finished(struct winbindd_cli_state
*state
);
663 static void response_main_sent(void *private_data
, bool success
);
664 static void response_extra_sent(void *private_data
, bool success
);
666 static void response_extra_sent(void *private_data
, bool success
)
668 struct winbindd_cli_state
*state
=
669 talloc_get_type_abort(private_data
, struct winbindd_cli_state
);
671 TALLOC_FREE(state
->mem_ctx
);
674 state
->finished
= True
;
678 setup_async_read(&state
->fd_event
, &state
->request
, sizeof(uint32
),
679 request_len_recv
, state
);
682 static void response_main_sent(void *private_data
, bool success
)
684 struct winbindd_cli_state
*state
=
685 talloc_get_type_abort(private_data
, struct winbindd_cli_state
);
688 state
->finished
= True
;
692 if (state
->response
.length
== sizeof(state
->response
)) {
693 TALLOC_FREE(state
->mem_ctx
);
695 setup_async_read(&state
->fd_event
, &state
->request
,
696 sizeof(uint32
), request_len_recv
, state
);
700 setup_async_write(&state
->fd_event
, state
->response
.extra_data
.data
,
701 state
->response
.length
- sizeof(state
->response
),
702 response_extra_sent
, state
);
705 static void request_finished(struct winbindd_cli_state
*state
)
707 /* Make sure request.extra_data is freed when finish processing a request */
708 SAFE_FREE(state
->request
.extra_data
.data
);
709 setup_async_write(&state
->fd_event
, &state
->response
,
710 sizeof(state
->response
), response_main_sent
, state
);
713 void request_error(struct winbindd_cli_state
*state
)
715 SMB_ASSERT(state
->response
.result
== WINBINDD_PENDING
);
716 state
->response
.result
= WINBINDD_ERROR
;
717 request_finished(state
);
720 void request_ok(struct winbindd_cli_state
*state
)
722 SMB_ASSERT(state
->response
.result
== WINBINDD_PENDING
);
723 state
->response
.result
= WINBINDD_OK
;
724 request_finished(state
);
727 static void request_len_recv(void *private_data
, bool success
)
729 struct winbindd_cli_state
*state
=
730 talloc_get_type_abort(private_data
, struct winbindd_cli_state
);
733 state
->finished
= True
;
737 if (*(uint32
*)(void *)(&state
->request
) != sizeof(state
->request
)) {
738 DEBUG(0,("request_len_recv: Invalid request size received: %d (expected %u)\n",
739 *(uint32_t *)(void *)(&state
->request
),
740 (uint32_t)sizeof(state
->request
)));
741 state
->finished
= True
;
745 setup_async_read(&state
->fd_event
,
746 (uint32
*)(void *)(&state
->request
)+1,
747 sizeof(state
->request
) - sizeof(uint32
),
748 request_main_recv
, state
);
751 static void request_main_recv(void *private_data
, bool success
)
753 struct winbindd_cli_state
*state
=
754 talloc_get_type_abort(private_data
, struct winbindd_cli_state
);
757 state
->finished
= True
;
761 if (state
->request
.extra_len
== 0) {
762 state
->request
.extra_data
.data
= NULL
;
763 request_recv(state
, True
);
767 if ((!state
->privileged
) &&
768 (state
->request
.extra_len
> WINBINDD_MAX_EXTRA_DATA
)) {
769 DEBUG(3, ("Got request with %d bytes extra data on "
770 "unprivileged socket\n", (int)state
->request
.extra_len
));
771 state
->request
.extra_data
.data
= NULL
;
772 state
->finished
= True
;
776 state
->request
.extra_data
.data
=
777 SMB_MALLOC_ARRAY(char, state
->request
.extra_len
+ 1);
779 if (state
->request
.extra_data
.data
== NULL
) {
780 DEBUG(0, ("malloc failed\n"));
781 state
->finished
= True
;
785 /* Ensure null termination */
786 state
->request
.extra_data
.data
[state
->request
.extra_len
] = '\0';
788 setup_async_read(&state
->fd_event
, state
->request
.extra_data
.data
,
789 state
->request
.extra_len
, request_recv
, state
);
792 static void request_recv(void *private_data
, bool success
)
794 struct winbindd_cli_state
*state
=
795 talloc_get_type_abort(private_data
, struct winbindd_cli_state
);
798 state
->finished
= True
;
802 process_request(state
);
805 /* Process a new connection by adding it to the client connection list */
807 static void new_connection(int listen_sock
, bool privileged
)
809 struct sockaddr_un sunaddr
;
810 struct winbindd_cli_state
*state
;
814 /* Accept connection */
816 len
= sizeof(sunaddr
);
819 sock
= accept(listen_sock
, (struct sockaddr
*)(void *)&sunaddr
,
821 } while (sock
== -1 && errno
== EINTR
);
826 DEBUG(6,("accepted socket %d\n", sock
));
828 /* Create new connection structure */
830 if ((state
= TALLOC_ZERO_P(NULL
, struct winbindd_cli_state
)) == NULL
) {
837 state
->last_access
= time(NULL
);
839 state
->privileged
= privileged
;
841 state
->fd_event
.fd
= state
->sock
;
842 state
->fd_event
.flags
= 0;
843 add_fd_event(&state
->fd_event
);
845 setup_async_read(&state
->fd_event
, &state
->request
, sizeof(uint32
),
846 request_len_recv
, state
);
848 /* Add to connection list */
850 winbindd_add_client(state
);
853 /* Remove a client connection from client connection list */
855 static void remove_client(struct winbindd_cli_state
*state
)
860 /* It's a dead client - hold a funeral */
866 /* tell client, we are closing ... */
867 nwritten
= write(state
->sock
, &c
, sizeof(c
));
868 if (nwritten
== -1) {
869 DEBUG(2, ("final write to client failed: %s\n",
877 /* Free any getent state */
879 free_getent_state(state
->getpwent_state
);
880 free_getent_state(state
->getgrent_state
);
882 TALLOC_FREE(state
->mem_ctx
);
884 remove_fd_event(&state
->fd_event
);
886 /* Remove from list and free */
888 winbindd_remove_client(state
);
892 /* Shutdown client connection which has been idle for the longest time */
894 static bool remove_idle_client(void)
896 struct winbindd_cli_state
*state
, *remove_state
= NULL
;
897 time_t last_access
= 0;
900 for (state
= winbindd_client_list(); state
; state
= state
->next
) {
901 if (state
->response
.result
!= WINBINDD_PENDING
&&
902 !state
->getpwent_state
&& !state
->getgrent_state
) {
904 if (!last_access
|| state
->last_access
< last_access
) {
905 last_access
= state
->last_access
;
906 remove_state
= state
;
912 DEBUG(5,("Found %d idle client connections, shutting down sock %d, pid %u\n",
913 nidle
, remove_state
->sock
, (unsigned int)remove_state
->pid
));
914 remove_client(remove_state
);
921 struct winbindd_listen_state
{
924 struct tevent_fd
*fde
;
927 static void winbindd_listen_fde_handler(struct tevent_context
*ev
,
928 struct tevent_fd
*fde
,
932 struct winbindd_listen_state
*s
= talloc_get_type_abort(private_data
,
933 struct winbindd_listen_state
);
935 while (winbindd_num_clients() >
936 WINBINDD_MAX_SIMULTANEOUS_CLIENTS
- 1) {
937 DEBUG(5,("winbindd: Exceeding %d client "
938 "connections, removing idle "
940 WINBINDD_MAX_SIMULTANEOUS_CLIENTS
));
941 if (!remove_idle_client()) {
942 DEBUG(0,("winbindd: Exceeding %d "
943 "client connections, no idle "
944 "connection found\n",
945 WINBINDD_MAX_SIMULTANEOUS_CLIENTS
));
949 new_connection(s
->fd
, s
->privileged
);
952 static bool winbindd_setup_listeners(void)
954 struct winbindd_listen_state
*pub_state
= NULL
;
955 struct winbindd_listen_state
*priv_state
= NULL
;
957 pub_state
= talloc(winbind_event_context(),
958 struct winbindd_listen_state
);
963 pub_state
->privileged
= false;
964 pub_state
->fd
= open_winbindd_socket();
965 if (pub_state
->fd
== -1) {
969 pub_state
->fde
= tevent_add_fd(winbind_event_context(),
970 pub_state
, pub_state
->fd
,
972 winbindd_listen_fde_handler
,
974 if (!pub_state
->fde
) {
975 close(pub_state
->fd
);
978 tevent_fd_set_auto_close(pub_state
->fde
);
980 priv_state
= talloc(winbind_event_context(),
981 struct winbindd_listen_state
);
986 priv_state
->privileged
= true;
987 priv_state
->fd
= open_winbindd_priv_socket();
988 if (priv_state
->fd
== -1) {
992 priv_state
->fde
= tevent_add_fd(winbind_event_context(),
993 priv_state
, priv_state
->fd
,
995 winbindd_listen_fde_handler
,
997 if (!priv_state
->fde
) {
998 close(priv_state
->fd
);
1001 tevent_fd_set_auto_close(priv_state
->fde
);
1005 TALLOC_FREE(pub_state
);
1006 TALLOC_FREE(priv_state
);
1010 /* Process incoming clients on listen_sock. We use a tricky non-blocking,
1011 non-forking, non-threaded model which allows us to handle many
1012 simultaneous connections while remaining impervious to many denial of
1015 static void process_loop(void)
1017 struct winbindd_fd_event
*ev
;
1018 fd_set r_fds
, w_fds
;
1019 int maxfd
= 0, selret
;
1020 struct timeval timeout
, ev_timeout
;
1022 run_events(winbind_event_context(), 0, NULL
, NULL
);
1024 /* Initialise fd lists for select() */
1029 timeout
.tv_sec
= WINBINDD_ESTABLISH_LOOP
;
1030 timeout
.tv_usec
= 0;
1032 /* Check for any event timeouts. */
1037 event_add_to_select_args(winbind_event_context(), &now
,
1038 &r_fds
, &w_fds
, &ev_timeout
, &maxfd
);
1040 if (get_timed_events_timeout(winbind_event_context(), &ev_timeout
)) {
1041 timeout
= timeval_min(&timeout
, &ev_timeout
);
1044 for (ev
= fd_events
; ev
; ev
= ev
->next
) {
1045 if (ev
->flags
& EVENT_FD_READ
) {
1046 FD_SET(ev
->fd
, &r_fds
);
1047 maxfd
= MAX(ev
->fd
, maxfd
);
1049 if (ev
->flags
& EVENT_FD_WRITE
) {
1050 FD_SET(ev
->fd
, &w_fds
);
1051 maxfd
= MAX(ev
->fd
, maxfd
);
1057 selret
= sys_select(maxfd
+ 1, &r_fds
, &w_fds
, NULL
, &timeout
);
1064 if (errno
== EINTR
) {
1068 /* Select error, something is badly wrong */
1076 run_events(winbind_event_context(), selret
, &r_fds
, &w_fds
);
1079 while (ev
!= NULL
) {
1080 struct winbindd_fd_event
*next
= ev
->next
;
1082 if (FD_ISSET(ev
->fd
, &r_fds
))
1083 flags
|= EVENT_FD_READ
;
1084 if (FD_ISSET(ev
->fd
, &w_fds
))
1085 flags
|= EVENT_FD_WRITE
;
1087 ev
->handler(ev
, flags
);
1095 run_events(winbind_event_context(), selret
, &r_fds
, &w_fds
);
1098 winbindd_check_cache_size(time(NULL
));
1102 bool winbindd_use_idmap_cache(void)
1104 return !opt_nocache
;
1107 bool winbindd_use_cache(void)
1109 return !opt_nocache
;
1114 int main(int argc
, char **argv
, char **envp
)
1116 static bool is_daemon
= False
;
1117 static bool Fork
= True
;
1118 static bool log_stdout
= False
;
1119 static bool no_process_group
= False
;
1123 OPT_NO_PROCESS_GROUP
,
1126 struct poptOption long_options
[] = {
1128 { "stdout", 'S', POPT_ARG_NONE
, NULL
, OPT_LOG_STDOUT
, "Log to stdout" },
1129 { "foreground", 'F', POPT_ARG_NONE
, NULL
, OPT_FORK
, "Daemon in foreground mode" },
1130 { "no-process-group", 0, POPT_ARG_NONE
, NULL
, OPT_NO_PROCESS_GROUP
, "Don't create a new process group" },
1131 { "daemon", 'D', POPT_ARG_NONE
, NULL
, OPT_DAEMON
, "Become a daemon (default)" },
1132 { "interactive", 'i', POPT_ARG_NONE
, NULL
, 'i', "Interactive mode" },
1133 { "no-caching", 'n', POPT_ARG_NONE
, NULL
, 'n', "Disable caching" },
1139 TALLOC_CTX
*frame
= talloc_stackframe();
1141 /* glibc (?) likes to print "User defined signal 1" and exit if a
1142 SIGUSR[12] is received before a handler is installed */
1144 CatchSignal(SIGUSR1
, SIG_IGN
);
1145 CatchSignal(SIGUSR2
, SIG_IGN
);
1147 fault_setup((void (*)(void *))fault_quit
);
1148 dump_core_setup("winbindd");
1152 /* Initialise for running in non-root mode */
1156 set_remote_machine_name("winbindd", False
);
1158 /* Set environment variable so we don't recursively call ourselves.
1159 This may also be useful interactively. */
1161 if ( !winbind_off() ) {
1162 DEBUG(0,("Failed to disable recusive winbindd calls. Exiting.\n"));
1166 /* Initialise samba/rpc client stuff */
1168 pc
= poptGetContext("winbindd", argc
, (const char **)argv
, long_options
, 0);
1170 while ((opt
= poptGetNextOpt(pc
)) != -1) {
1172 /* Don't become a daemon */
1184 case OPT_NO_PROCESS_GROUP
:
1185 no_process_group
= true;
1187 case OPT_LOG_STDOUT
:
1194 d_fprintf(stderr
, "\nInvalid option %s: %s\n\n",
1195 poptBadOption(pc
, 0), poptStrerror(opt
));
1196 poptPrintUsage(pc
, stderr
, 0);
1201 if (is_daemon
&& interactive
) {
1202 d_fprintf(stderr
,"\nERROR: "
1203 "Option -i|--interactive is not allowed together with -D|--daemon\n\n");
1204 poptPrintUsage(pc
, stderr
, 0);
1208 if (log_stdout
&& Fork
) {
1209 d_fprintf(stderr
, "\nERROR: "
1210 "Can't log to stdout (-S) unless daemon is in foreground +(-F) or interactive (-i)\n\n");
1211 poptPrintUsage(pc
, stderr
, 0);
1215 poptFreeContext(pc
);
1217 if (!override_logfile
) {
1219 if (asprintf(&lfile
,"%s/log.winbindd",
1220 get_dyn_LOGFILEBASE()) > 0) {
1221 lp_set_logfile(lfile
);
1225 setup_logging("winbindd", log_stdout
);
1228 DEBUG(0,("winbindd version %s started.\n", samba_version_string()));
1229 DEBUGADD(0,("%s\n", COPYRIGHT_STARTUP_MESSAGE
));
1231 if (!lp_load_initial_only(get_dyn_CONFIGFILE())) {
1232 DEBUG(0, ("error opening config file\n"));
1236 /* Initialise messaging system */
1238 if (winbind_messaging_context() == NULL
) {
1242 if (!reload_services_file(NULL
)) {
1243 DEBUG(0, ("error opening config file\n"));
1247 if (!directory_exist(lp_lockdir())) {
1248 mkdir(lp_lockdir(), 0755);
1258 if (!secrets_init()) {
1260 DEBUG(0,("Could not initialize domain trust account secrets. Giving up\n"));
1264 /* Enable netbios namecache */
1268 /* Unblock all signals we are interested in as they may have been
1269 blocked by the parent process. */
1271 BlockSignals(False
, SIGINT
);
1272 BlockSignals(False
, SIGQUIT
);
1273 BlockSignals(False
, SIGTERM
);
1274 BlockSignals(False
, SIGUSR1
);
1275 BlockSignals(False
, SIGUSR2
);
1276 BlockSignals(False
, SIGHUP
);
1277 BlockSignals(False
, SIGCHLD
);
1280 become_daemon(Fork
, no_process_group
);
1282 pidfile_create("winbindd");
1286 * If we're interactive we want to set our own process group for
1287 * signal management.
1289 if (interactive
&& !no_process_group
)
1290 setpgid( (pid_t
)0, (pid_t
)0);
1295 /* Don't use winbindd_reinit_after_fork here as
1296 * we're just starting up and haven't created any
1297 * winbindd-specific resources we must free yet. JRA.
1300 if (!reinit_after_fork(winbind_messaging_context(),
1301 winbind_event_context(), false)) {
1302 DEBUG(0,("reinit_after_fork() failed\n"));
1306 /* Setup signal handlers */
1308 if (!winbindd_setup_sig_term_handler(true))
1310 if (!winbindd_setup_sig_hup_handler(NULL
))
1312 if (!winbindd_setup_sig_chld_handler())
1314 if (!winbindd_setup_sig_usr2_handler())
1317 CatchSignal(SIGPIPE
, SIG_IGN
); /* Ignore sigpipe */
1320 * Ensure all cache and idmap caches are consistent
1321 * and initialized before we startup.
1323 if (!winbindd_cache_validate_and_initialize()) {
1327 /* get broadcast messages */
1328 claim_connection(NULL
,"",FLAG_MSG_GENERAL
|FLAG_MSG_DBWRAP
);
1330 /* React on 'smbcontrol winbindd reload-config' in the same way
1331 as to SIGHUP signal */
1332 messaging_register(winbind_messaging_context(), NULL
,
1333 MSG_SMB_CONF_UPDATED
, msg_reload_services
);
1334 messaging_register(winbind_messaging_context(), NULL
,
1335 MSG_SHUTDOWN
, msg_shutdown
);
1337 /* Handle online/offline messages. */
1338 messaging_register(winbind_messaging_context(), NULL
,
1339 MSG_WINBIND_OFFLINE
, winbind_msg_offline
);
1340 messaging_register(winbind_messaging_context(), NULL
,
1341 MSG_WINBIND_ONLINE
, winbind_msg_online
);
1342 messaging_register(winbind_messaging_context(), NULL
,
1343 MSG_WINBIND_ONLINESTATUS
, winbind_msg_onlinestatus
);
1345 messaging_register(winbind_messaging_context(), NULL
,
1346 MSG_DUMP_EVENT_LIST
, winbind_msg_dump_event_list
);
1348 messaging_register(winbind_messaging_context(), NULL
,
1349 MSG_WINBIND_VALIDATE_CACHE
,
1350 winbind_msg_validate_cache
);
1352 messaging_register(winbind_messaging_context(), NULL
,
1353 MSG_WINBIND_DUMP_DOMAIN_LIST
,
1354 winbind_msg_dump_domain_list
);
1356 /* Register handler for MSG_DEBUG. */
1357 messaging_register(winbind_messaging_context(), NULL
,
1361 netsamlogon_cache_init(); /* Non-critical */
1363 /* clear the cached list of trusted domains */
1367 if (!init_domain_list()) {
1368 DEBUG(0,("unable to initialize domain list\n"));
1373 init_locator_child();
1375 smb_nscd_flush_user_cache();
1376 smb_nscd_flush_group_cache();
1378 /* setup listen sockets */
1380 if (!winbindd_setup_listeners()) {
1381 DEBUG(0,("winbindd_setup_listeners() failed\n"));
1386 /* Loop waiting for requests */
1388 struct winbindd_cli_state
*state
;
1390 frame
= talloc_stackframe();
1392 /* refresh the trusted domain cache */
1394 rescan_trusted_domains();
1396 /* Dispose of client connection if it is marked as
1398 state
= winbindd_client_list();
1400 struct winbindd_cli_state
*next
= state
->next
;
1402 if (state
->finished
) {
1403 remove_client(state
);