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 oplock_set_capability(True, 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);
322 /* perhaps the config filename is now set */
324 reload_services(True);
331 if (smbd_server_fd() != -1) {
332 set_socket_options(smbd_server_fd(),"SO_KEEPALIVE");
333 set_socket_options(smbd_server_fd(),user_socket_options);
337 reset_mangled_cache();
340 /* this forces service parameters to be flushed */
341 become_service(NULL,True);
348 /****************************************************************************
350 ****************************************************************************/
352 VOLATILE SIG_ATOMIC_T reload_after_sighup = False;
354 static void sig_hup(int sig)
356 BlockSignals(True,SIGHUP);
357 DEBUG(0,("Got SIGHUP\n"));
360 * Fix from <branko.cibej@hermes.si> here.
361 * We used to reload in the signal handler - this
365 reload_after_sighup = True;
366 BlockSignals(False,SIGHUP);
372 /*******************************************************************
373 prepare to dump a core file - carefully!
374 ********************************************************************/
375 static BOOL dump_core(void)
379 pstrcpy(dname,debugf);
380 if ((p=strrchr(dname,'/'))) *p=0;
381 pstrcat(dname,"/corefiles");
383 sys_chown(dname,getuid(),getgid());
385 if (chdir(dname)) return(False);
388 #ifdef HAVE_GETRLIMIT
392 getrlimit(RLIMIT_CORE, &rlp);
393 rlp.rlim_cur = MAX(4*1024*1024,rlp.rlim_cur);
394 setrlimit(RLIMIT_CORE, &rlp);
395 getrlimit(RLIMIT_CORE, &rlp);
396 DEBUG(3,("Core limits now %d %d\n",
397 (int)rlp.rlim_cur,(int)rlp.rlim_max));
403 DEBUG(0,("Dumping core in %s\n",dname));
410 /****************************************************************************
412 ****************************************************************************/
413 void exit_server(char *reason)
415 static int firsttime=1;
416 extern char *last_inbuf;
419 if (!firsttime) exit(0);
423 DEBUG(2,("Closing connections\n"));
427 respond_to_all_remaining_local_messages();
430 if (dcelogin_atmost_once) {
436 int oldlevel = DEBUGLEVEL;
438 DEBUG(0,("Last message was %s\n",smb_fn_name(last_message)));
440 show_msg(last_inbuf);
441 DEBUGLEVEL = oldlevel;
442 DEBUG(0,("===============================================================\n"));
444 if (dump_core()) return;
450 DEBUG(3,("Server exit (%s)\n", (reason ? reason : "")));
456 /****************************************************************************
457 initialise connect, service and file structs
458 ****************************************************************************/
459 static void init_structs(void )
462 * Set the machine NETBIOS name if not already
463 * set from the config file.
466 if (!*global_myname) {
468 fstrcpy( global_myname, myhostname() );
469 p = strchr( global_myname, '.' );
474 strupper( global_myname );
483 /* for LSA handles */
484 init_lsa_policy_hnd();
486 /* for SPOOLSS handles */
494 /****************************************************************************
496 ****************************************************************************/
497 static void usage(char *pname)
500 printf("Usage: %s [-DaoPh?V] [-d debuglevel] [-l log basename] [-p port]\n", pname);
501 printf(" [-O socket options] [-s services file]\n");
502 printf("\t-D Become a daemon\n");
503 printf("\t-a Append to log file (default)\n");
504 printf("\t-o Overwrite log file, don't append\n");
505 printf("\t-h Print usage\n");
506 printf("\t-? Print usage\n");
507 printf("\t-V Print version\n");
508 printf("\t-d debuglevel Set the debuglevel\n");
509 printf("\t-l log basename. Basename for log/debug files\n");
510 printf("\t-p port Listen on the specified port\n");
511 printf("\t-O socket options Socket options\n");
512 printf("\t-s services file. Filename of services file\n");
517 /****************************************************************************
519 ****************************************************************************/
520 int main(int argc,char *argv[])
522 extern BOOL append_log;
523 /* shall I run as a daemon */
524 BOOL is_daemon = False;
525 BOOL specified_logfile = False;
530 #ifdef HAVE_SET_AUTH_PARAMETERS
531 set_auth_parameters(argc,argv);
534 /* this is for people who can't start the program correctly */
535 while (argc > 1 && (*argv[1] != '-')) {
540 while ( EOF != (opt = getopt(argc, argv, "O:l:s:d:Dp:h?Vaof:")) )
543 pstrcpy(user_socket_options,optarg);
547 pstrcpy(servicesf,optarg);
551 specified_logfile = True;
552 pstrcpy(debugf,optarg);
571 DEBUGLEVEL = atoi(optarg);
585 printf("Version %s\n",VERSION);
589 DEBUG(0,("Incorrect program usage - are you sure the command line is correct?\n"));
595 /* needed for SecureWare on SCO */
600 * gain_root_privilege uses an assert than will cause a core
601 * dump if euid != 0. Ensure this is the case.
604 if(geteuid() != (uid_t)0) {
605 fprintf(stderr, "%s: Version %s : Must have effective user id of zero to run.\n", argv[0], VERSION);
613 if(!specified_logfile) {
614 slprintf(debugf, sizeof(debugf), "%s/log.smbd", LOGFILEBASE);
617 pstrcpy(remote_machine, "smb");
619 setup_logging(argv[0],False);
621 charset_initialise();
623 /* we want to re-seed early to prevent time delays causing
624 client problems at a later date. (tridge) */
625 generate_random_buffer(NULL, 0, False);
627 /* make absolutely sure we run as root - to handle cases where people
628 are crazy enough to have it setuid */
630 gain_root_privilege();
631 gain_root_group_privilege();
633 fault_setup((void (*)(void *))exit_server);
634 CatchSignal(SIGTERM , SIGNAL_CAST dflt_sig);
636 /* we are never interested in SIGPIPE */
637 BlockSignals(True,SIGPIPE);
640 /* we are never interested in SIGFPE */
641 BlockSignals(True,SIGFPE);
644 /* we want total control over the permissions on created files,
645 so set our umask to 0 */
648 dos_GetWd(OriginalDir);
654 DEBUG(1,( "smbd version %s started.\n", VERSION));
655 DEBUGADD(1,( "Copyright Andrew Tridgell 1992-1998\n"));
657 DEBUG(2,("uid=%d gid=%d euid=%d egid=%d\n",
658 (int)getuid(),(int)getgid(),(int)geteuid(),(int)getegid()));
660 if (sizeof(uint16) < 2 || sizeof(uint32) < 4) {
661 DEBUG(0,("ERROR: Samba is not configured correctly for the word size on your machine\n"));
666 * Do this before reload_services.
669 if (!reload_services(False))
675 if (!profile_setup(False)) {
676 DEBUG(0,("ERROR: failed to setup profiling\n"));
683 extern BOOL sslEnabled;
684 sslEnabled = lp_ssl_enabled();
688 #endif /* WITH_SSL */
690 codepage_initialise(lp_client_code_page());
692 fstrcpy(global_myworkgroup, lp_workgroup());
694 CatchSignal(SIGHUP,SIGNAL_CAST sig_hup);
696 /* Setup the signals that allow the debug log level
697 to by dynamically changed. */
699 /* If we are using the malloc debug code we can't use
700 SIGUSR1 and SIGUSR2 to do debug level changes. */
703 CatchSignal( SIGUSR1, SIGNAL_CAST sig_usr1 );
707 CatchSignal( SIGUSR2, SIGNAL_CAST sig_usr2 );
710 DEBUG(3,( "loaded services\n"));
712 if (!is_daemon && !is_a_socket(0)) {
713 DEBUG(0,("standard input is not a socket, assuming -D option\n"));
718 DEBUG( 3, ( "Becoming a daemon.\n" ) );
722 if (!directory_exist(lp_lockdir(), NULL)) {
723 mkdir(lp_lockdir(), 0755);
726 check_kernel_oplocks();
729 pidfile_create("smbd");
732 if (!open_sockets(is_daemon,port))
736 * Note that this call should be done after the fork() call
737 * in open_sockets(), as some versions of the locking shared
738 * memory code register openers in a flat file.
741 if (!locking_init(0))
744 if (!print_backend_init()) {
748 if(!initialize_password_db())
751 /* possibly reload the services file. */
752 reload_services(True);
754 if(!pdb_generate_sam_sid()) {
755 DEBUG(0,("ERROR: Samba cannot create a SAM SID.\n"));
760 if (sys_chroot(lp_rootdir()) == 0)
761 DEBUG(2,("Changed root to %s\n", lp_rootdir()));
764 /* Setup the oplock IPC socket. */
765 if( !open_oplock_ipc() )
770 exit_server("normal exit");