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 LIST_ENTRY(poll_interest) entry;
47 static LIST_HEAD(, poll_interest) reg = LIST_HEAD_INITIALIZER(®);
49 static bool_t finished=False;
51 /* Parse the command line options */
52 static void parse_options(int argc, char **argv)
58 static struct option long_options[] = {
59 {"verbose", 0, 0, 'v'},
60 {"nowarnings", 0, 0, 'w'},
63 {"nodetach", 0, 0, 'n'},
64 {"managed", 0, 0, 'm'},
65 {"silent", 0, 0, 'f'},
68 {"config", 1, 0, 'c'},
69 {"just-check-config", 0, 0, 'j'},
70 {"sites-key", 1, 0, 's'},
74 c=getopt_long(argc, argv, "vwdnjc:ft:s:m",
75 long_options, &option_index);
82 printf("Usage: secnet [OPTION]...\n\n"
83 " -f, --silent, --quiet suppress error messages\n"
84 " -w, --nowarnings suppress warnings\n"
85 " -v, --verbose output extra diagnostics\n"
86 " -c, --config=filename specify a configuration file\n"
87 " -j, --just-check-config stop after reading "
88 "configuration file\n"
89 " -s, --sites-key=name configuration key that "
90 "specifies active sites\n"
91 " -n, --nodetach do not run in background\n"
92 " -m, --managed running under a supervisor\n"
93 " -d, --debug output debug messages\n"
94 " --help display this help and exit\n"
95 " --version output version information "
103 printf("%s\n",version);
108 message_level|=M_INFO|M_NOTICE|M_WARNING|M_ERR|M_SECURITY|
113 message_level&=(~M_WARNING);
117 message_level|=M_DEBUG_CONFIG|M_DEBUG_PHASE|M_DEBUG;
121 message_level=M_FATAL;
129 secnet_is_daemon=True;
134 configfile=safe_strdup(optarg,"config_filename");
136 fatal("secnet: no config filename specified");
140 just_check_config=True;
145 sites_key=safe_strdup(optarg,"sites-key");
147 fatal("secnet: no sites key specified");
155 Message(M_ERR,"secnet: Unknown getopt code %c\n",c);
159 if (argc-optind != 0) {
160 Message(M_ERR,"secnet: You gave extra command line parameters, "
161 "which were ignored.\n");
165 static void setup(dict_t *config)
174 l=dict_lookup(config,"system");
176 if (!l || list_elem(l,0)->type!=t_dict) {
177 fatal("configuration does not include a \"system\" dictionary");
179 system=list_elem(l,0)->data.dict;
180 loc=list_elem(l,0)->loc;
182 /* Arrange systemwide log facility */
183 l=dict_lookup(system,"log");
185 fatal("configuration does not include a system/log facility");
187 system_log=init_log(l);
189 /* Who are we supposed to run as? */
190 userid=dict_read_string(system,"userid",False,"system",loc);
192 if (!(pw=getpwnam(userid)))
193 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 LIST_INSERT_HEAD(®, i, entry);
245 static void system_phase_hook(void *sst, uint32_t newphase)
247 if (newphase==PHASE_SHUTDOWN && pidfile) {
248 /* Try to unlink the pidfile; don't care if it fails */
254 static int fakepoll(struct pollfd *fds, int nfds, int timeout) {
255 fd_set infds[1], outfds[1];
256 int maxfd = -1, i, rc;
257 struct timeval tvtimeout;
260 for(i = 0; i < nfds; ++i) {
261 if(fds[i].events & POLLIN)
262 FD_SET(fds[i].fd, infds);
263 if(fds[i].events & POLLOUT)
264 FD_SET(fds[i].fd, outfds);
265 if(fds[i].fd > maxfd)
269 tvtimeout.tv_sec = timeout / 1000;
270 tvtimeout.tv_usec = 1000 * (timeout % 1000);
272 rc = select(maxfd + 1, infds, outfds, NULL,
273 timeout == -1 ? NULL : &tvtimeout);
275 for(i = 0; i < nfds; ++i) {
277 if(FD_ISSET(fds[i].fd, infds))
279 if(FD_ISSET(fds[i].fd, outfds))
281 fds[i].revents = revents;
288 struct timeval tv_now_global;
291 static void run(void)
293 struct poll_interest *i;
296 struct pollfd *fds=0;
297 int allocdfds=0, shortfall=0;
299 Message(M_NOTICE,"%s [%d]: starting\n",version,secnet_pid);
302 if (gettimeofday(&tv_now_global, NULL)!=0) {
303 fatal_perror("main loop: gettimeofday");
305 now_global=((uint64_t)tv_now_global.tv_sec*(uint64_t)1000)+
306 ((uint64_t)tv_now_global.tv_usec/(uint64_t)1000);
308 LIST_FOREACH(i, ®, entry) {
310 for (check=0; check<i->nfds; check++) {
311 if(fds[idx+check].revents & POLLNVAL) {
312 fatal("run: poll (%s#%d) set POLLNVAL", i->desc, check);
315 i->after(i->state, fds+idx, i->nfds);
320 allocdfds += shortfall;
321 fds=safe_realloc_ary(fds,sizeof(*fds),allocdfds, "run");
326 LIST_FOREACH(i, ®, entry) {
327 int remain=allocdfds-idx;
329 rv=i->before(i->state, fds+idx, &nfds, &timeout);
332 fatal("run: beforepoll_fn (%s) returns %d",i->desc,rv);
333 assert(nfds < INT_MAX/4 - shortfall);
334 shortfall += nfds-remain;
339 fatal("run: beforepoll_fn (%s) set timeout to %d",
348 rv=fakepoll(fds, idx, timeout);
350 rv=poll(fds, idx, timeout);
354 fatal_perror("run: poll");
362 /* Surrender privileges, if necessary */
363 static void droppriv(void)
367 fatal_perror("can't set gid to %ld",(long)gid);
368 if (initgroups(userid, gid) < 0)
369 fatal_perror("initgroups");
370 if (setuid(uid)!=0) {
371 fatal_perror("can't set uid to \"%s\"",userid);
373 assert(getuid() == uid);
374 assert(geteuid() == uid);
375 assert(getgid() == gid);
376 assert(getegid() == gid);
380 /* Become a daemon, if necessary */
381 static void become_daemon(void)
387 add_hook(PHASE_SHUTDOWN,system_phase_hook,NULL);
389 /* We only want to become a daemon if we are not one
391 if (background && !secnet_is_daemon) {
394 /* Parent process - just exit */
397 /* Child process - all done, just carry on */
398 secnet_is_daemon=True;
400 fatal_perror("setsid");
403 fatal_perror("cannot fork");
407 if (secnet_is_daemon) {
408 /* stderr etc are redirected to the system/log facility */
409 pipe_cloexec(errfds);
410 if (dup2(errfds[1],0) < 0
411 || dup2(errfds[1],1) < 0
412 || dup2(errfds[1],2) < 0)
413 fatal_perror("can't dup2 pipe");
414 if (close(errfds[1]) < 0)
415 fatal_perror("can't close redundant pipe endpoint");
416 log_from_fd(errfds[0],"stderr",system_log);
420 /* Now we can write the pidfile */
422 pf=fopen(pidfile,"w");
424 fatal_perror("cannot open pidfile \"%s\"",pidfile);
426 if (fprintf(pf,"%ld\n",(long)secnet_pid) < 0
428 fatal_perror("cannot write to pidfile \"%s\"",pidfile);
432 static signal_notify_fn finish,ignore_hup;
433 static void finish(void *st, int signum)
436 Message(M_NOTICE,"%s [%d]: received %s\n",version,secnet_pid,(string_t)st);
438 static void ignore_hup(void *st, int signum)
440 Message(M_INFO,"%s [%d]: received SIGHUP\n",version,secnet_pid);
444 int main(int argc, char **argv)
448 enter_phase(PHASE_GETOPTS);
449 parse_options(argc,argv);
451 enter_phase(PHASE_READCONFIG);
452 config=read_conffile(configfile);
454 enter_phase(PHASE_SETUP);
457 if (just_check_config) {
458 Message(M_INFO,"configuration file check complete\n");
462 enter_phase(PHASE_DAEMONIZE);
465 enter_phase(PHASE_GETRESOURCES);
466 /* Appropriate phase hooks will have been run */
468 enter_phase(PHASE_DROPPRIV);
471 start_signal_handling();
472 request_signal_notification(SIGTERM,finish,safe_strdup("SIGTERM","run"));
473 if (!background) request_signal_notification(SIGINT,finish,
474 safe_strdup("SIGINT","run"));
475 request_signal_notification(SIGHUP,ignore_hup,NULL);
476 enter_phase(PHASE_RUN);
479 enter_phase(PHASE_SHUTDOWN);
480 Message(M_NOTICE,"%s [%d]: finished\n",version,secnet_pid);