Import release 0.1.15
[secnet] / secnet.c
CommitLineData
974d0468 1extern char version[];
2fe58dfd 2
8689b3a9 3#include "secnet.h"
2fe58dfd
SE
4#include <stdio.h>
5#include <string.h>
6#include <getopt.h>
2fe58dfd 7#include <errno.h>
8689b3a9 8#include <unistd.h>
2fe58dfd 9#include <sys/socket.h>
2fe58dfd 10#include <arpa/inet.h>
2fe58dfd 11#include <pwd.h>
2fe58dfd 12
2fe58dfd
SE
13#include "util.h"
14#include "conffile.h"
7138d0c5 15#include "process.h"
2fe58dfd 16
b2a56f7c 17/* XXX should be from autoconf */
fe5e9cc4
SE
18static const char *configfile="/etc/secnet/secnet.conf";
19static const char *sites_key="sites";
baa06aeb 20bool_t just_check_config=False;
2fe58dfd
SE
21static char *userid=NULL;
22static uid_t uid=0;
b2a56f7c 23bool_t background=True;
2fe58dfd 24static char *pidfile=NULL;
9d3a4132 25bool_t require_root_privileges=False;
fe5e9cc4 26cstring_t require_root_privileges_explanation=NULL;
2fe58dfd 27
7138d0c5
SE
28static pid_t secnet_pid;
29
30/* from log.c */
31extern uint32_t message_level;
32extern bool_t secnet_is_daemon;
33extern struct log_if *system_log;
34
35/* from process.c */
36extern void start_signal_handling(void);
37
2fe58dfd
SE
38/* Structures dealing with poll() call */
39struct poll_interest {
40 beforepoll_fn *before;
41 afterpoll_fn *after;
42 void *state;
43 uint32_t max_nfds;
44 uint32_t nfds;
fe5e9cc4 45 cstring_t desc;
2fe58dfd
SE
46 struct poll_interest *next;
47};
48static struct poll_interest *reg=NULL;
49static uint32_t total_nfds=10;
50
51static bool_t finished=False;
52
53/* Parse the command line options */
54static void parse_options(int argc, char **argv)
55{
56 int c;
57
58 while (True) {
59 int option_index = 0;
60 static struct option long_options[] = {
61 {"verbose", 0, 0, 'v'},
62 {"nowarnings", 0, 0, 'w'},
63 {"help", 0, 0, 2},
64 {"version", 0, 0, 1},
65 {"nodetach", 0, 0, 'n'},
66 {"silent", 0, 0, 'f'},
67 {"quiet", 0, 0, 'f'},
68 {"debug", 1, 0, 'd'},
69 {"config", 1, 0, 'c'},
baa06aeb 70 {"just-check-config", 0, 0, 'j'},
794f2398 71 {"sites-key", 1, 0, 's'},
2fe58dfd
SE
72 {0,0,0,0}
73 };
74
794f2398 75 c=getopt_long(argc, argv, "vwdnjc:ft:s:",
2fe58dfd
SE
76 long_options, &option_index);
77 if (c==-1)
78 break;
79
80 switch(c) {
81 case 2:
82 /* Help */
4f5e39ec
SE
83 printf("Usage: secnet [OPTION]...\n\n"
84 " -f, --silent, --quiet suppress error messages\n"
85 " -w, --nowarnings suppress warnings\n"
86 " -v, --verbose output extra diagnostics\n"
87 " -c, --config=filename specify a configuration file\n"
88 " -j, --just-check-config stop after reading "
89 "configuration file\n"
90 " -s, --sites-key=name configuration key that "
91 "specifies active sites\n"
92 " -n, --nodetach do not run in background\n"
93 " -d, --debug=item,... set debug options\n"
94 " --help display this help and exit\n"
95 " --version output version information "
96 "and exit\n"
2fe58dfd
SE
97 );
98 exit(0);
99 break;
100
101 case 1:
102 /* Version */
4f5e39ec 103 printf("%s\n",version);
2fe58dfd
SE
104 exit(0);
105 break;
106
107 case 'v':
469fd1d9 108 message_level|=M_INFO|M_NOTICE|M_WARNING|M_ERR|M_SECURITY|
b2a56f7c 109 M_FATAL;
2fe58dfd
SE
110 break;
111
b2a56f7c
SE
112 case 'w':
113 message_level&=(~M_WARNING);
2fe58dfd
SE
114 break;
115
116 case 'd':
b2a56f7c 117 message_level|=M_DEBUG_CONFIG|M_DEBUG_PHASE|M_DEBUG;
2fe58dfd
SE
118 break;
119
120 case 'f':
121 message_level=M_FATAL;
122 break;
123
b2a56f7c
SE
124 case 'n':
125 background=False;
126 break;
127
2fe58dfd
SE
128 case 'c':
129 if (optarg)
130 configfile=safe_strdup(optarg,"config_filename");
131 else
132 fatal("secnet: no config filename specified");
133 break;
134
baa06aeb
SE
135 case 'j':
136 just_check_config=True;
137 break;
138
794f2398
SE
139 case 's':
140 if (optarg)
141 sites_key=safe_strdup(optarg,"sites-key");
142 else
143 fatal("secnet: no sites key specified");
144 break;
145
2fe58dfd
SE
146 case '?':
147 break;
148
149 default:
469fd1d9 150 Message(M_ERR,"secnet: Unknown getopt code %c\n",c);
2fe58dfd
SE
151 }
152 }
153
154 if (argc-optind != 0) {
469fd1d9 155 Message(M_ERR,"secnet: You gave extra command line parameters, "
2fe58dfd
SE
156 "which were ignored.\n");
157 }
158}
159
160static void setup(dict_t *config)
161{
162 list_t *l;
163 item_t *site;
164 dict_t *system;
2fe58dfd
SE
165 struct passwd *pw;
166 struct cloc loc;
167 int i;
168
169 l=dict_lookup(config,"system");
170
171 if (!l || list_elem(l,0)->type!=t_dict) {
4f5e39ec 172 fatal("configuration does not include a \"system\" dictionary");
2fe58dfd
SE
173 }
174 system=list_elem(l,0)->data.dict;
175 loc=list_elem(l,0)->loc;
176
177 /* Arrange systemwide log facility */
178 l=dict_lookup(system,"log");
179 if (!l) {
4f5e39ec 180 fatal("configuration does not include a system/log facility");
2fe58dfd 181 }
b2a56f7c 182 system_log=init_log(l);
2fe58dfd
SE
183
184 /* Who are we supposed to run as? */
185 userid=dict_read_string(system,"userid",False,"system",loc);
186 if (userid) {
187 do {
188 pw=getpwent();
189 if (pw && strcmp(pw->pw_name,userid)==0) {
190 uid=pw->pw_uid;
191 break;
192 }
193 } while(pw);
194 endpwent();
195 if (uid==0) {
4f5e39ec 196 fatal("userid \"%s\" not found",userid);
2fe58dfd
SE
197 }
198 }
199
200 /* Pidfile name */
201 pidfile=dict_read_string(system,"pidfile",False,"system",loc);
202
9d3a4132
SE
203 /* Check whether we need root privileges */
204 if (require_root_privileges && uid!=0) {
4f5e39ec
SE
205 fatal("the configured feature \"%s\" requires "
206 "that secnet retain root privileges while running.",
9d3a4132
SE
207 require_root_privileges_explanation);
208 }
209
2fe58dfd 210 /* Go along site list, starting sites */
794f2398 211 l=dict_lookup(config,sites_key);
2fe58dfd 212 if (!l) {
794f2398
SE
213 Message(M_WARNING,"secnet: configuration key \"%s\" is missing; no "
214 "remote sites are defined\n",sites_key);
9d3a4132
SE
215 } else {
216 i=0;
217 while ((site=list_elem(l, i++))) {
218 struct site_if *s;
219 if (site->type!=t_closure) {
220 cfgfatal(site->loc,"system","non-closure in site list");
221 }
222 if (site->data.closure->type!=CL_SITE) {
223 cfgfatal(site->loc,"system","non-site closure in site list");
224 }
225 s=site->data.closure->interface;
226 s->control(s->st,True);
2fe58dfd 227 }
2fe58dfd
SE
228 }
229}
230
231void register_for_poll(void *st, beforepoll_fn *before,
fe5e9cc4 232 afterpoll_fn *after, uint32_t max_nfds, cstring_t desc)
2fe58dfd
SE
233{
234 struct poll_interest *i;
235
236 i=safe_malloc(sizeof(*i),"register_for_poll");
237 i->before=before;
238 i->after=after;
239 i->state=st;
240 i->max_nfds=max_nfds;
241 i->nfds=0;
242 i->desc=desc;
243 total_nfds+=max_nfds;
244 i->next=reg;
245 reg=i;
246 return;
247}
248
249static void system_phase_hook(void *sst, uint32_t newphase)
250{
251 if (newphase==PHASE_SHUTDOWN && pidfile) {
252 /* Try to unlink the pidfile; don't care if it fails */
253 unlink(pidfile);
254 }
255}
256
257static void run(void)
258{
259 struct timeval tv_now;
260 uint64_t now;
261 struct poll_interest *i;
262 int rv, nfds, remain, idx;
263 int timeout;
264 struct pollfd *fds;
265
266 fds=alloca(sizeof(*fds)*total_nfds);
267 if (!fds) {
4f5e39ec 268 fatal("run: couldn't alloca");
2fe58dfd
SE
269 }
270
7138d0c5 271 Message(M_NOTICE,"%s [%d]: starting\n",version,secnet_pid);
b2a56f7c 272
7138d0c5 273 do {
2fe58dfd
SE
274 if (gettimeofday(&tv_now, NULL)!=0) {
275 fatal_perror("main loop: gettimeofday");
276 }
d3fe100d
SE
277 now=((uint64_t)tv_now.tv_sec*(uint64_t)1000)+
278 ((uint64_t)tv_now.tv_usec/(uint64_t)1000);
2fe58dfd
SE
279 idx=0;
280 for (i=reg; i; i=i->next) {
281 i->after(i->state, fds+idx, i->nfds, &tv_now, &now);
282 idx+=i->nfds;
283 }
284 remain=total_nfds;
285 idx=0;
286 timeout=-1;
287 for (i=reg; i; i=i->next) {
288 nfds=remain;
289 rv=i->before(i->state, fds+idx, &nfds, &timeout, &tv_now, &now);
290 if (rv!=0) {
291 /* XXX we need to handle this properly: increase the
292 nfds available */
4f5e39ec 293 fatal("run: beforepoll_fn (%s) returns %d",i->desc,rv);
2fe58dfd
SE
294 }
295 if (timeout<-1) {
4f5e39ec 296 fatal("run: beforepoll_fn (%s) set timeout to %d",timeout);
2fe58dfd
SE
297 }
298 idx+=nfds;
299 remain-=nfds;
300 i->nfds=nfds;
301 }
302 do {
7138d0c5 303 if (finished) break;
2fe58dfd
SE
304 rv=poll(fds, idx, timeout);
305 if (rv<0) {
306 if (errno!=EINTR) {
307 fatal_perror("run: poll");
308 }
309 }
310 } while (rv<0);
7138d0c5 311 } while (!finished);
2fe58dfd
SE
312}
313
314static void droppriv(void)
315{
316 FILE *pf=NULL;
317 pid_t p;
fe5e9cc4 318 int errfds[2];
2fe58dfd
SE
319
320 add_hook(PHASE_SHUTDOWN,system_phase_hook,NULL);
321
b2a56f7c
SE
322 /* Open the pidfile for writing now: we may be unable to do so
323 once we drop privileges. */
324 if (pidfile) {
325 pf=fopen(pidfile,"w");
326 if (!pf) {
327 fatal_perror("cannot open pidfile \"%s\"",pidfile);
2fe58dfd 328 }
b2a56f7c
SE
329 }
330 if (!background && pf) {
331 fprintf(pf,"%d\n",getpid());
332 fclose(pf);
333 }
334
335 /* Now drop privileges */
336 if (uid!=0) {
337 if (setuid(uid)!=0) {
338 fatal_perror("can't set uid to \"%s\"",userid);
339 }
340 }
341 if (background) {
2fe58dfd
SE
342 p=fork();
343 if (p>0) {
344 if (pf) {
345 /* Parent process - write pidfile, exit */
346 fprintf(pf,"%d\n",p);
347 fclose(pf);
348 }
349 exit(0);
350 } else if (p==0) {
351 /* Child process - all done, just carry on */
352 if (pf) fclose(pf);
fe5e9cc4
SE
353 /* Close stdin and stdout; we don't need them any more.
354 stderr is redirected to the system/log facility */
355 if (pipe(errfds)!=0) {
356 fatal_perror("can't create pipe for stderr");
357 }
8dea8d37
SE
358 close(0);
359 close(1);
fe5e9cc4
SE
360 close(2);
361 dup2(errfds[1],0);
362 dup2(errfds[1],1);
363 dup2(errfds[1],2);
b2a56f7c 364 secnet_is_daemon=True;
ff05a229 365 setsid();
fe5e9cc4 366 log_from_fd(errfds[0],"stderr",system_log);
2fe58dfd
SE
367 } else {
368 /* Error */
369 fatal_perror("cannot fork");
370 exit(1);
371 }
2fe58dfd 372 }
7138d0c5
SE
373 secnet_pid=getpid();
374}
2fe58dfd 375
7138d0c5
SE
376static signal_notify_fn finish,ignore_hup;
377static void finish(void *st, int signum)
378{
379 finished=True;
380 Message(M_NOTICE,"%s [%d]: received %s\n",version,secnet_pid,(string_t)st);
381}
382static void ignore_hup(void *st, int signum)
383{
384 Message(M_INFO,"%s [%d]: received SIGHUP\n",version,secnet_pid);
385 return;
2fe58dfd
SE
386}
387
388int main(int argc, char **argv)
389{
390 dict_t *config;
391
392 enter_phase(PHASE_GETOPTS);
393 parse_options(argc,argv);
394
395 enter_phase(PHASE_READCONFIG);
396 config=read_conffile(configfile);
397
398 enter_phase(PHASE_SETUP);
399 setup(config);
baa06aeb
SE
400
401 if (just_check_config) {
402 Message(M_INFO,"configuration file check complete\n");
403 exit(0);
404 }
405
406 enter_phase(PHASE_GETRESOURCES);
407 /* Appropriate phase hooks will have been run */
2fe58dfd
SE
408
409 enter_phase(PHASE_DROPPRIV);
410 droppriv();
411
7138d0c5 412 start_signal_handling();
fe5e9cc4
SE
413 request_signal_notification(SIGTERM,finish,safe_strdup("SIGTERM","run"));
414 if (!background) request_signal_notification(SIGINT,finish,
415 safe_strdup("SIGINT","run"));
7138d0c5 416 request_signal_notification(SIGHUP,ignore_hup,NULL);
042a8da9 417 enter_phase(PHASE_RUN);
2fe58dfd
SE
418 run();
419
420 enter_phase(PHASE_SHUTDOWN);
7138d0c5 421 Message(M_NOTICE,"%s [%d]: finished\n",version,secnet_pid);
2fe58dfd
SE
422
423 return 0;
424}