2 * Copyright (c) 2024 Omar Polo <op@omarpolo.com>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
29 #include "telescope.h"
32 #define nitems(x) (sizeof(x) / sizeof((x)[0]))
37 static int cmd_generate(const struct cmd *, int, char **);
38 static int cmd_remove(const struct cmd *, int, char **);
39 static int cmd_import(const struct cmd *, int, char **);
40 static int cmd_export(const struct cmd *, int, char **);
41 static int cmd_list(const struct cmd *, int, char **);
42 static int cmd_mappings(const struct cmd *, int, char **);
43 static int cmd_use(const struct cmd *, int, char **);
44 static int cmd_forget(const struct cmd *, int, char **);
48 int (*fn)(const struct cmd *, int argc, char **argv);
52 static const struct cmd cmds[] = {
53 { "generate", cmd_generate, "[-t type] name" },
54 { "remove", cmd_remove, "name" },
55 { "import", cmd_import, "-C cert [-K key] name" },
56 { "export", cmd_export, "-C cert name" },
57 { "list", cmd_list, "" },
58 { "mappings", cmd_mappings, "" },
59 { "use", cmd_use, "name host[:port][/path]" },
60 { "forget", cmd_forget, "name host[:port][/path]" },
64 * Provide some symbols so that we can pull in some subsystems without
65 * their the dependencies.
68 const uint8_t *about_about;
69 size_t about_about_len;
70 const uint8_t *about_blank;
71 size_t about_blank_len;
72 const uint8_t *about_crash;
73 size_t about_crash_len;
74 const uint8_t *about_help;
75 size_t about_help_len;
76 const uint8_t *about_license;
77 size_t about_license_len;
78 const uint8_t *about_new;
80 const uint8_t *bookmarks;
83 void gemtext_initparser(struct parser *p) { return; }
84 void textpatch_initparser(struct parser *p) { return; }
85 void textplain_initparser(struct parser *p) { return; }
87 void load_page_from_str(struct tab *tab, const char *page) { return; }
88 void erase_buffer(struct buffer *buffer) { return; }
95 fprintf(stderr, "usage: %s command [args...]\n", getprogname());
96 fprintf(stderr, "Available subcommands are:");
97 for (i = 0; i < nitems(cmds); ++i)
98 fprintf(stderr, " %s", cmds[i].name);
104 cmd_usage(const struct cmd *cmd)
106 fprintf(stderr, "usage: %s %s%s%s\n", getprogname(), cmd->name,
107 *cmd->usage ? " " : "", cmd->usage);
112 main(int argc, char **argv)
114 const struct cmd *cmd;
118 * Can't use portably getopt() since there's no cross-platform
119 * way of resetting it.
129 if (!strcmp(*argv, "--"))
131 else if (**argv == '-')
137 for (i = 0; i < nitems(cmds); ++i) {
140 if (strcmp(cmd->name, argv[0]) != 0)
144 if (certs_init(certs_file) == -1)
145 errx(1, "failed to initialize the cert store.");
146 return (cmd->fn(cmd, argc, argv));
149 warnx("unknown command: %s", argv[0]);
154 cmd_generate(const struct cmd *cmd, int argc, char **argv)
161 while ((ch = getopt(argc, argv, "t:")) != -1) {
164 if (!strcasecmp(optarg, "ec")) {
168 if (!strcasecmp(optarg, "rsa")) {
172 errx(1, "Unknown key type requested: %s", optarg);
186 r = snprintf(path, sizeof(path), "%s%s", cert_dir, name);
187 if (r < 0 || (size_t)r >= sizeof(path))
188 errx(1, "path too long");
190 if (cert_new(name, path, ec) == -1)
191 errx(1, "failure generating the key");
197 cmd_remove(const struct cmd *cmd, int argc, char **argv)
203 while ((ch = getopt(argc, argv, "")) != -1) {
217 r = snprintf(path, sizeof(path), "%s%s", cert_dir, name);
218 if (r < 0 || (size_t)r >= sizeof(path))
219 errx(1, "path too long");
221 if (unlink(path) == -1)
222 err(1, "unlink %s", path);
227 cmd_import(const struct cmd *cmd, int argc, char **argv)
229 struct tls_config *conf;
230 const char *key = NULL, *cert = NULL;
231 char path[PATH_MAX], sfn[PATH_MAX];
233 uint8_t *keym, *certm;
238 while ((ch = getopt(argc, argv, "C:K:f")) != -1) {
264 if ((keym = tls_load_file(key, &keyl, NULL)) == NULL)
265 err(1, "can't open %s", key);
266 if ((certm = tls_load_file(cert, &certl, NULL)) == NULL)
267 err(1, "can't open %s", cert);
269 if ((conf = tls_config_new()) == NULL)
270 err(1, "tls_config_new");
272 if (tls_config_set_keypair_mem(conf, certm, certl, keym, keyl) == -1)
273 errx(1, "failed to load the keypair: %s",
274 tls_config_error(conf));
276 tls_config_free(conf);
278 r = snprintf(path, sizeof(path), "%s/%s", cert_dir, *argv);
279 if (r < 0 || (size_t)r >= sizeof(path))
280 err(1, "identity name too long");
282 strlcpy(sfn, cert_dir_tmp, sizeof(sfn));
283 if ((fd = mkstemp(sfn)) == -1 ||
284 (fp = fdopen(fd, "w")) == NULL) {
294 if (fwrite(certm, 1, certl, fp) != certl) {
300 if (strcmp(key, cert) != 0 &&
301 fwrite(keym, 1, keyl, fp) != keyl) {
308 if (fflush(fp) == EOF) {
316 if (!force && access(path, F_OK) == 0) {
317 warnx("identity %s already exists", *argv);
322 if (rename(sfn, path) == -1) {
323 warn("can't rename");
332 cmd_export(const struct cmd *cmd, int argc, char **argv)
335 const char *cert = NULL;
336 const char *identity = NULL;
342 while ((ch = getopt(argc, argv, "C:")) != -1) {
360 r = snprintf(path, sizeof(path), "%s/%s", cert_dir, identity);
361 if (r < 0 || (size_t)r >= sizeof(path))
362 err(1, "path too long");
363 if ((fp = fopen(path, "r")) == NULL)
364 err(1, "can't open %s", path);
366 if ((outfp = fopen(cert, "w")) == NULL)
367 err(1, "can't open %s", cert);
370 l = fread(buf, 1, sizeof(buf), fp);
373 if (fwrite(buf, 1, l, outfp) != l)
383 cmd_list(const struct cmd *cmd, int argc, char **argv)
388 while ((ch = getopt(argc, argv, "")) != -1) {
399 for (id = identities; *id; ++id)
406 cmd_mappings(const struct cmd *cmd, int argc, char **argv)
409 const char *id = NULL;
413 while ((ch = getopt(argc, argv, "")) != -1) {
422 if ((id = ccert(*argv)) == NULL)
423 errx(1, "unknown identity %s", *argv);
429 for (i = 0; i < cert_store.len; ++i) {
430 c = &cert_store.certs[i];
432 if (id && strcmp(id, c->cert) != 0)
435 defport = !strcmp(c->port, "1965");
437 printf("%s\t%s%s%s%s\n", c->cert, c->host,
438 defport ? "" : ":", defport ? "" : c->port,
448 static struct iri iri;
450 char *host, *port = NULL, *path = NULL;
452 memset(&iri, 0, sizeof(iri));
456 port = host + strcspn(host, ":/");
459 if ((path = strchr(port, '/')) != NULL)
461 } else if (*port == '/') {
468 strlcpy(iri.iri_host, host, sizeof(iri.iri_host));
469 strlcpy(iri.iri_portstr, port ? port : "1965", sizeof(iri.iri_portstr));
470 strlcpy(iri.iri_path, path ? path : "/", sizeof(iri.iri_path));
472 iri.iri_port = strtonum(iri.iri_portstr, 0, UINT16_MAX, &errstr);
474 err(1, "port number is %s: %s", errstr, iri.iri_portstr);
480 cmd_use(const struct cmd *cmd, int argc, char **argv)
485 while ((ch = getopt(argc, argv, "")) != -1) {
499 if (ccert(cert) == NULL)
500 err(1, "unknown identity %s", cert);
502 if (cert_save_for(cert, parseiri(spec), 1) == -1)
503 errx(1, "failed to save the certificate");
509 cmd_forget(const struct cmd *cmd, int argc, char **argv)
514 while ((ch = getopt(argc, argv, "")) != -1) {
528 if (ccert(cert) == NULL)
529 err(1, "unknown identity %s", cert);
531 if (cert_delete_for(cert, parseiri(spec), 1) == -1)
532 errx(1, "failed to save the certificate");