2 * This file is part of secnet.
3 * See README for full list of copyright holders.
5 * secnet is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version d of the License, or
8 * (at your option) any later version.
10 * secnet is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * version 3 along with secnet; if not, see
17 * https://www.gnu.org/licenses/gpl.html.
28 #include <sys/socket.h>
29 #include <arpa/inet.h>
38 /* apple's poll() does not work on char devs */
42 /* XXX should be from autoconf */
43 static const char *configfile
="/etc/secnet/secnet.conf";
44 static const char *sites_key
="sites";
45 bool_t just_check_config
=False
;
46 static char *userid
=NULL
;
49 bool_t background
=True
;
50 static char *pidfile
=NULL
;
51 bool_t require_root_privileges
=False
;
52 cstring_t require_root_privileges_explanation
=NULL
;
54 static pid_t secnet_pid
;
56 /* Structures dealing with poll() call */
57 struct poll_interest
{
58 beforepoll_fn
*before
; /* 0 if deregistered and waiting to be deleted */
63 LIST_ENTRY(poll_interest
) entry
;
65 static LIST_HEAD(, poll_interest
) reg
= LIST_HEAD_INITIALIZER(®
);
67 static bool_t
interest_isregistered(const struct poll_interest
*i
)
72 static bool_t finished
=False
;
74 /* Parse the command line options */
75 static void parse_options(int argc
, char **argv
)
81 static struct option long_options
[] = {
82 {"verbose", 0, 0, 'v'},
83 {"nowarnings", 0, 0, 'w'},
86 {"nodetach", 0, 0, 'n'},
87 {"managed", 0, 0, 'm'},
88 {"silent", 0, 0, 'f'},
91 {"config", 1, 0, 'c'},
92 {"just-check-config", 0, 0, 'j'},
93 {"sites-key", 1, 0, 's'},
97 c
=getopt_long(argc
, argv
, "vwdnjc:ft:s:m",
98 long_options
, &option_index
);
105 printf("Usage: secnet [OPTION]...\n\n"
106 " -f, --silent, --quiet suppress error messages\n"
107 " -w, --nowarnings suppress warnings\n"
108 " -v, --verbose output extra diagnostics\n"
109 " -c, --config=filename specify a configuration file\n"
110 " -j, --just-check-config stop after reading "
111 "configuration file\n"
112 " -s, --sites-key=name configuration key that "
113 "specifies active sites\n"
114 " -n, --nodetach do not run in background\n"
115 " -m, --managed running under a supervisor\n"
116 " -d, --debug output debug messages\n"
117 " --help display this help and exit\n"
118 " --version output version information "
126 printf("%s\n",version
);
131 message_level
|=M_INFO
|M_NOTICE
|M_WARNING
|M_ERR
|M_SECURITY
|
136 message_level
&=(~M_WARNING
);
140 message_level
|=M_DEBUG_CONFIG
|M_DEBUG_PHASE
|M_DEBUG
;
144 message_level
=M_FATAL
;
152 secnet_is_daemon
=True
;
157 configfile
=safe_strdup(optarg
,"config_filename");
159 fatal("secnet: no config filename specified");
163 just_check_config
=True
;
168 sites_key
=safe_strdup(optarg
,"sites-key");
170 fatal("secnet: no sites key specified");
178 Message(M_ERR
,"secnet: Unknown getopt code %c\n",c
);
182 if (argc
-optind
!= 0) {
183 Message(M_ERR
,"secnet: You gave extra command line parameters, "
184 "which were ignored.\n");
188 static void setup(dict_t
*config
)
197 l
=dict_lookup(config
,"system");
199 if (!l
|| list_elem(l
,0)->type
!=t_dict
) {
200 fatal("configuration does not include a \"system\" dictionary");
202 system
=list_elem(l
,0)->data
.dict
;
203 loc
=list_elem(l
,0)->loc
;
205 /* Arrange systemwide log facility */
206 l
=dict_lookup(system
,"log");
208 fatal("configuration does not include a system/log facility");
210 system_log
=init_log(l
);
212 /* Who are we supposed to run as? */
213 userid
=dict_read_string(system
,"userid",False
,"system",loc
);
215 if (!(pw
=getpwnam(userid
)))
216 fatal("userid \"%s\" not found",userid
);
222 pidfile
=dict_read_string(system
,"pidfile",False
,"system",loc
);
224 /* Check whether we need root privileges */
225 if (require_root_privileges
&& uid
!=0) {
226 fatal("the configured feature \"%s\" requires "
227 "that secnet retain root privileges while running.",
228 require_root_privileges_explanation
);
231 /* Go along site list, starting sites */
232 l
=dict_lookup(config
,sites_key
);
234 Message(M_WARNING
,"secnet: configuration key \"%s\" is missing; no "
235 "remote sites are defined\n",sites_key
);
238 while ((site
=list_elem(l
, i
++))) {
240 if (site
->type
!=t_closure
) {
241 cfgfatal(site
->loc
,"system","non-closure in site list");
243 if (site
->data
.closure
->type
!=CL_SITE
) {
244 cfgfatal(site
->loc
,"system","non-site closure in site list");
246 s
=site
->data
.closure
->interface
;
247 s
->control(s
->st
,True
);
252 struct poll_interest
*register_for_poll(void *st
, beforepoll_fn
*before
,
253 afterpoll_fn
*after
, cstring_t desc
)
255 struct poll_interest
*i
;
263 LIST_INSERT_HEAD(®
, i
, entry
);
267 void deregister_for_poll(struct poll_interest
*i
)
269 /* We cannot simply throw this away because we're reentrantly
270 * inside the main loop, which needs to remember which range of
271 * fds corresponds to this now-obsolete interest */
275 static void system_phase_hook(void *sst
, uint32_t newphase
)
277 if (newphase
==PHASE_SHUTDOWN
&& pidfile
) {
278 /* Try to unlink the pidfile; don't care if it fails */
284 static int fakepoll(struct pollfd
*fds
, int nfds
, int timeout
) {
285 fd_set infds
[1], outfds
[1];
286 int maxfd
= -1, i
, rc
;
287 struct timeval tvtimeout
;
290 for(i
= 0; i
< nfds
; ++i
) {
291 if(fds
[i
].events
& POLLIN
)
292 FD_SET(fds
[i
].fd
, infds
);
293 if(fds
[i
].events
& POLLOUT
)
294 FD_SET(fds
[i
].fd
, outfds
);
295 if(fds
[i
].fd
> maxfd
)
299 tvtimeout
.tv_sec
= timeout
/ 1000;
300 tvtimeout
.tv_usec
= 1000 * (timeout
% 1000);
302 rc
= select(maxfd
+ 1, infds
, outfds
, NULL
,
303 timeout
== -1 ? NULL
: &tvtimeout
);
305 for(i
= 0; i
< nfds
; ++i
) {
307 if(FD_ISSET(fds
[i
].fd
, infds
))
309 if(FD_ISSET(fds
[i
].fd
, outfds
))
311 fds
[i
].revents
= revents
;
318 struct timeval tv_now_global
;
321 static void run(void)
323 struct poll_interest
*i
, *itmp
;
326 struct pollfd
*fds
=0;
327 int allocdfds
=0, shortfall
=0;
329 Message(M_NOTICE
,"%s [%d]: starting\n",version
,secnet_pid
);
332 if (gettimeofday(&tv_now_global
, NULL
)!=0) {
333 fatal_perror("main loop: gettimeofday");
335 now_global
=((uint64_t)tv_now_global
.tv_sec
*(uint64_t)1000)+
336 ((uint64_t)tv_now_global
.tv_usec
/(uint64_t)1000);
338 LIST_FOREACH(i
, ®
, entry
) {
340 if (interest_isregistered(i
)) {
341 for (check
=0; check
<i
->nfds
; check
++) {
342 if(fds
[idx
+check
].revents
& POLLNVAL
) {
343 fatal("run: poll (%s#%d) set POLLNVAL", i
->desc
, check
);
346 i
->after(i
->state
, fds
+idx
, i
->nfds
);
352 allocdfds
+= shortfall
;
353 REALLOC_ARY(fds
,allocdfds
);
358 LIST_FOREACH_SAFE(i
, ®
, entry
, itmp
) {
359 int remain
=allocdfds
-idx
;
361 if (interest_isregistered(i
)) {
362 rv
=i
->before(i
->state
, fds
+idx
, &nfds
, &timeout
);
365 fatal("run: beforepoll_fn (%s) returns %d",i
->desc
,rv
);
366 assert(nfds
< INT_MAX
/4 - shortfall
);
367 shortfall
+= nfds
-remain
;
375 fatal("run: beforepoll_fn (%s) set timeout to %d",
378 if (!interest_isregistered(i
)) {
379 /* check this here, rather than earlier, so that we
380 handle the case where i->before() calls deregister */
381 LIST_REMOVE(i
, entry
);
391 rv
=fakepoll(fds
, idx
, timeout
);
393 rv
=poll(fds
, idx
, timeout
);
397 fatal_perror("run: poll");
405 bool_t
will_droppriv(void)
407 assert(current_phase
>= PHASE_SETUP
);
411 /* Surrender privileges, if necessary */
412 static void droppriv(void)
416 fatal_perror("can't set gid to %ld",(long)gid
);
417 if (initgroups(userid
, gid
) < 0)
418 fatal_perror("initgroups");
419 if (setuid(uid
)!=0) {
420 fatal_perror("can't set uid to \"%s\"",userid
);
422 assert(getuid() == uid
);
423 assert(geteuid() == uid
);
424 assert(getgid() == gid
);
425 assert(getegid() == gid
);
429 /* Become a daemon, if necessary */
430 static void become_daemon(void)
436 add_hook(PHASE_SHUTDOWN
,system_phase_hook
,NULL
);
438 /* We only want to become a daemon if we are not one
440 if (background
&& !secnet_is_daemon
) {
443 /* Parent process - just exit */
446 /* Child process - all done, just carry on */
447 secnet_is_daemon
=True
;
449 fatal_perror("setsid");
452 fatal_perror("cannot fork");
456 if (secnet_is_daemon
) {
457 /* stderr etc are redirected to the system/log facility */
458 pipe_cloexec(errfds
);
459 if (dup2(errfds
[1],0) < 0
460 || dup2(errfds
[1],1) < 0
461 || dup2(errfds
[1],2) < 0)
462 fatal_perror("can't dup2 pipe");
463 if (close(errfds
[1]) < 0)
464 fatal_perror("can't close redundant pipe endpoint");
465 log_from_fd(errfds
[0],"stderr",system_log
);
469 /* Now we can write the pidfile */
471 pf
=fopen(pidfile
,"w");
473 fatal_perror("cannot open pidfile \"%s\"",pidfile
);
475 if (fprintf(pf
,"%ld\n",(long)secnet_pid
) < 0
477 fatal_perror("cannot write to pidfile \"%s\"",pidfile
);
481 static signal_notify_fn finish
,ignore_hup
;
482 static void finish(void *st
, int signum
)
485 Message(M_NOTICE
,"%s [%d]: received %s\n",version
,secnet_pid
,(string_t
)st
);
487 static void ignore_hup(void *st
, int signum
)
489 Message(M_INFO
,"%s [%d]: received SIGHUP\n",version
,secnet_pid
);
493 int main(int argc
, char **argv
)
499 enter_phase(PHASE_GETOPTS
);
500 parse_options(argc
,argv
);
502 enter_phase(PHASE_READCONFIG
);
503 config
=read_conffile(configfile
);
505 enter_phase(PHASE_SETUP
);
508 if (just_check_config
) {
509 Message(M_INFO
,"configuration file check complete\n");
513 enter_phase(PHASE_DAEMONIZE
);
516 enter_phase(PHASE_GETRESOURCES
);
517 /* Appropriate phase hooks will have been run */
519 enter_phase(PHASE_DROPPRIV
);
522 start_signal_handling();
523 request_signal_notification(SIGTERM
,finish
,safe_strdup("SIGTERM","run"));
524 if (!background
) request_signal_notification(SIGINT
,finish
,
525 safe_strdup("SIGINT","run"));
526 request_signal_notification(SIGHUP
,ignore_hup
,NULL
);
527 enter_phase(PHASE_RUN
);
530 enter_phase(PHASE_SHUTDOWN
);
531 Message(M_NOTICE
,"%s [%d]: finished\n",version
,secnet_pid
);