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)
39 extern int DEBUGLEVEL;
41 extern pstring user_socket_options;
44 extern int dcelogin_atmost_once;
48 extern fstring remote_machine;
49 extern pstring OriginalDir;
50 extern pstring myhostname;
52 /****************************************************************************
53 when exiting, take the whole family
54 ****************************************************************************/
55 static void *dflt_sig(void)
57 exit_server("caught signal");
61 /****************************************************************************
62 Send a SIGTERM to our process group.
63 *****************************************************************************/
64 static void killkids(void)
66 if(am_parent) kill(0,SIGTERM);
70 /****************************************************************************
71 open the socket communication
72 ****************************************************************************/
73 static BOOL open_sockets_inetd(void)
77 /* Started from inetd. fd 0 is the socket. */
78 /* We will abort gracefully when the client or remote system
82 /* close our standard file descriptors */
85 set_socket_options(Client,"SO_KEEPALIVE");
86 set_socket_options(Client,user_socket_options);
92 /****************************************************************************
93 open the socket communication
94 ****************************************************************************/
95 static BOOL open_sockets(BOOL is_daemon,int port)
98 int num_interfaces = iface_count();
99 int fd_listenset[FD_SETSIZE];
105 return open_sockets_inetd();
111 static int atexit_set;
112 if(atexit_set == 0) {
123 FD_ZERO(&listen_set);
125 if(lp_interfaces() && lp_bind_interfaces_only()) {
126 /* We have been given an interfaces line, and been
127 told to only bind to those interfaces. Create a
128 socket per interface and bind to only these.
131 if(num_interfaces > FD_SETSIZE) {
132 DEBUG(0,("open_sockets: Too many interfaces specified to bind to. Number was %d \
134 num_interfaces, FD_SETSIZE));
138 /* Now open a listen socket for each of the
140 for(i = 0; i < num_interfaces; i++) {
141 struct in_addr *ifip = iface_n_ip(i);
144 DEBUG(0,("open_sockets: interface %d has NULL IP address !\n", i));
147 s = fd_listenset[i] = open_socket_in(SOCK_STREAM, port, 0, ifip->s_addr);
150 /* ready to listen */
151 if (listen(s, 5) == -1) {
152 DEBUG(0,("listen: %s\n",strerror(errno)));
156 FD_SET(s,&listen_set);
159 /* Just bind to 0.0.0.0 - accept connections
163 /* open an incoming socket */
164 s = open_socket_in(SOCK_STREAM, port, 0,
165 interpret_addr(lp_socket_address()));
169 /* ready to listen */
170 if (listen(s, 5) == -1) {
171 DEBUG(0,("open_sockets: listen: %s\n",
178 FD_SET(s,&listen_set);
181 /* now accept incoming connections - forking a new process
182 for each incoming connection */
183 DEBUG(2,("waiting for a connection\n"));
188 memcpy((char *)&lfds, (char *)&listen_set,
191 num = sys_select(256,&lfds,NULL);
193 if (num == -1 && errno == EINTR)
196 /* Find the sockets that are read-ready -
198 for( ; num > 0; num--) {
199 struct sockaddr addr;
200 int in_addrlen = sizeof(addr);
203 for(i = 0; i < num_interfaces; i++) {
204 if(FD_ISSET(fd_listenset[i],&lfds)) {
206 /* Clear this so we don't look
208 FD_CLR(fd_listenset[i],&lfds);
213 Client = accept(s,&addr,&in_addrlen);
215 if (Client == -1 && errno == EINTR)
219 DEBUG(0,("open_sockets: accept: %s\n",
224 if (Client != -1 && fork()==0) {
227 /* close the listening socket(s) */
228 for(i = 0; i < num_interfaces; i++)
229 close(fd_listenset[i]);
231 /* close our standard file
236 set_socket_options(Client,"SO_KEEPALIVE");
237 set_socket_options(Client,user_socket_options);
239 /* Reset global variables in util.c so
240 that client substitutions will be
241 done correctly in the process. */
242 reset_globals_after_fork();
245 * Ensure this child has kernel oplock
246 * capabilities, but not it's children.
248 set_process_capability(KERNEL_OPLOCK_CAPABILITY, True);
249 set_inherited_process_capability(KERNEL_OPLOCK_CAPABILITY, False);
253 /* The parent doesn't need this socket */
256 /* Force parent to check log size after
257 * spawning child. Fix from
258 * klausr@ITAP.Physik.Uni-Stuttgart.De. The
259 * parent smbd will log to logserver.smb. It
260 * writes only two messages for each child
261 * started/finished. But each child writes,
262 * say, 50 messages also in logserver.smb,
263 * begining with the debug_count of the
264 * parent, before the child opens its own log
265 * file logserver.client. In a worst case
266 * scenario the size of logserver.smb would be
267 * checked after about 50*50=2500 messages
270 force_check_log_size();
275 /* NOTREACHED return True; */
278 /****************************************************************************
279 reload the services file
280 **************************************************************************/
281 BOOL reload_services(BOOL test)
287 pstrcpy(fname,lp_configfile());
288 if (file_exist(fname,NULL) && !strcsequal(fname,servicesf)) {
289 pstrcpy(servicesf,fname);
296 if (test && !lp_file_list_changed())
299 lp_killunused(conn_snum_used);
301 ret = lp_load(servicesf,False,False,True);
305 /* perhaps the config filename is now set */
307 reload_services(True);
316 set_socket_options(Client,"SO_KEEPALIVE");
317 set_socket_options(Client,user_socket_options);
321 reset_mangled_cache();
323 /* this forces service parameters to be flushed */
324 become_service(NULL,True);
331 /****************************************************************************
332 this prevents zombie child processes
333 ****************************************************************************/
334 BOOL reload_after_sighup = False;
336 static void sig_hup(int sig)
338 BlockSignals(True,SIGHUP);
339 DEBUG(0,("Got SIGHUP\n"));
342 * Fix from <branko.cibej@hermes.si> here.
343 * We used to reload in the signal handler - this
347 reload_after_sighup = True;
348 BlockSignals(False,SIGHUP);
354 /*******************************************************************
355 prepare to dump a core file - carefully!
356 ********************************************************************/
357 static BOOL dump_core(void)
361 pstrcpy(dname,debugf);
362 if ((p=strrchr(dname,'/'))) *p=0;
363 pstrcat(dname,"/corefiles");
365 sys_chown(dname,getuid(),getgid());
367 if (chdir(dname)) return(False);
370 #ifdef HAVE_GETRLIMIT
374 getrlimit(RLIMIT_CORE, &rlp);
375 rlp.rlim_cur = MAX(4*1024*1024,rlp.rlim_cur);
376 setrlimit(RLIMIT_CORE, &rlp);
377 getrlimit(RLIMIT_CORE, &rlp);
378 DEBUG(3,("Core limits now %d %d\n",
379 (int)rlp.rlim_cur,(int)rlp.rlim_max));
385 DEBUG(0,("Dumping core in %s\n",dname));
392 /****************************************************************************
394 ****************************************************************************/
395 void exit_server(char *reason)
397 static int firsttime=1;
398 extern char *last_inbuf;
401 if (!firsttime) exit(0);
405 DEBUG(2,("Closing connections\n"));
410 if (dcelogin_atmost_once) {
416 int oldlevel = DEBUGLEVEL;
418 DEBUG(0,("Last message was %s\n",smb_fn_name(last_message)));
420 show_msg(last_inbuf);
421 DEBUGLEVEL = oldlevel;
422 DEBUG(0,("===============================================================\n"));
424 if (dump_core()) return;
430 DEBUG(3,("Server exit (%s)\n", (reason ? reason : "")));
436 /****************************************************************************
437 initialise connect, service and file structs
438 ****************************************************************************/
439 static void init_structs(void )
441 get_myname(myhostname,NULL);
444 * Set the machine NETBIOS name if not already
445 * set from the config file.
448 if (!*global_myname) {
450 fstrcpy( global_myname, myhostname );
451 p = strchr( global_myname, '.' );
456 strupper( global_myname );
465 /* for LSA handles */
466 init_lsa_policy_hnd();
471 /****************************************************************************
473 ****************************************************************************/
474 static void usage(char *pname)
476 DEBUG(0,("Incorrect program usage - are you sure the command line is correct?\n"));
478 printf("Usage: %s [-D] [-p port] [-d debuglevel] ", pname);
479 printf("[-l log basename] [-s services file]\n" );
480 printf("Version %s\n",VERSION);
481 printf("\t-D become a daemon\n");
482 printf("\t-p port listen on the specified port\n");
483 printf("\t-d debuglevel set the debuglevel\n");
484 printf("\t-l log basename. Basename for log/debug files\n");
485 printf("\t-s services file. Filename of services file\n");
486 printf("\t-P passive only\n");
487 printf("\t-a append to log file (default)\n");
488 printf("\t-o overwrite log file, don't append\n");
493 /****************************************************************************
495 ****************************************************************************/
496 int main(int argc,char *argv[])
498 extern BOOL append_log;
499 /* shall I run as a daemon */
500 BOOL is_daemon = False;
505 #ifdef HAVE_SET_AUTH_PARAMETERS
506 set_auth_parameters(argc,argv);
510 /* needed for SecureWare on SCO */
518 pstrcpy(debugf,SMBLOGFILE);
520 pstrcpy(remote_machine, "smb");
522 setup_logging(argv[0],False);
524 charset_initialise();
526 /* make absolutely sure we run as root - to handle cases where people
527 are crazy enough to have it setuid */
528 #ifdef HAVE_SETRESUID
537 fault_setup((void (*)(void *))exit_server);
538 CatchSignal(SIGTERM , SIGNAL_CAST dflt_sig);
540 /* we are never interested in SIGPIPE */
541 BlockSignals(True,SIGPIPE);
543 /* we want total control over the permissions on created files,
544 so set our umask to 0 */
551 /* this is for people who can't start the program correctly */
552 while (argc > 1 && (*argv[1] != '-')) {
557 while ( EOF != (opt = getopt(argc, argv, "O:i:l:s:d:Dp:h?Paof:")) )
560 pstrcpy(user_socket_options,optarg);
564 pstrcpy(scope,optarg);
575 pstrcpy(servicesf,optarg);
579 pstrcpy(debugf,optarg);
598 DEBUGLEVEL = atoi(optarg);
618 DEBUG(1,( "smbd version %s started.\n", VERSION));
619 DEBUGADD(1,( "Copyright Andrew Tridgell 1992-1998\n"));
621 DEBUG(2,("uid=%d gid=%d euid=%d egid=%d\n",
622 (int)getuid(),(int)getgid(),(int)geteuid(),(int)getegid()));
624 if (sizeof(uint16) < 2 || sizeof(uint32) < 4) {
625 DEBUG(0,("ERROR: Samba is not configured correctly for the word size on your machine\n"));
629 if (!reload_services(False))
636 extern BOOL sslEnabled;
637 sslEnabled = lp_ssl_enabled();
641 #endif /* WITH_SSL */
643 codepage_initialise(lp_client_code_page());
645 pstrcpy(global_myworkgroup, lp_workgroup());
647 if(!pdb_generate_sam_sid()) {
648 DEBUG(0,("ERROR: Samba cannot create a SAM SID.\n"));
652 CatchSignal(SIGHUP,SIGNAL_CAST sig_hup);
654 /* Setup the signals that allow the debug log level
655 to by dynamically changed. */
657 /* If we are using the malloc debug code we can't use
658 SIGUSR1 and SIGUSR2 to do debug level changes. */
662 CatchSignal( SIGUSR1, SIGNAL_CAST sig_usr1 );
666 CatchSignal( SIGUSR2, SIGNAL_CAST sig_usr2 );
670 DEBUG(3,( "loaded services\n"));
672 if (!is_daemon && !is_a_socket(0)) {
673 DEBUG(0,("standard input is not a socket, assuming -D option\n"));
678 DEBUG( 3, ( "Becoming a daemon.\n" ) );
682 check_kernel_oplocks();
684 if (!directory_exist(lp_lockdir(), NULL)) {
685 mkdir(lp_lockdir(), 0755);
689 pidfile_create("smbd");
692 if (!open_sockets(is_daemon,port))
695 if (!locking_init(0))
698 if(!initialize_password_db())
701 /* possibly reload the services file. */
702 reload_services(True);
705 if (sys_chroot(lp_rootdir()) == 0)
706 DEBUG(2,("Changed root to %s\n", lp_rootdir()));
709 /* Setup the oplock IPC socket. */
710 if( !open_oplock_ipc() )
716 exit_server("normal exit");