9 #include <sys/socket.h>
10 #include <arpa/inet.h>
17 /* XXX should be from autoconf */
18 static const char *configfile="/etc/secnet/secnet.conf";
19 static const char *sites_key="sites";
20 bool_t just_check_config=False;
21 static char *userid=NULL;
23 bool_t background=True;
24 static char *pidfile=NULL;
25 bool_t require_root_privileges=False;
26 cstring_t require_root_privileges_explanation=NULL;
28 static pid_t secnet_pid;
30 /* Structures dealing with poll() call */
31 struct poll_interest {
32 beforepoll_fn *before;
38 struct poll_interest *next;
40 static struct poll_interest *reg=NULL;
41 static int32_t total_nfds=10;
43 static bool_t finished=False;
45 /* Parse the command line options */
46 static void parse_options(int argc, char **argv)
52 static struct option long_options[] = {
53 {"verbose", 0, 0, 'v'},
54 {"nowarnings", 0, 0, 'w'},
57 {"nodetach", 0, 0, 'n'},
58 {"silent", 0, 0, 'f'},
61 {"config", 1, 0, 'c'},
62 {"just-check-config", 0, 0, 'j'},
63 {"sites-key", 1, 0, 's'},
67 c=getopt_long(argc, argv, "vwdnjc:ft:s:",
68 long_options, &option_index);
75 printf("Usage: secnet [OPTION]...\n\n"
76 " -f, --silent, --quiet suppress error messages\n"
77 " -w, --nowarnings suppress warnings\n"
78 " -v, --verbose output extra diagnostics\n"
79 " -c, --config=filename specify a configuration file\n"
80 " -j, --just-check-config stop after reading "
81 "configuration file\n"
82 " -s, --sites-key=name configuration key that "
83 "specifies active sites\n"
84 " -n, --nodetach do not run in background\n"
85 " -d, --debug=item,... set debug options\n"
86 " --help display this help and exit\n"
87 " --version output version information "
95 printf("%s\n",version);
100 message_level|=M_INFO|M_NOTICE|M_WARNING|M_ERR|M_SECURITY|
105 message_level&=(~M_WARNING);
109 message_level|=M_DEBUG_CONFIG|M_DEBUG_PHASE|M_DEBUG;
113 message_level=M_FATAL;
122 configfile=safe_strdup(optarg,"config_filename");
124 fatal("secnet: no config filename specified");
128 just_check_config=True;
133 sites_key=safe_strdup(optarg,"sites-key");
135 fatal("secnet: no sites key specified");
142 Message(M_ERR,"secnet: Unknown getopt code %c\n",c);
146 if (argc-optind != 0) {
147 Message(M_ERR,"secnet: You gave extra command line parameters, "
148 "which were ignored.\n");
152 static void setup(dict_t *config)
161 l=dict_lookup(config,"system");
163 if (!l || list_elem(l,0)->type!=t_dict) {
164 fatal("configuration does not include a \"system\" dictionary");
166 system=list_elem(l,0)->data.dict;
167 loc=list_elem(l,0)->loc;
169 /* Arrange systemwide log facility */
170 l=dict_lookup(system,"log");
172 fatal("configuration does not include a system/log facility");
174 system_log=init_log(l);
176 /* Who are we supposed to run as? */
177 userid=dict_read_string(system,"userid",False,"system",loc);
181 if (pw && strcmp(pw->pw_name,userid)==0) {
188 fatal("userid \"%s\" not found",userid);
193 pidfile=dict_read_string(system,"pidfile",False,"system",loc);
195 /* Check whether we need root privileges */
196 if (require_root_privileges && uid!=0) {
197 fatal("the configured feature \"%s\" requires "
198 "that secnet retain root privileges while running.",
199 require_root_privileges_explanation);
202 /* Go along site list, starting sites */
203 l=dict_lookup(config,sites_key);
205 Message(M_WARNING,"secnet: configuration key \"%s\" is missing; no "
206 "remote sites are defined\n",sites_key);
209 while ((site=list_elem(l, i++))) {
211 if (site->type!=t_closure) {
212 cfgfatal(site->loc,"system","non-closure in site list");
214 if (site->data.closure->type!=CL_SITE) {
215 cfgfatal(site->loc,"system","non-site closure in site list");
217 s=site->data.closure->interface;
218 s->control(s->st,True);
223 void register_for_poll(void *st, beforepoll_fn *before,
224 afterpoll_fn *after, int32_t max_nfds, cstring_t desc)
226 struct poll_interest *i;
228 i=safe_malloc(sizeof(*i),"register_for_poll");
232 i->max_nfds=max_nfds;
235 assert(total_nfds < INT_MAX - max_nfds);
236 total_nfds+=max_nfds;
242 static void system_phase_hook(void *sst, uint32_t newphase)
244 if (newphase==PHASE_SHUTDOWN && pidfile) {
245 /* Try to unlink the pidfile; don't care if it fails */
250 struct timeval tv_now_global;
253 static void run(void)
255 struct poll_interest *i;
256 int rv, nfds, remain, idx;
260 fds=safe_malloc(sizeof(*fds)*total_nfds, "run");
262 Message(M_NOTICE,"%s [%d]: starting\n",version,secnet_pid);
265 if (gettimeofday(&tv_now_global, NULL)!=0) {
266 fatal_perror("main loop: gettimeofday");
268 now_global=((uint64_t)tv_now_global.tv_sec*(uint64_t)1000)+
269 ((uint64_t)tv_now_global.tv_usec/(uint64_t)1000);
271 for (i=reg; i; i=i->next) {
272 i->after(i->state, fds+idx, i->nfds);
278 for (i=reg; i; i=i->next) {
280 rv=i->before(i->state, fds+idx, &nfds, &timeout);
282 /* XXX we need to handle this properly: increase the
284 fatal("run: beforepoll_fn (%s) returns %d",i->desc,rv);
287 fatal("run: beforepoll_fn (%s) set timeout to %d",timeout);
295 rv=poll(fds, idx, timeout);
298 fatal_perror("run: poll");
306 static void droppriv(void)
312 add_hook(PHASE_SHUTDOWN,system_phase_hook,NULL);
314 /* Open the pidfile for writing now: we may be unable to do so
315 once we drop privileges. */
317 pf=fopen(pidfile,"w");
319 fatal_perror("cannot open pidfile \"%s\"",pidfile);
322 if (!background && pf) {
323 fprintf(pf,"%d\n",getpid());
327 /* Now drop privileges */
329 if (setuid(uid)!=0) {
330 fatal_perror("can't set uid to \"%s\"",userid);
337 /* Parent process - write pidfile, exit */
338 fprintf(pf,"%d\n",p);
343 /* Child process - all done, just carry on */
345 /* Close stdin and stdout; we don't need them any more.
346 stderr is redirected to the system/log facility */
347 if (pipe(errfds)!=0) {
348 fatal_perror("can't create pipe for stderr");
356 secnet_is_daemon=True;
358 log_from_fd(errfds[0],"stderr",system_log);
361 fatal_perror("cannot fork");
368 static signal_notify_fn finish,ignore_hup;
369 static void finish(void *st, int signum)
372 Message(M_NOTICE,"%s [%d]: received %s\n",version,secnet_pid,(string_t)st);
374 static void ignore_hup(void *st, int signum)
376 Message(M_INFO,"%s [%d]: received SIGHUP\n",version,secnet_pid);
380 int main(int argc, char **argv)
384 enter_phase(PHASE_GETOPTS);
385 parse_options(argc,argv);
387 enter_phase(PHASE_READCONFIG);
388 config=read_conffile(configfile);
390 enter_phase(PHASE_SETUP);
393 if (just_check_config) {
394 Message(M_INFO,"configuration file check complete\n");
398 enter_phase(PHASE_GETRESOURCES);
399 /* Appropriate phase hooks will have been run */
401 enter_phase(PHASE_DROPPRIV);
404 start_signal_handling();
405 request_signal_notification(SIGTERM,finish,safe_strdup("SIGTERM","run"));
406 if (!background) request_signal_notification(SIGINT,finish,
407 safe_strdup("SIGINT","run"));
408 request_signal_notification(SIGHUP,ignore_hup,NULL);
409 enter_phase(PHASE_RUN);
412 enter_phase(PHASE_SHUTDOWN);
413 Message(M_NOTICE,"%s [%d]: finished\n",version,secnet_pid);