2 * Revision 1.1 1996/03/13 22:27:41 sde1000
15 #include <sys/socket.h>
16 #include <arpa/inet.h>
23 /* XXX should be from autoconf */
24 static char *configfile="/etc/secnet/secnet.conf";
25 static char *sites_key="sites";
26 bool_t just_check_config=False;
27 static char *userid=NULL;
29 bool_t background=True;
30 static char *pidfile=NULL;
31 bool_t require_root_privileges=False;
32 string_t require_root_privileges_explanation=NULL;
34 static pid_t secnet_pid;
37 extern uint32_t message_level;
38 extern bool_t secnet_is_daemon;
39 extern struct log_if *system_log;
42 extern void start_signal_handling(void);
44 /* Structures dealing with poll() call */
45 struct poll_interest {
46 beforepoll_fn *before;
52 struct poll_interest *next;
54 static struct poll_interest *reg=NULL;
55 static uint32_t total_nfds=10;
57 static bool_t finished=False;
59 /* Parse the command line options */
60 static void parse_options(int argc, char **argv)
66 static struct option long_options[] = {
67 {"verbose", 0, 0, 'v'},
68 {"nowarnings", 0, 0, 'w'},
71 {"nodetach", 0, 0, 'n'},
72 {"silent", 0, 0, 'f'},
75 {"config", 1, 0, 'c'},
76 {"just-check-config", 0, 0, 'j'},
77 {"sites-key", 1, 0, 's'},
81 c=getopt_long(argc, argv, "vwdnjc:ft:s:",
82 long_options, &option_index);
90 "Usage: secnet [OPTION]...\n\n"
91 " -f, --silent, --quiet suppress error messages\n"
92 " -w, --nowarnings suppress warnings\n"
93 " -v, --verbose output extra diagnostics\n"
94 " -c, --config=filename specify a configuration file\n"
95 " -j, --just-check-config stop after reading "
96 "configuration file\n"
97 " -s, --sites-key=name configuration key that "
98 "specifies active sites\n"
99 " -n, --nodetach do not run in background\n"
100 " -d, --debug=item,... set debug options\n"
101 " --help display this help and exit\n"
102 " --version output version information "
110 fprintf(stderr,"%s\n",version);
115 message_level|=M_INFO|M_NOTICE|M_WARNING|M_ERR|M_SECURITY|
120 message_level&=(~M_WARNING);
124 message_level|=M_DEBUG_CONFIG|M_DEBUG_PHASE|M_DEBUG;
128 message_level=M_FATAL;
137 configfile=safe_strdup(optarg,"config_filename");
139 fatal("secnet: no config filename specified");
143 just_check_config=True;
148 sites_key=safe_strdup(optarg,"sites-key");
150 fatal("secnet: no sites key specified");
157 Message(M_ERR,"secnet: Unknown getopt code %c\n",c);
161 if (argc-optind != 0) {
162 Message(M_ERR,"secnet: You gave extra command line parameters, "
163 "which were ignored.\n");
167 static void setup(dict_t *config)
176 l=dict_lookup(config,"system");
178 if (!l || list_elem(l,0)->type!=t_dict) {
179 fatal("configuration does not include a \"system\" dictionary\n");
181 system=list_elem(l,0)->data.dict;
182 loc=list_elem(l,0)->loc;
184 /* Arrange systemwide log facility */
185 l=dict_lookup(system,"log");
187 fatal("configuration does not include a system/log facility\n");
189 system_log=init_log(l);
191 /* Who are we supposed to run as? */
192 userid=dict_read_string(system,"userid",False,"system",loc);
196 if (pw && strcmp(pw->pw_name,userid)==0) {
203 fatal("userid \"%s\" not found\n",userid);
208 pidfile=dict_read_string(system,"pidfile",False,"system",loc);
210 /* Check whether we need root privileges */
211 if (require_root_privileges && uid!=0) {
212 fatal("the following configured feature (\"%s\") requires "
213 "that secnet retain root privileges while running.\n",
214 require_root_privileges_explanation);
217 /* Go along site list, starting sites */
218 l=dict_lookup(config,sites_key);
220 Message(M_WARNING,"secnet: configuration key \"%s\" is missing; no "
221 "remote sites are defined\n",sites_key);
224 while ((site=list_elem(l, i++))) {
226 if (site->type!=t_closure) {
227 cfgfatal(site->loc,"system","non-closure in site list");
229 if (site->data.closure->type!=CL_SITE) {
230 cfgfatal(site->loc,"system","non-site closure in site list");
232 s=site->data.closure->interface;
233 s->control(s->st,True);
238 void register_for_poll(void *st, beforepoll_fn *before,
239 afterpoll_fn *after, uint32_t max_nfds, string_t desc)
241 struct poll_interest *i;
243 i=safe_malloc(sizeof(*i),"register_for_poll");
247 i->max_nfds=max_nfds;
250 total_nfds+=max_nfds;
256 static void system_phase_hook(void *sst, uint32_t newphase)
258 if (newphase==PHASE_SHUTDOWN && pidfile) {
259 /* Try to unlink the pidfile; don't care if it fails */
264 static void run(void)
266 struct timeval tv_now;
268 struct poll_interest *i;
269 int rv, nfds, remain, idx;
273 fds=alloca(sizeof(*fds)*total_nfds);
275 fatal("run: couldn't alloca\n");
278 Message(M_NOTICE,"%s [%d]: starting\n",version,secnet_pid);
281 if (gettimeofday(&tv_now, NULL)!=0) {
282 fatal_perror("main loop: gettimeofday");
284 now=(tv_now.tv_sec*1000)+(tv_now.tv_usec/1000);
286 for (i=reg; i; i=i->next) {
287 i->after(i->state, fds+idx, i->nfds, &tv_now, &now);
293 for (i=reg; i; i=i->next) {
295 rv=i->before(i->state, fds+idx, &nfds, &timeout, &tv_now, &now);
297 /* XXX we need to handle this properly: increase the
299 fatal("run: beforepoll_fn (%s) returns %d\n",i->desc,rv);
302 fatal("run: beforepoll_fn (%s) set timeout to %d\n",timeout);
310 rv=poll(fds, idx, timeout);
313 fatal_perror("run: poll");
320 static void droppriv(void)
325 add_hook(PHASE_SHUTDOWN,system_phase_hook,NULL);
327 /* Open the pidfile for writing now: we may be unable to do so
328 once we drop privileges. */
330 pf=fopen(pidfile,"w");
332 fatal_perror("cannot open pidfile \"%s\"",pidfile);
335 if (!background && pf) {
336 fprintf(pf,"%d\n",getpid());
340 /* Now drop privileges */
342 if (setuid(uid)!=0) {
343 fatal_perror("can't set uid to \"%s\"",userid);
350 /* Parent process - write pidfile, exit */
351 fprintf(pf,"%d\n",p);
356 /* Child process - all done, just carry on */
358 /* Close stdin, stdout and stderr; we don't need them any more */
362 secnet_is_daemon=True;
365 fatal_perror("cannot fork");
372 static signal_notify_fn finish,ignore_hup;
373 static void finish(void *st, int signum)
376 Message(M_NOTICE,"%s [%d]: received %s\n",version,secnet_pid,(string_t)st);
378 static void ignore_hup(void *st, int signum)
380 Message(M_INFO,"%s [%d]: received SIGHUP\n",version,secnet_pid);
384 int main(int argc, char **argv)
388 enter_phase(PHASE_GETOPTS);
389 parse_options(argc,argv);
391 enter_phase(PHASE_READCONFIG);
392 config=read_conffile(configfile);
394 enter_phase(PHASE_SETUP);
397 if (just_check_config) {
398 Message(M_INFO,"configuration file check complete\n");
402 enter_phase(PHASE_GETRESOURCES);
403 /* Appropriate phase hooks will have been run */
405 enter_phase(PHASE_DROPPRIV);
408 start_signal_handling();
409 request_signal_notification(SIGTERM,finish,"SIGTERM");
410 if (!background) request_signal_notification(SIGINT,finish,"SIGINT");
411 request_signal_notification(SIGHUP,ignore_hup,NULL);
412 enter_phase(PHASE_RUN);
415 enter_phase(PHASE_SHUTDOWN);
416 Message(M_NOTICE,"%s [%d]: finished\n",version,secnet_pid);