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;
51 /****************************************************************************
52 when exiting, take the whole family
53 ****************************************************************************/
54 static void *dflt_sig(void)
56 exit_server("caught signal");
60 /****************************************************************************
61 Send a SIGTERM to our process group.
62 *****************************************************************************/
63 static void killkids(void)
65 if(am_parent) kill(0,SIGTERM);
69 /****************************************************************************
70 open the socket communication
71 ****************************************************************************/
72 static BOOL open_sockets_inetd(void)
76 /* Started from inetd. fd 0 is the socket. */
77 /* We will abort gracefully when the client or remote system
81 /* close our standard file descriptors */
84 set_socket_options(Client,"SO_KEEPALIVE");
85 set_socket_options(Client,user_socket_options);
91 /****************************************************************************
92 open the socket communication
93 ****************************************************************************/
94 static BOOL open_sockets(BOOL is_daemon,int port)
97 int num_interfaces = iface_count();
98 int fd_listenset[FD_SETSIZE];
104 return open_sockets_inetd();
110 static int atexit_set;
111 if(atexit_set == 0) {
122 FD_ZERO(&listen_set);
124 if(lp_interfaces() && lp_bind_interfaces_only()) {
125 /* We have been given an interfaces line, and been
126 told to only bind to those interfaces. Create a
127 socket per interface and bind to only these.
130 if(num_interfaces > FD_SETSIZE) {
131 DEBUG(0,("open_sockets: Too many interfaces specified to bind to. Number was %d \
133 num_interfaces, FD_SETSIZE));
137 /* Now open a listen socket for each of the
139 for(i = 0; i < num_interfaces; i++) {
140 struct in_addr *ifip = iface_n_ip(i);
143 DEBUG(0,("open_sockets: interface %d has NULL IP address !\n", i));
146 s = fd_listenset[i] = open_socket_in(SOCK_STREAM, port, 0, ifip->s_addr, True);
149 /* ready to listen */
150 if (listen(s, 5) == -1) {
151 DEBUG(0,("listen: %s\n",strerror(errno)));
155 FD_SET(s,&listen_set);
158 /* Just bind to 0.0.0.0 - accept connections
162 /* open an incoming socket */
163 s = open_socket_in(SOCK_STREAM, port, 0,
164 interpret_addr(lp_socket_address()),True);
168 /* ready to listen */
169 if (listen(s, 5) == -1) {
170 DEBUG(0,("open_sockets: listen: %s\n",
177 FD_SET(s,&listen_set);
180 /* now accept incoming connections - forking a new process
181 for each incoming connection */
182 DEBUG(2,("waiting for a connection\n"));
187 memcpy((char *)&lfds, (char *)&listen_set,
190 num = sys_select(FD_SETSIZE,&lfds,NULL);
192 if (num == -1 && errno == EINTR)
195 /* check if we need to reload services */
196 check_reload(time(NULL));
198 /* Find the sockets that are read-ready -
200 for( ; num > 0; num--) {
201 struct sockaddr addr;
202 int in_addrlen = sizeof(addr);
205 for(i = 0; i < num_interfaces; i++) {
206 if(FD_ISSET(fd_listenset[i],&lfds)) {
208 /* Clear this so we don't look
210 FD_CLR(fd_listenset[i],&lfds);
215 Client = accept(s,&addr,&in_addrlen);
217 if (Client == -1 && errno == EINTR)
221 DEBUG(0,("open_sockets: accept: %s\n",
226 if (Client != -1 && fork()==0) {
229 /* close the listening socket(s) */
230 for(i = 0; i < num_interfaces; i++)
231 close(fd_listenset[i]);
233 /* close our standard file
238 set_socket_options(Client,"SO_KEEPALIVE");
239 set_socket_options(Client,user_socket_options);
241 /* Reset global variables in util.c so
242 that client substitutions will be
243 done correctly in the process. */
244 reset_globals_after_fork();
247 * Ensure this child has kernel oplock
248 * capabilities, but not it's children.
250 set_process_capability(KERNEL_OPLOCK_CAPABILITY, True);
251 set_inherited_process_capability(KERNEL_OPLOCK_CAPABILITY, False);
255 /* The parent doesn't need this socket */
258 /* Force parent to check log size after
259 * spawning child. Fix from
260 * klausr@ITAP.Physik.Uni-Stuttgart.De. The
261 * parent smbd will log to logserver.smb. It
262 * writes only two messages for each child
263 * started/finished. But each child writes,
264 * say, 50 messages also in logserver.smb,
265 * begining with the debug_count of the
266 * parent, before the child opens its own log
267 * file logserver.client. In a worst case
268 * scenario the size of logserver.smb would be
269 * checked after about 50*50=2500 messages
272 force_check_log_size();
277 /* NOTREACHED return True; */
280 /****************************************************************************
281 reload the services file
282 **************************************************************************/
283 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);
307 /* perhaps the config filename is now set */
309 reload_services(True);
318 set_socket_options(Client,"SO_KEEPALIVE");
319 set_socket_options(Client,user_socket_options);
323 reset_mangled_cache();
325 /* this forces service parameters to be flushed */
326 become_service(NULL,True);
333 /****************************************************************************
335 ****************************************************************************/
337 VOLATILE SIG_ATOMIC_T reload_after_sighup = False;
339 static void sig_hup(int sig)
341 BlockSignals(True,SIGHUP);
342 DEBUG(0,("Got SIGHUP\n"));
345 * Fix from <branko.cibej@hermes.si> here.
346 * We used to reload in the signal handler - this
350 reload_after_sighup = True;
351 BlockSignals(False,SIGHUP);
357 /*******************************************************************
358 prepare to dump a core file - carefully!
359 ********************************************************************/
360 static BOOL dump_core(void)
364 pstrcpy(dname,debugf);
365 if ((p=strrchr(dname,'/'))) *p=0;
366 pstrcat(dname,"/corefiles");
368 sys_chown(dname,getuid(),getgid());
370 if (chdir(dname)) return(False);
373 #ifdef HAVE_GETRLIMIT
377 getrlimit(RLIMIT_CORE, &rlp);
378 rlp.rlim_cur = MAX(4*1024*1024,rlp.rlim_cur);
379 setrlimit(RLIMIT_CORE, &rlp);
380 getrlimit(RLIMIT_CORE, &rlp);
381 DEBUG(3,("Core limits now %d %d\n",
382 (int)rlp.rlim_cur,(int)rlp.rlim_max));
388 DEBUG(0,("Dumping core in %s\n",dname));
395 /****************************************************************************
397 ****************************************************************************/
398 void exit_server(char *reason)
400 static int firsttime=1;
401 extern char *last_inbuf;
404 if (!firsttime) exit(0);
408 DEBUG(2,("Closing connections\n"));
412 respond_to_all_remaining_local_messages();
415 if (dcelogin_atmost_once) {
421 int oldlevel = DEBUGLEVEL;
423 DEBUG(0,("Last message was %s\n",smb_fn_name(last_message)));
425 show_msg(last_inbuf);
426 DEBUGLEVEL = oldlevel;
427 DEBUG(0,("===============================================================\n"));
429 if (dump_core()) return;
435 DEBUG(3,("Server exit (%s)\n", (reason ? reason : "")));
441 /****************************************************************************
442 initialise connect, service and file structs
443 ****************************************************************************/
444 static void init_structs(void )
447 * Set the machine NETBIOS name if not already
448 * set from the config file.
451 if (!*global_myname) {
453 fstrcpy( global_myname, myhostname() );
454 p = strchr( global_myname, '.' );
459 strupper( global_myname );
468 /* for LSA handles */
469 init_lsa_policy_hnd();
474 /****************************************************************************
476 ****************************************************************************/
477 static void usage(char *pname)
480 printf("Usage: %s [-DaoPh?V] [-d debuglevel] [-l log basename] [-p port]\n", pname);
481 printf(" [-O socket options] [-s services file] [-i scope]\n");
482 printf("\t-D Become a daemon\n");
483 printf("\t-a Append to log file (default)\n");
484 printf("\t-o Overwrite log file, don't append\n");
485 printf("\t-P Passive only\n");
486 printf("\t-h Print usage\n");
487 printf("\t-? Print usage\n");
488 printf("\t-V Print version\n");
489 printf("\t-d debuglevel Set the debuglevel\n");
490 printf("\t-l log basename. Basename for log/debug files\n");
491 printf("\t-p port Listen on the specified port\n");
492 printf("\t-O socket options Socket options\n");
493 printf("\t-s services file. Filename of services file\n");
494 printf("\t-i scope NetBIOS scope to use (default none)\n");
499 /****************************************************************************
501 ****************************************************************************/
502 int main(int argc,char *argv[])
504 extern BOOL append_log;
505 /* shall I run as a daemon */
506 BOOL is_daemon = False;
511 #ifdef HAVE_SET_AUTH_PARAMETERS
512 set_auth_parameters(argc,argv);
515 /* this is for people who can't start the program correctly */
516 while (argc > 1 && (*argv[1] != '-')) {
521 while ( EOF != (opt = getopt(argc, argv, "O:i:l:s:d:Dp:h?VPaof:")) )
524 pstrcpy(user_socket_options,optarg);
528 pstrcpy(scope,optarg);
539 pstrcpy(servicesf,optarg);
543 pstrcpy(debugf,optarg);
562 DEBUGLEVEL = atoi(optarg);
576 printf("Version %s\n",VERSION);
580 DEBUG(0,("Incorrect program usage - are you sure the command line is correct?\n"));
586 /* needed for SecureWare on SCO */
591 * gain_root_privilege uses an assert than will cause a core
592 * dump if euid != 0. Ensure this is the case.
595 if(geteuid() != (uid_t)0) {
596 fprintf(stderr, "%s: Version %s : Must have effective user id of zero to run.\n", argv[0], VERSION);
604 pstrcpy(debugf,SMBLOGFILE);
606 pstrcpy(remote_machine, "smb");
608 setup_logging(argv[0],False);
610 charset_initialise();
612 /* we want to re-seed early to prevent time delays causing
613 client problems at a later date. (tridge) */
614 generate_random_buffer(NULL, 0, False);
616 /* make absolutely sure we run as root - to handle cases where people
617 are crazy enough to have it setuid */
619 gain_root_privilege();
620 gain_root_group_privilege();
622 fault_setup((void (*)(void *))exit_server);
623 CatchSignal(SIGTERM , SIGNAL_CAST dflt_sig);
625 /* we are never interested in SIGPIPE */
626 BlockSignals(True,SIGPIPE);
629 /* we are never interested in SIGFPE */
630 BlockSignals(True,SIGFPE);
633 /* we want total control over the permissions on created files,
634 so set our umask to 0 */
637 dos_GetWd(OriginalDir);
643 DEBUG(1,( "smbd version %s started.\n", VERSION));
644 DEBUGADD(1,( "Copyright Andrew Tridgell 1992-1998\n"));
646 DEBUG(2,("uid=%d gid=%d euid=%d egid=%d\n",
647 (int)getuid(),(int)getgid(),(int)geteuid(),(int)getegid()));
649 if (sizeof(uint16) < 2 || sizeof(uint32) < 4) {
650 DEBUG(0,("ERROR: Samba is not configured correctly for the word size on your machine\n"));
655 * Do this before reload_services.
658 if (!reload_services(False))
664 if (!profile_setup(False)) {
665 DEBUG(0,("ERROR: failed to setup profiling\n"));
672 extern BOOL sslEnabled;
673 sslEnabled = lp_ssl_enabled();
677 #endif /* WITH_SSL */
679 codepage_initialise(lp_client_code_page());
681 fstrcpy(global_myworkgroup, lp_workgroup());
683 if(!pdb_generate_sam_sid()) {
684 DEBUG(0,("ERROR: Samba cannot create a SAM SID.\n"));
688 CatchSignal(SIGHUP,SIGNAL_CAST sig_hup);
690 /* Setup the signals that allow the debug log level
691 to by dynamically changed. */
693 /* If we are using the malloc debug code we can't use
694 SIGUSR1 and SIGUSR2 to do debug level changes. */
698 CatchSignal( SIGUSR1, SIGNAL_CAST sig_usr1 );
702 CatchSignal( SIGUSR2, SIGNAL_CAST sig_usr2 );
706 DEBUG(3,( "loaded services\n"));
708 if (!is_daemon && !is_a_socket(0)) {
709 DEBUG(0,("standard input is not a socket, assuming -D option\n"));
714 DEBUG( 3, ( "Becoming a daemon.\n" ) );
718 check_kernel_oplocks();
720 if (!directory_exist(lp_lockdir(), NULL)) {
721 mkdir(lp_lockdir(), 0755);
725 pidfile_create("smbd");
728 if (!locking_init(0))
731 if (!open_sockets(is_daemon,port))
734 if(!initialize_password_db())
737 /* possibly reload the services file. */
738 reload_services(True);
741 if (sys_chroot(lp_rootdir()) == 0)
742 DEBUG(2,("Changed root to %s\n", lp_rootdir()));
745 /* Setup the oplock IPC socket. */
746 if( !open_oplock_ipc() )
752 exit_server("normal exit");