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.
25 pstring servicesf
= CONFIGFILE
;
26 extern pstring debugf
;
27 extern fstring global_myworkgroup
;
28 extern pstring global_myname
;
32 /* the last message the was processed */
33 int last_message
= -1;
35 /* a useful macro to debug the last message processed */
36 #define LAST_MESSAGE() smb_fn_name(last_message)
38 extern int DEBUGLEVEL
;
40 extern pstring user_socket_options
;
43 extern int dcelogin_atmost_once
;
47 extern fstring remote_machine
;
48 extern pstring OriginalDir
;
50 /****************************************************************************
51 when exiting, take the whole family
52 ****************************************************************************/
53 static void *dflt_sig(void)
55 exit_server("caught signal");
59 /****************************************************************************
60 Send a SIGTERM to our process group.
61 *****************************************************************************/
62 static void killkids(void)
64 if(am_parent
) kill(0,SIGTERM
);
68 /****************************************************************************
69 open the socket communication
70 ****************************************************************************/
71 static BOOL
open_sockets_inetd(void)
75 /* Started from inetd. fd 0 is the socket. */
76 /* We will abort gracefully when the client or remote system
80 /* close our standard file descriptors */
83 set_socket_options(Client
,"SO_KEEPALIVE");
84 set_socket_options(Client
,user_socket_options
);
90 /****************************************************************************
91 open the socket communication
92 ****************************************************************************/
93 static BOOL
open_sockets(BOOL is_daemon
,int port
)
96 int num_interfaces
= iface_count();
97 int fd_listenset
[FD_SETSIZE
];
103 return open_sockets_inetd();
109 static int atexit_set
;
110 if(atexit_set
== 0) {
121 FD_ZERO(&listen_set
);
123 if(lp_interfaces() && lp_bind_interfaces_only()) {
124 /* We have been given an interfaces line, and been
125 told to only bind to those interfaces. Create a
126 socket per interface and bind to only these.
129 if(num_interfaces
> FD_SETSIZE
) {
130 DEBUG(0,("open_sockets: Too many interfaces specified to bind to. Number was %d \
132 num_interfaces
, FD_SETSIZE
));
136 /* Now open a listen socket for each of the
138 for(i
= 0; i
< num_interfaces
; i
++) {
139 struct in_addr
*ifip
= iface_n_ip(i
);
142 DEBUG(0,("open_sockets: interface %d has NULL IP address !\n", i
));
145 s
= fd_listenset
[i
] = open_socket_in(SOCK_STREAM
, port
, 0, ifip
->s_addr
, True
);
148 /* ready to listen */
149 if (listen(s
, 5) == -1) {
150 DEBUG(0,("listen: %s\n",strerror(errno
)));
154 FD_SET(s
,&listen_set
);
157 /* Just bind to 0.0.0.0 - accept connections
161 /* open an incoming socket */
162 s
= open_socket_in(SOCK_STREAM
, port
, 0,
163 interpret_addr(lp_socket_address()),True
);
167 /* ready to listen */
168 if (listen(s
, 5) == -1) {
169 DEBUG(0,("open_sockets: listen: %s\n",
176 FD_SET(s
,&listen_set
);
179 /* now accept incoming connections - forking a new process
180 for each incoming connection */
181 DEBUG(2,("waiting for a connection\n"));
186 memcpy((char *)&lfds
, (char *)&listen_set
,
189 num
= sys_select(FD_SETSIZE
,&lfds
,NULL
);
191 if (num
== -1 && errno
== EINTR
)
194 /* check if we need to reload services */
195 check_reload(time(NULL
));
197 /* Find the sockets that are read-ready -
199 for( ; num
> 0; num
--) {
200 struct sockaddr addr
;
201 int in_addrlen
= sizeof(addr
);
204 for(i
= 0; i
< num_interfaces
; i
++) {
205 if(FD_ISSET(fd_listenset
[i
],&lfds
)) {
207 /* Clear this so we don't look
209 FD_CLR(fd_listenset
[i
],&lfds
);
214 Client
= accept(s
,&addr
,&in_addrlen
);
216 if (Client
== -1 && errno
== EINTR
)
220 DEBUG(0,("open_sockets: accept: %s\n",
225 if (Client
!= -1 && fork()==0) {
228 /* close the listening socket(s) */
229 for(i
= 0; i
< num_interfaces
; i
++)
230 close(fd_listenset
[i
]);
232 /* close our standard file
237 set_socket_options(Client
,"SO_KEEPALIVE");
238 set_socket_options(Client
,user_socket_options
);
240 /* Reset global variables in util.c so
241 that client substitutions will be
242 done correctly in the process. */
243 reset_globals_after_fork();
246 * Ensure this child has kernel oplock
247 * capabilities, but not it's children.
249 set_process_capability(KERNEL_OPLOCK_CAPABILITY
, True
);
250 set_inherited_process_capability(KERNEL_OPLOCK_CAPABILITY
, False
);
254 /* The parent doesn't need this socket */
257 /* Sun May 6 18:56:14 2001 ackley@cs.unm.edu:
258 Clear the closed fd info out of Client in
259 util_sock.c to avoid a possible
260 getpeername failure if we reopen the logs
261 and use %I in the filename.
265 /* Force parent to check log size after
266 * spawning child. Fix from
267 * klausr@ITAP.Physik.Uni-Stuttgart.De. The
268 * parent smbd will log to logserver.smb. It
269 * writes only two messages for each child
270 * started/finished. But each child writes,
271 * say, 50 messages also in logserver.smb,
272 * begining with the debug_count of the
273 * parent, before the child opens its own log
274 * file logserver.client. In a worst case
275 * scenario the size of logserver.smb would be
276 * checked after about 50*50=2500 messages
279 force_check_log_size();
284 /* NOTREACHED return True; */
287 /****************************************************************************
288 reload the services file
289 **************************************************************************/
290 BOOL
reload_services(BOOL test
)
296 pstrcpy(fname
,lp_configfile());
297 if (file_exist(fname
,NULL
) && !strcsequal(fname
,servicesf
)) {
298 pstrcpy(servicesf
,fname
);
305 if (test
&& !lp_file_list_changed())
308 lp_killunused(conn_snum_used
);
310 ret
= lp_load(servicesf
,False
,False
,True
);
314 /* perhaps the config filename is now set */
316 reload_services(True
);
325 set_socket_options(Client
,"SO_KEEPALIVE");
326 set_socket_options(Client
,user_socket_options
);
330 reset_mangled_cache();
333 /* this forces service parameters to be flushed */
334 become_service(NULL
,True
);
341 /****************************************************************************
343 ****************************************************************************/
345 VOLATILE SIG_ATOMIC_T reload_after_sighup
= False
;
347 static void sig_hup(int sig
)
349 BlockSignals(True
,SIGHUP
);
350 DEBUG(1,("Got SIGHUP\n"));
353 * Fix from <branko.cibej@hermes.si> here.
354 * We used to reload in the signal handler - this
358 reload_after_sighup
= True
;
359 BlockSignals(False
,SIGHUP
);
365 /*******************************************************************
366 prepare to dump a core file - carefully!
367 ********************************************************************/
368 static BOOL
dump_core(void)
372 pstrcpy(dname
,debugf
);
373 if ((p
=strrchr(dname
,'/'))) *p
=0;
374 pstrcat(dname
,"/corefiles");
376 sys_chown(dname
,getuid(),getgid());
378 if (chdir(dname
)) return(False
);
381 #ifdef HAVE_GETRLIMIT
385 getrlimit(RLIMIT_CORE
, &rlp
);
386 rlp
.rlim_cur
= MAX(4*1024*1024,rlp
.rlim_cur
);
387 setrlimit(RLIMIT_CORE
, &rlp
);
388 getrlimit(RLIMIT_CORE
, &rlp
);
389 DEBUG(3,("Core limits now %d %d\n",
390 (int)rlp
.rlim_cur
,(int)rlp
.rlim_max
));
396 DEBUG(0,("Dumping core in %s\n",dname
));
403 /****************************************************************************
405 ****************************************************************************/
406 void exit_server(char *reason
)
408 static int firsttime
=1;
409 extern char *last_inbuf
;
412 if (!firsttime
) exit(0);
416 DEBUG(2,("Closing connections\n"));
420 respond_to_all_remaining_local_messages();
423 if (dcelogin_atmost_once
) {
429 int oldlevel
= DEBUGLEVEL
;
431 DEBUG(0,("Last message was %s\n",smb_fn_name(last_message
)));
433 show_msg(last_inbuf
);
434 DEBUGLEVEL
= oldlevel
;
435 DEBUG(0,("===============================================================\n"));
437 if (dump_core()) return;
443 DEBUG(3,("Server exit (%s)\n", (reason
? reason
: "")));
449 /****************************************************************************
450 initialise connect, service and file structs
451 ****************************************************************************/
452 static void init_structs(void )
455 * Set the machine NETBIOS name if not already
456 * set from the config file.
459 if (!*global_myname
) {
461 fstrcpy( global_myname
, myhostname() );
462 p
= strchr( global_myname
, '.' );
467 strupper( global_myname
);
476 /* for LSA handles */
477 init_lsa_policy_hnd();
482 /****************************************************************************
484 ****************************************************************************/
485 static void usage(char *pname
)
488 printf("Usage: %s [-DaoPh?V] [-d debuglevel] [-l log basename] [-p port]\n", pname
);
489 printf(" [-O socket options] [-s services file]\n");
490 printf("\t-D Become a daemon\n");
491 printf("\t-a Append to log file (default)\n");
492 printf("\t-o Overwrite log file, don't append\n");
493 printf("\t-P Passive only\n");
494 printf("\t-h Print usage\n");
495 printf("\t-? Print usage\n");
496 printf("\t-V Print version\n");
497 printf("\t-d debuglevel Set the debuglevel\n");
498 printf("\t-l log basename. Basename for log/debug files\n");
499 printf("\t-p port Listen on the specified port\n");
500 printf("\t-O socket options Socket options\n");
501 printf("\t-s services file. Filename of services file\n");
506 /****************************************************************************
508 ****************************************************************************/
509 int main(int argc
,char *argv
[])
511 extern BOOL append_log
;
512 /* shall I run as a daemon */
513 BOOL is_daemon
= False
;
514 BOOL specified_logfile
= False
;
519 #ifdef HAVE_SET_AUTH_PARAMETERS
520 set_auth_parameters(argc
,argv
);
523 /* this is for people who can't start the program correctly */
524 while (argc
> 1 && (*argv
[1] != '-')) {
529 while ( EOF
!= (opt
= getopt(argc
, argv
, "O:i:l:s:d:Dp:h?VPaof:")) )
532 pstrcpy(user_socket_options
,optarg
);
543 pstrcpy(servicesf
,optarg
);
547 specified_logfile
= True
;
548 pstrcpy(debugf
,optarg
);
567 DEBUGLEVEL
= atoi(optarg
);
581 printf("Version %s\n",VERSION
);
585 DEBUG(0,("Incorrect program usage - are you sure the command line is correct?\n"));
591 /* needed for SecureWare on SCO */
596 * gain_root_privilege uses an assert than will cause a core
597 * dump if euid != 0. Ensure this is the case.
600 if(geteuid() != (uid_t
)0) {
601 fprintf(stderr
, "%s: Version %s : Must have effective user id of zero to run.\n", argv
[0], VERSION
);
609 if(!specified_logfile
)
610 pstrcpy(debugf
,SMBLOGFILE
);
612 pstrcpy(remote_machine
, "smb");
614 setup_logging(argv
[0],False
);
616 charset_initialise();
618 /* we want to re-seed early to prevent time delays causing
619 client problems at a later date. (tridge) */
620 generate_random_buffer(NULL
, 0, False
);
622 /* make absolutely sure we run as root - to handle cases where people
623 are crazy enough to have it setuid */
625 gain_root_privilege();
626 gain_root_group_privilege();
628 fault_setup((void (*)(void *))exit_server
);
629 CatchSignal(SIGTERM
, SIGNAL_CAST dflt_sig
);
631 /* we are never interested in SIGPIPE */
632 BlockSignals(True
,SIGPIPE
);
635 /* we are never interested in SIGFPE */
636 BlockSignals(True
,SIGFPE
);
639 /* we want total control over the permissions on created files,
640 so set our umask to 0 */
643 dos_GetWd(OriginalDir
);
649 DEBUG(1,( "smbd version %s started.\n", VERSION
));
650 DEBUGADD(1,( "Copyright Andrew Tridgell 1992-1998\n"));
652 DEBUG(2,("uid=%d gid=%d euid=%d egid=%d\n",
653 (int)getuid(),(int)getgid(),(int)geteuid(),(int)getegid()));
655 if (sizeof(uint16
) < 2 || sizeof(uint32
) < 4) {
656 DEBUG(0,("ERROR: Samba is not configured correctly for the word size on your machine\n"));
661 * Do this before reload_services.
664 if (!reload_services(False
))
670 if (!profile_setup(False
)) {
671 DEBUG(0,("ERROR: failed to setup profiling\n"));
678 extern BOOL sslEnabled
;
679 sslEnabled
= lp_ssl_enabled();
683 #endif /* WITH_SSL */
685 codepage_initialise(lp_client_code_page());
687 fstrcpy(global_myworkgroup
, lp_workgroup());
689 if(!pdb_generate_sam_sid()) {
690 DEBUG(0,("ERROR: Samba cannot create a SAM SID.\n"));
694 CatchSignal(SIGHUP
,SIGNAL_CAST sig_hup
);
696 /* Setup the signals that allow the debug log level
697 to by dynamically changed. */
699 /* If we are using the malloc debug code we can't use
700 SIGUSR1 and SIGUSR2 to do debug level changes. */
704 CatchSignal( SIGUSR1
, SIGNAL_CAST sig_usr1
);
708 CatchSignal( SIGUSR2
, SIGNAL_CAST sig_usr2
);
712 DEBUG(3,( "loaded services\n"));
714 if (!is_daemon
&& !is_a_socket(0)) {
715 DEBUG(0,("standard input is not a socket, assuming -D option\n"));
720 DEBUG( 3, ( "Becoming a daemon.\n" ) );
724 check_kernel_oplocks();
726 if (!directory_exist(lp_lockdir(), NULL
)) {
727 mkdir(lp_lockdir(), 0755);
731 pidfile_create("smbd");
734 if (!open_sockets(is_daemon
,port
))
738 * Note that this call should be done after the fork() call
739 * in open_sockets(), as some versions of the locking shared
740 * memory code register openers in a flat file.
743 if (!locking_init(0))
746 if(!initialize_password_db())
749 /* possibly reload the services file. */
750 reload_services(True
);
753 if (sys_chroot(lp_rootdir()) == 0)
754 DEBUG(2,("Changed root to %s\n", lp_rootdir()));
757 /* Setup the oplock IPC socket. */
758 if( !open_oplock_ipc() )
764 exit_server("normal exit");