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 extern pstring servicesf;
26 extern pstring debugf;
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)
38 extern int DEBUGLEVEL;
40 extern fstring remote_machine;
41 extern pstring myhostname;
42 extern pstring pipe_name;
44 extern pstring OriginalDir;
46 /****************************************************************************
47 when exiting, take the whole family
48 ****************************************************************************/
49 static void *dflt_sig(void)
51 exit_server("caught signal");
55 /****************************************************************************
56 Send a SIGTERM to our process group.
57 *****************************************************************************/
58 static void killkids(void)
60 if(am_parent) kill(0,SIGTERM);
64 /****************************************************************************
65 open and listen to a socket
66 ****************************************************************************/
67 static int open_server_socket(void)
73 slprintf(dir, sizeof(dir)-1, "/tmp/.msrpc");
74 slprintf(path, sizeof(path)-1, "%s/%s", dir, pipe_name);
76 s = create_pipe_socket(dir, 0777, path, 0777);
81 if (listen(s, 5) == -1) {
82 DEBUG(0,("listen: %s\n", strerror(errno)));
89 /****************************************************************************
90 open the socket communication
91 ****************************************************************************/
92 static int open_sockets(BOOL is_daemon)
95 int num_interfaces = iface_count();
100 memset(&fd_listenset, 0, sizeof(fd_listenset));
104 static int atexit_set;
105 if(atexit_set == 0) {
116 FD_ZERO(&listen_set);
118 /* Just bind to 0.0.0.0 - accept connections
122 /* open an incoming socket */
123 s = open_server_socket();
127 FD_SET(s,&listen_set);
129 /* now accept incoming connections - forking a new process
130 for each incoming connection */
131 DEBUG(2,("waiting for a connection\n"));
134 struct sockaddr_un addr;
135 int in_addrlen = sizeof(addr);
139 memcpy((char *)&lfds, (char *)&listen_set,
142 num = sys_select(256,&lfds,NULL, NULL);
144 if (num == -1 && errno == EINTR)
147 /* Find the sockets that are read-ready -
151 if(FD_ISSET(fd_listenset,&lfds))
156 /* Clear this so we don't look at it again. */
159 ClientMSRPC = accept(s,(struct sockaddr*)&addr,&in_addrlen);
161 if (ClientMSRPC == -1 && errno == EINTR)
164 if (ClientMSRPC == -1)
166 DEBUG(0,("open_sockets: accept: %s\n",
171 if (ClientMSRPC != -1 && fork()==0)
175 /* close the listening socket(s) */
178 /* close our standard file
183 /* Reset global variables in util.c so
184 that client substitutions will be
185 done correctly in the process. */
186 reset_globals_after_fork();
190 /* The parent doesn't need this socket */
193 /* Force parent to check log size after
194 * spawning child. Fix from
195 * klausr@ITAP.Physik.Uni-Stuttgart.De. The
196 * parent daemon will log to logserver.smb. It
197 * writes only two messages for each child
198 * started/finished. But each child writes,
199 * say, 50 messages also in logserver.smb,
200 * begining with the debug_count of the
201 * parent, before the child opens its own log
202 * file logserver.client. In a worst case
203 * scenario the size of logserver.smb would be
204 * checked after about 50*50=2500 messages
207 force_check_log_size();
215 /****************************************************************************
216 this prevents zombie child processes
217 ****************************************************************************/
218 BOOL reload_after_sighup = False;
220 static void sig_hup(int sig)
222 BlockSignals(True,SIGHUP);
223 DEBUG(0,("Got SIGHUP\n"));
226 * Fix from <branko.cibej@hermes.si> here.
227 * We used to reload in the signal handler - this
231 reload_after_sighup = True;
232 BlockSignals(False,SIGHUP);
238 /*******************************************************************
239 prepare to dump a core file - carefully!
240 ********************************************************************/
241 static BOOL dump_core(void)
245 pstrcpy(dname,debugf);
246 if ((p=strrchr(dname,'/'))) *p=0;
247 pstrcat(dname,"/corefiles");
249 sys_chown(dname,getuid(),getgid());
251 if (chdir(dname)) return(False);
254 #ifdef HAVE_GETRLIMIT
258 getrlimit(RLIMIT_CORE, &rlp);
259 rlp.rlim_cur = MAX(4*1024*1024,rlp.rlim_cur);
260 setrlimit(RLIMIT_CORE, &rlp);
261 getrlimit(RLIMIT_CORE, &rlp);
262 DEBUG(3,("Core limits now %d %d\n",
263 (int)rlp.rlim_cur,(int)rlp.rlim_max));
269 DEBUG(0,("Dumping core in %s\n",dname));
276 /****************************************************************************
278 ****************************************************************************/
279 void exit_server(char *reason)
281 static int firsttime=1;
282 extern char *last_inbuf;
285 if (!firsttime) exit(0);
289 DEBUG(2,("Closing connections\n"));
292 if (dcelogin_atmost_once) {
298 int oldlevel = DEBUGLEVEL;
301 show_msg(last_inbuf);
302 DEBUGLEVEL = oldlevel;
303 DEBUG(0,("===============================================================\n"));
305 if (dump_core()) return;
311 DEBUG(3,("Server exit (%s)\n", (reason ? reason : "")));
315 smb_mem_write_verbose(dbf);
324 /****************************************************************************
325 initialise connect, service and file structs
326 ****************************************************************************/
327 static void init_structs(void)
332 init_rpc_pipe_hnd(); /* for RPC pipes */
333 if (!init_policy_hnd(MAX_SERVER_POLICY_HANDLES))
335 exit_server("could not allocate policy handles\n");
339 /****************************************************************************
341 ****************************************************************************/
342 static void usage(char *pname)
344 DEBUG(0,("Incorrect program usage - are you sure the command line is correct?\n"));
346 printf("Usage: %s [-D] [-p port] [-d debuglevel] ", pname);
347 printf("[-l log basename] [-s services file]\n" );
348 printf("Version %s\n",VERSION);
349 printf("\t-D become a daemon\n");
350 printf("\t-p port listen on the specified port\n");
351 printf("\t-d debuglevel set the debuglevel\n");
352 printf("\t-l log basename. Basename for log/debug files\n");
353 printf("\t-s services file. Filename of services file\n");
354 printf("\t-P passive only\n");
355 printf("\t-a append to log file (default)\n");
356 printf("\t-o overwrite log file, don't append\n");
357 printf("\t-i scope NetBIOS scope to use (default none)\n");
362 /****************************************************************************
364 ****************************************************************************/
365 int msrpc_main(int argc,char *argv[])
367 extern BOOL append_log;
368 /* shall I run as a daemon */
369 BOOL is_daemon = False;
372 int ClientMSRPC = -1;
373 pipes_struct static_pipe;
375 pstrcpy(remote_machine, pipe_name);
377 charset_initialise();
379 /* make absolutely sure we run as root - to handle cases where people
380 are crazy enough to have it setuid */
381 #ifdef HAVE_SETRESUID
390 fault_setup((void (*)(void *))exit_server);
391 CatchSignal(SIGTERM , SIGNAL_CAST dflt_sig);
393 /* we are never interested in SIGPIPE */
394 BlockSignals(True,SIGPIPE);
396 /* we want total control over the permissions on created files,
397 so set our umask to 0 */
400 dos_GetWd(OriginalDir);
404 /* this is for people who can't start the program correctly */
405 while (argc > 1 && (*argv[1] != '-')) {
410 while ( EOF != (opt = getopt(argc, argv, "i:l:s:d:Dh?Paof:")) )
413 pstrcpy(scope,optarg);
424 pstrcpy(servicesf,optarg);
428 pstrcpy(debugf,optarg);
447 DEBUGLEVEL = atoi(optarg);
463 DEBUG(1,( "%s version %s started.\n", argv[0], VERSION));
464 DEBUGADD(1,( "Copyright Andrew Tridgell 1992-1999\n"));
466 DEBUG(2,("uid=%d gid=%d euid=%d egid=%d\n",
467 (int)getuid(),(int)getgid(),(int)geteuid(),(int)getegid()));
469 if (sizeof(uint16) < 2 || sizeof(uint32) < 4) {
470 DEBUG(0,("ERROR: Samba is not configured correctly for the word size on your machine\n"));
474 get_myname(myhostname,NULL);
476 if (!reload_services(False))
482 if (!profile_setup(False)) {
483 DEBUG(0,("ERROR: failed to setup profiling\n"));
489 * Set the machine NETBIOS name if not already
490 * set from the config file.
494 fstrcpy(global_myname, dns_to_netbios_name(myhostname));
496 strupper(global_myname);
498 codepage_initialise(lp_client_code_page());
500 CatchSignal(SIGHUP,SIGNAL_CAST sig_hup);
502 /* Setup the signals that allow the debug log level
503 to by dynamically changed. */
505 /* If we are using the malloc debug code we can't use
506 SIGUSR1 and SIGUSR2 to do debug level changes. */
510 CatchSignal( SIGUSR1, SIGNAL_CAST sig_usr1 );
514 CatchSignal( SIGUSR2, SIGNAL_CAST sig_usr2 );
518 DEBUG(3,( "loaded services\n"));
520 if (!is_daemon && !is_a_socket(0)) {
521 DEBUG(0,("standard input is not a socket, assuming -D option\n"));
526 DEBUG( 3, ( "Becoming a daemon.\n" ) );
530 if (!directory_exist(lp_lockdir(), NULL)) {
531 mkdir(lp_lockdir(), 0755);
535 pidfile_create(pipe_name);
538 ClientMSRPC = open_sockets(is_daemon);
539 if (ClientMSRPC == -1)
541 exit_server("open socket failed");
544 if (!locking_init(0))
547 /* possibly reload the services file. */
548 reload_services(True);
551 if (sys_chroot(lp_rootdir()) == 0)
552 DEBUG(2,("Changed root to %s\n", lp_rootdir()));
555 msrpc_service_init();
557 ZERO_STRUCT(static_pipe);
558 fstrcpy(static_pipe.name, pipe_name);
559 if (msrpcd_init(ClientMSRPC, &static_pipe))
561 reload_services(False);
562 msrpcd_process(ClientMSRPC, &static_pipe);
564 if (ClientMSRPC != -1)
569 exit_server("normal exit");