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 2 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, write to the Free Software
23 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
30 #define DBGC_CLASS DBGC_WINBIND
32 BOOL opt_nocache
= False
;
33 static BOOL interactive
= 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)
124 /* Remove socket file */
125 pstr_sprintf(path
, "%s/%s",
126 WINBINDD_SOCKET_DIR
, WINBINDD_SOCKET_NAME
);
131 trustdom_cache_shutdown();
135 TALLOC_CTX
*mem_ctx
= talloc_init("end_description");
136 char *description
= talloc_describe_all(mem_ctx
);
138 DEBUG(3, ("tallocs left:\n%s\n", description
));
139 talloc_destroy(mem_ctx
);
146 static BOOL do_sigterm
;
148 static void termination_handler(int signum
)
151 sys_select_signal(signum
);
154 static BOOL do_sigusr2
;
156 static void sigusr2_handler(int signum
)
159 sys_select_signal(SIGUSR2
);
162 static BOOL do_sighup
;
164 static void sighup_handler(int signum
)
167 sys_select_signal(SIGHUP
);
170 static BOOL do_sigchld
;
172 static void sigchld_handler(int signum
)
175 sys_select_signal(SIGCHLD
);
178 /* React on 'smbcontrol winbindd reload-config' in the same way as on SIGHUP*/
179 static void msg_reload_services(int msg_type
, struct process_id src
,
180 void *buf
, size_t len
, void *private_data
)
182 /* Flush various caches */
184 reload_services_file();
187 /* React on 'smbcontrol winbindd shutdown' in the same way as on SIGTERM*/
188 static void msg_shutdown(int msg_type
, struct process_id src
,
189 void *buf
, size_t len
, void *private_data
)
194 static struct winbindd_dispatch_table
{
195 enum winbindd_cmd cmd
;
196 void (*fn
)(struct winbindd_cli_state
*state
);
197 const char *winbindd_cmd_name
;
198 } dispatch_table
[] = {
202 { WINBINDD_GETPWNAM
, winbindd_getpwnam
, "GETPWNAM" },
203 { WINBINDD_GETPWUID
, winbindd_getpwuid
, "GETPWUID" },
205 { WINBINDD_SETPWENT
, winbindd_setpwent
, "SETPWENT" },
206 { WINBINDD_ENDPWENT
, winbindd_endpwent
, "ENDPWENT" },
207 { WINBINDD_GETPWENT
, winbindd_getpwent
, "GETPWENT" },
209 { WINBINDD_GETGROUPS
, winbindd_getgroups
, "GETGROUPS" },
210 { WINBINDD_GETUSERSIDS
, winbindd_getusersids
, "GETUSERSIDS" },
211 { WINBINDD_GETUSERDOMGROUPS
, winbindd_getuserdomgroups
,
212 "GETUSERDOMGROUPS" },
214 /* Group functions */
216 { WINBINDD_GETGRNAM
, winbindd_getgrnam
, "GETGRNAM" },
217 { WINBINDD_GETGRGID
, winbindd_getgrgid
, "GETGRGID" },
218 { WINBINDD_SETGRENT
, winbindd_setgrent
, "SETGRENT" },
219 { WINBINDD_ENDGRENT
, winbindd_endgrent
, "ENDGRENT" },
220 { WINBINDD_GETGRENT
, winbindd_getgrent
, "GETGRENT" },
221 { WINBINDD_GETGRLST
, winbindd_getgrent
, "GETGRLST" },
223 /* PAM auth functions */
225 { WINBINDD_PAM_AUTH
, winbindd_pam_auth
, "PAM_AUTH" },
226 { WINBINDD_PAM_AUTH_CRAP
, winbindd_pam_auth_crap
, "AUTH_CRAP" },
227 { WINBINDD_PAM_CHAUTHTOK
, winbindd_pam_chauthtok
, "CHAUTHTOK" },
228 { WINBINDD_PAM_LOGOFF
, winbindd_pam_logoff
, "PAM_LOGOFF" },
229 { WINBINDD_PAM_CHNG_PSWD_AUTH_CRAP
, winbindd_pam_chng_pswd_auth_crap
, "CHNG_PSWD_AUTH_CRAP" },
231 /* Enumeration functions */
233 { WINBINDD_LIST_USERS
, winbindd_list_users
, "LIST_USERS" },
234 { WINBINDD_LIST_GROUPS
, winbindd_list_groups
, "LIST_GROUPS" },
235 { WINBINDD_LIST_TRUSTDOM
, winbindd_list_trusted_domains
,
237 { WINBINDD_SHOW_SEQUENCE
, winbindd_show_sequence
, "SHOW_SEQUENCE" },
239 /* SID related functions */
241 { WINBINDD_LOOKUPSID
, winbindd_lookupsid
, "LOOKUPSID" },
242 { WINBINDD_LOOKUPNAME
, winbindd_lookupname
, "LOOKUPNAME" },
243 { WINBINDD_LOOKUPRIDS
, winbindd_lookuprids
, "LOOKUPRIDS" },
245 /* Lookup related functions */
247 { WINBINDD_SID_TO_UID
, winbindd_sid_to_uid
, "SID_TO_UID" },
248 { WINBINDD_SID_TO_GID
, winbindd_sid_to_gid
, "SID_TO_GID" },
249 { WINBINDD_UID_TO_SID
, winbindd_uid_to_sid
, "UID_TO_SID" },
250 { WINBINDD_GID_TO_SID
, winbindd_gid_to_sid
, "GID_TO_SID" },
251 #if 0 /* DISABLED until we fix the interface in Samba 3.0.26 --jerry */
252 { WINBINDD_SIDS_TO_XIDS
, winbindd_sids_to_unixids
, "SIDS_TO_XIDS" },
253 #endif /* end DISABLED */
254 { WINBINDD_ALLOCATE_UID
, winbindd_allocate_uid
, "ALLOCATE_UID" },
255 { WINBINDD_ALLOCATE_GID
, winbindd_allocate_gid
, "ALLOCATE_GID" },
256 { WINBINDD_SET_MAPPING
, winbindd_set_mapping
, "SET_MAPPING" },
257 { WINBINDD_SET_HWM
, winbindd_set_hwm
, "SET_HWMS" },
261 { WINBINDD_DUMP_MAPS
, winbindd_dump_maps
, "DUMP_MAPS" },
263 { WINBINDD_CHECK_MACHACC
, winbindd_check_machine_acct
, "CHECK_MACHACC" },
264 { WINBINDD_PING
, winbindd_ping
, "PING" },
265 { WINBINDD_INFO
, winbindd_info
, "INFO" },
266 { WINBINDD_INTERFACE_VERSION
, winbindd_interface_version
,
267 "INTERFACE_VERSION" },
268 { WINBINDD_DOMAIN_NAME
, winbindd_domain_name
, "DOMAIN_NAME" },
269 { WINBINDD_DOMAIN_INFO
, winbindd_domain_info
, "DOMAIN_INFO" },
270 { WINBINDD_NETBIOS_NAME
, winbindd_netbios_name
, "NETBIOS_NAME" },
271 { WINBINDD_PRIV_PIPE_DIR
, winbindd_priv_pipe_dir
,
272 "WINBINDD_PRIV_PIPE_DIR" },
273 { WINBINDD_GETDCNAME
, winbindd_getdcname
, "GETDCNAME" },
275 /* Credential cache access */
276 { WINBINDD_CCACHE_NTLMAUTH
, winbindd_ccache_ntlm_auth
, "NTLMAUTH" },
280 { WINBINDD_WINS_BYNAME
, winbindd_wins_byname
, "WINS_BYNAME" },
281 { WINBINDD_WINS_BYIP
, winbindd_wins_byip
, "WINS_BYIP" },
285 { WINBINDD_NUM_CMDS
, NULL
, "NONE" }
288 static void process_request(struct winbindd_cli_state
*state
)
290 struct winbindd_dispatch_table
*table
= dispatch_table
;
292 /* Free response data - we may be interrupted and receive another
293 command before being able to send this data off. */
295 SAFE_FREE(state
->response
.extra_data
.data
);
297 ZERO_STRUCT(state
->response
);
299 state
->response
.result
= WINBINDD_PENDING
;
300 state
->response
.length
= sizeof(struct winbindd_response
);
302 state
->mem_ctx
= talloc_init("winbind request");
303 if (state
->mem_ctx
== NULL
)
306 /* Remember who asked us. */
307 state
->pid
= state
->request
.pid
;
309 /* Process command */
311 for (table
= dispatch_table
; table
->fn
; table
++) {
312 if (state
->request
.cmd
== table
->cmd
) {
313 DEBUG(10,("process_request: request fn %s\n",
314 table
->winbindd_cmd_name
));
321 DEBUG(10,("process_request: unknown request fn number %d\n",
322 (int)state
->request
.cmd
));
323 request_error(state
);
328 * A list of file descriptors being monitored by select in the main processing
329 * loop. fd_event->handler is called whenever the socket is readable/writable.
332 static struct fd_event
*fd_events
= NULL
;
334 void add_fd_event(struct fd_event
*ev
)
336 struct fd_event
*match
;
338 /* only add unique fd_event structs */
340 for (match
=fd_events
; match
; match
=match
->next
) {
342 SMB_ASSERT( match
!= ev
);
349 DLIST_ADD(fd_events
, ev
);
352 void remove_fd_event(struct fd_event
*ev
)
354 DLIST_REMOVE(fd_events
, ev
);
358 * Handler for fd_events to complete a read/write request, set up by
359 * setup_async_read/setup_async_write.
362 static void rw_callback(struct fd_event
*event
, int flags
)
367 todo
= event
->length
- event
->done
;
369 if (event
->flags
& EVENT_FD_WRITE
) {
370 SMB_ASSERT(flags
== EVENT_FD_WRITE
);
371 done
= sys_write(event
->fd
,
372 &((char *)event
->data
)[event
->done
],
377 event
->finished(event
->private_data
, False
);
382 if (event
->flags
& EVENT_FD_READ
) {
383 SMB_ASSERT(flags
== EVENT_FD_READ
);
384 done
= sys_read(event
->fd
, &((char *)event
->data
)[event
->done
],
389 event
->finished(event
->private_data
, False
);
396 if (event
->done
== event
->length
) {
398 event
->finished(event
->private_data
, True
);
403 * Request an async read/write on a fd_event structure. (*finished) is called
404 * when the request is completed or an error had occurred.
407 void setup_async_read(struct fd_event
*event
, void *data
, size_t length
,
408 void (*finished
)(void *private_data
, BOOL success
),
411 SMB_ASSERT(event
->flags
== 0);
413 event
->length
= length
;
415 event
->handler
= rw_callback
;
416 event
->finished
= finished
;
417 event
->private_data
= private_data
;
418 event
->flags
= EVENT_FD_READ
;
421 void setup_async_write(struct fd_event
*event
, void *data
, size_t length
,
422 void (*finished
)(void *private_data
, BOOL success
),
425 SMB_ASSERT(event
->flags
== 0);
427 event
->length
= length
;
429 event
->handler
= rw_callback
;
430 event
->finished
= finished
;
431 event
->private_data
= private_data
;
432 event
->flags
= EVENT_FD_WRITE
;
436 * This is the main event loop of winbind requests. It goes through a
437 * state-machine of 3 read/write requests, 4 if you have extra data to send.
439 * An idle winbind client has a read request of 4 bytes outstanding,
440 * finalizing function is request_len_recv, checking the length. request_recv
441 * then processes the packet. The processing function then at some point has
442 * to call request_finished which schedules sending the response.
445 static void request_len_recv(void *private_data
, BOOL success
);
446 static void request_recv(void *private_data
, BOOL success
);
447 static void request_main_recv(void *private_data
, BOOL success
);
448 static void request_finished(struct winbindd_cli_state
*state
);
449 void request_finished_cont(void *private_data
, BOOL success
);
450 static void response_main_sent(void *private_data
, BOOL success
);
451 static void response_extra_sent(void *private_data
, BOOL success
);
453 static void response_extra_sent(void *private_data
, BOOL success
)
455 struct winbindd_cli_state
*state
=
456 talloc_get_type_abort(private_data
, struct winbindd_cli_state
);
458 if (state
->mem_ctx
!= NULL
) {
459 talloc_destroy(state
->mem_ctx
);
460 state
->mem_ctx
= NULL
;
464 state
->finished
= True
;
468 SAFE_FREE(state
->request
.extra_data
.data
);
469 SAFE_FREE(state
->response
.extra_data
.data
);
471 setup_async_read(&state
->fd_event
, &state
->request
, sizeof(uint32
),
472 request_len_recv
, state
);
475 static void response_main_sent(void *private_data
, BOOL success
)
477 struct winbindd_cli_state
*state
=
478 talloc_get_type_abort(private_data
, struct winbindd_cli_state
);
481 state
->finished
= True
;
485 if (state
->response
.length
== sizeof(state
->response
)) {
486 if (state
->mem_ctx
!= NULL
) {
487 talloc_destroy(state
->mem_ctx
);
488 state
->mem_ctx
= NULL
;
491 setup_async_read(&state
->fd_event
, &state
->request
,
492 sizeof(uint32
), request_len_recv
, state
);
496 setup_async_write(&state
->fd_event
, state
->response
.extra_data
.data
,
497 state
->response
.length
- sizeof(state
->response
),
498 response_extra_sent
, state
);
501 static void request_finished(struct winbindd_cli_state
*state
)
503 setup_async_write(&state
->fd_event
, &state
->response
,
504 sizeof(state
->response
), response_main_sent
, state
);
507 void request_error(struct winbindd_cli_state
*state
)
509 SMB_ASSERT(state
->response
.result
== WINBINDD_PENDING
);
510 state
->response
.result
= WINBINDD_ERROR
;
511 request_finished(state
);
514 void request_ok(struct winbindd_cli_state
*state
)
516 SMB_ASSERT(state
->response
.result
== WINBINDD_PENDING
);
517 state
->response
.result
= WINBINDD_OK
;
518 request_finished(state
);
521 void request_finished_cont(void *private_data
, BOOL success
)
523 struct winbindd_cli_state
*state
=
524 talloc_get_type_abort(private_data
, struct winbindd_cli_state
);
529 request_error(state
);
532 static void request_len_recv(void *private_data
, BOOL success
)
534 struct winbindd_cli_state
*state
=
535 talloc_get_type_abort(private_data
, struct winbindd_cli_state
);
538 state
->finished
= True
;
542 if (*(uint32
*)(&state
->request
) != sizeof(state
->request
)) {
543 DEBUG(0,("request_len_recv: Invalid request size received: %d (expected %d)\n",
544 *(uint32
*)(&state
->request
), (uint32
)sizeof(state
->request
)));
545 state
->finished
= True
;
549 setup_async_read(&state
->fd_event
, (uint32
*)(&state
->request
)+1,
550 sizeof(state
->request
) - sizeof(uint32
),
551 request_main_recv
, state
);
554 static void request_main_recv(void *private_data
, BOOL success
)
556 struct winbindd_cli_state
*state
=
557 talloc_get_type_abort(private_data
, struct winbindd_cli_state
);
560 state
->finished
= True
;
564 if (state
->request
.extra_len
== 0) {
565 state
->request
.extra_data
.data
= NULL
;
566 request_recv(state
, True
);
570 if ((!state
->privileged
) &&
571 (state
->request
.extra_len
> WINBINDD_MAX_EXTRA_DATA
)) {
572 DEBUG(3, ("Got request with %d bytes extra data on "
573 "unprivileged socket\n", (int)state
->request
.extra_len
));
574 state
->request
.extra_data
.data
= NULL
;
575 state
->finished
= True
;
579 state
->request
.extra_data
.data
=
580 SMB_MALLOC_ARRAY(char, state
->request
.extra_len
+ 1);
582 if (state
->request
.extra_data
.data
== NULL
) {
583 DEBUG(0, ("malloc failed\n"));
584 state
->finished
= True
;
588 /* Ensure null termination */
589 state
->request
.extra_data
.data
[state
->request
.extra_len
] = '\0';
591 setup_async_read(&state
->fd_event
, state
->request
.extra_data
.data
,
592 state
->request
.extra_len
, request_recv
, state
);
595 static void request_recv(void *private_data
, BOOL success
)
597 struct winbindd_cli_state
*state
=
598 talloc_get_type_abort(private_data
, struct winbindd_cli_state
);
601 state
->finished
= True
;
605 process_request(state
);
608 /* Process a new connection by adding it to the client connection list */
610 static void new_connection(int listen_sock
, BOOL privileged
)
612 struct sockaddr_un sunaddr
;
613 struct winbindd_cli_state
*state
;
617 /* Accept connection */
619 len
= sizeof(sunaddr
);
622 sock
= accept(listen_sock
, (struct sockaddr
*)&sunaddr
, &len
);
623 } while (sock
== -1 && errno
== EINTR
);
628 DEBUG(6,("accepted socket %d\n", sock
));
630 /* Create new connection structure */
632 if ((state
= TALLOC_ZERO_P(NULL
, struct winbindd_cli_state
)) == NULL
) {
639 state
->last_access
= time(NULL
);
641 state
->privileged
= privileged
;
643 state
->fd_event
.fd
= state
->sock
;
644 state
->fd_event
.flags
= 0;
645 add_fd_event(&state
->fd_event
);
647 setup_async_read(&state
->fd_event
, &state
->request
, sizeof(uint32
),
648 request_len_recv
, state
);
650 /* Add to connection list */
652 winbindd_add_client(state
);
655 /* Remove a client connection from client connection list */
657 static void remove_client(struct winbindd_cli_state
*state
)
659 /* It's a dead client - hold a funeral */
669 /* Free any getent state */
671 free_getent_state(state
->getpwent_state
);
672 free_getent_state(state
->getgrent_state
);
674 /* We may have some extra data that was not freed if the client was
675 killed unexpectedly */
677 SAFE_FREE(state
->response
.extra_data
.data
);
679 if (state
->mem_ctx
!= NULL
) {
680 talloc_destroy(state
->mem_ctx
);
681 state
->mem_ctx
= NULL
;
684 remove_fd_event(&state
->fd_event
);
686 /* Remove from list and free */
688 winbindd_remove_client(state
);
692 /* Shutdown client connection which has been idle for the longest time */
694 static BOOL
remove_idle_client(void)
696 struct winbindd_cli_state
*state
, *remove_state
= NULL
;
697 time_t last_access
= 0;
700 for (state
= winbindd_client_list(); state
; state
= state
->next
) {
701 if (state
->response
.result
!= WINBINDD_PENDING
&&
702 !state
->getpwent_state
&& !state
->getgrent_state
) {
704 if (!last_access
|| state
->last_access
< last_access
) {
705 last_access
= state
->last_access
;
706 remove_state
= state
;
712 DEBUG(5,("Found %d idle client connections, shutting down sock %d, pid %u\n",
713 nidle
, remove_state
->sock
, (unsigned int)remove_state
->pid
));
714 remove_client(remove_state
);
721 /* Process incoming clients on listen_sock. We use a tricky non-blocking,
722 non-forking, non-threaded model which allows us to handle many
723 simultaneous connections while remaining impervious to many denial of
726 static void process_loop(void)
728 struct winbindd_cli_state
*state
;
731 int maxfd
, listen_sock
, listen_priv_sock
, selret
;
732 struct timeval timeout
, ev_timeout
;
734 /* Open Sockets here to get stuff going ASAP */
735 listen_sock
= open_winbindd_socket();
736 listen_priv_sock
= open_winbindd_priv_socket();
738 if (listen_sock
== -1 || listen_priv_sock
== -1) {
739 perror("open_winbind_socket");
743 /* We'll be doing this a lot */
745 /* Handle messages */
749 run_events(winbind_event_context(), 0, NULL
, NULL
);
751 /* refresh the trusted domain cache */
753 rescan_trusted_domains();
755 /* Free up temporary memory */
758 main_loop_TALLOC_FREE();
760 /* Initialise fd lists for select() */
762 maxfd
= MAX(listen_sock
, listen_priv_sock
);
766 FD_SET(listen_sock
, &r_fds
);
767 FD_SET(listen_priv_sock
, &r_fds
);
769 timeout
.tv_sec
= WINBINDD_ESTABLISH_LOOP
;
772 /* Check for any event timeouts. */
773 if (get_timed_events_timeout(winbind_event_context(), &ev_timeout
)) {
774 timeout
= timeval_min(&timeout
, &ev_timeout
);
777 /* Set up client readers and writers */
779 state
= winbindd_client_list();
783 struct winbindd_cli_state
*next
= state
->next
;
785 /* Dispose of client connection if it is marked as
789 remove_client(state
);
794 for (ev
= fd_events
; ev
; ev
= ev
->next
) {
795 if (ev
->flags
& EVENT_FD_READ
) {
796 FD_SET(ev
->fd
, &r_fds
);
797 maxfd
= MAX(ev
->fd
, maxfd
);
799 if (ev
->flags
& EVENT_FD_WRITE
) {
800 FD_SET(ev
->fd
, &w_fds
);
801 maxfd
= MAX(ev
->fd
, maxfd
);
807 selret
= sys_select(maxfd
+ 1, &r_fds
, &w_fds
, NULL
, &timeout
);
814 if (errno
== EINTR
) {
818 /* Select error, something is badly wrong */
828 struct fd_event
*next
= ev
->next
;
830 if (FD_ISSET(ev
->fd
, &r_fds
))
831 flags
|= EVENT_FD_READ
;
832 if (FD_ISSET(ev
->fd
, &w_fds
))
833 flags
|= EVENT_FD_WRITE
;
835 ev
->handler(ev
, flags
);
839 if (FD_ISSET(listen_sock
, &r_fds
)) {
840 while (winbindd_num_clients() >
841 WINBINDD_MAX_SIMULTANEOUS_CLIENTS
- 1) {
842 DEBUG(5,("winbindd: Exceeding %d client "
843 "connections, removing idle "
845 WINBINDD_MAX_SIMULTANEOUS_CLIENTS
));
846 if (!remove_idle_client()) {
847 DEBUG(0,("winbindd: Exceeding %d "
848 "client connections, no idle "
849 "connection found\n",
850 WINBINDD_MAX_SIMULTANEOUS_CLIENTS
));
854 /* new, non-privileged connection */
855 new_connection(listen_sock
, False
);
858 if (FD_ISSET(listen_priv_sock
, &r_fds
)) {
859 while (winbindd_num_clients() >
860 WINBINDD_MAX_SIMULTANEOUS_CLIENTS
- 1) {
861 DEBUG(5,("winbindd: Exceeding %d client "
862 "connections, removing idle "
864 WINBINDD_MAX_SIMULTANEOUS_CLIENTS
));
865 if (!remove_idle_client()) {
866 DEBUG(0,("winbindd: Exceeding %d "
867 "client connections, no idle "
868 "connection found\n",
869 WINBINDD_MAX_SIMULTANEOUS_CLIENTS
));
873 /* new, privileged connection */
874 new_connection(listen_priv_sock
, True
);
880 winbindd_check_cache_size(time(NULL
));
883 /* Check signal handling things */
890 DEBUG(3, ("got SIGHUP\n"));
892 msg_reload_services(MSG_SMB_CONF_UPDATED
, pid_to_procid(0), NULL
, 0, NULL
);
897 print_winbindd_status();
906 while ((pid
= sys_waitpid(-1, NULL
, WNOHANG
)) > 0) {
907 winbind_child_died(pid
);
914 int main(int argc
, char **argv
, char **envp
)
917 static BOOL Fork
= True
;
918 static BOOL log_stdout
= False
;
919 static BOOL no_process_group
= False
;
920 struct poptOption long_options
[] = {
922 { "stdout", 'S', POPT_ARG_VAL
, &log_stdout
, True
, "Log to stdout" },
923 { "foreground", 'F', POPT_ARG_VAL
, &Fork
, False
, "Daemon in foreground mode" },
924 { "no-process-group", 0, POPT_ARG_VAL
, &no_process_group
, True
, "Don't create a new process group" },
925 { "interactive", 'i', POPT_ARG_NONE
, NULL
, 'i', "Interactive mode" },
926 { "no-caching", 'n', POPT_ARG_VAL
, &opt_nocache
, True
, "Disable caching" },
933 /* glibc (?) likes to print "User defined signal 1" and exit if a
934 SIGUSR[12] is received before a handler is installed */
936 CatchSignal(SIGUSR1
, SIG_IGN
);
937 CatchSignal(SIGUSR2
, SIG_IGN
);
939 fault_setup((void (*)(void *))fault_quit
);
940 dump_core_setup("winbindd");
944 /* Initialise for running in non-root mode */
948 set_remote_machine_name("winbindd", False
);
950 /* Set environment variable so we don't recursively call ourselves.
951 This may also be useful interactively. */
953 if ( !winbind_off() ) {
954 DEBUG(0,("Failed to disable recusive winbindd calls. Exiting.\n"));
958 /* Initialise samba/rpc client stuff */
960 pc
= poptGetContext("winbindd", argc
, (const char **)argv
, long_options
,
961 POPT_CONTEXT_KEEP_FIRST
);
963 while ((opt
= poptGetNextOpt(pc
)) != -1) {
965 /* Don't become a daemon */
975 if (log_stdout
&& Fork
) {
976 printf("Can't log to stdout (-S) unless daemon is in foreground +(-F) or interactive (-i)\n");
977 poptPrintUsage(pc
, stderr
, 0);
981 if (!override_logfile
) {
982 pstr_sprintf(logfile
, "%s/log.winbindd", dyn_LOGFILEBASE
);
983 lp_set_logfile(logfile
);
985 setup_logging("winbindd", log_stdout
);
988 DEBUG(1, ("winbindd version %s started.\n%s\n",
989 SAMBA_VERSION_STRING
,
990 COPYRIGHT_STARTUP_MESSAGE
) );
992 if (!reload_services_file()) {
993 DEBUG(0, ("error opening config file\n"));
997 if (!directory_exist(lp_lockdir(), NULL
)) {
998 mkdir(lp_lockdir(), 0755);
1008 if (!secrets_init()) {
1010 DEBUG(0,("Could not initialize domain trust account secrets. Giving up\n"));
1014 /* Enable netbios namecache */
1018 /* Winbind daemon initialisation */
1020 if ( ! NT_STATUS_IS_OK(idmap_init_cache()) ) {
1021 DEBUG(1, ("Could not init idmap cache!\n"));
1024 /* Unblock all signals we are interested in as they may have been
1025 blocked by the parent process. */
1027 BlockSignals(False
, SIGINT
);
1028 BlockSignals(False
, SIGQUIT
);
1029 BlockSignals(False
, SIGTERM
);
1030 BlockSignals(False
, SIGUSR1
);
1031 BlockSignals(False
, SIGUSR2
);
1032 BlockSignals(False
, SIGHUP
);
1033 BlockSignals(False
, SIGCHLD
);
1035 /* Setup signal handlers */
1037 CatchSignal(SIGINT
, termination_handler
); /* Exit on these sigs */
1038 CatchSignal(SIGQUIT
, termination_handler
);
1039 CatchSignal(SIGTERM
, termination_handler
);
1040 CatchSignal(SIGCHLD
, sigchld_handler
);
1042 CatchSignal(SIGPIPE
, SIG_IGN
); /* Ignore sigpipe */
1044 CatchSignal(SIGUSR2
, sigusr2_handler
); /* Debugging sigs */
1045 CatchSignal(SIGHUP
, sighup_handler
);
1048 become_daemon(Fork
, no_process_group
);
1050 pidfile_create("winbindd");
1054 * If we're interactive we want to set our own process group for
1055 * signal management.
1057 if (interactive
&& !no_process_group
)
1058 setpgid( (pid_t
)0, (pid_t
)0);
1063 /* Initialise messaging system */
1065 if (!message_init()) {
1066 DEBUG(0, ("unable to initialize messaging system\n"));
1070 /* Initialize cache (ensure version is correct). */
1071 if (!initialize_winbindd_cache()) {
1075 /* React on 'smbcontrol winbindd reload-config' in the same way
1076 as to SIGHUP signal */
1077 message_register(MSG_SMB_CONF_UPDATED
, msg_reload_services
, NULL
);
1078 message_register(MSG_SHUTDOWN
, msg_shutdown
, NULL
);
1080 /* Handle online/offline messages. */
1081 message_register(MSG_WINBIND_OFFLINE
, winbind_msg_offline
, NULL
);
1082 message_register(MSG_WINBIND_ONLINE
, winbind_msg_online
, NULL
);
1083 message_register(MSG_WINBIND_ONLINESTATUS
, winbind_msg_onlinestatus
,
1086 poptFreeContext(pc
);
1088 netsamlogon_cache_init(); /* Non-critical */
1090 if (!init_domain_list()) {
1091 DEBUG(0,("unable to initalize domain list\n"));
1097 smb_nscd_flush_user_cache();
1098 smb_nscd_flush_group_cache();
1100 /* Loop waiting for requests */