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, ulong 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];
129 return open_sockets_inetd();
135 static int atexit_set;
136 if(atexit_set == 0) {
147 FD_ZERO(&listen_set);
149 if(lp_interfaces() && lp_bind_interfaces_only()) {
150 /* We have been given an interfaces line, and been
151 told to only bind to those interfaces. Create a
152 socket per interface and bind to only these.
155 if(num_interfaces * 2 > FD_SETSIZE) {
156 DEBUG(0,("open_sockets: Too many interfaces specified to bind to. Number was %d \
158 num_interfaces, FD_SETSIZE));
162 /* Now open a listen socket for each of the
164 for(i = 0; i < num_interfaces; i++) {
165 struct in_addr *ifip = iface_n_ip(i);
168 DEBUG(0,("open_sockets: interface %d has NULL IP address !\n", i));
171 s = fd_listenset[i * 2] = open_server_socket(port, ifip->s_addr);
172 if(s == -1) return False;
173 FD_SET(s,&listen_set);
174 s = fd_listenset[i * 2 + 1] = open_server_socket(port445, ifip->s_addr);
175 if(s == -1) return False;
176 FD_SET(s,&listen_set);
179 /* Just bind to 0.0.0.0 - accept connections
183 /* open an incoming socket */
184 s = open_server_socket(port, interpret_addr(lp_socket_address()));
188 FD_SET(s,&listen_set);
189 s = open_server_socket(port445, interpret_addr(lp_socket_address()));
193 FD_SET(s,&listen_set);
196 /* now accept incoming connections - forking a new process
197 for each incoming connection */
198 DEBUG(2,("waiting for a connection\n"));
203 memcpy((char *)&lfds, (char *)&listen_set,
206 num = sys_select(256,&lfds,NULL);
208 if (num == -1 && errno == EINTR)
211 /* Find the sockets that are read-ready -
213 for( ; num > 0; num--) {
214 struct sockaddr addr;
215 int in_addrlen = sizeof(addr);
218 for(i = 0; i < num_interfaces; i++) {
219 if(FD_ISSET(fd_listenset[i * 2],&lfds)) {
220 s = fd_listenset[i * 2];
221 ClientPort = SMB_PORT;
224 if(FD_ISSET(fd_listenset[i * 2 + 1],&lfds)) {
225 s = fd_listenset[i * 2 + 1];
226 ClientPort = SMB_PORT2;
231 /* Clear this so we don't look
235 Client = accept(s,&addr,&in_addrlen);
237 if (Client == -1 && errno == EINTR)
241 DEBUG(0,("open_sockets: accept: %s\n",
246 if (Client != -1 && fork()==0) {
249 /* close the listening socket(s) */
250 for(i = 0; i < num_interfaces; i++)
251 close(fd_listenset[i]);
253 /* close our standard file
258 set_socket_options(Client,"SO_KEEPALIVE");
259 set_socket_options(Client,user_socket_options);
261 /* Reset global variables in util.c so
262 that client substitutions will be
263 done correctly in the process. */
264 reset_globals_after_fork();
267 * Ensure this child has kernel oplock
268 * capabilities, but not it's children.
270 set_process_capability(KERNEL_OPLOCK_CAPABILITY, True);
271 set_inherited_process_capability(KERNEL_OPLOCK_CAPABILITY, False);
275 /* The parent doesn't need this socket */
278 /* Force parent to check log size after
279 * spawning child. Fix from
280 * klausr@ITAP.Physik.Uni-Stuttgart.De. The
281 * parent smbd will log to logserver.smb. It
282 * writes only two messages for each child
283 * started/finished. But each child writes,
284 * say, 50 messages also in logserver.smb,
285 * begining with the debug_count of the
286 * parent, before the child opens its own log
287 * file logserver.client. In a worst case
288 * scenario the size of logserver.smb would be
289 * checked after about 50*50=2500 messages
292 force_check_log_size();
297 /* NOTREACHED return True; */
300 /****************************************************************************
301 reload the services file
302 **************************************************************************/
303 BOOL reload_services(BOOL test)
309 pstrcpy(fname,lp_configfile());
310 if (file_exist(fname,NULL) && !strcsequal(fname,servicesf)) {
311 pstrcpy(servicesf,fname);
318 if (test && !lp_file_list_changed())
321 lp_killunused(conn_snum_used);
323 ret = lp_load(servicesf,False,False,True);
327 /* perhaps the config filename is now set */
329 reload_services(True);
338 set_socket_options(Client,"SO_KEEPALIVE");
339 set_socket_options(Client,user_socket_options);
343 reset_mangled_cache();
345 /* this forces service parameters to be flushed */
346 become_service(NULL,True);
353 /****************************************************************************
354 this prevents zombie child processes
355 ****************************************************************************/
356 BOOL reload_after_sighup = False;
358 static void sig_hup(int sig)
360 BlockSignals(True,SIGHUP);
361 DEBUG(0,("Got SIGHUP\n"));
364 * Fix from <branko.cibej@hermes.si> here.
365 * We used to reload in the signal handler - this
369 reload_after_sighup = True;
370 BlockSignals(False,SIGHUP);
376 /*******************************************************************
377 prepare to dump a core file - carefully!
378 ********************************************************************/
379 static BOOL dump_core(void)
383 pstrcpy(dname,debugf);
384 if ((p=strrchr(dname,'/'))) *p=0;
385 pstrcat(dname,"/corefiles");
387 sys_chown(dname,getuid(),getgid());
389 if (chdir(dname)) return(False);
392 #ifdef HAVE_GETRLIMIT
396 getrlimit(RLIMIT_CORE, &rlp);
397 rlp.rlim_cur = MAX(4*1024*1024,rlp.rlim_cur);
398 setrlimit(RLIMIT_CORE, &rlp);
399 getrlimit(RLIMIT_CORE, &rlp);
400 DEBUG(3,("Core limits now %d %d\n",
401 (int)rlp.rlim_cur,(int)rlp.rlim_max));
407 DEBUG(0,("Dumping core in %s\n",dname));
414 /****************************************************************************
416 ****************************************************************************/
417 void exit_server(char *reason)
419 static int firsttime=1;
420 extern char *last_inbuf;
423 if (!firsttime) exit(0);
427 DEBUG(2,("Closing connections\n"));
432 if (dcelogin_atmost_once) {
438 int oldlevel = DEBUGLEVEL;
440 DEBUG(0,("Last message was %s\n",smb_fn_name(last_message)));
442 show_msg(last_inbuf);
443 DEBUGLEVEL = oldlevel;
444 DEBUG(0,("===============================================================\n"));
446 if (dump_core()) return;
452 DEBUG(3,("Server exit (%s)\n", (reason ? reason : "")));
456 smb_mem_write_verbose(dbf);
465 /****************************************************************************
466 initialise connect, service and file structs
467 ****************************************************************************/
468 static void init_structs(void)
472 init_rpc_pipe_hnd(); /* for RPC pipes */
473 init_lsa_policy_hnd(); /* for LSA handles */
474 init_printer_hnd(); /* for SPOOLSS handles */
479 /****************************************************************************
481 ****************************************************************************/
482 static void usage(char *pname)
484 DEBUG(0,("Incorrect program usage - are you sure the command line is correct?\n"));
486 printf("Usage: %s [-D] [-p port] [-d debuglevel] ", pname);
487 printf("[-l log basename] [-s services file]\n" );
488 printf("Version %s\n",VERSION);
489 printf("\t-D become a daemon\n");
490 printf("\t-p port listen on the specified port\n");
491 printf("\t-d debuglevel set the debuglevel\n");
492 printf("\t-l log basename. Basename for log/debug files\n");
493 printf("\t-s services file. Filename of services file\n");
494 printf("\t-P passive only\n");
495 printf("\t-a append to log file (default)\n");
496 printf("\t-o overwrite log file, don't append\n");
497 printf("\t-i scope NetBIOS scope to use (default none)\n");
502 /****************************************************************************
504 ****************************************************************************/
505 int main(int argc,char *argv[])
507 extern BOOL append_log;
508 /* shall I run as a daemon */
509 BOOL is_daemon = False;
511 int port445 = SMB_PORT2;
515 #ifdef HAVE_SET_AUTH_PARAMETERS
516 set_auth_parameters(argc,argv);
520 /* needed for SecureWare on SCO */
528 pstrcpy(debugf,SMBLOGFILE);
530 pstrcpy(remote_machine, "smb");
532 setup_logging(argv[0],False);
534 charset_initialise();
536 /* make absolutely sure we run as root - to handle cases where people
537 are crazy enough to have it setuid */
538 #ifdef HAVE_SETRESUID
547 fault_setup((void (*)(void *))exit_server);
548 CatchSignal(SIGTERM , SIGNAL_CAST dflt_sig);
550 /* we are never interested in SIGPIPE */
551 BlockSignals(True,SIGPIPE);
553 /* we want total control over the permissions on created files,
554 so set our umask to 0 */
557 dos_GetWd(OriginalDir);
561 /* this is for people who can't start the program correctly */
562 while (argc > 1 && (*argv[1] != '-')) {
567 while ( EOF != (opt = getopt(argc, argv, "O:i:l:s:d:Dp:h?Paof:")) )
570 pstrcpy(user_socket_options,optarg);
574 pstrcpy(scope,optarg);
585 pstrcpy(servicesf,optarg);
589 pstrcpy(debugf,optarg);
608 DEBUGLEVEL = atoi(optarg);
628 DEBUG(1,( "smbd version %s started.\n", VERSION));
629 DEBUGADD(1,( "Copyright Andrew Tridgell 1992-1998\n"));
631 DEBUG(2,("uid=%d gid=%d euid=%d egid=%d\n",
632 (int)getuid(),(int)getgid(),(int)geteuid(),(int)getegid()));
634 if (sizeof(uint16) < 2 || sizeof(uint32) < 4) {
635 DEBUG(0,("ERROR: Samba is not configured correctly for the word size on your machine\n"));
639 get_myname(myhostname,NULL);
641 if (!reload_services(False))
647 if (!profile_setup(False)) {
648 DEBUG(0,("ERROR: failed to setup profiling\n"));
654 * Set the machine NETBIOS name if not already
655 * set from the config file.
659 fstrcpy(global_myname, dns_to_netbios_name(myhostname));
661 strupper(global_myname);
665 extern BOOL sslEnabled;
666 sslEnabled = lp_ssl_enabled();
670 #endif /* WITH_SSL */
672 codepage_initialise(lp_client_code_page());
674 if (!pwdb_initialise(True))
679 if(!initialise_sam_password_db())
684 if(!initialise_passgrp_db())
689 if(!initialise_group_db())
694 if(!initialise_alias_db())
699 if(!initialise_builtin_db())
704 if (!get_member_domain_sid())
706 DEBUG(0,("ERROR: Samba cannot obtain PDC SID from PDC(s) %s.\n",
707 lp_passwordserver()));
711 CatchSignal(SIGHUP,SIGNAL_CAST sig_hup);
713 /* Setup the signals that allow the debug log level
714 to by dynamically changed. */
716 /* If we are using the malloc debug code we can't use
717 SIGUSR1 and SIGUSR2 to do debug level changes. */
721 CatchSignal( SIGUSR1, SIGNAL_CAST sig_usr1 );
725 CatchSignal( SIGUSR2, SIGNAL_CAST sig_usr2 );
729 DEBUG(3,( "loaded services\n"));
731 if (!is_daemon && !is_a_socket(0)) {
732 DEBUG(0,("standard input is not a socket, assuming -D option\n"));
737 DEBUG( 3, ( "Becoming a daemon.\n" ) );
741 check_kernel_oplocks();
743 if (!directory_exist(lp_lockdir(), NULL)) {
744 mkdir(lp_lockdir(), 0755);
748 pidfile_create("smbd");
751 if (!open_sockets(is_daemon,port,port445))
754 if (!locking_init(0))
757 /* possibly reload the services file. */
758 reload_services(True);
761 if (sys_chroot(lp_rootdir()) == 0)
762 DEBUG(2,("Changed root to %s\n", lp_rootdir()));
765 /* Setup the oplock IPC socket. */
766 if( !open_oplock_ipc() )
772 exit_server("normal exit");