Commit | Line | Data |
---|---|---|
8689b3a9 | 1 | #include "secnet.h" |
2fe58dfd | 2 | #include <stdio.h> |
59230b9b IJ |
3 | #include <assert.h> |
4 | #include <limits.h> | |
2fe58dfd SE |
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> |
08daaaff | 12 | #include <grp.h> |
2fe58dfd | 13 | |
2fe58dfd SE |
14 | #include "util.h" |
15 | #include "conffile.h" | |
7138d0c5 | 16 | #include "process.h" |
2fe58dfd | 17 | |
29672515 RK |
18 | #if __APPLE__ |
19 | /* apple's poll() does not work on char devs */ | |
20 | # define USE_SELECT 1 | |
21 | #endif | |
22 | ||
b2a56f7c | 23 | /* XXX should be from autoconf */ |
fe5e9cc4 SE |
24 | static const char *configfile="/etc/secnet/secnet.conf"; |
25 | static const char *sites_key="sites"; | |
baa06aeb | 26 | bool_t just_check_config=False; |
2fe58dfd SE |
27 | static char *userid=NULL; |
28 | static uid_t uid=0; | |
08daaaff | 29 | static gid_t gid; |
b2a56f7c | 30 | bool_t background=True; |
2fe58dfd | 31 | static char *pidfile=NULL; |
9d3a4132 | 32 | bool_t require_root_privileges=False; |
fe5e9cc4 | 33 | cstring_t require_root_privileges_explanation=NULL; |
2fe58dfd | 34 | |
7138d0c5 SE |
35 | static pid_t secnet_pid; |
36 | ||
2fe58dfd SE |
37 | /* Structures dealing with poll() call */ |
38 | struct poll_interest { | |
39 | beforepoll_fn *before; | |
40 | afterpoll_fn *after; | |
41 | void *state; | |
1caa23ff IJ |
42 | int32_t max_nfds; |
43 | int32_t nfds; | |
fe5e9cc4 | 44 | cstring_t desc; |
2fe58dfd SE |
45 | struct poll_interest *next; |
46 | }; | |
47 | static struct poll_interest *reg=NULL; | |
1caa23ff | 48 | static int32_t total_nfds=10; |
2fe58dfd SE |
49 | |
50 | static bool_t finished=False; | |
51 | ||
52 | /* Parse the command line options */ | |
53 | static void parse_options(int argc, char **argv) | |
54 | { | |
55 | int c; | |
56 | ||
57 | while (True) { | |
58 | int option_index = 0; | |
59 | static struct option long_options[] = { | |
60 | {"verbose", 0, 0, 'v'}, | |
61 | {"nowarnings", 0, 0, 'w'}, | |
62 | {"help", 0, 0, 2}, | |
63 | {"version", 0, 0, 1}, | |
64 | {"nodetach", 0, 0, 'n'}, | |
7b1a9fb7 | 65 | {"managed", 0, 0, 'm'}, |
2fe58dfd SE |
66 | {"silent", 0, 0, 'f'}, |
67 | {"quiet", 0, 0, 'f'}, | |
dd568518 | 68 | {"debug", 0, 0, 'd'}, |
2fe58dfd | 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 | ||
7b1a9fb7 | 75 | c=getopt_long(argc, argv, "vwdnjc:ft:s:m", |
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" | |
7b1a9fb7 | 93 | " -m, --managed running under a supervisor\n" |
dd568518 | 94 | " -d, --debug output debug messages\n" |
4f5e39ec SE |
95 | " --help display this help and exit\n" |
96 | " --version output version information " | |
97 | "and exit\n" | |
2fe58dfd SE |
98 | ); |
99 | exit(0); | |
100 | break; | |
101 | ||
102 | case 1: | |
103 | /* Version */ | |
4f5e39ec | 104 | printf("%s\n",version); |
2fe58dfd SE |
105 | exit(0); |
106 | break; | |
107 | ||
108 | case 'v': | |
469fd1d9 | 109 | message_level|=M_INFO|M_NOTICE|M_WARNING|M_ERR|M_SECURITY| |
b2a56f7c | 110 | M_FATAL; |
2fe58dfd SE |
111 | break; |
112 | ||
b2a56f7c SE |
113 | case 'w': |
114 | message_level&=(~M_WARNING); | |
2fe58dfd SE |
115 | break; |
116 | ||
117 | case 'd': | |
b2a56f7c | 118 | message_level|=M_DEBUG_CONFIG|M_DEBUG_PHASE|M_DEBUG; |
2fe58dfd SE |
119 | break; |
120 | ||
121 | case 'f': | |
122 | message_level=M_FATAL; | |
123 | break; | |
124 | ||
b2a56f7c SE |
125 | case 'n': |
126 | background=False; | |
127 | break; | |
128 | ||
7b1a9fb7 RK |
129 | case 'm': |
130 | secnet_is_daemon=True; | |
131 | break; | |
132 | ||
2fe58dfd SE |
133 | case 'c': |
134 | if (optarg) | |
135 | configfile=safe_strdup(optarg,"config_filename"); | |
136 | else | |
137 | fatal("secnet: no config filename specified"); | |
138 | break; | |
139 | ||
baa06aeb SE |
140 | case 'j': |
141 | just_check_config=True; | |
142 | break; | |
143 | ||
794f2398 SE |
144 | case 's': |
145 | if (optarg) | |
146 | sites_key=safe_strdup(optarg,"sites-key"); | |
147 | else | |
148 | fatal("secnet: no sites key specified"); | |
149 | break; | |
150 | ||
2fe58dfd | 151 | case '?': |
20c68a90 | 152 | exit(1); |
2fe58dfd SE |
153 | break; |
154 | ||
155 | default: | |
469fd1d9 | 156 | Message(M_ERR,"secnet: Unknown getopt code %c\n",c); |
2fe58dfd SE |
157 | } |
158 | } | |
159 | ||
160 | if (argc-optind != 0) { | |
469fd1d9 | 161 | Message(M_ERR,"secnet: You gave extra command line parameters, " |
2fe58dfd SE |
162 | "which were ignored.\n"); |
163 | } | |
164 | } | |
165 | ||
166 | static void setup(dict_t *config) | |
167 | { | |
168 | list_t *l; | |
169 | item_t *site; | |
170 | dict_t *system; | |
2fe58dfd SE |
171 | struct passwd *pw; |
172 | struct cloc loc; | |
173 | int i; | |
174 | ||
175 | l=dict_lookup(config,"system"); | |
176 | ||
177 | if (!l || list_elem(l,0)->type!=t_dict) { | |
4f5e39ec | 178 | fatal("configuration does not include a \"system\" dictionary"); |
2fe58dfd SE |
179 | } |
180 | system=list_elem(l,0)->data.dict; | |
181 | loc=list_elem(l,0)->loc; | |
182 | ||
183 | /* Arrange systemwide log facility */ | |
184 | l=dict_lookup(system,"log"); | |
185 | if (!l) { | |
4f5e39ec | 186 | fatal("configuration does not include a system/log facility"); |
2fe58dfd | 187 | } |
b2a56f7c | 188 | system_log=init_log(l); |
2fe58dfd SE |
189 | |
190 | /* Who are we supposed to run as? */ | |
191 | userid=dict_read_string(system,"userid",False,"system",loc); | |
192 | if (userid) { | |
7b1a9fb7 | 193 | if (!(pw=getpwnam(userid))) |
4f5e39ec | 194 | fatal("userid \"%s\" not found",userid); |
08daaaff RK |
195 | uid=pw->pw_uid; |
196 | gid=pw->pw_gid; | |
2fe58dfd SE |
197 | } |
198 | ||
199 | /* Pidfile name */ | |
200 | pidfile=dict_read_string(system,"pidfile",False,"system",loc); | |
201 | ||
9d3a4132 SE |
202 | /* Check whether we need root privileges */ |
203 | if (require_root_privileges && uid!=0) { | |
4f5e39ec SE |
204 | fatal("the configured feature \"%s\" requires " |
205 | "that secnet retain root privileges while running.", | |
9d3a4132 SE |
206 | require_root_privileges_explanation); |
207 | } | |
208 | ||
2fe58dfd | 209 | /* Go along site list, starting sites */ |
794f2398 | 210 | l=dict_lookup(config,sites_key); |
2fe58dfd | 211 | if (!l) { |
794f2398 SE |
212 | Message(M_WARNING,"secnet: configuration key \"%s\" is missing; no " |
213 | "remote sites are defined\n",sites_key); | |
9d3a4132 SE |
214 | } else { |
215 | i=0; | |
216 | while ((site=list_elem(l, i++))) { | |
217 | struct site_if *s; | |
218 | if (site->type!=t_closure) { | |
219 | cfgfatal(site->loc,"system","non-closure in site list"); | |
220 | } | |
221 | if (site->data.closure->type!=CL_SITE) { | |
222 | cfgfatal(site->loc,"system","non-site closure in site list"); | |
223 | } | |
224 | s=site->data.closure->interface; | |
225 | s->control(s->st,True); | |
2fe58dfd | 226 | } |
2fe58dfd SE |
227 | } |
228 | } | |
229 | ||
230 | void register_for_poll(void *st, beforepoll_fn *before, | |
1caa23ff | 231 | afterpoll_fn *after, int32_t max_nfds, cstring_t desc) |
2fe58dfd SE |
232 | { |
233 | struct poll_interest *i; | |
234 | ||
235 | i=safe_malloc(sizeof(*i),"register_for_poll"); | |
236 | i->before=before; | |
237 | i->after=after; | |
238 | i->state=st; | |
239 | i->max_nfds=max_nfds; | |
240 | i->nfds=0; | |
241 | i->desc=desc; | |
59230b9b | 242 | assert(total_nfds < INT_MAX - max_nfds); |
2fe58dfd SE |
243 | total_nfds+=max_nfds; |
244 | i->next=reg; | |
245 | reg=i; | |
246 | return; | |
247 | } | |
248 | ||
249 | static 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 | ||
29672515 RK |
257 | #if USE_SELECT |
258 | static int fakepoll(struct pollfd *fds, int nfds, int timeout) { | |
259 | fd_set infds[1], outfds[1]; | |
260 | int maxfd = -1, i, rc; | |
261 | struct timeval tvtimeout; | |
262 | FD_ZERO(infds); | |
263 | FD_ZERO(outfds); | |
264 | for(i = 0; i < nfds; ++i) { | |
265 | if(fds[i].events & POLLIN) | |
266 | FD_SET(fds[i].fd, infds); | |
267 | if(fds[i].events & POLLOUT) | |
268 | FD_SET(fds[i].fd, outfds); | |
269 | if(fds[i].fd > maxfd) | |
270 | maxfd = fds[i].fd; | |
271 | } | |
272 | if(timeout != -1) { | |
273 | tvtimeout.tv_sec = timeout / 1000; | |
274 | tvtimeout.tv_usec = 1000 * (timeout % 1000); | |
275 | } | |
276 | rc = select(maxfd + 1, infds, outfds, NULL, | |
277 | timeout == -1 ? NULL : &tvtimeout); | |
278 | if(rc >= 0) { | |
279 | for(i = 0; i < nfds; ++i) { | |
280 | int revents = 0; | |
281 | if(FD_ISSET(fds[i].fd, infds)) | |
282 | revents |= POLLIN; | |
283 | if(FD_ISSET(fds[i].fd, outfds)) | |
284 | revents |= POLLOUT; | |
285 | fds[i].revents = revents; | |
286 | } | |
287 | } | |
288 | return rc; | |
289 | } | |
290 | #endif | |
291 | ||
698280de IJ |
292 | struct timeval tv_now_global; |
293 | uint64_t now_global; | |
294 | ||
2fe58dfd SE |
295 | static void run(void) |
296 | { | |
2fe58dfd SE |
297 | struct poll_interest *i; |
298 | int rv, nfds, remain, idx; | |
299 | int timeout; | |
300 | struct pollfd *fds; | |
301 | ||
3b83c932 | 302 | fds=safe_malloc(sizeof(*fds)*total_nfds, "run"); |
2fe58dfd | 303 | |
7138d0c5 | 304 | Message(M_NOTICE,"%s [%d]: starting\n",version,secnet_pid); |
b2a56f7c | 305 | |
7138d0c5 | 306 | do { |
698280de | 307 | if (gettimeofday(&tv_now_global, NULL)!=0) { |
2fe58dfd SE |
308 | fatal_perror("main loop: gettimeofday"); |
309 | } | |
698280de IJ |
310 | now_global=((uint64_t)tv_now_global.tv_sec*(uint64_t)1000)+ |
311 | ((uint64_t)tv_now_global.tv_usec/(uint64_t)1000); | |
2fe58dfd SE |
312 | idx=0; |
313 | for (i=reg; i; i=i->next) { | |
508bb583 IJ |
314 | int check; |
315 | for (check=0; check<i->nfds; check++) { | |
316 | if(fds[idx+check].revents & POLLNVAL) { | |
317 | fatal("run: poll (%s#%d) set POLLNVAL", i->desc, check); | |
318 | } | |
29672515 | 319 | } |
90a39563 | 320 | i->after(i->state, fds+idx, i->nfds); |
2fe58dfd SE |
321 | idx+=i->nfds; |
322 | } | |
323 | remain=total_nfds; | |
324 | idx=0; | |
325 | timeout=-1; | |
326 | for (i=reg; i; i=i->next) { | |
327 | nfds=remain; | |
90a39563 | 328 | rv=i->before(i->state, fds+idx, &nfds, &timeout); |
2fe58dfd SE |
329 | if (rv!=0) { |
330 | /* XXX we need to handle this properly: increase the | |
331 | nfds available */ | |
4f5e39ec | 332 | fatal("run: beforepoll_fn (%s) returns %d",i->desc,rv); |
2fe58dfd SE |
333 | } |
334 | if (timeout<-1) { | |
779837e1 IJ |
335 | fatal("run: beforepoll_fn (%s) set timeout to %d", |
336 | i->desc,timeout); | |
2fe58dfd SE |
337 | } |
338 | idx+=nfds; | |
339 | remain-=nfds; | |
340 | i->nfds=nfds; | |
341 | } | |
342 | do { | |
7138d0c5 | 343 | if (finished) break; |
29672515 RK |
344 | #if USE_SELECT |
345 | rv=fakepoll(fds, idx, timeout); | |
346 | #else | |
2fe58dfd | 347 | rv=poll(fds, idx, timeout); |
29672515 | 348 | #endif |
2fe58dfd SE |
349 | if (rv<0) { |
350 | if (errno!=EINTR) { | |
351 | fatal_perror("run: poll"); | |
352 | } | |
353 | } | |
354 | } while (rv<0); | |
7138d0c5 | 355 | } while (!finished); |
3b83c932 | 356 | free(fds); |
2fe58dfd SE |
357 | } |
358 | ||
7b1a9fb7 | 359 | /* Surrender privileges, if necessary */ |
2fe58dfd SE |
360 | static void droppriv(void) |
361 | { | |
08daaaff RK |
362 | if (userid) { |
363 | if (setgid(gid)!=0) | |
364 | fatal_perror("can't set gid to %ld",(long)gid); | |
7b1a9fb7 | 365 | if (initgroups(userid, gid) < 0) |
08daaaff | 366 | fatal_perror("initgroups"); |
b2a56f7c SE |
367 | if (setuid(uid)!=0) { |
368 | fatal_perror("can't set uid to \"%s\"",userid); | |
369 | } | |
08daaaff RK |
370 | assert(getuid() == uid); |
371 | assert(geteuid() == uid); | |
372 | assert(getgid() == gid); | |
373 | assert(getegid() == gid); | |
b2a56f7c | 374 | } |
7b1a9fb7 RK |
375 | } |
376 | ||
377 | /* Become a daemon, if necessary */ | |
378 | static void become_daemon(void) | |
379 | { | |
380 | FILE *pf=NULL; | |
381 | pid_t p; | |
382 | int errfds[2]; | |
383 | ||
384 | add_hook(PHASE_SHUTDOWN,system_phase_hook,NULL); | |
385 | ||
386 | /* We only want to become a daemon if we are not one | |
387 | already */ | |
388 | if (background && !secnet_is_daemon) { | |
2fe58dfd SE |
389 | p=fork(); |
390 | if (p>0) { | |
7b1a9fb7 RK |
391 | /* Parent process - just exit */ |
392 | _exit(0); | |
2fe58dfd SE |
393 | } else if (p==0) { |
394 | /* Child process - all done, just carry on */ | |
b2a56f7c | 395 | secnet_is_daemon=True; |
7b1a9fb7 RK |
396 | if (setsid() < 0) |
397 | fatal_perror("setsid"); | |
2fe58dfd SE |
398 | } else { |
399 | /* Error */ | |
400 | fatal_perror("cannot fork"); | |
401 | exit(1); | |
402 | } | |
2fe58dfd | 403 | } |
7b1a9fb7 RK |
404 | if (secnet_is_daemon) { |
405 | /* stderr etc are redirected to the system/log facility */ | |
406 | if (pipe(errfds)!=0) { | |
407 | fatal_perror("can't create pipe for stderr"); | |
408 | } | |
409 | if (dup2(errfds[1],0) < 0 | |
410 | || dup2(errfds[1],1) < 0 | |
411 | || dup2(errfds[1],2) < 0) | |
412 | fatal_perror("can't dup2 pipe"); | |
413 | if (close(errfds[1]) < 0) | |
414 | fatal_perror("can't close redundant pipe endpoint"); | |
415 | log_from_fd(errfds[0],"stderr",system_log); | |
416 | } | |
7138d0c5 | 417 | secnet_pid=getpid(); |
7b1a9fb7 RK |
418 | |
419 | /* Now we can write the pidfile */ | |
420 | if (pidfile) { | |
421 | pf=fopen(pidfile,"w"); | |
422 | if (!pf) { | |
423 | fatal_perror("cannot open pidfile \"%s\"",pidfile); | |
424 | } | |
425 | if (fprintf(pf,"%ld\n",(long)secnet_pid) < 0 | |
426 | || fclose(pf) < 0) | |
427 | fatal_perror("cannot write to pidfile \"%s\"",pidfile); | |
428 | } | |
7138d0c5 | 429 | } |
2fe58dfd | 430 | |
7138d0c5 SE |
431 | static signal_notify_fn finish,ignore_hup; |
432 | static void finish(void *st, int signum) | |
433 | { | |
434 | finished=True; | |
435 | Message(M_NOTICE,"%s [%d]: received %s\n",version,secnet_pid,(string_t)st); | |
436 | } | |
437 | static void ignore_hup(void *st, int signum) | |
438 | { | |
439 | Message(M_INFO,"%s [%d]: received SIGHUP\n",version,secnet_pid); | |
440 | return; | |
2fe58dfd SE |
441 | } |
442 | ||
443 | int main(int argc, char **argv) | |
444 | { | |
445 | dict_t *config; | |
446 | ||
447 | enter_phase(PHASE_GETOPTS); | |
448 | parse_options(argc,argv); | |
449 | ||
450 | enter_phase(PHASE_READCONFIG); | |
451 | config=read_conffile(configfile); | |
452 | ||
453 | enter_phase(PHASE_SETUP); | |
454 | setup(config); | |
baa06aeb SE |
455 | |
456 | if (just_check_config) { | |
457 | Message(M_INFO,"configuration file check complete\n"); | |
458 | exit(0); | |
459 | } | |
460 | ||
7b1a9fb7 RK |
461 | enter_phase(PHASE_DAEMONIZE); |
462 | become_daemon(); | |
463 | ||
baa06aeb SE |
464 | enter_phase(PHASE_GETRESOURCES); |
465 | /* Appropriate phase hooks will have been run */ | |
2fe58dfd SE |
466 | |
467 | enter_phase(PHASE_DROPPRIV); | |
468 | droppriv(); | |
469 | ||
7138d0c5 | 470 | start_signal_handling(); |
fe5e9cc4 SE |
471 | request_signal_notification(SIGTERM,finish,safe_strdup("SIGTERM","run")); |
472 | if (!background) request_signal_notification(SIGINT,finish, | |
473 | safe_strdup("SIGINT","run")); | |
7138d0c5 | 474 | request_signal_notification(SIGHUP,ignore_hup,NULL); |
042a8da9 | 475 | enter_phase(PHASE_RUN); |
2fe58dfd SE |
476 | run(); |
477 | ||
478 | enter_phase(PHASE_SHUTDOWN); | |
7138d0c5 | 479 | Message(M_NOTICE,"%s [%d]: finished\n",version,secnet_pid); |
2fe58dfd SE |
480 | |
481 | return 0; | |
482 | } |