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 fd1c80ce 2024-06-14 op const struct parser gemtext_parser, textplain_parser, textpatch_parser;
85 f0e62b85 2024-02-15 op void load_page_from_str(struct tab *tab, const char *page) { return; }
86 f0e62b85 2024-02-15 op void erase_buffer(struct buffer *buffer) { return; }
88 f0e62b85 2024-02-15 op static void __dead
93 f0e62b85 2024-02-15 op fprintf(stderr, "usage: %s command [args...]\n", getprogname());
94 f0e62b85 2024-02-15 op fprintf(stderr, "Available subcommands are:");
95 f0e62b85 2024-02-15 op for (i = 0; i < nitems(cmds); ++i)
96 f0e62b85 2024-02-15 op fprintf(stderr, " %s", cmds[i].name);
97 f0e62b85 2024-02-15 op fputs(".\n", stderr);
101 f0e62b85 2024-02-15 op static void __dead
102 f0e62b85 2024-02-15 op cmd_usage(const struct cmd *cmd)
104 f0e62b85 2024-02-15 op fprintf(stderr, "usage: %s %s%s%s\n", getprogname(), cmd->name,
105 f0e62b85 2024-02-15 op *cmd->usage ? " " : "", cmd->usage);
110 f0e62b85 2024-02-15 op main(int argc, char **argv)
112 f0e62b85 2024-02-15 op const struct cmd *cmd;
116 c3465169 2024-02-19 op * Can't use portably getopt() since there's no cross-platform
117 c3465169 2024-02-19 op * way of resetting it.
120 f0e62b85 2024-02-15 op if (argc == 0)
122 c3465169 2024-02-19 op argc--, argv++;
124 c3465169 2024-02-19 op if (argc == 0)
127 c3465169 2024-02-19 op if (!strcmp(*argv, "--"))
128 c3465169 2024-02-19 op argc--, argv++;
129 c3465169 2024-02-19 op else if (**argv == '-')
132 c3465169 2024-02-19 op if (argc == 0)
135 f0e62b85 2024-02-15 op for (i = 0; i < nitems(cmds); ++i) {
136 f0e62b85 2024-02-15 op cmd = &cmds[i];
138 f0e62b85 2024-02-15 op if (strcmp(cmd->name, argv[0]) != 0)
142 f0e62b85 2024-02-15 op if (certs_init(certs_file) == -1)
143 f0e62b85 2024-02-15 op errx(1, "failed to initialize the cert store.");
144 f0e62b85 2024-02-15 op return (cmd->fn(cmd, argc, argv));
147 e1ccda6c 2024-02-16 op warnx("unknown command: %s", argv[0]);
152 f0e62b85 2024-02-15 op cmd_generate(const struct cmd *cmd, int argc, char **argv)
154 f0e62b85 2024-02-15 op const char *name;
155 f0e62b85 2024-02-15 op char path[PATH_MAX];
159 f0e62b85 2024-02-15 op while ((ch = getopt(argc, argv, "t:")) != -1) {
160 f0e62b85 2024-02-15 op switch (ch) {
162 f0e62b85 2024-02-15 op if (!strcasecmp(optarg, "ec")) {
166 f0e62b85 2024-02-15 op if (!strcasecmp(optarg, "rsa")) {
170 f0e62b85 2024-02-15 op errx(1, "Unknown key type requested: %s", optarg);
173 f0e62b85 2024-02-15 op cmd_usage(cmd);
176 f0e62b85 2024-02-15 op argc -= optind;
177 f0e62b85 2024-02-15 op argv += optind;
179 f0e62b85 2024-02-15 op if (argc != 1)
180 f0e62b85 2024-02-15 op cmd_usage(cmd);
182 f0e62b85 2024-02-15 op name = *argv;
184 f0e62b85 2024-02-15 op r = snprintf(path, sizeof(path), "%s%s", cert_dir, name);
185 f0e62b85 2024-02-15 op if (r < 0 || (size_t)r >= sizeof(path))
186 f0e62b85 2024-02-15 op errx(1, "path too long");
188 f0e62b85 2024-02-15 op if (cert_new(name, path, ec) == -1)
189 f0e62b85 2024-02-15 op errx(1, "failure generating the key");
195 f0e62b85 2024-02-15 op cmd_remove(const struct cmd *cmd, int argc, char **argv)
197 f0e62b85 2024-02-15 op const char *name;
198 f0e62b85 2024-02-15 op char path[PATH_MAX];
201 f0e62b85 2024-02-15 op while ((ch = getopt(argc, argv, "")) != -1) {
202 f0e62b85 2024-02-15 op switch (ch) {
204 f0e62b85 2024-02-15 op cmd_usage(cmd);
207 f0e62b85 2024-02-15 op argc -= optind;
208 f0e62b85 2024-02-15 op argv += optind;
210 f0e62b85 2024-02-15 op if (argc != 1)
211 f0e62b85 2024-02-15 op cmd_usage(cmd);
213 f0e62b85 2024-02-15 op name = *argv;
215 f0e62b85 2024-02-15 op r = snprintf(path, sizeof(path), "%s%s", cert_dir, name);
216 f0e62b85 2024-02-15 op if (r < 0 || (size_t)r >= sizeof(path))
217 f0e62b85 2024-02-15 op errx(1, "path too long");
219 f0e62b85 2024-02-15 op if (unlink(path) == -1)
220 f0e62b85 2024-02-15 op err(1, "unlink %s", path);
225 f0e62b85 2024-02-15 op cmd_import(const struct cmd *cmd, int argc, char **argv)
227 f0e62b85 2024-02-15 op struct tls_config *conf;
228 f0e62b85 2024-02-15 op const char *key = NULL, *cert = NULL;
229 f0e62b85 2024-02-15 op char path[PATH_MAX], sfn[PATH_MAX];
231 f0e62b85 2024-02-15 op uint8_t *keym, *certm;
232 f0e62b85 2024-02-15 op size_t keyl, certl;
233 f0e62b85 2024-02-15 op int ch, r, fd;
234 f0e62b85 2024-02-15 op int force = 0;
236 f0e62b85 2024-02-15 op while ((ch = getopt(argc, argv, "C:K:f")) != -1) {
237 f0e62b85 2024-02-15 op switch (ch) {
239 f0e62b85 2024-02-15 op cert = optarg;
242 f0e62b85 2024-02-15 op key = optarg;
248 f0e62b85 2024-02-15 op cmd_usage(cmd);
251 f0e62b85 2024-02-15 op argc -= optind;
252 f0e62b85 2024-02-15 op argv += optind;
254 f0e62b85 2024-02-15 op if (argc != 1)
255 f0e62b85 2024-02-15 op cmd_usage(cmd);
257 f0e62b85 2024-02-15 op if (key == NULL)
259 f0e62b85 2024-02-15 op if (cert == NULL)
260 f0e62b85 2024-02-15 op cmd_usage(cmd);
262 f0e62b85 2024-02-15 op if ((keym = tls_load_file(key, &keyl, NULL)) == NULL)
263 f0e62b85 2024-02-15 op err(1, "can't open %s", key);
264 f0e62b85 2024-02-15 op if ((certm = tls_load_file(cert, &certl, NULL)) == NULL)
265 f0e62b85 2024-02-15 op err(1, "can't open %s", cert);
267 f0e62b85 2024-02-15 op if ((conf = tls_config_new()) == NULL)
268 f0e62b85 2024-02-15 op err(1, "tls_config_new");
270 f0e62b85 2024-02-15 op if (tls_config_set_keypair_mem(conf, certm, certl, keym, keyl) == -1)
271 f0e62b85 2024-02-15 op errx(1, "failed to load the keypair: %s",
272 f0e62b85 2024-02-15 op tls_config_error(conf));
274 f0e62b85 2024-02-15 op tls_config_free(conf);
276 f0e62b85 2024-02-15 op r = snprintf(path, sizeof(path), "%s/%s", cert_dir, *argv);
277 f0e62b85 2024-02-15 op if (r < 0 || (size_t)r >= sizeof(path))
278 f0e62b85 2024-02-15 op err(1, "identity name too long");
280 f0e62b85 2024-02-15 op strlcpy(sfn, cert_dir_tmp, sizeof(sfn));
281 f0e62b85 2024-02-15 op if ((fd = mkstemp(sfn)) == -1 ||
282 f0e62b85 2024-02-15 op (fp = fdopen(fd, "w")) == NULL) {
283 f0e62b85 2024-02-15 op if (fd != -1) {
284 f0e62b85 2024-02-15 op warn("fdopen");
288 f0e62b85 2024-02-15 op warn("mkstamp");
292 f0e62b85 2024-02-15 op if (fwrite(certm, 1, certl, fp) != certl) {
293 f0e62b85 2024-02-15 op warn("fwrite");
298 f0e62b85 2024-02-15 op if (strcmp(key, cert) != 0 &&
299 f0e62b85 2024-02-15 op fwrite(keym, 1, keyl, fp) != keyl) {
300 f0e62b85 2024-02-15 op warn("fwrite");
306 f0e62b85 2024-02-15 op if (fflush(fp) == EOF) {
307 f0e62b85 2024-02-15 op warn("fflush");
314 f0e62b85 2024-02-15 op if (!force && access(path, F_OK) == 0) {
315 f0e62b85 2024-02-15 op warnx("identity %s already exists", *argv);
320 f0e62b85 2024-02-15 op if (rename(sfn, path) == -1) {
321 f0e62b85 2024-02-15 op warn("can't rename");
330 f0e62b85 2024-02-15 op cmd_export(const struct cmd *cmd, int argc, char **argv)
332 f0e62b85 2024-02-15 op FILE *fp, *outfp;
333 f0e62b85 2024-02-15 op const char *cert = NULL;
334 f0e62b85 2024-02-15 op const char *identity = NULL;
335 f0e62b85 2024-02-15 op char path[PATH_MAX];
336 f0e62b85 2024-02-15 op char buf[BUFSIZ];
340 f0e62b85 2024-02-15 op while ((ch = getopt(argc, argv, "C:")) != -1) {
341 f0e62b85 2024-02-15 op switch (ch) {
343 f0e62b85 2024-02-15 op cert = optarg;
346 f0e62b85 2024-02-15 op cmd_usage(cmd);
349 f0e62b85 2024-02-15 op argc -= optind;
350 f0e62b85 2024-02-15 op argv += optind;
351 f0e62b85 2024-02-15 op if (argc != 1)
352 f0e62b85 2024-02-15 op cmd_usage(cmd);
353 f0e62b85 2024-02-15 op identity = argv[0];
355 f0e62b85 2024-02-15 op if (cert == NULL)
356 f0e62b85 2024-02-15 op cmd_usage(cmd);
358 f0e62b85 2024-02-15 op r = snprintf(path, sizeof(path), "%s/%s", cert_dir, identity);
359 f0e62b85 2024-02-15 op if (r < 0 || (size_t)r >= sizeof(path))
360 f0e62b85 2024-02-15 op err(1, "path too long");
361 f0e62b85 2024-02-15 op if ((fp = fopen(path, "r")) == NULL)
362 f0e62b85 2024-02-15 op err(1, "can't open %s", path);
364 f0e62b85 2024-02-15 op if ((outfp = fopen(cert, "w")) == NULL)
365 f0e62b85 2024-02-15 op err(1, "can't open %s", cert);
368 f0e62b85 2024-02-15 op l = fread(buf, 1, sizeof(buf), fp);
371 f0e62b85 2024-02-15 op if (fwrite(buf, 1, l, outfp) != l)
372 f0e62b85 2024-02-15 op err(1, "fwrite");
374 f0e62b85 2024-02-15 op if (ferror(fp))
375 f0e62b85 2024-02-15 op err(1, "fread");
381 f0e62b85 2024-02-15 op cmd_list(const struct cmd *cmd, int argc, char **argv)
386 f0e62b85 2024-02-15 op while ((ch = getopt(argc, argv, "")) != -1) {
387 f0e62b85 2024-02-15 op switch (ch) {
389 f0e62b85 2024-02-15 op cmd_usage(cmd);
392 f0e62b85 2024-02-15 op argc -= optind;
393 f0e62b85 2024-02-15 op argv += optind;
394 f0e62b85 2024-02-15 op if (argc != 0)
395 f0e62b85 2024-02-15 op cmd_usage(cmd);
397 f0e62b85 2024-02-15 op for (id = identities; *id; ++id)
404 f0e62b85 2024-02-15 op cmd_mappings(const struct cmd *cmd, int argc, char **argv)
406 e1ccda6c 2024-02-16 op struct ccert *c;
407 e1ccda6c 2024-02-16 op const char *id = NULL;
408 f0e62b85 2024-02-15 op int ch, defport;
411 f0e62b85 2024-02-15 op while ((ch = getopt(argc, argv, "")) != -1) {
412 f0e62b85 2024-02-15 op switch (ch) {
414 f0e62b85 2024-02-15 op cmd_usage(cmd);
417 f0e62b85 2024-02-15 op argc -= optind;
418 f0e62b85 2024-02-15 op argv += optind;
419 e1ccda6c 2024-02-16 op if (argc == 1) {
420 e1ccda6c 2024-02-16 op if ((id = ccert(*argv)) == NULL)
421 e1ccda6c 2024-02-16 op errx(1, "unknown identity %s", *argv);
422 e1ccda6c 2024-02-16 op argc--, argv++;
424 f0e62b85 2024-02-15 op if (argc != 0)
425 f0e62b85 2024-02-15 op cmd_usage(cmd);
427 f0e62b85 2024-02-15 op for (i = 0; i < cert_store.len; ++i) {
428 e1ccda6c 2024-02-16 op c = &cert_store.certs[i];
430 e1ccda6c 2024-02-16 op if (id && strcmp(id, c->cert) != 0)
433 e1ccda6c 2024-02-16 op defport = !strcmp(c->port, "1965");
435 e1ccda6c 2024-02-16 op printf("%s\t%s%s%s%s\n", c->cert, c->host,
436 e1ccda6c 2024-02-16 op defport ? "" : ":", defport ? "" : c->port,
443 f0e62b85 2024-02-15 op static struct iri *
444 f0e62b85 2024-02-15 op parseiri(char *spec)
446 f0e62b85 2024-02-15 op static struct iri iri;
447 f0e62b85 2024-02-15 op const char *errstr;
448 f0e62b85 2024-02-15 op char *host, *port = NULL, *path = NULL;
450 f0e62b85 2024-02-15 op memset(&iri, 0, sizeof(iri));
454 f0e62b85 2024-02-15 op port = host + strcspn(host, ":/");
455 f0e62b85 2024-02-15 op if (*port == ':') {
456 f0e62b85 2024-02-15 op *port++ = '\0';
457 f0e62b85 2024-02-15 op if ((path = strchr(port, '/')) != NULL)
458 f0e62b85 2024-02-15 op *path++ = '\0';
459 f0e62b85 2024-02-15 op } else if (*port == '/') {
460 f0e62b85 2024-02-15 op *port++ = '\0';
466 f0e62b85 2024-02-15 op strlcpy(iri.iri_host, host, sizeof(iri.iri_host));
467 f0e62b85 2024-02-15 op strlcpy(iri.iri_portstr, port ? port : "1965", sizeof(iri.iri_portstr));
468 f0e62b85 2024-02-15 op strlcpy(iri.iri_path, path ? path : "/", sizeof(iri.iri_path));
470 f0e62b85 2024-02-15 op iri.iri_port = strtonum(iri.iri_portstr, 0, UINT16_MAX, &errstr);
472 f0e62b85 2024-02-15 op err(1, "port number is %s: %s", errstr, iri.iri_portstr);
478 f0e62b85 2024-02-15 op cmd_use(const struct cmd *cmd, int argc, char **argv)
480 f0e62b85 2024-02-15 op char *cert, *spec;
483 f0e62b85 2024-02-15 op while ((ch = getopt(argc, argv, "")) != -1) {
484 f0e62b85 2024-02-15 op switch (ch) {
486 f0e62b85 2024-02-15 op cmd_usage(cmd);
489 f0e62b85 2024-02-15 op argc -= optind;
490 f0e62b85 2024-02-15 op argv += optind;
491 f0e62b85 2024-02-15 op if (argc != 2)
492 f0e62b85 2024-02-15 op cmd_usage(cmd);
494 f0e62b85 2024-02-15 op cert = argv[0];
495 f0e62b85 2024-02-15 op spec = argv[1];
497 f0e62b85 2024-02-15 op if (ccert(cert) == NULL)
498 f0e62b85 2024-02-15 op err(1, "unknown identity %s", cert);
500 f0e62b85 2024-02-15 op if (cert_save_for(cert, parseiri(spec), 1) == -1)
501 f0e62b85 2024-02-15 op errx(1, "failed to save the certificate");
507 f0e62b85 2024-02-15 op cmd_forget(const struct cmd *cmd, int argc, char **argv)
509 f0e62b85 2024-02-15 op char *cert, *spec;
512 f0e62b85 2024-02-15 op while ((ch = getopt(argc, argv, "")) != -1) {
513 f0e62b85 2024-02-15 op switch (ch) {
515 f0e62b85 2024-02-15 op cmd_usage(cmd);
518 f0e62b85 2024-02-15 op argc -= optind;
519 f0e62b85 2024-02-15 op argv += optind;
520 f0e62b85 2024-02-15 op if (argc != 2)
521 f0e62b85 2024-02-15 op cmd_usage(cmd);
523 f0e62b85 2024-02-15 op cert = argv[0];
524 f0e62b85 2024-02-15 op spec = argv[1];
526 f0e62b85 2024-02-15 op if (ccert(cert) == NULL)
527 f0e62b85 2024-02-15 op err(1, "unknown identity %s", cert);
529 f0e62b85 2024-02-15 op if (cert_delete_for(cert, parseiri(spec), 1) == -1)
530 f0e62b85 2024-02-15 op errx(1, "failed to save the certificate");