2 * Revision 1.1 1996/03/13 22:27:41 sde1000
7 static char *version
="secnet version " VERSION
" $Date: 1996/03/13 22:27:41 $";
14 #include <sys/socket.h>
17 #include <netinet/in.h>
18 #include <arpa/inet.h>
21 #include <sys/types.h>
28 /* Command-line options (possibly config-file options too) */
29 static char *configfile
="/etc/secnet/secnet.conf";
30 static char *userid
=NULL
;
32 static bool_t background
=True
;
33 static char *pidfile
=NULL
;
35 /* Structures dealing with poll() call */
36 struct poll_interest
{
37 beforepoll_fn
*before
;
43 struct poll_interest
*next
;
45 static struct poll_interest
*reg
=NULL
;
46 static uint32_t total_nfds
=10;
48 static bool_t finished
=False
;
50 /* Parse the command line options */
51 static void parse_options(int argc
, char **argv
)
57 static struct option long_options
[] = {
58 {"verbose", 0, 0, 'v'},
59 {"nowarnings", 0, 0, 'w'},
62 {"nodetach", 0, 0, 'n'},
63 {"silent", 0, 0, 'f'},
66 {"config", 1, 0, 'c'},
70 c
=getopt_long(argc
, argv
, "vwdnc:ft:",
71 long_options
, &option_index
);
79 "Usage: secnet [OPTION]...\n\n"
80 " -f, --silent, --quiet suppress error messages\n"
81 " -w, --nowarnings suppress warnings\n"
82 " -v, --verbose output extra diagnostics\n"
83 " -c, --config=filename specify a configuration file\n"
84 " -n, --nodetach do not run in background\n"
85 " -d, --debug=item,... set debug options\n"
86 " --help display this help and exit\n"
87 " --version output version information and exit\n"
94 fprintf(stderr
,"%s\n",version
);
99 message_level
|=M_INFO
|M_WARNING
|M_ERROR
|M_FATAL
;
107 message_level
|=M_DEBUG_CONFIG
|M_DEBUG_PHASE
;
111 message_level
=M_FATAL
;
116 configfile
=safe_strdup(optarg
,"config_filename");
118 fatal("secnet: no config filename specified");
125 Message(M_WARNING
,"secnet: Unknown getopt code %c\n",c
);
129 if (argc
-optind
!= 0) {
130 Message(M_WARNING
,"secnet: You gave extra command line parameters, "
131 "which were ignored.\n");
135 static void setup(dict_t
*config
)
145 l
=dict_lookup(config
,"system");
147 if (!l
|| list_elem(l
,0)->type
!=t_dict
) {
148 fatal("configuration does not include a \"system\" dictionary\n");
150 system
=list_elem(l
,0)->data
.dict
;
151 loc
=list_elem(l
,0)->loc
;
153 /* Arrange systemwide log facility */
154 l
=dict_lookup(system
,"log");
156 fatal("configuration does not include a system/log facility\n");
159 log
->log(log
->st
,LOG_DEBUG
,"setup: logging started");
161 /* Who are we supposed to run as? */
162 userid
=dict_read_string(system
,"userid",False
,"system",loc
);
166 if (pw
&& strcmp(pw
->pw_name
,userid
)==0) {
173 fatal("userid \"%s\" not found\n",userid
);
178 pidfile
=dict_read_string(system
,"pidfile",False
,"system",loc
);
180 /* Go along site list, starting sites */
181 l
=dict_lookup(config
,"sites");
183 fatal("configuration did not define any remote sites\n");
186 while ((site
=list_elem(l
, i
++))) {
188 if (site
->type
!=t_closure
) {
189 cfgfatal(site
->loc
,"system","non-closure in site list");
191 if (site
->data
.closure
->type
!=CL_SITE
) {
192 cfgfatal(site
->loc
,"system","non-site closure in site list");
194 s
=site
->data
.closure
->interface
;
195 s
->control(s
->st
,True
);
199 void register_for_poll(void *st
, beforepoll_fn
*before
,
200 afterpoll_fn
*after
, uint32_t max_nfds
, string_t desc
)
202 struct poll_interest
*i
;
204 i
=safe_malloc(sizeof(*i
),"register_for_poll");
208 i
->max_nfds
=max_nfds
;
211 total_nfds
+=max_nfds
;
217 static void system_phase_hook(void *sst
, uint32_t newphase
)
219 if (newphase
==PHASE_SHUTDOWN
&& pidfile
) {
220 /* Try to unlink the pidfile; don't care if it fails */
225 static void run(void)
227 struct timeval tv_now
;
229 struct poll_interest
*i
;
230 int rv
, nfds
, remain
, idx
;
234 fds
=alloca(sizeof(*fds
)*total_nfds
);
236 fatal("run: couldn't alloca\n");
240 if (gettimeofday(&tv_now
, NULL
)!=0) {
241 fatal_perror("main loop: gettimeofday");
243 now
=(tv_now
.tv_sec
*1000)+(tv_now
.tv_usec
/1000);
245 for (i
=reg
; i
; i
=i
->next
) {
246 i
->after(i
->state
, fds
+idx
, i
->nfds
, &tv_now
, &now
);
252 for (i
=reg
; i
; i
=i
->next
) {
254 rv
=i
->before(i
->state
, fds
+idx
, &nfds
, &timeout
, &tv_now
, &now
);
256 /* XXX we need to handle this properly: increase the
258 fatal("run: beforepoll_fn (%s) returns %d\n",i
->desc
,rv
);
261 fatal("run: beforepoll_fn (%s) set timeout to %d\n",timeout
);
268 rv
=poll(fds
, idx
, timeout
);
271 fatal_perror("run: poll");
278 static void droppriv(void)
283 add_hook(PHASE_SHUTDOWN
,system_phase_hook
,NULL
);
285 /* Background now, if we're supposed to: we may be unable to write the
286 pidfile if we don't. */
288 printf("goto background\n");
289 /* Open the pidfile before forking - that way the parent can tell
290 whether it succeeds */
292 pf
=fopen(pidfile
,"w");
294 fatal_perror("cannot open pidfile \"%s\"",pidfile
);
297 Message(M_WARNING
,"secnet: no pidfile configured, but "
298 "backgrounding anyway\n");
303 /* Parent process - write pidfile, exit */
304 fprintf(pf
,"%d\n",p
);
309 /* Child process - all done, just carry on */
314 fatal_perror("cannot fork");
319 pf
=fopen(pidfile
,"w");
321 fatal_perror("cannot open pidfile \"%s\"",pidfile
);
323 fprintf(pf
,"%d\n",getpid());
328 /* Drop privilege now, if configured to do so */
330 if (setuid(uid
)!=0) {
331 fatal_perror("can't set uid to \"%s\"",userid
);
336 int main(int argc
, char **argv
)
340 enter_phase(PHASE_GETOPTS
);
341 parse_options(argc
,argv
);
343 enter_phase(PHASE_READCONFIG
);
344 config
=read_conffile(configfile
);
346 enter_phase(PHASE_SETUP
);
349 enter_phase(PHASE_DROPPRIV
);
352 enter_phase(PHASE_RUN
);
355 enter_phase(PHASE_SHUTDOWN
);