2 * Copyright (c) 2022 Omar Polo <op@omarpolo.com>
3 * Copyright (c) 2011 - 2015 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>
20 #include <sys/socket.h>
26 #include <sys/stat.h> /* umask */
27 #include <sys/un.h> /* sockaddr_un */
46 config_init(struct galileo *env)
48 /* Global configuration */
49 if (privsep_process == PROC_PARENT)
50 env->sc_prefork = PROXY_NUMPROC;
52 /* Other configuration. */
53 TAILQ_INIT(&env->sc_proxies);
61 config_purge(struct galileo *env)
67 while ((fcgi = SPLAY_MIN(fcgi_tree, &env->sc_fcgi_socks))) {
68 while ((clt = SPLAY_MIN(client_tree, &fcgi->fcg_clients))) {
69 if (fcgi_abort_request(clt) == -1) {
78 SPLAY_REMOVE(fcgi_tree, &env->sc_fcgi_socks, fcgi);
82 event_del(&env->sc_evsock);
83 event_del(&env->sc_evpause);
84 close(env->sc_sock_fd);
87 while ((p = TAILQ_FIRST(&env->sc_proxies)) != NULL) {
88 TAILQ_REMOVE(&env->sc_proxies, p, pr_entry);
94 config_setproxy(struct galileo *env, struct proxy *p)
96 struct privsep *ps = env->sc_ps;
98 if (proc_compose(ps, PROC_PROXY, IMSG_CFG_SRV, p, sizeof(*p)) == -1)
99 fatal("proc_compose");
104 config_getproxy(struct galileo *env, struct imsg *imsg)
108 proxy = xcalloc(1, sizeof(*proxy));
109 if (IMSG_DATA_SIZE(imsg) != sizeof(*proxy))
110 fatalx("%s: bad imsg size", __func__);
112 memcpy(proxy, imsg->data, sizeof(*proxy));
114 log_debug("%s: proxy=%s -> %s:%s (%s)", __func__,
115 proxy->pr_conf.host, proxy->pr_conf.proxy_addr,
116 proxy->pr_conf.proxy_port, proxy->pr_conf.proxy_name);
118 TAILQ_INSERT_TAIL(&env->sc_proxies, proxy, pr_entry);
124 config_setsock(struct galileo *env)
126 struct privsep *ps = env->sc_ps;
127 struct passwd *pw = ps->ps_pw;
128 struct sockaddr_un sun;
129 const char *path = GALILEO_SOCK;
130 int id, fd, old_umask;
133 * open listening socket.
135 * XXX: move to server.c as server_privinit like httpd once we
136 * support more than one listening socket.
138 if ((fd = socket(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK, 0)) == -1) {
139 log_warn("%s: socket", __func__);
143 memset(&sun, 0, sizeof(sun));
144 sun.sun_family = AF_UNIX;
145 strlcpy(sun.sun_path, path, sizeof(sun.sun_path));
147 if (unlink(path) == -1)
148 if (errno != ENOENT) {
149 log_warn("%s: unlink %s", __func__, path);
154 old_umask = umask(S_IXUSR|S_IXGRP|S_IWOTH|S_IROTH|S_IXOTH);
155 if (bind(fd, (struct sockaddr *)&sun, sizeof(sun)) == -1) {
156 log_warn("%s: bind: %s (%d)", __func__, path, geteuid());
163 if (chmod(path, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP) == -1) {
164 log_warn("%s: chmod", __func__);
170 if (chown(path, pw->pw_uid, pw->pw_gid) == -1) {
171 log_warn("%s: chown", __func__);
177 if (listen(fd, 5) == -1) {
178 log_warn("%s: listen", __func__);
184 for (id = 0; id < PROC_MAX; ++id) {
187 if (id == privsep_process || id != PROC_PROXY)
191 proc_range(ps, id, &n, &m);
192 for (n = 0; n < m; ++n) {
195 if ((d = dup(fd)) == -1) {
196 log_warn("%s: dup", __func__);
201 if (proc_compose_imsg(ps, id, n, IMSG_CFG_SOCK,
202 -1, d, NULL, 0) == -1) {
203 log_warn("%s: failed to compose "
204 "IMSG_CFG_SOCK", __func__);
208 if (proc_flush_imsg(ps, id, n) == -1) {
209 log_warn("%s: failed to flush", __func__);
221 config_getsock(struct galileo *env, struct imsg *imsg)
223 /* XXX: make it more like httpd/gotwebd' one */
228 config_setreset(struct galileo *env)
230 struct privsep *ps = env->sc_ps;
233 for (id = 0; id < PROC_MAX; ++id)
234 if (id != PROC_PARENT)
235 proc_compose(ps, id, IMSG_CTL_RESET, NULL, 0);
241 config_getreset(struct galileo *env, struct imsg *imsg)
249 config_getcfg(struct galileo *env, struct imsg *imsg)
251 if (privsep_process != PROC_PARENT)
252 proc_compose(env->sc_ps, PROC_PARENT,
253 IMSG_CFG_DONE, NULL, 0);