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 const struct parser gemtext_parser, textplain_parser, textpatch_parser;
85 void load_page_from_str(struct tab *tab, const char *page) { return; }
86 void erase_buffer(struct buffer *buffer) { return; }
93 fprintf(stderr, "usage: %s command [args...]\n", getprogname());
94 fprintf(stderr, "Available subcommands are:");
95 for (i = 0; i < nitems(cmds); ++i)
96 fprintf(stderr, " %s", cmds[i].name);
102 cmd_usage(const struct cmd *cmd)
104 fprintf(stderr, "usage: %s %s%s%s\n", getprogname(), cmd->name,
105 *cmd->usage ? " " : "", cmd->usage);
110 main(int argc, char **argv)
112 const struct cmd *cmd;
116 * Can't use portably getopt() since there's no cross-platform
117 * way of resetting it.
127 if (!strcmp(*argv, "--"))
129 else if (**argv == '-')
135 for (i = 0; i < nitems(cmds); ++i) {
138 if (strcmp(cmd->name, argv[0]) != 0)
142 if (certs_init(certs_file) == -1)
143 errx(1, "failed to initialize the cert store.");
144 return (cmd->fn(cmd, argc, argv));
147 warnx("unknown command: %s", argv[0]);
152 cmd_generate(const struct cmd *cmd, int argc, char **argv)
159 while ((ch = getopt(argc, argv, "t:")) != -1) {
162 if (!strcasecmp(optarg, "ec")) {
166 if (!strcasecmp(optarg, "rsa")) {
170 errx(1, "Unknown key type requested: %s", optarg);
184 r = snprintf(path, sizeof(path), "%s%s", cert_dir, name);
185 if (r < 0 || (size_t)r >= sizeof(path))
186 errx(1, "path too long");
188 if (cert_new(name, path, ec) == -1)
189 errx(1, "failure generating the key");
195 cmd_remove(const struct cmd *cmd, int argc, char **argv)
201 while ((ch = getopt(argc, argv, "")) != -1) {
215 r = snprintf(path, sizeof(path), "%s%s", cert_dir, name);
216 if (r < 0 || (size_t)r >= sizeof(path))
217 errx(1, "path too long");
219 if (unlink(path) == -1)
220 err(1, "unlink %s", path);
225 cmd_import(const struct cmd *cmd, int argc, char **argv)
227 struct tls_config *conf;
228 const char *key = NULL, *cert = NULL;
229 char path[PATH_MAX], sfn[PATH_MAX];
231 uint8_t *keym, *certm;
236 while ((ch = getopt(argc, argv, "C:K:f")) != -1) {
262 if ((keym = tls_load_file(key, &keyl, NULL)) == NULL)
263 err(1, "can't open %s", key);
264 if ((certm = tls_load_file(cert, &certl, NULL)) == NULL)
265 err(1, "can't open %s", cert);
267 if ((conf = tls_config_new()) == NULL)
268 err(1, "tls_config_new");
270 if (tls_config_set_keypair_mem(conf, certm, certl, keym, keyl) == -1)
271 errx(1, "failed to load the keypair: %s",
272 tls_config_error(conf));
274 tls_config_free(conf);
276 r = snprintf(path, sizeof(path), "%s/%s", cert_dir, *argv);
277 if (r < 0 || (size_t)r >= sizeof(path))
278 err(1, "identity name too long");
280 strlcpy(sfn, cert_dir_tmp, sizeof(sfn));
281 if ((fd = mkstemp(sfn)) == -1 ||
282 (fp = fdopen(fd, "w")) == NULL) {
292 if (fwrite(certm, 1, certl, fp) != certl) {
298 if (strcmp(key, cert) != 0 &&
299 fwrite(keym, 1, keyl, fp) != keyl) {
306 if (fflush(fp) == EOF) {
314 if (!force && access(path, F_OK) == 0) {
315 warnx("identity %s already exists", *argv);
320 if (rename(sfn, path) == -1) {
321 warn("can't rename");
330 cmd_export(const struct cmd *cmd, int argc, char **argv)
333 const char *cert = NULL;
334 const char *identity = NULL;
340 while ((ch = getopt(argc, argv, "C:")) != -1) {
358 r = snprintf(path, sizeof(path), "%s/%s", cert_dir, identity);
359 if (r < 0 || (size_t)r >= sizeof(path))
360 err(1, "path too long");
361 if ((fp = fopen(path, "r")) == NULL)
362 err(1, "can't open %s", path);
364 if ((outfp = fopen(cert, "w")) == NULL)
365 err(1, "can't open %s", cert);
368 l = fread(buf, 1, sizeof(buf), fp);
371 if (fwrite(buf, 1, l, outfp) != l)
381 cmd_list(const struct cmd *cmd, int argc, char **argv)
386 while ((ch = getopt(argc, argv, "")) != -1) {
397 for (id = identities; *id; ++id)
404 cmd_mappings(const struct cmd *cmd, int argc, char **argv)
407 const char *id = NULL;
411 while ((ch = getopt(argc, argv, "")) != -1) {
420 if ((id = ccert(*argv)) == NULL)
421 errx(1, "unknown identity %s", *argv);
427 for (i = 0; i < cert_store.len; ++i) {
428 c = &cert_store.certs[i];
430 if (id && strcmp(id, c->cert) != 0)
433 defport = !strcmp(c->port, "1965");
435 printf("%s\t%s%s%s%s\n", c->cert, c->host,
436 defport ? "" : ":", defport ? "" : c->port,
446 static struct iri iri;
448 char *host, *port = NULL, *path = NULL;
450 memset(&iri, 0, sizeof(iri));
454 port = host + strcspn(host, ":/");
457 if ((path = strchr(port, '/')) != NULL)
459 } else if (*port == '/') {
466 strlcpy(iri.iri_host, host, sizeof(iri.iri_host));
467 strlcpy(iri.iri_portstr, port ? port : "1965", sizeof(iri.iri_portstr));
468 strlcpy(iri.iri_path, path ? path : "/", sizeof(iri.iri_path));
470 iri.iri_port = strtonum(iri.iri_portstr, 0, UINT16_MAX, &errstr);
472 err(1, "port number is %s: %s", errstr, iri.iri_portstr);
478 cmd_use(const struct cmd *cmd, int argc, char **argv)
483 while ((ch = getopt(argc, argv, "")) != -1) {
497 if (ccert(cert) == NULL)
498 err(1, "unknown identity %s", cert);
500 if (cert_save_for(cert, parseiri(spec), 1) == -1)
501 errx(1, "failed to save the certificate");
507 cmd_forget(const struct cmd *cmd, int argc, char **argv)
512 while ((ch = getopt(argc, argv, "")) != -1) {
526 if (ccert(cert) == NULL)
527 err(1, "unknown identity %s", cert);
529 if (cert_delete_for(cert, parseiri(spec), 1) == -1)
530 errx(1, "failed to save the certificate");