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);
323 /* perhaps the config filename is now set */
325 reload_services(True);
332 if (smbd_server_fd() != -1) {
333 set_socket_options(smbd_server_fd(),"SO_KEEPALIVE");
334 set_socket_options(smbd_server_fd(),user_socket_options);
338 reset_mangled_cache();
341 /* this forces service parameters to be flushed */
342 become_service(NULL,True);
349 /****************************************************************************
351 ****************************************************************************/
353 VOLATILE SIG_ATOMIC_T reload_after_sighup = False;
355 static void sig_hup(int sig)
357 BlockSignals(True,SIGHUP);
358 DEBUG(0,("Got SIGHUP\n"));
361 * Fix from <branko.cibej@hermes.si> here.
362 * We used to reload in the signal handler - this
366 reload_after_sighup = True;
367 BlockSignals(False,SIGHUP);
373 /*******************************************************************
374 prepare to dump a core file - carefully!
375 ********************************************************************/
376 static BOOL dump_core(void)
380 pstrcpy(dname,debugf);
381 if ((p=strrchr(dname,'/'))) *p=0;
382 pstrcat(dname,"/corefiles");
384 sys_chown(dname,getuid(),getgid());
386 if (chdir(dname)) return(False);
389 #ifdef HAVE_GETRLIMIT
393 getrlimit(RLIMIT_CORE, &rlp);
394 rlp.rlim_cur = MAX(4*1024*1024,rlp.rlim_cur);
395 setrlimit(RLIMIT_CORE, &rlp);
396 getrlimit(RLIMIT_CORE, &rlp);
397 DEBUG(3,("Core limits now %d %d\n",
398 (int)rlp.rlim_cur,(int)rlp.rlim_max));
404 DEBUG(0,("Dumping core in %s\n",dname));
411 /****************************************************************************
413 ****************************************************************************/
414 void exit_server(char *reason)
416 static int firsttime=1;
417 extern char *last_inbuf;
420 if (!firsttime) exit(0);
424 DEBUG(2,("Closing connections\n"));
428 respond_to_all_remaining_local_messages();
431 if (dcelogin_atmost_once) {
437 int oldlevel = DEBUGLEVEL;
439 DEBUG(0,("Last message was %s\n",smb_fn_name(last_message)));
441 show_msg(last_inbuf);
442 DEBUGLEVEL = oldlevel;
443 DEBUG(0,("===============================================================\n"));
445 if (dump_core()) return;
451 DEBUG(3,("Server exit (%s)\n", (reason ? reason : "")));
457 /****************************************************************************
458 initialise connect, service and file structs
459 ****************************************************************************/
460 static void init_structs(void )
463 * Set the machine NETBIOS name if not already
464 * set from the config file.
467 if (!*global_myname) {
469 fstrcpy( global_myname, myhostname() );
470 p = strchr( global_myname, '.' );
475 strupper( global_myname );
484 /* for LSA handles */
485 init_lsa_policy_hnd();
487 /* for SPOOLSS handles */
495 /****************************************************************************
497 ****************************************************************************/
498 static void usage(char *pname)
501 printf("Usage: %s [-DaoPh?V] [-d debuglevel] [-l log basename] [-p port]\n", pname);
502 printf(" [-O socket options] [-s services file]\n");
503 printf("\t-D Become a daemon\n");
504 printf("\t-a Append to log file (default)\n");
505 printf("\t-o Overwrite log file, don't append\n");
506 printf("\t-h Print usage\n");
507 printf("\t-? Print usage\n");
508 printf("\t-V Print version\n");
509 printf("\t-d debuglevel Set the debuglevel\n");
510 printf("\t-l log basename. Basename for log/debug files\n");
511 printf("\t-p port Listen on the specified port\n");
512 printf("\t-O socket options Socket options\n");
513 printf("\t-s services file. Filename of services file\n");
518 /****************************************************************************
520 ****************************************************************************/
521 int main(int argc,char *argv[])
523 extern BOOL append_log;
524 /* shall I run as a daemon */
525 BOOL is_daemon = False;
526 BOOL specified_logfile = False;
531 #ifdef HAVE_SET_AUTH_PARAMETERS
532 set_auth_parameters(argc,argv);
535 /* this is for people who can't start the program correctly */
536 while (argc > 1 && (*argv[1] != '-')) {
541 while ( EOF != (opt = getopt(argc, argv, "O:l:s:d:Dp:h?Vaof:")) )
544 pstrcpy(user_socket_options,optarg);
548 pstrcpy(servicesf,optarg);
552 specified_logfile = True;
553 pstrcpy(debugf,optarg);
572 DEBUGLEVEL = atoi(optarg);
586 printf("Version %s\n",VERSION);
590 DEBUG(0,("Incorrect program usage - are you sure the command line is correct?\n"));
596 /* needed for SecureWare on SCO */
601 * gain_root_privilege uses an assert than will cause a core
602 * dump if euid != 0. Ensure this is the case.
605 if(geteuid() != (uid_t)0) {
606 fprintf(stderr, "%s: Version %s : Must have effective user id of zero to run.\n", argv[0], VERSION);
614 if(!specified_logfile) {
615 slprintf(debugf, sizeof(debugf), "%s/log.smbd", LOGFILEBASE);
618 pstrcpy(remote_machine, "smb");
620 setup_logging(argv[0],False);
622 charset_initialise();
624 /* we want to re-seed early to prevent time delays causing
625 client problems at a later date. (tridge) */
626 generate_random_buffer(NULL, 0, False);
628 /* make absolutely sure we run as root - to handle cases where people
629 are crazy enough to have it setuid */
631 gain_root_privilege();
632 gain_root_group_privilege();
634 fault_setup((void (*)(void *))exit_server);
635 CatchSignal(SIGTERM , SIGNAL_CAST dflt_sig);
637 /* we are never interested in SIGPIPE */
638 BlockSignals(True,SIGPIPE);
641 /* we are never interested in SIGFPE */
642 BlockSignals(True,SIGFPE);
645 /* we want total control over the permissions on created files,
646 so set our umask to 0 */
649 dos_GetWd(OriginalDir);
655 DEBUG(1,( "smbd version %s started.\n", VERSION));
656 DEBUGADD(1,( "Copyright Andrew Tridgell 1992-1998\n"));
658 DEBUG(2,("uid=%d gid=%d euid=%d egid=%d\n",
659 (int)getuid(),(int)getgid(),(int)geteuid(),(int)getegid()));
661 if (sizeof(uint16) < 2 || sizeof(uint32) < 4) {
662 DEBUG(0,("ERROR: Samba is not configured correctly for the word size on your machine\n"));
667 * Do this before reload_services.
670 if (!reload_services(False))
676 if (!profile_setup(False)) {
677 DEBUG(0,("ERROR: failed to setup profiling\n"));
684 extern BOOL sslEnabled;
685 sslEnabled = lp_ssl_enabled();
689 #endif /* WITH_SSL */
691 codepage_initialise(lp_client_code_page());
693 fstrcpy(global_myworkgroup, lp_workgroup());
695 CatchSignal(SIGHUP,SIGNAL_CAST sig_hup);
697 /* Setup the signals that allow the debug log level
698 to by dynamically changed. */
700 /* If we are using the malloc debug code we can't use
701 SIGUSR1 and SIGUSR2 to do debug level changes. */
704 CatchSignal( SIGUSR1, SIGNAL_CAST sig_usr1 );
708 CatchSignal( SIGUSR2, SIGNAL_CAST sig_usr2 );
711 DEBUG(3,( "loaded services\n"));
713 if (!is_daemon && !is_a_socket(0)) {
714 DEBUG(0,("standard input is not a socket, assuming -D option\n"));
719 DEBUG( 3, ( "Becoming a daemon.\n" ) );
723 if (!directory_exist(lp_lockdir(), NULL)) {
724 mkdir(lp_lockdir(), 0755);
727 check_kernel_oplocks();
730 pidfile_create("smbd");
733 if (!open_sockets(is_daemon,port))
737 * Note that this call should be done after the fork() call
738 * in open_sockets(), as some versions of the locking shared
739 * memory code register openers in a flat file.
742 if (!locking_init(0))
745 if (!print_backend_init()) {
749 if(!initialize_password_db())
752 /* possibly reload the services file. */
753 reload_services(True);
755 if(!pdb_generate_sam_sid()) {
756 DEBUG(0,("ERROR: Samba cannot create a SAM SID.\n"));
761 if (sys_chroot(lp_rootdir()) == 0)
762 DEBUG(2,("Changed root to %s\n", lp_rootdir()));
765 /* Setup the oplock IPC socket. */
766 if( !open_oplock_ipc() )
771 exit_server("normal exit");