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 {"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:",
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 " -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;
129 configfile=safe_strdup(optarg,"config_filename");
131 fatal("secnet: no config filename specified");
135 just_check_config=True;
140 sites_key=safe_strdup(optarg,"sites-key");
142 fatal("secnet: no sites key specified");
150 Message(M_ERR,"secnet: Unknown getopt code %c\n",c);
154 if (argc-optind != 0) {
155 Message(M_ERR,"secnet: You gave extra command line parameters, "
156 "which were ignored.\n");
160 static void setup(dict_t *config)
169 l=dict_lookup(config,"system");
171 if (!l || list_elem(l,0)->type!=t_dict) {
172 fatal("configuration does not include a \"system\" dictionary");
174 system=list_elem(l,0)->data.dict;
175 loc=list_elem(l,0)->loc;
177 /* Arrange systemwide log facility */
178 l=dict_lookup(system,"log");
180 fatal("configuration does not include a system/log facility");
182 system_log=init_log(l);
184 /* Who are we supposed to run as? */
185 userid=dict_read_string(system,"userid",False,"system",loc);
187 if(!(pw=getpwnam(userid)))
188 fatal("userid \"%s\" not found",userid);
194 pidfile=dict_read_string(system,"pidfile",False,"system",loc);
196 /* Check whether we need root privileges */
197 if (require_root_privileges && uid!=0) {
198 fatal("the configured feature \"%s\" requires "
199 "that secnet retain root privileges while running.",
200 require_root_privileges_explanation);
203 /* Go along site list, starting sites */
204 l=dict_lookup(config,sites_key);
206 Message(M_WARNING,"secnet: configuration key \"%s\" is missing; no "
207 "remote sites are defined\n",sites_key);
210 while ((site=list_elem(l, i++))) {
212 if (site->type!=t_closure) {
213 cfgfatal(site->loc,"system","non-closure in site list");
215 if (site->data.closure->type!=CL_SITE) {
216 cfgfatal(site->loc,"system","non-site closure in site list");
218 s=site->data.closure->interface;
219 s->control(s->st,True);
224 void register_for_poll(void *st, beforepoll_fn *before,
225 afterpoll_fn *after, int32_t max_nfds, cstring_t desc)
227 struct poll_interest *i;
229 i=safe_malloc(sizeof(*i),"register_for_poll");
233 i->max_nfds=max_nfds;
236 assert(total_nfds < INT_MAX - max_nfds);
237 total_nfds+=max_nfds;
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;
292 int rv, nfds, remain, idx;
296 fds=safe_malloc(sizeof(*fds)*total_nfds, "run");
298 Message(M_NOTICE,"%s [%d]: starting\n",version,secnet_pid);
301 if (gettimeofday(&tv_now_global, NULL)!=0) {
302 fatal_perror("main loop: gettimeofday");
304 now_global=((uint64_t)tv_now_global.tv_sec*(uint64_t)1000)+
305 ((uint64_t)tv_now_global.tv_usec/(uint64_t)1000);
307 for (i=reg; i; i=i->next) {
309 for (check=0; check<i->nfds; check++) {
310 if(fds[idx+check].revents & POLLNVAL) {
311 fatal("run: poll (%s#%d) set POLLNVAL", i->desc, check);
314 i->after(i->state, fds+idx, i->nfds);
320 for (i=reg; i; i=i->next) {
322 rv=i->before(i->state, fds+idx, &nfds, &timeout);
324 /* XXX we need to handle this properly: increase the
326 fatal("run: beforepoll_fn (%s) returns %d",i->desc,rv);
329 fatal("run: beforepoll_fn (%s) set timeout to %d",timeout);
338 rv=fakepoll(fds, idx, timeout);
340 rv=poll(fds, idx, timeout);
344 fatal_perror("run: poll");
352 static void droppriv(void)
358 add_hook(PHASE_SHUTDOWN,system_phase_hook,NULL);
360 /* Open the pidfile for writing now: we may be unable to do so
361 once we drop privileges. */
363 pf=fopen(pidfile,"w");
365 fatal_perror("cannot open pidfile \"%s\"",pidfile);
368 if (!background && pf) {
369 fprintf(pf,"%d\n",getpid());
373 /* Now drop privileges */
376 fatal_perror("can't set gid to %ld",(long)gid);
377 if(initgroups(userid, gid) < 0)
378 fatal_perror("initgroups");
379 if (setuid(uid)!=0) {
380 fatal_perror("can't set uid to \"%s\"",userid);
382 assert(getuid() == uid);
383 assert(geteuid() == uid);
384 assert(getgid() == gid);
385 assert(getegid() == gid);
391 /* Parent process - write pidfile, exit */
392 fprintf(pf,"%d\n",p);
397 /* Child process - all done, just carry on */
399 /* Close stdin and stdout; we don't need them any more.
400 stderr is redirected to the system/log facility */
401 if (pipe(errfds)!=0) {
402 fatal_perror("can't create pipe for stderr");
410 secnet_is_daemon=True;
412 log_from_fd(errfds[0],"stderr",system_log);
415 fatal_perror("cannot fork");
422 static signal_notify_fn finish,ignore_hup;
423 static void finish(void *st, int signum)
426 Message(M_NOTICE,"%s [%d]: received %s\n",version,secnet_pid,(string_t)st);
428 static void ignore_hup(void *st, int signum)
430 Message(M_INFO,"%s [%d]: received SIGHUP\n",version,secnet_pid);
434 int main(int argc, char **argv)
438 enter_phase(PHASE_GETOPTS);
439 parse_options(argc,argv);
441 enter_phase(PHASE_READCONFIG);
442 config=read_conffile(configfile);
444 enter_phase(PHASE_SETUP);
447 if (just_check_config) {
448 Message(M_INFO,"configuration file check complete\n");
452 enter_phase(PHASE_GETRESOURCES);
453 /* Appropriate phase hooks will have been run */
455 enter_phase(PHASE_DROPPRIV);
458 start_signal_handling();
459 request_signal_notification(SIGTERM,finish,safe_strdup("SIGTERM","run"));
460 if (!background) request_signal_notification(SIGINT,finish,
461 safe_strdup("SIGINT","run"));
462 request_signal_notification(SIGHUP,ignore_hup,NULL);
463 enter_phase(PHASE_RUN);
466 enter_phase(PHASE_SHUTDOWN);
467 Message(M_NOTICE,"%s [%d]: finished\n",version,secnet_pid);