Import release 0.1.5
[secnet] / secnet.c
1 /* $Log: secnet.c,v $
2 * Revision 1.1 1996/03/13 22:27:41 sde1000
3 * Initial revision
4 *
5 */
6
7 extern char version[];
8
9 #include "secnet.h"
10 #include <stdio.h>
11 #include <string.h>
12 #include <getopt.h>
13 #include <errno.h>
14 #include <unistd.h>
15 #include <sys/socket.h>
16 #include <arpa/inet.h>
17 #include <pwd.h>
18
19 #include "util.h"
20 #include "conffile.h"
21
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;
26 static uid_t uid=0;
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;
31
32 /* Structures dealing with poll() call */
33 struct poll_interest {
34 beforepoll_fn *before;
35 afterpoll_fn *after;
36 void *state;
37 uint32_t max_nfds;
38 uint32_t nfds;
39 string_t desc;
40 struct poll_interest *next;
41 };
42 static struct poll_interest *reg=NULL;
43 static uint32_t total_nfds=10;
44
45 static bool_t finished=False;
46
47 /* Parse the command line options */
48 static void parse_options(int argc, char **argv)
49 {
50 int c;
51
52 while (True) {
53 int option_index = 0;
54 static struct option long_options[] = {
55 {"verbose", 0, 0, 'v'},
56 {"nowarnings", 0, 0, 'w'},
57 {"help", 0, 0, 2},
58 {"version", 0, 0, 1},
59 {"nodetach", 0, 0, 'n'},
60 {"silent", 0, 0, 'f'},
61 {"quiet", 0, 0, 'f'},
62 {"debug", 1, 0, 'd'},
63 {"config", 1, 0, 'c'},
64 {"just-check-config", 0, 0, 'j'},
65 {0,0,0,0}
66 };
67
68 c=getopt_long(argc, argv, "vwdnjc:ft:",
69 long_options, &option_index);
70 if (c==-1)
71 break;
72
73 switch(c) {
74 case 2:
75 /* Help */
76 fprintf(stderr,
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"
87 );
88 exit(0);
89 break;
90
91 case 1:
92 /* Version */
93 fprintf(stderr,"%s\n",version);
94 exit(0);
95 break;
96
97 case 'v':
98 message_level|=M_INFO|M_NOTICE|M_WARNING|M_ERROR|M_SECURITY|
99 M_FATAL;
100 break;
101
102 case 'w':
103 message_level&=(~M_WARNING);
104 break;
105
106 case 'd':
107 message_level|=M_DEBUG_CONFIG|M_DEBUG_PHASE|M_DEBUG;
108 break;
109
110 case 'f':
111 message_level=M_FATAL;
112 break;
113
114 case 'n':
115 background=False;
116 break;
117
118 case 'c':
119 if (optarg)
120 configfile=safe_strdup(optarg,"config_filename");
121 else
122 fatal("secnet: no config filename specified");
123 break;
124
125 case 'j':
126 just_check_config=True;
127 break;
128
129 case '?':
130 break;
131
132 default:
133 Message(M_ERROR,"secnet: Unknown getopt code %c\n",c);
134 }
135 }
136
137 if (argc-optind != 0) {
138 Message(M_ERROR,"secnet: You gave extra command line parameters, "
139 "which were ignored.\n");
140 }
141 }
142
143 static void setup(dict_t *config)
144 {
145 list_t *l;
146 item_t *site;
147 dict_t *system;
148 struct passwd *pw;
149 struct cloc loc;
150 int i;
151
152 l=dict_lookup(config,"system");
153
154 if (!l || list_elem(l,0)->type!=t_dict) {
155 fatal("configuration does not include a \"system\" dictionary\n");
156 }
157 system=list_elem(l,0)->data.dict;
158 loc=list_elem(l,0)->loc;
159
160 /* Arrange systemwide log facility */
161 l=dict_lookup(system,"log");
162 if (!l) {
163 fatal("configuration does not include a system/log facility\n");
164 }
165 system_log=init_log(l);
166
167 /* Who are we supposed to run as? */
168 userid=dict_read_string(system,"userid",False,"system",loc);
169 if (userid) {
170 do {
171 pw=getpwent();
172 if (pw && strcmp(pw->pw_name,userid)==0) {
173 uid=pw->pw_uid;
174 break;
175 }
176 } while(pw);
177 endpwent();
178 if (uid==0) {
179 fatal("userid \"%s\" not found\n",userid);
180 }
181 }
182
183 /* Pidfile name */
184 pidfile=dict_read_string(system,"pidfile",False,"system",loc);
185
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);
191 }
192
193 /* Go along site list, starting sites */
194 l=dict_lookup(config,"sites");
195 if (!l) {
196 Message(M_WARNING,"secnet: configuration did not define any "
197 "remote sites\n");
198 } else {
199 i=0;
200 while ((site=list_elem(l, i++))) {
201 struct site_if *s;
202 if (site->type!=t_closure) {
203 cfgfatal(site->loc,"system","non-closure in site list");
204 }
205 if (site->data.closure->type!=CL_SITE) {
206 cfgfatal(site->loc,"system","non-site closure in site list");
207 }
208 s=site->data.closure->interface;
209 s->control(s->st,True);
210 }
211 }
212 }
213
214 void register_for_poll(void *st, beforepoll_fn *before,
215 afterpoll_fn *after, uint32_t max_nfds, string_t desc)
216 {
217 struct poll_interest *i;
218
219 i=safe_malloc(sizeof(*i),"register_for_poll");
220 i->before=before;
221 i->after=after;
222 i->state=st;
223 i->max_nfds=max_nfds;
224 i->nfds=0;
225 i->desc=desc;
226 total_nfds+=max_nfds;
227 i->next=reg;
228 reg=i;
229 return;
230 }
231
232 static void system_phase_hook(void *sst, uint32_t newphase)
233 {
234 if (newphase==PHASE_SHUTDOWN && pidfile) {
235 /* Try to unlink the pidfile; don't care if it fails */
236 unlink(pidfile);
237 }
238 }
239
240 static void run(void)
241 {
242 struct timeval tv_now;
243 uint64_t now;
244 struct poll_interest *i;
245 int rv, nfds, remain, idx;
246 int timeout;
247 struct pollfd *fds;
248
249 fds=alloca(sizeof(*fds)*total_nfds);
250 if (!fds) {
251 fatal("run: couldn't alloca\n");
252 }
253
254 Message(M_NOTICE,"%s: starting\n",version);
255
256 while (!finished) {
257 if (gettimeofday(&tv_now, NULL)!=0) {
258 fatal_perror("main loop: gettimeofday");
259 }
260 now=(tv_now.tv_sec*1000)+(tv_now.tv_usec/1000);
261 idx=0;
262 for (i=reg; i; i=i->next) {
263 i->after(i->state, fds+idx, i->nfds, &tv_now, &now);
264 idx+=i->nfds;
265 }
266 remain=total_nfds;
267 idx=0;
268 timeout=-1;
269 for (i=reg; i; i=i->next) {
270 nfds=remain;
271 rv=i->before(i->state, fds+idx, &nfds, &timeout, &tv_now, &now);
272 if (rv!=0) {
273 /* XXX we need to handle this properly: increase the
274 nfds available */
275 fatal("run: beforepoll_fn (%s) returns %d\n",i->desc,rv);
276 }
277 if (timeout<-1) {
278 fatal("run: beforepoll_fn (%s) set timeout to %d\n",timeout);
279 }
280 idx+=nfds;
281 remain-=nfds;
282 i->nfds=nfds;
283 }
284 do {
285 rv=poll(fds, idx, timeout);
286 if (rv<0) {
287 if (errno!=EINTR) {
288 fatal_perror("run: poll");
289 }
290 }
291 } while (rv<0);
292 }
293 }
294
295 static void droppriv(void)
296 {
297 FILE *pf=NULL;
298 pid_t p;
299
300 add_hook(PHASE_SHUTDOWN,system_phase_hook,NULL);
301
302 /* Open the pidfile for writing now: we may be unable to do so
303 once we drop privileges. */
304 if (pidfile) {
305 pf=fopen(pidfile,"w");
306 if (!pf) {
307 fatal_perror("cannot open pidfile \"%s\"",pidfile);
308 }
309 }
310 if (!background && pf) {
311 fprintf(pf,"%d\n",getpid());
312 fclose(pf);
313 }
314
315 /* Now drop privileges */
316 if (uid!=0) {
317 if (setuid(uid)!=0) {
318 fatal_perror("can't set uid to \"%s\"",userid);
319 }
320 }
321 if (background) {
322 p=fork();
323 if (p>0) {
324 if (pf) {
325 /* Parent process - write pidfile, exit */
326 fprintf(pf,"%d\n",p);
327 fclose(pf);
328 }
329 exit(0);
330 } else if (p==0) {
331 /* Child process - all done, just carry on */
332 if (pf) fclose(pf);
333 secnet_is_daemon=True;
334 } else {
335 /* Error */
336 fatal_perror("cannot fork");
337 exit(1);
338 }
339 }
340
341 }
342
343 int main(int argc, char **argv)
344 {
345 dict_t *config;
346
347 enter_phase(PHASE_GETOPTS);
348 parse_options(argc,argv);
349
350 enter_phase(PHASE_READCONFIG);
351 config=read_conffile(configfile);
352
353 enter_phase(PHASE_SETUP);
354 setup(config);
355
356 if (just_check_config) {
357 Message(M_INFO,"configuration file check complete\n");
358 exit(0);
359 }
360
361 enter_phase(PHASE_GETRESOURCES);
362 /* Appropriate phase hooks will have been run */
363
364 enter_phase(PHASE_DROPPRIV);
365 droppriv();
366
367 enter_phase(PHASE_RUN);
368 run();
369
370 enter_phase(PHASE_SHUTDOWN);
371
372 return 0;
373 }
374