Blame


1 f0e62b85 2024-02-15 op /*
2 f0e62b85 2024-02-15 op * Copyright (c) 2024 Omar Polo <op@omarpolo.com>
3 f0e62b85 2024-02-15 op *
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.
7 f0e62b85 2024-02-15 op *
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.
15 f0e62b85 2024-02-15 op */
16 f0e62b85 2024-02-15 op
17 f0e62b85 2024-02-15 op #include "compat.h"
18 f0e62b85 2024-02-15 op
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>
25 f0e62b85 2024-02-15 op
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"
30 f0e62b85 2024-02-15 op
31 f0e62b85 2024-02-15 op #ifndef nitems
32 f0e62b85 2024-02-15 op #define nitems(x) (sizeof(x) / sizeof((x)[0]))
33 f0e62b85 2024-02-15 op #endif
34 f0e62b85 2024-02-15 op
35 f0e62b85 2024-02-15 op struct cmd;
36 f0e62b85 2024-02-15 op
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 **);
45 f0e62b85 2024-02-15 op
46 f0e62b85 2024-02-15 op struct cmd {
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;
50 f0e62b85 2024-02-15 op };
51 f0e62b85 2024-02-15 op
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]" },
61 f0e62b85 2024-02-15 op };
62 f0e62b85 2024-02-15 op
63 f0e62b85 2024-02-15 op /*
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.
66 f0e62b85 2024-02-15 op */
67 f0e62b85 2024-02-15 op
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;
82 f0e62b85 2024-02-15 op
83 fd1c80ce 2024-06-14 op const struct parser gemtext_parser, textplain_parser, textpatch_parser;
84 f0e62b85 2024-02-15 op
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; }
87 f0e62b85 2024-02-15 op
88 f0e62b85 2024-02-15 op static void __dead
89 f0e62b85 2024-02-15 op usage(void)
90 f0e62b85 2024-02-15 op {
91 f0e62b85 2024-02-15 op size_t i;
92 f0e62b85 2024-02-15 op
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);
98 f0e62b85 2024-02-15 op exit(1);
99 f0e62b85 2024-02-15 op }
100 f0e62b85 2024-02-15 op
101 f0e62b85 2024-02-15 op static void __dead
102 f0e62b85 2024-02-15 op cmd_usage(const struct cmd *cmd)
103 f0e62b85 2024-02-15 op {
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);
106 f0e62b85 2024-02-15 op exit(1);
107 f0e62b85 2024-02-15 op }
108 f0e62b85 2024-02-15 op
109 f0e62b85 2024-02-15 op int
110 f0e62b85 2024-02-15 op main(int argc, char **argv)
111 f0e62b85 2024-02-15 op {
112 f0e62b85 2024-02-15 op const struct cmd *cmd;
113 f0e62b85 2024-02-15 op size_t i;
114 f0e62b85 2024-02-15 op
115 c3465169 2024-02-19 op /*
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.
118 c3465169 2024-02-19 op */
119 f0e62b85 2024-02-15 op
120 f0e62b85 2024-02-15 op if (argc == 0)
121 f0e62b85 2024-02-15 op usage();
122 c3465169 2024-02-19 op argc--, argv++;
123 f0e62b85 2024-02-15 op
124 c3465169 2024-02-19 op if (argc == 0)
125 c3465169 2024-02-19 op usage();
126 c3465169 2024-02-19 op
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 == '-')
130 c3465169 2024-02-19 op usage();
131 c3465169 2024-02-19 op
132 c3465169 2024-02-19 op if (argc == 0)
133 c3465169 2024-02-19 op usage();
134 c3465169 2024-02-19 op
135 f0e62b85 2024-02-15 op for (i = 0; i < nitems(cmds); ++i) {
136 f0e62b85 2024-02-15 op cmd = &cmds[i];
137 f0e62b85 2024-02-15 op
138 f0e62b85 2024-02-15 op if (strcmp(cmd->name, argv[0]) != 0)
139 f0e62b85 2024-02-15 op continue;
140 f0e62b85 2024-02-15 op
141 f0e62b85 2024-02-15 op fs_init();
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));
145 f0e62b85 2024-02-15 op }
146 f0e62b85 2024-02-15 op
147 e1ccda6c 2024-02-16 op warnx("unknown command: %s", argv[0]);
148 f0e62b85 2024-02-15 op usage();
149 f0e62b85 2024-02-15 op }
150 f0e62b85 2024-02-15 op
151 f0e62b85 2024-02-15 op static int
152 f0e62b85 2024-02-15 op cmd_generate(const struct cmd *cmd, int argc, char **argv)
153 f0e62b85 2024-02-15 op {
154 f0e62b85 2024-02-15 op const char *name;
155 f0e62b85 2024-02-15 op char path[PATH_MAX];
156 f0e62b85 2024-02-15 op int ch, r;
157 f0e62b85 2024-02-15 op int ec = 1;
158 f0e62b85 2024-02-15 op
159 f0e62b85 2024-02-15 op while ((ch = getopt(argc, argv, "t:")) != -1) {
160 f0e62b85 2024-02-15 op switch (ch) {
161 f0e62b85 2024-02-15 op case 't':
162 f0e62b85 2024-02-15 op if (!strcasecmp(optarg, "ec")) {
163 f0e62b85 2024-02-15 op ec = 1;
164 f0e62b85 2024-02-15 op break;
165 f0e62b85 2024-02-15 op }
166 f0e62b85 2024-02-15 op if (!strcasecmp(optarg, "rsa")) {
167 f0e62b85 2024-02-15 op ec = 0;
168 f0e62b85 2024-02-15 op break;
169 f0e62b85 2024-02-15 op }
170 f0e62b85 2024-02-15 op errx(1, "Unknown key type requested: %s", optarg);
171 f0e62b85 2024-02-15 op
172 f0e62b85 2024-02-15 op default:
173 f0e62b85 2024-02-15 op cmd_usage(cmd);
174 f0e62b85 2024-02-15 op }
175 f0e62b85 2024-02-15 op }
176 f0e62b85 2024-02-15 op argc -= optind;
177 f0e62b85 2024-02-15 op argv += optind;
178 f0e62b85 2024-02-15 op
179 f0e62b85 2024-02-15 op if (argc != 1)
180 f0e62b85 2024-02-15 op cmd_usage(cmd);
181 f0e62b85 2024-02-15 op
182 f0e62b85 2024-02-15 op name = *argv;
183 f0e62b85 2024-02-15 op
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");
187 f0e62b85 2024-02-15 op
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");
190 f0e62b85 2024-02-15 op
191 f0e62b85 2024-02-15 op return 0;
192 f0e62b85 2024-02-15 op }
193 f0e62b85 2024-02-15 op
194 f0e62b85 2024-02-15 op static int
195 f0e62b85 2024-02-15 op cmd_remove(const struct cmd *cmd, int argc, char **argv)
196 f0e62b85 2024-02-15 op {
197 f0e62b85 2024-02-15 op const char *name;
198 f0e62b85 2024-02-15 op char path[PATH_MAX];
199 f0e62b85 2024-02-15 op int ch, r;
200 f0e62b85 2024-02-15 op
201 f0e62b85 2024-02-15 op while ((ch = getopt(argc, argv, "")) != -1) {
202 f0e62b85 2024-02-15 op switch (ch) {
203 f0e62b85 2024-02-15 op default:
204 f0e62b85 2024-02-15 op cmd_usage(cmd);
205 f0e62b85 2024-02-15 op }
206 f0e62b85 2024-02-15 op }
207 f0e62b85 2024-02-15 op argc -= optind;
208 f0e62b85 2024-02-15 op argv += optind;
209 f0e62b85 2024-02-15 op
210 f0e62b85 2024-02-15 op if (argc != 1)
211 f0e62b85 2024-02-15 op cmd_usage(cmd);
212 f0e62b85 2024-02-15 op
213 f0e62b85 2024-02-15 op name = *argv;
214 f0e62b85 2024-02-15 op
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");
218 f0e62b85 2024-02-15 op
219 f0e62b85 2024-02-15 op if (unlink(path) == -1)
220 f0e62b85 2024-02-15 op err(1, "unlink %s", path);
221 f0e62b85 2024-02-15 op return 0;
222 f0e62b85 2024-02-15 op }
223 f0e62b85 2024-02-15 op
224 f0e62b85 2024-02-15 op static int
225 f0e62b85 2024-02-15 op cmd_import(const struct cmd *cmd, int argc, char **argv)
226 f0e62b85 2024-02-15 op {
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];
230 f0e62b85 2024-02-15 op FILE *fp;
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;
235 f0e62b85 2024-02-15 op
236 f0e62b85 2024-02-15 op while ((ch = getopt(argc, argv, "C:K:f")) != -1) {
237 f0e62b85 2024-02-15 op switch (ch) {
238 f0e62b85 2024-02-15 op case 'C':
239 f0e62b85 2024-02-15 op cert = optarg;
240 f0e62b85 2024-02-15 op break;
241 f0e62b85 2024-02-15 op case 'K':
242 f0e62b85 2024-02-15 op key = optarg;
243 f0e62b85 2024-02-15 op break;
244 f0e62b85 2024-02-15 op case 'f':
245 f0e62b85 2024-02-15 op force = 1;
246 f0e62b85 2024-02-15 op break;
247 f0e62b85 2024-02-15 op default:
248 f0e62b85 2024-02-15 op cmd_usage(cmd);
249 f0e62b85 2024-02-15 op }
250 f0e62b85 2024-02-15 op }
251 f0e62b85 2024-02-15 op argc -= optind;
252 f0e62b85 2024-02-15 op argv += optind;
253 f0e62b85 2024-02-15 op
254 f0e62b85 2024-02-15 op if (argc != 1)
255 f0e62b85 2024-02-15 op cmd_usage(cmd);
256 f0e62b85 2024-02-15 op
257 f0e62b85 2024-02-15 op if (key == NULL)
258 f0e62b85 2024-02-15 op key = cert;
259 f0e62b85 2024-02-15 op if (cert == NULL)
260 f0e62b85 2024-02-15 op cmd_usage(cmd);
261 f0e62b85 2024-02-15 op
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);
266 f0e62b85 2024-02-15 op
267 f0e62b85 2024-02-15 op if ((conf = tls_config_new()) == NULL)
268 f0e62b85 2024-02-15 op err(1, "tls_config_new");
269 f0e62b85 2024-02-15 op
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));
273 f0e62b85 2024-02-15 op
274 f0e62b85 2024-02-15 op tls_config_free(conf);
275 f0e62b85 2024-02-15 op
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");
279 f0e62b85 2024-02-15 op
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");
285 f0e62b85 2024-02-15 op unlink(sfn);
286 f0e62b85 2024-02-15 op close(fd);
287 f0e62b85 2024-02-15 op } else
288 f0e62b85 2024-02-15 op warn("mkstamp");
289 f0e62b85 2024-02-15 op return 1;
290 f0e62b85 2024-02-15 op }
291 f0e62b85 2024-02-15 op
292 f0e62b85 2024-02-15 op if (fwrite(certm, 1, certl, fp) != certl) {
293 f0e62b85 2024-02-15 op warn("fwrite");
294 f0e62b85 2024-02-15 op unlink(sfn);
295 f0e62b85 2024-02-15 op fclose(fp);
296 f0e62b85 2024-02-15 op return 1;
297 f0e62b85 2024-02-15 op }
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");
301 f0e62b85 2024-02-15 op unlink(sfn);
302 f0e62b85 2024-02-15 op fclose(fp);
303 f0e62b85 2024-02-15 op return 1;
304 f0e62b85 2024-02-15 op }
305 f0e62b85 2024-02-15 op
306 f0e62b85 2024-02-15 op if (fflush(fp) == EOF) {
307 f0e62b85 2024-02-15 op warn("fflush");
308 f0e62b85 2024-02-15 op unlink(sfn);
309 f0e62b85 2024-02-15 op fclose(fp);
310 f0e62b85 2024-02-15 op return 1;
311 f0e62b85 2024-02-15 op }
312 f0e62b85 2024-02-15 op fclose(fp);
313 f0e62b85 2024-02-15 op
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);
316 f0e62b85 2024-02-15 op unlink(sfn);
317 f0e62b85 2024-02-15 op return 1;
318 f0e62b85 2024-02-15 op }
319 f0e62b85 2024-02-15 op
320 f0e62b85 2024-02-15 op if (rename(sfn, path) == -1) {
321 f0e62b85 2024-02-15 op warn("can't rename");
322 f0e62b85 2024-02-15 op unlink(sfn);
323 f0e62b85 2024-02-15 op return 1;
324 f0e62b85 2024-02-15 op }
325 f0e62b85 2024-02-15 op
326 f0e62b85 2024-02-15 op return (0);
327 f0e62b85 2024-02-15 op }
328 f0e62b85 2024-02-15 op
329 f0e62b85 2024-02-15 op static int
330 f0e62b85 2024-02-15 op cmd_export(const struct cmd *cmd, int argc, char **argv)
331 f0e62b85 2024-02-15 op {
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];
337 f0e62b85 2024-02-15 op size_t l;
338 f0e62b85 2024-02-15 op int ch, r;
339 f0e62b85 2024-02-15 op
340 f0e62b85 2024-02-15 op while ((ch = getopt(argc, argv, "C:")) != -1) {
341 f0e62b85 2024-02-15 op switch (ch) {
342 f0e62b85 2024-02-15 op case 'C':
343 f0e62b85 2024-02-15 op cert = optarg;
344 f0e62b85 2024-02-15 op break;
345 f0e62b85 2024-02-15 op default:
346 f0e62b85 2024-02-15 op cmd_usage(cmd);
347 f0e62b85 2024-02-15 op }
348 f0e62b85 2024-02-15 op }
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];
354 f0e62b85 2024-02-15 op
355 f0e62b85 2024-02-15 op if (cert == NULL)
356 f0e62b85 2024-02-15 op cmd_usage(cmd);
357 f0e62b85 2024-02-15 op
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);
363 f0e62b85 2024-02-15 op
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);
366 f0e62b85 2024-02-15 op
367 f0e62b85 2024-02-15 op for (;;) {
368 f0e62b85 2024-02-15 op l = fread(buf, 1, sizeof(buf), fp);
369 f0e62b85 2024-02-15 op if (l == 0)
370 f0e62b85 2024-02-15 op break;
371 f0e62b85 2024-02-15 op if (fwrite(buf, 1, l, outfp) != l)
372 f0e62b85 2024-02-15 op err(1, "fwrite");
373 f0e62b85 2024-02-15 op }
374 f0e62b85 2024-02-15 op if (ferror(fp))
375 f0e62b85 2024-02-15 op err(1, "fread");
376 f0e62b85 2024-02-15 op
377 f0e62b85 2024-02-15 op return 0;
378 f0e62b85 2024-02-15 op }
379 f0e62b85 2024-02-15 op
380 f0e62b85 2024-02-15 op static int
381 f0e62b85 2024-02-15 op cmd_list(const struct cmd *cmd, int argc, char **argv)
382 f0e62b85 2024-02-15 op {
383 f0e62b85 2024-02-15 op char **id;
384 f0e62b85 2024-02-15 op int ch;
385 f0e62b85 2024-02-15 op
386 f0e62b85 2024-02-15 op while ((ch = getopt(argc, argv, "")) != -1) {
387 f0e62b85 2024-02-15 op switch (ch) {
388 f0e62b85 2024-02-15 op default:
389 f0e62b85 2024-02-15 op cmd_usage(cmd);
390 f0e62b85 2024-02-15 op }
391 f0e62b85 2024-02-15 op }
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);
396 f0e62b85 2024-02-15 op
397 f0e62b85 2024-02-15 op for (id = identities; *id; ++id)
398 f0e62b85 2024-02-15 op puts(*id);
399 f0e62b85 2024-02-15 op
400 f0e62b85 2024-02-15 op return (0);
401 f0e62b85 2024-02-15 op }
402 f0e62b85 2024-02-15 op
403 f0e62b85 2024-02-15 op static int
404 f0e62b85 2024-02-15 op cmd_mappings(const struct cmd *cmd, int argc, char **argv)
405 f0e62b85 2024-02-15 op {
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;
409 f0e62b85 2024-02-15 op size_t i;
410 f0e62b85 2024-02-15 op
411 f0e62b85 2024-02-15 op while ((ch = getopt(argc, argv, "")) != -1) {
412 f0e62b85 2024-02-15 op switch (ch) {
413 f0e62b85 2024-02-15 op default:
414 f0e62b85 2024-02-15 op cmd_usage(cmd);
415 f0e62b85 2024-02-15 op }
416 f0e62b85 2024-02-15 op }
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++;
423 e1ccda6c 2024-02-16 op }
424 f0e62b85 2024-02-15 op if (argc != 0)
425 f0e62b85 2024-02-15 op cmd_usage(cmd);
426 f0e62b85 2024-02-15 op
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];
429 e1ccda6c 2024-02-16 op
430 e1ccda6c 2024-02-16 op if (id && strcmp(id, c->cert) != 0)
431 e1ccda6c 2024-02-16 op continue;
432 f0e62b85 2024-02-15 op
433 e1ccda6c 2024-02-16 op defport = !strcmp(c->port, "1965");
434 f0e62b85 2024-02-15 op
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,
437 e1ccda6c 2024-02-16 op c->path);
438 f0e62b85 2024-02-15 op }
439 f0e62b85 2024-02-15 op
440 f0e62b85 2024-02-15 op return (0);
441 f0e62b85 2024-02-15 op }
442 f0e62b85 2024-02-15 op
443 f0e62b85 2024-02-15 op static struct iri *
444 f0e62b85 2024-02-15 op parseiri(char *spec)
445 f0e62b85 2024-02-15 op {
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;
449 f0e62b85 2024-02-15 op
450 f0e62b85 2024-02-15 op memset(&iri, 0, sizeof(iri));
451 f0e62b85 2024-02-15 op
452 f0e62b85 2024-02-15 op host = spec;
453 f0e62b85 2024-02-15 op
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';
461 f0e62b85 2024-02-15 op path = port;
462 f0e62b85 2024-02-15 op port = NULL;
463 f0e62b85 2024-02-15 op } else
464 f0e62b85 2024-02-15 op port = NULL;
465 f0e62b85 2024-02-15 op
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));
469 f0e62b85 2024-02-15 op
470 f0e62b85 2024-02-15 op iri.iri_port = strtonum(iri.iri_portstr, 0, UINT16_MAX, &errstr);
471 f0e62b85 2024-02-15 op if (errstr)
472 f0e62b85 2024-02-15 op err(1, "port number is %s: %s", errstr, iri.iri_portstr);
473 f0e62b85 2024-02-15 op
474 f0e62b85 2024-02-15 op return &iri;
475 f0e62b85 2024-02-15 op }
476 f0e62b85 2024-02-15 op
477 f0e62b85 2024-02-15 op static int
478 f0e62b85 2024-02-15 op cmd_use(const struct cmd *cmd, int argc, char **argv)
479 f0e62b85 2024-02-15 op {
480 f0e62b85 2024-02-15 op char *cert, *spec;
481 f0e62b85 2024-02-15 op int ch;
482 f0e62b85 2024-02-15 op
483 f0e62b85 2024-02-15 op while ((ch = getopt(argc, argv, "")) != -1) {
484 f0e62b85 2024-02-15 op switch (ch) {
485 f0e62b85 2024-02-15 op default:
486 f0e62b85 2024-02-15 op cmd_usage(cmd);
487 f0e62b85 2024-02-15 op }
488 f0e62b85 2024-02-15 op }
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);
493 f0e62b85 2024-02-15 op
494 f0e62b85 2024-02-15 op cert = argv[0];
495 f0e62b85 2024-02-15 op spec = argv[1];
496 f0e62b85 2024-02-15 op
497 f0e62b85 2024-02-15 op if (ccert(cert) == NULL)
498 f0e62b85 2024-02-15 op err(1, "unknown identity %s", cert);
499 f0e62b85 2024-02-15 op
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");
502 f0e62b85 2024-02-15 op
503 f0e62b85 2024-02-15 op return 0;
504 f0e62b85 2024-02-15 op }
505 f0e62b85 2024-02-15 op
506 f0e62b85 2024-02-15 op static int
507 f0e62b85 2024-02-15 op cmd_forget(const struct cmd *cmd, int argc, char **argv)
508 f0e62b85 2024-02-15 op {
509 f0e62b85 2024-02-15 op char *cert, *spec;
510 f0e62b85 2024-02-15 op int ch;
511 f0e62b85 2024-02-15 op
512 f0e62b85 2024-02-15 op while ((ch = getopt(argc, argv, "")) != -1) {
513 f0e62b85 2024-02-15 op switch (ch) {
514 f0e62b85 2024-02-15 op default:
515 f0e62b85 2024-02-15 op cmd_usage(cmd);
516 f0e62b85 2024-02-15 op }
517 f0e62b85 2024-02-15 op }
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);
522 f0e62b85 2024-02-15 op
523 f0e62b85 2024-02-15 op cert = argv[0];
524 f0e62b85 2024-02-15 op spec = argv[1];
525 f0e62b85 2024-02-15 op
526 f0e62b85 2024-02-15 op if (ccert(cert) == NULL)
527 f0e62b85 2024-02-15 op err(1, "unknown identity %s", cert);
528 f0e62b85 2024-02-15 op
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");
531 f0e62b85 2024-02-15 op
532 f0e62b85 2024-02-15 op return 0;
533 f0e62b85 2024-02-15 op }
534 f0e62b85 2024-02-15 op