2 * Revision 1.1 1996/03/13 22:27:41 sde1000
15 #include <sys/socket.h>
16 #include <arpa/inet.h>
22 /* XXX should be from autoconf */
23 static char *configfile
="/etc/secnet/secnet.conf";
24 bool_t just_check_config
=False
;
25 static char *userid
=NULL
;
27 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_NOTICE
|M_WARNING
|M_ERROR
|M_SECURITY
|
103 message_level
&=(~M_WARNING
);
107 message_level
|=M_DEBUG_CONFIG
|M_DEBUG_PHASE
|M_DEBUG
;
111 message_level
=M_FATAL
;
120 configfile
=safe_strdup(optarg
,"config_filename");
122 fatal("secnet: no config filename specified");
126 just_check_config
=True
;
133 Message(M_ERROR
,"secnet: Unknown getopt code %c\n",c
);
137 if (argc
-optind
!= 0) {
138 Message(M_ERROR
,"secnet: You gave extra command line parameters, "
139 "which were ignored.\n");
143 static void setup(dict_t
*config
)
152 l
=dict_lookup(config
,"system");
154 if (!l
|| list_elem(l
,0)->type
!=t_dict
) {
155 fatal("configuration does not include a \"system\" dictionary\n");
157 system
=list_elem(l
,0)->data
.dict
;
158 loc
=list_elem(l
,0)->loc
;
160 /* Arrange systemwide log facility */
161 l
=dict_lookup(system
,"log");
163 fatal("configuration does not include a system/log facility\n");
165 system_log
=init_log(l
);
167 /* Who are we supposed to run as? */
168 userid
=dict_read_string(system
,"userid",False
,"system",loc
);
172 if (pw
&& strcmp(pw
->pw_name
,userid
)==0) {
179 fatal("userid \"%s\" not found\n",userid
);
184 pidfile
=dict_read_string(system
,"pidfile",False
,"system",loc
);
186 /* Check whether we need root privileges */
187 if (require_root_privileges
&& uid
!=0) {
188 fatal("the following configured feature (\"%s\") requires "
189 "that secnet retain root privileges while running.\n",
190 require_root_privileges_explanation
);
193 /* Go along site list, starting sites */
194 l
=dict_lookup(config
,"sites");
196 Message(M_WARNING
,"secnet: configuration did not define any "
200 while ((site
=list_elem(l
, i
++))) {
202 if (site
->type
!=t_closure
) {
203 cfgfatal(site
->loc
,"system","non-closure in site list");
205 if (site
->data
.closure
->type
!=CL_SITE
) {
206 cfgfatal(site
->loc
,"system","non-site closure in site list");
208 s
=site
->data
.closure
->interface
;
209 s
->control(s
->st
,True
);
214 void register_for_poll(void *st
, beforepoll_fn
*before
,
215 afterpoll_fn
*after
, uint32_t max_nfds
, string_t desc
)
217 struct poll_interest
*i
;
219 i
=safe_malloc(sizeof(*i
),"register_for_poll");
223 i
->max_nfds
=max_nfds
;
226 total_nfds
+=max_nfds
;
232 static void system_phase_hook(void *sst
, uint32_t newphase
)
234 if (newphase
==PHASE_SHUTDOWN
&& pidfile
) {
235 /* Try to unlink the pidfile; don't care if it fails */
240 static void run(void)
242 struct timeval tv_now
;
244 struct poll_interest
*i
;
245 int rv
, nfds
, remain
, idx
;
249 fds
=alloca(sizeof(*fds
)*total_nfds
);
251 fatal("run: couldn't alloca\n");
254 Message(M_NOTICE
,"%s: starting\n",version
);
257 if (gettimeofday(&tv_now
, NULL
)!=0) {
258 fatal_perror("main loop: gettimeofday");
260 now
=(tv_now
.tv_sec
*1000)+(tv_now
.tv_usec
/1000);
262 for (i
=reg
; i
; i
=i
->next
) {
263 i
->after(i
->state
, fds
+idx
, i
->nfds
, &tv_now
, &now
);
269 for (i
=reg
; i
; i
=i
->next
) {
271 rv
=i
->before(i
->state
, fds
+idx
, &nfds
, &timeout
, &tv_now
, &now
);
273 /* XXX we need to handle this properly: increase the
275 fatal("run: beforepoll_fn (%s) returns %d\n",i
->desc
,rv
);
278 fatal("run: beforepoll_fn (%s) set timeout to %d\n",timeout
);
285 rv
=poll(fds
, idx
, timeout
);
288 fatal_perror("run: poll");
295 static void droppriv(void)
300 add_hook(PHASE_SHUTDOWN
,system_phase_hook
,NULL
);
302 /* Open the pidfile for writing now: we may be unable to do so
303 once we drop privileges. */
305 pf
=fopen(pidfile
,"w");
307 fatal_perror("cannot open pidfile \"%s\"",pidfile
);
310 if (!background
&& pf
) {
311 fprintf(pf
,"%d\n",getpid());
315 /* Now drop privileges */
317 if (setuid(uid
)!=0) {
318 fatal_perror("can't set uid to \"%s\"",userid
);
325 /* Parent process - write pidfile, exit */
326 fprintf(pf
,"%d\n",p
);
331 /* Child process - all done, just carry on */
333 secnet_is_daemon
=True
;
336 fatal_perror("cannot fork");
343 int main(int argc
, char **argv
)
347 enter_phase(PHASE_GETOPTS
);
348 parse_options(argc
,argv
);
350 enter_phase(PHASE_READCONFIG
);
351 config
=read_conffile(configfile
);
353 enter_phase(PHASE_SETUP
);
356 if (just_check_config
) {
357 Message(M_INFO
,"configuration file check complete\n");
361 enter_phase(PHASE_GETRESOURCES
);
362 /* Appropriate phase hooks will have been run */
364 enter_phase(PHASE_DROPPRIV
);
367 enter_phase(PHASE_RUN
);
370 enter_phase(PHASE_SHUTDOWN
);