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