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 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
;
50 if (!ctx
&& !(ctx
= messaging_init(NULL
, server_id_self(),
51 winbind_event_context()))) {
52 smb_panic("Could not init winbind messaging context");
57 /* Reload configuration */
59 static bool reload_services_file(const char *logfile
)
64 const char *fname
= lp_configfile();
66 if (file_exist(fname
,NULL
) && !strcsequal(fname
,get_dyn_CONFIGFILE())) {
67 set_dyn_CONFIGFILE(fname
);
71 /* if this is a child, restore the logfile to the special
72 name - <domain>, idmap, etc. */
73 if (logfile
&& *logfile
) {
74 lp_set_logfile(logfile
);
78 ret
= lp_load(get_dyn_CONFIGFILE(),False
,False
,True
,True
);
87 /**************************************************************************** **
89 **************************************************************************** */
91 static void fault_quit(void)
96 static void winbindd_status(void)
98 struct winbindd_cli_state
*tmp
;
100 DEBUG(0, ("winbindd status:\n"));
102 /* Print client state information */
104 DEBUG(0, ("\t%d clients currently active\n", winbindd_num_clients()));
106 if (DEBUGLEVEL
>= 2 && winbindd_num_clients()) {
107 DEBUG(2, ("\tclient list:\n"));
108 for(tmp
= winbindd_client_list(); tmp
; tmp
= tmp
->next
) {
109 DEBUGADD(2, ("\t\tpid %lu, sock %d\n",
110 (unsigned long)tmp
->pid
, tmp
->sock
));
115 /* Print winbindd status to log file */
117 static void print_winbindd_status(void)
122 /* Flush client cache */
124 static void flush_caches(void)
126 /* We need to invalidate cached user list entries on a SIGHUP
127 otherwise cached access denied errors due to restrict anonymous
128 hang around until the sequence number changes. */
130 if (!wcache_invalidate_cache()) {
131 DEBUG(0, ("invalidating the cache failed; revalidate the cache\n"));
132 if (!winbindd_cache_validate_and_initialize()) {
138 /* Handle the signal by unlinking socket and exiting */
140 static void terminate(bool is_parent
)
143 /* When parent goes away we should
144 * remove the socket file. Not so
145 * when children terminate.
149 if (asprintf(&path
, "%s/%s",
150 get_winbind_pipe_dir(), WINBINDD_SOCKET_NAME
) > 0) {
158 trustdom_cache_shutdown();
162 TALLOC_CTX
*mem_ctx
= talloc_init("end_description");
163 char *description
= talloc_describe_all(mem_ctx
);
165 DEBUG(3, ("tallocs left:\n%s\n", description
));
166 talloc_destroy(mem_ctx
);
173 static bool do_sigterm
;
175 static void termination_handler(int signum
)
178 sys_select_signal(signum
);
181 static bool do_sigusr2
;
183 static void sigusr2_handler(int signum
)
186 sys_select_signal(SIGUSR2
);
189 static bool do_sighup
;
191 static void sighup_handler(int signum
)
194 sys_select_signal(SIGHUP
);
197 static bool do_sigchld
;
199 static void sigchld_handler(int signum
)
202 sys_select_signal(SIGCHLD
);
205 /* React on 'smbcontrol winbindd reload-config' in the same way as on SIGHUP*/
206 static void msg_reload_services(struct messaging_context
*msg
,
209 struct server_id server_id
,
212 /* Flush various caches */
214 reload_services_file((const char *) private_data
);
217 /* React on 'smbcontrol winbindd shutdown' in the same way as on SIGTERM*/
218 static void msg_shutdown(struct messaging_context
*msg
,
221 struct server_id server_id
,
228 static void winbind_msg_validate_cache(struct messaging_context
*msg_ctx
,
231 struct server_id server_id
,
236 struct sigaction act
;
237 struct sigaction oldact
;
239 DEBUG(10, ("winbindd_msg_validate_cache: got validate-cache "
243 * call the validation code from a child:
244 * so we don't block the main winbindd and the validation
245 * code can safely use fork/waitpid...
248 child_pid
= sys_fork();
250 if (child_pid
== -1) {
251 DEBUG(1, ("winbind_msg_validate_cache: Could not fork: %s\n",
256 if (child_pid
!= 0) {
258 DEBUG(5, ("winbind_msg_validate_cache: child created with "
259 "pid %d.\n", child_pid
));
265 /* install default SIGCHLD handler: validation code uses fork/waitpid */
267 act
.sa_handler
= SIG_DFL
;
269 /* We *want* SIGALRM to interrupt a system call. */
270 act
.sa_flags
= SA_RESTART
;
272 sigemptyset(&act
.sa_mask
);
273 sigaddset(&act
.sa_mask
,SIGCHLD
);
274 sigaction(SIGCHLD
,&act
,&oldact
);
276 ret
= (uint8
)winbindd_validate_cache_nobackup();
277 DEBUG(10, ("winbindd_msg_validata_cache: got return value %d\n", ret
));
278 messaging_send_buf(msg_ctx
, server_id
, MSG_WINBIND_VALIDATE_CACHE
, &ret
,
283 static struct winbindd_dispatch_table
{
284 enum winbindd_cmd cmd
;
285 void (*fn
)(struct winbindd_cli_state
*state
);
286 const char *winbindd_cmd_name
;
287 } dispatch_table
[] = {
291 { WINBINDD_GETPWNAM
, winbindd_getpwnam
, "GETPWNAM" },
292 { WINBINDD_GETPWUID
, winbindd_getpwuid
, "GETPWUID" },
294 { WINBINDD_SETPWENT
, winbindd_setpwent
, "SETPWENT" },
295 { WINBINDD_ENDPWENT
, winbindd_endpwent
, "ENDPWENT" },
296 { WINBINDD_GETPWENT
, winbindd_getpwent
, "GETPWENT" },
298 { WINBINDD_GETGROUPS
, winbindd_getgroups
, "GETGROUPS" },
299 { WINBINDD_GETUSERSIDS
, winbindd_getusersids
, "GETUSERSIDS" },
300 { WINBINDD_GETUSERDOMGROUPS
, winbindd_getuserdomgroups
,
301 "GETUSERDOMGROUPS" },
303 /* Group functions */
305 { WINBINDD_GETGRNAM
, winbindd_getgrnam
, "GETGRNAM" },
306 { WINBINDD_GETGRGID
, winbindd_getgrgid
, "GETGRGID" },
307 { WINBINDD_SETGRENT
, winbindd_setgrent
, "SETGRENT" },
308 { WINBINDD_ENDGRENT
, winbindd_endgrent
, "ENDGRENT" },
309 { WINBINDD_GETGRENT
, winbindd_getgrent
, "GETGRENT" },
310 { WINBINDD_GETGRLST
, winbindd_getgrent
, "GETGRLST" },
312 /* PAM auth functions */
314 { WINBINDD_PAM_AUTH
, winbindd_pam_auth
, "PAM_AUTH" },
315 { WINBINDD_PAM_AUTH_CRAP
, winbindd_pam_auth_crap
, "AUTH_CRAP" },
316 { WINBINDD_PAM_CHAUTHTOK
, winbindd_pam_chauthtok
, "CHAUTHTOK" },
317 { WINBINDD_PAM_LOGOFF
, winbindd_pam_logoff
, "PAM_LOGOFF" },
318 { WINBINDD_PAM_CHNG_PSWD_AUTH_CRAP
, winbindd_pam_chng_pswd_auth_crap
, "CHNG_PSWD_AUTH_CRAP" },
320 /* Enumeration functions */
322 { WINBINDD_LIST_USERS
, winbindd_list_users
, "LIST_USERS" },
323 { WINBINDD_LIST_GROUPS
, winbindd_list_groups
, "LIST_GROUPS" },
324 { WINBINDD_LIST_TRUSTDOM
, winbindd_list_trusted_domains
,
326 { WINBINDD_SHOW_SEQUENCE
, winbindd_show_sequence
, "SHOW_SEQUENCE" },
328 /* SID related functions */
330 { WINBINDD_LOOKUPSID
, winbindd_lookupsid
, "LOOKUPSID" },
331 { WINBINDD_LOOKUPNAME
, winbindd_lookupname
, "LOOKUPNAME" },
332 { WINBINDD_LOOKUPRIDS
, winbindd_lookuprids
, "LOOKUPRIDS" },
334 /* Lookup related functions */
336 { WINBINDD_SID_TO_UID
, winbindd_sid_to_uid
, "SID_TO_UID" },
337 { WINBINDD_SID_TO_GID
, winbindd_sid_to_gid
, "SID_TO_GID" },
338 { WINBINDD_UID_TO_SID
, winbindd_uid_to_sid
, "UID_TO_SID" },
339 { WINBINDD_GID_TO_SID
, winbindd_gid_to_sid
, "GID_TO_SID" },
340 #if 0 /* DISABLED until we fix the interface in Samba 3.0.26 --jerry */
341 { WINBINDD_SIDS_TO_XIDS
, winbindd_sids_to_unixids
, "SIDS_TO_XIDS" },
342 #endif /* end DISABLED */
343 { WINBINDD_ALLOCATE_UID
, winbindd_allocate_uid
, "ALLOCATE_UID" },
344 { WINBINDD_ALLOCATE_GID
, winbindd_allocate_gid
, "ALLOCATE_GID" },
345 { WINBINDD_SET_MAPPING
, winbindd_set_mapping
, "SET_MAPPING" },
346 { WINBINDD_SET_HWM
, winbindd_set_hwm
, "SET_HWMS" },
350 { WINBINDD_CHECK_MACHACC
, winbindd_check_machine_acct
, "CHECK_MACHACC" },
351 { WINBINDD_PING
, winbindd_ping
, "PING" },
352 { WINBINDD_INFO
, winbindd_info
, "INFO" },
353 { WINBINDD_INTERFACE_VERSION
, winbindd_interface_version
,
354 "INTERFACE_VERSION" },
355 { WINBINDD_DOMAIN_NAME
, winbindd_domain_name
, "DOMAIN_NAME" },
356 { WINBINDD_DOMAIN_INFO
, winbindd_domain_info
, "DOMAIN_INFO" },
357 { WINBINDD_NETBIOS_NAME
, winbindd_netbios_name
, "NETBIOS_NAME" },
358 { WINBINDD_PRIV_PIPE_DIR
, winbindd_priv_pipe_dir
,
359 "WINBINDD_PRIV_PIPE_DIR" },
360 { WINBINDD_GETDCNAME
, winbindd_getdcname
, "GETDCNAME" },
361 { WINBINDD_DSGETDCNAME
, winbindd_dsgetdcname
, "DSGETDCNAME" },
363 /* Credential cache access */
364 { WINBINDD_CCACHE_NTLMAUTH
, winbindd_ccache_ntlm_auth
, "NTLMAUTH" },
368 { WINBINDD_WINS_BYNAME
, winbindd_wins_byname
, "WINS_BYNAME" },
369 { WINBINDD_WINS_BYIP
, winbindd_wins_byip
, "WINS_BYIP" },
373 { WINBINDD_NUM_CMDS
, NULL
, "NONE" }
376 static void process_request(struct winbindd_cli_state
*state
)
378 struct winbindd_dispatch_table
*table
= dispatch_table
;
380 /* Free response data - we may be interrupted and receive another
381 command before being able to send this data off. */
383 SAFE_FREE(state
->response
.extra_data
.data
);
385 ZERO_STRUCT(state
->response
);
387 state
->response
.result
= WINBINDD_PENDING
;
388 state
->response
.length
= sizeof(struct winbindd_response
);
390 state
->mem_ctx
= talloc_init("winbind request");
391 if (state
->mem_ctx
== NULL
)
394 /* Remember who asked us. */
395 state
->pid
= state
->request
.pid
;
397 /* Process command */
399 for (table
= dispatch_table
; table
->fn
; table
++) {
400 if (state
->request
.cmd
== table
->cmd
) {
401 DEBUG(10,("process_request: request fn %s\n",
402 table
->winbindd_cmd_name
));
409 DEBUG(10,("process_request: unknown request fn number %d\n",
410 (int)state
->request
.cmd
));
411 request_error(state
);
416 * A list of file descriptors being monitored by select in the main processing
417 * loop. fd_event->handler is called whenever the socket is readable/writable.
420 static struct fd_event
*fd_events
= NULL
;
422 void add_fd_event(struct fd_event
*ev
)
424 struct fd_event
*match
;
426 /* only add unique fd_event structs */
428 for (match
=fd_events
; match
; match
=match
->next
) {
430 SMB_ASSERT( match
!= ev
);
437 DLIST_ADD(fd_events
, ev
);
440 void remove_fd_event(struct fd_event
*ev
)
442 DLIST_REMOVE(fd_events
, ev
);
446 * Handler for fd_events to complete a read/write request, set up by
447 * setup_async_read/setup_async_write.
450 static void rw_callback(struct fd_event
*event
, int flags
)
455 todo
= event
->length
- event
->done
;
457 if (event
->flags
& EVENT_FD_WRITE
) {
458 SMB_ASSERT(flags
== EVENT_FD_WRITE
);
459 done
= sys_write(event
->fd
,
460 &((char *)event
->data
)[event
->done
],
465 event
->finished(event
->private_data
, False
);
470 if (event
->flags
& EVENT_FD_READ
) {
471 SMB_ASSERT(flags
== EVENT_FD_READ
);
472 done
= sys_read(event
->fd
, &((char *)event
->data
)[event
->done
],
477 event
->finished(event
->private_data
, False
);
484 if (event
->done
== event
->length
) {
486 event
->finished(event
->private_data
, True
);
491 * Request an async read/write on a fd_event structure. (*finished) is called
492 * when the request is completed or an error had occurred.
495 void setup_async_read(struct fd_event
*event
, void *data
, size_t length
,
496 void (*finished
)(void *private_data
, bool success
),
499 SMB_ASSERT(event
->flags
== 0);
501 event
->length
= length
;
503 event
->handler
= rw_callback
;
504 event
->finished
= finished
;
505 event
->private_data
= private_data
;
506 event
->flags
= EVENT_FD_READ
;
509 void setup_async_write(struct fd_event
*event
, void *data
, size_t length
,
510 void (*finished
)(void *private_data
, bool success
),
513 SMB_ASSERT(event
->flags
== 0);
515 event
->length
= length
;
517 event
->handler
= rw_callback
;
518 event
->finished
= finished
;
519 event
->private_data
= private_data
;
520 event
->flags
= EVENT_FD_WRITE
;
524 * This is the main event loop of winbind requests. It goes through a
525 * state-machine of 3 read/write requests, 4 if you have extra data to send.
527 * An idle winbind client has a read request of 4 bytes outstanding,
528 * finalizing function is request_len_recv, checking the length. request_recv
529 * then processes the packet. The processing function then at some point has
530 * to call request_finished which schedules sending the response.
533 static void request_len_recv(void *private_data
, bool success
);
534 static void request_recv(void *private_data
, bool success
);
535 static void request_main_recv(void *private_data
, bool success
);
536 static void request_finished(struct winbindd_cli_state
*state
);
537 void request_finished_cont(void *private_data
, bool success
);
538 static void response_main_sent(void *private_data
, bool success
);
539 static void response_extra_sent(void *private_data
, bool success
);
541 static void response_extra_sent(void *private_data
, bool success
)
543 struct winbindd_cli_state
*state
=
544 talloc_get_type_abort(private_data
, struct winbindd_cli_state
);
546 if (state
->mem_ctx
!= NULL
) {
547 talloc_destroy(state
->mem_ctx
);
548 state
->mem_ctx
= NULL
;
552 state
->finished
= True
;
556 SAFE_FREE(state
->response
.extra_data
.data
);
558 setup_async_read(&state
->fd_event
, &state
->request
, sizeof(uint32
),
559 request_len_recv
, state
);
562 static void response_main_sent(void *private_data
, bool success
)
564 struct winbindd_cli_state
*state
=
565 talloc_get_type_abort(private_data
, struct winbindd_cli_state
);
568 state
->finished
= True
;
572 if (state
->response
.length
== sizeof(state
->response
)) {
573 if (state
->mem_ctx
!= NULL
) {
574 talloc_destroy(state
->mem_ctx
);
575 state
->mem_ctx
= NULL
;
578 setup_async_read(&state
->fd_event
, &state
->request
,
579 sizeof(uint32
), request_len_recv
, state
);
583 setup_async_write(&state
->fd_event
, state
->response
.extra_data
.data
,
584 state
->response
.length
- sizeof(state
->response
),
585 response_extra_sent
, state
);
588 static void request_finished(struct winbindd_cli_state
*state
)
590 /* Make sure request.extra_data is freed when finish processing a request */
591 SAFE_FREE(state
->request
.extra_data
.data
);
592 setup_async_write(&state
->fd_event
, &state
->response
,
593 sizeof(state
->response
), response_main_sent
, state
);
596 void request_error(struct winbindd_cli_state
*state
)
598 SMB_ASSERT(state
->response
.result
== WINBINDD_PENDING
);
599 state
->response
.result
= WINBINDD_ERROR
;
600 request_finished(state
);
603 void request_ok(struct winbindd_cli_state
*state
)
605 SMB_ASSERT(state
->response
.result
== WINBINDD_PENDING
);
606 state
->response
.result
= WINBINDD_OK
;
607 request_finished(state
);
610 void request_finished_cont(void *private_data
, bool success
)
612 struct winbindd_cli_state
*state
=
613 talloc_get_type_abort(private_data
, struct winbindd_cli_state
);
618 request_error(state
);
621 static void request_len_recv(void *private_data
, bool success
)
623 struct winbindd_cli_state
*state
=
624 talloc_get_type_abort(private_data
, struct winbindd_cli_state
);
627 state
->finished
= True
;
631 if (*(uint32
*)(&state
->request
) != sizeof(state
->request
)) {
632 DEBUG(0,("request_len_recv: Invalid request size received: %d (expected %u)\n",
633 *(uint32_t *)(&state
->request
), (uint32_t)sizeof(state
->request
)));
634 state
->finished
= True
;
638 setup_async_read(&state
->fd_event
, (uint32
*)(&state
->request
)+1,
639 sizeof(state
->request
) - sizeof(uint32
),
640 request_main_recv
, state
);
643 static void request_main_recv(void *private_data
, bool success
)
645 struct winbindd_cli_state
*state
=
646 talloc_get_type_abort(private_data
, struct winbindd_cli_state
);
649 state
->finished
= True
;
653 if (state
->request
.extra_len
== 0) {
654 state
->request
.extra_data
.data
= NULL
;
655 request_recv(state
, True
);
659 if ((!state
->privileged
) &&
660 (state
->request
.extra_len
> WINBINDD_MAX_EXTRA_DATA
)) {
661 DEBUG(3, ("Got request with %d bytes extra data on "
662 "unprivileged socket\n", (int)state
->request
.extra_len
));
663 state
->request
.extra_data
.data
= NULL
;
664 state
->finished
= True
;
668 state
->request
.extra_data
.data
=
669 SMB_MALLOC_ARRAY(char, state
->request
.extra_len
+ 1);
671 if (state
->request
.extra_data
.data
== NULL
) {
672 DEBUG(0, ("malloc failed\n"));
673 state
->finished
= True
;
677 /* Ensure null termination */
678 state
->request
.extra_data
.data
[state
->request
.extra_len
] = '\0';
680 setup_async_read(&state
->fd_event
, state
->request
.extra_data
.data
,
681 state
->request
.extra_len
, request_recv
, state
);
684 static void request_recv(void *private_data
, bool success
)
686 struct winbindd_cli_state
*state
=
687 talloc_get_type_abort(private_data
, struct winbindd_cli_state
);
690 state
->finished
= True
;
694 process_request(state
);
697 /* Process a new connection by adding it to the client connection list */
699 static void new_connection(int listen_sock
, bool privileged
)
701 struct sockaddr_un sunaddr
;
702 struct winbindd_cli_state
*state
;
706 /* Accept connection */
708 len
= sizeof(sunaddr
);
711 sock
= accept(listen_sock
, (struct sockaddr
*)&sunaddr
, &len
);
712 } while (sock
== -1 && errno
== EINTR
);
717 DEBUG(6,("accepted socket %d\n", sock
));
719 /* Create new connection structure */
721 if ((state
= TALLOC_ZERO_P(NULL
, struct winbindd_cli_state
)) == NULL
) {
728 state
->last_access
= time(NULL
);
730 state
->privileged
= privileged
;
732 state
->fd_event
.fd
= state
->sock
;
733 state
->fd_event
.flags
= 0;
734 add_fd_event(&state
->fd_event
);
736 setup_async_read(&state
->fd_event
, &state
->request
, sizeof(uint32
),
737 request_len_recv
, state
);
739 /* Add to connection list */
741 winbindd_add_client(state
);
744 /* Remove a client connection from client connection list */
746 static void remove_client(struct winbindd_cli_state
*state
)
748 /* It's a dead client - hold a funeral */
758 /* Free any getent state */
760 free_getent_state(state
->getpwent_state
);
761 free_getent_state(state
->getgrent_state
);
763 /* We may have some extra data that was not freed if the client was
764 killed unexpectedly */
766 SAFE_FREE(state
->response
.extra_data
.data
);
768 if (state
->mem_ctx
!= NULL
) {
769 talloc_destroy(state
->mem_ctx
);
770 state
->mem_ctx
= NULL
;
773 remove_fd_event(&state
->fd_event
);
775 /* Remove from list and free */
777 winbindd_remove_client(state
);
781 /* Shutdown client connection which has been idle for the longest time */
783 static bool remove_idle_client(void)
785 struct winbindd_cli_state
*state
, *remove_state
= NULL
;
786 time_t last_access
= 0;
789 for (state
= winbindd_client_list(); state
; state
= state
->next
) {
790 if (state
->response
.result
!= WINBINDD_PENDING
&&
791 !state
->getpwent_state
&& !state
->getgrent_state
) {
793 if (!last_access
|| state
->last_access
< last_access
) {
794 last_access
= state
->last_access
;
795 remove_state
= state
;
801 DEBUG(5,("Found %d idle client connections, shutting down sock %d, pid %u\n",
802 nidle
, remove_state
->sock
, (unsigned int)remove_state
->pid
));
803 remove_client(remove_state
);
810 /* check if HUP has been received and reload files */
811 void winbind_check_sighup(const char *logfile
)
815 DEBUG(3, ("got SIGHUP\n"));
818 reload_services_file(logfile
);
824 /* check if TERM has been received */
825 void winbind_check_sigterm(bool is_parent
)
828 terminate(is_parent
);
831 /* Process incoming clients on listen_sock. We use a tricky non-blocking,
832 non-forking, non-threaded model which allows us to handle many
833 simultaneous connections while remaining impervious to many denial of
836 static void process_loop(void)
838 struct winbindd_cli_state
*state
;
841 int maxfd
, listen_sock
, listen_priv_sock
, selret
;
842 struct timeval timeout
, ev_timeout
;
844 /* Open Sockets here to get stuff going ASAP */
845 listen_sock
= open_winbindd_socket();
846 listen_priv_sock
= open_winbindd_priv_socket();
848 if (listen_sock
== -1 || listen_priv_sock
== -1) {
849 perror("open_winbind_socket");
853 /* We'll be doing this a lot */
855 /* Handle messages */
857 message_dispatch(winbind_messaging_context());
859 run_events(winbind_event_context(), 0, NULL
, NULL
);
861 /* refresh the trusted domain cache */
863 rescan_trusted_domains();
865 /* Initialise fd lists for select() */
867 maxfd
= MAX(listen_sock
, listen_priv_sock
);
871 FD_SET(listen_sock
, &r_fds
);
872 FD_SET(listen_priv_sock
, &r_fds
);
874 timeout
.tv_sec
= WINBINDD_ESTABLISH_LOOP
;
877 /* Check for any event timeouts. */
878 if (get_timed_events_timeout(winbind_event_context(), &ev_timeout
)) {
879 timeout
= timeval_min(&timeout
, &ev_timeout
);
882 /* Set up client readers and writers */
884 state
= winbindd_client_list();
888 struct winbindd_cli_state
*next
= state
->next
;
890 /* Dispose of client connection if it is marked as
894 remove_client(state
);
899 for (ev
= fd_events
; ev
; ev
= ev
->next
) {
900 if (ev
->flags
& EVENT_FD_READ
) {
901 FD_SET(ev
->fd
, &r_fds
);
902 maxfd
= MAX(ev
->fd
, maxfd
);
904 if (ev
->flags
& EVENT_FD_WRITE
) {
905 FD_SET(ev
->fd
, &w_fds
);
906 maxfd
= MAX(ev
->fd
, maxfd
);
912 selret
= sys_select(maxfd
+ 1, &r_fds
, &w_fds
, NULL
, &timeout
);
919 if (errno
== EINTR
) {
923 /* Select error, something is badly wrong */
933 struct fd_event
*next
= ev
->next
;
935 if (FD_ISSET(ev
->fd
, &r_fds
))
936 flags
|= EVENT_FD_READ
;
937 if (FD_ISSET(ev
->fd
, &w_fds
))
938 flags
|= EVENT_FD_WRITE
;
940 ev
->handler(ev
, flags
);
944 if (FD_ISSET(listen_sock
, &r_fds
)) {
945 while (winbindd_num_clients() >
946 WINBINDD_MAX_SIMULTANEOUS_CLIENTS
- 1) {
947 DEBUG(5,("winbindd: Exceeding %d client "
948 "connections, removing idle "
950 WINBINDD_MAX_SIMULTANEOUS_CLIENTS
));
951 if (!remove_idle_client()) {
952 DEBUG(0,("winbindd: Exceeding %d "
953 "client connections, no idle "
954 "connection found\n",
955 WINBINDD_MAX_SIMULTANEOUS_CLIENTS
));
959 /* new, non-privileged connection */
960 new_connection(listen_sock
, False
);
963 if (FD_ISSET(listen_priv_sock
, &r_fds
)) {
964 while (winbindd_num_clients() >
965 WINBINDD_MAX_SIMULTANEOUS_CLIENTS
- 1) {
966 DEBUG(5,("winbindd: Exceeding %d client "
967 "connections, removing idle "
969 WINBINDD_MAX_SIMULTANEOUS_CLIENTS
));
970 if (!remove_idle_client()) {
971 DEBUG(0,("winbindd: Exceeding %d "
972 "client connections, no idle "
973 "connection found\n",
974 WINBINDD_MAX_SIMULTANEOUS_CLIENTS
));
978 /* new, privileged connection */
979 new_connection(listen_priv_sock
, True
);
985 winbindd_check_cache_size(time(NULL
));
988 /* Check signal handling things */
990 winbind_check_sigterm(true);
991 winbind_check_sighup(NULL
);
994 print_winbindd_status();
1003 while ((pid
= sys_waitpid(-1, NULL
, WNOHANG
)) > 0) {
1004 winbind_child_died(pid
);
1011 int main(int argc
, char **argv
, char **envp
)
1013 static bool is_daemon
= False
;
1014 static bool Fork
= True
;
1015 static bool log_stdout
= False
;
1016 static bool no_process_group
= False
;
1020 OPT_NO_PROCESS_GROUP
,
1023 struct poptOption long_options
[] = {
1025 { "stdout", 'S', POPT_ARG_NONE
, NULL
, OPT_LOG_STDOUT
, "Log to stdout" },
1026 { "foreground", 'F', POPT_ARG_NONE
, NULL
, OPT_FORK
, "Daemon in foreground mode" },
1027 { "no-process-group", 0, POPT_ARG_NONE
, NULL
, OPT_NO_PROCESS_GROUP
, "Don't create a new process group" },
1028 { "daemon", 'D', POPT_ARG_NONE
, NULL
, OPT_DAEMON
, "Become a daemon (default)" },
1029 { "interactive", 'i', POPT_ARG_NONE
, NULL
, 'i', "Interactive mode" },
1030 { "no-caching", 'n', POPT_ARG_NONE
, NULL
, 'n', "Disable caching" },
1036 TALLOC_CTX
*frame
= talloc_stackframe();
1038 /* glibc (?) likes to print "User defined signal 1" and exit if a
1039 SIGUSR[12] is received before a handler is installed */
1041 CatchSignal(SIGUSR1
, SIG_IGN
);
1042 CatchSignal(SIGUSR2
, SIG_IGN
);
1044 fault_setup((void (*)(void *))fault_quit
);
1045 dump_core_setup("winbindd");
1049 db_tdb2_setup_messaging(NULL
, false);
1051 /* Initialise for running in non-root mode */
1055 set_remote_machine_name("winbindd", False
);
1057 /* Set environment variable so we don't recursively call ourselves.
1058 This may also be useful interactively. */
1060 if ( !winbind_off() ) {
1061 DEBUG(0,("Failed to disable recusive winbindd calls. Exiting.\n"));
1065 /* Initialise samba/rpc client stuff */
1067 pc
= poptGetContext("winbindd", argc
, (const char **)argv
, long_options
, 0);
1069 while ((opt
= poptGetNextOpt(pc
)) != -1) {
1071 /* Don't become a daemon */
1083 case OPT_NO_PROCESS_GROUP
:
1084 no_process_group
= true;
1086 case OPT_LOG_STDOUT
:
1093 d_fprintf(stderr
, "\nInvalid option %s: %s\n\n",
1094 poptBadOption(pc
, 0), poptStrerror(opt
));
1095 poptPrintUsage(pc
, stderr
, 0);
1100 if (is_daemon
&& interactive
) {
1101 d_fprintf(stderr
,"\nERROR: "
1102 "Option -i|--interactive is not allowed together with -D|--daemon\n\n");
1103 poptPrintUsage(pc
, stderr
, 0);
1107 if (log_stdout
&& Fork
) {
1108 d_fprintf(stderr
, "\nERROR: "
1109 "Can't log to stdout (-S) unless daemon is in foreground +(-F) or interactive (-i)\n\n");
1110 poptPrintUsage(pc
, stderr
, 0);
1114 poptFreeContext(pc
);
1116 if (!override_logfile
) {
1117 char *logfile
= NULL
;
1118 if (asprintf(&logfile
,"%s/log.winbindd",
1119 get_dyn_LOGFILEBASE()) > 0) {
1120 lp_set_logfile(logfile
);
1124 setup_logging("winbindd", log_stdout
);
1127 DEBUG(0,("winbindd version %s started.\n", SAMBA_VERSION_STRING
));
1128 DEBUGADD(0,("%s\n", COPYRIGHT_STARTUP_MESSAGE
));
1130 if (!lp_load_initial_only(get_dyn_CONFIGFILE())) {
1131 DEBUG(0, ("error opening config file\n"));
1135 /* Initialise messaging system */
1137 if (winbind_messaging_context() == NULL
) {
1138 DEBUG(0, ("unable to initialize messaging system\n"));
1142 db_tdb2_setup_messaging(winbind_messaging_context(), true);
1144 if (!reload_services_file(NULL
)) {
1145 DEBUG(0, ("error opening config file\n"));
1149 if (!directory_exist(lp_lockdir(), NULL
)) {
1150 mkdir(lp_lockdir(), 0755);
1160 if (!secrets_init()) {
1162 DEBUG(0,("Could not initialize domain trust account secrets. Giving up\n"));
1166 /* Enable netbios namecache */
1170 /* Winbind daemon initialisation */
1172 if ( ! NT_STATUS_IS_OK(idmap_init_cache()) ) {
1173 DEBUG(1, ("Could not init idmap cache!\n"));
1176 /* Unblock all signals we are interested in as they may have been
1177 blocked by the parent process. */
1179 BlockSignals(False
, SIGINT
);
1180 BlockSignals(False
, SIGQUIT
);
1181 BlockSignals(False
, SIGTERM
);
1182 BlockSignals(False
, SIGUSR1
);
1183 BlockSignals(False
, SIGUSR2
);
1184 BlockSignals(False
, SIGHUP
);
1185 BlockSignals(False
, SIGCHLD
);
1187 /* Setup signal handlers */
1189 CatchSignal(SIGINT
, termination_handler
); /* Exit on these sigs */
1190 CatchSignal(SIGQUIT
, termination_handler
);
1191 CatchSignal(SIGTERM
, termination_handler
);
1192 CatchSignal(SIGCHLD
, sigchld_handler
);
1194 CatchSignal(SIGPIPE
, SIG_IGN
); /* Ignore sigpipe */
1196 CatchSignal(SIGUSR2
, sigusr2_handler
); /* Debugging sigs */
1197 CatchSignal(SIGHUP
, sighup_handler
);
1200 become_daemon(Fork
, no_process_group
);
1202 pidfile_create("winbindd");
1206 * If we're interactive we want to set our own process group for
1207 * signal management.
1209 if (interactive
&& !no_process_group
)
1210 setpgid( (pid_t
)0, (pid_t
)0);
1215 if (!reinit_after_fork(winbind_messaging_context(), false)) {
1216 DEBUG(0,("reinit_after_fork() failed\n"));
1221 * Ensure all cache and idmap caches are consistent
1222 * and initialized before we startup.
1224 if (!winbindd_cache_validate_and_initialize()) {
1228 /* get broadcast messages */
1229 claim_connection(NULL
,"",FLAG_MSG_GENERAL
|FLAG_MSG_DBWRAP
);
1231 /* React on 'smbcontrol winbindd reload-config' in the same way
1232 as to SIGHUP signal */
1233 messaging_register(winbind_messaging_context(), NULL
,
1234 MSG_SMB_CONF_UPDATED
, msg_reload_services
);
1235 messaging_register(winbind_messaging_context(), NULL
,
1236 MSG_SHUTDOWN
, msg_shutdown
);
1238 /* Handle online/offline messages. */
1239 messaging_register(winbind_messaging_context(), NULL
,
1240 MSG_WINBIND_OFFLINE
, winbind_msg_offline
);
1241 messaging_register(winbind_messaging_context(), NULL
,
1242 MSG_WINBIND_ONLINE
, winbind_msg_online
);
1243 messaging_register(winbind_messaging_context(), NULL
,
1244 MSG_WINBIND_ONLINESTATUS
, winbind_msg_onlinestatus
);
1246 messaging_register(winbind_messaging_context(), NULL
,
1247 MSG_DUMP_EVENT_LIST
, winbind_msg_dump_event_list
);
1249 messaging_register(winbind_messaging_context(), NULL
,
1250 MSG_WINBIND_VALIDATE_CACHE
,
1251 winbind_msg_validate_cache
);
1253 messaging_register(winbind_messaging_context(), NULL
,
1254 MSG_WINBIND_DUMP_DOMAIN_LIST
,
1255 winbind_msg_dump_domain_list
);
1257 /* Register handler for MSG_DEBUG. */
1258 messaging_register(winbind_messaging_context(), NULL
,
1262 netsamlogon_cache_init(); /* Non-critical */
1264 /* clear the cached list of trusted domains */
1268 if (!init_domain_list()) {
1269 DEBUG(0,("unable to initialize domain list\n"));
1274 init_locator_child();
1276 smb_nscd_flush_user_cache();
1277 smb_nscd_flush_group_cache();
1279 /* Loop waiting for requests */
1283 frame
= talloc_stackframe();