2 * Revision 1.1 1996/03/13 22:27:41 sde1000
14 #include <sys/socket.h>
17 #include <netinet/in.h>
18 #include <arpa/inet.h>
21 #include <sys/types.h>
27 /* Command-line options (possibly config-file options too) */
28 static char *configfile
="/etc/secnet/secnet.conf";
29 static char *userid
=NULL
;
31 static bool_t background
=True
;
32 static char *pidfile
=NULL
;
34 /* Structures dealing with poll() call */
35 struct poll_interest
{
36 beforepoll_fn
*before
;
42 struct poll_interest
*next
;
44 static struct poll_interest
*reg
=NULL
;
45 static uint32_t total_nfds
=10;
47 static bool_t finished
=False
;
49 /* Parse the command line options */
50 static void parse_options(int argc
, char **argv
)
56 static struct option long_options
[] = {
57 {"verbose", 0, 0, 'v'},
58 {"nowarnings", 0, 0, 'w'},
61 {"nodetach", 0, 0, 'n'},
62 {"silent", 0, 0, 'f'},
65 {"config", 1, 0, 'c'},
69 c
=getopt_long(argc
, argv
, "vwdnc:ft:",
70 long_options
, &option_index
);
78 "Usage: secnet [OPTION]...\n\n"
79 " -f, --silent, --quiet suppress error messages\n"
80 " -w, --nowarnings suppress warnings\n"
81 " -v, --verbose output extra diagnostics\n"
82 " -c, --config=filename specify a configuration file\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");
124 Message(M_WARNING
,"secnet: Unknown getopt code %c\n",c
);
128 if (argc
-optind
!= 0) {
129 Message(M_WARNING
,"secnet: You gave extra command line parameters, "
130 "which were ignored.\n");
134 static void setup(dict_t
*config
)
144 l
=dict_lookup(config
,"system");
146 if (!l
|| list_elem(l
,0)->type
!=t_dict
) {
147 fatal("configuration does not include a \"system\" dictionary\n");
149 system
=list_elem(l
,0)->data
.dict
;
150 loc
=list_elem(l
,0)->loc
;
152 /* Arrange systemwide log facility */
153 l
=dict_lookup(system
,"log");
155 fatal("configuration does not include a system/log facility\n");
158 log
->log(log
->st
,LOG_DEBUG
,"%s: logging started",version
);
160 /* Who are we supposed to run as? */
161 userid
=dict_read_string(system
,"userid",False
,"system",loc
);
165 if (pw
&& strcmp(pw
->pw_name
,userid
)==0) {
172 fatal("userid \"%s\" not found\n",userid
);
177 pidfile
=dict_read_string(system
,"pidfile",False
,"system",loc
);
179 /* Go along site list, starting sites */
180 l
=dict_lookup(config
,"sites");
182 fatal("configuration did not define any remote sites\n");
185 while ((site
=list_elem(l
, i
++))) {
187 if (site
->type
!=t_closure
) {
188 cfgfatal(site
->loc
,"system","non-closure in site list");
190 if (site
->data
.closure
->type
!=CL_SITE
) {
191 cfgfatal(site
->loc
,"system","non-site closure in site list");
193 s
=site
->data
.closure
->interface
;
194 s
->control(s
->st
,True
);
198 void register_for_poll(void *st
, beforepoll_fn
*before
,
199 afterpoll_fn
*after
, uint32_t max_nfds
, string_t desc
)
201 struct poll_interest
*i
;
203 i
=safe_malloc(sizeof(*i
),"register_for_poll");
207 i
->max_nfds
=max_nfds
;
210 total_nfds
+=max_nfds
;
216 static void system_phase_hook(void *sst
, uint32_t newphase
)
218 if (newphase
==PHASE_SHUTDOWN
&& pidfile
) {
219 /* Try to unlink the pidfile; don't care if it fails */
224 static void run(void)
226 struct timeval tv_now
;
228 struct poll_interest
*i
;
229 int rv
, nfds
, remain
, idx
;
233 fds
=alloca(sizeof(*fds
)*total_nfds
);
235 fatal("run: couldn't alloca\n");
239 if (gettimeofday(&tv_now
, NULL
)!=0) {
240 fatal_perror("main loop: gettimeofday");
242 now
=(tv_now
.tv_sec
*1000)+(tv_now
.tv_usec
/1000);
244 for (i
=reg
; i
; i
=i
->next
) {
245 i
->after(i
->state
, fds
+idx
, i
->nfds
, &tv_now
, &now
);
251 for (i
=reg
; i
; i
=i
->next
) {
253 rv
=i
->before(i
->state
, fds
+idx
, &nfds
, &timeout
, &tv_now
, &now
);
255 /* XXX we need to handle this properly: increase the
257 fatal("run: beforepoll_fn (%s) returns %d\n",i
->desc
,rv
);
260 fatal("run: beforepoll_fn (%s) set timeout to %d\n",timeout
);
267 rv
=poll(fds
, idx
, timeout
);
270 fatal_perror("run: poll");
277 static void droppriv(void)
282 add_hook(PHASE_SHUTDOWN
,system_phase_hook
,NULL
);
284 /* Background now, if we're supposed to: we may be unable to write the
285 pidfile if we don't. */
287 printf("goto background\n");
288 /* Open the pidfile before forking - that way the parent can tell
289 whether it succeeds */
291 pf
=fopen(pidfile
,"w");
293 fatal_perror("cannot open pidfile \"%s\"",pidfile
);
296 Message(M_WARNING
,"secnet: no pidfile configured, but "
297 "backgrounding anyway\n");
302 /* Parent process - write pidfile, exit */
303 fprintf(pf
,"%d\n",p
);
308 /* Child process - all done, just carry on */
313 fatal_perror("cannot fork");
318 pf
=fopen(pidfile
,"w");
320 fatal_perror("cannot open pidfile \"%s\"",pidfile
);
322 fprintf(pf
,"%d\n",getpid());
327 /* Drop privilege now, if configured to do so */
329 if (setuid(uid
)!=0) {
330 fatal_perror("can't set uid to \"%s\"",userid
);
335 int main(int argc
, char **argv
)
339 enter_phase(PHASE_GETOPTS
);
340 parse_options(argc
,argv
);
342 enter_phase(PHASE_READCONFIG
);
343 config
=read_conffile(configfile
);
345 enter_phase(PHASE_SETUP
);
348 enter_phase(PHASE_DROPPRIV
);
351 enter_phase(PHASE_RUN
);
354 enter_phase(PHASE_SHUTDOWN
);