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'}, | |
65 | {"silent", 0, 0, 'f'}, | |
66 | {"quiet", 0, 0, 'f'}, | |
dd568518 | 67 | {"debug", 0, 0, 'd'}, |
2fe58dfd | 68 | {"config", 1, 0, 'c'}, |
baa06aeb | 69 | {"just-check-config", 0, 0, 'j'}, |
794f2398 | 70 | {"sites-key", 1, 0, 's'}, |
2fe58dfd SE |
71 | {0,0,0,0} |
72 | }; | |
73 | ||
794f2398 | 74 | c=getopt_long(argc, argv, "vwdnjc:ft:s:", |
2fe58dfd SE |
75 | long_options, &option_index); |
76 | if (c==-1) | |
77 | break; | |
78 | ||
79 | switch(c) { | |
80 | case 2: | |
81 | /* Help */ | |
4f5e39ec SE |
82 | printf("Usage: secnet [OPTION]...\n\n" |
83 | " -f, --silent, --quiet suppress error messages\n" | |
84 | " -w, --nowarnings suppress warnings\n" | |
85 | " -v, --verbose output extra diagnostics\n" | |
86 | " -c, --config=filename specify a configuration file\n" | |
87 | " -j, --just-check-config stop after reading " | |
88 | "configuration file\n" | |
89 | " -s, --sites-key=name configuration key that " | |
90 | "specifies active sites\n" | |
91 | " -n, --nodetach do not run in background\n" | |
dd568518 | 92 | " -d, --debug output debug messages\n" |
4f5e39ec SE |
93 | " --help display this help and exit\n" |
94 | " --version output version information " | |
95 | "and exit\n" | |
2fe58dfd SE |
96 | ); |
97 | exit(0); | |
98 | break; | |
99 | ||
100 | case 1: | |
101 | /* Version */ | |
4f5e39ec | 102 | printf("%s\n",version); |
2fe58dfd SE |
103 | exit(0); |
104 | break; | |
105 | ||
106 | case 'v': | |
469fd1d9 | 107 | message_level|=M_INFO|M_NOTICE|M_WARNING|M_ERR|M_SECURITY| |
b2a56f7c | 108 | M_FATAL; |
2fe58dfd SE |
109 | break; |
110 | ||
b2a56f7c SE |
111 | case 'w': |
112 | message_level&=(~M_WARNING); | |
2fe58dfd SE |
113 | break; |
114 | ||
115 | case 'd': | |
b2a56f7c | 116 | message_level|=M_DEBUG_CONFIG|M_DEBUG_PHASE|M_DEBUG; |
2fe58dfd SE |
117 | break; |
118 | ||
119 | case 'f': | |
120 | message_level=M_FATAL; | |
121 | break; | |
122 | ||
b2a56f7c SE |
123 | case 'n': |
124 | background=False; | |
125 | break; | |
126 | ||
2fe58dfd SE |
127 | case 'c': |
128 | if (optarg) | |
129 | configfile=safe_strdup(optarg,"config_filename"); | |
130 | else | |
131 | fatal("secnet: no config filename specified"); | |
132 | break; | |
133 | ||
baa06aeb SE |
134 | case 'j': |
135 | just_check_config=True; | |
136 | break; | |
137 | ||
794f2398 SE |
138 | case 's': |
139 | if (optarg) | |
140 | sites_key=safe_strdup(optarg,"sites-key"); | |
141 | else | |
142 | fatal("secnet: no sites key specified"); | |
143 | break; | |
144 | ||
2fe58dfd | 145 | case '?': |
20c68a90 | 146 | exit(1); |
2fe58dfd SE |
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 | ||
160 | static 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) { | |
08daaaff | 187 | if(!(pw=getpwnam(userid))) |
4f5e39ec | 188 | fatal("userid \"%s\" not found",userid); |
08daaaff RK |
189 | uid=pw->pw_uid; |
190 | gid=pw->pw_gid; | |
2fe58dfd SE |
191 | } |
192 | ||
193 | /* Pidfile name */ | |
194 | pidfile=dict_read_string(system,"pidfile",False,"system",loc); | |
195 | ||
9d3a4132 SE |
196 | /* Check whether we need root privileges */ |
197 | if (require_root_privileges && uid!=0) { | |
4f5e39ec SE |
198 | fatal("the configured feature \"%s\" requires " |
199 | "that secnet retain root privileges while running.", | |
9d3a4132 SE |
200 | require_root_privileges_explanation); |
201 | } | |
202 | ||
2fe58dfd | 203 | /* Go along site list, starting sites */ |
794f2398 | 204 | l=dict_lookup(config,sites_key); |
2fe58dfd | 205 | if (!l) { |
794f2398 SE |
206 | Message(M_WARNING,"secnet: configuration key \"%s\" is missing; no " |
207 | "remote sites are defined\n",sites_key); | |
9d3a4132 SE |
208 | } else { |
209 | i=0; | |
210 | while ((site=list_elem(l, i++))) { | |
211 | struct site_if *s; | |
212 | if (site->type!=t_closure) { | |
213 | cfgfatal(site->loc,"system","non-closure in site list"); | |
214 | } | |
215 | if (site->data.closure->type!=CL_SITE) { | |
216 | cfgfatal(site->loc,"system","non-site closure in site list"); | |
217 | } | |
218 | s=site->data.closure->interface; | |
219 | s->control(s->st,True); | |
2fe58dfd | 220 | } |
2fe58dfd SE |
221 | } |
222 | } | |
223 | ||
224 | void register_for_poll(void *st, beforepoll_fn *before, | |
1caa23ff | 225 | afterpoll_fn *after, int32_t max_nfds, cstring_t desc) |
2fe58dfd SE |
226 | { |
227 | struct poll_interest *i; | |
228 | ||
229 | i=safe_malloc(sizeof(*i),"register_for_poll"); | |
230 | i->before=before; | |
231 | i->after=after; | |
232 | i->state=st; | |
233 | i->max_nfds=max_nfds; | |
234 | i->nfds=0; | |
235 | i->desc=desc; | |
59230b9b | 236 | assert(total_nfds < INT_MAX - max_nfds); |
2fe58dfd SE |
237 | total_nfds+=max_nfds; |
238 | i->next=reg; | |
239 | reg=i; | |
240 | return; | |
241 | } | |
242 | ||
243 | static void system_phase_hook(void *sst, uint32_t newphase) | |
244 | { | |
245 | if (newphase==PHASE_SHUTDOWN && pidfile) { | |
246 | /* Try to unlink the pidfile; don't care if it fails */ | |
247 | unlink(pidfile); | |
248 | } | |
249 | } | |
250 | ||
29672515 RK |
251 | #if USE_SELECT |
252 | static int fakepoll(struct pollfd *fds, int nfds, int timeout) { | |
253 | fd_set infds[1], outfds[1]; | |
254 | int maxfd = -1, i, rc; | |
255 | struct timeval tvtimeout; | |
256 | FD_ZERO(infds); | |
257 | FD_ZERO(outfds); | |
258 | for(i = 0; i < nfds; ++i) { | |
259 | if(fds[i].events & POLLIN) | |
260 | FD_SET(fds[i].fd, infds); | |
261 | if(fds[i].events & POLLOUT) | |
262 | FD_SET(fds[i].fd, outfds); | |
263 | if(fds[i].fd > maxfd) | |
264 | maxfd = fds[i].fd; | |
265 | } | |
266 | if(timeout != -1) { | |
267 | tvtimeout.tv_sec = timeout / 1000; | |
268 | tvtimeout.tv_usec = 1000 * (timeout % 1000); | |
269 | } | |
270 | rc = select(maxfd + 1, infds, outfds, NULL, | |
271 | timeout == -1 ? NULL : &tvtimeout); | |
272 | if(rc >= 0) { | |
273 | for(i = 0; i < nfds; ++i) { | |
274 | int revents = 0; | |
275 | if(FD_ISSET(fds[i].fd, infds)) | |
276 | revents |= POLLIN; | |
277 | if(FD_ISSET(fds[i].fd, outfds)) | |
278 | revents |= POLLOUT; | |
279 | fds[i].revents = revents; | |
280 | } | |
281 | } | |
282 | return rc; | |
283 | } | |
284 | #endif | |
285 | ||
698280de IJ |
286 | struct timeval tv_now_global; |
287 | uint64_t now_global; | |
288 | ||
2fe58dfd SE |
289 | static void run(void) |
290 | { | |
2fe58dfd SE |
291 | struct poll_interest *i; |
292 | int rv, nfds, remain, idx; | |
293 | int timeout; | |
294 | struct pollfd *fds; | |
295 | ||
3b83c932 | 296 | fds=safe_malloc(sizeof(*fds)*total_nfds, "run"); |
2fe58dfd | 297 | |
7138d0c5 | 298 | Message(M_NOTICE,"%s [%d]: starting\n",version,secnet_pid); |
b2a56f7c | 299 | |
7138d0c5 | 300 | do { |
698280de | 301 | if (gettimeofday(&tv_now_global, NULL)!=0) { |
2fe58dfd SE |
302 | fatal_perror("main loop: gettimeofday"); |
303 | } | |
698280de IJ |
304 | now_global=((uint64_t)tv_now_global.tv_sec*(uint64_t)1000)+ |
305 | ((uint64_t)tv_now_global.tv_usec/(uint64_t)1000); | |
2fe58dfd SE |
306 | idx=0; |
307 | for (i=reg; i; i=i->next) { | |
508bb583 IJ |
308 | int check; |
309 | for (check=0; check<i->nfds; check++) { | |
310 | if(fds[idx+check].revents & POLLNVAL) { | |
311 | fatal("run: poll (%s#%d) set POLLNVAL", i->desc, check); | |
312 | } | |
29672515 | 313 | } |
90a39563 | 314 | i->after(i->state, fds+idx, i->nfds); |
2fe58dfd SE |
315 | idx+=i->nfds; |
316 | } | |
317 | remain=total_nfds; | |
318 | idx=0; | |
319 | timeout=-1; | |
320 | for (i=reg; i; i=i->next) { | |
321 | nfds=remain; | |
90a39563 | 322 | rv=i->before(i->state, fds+idx, &nfds, &timeout); |
2fe58dfd SE |
323 | if (rv!=0) { |
324 | /* XXX we need to handle this properly: increase the | |
325 | nfds available */ | |
4f5e39ec | 326 | fatal("run: beforepoll_fn (%s) returns %d",i->desc,rv); |
2fe58dfd SE |
327 | } |
328 | if (timeout<-1) { | |
4f5e39ec | 329 | fatal("run: beforepoll_fn (%s) set timeout to %d",timeout); |
2fe58dfd SE |
330 | } |
331 | idx+=nfds; | |
332 | remain-=nfds; | |
333 | i->nfds=nfds; | |
334 | } | |
335 | do { | |
7138d0c5 | 336 | if (finished) break; |
29672515 RK |
337 | #if USE_SELECT |
338 | rv=fakepoll(fds, idx, timeout); | |
339 | #else | |
2fe58dfd | 340 | rv=poll(fds, idx, timeout); |
29672515 | 341 | #endif |
2fe58dfd SE |
342 | if (rv<0) { |
343 | if (errno!=EINTR) { | |
344 | fatal_perror("run: poll"); | |
345 | } | |
346 | } | |
347 | } while (rv<0); | |
7138d0c5 | 348 | } while (!finished); |
3b83c932 | 349 | free(fds); |
2fe58dfd SE |
350 | } |
351 | ||
352 | static void droppriv(void) | |
353 | { | |
354 | FILE *pf=NULL; | |
355 | pid_t p; | |
fe5e9cc4 | 356 | int errfds[2]; |
2fe58dfd SE |
357 | |
358 | add_hook(PHASE_SHUTDOWN,system_phase_hook,NULL); | |
359 | ||
b2a56f7c SE |
360 | /* Open the pidfile for writing now: we may be unable to do so |
361 | once we drop privileges. */ | |
362 | if (pidfile) { | |
363 | pf=fopen(pidfile,"w"); | |
364 | if (!pf) { | |
365 | fatal_perror("cannot open pidfile \"%s\"",pidfile); | |
2fe58dfd | 366 | } |
b2a56f7c SE |
367 | } |
368 | if (!background && pf) { | |
369 | fprintf(pf,"%d\n",getpid()); | |
370 | fclose(pf); | |
371 | } | |
372 | ||
373 | /* Now drop privileges */ | |
08daaaff RK |
374 | if (userid) { |
375 | if (setgid(gid)!=0) | |
376 | fatal_perror("can't set gid to %ld",(long)gid); | |
377 | if(initgroups(userid, gid) < 0) | |
378 | fatal_perror("initgroups"); | |
b2a56f7c SE |
379 | if (setuid(uid)!=0) { |
380 | fatal_perror("can't set uid to \"%s\"",userid); | |
381 | } | |
08daaaff RK |
382 | assert(getuid() == uid); |
383 | assert(geteuid() == uid); | |
384 | assert(getgid() == gid); | |
385 | assert(getegid() == gid); | |
b2a56f7c SE |
386 | } |
387 | if (background) { | |
2fe58dfd SE |
388 | p=fork(); |
389 | if (p>0) { | |
390 | if (pf) { | |
391 | /* Parent process - write pidfile, exit */ | |
392 | fprintf(pf,"%d\n",p); | |
393 | fclose(pf); | |
394 | } | |
395 | exit(0); | |
396 | } else if (p==0) { | |
397 | /* Child process - all done, just carry on */ | |
398 | if (pf) fclose(pf); | |
fe5e9cc4 SE |
399 | /* Close stdin and stdout; we don't need them any more. |
400 | stderr is redirected to the system/log facility */ | |
401 | if (pipe(errfds)!=0) { | |
402 | fatal_perror("can't create pipe for stderr"); | |
403 | } | |
8dea8d37 SE |
404 | close(0); |
405 | close(1); | |
fe5e9cc4 SE |
406 | close(2); |
407 | dup2(errfds[1],0); | |
408 | dup2(errfds[1],1); | |
409 | dup2(errfds[1],2); | |
b2a56f7c | 410 | secnet_is_daemon=True; |
ff05a229 | 411 | setsid(); |
fe5e9cc4 | 412 | log_from_fd(errfds[0],"stderr",system_log); |
2fe58dfd SE |
413 | } else { |
414 | /* Error */ | |
415 | fatal_perror("cannot fork"); | |
416 | exit(1); | |
417 | } | |
2fe58dfd | 418 | } |
7138d0c5 SE |
419 | secnet_pid=getpid(); |
420 | } | |
2fe58dfd | 421 | |
7138d0c5 SE |
422 | static signal_notify_fn finish,ignore_hup; |
423 | static void finish(void *st, int signum) | |
424 | { | |
425 | finished=True; | |
426 | Message(M_NOTICE,"%s [%d]: received %s\n",version,secnet_pid,(string_t)st); | |
427 | } | |
428 | static void ignore_hup(void *st, int signum) | |
429 | { | |
430 | Message(M_INFO,"%s [%d]: received SIGHUP\n",version,secnet_pid); | |
431 | return; | |
2fe58dfd SE |
432 | } |
433 | ||
434 | int main(int argc, char **argv) | |
435 | { | |
436 | dict_t *config; | |
437 | ||
438 | enter_phase(PHASE_GETOPTS); | |
439 | parse_options(argc,argv); | |
440 | ||
441 | enter_phase(PHASE_READCONFIG); | |
442 | config=read_conffile(configfile); | |
443 | ||
444 | enter_phase(PHASE_SETUP); | |
445 | setup(config); | |
baa06aeb SE |
446 | |
447 | if (just_check_config) { | |
448 | Message(M_INFO,"configuration file check complete\n"); | |
449 | exit(0); | |
450 | } | |
451 | ||
452 | enter_phase(PHASE_GETRESOURCES); | |
453 | /* Appropriate phase hooks will have been run */ | |
2fe58dfd SE |
454 | |
455 | enter_phase(PHASE_DROPPRIV); | |
456 | droppriv(); | |
457 | ||
7138d0c5 | 458 | start_signal_handling(); |
fe5e9cc4 SE |
459 | request_signal_notification(SIGTERM,finish,safe_strdup("SIGTERM","run")); |
460 | if (!background) request_signal_notification(SIGINT,finish, | |
461 | safe_strdup("SIGINT","run")); | |
7138d0c5 | 462 | request_signal_notification(SIGHUP,ignore_hup,NULL); |
042a8da9 | 463 | enter_phase(PHASE_RUN); |
2fe58dfd SE |
464 | run(); |
465 | ||
466 | enter_phase(PHASE_SHUTDOWN); | |
7138d0c5 | 467 | Message(M_NOTICE,"%s [%d]: finished\n",version,secnet_pid); |
2fe58dfd SE |
468 | |
469 | return 0; | |
470 | } |