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 fstring global_sam_name;
29 extern pstring global_myname;
33 /* the last message the was processed */
34 int last_message = -1;
36 /* a useful macro to debug the last message processed */
37 #define LAST_MESSAGE() smb_fn_name(last_message)
40 extern int DEBUGLEVEL;
42 extern pstring user_socket_options;
45 extern int dcelogin_atmost_once;
49 extern fstring remote_machine;
50 extern pstring OriginalDir;
51 extern pstring myhostname;
53 /****************************************************************************
54 when exiting, take the whole family
55 ****************************************************************************/
56 static void *dflt_sig(void)
58 exit_server("caught signal");
62 /****************************************************************************
63 Send a SIGTERM to our process group.
64 *****************************************************************************/
65 static void killkids(void)
67 if(am_parent) kill(0,SIGTERM);
71 /****************************************************************************
72 open the socket communication
73 ****************************************************************************/
74 static BOOL open_sockets_inetd(void)
78 /* Started from inetd. fd 0 is the socket. */
79 /* We will abort gracefully when the client or remote system
83 /* close our standard file descriptors */
86 set_socket_options(Client,"SO_KEEPALIVE");
87 set_socket_options(Client,user_socket_options);
93 /****************************************************************************
94 open the socket communication
95 ****************************************************************************/
96 static BOOL open_sockets(BOOL is_daemon,int port)
99 int num_interfaces = iface_count();
100 int fd_listenset[FD_SETSIZE];
106 return open_sockets_inetd();
112 static int atexit_set;
113 if(atexit_set == 0) {
124 FD_ZERO(&listen_set);
126 if(lp_interfaces() && lp_bind_interfaces_only()) {
127 /* We have been given an interfaces line, and been
128 told to only bind to those interfaces. Create a
129 socket per interface and bind to only these.
132 if(num_interfaces > FD_SETSIZE) {
133 DEBUG(0,("open_sockets: Too many interfaces specified to bind to. Number was %d \
135 num_interfaces, FD_SETSIZE));
139 /* Now open a listen socket for each of the
141 for(i = 0; i < num_interfaces; i++) {
142 struct in_addr *ifip = iface_n_ip(i);
145 DEBUG(0,("open_sockets: interface %d has NULL IP address !\n", i));
148 s = fd_listenset[i] = open_socket_in(SOCK_STREAM, port, 0, ifip->s_addr);
151 /* ready to listen */
152 if (listen(s, 5) == -1) {
153 DEBUG(0,("listen: %s\n",strerror(errno)));
157 FD_SET(s,&listen_set);
160 /* Just bind to 0.0.0.0 - accept connections
164 /* open an incoming socket */
165 s = open_socket_in(SOCK_STREAM, port, 0,
166 interpret_addr(lp_socket_address()));
170 /* ready to listen */
171 if (listen(s, 5) == -1) {
172 DEBUG(0,("open_sockets: listen: %s\n",
179 FD_SET(s,&listen_set);
182 /* now accept incoming connections - forking a new process
183 for each incoming connection */
184 DEBUG(2,("waiting for a connection\n"));
189 memcpy((char *)&lfds, (char *)&listen_set,
192 num = sys_select(256,&lfds,NULL);
194 if (num == -1 && errno == EINTR)
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)
288 pstrcpy(fname,lp_configfile());
289 if (file_exist(fname,NULL) && !strcsequal(fname,servicesf)) {
290 pstrcpy(servicesf,fname);
297 if (test && !lp_file_list_changed())
300 lp_killunused(conn_snum_used);
302 ret = lp_load(servicesf,False,False,True);
306 /* perhaps the config filename is now set */
308 reload_services(True);
317 set_socket_options(Client,"SO_KEEPALIVE");
318 set_socket_options(Client,user_socket_options);
322 reset_mangled_cache();
324 /* this forces service parameters to be flushed */
325 become_service(NULL,True);
332 /****************************************************************************
333 this prevents zombie child processes
334 ****************************************************************************/
335 BOOL reload_after_sighup = False;
337 static void sig_hup(int sig)
339 BlockSignals(True,SIGHUP);
340 DEBUG(0,("Got SIGHUP\n"));
343 * Fix from <branko.cibej@hermes.si> here.
344 * We used to reload in the signal handler - this
348 reload_after_sighup = True;
349 BlockSignals(False,SIGHUP);
355 /*******************************************************************
356 prepare to dump a core file - carefully!
357 ********************************************************************/
358 static BOOL dump_core(void)
362 pstrcpy(dname,debugf);
363 if ((p=strrchr(dname,'/'))) *p=0;
364 pstrcat(dname,"/corefiles");
366 sys_chown(dname,getuid(),getgid());
368 if (chdir(dname)) return(False);
371 #ifdef HAVE_GETRLIMIT
375 getrlimit(RLIMIT_CORE, &rlp);
376 rlp.rlim_cur = MAX(4*1024*1024,rlp.rlim_cur);
377 setrlimit(RLIMIT_CORE, &rlp);
378 getrlimit(RLIMIT_CORE, &rlp);
379 DEBUG(3,("Core limits now %d %d\n",
380 (int)rlp.rlim_cur,(int)rlp.rlim_max));
386 DEBUG(0,("Dumping core in %s\n",dname));
393 /****************************************************************************
395 ****************************************************************************/
396 void exit_server(char *reason)
398 static int firsttime=1;
399 extern char *last_inbuf;
402 if (!firsttime) exit(0);
406 DEBUG(2,("Closing connections\n"));
411 if (dcelogin_atmost_once) {
417 int oldlevel = DEBUGLEVEL;
419 DEBUG(0,("Last message was %s\n",smb_fn_name(last_message)));
421 show_msg(last_inbuf);
422 DEBUGLEVEL = oldlevel;
423 DEBUG(0,("===============================================================\n"));
425 if (dump_core()) return;
431 DEBUG(3,("Server exit (%s)\n", (reason ? reason : "")));
435 smb_mem_write_verbose(dbf);
444 /****************************************************************************
445 initialise connect, service and file structs
446 ****************************************************************************/
447 static void init_structs(void )
449 get_myname(myhostname,NULL);
452 * Set the machine NETBIOS name if not already
453 * set from the config file.
456 if (!*global_myname) {
458 fstrcpy( global_myname, myhostname );
459 p = strchr( global_myname, '.' );
464 strupper( global_myname );
473 /* for LSA handles */
474 init_lsa_policy_hnd();
479 /****************************************************************************
481 ****************************************************************************/
482 static void usage(char *pname)
484 DEBUG(0,("Incorrect program usage - are you sure the command line is correct?\n"));
486 printf("Usage: %s [-D] [-p port] [-d debuglevel] ", pname);
487 printf("[-l log basename] [-s services file]\n" );
488 printf("Version %s\n",VERSION);
489 printf("\t-D become a daemon\n");
490 printf("\t-p port listen on the specified port\n");
491 printf("\t-d debuglevel set the debuglevel\n");
492 printf("\t-l log basename. Basename for log/debug files\n");
493 printf("\t-s services file. Filename of services file\n");
494 printf("\t-P passive only\n");
495 printf("\t-a append to log file (default)\n");
496 printf("\t-o overwrite log file, don't append\n");
497 printf("\t-i scope NetBIOS scope to use (default none)\n");
502 /****************************************************************************
504 ****************************************************************************/
505 int main(int argc,char *argv[])
507 extern BOOL append_log;
508 /* shall I run as a daemon */
509 BOOL is_daemon = False;
514 #ifdef HAVE_SET_AUTH_PARAMETERS
515 set_auth_parameters(argc,argv);
519 /* needed for SecureWare on SCO */
527 pstrcpy(debugf,SMBLOGFILE);
529 pstrcpy(remote_machine, "smb");
531 setup_logging(argv[0],False);
533 charset_initialise();
535 /* make absolutely sure we run as root - to handle cases where people
536 are crazy enough to have it setuid */
537 #ifdef HAVE_SETRESUID
546 fault_setup((void (*)(void *))exit_server);
547 CatchSignal(SIGTERM , SIGNAL_CAST dflt_sig);
549 /* we are never interested in SIGPIPE */
550 BlockSignals(True,SIGPIPE);
552 /* we want total control over the permissions on created files,
553 so set our umask to 0 */
560 /* this is for people who can't start the program correctly */
561 while (argc > 1 && (*argv[1] != '-')) {
566 while ( EOF != (opt = getopt(argc, argv, "O:i:l:s:d:Dp:h?Paof:")) )
569 pstrcpy(user_socket_options,optarg);
573 pstrcpy(scope,optarg);
584 pstrcpy(servicesf,optarg);
588 pstrcpy(debugf,optarg);
607 DEBUGLEVEL = atoi(optarg);
627 DEBUG(1,( "smbd version %s started.\n", VERSION));
628 DEBUGADD(1,( "Copyright Andrew Tridgell 1992-1998\n"));
630 DEBUG(2,("uid=%d gid=%d euid=%d egid=%d\n",
631 (int)getuid(),(int)getgid(),(int)geteuid(),(int)getegid()));
633 if (sizeof(uint16) < 2 || sizeof(uint32) < 4) {
634 DEBUG(0,("ERROR: Samba is not configured correctly for the word size on your machine\n"));
638 if (!reload_services(False))
645 extern BOOL sslEnabled;
646 sslEnabled = lp_ssl_enabled();
650 #endif /* WITH_SSL */
652 codepage_initialise(lp_client_code_page());
654 fstrcpy(global_myworkgroup, lp_workgroup());
656 get_sam_domain_name();
658 generate_wellknown_sids();
660 if (!generate_sam_sid())
662 DEBUG(0,("ERROR: Samba cannot create a SAM SID.\n"));
666 if (!get_member_domain_sid())
668 DEBUG(0,("ERROR: Samba cannot obtain PDC SID from PDC(s) %s.\n",
669 lp_passwordserver()));
673 CatchSignal(SIGHUP,SIGNAL_CAST sig_hup);
675 /* Setup the signals that allow the debug log level
676 to by dynamically changed. */
678 /* If we are using the malloc debug code we can't use
679 SIGUSR1 and SIGUSR2 to do debug level changes. */
683 CatchSignal( SIGUSR1, SIGNAL_CAST sig_usr1 );
687 CatchSignal( SIGUSR2, SIGNAL_CAST sig_usr2 );
691 DEBUG(3,( "loaded services\n"));
693 if (!is_daemon && !is_a_socket(0)) {
694 DEBUG(0,("standard input is not a socket, assuming -D option\n"));
699 DEBUG( 3, ( "Becoming a daemon.\n" ) );
703 check_kernel_oplocks();
705 if (!directory_exist(lp_lockdir(), NULL)) {
706 mkdir(lp_lockdir(), 0755);
710 pidfile_create("smbd");
713 if (!open_sockets(is_daemon,port))
716 if (!locking_init(0))
719 if(!initialise_passgrp_db())
722 if(!initialise_password_db())
725 if(!initialise_group_db())
728 if(!initialise_alias_db())
731 /* possibly reload the services file. */
732 reload_services(True);
735 if (sys_chroot(lp_rootdir()) == 0)
736 DEBUG(2,("Changed root to %s\n", lp_rootdir()));
739 /* Setup the oplock IPC socket. */
740 if( !open_oplock_ipc() )
746 exit_server("normal exit");