47 using std::ostringstream ;
118 ServerApp::set_group_id()
120 #if !defined(OS2) && !defined(TPF)
126 BESDEBUG(
"server",
"ServerApp: Setting group id ... " << endl ) ;
128 string key =
"BES.Group" ;
136 BESDEBUG(
"server",
"FAILED" << endl ) ;
137 string err = string(
"FAILED: ") + e.
get_message() ;
138 cerr << err << endl ;
142 if( !found || group_str.empty() )
144 BESDEBUG(
"server",
"FAILED" << endl ) ;
145 string err =
"FAILED: Group not specified in BES configuration file" ;
146 cerr << err << endl ;
150 BESDEBUG(
"server",
"to " << group_str <<
" ... " << endl ) ;
153 if( group_str[0] ==
'#' )
156 const char *group_c = group_str.c_str() ;
158 new_gid = atoi( group_c ) ;
164 ent = getgrnam( group_str.c_str() ) ;
167 BESDEBUG(
"server",
"FAILED" << endl ) ;
168 string err = (string)
"FAILED: Group " + group_str
169 +
" does not exist" ;
170 cerr << err << endl ;
174 new_gid = ent->gr_gid ;
179 BESDEBUG(
"server",
"FAILED" << endl ) ;
181 err <<
"FAILED: Group id " << new_gid
182 <<
" not a valid group id for BES" ;
183 cerr << err.str() << endl ;
188 BESDEBUG(
"server",
"to id " << new_gid <<
" ... " << endl ) ;
189 if( setgid( new_gid ) == -1 )
191 BESDEBUG(
"server",
"FAILED" << endl ) ;
193 err <<
"FAILED: unable to set the group id to " << new_gid ;
194 cerr << err.str() << endl ;
199 BESDEBUG(
"server",
"OK" << endl ) ;
201 BESDEBUG(
"server",
"ServerApp: Groups not supported in this OS" << endl ) ;
206 ServerApp::set_user_id()
208 BESDEBUG(
"server",
"ServerApp: Setting user id ... " << endl ) ;
215 string key =
"BES.User" ;
223 BESDEBUG(
"server",
"FAILED" << endl ) ;
224 string err = (string)
"FAILED: " + e.
get_message() ;
225 cerr << err << endl ;
229 if( !found || user_str.empty() )
231 BESDEBUG(
"server",
"FAILED" << endl ) ;
232 string err = (string)
"FAILED: User not specified in BES config file" ;
233 cerr << err << endl ;
237 BESDEBUG(
"server",
"to " << user_str <<
" ... " << endl ) ;
240 if( user_str[0] ==
'#' )
242 const char *user_str_c = user_str.c_str() ;
244 new_id = atoi( user_str_c ) ;
249 ent = getpwnam( user_str.c_str() ) ;
252 BESDEBUG(
"server",
"FAILED" << endl ) ;
253 string err = (string)
"FAILED: Bad user name specified: "
255 cerr << err << endl ;
259 new_id = ent->pw_uid ;
265 BESDEBUG(
"server",
"FAILED" << endl ) ;
266 string err = (string)
"FAILED: BES cannot run as root" ;
267 cerr << err << endl ;
272 BESDEBUG(
"server",
"to " << new_id <<
" ... " << endl ) ;
273 if( setuid( new_id ) == -1 )
275 BESDEBUG(
"server",
"FAILED" << endl ) ;
277 err <<
"FAILED: Unable to set user id to " << new_id ;
278 cerr << err.str() << endl ;
288 bool needhelp = false ;
294 while( ( c = getopt( argc, argv,
"hvsd:c:p:u:i:r:" ) ) != EOF )
307 _portVal = atoi( optarg ) ;
311 _unixSocket = optarg ;
345 if( dashc.empty() && !dashi.empty() )
347 if( dashi[dashi.length()-1] !=
'/' )
351 string conf_file = dashi +
"etc/bes/bes.conf" ;
358 uid_t curr_euid = geteuid() ;
359 #ifndef BES_DEVELOPER
363 string err =
"FAILED: Must be root to run BES" ;
364 cerr << err << endl ;
369 cout <<
"Developer Mode: not testing if BES is run by root" << endl ;
384 cout <<
"Developer Mode: Running as root - setting group and user ids"
392 cout <<
"Developer Mode: Not setting group or user ids" << endl ;
400 string port_key =
"BES.ServerPort" ;
410 BESDEBUG(
"server",
"FAILED" << endl ) ;
411 string err = (string)
"FAILED: " + e.
get_message() ;
412 cerr << err << endl ;
418 _portVal = atoi( sPort.c_str() ) ;
427 string socket_key =
"BES.ServerUnixSocket" ;
428 if( _unixSocket ==
"" )
436 BESDEBUG(
"server",
"FAILED" << endl ) ;
437 string err = (string)
"FAILED: " + e.
get_message() ;
438 cerr << err << endl ;
444 if( !_gotPort && _unixSocket ==
"" )
446 string msg =
"Must specify a tcp port or a unix socket or both\n" ;
447 msg +=
"Please specify on the command line with -p <port>" ;
448 msg +=
" and/or -u <unix_socket>\n" ;
449 msg +=
"Or specify in the bes configuration file with "
450 + port_key +
" and/or " + socket_key +
"\n" ;
451 cout << endl << msg ;
457 if( _secure ==
false )
459 string key =
"BES.ServerSecure" ;
467 BESDEBUG(
"server",
"FAILED" << endl ) ;
468 string err = (string)
"FAILED: " + e.
get_message() ;
469 cerr << err << endl ;
473 if( isSecure ==
"Yes" || isSecure ==
"YES" || isSecure ==
"yes" )
479 BESDEBUG(
"server",
"ServerApp: Registering signal SIGTERM ... " << endl ) ;
482 BESDEBUG(
"server",
"FAILED" << endl ) ;
483 string err =
"FAILED: cannot register SIGTERM signal handler" ;
484 cerr << err << endl ;
488 BESDEBUG(
"server",
"OK" << endl ) ;
490 BESDEBUG(
"server",
"ServerApp: Registering signal SIGINT ... " << endl ) ;
493 BESDEBUG(
"server",
"FAILED" << endl ) ;
494 string err =
"FAILED: cannot register SIGINT signal handler" ;
495 cerr << err << endl ;
499 BESDEBUG(
"server",
"OK" << endl ) ;
501 BESDEBUG(
"server",
"ServerApp: Registering signal SIGUSR1 ... " << endl ) ;
504 BESDEBUG(
"server",
"FAILED" << endl ) ;
505 string err =
"FAILED: cannot register SIGUSR1 signal handler" ;
506 cerr << err << endl ;
510 BESDEBUG(
"server",
"OK" << endl ) ;
512 BESDEBUG(
"server",
"ServerApp: initializing default module ... "
515 BESDEBUG(
"server",
"ServerApp: done initializing default module"
518 BESDEBUG(
"server",
"ServerApp: initializing default commands ... "
521 BESDEBUG(
"server",
"ServerApp: done initializing default commands"
525 BESDEBUG(
"server",
"ServerApp: initializing loaded modules ... "
528 BESDEBUG(
"server",
"ServerApp: done initializing loaded modules"
531 BESDEBUG(
"server",
"ServerApp: initialized settings:" << *
this ) ;
546 BESDEBUG(
"server",
"ServerApp: initializing memory pool ... "
549 BESDEBUG(
"server",
"OK" << endl ) ;
557 BESDEBUG(
"server",
"ServerApp: listening on port ("
558 << _portVal <<
")" << endl ) ;
561 if( !_unixSocket.empty() )
565 BESDEBUG(
"server",
"ServerApp: listening on unix socket ("
566 << _unixSocket <<
")" << endl ) ;
571 _ps =
new PPTServer( &handler, &listener, _secure ) ;
582 cerr <<
"caught unknown exception" << endl ;
583 (*
BESLog::TheLog()) <<
"caught unknown exception initializing sockets"
594 pid_t apppid = getpid() ;
595 if( apppid == _mypid )
616 BESDEBUG(
"server",
"ServerApp: terminating loaded modules ... "
619 BESDEBUG(
"server",
"ServerApp: done terminating loaded modules"
622 BESDEBUG(
"server",
"ServerApp: terminating default commands ... "
625 BESDEBUG(
"server",
"ServerApp: done terminating default commands ... "
628 BESDEBUG(
"server",
"ServerApp: terminating default module ... "
631 BESDEBUG(
"server",
"ServerApp: done terminating default module ... "
647 << (
void *)
this <<
")" << endl ;
657 BESIndent::Indent() ;
668 BESIndent::Indent() ;
679 BESIndent::Indent() ;
697 return app.
main( argc, argv ) ;
701 cerr <<
"Caught unhandled exception: " << endl ;
707 cerr <<
"Caught unhandled, unknown exception" << endl ;