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_servers);
55 env->sc_sock_fd = -1;
57 return 0;
58 }
60 void
61 config_purge(struct galileo *env)
62 {
63 struct server *srv;
65 while ((srv = TAILQ_FIRST(&env->sc_servers)) != NULL) {
66 TAILQ_REMOVE(&env->sc_servers, srv, srv_entry);
67 proxy_purge(srv);
68 }
69 }
71 int
72 config_setserver(struct galileo *env, struct server *srv)
73 {
74 struct privsep *ps = env->sc_ps;
76 if (proc_compose(ps, PROC_PROXY, IMSG_CFG_SRV, srv, sizeof(*srv))
77 == -1)
78 fatal("proc_compose");
79 return 0;
80 }
82 int
83 config_getserver(struct galileo *env, struct imsg *imsg)
84 {
85 struct server *srv;
87 srv = xcalloc(1, sizeof(*srv));
88 if (IMSG_DATA_SIZE(imsg) != sizeof(*srv))
89 fatalx("%s: bad imsg size", __func__);
91 memcpy(srv, imsg->data, sizeof(*srv));
93 log_debug("%s: server=%s proxy-to=%s:%d (%s)", __func__,
94 srv->srv_conf.host, srv->srv_conf.proxy_addr,
95 srv->srv_conf.proxy_port, srv->srv_conf.proxy_name);
97 TAILQ_INSERT_TAIL(&env->sc_servers, srv, srv_entry);
99 return 0;
102 int
103 config_setsock(struct galileo *env)
105 struct privsep *ps = env->sc_ps;
106 struct passwd *pw = ps->ps_pw;
107 struct sockaddr_un sun;
108 const char *path = GALILEO_SOCK;
109 int id, fd, old_umask;
111 /*
112 * open listening socket.
114 * XXX: move to server.c as server_privinit like httpd once we
115 * support more than one listening socket.
116 */
117 if ((fd = socket(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK, 0)) == -1) {
118 log_warn("%s: socket", __func__);
119 return (-1);
122 memset(&sun, 0, sizeof(sun));
123 sun.sun_family = AF_UNIX;
124 strlcpy(sun.sun_path, path, sizeof(sun.sun_path));
126 if (unlink(path) == -1)
127 if (errno != ENOENT) {
128 log_warn("%s: unlink %s", __func__, path);
129 close(fd);
130 return (-1);
133 old_umask = umask(S_IXUSR|S_IXGRP|S_IWOTH|S_IROTH|S_IXOTH);
134 if (bind(fd, (struct sockaddr *)&sun, sizeof(sun)) == -1) {
135 log_warn("%s: bind: %s (%d)", __func__, path, geteuid());
136 close(fd);
137 umask(old_umask);
138 return (-1);
140 umask(old_umask);
142 if (chmod(path, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP) == -1) {
143 log_warn("%s: chmod", __func__);
144 close(fd);
145 (void)unlink(path);
146 return (-1);
149 if (chown(path, pw->pw_uid, pw->pw_gid) == -1) {
150 log_warn("%s: chown", __func__);
151 close(fd);
152 (void)unlink(path);
153 return (-1);
156 if (listen(fd, 5) == -1) {
157 log_warn("%s: listen", __func__);
158 close(fd);
159 (void)unlink(path);
160 return (-1);
163 for (id = 0; id < PROC_MAX; ++id) {
164 int n, m;
166 if (id == privsep_process || id != PROC_PROXY)
167 continue;
169 n = -1;
170 proc_range(ps, id, &n, &m);
171 for (n = 0; n < m; ++n) {
172 int d;
174 if ((d = dup(fd)) == -1) {
175 log_warn("%s: dup", __func__);
176 close(fd);
177 return (-1);
180 if (proc_compose_imsg(ps, id, n, IMSG_CFG_SOCK,
181 -1, d, NULL, 0) == -1) {
182 log_warn("%s: failed to compose "
183 "IMSG_CFG_SOCK", __func__);
184 close(fd);
185 return (-1);
187 if (proc_flush_imsg(ps, id, n) == -1) {
188 log_warn("%s: failed to flush", __func__);
189 close(fd);
190 return (-1);
195 /* close(fd); */
196 return (0);
199 int
200 config_getsock(struct galileo *env, struct imsg *imsg)
202 /* XXX: make it more like httpd/gotwebd' one */
203 return imsg->fd;
206 int
207 config_setreset(struct galileo *env)
209 struct privsep *ps = env->sc_ps;
210 int id;
212 for (id = 0; id < PROC_MAX; ++id)
213 proc_compose(ps, id, IMSG_CTL_RESET, NULL, 0);
215 return (0);
218 int
219 config_getreset(struct galileo *env, struct imsg *imsg)
221 config_purge(env);
223 return (0);