7 #include <sys/socket.h>
15 /* XXX should be from autoconf */
16 static const char *configfile="/etc/secnet/secnet.conf";
17 static const char *sites_key="sites";
18 bool_t just_check_config=False;
19 static char *userid=NULL;
21 bool_t background=True;
22 static char *pidfile=NULL;
23 bool_t require_root_privileges=False;
24 cstring_t require_root_privileges_explanation=NULL;
26 static pid_t secnet_pid;
28 /* Structures dealing with poll() call */
29 struct poll_interest {
30 beforepoll_fn *before;
36 struct poll_interest *next;
38 static struct poll_interest *reg=NULL;
39 static uint32_t total_nfds=10;
41 static bool_t finished=False;
43 /* Parse the command line options */
44 static void parse_options(int argc, char **argv)
50 static struct option long_options[] = {
51 {"verbose", 0, 0, 'v'},
52 {"nowarnings", 0, 0, 'w'},
55 {"nodetach", 0, 0, 'n'},
56 {"silent", 0, 0, 'f'},
59 {"config", 1, 0, 'c'},
60 {"just-check-config", 0, 0, 'j'},
61 {"sites-key", 1, 0, 's'},
65 c=getopt_long(argc, argv, "vwdnjc:ft:s:",
66 long_options, &option_index);
73 printf("Usage: secnet [OPTION]...\n\n"
74 " -f, --silent, --quiet suppress error messages\n"
75 " -w, --nowarnings suppress warnings\n"
76 " -v, --verbose output extra diagnostics\n"
77 " -c, --config=filename specify a configuration file\n"
78 " -j, --just-check-config stop after reading "
79 "configuration file\n"
80 " -s, --sites-key=name configuration key that "
81 "specifies active sites\n"
82 " -n, --nodetach do not run in background\n"
83 " -d, --debug=item,... set debug options\n"
84 " --help display this help and exit\n"
85 " --version output version information "
93 printf("%s\n",version);
98 message_level|=M_INFO|M_NOTICE|M_WARNING|M_ERR|M_SECURITY|
103 message_level&=(~M_WARNING);
107 message_level|=M_DEBUG_CONFIG|M_DEBUG_PHASE|M_DEBUG;
111 message_level=M_FATAL;
120 configfile=safe_strdup(optarg,"config_filename");
122 fatal("secnet: no config filename specified");
126 just_check_config=True;
131 sites_key=safe_strdup(optarg,"sites-key");
133 fatal("secnet: no sites key specified");
140 Message(M_ERR,"secnet: Unknown getopt code %c\n",c);
144 if (argc-optind != 0) {
145 Message(M_ERR,"secnet: You gave extra command line parameters, "
146 "which were ignored.\n");
150 static void setup(dict_t *config)
159 l=dict_lookup(config,"system");
161 if (!l || list_elem(l,0)->type!=t_dict) {
162 fatal("configuration does not include a \"system\" dictionary");
164 system=list_elem(l,0)->data.dict;
165 loc=list_elem(l,0)->loc;
167 /* Arrange systemwide log facility */
168 l=dict_lookup(system,"log");
170 fatal("configuration does not include a system/log facility");
172 system_log=init_log(l);
174 /* Who are we supposed to run as? */
175 userid=dict_read_string(system,"userid",False,"system",loc);
179 if (pw && strcmp(pw->pw_name,userid)==0) {
186 fatal("userid \"%s\" not found",userid);
191 pidfile=dict_read_string(system,"pidfile",False,"system",loc);
193 /* Check whether we need root privileges */
194 if (require_root_privileges && uid!=0) {
195 fatal("the configured feature \"%s\" requires "
196 "that secnet retain root privileges while running.",
197 require_root_privileges_explanation);
200 /* Go along site list, starting sites */
201 l=dict_lookup(config,sites_key);
203 Message(M_WARNING,"secnet: configuration key \"%s\" is missing; no "
204 "remote sites are defined\n",sites_key);
207 while ((site=list_elem(l, i++))) {
209 if (site->type!=t_closure) {
210 cfgfatal(site->loc,"system","non-closure in site list");
212 if (site->data.closure->type!=CL_SITE) {
213 cfgfatal(site->loc,"system","non-site closure in site list");
215 s=site->data.closure->interface;
216 s->control(s->st,True);
221 void register_for_poll(void *st, beforepoll_fn *before,
222 afterpoll_fn *after, uint32_t max_nfds, cstring_t desc)
224 struct poll_interest *i;
226 i=safe_malloc(sizeof(*i),"register_for_poll");
230 i->max_nfds=max_nfds;
233 total_nfds+=max_nfds;
239 static void system_phase_hook(void *sst, uint32_t newphase)
241 if (newphase==PHASE_SHUTDOWN && pidfile) {
242 /* Try to unlink the pidfile; don't care if it fails */
247 static void run(void)
249 struct timeval tv_now;
251 struct poll_interest *i;
252 int rv, nfds, remain, idx;
256 fds=safe_malloc(sizeof(*fds)*total_nfds, "run");
258 Message(M_NOTICE,"%s [%d]: starting\n",version,secnet_pid);
261 if (gettimeofday(&tv_now, NULL)!=0) {
262 fatal_perror("main loop: gettimeofday");
264 now=((uint64_t)tv_now.tv_sec*(uint64_t)1000)+
265 ((uint64_t)tv_now.tv_usec/(uint64_t)1000);
267 for (i=reg; i; i=i->next) {
268 i->after(i->state, fds+idx, i->nfds, &tv_now, &now);
274 for (i=reg; i; i=i->next) {
276 rv=i->before(i->state, fds+idx, &nfds, &timeout, &tv_now, &now);
278 /* XXX we need to handle this properly: increase the
280 fatal("run: beforepoll_fn (%s) returns %d",i->desc,rv);
283 fatal("run: beforepoll_fn (%s) set timeout to %d",timeout);
291 rv=poll(fds, idx, timeout);
294 fatal_perror("run: poll");
302 static void droppriv(void)
308 add_hook(PHASE_SHUTDOWN,system_phase_hook,NULL);
310 /* Open the pidfile for writing now: we may be unable to do so
311 once we drop privileges. */
313 pf=fopen(pidfile,"w");
315 fatal_perror("cannot open pidfile \"%s\"",pidfile);
318 if (!background && pf) {
319 fprintf(pf,"%d\n",getpid());
323 /* Now drop privileges */
325 if (setuid(uid)!=0) {
326 fatal_perror("can't set uid to \"%s\"",userid);
333 /* Parent process - write pidfile, exit */
334 fprintf(pf,"%d\n",p);
339 /* Child process - all done, just carry on */
341 /* Close stdin and stdout; we don't need them any more.
342 stderr is redirected to the system/log facility */
343 if (pipe(errfds)!=0) {
344 fatal_perror("can't create pipe for stderr");
352 secnet_is_daemon=True;
354 log_from_fd(errfds[0],"stderr",system_log);
357 fatal_perror("cannot fork");
364 static signal_notify_fn finish,ignore_hup;
365 static void finish(void *st, int signum)
368 Message(M_NOTICE,"%s [%d]: received %s\n",version,secnet_pid,(string_t)st);
370 static void ignore_hup(void *st, int signum)
372 Message(M_INFO,"%s [%d]: received SIGHUP\n",version,secnet_pid);
376 int main(int argc, char **argv)
380 enter_phase(PHASE_GETOPTS);
381 parse_options(argc,argv);
383 enter_phase(PHASE_READCONFIG);
384 config=read_conffile(configfile);
386 enter_phase(PHASE_SETUP);
389 if (just_check_config) {
390 Message(M_INFO,"configuration file check complete\n");
394 enter_phase(PHASE_GETRESOURCES);
395 /* Appropriate phase hooks will have been run */
397 enter_phase(PHASE_DROPPRIV);
400 start_signal_handling();
401 request_signal_notification(SIGTERM,finish,safe_strdup("SIGTERM","run"));
402 if (!background) request_signal_notification(SIGINT,finish,
403 safe_strdup("SIGINT","run"));
404 request_signal_notification(SIGHUP,ignore_hup,NULL);
405 enter_phase(PHASE_RUN);
408 enter_phase(PHASE_SHUTDOWN);
409 Message(M_NOTICE,"%s [%d]: finished\n",version,secnet_pid);