2 * Copyright (c) 2022 Omar Polo <op@omarpolo.com>
3 * Copyright (c) 2014 Reyk Floeter <reyk@openbsd.org>
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 #include <sys/types.h>
19 #include <sys/queue.h>
21 #include <sys/socket.h>
24 #include <netinet/in.h>
25 #include <arpa/inet.h>
47 static int parent_configure(struct galileo *);
48 static void parent_configure_done(struct galileo *);
49 static void parent_reload(struct galileo *);
50 static void parent_sig_handler(int, short, void *);
51 static int parent_dispatch_proxy(int, struct privsep_proc *,
53 static __dead void parent_shutdown(struct galileo *);
55 static struct privsep_proc procs[] = {
56 { "proxy", PROC_PROXY, parent_dispatch_proxy, proxy },
61 const char *conffile = GALILEO_CONF;
66 fprintf(stderr, "usage: %s [-dnv] [-D macro=value] [-f file]\n",
72 main(int argc, char **argv)
77 const char *title = NULL;
79 int conftest = 0, debug = 0, verbose = 0;
81 int proc_id = PROC_PARENT;
82 int proc_instance = 0;
84 setlocale(LC_CTYPE, "");
86 /* log to stderr until daemonized */
87 log_init(1, LOG_DAEMON);
88 log_setverbose(verbose);
90 while ((ch = getopt(argc, argv, "D:df:I:nP:v")) != -1) {
93 if (cmdline_symset(optarg) < 0)
94 log_warnx("could not parse macro definition %s",
104 proc_instance = strtonum(optarg, 0, PROC_MAX_INSTANCES,
107 fatalx("invalid process instance");
114 proc_id = proc_getid(procs, nitems(procs), title);
115 if (proc_id == PROC_MAX)
116 fatalx("invalid process name");
130 fatalx("need root privileges");
132 log_setverbose(verbose);
134 env = xcalloc(1, sizeof(*env));
136 if (parse_config(conffile, env) == -1)
140 fprintf(stderr, "configuration OK\n");
144 ps = xcalloc(1, sizeof(*ps));
147 if ((ps->ps_pw = getpwnam(GALILEO_USER)) == NULL)
148 fatalx("unknown user %s", GALILEO_USER);
150 ps->ps_instances[PROC_PROXY] = env->sc_prefork;
151 ps->ps_instance = proc_instance;
153 ps->ps_title[proc_id] = title;
155 if (*env->sc_chroot == '\0') {
156 if (strlcpy(env->sc_chroot, ps->ps_pw->pw_dir,
157 sizeof(env->sc_chroot)) >= sizeof(env->sc_chroot))
158 fatalx("chroot path too long!");
161 for (i = 0; i < nitems(procs); ++i)
162 procs[i].p_chroot = env->sc_chroot;
164 /* only the parent returns */
165 proc_init(ps, procs, nitems(procs), debug, argc0, argv, proc_id);
167 log_procinit("parent");
168 if (!debug && daemon(0, 0) == -1)
169 fatal("failed to daemonize");
171 log_init(debug, LOG_DAEMON);
175 if (pledge("stdio rpath wpath cpath chown unix fattr sendfd", NULL)
181 signal(SIGPIPE, SIG_IGN);
183 signal_set(&ps->ps_evsigint, SIGINT, parent_sig_handler, ps);
184 signal_set(&ps->ps_evsigterm, SIGTERM, parent_sig_handler, ps);
185 signal_set(&ps->ps_evsigchld, SIGCHLD, parent_sig_handler, ps);
186 signal_set(&ps->ps_evsighup, SIGHUP, parent_sig_handler, ps);
188 signal_add(&ps->ps_evsigint, NULL);
189 signal_add(&ps->ps_evsigterm, NULL);
190 signal_add(&ps->ps_evsigchld, NULL);
191 signal_add(&ps->ps_evsighup, NULL);
195 if (parent_configure(env) == -1)
196 fatalx("configuration failed");
200 parent_shutdown(env);
207 parent_configure(struct galileo *env)
212 TAILQ_FOREACH(proxy, &env->sc_proxies, pr_entry) {
213 if (config_setproxy(env, proxy) == -1)
217 /* XXX: eventually they will be more than just one */
218 if (config_setsock(env) == -1)
219 fatal("send socket");
221 /* The proxiess need to reload their config. */
222 env->sc_reload = env->sc_prefork;
224 for (id = 0; id < PROC_MAX; id++) {
225 if (id == privsep_process)
227 proc_compose(env->sc_ps, id, IMSG_CFG_DONE, env, sizeof(env));
235 parent_configure_done(struct galileo *env)
239 if (env->sc_reload == 0) {
240 log_warnx("configuration already finished");
245 if (env->sc_reload == 0) {
246 for (id = 0; id < PROC_MAX; ++id) {
247 if (id == privsep_process)
250 proc_compose(env->sc_ps, id, IMSG_CTL_START, NULL, 0);
256 parent_reload(struct galileo *env)
258 if (env->sc_reload) {
259 log_debug("%s: already in progress: %d pending",
260 __func__, env->sc_reload);
263 log_debug("%s: config file %s", __func__, conffile);
267 if (parse_config(conffile, env) == -1) {
268 log_warnx("failed to load config file: %s", conffile);
272 config_setreset(env);
273 parent_configure(env);
277 parent_sig_handler(int sig, short ev, void *arg)
279 struct privsep *ps = arg;
282 * Normal signal handler rules don't apply because libevent
288 if (privsep_process != PROC_PARENT)
290 log_info("reload requested with SIGHUP");
291 parent_reload(ps->ps_env);
294 log_warnx("one child died, quitting.");
298 parent_shutdown(ps->ps_env);
301 fatalx("unexpected signal %d", sig);
306 parent_dispatch_proxy(int fd, struct privsep_proc *p, struct imsg *imsg)
308 struct privsep *ps = p->p_ps;
309 struct galileo *env = ps->ps_env;
311 switch (imsg->hdr.type) {
313 parent_configure_done(env);
323 parent_shutdown(struct galileo *env)
327 proc_kill(env->sc_ps);
332 log_info("parent terminating, pid %d", getpid());
337 accept_reserve(int sockfd, struct sockaddr *addr, socklen_t *addrlen,
338 int reserve, volatile int *counter)
341 if (getdtablecount() + reserve +
342 *counter >= getdtablesize()) {
347 if ((ret = accept4(sockfd, addr, addrlen, SOCK_NONBLOCK)) > -1) {
349 log_debug("%s: inflight incremented, now %d",__func__, *counter);