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 /* Force parent to check log size after
258 * spawning child. Fix from
259 * klausr@ITAP.Physik.Uni-Stuttgart.De. The
260 * parent smbd will log to logserver.smb. It
261 * writes only two messages for each child
262 * started/finished. But each child writes,
263 * say, 50 messages also in logserver.smb,
264 * begining with the debug_count of the
265 * parent, before the child opens its own log
266 * file logserver.client. In a worst case
267 * scenario the size of logserver.smb would be
268 * checked after about 50*50=2500 messages
271 force_check_log_size();
276 /* NOTREACHED return True; */
279 /****************************************************************************
280 reload the services file
281 **************************************************************************/
282 BOOL reload_services(BOOL test)
289 pstrcpy(fname,lp_configfile());
290 if (file_exist(fname,NULL) && !strcsequal(fname,servicesf)) {
291 pstrcpy(servicesf,fname);
298 if (test && !lp_file_list_changed())
301 lp_killunused(conn_snum_used);
303 ret = lp_load(servicesf,False,False,True);
305 /* load the dfs maps of all the services having
307 we don't want to do this in lp_load because we want just the smbd
308 server to load up the dfs maps into msdfds.tdb. not nmbd, swat etc*/
313 /* perhaps the config filename is now set */
315 reload_services(True);
324 set_socket_options(Client,"SO_KEEPALIVE");
325 set_socket_options(Client,user_socket_options);
329 reset_mangled_cache();
332 /* this forces service parameters to be flushed */
333 become_service(NULL,True);
340 /****************************************************************************
342 ****************************************************************************/
344 VOLATILE SIG_ATOMIC_T reload_after_sighup = False;
346 static void sig_hup(int sig)
348 BlockSignals(True,SIGHUP);
349 DEBUG(0,("Got SIGHUP\n"));
352 * Fix from <branko.cibej@hermes.si> here.
353 * We used to reload in the signal handler - this
357 reload_after_sighup = True;
358 BlockSignals(False,SIGHUP);
364 /*******************************************************************
365 prepare to dump a core file - carefully!
366 ********************************************************************/
367 static BOOL dump_core(void)
371 pstrcpy(dname,debugf);
372 if ((p=strrchr(dname,'/'))) *p=0;
373 pstrcat(dname,"/corefiles");
375 sys_chown(dname,getuid(),getgid());
377 if (chdir(dname)) return(False);
380 #ifdef HAVE_GETRLIMIT
384 getrlimit(RLIMIT_CORE, &rlp);
385 rlp.rlim_cur = MAX(4*1024*1024,rlp.rlim_cur);
386 setrlimit(RLIMIT_CORE, &rlp);
387 getrlimit(RLIMIT_CORE, &rlp);
388 DEBUG(3,("Core limits now %d %d\n",
389 (int)rlp.rlim_cur,(int)rlp.rlim_max));
395 DEBUG(0,("Dumping core in %s\n",dname));
402 /****************************************************************************
404 ****************************************************************************/
405 void exit_server(char *reason)
407 static int firsttime=1;
408 extern char *last_inbuf;
411 if (!firsttime) exit(0);
415 DEBUG(2,("Closing connections\n"));
419 respond_to_all_remaining_local_messages();
422 if (dcelogin_atmost_once) {
428 int oldlevel = DEBUGLEVEL;
430 DEBUG(0,("Last message was %s\n",smb_fn_name(last_message)));
432 show_msg(last_inbuf);
433 DEBUGLEVEL = oldlevel;
434 DEBUG(0,("===============================================================\n"));
436 if (dump_core()) return;
445 DEBUG(3,("Server exit (%s)\n", (reason ? reason : "")));
451 /****************************************************************************
452 initialise connect, service and file structs
453 ****************************************************************************/
454 static void init_structs(void )
457 * Set the machine NETBIOS name if not already
458 * set from the config file.
461 if (!*global_myname) {
463 fstrcpy( global_myname, myhostname() );
464 p = strchr( global_myname, '.' );
469 strupper( global_myname );
478 /* for LSA handles */
479 init_lsa_policy_hnd();
481 /* for SPOOLSS handles */
487 /****************************************************************************
489 ****************************************************************************/
490 static void usage(char *pname)
493 printf("Usage: %s [-DaoPh?V] [-d debuglevel] [-l log basename] [-p port]\n", pname);
494 printf(" [-O socket options] [-s services file]\n");
495 printf("\t-D Become a daemon\n");
496 printf("\t-a Append to log file (default)\n");
497 printf("\t-o Overwrite log file, don't append\n");
498 printf("\t-P Passive only\n");
499 printf("\t-h Print usage\n");
500 printf("\t-? Print usage\n");
501 printf("\t-V Print version\n");
502 printf("\t-d debuglevel Set the debuglevel\n");
503 printf("\t-l log basename. Basename for log/debug files\n");
504 printf("\t-p port Listen on the specified port\n");
505 printf("\t-O socket options Socket options\n");
506 printf("\t-s services file. Filename of services file\n");
511 /****************************************************************************
513 ****************************************************************************/
514 int main(int argc,char *argv[])
518 extern BOOL append_log;
519 /* shall I run as a daemon */
520 BOOL is_daemon = False;
521 BOOL specified_logfile = False;
526 #ifdef HAVE_SET_AUTH_PARAMETERS
527 set_auth_parameters(argc,argv);
530 /* this is for people who can't start the program correctly */
531 while (argc > 1 && (*argv[1] != '-')) {
536 while ( EOF != (opt = getopt(argc, argv, "O:l:s:d:Dp:h?VPaof:")) )
539 pstrcpy(user_socket_options,optarg);
550 pstrcpy(servicesf,optarg);
554 specified_logfile = True;
555 pstrcpy(debugf,optarg);
574 DEBUGLEVEL = atoi(optarg);
588 printf("Version %s\n",VERSION);
592 DEBUG(0,("Incorrect program usage - are you sure the command line is correct?\n"));
598 /* needed for SecureWare on SCO */
603 * gain_root_privilege uses an assert than will cause a core
604 * dump if euid != 0. Ensure this is the case.
607 if(geteuid() != (uid_t)0) {
608 fprintf(stderr, "%s: Version %s : Must have effective user id of zero to run.\n", argv[0], VERSION);
616 if(!specified_logfile)
617 pstrcpy(debugf,SMBLOGFILE);
619 pstrcpy(remote_machine, "smb");
621 setup_logging(argv[0],False);
623 charset_initialise();
625 /* we want to re-seed early to prevent time delays causing
626 client problems at a later date. (tridge) */
627 generate_random_buffer(NULL, 0, False);
629 /* make absolutely sure we run as root - to handle cases where people
630 are crazy enough to have it setuid */
632 gain_root_privilege();
633 gain_root_group_privilege();
635 fault_setup((void (*)(void *))exit_server);
636 CatchSignal(SIGTERM , SIGNAL_CAST dflt_sig);
638 /* we are never interested in SIGPIPE */
639 BlockSignals(True,SIGPIPE);
642 /* we are never interested in SIGFPE */
643 BlockSignals(True,SIGFPE);
646 /* we want total control over the permissions on created files,
647 so set our umask to 0 */
650 dos_GetWd(OriginalDir);
656 DEBUG(1,( "smbd version %s started.\n", VERSION));
657 DEBUGADD(1,( "Copyright Andrew Tridgell 1992-1998\n"));
659 DEBUG(2,("uid=%d gid=%d euid=%d egid=%d\n",
660 (int)getuid(),(int)getgid(),(int)geteuid(),(int)getegid()));
662 if (sizeof(uint16) < 2 || sizeof(uint32) < 4) {
663 DEBUG(0,("ERROR: Samba is not configured correctly for the word size on your machine\n"));
668 * Do this before reload_services.
671 if (!reload_services(False))
677 if (!profile_setup(False)) {
678 DEBUG(0,("ERROR: failed to setup profiling\n"));
685 extern BOOL sslEnabled;
686 sslEnabled = lp_ssl_enabled();
690 #endif /* WITH_SSL */
692 codepage_initialise(lp_client_code_page());
694 fstrcpy(global_myworkgroup, lp_workgroup());
696 CatchSignal(SIGHUP,SIGNAL_CAST sig_hup);
698 /* Setup the signals that allow the debug log level
699 to by dynamically changed. */
701 /* If we are using the malloc debug code we can't use
702 SIGUSR1 and SIGUSR2 to do debug level changes. */
705 CatchSignal( SIGUSR1, SIGNAL_CAST sig_usr1 );
709 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);
752 /* obtain or create a SAM SID */
753 if (lp_domain_logons())
755 fstrcpy(sam_name, global_myworkgroup);
759 fstrcpy(sam_name, global_myname);
762 if(!pdb_generate_sam_sid(sam_name, NULL))
764 DEBUG(0,("ERROR: Samba cannot create a SAM SID.\n"));
769 if (sys_chroot(lp_rootdir()) == 0)
770 DEBUG(2,("Changed root to %s\n", lp_rootdir()));
773 /* Setup the oplock IPC socket. */
774 if( !open_oplock_ipc() )
780 exit_server("normal exit");