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 int DEBUGLEVEL;
39 extern pstring user_socket_options;
42 extern int dcelogin_atmost_once;
46 extern fstring remote_machine;
47 extern pstring OriginalDir;
50 /* really we should have a top level context structure that has the
51 client file descriptor as an element. That would require a major rewrite :(
53 the following 2 functions are an alternative - they make the file
54 descriptor private to smbd
58 int smbd_server_fd(void)
63 void smbd_set_server_fd(int fd)
69 /****************************************************************************
70 when exiting, take the whole family
71 ****************************************************************************/
72 static void *dflt_sig(void)
74 exit_server("caught signal");
78 /****************************************************************************
79 Send a SIGTERM to our process group.
80 *****************************************************************************/
81 static void killkids(void)
83 if(am_parent) kill(0,SIGTERM);
87 /****************************************************************************
88 open the socket communication
89 ****************************************************************************/
90 static BOOL open_sockets_inetd(void)
92 /* Started from inetd. fd 0 is the socket. */
93 /* We will abort gracefully when the client or remote system
95 smbd_set_server_fd(dup(0));
97 /* close our standard file descriptors */
100 set_socket_options(smbd_server_fd(),"SO_KEEPALIVE");
101 set_socket_options(smbd_server_fd(),user_socket_options);
107 /****************************************************************************
108 open the socket communication
109 ****************************************************************************/
110 static BOOL open_sockets(BOOL is_daemon,int port)
112 int num_interfaces = iface_count();
113 int fd_listenset[FD_SETSIZE];
119 return open_sockets_inetd();
125 static int atexit_set;
126 if(atexit_set == 0) {
137 FD_ZERO(&listen_set);
139 if(lp_interfaces() && lp_bind_interfaces_only()) {
140 /* We have been given an interfaces line, and been
141 told to only bind to those interfaces. Create a
142 socket per interface and bind to only these.
145 if(num_interfaces > FD_SETSIZE) {
146 DEBUG(0,("open_sockets: Too many interfaces specified to bind to. Number was %d \
148 num_interfaces, FD_SETSIZE));
152 /* Now open a listen socket for each of the
154 for(i = 0; i < num_interfaces; i++) {
155 struct in_addr *ifip = iface_n_ip(i);
158 DEBUG(0,("open_sockets: interface %d has NULL IP address !\n", i));
161 s = fd_listenset[i] = open_socket_in(SOCK_STREAM, port, 0, ifip->s_addr, True);
164 /* ready to listen */
165 if (listen(s, 5) == -1) {
166 DEBUG(0,("listen: %s\n",strerror(errno)));
170 FD_SET(s,&listen_set);
173 /* Just bind to 0.0.0.0 - accept connections
177 /* open an incoming socket */
178 s = open_socket_in(SOCK_STREAM, port, 0,
179 interpret_addr(lp_socket_address()),True);
183 /* ready to listen */
184 if (listen(s, 5) == -1) {
185 DEBUG(0,("open_sockets: listen: %s\n",
192 FD_SET(s,&listen_set);
195 /* now accept incoming connections - forking a new process
196 for each incoming connection */
197 DEBUG(2,("waiting for a connection\n"));
202 memcpy((char *)&lfds, (char *)&listen_set,
205 num = sys_select(FD_SETSIZE,&lfds,NULL);
207 if (num == -1 && errno == EINTR)
210 /* check if we need to reload services */
211 check_reload(time(NULL));
213 /* Find the sockets that are read-ready -
215 for( ; num > 0; num--) {
216 struct sockaddr addr;
217 int in_addrlen = sizeof(addr);
220 for(i = 0; i < num_interfaces; i++) {
221 if(FD_ISSET(fd_listenset[i],&lfds)) {
223 /* Clear this so we don't look
225 FD_CLR(fd_listenset[i],&lfds);
230 smbd_set_server_fd(accept(s,&addr,&in_addrlen));
232 if (smbd_server_fd() == -1 && errno == EINTR)
235 if (smbd_server_fd() == -1) {
236 DEBUG(0,("open_sockets: accept: %s\n",
241 if (smbd_server_fd() != -1 && sys_fork()==0) {
244 /* close the listening socket(s) */
245 for(i = 0; i < num_interfaces; i++)
246 close(fd_listenset[i]);
248 /* close our standard file
253 set_socket_options(smbd_server_fd(),"SO_KEEPALIVE");
254 set_socket_options(smbd_server_fd(),user_socket_options);
256 /* Reset global variables in util.c so
257 that client substitutions will be
258 done correctly in the process. */
259 reset_globals_after_fork();
262 * Ensure this child has kernel oplock
263 * capabilities, but not it's children.
265 set_process_capability(KERNEL_OPLOCK_CAPABILITY, True);
266 set_inherited_process_capability(KERNEL_OPLOCK_CAPABILITY, False);
270 /* The parent doesn't need this socket */
271 close(smbd_server_fd());
273 /* Force parent to check log size after
274 * spawning child. Fix from
275 * klausr@ITAP.Physik.Uni-Stuttgart.De. The
276 * parent smbd will log to logserver.smb. It
277 * writes only two messages for each child
278 * started/finished. But each child writes,
279 * say, 50 messages also in logserver.smb,
280 * begining with the debug_count of the
281 * parent, before the child opens its own log
282 * file logserver.client. In a worst case
283 * scenario the size of logserver.smb would be
284 * checked after about 50*50=2500 messages
287 force_check_log_size();
292 /* NOTREACHED return True; */
295 /****************************************************************************
296 reload the services file
297 **************************************************************************/
298 BOOL reload_services(BOOL test)
304 pstrcpy(fname,lp_configfile());
305 if (file_exist(fname,NULL) && !strcsequal(fname,servicesf)) {
306 pstrcpy(servicesf,fname);
313 if (test && !lp_file_list_changed())
316 lp_killunused(conn_snum_used);
318 ret = lp_load(servicesf,False,False,True);
321 /* load the dfs maps of all the services having
323 we don't want to do this in lp_load because we want just the smbd
324 server to load up the dfs maps into msdfs.tdb. not nmbd, swat etc*/
330 /* perhaps the config filename is now set */
332 reload_services(True);
339 if (smbd_server_fd() != -1) {
340 set_socket_options(smbd_server_fd(),"SO_KEEPALIVE");
341 set_socket_options(smbd_server_fd(),user_socket_options);
345 reset_mangled_cache();
348 /* this forces service parameters to be flushed */
349 become_service(NULL,True);
356 /****************************************************************************
358 ****************************************************************************/
360 VOLATILE SIG_ATOMIC_T reload_after_sighup = False;
362 static void sig_hup(int sig)
364 BlockSignals(True,SIGHUP);
365 DEBUG(0,("Got SIGHUP\n"));
368 * Fix from <branko.cibej@hermes.si> here.
369 * We used to reload in the signal handler - this
373 reload_after_sighup = True;
374 BlockSignals(False,SIGHUP);
380 /*******************************************************************
381 prepare to dump a core file - carefully!
382 ********************************************************************/
383 static BOOL dump_core(void)
387 pstrcpy(dname,debugf);
388 if ((p=strrchr(dname,'/'))) *p=0;
389 pstrcat(dname,"/corefiles");
391 sys_chown(dname,getuid(),getgid());
393 if (chdir(dname)) return(False);
396 #ifdef HAVE_GETRLIMIT
400 getrlimit(RLIMIT_CORE, &rlp);
401 rlp.rlim_cur = MAX(4*1024*1024,rlp.rlim_cur);
402 setrlimit(RLIMIT_CORE, &rlp);
403 getrlimit(RLIMIT_CORE, &rlp);
404 DEBUG(3,("Core limits now %d %d\n",
405 (int)rlp.rlim_cur,(int)rlp.rlim_max));
411 DEBUG(0,("Dumping core in %s\n",dname));
418 /****************************************************************************
420 ****************************************************************************/
421 void exit_server(char *reason)
423 static int firsttime=1;
424 extern char *last_inbuf;
427 if (!firsttime) exit(0);
431 DEBUG(2,("Closing connections\n"));
435 respond_to_all_remaining_local_messages();
438 if (dcelogin_atmost_once) {
444 int oldlevel = DEBUGLEVEL;
446 DEBUG(0,("Last message was %s\n",smb_fn_name(last_message)));
448 show_msg(last_inbuf);
449 DEBUGLEVEL = oldlevel;
450 DEBUG(0,("===============================================================\n"));
452 if (dump_core()) return;
461 DEBUG(3,("Server exit (%s)\n", (reason ? reason : "")));
467 /****************************************************************************
468 initialise connect, service and file structs
469 ****************************************************************************/
470 static void init_structs(void )
473 * Set the machine NETBIOS name if not already
474 * set from the config file.
477 if (!*global_myname) {
479 fstrcpy( global_myname, myhostname() );
480 p = strchr( global_myname, '.' );
485 strupper( global_myname );
494 /* for LSA handles */
495 init_lsa_policy_hnd();
497 /* for SPOOLSS handles */
505 /****************************************************************************
507 ****************************************************************************/
508 static void usage(char *pname)
511 printf("Usage: %s [-DaoPh?V] [-d debuglevel] [-l log basename] [-p port]\n", pname);
512 printf(" [-O socket options] [-s services file]\n");
513 printf("\t-D Become a daemon\n");
514 printf("\t-a Append to log file (default)\n");
515 printf("\t-o Overwrite log file, don't append\n");
516 printf("\t-h Print usage\n");
517 printf("\t-? Print usage\n");
518 printf("\t-V Print version\n");
519 printf("\t-d debuglevel Set the debuglevel\n");
520 printf("\t-l log basename. Basename for log/debug files\n");
521 printf("\t-p port Listen on the specified port\n");
522 printf("\t-O socket options Socket options\n");
523 printf("\t-s services file. Filename of services file\n");
528 /****************************************************************************
530 ****************************************************************************/
531 int main(int argc,char *argv[])
533 extern BOOL append_log;
534 /* shall I run as a daemon */
535 BOOL is_daemon = False;
536 BOOL specified_logfile = False;
541 #ifdef HAVE_SET_AUTH_PARAMETERS
542 set_auth_parameters(argc,argv);
545 /* this is for people who can't start the program correctly */
546 while (argc > 1 && (*argv[1] != '-')) {
551 while ( EOF != (opt = getopt(argc, argv, "O:l:s:d:Dp:h?Vaof:")) )
554 pstrcpy(user_socket_options,optarg);
558 pstrcpy(servicesf,optarg);
562 specified_logfile = True;
563 pstrcpy(debugf,optarg);
582 DEBUGLEVEL = atoi(optarg);
596 printf("Version %s\n",VERSION);
600 DEBUG(0,("Incorrect program usage - are you sure the command line is correct?\n"));
606 /* needed for SecureWare on SCO */
611 * gain_root_privilege uses an assert than will cause a core
612 * dump if euid != 0. Ensure this is the case.
615 if(geteuid() != (uid_t)0) {
616 fprintf(stderr, "%s: Version %s : Must have effective user id of zero to run.\n", argv[0], VERSION);
624 if(!specified_logfile)
625 pstrcpy(debugf,SMBLOGFILE);
627 pstrcpy(remote_machine, "smb");
629 setup_logging(argv[0],False);
631 charset_initialise();
633 /* we want to re-seed early to prevent time delays causing
634 client problems at a later date. (tridge) */
635 generate_random_buffer(NULL, 0, False);
637 /* make absolutely sure we run as root - to handle cases where people
638 are crazy enough to have it setuid */
640 gain_root_privilege();
641 gain_root_group_privilege();
643 fault_setup((void (*)(void *))exit_server);
644 CatchSignal(SIGTERM , SIGNAL_CAST dflt_sig);
646 /* we are never interested in SIGPIPE */
647 BlockSignals(True,SIGPIPE);
650 /* we are never interested in SIGFPE */
651 BlockSignals(True,SIGFPE);
654 /* we want total control over the permissions on created files,
655 so set our umask to 0 */
658 dos_GetWd(OriginalDir);
664 DEBUG(1,( "smbd version %s started.\n", VERSION));
665 DEBUGADD(1,( "Copyright Andrew Tridgell 1992-1998\n"));
667 DEBUG(2,("uid=%d gid=%d euid=%d egid=%d\n",
668 (int)getuid(),(int)getgid(),(int)geteuid(),(int)getegid()));
670 if (sizeof(uint16) < 2 || sizeof(uint32) < 4) {
671 DEBUG(0,("ERROR: Samba is not configured correctly for the word size on your machine\n"));
676 * Do this before reload_services.
679 if (!reload_services(False))
685 if (!profile_setup(False)) {
686 DEBUG(0,("ERROR: failed to setup profiling\n"));
693 extern BOOL sslEnabled;
694 sslEnabled = lp_ssl_enabled();
698 #endif /* WITH_SSL */
700 codepage_initialise(lp_client_code_page());
702 fstrcpy(global_myworkgroup, lp_workgroup());
704 CatchSignal(SIGHUP,SIGNAL_CAST sig_hup);
706 /* Setup the signals that allow the debug log level
707 to by dynamically changed. */
709 /* If we are using the malloc debug code we can't use
710 SIGUSR1 and SIGUSR2 to do debug level changes. */
713 CatchSignal( SIGUSR1, SIGNAL_CAST sig_usr1 );
717 CatchSignal( SIGUSR2, SIGNAL_CAST sig_usr2 );
720 DEBUG(3,( "loaded services\n"));
722 if (!is_daemon && !is_a_socket(0)) {
723 DEBUG(0,("standard input is not a socket, assuming -D option\n"));
728 DEBUG( 3, ( "Becoming a daemon.\n" ) );
732 check_kernel_oplocks();
734 if (!directory_exist(lp_lockdir(), NULL)) {
735 mkdir(lp_lockdir(), 0755);
739 pidfile_create("smbd");
742 if (!open_sockets(is_daemon,port))
746 * Note that this call should be done after the fork() call
747 * in open_sockets(), as some versions of the locking shared
748 * memory code register openers in a flat file.
751 if (!locking_init(0))
754 if (!print_backend_init()) {
758 if(!initialize_password_db())
761 /* possibly reload the services file. */
762 reload_services(True);
764 if(!pdb_generate_sam_sid()) {
765 DEBUG(0,("ERROR: Samba cannot create a SAM SID.\n"));
770 if (sys_chroot(lp_rootdir()) == 0)
771 DEBUG(2,("Changed root to %s\n", lp_rootdir()));
774 /* Setup the oplock IPC socket. */
775 if( !open_oplock_ipc() )
780 exit_server("normal exit");