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;
30 extern dfs_internal dfs_struct;
34 /* the last message the was processed */
35 int last_message = -1;
37 /* a useful macro to debug the last message processed */
38 #define LAST_MESSAGE() smb_fn_name(last_message)
41 extern int DEBUGLEVEL;
43 extern pstring user_socket_options;
46 extern int dcelogin_atmost_once;
50 extern fstring remote_machine;
51 extern pstring OriginalDir;
52 extern pstring myhostname;
55 /****************************************************************************
56 when exiting, take the whole family
57 ****************************************************************************/
58 static void *dflt_sig(void)
60 exit_server("caught signal");
64 /****************************************************************************
65 Send a SIGTERM to our process group.
66 *****************************************************************************/
67 static void killkids(void)
69 if(am_parent) kill(0,SIGTERM);
73 /****************************************************************************
74 open the socket communication
75 ****************************************************************************/
76 static BOOL open_sockets_inetd(void)
79 extern int ClientPort;
81 /* Started from inetd. fd 0 is the socket. */
82 /* We will abort gracefully when the client or remote system
85 ClientPort = SMB_PORT;
87 /* close our standard file descriptors */
90 set_socket_options(Client,"SO_KEEPALIVE");
91 set_socket_options(Client,user_socket_options);
96 /****************************************************************************
97 open and listen to a socket
98 ****************************************************************************/
99 static int open_server_socket(int port, uint32 ipaddr)
103 s = open_socket_in(SOCK_STREAM, port, 0, ipaddr);
106 /* ready to listen */
107 if (listen(s, 5) == -1) {
108 DEBUG(0,("listen: %s\n", strerror(errno)));
115 /****************************************************************************
116 open the socket communication
117 ****************************************************************************/
118 static BOOL open_sockets(BOOL is_daemon,int port,int port445)
121 extern int ClientPort;
122 int num_interfaces = iface_count();
123 int fd_listenset[FD_SETSIZE];
128 memset(&fd_listenset, 0, sizeof(fd_listenset));
131 return open_sockets_inetd();
137 static int atexit_set;
138 if(atexit_set == 0) {
149 FD_ZERO(&listen_set);
151 if(lp_interfaces() && lp_bind_interfaces_only()) {
152 /* We have been given an interfaces line, and been
153 told to only bind to those interfaces. Create a
154 socket per interface and bind to only these.
157 if(num_interfaces * 2 > FD_SETSIZE) {
158 DEBUG(0,("open_sockets: Too many interfaces specified to bind to. Number was %d \
160 num_interfaces, FD_SETSIZE));
164 /* Now open a listen socket for each of the
166 for(i = 0; i < num_interfaces; i++) {
167 struct in_addr *ifip = iface_n_ip(i);
170 DEBUG(0,("open_sockets: interface %d has NULL IP address !\n", i));
173 s = fd_listenset[i * 2] = open_server_socket(port, ifip->s_addr);
174 if(s == -1) return False;
175 FD_SET(s,&listen_set);
176 s = fd_listenset[i * 2 + 1] = open_server_socket(port445, ifip->s_addr);
177 if(s == -1) return False;
178 FD_SET(s,&listen_set);
181 /* Just bind to 0.0.0.0 - accept connections
185 /* open an incoming socket */
186 s = open_server_socket(port, interpret_addr(lp_socket_address()));
190 FD_SET(s,&listen_set);
192 s = open_server_socket(port445, interpret_addr(lp_socket_address()));
196 FD_SET(s,&listen_set);
200 /* now accept incoming connections - forking a new process
201 for each incoming connection */
202 DEBUG(2,("waiting for a connection\n"));
207 memcpy((char *)&lfds, (char *)&listen_set,
210 num = sys_select(256,&lfds,NULL, NULL);
212 if (num == -1 && errno == EINTR)
215 /* Find the sockets that are read-ready -
217 for( ; num > 0; num--) {
218 struct sockaddr addr;
219 int in_addrlen = sizeof(addr);
222 for(i = 0; i < num_interfaces; i++) {
223 if(FD_ISSET(fd_listenset[i * 2],&lfds)) {
224 s = fd_listenset[i * 2];
225 ClientPort = SMB_PORT;
229 if(FD_ISSET(fd_listenset[i * 2 + 1],&lfds)) {
230 s = fd_listenset[i * 2 + 1];
231 ClientPort = SMB_PORT2;
237 /* Clear this so we don't look
241 Client = accept(s,&addr,&in_addrlen);
243 if (Client == -1 && errno == EINTR)
247 DEBUG(0,("open_sockets: accept: %s\n",
252 if (Client != -1 && fork()==0) {
255 /* close the listening socket(s) */
256 for(i = 0; i < num_interfaces; i++)
257 close(fd_listenset[i]);
259 /* close our standard file
264 set_socket_options(Client,"SO_KEEPALIVE");
265 set_socket_options(Client,user_socket_options);
267 /* Reset global variables in util.c so
268 that client substitutions will be
269 done correctly in the process. */
270 reset_globals_after_fork();
273 * Ensure this child has kernel oplock
274 * capabilities, but not it's children.
276 set_process_capability(KERNEL_OPLOCK_CAPABILITY, True);
277 set_inherited_process_capability(KERNEL_OPLOCK_CAPABILITY, False);
281 /* The parent doesn't need this socket */
284 /* Force parent to check log size after
285 * spawning child. Fix from
286 * klausr@ITAP.Physik.Uni-Stuttgart.De. The
287 * parent smbd will log to logserver.smb. It
288 * writes only two messages for each child
289 * started/finished. But each child writes,
290 * say, 50 messages also in logserver.smb,
291 * begining with the debug_count of the
292 * parent, before the child opens its own log
293 * file logserver.client. In a worst case
294 * scenario the size of logserver.smb would be
295 * checked after about 50*50=2500 messages
298 force_check_log_size();
303 /* NOTREACHED return True; */
306 /****************************************************************************
307 reload the services file
308 **************************************************************************/
309 BOOL reload_services(BOOL test)
315 pstrcpy(fname,lp_configfile());
316 if (file_exist(fname,NULL) && !strcsequal(fname,servicesf)) {
317 pstrcpy(servicesf,fname);
324 if (test && !lp_file_list_changed())
327 lp_killunused(conn_snum_used);
329 ret = lp_load(servicesf,False,False,True);
333 /* perhaps the config filename is now set */
335 reload_services(True);
344 set_socket_options(Client,"SO_KEEPALIVE");
345 set_socket_options(Client,user_socket_options);
349 reset_mangled_cache();
351 /* this forces service parameters to be flushed */
352 become_service(NULL,True);
359 /****************************************************************************
360 this prevents zombie child processes
361 ****************************************************************************/
362 BOOL reload_after_sighup = False;
364 static void sig_hup(int sig)
366 BlockSignals(True,SIGHUP);
367 DEBUG(0,("Got SIGHUP\n"));
370 * Fix from <branko.cibej@hermes.si> here.
371 * We used to reload in the signal handler - this
375 reload_after_sighup = True;
376 BlockSignals(False,SIGHUP);
382 /*******************************************************************
383 prepare to dump a core file - carefully!
384 ********************************************************************/
385 static BOOL dump_core(void)
389 pstrcpy(dname,debugf);
390 if ((p=strrchr(dname,'/'))) *p=0;
391 pstrcat(dname,"/corefiles");
393 sys_chown(dname,getuid(),getgid());
395 if (chdir(dname)) return(False);
398 #ifdef HAVE_GETRLIMIT
402 getrlimit(RLIMIT_CORE, &rlp);
403 rlp.rlim_cur = MAX(4*1024*1024,rlp.rlim_cur);
404 setrlimit(RLIMIT_CORE, &rlp);
405 getrlimit(RLIMIT_CORE, &rlp);
406 DEBUG(3,("Core limits now %d %d\n",
407 (int)rlp.rlim_cur,(int)rlp.rlim_max));
413 DEBUG(0,("Dumping core in %s\n",dname));
420 /****************************************************************************
422 ****************************************************************************/
423 void exit_server(char *reason)
425 static int firsttime=1;
426 extern char *last_inbuf;
429 if (!firsttime) exit(0);
433 DEBUG(2,("Closing connections\n"));
438 if (dcelogin_atmost_once) {
444 int oldlevel = DEBUGLEVEL;
446 DEBUG(0,("Last message was %s\n",smb_fn_name(last_message)));
448 show_msg(last_inbuf);
449 DEBUGLEVEL = oldlevel;
450 DEBUG(0,("===============================================================\n"));
452 if (dump_core()) return;
458 DEBUG(3,("Server exit (%s)\n", (reason ? reason : "")));
462 smb_mem_write_verbose(dbf);
471 /****************************************************************************
472 initialise connect, service and file structs
473 ****************************************************************************/
474 static void init_structs(void)
478 init_rpc_pipe_hnd(); /* for RPC pipes */
479 if (!init_policy_hnd(MAX_SERVER_POLICY_HANDLES))
481 exit_server("could not allocate policy handles\n");
487 /****************************************************************************
489 ****************************************************************************/
490 static void usage(char *pname)
492 DEBUG(0,("Incorrect program usage - are you sure the command line is correct?\n"));
494 printf("Usage: %s [-D] [-p port] [-d debuglevel] ", pname);
495 printf("[-l log basename] [-s services file]\n" );
496 printf("Version %s\n",VERSION);
497 printf("\t-D become a daemon\n");
498 printf("\t-p port listen on the specified port\n");
499 printf("\t-d debuglevel set the debuglevel\n");
500 printf("\t-l log basename. Basename for log/debug files\n");
501 printf("\t-s services file. Filename of services file\n");
502 printf("\t-P passive only\n");
503 printf("\t-a append to log file (default)\n");
504 printf("\t-o overwrite log file, don't append\n");
505 printf("\t-i scope NetBIOS scope to use (default none)\n");
510 /****************************************************************************
512 ****************************************************************************/
513 int main(int argc,char *argv[])
515 extern BOOL append_log;
516 /* shall I run as a daemon */
517 BOOL is_daemon = False;
519 int port445 = SMB_PORT2;
523 #ifdef HAVE_SET_AUTH_PARAMETERS
524 set_auth_parameters(argc,argv);
528 /* needed for SecureWare on SCO */
536 pstrcpy(debugf,SMBLOGFILE);
538 pstrcpy(remote_machine, "smb");
540 setup_logging(argv[0],False);
542 charset_initialise();
544 /* make absolutely sure we run as root - to handle cases where people
545 are crazy enough to have it setuid */
546 #ifdef HAVE_SETRESUID
555 fault_setup((void (*)(void *))exit_server);
556 CatchSignal(SIGTERM , SIGNAL_CAST dflt_sig);
558 /* we are never interested in SIGPIPE */
559 BlockSignals(True,SIGPIPE);
561 /* we want total control over the permissions on created files,
562 so set our umask to 0 */
565 dos_GetWd(OriginalDir);
569 /* this is for people who can't start the program correctly */
570 while (argc > 1 && (*argv[1] != '-')) {
575 while ( EOF != (opt = getopt(argc, argv, "O:i:l:s:d:Dp:h?Paof:")) )
578 pstrcpy(user_socket_options,optarg);
582 pstrcpy(scope,optarg);
593 pstrcpy(servicesf,optarg);
597 pstrcpy(debugf,optarg);
616 DEBUGLEVEL = atoi(optarg);
636 DEBUG(1,( "smbd version %s started.\n", VERSION));
637 DEBUGADD(1,( "Copyright Andrew Tridgell 1992-1998\n"));
639 DEBUG(2,("uid=%d gid=%d euid=%d egid=%d\n",
640 (int)getuid(),(int)getgid(),(int)geteuid(),(int)getegid()));
642 if (sizeof(uint16) < 2 || sizeof(uint32) < 4) {
643 DEBUG(0,("ERROR: Samba is not configured correctly for the word size on your machine\n"));
647 get_myname(myhostname,NULL);
649 if (!reload_services(False))
655 if (!profile_setup(False)) {
656 DEBUG(0,("ERROR: failed to setup profiling\n"));
662 * Set the machine NETBIOS name if not already
663 * set from the config file.
667 fstrcpy(global_myname, dns_to_netbios_name(myhostname));
669 strupper(global_myname);
673 extern BOOL sslEnabled;
674 sslEnabled = lp_ssl_enabled();
678 #endif /* WITH_SSL */
680 codepage_initialise(lp_client_code_page());
682 if (!pwdb_initialise(True))
687 if(!initialise_sam_password_db())
692 if(!initialise_passgrp_db())
697 if(!initialise_group_db())
702 if(!initialise_alias_db())
707 if(!initialise_builtin_db())
712 if (!get_member_domain_sid())
714 DEBUG(0,("ERROR: Samba cannot obtain PDC SID from PDC(s) %s.\n",
715 lp_passwordserver()));
719 CatchSignal(SIGHUP,SIGNAL_CAST sig_hup);
721 /* Setup the signals that allow the debug log level
722 to by dynamically changed. */
724 /* If we are using the malloc debug code we can't use
725 SIGUSR1 and SIGUSR2 to do debug level changes. */
729 CatchSignal( SIGUSR1, SIGNAL_CAST sig_usr1 );
733 CatchSignal( SIGUSR2, SIGNAL_CAST sig_usr2 );
737 DEBUG(3,( "loaded services\n"));
739 if (!is_daemon && !is_a_socket(0)) {
740 DEBUG(0,("standard input is not a socket, assuming -D option\n"));
745 DEBUG( 3, ( "Becoming a daemon.\n" ) );
749 check_kernel_oplocks();
751 if (!directory_exist(lp_lockdir(), NULL)) {
752 mkdir(lp_lockdir(), 0755);
756 pidfile_create("smbd");
759 if (!open_sockets(is_daemon,port,port445))
762 if (!locking_init(0))
765 /* possibly reload the services file. */
766 reload_services(True);
769 if (sys_chroot(lp_rootdir()) == 0)
770 DEBUG(2,("Changed root to %s\n", lp_rootdir()));
773 /* Setup the oplock IPC socket. */
774 if( !open_oplock_ipc() )
780 exit_server("normal exit");