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 pstring servicesf = CONFIGFILE;
25 extern pstring debugf;
26 extern fstring global_myworkgroup;
27 extern pstring global_myname;
31 /* the last message the was processed */
32 int last_message = -1;
34 /* a useful macro to debug the last message processed */
35 #define LAST_MESSAGE() smb_fn_name(last_message)
37 extern int DEBUGLEVEL;
39 extern pstring user_socket_options;
42 extern int dcelogin_atmost_once;
45 extern fstring remote_machine;
47 /* really we should have a top level context structure that has the
48 client file descriptor as an element. That would require a major rewrite :(
50 the following 2 functions are an alternative - they make the file
51 descriptor private to smbd
53 static int server_fd = -1;
55 int smbd_server_fd(void)
60 void smbd_set_server_fd(int fd)
66 /****************************************************************************
67 when exiting, take the whole family
68 ****************************************************************************/
69 static void *dflt_sig(void)
71 exit_server("caught signal");
75 /****************************************************************************
76 Send a SIGTERM to our process group.
77 *****************************************************************************/
78 static void killkids(void)
80 if(am_parent) kill(0,SIGTERM);
83 /****************************************************************************
84 process a sam sync message - not sure whether to do this here or
86 ****************************************************************************/
87 static void msg_sam_sync(int msg_type, pid_t pid, void *buf, size_t len)
89 DEBUG(10, ("** sam sync message received, ignoring\n"));
92 /****************************************************************************
93 process a sam sync replicate message - not sure whether to do this here or
95 ****************************************************************************/
96 static void msg_sam_repl(int msg_type, pid_t pid, void *buf, size_t len)
100 if (len != sizeof(uint32))
103 low_serial = *((uint32 *)buf);
105 DEBUG(3, ("received sam replication message, serial = 0x%04x\n",
109 /****************************************************************************
110 open the socket communication
111 ****************************************************************************/
112 static BOOL open_sockets_inetd(void)
114 /* Started from inetd. fd 0 is the socket. */
115 /* We will abort gracefully when the client or remote system
117 smbd_set_server_fd(dup(0));
119 /* close our standard file descriptors */
122 set_socket_options(smbd_server_fd(),"SO_KEEPALIVE");
123 set_socket_options(smbd_server_fd(),user_socket_options);
129 /****************************************************************************
130 open the socket communication
131 ****************************************************************************/
132 static BOOL open_sockets(BOOL is_daemon,int port)
134 int num_interfaces = iface_count();
135 int fd_listenset[FD_SETSIZE];
141 return open_sockets_inetd();
147 static int atexit_set;
148 if(atexit_set == 0) {
159 FD_ZERO(&listen_set);
161 if(lp_interfaces() && lp_bind_interfaces_only()) {
162 /* We have been given an interfaces line, and been
163 told to only bind to those interfaces. Create a
164 socket per interface and bind to only these.
167 if(num_interfaces > FD_SETSIZE) {
168 DEBUG(0,("open_sockets: Too many interfaces specified to bind to. Number was %d \
170 num_interfaces, FD_SETSIZE));
174 /* Now open a listen socket for each of the
176 for(i = 0; i < num_interfaces; i++) {
177 struct in_addr *ifip = iface_n_ip(i);
180 DEBUG(0,("open_sockets: interface %d has NULL IP address !\n", i));
183 s = fd_listenset[i] = open_socket_in(SOCK_STREAM, port, 0, ifip->s_addr, True);
187 /* ready to listen */
188 set_socket_options(s,"SO_KEEPALIVE");
189 set_socket_options(s,user_socket_options);
191 if (listen(s, 5) == -1) {
192 DEBUG(0,("listen: %s\n",strerror(errno)));
196 FD_SET(s,&listen_set);
199 /* Just bind to 0.0.0.0 - accept connections
203 /* open an incoming socket */
204 s = open_socket_in(SOCK_STREAM, port, 0,
205 interpret_addr(lp_socket_address()),True);
209 /* ready to listen */
210 set_socket_options(s,"SO_KEEPALIVE");
211 set_socket_options(s,user_socket_options);
213 if (listen(s, 5) == -1) {
214 DEBUG(0,("open_sockets: listen: %s\n",
221 FD_SET(s,&listen_set);
224 /* Listen to messages */
226 message_register(MSG_SMB_SAM_SYNC, msg_sam_sync);
227 message_register(MSG_SMB_SAM_REPL, msg_sam_repl);
229 /* now accept incoming connections - forking a new process
230 for each incoming connection */
231 DEBUG(2,("waiting for a connection\n"));
236 /* Free up temporary memory from the main smbd. */
239 /* Ensure we respond to PING and DEBUG messages from the main smbd. */
242 memcpy((char *)&lfds, (char *)&listen_set,
245 num = sys_select(FD_SETSIZE,&lfds,NULL);
247 if (num == -1 && errno == EINTR) {
248 extern VOLATILE sig_atomic_t reload_after_sighup;
250 /* check for sighup processing */
251 if (reload_after_sighup) {
253 DEBUG(1,("Reloading services after SIGHUP\n"));
254 reload_services(False);
255 reload_after_sighup = False;
261 /* check if we need to reload services */
262 check_reload(time(NULL));
264 /* Find the sockets that are read-ready -
266 for( ; num > 0; num--) {
267 struct sockaddr addr;
268 socklen_t in_addrlen = sizeof(addr);
271 for(i = 0; i < num_interfaces; i++) {
272 if(FD_ISSET(fd_listenset[i],&lfds)) {
274 /* Clear this so we don't look
276 FD_CLR(fd_listenset[i],&lfds);
281 smbd_set_server_fd(accept(s,&addr,&in_addrlen));
283 if (smbd_server_fd() == -1 && errno == EINTR)
286 if (smbd_server_fd() == -1) {
287 DEBUG(0,("open_sockets: accept: %s\n",
292 if (smbd_server_fd() != -1 && sys_fork()==0) {
295 /* close the listening socket(s) */
296 for(i = 0; i < num_interfaces; i++)
297 close(fd_listenset[i]);
299 /* close our standard file
304 set_socket_options(smbd_server_fd(),"SO_KEEPALIVE");
305 set_socket_options(smbd_server_fd(),user_socket_options);
307 /* Reset global variables in util.c so
308 that client substitutions will be
309 done correctly in the process. */
310 reset_globals_after_fork();
312 /* tdb needs special fork handling */
317 /* The parent doesn't need this socket */
318 close(smbd_server_fd());
320 /* Sun May 6 18:56:14 2001 ackley@cs.unm.edu:
321 Clear the closed fd info out of server_fd --
322 and more importantly, out of client_fd in
323 util_sock.c, to avoid a possible
324 getpeername failure if we reopen the logs
325 and use %I in the filename.
328 smbd_set_server_fd(-1);
330 /* Force parent to check log size after
331 * spawning child. Fix from
332 * klausr@ITAP.Physik.Uni-Stuttgart.De. The
333 * parent smbd will log to logserver.smb. It
334 * writes only two messages for each child
335 * started/finished. But each child writes,
336 * say, 50 messages also in logserver.smb,
337 * begining with the debug_count of the
338 * parent, before the child opens its own log
339 * file logserver.client. In a worst case
340 * scenario the size of logserver.smb would be
341 * checked after about 50*50=2500 messages
344 force_check_log_size();
349 /* NOTREACHED return True; */
352 /****************************************************************************
353 reload the services file
354 **************************************************************************/
355 BOOL reload_services(BOOL test)
361 pstrcpy(fname,lp_configfile());
362 if (file_exist(fname,NULL) && !strcsequal(fname,servicesf)) {
363 pstrcpy(servicesf,fname);
370 if (test && !lp_file_list_changed())
373 lp_killunused(conn_snum_used);
375 ret = lp_load(servicesf,False,False,True);
379 /* perhaps the config filename is now set */
381 reload_services(True);
388 if (smbd_server_fd() != -1) {
389 set_socket_options(smbd_server_fd(),"SO_KEEPALIVE");
390 set_socket_options(smbd_server_fd(),user_socket_options);
394 reset_mangled_cache();
397 /* this forces service parameters to be flushed */
398 become_service(NULL,True);
405 /****************************************************************************
407 ****************************************************************************/
409 VOLATILE sig_atomic_t reload_after_sighup = False;
411 static void sig_hup(int sig)
413 BlockSignals(True,SIGHUP);
414 DEBUG(0,("Got SIGHUP\n"));
417 reload_after_sighup = True;
418 BlockSignals(False,SIGHUP);
424 /*******************************************************************
425 prepare to dump a core file - carefully!
426 ********************************************************************/
427 static BOOL dump_core(void)
431 pstrcpy(dname,debugf);
432 if ((p=strrchr_m(dname,'/'))) *p=0;
433 pstrcat(dname,"/corefiles");
435 sys_chown(dname,getuid(),getgid());
437 if (chdir(dname)) return(False);
440 #ifdef HAVE_GETRLIMIT
444 getrlimit(RLIMIT_CORE, &rlp);
445 rlp.rlim_cur = MAX(4*1024*1024,rlp.rlim_cur);
446 setrlimit(RLIMIT_CORE, &rlp);
447 getrlimit(RLIMIT_CORE, &rlp);
448 DEBUG(3,("Core limits now %d %d\n",
449 (int)rlp.rlim_cur,(int)rlp.rlim_max));
455 DEBUG(0,("Dumping core in %s\n",dname));
461 /****************************************************************************
462 update the current smbd process count
463 ****************************************************************************/
465 static void decrement_smbd_process_count(void)
469 if (lp_max_smbd_processes()) {
471 tdb_change_int_atomic(conn_tdb_ctx(), "INFO/total_smbds", &total_smbds, -1);
475 /****************************************************************************
477 ****************************************************************************/
478 void exit_server(char *reason)
480 static int firsttime=1;
481 extern char *last_inbuf;
484 if (!firsttime) exit(0);
488 DEBUG(2,("Closing connections\n"));
492 invalidate_all_vuids();
494 /* delete our entry in the connections database. */
495 yield_connection(NULL,"",MAXSTATUS);
497 respond_to_all_remaining_local_messages();
498 decrement_smbd_process_count();
501 if (dcelogin_atmost_once) {
507 int oldlevel = DEBUGLEVEL;
509 DEBUG(0,("Last message was %s\n",smb_fn_name(last_message)));
511 show_msg(last_inbuf);
512 DEBUGLEVEL = oldlevel;
513 DEBUG(0,("===============================================================\n"));
515 if (dump_core()) return;
521 DEBUG(3,("Server exit (%s)\n", (reason ? reason : "")));
525 /****************************************************************************
526 initialise connect, service and file structs
527 ****************************************************************************/
528 static void init_structs(void )
531 * Set the machine NETBIOS name if not already
532 * set from the config file.
535 if (!*global_myname) {
537 fstrcpy( global_myname, myhostname() );
538 p = strchr_m( global_myname, '.' );
543 strupper( global_myname );
557 /****************************************************************************
559 ****************************************************************************/
560 static void usage(char *pname)
563 printf("Usage: %s [-DaoPh?V] [-d debuglevel] [-l log basename] [-p port]\n", pname);
564 printf(" [-O socket options] [-s services file]\n");
565 printf("\t-D Become a daemon\n");
566 printf("\t-a Append to log file (default)\n");
567 printf("\t-o Overwrite log file, don't append\n");
568 printf("\t-h Print usage\n");
569 printf("\t-? Print usage\n");
570 printf("\t-V Print version\n");
571 printf("\t-d debuglevel Set the debuglevel\n");
572 printf("\t-l log basename. Basename for log/debug files\n");
573 printf("\t-p port Listen on the specified port\n");
574 printf("\t-O socket options Socket options\n");
575 printf("\t-s services file. Filename of services file\n");
580 /****************************************************************************
582 ****************************************************************************/
583 int main(int argc,char *argv[])
585 extern BOOL append_log;
586 /* shall I run as a daemon */
587 BOOL is_daemon = False;
588 BOOL specified_logfile = False;
593 #ifdef HAVE_SET_AUTH_PARAMETERS
594 set_auth_parameters(argc,argv);
597 /* this is for people who can't start the program correctly */
598 while (argc > 1 && (*argv[1] != '-')) {
603 while ( EOF != (opt = getopt(argc, argv, "O:l:s:d:Dp:h?Vaof:")) )
606 pstrcpy(user_socket_options,optarg);
610 pstrcpy(servicesf,optarg);
614 specified_logfile = True;
615 slprintf(debugf, sizeof(debugf)-1, "%s/log.smbd", optarg);
634 DEBUGLEVEL = atoi(optarg);
648 d_printf("Version %s\n",VERSION);
652 DEBUG(0,("Incorrect program usage - are you sure the command line is correct?\n"));
658 /* needed for SecureWare on SCO */
668 if(!specified_logfile) {
669 slprintf(debugf, sizeof(debugf)-1, "%s/log.smbd", LOGFILEBASE);
672 pstrcpy(remote_machine, "smbd");
674 setup_logging(argv[0],False);
676 /* we want to re-seed early to prevent time delays causing
677 client problems at a later date. (tridge) */
678 generate_random_buffer(NULL, 0, False);
680 /* make absolutely sure we run as root - to handle cases where people
681 are crazy enough to have it setuid */
683 gain_root_privilege();
684 gain_root_group_privilege();
686 fault_setup((void (*)(void *))exit_server);
687 CatchSignal(SIGTERM , SIGNAL_CAST dflt_sig);
689 /* we are never interested in SIGPIPE */
690 BlockSignals(True,SIGPIPE);
693 /* we are never interested in SIGFPE */
694 BlockSignals(True,SIGFPE);
698 /* We are no longer interested in USR2 */
699 BlockSignals(True,SIGUSR2);
702 /* POSIX demands that signals are inherited. If the invoking process has
703 * these signals masked, we will have problems, as we won't recieve them. */
704 BlockSignals(False, SIGHUP);
705 BlockSignals(False, SIGUSR1);
707 /* we want total control over the permissions on created files,
708 so set our umask to 0 */
715 DEBUG(1,( "smbd version %s started.\n", VERSION));
716 DEBUGADD(1,( "Copyright Andrew Tridgell 1992-1998\n"));
718 DEBUG(2,("uid=%d gid=%d euid=%d egid=%d\n",
719 (int)getuid(),(int)getgid(),(int)geteuid(),(int)getegid()));
721 if (sizeof(uint16) < 2 || sizeof(uint32) < 4) {
722 DEBUG(0,("ERROR: Samba is not configured correctly for the word size on your machine\n"));
727 * Do this before reload_services.
730 if (!reload_services(False))
735 /* don't call winbind for our domain if we are the DC */
736 if (lp_domain_logons()) {
737 winbind_exclude_domain(lp_workgroup());
741 if (!profile_setup(False)) {
742 DEBUG(0,("ERROR: failed to setup profiling\n"));
749 extern BOOL sslEnabled;
750 sslEnabled = lp_ssl_enabled();
754 #endif /* WITH_SSL */
756 fstrcpy(global_myworkgroup, lp_workgroup());
758 CatchSignal(SIGHUP,SIGNAL_CAST sig_hup);
760 DEBUG(3,( "loaded services\n"));
762 if (!is_daemon && !is_a_socket(0)) {
763 DEBUG(0,("standard input is not a socket, assuming -D option\n"));
768 DEBUG( 3, ( "Becoming a daemon.\n" ) );
772 if (!directory_exist(lp_lockdir(), NULL)) {
773 mkdir(lp_lockdir(), 0755);
777 pidfile_create("smbd");
780 if (!message_init()) {
784 /* Setup the main smbd so that we can get messages. */
785 claim_connection(NULL,"",MAXSTATUS,True);
787 if (!open_sockets(is_daemon,port))
791 * everything after this point is run after the fork()
794 if (!locking_init(0)) {
798 if (!print_backend_init()) {
802 if (!share_info_db_init()) {
806 if(!initialize_password_db(False)) {
810 /* possibly reload the services file. */
811 reload_services(True);
813 if (init_group_mapping()==False) {
814 printf("Could not open tdb mapping file.\n");
818 if(!pdb_generate_sam_sid()) {
819 DEBUG(0,("ERROR: Samba cannot create a SAM SID.\n"));
824 if (sys_chroot(lp_rootdir()) == 0)
825 DEBUG(2,("Changed root to %s\n", lp_rootdir()));
829 if (!init_oplocks()) {
833 /* Setup change notify */
834 if (!init_change_notify()) {
840 exit_server("normal exit");