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.
29 BOOL opt_nocache
= False
;
30 BOOL opt_dual_daemon
= True
;
31 static BOOL interactive
= False
;
33 extern BOOL override_logfile
;
35 /* Reload configuration */
37 static BOOL
reload_services_file(void)
44 pstrcpy(fname
,lp_configfile());
45 if (file_exist(fname
,NULL
) && !strcsequal(fname
,dyn_CONFIGFILE
)) {
46 pstrcpy(dyn_CONFIGFILE
,fname
);
51 ret
= lp_load(dyn_CONFIGFILE
,False
,False
,True
);
62 /**************************************************************************** **
63 Prepare to dump a core file - carefully!
64 **************************************************************************** */
66 static BOOL
dump_core(void)
70 pstrcpy( dname
, lp_logfile() );
71 if ((p
=strrchr(dname
,'/')))
73 pstrcat( dname
, "/corefiles" );
75 sys_chown( dname
, getuid(), getgid() );
85 getrlimit( RLIMIT_CORE
, &rlp
);
86 rlp
.rlim_cur
= MAX( 4*1024*1024, rlp
.rlim_cur
);
87 setrlimit( RLIMIT_CORE
, &rlp
);
88 getrlimit( RLIMIT_CORE
, &rlp
);
89 DEBUG( 3, ( "Core limits now %d %d\n", (int)rlp
.rlim_cur
, (int)rlp
.rlim_max
) );
94 DEBUG(0,("Dumping core in %s\n",dname
));
100 /**************************************************************************** **
102 **************************************************************************** */
104 static void fault_quit(void)
111 static void winbindd_status(void)
113 struct winbindd_cli_state
*tmp
;
115 DEBUG(0, ("winbindd status:\n"));
117 /* Print client state information */
119 DEBUG(0, ("\t%d clients currently active\n", winbindd_num_clients()));
121 if (DEBUGLEVEL
>= 2 && winbindd_num_clients()) {
122 DEBUG(2, ("\tclient list:\n"));
123 for(tmp
= winbindd_client_list(); tmp
; tmp
= tmp
->next
) {
124 DEBUG(2, ("\t\tpid %lu, sock %d, rbl %d, wbl %d\n",
125 (unsigned long)tmp
->pid
, tmp
->sock
, tmp
->read_buf_len
,
126 tmp
->write_buf_len
));
131 /* Print winbindd status to log file */
133 static void print_winbindd_status(void)
138 /* Flush client cache */
140 static void flush_caches(void)
143 /* Clear cached user and group enumation info */
144 if (!opt_dual_daemon
) /* Until we have coherent cache flush. */
145 wcache_flush_cache();
148 /* We need to invalidate cached user list entries on a SIGHUP
149 otherwise cached access denied errors due to restrict anonymous
150 hang around until the sequence number changes. */
152 wcache_invalidate_cache();
155 /* Handle the signal by unlinking socket and exiting */
157 static void terminate(void)
163 /* Remove socket file */
164 pstr_sprintf(path
, "%s/%s",
165 WINBINDD_SOCKET_DIR
, WINBINDD_SOCKET_NAME
);
170 TALLOC_CTX
*mem_ctx
= talloc_init("end_description");
171 char *description
= talloc_describe_all(mem_ctx
);
173 DEBUG(3, ("tallocs left:\n%s\n", description
));
174 talloc_destroy(mem_ctx
);
181 static BOOL do_sigterm
;
183 static void termination_handler(int signum
)
186 sys_select_signal(signum
);
189 static BOOL do_sigusr2
;
191 static void sigusr2_handler(int signum
)
194 sys_select_signal(SIGUSR2
);
197 static BOOL do_sighup
;
199 static void sighup_handler(int signum
)
202 sys_select_signal(SIGHUP
);
205 static BOOL do_sigchld
;
207 static void sigchld_handler(int signum
)
210 sys_select_signal(SIGCHLD
);
213 /* React on 'smbcontrol winbindd reload-config' in the same way as on SIGHUP*/
214 static void msg_reload_services(int msg_type
, pid_t src
, void *buf
, size_t len
)
216 /* Flush various caches */
218 reload_services_file();
221 /* React on 'smbcontrol winbindd shutdown' in the same way as on SIGTERM*/
222 static void msg_shutdown(int msg_type
, pid_t src
, void *buf
, size_t len
)
227 static struct winbindd_dispatch_table
{
228 enum winbindd_cmd cmd
;
229 void (*fn
)(struct winbindd_cli_state
*state
);
230 const char *winbindd_cmd_name
;
231 } dispatch_table
[] = {
235 { WINBINDD_GETPWNAM
, winbindd_getpwnam
, "GETPWNAM" },
236 { WINBINDD_GETPWUID
, winbindd_getpwuid
, "GETPWUID" },
238 { WINBINDD_SETPWENT
, winbindd_setpwent
, "SETPWENT" },
239 { WINBINDD_ENDPWENT
, winbindd_endpwent
, "ENDPWENT" },
240 { WINBINDD_GETPWENT
, winbindd_getpwent
, "GETPWENT" },
242 { WINBINDD_GETGROUPS
, winbindd_getgroups
, "GETGROUPS" },
243 { WINBINDD_GETUSERSIDS
, winbindd_getusersids
, "GETUSERSIDS" },
244 { WINBINDD_GETUSERDOMGROUPS
, winbindd_getuserdomgroups
,
245 "GETUSERDOMGROUPS" },
247 /* Group functions */
249 { WINBINDD_GETGRNAM
, winbindd_getgrnam
, "GETGRNAM" },
250 { WINBINDD_GETGRGID
, winbindd_getgrgid
, "GETGRGID" },
251 { WINBINDD_SETGRENT
, winbindd_setgrent
, "SETGRENT" },
252 { WINBINDD_ENDGRENT
, winbindd_endgrent
, "ENDGRENT" },
253 { WINBINDD_GETGRENT
, winbindd_getgrent
, "GETGRENT" },
254 { WINBINDD_GETGRLST
, winbindd_getgrent
, "GETGRLST" },
256 /* PAM auth functions */
258 { WINBINDD_PAM_AUTH
, winbindd_pam_auth
, "PAM_AUTH" },
259 { WINBINDD_PAM_AUTH_CRAP
, winbindd_pam_auth_crap
, "AUTH_CRAP" },
260 { WINBINDD_PAM_CHAUTHTOK
, winbindd_pam_chauthtok
, "CHAUTHTOK" },
262 /* Enumeration functions */
264 { WINBINDD_LIST_USERS
, winbindd_list_users
, "LIST_USERS" },
265 { WINBINDD_LIST_GROUPS
, winbindd_list_groups
, "LIST_GROUPS" },
266 { WINBINDD_LIST_TRUSTDOM
, winbindd_list_trusted_domains
,
268 { WINBINDD_SHOW_SEQUENCE
, winbindd_show_sequence
, "SHOW_SEQUENCE" },
270 /* SID related functions */
272 { WINBINDD_LOOKUPSID
, winbindd_lookupsid
, "LOOKUPSID" },
273 { WINBINDD_LOOKUPNAME
, winbindd_lookupname
, "LOOKUPNAME" },
275 /* Lookup related functions */
277 { WINBINDD_SID_TO_UID
, winbindd_sid_to_uid
, "SID_TO_UID" },
278 { WINBINDD_SID_TO_GID
, winbindd_sid_to_gid
, "SID_TO_GID" },
279 { WINBINDD_UID_TO_SID
, winbindd_uid_to_sid
, "UID_TO_SID" },
280 { WINBINDD_GID_TO_SID
, winbindd_gid_to_sid
, "GID_TO_SID" },
281 { WINBINDD_ALLOCATE_RID
, winbindd_allocate_rid
, "ALLOCATE_RID" },
282 { WINBINDD_ALLOCATE_RID_AND_GID
, winbindd_allocate_rid_and_gid
,
283 "ALLOCATE_RID_AND_GID" },
287 { WINBINDD_CHECK_MACHACC
, winbindd_check_machine_acct
, "CHECK_MACHACC" },
288 { WINBINDD_PING
, winbindd_ping
, "PING" },
289 { WINBINDD_INFO
, winbindd_info
, "INFO" },
290 { WINBINDD_INTERFACE_VERSION
, winbindd_interface_version
,
291 "INTERFACE_VERSION" },
292 { WINBINDD_DOMAIN_NAME
, winbindd_domain_name
, "DOMAIN_NAME" },
293 { WINBINDD_DOMAIN_INFO
, winbindd_domain_info
, "DOMAIN_INFO" },
294 { WINBINDD_NETBIOS_NAME
, winbindd_netbios_name
, "NETBIOS_NAME" },
295 { WINBINDD_PRIV_PIPE_DIR
, winbindd_priv_pipe_dir
,
296 "WINBINDD_PRIV_PIPE_DIR" },
297 { WINBINDD_GETDCNAME
, winbindd_getdcname
, "GETDCNAME" },
301 { WINBINDD_WINS_BYNAME
, winbindd_wins_byname
, "WINS_BYNAME" },
302 { WINBINDD_WINS_BYIP
, winbindd_wins_byip
, "WINS_BYIP" },
306 { WINBINDD_NUM_CMDS
, NULL
, "NONE" }
309 static void process_request(struct winbindd_cli_state
*state
)
311 struct winbindd_dispatch_table
*table
= dispatch_table
;
313 /* Free response data - we may be interrupted and receive another
314 command before being able to send this data off. */
316 SAFE_FREE(state
->response
.extra_data
);
318 ZERO_STRUCT(state
->response
);
320 state
->response
.result
= WINBINDD_PENDING
;
321 state
->response
.length
= sizeof(struct winbindd_response
);
323 state
->mem_ctx
= talloc_init("winbind request");
324 if (state
->mem_ctx
== NULL
)
327 /* Process command */
329 for (table
= dispatch_table
; table
->fn
; table
++) {
330 if (state
->request
.cmd
== table
->cmd
) {
331 DEBUG(10,("process_request: request fn %s\n",
332 table
->winbindd_cmd_name
));
339 DEBUG(10,("process_request: unknown request fn number %d\n",
340 (int)state
->request
.cmd
));
341 request_error(state
);
346 * A list of file descriptors being monitored by select in the main processing
347 * loop. fd_event->handler is called whenever the socket is readable/writable.
350 static struct fd_event
*fd_events
= NULL
;
352 void add_fd_event(struct fd_event
*ev
)
354 struct fd_event
*match
;
356 /* only add unique fd_event structs */
358 for (match
=fd_events
; match
; match
=match
->next
) {
360 SMB_ASSERT( match
!= ev
);
367 DLIST_ADD(fd_events
, ev
);
370 void remove_fd_event(struct fd_event
*ev
)
372 DLIST_REMOVE(fd_events
, ev
);
376 * Handler for fd_events to complete a read/write request, set up by
377 * setup_async_read/setup_async_write.
380 static void rw_callback(struct fd_event
*event
, int flags
)
385 todo
= event
->length
- event
->done
;
387 if (event
->flags
& EVENT_FD_WRITE
) {
388 SMB_ASSERT(flags
== EVENT_FD_WRITE
);
389 done
= sys_write(event
->fd
,
390 &((char *)event
->data
)[event
->done
],
395 event
->finished(event
->private_data
, False
);
400 if (event
->flags
& EVENT_FD_READ
) {
401 SMB_ASSERT(flags
== EVENT_FD_READ
);
402 done
= sys_read(event
->fd
, &((char *)event
->data
)[event
->done
],
407 event
->finished(event
->private_data
, False
);
414 if (event
->done
== event
->length
) {
416 event
->finished(event
->private_data
, True
);
421 * Request an async read/write on a fd_event structure. (*finished) is called
422 * when the request is completed or an error had occurred.
425 void setup_async_read(struct fd_event
*event
, void *data
, size_t length
,
426 void (*finished
)(void *private_data
, BOOL success
),
429 SMB_ASSERT(event
->flags
== 0);
431 event
->length
= length
;
433 event
->handler
= rw_callback
;
434 event
->finished
= finished
;
435 event
->private_data
= private_data
;
436 event
->flags
= EVENT_FD_READ
;
439 void setup_async_write(struct fd_event
*event
, void *data
, size_t length
,
440 void (*finished
)(void *private_data
, BOOL success
),
443 SMB_ASSERT(event
->flags
== 0);
445 event
->length
= length
;
447 event
->handler
= rw_callback
;
448 event
->finished
= finished
;
449 event
->private_data
= private_data
;
450 event
->flags
= EVENT_FD_WRITE
;
454 * This is the main event loop of winbind requests. It goes through a
455 * state-machine of 3 read/write requests, 4 if you have extra data to send.
457 * An idle winbind client has a read request of 4 bytes outstanding,
458 * finalizing function is request_len_recv, checking the length. request_recv
459 * then processes the packet. The processing function then at some point has
460 * to call request_finished which schedules sending the response.
463 static void request_len_recv(void *private_data
, BOOL success
);
464 static void request_recv(void *private_data
, BOOL success
);
465 static void request_finished(struct winbindd_cli_state
*state
);
466 void request_finished_cont(void *private_data
, BOOL success
);
467 static void response_main_sent(void *private_data
, BOOL success
);
468 static void response_extra_sent(void *private_data
, BOOL success
);
470 static void response_extra_sent(void *private_data
, BOOL success
)
472 struct winbindd_cli_state
*state
=
473 talloc_get_type_abort(private_data
, struct winbindd_cli_state
);
475 if (state
->mem_ctx
!= NULL
) {
476 talloc_destroy(state
->mem_ctx
);
477 state
->mem_ctx
= NULL
;
481 state
->finished
= True
;
485 SAFE_FREE(state
->response
.extra_data
);
487 setup_async_read(&state
->fd_event
, &state
->request
, sizeof(uint32
),
488 request_len_recv
, state
);
491 static void response_main_sent(void *private_data
, BOOL success
)
493 struct winbindd_cli_state
*state
=
494 talloc_get_type_abort(private_data
, struct winbindd_cli_state
);
497 state
->finished
= True
;
501 if (state
->response
.length
== sizeof(state
->response
)) {
502 if (state
->mem_ctx
!= NULL
) {
503 talloc_destroy(state
->mem_ctx
);
504 state
->mem_ctx
= NULL
;
507 setup_async_read(&state
->fd_event
, &state
->request
,
508 sizeof(uint32
), request_len_recv
, state
);
512 setup_async_write(&state
->fd_event
, state
->response
.extra_data
,
513 state
->response
.length
- sizeof(state
->response
),
514 response_extra_sent
, state
);
517 static void request_finished(struct winbindd_cli_state
*state
)
519 setup_async_write(&state
->fd_event
, &state
->response
,
520 sizeof(state
->response
), response_main_sent
, state
);
523 void request_error(struct winbindd_cli_state
*state
)
525 SMB_ASSERT(state
->response
.result
== WINBINDD_PENDING
);
526 state
->response
.result
= WINBINDD_ERROR
;
527 request_finished(state
);
530 void request_ok(struct winbindd_cli_state
*state
)
532 SMB_ASSERT(state
->response
.result
== WINBINDD_PENDING
);
533 state
->response
.result
= WINBINDD_OK
;
534 request_finished(state
);
537 void request_finished_cont(void *private_data
, BOOL success
)
539 struct winbindd_cli_state
*state
=
540 talloc_get_type_abort(private_data
, struct winbindd_cli_state
);
545 request_error(state
);
548 static void request_recv(void *private_data
, BOOL success
)
550 struct winbindd_cli_state
*state
=
551 talloc_get_type_abort(private_data
, struct winbindd_cli_state
);
554 state
->finished
= True
;
558 process_request(state
);
561 static void request_len_recv(void *private_data
, BOOL success
)
563 struct winbindd_cli_state
*state
=
564 talloc_get_type_abort(private_data
, struct winbindd_cli_state
);
567 state
->finished
= True
;
571 if (*(uint32
*)(&state
->request
) != sizeof(state
->request
)) {
572 DEBUG(0,("process_loop: Invalid request size received: %d\n",
573 *(uint32
*)(&state
->request
)));
574 state
->finished
= True
;
578 setup_async_read(&state
->fd_event
, (uint32
*)(&state
->request
)+1,
579 sizeof(state
->request
) - sizeof(uint32
),
580 request_recv
, state
);
583 /* Process a new connection by adding it to the client connection list */
585 static void new_connection(int listen_sock
, BOOL privileged
)
587 struct sockaddr_un sunaddr
;
588 struct winbindd_cli_state
*state
;
592 /* Accept connection */
594 len
= sizeof(sunaddr
);
597 sock
= accept(listen_sock
, (struct sockaddr
*)&sunaddr
, &len
);
598 } while (sock
== -1 && errno
== EINTR
);
603 DEBUG(6,("accepted socket %d\n", sock
));
605 /* Create new connection structure */
607 if ((state
= TALLOC_ZERO_P(NULL
, struct winbindd_cli_state
)) == NULL
)
612 state
->last_access
= time(NULL
);
614 state
->privileged
= privileged
;
616 state
->fd_event
.fd
= state
->sock
;
617 state
->fd_event
.flags
= 0;
618 add_fd_event(&state
->fd_event
);
620 setup_async_read(&state
->fd_event
, &state
->request
, sizeof(uint32
),
621 request_len_recv
, state
);
623 /* Add to connection list */
625 winbindd_add_client(state
);
628 /* Remove a client connection from client connection list */
630 static void remove_client(struct winbindd_cli_state
*state
)
632 /* It's a dead client - hold a funeral */
640 /* Free any getent state */
642 free_getent_state(state
->getpwent_state
);
643 free_getent_state(state
->getgrent_state
);
645 /* We may have some extra data that was not freed if the
646 client was killed unexpectedly */
648 SAFE_FREE(state
->response
.extra_data
);
650 if (state
->mem_ctx
!= NULL
) {
651 talloc_destroy(state
->mem_ctx
);
652 state
->mem_ctx
= NULL
;
655 remove_fd_event(&state
->fd_event
);
657 /* Remove from list and free */
659 winbindd_remove_client(state
);
665 /* Shutdown client connection which has been idle for the longest time */
667 static BOOL
remove_idle_client(void)
669 struct winbindd_cli_state
*state
, *remove_state
= NULL
;
670 time_t last_access
= 0;
673 for (state
= winbindd_client_list(); state
; state
= state
->next
) {
674 if (state
->read_buf_len
== 0 && state
->write_buf_len
== 0 &&
675 state
->response
.result
!= WINBINDD_PENDING
&&
676 !state
->getpwent_state
&& !state
->getgrent_state
) {
678 if (!last_access
|| state
->last_access
< last_access
) {
679 last_access
= state
->last_access
;
680 remove_state
= state
;
686 DEBUG(5,("Found %d idle client connections, shutting down sock %d, pid %u\n",
687 nidle
, remove_state
->sock
, (unsigned int)remove_state
->pid
));
688 remove_client(remove_state
);
695 /* Process a complete received packet from a client */
697 void winbind_process_packet(struct winbindd_cli_state
*state
)
699 /* Process request */
701 /* Ensure null termination of entire request */
702 state
->request
.null_term
= '\0';
704 state
->pid
= state
->request
.pid
;
706 process_request(state
);
708 /* Update client state */
710 state
->read_buf_len
= 0;
711 state
->write_buf_len
= sizeof(struct winbindd_response
);
713 /* we might need to send it to the dual daemon */
714 if (opt_dual_daemon
) {
715 dual_send_request(state
);
719 /* Process incoming clients on listen_sock. We use a tricky non-blocking,
720 non-forking, non-threaded model which allows us to handle many
721 simultaneous connections while remaining impervious to many denial of
724 static void process_loop(void)
726 struct winbindd_cli_state
*state
;
729 int maxfd
, listen_sock
, listen_priv_sock
, selret
;
730 struct timeval timeout
;
732 /* We'll be doing this a lot */
734 /* Handle messages */
738 /* refresh the trusted domain cache */
740 rescan_trusted_domains();
742 /* Free up temporary memory */
745 main_loop_talloc_free();
747 /* Initialise fd lists for select() */
749 listen_sock
= open_winbindd_socket();
750 listen_priv_sock
= open_winbindd_priv_socket();
752 if (listen_sock
== -1 || listen_priv_sock
== -1) {
753 perror("open_winbind_socket");
757 maxfd
= MAX(listen_sock
, listen_priv_sock
);
761 FD_SET(listen_sock
, &r_fds
);
762 FD_SET(listen_priv_sock
, &r_fds
);
764 timeout
.tv_sec
= WINBINDD_ESTABLISH_LOOP
;
767 if (opt_dual_daemon
) {
768 maxfd
= dual_select_setup(&w_fds
, maxfd
);
771 /* Set up client readers and writers */
773 state
= winbindd_client_list();
777 struct winbindd_cli_state
*next
= state
->next
;
779 /* Dispose of client connection if it is marked as
783 remove_client(state
);
788 for (ev
= fd_events
; ev
; ev
= ev
->next
) {
789 if (ev
->flags
& EVENT_FD_READ
) {
790 FD_SET(ev
->fd
, &r_fds
);
791 maxfd
= MAX(ev
->fd
, maxfd
);
793 if (ev
->flags
& EVENT_FD_WRITE
) {
794 FD_SET(ev
->fd
, &w_fds
);
795 maxfd
= MAX(ev
->fd
, maxfd
);
801 selret
= sys_select(maxfd
+ 1, &r_fds
, &w_fds
, NULL
, &timeout
);
806 if ((selret
== -1 && errno
!= EINTR
) || selret
== 0) {
808 /* Select error, something is badly wrong */
814 /* Create a new connection if listen_sock readable */
816 if (opt_dual_daemon
) {
822 struct fd_event
*next
= ev
->next
;
824 if (FD_ISSET(ev
->fd
, &r_fds
))
825 flags
|= EVENT_FD_READ
;
826 if (FD_ISSET(ev
->fd
, &w_fds
))
827 flags
|= EVENT_FD_WRITE
;
829 ev
->handler(ev
, flags
);
833 if (FD_ISSET(listen_sock
, &r_fds
)) {
834 while (winbindd_num_clients() >
835 WINBINDD_MAX_SIMULTANEOUS_CLIENTS
- 1) {
836 DEBUG(5,("winbindd: Exceeding %d client "
837 "connections, removing idle "
839 WINBINDD_MAX_SIMULTANEOUS_CLIENTS
));
840 if (!remove_idle_client()) {
841 DEBUG(0,("winbindd: Exceeding %d "
842 "client connections, no idle "
843 "connection found\n",
844 WINBINDD_MAX_SIMULTANEOUS_CLIENTS
));
848 /* new, non-privileged connection */
849 new_connection(listen_sock
, False
);
852 if (FD_ISSET(listen_priv_sock
, &r_fds
)) {
853 while (winbindd_num_clients() >
854 WINBINDD_MAX_SIMULTANEOUS_CLIENTS
- 1) {
855 DEBUG(5,("winbindd: Exceeding %d client "
856 "connections, removing idle "
858 WINBINDD_MAX_SIMULTANEOUS_CLIENTS
));
859 if (!remove_idle_client()) {
860 DEBUG(0,("winbindd: Exceeding %d "
861 "client connections, no idle "
862 "connection found\n",
863 WINBINDD_MAX_SIMULTANEOUS_CLIENTS
));
867 /* new, privileged connection */
868 new_connection(listen_priv_sock
, True
);
874 winbindd_check_cache_size(time(NULL
));
877 /* Check signal handling things */
884 DEBUG(3, ("got SIGHUP\n"));
886 msg_reload_services(MSG_SMB_CONF_UPDATED
, (pid_t
) 0, NULL
, 0);
891 print_winbindd_status();
900 while ((pid
= sys_waitpid(-1, NULL
, WNOHANG
)) > 0) {
901 winbind_child_died(pid
);
908 struct winbindd_state server_state
; /* Server state information */
910 int main(int argc
, char **argv
)
913 static BOOL Fork
= True
;
914 static BOOL log_stdout
= False
;
915 struct poptOption long_options
[] = {
917 { "stdout", 'S', POPT_ARG_VAL
, &log_stdout
, True
, "Log to stdout" },
918 { "foreground", 'F', POPT_ARG_VAL
, &Fork
, False
, "Daemon in foreground mode" },
919 { "interactive", 'i', POPT_ARG_NONE
, NULL
, 'i', "Interactive mode" },
920 { "single-daemon", 'Y', POPT_ARG_VAL
, &opt_dual_daemon
, False
, "Single daemon mode" },
921 { "no-caching", 'n', POPT_ARG_VAL
, &opt_nocache
, True
, "Disable caching" },
928 /* glibc (?) likes to print "User defined signal 1" and exit if a
929 SIGUSR[12] is received before a handler is installed */
931 CatchSignal(SIGUSR1
, SIG_IGN
);
932 CatchSignal(SIGUSR2
, SIG_IGN
);
934 fault_setup((void (*)(void *))fault_quit
);
936 /* Initialise for running in non-root mode */
940 set_remote_machine_name("winbindd", False
);
942 /* Set environment variable so we don't recursively call ourselves.
943 This may also be useful interactively. */
945 setenv(WINBINDD_DONT_ENV
, "1", 1);
947 /* Initialise samba/rpc client stuff */
949 pc
= poptGetContext("winbindd", argc
, (const char **)argv
, long_options
,
950 POPT_CONTEXT_KEEP_FIRST
);
952 while ((opt
= poptGetNextOpt(pc
)) != -1) {
954 /* Don't become a daemon */
964 if (log_stdout
&& Fork
) {
965 printf("Can't log to stdout (-S) unless daemon is in foreground +(-F) or interactive (-i)\n");
966 poptPrintUsage(pc
, stderr
, 0);
970 if (!override_logfile
) {
971 pstr_sprintf(logfile
, "%s/log.winbindd", dyn_LOGFILEBASE
);
972 lp_set_logfile(logfile
);
974 setup_logging("winbindd", log_stdout
);
977 DEBUG(1, ("winbindd version %s started.\n", SAMBA_VERSION_STRING
) );
978 DEBUGADD( 1, ( "Copyright The Samba Team 2000-2004\n" ) );
980 if (!reload_services_file()) {
981 DEBUG(0, ("error opening config file\n"));
992 if (!secrets_init()) {
994 DEBUG(0,("Could not initialize domain trust account secrets. Giving up\n"));
998 /* Enable netbios namecache */
1002 /* Check winbindd parameters are valid */
1004 ZERO_STRUCT(server_state
);
1006 /* Winbind daemon initialisation */
1008 if ( (!winbindd_param_init()) || (!winbindd_upgrade_idmap()) ||
1009 (!idmap_init(lp_idmap_backend())) ) {
1010 DEBUG(1, ("Could not init idmap -- netlogon proxy only\n"));
1011 idmap_set_proxyonly();
1014 /* Unblock all signals we are interested in as they may have been
1015 blocked by the parent process. */
1017 BlockSignals(False
, SIGINT
);
1018 BlockSignals(False
, SIGQUIT
);
1019 BlockSignals(False
, SIGTERM
);
1020 BlockSignals(False
, SIGUSR1
);
1021 BlockSignals(False
, SIGUSR2
);
1022 BlockSignals(False
, SIGHUP
);
1023 BlockSignals(False
, SIGCHLD
);
1025 /* Setup signal handlers */
1027 CatchSignal(SIGINT
, termination_handler
); /* Exit on these sigs */
1028 CatchSignal(SIGQUIT
, termination_handler
);
1029 CatchSignal(SIGTERM
, termination_handler
);
1030 CatchSignal(SIGCHLD
, sigchld_handler
);
1032 CatchSignal(SIGPIPE
, SIG_IGN
); /* Ignore sigpipe */
1034 CatchSignal(SIGUSR2
, sigusr2_handler
); /* Debugging sigs */
1035 CatchSignal(SIGHUP
, sighup_handler
);
1038 become_daemon(Fork
);
1040 pidfile_create("winbindd");
1044 * If we're interactive we want to set our own process group for
1045 * signal management.
1048 setpgid( (pid_t
)0, (pid_t
)0);
1051 if (opt_dual_daemon
) {
1055 /* Initialise messaging system */
1057 if (!message_init()) {
1058 DEBUG(0, ("unable to initialise messaging system\n"));
1062 /* React on 'smbcontrol winbindd reload-config' in the same way
1063 as to SIGHUP signal */
1064 message_register(MSG_SMB_CONF_UPDATED
, msg_reload_services
);
1065 message_register(MSG_SHUTDOWN
, msg_shutdown
);
1067 poptFreeContext(pc
);
1069 netsamlogon_cache_init(); /* Non-critical */
1075 /* Loop waiting for requests */
1080 trustdom_cache_shutdown();