2 f0e62b85 2024-02-15 op * Copyright (c) 2024 Omar Polo <op@omarpolo.com>
4 f0e62b85 2024-02-15 op * Permission to use, copy, modify, and distribute this software for any
5 f0e62b85 2024-02-15 op * purpose with or without fee is hereby granted, provided that the above
6 f0e62b85 2024-02-15 op * copyright notice and this permission notice appear in all copies.
8 f0e62b85 2024-02-15 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 f0e62b85 2024-02-15 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 f0e62b85 2024-02-15 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 f0e62b85 2024-02-15 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 f0e62b85 2024-02-15 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 f0e62b85 2024-02-15 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 f0e62b85 2024-02-15 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 f0e62b85 2024-02-15 op #include "compat.h"
19 f0e62b85 2024-02-15 op #include <limits.h>
20 f0e62b85 2024-02-15 op #include <stdio.h>
21 f0e62b85 2024-02-15 op #include <stdlib.h>
22 f0e62b85 2024-02-15 op #include <string.h>
23 f0e62b85 2024-02-15 op #include <tls.h>
24 f0e62b85 2024-02-15 op #include <unistd.h>
26 f0e62b85 2024-02-15 op #include "certs.h"
27 f0e62b85 2024-02-15 op #include "fs.h"
28 f0e62b85 2024-02-15 op #include "parser.h"
29 f0e62b85 2024-02-15 op #include "telescope.h"
31 f0e62b85 2024-02-15 op #ifndef nitems
32 f0e62b85 2024-02-15 op #define nitems(x) (sizeof(x) / sizeof((x)[0]))
37 f0e62b85 2024-02-15 op static int cmd_generate(const struct cmd *, int, char **);
38 f0e62b85 2024-02-15 op static int cmd_remove(const struct cmd *, int, char **);
39 f0e62b85 2024-02-15 op static int cmd_import(const struct cmd *, int, char **);
40 f0e62b85 2024-02-15 op static int cmd_export(const struct cmd *, int, char **);
41 f0e62b85 2024-02-15 op static int cmd_list(const struct cmd *, int, char **);
42 f0e62b85 2024-02-15 op static int cmd_mappings(const struct cmd *, int, char **);
43 f0e62b85 2024-02-15 op static int cmd_use(const struct cmd *, int, char **);
44 f0e62b85 2024-02-15 op static int cmd_forget(const struct cmd *, int, char **);
47 f0e62b85 2024-02-15 op const char *name;
48 f0e62b85 2024-02-15 op int (*fn)(const struct cmd *, int argc, char **argv);
49 f0e62b85 2024-02-15 op const char *usage;
52 f0e62b85 2024-02-15 op static const struct cmd cmds[] = {
53 f0e62b85 2024-02-15 op { "generate", cmd_generate, "[-t type] name" },
54 f0e62b85 2024-02-15 op { "remove", cmd_remove, "name" },
55 f0e62b85 2024-02-15 op { "import", cmd_import, "-C cert [-K key] name" },
56 e1ccda6c 2024-02-16 op { "export", cmd_export, "-C cert name" },
57 f0e62b85 2024-02-15 op { "list", cmd_list, "" },
58 f0e62b85 2024-02-15 op { "mappings", cmd_mappings, "" },
59 f0e62b85 2024-02-15 op { "use", cmd_use, "name host[:port][/path]" },
60 f0e62b85 2024-02-15 op { "forget", cmd_forget, "name host[:port][/path]" },
64 f0e62b85 2024-02-15 op * Provide some symbols so that we can pull in some subsystems without
65 f0e62b85 2024-02-15 op * their the dependencies.
68 f0e62b85 2024-02-15 op const uint8_t *about_about;
69 f0e62b85 2024-02-15 op size_t about_about_len;
70 f0e62b85 2024-02-15 op const uint8_t *about_blank;
71 f0e62b85 2024-02-15 op size_t about_blank_len;
72 f0e62b85 2024-02-15 op const uint8_t *about_crash;
73 f0e62b85 2024-02-15 op size_t about_crash_len;
74 f0e62b85 2024-02-15 op const uint8_t *about_help;
75 f0e62b85 2024-02-15 op size_t about_help_len;
76 f0e62b85 2024-02-15 op const uint8_t *about_license;
77 f0e62b85 2024-02-15 op size_t about_license_len;
78 f0e62b85 2024-02-15 op const uint8_t *about_new;
79 f0e62b85 2024-02-15 op size_t about_new_len;
80 f0e62b85 2024-02-15 op const uint8_t *bookmarks;
81 f0e62b85 2024-02-15 op size_t bookmarks_len;
83 f0e62b85 2024-02-15 op void gemtext_initparser(struct parser *p) { return; }
84 f0e62b85 2024-02-15 op void textpatch_initparser(struct parser *p) { return; }
85 f0e62b85 2024-02-15 op void textplain_initparser(struct parser *p) { return; }
87 f0e62b85 2024-02-15 op void load_page_from_str(struct tab *tab, const char *page) { return; }
88 f0e62b85 2024-02-15 op void erase_buffer(struct buffer *buffer) { return; }
90 f0e62b85 2024-02-15 op static void __dead
95 f0e62b85 2024-02-15 op fprintf(stderr, "usage: %s command [args...]\n", getprogname());
96 f0e62b85 2024-02-15 op fprintf(stderr, "Available subcommands are:");
97 f0e62b85 2024-02-15 op for (i = 0; i < nitems(cmds); ++i)
98 f0e62b85 2024-02-15 op fprintf(stderr, " %s", cmds[i].name);
99 f0e62b85 2024-02-15 op fputs(".\n", stderr);
103 f0e62b85 2024-02-15 op static void __dead
104 f0e62b85 2024-02-15 op cmd_usage(const struct cmd *cmd)
106 f0e62b85 2024-02-15 op fprintf(stderr, "usage: %s %s%s%s\n", getprogname(), cmd->name,
107 f0e62b85 2024-02-15 op *cmd->usage ? " " : "", cmd->usage);
112 f0e62b85 2024-02-15 op main(int argc, char **argv)
114 f0e62b85 2024-02-15 op const struct cmd *cmd;
118 c3465169 2024-02-19 op * Can't use portably getopt() since there's no cross-platform
119 c3465169 2024-02-19 op * way of resetting it.
122 f0e62b85 2024-02-15 op if (argc == 0)
124 c3465169 2024-02-19 op argc--, argv++;
126 c3465169 2024-02-19 op if (argc == 0)
129 c3465169 2024-02-19 op if (!strcmp(*argv, "--"))
130 c3465169 2024-02-19 op argc--, argv++;
131 c3465169 2024-02-19 op else if (**argv == '-')
134 c3465169 2024-02-19 op if (argc == 0)
137 f0e62b85 2024-02-15 op for (i = 0; i < nitems(cmds); ++i) {
138 f0e62b85 2024-02-15 op cmd = &cmds[i];
140 f0e62b85 2024-02-15 op if (strcmp(cmd->name, argv[0]) != 0)
144 f0e62b85 2024-02-15 op if (certs_init(certs_file) == -1)
145 f0e62b85 2024-02-15 op errx(1, "failed to initialize the cert store.");
146 f0e62b85 2024-02-15 op return (cmd->fn(cmd, argc, argv));
149 e1ccda6c 2024-02-16 op warnx("unknown command: %s", argv[0]);
154 f0e62b85 2024-02-15 op cmd_generate(const struct cmd *cmd, int argc, char **argv)
156 f0e62b85 2024-02-15 op const char *name;
157 f0e62b85 2024-02-15 op char path[PATH_MAX];
161 f0e62b85 2024-02-15 op while ((ch = getopt(argc, argv, "t:")) != -1) {
162 f0e62b85 2024-02-15 op switch (ch) {
164 f0e62b85 2024-02-15 op if (!strcasecmp(optarg, "ec")) {
168 f0e62b85 2024-02-15 op if (!strcasecmp(optarg, "rsa")) {
172 f0e62b85 2024-02-15 op errx(1, "Unknown key type requested: %s", optarg);
175 f0e62b85 2024-02-15 op cmd_usage(cmd);
178 f0e62b85 2024-02-15 op argc -= optind;
179 f0e62b85 2024-02-15 op argv += optind;
181 f0e62b85 2024-02-15 op if (argc != 1)
182 f0e62b85 2024-02-15 op cmd_usage(cmd);
184 f0e62b85 2024-02-15 op name = *argv;
186 f0e62b85 2024-02-15 op r = snprintf(path, sizeof(path), "%s%s", cert_dir, name);
187 f0e62b85 2024-02-15 op if (r < 0 || (size_t)r >= sizeof(path))
188 f0e62b85 2024-02-15 op errx(1, "path too long");
190 f0e62b85 2024-02-15 op if (cert_new(name, path, ec) == -1)
191 f0e62b85 2024-02-15 op errx(1, "failure generating the key");
197 f0e62b85 2024-02-15 op cmd_remove(const struct cmd *cmd, int argc, char **argv)
199 f0e62b85 2024-02-15 op const char *name;
200 f0e62b85 2024-02-15 op char path[PATH_MAX];
203 f0e62b85 2024-02-15 op while ((ch = getopt(argc, argv, "")) != -1) {
204 f0e62b85 2024-02-15 op switch (ch) {
206 f0e62b85 2024-02-15 op cmd_usage(cmd);
209 f0e62b85 2024-02-15 op argc -= optind;
210 f0e62b85 2024-02-15 op argv += optind;
212 f0e62b85 2024-02-15 op if (argc != 1)
213 f0e62b85 2024-02-15 op cmd_usage(cmd);
215 f0e62b85 2024-02-15 op name = *argv;
217 f0e62b85 2024-02-15 op r = snprintf(path, sizeof(path), "%s%s", cert_dir, name);
218 f0e62b85 2024-02-15 op if (r < 0 || (size_t)r >= sizeof(path))
219 f0e62b85 2024-02-15 op errx(1, "path too long");
221 f0e62b85 2024-02-15 op if (unlink(path) == -1)
222 f0e62b85 2024-02-15 op err(1, "unlink %s", path);
227 f0e62b85 2024-02-15 op cmd_import(const struct cmd *cmd, int argc, char **argv)
229 f0e62b85 2024-02-15 op struct tls_config *conf;
230 f0e62b85 2024-02-15 op const char *key = NULL, *cert = NULL;
231 f0e62b85 2024-02-15 op char path[PATH_MAX], sfn[PATH_MAX];
233 f0e62b85 2024-02-15 op uint8_t *keym, *certm;
234 f0e62b85 2024-02-15 op size_t keyl, certl;
235 f0e62b85 2024-02-15 op int ch, r, fd;
236 f0e62b85 2024-02-15 op int force = 0;
238 f0e62b85 2024-02-15 op while ((ch = getopt(argc, argv, "C:K:f")) != -1) {
239 f0e62b85 2024-02-15 op switch (ch) {
241 f0e62b85 2024-02-15 op cert = optarg;
244 f0e62b85 2024-02-15 op key = optarg;
250 f0e62b85 2024-02-15 op cmd_usage(cmd);
253 f0e62b85 2024-02-15 op argc -= optind;
254 f0e62b85 2024-02-15 op argv += optind;
256 f0e62b85 2024-02-15 op if (argc != 1)
257 f0e62b85 2024-02-15 op cmd_usage(cmd);
259 f0e62b85 2024-02-15 op if (key == NULL)
261 f0e62b85 2024-02-15 op if (cert == NULL)
262 f0e62b85 2024-02-15 op cmd_usage(cmd);
264 f0e62b85 2024-02-15 op if ((keym = tls_load_file(key, &keyl, NULL)) == NULL)
265 f0e62b85 2024-02-15 op err(1, "can't open %s", key);
266 f0e62b85 2024-02-15 op if ((certm = tls_load_file(cert, &certl, NULL)) == NULL)
267 f0e62b85 2024-02-15 op err(1, "can't open %s", cert);
269 f0e62b85 2024-02-15 op if ((conf = tls_config_new()) == NULL)
270 f0e62b85 2024-02-15 op err(1, "tls_config_new");
272 f0e62b85 2024-02-15 op if (tls_config_set_keypair_mem(conf, certm, certl, keym, keyl) == -1)
273 f0e62b85 2024-02-15 op errx(1, "failed to load the keypair: %s",
274 f0e62b85 2024-02-15 op tls_config_error(conf));
276 f0e62b85 2024-02-15 op tls_config_free(conf);
278 f0e62b85 2024-02-15 op r = snprintf(path, sizeof(path), "%s/%s", cert_dir, *argv);
279 f0e62b85 2024-02-15 op if (r < 0 || (size_t)r >= sizeof(path))
280 f0e62b85 2024-02-15 op err(1, "identity name too long");
282 f0e62b85 2024-02-15 op strlcpy(sfn, cert_dir_tmp, sizeof(sfn));
283 f0e62b85 2024-02-15 op if ((fd = mkstemp(sfn)) == -1 ||
284 f0e62b85 2024-02-15 op (fp = fdopen(fd, "w")) == NULL) {
285 f0e62b85 2024-02-15 op if (fd != -1) {
286 f0e62b85 2024-02-15 op warn("fdopen");
290 f0e62b85 2024-02-15 op warn("mkstamp");
294 f0e62b85 2024-02-15 op if (fwrite(certm, 1, certl, fp) != certl) {
295 f0e62b85 2024-02-15 op warn("fwrite");
300 f0e62b85 2024-02-15 op if (strcmp(key, cert) != 0 &&
301 f0e62b85 2024-02-15 op fwrite(keym, 1, keyl, fp) != keyl) {
302 f0e62b85 2024-02-15 op warn("fwrite");
308 f0e62b85 2024-02-15 op if (fflush(fp) == EOF) {
309 f0e62b85 2024-02-15 op warn("fflush");
316 f0e62b85 2024-02-15 op if (!force && access(path, F_OK) == 0) {
317 f0e62b85 2024-02-15 op warnx("identity %s already exists", *argv);
322 f0e62b85 2024-02-15 op if (rename(sfn, path) == -1) {
323 f0e62b85 2024-02-15 op warn("can't rename");
332 f0e62b85 2024-02-15 op cmd_export(const struct cmd *cmd, int argc, char **argv)
334 f0e62b85 2024-02-15 op FILE *fp, *outfp;
335 f0e62b85 2024-02-15 op const char *cert = NULL;
336 f0e62b85 2024-02-15 op const char *identity = NULL;
337 f0e62b85 2024-02-15 op char path[PATH_MAX];
338 f0e62b85 2024-02-15 op char buf[BUFSIZ];
342 f0e62b85 2024-02-15 op while ((ch = getopt(argc, argv, "C:")) != -1) {
343 f0e62b85 2024-02-15 op switch (ch) {
345 f0e62b85 2024-02-15 op cert = optarg;
348 f0e62b85 2024-02-15 op cmd_usage(cmd);
351 f0e62b85 2024-02-15 op argc -= optind;
352 f0e62b85 2024-02-15 op argv += optind;
353 f0e62b85 2024-02-15 op if (argc != 1)
354 f0e62b85 2024-02-15 op cmd_usage(cmd);
355 f0e62b85 2024-02-15 op identity = argv[0];
357 f0e62b85 2024-02-15 op if (cert == NULL)
358 f0e62b85 2024-02-15 op cmd_usage(cmd);
360 f0e62b85 2024-02-15 op r = snprintf(path, sizeof(path), "%s/%s", cert_dir, identity);
361 f0e62b85 2024-02-15 op if (r < 0 || (size_t)r >= sizeof(path))
362 f0e62b85 2024-02-15 op err(1, "path too long");
363 f0e62b85 2024-02-15 op if ((fp = fopen(path, "r")) == NULL)
364 f0e62b85 2024-02-15 op err(1, "can't open %s", path);
366 f0e62b85 2024-02-15 op if ((outfp = fopen(cert, "w")) == NULL)
367 f0e62b85 2024-02-15 op err(1, "can't open %s", cert);
370 f0e62b85 2024-02-15 op l = fread(buf, 1, sizeof(buf), fp);
373 f0e62b85 2024-02-15 op if (fwrite(buf, 1, l, outfp) != l)
374 f0e62b85 2024-02-15 op err(1, "fwrite");
376 f0e62b85 2024-02-15 op if (ferror(fp))
377 f0e62b85 2024-02-15 op err(1, "fread");
383 f0e62b85 2024-02-15 op cmd_list(const struct cmd *cmd, int argc, char **argv)
388 f0e62b85 2024-02-15 op while ((ch = getopt(argc, argv, "")) != -1) {
389 f0e62b85 2024-02-15 op switch (ch) {
391 f0e62b85 2024-02-15 op cmd_usage(cmd);
394 f0e62b85 2024-02-15 op argc -= optind;
395 f0e62b85 2024-02-15 op argv += optind;
396 f0e62b85 2024-02-15 op if (argc != 0)
397 f0e62b85 2024-02-15 op cmd_usage(cmd);
399 f0e62b85 2024-02-15 op for (id = identities; *id; ++id)
406 f0e62b85 2024-02-15 op cmd_mappings(const struct cmd *cmd, int argc, char **argv)
408 e1ccda6c 2024-02-16 op struct ccert *c;
409 e1ccda6c 2024-02-16 op const char *id = NULL;
410 f0e62b85 2024-02-15 op int ch, defport;
413 f0e62b85 2024-02-15 op while ((ch = getopt(argc, argv, "")) != -1) {
414 f0e62b85 2024-02-15 op switch (ch) {
416 f0e62b85 2024-02-15 op cmd_usage(cmd);
419 f0e62b85 2024-02-15 op argc -= optind;
420 f0e62b85 2024-02-15 op argv += optind;
421 e1ccda6c 2024-02-16 op if (argc == 1) {
422 e1ccda6c 2024-02-16 op if ((id = ccert(*argv)) == NULL)
423 e1ccda6c 2024-02-16 op errx(1, "unknown identity %s", *argv);
424 e1ccda6c 2024-02-16 op argc--, argv++;
426 f0e62b85 2024-02-15 op if (argc != 0)
427 f0e62b85 2024-02-15 op cmd_usage(cmd);
429 f0e62b85 2024-02-15 op for (i = 0; i < cert_store.len; ++i) {
430 e1ccda6c 2024-02-16 op c = &cert_store.certs[i];
432 e1ccda6c 2024-02-16 op if (id && strcmp(id, c->cert) != 0)
435 e1ccda6c 2024-02-16 op defport = !strcmp(c->port, "1965");
437 e1ccda6c 2024-02-16 op printf("%s\t%s%s%s%s\n", c->cert, c->host,
438 e1ccda6c 2024-02-16 op defport ? "" : ":", defport ? "" : c->port,
445 f0e62b85 2024-02-15 op static struct iri *
446 f0e62b85 2024-02-15 op parseiri(char *spec)
448 f0e62b85 2024-02-15 op static struct iri iri;
449 f0e62b85 2024-02-15 op const char *errstr;
450 f0e62b85 2024-02-15 op char *host, *port = NULL, *path = NULL;
452 f0e62b85 2024-02-15 op memset(&iri, 0, sizeof(iri));
456 f0e62b85 2024-02-15 op port = host + strcspn(host, ":/");
457 f0e62b85 2024-02-15 op if (*port == ':') {
458 f0e62b85 2024-02-15 op *port++ = '\0';
459 f0e62b85 2024-02-15 op if ((path = strchr(port, '/')) != NULL)
460 f0e62b85 2024-02-15 op *path++ = '\0';
461 f0e62b85 2024-02-15 op } else if (*port == '/') {
462 f0e62b85 2024-02-15 op *port++ = '\0';
468 f0e62b85 2024-02-15 op strlcpy(iri.iri_host, host, sizeof(iri.iri_host));
469 f0e62b85 2024-02-15 op strlcpy(iri.iri_portstr, port ? port : "1965", sizeof(iri.iri_portstr));
470 f0e62b85 2024-02-15 op strlcpy(iri.iri_path, path ? path : "/", sizeof(iri.iri_path));
472 f0e62b85 2024-02-15 op iri.iri_port = strtonum(iri.iri_portstr, 0, UINT16_MAX, &errstr);
474 f0e62b85 2024-02-15 op err(1, "port number is %s: %s", errstr, iri.iri_portstr);
480 f0e62b85 2024-02-15 op cmd_use(const struct cmd *cmd, int argc, char **argv)
482 f0e62b85 2024-02-15 op char *cert, *spec;
485 f0e62b85 2024-02-15 op while ((ch = getopt(argc, argv, "")) != -1) {
486 f0e62b85 2024-02-15 op switch (ch) {
488 f0e62b85 2024-02-15 op cmd_usage(cmd);
491 f0e62b85 2024-02-15 op argc -= optind;
492 f0e62b85 2024-02-15 op argv += optind;
493 f0e62b85 2024-02-15 op if (argc != 2)
494 f0e62b85 2024-02-15 op cmd_usage(cmd);
496 f0e62b85 2024-02-15 op cert = argv[0];
497 f0e62b85 2024-02-15 op spec = argv[1];
499 f0e62b85 2024-02-15 op if (ccert(cert) == NULL)
500 f0e62b85 2024-02-15 op err(1, "unknown identity %s", cert);
502 f0e62b85 2024-02-15 op if (cert_save_for(cert, parseiri(spec), 1) == -1)
503 f0e62b85 2024-02-15 op errx(1, "failed to save the certificate");
509 f0e62b85 2024-02-15 op cmd_forget(const struct cmd *cmd, int argc, char **argv)
511 f0e62b85 2024-02-15 op char *cert, *spec;
514 f0e62b85 2024-02-15 op while ((ch = getopt(argc, argv, "")) != -1) {
515 f0e62b85 2024-02-15 op switch (ch) {
517 f0e62b85 2024-02-15 op cmd_usage(cmd);
520 f0e62b85 2024-02-15 op argc -= optind;
521 f0e62b85 2024-02-15 op argv += optind;
522 f0e62b85 2024-02-15 op if (argc != 2)
523 f0e62b85 2024-02-15 op cmd_usage(cmd);
525 f0e62b85 2024-02-15 op cert = argv[0];
526 f0e62b85 2024-02-15 op spec = argv[1];
528 f0e62b85 2024-02-15 op if (ccert(cert) == NULL)
529 f0e62b85 2024-02-15 op err(1, "unknown identity %s", cert);
531 f0e62b85 2024-02-15 op if (cert_delete_for(cert, parseiri(spec), 1) == -1)
532 f0e62b85 2024-02-15 op errx(1, "failed to save the certificate");