2 * Revision 1.1 1996/03/13 22:27:41 sde1000
15 #include <sys/socket.h>
16 #include <arpa/inet.h>
23 /* XXX should be from autoconf */
24 static char *configfile
="/etc/secnet/secnet.conf";
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 string_t require_root_privileges_explanation
=NULL
;
33 static pid_t secnet_pid
;
36 extern uint32_t message_level
;
37 extern bool_t secnet_is_daemon
;
38 extern struct log_if
*system_log
;
41 extern void start_signal_handling(void);
43 /* Structures dealing with poll() call */
44 struct poll_interest
{
45 beforepoll_fn
*before
;
51 struct poll_interest
*next
;
53 static struct poll_interest
*reg
=NULL
;
54 static uint32_t total_nfds
=10;
56 static bool_t finished
=False
;
58 /* Parse the command line options */
59 static void parse_options(int argc
, char **argv
)
65 static struct option long_options
[] = {
66 {"verbose", 0, 0, 'v'},
67 {"nowarnings", 0, 0, 'w'},
70 {"nodetach", 0, 0, 'n'},
71 {"silent", 0, 0, 'f'},
74 {"config", 1, 0, 'c'},
75 {"just-check-config", 0, 0, 'j'},
79 c
=getopt_long(argc
, argv
, "vwdnjc:ft:",
80 long_options
, &option_index
);
88 "Usage: secnet [OPTION]...\n\n"
89 " -f, --silent, --quiet suppress error messages\n"
90 " -w, --nowarnings suppress warnings\n"
91 " -v, --verbose output extra diagnostics\n"
92 " -c, --config=filename specify a configuration file\n"
93 " -j, --just-check-config stop after reading configfile\n"
94 " -n, --nodetach do not run in background\n"
95 " -d, --debug=item,... set debug options\n"
96 " --help display this help and exit\n"
97 " --version output version information and exit\n"
104 fprintf(stderr
,"%s\n",version
);
109 message_level
|=M_INFO
|M_NOTICE
|M_WARNING
|M_ERROR
|M_SECURITY
|
114 message_level
&=(~M_WARNING
);
118 message_level
|=M_DEBUG_CONFIG
|M_DEBUG_PHASE
|M_DEBUG
;
122 message_level
=M_FATAL
;
131 configfile
=safe_strdup(optarg
,"config_filename");
133 fatal("secnet: no config filename specified");
137 just_check_config
=True
;
144 Message(M_ERROR
,"secnet: Unknown getopt code %c\n",c
);
148 if (argc
-optind
!= 0) {
149 Message(M_ERROR
,"secnet: You gave extra command line parameters, "
150 "which were ignored.\n");
154 static void setup(dict_t
*config
)
163 l
=dict_lookup(config
,"system");
165 if (!l
|| list_elem(l
,0)->type
!=t_dict
) {
166 fatal("configuration does not include a \"system\" dictionary\n");
168 system
=list_elem(l
,0)->data
.dict
;
169 loc
=list_elem(l
,0)->loc
;
171 /* Arrange systemwide log facility */
172 l
=dict_lookup(system
,"log");
174 fatal("configuration does not include a system/log facility\n");
176 system_log
=init_log(l
);
178 /* Who are we supposed to run as? */
179 userid
=dict_read_string(system
,"userid",False
,"system",loc
);
183 if (pw
&& strcmp(pw
->pw_name
,userid
)==0) {
190 fatal("userid \"%s\" not found\n",userid
);
195 pidfile
=dict_read_string(system
,"pidfile",False
,"system",loc
);
197 /* Check whether we need root privileges */
198 if (require_root_privileges
&& uid
!=0) {
199 fatal("the following configured feature (\"%s\") requires "
200 "that secnet retain root privileges while running.\n",
201 require_root_privileges_explanation
);
204 /* Go along site list, starting sites */
205 l
=dict_lookup(config
,"sites");
207 Message(M_WARNING
,"secnet: configuration did not define any "
211 while ((site
=list_elem(l
, i
++))) {
213 if (site
->type
!=t_closure
) {
214 cfgfatal(site
->loc
,"system","non-closure in site list");
216 if (site
->data
.closure
->type
!=CL_SITE
) {
217 cfgfatal(site
->loc
,"system","non-site closure in site list");
219 s
=site
->data
.closure
->interface
;
220 s
->control(s
->st
,True
);
225 void register_for_poll(void *st
, beforepoll_fn
*before
,
226 afterpoll_fn
*after
, uint32_t max_nfds
, string_t desc
)
228 struct poll_interest
*i
;
230 i
=safe_malloc(sizeof(*i
),"register_for_poll");
234 i
->max_nfds
=max_nfds
;
237 total_nfds
+=max_nfds
;
243 static void system_phase_hook(void *sst
, uint32_t newphase
)
245 if (newphase
==PHASE_SHUTDOWN
&& pidfile
) {
246 /* Try to unlink the pidfile; don't care if it fails */
251 static void run(void)
253 struct timeval tv_now
;
255 struct poll_interest
*i
;
256 int rv
, nfds
, remain
, idx
;
260 fds
=alloca(sizeof(*fds
)*total_nfds
);
262 fatal("run: couldn't alloca\n");
265 Message(M_NOTICE
,"%s [%d]: starting\n",version
,secnet_pid
);
268 if (gettimeofday(&tv_now
, NULL
)!=0) {
269 fatal_perror("main loop: gettimeofday");
271 now
=(tv_now
.tv_sec
*1000)+(tv_now
.tv_usec
/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\n",i
->desc
,rv
);
289 fatal("run: beforepoll_fn (%s) set timeout to %d\n",timeout
);
297 rv
=poll(fds
, idx
, timeout
);
300 fatal_perror("run: poll");
307 static void droppriv(void)
312 add_hook(PHASE_SHUTDOWN
,system_phase_hook
,NULL
);
314 /* Open the pidfile for writing now: we may be unable to do so
315 once we drop privileges. */
317 pf
=fopen(pidfile
,"w");
319 fatal_perror("cannot open pidfile \"%s\"",pidfile
);
322 if (!background
&& pf
) {
323 fprintf(pf
,"%d\n",getpid());
327 /* Now drop privileges */
329 if (setuid(uid
)!=0) {
330 fatal_perror("can't set uid to \"%s\"",userid
);
337 /* Parent process - write pidfile, exit */
338 fprintf(pf
,"%d\n",p
);
343 /* Child process - all done, just carry on */
345 secnet_is_daemon
=True
;
348 fatal_perror("cannot fork");
355 static signal_notify_fn finish
,ignore_hup
;
356 static void finish(void *st
, int signum
)
359 Message(M_NOTICE
,"%s [%d]: received %s\n",version
,secnet_pid
,(string_t
)st
);
361 static void ignore_hup(void *st
, int signum
)
363 Message(M_INFO
,"%s [%d]: received SIGHUP\n",version
,secnet_pid
);
367 int main(int argc
, char **argv
)
371 enter_phase(PHASE_GETOPTS
);
372 parse_options(argc
,argv
);
374 enter_phase(PHASE_READCONFIG
);
375 config
=read_conffile(configfile
);
377 enter_phase(PHASE_SETUP
);
380 if (just_check_config
) {
381 Message(M_INFO
,"configuration file check complete\n");
385 enter_phase(PHASE_GETRESOURCES
);
386 /* Appropriate phase hooks will have been run */
388 enter_phase(PHASE_DROPPRIV
);
391 enter_phase(PHASE_RUN
);
392 start_signal_handling();
393 request_signal_notification(SIGTERM
,finish
,"SIGTERM");
394 if (!background
) request_signal_notification(SIGINT
,finish
,"SIGINT");
395 request_signal_notification(SIGHUP
,ignore_hup
,NULL
);
398 enter_phase(PHASE_SHUTDOWN
);
399 Message(M_NOTICE
,"%s [%d]: finished\n",version
,secnet_pid
);