9 #include <sys/socket.h>
10 #include <arpa/inet.h>
18 /* apple's poll() does not work on char devs */
22 /* XXX should be from autoconf */
23 static const char *configfile="/etc/secnet/secnet.conf";
24 static const char *sites_key="sites";
25 bool_t just_check_config=False;
26 static char *userid=NULL;
28 bool_t background=True;
29 static char *pidfile=NULL;
30 bool_t require_root_privileges=False;
31 cstring_t require_root_privileges_explanation=NULL;
33 static pid_t secnet_pid;
35 /* Structures dealing with poll() call */
36 struct poll_interest {
37 beforepoll_fn *before;
43 struct poll_interest *next;
45 static struct poll_interest *reg=NULL;
46 static int32_t total_nfds=10;
48 static bool_t finished=False;
50 /* Parse the command line options */
51 static void parse_options(int argc, char **argv)
57 static struct option long_options[] = {
58 {"verbose", 0, 0, 'v'},
59 {"nowarnings", 0, 0, 'w'},
62 {"nodetach", 0, 0, 'n'},
63 {"silent", 0, 0, 'f'},
66 {"config", 1, 0, 'c'},
67 {"just-check-config", 0, 0, 'j'},
68 {"sites-key", 1, 0, 's'},
72 c=getopt_long(argc, argv, "vwdnjc:ft:s:",
73 long_options, &option_index);
80 printf("Usage: secnet [OPTION]...\n\n"
81 " -f, --silent, --quiet suppress error messages\n"
82 " -w, --nowarnings suppress warnings\n"
83 " -v, --verbose output extra diagnostics\n"
84 " -c, --config=filename specify a configuration file\n"
85 " -j, --just-check-config stop after reading "
86 "configuration file\n"
87 " -s, --sites-key=name configuration key that "
88 "specifies active sites\n"
89 " -n, --nodetach do not run in background\n"
90 " -d, --debug output debug messages\n"
91 " --help display this help and exit\n"
92 " --version output version information "
100 printf("%s\n",version);
105 message_level|=M_INFO|M_NOTICE|M_WARNING|M_ERR|M_SECURITY|
110 message_level&=(~M_WARNING);
114 message_level|=M_DEBUG_CONFIG|M_DEBUG_PHASE|M_DEBUG;
118 message_level=M_FATAL;
127 configfile=safe_strdup(optarg,"config_filename");
129 fatal("secnet: no config filename specified");
133 just_check_config=True;
138 sites_key=safe_strdup(optarg,"sites-key");
140 fatal("secnet: no sites key specified");
148 Message(M_ERR,"secnet: Unknown getopt code %c\n",c);
152 if (argc-optind != 0) {
153 Message(M_ERR,"secnet: You gave extra command line parameters, "
154 "which were ignored.\n");
158 static void setup(dict_t *config)
167 l=dict_lookup(config,"system");
169 if (!l || list_elem(l,0)->type!=t_dict) {
170 fatal("configuration does not include a \"system\" dictionary");
172 system=list_elem(l,0)->data.dict;
173 loc=list_elem(l,0)->loc;
175 /* Arrange systemwide log facility */
176 l=dict_lookup(system,"log");
178 fatal("configuration does not include a system/log facility");
180 system_log=init_log(l);
182 /* Who are we supposed to run as? */
183 userid=dict_read_string(system,"userid",False,"system",loc);
187 if (pw && strcmp(pw->pw_name,userid)==0) {
194 fatal("userid \"%s\" not found",userid);
199 pidfile=dict_read_string(system,"pidfile",False,"system",loc);
201 /* Check whether we need root privileges */
202 if (require_root_privileges && uid!=0) {
203 fatal("the configured feature \"%s\" requires "
204 "that secnet retain root privileges while running.",
205 require_root_privileges_explanation);
208 /* Go along site list, starting sites */
209 l=dict_lookup(config,sites_key);
211 Message(M_WARNING,"secnet: configuration key \"%s\" is missing; no "
212 "remote sites are defined\n",sites_key);
215 while ((site=list_elem(l, i++))) {
217 if (site->type!=t_closure) {
218 cfgfatal(site->loc,"system","non-closure in site list");
220 if (site->data.closure->type!=CL_SITE) {
221 cfgfatal(site->loc,"system","non-site closure in site list");
223 s=site->data.closure->interface;
224 s->control(s->st,True);
229 void register_for_poll(void *st, beforepoll_fn *before,
230 afterpoll_fn *after, int32_t max_nfds, cstring_t desc)
232 struct poll_interest *i;
234 i=safe_malloc(sizeof(*i),"register_for_poll");
238 i->max_nfds=max_nfds;
241 assert(total_nfds < INT_MAX - max_nfds);
242 total_nfds+=max_nfds;
248 static void system_phase_hook(void *sst, uint32_t newphase)
250 if (newphase==PHASE_SHUTDOWN && pidfile) {
251 /* Try to unlink the pidfile; don't care if it fails */
257 static int fakepoll(struct pollfd *fds, int nfds, int timeout) {
258 fd_set infds[1], outfds[1];
259 int maxfd = -1, i, rc;
260 struct timeval tvtimeout;
263 for(i = 0; i < nfds; ++i) {
264 if(fds[i].events & POLLIN)
265 FD_SET(fds[i].fd, infds);
266 if(fds[i].events & POLLOUT)
267 FD_SET(fds[i].fd, outfds);
268 if(fds[i].fd > maxfd)
272 tvtimeout.tv_sec = timeout / 1000;
273 tvtimeout.tv_usec = 1000 * (timeout % 1000);
275 rc = select(maxfd + 1, infds, outfds, NULL,
276 timeout == -1 ? NULL : &tvtimeout);
278 for(i = 0; i < nfds; ++i) {
280 if(FD_ISSET(fds[i].fd, infds))
282 if(FD_ISSET(fds[i].fd, outfds))
284 fds[i].revents = revents;
291 struct timeval tv_now_global;
294 static void run(void)
296 struct poll_interest *i;
297 int rv, nfds, remain, idx;
301 fds=safe_malloc(sizeof(*fds)*total_nfds, "run");
303 Message(M_NOTICE,"%s [%d]: starting\n",version,secnet_pid);
306 if (gettimeofday(&tv_now_global, NULL)!=0) {
307 fatal_perror("main loop: gettimeofday");
309 now_global=((uint64_t)tv_now_global.tv_sec*(uint64_t)1000)+
310 ((uint64_t)tv_now_global.tv_usec/(uint64_t)1000);
312 for (i=reg; i; i=i->next) {
314 for (check=0; check<i->nfds; check++) {
315 if(fds[idx+check].revents & POLLNVAL) {
316 fatal("run: poll (%s#%d) set POLLNVAL", i->desc, check);
319 i->after(i->state, fds+idx, i->nfds);
325 for (i=reg; i; i=i->next) {
327 rv=i->before(i->state, fds+idx, &nfds, &timeout);
329 /* XXX we need to handle this properly: increase the
331 fatal("run: beforepoll_fn (%s) returns %d",i->desc,rv);
334 fatal("run: beforepoll_fn (%s) set timeout to %d",timeout);
343 rv=fakepoll(fds, idx, timeout);
345 rv=poll(fds, idx, timeout);
349 fatal_perror("run: poll");
357 static void droppriv(void)
363 add_hook(PHASE_SHUTDOWN,system_phase_hook,NULL);
365 /* Open the pidfile for writing now: we may be unable to do so
366 once we drop privileges. */
368 pf=fopen(pidfile,"w");
370 fatal_perror("cannot open pidfile \"%s\"",pidfile);
373 if (!background && pf) {
374 fprintf(pf,"%d\n",getpid());
378 /* Now drop privileges */
380 if (setuid(uid)!=0) {
381 fatal_perror("can't set uid to \"%s\"",userid);
388 /* Parent process - write pidfile, exit */
389 fprintf(pf,"%d\n",p);
394 /* Child process - all done, just carry on */
396 /* Close stdin and stdout; we don't need them any more.
397 stderr is redirected to the system/log facility */
398 if (pipe(errfds)!=0) {
399 fatal_perror("can't create pipe for stderr");
407 secnet_is_daemon=True;
409 log_from_fd(errfds[0],"stderr",system_log);
412 fatal_perror("cannot fork");
419 static signal_notify_fn finish,ignore_hup;
420 static void finish(void *st, int signum)
423 Message(M_NOTICE,"%s [%d]: received %s\n",version,secnet_pid,(string_t)st);
425 static void ignore_hup(void *st, int signum)
427 Message(M_INFO,"%s [%d]: received SIGHUP\n",version,secnet_pid);
431 int main(int argc, char **argv)
435 enter_phase(PHASE_GETOPTS);
436 parse_options(argc,argv);
438 enter_phase(PHASE_READCONFIG);
439 config=read_conffile(configfile);
441 enter_phase(PHASE_SETUP);
444 if (just_check_config) {
445 Message(M_INFO,"configuration file check complete\n");
449 enter_phase(PHASE_GETRESOURCES);
450 /* Appropriate phase hooks will have been run */
452 enter_phase(PHASE_DROPPRIV);
455 start_signal_handling();
456 request_signal_notification(SIGTERM,finish,safe_strdup("SIGTERM","run"));
457 if (!background) request_signal_notification(SIGINT,finish,
458 safe_strdup("SIGINT","run"));
459 request_signal_notification(SIGHUP,ignore_hup,NULL);
460 enter_phase(PHASE_RUN);
463 enter_phase(PHASE_SHUTDOWN);
464 Message(M_NOTICE,"%s [%d]: finished\n",version,secnet_pid);