9 #include <sys/socket.h>
10 #include <arpa/inet.h>
19 /* apple's poll() does not work on char devs */
23 /* XXX should be from autoconf */
24 static const char *configfile="/etc/secnet/secnet.conf";
25 static const char *sites_key="sites";
26 bool_t just_check_config=False;
27 static char *userid=NULL;
30 bool_t background=True;
31 static char *pidfile=NULL;
32 bool_t require_root_privileges=False;
33 cstring_t require_root_privileges_explanation=NULL;
35 static pid_t secnet_pid;
37 /* Structures dealing with poll() call */
38 struct poll_interest {
39 beforepoll_fn *before;
45 struct poll_interest *next;
47 static struct poll_interest *reg=NULL;
48 static int32_t total_nfds=10;
50 static bool_t finished=False;
52 /* Parse the command line options */
53 static void parse_options(int argc, char **argv)
59 static struct option long_options[] = {
60 {"verbose", 0, 0, 'v'},
61 {"nowarnings", 0, 0, 'w'},
64 {"nodetach", 0, 0, 'n'},
65 {"managed", 0, 0, 'm'},
66 {"silent", 0, 0, 'f'},
69 {"config", 1, 0, 'c'},
70 {"just-check-config", 0, 0, 'j'},
71 {"sites-key", 1, 0, 's'},
75 c=getopt_long(argc, argv, "vwdnjc:ft:s:m",
76 long_options, &option_index);
83 printf("Usage: secnet [OPTION]...\n\n"
84 " -f, --silent, --quiet suppress error messages\n"
85 " -w, --nowarnings suppress warnings\n"
86 " -v, --verbose output extra diagnostics\n"
87 " -c, --config=filename specify a configuration file\n"
88 " -j, --just-check-config stop after reading "
89 "configuration file\n"
90 " -s, --sites-key=name configuration key that "
91 "specifies active sites\n"
92 " -n, --nodetach do not run in background\n"
93 " -m, --managed running under a supervisor\n"
94 " -d, --debug output debug messages\n"
95 " --help display this help and exit\n"
96 " --version output version information "
104 printf("%s\n",version);
109 message_level|=M_INFO|M_NOTICE|M_WARNING|M_ERR|M_SECURITY|
114 message_level&=(~M_WARNING);
118 message_level|=M_DEBUG_CONFIG|M_DEBUG_PHASE|M_DEBUG;
122 message_level=M_FATAL;
130 secnet_is_daemon=True;
135 configfile=safe_strdup(optarg,"config_filename");
137 fatal("secnet: no config filename specified");
141 just_check_config=True;
146 sites_key=safe_strdup(optarg,"sites-key");
148 fatal("secnet: no sites key specified");
156 Message(M_ERR,"secnet: Unknown getopt code %c\n",c);
160 if (argc-optind != 0) {
161 Message(M_ERR,"secnet: You gave extra command line parameters, "
162 "which were ignored.\n");
166 static void setup(dict_t *config)
175 l=dict_lookup(config,"system");
177 if (!l || list_elem(l,0)->type!=t_dict) {
178 fatal("configuration does not include a \"system\" dictionary");
180 system=list_elem(l,0)->data.dict;
181 loc=list_elem(l,0)->loc;
183 /* Arrange systemwide log facility */
184 l=dict_lookup(system,"log");
186 fatal("configuration does not include a system/log facility");
188 system_log=init_log(l);
190 /* Who are we supposed to run as? */
191 userid=dict_read_string(system,"userid",False,"system",loc);
193 if (!(pw=getpwnam(userid)))
194 fatal("userid \"%s\" not found",userid);
200 pidfile=dict_read_string(system,"pidfile",False,"system",loc);
202 /* Check whether we need root privileges */
203 if (require_root_privileges && uid!=0) {
204 fatal("the configured feature \"%s\" requires "
205 "that secnet retain root privileges while running.",
206 require_root_privileges_explanation);
209 /* Go along site list, starting sites */
210 l=dict_lookup(config,sites_key);
212 Message(M_WARNING,"secnet: configuration key \"%s\" is missing; no "
213 "remote sites are defined\n",sites_key);
216 while ((site=list_elem(l, i++))) {
218 if (site->type!=t_closure) {
219 cfgfatal(site->loc,"system","non-closure in site list");
221 if (site->data.closure->type!=CL_SITE) {
222 cfgfatal(site->loc,"system","non-site closure in site list");
224 s=site->data.closure->interface;
225 s->control(s->st,True);
230 void register_for_poll(void *st, beforepoll_fn *before,
231 afterpoll_fn *after, int32_t max_nfds, cstring_t desc)
233 struct poll_interest *i;
235 i=safe_malloc(sizeof(*i),"register_for_poll");
239 i->max_nfds=max_nfds;
242 assert(total_nfds < INT_MAX - max_nfds);
243 total_nfds+=max_nfds;
249 static void system_phase_hook(void *sst, uint32_t newphase)
251 if (newphase==PHASE_SHUTDOWN && pidfile) {
252 /* Try to unlink the pidfile; don't care if it fails */
258 static int fakepoll(struct pollfd *fds, int nfds, int timeout) {
259 fd_set infds[1], outfds[1];
260 int maxfd = -1, i, rc;
261 struct timeval tvtimeout;
264 for(i = 0; i < nfds; ++i) {
265 if(fds[i].events & POLLIN)
266 FD_SET(fds[i].fd, infds);
267 if(fds[i].events & POLLOUT)
268 FD_SET(fds[i].fd, outfds);
269 if(fds[i].fd > maxfd)
273 tvtimeout.tv_sec = timeout / 1000;
274 tvtimeout.tv_usec = 1000 * (timeout % 1000);
276 rc = select(maxfd + 1, infds, outfds, NULL,
277 timeout == -1 ? NULL : &tvtimeout);
279 for(i = 0; i < nfds; ++i) {
281 if(FD_ISSET(fds[i].fd, infds))
283 if(FD_ISSET(fds[i].fd, outfds))
285 fds[i].revents = revents;
292 struct timeval tv_now_global;
295 static void run(void)
297 struct poll_interest *i;
298 int rv, nfds, remain, idx;
302 fds=safe_malloc(sizeof(*fds)*total_nfds, "run");
304 Message(M_NOTICE,"%s [%d]: starting\n",version,secnet_pid);
307 if (gettimeofday(&tv_now_global, NULL)!=0) {
308 fatal_perror("main loop: gettimeofday");
310 now_global=((uint64_t)tv_now_global.tv_sec*(uint64_t)1000)+
311 ((uint64_t)tv_now_global.tv_usec/(uint64_t)1000);
313 for (i=reg; i; i=i->next) {
315 for (check=0; check<i->nfds; check++) {
316 if(fds[idx+check].revents & POLLNVAL) {
317 fatal("run: poll (%s#%d) set POLLNVAL", i->desc, check);
320 i->after(i->state, fds+idx, i->nfds);
326 for (i=reg; i; i=i->next) {
328 rv=i->before(i->state, fds+idx, &nfds, &timeout);
330 /* XXX we need to handle this properly: increase the
332 fatal("run: beforepoll_fn (%s) returns %d",i->desc,rv);
335 fatal("run: beforepoll_fn (%s) set timeout to %d",
345 rv=fakepoll(fds, idx, timeout);
347 rv=poll(fds, idx, timeout);
351 fatal_perror("run: poll");
359 /* Surrender privileges, if necessary */
360 static void droppriv(void)
364 fatal_perror("can't set gid to %ld",(long)gid);
365 if (initgroups(userid, gid) < 0)
366 fatal_perror("initgroups");
367 if (setuid(uid)!=0) {
368 fatal_perror("can't set uid to \"%s\"",userid);
370 assert(getuid() == uid);
371 assert(geteuid() == uid);
372 assert(getgid() == gid);
373 assert(getegid() == gid);
377 /* Become a daemon, if necessary */
378 static void become_daemon(void)
384 add_hook(PHASE_SHUTDOWN,system_phase_hook,NULL);
386 /* We only want to become a daemon if we are not one
388 if (background && !secnet_is_daemon) {
391 /* Parent process - just exit */
394 /* Child process - all done, just carry on */
395 secnet_is_daemon=True;
397 fatal_perror("setsid");
400 fatal_perror("cannot fork");
404 if (secnet_is_daemon) {
405 /* stderr etc are redirected to the system/log facility */
406 pipe_cloexec(errfds);
407 if (dup2(errfds[1],0) < 0
408 || dup2(errfds[1],1) < 0
409 || dup2(errfds[1],2) < 0)
410 fatal_perror("can't dup2 pipe");
411 if (close(errfds[1]) < 0)
412 fatal_perror("can't close redundant pipe endpoint");
413 log_from_fd(errfds[0],"stderr",system_log);
417 /* Now we can write the pidfile */
419 pf=fopen(pidfile,"w");
421 fatal_perror("cannot open pidfile \"%s\"",pidfile);
423 if (fprintf(pf,"%ld\n",(long)secnet_pid) < 0
425 fatal_perror("cannot write to pidfile \"%s\"",pidfile);
429 static signal_notify_fn finish,ignore_hup;
430 static void finish(void *st, int signum)
433 Message(M_NOTICE,"%s [%d]: received %s\n",version,secnet_pid,(string_t)st);
435 static void ignore_hup(void *st, int signum)
437 Message(M_INFO,"%s [%d]: received SIGHUP\n",version,secnet_pid);
441 int main(int argc, char **argv)
445 enter_phase(PHASE_GETOPTS);
446 parse_options(argc,argv);
448 enter_phase(PHASE_READCONFIG);
449 config=read_conffile(configfile);
451 enter_phase(PHASE_SETUP);
454 if (just_check_config) {
455 Message(M_INFO,"configuration file check complete\n");
459 enter_phase(PHASE_DAEMONIZE);
462 enter_phase(PHASE_GETRESOURCES);
463 /* Appropriate phase hooks will have been run */
465 enter_phase(PHASE_DROPPRIV);
468 start_signal_handling();
469 request_signal_notification(SIGTERM,finish,safe_strdup("SIGTERM","run"));
470 if (!background) request_signal_notification(SIGINT,finish,
471 safe_strdup("SIGINT","run"));
472 request_signal_notification(SIGHUP,ignore_hup,NULL);
473 enter_phase(PHASE_RUN);
476 enter_phase(PHASE_SHUTDOWN);
477 Message(M_NOTICE,"%s [%d]: finished\n",version,secnet_pid);