Blob


1 /*
2 * Copyright (c) 2022 Omar Polo <op@omarpolo.com>
3 * Copyright (c) 2011 - 2015 Reyk Floeter <reyk@openbsd.org>
4 *
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.
8 *
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.
16 */
18 #include <sys/types.h>
19 #include <sys/queue.h>
20 #include <sys/socket.h>
21 #include <sys/un.h>
22 #include <sys/tree.h>
23 #include <sys/time.h>
24 #include <sys/uio.h>
26 #include <sys/stat.h> /* umask */
27 #include <sys/un.h> /* sockaddr_un */
29 #include <errno.h>
30 #include <event.h>
31 #include <limits.h>
32 #include <pwd.h>
33 #include <unistd.h>
34 #include <stdlib.h>
35 #include <stdio.h>
36 #include <string.h>
37 #include <imsg.h>
39 #include "proc.h"
40 #include "log.h"
41 #include "xmalloc.h"
43 #include "galileo.h"
45 int
46 config_init(struct galileo *env)
47 {
48 /* Global configuration */
49 if (privsep_process == PROC_PARENT)
50 env->sc_prefork = PROXY_NUMPROC;
52 /* Other configuration. */
53 TAILQ_INIT(&env->sc_proxies);
55 env->sc_sock_fd = -1;
57 return 0;
58 }
60 void
61 config_purge(struct galileo *env)
62 {
63 struct proxy *p;
65 while ((p = TAILQ_FIRST(&env->sc_proxies)) != NULL) {
66 TAILQ_REMOVE(&env->sc_proxies, p, pr_entry);
67 proxy_purge(p);
68 }
69 }
71 int
72 config_setproxy(struct galileo *env, struct proxy *p)
73 {
74 struct privsep *ps = env->sc_ps;
76 if (proc_compose(ps, PROC_PROXY, IMSG_CFG_SRV, p, sizeof(*p)) == -1)
77 fatal("proc_compose");
78 return 0;
79 }
81 int
82 config_getproxy(struct galileo *env, struct imsg *imsg)
83 {
84 struct proxy *proxy;
86 proxy = xcalloc(1, sizeof(*proxy));
87 if (IMSG_DATA_SIZE(imsg) != sizeof(*proxy))
88 fatalx("%s: bad imsg size", __func__);
90 memcpy(proxy, imsg->data, sizeof(*proxy));
92 log_debug("%s: proxy=%s -> %s:%s (%s)", __func__,
93 proxy->pr_conf.host, proxy->pr_conf.proxy_addr,
94 proxy->pr_conf.proxy_port, proxy->pr_conf.proxy_name);
96 TAILQ_INSERT_TAIL(&env->sc_proxies, proxy, pr_entry);
98 return 0;
99 }
101 int
102 config_setsock(struct galileo *env)
104 struct privsep *ps = env->sc_ps;
105 struct passwd *pw = ps->ps_pw;
106 struct sockaddr_un sun;
107 const char *path = GALILEO_SOCK;
108 int id, fd, old_umask;
110 /*
111 * open listening socket.
113 * XXX: move to server.c as server_privinit like httpd once we
114 * support more than one listening socket.
115 */
116 if ((fd = socket(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK, 0)) == -1) {
117 log_warn("%s: socket", __func__);
118 return (-1);
121 memset(&sun, 0, sizeof(sun));
122 sun.sun_family = AF_UNIX;
123 strlcpy(sun.sun_path, path, sizeof(sun.sun_path));
125 if (unlink(path) == -1)
126 if (errno != ENOENT) {
127 log_warn("%s: unlink %s", __func__, path);
128 close(fd);
129 return (-1);
132 old_umask = umask(S_IXUSR|S_IXGRP|S_IWOTH|S_IROTH|S_IXOTH);
133 if (bind(fd, (struct sockaddr *)&sun, sizeof(sun)) == -1) {
134 log_warn("%s: bind: %s (%d)", __func__, path, geteuid());
135 close(fd);
136 umask(old_umask);
137 return (-1);
139 umask(old_umask);
141 if (chmod(path, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP) == -1) {
142 log_warn("%s: chmod", __func__);
143 close(fd);
144 (void)unlink(path);
145 return (-1);
148 if (chown(path, pw->pw_uid, pw->pw_gid) == -1) {
149 log_warn("%s: chown", __func__);
150 close(fd);
151 (void)unlink(path);
152 return (-1);
155 if (listen(fd, 5) == -1) {
156 log_warn("%s: listen", __func__);
157 close(fd);
158 (void)unlink(path);
159 return (-1);
162 for (id = 0; id < PROC_MAX; ++id) {
163 int n, m;
165 if (id == privsep_process || id != PROC_PROXY)
166 continue;
168 n = -1;
169 proc_range(ps, id, &n, &m);
170 for (n = 0; n < m; ++n) {
171 int d;
173 if ((d = dup(fd)) == -1) {
174 log_warn("%s: dup", __func__);
175 close(fd);
176 return (-1);
179 if (proc_compose_imsg(ps, id, n, IMSG_CFG_SOCK,
180 -1, d, NULL, 0) == -1) {
181 log_warn("%s: failed to compose "
182 "IMSG_CFG_SOCK", __func__);
183 close(fd);
184 return (-1);
186 if (proc_flush_imsg(ps, id, n) == -1) {
187 log_warn("%s: failed to flush", __func__);
188 close(fd);
189 return (-1);
194 close(fd);
195 return (0);
198 int
199 config_getsock(struct galileo *env, struct imsg *imsg)
201 /* XXX: make it more like httpd/gotwebd' one */
202 return imsg->fd;
205 int
206 config_setreset(struct galileo *env)
208 struct privsep *ps = env->sc_ps;
209 int id;
211 for (id = 0; id < PROC_MAX; ++id)
212 proc_compose(ps, id, IMSG_CTL_RESET, NULL, 0);
214 return (0);
217 int
218 config_getreset(struct galileo *env, struct imsg *imsg)
220 config_purge(env);
222 return (0);