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 /* Free up temporary memory from the main smbd. */
203 /* Ensure we respond to PING and DEBUG messages from the main smbd. */
206 memcpy((char *)&lfds, (char *)&listen_set,
209 num = sys_select(FD_SETSIZE,&lfds,NULL);
211 if (num == -1 && errno == EINTR) {
212 extern VOLATILE SIG_ATOMIC_T reload_after_sighup;
214 /* check for sighup processing */
215 if (reload_after_sighup) {
217 DEBUG(1,("Reloading services after SIGHUP\n"));
218 reload_services(False);
219 reload_after_sighup = False;
225 /* check if we need to reload services */
226 check_reload(time(NULL));
228 /* Find the sockets that are read-ready -
230 for( ; num > 0; num--) {
231 struct sockaddr addr;
232 int in_addrlen = sizeof(addr);
235 for(i = 0; i < num_interfaces; i++) {
236 if(FD_ISSET(fd_listenset[i],&lfds)) {
238 /* Clear this so we don't look
240 FD_CLR(fd_listenset[i],&lfds);
245 smbd_set_server_fd(accept(s,&addr,&in_addrlen));
247 if (smbd_server_fd() == -1 && errno == EINTR)
250 if (smbd_server_fd() == -1) {
251 DEBUG(0,("open_sockets: accept: %s\n",
256 if (smbd_server_fd() != -1 && sys_fork()==0) {
259 /* close the listening socket(s) */
260 for(i = 0; i < num_interfaces; i++)
261 close(fd_listenset[i]);
263 /* close our standard file
268 set_socket_options(smbd_server_fd(),"SO_KEEPALIVE");
269 set_socket_options(smbd_server_fd(),user_socket_options);
271 /* Reset global variables in util.c so
272 that client substitutions will be
273 done correctly in the process. */
274 reset_globals_after_fork();
278 /* The parent doesn't need this socket */
279 close(smbd_server_fd());
281 /* Force parent to check log size after
282 * spawning child. Fix from
283 * klausr@ITAP.Physik.Uni-Stuttgart.De. The
284 * parent smbd will log to logserver.smb. It
285 * writes only two messages for each child
286 * started/finished. But each child writes,
287 * say, 50 messages also in logserver.smb,
288 * begining with the debug_count of the
289 * parent, before the child opens its own log
290 * file logserver.client. In a worst case
291 * scenario the size of logserver.smb would be
292 * checked after about 50*50=2500 messages
295 force_check_log_size();
300 /* NOTREACHED return True; */
303 /****************************************************************************
304 reload the services file
305 **************************************************************************/
306 BOOL reload_services(BOOL test)
312 pstrcpy(fname,lp_configfile());
313 if (file_exist(fname,NULL) && !strcsequal(fname,servicesf)) {
314 pstrcpy(servicesf,fname);
321 if (test && !lp_file_list_changed())
324 lp_killunused(conn_snum_used);
326 ret = lp_load(servicesf,False,False,True);
330 /* perhaps the config filename is now set */
332 reload_services(True);
339 if (smbd_server_fd() != -1) {
340 set_socket_options(smbd_server_fd(),"SO_KEEPALIVE");
341 set_socket_options(smbd_server_fd(),user_socket_options);
345 reset_mangled_cache();
348 /* this forces service parameters to be flushed */
349 become_service(NULL,True);
356 /****************************************************************************
358 ****************************************************************************/
360 VOLATILE SIG_ATOMIC_T reload_after_sighup = False;
362 static void sig_hup(int sig)
364 BlockSignals(True,SIGHUP);
365 DEBUG(0,("Got SIGHUP\n"));
368 reload_after_sighup = True;
369 BlockSignals(False,SIGHUP);
375 /*******************************************************************
376 prepare to dump a core file - carefully!
377 ********************************************************************/
378 static BOOL dump_core(void)
382 pstrcpy(dname,debugf);
383 if ((p=strrchr(dname,'/'))) *p=0;
384 pstrcat(dname,"/corefiles");
386 sys_chown(dname,getuid(),getgid());
388 if (chdir(dname)) return(False);
391 #ifdef HAVE_GETRLIMIT
395 getrlimit(RLIMIT_CORE, &rlp);
396 rlp.rlim_cur = MAX(4*1024*1024,rlp.rlim_cur);
397 setrlimit(RLIMIT_CORE, &rlp);
398 getrlimit(RLIMIT_CORE, &rlp);
399 DEBUG(3,("Core limits now %d %d\n",
400 (int)rlp.rlim_cur,(int)rlp.rlim_max));
406 DEBUG(0,("Dumping core in %s\n",dname));
412 /****************************************************************************
414 ****************************************************************************/
415 void exit_server(char *reason)
417 static int firsttime=1;
418 extern char *last_inbuf;
421 if (!firsttime) exit(0);
425 DEBUG(2,("Closing connections\n"));
429 /* delete our entry in the connections database. */
431 yield_connection(NULL,"",MAXSTATUS);
434 respond_to_all_remaining_local_messages();
437 if (dcelogin_atmost_once) {
443 int oldlevel = DEBUGLEVEL;
445 DEBUG(0,("Last message was %s\n",smb_fn_name(last_message)));
447 show_msg(last_inbuf);
448 DEBUGLEVEL = oldlevel;
449 DEBUG(0,("===============================================================\n"));
451 if (dump_core()) return;
457 DEBUG(3,("Server exit (%s)\n", (reason ? reason : "")));
461 /****************************************************************************
462 initialise connect, service and file structs
463 ****************************************************************************/
464 static void init_structs(void )
467 * Set the machine NETBIOS name if not already
468 * set from the config file.
471 if (!*global_myname) {
473 fstrcpy( global_myname, myhostname() );
474 p = strchr( global_myname, '.' );
479 strupper( global_myname );
488 /* for LSA handles */
489 init_lsa_policy_hnd();
491 /* for SPOOLSS handles */
499 /****************************************************************************
501 ****************************************************************************/
502 static void usage(char *pname)
505 printf("Usage: %s [-DaoPh?V] [-d debuglevel] [-l log basename] [-p port]\n", pname);
506 printf(" [-O socket options] [-s services file]\n");
507 printf("\t-D Become a daemon\n");
508 printf("\t-a Append to log file (default)\n");
509 printf("\t-o Overwrite log file, don't append\n");
510 printf("\t-h Print usage\n");
511 printf("\t-? Print usage\n");
512 printf("\t-V Print version\n");
513 printf("\t-d debuglevel Set the debuglevel\n");
514 printf("\t-l log basename. Basename for log/debug files\n");
515 printf("\t-p port Listen on the specified port\n");
516 printf("\t-O socket options Socket options\n");
517 printf("\t-s services file. Filename of services file\n");
522 /****************************************************************************
524 ****************************************************************************/
525 int main(int argc,char *argv[])
527 extern BOOL append_log;
528 /* shall I run as a daemon */
529 BOOL is_daemon = False;
530 BOOL specified_logfile = False;
535 #ifdef HAVE_SET_AUTH_PARAMETERS
536 set_auth_parameters(argc,argv);
539 /* this is for people who can't start the program correctly */
540 while (argc > 1 && (*argv[1] != '-')) {
545 while ( EOF != (opt = getopt(argc, argv, "O:l:s:d:Dp:h?Vaof:")) )
548 pstrcpy(user_socket_options,optarg);
552 pstrcpy(servicesf,optarg);
556 specified_logfile = True;
557 pstrcpy(debugf,optarg);
576 DEBUGLEVEL = atoi(optarg);
590 printf("Version %s\n",VERSION);
594 DEBUG(0,("Incorrect program usage - are you sure the command line is correct?\n"));
600 /* needed for SecureWare on SCO */
605 * gain_root_privilege uses an assert than will cause a core
606 * dump if euid != 0. Ensure this is the case.
609 if(geteuid() != (uid_t)0) {
610 fprintf(stderr, "%s: Version %s : Must have effective user id of zero to run.\n", argv[0], VERSION);
618 if(!specified_logfile) {
619 slprintf(debugf, sizeof(debugf), "%s/log.smbd", LOGFILEBASE);
622 pstrcpy(remote_machine, "smb");
624 setup_logging(argv[0],False);
626 charset_initialise();
628 /* we want to re-seed early to prevent time delays causing
629 client problems at a later date. (tridge) */
630 generate_random_buffer(NULL, 0, False);
632 /* make absolutely sure we run as root - to handle cases where people
633 are crazy enough to have it setuid */
635 gain_root_privilege();
636 gain_root_group_privilege();
638 fault_setup((void (*)(void *))exit_server);
639 CatchSignal(SIGTERM , SIGNAL_CAST dflt_sig);
641 /* we are never interested in SIGPIPE */
642 BlockSignals(True,SIGPIPE);
645 /* we are never interested in SIGFPE */
646 BlockSignals(True,SIGFPE);
650 /* We are no longer interested in USR2 */
651 BlockSignals(True,SIGUSR2);
654 /* we want total control over the permissions on created files,
655 so set our umask to 0 */
662 DEBUG(1,( "smbd version %s started.\n", VERSION));
663 DEBUGADD(1,( "Copyright Andrew Tridgell 1992-1998\n"));
665 DEBUG(2,("uid=%d gid=%d euid=%d egid=%d\n",
666 (int)getuid(),(int)getgid(),(int)geteuid(),(int)getegid()));
668 if (sizeof(uint16) < 2 || sizeof(uint32) < 4) {
669 DEBUG(0,("ERROR: Samba is not configured correctly for the word size on your machine\n"));
674 * Do this before reload_services.
677 if (!reload_services(False))
683 if (!profile_setup(False)) {
684 DEBUG(0,("ERROR: failed to setup profiling\n"));
691 extern BOOL sslEnabled;
692 sslEnabled = lp_ssl_enabled();
696 #endif /* WITH_SSL */
698 codepage_initialise(lp_client_code_page());
700 fstrcpy(global_myworkgroup, lp_workgroup());
702 CatchSignal(SIGHUP,SIGNAL_CAST sig_hup);
704 DEBUG(3,( "loaded services\n"));
706 if (!is_daemon && !is_a_socket(0)) {
707 DEBUG(0,("standard input is not a socket, assuming -D option\n"));
712 DEBUG( 3, ( "Becoming a daemon.\n" ) );
716 if (!directory_exist(lp_lockdir(), NULL)) {
717 mkdir(lp_lockdir(), 0755);
721 pidfile_create("smbd");
724 if (!message_init()) {
728 /* Setup the main smbd so that we can get messages. */
730 claim_connection(NULL,"",MAXSTATUS,True);
733 /* Attempt to migrate from an old 2.0.x machine account file. */
734 if (!migrate_from_old_password_file(global_myworkgroup)) {
735 DEBUG(0,("Failed to migrate from old MAC file.\n"));
738 if (!open_sockets(is_daemon,port))
742 * everything after this point is run after the fork()
745 if (!locking_init(0)) {
749 if (!print_backend_init()) {
753 if(!initialize_password_db(False)) {
757 /* possibly reload the services file. */
758 reload_services(True);
760 if(!pdb_generate_sam_sid()) {
761 DEBUG(0,("ERROR: Samba cannot create a SAM SID.\n"));
766 if (sys_chroot(lp_rootdir()) == 0)
767 DEBUG(2,("Changed root to %s\n", lp_rootdir()));
771 if (!init_oplocks()) {
775 /* Setup change notify */
776 if (!init_change_notify()) {
782 exit_server("normal exit");