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 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;
44 /****************************************************************************
45 when exiting, take the whole family
46 ****************************************************************************/
47 static void *dflt_sig(void)
49 exit_server("caught signal");
53 /****************************************************************************
54 Send a SIGTERM to our process group.
55 *****************************************************************************/
56 static void killkids(void)
58 if(am_parent) kill(0,SIGTERM);
62 /****************************************************************************
63 open and listen to a socket
64 ****************************************************************************/
65 static int open_server_socket(void)
71 slprintf(dir, sizeof(dir)-1, "/tmp/.msrpc");
72 slprintf(path, sizeof(path)-1, "%s/lsarpc", dir);
74 s = create_pipe_socket(dir, 0777, path, 0777);
79 if (listen(s, 5) == -1) {
80 DEBUG(0,("listen: %s\n", strerror(errno)));
87 /****************************************************************************
88 open the socket communication
89 ****************************************************************************/
90 static BOOL open_sockets(BOOL is_daemon)
93 int num_interfaces = iface_count();
98 memset(&fd_listenset, 0, sizeof(fd_listenset));
102 static int atexit_set;
103 if(atexit_set == 0) {
114 FD_ZERO(&listen_set);
116 /* Just bind to 0.0.0.0 - accept connections
120 /* open an incoming socket */
121 s = open_server_socket();
125 FD_SET(s,&listen_set);
127 /* now accept incoming connections - forking a new process
128 for each incoming connection */
129 DEBUG(2,("waiting for a connection\n"));
132 struct sockaddr_un addr;
133 int in_addrlen = sizeof(addr);
137 memcpy((char *)&lfds, (char *)&listen_set,
140 num = sys_select(256,&lfds,NULL, NULL);
142 if (num == -1 && errno == EINTR)
145 /* Find the sockets that are read-ready -
149 if(FD_ISSET(fd_listenset,&lfds))
154 /* Clear this so we don't look at it again. */
157 Client = accept(s,(struct sockaddr*)&addr,&in_addrlen);
159 if (Client == -1 && errno == EINTR)
164 DEBUG(0,("open_sockets: accept: %s\n",
169 if (Client != -1 && fork()==0)
173 /* close the listening socket(s) */
176 /* close our standard file
181 /* Reset global variables in util.c so
182 that client substitutions will be
183 done correctly in the process. */
184 reset_globals_after_fork();
187 * Ensure this child has kernel oplock
188 * capabilities, but not it's children.
190 set_process_capability(KERNEL_OPLOCK_CAPABILITY, True);
191 set_inherited_process_capability(KERNEL_OPLOCK_CAPABILITY, False);
195 /* The parent doesn't need this socket */
198 /* Force parent to check log size after
199 * spawning child. Fix from
200 * klausr@ITAP.Physik.Uni-Stuttgart.De. The
201 * parent lsarpcd will log to logserver.smb. It
202 * writes only two messages for each child
203 * started/finished. But each child writes,
204 * say, 50 messages also in logserver.smb,
205 * begining with the debug_count of the
206 * parent, before the child opens its own log
207 * file logserver.client. In a worst case
208 * scenario the size of logserver.smb would be
209 * checked after about 50*50=2500 messages
212 force_check_log_size();
216 /* NOTREACHED return True; */
219 /****************************************************************************
220 reload the services file
221 **************************************************************************/
222 BOOL reload_services(BOOL test)
228 pstrcpy(fname,lp_configfile());
229 if (file_exist(fname,NULL) && !strcsequal(fname,servicesf)) {
230 pstrcpy(servicesf,fname);
237 if (test && !lp_file_list_changed())
242 ret = lp_load(servicesf,False,False,True);
246 /* perhaps the config filename is now set */
248 reload_services(True);
259 /****************************************************************************
260 this prevents zombie child processes
261 ****************************************************************************/
262 BOOL reload_after_sighup = False;
264 static void sig_hup(int sig)
266 BlockSignals(True,SIGHUP);
267 DEBUG(0,("Got SIGHUP\n"));
270 * Fix from <branko.cibej@hermes.si> here.
271 * We used to reload in the signal handler - this
275 reload_after_sighup = True;
276 BlockSignals(False,SIGHUP);
282 /*******************************************************************
283 prepare to dump a core file - carefully!
284 ********************************************************************/
285 static BOOL dump_core(void)
289 pstrcpy(dname,debugf);
290 if ((p=strrchr(dname,'/'))) *p=0;
291 pstrcat(dname,"/corefiles");
293 sys_chown(dname,getuid(),getgid());
295 if (chdir(dname)) return(False);
298 #ifdef HAVE_GETRLIMIT
302 getrlimit(RLIMIT_CORE, &rlp);
303 rlp.rlim_cur = MAX(4*1024*1024,rlp.rlim_cur);
304 setrlimit(RLIMIT_CORE, &rlp);
305 getrlimit(RLIMIT_CORE, &rlp);
306 DEBUG(3,("Core limits now %d %d\n",
307 (int)rlp.rlim_cur,(int)rlp.rlim_max));
313 DEBUG(0,("Dumping core in %s\n",dname));
320 /****************************************************************************
322 ****************************************************************************/
323 void exit_server(char *reason)
325 static int firsttime=1;
326 extern char *last_inbuf;
329 if (!firsttime) exit(0);
333 DEBUG(2,("Closing connections\n"));
336 if (dcelogin_atmost_once) {
342 int oldlevel = DEBUGLEVEL;
345 show_msg(last_inbuf);
346 DEBUGLEVEL = oldlevel;
347 DEBUG(0,("===============================================================\n"));
349 if (dump_core()) return;
355 DEBUG(3,("Server exit (%s)\n", (reason ? reason : "")));
359 smb_mem_write_verbose(dbf);
368 /****************************************************************************
369 initialise connect, service and file structs
370 ****************************************************************************/
371 static void init_structs(void)
376 init_rpc_pipe_hnd(); /* for RPC pipes */
377 if (!init_policy_hnd(MAX_SERVER_POLICY_HANDLES))
379 exit_server("could not allocate policy handles\n");
383 /****************************************************************************
385 ****************************************************************************/
386 static void usage(char *pname)
388 DEBUG(0,("Incorrect program usage - are you sure the command line is correct?\n"));
390 printf("Usage: %s [-D] [-p port] [-d debuglevel] ", pname);
391 printf("[-l log basename] [-s services file]\n" );
392 printf("Version %s\n",VERSION);
393 printf("\t-D become a daemon\n");
394 printf("\t-p port listen on the specified port\n");
395 printf("\t-d debuglevel set the debuglevel\n");
396 printf("\t-l log basename. Basename for log/debug files\n");
397 printf("\t-s services file. Filename of services file\n");
398 printf("\t-P passive only\n");
399 printf("\t-a append to log file (default)\n");
400 printf("\t-o overwrite log file, don't append\n");
401 printf("\t-i scope NetBIOS scope to use (default none)\n");
406 /****************************************************************************
408 ****************************************************************************/
409 int main(int argc,char *argv[])
411 extern BOOL append_log;
412 /* shall I run as a daemon */
413 BOOL is_daemon = False;
417 #ifdef HAVE_SET_AUTH_PARAMETERS
418 set_auth_parameters(argc,argv);
422 /* needed for SecureWare on SCO */
430 pstrcpy(debugf,LSARPCLOGFILE);
432 pstrcpy(remote_machine, "lsarpcd");
434 setup_logging(argv[0],False);
436 charset_initialise();
438 /* make absolutely sure we run as root - to handle cases where people
439 are crazy enough to have it setuid */
440 #ifdef HAVE_SETRESUID
449 fault_setup((void (*)(void *))exit_server);
450 CatchSignal(SIGTERM , SIGNAL_CAST dflt_sig);
452 /* we are never interested in SIGPIPE */
453 BlockSignals(True,SIGPIPE);
455 /* we want total control over the permissions on created files,
456 so set our umask to 0 */
461 /* this is for people who can't start the program correctly */
462 while (argc > 1 && (*argv[1] != '-')) {
467 while ( EOF != (opt = getopt(argc, argv, "i:l:s:d:Dh?Paof:")) )
470 pstrcpy(scope,optarg);
481 pstrcpy(servicesf,optarg);
485 pstrcpy(debugf,optarg);
504 DEBUGLEVEL = atoi(optarg);
520 DEBUG(1,( "lsarpcd version %s started.\n", VERSION));
521 DEBUGADD(1,( "Copyright Andrew Tridgell 1992-1999\n"));
523 DEBUG(2,("uid=%d gid=%d euid=%d egid=%d\n",
524 (int)getuid(),(int)getgid(),(int)geteuid(),(int)getegid()));
526 if (sizeof(uint16) < 2 || sizeof(uint32) < 4) {
527 DEBUG(0,("ERROR: Samba is not configured correctly for the word size on your machine\n"));
531 get_myname(myhostname,NULL);
533 if (!reload_services(False))
539 if (!profile_setup(False)) {
540 DEBUG(0,("ERROR: failed to setup profiling\n"));
546 * Set the machine NETBIOS name if not already
547 * set from the config file.
551 fstrcpy(global_myname, dns_to_netbios_name(myhostname));
553 strupper(global_myname);
555 add_msrpc_command_processor( "lsarpc", argv[0], api_ntlsa_rpc );
557 codepage_initialise(lp_client_code_page());
559 if (!pwdb_initialise(True))
564 if(!initialise_sam_password_db())
569 if(!initialise_passgrp_db())
574 if(!initialise_group_db())
579 if(!initialise_alias_db())
584 if(!initialise_builtin_db())
589 if (!get_member_domain_sid())
591 DEBUG(0,("ERROR: Samba cannot obtain PDC SID from PDC(s) %s.\n",
592 lp_passwordserver()));
596 CatchSignal(SIGHUP,SIGNAL_CAST sig_hup);
598 /* Setup the signals that allow the debug log level
599 to by dynamically changed. */
601 /* If we are using the malloc debug code we can't use
602 SIGUSR1 and SIGUSR2 to do debug level changes. */
606 CatchSignal( SIGUSR1, SIGNAL_CAST sig_usr1 );
610 CatchSignal( SIGUSR2, SIGNAL_CAST sig_usr2 );
614 DEBUG(3,( "loaded services\n"));
616 if (!is_daemon && !is_a_socket(0)) {
617 DEBUG(0,("standard input is not a socket, assuming -D option\n"));
622 DEBUG( 3, ( "Becoming a daemon.\n" ) );
626 if (!directory_exist(lp_lockdir(), NULL)) {
627 mkdir(lp_lockdir(), 0755);
631 pidfile_create("lsarpcd");
634 if (!open_sockets(is_daemon))
637 if (!locking_init(0))
640 /* possibly reload the services file. */
641 reload_services(True);
644 if (sys_chroot(lp_rootdir()) == 0)
645 DEBUG(2,("Changed root to %s\n", lp_rootdir()));
651 exit_server("normal exit");