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;
44 LIST_ENTRY(poll_interest) entry;
46 static LIST_HEAD(, poll_interest) reg = LIST_HEAD_INITIALIZER(®);
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 {"managed", 0, 0, 'm'},
64 {"silent", 0, 0, 'f'},
67 {"config", 1, 0, 'c'},
68 {"just-check-config", 0, 0, 'j'},
69 {"sites-key", 1, 0, 's'},
73 c=getopt_long(argc, argv, "vwdnjc:ft:s:m",
74 long_options, &option_index);
81 printf("Usage: secnet [OPTION]...\n\n"
82 " -f, --silent, --quiet suppress error messages\n"
83 " -w, --nowarnings suppress warnings\n"
84 " -v, --verbose output extra diagnostics\n"
85 " -c, --config=filename specify a configuration file\n"
86 " -j, --just-check-config stop after reading "
87 "configuration file\n"
88 " -s, --sites-key=name configuration key that "
89 "specifies active sites\n"
90 " -n, --nodetach do not run in background\n"
91 " -m, --managed running under a supervisor\n"
92 " -d, --debug output debug messages\n"
93 " --help display this help and exit\n"
94 " --version output version information "
102 printf("%s\n",version);
107 message_level|=M_INFO|M_NOTICE|M_WARNING|M_ERR|M_SECURITY|
112 message_level&=(~M_WARNING);
116 message_level|=M_DEBUG_CONFIG|M_DEBUG_PHASE|M_DEBUG;
120 message_level=M_FATAL;
128 secnet_is_daemon=True;
133 configfile=safe_strdup(optarg,"config_filename");
135 fatal("secnet: no config filename specified");
139 just_check_config=True;
144 sites_key=safe_strdup(optarg,"sites-key");
146 fatal("secnet: no sites key specified");
154 Message(M_ERR,"secnet: Unknown getopt code %c\n",c);
158 if (argc-optind != 0) {
159 Message(M_ERR,"secnet: You gave extra command line parameters, "
160 "which were ignored.\n");
164 static void setup(dict_t *config)
173 l=dict_lookup(config,"system");
175 if (!l || list_elem(l,0)->type!=t_dict) {
176 fatal("configuration does not include a \"system\" dictionary");
178 system=list_elem(l,0)->data.dict;
179 loc=list_elem(l,0)->loc;
181 /* Arrange systemwide log facility */
182 l=dict_lookup(system,"log");
184 fatal("configuration does not include a system/log facility");
186 system_log=init_log(l);
188 /* Who are we supposed to run as? */
189 userid=dict_read_string(system,"userid",False,"system",loc);
191 if (!(pw=getpwnam(userid)))
192 fatal("userid \"%s\" not found",userid);
198 pidfile=dict_read_string(system,"pidfile",False,"system",loc);
200 /* Check whether we need root privileges */
201 if (require_root_privileges && uid!=0) {
202 fatal("the configured feature \"%s\" requires "
203 "that secnet retain root privileges while running.",
204 require_root_privileges_explanation);
207 /* Go along site list, starting sites */
208 l=dict_lookup(config,sites_key);
210 Message(M_WARNING,"secnet: configuration key \"%s\" is missing; no "
211 "remote sites are defined\n",sites_key);
214 while ((site=list_elem(l, i++))) {
216 if (site->type!=t_closure) {
217 cfgfatal(site->loc,"system","non-closure in site list");
219 if (site->data.closure->type!=CL_SITE) {
220 cfgfatal(site->loc,"system","non-site closure in site list");
222 s=site->data.closure->interface;
223 s->control(s->st,True);
228 void register_for_poll(void *st, beforepoll_fn *before,
229 afterpoll_fn *after, cstring_t desc)
231 struct poll_interest *i;
233 i=safe_malloc(sizeof(*i),"register_for_poll");
239 LIST_INSERT_HEAD(®, i, entry);
243 static void system_phase_hook(void *sst, uint32_t newphase)
245 if (newphase==PHASE_SHUTDOWN && pidfile) {
246 /* Try to unlink the pidfile; don't care if it fails */
252 static int fakepoll(struct pollfd *fds, int nfds, int timeout) {
253 fd_set infds[1], outfds[1];
254 int maxfd = -1, i, rc;
255 struct timeval tvtimeout;
258 for(i = 0; i < nfds; ++i) {
259 if(fds[i].events & POLLIN)
260 FD_SET(fds[i].fd, infds);
261 if(fds[i].events & POLLOUT)
262 FD_SET(fds[i].fd, outfds);
263 if(fds[i].fd > maxfd)
267 tvtimeout.tv_sec = timeout / 1000;
268 tvtimeout.tv_usec = 1000 * (timeout % 1000);
270 rc = select(maxfd + 1, infds, outfds, NULL,
271 timeout == -1 ? NULL : &tvtimeout);
273 for(i = 0; i < nfds; ++i) {
275 if(FD_ISSET(fds[i].fd, infds))
277 if(FD_ISSET(fds[i].fd, outfds))
279 fds[i].revents = revents;
286 struct timeval tv_now_global;
289 static void run(void)
291 struct poll_interest *i;
294 struct pollfd *fds=0;
295 int allocdfds=0, shortfall=0;
297 Message(M_NOTICE,"%s [%d]: starting\n",version,secnet_pid);
300 if (gettimeofday(&tv_now_global, NULL)!=0) {
301 fatal_perror("main loop: gettimeofday");
303 now_global=((uint64_t)tv_now_global.tv_sec*(uint64_t)1000)+
304 ((uint64_t)tv_now_global.tv_usec/(uint64_t)1000);
306 LIST_FOREACH(i, ®, entry) {
308 for (check=0; check<i->nfds; check++) {
309 if(fds[idx+check].revents & POLLNVAL) {
310 fatal("run: poll (%s#%d) set POLLNVAL", i->desc, check);
313 i->after(i->state, fds+idx, i->nfds);
318 allocdfds += shortfall;
319 fds=safe_realloc_ary(fds,sizeof(*fds),allocdfds, "run");
324 LIST_FOREACH(i, ®, entry) {
325 int remain=allocdfds-idx;
327 rv=i->before(i->state, fds+idx, &nfds, &timeout);
330 fatal("run: beforepoll_fn (%s) returns %d",i->desc,rv);
331 assert(nfds < INT_MAX/4 - shortfall);
332 shortfall += nfds-remain;
337 fatal("run: beforepoll_fn (%s) set timeout to %d",
346 rv=fakepoll(fds, idx, timeout);
348 rv=poll(fds, idx, timeout);
352 fatal_perror("run: poll");
360 /* Surrender privileges, if necessary */
361 static void droppriv(void)
365 fatal_perror("can't set gid to %ld",(long)gid);
366 if (initgroups(userid, gid) < 0)
367 fatal_perror("initgroups");
368 if (setuid(uid)!=0) {
369 fatal_perror("can't set uid to \"%s\"",userid);
371 assert(getuid() == uid);
372 assert(geteuid() == uid);
373 assert(getgid() == gid);
374 assert(getegid() == gid);
378 /* Become a daemon, if necessary */
379 static void become_daemon(void)
385 add_hook(PHASE_SHUTDOWN,system_phase_hook,NULL);
387 /* We only want to become a daemon if we are not one
389 if (background && !secnet_is_daemon) {
392 /* Parent process - just exit */
395 /* Child process - all done, just carry on */
396 secnet_is_daemon=True;
398 fatal_perror("setsid");
401 fatal_perror("cannot fork");
405 if (secnet_is_daemon) {
406 /* stderr etc are redirected to the system/log facility */
407 pipe_cloexec(errfds);
408 if (dup2(errfds[1],0) < 0
409 || dup2(errfds[1],1) < 0
410 || dup2(errfds[1],2) < 0)
411 fatal_perror("can't dup2 pipe");
412 if (close(errfds[1]) < 0)
413 fatal_perror("can't close redundant pipe endpoint");
414 log_from_fd(errfds[0],"stderr",system_log);
418 /* Now we can write the pidfile */
420 pf=fopen(pidfile,"w");
422 fatal_perror("cannot open pidfile \"%s\"",pidfile);
424 if (fprintf(pf,"%ld\n",(long)secnet_pid) < 0
426 fatal_perror("cannot write to pidfile \"%s\"",pidfile);
430 static signal_notify_fn finish,ignore_hup;
431 static void finish(void *st, int signum)
434 Message(M_NOTICE,"%s [%d]: received %s\n",version,secnet_pid,(string_t)st);
436 static void ignore_hup(void *st, int signum)
438 Message(M_INFO,"%s [%d]: received SIGHUP\n",version,secnet_pid);
442 int main(int argc, char **argv)
446 enter_phase(PHASE_GETOPTS);
447 parse_options(argc,argv);
449 enter_phase(PHASE_READCONFIG);
450 config=read_conffile(configfile);
452 enter_phase(PHASE_SETUP);
455 if (just_check_config) {
456 Message(M_INFO,"configuration file check complete\n");
460 enter_phase(PHASE_DAEMONIZE);
463 enter_phase(PHASE_GETRESOURCES);
464 /* Appropriate phase hooks will have been run */
466 enter_phase(PHASE_DROPPRIV);
469 start_signal_handling();
470 request_signal_notification(SIGTERM,finish,safe_strdup("SIGTERM","run"));
471 if (!background) request_signal_notification(SIGINT,finish,
472 safe_strdup("SIGINT","run"));
473 request_signal_notification(SIGHUP,ignore_hup,NULL);
474 enter_phase(PHASE_RUN);
477 enter_phase(PHASE_SHUTDOWN);
478 Message(M_NOTICE,"%s [%d]: finished\n",version,secnet_pid);