Blame


1 0126d91d 2022-09-07 op /*
2 0126d91d 2022-09-07 op * Copyright (c) 2022 Omar Polo <op@omarpolo.com>
3 0126d91d 2022-09-07 op *
4 0126d91d 2022-09-07 op * Permission to use, copy, modify, and distribute this software for any
5 0126d91d 2022-09-07 op * purpose with or without fee is hereby granted, provided that the above
6 0126d91d 2022-09-07 op * copyright notice and this permission notice appear in all copies.
7 0126d91d 2022-09-07 op *
8 0126d91d 2022-09-07 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 0126d91d 2022-09-07 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 0126d91d 2022-09-07 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 0126d91d 2022-09-07 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 0126d91d 2022-09-07 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 0126d91d 2022-09-07 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 0126d91d 2022-09-07 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 0126d91d 2022-09-07 op */
16 0126d91d 2022-09-07 op
17 0126d91d 2022-09-07 op #include "gmid.h"
18 0126d91d 2022-09-07 op
19 0126d91d 2022-09-07 op #include <sys/stat.h>
20 0126d91d 2022-09-07 op #include <sys/wait.h>
21 0126d91d 2022-09-07 op
22 0126d91d 2022-09-07 op #include <errno.h>
23 0126d91d 2022-09-07 op #include <fcntl.h>
24 0126d91d 2022-09-07 op #include <getopt.h>
25 0046c1fe 2023-06-06 op #include <locale.h>
26 0126d91d 2022-09-07 op #include <libgen.h>
27 0126d91d 2022-09-07 op #include <signal.h>
28 0126d91d 2022-09-07 op #include <string.h>
29 eae52ad4 2023-06-06 op #include <syslog.h>
30 0126d91d 2022-09-07 op #include <unistd.h>
31 0126d91d 2022-09-07 op
32 281a8852 2023-06-06 op #include "logger.h"
33 eae52ad4 2023-06-06 op #include "log.h"
34 df5058c9 2023-06-05 op
35 0126d91d 2022-09-07 op struct imsgbuf ibuf, logibuf;
36 0126d91d 2022-09-07 op struct conf conf;
37 0126d91d 2022-09-07 op
38 0126d91d 2022-09-07 op struct fcgi fcgi[FCGI_MAX]; /* just because it's referenced */
39 070b3295 2023-06-05 op struct vhosthead hosts = TAILQ_HEAD_INITIALIZER(hosts);
40 0126d91d 2022-09-07 op
41 0126d91d 2022-09-07 op static const struct option opts[] = {
42 0126d91d 2022-09-07 op {"help", no_argument, NULL, 'h'},
43 0126d91d 2022-09-07 op {"version", no_argument, NULL, 'V'},
44 0126d91d 2022-09-07 op {NULL, 0, NULL, 0},
45 0126d91d 2022-09-07 op };
46 0126d91d 2022-09-07 op
47 0126d91d 2022-09-07 op void
48 a01a91db 2023-06-05 op drop_priv(void)
49 a01a91db 2023-06-05 op {
50 a01a91db 2023-06-05 op return;
51 a01a91db 2023-06-05 op }
52 a01a91db 2023-06-05 op
53 a01a91db 2023-06-05 op void
54 0126d91d 2022-09-07 op load_local_cert(struct vhost *h, const char *hostname, const char *dir)
55 0126d91d 2022-09-07 op {
56 0126d91d 2022-09-07 op char *cert, *key;
57 0126d91d 2022-09-07 op
58 0126d91d 2022-09-07 op if (asprintf(&cert, "%s/%s.cert.pem", dir, hostname) == -1)
59 2dd5994a 2023-06-06 op fatal("asprintf");
60 0126d91d 2022-09-07 op if (asprintf(&key, "%s/%s.key.pem", dir, hostname) == -1)
61 2dd5994a 2023-06-06 op fatal("asprintf");
62 0126d91d 2022-09-07 op
63 0126d91d 2022-09-07 op if (access(cert, R_OK) == -1 || access(key, R_OK) == -1)
64 0126d91d 2022-09-07 op gen_certificate(hostname, cert, key);
65 0126d91d 2022-09-07 op
66 534afd0d 2022-10-05 op strlcpy(h->cert, cert, sizeof(h->cert));
67 534afd0d 2022-10-05 op strlcpy(h->key, key, sizeof(h->key));
68 534afd0d 2022-10-05 op strlcpy(h->domain, hostname, sizeof(h->domain));
69 0126d91d 2022-09-07 op }
70 0126d91d 2022-09-07 op
71 0126d91d 2022-09-07 op /* wrapper around dirname(3). dn must be PATH_MAX+1 at least. */
72 0126d91d 2022-09-07 op static void
73 0126d91d 2022-09-07 op pdirname(const char *path, char *dn)
74 0126d91d 2022-09-07 op {
75 0126d91d 2022-09-07 op char p[PATH_MAX+1];
76 0126d91d 2022-09-07 op char *t;
77 0126d91d 2022-09-07 op
78 0126d91d 2022-09-07 op strlcpy(p, path, sizeof(p));
79 0126d91d 2022-09-07 op t = dirname(p);
80 0126d91d 2022-09-07 op memmove(dn, t, strlen(t)+1);
81 0126d91d 2022-09-07 op }
82 0126d91d 2022-09-07 op
83 0126d91d 2022-09-07 op static void
84 0126d91d 2022-09-07 op mkdirs(const char *path, mode_t mode)
85 0126d91d 2022-09-07 op {
86 0126d91d 2022-09-07 op char dname[PATH_MAX+1];
87 0126d91d 2022-09-07 op
88 0126d91d 2022-09-07 op pdirname(path, dname);
89 0126d91d 2022-09-07 op if (!strcmp(dname, "/"))
90 0126d91d 2022-09-07 op return;
91 0126d91d 2022-09-07 op mkdirs(dname, mode);
92 0126d91d 2022-09-07 op if (mkdir(path, mode) != 0 && errno != EEXIST)
93 df5058c9 2023-06-05 op fatal("can't mkdir %s", path);
94 0126d91d 2022-09-07 op }
95 0126d91d 2022-09-07 op
96 0126d91d 2022-09-07 op /* $XDG_DATA_HOME/gmid */
97 0126d91d 2022-09-07 op char *
98 0126d91d 2022-09-07 op data_dir(void)
99 0126d91d 2022-09-07 op {
100 0126d91d 2022-09-07 op const char *home, *xdg;
101 0126d91d 2022-09-07 op char *t;
102 0126d91d 2022-09-07 op
103 0126d91d 2022-09-07 op if ((xdg = getenv("XDG_DATA_HOME")) == NULL) {
104 0126d91d 2022-09-07 op if ((home = getenv("HOME")) == NULL)
105 2dd5994a 2023-06-06 op fatalx("XDG_DATA_HOME and HOME both empty");
106 0126d91d 2022-09-07 op if (asprintf(&t, "%s/.local/share/gmid", home) == -1)
107 2dd5994a 2023-06-06 op fatalx("asprintf");
108 0126d91d 2022-09-07 op } else {
109 0126d91d 2022-09-07 op if (asprintf(&t, "%s/gmid", xdg) == -1)
110 2dd5994a 2023-06-06 op fatal("asprintf");
111 0126d91d 2022-09-07 op }
112 0126d91d 2022-09-07 op
113 0126d91d 2022-09-07 op mkdirs(t, 0755);
114 0126d91d 2022-09-07 op return t;
115 0126d91d 2022-09-07 op }
116 0126d91d 2022-09-07 op
117 0126d91d 2022-09-07 op static void
118 0126d91d 2022-09-07 op logger_init(void)
119 0126d91d 2022-09-07 op {
120 0126d91d 2022-09-07 op int p[2];
121 0126d91d 2022-09-07 op
122 0126d91d 2022-09-07 op if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, p) == -1)
123 2dd5994a 2023-06-06 op fatal("socketpair");
124 0126d91d 2022-09-07 op
125 0126d91d 2022-09-07 op switch (fork()) {
126 0126d91d 2022-09-07 op case -1:
127 2dd5994a 2023-06-06 op fatal("fork");
128 0126d91d 2022-09-07 op case 0:
129 0126d91d 2022-09-07 op close(p[0]);
130 0126d91d 2022-09-07 op setproctitle("logger");
131 0126d91d 2022-09-07 op imsg_init(&logibuf, p[1]);
132 0126d91d 2022-09-07 op _exit(logger_main(p[1], &logibuf));
133 0126d91d 2022-09-07 op default:
134 0126d91d 2022-09-07 op close(p[1]);
135 0126d91d 2022-09-07 op imsg_init(&logibuf, p[0]);
136 0126d91d 2022-09-07 op return;
137 0126d91d 2022-09-07 op }
138 0126d91d 2022-09-07 op }
139 0126d91d 2022-09-07 op
140 0126d91d 2022-09-07 op static int
141 070b3295 2023-06-05 op serve(const char *host, int port, const char *dir)
142 0126d91d 2022-09-07 op {
143 0126d91d 2022-09-07 op struct addrinfo hints, *res, *res0;
144 4267093e 2023-06-06 op int r, error, saved_errno, sock = -1;
145 0126d91d 2022-09-07 op const char *cause = NULL;
146 0126d91d 2022-09-07 op char service[32];
147 0126d91d 2022-09-07 op
148 4267093e 2023-06-06 op r = snprintf(service, sizeof(service), "%d", port);
149 4267093e 2023-06-06 op if (r < 0 || (size_t)r >= sizeof(service))
150 0126d91d 2022-09-07 op fatal("snprintf");
151 0126d91d 2022-09-07 op
152 0126d91d 2022-09-07 op memset(&hints, 0, sizeof(hints));
153 0126d91d 2022-09-07 op hints.ai_family = AF_UNSPEC;
154 0126d91d 2022-09-07 op hints.ai_socktype = SOCK_STREAM;
155 0126d91d 2022-09-07 op hints.ai_flags = AI_PASSIVE;
156 0126d91d 2022-09-07 op error = getaddrinfo(host, service, &hints, &res0);
157 0126d91d 2022-09-07 op if (error)
158 df5058c9 2023-06-05 op fatalx("%s", gai_strerror(error));
159 0126d91d 2022-09-07 op for (res = res0; res; res = res->ai_next) {
160 0126d91d 2022-09-07 op sock = socket(res->ai_family, res->ai_socktype,
161 0126d91d 2022-09-07 op res->ai_protocol);
162 0126d91d 2022-09-07 op if (sock == -1) {
163 0126d91d 2022-09-07 op cause = "socket";
164 0126d91d 2022-09-07 op continue;
165 0126d91d 2022-09-07 op }
166 0126d91d 2022-09-07 op
167 0126d91d 2022-09-07 op if (bind(sock, res->ai_addr, res->ai_addrlen) == -1) {
168 0126d91d 2022-09-07 op cause = "bind";
169 0126d91d 2022-09-07 op saved_errno = errno;
170 0126d91d 2022-09-07 op close(sock);
171 0126d91d 2022-09-07 op errno = saved_errno;
172 0126d91d 2022-09-07 op continue;
173 0126d91d 2022-09-07 op }
174 0126d91d 2022-09-07 op
175 0126d91d 2022-09-07 op if (listen(sock, 5) == -1)
176 0126d91d 2022-09-07 op fatal("listen");
177 0126d91d 2022-09-07 op
178 0126d91d 2022-09-07 op /*
179 0126d91d 2022-09-07 op * for the time being, we're happy as soon as
180 0126d91d 2022-09-07 op * something binds.
181 0126d91d 2022-09-07 op */
182 0126d91d 2022-09-07 op break;
183 0126d91d 2022-09-07 op }
184 0126d91d 2022-09-07 op
185 0126d91d 2022-09-07 op if (sock == -1)
186 0126d91d 2022-09-07 op fatal("%s", cause);
187 0126d91d 2022-09-07 op freeaddrinfo(res0);
188 0126d91d 2022-09-07 op
189 eae52ad4 2023-06-06 op log_info("serving %s on port %d", dir, port);
190 070b3295 2023-06-05 op return server_main(NULL, sock, -1);
191 0126d91d 2022-09-07 op }
192 0126d91d 2022-09-07 op
193 0126d91d 2022-09-07 op static __dead void
194 0126d91d 2022-09-07 op usage(void)
195 0126d91d 2022-09-07 op {
196 0126d91d 2022-09-07 op fprintf(stderr,
197 c5b4db93 2022-09-10 op "Version: " GE_STRING "\n"
198 0126d91d 2022-09-07 op "Usage: %s [-hVv] [-d certs-dir] [-H hostname] [-p port] [dir]\n",
199 0126d91d 2022-09-07 op getprogname());
200 0126d91d 2022-09-07 op exit(1);
201 0126d91d 2022-09-07 op }
202 0126d91d 2022-09-07 op
203 0126d91d 2022-09-07 op int
204 0126d91d 2022-09-07 op main(int argc, char **argv)
205 0126d91d 2022-09-07 op {
206 0126d91d 2022-09-07 op struct vhost *host;
207 0126d91d 2022-09-07 op struct location *loc;
208 0126d91d 2022-09-07 op const char *errstr, *certs_dir = NULL, *hostname = "localhost";
209 0126d91d 2022-09-07 op char path[PATH_MAX];
210 0126d91d 2022-09-07 op int ch;
211 0126d91d 2022-09-07 op
212 0046c1fe 2023-06-06 op setlocale(LC_CTYPE, "");
213 0046c1fe 2023-06-06 op
214 eae52ad4 2023-06-06 op log_init(1, LOG_DAEMON);
215 eae52ad4 2023-06-06 op log_setverbose(0);
216 0126d91d 2022-09-07 op logger_init();
217 c68baad2 2023-06-06 op config_init();
218 0126d91d 2022-09-07 op
219 0126d91d 2022-09-07 op while ((ch = getopt_long(argc, argv, "d:H:hp:Vv", opts, NULL)) != -1) {
220 0126d91d 2022-09-07 op switch (ch) {
221 0126d91d 2022-09-07 op case 'd':
222 0126d91d 2022-09-07 op certs_dir = optarg;
223 0126d91d 2022-09-07 op break;
224 0126d91d 2022-09-07 op case 'H':
225 0126d91d 2022-09-07 op hostname = optarg;
226 0126d91d 2022-09-07 op break;
227 0126d91d 2022-09-07 op case 'h':
228 0126d91d 2022-09-07 op usage();
229 0126d91d 2022-09-07 op break;
230 0126d91d 2022-09-07 op case 'p':
231 0126d91d 2022-09-07 op conf.port = strtonum(optarg, 0, UINT16_MAX, &errstr);
232 0126d91d 2022-09-07 op if (errstr)
233 df5058c9 2023-06-05 op fatalx("port number is %s: %s", errstr,
234 df5058c9 2023-06-05 op optarg);
235 0126d91d 2022-09-07 op break;
236 0126d91d 2022-09-07 op case 'V':
237 c5b4db93 2022-09-10 op puts("Version: " GE_STRING);
238 0126d91d 2022-09-07 op return 0;
239 0126d91d 2022-09-07 op default:
240 0126d91d 2022-09-07 op usage();
241 0126d91d 2022-09-07 op break;
242 0126d91d 2022-09-07 op }
243 0126d91d 2022-09-07 op }
244 0126d91d 2022-09-07 op argc -= optind;
245 0126d91d 2022-09-07 op argv += optind;
246 0126d91d 2022-09-07 op
247 0126d91d 2022-09-07 op if (argc > 1)
248 0126d91d 2022-09-07 op usage();
249 0126d91d 2022-09-07 op
250 0126d91d 2022-09-07 op /* prepare the configuration */
251 0126d91d 2022-09-07 op conf.verbose = 1;
252 0126d91d 2022-09-07 op init_mime(&conf.mime);
253 0126d91d 2022-09-07 op
254 0126d91d 2022-09-07 op if (certs_dir == NULL)
255 0126d91d 2022-09-07 op certs_dir = data_dir();
256 0126d91d 2022-09-07 op
257 0126d91d 2022-09-07 op /* set up the implicit vhost and location */
258 0126d91d 2022-09-07 op host = xcalloc(1, sizeof(*host));
259 0126d91d 2022-09-07 op TAILQ_INSERT_HEAD(&hosts, host, vhosts);
260 0126d91d 2022-09-07 op
261 0126d91d 2022-09-07 op loc = xcalloc(1, sizeof(*loc));
262 0126d91d 2022-09-07 op loc->fcgi = -1;
263 0126d91d 2022-09-07 op TAILQ_INSERT_HEAD(&host->locations, loc, locations);
264 0126d91d 2022-09-07 op
265 0126d91d 2022-09-07 op load_local_cert(host, hostname, certs_dir);
266 0126d91d 2022-09-07 op
267 534afd0d 2022-10-05 op strlcpy(host->domain, "*", sizeof(host->domain));
268 0126d91d 2022-09-07 op loc->auto_index = 1;
269 534afd0d 2022-10-05 op strlcpy(loc->match, "*", sizeof(loc->match));
270 0126d91d 2022-09-07 op
271 0126d91d 2022-09-07 op if (*argv == NULL) {
272 0126d91d 2022-09-07 op if (getcwd(path, sizeof(path)) == NULL)
273 0126d91d 2022-09-07 op fatal("getcwd");
274 534afd0d 2022-10-05 op strlcpy(loc->dir, path, sizeof(loc->dir));
275 534afd0d 2022-10-05 op } else {
276 534afd0d 2022-10-05 op char *tmp;
277 0126d91d 2022-09-07 op
278 534afd0d 2022-10-05 op tmp = absolutify_path(*argv);
279 534afd0d 2022-10-05 op strlcpy(loc->dir, tmp, sizeof(loc->dir));
280 534afd0d 2022-10-05 op free(tmp);
281 534afd0d 2022-10-05 op }
282 534afd0d 2022-10-05 op
283 0126d91d 2022-09-07 op /* start the server */
284 0126d91d 2022-09-07 op signal(SIGPIPE, SIG_IGN);
285 0126d91d 2022-09-07 op setproctitle("%s", loc->dir);
286 070b3295 2023-06-05 op return serve(hostname, conf.port, loc->dir);
287 0126d91d 2022-09-07 op }