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;
46 extern fstring remote_machine;
48 /* really we should have a top level context structure that has the
49 client file descriptor as an element. That would require a major rewrite :(
51 the following 2 functions are an alternative - they make the file
52 descriptor private to smbd
54 static int server_fd = -1;
56 int smbd_server_fd(void)
61 void smbd_set_server_fd(int fd)
67 /****************************************************************************
68 when exiting, take the whole family
69 ****************************************************************************/
70 static void *dflt_sig(void)
72 exit_server("caught signal");
76 /****************************************************************************
77 Send a SIGTERM to our process group.
78 *****************************************************************************/
79 static void killkids(void)
81 if(am_parent) kill(0,SIGTERM);
85 /****************************************************************************
86 open the socket communication
87 ****************************************************************************/
88 static BOOL open_sockets_inetd(void)
90 /* Started from inetd. fd 0 is the socket. */
91 /* We will abort gracefully when the client or remote system
93 smbd_set_server_fd(dup(0));
95 /* close our standard file descriptors */
98 set_socket_options(smbd_server_fd(),"SO_KEEPALIVE");
99 set_socket_options(smbd_server_fd(),user_socket_options);
105 /****************************************************************************
106 open the socket communication
107 ****************************************************************************/
108 static BOOL open_sockets(BOOL is_daemon,int port)
110 int num_interfaces = iface_count();
111 int fd_listenset[FD_SETSIZE];
117 return open_sockets_inetd();
123 static int atexit_set;
124 if(atexit_set == 0) {
135 FD_ZERO(&listen_set);
137 if(lp_interfaces() && lp_bind_interfaces_only()) {
138 /* We have been given an interfaces line, and been
139 told to only bind to those interfaces. Create a
140 socket per interface and bind to only these.
143 if(num_interfaces > FD_SETSIZE) {
144 DEBUG(0,("open_sockets: Too many interfaces specified to bind to. Number was %d \
146 num_interfaces, FD_SETSIZE));
150 /* Now open a listen socket for each of the
152 for(i = 0; i < num_interfaces; i++) {
153 struct in_addr *ifip = iface_n_ip(i);
156 DEBUG(0,("open_sockets: interface %d has NULL IP address !\n", i));
159 s = fd_listenset[i] = open_socket_in(SOCK_STREAM, port, 0, ifip->s_addr, True);
162 /* ready to listen */
163 if (listen(s, 5) == -1) {
164 DEBUG(0,("listen: %s\n",strerror(errno)));
168 FD_SET(s,&listen_set);
171 /* Just bind to 0.0.0.0 - accept connections
175 /* open an incoming socket */
176 s = open_socket_in(SOCK_STREAM, port, 0,
177 interpret_addr(lp_socket_address()),True);
181 /* ready to listen */
182 if (listen(s, 5) == -1) {
183 DEBUG(0,("open_sockets: listen: %s\n",
190 FD_SET(s,&listen_set);
193 /* now accept incoming connections - forking a new process
194 for each incoming connection */
195 DEBUG(2,("waiting for a connection\n"));
200 memcpy((char *)&lfds, (char *)&listen_set,
203 num = sys_select(FD_SETSIZE,&lfds,NULL);
205 if (num == -1 && errno == EINTR) {
206 extern VOLATILE SIG_ATOMIC_T reload_after_sighup;
208 /* check for sighup processing */
209 if (reload_after_sighup) {
211 DEBUG(1,("Reloading services after SIGHUP\n"));
212 reload_services(False);
213 reload_after_sighup = False;
219 /* check if we need to reload services */
220 check_reload(time(NULL));
222 /* Find the sockets that are read-ready -
224 for( ; num > 0; num--) {
225 struct sockaddr addr;
226 int in_addrlen = sizeof(addr);
229 for(i = 0; i < num_interfaces; i++) {
230 if(FD_ISSET(fd_listenset[i],&lfds)) {
232 /* Clear this so we don't look
234 FD_CLR(fd_listenset[i],&lfds);
239 smbd_set_server_fd(accept(s,&addr,&in_addrlen));
241 if (smbd_server_fd() == -1 && errno == EINTR)
244 if (smbd_server_fd() == -1) {
245 DEBUG(0,("open_sockets: accept: %s\n",
250 if (smbd_server_fd() != -1 && sys_fork()==0) {
253 /* close the listening socket(s) */
254 for(i = 0; i < num_interfaces; i++)
255 close(fd_listenset[i]);
257 /* close our standard file
262 set_socket_options(smbd_server_fd(),"SO_KEEPALIVE");
263 set_socket_options(smbd_server_fd(),user_socket_options);
265 /* Reset global variables in util.c so
266 that client substitutions will be
267 done correctly in the process. */
268 reset_globals_after_fork();
272 /* The parent doesn't need this socket */
273 close(smbd_server_fd());
275 /* Force parent to check log size after
276 * spawning child. Fix from
277 * klausr@ITAP.Physik.Uni-Stuttgart.De. The
278 * parent smbd will log to logserver.smb. It
279 * writes only two messages for each child
280 * started/finished. But each child writes,
281 * say, 50 messages also in logserver.smb,
282 * begining with the debug_count of the
283 * parent, before the child opens its own log
284 * file logserver.client. In a worst case
285 * scenario the size of logserver.smb would be
286 * checked after about 50*50=2500 messages
289 force_check_log_size();
294 /* NOTREACHED return True; */
297 /****************************************************************************
298 reload the services file
299 **************************************************************************/
300 BOOL reload_services(BOOL test)
306 pstrcpy(fname,lp_configfile());
307 if (file_exist(fname,NULL) && !strcsequal(fname,servicesf)) {
308 pstrcpy(servicesf,fname);
315 if (test && !lp_file_list_changed())
318 lp_killunused(conn_snum_used);
320 ret = lp_load(servicesf,False,False,True);
324 /* perhaps the config filename is now set */
326 reload_services(True);
333 if (smbd_server_fd() != -1) {
334 set_socket_options(smbd_server_fd(),"SO_KEEPALIVE");
335 set_socket_options(smbd_server_fd(),user_socket_options);
339 reset_mangled_cache();
342 /* this forces service parameters to be flushed */
343 become_service(NULL,True);
350 /****************************************************************************
352 ****************************************************************************/
354 VOLATILE SIG_ATOMIC_T reload_after_sighup = False;
356 static void sig_hup(int sig)
358 BlockSignals(True,SIGHUP);
359 DEBUG(0,("Got SIGHUP\n"));
362 reload_after_sighup = True;
363 BlockSignals(False,SIGHUP);
369 /*******************************************************************
370 prepare to dump a core file - carefully!
371 ********************************************************************/
372 static BOOL dump_core(void)
376 pstrcpy(dname,debugf);
377 if ((p=strrchr(dname,'/'))) *p=0;
378 pstrcat(dname,"/corefiles");
380 sys_chown(dname,getuid(),getgid());
382 if (chdir(dname)) return(False);
385 #ifdef HAVE_GETRLIMIT
389 getrlimit(RLIMIT_CORE, &rlp);
390 rlp.rlim_cur = MAX(4*1024*1024,rlp.rlim_cur);
391 setrlimit(RLIMIT_CORE, &rlp);
392 getrlimit(RLIMIT_CORE, &rlp);
393 DEBUG(3,("Core limits now %d %d\n",
394 (int)rlp.rlim_cur,(int)rlp.rlim_max));
400 DEBUG(0,("Dumping core in %s\n",dname));
406 /****************************************************************************
408 ****************************************************************************/
409 void exit_server(char *reason)
411 static int firsttime=1;
412 extern char *last_inbuf;
415 if (!firsttime) exit(0);
419 DEBUG(2,("Closing connections\n"));
423 respond_to_all_remaining_local_messages();
426 if (dcelogin_atmost_once) {
432 int oldlevel = DEBUGLEVEL;
434 DEBUG(0,("Last message was %s\n",smb_fn_name(last_message)));
436 show_msg(last_inbuf);
437 DEBUGLEVEL = oldlevel;
438 DEBUG(0,("===============================================================\n"));
440 if (dump_core()) return;
446 DEBUG(3,("Server exit (%s)\n", (reason ? reason : "")));
450 /****************************************************************************
451 initialise connect, service and file structs
452 ****************************************************************************/
453 static void init_structs(void )
456 * Set the machine NETBIOS name if not already
457 * set from the config file.
460 if (!*global_myname) {
462 fstrcpy( global_myname, myhostname() );
463 p = strchr( global_myname, '.' );
468 strupper( global_myname );
477 /* for LSA handles */
478 init_lsa_policy_hnd();
480 /* for SPOOLSS handles */
488 /****************************************************************************
490 ****************************************************************************/
491 static void usage(char *pname)
494 printf("Usage: %s [-DaoPh?V] [-d debuglevel] [-l log basename] [-p port]\n", pname);
495 printf(" [-O socket options] [-s services file]\n");
496 printf("\t-D Become a daemon\n");
497 printf("\t-a Append to log file (default)\n");
498 printf("\t-o Overwrite log file, don't append\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[])
516 extern BOOL append_log;
517 /* shall I run as a daemon */
518 BOOL is_daemon = False;
519 BOOL specified_logfile = False;
524 #ifdef HAVE_SET_AUTH_PARAMETERS
525 set_auth_parameters(argc,argv);
528 /* this is for people who can't start the program correctly */
529 while (argc > 1 && (*argv[1] != '-')) {
534 while ( EOF != (opt = getopt(argc, argv, "O:l:s:d:Dp:h?Vaof:")) )
537 pstrcpy(user_socket_options,optarg);
541 pstrcpy(servicesf,optarg);
545 specified_logfile = True;
546 pstrcpy(debugf,optarg);
565 DEBUGLEVEL = atoi(optarg);
579 printf("Version %s\n",VERSION);
583 DEBUG(0,("Incorrect program usage - are you sure the command line is correct?\n"));
589 /* needed for SecureWare on SCO */
594 * gain_root_privilege uses an assert than will cause a core
595 * dump if euid != 0. Ensure this is the case.
598 if(geteuid() != (uid_t)0) {
599 fprintf(stderr, "%s: Version %s : Must have effective user id of zero to run.\n", argv[0], VERSION);
607 if(!specified_logfile) {
608 slprintf(debugf, sizeof(debugf), "%s/log.smbd", LOGFILEBASE);
611 pstrcpy(remote_machine, "smb");
613 setup_logging(argv[0],False);
615 charset_initialise();
617 /* we want to re-seed early to prevent time delays causing
618 client problems at a later date. (tridge) */
619 generate_random_buffer(NULL, 0, False);
621 /* make absolutely sure we run as root - to handle cases where people
622 are crazy enough to have it setuid */
624 gain_root_privilege();
625 gain_root_group_privilege();
627 fault_setup((void (*)(void *))exit_server);
628 CatchSignal(SIGTERM , SIGNAL_CAST dflt_sig);
630 /* we are never interested in SIGPIPE */
631 BlockSignals(True,SIGPIPE);
634 /* we are never interested in SIGFPE */
635 BlockSignals(True,SIGFPE);
638 /* we want total control over the permissions on created files,
639 so set our umask to 0 */
646 DEBUG(1,( "smbd version %s started.\n", VERSION));
647 DEBUGADD(1,( "Copyright Andrew Tridgell 1992-1998\n"));
649 DEBUG(2,("uid=%d gid=%d euid=%d egid=%d\n",
650 (int)getuid(),(int)getgid(),(int)geteuid(),(int)getegid()));
652 if (sizeof(uint16) < 2 || sizeof(uint32) < 4) {
653 DEBUG(0,("ERROR: Samba is not configured correctly for the word size on your machine\n"));
658 * Do this before reload_services.
661 if (!reload_services(False))
667 if (!profile_setup(False)) {
668 DEBUG(0,("ERROR: failed to setup profiling\n"));
675 extern BOOL sslEnabled;
676 sslEnabled = lp_ssl_enabled();
680 #endif /* WITH_SSL */
682 codepage_initialise(lp_client_code_page());
684 fstrcpy(global_myworkgroup, lp_workgroup());
686 CatchSignal(SIGHUP,SIGNAL_CAST sig_hup);
688 DEBUG(3,( "loaded services\n"));
690 if (!is_daemon && !is_a_socket(0)) {
691 DEBUG(0,("standard input is not a socket, assuming -D option\n"));
696 DEBUG( 3, ( "Becoming a daemon.\n" ) );
700 if (!directory_exist(lp_lockdir(), NULL)) {
701 mkdir(lp_lockdir(), 0755);
705 pidfile_create("smbd");
708 if (!open_sockets(is_daemon,port))
712 * everything after this point is run after the fork()
715 if (!message_init()) {
719 if (!locking_init(0)) {
723 if (!print_backend_init()) {
727 if(!initialize_password_db()) {
731 /* possibly reload the services file. */
732 reload_services(True);
734 if(!pdb_generate_sam_sid()) {
735 DEBUG(0,("ERROR: Samba cannot create a SAM SID.\n"));
740 if (sys_chroot(lp_rootdir()) == 0)
741 DEBUG(2,("Changed root to %s\n", lp_rootdir()));
745 if (!init_oplocks()) {
749 /* Setup change notify */
750 if (!init_change_notify()) {
756 exit_server("normal exit");