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
;
29 bool_t require_root_privileges
=False
;
30 string_t require_root_privileges_explanation
=NULL
;
32 /* Structures dealing with poll() call */
33 struct poll_interest
{
34 beforepoll_fn
*before
;
40 struct poll_interest
*next
;
42 static struct poll_interest
*reg
=NULL
;
43 static uint32_t total_nfds
=10;
45 static bool_t finished
=False
;
47 /* Parse the command line options */
48 static void parse_options(int argc
, char **argv
)
54 static struct option long_options
[] = {
55 {"verbose", 0, 0, 'v'},
56 {"nowarnings", 0, 0, 'w'},
59 {"nodetach", 0, 0, 'n'},
60 {"silent", 0, 0, 'f'},
63 {"config", 1, 0, 'c'},
64 {"just-check-config", 0, 0, 'j'},
68 c
=getopt_long(argc
, argv
, "vwdnjc:ft:",
69 long_options
, &option_index
);
77 "Usage: secnet [OPTION]...\n\n"
78 " -f, --silent, --quiet suppress error messages\n"
79 " -w, --nowarnings suppress warnings\n"
80 " -v, --verbose output extra diagnostics\n"
81 " -c, --config=filename specify a configuration file\n"
82 " -j, --just-check-config stop after reading configfile\n"
83 " -n, --nodetach do not run in background\n"
84 " -d, --debug=item,... set debug options\n"
85 " --help display this help and exit\n"
86 " --version output version information and exit\n"
93 fprintf(stderr
,"%s\n",version
);
98 message_level
|=M_INFO
|M_WARNING
|M_ERROR
|M_FATAL
;
106 message_level
|=M_DEBUG_CONFIG
|M_DEBUG_PHASE
;
110 message_level
=M_FATAL
;
115 configfile
=safe_strdup(optarg
,"config_filename");
117 fatal("secnet: no config filename specified");
121 just_check_config
=True
;
128 Message(M_WARNING
,"secnet: Unknown getopt code %c\n",c
);
132 if (argc
-optind
!= 0) {
133 Message(M_WARNING
,"secnet: You gave extra command line parameters, "
134 "which were ignored.\n");
138 static void setup(dict_t
*config
)
148 l
=dict_lookup(config
,"system");
150 if (!l
|| list_elem(l
,0)->type
!=t_dict
) {
151 fatal("configuration does not include a \"system\" dictionary\n");
153 system
=list_elem(l
,0)->data
.dict
;
154 loc
=list_elem(l
,0)->loc
;
156 /* Arrange systemwide log facility */
157 l
=dict_lookup(system
,"log");
159 fatal("configuration does not include a system/log facility\n");
162 log
->log(log
->st
,LOG_DEBUG
,"%s: logging started",version
);
164 /* Who are we supposed to run as? */
165 userid
=dict_read_string(system
,"userid",False
,"system",loc
);
169 if (pw
&& strcmp(pw
->pw_name
,userid
)==0) {
176 fatal("userid \"%s\" not found\n",userid
);
181 pidfile
=dict_read_string(system
,"pidfile",False
,"system",loc
);
183 /* Check whether we need root privileges */
184 if (require_root_privileges
&& uid
!=0) {
185 fatal("the following configured feature (\"%s\") requires "
186 "that secnet retain root privileges while running.\n",
187 require_root_privileges_explanation
);
190 /* Go along site list, starting sites */
191 l
=dict_lookup(config
,"sites");
193 Message(M_WARNING
,"secnet: configuration did not define any "
197 while ((site
=list_elem(l
, i
++))) {
199 if (site
->type
!=t_closure
) {
200 cfgfatal(site
->loc
,"system","non-closure in site list");
202 if (site
->data
.closure
->type
!=CL_SITE
) {
203 cfgfatal(site
->loc
,"system","non-site closure in site list");
205 s
=site
->data
.closure
->interface
;
206 s
->control(s
->st
,True
);
211 void register_for_poll(void *st
, beforepoll_fn
*before
,
212 afterpoll_fn
*after
, uint32_t max_nfds
, string_t desc
)
214 struct poll_interest
*i
;
216 i
=safe_malloc(sizeof(*i
),"register_for_poll");
220 i
->max_nfds
=max_nfds
;
223 total_nfds
+=max_nfds
;
229 static void system_phase_hook(void *sst
, uint32_t newphase
)
231 if (newphase
==PHASE_SHUTDOWN
&& pidfile
) {
232 /* Try to unlink the pidfile; don't care if it fails */
237 static void run(void)
239 struct timeval tv_now
;
241 struct poll_interest
*i
;
242 int rv
, nfds
, remain
, idx
;
246 fds
=alloca(sizeof(*fds
)*total_nfds
);
248 fatal("run: couldn't alloca\n");
252 if (gettimeofday(&tv_now
, NULL
)!=0) {
253 fatal_perror("main loop: gettimeofday");
255 now
=(tv_now
.tv_sec
*1000)+(tv_now
.tv_usec
/1000);
257 for (i
=reg
; i
; i
=i
->next
) {
258 i
->after(i
->state
, fds
+idx
, i
->nfds
, &tv_now
, &now
);
264 for (i
=reg
; i
; i
=i
->next
) {
266 rv
=i
->before(i
->state
, fds
+idx
, &nfds
, &timeout
, &tv_now
, &now
);
268 /* XXX we need to handle this properly: increase the
270 fatal("run: beforepoll_fn (%s) returns %d\n",i
->desc
,rv
);
273 fatal("run: beforepoll_fn (%s) set timeout to %d\n",timeout
);
280 rv
=poll(fds
, idx
, timeout
);
283 fatal_perror("run: poll");
290 static void droppriv(void)
295 add_hook(PHASE_SHUTDOWN
,system_phase_hook
,NULL
);
297 /* Background now, if we're supposed to: we may be unable to write the
298 pidfile if we don't. */
300 /* Open the pidfile before forking - that way the parent can tell
301 whether it succeeds */
303 pf
=fopen(pidfile
,"w");
305 fatal_perror("cannot open pidfile \"%s\"",pidfile
);
308 Message(M_WARNING
,"secnet: no pidfile configured, but "
309 "backgrounding anyway\n");
314 /* Parent process - write pidfile, exit */
315 fprintf(pf
,"%d\n",p
);
320 /* Child process - all done, just carry on */
324 fatal_perror("cannot fork");
329 pf
=fopen(pidfile
,"w");
331 fatal_perror("cannot open pidfile \"%s\"",pidfile
);
333 fprintf(pf
,"%d\n",getpid());
338 /* Drop privilege now, if configured to do so */
340 if (setuid(uid
)!=0) {
341 fatal_perror("can't set uid to \"%s\"",userid
);
346 int main(int argc
, char **argv
)
350 enter_phase(PHASE_GETOPTS
);
351 parse_options(argc
,argv
);
353 enter_phase(PHASE_READCONFIG
);
354 config
=read_conffile(configfile
);
356 enter_phase(PHASE_SETUP
);
359 if (just_check_config
) {
360 Message(M_INFO
,"configuration file check complete\n");
364 enter_phase(PHASE_GETRESOURCES
);
365 /* Appropriate phase hooks will have been run */
367 enter_phase(PHASE_DROPPRIV
);
370 enter_phase(PHASE_RUN
);
373 enter_phase(PHASE_SHUTDOWN
);