10 #include <sys/socket.h>
11 #include <arpa/inet.h>
19 /* XXX should be from autoconf */
20 static const char *configfile="/etc/secnet/secnet.conf";
21 static const char *sites_key="sites";
22 bool_t just_check_config=False;
23 static char *userid=NULL;
26 bool_t background=True;
27 static char *pidfile=NULL;
28 bool_t require_root_privileges=False;
29 cstring_t require_root_privileges_explanation=NULL;
31 static pid_t secnet_pid;
34 extern uint32_t message_level;
35 extern bool_t secnet_is_daemon;
36 extern struct log_if *system_log;
39 extern void start_signal_handling(void);
41 /* Structures dealing with poll() call */
42 struct poll_interest {
43 beforepoll_fn *before;
49 struct poll_interest *next;
51 static struct poll_interest *reg=NULL;
52 static uint32_t total_nfds=10;
54 static bool_t finished=False;
56 /* Parse the command line options */
57 static void parse_options(int argc, char **argv)
63 static struct option long_options[] = {
64 {"verbose", 0, 0, 'v'},
65 {"nowarnings", 0, 0, 'w'},
68 {"nodetach", 0, 0, 'n'},
69 {"silent", 0, 0, 'f'},
72 {"config", 1, 0, 'c'},
73 {"just-check-config", 0, 0, 'j'},
74 {"sites-key", 1, 0, 's'},
78 c=getopt_long(argc, argv, "vwdnjc:ft:s:",
79 long_options, &option_index);
86 printf("Usage: secnet [OPTION]...\n\n"
87 " -f, --silent, --quiet suppress error messages\n"
88 " -w, --nowarnings suppress warnings\n"
89 " -v, --verbose output extra diagnostics\n"
90 " -c, --config=filename specify a configuration file\n"
91 " -j, --just-check-config stop after reading "
92 "configuration file\n"
93 " -s, --sites-key=name configuration key that "
94 "specifies active sites\n"
95 " -n, --nodetach do not run in background\n"
96 " -d, --debug=item,... set debug options\n"
97 " --help display this help and exit\n"
98 " --version output version information "
106 printf("%s\n",version);
111 message_level|=M_INFO|M_NOTICE|M_WARNING|M_ERR|M_SECURITY|
116 message_level&=(~M_WARNING);
120 message_level|=M_DEBUG_CONFIG|M_DEBUG_PHASE|M_DEBUG;
124 message_level=M_FATAL;
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");
153 Message(M_ERR,"secnet: Unknown getopt code %c\n",c);
157 if (argc-optind != 0) {
158 Message(M_ERR,"secnet: You gave extra command line parameters, "
159 "which were ignored.\n");
163 static void setup(dict_t *config)
172 l=dict_lookup(config,"system");
174 if (!l || list_elem(l,0)->type!=t_dict) {
175 fatal("configuration does not include a \"system\" dictionary");
177 system=list_elem(l,0)->data.dict;
178 loc=list_elem(l,0)->loc;
180 /* Arrange systemwide log facility */
181 l=dict_lookup(system,"log");
183 fatal("configuration does not include a system/log facility");
185 system_log=init_log(l);
187 /* Who are we supposed to run as? */
188 userid=dict_read_string(system,"userid",False,"system",loc);
190 if(!(pw=getpwnam(userid)))
191 fatal("userid \"%s\" not found",userid);
197 pidfile=dict_read_string(system,"pidfile",False,"system",loc);
199 /* Check whether we need root privileges */
200 if (require_root_privileges && uid!=0) {
201 fatal("the configured feature \"%s\" requires "
202 "that secnet retain root privileges while running.",
203 require_root_privileges_explanation);
206 /* Go along site list, starting sites */
207 l=dict_lookup(config,sites_key);
209 Message(M_WARNING,"secnet: configuration key \"%s\" is missing; no "
210 "remote sites are defined\n",sites_key);
213 while ((site=list_elem(l, i++))) {
215 if (site->type!=t_closure) {
216 cfgfatal(site->loc,"system","non-closure in site list");
218 if (site->data.closure->type!=CL_SITE) {
219 cfgfatal(site->loc,"system","non-site closure in site list");
221 s=site->data.closure->interface;
222 s->control(s->st,True);
227 void register_for_poll(void *st, beforepoll_fn *before,
228 afterpoll_fn *after, uint32_t max_nfds, cstring_t desc)
230 struct poll_interest *i;
232 i=safe_malloc(sizeof(*i),"register_for_poll");
236 i->max_nfds=max_nfds;
239 total_nfds+=max_nfds;
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 */
253 static void run(void)
255 struct timeval tv_now;
257 struct poll_interest *i;
258 int rv, nfds, remain, idx;
262 fds=safe_malloc(sizeof(*fds)*total_nfds, "run");
264 Message(M_NOTICE,"%s [%d]: starting\n",version,secnet_pid);
267 if (gettimeofday(&tv_now, NULL)!=0) {
268 fatal_perror("main loop: gettimeofday");
270 now=((uint64_t)tv_now.tv_sec*(uint64_t)1000)+
271 ((uint64_t)tv_now.tv_usec/(uint64_t)1000);
273 for (i=reg; i; i=i->next) {
274 i->after(i->state, fds+idx, i->nfds, &tv_now, &now);
280 for (i=reg; i; i=i->next) {
282 rv=i->before(i->state, fds+idx, &nfds, &timeout, &tv_now, &now);
284 /* XXX we need to handle this properly: increase the
286 fatal("run: beforepoll_fn (%s) returns %d",i->desc,rv);
289 fatal("run: beforepoll_fn (%s) set timeout to %d",timeout);
297 rv=poll(fds, idx, timeout);
300 fatal_perror("run: poll");
308 static void droppriv(void)
314 add_hook(PHASE_SHUTDOWN,system_phase_hook,NULL);
316 /* Open the pidfile for writing now: we may be unable to do so
317 once we drop privileges. */
319 pf=fopen(pidfile,"w");
321 fatal_perror("cannot open pidfile \"%s\"",pidfile);
324 if (!background && pf) {
325 fprintf(pf,"%d\n",getpid());
329 /* Now drop privileges */
332 fatal_perror("can't set gid to %ld",(long)gid);
333 if(initgroups(userid, gid) < 0)
334 fatal_perror("initgroups");
335 if (setuid(uid)!=0) {
336 fatal_perror("can't set uid to \"%s\"",userid);
338 assert(getuid() == uid);
339 assert(geteuid() == uid);
340 assert(getgid() == gid);
341 assert(getegid() == gid);
347 /* Parent process - write pidfile, exit */
348 fprintf(pf,"%d\n",p);
353 /* Child process - all done, just carry on */
355 /* Close stdin and stdout; we don't need them any more.
356 stderr is redirected to the system/log facility */
357 if (pipe(errfds)!=0) {
358 fatal_perror("can't create pipe for stderr");
366 secnet_is_daemon=True;
368 log_from_fd(errfds[0],"stderr",system_log);
371 fatal_perror("cannot fork");
378 static signal_notify_fn finish,ignore_hup;
379 static void finish(void *st, int signum)
382 Message(M_NOTICE,"%s [%d]: received %s\n",version,secnet_pid,(string_t)st);
384 static void ignore_hup(void *st, int signum)
386 Message(M_INFO,"%s [%d]: received SIGHUP\n",version,secnet_pid);
390 int main(int argc, char **argv)
394 enter_phase(PHASE_GETOPTS);
395 parse_options(argc,argv);
397 enter_phase(PHASE_READCONFIG);
398 config=read_conffile(configfile);
400 enter_phase(PHASE_SETUP);
403 if (just_check_config) {
404 Message(M_INFO,"configuration file check complete\n");
408 enter_phase(PHASE_GETRESOURCES);
409 /* Appropriate phase hooks will have been run */
411 enter_phase(PHASE_DROPPRIV);
414 start_signal_handling();
415 request_signal_notification(SIGTERM,finish,safe_strdup("SIGTERM","run"));
416 if (!background) request_signal_notification(SIGINT,finish,
417 safe_strdup("SIGINT","run"));
418 request_signal_notification(SIGHUP,ignore_hup,NULL);
419 enter_phase(PHASE_RUN);
422 enter_phase(PHASE_SHUTDOWN);
423 Message(M_NOTICE,"%s [%d]: finished\n",version,secnet_pid);