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");
147 Message(M_ERR,"secnet: Unknown getopt code %c\n",c);
151 if (argc-optind != 0) {
152 Message(M_ERR,"secnet: You gave extra command line parameters, "
153 "which were ignored.\n");
157 static void setup(dict_t *config)
166 l=dict_lookup(config,"system");
168 if (!l || list_elem(l,0)->type!=t_dict) {
169 fatal("configuration does not include a \"system\" dictionary");
171 system=list_elem(l,0)->data.dict;
172 loc=list_elem(l,0)->loc;
174 /* Arrange systemwide log facility */
175 l=dict_lookup(system,"log");
177 fatal("configuration does not include a system/log facility");
179 system_log=init_log(l);
181 /* Who are we supposed to run as? */
182 userid=dict_read_string(system,"userid",False,"system",loc);
186 if (pw && strcmp(pw->pw_name,userid)==0) {
193 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, int32_t max_nfds, cstring_t desc)
231 struct poll_interest *i;
233 i=safe_malloc(sizeof(*i),"register_for_poll");
237 i->max_nfds=max_nfds;
240 assert(total_nfds < INT_MAX - max_nfds);
241 total_nfds+=max_nfds;
247 static void system_phase_hook(void *sst, uint32_t newphase)
249 if (newphase==PHASE_SHUTDOWN && pidfile) {
250 /* Try to unlink the pidfile; don't care if it fails */
256 static int fakepoll(struct pollfd *fds, int nfds, int timeout) {
257 fd_set infds[1], outfds[1];
258 int maxfd = -1, i, rc;
259 struct timeval tvtimeout;
262 for(i = 0; i < nfds; ++i) {
263 if(fds[i].events & POLLIN)
264 FD_SET(fds[i].fd, infds);
265 if(fds[i].events & POLLOUT)
266 FD_SET(fds[i].fd, outfds);
267 if(fds[i].fd > maxfd)
271 tvtimeout.tv_sec = timeout / 1000;
272 tvtimeout.tv_usec = 1000 * (timeout % 1000);
274 rc = select(maxfd + 1, infds, outfds, NULL,
275 timeout == -1 ? NULL : &tvtimeout);
277 for(i = 0; i < nfds; ++i) {
279 if(FD_ISSET(fds[i].fd, infds))
281 if(FD_ISSET(fds[i].fd, outfds))
283 fds[i].revents = revents;
290 struct timeval tv_now_global;
293 static void run(void)
295 struct poll_interest *i;
296 int rv, nfds, remain, idx;
300 fds=safe_malloc(sizeof(*fds)*total_nfds, "run");
302 Message(M_NOTICE,"%s [%d]: starting\n",version,secnet_pid);
305 if (gettimeofday(&tv_now_global, NULL)!=0) {
306 fatal_perror("main loop: gettimeofday");
308 now_global=((uint64_t)tv_now_global.tv_sec*(uint64_t)1000)+
309 ((uint64_t)tv_now_global.tv_usec/(uint64_t)1000);
311 for (i=reg; i; i=i->next) {
313 for (check=0; check<i->nfds; check++) {
314 if(fds[idx+check].revents & POLLNVAL) {
315 fatal("run: poll (%s#%d) set POLLNVAL", i->desc, check);
318 i->after(i->state, fds+idx, i->nfds);
324 for (i=reg; i; i=i->next) {
326 rv=i->before(i->state, fds+idx, &nfds, &timeout);
328 /* XXX we need to handle this properly: increase the
330 fatal("run: beforepoll_fn (%s) returns %d",i->desc,rv);
333 fatal("run: beforepoll_fn (%s) set timeout to %d",timeout);
342 rv=fakepoll(fds, idx, timeout);
344 rv=poll(fds, idx, timeout);
348 fatal_perror("run: poll");
356 static void droppriv(void)
362 add_hook(PHASE_SHUTDOWN,system_phase_hook,NULL);
364 /* Open the pidfile for writing now: we may be unable to do so
365 once we drop privileges. */
367 pf=fopen(pidfile,"w");
369 fatal_perror("cannot open pidfile \"%s\"",pidfile);
372 if (!background && pf) {
373 fprintf(pf,"%d\n",getpid());
377 /* Now drop privileges */
379 if (setuid(uid)!=0) {
380 fatal_perror("can't set uid to \"%s\"",userid);
387 /* Parent process - write pidfile, exit */
388 fprintf(pf,"%d\n",p);
393 /* Child process - all done, just carry on */
395 /* Close stdin and stdout; we don't need them any more.
396 stderr is redirected to the system/log facility */
397 if (pipe(errfds)!=0) {
398 fatal_perror("can't create pipe for stderr");
406 secnet_is_daemon=True;
408 log_from_fd(errfds[0],"stderr",system_log);
411 fatal_perror("cannot fork");
418 static signal_notify_fn finish,ignore_hup;
419 static void finish(void *st, int signum)
422 Message(M_NOTICE,"%s [%d]: received %s\n",version,secnet_pid,(string_t)st);
424 static void ignore_hup(void *st, int signum)
426 Message(M_INFO,"%s [%d]: received SIGHUP\n",version,secnet_pid);
430 int main(int argc, char **argv)
434 enter_phase(PHASE_GETOPTS);
435 parse_options(argc,argv);
437 enter_phase(PHASE_READCONFIG);
438 config=read_conffile(configfile);
440 enter_phase(PHASE_SETUP);
443 if (just_check_config) {
444 Message(M_INFO,"configuration file check complete\n");
448 enter_phase(PHASE_GETRESOURCES);
449 /* Appropriate phase hooks will have been run */
451 enter_phase(PHASE_DROPPRIV);
454 start_signal_handling();
455 request_signal_notification(SIGTERM,finish,safe_strdup("SIGTERM","run"));
456 if (!background) request_signal_notification(SIGINT,finish,
457 safe_strdup("SIGINT","run"));
458 request_signal_notification(SIGHUP,ignore_hup,NULL);
459 enter_phase(PHASE_RUN);
462 enter_phase(PHASE_SHUTDOWN);
463 Message(M_NOTICE,"%s [%d]: finished\n",version,secnet_pid);