2 Unix SMB/Netbios implementation.
4 Main SMB server routines
5 Copyright (C) Andrew Tridgell 1992-1998
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 pstring servicesf
= CONFIGFILE
;
25 extern pstring debugf
;
26 extern fstring global_myworkgroup
;
27 extern pstring global_myname
;
31 /* the last message the was processed */
32 int last_message
= -1;
34 /* a useful macro to debug the last message processed */
35 #define LAST_MESSAGE() smb_fn_name(last_message)
37 extern pstring user_socket_options
;
40 extern int dcelogin_atmost_once
;
43 extern fstring remote_machine
;
45 /* really we should have a top level context structure that has the
46 client file descriptor as an element. That would require a major rewrite :(
48 the following 2 functions are an alternative - they make the file
49 descriptor private to smbd
51 static int server_fd
= -1;
53 int smbd_server_fd(void)
58 void smbd_set_server_fd(int fd
)
64 /****************************************************************************
65 when exiting, take the whole family
66 ****************************************************************************/
67 static void *dflt_sig(void)
69 exit_server("caught signal");
73 /****************************************************************************
74 Send a SIGTERM to our process group.
75 *****************************************************************************/
76 static void killkids(void)
78 if(am_parent
) kill(0,SIGTERM
);
81 /****************************************************************************
82 process a sam sync message - not sure whether to do this here or
84 ****************************************************************************/
85 static void msg_sam_sync(int msg_type
, pid_t pid
, void *buf
, size_t len
)
87 DEBUG(10, ("** sam sync message received, ignoring\n"));
90 /****************************************************************************
91 process a sam sync replicate message - not sure whether to do this here or
93 ****************************************************************************/
94 static void msg_sam_repl(int msg_type
, pid_t pid
, void *buf
, size_t len
)
98 if (len
!= sizeof(uint32
))
101 low_serial
= *((uint32
*)buf
);
103 DEBUG(3, ("received sam replication message, serial = 0x%04x\n",
107 /****************************************************************************
108 open the socket communication
109 ****************************************************************************/
110 static BOOL
open_sockets_inetd(void)
112 /* Started from inetd. fd 0 is the socket. */
113 /* We will abort gracefully when the client or remote system
115 smbd_set_server_fd(dup(0));
117 /* close our standard file descriptors */
120 set_socket_options(smbd_server_fd(),"SO_KEEPALIVE");
121 set_socket_options(smbd_server_fd(),user_socket_options
);
127 /****************************************************************************
128 open the socket communication
129 ****************************************************************************/
130 static BOOL
open_sockets(BOOL is_daemon
,int port
)
132 int num_interfaces
= iface_count();
133 int fd_listenset
[FD_SETSIZE
];
139 return open_sockets_inetd();
145 static int atexit_set
;
146 if(atexit_set
== 0) {
157 FD_ZERO(&listen_set
);
159 if(lp_interfaces() && lp_bind_interfaces_only()) {
160 /* We have been given an interfaces line, and been
161 told to only bind to those interfaces. Create a
162 socket per interface and bind to only these.
165 if(num_interfaces
> FD_SETSIZE
) {
166 DEBUG(0,("open_sockets: Too many interfaces specified to bind to. Number was %d \
168 num_interfaces
, FD_SETSIZE
));
172 /* Now open a listen socket for each of the
174 for(i
= 0; i
< num_interfaces
; i
++) {
175 struct in_addr
*ifip
= iface_n_ip(i
);
178 DEBUG(0,("open_sockets: interface %d has NULL IP address !\n", i
));
181 s
= fd_listenset
[i
] = open_socket_in(SOCK_STREAM
, port
, 0, ifip
->s_addr
, True
);
185 /* ready to listen */
186 set_socket_options(s
,"SO_KEEPALIVE");
187 set_socket_options(s
,user_socket_options
);
189 if (listen(s
, 5) == -1) {
190 DEBUG(0,("listen: %s\n",strerror(errno
)));
194 FD_SET(s
,&listen_set
);
197 /* Just bind to 0.0.0.0 - accept connections
201 /* open an incoming socket */
202 s
= open_socket_in(SOCK_STREAM
, port
, 0,
203 interpret_addr(lp_socket_address()),True
);
207 /* ready to listen */
208 set_socket_options(s
,"SO_KEEPALIVE");
209 set_socket_options(s
,user_socket_options
);
211 if (listen(s
, 5) == -1) {
212 DEBUG(0,("open_sockets: listen: %s\n",
219 FD_SET(s
,&listen_set
);
222 /* Listen to messages */
224 message_register(MSG_SMB_SAM_SYNC
, msg_sam_sync
);
225 message_register(MSG_SMB_SAM_REPL
, msg_sam_repl
);
227 /* now accept incoming connections - forking a new process
228 for each incoming connection */
229 DEBUG(2,("waiting for a connection\n"));
234 /* Free up temporary memory from the main smbd. */
237 /* Ensure we respond to PING and DEBUG messages from the main smbd. */
240 memcpy((char *)&lfds
, (char *)&listen_set
,
243 num
= sys_select(FD_SETSIZE
,&lfds
,NULL
);
245 if (num
== -1 && errno
== EINTR
) {
246 extern VOLATILE
sig_atomic_t reload_after_sighup
;
248 /* check for sighup processing */
249 if (reload_after_sighup
) {
250 change_to_root_user();
251 DEBUG(1,("Reloading services after SIGHUP\n"));
252 reload_services(False
);
253 reload_after_sighup
= False
;
259 /* check if we need to reload services */
260 check_reload(time(NULL
));
262 /* Find the sockets that are read-ready -
264 for( ; num
> 0; num
--) {
265 struct sockaddr addr
;
266 socklen_t in_addrlen
= sizeof(addr
);
269 for(i
= 0; i
< num_interfaces
; i
++) {
270 if(FD_ISSET(fd_listenset
[i
],&lfds
)) {
272 /* Clear this so we don't look
274 FD_CLR(fd_listenset
[i
],&lfds
);
279 smbd_set_server_fd(accept(s
,&addr
,&in_addrlen
));
281 if (smbd_server_fd() == -1 && errno
== EINTR
)
284 if (smbd_server_fd() == -1) {
285 DEBUG(0,("open_sockets: accept: %s\n",
290 if (smbd_server_fd() != -1 && sys_fork()==0) {
293 /* close the listening socket(s) */
294 for(i
= 0; i
< num_interfaces
; i
++)
295 close(fd_listenset
[i
]);
297 /* close our standard file
302 set_socket_options(smbd_server_fd(),"SO_KEEPALIVE");
303 set_socket_options(smbd_server_fd(),user_socket_options
);
305 /* Reset global variables in util.c so
306 that client substitutions will be
307 done correctly in the process. */
308 reset_globals_after_fork();
310 /* tdb needs special fork handling */
315 /* The parent doesn't need this socket */
316 close(smbd_server_fd());
318 /* Sun May 6 18:56:14 2001 ackley@cs.unm.edu:
319 Clear the closed fd info out of server_fd --
320 and more importantly, out of client_fd in
321 util_sock.c, to avoid a possible
322 getpeername failure if we reopen the logs
323 and use %I in the filename.
326 smbd_set_server_fd(-1);
328 /* Force parent to check log size after
329 * spawning child. Fix from
330 * klausr@ITAP.Physik.Uni-Stuttgart.De. The
331 * parent smbd will log to logserver.smb. It
332 * writes only two messages for each child
333 * started/finished. But each child writes,
334 * say, 50 messages also in logserver.smb,
335 * begining with the debug_count of the
336 * parent, before the child opens its own log
337 * file logserver.client. In a worst case
338 * scenario the size of logserver.smb would be
339 * checked after about 50*50=2500 messages
342 force_check_log_size();
347 /* NOTREACHED return True; */
350 /****************************************************************************
351 reload the services file
352 **************************************************************************/
353 BOOL
reload_services(BOOL test
)
359 pstrcpy(fname
,lp_configfile());
360 if (file_exist(fname
,NULL
) && !strcsequal(fname
,servicesf
)) {
361 pstrcpy(servicesf
,fname
);
368 if (test
&& !lp_file_list_changed())
371 lp_killunused(conn_snum_used
);
373 ret
= lp_load(servicesf
,False
,False
,True
);
377 /* perhaps the config filename is now set */
379 reload_services(True
);
386 if (smbd_server_fd() != -1) {
387 set_socket_options(smbd_server_fd(),"SO_KEEPALIVE");
388 set_socket_options(smbd_server_fd(),user_socket_options
);
392 reset_mangled_cache();
395 /* this forces service parameters to be flushed */
396 set_current_service(NULL
,True
);
403 /****************************************************************************
405 ****************************************************************************/
407 VOLATILE
sig_atomic_t reload_after_sighup
= False
;
409 static void sig_hup(int sig
)
411 BlockSignals(True
,SIGHUP
);
412 DEBUG(0,("Got SIGHUP\n"));
415 reload_after_sighup
= True
;
416 BlockSignals(False
,SIGHUP
);
422 /*******************************************************************
423 prepare to dump a core file - carefully!
424 ********************************************************************/
425 static BOOL
dump_core(void)
429 pstrcpy(dname
,debugf
);
430 if ((p
=strrchr_m(dname
,'/'))) *p
=0;
431 pstrcat(dname
,"/corefiles");
433 sys_chown(dname
,getuid(),getgid());
435 if (chdir(dname
)) return(False
);
438 #ifdef HAVE_GETRLIMIT
442 getrlimit(RLIMIT_CORE
, &rlp
);
443 rlp
.rlim_cur
= MAX(4*1024*1024,rlp
.rlim_cur
);
444 setrlimit(RLIMIT_CORE
, &rlp
);
445 getrlimit(RLIMIT_CORE
, &rlp
);
446 DEBUG(3,("Core limits now %d %d\n",
447 (int)rlp
.rlim_cur
,(int)rlp
.rlim_max
));
453 DEBUG(0,("Dumping core in %s\n",dname
));
459 /****************************************************************************
460 update the current smbd process count
461 ****************************************************************************/
463 static void decrement_smbd_process_count(void)
467 if (lp_max_smbd_processes()) {
469 tdb_change_int_atomic(conn_tdb_ctx(), "INFO/total_smbds", &total_smbds
, -1);
473 /****************************************************************************
475 ****************************************************************************/
476 void exit_server(char *reason
)
478 static int firsttime
=1;
479 extern char *last_inbuf
;
486 change_to_root_user();
487 DEBUG(2,("Closing connections\n"));
491 invalidate_all_vuids();
493 /* delete our entry in the connections database. */
494 yield_connection(NULL
,"",MAXSTATUS
);
496 respond_to_all_remaining_local_messages();
497 decrement_smbd_process_count();
500 if (dcelogin_atmost_once
) {
506 int oldlevel
= DEBUGLEVEL
;
508 DEBUG(0,("Last message was %s\n",smb_fn_name(last_message
)));
510 show_msg(last_inbuf
);
511 DEBUGLEVEL
= oldlevel
;
512 DEBUG(0,("===============================================================\n"));
514 if (dump_core()) return;
520 DEBUG(3,("Server exit (%s)\n", (reason
? reason
: "")));
524 /****************************************************************************
525 initialise connect, service and file structs
526 ****************************************************************************/
527 static void init_structs(void )
530 * Set the machine NETBIOS name if not already
531 * set from the config file.
534 if (!*global_myname
) {
536 fstrcpy( global_myname
, myhostname() );
537 p
= strchr_m( global_myname
, '.' );
542 strupper( global_myname
);
556 /****************************************************************************
558 ****************************************************************************/
559 static void usage(char *pname
)
562 d_printf("Usage: %s [-DaoPh?Vb] [-d debuglevel] [-l log basename] [-p port]\n", pname
);
563 d_printf(" [-O socket options] [-s services file]\n");
564 d_printf("\t-D Become a daemon\n");
565 d_printf("\t-a Append to log file (default)\n");
566 d_printf("\t-o Overwrite log file, don't append\n");
567 d_printf("\t-h Print usage\n");
568 d_printf("\t-? Print usage\n");
569 d_printf("\t-V Print version\n");
570 d_printf("\t-b Print build options\n");
571 d_printf("\t-d debuglevel Set the debuglevel\n");
572 d_printf("\t-l log basename. Basename for log/debug files\n");
573 d_printf("\t-p port Listen on the specified port\n");
574 d_printf("\t-O socket options Socket options\n");
575 d_printf("\t-s services file. Filename of services file\n");
579 /****************************************************************************
581 ****************************************************************************/
582 int main(int argc
,char *argv
[])
584 extern BOOL append_log
;
585 /* shall I run as a daemon */
586 BOOL is_daemon
= False
;
587 BOOL specified_logfile
= False
;
592 #ifdef HAVE_SET_AUTH_PARAMETERS
593 set_auth_parameters(argc
,argv
);
596 /* this is for people who can't start the program correctly */
597 while (argc
> 1 && (*argv
[1] != '-')) {
602 while ( EOF
!= (opt
= getopt(argc
, argv
, "O:l:s:d:Dp:h?bVaof:")) )
605 pstrcpy(user_socket_options
,optarg
);
609 pstrcpy(servicesf
,optarg
);
613 specified_logfile
= True
;
614 slprintf(debugf
, sizeof(debugf
)-1, "%s/log.smbd", optarg
);
633 DEBUGLEVEL
= atoi(optarg
);
647 d_printf("Version %s\n",VERSION
);
651 build_options(True
); /* Display output to screen as well as debug */
655 DEBUG(0,("Incorrect program usage - are you sure the command line is correct?\n"));
661 /* needed for SecureWare on SCO */
673 if(!specified_logfile
) {
674 slprintf(debugf
, sizeof(debugf
)-1, "%s/log.smbd", LOGFILEBASE
);
677 pstrcpy(remote_machine
, "smbd");
679 setup_logging(argv
[0],False
);
681 /* we want to re-seed early to prevent time delays causing
682 client problems at a later date. (tridge) */
683 generate_random_buffer(NULL
, 0, False
);
685 /* make absolutely sure we run as root - to handle cases where people
686 are crazy enough to have it setuid */
688 gain_root_privilege();
689 gain_root_group_privilege();
691 fault_setup((void (*)(void *))exit_server
);
692 CatchSignal(SIGTERM
, SIGNAL_CAST dflt_sig
);
694 /* we are never interested in SIGPIPE */
695 BlockSignals(True
,SIGPIPE
);
698 /* we are never interested in SIGFPE */
699 BlockSignals(True
,SIGFPE
);
703 /* We are no longer interested in USR2 */
704 BlockSignals(True
,SIGUSR2
);
707 /* POSIX demands that signals are inherited. If the invoking process has
708 * these signals masked, we will have problems, as we won't recieve them. */
709 BlockSignals(False
, SIGHUP
);
710 BlockSignals(False
, SIGUSR1
);
712 /* we want total control over the permissions on created files,
713 so set our umask to 0 */
720 DEBUG(1,( "smbd version %s started.\n", VERSION
));
721 DEBUGADD(1,( "Copyright Andrew Tridgell 1992-1998\n"));
723 DEBUG(2,("uid=%d gid=%d euid=%d egid=%d\n",
724 (int)getuid(),(int)getgid(),(int)geteuid(),(int)getegid()));
726 /* Output the build options to the debug log */
727 build_options(False
);
729 if (sizeof(uint16
) < 2 || sizeof(uint32
) < 4) {
730 DEBUG(0,("ERROR: Samba is not configured correctly for the word size on your machine\n"));
735 * Do this before reload_services.
738 if (!reload_services(False
))
743 /* don't call winbind for our domain if we are the DC */
744 if (lp_domain_logons()) {
745 winbind_exclude_domain(lp_workgroup());
749 if (!profile_setup(False
)) {
750 DEBUG(0,("ERROR: failed to setup profiling\n"));
757 extern BOOL sslEnabled
;
758 sslEnabled
= lp_ssl_enabled();
762 #endif /* WITH_SSL */
764 fstrcpy(global_myworkgroup
, lp_workgroup());
766 CatchSignal(SIGHUP
,SIGNAL_CAST sig_hup
);
768 DEBUG(3,( "loaded services\n"));
770 if (!is_daemon
&& !is_a_socket(0)) {
771 DEBUG(0,("standard input is not a socket, assuming -D option\n"));
776 DEBUG( 3, ( "Becoming a daemon.\n" ) );
780 if (!directory_exist(lp_lockdir(), NULL
)) {
781 mkdir(lp_lockdir(), 0755);
785 pidfile_create("smbd");
788 if (!message_init()) {
792 /* Setup the main smbd so that we can get messages. */
793 claim_connection(NULL
,"",MAXSTATUS
,True
);
796 DO NOT ENABLE THIS TILL YOU COPE WITH KILLING THESE TASKS AND INETD
797 THIS *killed* LOTS OF BUILD FARM MACHINES. IT CREATED HUNDREDS OF
798 smbd PROCESSES THAT NEVER DIE
799 start_background_queue();
802 if (!open_sockets(is_daemon
,port
))
806 * everything after this point is run after the fork()
809 if (!locking_init(0)) {
813 if (!print_backend_init()) {
817 if (!share_info_db_init()) {
821 if(!initialize_password_db(False
)) {
825 /* possibly reload the services file. */
826 reload_services(True
);
828 if (init_group_mapping()==False
) {
829 printf("Could not open tdb mapping file.\n");
833 if(!pdb_generate_sam_sid()) {
834 DEBUG(0,("ERROR: Samba cannot create a SAM SID.\n"));
839 if (sys_chroot(lp_rootdir()) == 0)
840 DEBUG(2,("Changed root to %s\n", lp_rootdir()));
844 if (!init_oplocks()) {
848 /* Setup change notify */
849 if (!init_change_notify()) {
855 exit_server("normal exit");