2 * Revision 1.1 1996/03/13 22:27:41 sde1000
15 #include <sys/socket.h>
16 #include <arpa/inet.h>
22 /* Command-line options (possibly config-file options too) */
23 static char *configfile="/etc/secnet/secnet.conf";
24 bool_t just_check_config=False;
25 static char *userid=NULL;
27 static bool_t background=True;
28 static char *pidfile=NULL;
30 /* Structures dealing with poll() call */
31 struct poll_interest {
32 beforepoll_fn *before;
38 struct poll_interest *next;
40 static struct poll_interest *reg=NULL;
41 static uint32_t total_nfds=10;
43 static bool_t finished=False;
45 /* Parse the command line options */
46 static void parse_options(int argc, char **argv)
52 static struct option long_options[] = {
53 {"verbose", 0, 0, 'v'},
54 {"nowarnings", 0, 0, 'w'},
57 {"nodetach", 0, 0, 'n'},
58 {"silent", 0, 0, 'f'},
61 {"config", 1, 0, 'c'},
62 {"just-check-config", 0, 0, 'j'},
66 c=getopt_long(argc, argv, "vwdnjc:ft:",
67 long_options, &option_index);
75 "Usage: secnet [OPTION]...\n\n"
76 " -f, --silent, --quiet suppress error messages\n"
77 " -w, --nowarnings suppress warnings\n"
78 " -v, --verbose output extra diagnostics\n"
79 " -c, --config=filename specify a configuration file\n"
80 " -j, --just-check-config stop after reading configfile\n"
81 " -n, --nodetach do not run in background\n"
82 " -d, --debug=item,... set debug options\n"
83 " --help display this help and exit\n"
84 " --version output version information and exit\n"
91 fprintf(stderr,"%s\n",version);
96 message_level|=M_INFO|M_WARNING|M_ERROR|M_FATAL;
104 message_level|=M_DEBUG_CONFIG|M_DEBUG_PHASE;
108 message_level=M_FATAL;
113 configfile=safe_strdup(optarg,"config_filename");
115 fatal("secnet: no config filename specified");
119 just_check_config=True;
126 Message(M_WARNING,"secnet: Unknown getopt code %c\n",c);
130 if (argc-optind != 0) {
131 Message(M_WARNING,"secnet: You gave extra command line parameters, "
132 "which were ignored.\n");
136 static void setup(dict_t *config)
146 l=dict_lookup(config,"system");
148 if (!l || list_elem(l,0)->type!=t_dict) {
149 fatal("configuration does not include a \"system\" dictionary\n");
151 system=list_elem(l,0)->data.dict;
152 loc=list_elem(l,0)->loc;
154 /* Arrange systemwide log facility */
155 l=dict_lookup(system,"log");
157 fatal("configuration does not include a system/log facility\n");
160 log->log(log->st,LOG_DEBUG,"%s: logging started",version);
162 /* Who are we supposed to run as? */
163 userid=dict_read_string(system,"userid",False,"system",loc);
167 if (pw && strcmp(pw->pw_name,userid)==0) {
174 fatal("userid \"%s\" not found\n",userid);
179 pidfile=dict_read_string(system,"pidfile",False,"system",loc);
181 /* Go along site list, starting sites */
182 l=dict_lookup(config,"sites");
184 fatal("configuration did not define any remote sites\n");
187 while ((site=list_elem(l, i++))) {
189 if (site->type!=t_closure) {
190 cfgfatal(site->loc,"system","non-closure in site list");
192 if (site->data.closure->type!=CL_SITE) {
193 cfgfatal(site->loc,"system","non-site closure in site list");
195 s=site->data.closure->interface;
196 s->control(s->st,True);
200 void register_for_poll(void *st, beforepoll_fn *before,
201 afterpoll_fn *after, uint32_t max_nfds, string_t desc)
203 struct poll_interest *i;
205 i=safe_malloc(sizeof(*i),"register_for_poll");
209 i->max_nfds=max_nfds;
212 total_nfds+=max_nfds;
218 static void system_phase_hook(void *sst, uint32_t newphase)
220 if (newphase==PHASE_SHUTDOWN && pidfile) {
221 /* Try to unlink the pidfile; don't care if it fails */
226 static void run(void)
228 struct timeval tv_now;
230 struct poll_interest *i;
231 int rv, nfds, remain, idx;
235 fds=alloca(sizeof(*fds)*total_nfds);
237 fatal("run: couldn't alloca\n");
241 if (gettimeofday(&tv_now, NULL)!=0) {
242 fatal_perror("main loop: gettimeofday");
244 now=(tv_now.tv_sec*1000)+(tv_now.tv_usec/1000);
246 for (i=reg; i; i=i->next) {
247 i->after(i->state, fds+idx, i->nfds, &tv_now, &now);
253 for (i=reg; i; i=i->next) {
255 rv=i->before(i->state, fds+idx, &nfds, &timeout, &tv_now, &now);
257 /* XXX we need to handle this properly: increase the
259 fatal("run: beforepoll_fn (%s) returns %d\n",i->desc,rv);
262 fatal("run: beforepoll_fn (%s) set timeout to %d\n",timeout);
269 rv=poll(fds, idx, timeout);
272 fatal_perror("run: poll");
279 static void droppriv(void)
284 add_hook(PHASE_SHUTDOWN,system_phase_hook,NULL);
286 /* Background now, if we're supposed to: we may be unable to write the
287 pidfile if we don't. */
289 printf("goto background\n");
290 /* Open the pidfile before forking - that way the parent can tell
291 whether it succeeds */
293 pf=fopen(pidfile,"w");
295 fatal_perror("cannot open pidfile \"%s\"",pidfile);
298 Message(M_WARNING,"secnet: no pidfile configured, but "
299 "backgrounding anyway\n");
304 /* Parent process - write pidfile, exit */
305 fprintf(pf,"%d\n",p);
310 /* Child process - all done, just carry on */
315 fatal_perror("cannot fork");
320 pf=fopen(pidfile,"w");
322 fatal_perror("cannot open pidfile \"%s\"",pidfile);
324 fprintf(pf,"%d\n",getpid());
329 /* Drop privilege now, if configured to do so */
331 if (setuid(uid)!=0) {
332 fatal_perror("can't set uid to \"%s\"",userid);
337 int main(int argc, char **argv)
341 enter_phase(PHASE_GETOPTS);
342 parse_options(argc,argv);
344 enter_phase(PHASE_READCONFIG);
345 config=read_conffile(configfile);
347 enter_phase(PHASE_SETUP);
350 if (just_check_config) {
351 Message(M_INFO,"configuration file check complete\n");
355 enter_phase(PHASE_GETRESOURCES);
356 /* Appropriate phase hooks will have been run */
358 enter_phase(PHASE_DROPPRIV);
361 enter_phase(PHASE_RUN);
364 enter_phase(PHASE_SHUTDOWN);