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.
24 extern fstring global_myworkgroup;
25 extern pstring global_myname;
29 /* the last message the was processed */
30 int last_message = -1;
32 /* a useful macro to debug the last message processed */
33 #define LAST_MESSAGE() smb_fn_name(last_message)
35 extern pstring user_socket_options;
38 extern int dcelogin_atmost_once;
41 extern fstring remote_machine;
43 /* really we should have a top level context structure that has the
44 client file descriptor as an element. That would require a major rewrite :(
46 the following 2 functions are an alternative - they make the file
47 descriptor private to smbd
49 static int server_fd = -1;
51 int smbd_server_fd(void)
56 void smbd_set_server_fd(int fd)
62 /****************************************************************************
63 when exiting, take the whole family
64 ****************************************************************************/
65 static void *dflt_sig(void)
67 exit_server("caught signal");
71 /****************************************************************************
72 Send a SIGTERM to our process group.
73 *****************************************************************************/
74 static void killkids(void)
76 if(am_parent) kill(0,SIGTERM);
79 /****************************************************************************
80 process a sam sync message - not sure whether to do this here or
82 ****************************************************************************/
83 static void msg_sam_sync(int UNUSED(msg_type), pid_t UNUSED(pid),
84 void *UNUSED(buf), size_t UNUSED(len))
86 DEBUG(10, ("** sam sync message received, ignoring\n"));
89 /****************************************************************************
90 process a sam sync replicate message - not sure whether to do this here or
92 ****************************************************************************/
93 static void msg_sam_repl(int msg_type, pid_t pid, void *buf, size_t len)
97 if (len != sizeof(uint32))
100 low_serial = *((uint32 *)buf);
102 DEBUG(3, ("received sam replication message, serial = 0x%04x\n",
106 /****************************************************************************
107 open the socket communication
108 ****************************************************************************/
109 static BOOL open_sockets_inetd(void)
111 /* Started from inetd. fd 0 is the socket. */
112 /* We will abort gracefully when the client or remote system
114 smbd_set_server_fd(dup(0));
116 /* close our standard file descriptors */
119 set_socket_options(smbd_server_fd(),"SO_KEEPALIVE");
120 set_socket_options(smbd_server_fd(),user_socket_options);
126 /****************************************************************************
127 open the socket communication
128 ****************************************************************************/
129 static BOOL open_sockets(BOOL is_daemon,int port)
131 int num_interfaces = iface_count();
132 int fd_listenset[FD_SETSIZE];
138 return open_sockets_inetd();
144 static int atexit_set;
145 if(atexit_set == 0) {
156 FD_ZERO(&listen_set);
158 if(lp_interfaces() && lp_bind_interfaces_only()) {
159 /* We have been given an interfaces line, and been
160 told to only bind to those interfaces. Create a
161 socket per interface and bind to only these.
164 if(num_interfaces > FD_SETSIZE) {
165 DEBUG(0,("open_sockets: Too many interfaces specified to bind to. Number was %d \
167 num_interfaces, FD_SETSIZE));
171 /* Now open a listen socket for each of the
173 for(i = 0; i < num_interfaces; i++) {
174 struct in_addr *ifip = iface_n_ip(i);
177 DEBUG(0,("open_sockets: interface %d has NULL IP address !\n", i));
180 s = fd_listenset[i] = open_socket_in(SOCK_STREAM, port, 0, ifip->s_addr, True);
184 /* ready to listen */
185 set_socket_options(s,"SO_KEEPALIVE");
186 set_socket_options(s,user_socket_options);
188 if (listen(s, 5) == -1) {
189 DEBUG(0,("listen: %s\n",strerror(errno)));
193 FD_SET(s,&listen_set);
196 /* Just bind to 0.0.0.0 - accept connections
200 /* open an incoming socket */
201 s = open_socket_in(SOCK_STREAM, port, 0,
202 interpret_addr(lp_socket_address()),True);
206 /* ready to listen */
207 set_socket_options(s,"SO_KEEPALIVE");
208 set_socket_options(s,user_socket_options);
210 if (listen(s, 5) == -1) {
211 DEBUG(0,("open_sockets: listen: %s\n",
218 FD_SET(s,&listen_set);
221 /* Listen to messages */
223 message_register(MSG_SMB_SAM_SYNC, msg_sam_sync);
224 message_register(MSG_SMB_SAM_REPL, msg_sam_repl);
226 /* now accept incoming connections - forking a new process
227 for each incoming connection */
228 DEBUG(2,("waiting for a connection\n"));
233 /* Free up temporary memory from the main smbd. */
236 /* Ensure we respond to PING and DEBUG messages from the main smbd. */
239 memcpy((char *)&lfds, (char *)&listen_set,
242 num = sys_select(FD_SETSIZE,&lfds,NULL);
244 if (num == -1 && errno == EINTR) {
245 extern VOLATILE sig_atomic_t reload_after_sighup;
247 /* check for sighup processing */
248 if (reload_after_sighup) {
249 change_to_root_user();
250 DEBUG(1,("Reloading services after SIGHUP\n"));
251 reload_services(False);
252 reload_after_sighup = False;
258 /* check if we need to reload services */
259 check_reload(time(NULL));
261 /* Find the sockets that are read-ready -
263 for( ; num > 0; num--) {
264 struct sockaddr addr;
265 socklen_t in_addrlen = sizeof(addr);
268 for(i = 0; i < num_interfaces; i++) {
269 if(FD_ISSET(fd_listenset[i],&lfds)) {
271 /* Clear this so we don't look
273 FD_CLR(fd_listenset[i],&lfds);
278 smbd_set_server_fd(accept(s,&addr,&in_addrlen));
280 if (smbd_server_fd() == -1 && errno == EINTR)
283 if (smbd_server_fd() == -1) {
284 DEBUG(0,("open_sockets: accept: %s\n",
289 if (smbd_server_fd() != -1 && sys_fork()==0) {
292 /* close the listening socket(s) */
293 for(i = 0; i < num_interfaces; i++)
294 close(fd_listenset[i]);
296 /* close our standard file
301 set_socket_options(smbd_server_fd(),"SO_KEEPALIVE");
302 set_socket_options(smbd_server_fd(),user_socket_options);
304 /* Reset global variables in util.c so
305 that client substitutions will be
306 done correctly in the process. */
307 reset_globals_after_fork();
309 /* tdb needs special fork handling */
314 /* The parent doesn't need this socket */
315 close(smbd_server_fd());
317 /* Sun May 6 18:56:14 2001 ackley@cs.unm.edu:
318 Clear the closed fd info out of server_fd --
319 and more importantly, out of client_fd in
320 util_sock.c, to avoid a possible
321 getpeername failure if we reopen the logs
322 and use %I in the filename.
325 smbd_set_server_fd(-1);
327 /* Force parent to check log size after
328 * spawning child. Fix from
329 * klausr@ITAP.Physik.Uni-Stuttgart.De. The
330 * parent smbd will log to logserver.smb. It
331 * writes only two messages for each child
332 * started/finished. But each child writes,
333 * say, 50 messages also in logserver.smb,
334 * begining with the debug_count of the
335 * parent, before the child opens its own log
336 * file logserver.client. In a worst case
337 * scenario the size of logserver.smb would be
338 * checked after about 50*50=2500 messages
341 force_check_log_size();
346 /* NOTREACHED return True; */
349 /****************************************************************************
350 reload the services file
351 **************************************************************************/
352 BOOL reload_services(BOOL test)
358 pstrcpy(fname,lp_configfile());
359 if (file_exist(fname,NULL) && !strcsequal(fname,dyn_CONFIGFILE)) {
360 pstrcpy(dyn_CONFIGFILE,fname);
367 if (test && !lp_file_list_changed())
370 lp_killunused(conn_snum_used);
372 ret = lp_load(dyn_CONFIGFILE,False,False,True);
376 /* perhaps the config filename is now set */
378 reload_services(True);
385 if (smbd_server_fd() != -1) {
386 set_socket_options(smbd_server_fd(),"SO_KEEPALIVE");
387 set_socket_options(smbd_server_fd(),user_socket_options);
391 reset_mangled_cache();
394 /* this forces service parameters to be flushed */
395 set_current_service(NULL,True);
402 /****************************************************************************
404 ****************************************************************************/
406 VOLATILE sig_atomic_t reload_after_sighup = False;
408 static void sig_hup(int sig)
410 BlockSignals(True,SIGHUP);
411 DEBUG(0,("Got SIGHUP\n"));
414 reload_after_sighup = True;
415 BlockSignals(False,SIGHUP);
421 /*******************************************************************
422 prepare to dump a core file - carefully!
423 ********************************************************************/
424 static BOOL dump_core(void)
428 pstrcpy(dname,lp_logfile());
429 if ((p=strrchr_m(dname,'/'))) *p=0;
430 pstrcat(dname,"/corefiles");
432 sys_chown(dname,getuid(),getgid());
434 if (chdir(dname)) return(False);
437 #ifdef HAVE_GETRLIMIT
441 getrlimit(RLIMIT_CORE, &rlp);
442 rlp.rlim_cur = MAX(4*1024*1024,rlp.rlim_cur);
443 setrlimit(RLIMIT_CORE, &rlp);
444 getrlimit(RLIMIT_CORE, &rlp);
445 DEBUG(3,("Core limits now %d %d\n",
446 (int)rlp.rlim_cur,(int)rlp.rlim_max));
452 DEBUG(0,("Dumping core in %s\n",dname));
458 /****************************************************************************
459 update the current smbd process count
460 ****************************************************************************/
462 static void decrement_smbd_process_count(void)
466 if (lp_max_smbd_processes()) {
468 tdb_change_int32_atomic(conn_tdb_ctx(), "INFO/total_smbds", &total_smbds, -1);
472 /****************************************************************************
474 ****************************************************************************/
475 void exit_server(char *reason)
477 static int firsttime=1;
478 extern char *last_inbuf;
479 extern struct auth_context *negprot_global_auth_context;
485 change_to_root_user();
486 DEBUG(2,("Closing connections\n"));
488 if (negprot_global_auth_context) {
489 negprot_global_auth_context->free(&negprot_global_auth_context);
494 invalidate_all_vuids();
496 /* delete our entry in the connections database. */
497 yield_connection(NULL,"",MAXSTATUS);
499 respond_to_all_remaining_local_messages();
500 decrement_smbd_process_count();
503 if (dcelogin_atmost_once) {
509 int oldlevel = DEBUGLEVEL;
511 DEBUG(0,("Last message was %s\n",smb_fn_name(last_message)));
513 show_msg(last_inbuf);
514 DEBUGLEVEL = oldlevel;
515 DEBUG(0,("===============================================================\n"));
517 if (dump_core()) return;
523 DEBUG(3,("Server exit (%s)\n", (reason ? reason : "")));
527 /****************************************************************************
528 initialise connect, service and file structs
529 ****************************************************************************/
530 static void init_structs(void )
533 * Set the machine NETBIOS name if not already
534 * set from the config file.
537 if (!*global_myname) {
539 fstrcpy( global_myname, myhostname() );
540 p = strchr_m( global_myname, '.' );
545 strupper( global_myname );
559 /****************************************************************************
561 ****************************************************************************/
562 static void usage(char *pname)
565 d_printf("Usage: %s [-DaioPh?Vb] [-d debuglevel] [-l log basename] [-p port]\n", pname);
566 d_printf(" [-O socket options] [-s services file]\n");
567 d_printf("\t-D Become a daemon (default)\n");
568 d_printf("\t-a Append to log file (default)\n");
569 d_printf("\t-i Run interactive (not a daemon)\n" );
570 d_printf("\t-o Overwrite log file, don't append\n");
571 d_printf("\t-h Print usage\n");
572 d_printf("\t-? Print usage\n");
573 d_printf("\t-V Print version\n");
574 d_printf("\t-b Print build options\n");
575 d_printf("\t-d debuglevel Set the debuglevel\n");
576 d_printf("\t-l log basename. Basename for log/debug files\n");
577 d_printf("\t-p port Listen on the specified port\n");
578 d_printf("\t-O socket options Socket options\n");
579 d_printf("\t-s services file. Filename of services file\n");
583 /****************************************************************************
585 ****************************************************************************/
586 int main(int argc,char *argv[])
588 extern BOOL append_log;
590 /* shall I run as a daemon */
591 BOOL is_daemon = False;
592 BOOL interactive = False;
593 BOOL specified_logfile = False;
598 #ifdef HAVE_SET_AUTH_PARAMETERS
599 set_auth_parameters(argc,argv);
602 /* this is for people who can't start the program correctly */
603 while (argc > 1 && (*argv[1] != '-')) {
608 while ( EOF != (opt = getopt(argc, argv, "O:l:s:d:Dp:h?bVaiof:")) )
611 pstrcpy(user_socket_options,optarg);
615 pstrcpy(dyn_CONFIGFILE,optarg);
619 specified_logfile = True;
620 slprintf(logfile, sizeof(logfile)-1, "%s/log.smbd", optarg);
621 lp_set_logfile(logfile);
644 DEBUGLEVEL = atoi(optarg);
658 d_printf("Version %s\n",VERSION);
662 build_options(True); /* Display output to screen as well as debug */
666 DEBUG(0,("Incorrect program usage - are you sure the command line is correct?\n"));
672 /* needed for SecureWare on SCO */
682 if(!specified_logfile) {
683 slprintf(logfile, sizeof(logfile)-1, "%s/log.smbd",
685 lp_set_logfile(logfile);
688 pstrcpy(remote_machine, "smbd");
690 setup_logging(argv[0],interactive);
692 /* we want to re-seed early to prevent time delays causing
693 client problems at a later date. (tridge) */
694 generate_random_buffer(NULL, 0, False);
696 /* make absolutely sure we run as root - to handle cases where people
697 are crazy enough to have it setuid */
699 gain_root_privilege();
700 gain_root_group_privilege();
702 fault_setup((void (*)(void *))exit_server);
703 CatchSignal(SIGTERM , SIGNAL_CAST dflt_sig);
705 /* we are never interested in SIGPIPE */
706 BlockSignals(True,SIGPIPE);
709 /* we are never interested in SIGFPE */
710 BlockSignals(True,SIGFPE);
714 /* We are no longer interested in USR2 */
715 BlockSignals(True,SIGUSR2);
718 /* POSIX demands that signals are inherited. If the invoking process has
719 * these signals masked, we will have problems, as we won't recieve them. */
720 BlockSignals(False, SIGHUP);
721 BlockSignals(False, SIGUSR1);
723 /* we want total control over the permissions on created files,
724 so set our umask to 0 */
731 DEBUG(1,( "smbd version %s started.\n", VERSION));
732 DEBUGADD(1,( "Copyright Andrew Tridgell 1992-2002\n"));
734 DEBUG(2,("uid=%d gid=%d euid=%d egid=%d\n",
735 (int)getuid(),(int)getgid(),(int)geteuid(),(int)getegid()));
737 /* Output the build options to the debug log */
738 build_options(False);
740 if (sizeof(uint16) < 2 || sizeof(uint32) < 4) {
741 DEBUG(0,("ERROR: Samba is not configured correctly for the word size on your machine\n"));
746 * Do this before reload_services.
749 if (!reload_services(False))
754 /* don't call winbind for our domain if we are the DC */
755 if (lp_domain_logons()) {
756 winbind_exclude_domain(lp_workgroup());
760 if (!profile_setup(False)) {
761 DEBUG(0,("ERROR: failed to setup profiling\n"));
768 extern BOOL sslEnabled;
769 sslEnabled = lp_ssl_enabled();
773 #endif /* WITH_SSL */
775 fstrcpy(global_myworkgroup, lp_workgroup());
777 CatchSignal(SIGHUP,SIGNAL_CAST sig_hup);
779 DEBUG(3,( "loaded services\n"));
781 if (!is_daemon && !is_a_socket(0)) {
782 DEBUG(0,("standard input is not a socket, assuming -D option\n"));
786 if (is_daemon && !interactive) {
787 DEBUG( 3, ( "Becoming a daemon.\n" ) );
793 * If we're interactive we want to set our own process group for
797 setpgid( (pid_t)0, (pid_t)0);
800 if (!directory_exist(lp_lockdir(), NULL)) {
801 mkdir(lp_lockdir(), 0755);
805 pidfile_create("smbd");
808 if (!message_init()) {
811 register_msg_pool_usage();
813 /* Setup the main smbd so that we can get messages. */
814 claim_connection(NULL,"",MAXSTATUS,True);
817 DO NOT ENABLE THIS TILL YOU COPE WITH KILLING THESE TASKS AND INETD
818 THIS *killed* LOTS OF BUILD FARM MACHINES. IT CREATED HUNDREDS OF
819 smbd PROCESSES THAT NEVER DIE
820 start_background_queue();
823 if (!open_sockets(is_daemon,port))
827 * everything after this point is run after the fork()
830 if (!locking_init(0)) {
834 if (!print_backend_init()) {
838 if (!share_info_db_init()) {
842 if(!initialize_password_db(False)) {
846 /* possibly reload the services file. */
847 reload_services(True);
849 if(!pdb_generate_sam_sid()) {
850 DEBUG(0,("ERROR: Samba cannot create a SAM SID.\n"));
854 if (!init_group_mapping()) {
855 DEBUG(0,("Could not open tdb mapping file.\n"));
859 if (!init_account_policy()) {
860 DEBUG(0,("Could not open account policy tdb.\n"));
865 if (sys_chroot(lp_rootdir()) == 0)
866 DEBUG(2,("Changed root to %s\n", lp_rootdir()));
870 if (!init_oplocks()) {
875 if (!init_mangle_tdb()) {
879 /* Setup change notify */
880 if (!init_change_notify()) {
886 exit_server("normal exit");