3 Unix SMB/Netbios implementation.
5 Main SMB server routines
6 Copyright (C) Andrew Tridgell 1992-1998
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 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;
51 /* really we should have a top level context structure that has the
52 client file descriptor as an element. That would require a major rewrite :(
54 the following 2 functions are an alternative - they make the file
55 descriptor private to smbd
57 static int server_fd = -1;
59 int smbd_server_fd(void)
64 void smbd_set_server_fd(int fd)
70 /****************************************************************************
71 when exiting, take the whole family
72 ****************************************************************************/
73 static void *dflt_sig(void)
75 exit_server("caught signal");
79 /****************************************************************************
80 Send a SIGTERM to our process group.
81 *****************************************************************************/
82 static void killkids(void)
84 if(am_parent) kill(0,SIGTERM);
88 /****************************************************************************
89 open the socket communication
90 ****************************************************************************/
91 static BOOL open_sockets_inetd(void)
93 /* Started from inetd. fd 0 is the socket. */
94 /* We will abort gracefully when the client or remote system
96 smbd_set_server_fd(dup(0));
98 /* close our standard file descriptors */
101 set_socket_options(smbd_server_fd(),"SO_KEEPALIVE");
102 set_socket_options(smbd_server_fd(),user_socket_options);
108 /****************************************************************************
109 open the socket communication
110 ****************************************************************************/
111 static BOOL open_sockets(BOOL is_daemon,int port)
113 int num_interfaces = iface_count();
114 int fd_listenset[FD_SETSIZE];
120 return open_sockets_inetd();
126 static int atexit_set;
127 if(atexit_set == 0) {
138 FD_ZERO(&listen_set);
140 if(lp_interfaces() && lp_bind_interfaces_only()) {
141 /* We have been given an interfaces line, and been
142 told to only bind to those interfaces. Create a
143 socket per interface and bind to only these.
146 if(num_interfaces > FD_SETSIZE) {
147 DEBUG(0,("open_sockets: Too many interfaces specified to bind to. Number was %d \
149 num_interfaces, FD_SETSIZE));
153 /* Now open a listen socket for each of the
155 for(i = 0; i < num_interfaces; i++) {
156 struct in_addr *ifip = iface_n_ip(i);
159 DEBUG(0,("open_sockets: interface %d has NULL IP address !\n", i));
162 s = fd_listenset[i] = open_socket_in(SOCK_STREAM, port, 0, ifip->s_addr, True);
165 /* ready to listen */
166 if (listen(s, 5) == -1) {
167 DEBUG(0,("listen: %s\n",strerror(errno)));
171 FD_SET(s,&listen_set);
174 /* Just bind to 0.0.0.0 - accept connections
178 /* open an incoming socket */
179 s = open_socket_in(SOCK_STREAM, port, 0,
180 interpret_addr(lp_socket_address()),True);
184 /* ready to listen */
185 if (listen(s, 5) == -1) {
186 DEBUG(0,("open_sockets: listen: %s\n",
193 FD_SET(s,&listen_set);
196 /* now accept incoming connections - forking a new process
197 for each incoming connection */
198 DEBUG(2,("waiting for a connection\n"));
203 memcpy((char *)&lfds, (char *)&listen_set,
206 num = sys_select(FD_SETSIZE,&lfds,NULL);
208 if (num == -1 && errno == EINTR)
211 /* check if we need to reload services */
212 check_reload(time(NULL));
214 /* Find the sockets that are read-ready -
216 for( ; num > 0; num--) {
217 struct sockaddr addr;
218 int in_addrlen = sizeof(addr);
221 for(i = 0; i < num_interfaces; i++) {
222 if(FD_ISSET(fd_listenset[i],&lfds)) {
224 /* Clear this so we don't look
226 FD_CLR(fd_listenset[i],&lfds);
231 smbd_set_server_fd(accept(s,&addr,&in_addrlen));
233 if (smbd_server_fd() == -1 && errno == EINTR)
236 if (smbd_server_fd() == -1) {
237 DEBUG(0,("open_sockets: accept: %s\n",
242 if (smbd_server_fd() != -1 && sys_fork()==0) {
245 /* close the listening socket(s) */
246 for(i = 0; i < num_interfaces; i++)
247 close(fd_listenset[i]);
249 /* close our standard file
254 set_socket_options(smbd_server_fd(),"SO_KEEPALIVE");
255 set_socket_options(smbd_server_fd(),user_socket_options);
257 /* Reset global variables in util.c so
258 that client substitutions will be
259 done correctly in the process. */
260 reset_globals_after_fork();
263 * Ensure this child has kernel oplock
264 * capabilities, but not it's children.
266 set_process_capability(KERNEL_OPLOCK_CAPABILITY, True);
267 set_inherited_process_capability(KERNEL_OPLOCK_CAPABILITY, False);
271 /* The parent doesn't need this socket */
272 close(smbd_server_fd());
274 /* Force parent to check log size after
275 * spawning child. Fix from
276 * klausr@ITAP.Physik.Uni-Stuttgart.De. The
277 * parent smbd will log to logserver.smb. It
278 * writes only two messages for each child
279 * started/finished. But each child writes,
280 * say, 50 messages also in logserver.smb,
281 * begining with the debug_count of the
282 * parent, before the child opens its own log
283 * file logserver.client. In a worst case
284 * scenario the size of logserver.smb would be
285 * checked after about 50*50=2500 messages
288 force_check_log_size();
293 /* NOTREACHED return True; */
296 /****************************************************************************
297 reload the services file
298 **************************************************************************/
299 BOOL reload_services(BOOL test)
305 pstrcpy(fname,lp_configfile());
306 if (file_exist(fname,NULL) && !strcsequal(fname,servicesf)) {
307 pstrcpy(servicesf,fname);
314 if (test && !lp_file_list_changed())
317 lp_killunused(conn_snum_used);
319 ret = lp_load(servicesf,False,False,True);
322 /* load the dfs maps of all the services having
324 we don't want to do this in lp_load because we want just the smbd
325 server to load up the dfs maps into msdfs.tdb. not nmbd, swat etc*/
331 /* perhaps the config filename is now set */
333 reload_services(True);
340 if (smbd_server_fd() != -1) {
341 set_socket_options(smbd_server_fd(),"SO_KEEPALIVE");
342 set_socket_options(smbd_server_fd(),user_socket_options);
346 reset_mangled_cache();
349 /* this forces service parameters to be flushed */
350 become_service(NULL,True);
357 /****************************************************************************
359 ****************************************************************************/
361 VOLATILE SIG_ATOMIC_T reload_after_sighup = False;
363 static void sig_hup(int sig)
365 BlockSignals(True,SIGHUP);
366 DEBUG(0,("Got SIGHUP\n"));
369 * Fix from <branko.cibej@hermes.si> here.
370 * We used to reload in the signal handler - this
374 reload_after_sighup = True;
375 BlockSignals(False,SIGHUP);
381 /*******************************************************************
382 prepare to dump a core file - carefully!
383 ********************************************************************/
384 static BOOL dump_core(void)
388 pstrcpy(dname,debugf);
389 if ((p=strrchr(dname,'/'))) *p=0;
390 pstrcat(dname,"/corefiles");
392 sys_chown(dname,getuid(),getgid());
394 if (chdir(dname)) return(False);
397 #ifdef HAVE_GETRLIMIT
401 getrlimit(RLIMIT_CORE, &rlp);
402 rlp.rlim_cur = MAX(4*1024*1024,rlp.rlim_cur);
403 setrlimit(RLIMIT_CORE, &rlp);
404 getrlimit(RLIMIT_CORE, &rlp);
405 DEBUG(3,("Core limits now %d %d\n",
406 (int)rlp.rlim_cur,(int)rlp.rlim_max));
412 DEBUG(0,("Dumping core in %s\n",dname));
419 /****************************************************************************
421 ****************************************************************************/
422 void exit_server(char *reason)
424 static int firsttime=1;
425 extern char *last_inbuf;
428 if (!firsttime) exit(0);
432 DEBUG(2,("Closing connections\n"));
436 respond_to_all_remaining_local_messages();
439 if (dcelogin_atmost_once) {
445 int oldlevel = DEBUGLEVEL;
447 DEBUG(0,("Last message was %s\n",smb_fn_name(last_message)));
449 show_msg(last_inbuf);
450 DEBUGLEVEL = oldlevel;
451 DEBUG(0,("===============================================================\n"));
453 if (dump_core()) return;
462 DEBUG(3,("Server exit (%s)\n", (reason ? reason : "")));
468 /****************************************************************************
469 initialise connect, service and file structs
470 ****************************************************************************/
471 static void init_structs(void )
474 * Set the machine NETBIOS name if not already
475 * set from the config file.
478 if (!*global_myname) {
480 fstrcpy( global_myname, myhostname() );
481 p = strchr( global_myname, '.' );
486 strupper( global_myname );
495 /* for LSA handles */
496 init_lsa_policy_hnd();
498 /* for SPOOLSS handles */
506 /****************************************************************************
508 ****************************************************************************/
509 static void usage(char *pname)
512 printf("Usage: %s [-DaoPh?V] [-d debuglevel] [-l log basename] [-p port]\n", pname);
513 printf(" [-O socket options] [-s services file]\n");
514 printf("\t-D Become a daemon\n");
515 printf("\t-a Append to log file (default)\n");
516 printf("\t-o Overwrite log file, don't append\n");
517 printf("\t-h Print usage\n");
518 printf("\t-? Print usage\n");
519 printf("\t-V Print version\n");
520 printf("\t-d debuglevel Set the debuglevel\n");
521 printf("\t-l log basename. Basename for log/debug files\n");
522 printf("\t-p port Listen on the specified port\n");
523 printf("\t-O socket options Socket options\n");
524 printf("\t-s services file. Filename of services file\n");
529 /****************************************************************************
531 ****************************************************************************/
532 int main(int argc,char *argv[])
534 extern BOOL append_log;
535 /* shall I run as a daemon */
536 BOOL is_daemon = False;
537 BOOL specified_logfile = False;
542 #ifdef HAVE_SET_AUTH_PARAMETERS
543 set_auth_parameters(argc,argv);
546 /* this is for people who can't start the program correctly */
547 while (argc > 1 && (*argv[1] != '-')) {
552 while ( EOF != (opt = getopt(argc, argv, "O:l:s:d:Dp:h?Vaof:")) )
555 pstrcpy(user_socket_options,optarg);
559 pstrcpy(servicesf,optarg);
563 specified_logfile = True;
564 pstrcpy(debugf,optarg);
583 DEBUGLEVEL = atoi(optarg);
597 printf("Version %s\n",VERSION);
601 DEBUG(0,("Incorrect program usage - are you sure the command line is correct?\n"));
607 /* needed for SecureWare on SCO */
612 * gain_root_privilege uses an assert than will cause a core
613 * dump if euid != 0. Ensure this is the case.
616 if(geteuid() != (uid_t)0) {
617 fprintf(stderr, "%s: Version %s : Must have effective user id of zero to run.\n", argv[0], VERSION);
625 if(!specified_logfile) {
626 slprintf(debugf, sizeof(debugf), "%s/log.smbd", LOGFILEBASE);
629 pstrcpy(remote_machine, "smb");
631 setup_logging(argv[0],False);
633 charset_initialise();
635 /* we want to re-seed early to prevent time delays causing
636 client problems at a later date. (tridge) */
637 generate_random_buffer(NULL, 0, False);
639 /* make absolutely sure we run as root - to handle cases where people
640 are crazy enough to have it setuid */
642 gain_root_privilege();
643 gain_root_group_privilege();
645 fault_setup((void (*)(void *))exit_server);
646 CatchSignal(SIGTERM , SIGNAL_CAST dflt_sig);
648 /* we are never interested in SIGPIPE */
649 BlockSignals(True,SIGPIPE);
652 /* we are never interested in SIGFPE */
653 BlockSignals(True,SIGFPE);
656 /* we want total control over the permissions on created files,
657 so set our umask to 0 */
660 dos_GetWd(OriginalDir);
666 DEBUG(1,( "smbd version %s started.\n", VERSION));
667 DEBUGADD(1,( "Copyright Andrew Tridgell 1992-1998\n"));
669 DEBUG(2,("uid=%d gid=%d euid=%d egid=%d\n",
670 (int)getuid(),(int)getgid(),(int)geteuid(),(int)getegid()));
672 if (sizeof(uint16) < 2 || sizeof(uint32) < 4) {
673 DEBUG(0,("ERROR: Samba is not configured correctly for the word size on your machine\n"));
678 * Do this before reload_services.
681 if (!reload_services(False))
687 if (!profile_setup(False)) {
688 DEBUG(0,("ERROR: failed to setup profiling\n"));
695 extern BOOL sslEnabled;
696 sslEnabled = lp_ssl_enabled();
700 #endif /* WITH_SSL */
702 codepage_initialise(lp_client_code_page());
704 fstrcpy(global_myworkgroup, lp_workgroup());
706 CatchSignal(SIGHUP,SIGNAL_CAST sig_hup);
708 /* Setup the signals that allow the debug log level
709 to by dynamically changed. */
711 /* If we are using the malloc debug code we can't use
712 SIGUSR1 and SIGUSR2 to do debug level changes. */
715 CatchSignal( SIGUSR1, SIGNAL_CAST sig_usr1 );
719 CatchSignal( SIGUSR2, SIGNAL_CAST sig_usr2 );
722 DEBUG(3,( "loaded services\n"));
724 if (!is_daemon && !is_a_socket(0)) {
725 DEBUG(0,("standard input is not a socket, assuming -D option\n"));
730 DEBUG( 3, ( "Becoming a daemon.\n" ) );
734 if (!directory_exist(lp_lockdir(), NULL)) {
735 mkdir(lp_lockdir(), 0755);
738 check_kernel_oplocks();
741 pidfile_create("smbd");
744 if (!open_sockets(is_daemon,port))
748 * Note that this call should be done after the fork() call
749 * in open_sockets(), as some versions of the locking shared
750 * memory code register openers in a flat file.
753 if (!locking_init(0))
756 if (!print_backend_init()) {
760 if(!initialize_password_db())
763 /* possibly reload the services file. */
764 reload_services(True);
766 if(!pdb_generate_sam_sid()) {
767 DEBUG(0,("ERROR: Samba cannot create a SAM SID.\n"));
772 if (sys_chroot(lp_rootdir()) == 0)
773 DEBUG(2,("Changed root to %s\n", lp_rootdir()));
776 /* Setup the oplock IPC socket. */
777 if( !open_oplock_ipc() )
782 exit_server("normal exit");