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
10 Copyright (C) James Peach 2007
12 This program is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 2 of the License, or
15 (at your option) any later version.
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with this program; if not, write to the Free Software
24 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
31 #define DBGC_CLASS DBGC_WINBIND
33 BOOL opt_nocache
= False
;
35 extern BOOL override_logfile
;
37 struct event_context
*winbind_event_context(void)
39 static struct event_context
*ctx
;
41 if (!ctx
&& !(ctx
= event_context_init(NULL
))) {
42 smb_panic("Could not init winbind event context\n");
47 /* Reload configuration */
49 static BOOL
reload_services_file(void)
56 pstrcpy(fname
,lp_configfile());
57 if (file_exist(fname
,NULL
) && !strcsequal(fname
,dyn_CONFIGFILE
)) {
58 pstrcpy(dyn_CONFIGFILE
,fname
);
63 ret
= lp_load(dyn_CONFIGFILE
,False
,False
,True
,True
);
72 /**************************************************************************** **
74 **************************************************************************** */
76 static void fault_quit(void)
81 static void winbindd_status(void)
83 struct winbindd_cli_state
*tmp
;
85 DEBUG(0, ("winbindd status:\n"));
87 /* Print client state information */
89 DEBUG(0, ("\t%d clients currently active\n", winbindd_num_clients()));
91 if (DEBUGLEVEL
>= 2 && winbindd_num_clients()) {
92 DEBUG(2, ("\tclient list:\n"));
93 for(tmp
= winbindd_client_list(); tmp
; tmp
= tmp
->next
) {
94 DEBUGADD(2, ("\t\tpid %lu, sock %d\n",
95 (unsigned long)tmp
->pid
, tmp
->sock
));
100 /* Print winbindd status to log file */
102 static void print_winbindd_status(void)
107 /* Flush client cache */
109 static void flush_caches(void)
111 /* We need to invalidate cached user list entries on a SIGHUP
112 otherwise cached access denied errors due to restrict anonymous
113 hang around until the sequence number changes. */
115 wcache_invalidate_cache();
118 /* Handle the signal by unlinking socket and exiting */
120 static void terminate(void)
123 winbindd_release_sockets();
126 trustdom_cache_shutdown();
130 TALLOC_CTX
*mem_ctx
= talloc_init("end_description");
131 char *description
= talloc_describe_all(mem_ctx
);
133 DEBUG(3, ("tallocs left:\n%s\n", description
));
134 talloc_destroy(mem_ctx
);
141 static BOOL do_sigterm
;
143 static void termination_handler(int signum
)
146 sys_select_signal(signum
);
149 static BOOL do_sigusr2
;
151 static void sigusr2_handler(int signum
)
154 sys_select_signal(SIGUSR2
);
157 static BOOL do_sighup
;
159 static void sighup_handler(int signum
)
162 sys_select_signal(SIGHUP
);
165 static BOOL do_sigchld
;
167 static void sigchld_handler(int signum
)
170 sys_select_signal(SIGCHLD
);
173 /* React on 'smbcontrol winbindd reload-config' in the same way as on SIGHUP*/
174 static void msg_reload_services(int msg_type
, struct process_id src
,
175 void *buf
, size_t len
, void *private_data
)
177 /* Flush various caches */
179 reload_services_file();
182 /* React on 'smbcontrol winbindd shutdown' in the same way as on SIGTERM*/
183 static void msg_shutdown(int msg_type
, struct process_id src
,
184 void *buf
, size_t len
, void *private_data
)
189 static struct winbindd_dispatch_table
{
190 enum winbindd_cmd cmd
;
191 void (*fn
)(struct winbindd_cli_state
*state
);
192 const char *winbindd_cmd_name
;
193 } dispatch_table
[] = {
197 { WINBINDD_GETPWNAM
, winbindd_getpwnam
, "GETPWNAM" },
198 { WINBINDD_GETPWUID
, winbindd_getpwuid
, "GETPWUID" },
200 { WINBINDD_SETPWENT
, winbindd_setpwent
, "SETPWENT" },
201 { WINBINDD_ENDPWENT
, winbindd_endpwent
, "ENDPWENT" },
202 { WINBINDD_GETPWENT
, winbindd_getpwent
, "GETPWENT" },
204 { WINBINDD_GETGROUPS
, winbindd_getgroups
, "GETGROUPS" },
205 { WINBINDD_GETUSERSIDS
, winbindd_getusersids
, "GETUSERSIDS" },
206 { WINBINDD_GETUSERDOMGROUPS
, winbindd_getuserdomgroups
,
207 "GETUSERDOMGROUPS" },
209 /* Group functions */
211 { WINBINDD_GETGRNAM
, winbindd_getgrnam
, "GETGRNAM" },
212 { WINBINDD_GETGRGID
, winbindd_getgrgid
, "GETGRGID" },
213 { WINBINDD_SETGRENT
, winbindd_setgrent
, "SETGRENT" },
214 { WINBINDD_ENDGRENT
, winbindd_endgrent
, "ENDGRENT" },
215 { WINBINDD_GETGRENT
, winbindd_getgrent
, "GETGRENT" },
216 { WINBINDD_GETGRLST
, winbindd_getgrent
, "GETGRLST" },
218 /* PAM auth functions */
220 { WINBINDD_PAM_AUTH
, winbindd_pam_auth
, "PAM_AUTH" },
221 { WINBINDD_PAM_AUTH_CRAP
, winbindd_pam_auth_crap
, "AUTH_CRAP" },
222 { WINBINDD_PAM_CHAUTHTOK
, winbindd_pam_chauthtok
, "CHAUTHTOK" },
223 { WINBINDD_PAM_LOGOFF
, winbindd_pam_logoff
, "PAM_LOGOFF" },
224 { WINBINDD_PAM_CHNG_PSWD_AUTH_CRAP
, winbindd_pam_chng_pswd_auth_crap
, "CHNG_PSWD_AUTH_CRAP" },
226 /* Enumeration functions */
228 { WINBINDD_LIST_USERS
, winbindd_list_users
, "LIST_USERS" },
229 { WINBINDD_LIST_GROUPS
, winbindd_list_groups
, "LIST_GROUPS" },
230 { WINBINDD_LIST_TRUSTDOM
, winbindd_list_trusted_domains
,
232 { WINBINDD_SHOW_SEQUENCE
, winbindd_show_sequence
, "SHOW_SEQUENCE" },
234 /* SID related functions */
236 { WINBINDD_LOOKUPSID
, winbindd_lookupsid
, "LOOKUPSID" },
237 { WINBINDD_LOOKUPNAME
, winbindd_lookupname
, "LOOKUPNAME" },
238 { WINBINDD_LOOKUPRIDS
, winbindd_lookuprids
, "LOOKUPRIDS" },
240 /* Lookup related functions */
242 { WINBINDD_SID_TO_UID
, winbindd_sid_to_uid
, "SID_TO_UID" },
243 { WINBINDD_SID_TO_GID
, winbindd_sid_to_gid
, "SID_TO_GID" },
244 { WINBINDD_UID_TO_SID
, winbindd_uid_to_sid
, "UID_TO_SID" },
245 { WINBINDD_GID_TO_SID
, winbindd_gid_to_sid
, "GID_TO_SID" },
246 #if 0 /* DISABLED until we fix the interface in Samba 3.0.26 --jerry */
247 { WINBINDD_SIDS_TO_XIDS
, winbindd_sids_to_unixids
, "SIDS_TO_XIDS" },
248 #endif /* end DISABLED */
249 { WINBINDD_ALLOCATE_UID
, winbindd_allocate_uid
, "ALLOCATE_UID" },
250 { WINBINDD_ALLOCATE_GID
, winbindd_allocate_gid
, "ALLOCATE_GID" },
251 { WINBINDD_SET_MAPPING
, winbindd_set_mapping
, "SET_MAPPING" },
252 { WINBINDD_SET_HWM
, winbindd_set_hwm
, "SET_HWMS" },
256 { WINBINDD_DUMP_MAPS
, winbindd_dump_maps
, "DUMP_MAPS" },
258 { WINBINDD_CHECK_MACHACC
, winbindd_check_machine_acct
, "CHECK_MACHACC" },
259 { WINBINDD_PING
, winbindd_ping
, "PING" },
260 { WINBINDD_INFO
, winbindd_info
, "INFO" },
261 { WINBINDD_INTERFACE_VERSION
, winbindd_interface_version
,
262 "INTERFACE_VERSION" },
263 { WINBINDD_DOMAIN_NAME
, winbindd_domain_name
, "DOMAIN_NAME" },
264 { WINBINDD_DOMAIN_INFO
, winbindd_domain_info
, "DOMAIN_INFO" },
265 { WINBINDD_NETBIOS_NAME
, winbindd_netbios_name
, "NETBIOS_NAME" },
266 { WINBINDD_PRIV_PIPE_DIR
, winbindd_priv_pipe_dir
,
267 "WINBINDD_PRIV_PIPE_DIR" },
268 { WINBINDD_GETDCNAME
, winbindd_getdcname
, "GETDCNAME" },
270 /* Credential cache access */
271 { WINBINDD_CCACHE_NTLMAUTH
, winbindd_ccache_ntlm_auth
, "NTLMAUTH" },
275 { WINBINDD_WINS_BYNAME
, winbindd_wins_byname
, "WINS_BYNAME" },
276 { WINBINDD_WINS_BYIP
, winbindd_wins_byip
, "WINS_BYIP" },
280 { WINBINDD_NUM_CMDS
, NULL
, "NONE" }
283 static void process_request(struct winbindd_cli_state
*state
)
285 struct winbindd_dispatch_table
*table
= dispatch_table
;
287 /* Free response data - we may be interrupted and receive another
288 command before being able to send this data off. */
290 SAFE_FREE(state
->response
.extra_data
.data
);
292 ZERO_STRUCT(state
->response
);
294 state
->response
.result
= WINBINDD_PENDING
;
295 state
->response
.length
= sizeof(struct winbindd_response
);
297 state
->mem_ctx
= talloc_init("winbind request");
298 if (state
->mem_ctx
== NULL
)
301 /* Process command */
303 for (table
= dispatch_table
; table
->fn
; table
++) {
304 if (state
->request
.cmd
== table
->cmd
) {
305 DEBUG(10,("process_request: request fn %s\n",
306 table
->winbindd_cmd_name
));
313 DEBUG(10,("process_request: unknown request fn number %d\n",
314 (int)state
->request
.cmd
));
315 request_error(state
);
320 * A list of file descriptors being monitored by select in the main processing
321 * loop. fd_event->handler is called whenever the socket is readable/writable.
324 static struct fd_event
*fd_events
= NULL
;
326 void add_fd_event(struct fd_event
*ev
)
328 struct fd_event
*match
;
330 /* only add unique fd_event structs */
332 for (match
=fd_events
; match
; match
=match
->next
) {
334 SMB_ASSERT( match
!= ev
);
341 DLIST_ADD(fd_events
, ev
);
344 void remove_fd_event(struct fd_event
*ev
)
346 DLIST_REMOVE(fd_events
, ev
);
350 * Handler for fd_events to complete a read/write request, set up by
351 * setup_async_read/setup_async_write.
354 static void rw_callback(struct fd_event
*event
, int flags
)
359 todo
= event
->length
- event
->done
;
361 if (event
->flags
& EVENT_FD_WRITE
) {
362 SMB_ASSERT(flags
== EVENT_FD_WRITE
);
363 done
= sys_write(event
->fd
,
364 &((char *)event
->data
)[event
->done
],
369 event
->finished(event
->private_data
, False
);
374 if (event
->flags
& EVENT_FD_READ
) {
375 SMB_ASSERT(flags
== EVENT_FD_READ
);
376 done
= sys_read(event
->fd
, &((char *)event
->data
)[event
->done
],
381 event
->finished(event
->private_data
, False
);
388 if (event
->done
== event
->length
) {
390 event
->finished(event
->private_data
, True
);
395 * Request an async read/write on a fd_event structure. (*finished) is called
396 * when the request is completed or an error had occurred.
399 void setup_async_read(struct fd_event
*event
, void *data
, size_t length
,
400 void (*finished
)(void *private_data
, BOOL success
),
403 SMB_ASSERT(event
->flags
== 0);
405 event
->length
= length
;
407 event
->handler
= rw_callback
;
408 event
->finished
= finished
;
409 event
->private_data
= private_data
;
410 event
->flags
= EVENT_FD_READ
;
413 void setup_async_write(struct fd_event
*event
, void *data
, size_t length
,
414 void (*finished
)(void *private_data
, BOOL success
),
417 SMB_ASSERT(event
->flags
== 0);
419 event
->length
= length
;
421 event
->handler
= rw_callback
;
422 event
->finished
= finished
;
423 event
->private_data
= private_data
;
424 event
->flags
= EVENT_FD_WRITE
;
428 * This is the main event loop of winbind requests. It goes through a
429 * state-machine of 3 read/write requests, 4 if you have extra data to send.
431 * An idle winbind client has a read request of 4 bytes outstanding,
432 * finalizing function is request_len_recv, checking the length. request_recv
433 * then processes the packet. The processing function then at some point has
434 * to call request_finished which schedules sending the response.
437 static void request_len_recv(void *private_data
, BOOL success
);
438 static void request_recv(void *private_data
, BOOL success
);
439 static void request_main_recv(void *private_data
, BOOL success
);
440 static void request_finished(struct winbindd_cli_state
*state
);
441 void request_finished_cont(void *private_data
, BOOL success
);
442 static void response_main_sent(void *private_data
, BOOL success
);
443 static void response_extra_sent(void *private_data
, BOOL success
);
445 static void response_extra_sent(void *private_data
, BOOL success
)
447 struct winbindd_cli_state
*state
=
448 talloc_get_type_abort(private_data
, struct winbindd_cli_state
);
450 if (state
->mem_ctx
!= NULL
) {
451 talloc_destroy(state
->mem_ctx
);
452 state
->mem_ctx
= NULL
;
456 state
->finished
= True
;
460 SAFE_FREE(state
->request
.extra_data
.data
);
461 SAFE_FREE(state
->response
.extra_data
.data
);
463 setup_async_read(&state
->fd_event
, &state
->request
, sizeof(uint32
),
464 request_len_recv
, state
);
467 static void response_main_sent(void *private_data
, BOOL success
)
469 struct winbindd_cli_state
*state
=
470 talloc_get_type_abort(private_data
, struct winbindd_cli_state
);
473 state
->finished
= True
;
477 if (state
->response
.length
== sizeof(state
->response
)) {
478 if (state
->mem_ctx
!= NULL
) {
479 talloc_destroy(state
->mem_ctx
);
480 state
->mem_ctx
= NULL
;
483 setup_async_read(&state
->fd_event
, &state
->request
,
484 sizeof(uint32
), request_len_recv
, state
);
488 setup_async_write(&state
->fd_event
, state
->response
.extra_data
.data
,
489 state
->response
.length
- sizeof(state
->response
),
490 response_extra_sent
, state
);
493 static void request_finished(struct winbindd_cli_state
*state
)
495 setup_async_write(&state
->fd_event
, &state
->response
,
496 sizeof(state
->response
), response_main_sent
, state
);
499 void request_error(struct winbindd_cli_state
*state
)
501 SMB_ASSERT(state
->response
.result
== WINBINDD_PENDING
);
502 state
->response
.result
= WINBINDD_ERROR
;
503 request_finished(state
);
506 void request_ok(struct winbindd_cli_state
*state
)
508 SMB_ASSERT(state
->response
.result
== WINBINDD_PENDING
);
509 state
->response
.result
= WINBINDD_OK
;
510 request_finished(state
);
513 void request_finished_cont(void *private_data
, BOOL success
)
515 struct winbindd_cli_state
*state
=
516 talloc_get_type_abort(private_data
, struct winbindd_cli_state
);
521 request_error(state
);
524 static void request_len_recv(void *private_data
, BOOL success
)
526 struct winbindd_cli_state
*state
=
527 talloc_get_type_abort(private_data
, struct winbindd_cli_state
);
530 state
->finished
= True
;
534 if (*(uint32
*)(&state
->request
) != sizeof(state
->request
)) {
535 DEBUG(0,("request_len_recv: Invalid request size received: %d\n",
536 *(uint32
*)(&state
->request
)));
537 state
->finished
= True
;
541 setup_async_read(&state
->fd_event
, (uint32
*)(&state
->request
)+1,
542 sizeof(state
->request
) - sizeof(uint32
),
543 request_main_recv
, state
);
546 static void request_main_recv(void *private_data
, BOOL success
)
548 struct winbindd_cli_state
*state
=
549 talloc_get_type_abort(private_data
, struct winbindd_cli_state
);
552 state
->finished
= True
;
556 if (state
->request
.extra_len
== 0) {
557 state
->request
.extra_data
.data
= NULL
;
558 request_recv(state
, True
);
562 if ((!state
->privileged
) &&
563 (state
->request
.extra_len
> WINBINDD_MAX_EXTRA_DATA
)) {
564 DEBUG(3, ("Got request with %d bytes extra data on "
565 "unprivileged socket\n", (int)state
->request
.extra_len
));
566 state
->request
.extra_data
.data
= NULL
;
567 state
->finished
= True
;
571 state
->request
.extra_data
.data
=
572 SMB_MALLOC_ARRAY(char, state
->request
.extra_len
+ 1);
574 if (state
->request
.extra_data
.data
== NULL
) {
575 DEBUG(0, ("malloc failed\n"));
576 state
->finished
= True
;
580 /* Ensure null termination */
581 state
->request
.extra_data
.data
[state
->request
.extra_len
] = '\0';
583 setup_async_read(&state
->fd_event
, state
->request
.extra_data
.data
,
584 state
->request
.extra_len
, request_recv
, state
);
587 static void request_recv(void *private_data
, BOOL success
)
589 struct winbindd_cli_state
*state
=
590 talloc_get_type_abort(private_data
, struct winbindd_cli_state
);
593 state
->finished
= True
;
597 process_request(state
);
600 /* Process a new connection by adding it to the client connection list */
602 static void new_connection(int listen_sock
, BOOL privileged
)
604 struct sockaddr_un sunaddr
;
605 struct winbindd_cli_state
*state
;
609 /* Accept connection */
611 len
= sizeof(sunaddr
);
614 sock
= accept(listen_sock
, (struct sockaddr
*)&sunaddr
, &len
);
615 } while (sock
== -1 && errno
== EINTR
);
620 DEBUG(6,("accepted socket %d\n", sock
));
622 /* Create new connection structure */
624 if ((state
= TALLOC_ZERO_P(NULL
, struct winbindd_cli_state
)) == NULL
) {
631 state
->last_access
= time(NULL
);
633 state
->privileged
= privileged
;
635 state
->fd_event
.fd
= state
->sock
;
636 state
->fd_event
.flags
= 0;
637 add_fd_event(&state
->fd_event
);
639 setup_async_read(&state
->fd_event
, &state
->request
, sizeof(uint32
),
640 request_len_recv
, state
);
642 /* Add to connection list */
644 winbindd_add_client(state
);
647 /* Remove a client connection from client connection list */
649 static void remove_client(struct winbindd_cli_state
*state
)
651 /* It's a dead client - hold a funeral */
661 /* Free any getent state */
663 free_getent_state(state
->getpwent_state
);
664 free_getent_state(state
->getgrent_state
);
666 /* We may have some extra data that was not freed if the client was
667 killed unexpectedly */
669 SAFE_FREE(state
->response
.extra_data
.data
);
671 if (state
->mem_ctx
!= NULL
) {
672 talloc_destroy(state
->mem_ctx
);
673 state
->mem_ctx
= NULL
;
676 remove_fd_event(&state
->fd_event
);
678 /* Remove from list and free */
680 winbindd_remove_client(state
);
684 /* Shutdown client connection which has been idle for the longest time */
686 static BOOL
remove_idle_client(void)
688 struct winbindd_cli_state
*state
, *remove_state
= NULL
;
689 time_t last_access
= 0;
692 for (state
= winbindd_client_list(); state
; state
= state
->next
) {
693 if (state
->response
.result
!= WINBINDD_PENDING
&&
694 !state
->getpwent_state
&& !state
->getgrent_state
) {
696 if (!last_access
|| state
->last_access
< last_access
) {
697 last_access
= state
->last_access
;
698 remove_state
= state
;
704 DEBUG(5,("Found %d idle client connections, shutting down sock %d, pid %u\n",
705 nidle
, remove_state
->sock
, (unsigned int)remove_state
->pid
));
706 remove_client(remove_state
);
713 /* Process incoming clients on listen_sock. We use a tricky non-blocking,
714 non-forking, non-threaded model which allows us to handle many
715 simultaneous connections while remaining impervious to many denial of
718 static int process_loop(int listen_sock
, int listen_priv_sock
)
720 struct winbindd_cli_state
*state
;
724 struct timeval timeout
, ev_timeout
;
726 /* We'll be doing this a lot */
728 /* Handle messages */
732 run_events(winbind_event_context(), 0, NULL
, NULL
);
734 /* refresh the trusted domain cache */
736 rescan_trusted_domains();
738 /* Free up temporary memory */
741 main_loop_TALLOC_FREE();
743 /* Initialise fd lists for select() */
745 maxfd
= MAX(listen_sock
, listen_priv_sock
);
749 FD_SET(listen_sock
, &r_fds
);
750 FD_SET(listen_priv_sock
, &r_fds
);
752 timeout
.tv_sec
= WINBINDD_ESTABLISH_LOOP
;
755 /* Check for any event timeouts. */
756 if (get_timed_events_timeout(winbind_event_context(), &ev_timeout
)) {
757 timeout
= timeval_min(&timeout
, &ev_timeout
);
760 /* Set up client readers and writers */
762 state
= winbindd_client_list();
766 struct winbindd_cli_state
*next
= state
->next
;
768 /* Dispose of client connection if it is marked as
772 remove_client(state
);
777 for (ev
= fd_events
; ev
; ev
= ev
->next
) {
778 if (ev
->flags
& EVENT_FD_READ
) {
779 FD_SET(ev
->fd
, &r_fds
);
780 maxfd
= MAX(ev
->fd
, maxfd
);
782 if (ev
->flags
& EVENT_FD_WRITE
) {
783 FD_SET(ev
->fd
, &w_fds
);
784 maxfd
= MAX(ev
->fd
, maxfd
);
790 selret
= sys_select(maxfd
+ 1, &r_fds
, &w_fds
, NULL
, &timeout
);
797 if (errno
== EINTR
) {
801 /* Select error, something is badly wrong */
811 struct fd_event
*next
= ev
->next
;
813 if (FD_ISSET(ev
->fd
, &r_fds
))
814 flags
|= EVENT_FD_READ
;
815 if (FD_ISSET(ev
->fd
, &w_fds
))
816 flags
|= EVENT_FD_WRITE
;
818 ev
->handler(ev
, flags
);
822 if (FD_ISSET(listen_sock
, &r_fds
)) {
823 while (winbindd_num_clients() >
824 WINBINDD_MAX_SIMULTANEOUS_CLIENTS
- 1) {
825 DEBUG(5,("winbindd: Exceeding %d client "
826 "connections, removing idle "
828 WINBINDD_MAX_SIMULTANEOUS_CLIENTS
));
829 if (!remove_idle_client()) {
830 DEBUG(0,("winbindd: Exceeding %d "
831 "client connections, no idle "
832 "connection found\n",
833 WINBINDD_MAX_SIMULTANEOUS_CLIENTS
));
837 /* new, non-privileged connection */
838 new_connection(listen_sock
, False
);
841 if (FD_ISSET(listen_priv_sock
, &r_fds
)) {
842 while (winbindd_num_clients() >
843 WINBINDD_MAX_SIMULTANEOUS_CLIENTS
- 1) {
844 DEBUG(5,("winbindd: Exceeding %d client "
845 "connections, removing idle "
847 WINBINDD_MAX_SIMULTANEOUS_CLIENTS
));
848 if (!remove_idle_client()) {
849 DEBUG(0,("winbindd: Exceeding %d "
850 "client connections, no idle "
851 "connection found\n",
852 WINBINDD_MAX_SIMULTANEOUS_CLIENTS
));
856 /* new, privileged connection */
857 new_connection(listen_priv_sock
, True
);
863 winbindd_check_cache_size(time(NULL
));
866 /* Check signal handling things */
873 DEBUG(3, ("got SIGHUP\n"));
875 msg_reload_services(MSG_SMB_CONF_UPDATED
, pid_to_procid(0), NULL
, 0, NULL
);
880 print_winbindd_status();
889 while ((pid
= sys_waitpid(-1, NULL
, WNOHANG
)) > 0) {
890 winbind_child_died(pid
);
895 return winbindd_num_clients();
898 static void winbindd_process_loop(enum smb_server_mode server_mode
)
900 int idle_timeout_sec
;
901 struct timeval starttime
;
902 int listen_public
, listen_priv
;
905 if (!winbindd_init_sockets(&listen_public
, &listen_priv
,
906 &idle_timeout_sec
)) {
910 starttime
= timeval_current();
912 if (listen_public
== -1 || listen_priv
== -1) {
913 DEBUG(0, ("failed to open winbindd pipes: %s\n",
914 errno
? strerror(errno
) : "unknown error"));
919 int clients
= process_loop(listen_public
, listen_priv
);
921 /* Don't bother figuring out the idle time if we won't be
924 if (idle_timeout_sec
< 0) {
928 if (clients
== 0 && server_mode
== SERVER_MODE_FOREGROUND
) {
931 now
= timeval_current();
932 if (timeval_elapsed2(&starttime
, &now
) >
933 (double)idle_timeout_sec
) {
934 DEBUG(0, ("idle for %d secs, exitting\n",
939 starttime
= timeval_current();
946 int main(int argc
, char **argv
, char **envp
)
949 BOOL log_stdout
= False
;
950 BOOL no_process_group
= False
;
952 enum smb_server_mode server_mode
= SERVER_MODE_DAEMON
;
954 struct poptOption long_options
[] = {
956 { "stdout", 'S', POPT_ARG_VAL
, &log_stdout
, True
, "Log to stdout" },
957 { "foreground", 'F', POPT_ARG_VAL
, &server_mode
, SERVER_MODE_FOREGROUND
, "Daemon in foreground mode" },
958 { "no-process-group", 0, POPT_ARG_VAL
, &no_process_group
, True
, "Don't create a new process group" },
959 { "interactive", 'i', POPT_ARG_VAL
, &server_mode
, SERVER_MODE_INTERACTIVE
, "Interactive mode" },
960 { "no-caching", 'n', POPT_ARG_VAL
, &opt_nocache
, True
, "Disable caching" },
967 /* glibc (?) likes to print "User defined signal 1" and exit if a
968 SIGUSR[12] is received before a handler is installed */
970 CatchSignal(SIGUSR1
, SIG_IGN
);
971 CatchSignal(SIGUSR2
, SIG_IGN
);
973 fault_setup((void (*)(void *))fault_quit
);
974 dump_core_setup("winbindd");
978 /* Initialise for running in non-root mode */
982 set_remote_machine_name("winbindd", False
);
984 /* Set environment variable so we don't recursively call ourselves.
985 This may also be useful interactively. */
987 if ( !winbind_off() ) {
988 DEBUG(0,("Failed to disable recusive winbindd calls. Exiting.\n"));
992 /* Initialise samba/rpc client stuff */
994 pc
= poptGetContext("winbindd", argc
, (const char **)argv
, long_options
,
995 POPT_CONTEXT_KEEP_FIRST
);
997 while ((opt
= poptGetNextOpt(pc
)) != -1) {}
999 if (server_mode
== SERVER_MODE_INTERACTIVE
) {
1001 if (DEBUGLEVEL
>= 9) {
1002 talloc_enable_leak_report();
1006 if (log_stdout
&& server_mode
== SERVER_MODE_DAEMON
) {
1007 printf("Can't log to stdout (-S) unless daemon is in foreground (-F) or interactive (-i)\n");
1008 poptPrintUsage(pc
, stderr
, 0);
1012 if (!override_logfile
) {
1013 pstr_sprintf(logfile
, "%s/log.winbindd", dyn_LOGFILEBASE
);
1014 lp_set_logfile(logfile
);
1016 setup_logging("winbindd", log_stdout
);
1019 DEBUG(1, ("winbindd version %s started.\n%s\n",
1020 SAMBA_VERSION_STRING
,
1021 COPYRIGHT_STARTUP_MESSAGE
) );
1023 if (!reload_services_file()) {
1024 DEBUG(0, ("error opening config file\n"));
1028 if (!directory_exist(lp_lockdir(), NULL
)) {
1029 mkdir(lp_lockdir(), 0755);
1039 if (!secrets_init()) {
1041 DEBUG(0,("Could not initialize domain trust account secrets. Giving up\n"));
1045 /* Enable netbios namecache */
1049 /* Winbind daemon initialisation */
1051 if ( ! NT_STATUS_IS_OK(idmap_init_cache()) ) {
1052 DEBUG(1, ("Could not init idmap cache!\n"));
1055 /* Unblock all signals we are interested in as they may have been
1056 blocked by the parent process. */
1058 BlockSignals(False
, SIGINT
);
1059 BlockSignals(False
, SIGQUIT
);
1060 BlockSignals(False
, SIGTERM
);
1061 BlockSignals(False
, SIGUSR1
);
1062 BlockSignals(False
, SIGUSR2
);
1063 BlockSignals(False
, SIGHUP
);
1064 BlockSignals(False
, SIGCHLD
);
1066 /* Setup signal handlers */
1068 CatchSignal(SIGINT
, termination_handler
); /* Exit on these sigs */
1069 CatchSignal(SIGQUIT
, termination_handler
);
1070 CatchSignal(SIGTERM
, termination_handler
);
1071 CatchSignal(SIGCHLD
, sigchld_handler
);
1073 CatchSignal(SIGPIPE
, SIG_IGN
); /* Ignore sigpipe */
1075 CatchSignal(SIGUSR2
, sigusr2_handler
); /* Debugging sigs */
1076 CatchSignal(SIGHUP
, sighup_handler
);
1078 if (server_mode
== SERVER_MODE_DAEMON
) {
1079 DEBUG( 3, ( "Becoming a daemon.\n" ) );
1080 become_daemon(True
, no_process_group
);
1081 } else if (server_mode
== SERVER_MODE_FOREGROUND
) {
1082 become_daemon(False
, no_process_group
);
1085 pidfile_create("winbindd");
1087 #if 0 /* not finished yet */
1088 /* Ensure all cache and idmap caches are consistent
1089 before we startup. */
1091 if (winbindd_validate_cache()) {
1092 /* We have a bad cache, but luckily we
1093 just deleted it. Restart ourselves */
1095 /* Ensure we have no open low fd's. */
1096 for (i
= 3; i
< 100; i
++) {
1099 return execve(argv
[0], argv
, envp
);
1105 * If we're interactive we want to set our own process group for
1106 * signal management.
1108 if (server_mode
== SERVER_MODE_INTERACTIVE
&& !no_process_group
) {
1109 setpgid( (pid_t
)0, (pid_t
)0);
1115 /* Initialise messaging system */
1117 if (!message_init()) {
1118 DEBUG(0, ("unable to initialize messaging system\n"));
1122 /* Initialize cache (ensure version is correct). */
1123 if (!initialize_winbindd_cache()) {
1127 /* React on 'smbcontrol winbindd reload-config' in the same way
1128 as to SIGHUP signal */
1129 message_register(MSG_SMB_CONF_UPDATED
, msg_reload_services
, NULL
);
1130 message_register(MSG_SHUTDOWN
, msg_shutdown
, NULL
);
1132 /* Handle online/offline messages. */
1133 message_register(MSG_WINBIND_OFFLINE
, winbind_msg_offline
, NULL
);
1134 message_register(MSG_WINBIND_ONLINE
, winbind_msg_online
, NULL
);
1135 message_register(MSG_WINBIND_ONLINESTATUS
, winbind_msg_onlinestatus
,
1138 poptFreeContext(pc
);
1140 netsamlogon_cache_init(); /* Non-critical */
1142 /* clear the cached list of trusted domains */
1146 if (!init_domain_list()) {
1147 DEBUG(0,("unable to initalize domain list\n"));
1153 smb_nscd_flush_user_cache();
1154 smb_nscd_flush_group_cache();
1156 /* Loop waiting for requests */
1157 winbindd_process_loop(server_mode
);