9 #include <sys/socket.h>
10 #include <arpa/inet.h>
17 /* XXX should be from autoconf */
18 static const char *configfile
="/etc/secnet/secnet.conf";
19 static const char *sites_key
="sites";
20 bool_t just_check_config
=False
;
21 static char *userid
=NULL
;
23 bool_t background
=True
;
24 static char *pidfile
=NULL
;
25 bool_t require_root_privileges
=False
;
26 cstring_t require_root_privileges_explanation
=NULL
;
28 static pid_t secnet_pid
;
31 extern uint32_t message_level
;
32 extern bool_t secnet_is_daemon
;
33 extern struct log_if
*system_log
;
36 extern void start_signal_handling(void);
38 /* Structures dealing with poll() call */
39 struct poll_interest
{
40 beforepoll_fn
*before
;
46 struct poll_interest
*next
;
48 static struct poll_interest
*reg
=NULL
;
49 static uint32_t total_nfds
=10;
51 static bool_t finished
=False
;
53 /* Parse the command line options */
54 static void parse_options(int argc
, char **argv
)
60 static struct option long_options
[] = {
61 {"verbose", 0, 0, 'v'},
62 {"nowarnings", 0, 0, 'w'},
65 {"nodetach", 0, 0, 'n'},
66 {"silent", 0, 0, 'f'},
69 {"config", 1, 0, 'c'},
70 {"just-check-config", 0, 0, 'j'},
71 {"sites-key", 1, 0, 's'},
75 c
=getopt_long(argc
, argv
, "vwdnjc:ft:s:",
76 long_options
, &option_index
);
83 printf("Usage: secnet [OPTION]...\n\n"
84 " -f, --silent, --quiet suppress error messages\n"
85 " -w, --nowarnings suppress warnings\n"
86 " -v, --verbose output extra diagnostics\n"
87 " -c, --config=filename specify a configuration file\n"
88 " -j, --just-check-config stop after reading "
89 "configuration file\n"
90 " -s, --sites-key=name configuration key that "
91 "specifies active sites\n"
92 " -n, --nodetach do not run in background\n"
93 " -d, --debug=item,... set debug options\n"
94 " --help display this help and exit\n"
95 " --version output version information "
103 printf("%s\n",version
);
108 message_level
|=M_INFO
|M_NOTICE
|M_WARNING
|M_ERR
|M_SECURITY
|
113 message_level
&=(~M_WARNING
);
117 message_level
|=M_DEBUG_CONFIG
|M_DEBUG_PHASE
|M_DEBUG
;
121 message_level
=M_FATAL
;
130 configfile
=safe_strdup(optarg
,"config_filename");
132 fatal("secnet: no config filename specified");
136 just_check_config
=True
;
141 sites_key
=safe_strdup(optarg
,"sites-key");
143 fatal("secnet: no sites key specified");
150 Message(M_ERR
,"secnet: Unknown getopt code %c\n",c
);
154 if (argc
-optind
!= 0) {
155 Message(M_ERR
,"secnet: You gave extra command line parameters, "
156 "which were ignored.\n");
160 static void setup(dict_t
*config
)
169 l
=dict_lookup(config
,"system");
171 if (!l
|| list_elem(l
,0)->type
!=t_dict
) {
172 fatal("configuration does not include a \"system\" dictionary");
174 system
=list_elem(l
,0)->data
.dict
;
175 loc
=list_elem(l
,0)->loc
;
177 /* Arrange systemwide log facility */
178 l
=dict_lookup(system
,"log");
180 fatal("configuration does not include a system/log facility");
182 system_log
=init_log(l
);
184 /* Who are we supposed to run as? */
185 userid
=dict_read_string(system
,"userid",False
,"system",loc
);
189 if (pw
&& strcmp(pw
->pw_name
,userid
)==0) {
196 fatal("userid \"%s\" not found",userid
);
201 pidfile
=dict_read_string(system
,"pidfile",False
,"system",loc
);
203 /* Check whether we need root privileges */
204 if (require_root_privileges
&& uid
!=0) {
205 fatal("the configured feature \"%s\" requires "
206 "that secnet retain root privileges while running.",
207 require_root_privileges_explanation
);
210 /* Go along site list, starting sites */
211 l
=dict_lookup(config
,sites_key
);
213 Message(M_WARNING
,"secnet: configuration key \"%s\" is missing; no "
214 "remote sites are defined\n",sites_key
);
217 while ((site
=list_elem(l
, i
++))) {
219 if (site
->type
!=t_closure
) {
220 cfgfatal(site
->loc
,"system","non-closure in site list");
222 if (site
->data
.closure
->type
!=CL_SITE
) {
223 cfgfatal(site
->loc
,"system","non-site closure in site list");
225 s
=site
->data
.closure
->interface
;
226 s
->control(s
->st
,True
);
231 void register_for_poll(void *st
, beforepoll_fn
*before
,
232 afterpoll_fn
*after
, uint32_t max_nfds
, cstring_t desc
)
234 struct poll_interest
*i
;
236 i
=safe_malloc(sizeof(*i
),"register_for_poll");
240 i
->max_nfds
=max_nfds
;
243 total_nfds
+=max_nfds
;
249 static void system_phase_hook(void *sst
, uint32_t newphase
)
251 if (newphase
==PHASE_SHUTDOWN
&& pidfile
) {
252 /* Try to unlink the pidfile; don't care if it fails */
257 static void run(void)
259 struct timeval tv_now
;
261 struct poll_interest
*i
;
262 int rv
, nfds
, remain
, idx
;
266 fds
=safe_malloc(sizeof(*fds
)*total_nfds
, "run");
268 Message(M_NOTICE
,"%s [%d]: starting\n",version
,secnet_pid
);
271 if (gettimeofday(&tv_now
, NULL
)!=0) {
272 fatal_perror("main loop: gettimeofday");
274 now
=((uint64_t)tv_now
.tv_sec
*(uint64_t)1000)+
275 ((uint64_t)tv_now
.tv_usec
/(uint64_t)1000);
277 for (i
=reg
; i
; i
=i
->next
) {
278 i
->after(i
->state
, fds
+idx
, i
->nfds
, &tv_now
, &now
);
284 for (i
=reg
; i
; i
=i
->next
) {
286 rv
=i
->before(i
->state
, fds
+idx
, &nfds
, &timeout
, &tv_now
, &now
);
288 /* XXX we need to handle this properly: increase the
290 fatal("run: beforepoll_fn (%s) returns %d",i
->desc
,rv
);
293 fatal("run: beforepoll_fn (%s) set timeout to %d",timeout
);
301 rv
=poll(fds
, idx
, timeout
);
304 fatal_perror("run: poll");
312 static void droppriv(void)
318 add_hook(PHASE_SHUTDOWN
,system_phase_hook
,NULL
);
320 /* Open the pidfile for writing now: we may be unable to do so
321 once we drop privileges. */
323 pf
=fopen(pidfile
,"w");
325 fatal_perror("cannot open pidfile \"%s\"",pidfile
);
328 if (!background
&& pf
) {
329 fprintf(pf
,"%d\n",getpid());
333 /* Now drop privileges */
335 if (setuid(uid
)!=0) {
336 fatal_perror("can't set uid to \"%s\"",userid
);
343 /* Parent process - write pidfile, exit */
344 fprintf(pf
,"%d\n",p
);
349 /* Child process - all done, just carry on */
351 /* Close stdin and stdout; we don't need them any more.
352 stderr is redirected to the system/log facility */
353 if (pipe(errfds
)!=0) {
354 fatal_perror("can't create pipe for stderr");
362 secnet_is_daemon
=True
;
364 log_from_fd(errfds
[0],"stderr",system_log
);
367 fatal_perror("cannot fork");
374 static signal_notify_fn finish
,ignore_hup
;
375 static void finish(void *st
, int signum
)
378 Message(M_NOTICE
,"%s [%d]: received %s\n",version
,secnet_pid
,(string_t
)st
);
380 static void ignore_hup(void *st
, int signum
)
382 Message(M_INFO
,"%s [%d]: received SIGHUP\n",version
,secnet_pid
);
386 int main(int argc
, char **argv
)
390 enter_phase(PHASE_GETOPTS
);
391 parse_options(argc
,argv
);
393 enter_phase(PHASE_READCONFIG
);
394 config
=read_conffile(configfile
);
396 enter_phase(PHASE_SETUP
);
399 if (just_check_config
) {
400 Message(M_INFO
,"configuration file check complete\n");
404 enter_phase(PHASE_GETRESOURCES
);
405 /* Appropriate phase hooks will have been run */
407 enter_phase(PHASE_DROPPRIV
);
410 start_signal_handling();
411 request_signal_notification(SIGTERM
,finish
,safe_strdup("SIGTERM","run"));
412 if (!background
) request_signal_notification(SIGINT
,finish
,
413 safe_strdup("SIGINT","run"));
414 request_signal_notification(SIGHUP
,ignore_hup
,NULL
);
415 enter_phase(PHASE_RUN
);
418 enter_phase(PHASE_SHUTDOWN
);
419 Message(M_NOTICE
,"%s [%d]: finished\n",version
,secnet_pid
);