auth-options.c revision 1.30 1 1.21 christos /* $NetBSD: auth-options.c,v 1.30 2025/10/11 15:45:06 christos Exp $ */
2 1.30 christos /* $OpenBSD: auth-options.c,v 1.102 2025/09/15 04:38:00 djm Exp $ */
3 1.29 christos
4 1.1 christos /*
5 1.17 christos * Copyright (c) 2018 Damien Miller <djm (at) mindrot.org>
6 1.17 christos *
7 1.17 christos * Permission to use, copy, modify, and distribute this software for any
8 1.17 christos * purpose with or without fee is hereby granted, provided that the above
9 1.17 christos * copyright notice and this permission notice appear in all copies.
10 1.17 christos *
11 1.17 christos * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 1.17 christos * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 1.17 christos * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 1.17 christos * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 1.17 christos * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 1.17 christos * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 1.17 christos * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 1.1 christos */
19 1.1 christos
20 1.2 christos #include "includes.h"
21 1.21 christos __RCSID("$NetBSD: auth-options.c,v 1.30 2025/10/11 15:45:06 christos Exp $");
22 1.1 christos #include <sys/types.h>
23 1.1 christos #include <sys/queue.h>
24 1.1 christos
25 1.21 christos #include <stdlib.h>
26 1.1 christos #include <netdb.h>
27 1.1 christos #include <pwd.h>
28 1.1 christos #include <string.h>
29 1.1 christos #include <stdio.h>
30 1.29 christos #include <stdint.h>
31 1.1 christos #include <stdarg.h>
32 1.2 christos #include <time.h>
33 1.17 christos #include <ctype.h>
34 1.17 christos #include <limits.h>
35 1.1 christos
36 1.1 christos #include "xmalloc.h"
37 1.9 christos #include "ssherr.h"
38 1.1 christos #include "log.h"
39 1.9 christos #include "sshbuf.h"
40 1.8 christos #include "misc.h"
41 1.9 christos #include "sshkey.h"
42 1.17 christos #include "match.h"
43 1.17 christos #include "ssh2.h"
44 1.3 adam #include "auth-options.h"
45 1.12 christos
46 1.17 christos static int
47 1.17 christos dup_strings(char ***dstp, size_t *ndstp, char **src, size_t nsrc)
48 1.17 christos {
49 1.17 christos char **dst;
50 1.17 christos size_t i, j;
51 1.1 christos
52 1.17 christos *dstp = NULL;
53 1.17 christos *ndstp = 0;
54 1.29 christos
55 1.17 christos if (nsrc == 0)
56 1.17 christos return 0;
57 1.29 christos if (nsrc >= SIZE_MAX / sizeof(*src) ||
58 1.29 christos (dst = calloc(nsrc, sizeof(*src))) == NULL)
59 1.17 christos return -1;
60 1.17 christos for (i = 0; i < nsrc; i++) {
61 1.17 christos if ((dst[i] = strdup(src[i])) == NULL) {
62 1.17 christos for (j = 0; j < i; j++)
63 1.17 christos free(dst[j]);
64 1.17 christos free(dst);
65 1.17 christos return -1;
66 1.17 christos }
67 1.17 christos }
68 1.17 christos /* success */
69 1.17 christos *dstp = dst;
70 1.17 christos *ndstp = nsrc;
71 1.3 adam return 0;
72 1.3 adam }
73 1.3 adam
74 1.3 adam #define OPTIONS_CRITICAL 1
75 1.3 adam #define OPTIONS_EXTENSIONS 2
76 1.3 adam static int
77 1.17 christos cert_option_list(struct sshauthopt *opts, struct sshbuf *oblob,
78 1.17 christos u_int which, int crit)
79 1.3 adam {
80 1.3 adam char *command, *allowed;
81 1.7 christos char *name = NULL;
82 1.9 christos struct sshbuf *c = NULL, *data = NULL;
83 1.17 christos int r, ret = -1, found;
84 1.9 christos
85 1.9 christos if ((c = sshbuf_fromb(oblob)) == NULL) {
86 1.25 christos error_f("sshbuf_fromb failed");
87 1.9 christos goto out;
88 1.9 christos }
89 1.9 christos
90 1.9 christos while (sshbuf_len(c) > 0) {
91 1.9 christos sshbuf_free(data);
92 1.9 christos data = NULL;
93 1.9 christos if ((r = sshbuf_get_cstring(c, &name, NULL)) != 0 ||
94 1.9 christos (r = sshbuf_froms(c, &data)) != 0) {
95 1.25 christos error_r(r, "Unable to parse certificate options");
96 1.3 adam goto out;
97 1.3 adam }
98 1.9 christos debug3("found certificate option \"%.100s\" len %zu",
99 1.9 christos name, sshbuf_len(data));
100 1.3 adam found = 0;
101 1.3 adam if ((which & OPTIONS_EXTENSIONS) != 0) {
102 1.22 christos if (strcmp(name, "no-touch-required") == 0) {
103 1.22 christos opts->no_require_user_presence = 1;
104 1.22 christos found = 1;
105 1.22 christos } else if (strcmp(name, "permit-X11-forwarding") == 0) {
106 1.17 christos opts->permit_x11_forwarding_flag = 1;
107 1.3 adam found = 1;
108 1.3 adam } else if (strcmp(name,
109 1.3 adam "permit-agent-forwarding") == 0) {
110 1.17 christos opts->permit_agent_forwarding_flag = 1;
111 1.3 adam found = 1;
112 1.3 adam } else if (strcmp(name,
113 1.3 adam "permit-port-forwarding") == 0) {
114 1.17 christos opts->permit_port_forwarding_flag = 1;
115 1.3 adam found = 1;
116 1.3 adam } else if (strcmp(name, "permit-pty") == 0) {
117 1.17 christos opts->permit_pty_flag = 1;
118 1.3 adam found = 1;
119 1.3 adam } else if (strcmp(name, "permit-user-rc") == 0) {
120 1.17 christos opts->permit_user_rc = 1;
121 1.3 adam found = 1;
122 1.3 adam }
123 1.3 adam }
124 1.3 adam if (!found && (which & OPTIONS_CRITICAL) != 0) {
125 1.24 christos if (strcmp(name, "verify-required") == 0) {
126 1.24 christos opts->require_verify = 1;
127 1.24 christos found = 1;
128 1.24 christos } else if (strcmp(name, "force-command") == 0) {
129 1.9 christos if ((r = sshbuf_get_cstring(data, &command,
130 1.9 christos NULL)) != 0) {
131 1.25 christos error_r(r, "Unable to parse \"%s\" "
132 1.25 christos "section", name);
133 1.3 adam goto out;
134 1.3 adam }
135 1.17 christos if (opts->force_command != NULL) {
136 1.3 adam error("Certificate has multiple "
137 1.3 adam "force-command options");
138 1.7 christos free(command);
139 1.3 adam goto out;
140 1.3 adam }
141 1.17 christos opts->force_command = command;
142 1.3 adam found = 1;
143 1.24 christos } else if (strcmp(name, "source-address") == 0) {
144 1.9 christos if ((r = sshbuf_get_cstring(data, &allowed,
145 1.9 christos NULL)) != 0) {
146 1.25 christos error_r(r, "Unable to parse \"%s\" "
147 1.25 christos "section", name);
148 1.3 adam goto out;
149 1.3 adam }
150 1.17 christos if (opts->required_from_host_cert != NULL) {
151 1.3 adam error("Certificate has multiple "
152 1.3 adam "source-address options");
153 1.7 christos free(allowed);
154 1.3 adam goto out;
155 1.3 adam }
156 1.17 christos /* Check syntax */
157 1.17 christos if (addr_match_cidr_list(NULL, allowed) == -1) {
158 1.3 adam error("Certificate source-address "
159 1.3 adam "contents invalid");
160 1.30 christos free(allowed);
161 1.3 adam goto out;
162 1.3 adam }
163 1.17 christos opts->required_from_host_cert = allowed;
164 1.3 adam found = 1;
165 1.3 adam }
166 1.3 adam }
167 1.3 adam
168 1.3 adam if (!found) {
169 1.3 adam if (crit) {
170 1.3 adam error("Certificate critical option \"%s\" "
171 1.3 adam "is not supported", name);
172 1.3 adam goto out;
173 1.3 adam } else {
174 1.3 adam logit("Certificate extension \"%s\" "
175 1.3 adam "is not supported", name);
176 1.3 adam }
177 1.9 christos } else if (sshbuf_len(data) != 0) {
178 1.3 adam error("Certificate option \"%s\" corrupt "
179 1.3 adam "(extra data)", name);
180 1.3 adam goto out;
181 1.3 adam }
182 1.7 christos free(name);
183 1.7 christos name = NULL;
184 1.3 adam }
185 1.3 adam /* successfully parsed all options */
186 1.3 adam ret = 0;
187 1.3 adam
188 1.3 adam out:
189 1.12 christos free(name);
190 1.9 christos sshbuf_free(data);
191 1.9 christos sshbuf_free(c);
192 1.3 adam return ret;
193 1.3 adam }
194 1.3 adam
195 1.17 christos struct sshauthopt *
196 1.17 christos sshauthopt_new(void)
197 1.17 christos {
198 1.17 christos struct sshauthopt *ret;
199 1.17 christos
200 1.17 christos if ((ret = calloc(1, sizeof(*ret))) == NULL)
201 1.17 christos return NULL;
202 1.17 christos ret->force_tun_device = -1;
203 1.17 christos return ret;
204 1.17 christos }
205 1.17 christos
206 1.17 christos void
207 1.17 christos sshauthopt_free(struct sshauthopt *opts)
208 1.17 christos {
209 1.17 christos size_t i;
210 1.17 christos
211 1.17 christos if (opts == NULL)
212 1.17 christos return;
213 1.17 christos
214 1.17 christos free(opts->cert_principals);
215 1.17 christos free(opts->force_command);
216 1.17 christos free(opts->required_from_host_cert);
217 1.17 christos free(opts->required_from_host_keys);
218 1.17 christos
219 1.17 christos for (i = 0; i < opts->nenv; i++)
220 1.17 christos free(opts->env[i]);
221 1.17 christos free(opts->env);
222 1.17 christos
223 1.17 christos for (i = 0; i < opts->npermitopen; i++)
224 1.17 christos free(opts->permitopen[i]);
225 1.17 christos free(opts->permitopen);
226 1.17 christos
227 1.18 christos for (i = 0; i < opts->npermitlisten; i++)
228 1.18 christos free(opts->permitlisten[i]);
229 1.18 christos free(opts->permitlisten);
230 1.18 christos
231 1.23 christos freezero(opts, sizeof(*opts));
232 1.17 christos }
233 1.17 christos
234 1.17 christos struct sshauthopt *
235 1.17 christos sshauthopt_new_with_keys_defaults(void)
236 1.17 christos {
237 1.17 christos struct sshauthopt *ret = NULL;
238 1.17 christos
239 1.17 christos if ((ret = sshauthopt_new()) == NULL)
240 1.17 christos return NULL;
241 1.17 christos
242 1.17 christos /* Defaults for authorized_keys flags */
243 1.17 christos ret->permit_port_forwarding_flag = 1;
244 1.17 christos ret->permit_agent_forwarding_flag = 1;
245 1.17 christos ret->permit_x11_forwarding_flag = 1;
246 1.17 christos ret->permit_pty_flag = 1;
247 1.17 christos ret->permit_user_rc = 1;
248 1.17 christos return ret;
249 1.17 christos }
250 1.17 christos
251 1.18 christos /*
252 1.18 christos * Parse and record a permitopen/permitlisten directive.
253 1.18 christos * Return 0 on success. Return -1 on failure and sets *errstrp to error reason.
254 1.18 christos */
255 1.18 christos static int
256 1.18 christos handle_permit(const char **optsp, int allow_bare_port,
257 1.18 christos char ***permitsp, size_t *npermitsp, const char **errstrp)
258 1.18 christos {
259 1.18 christos char *opt, *tmp, *cp, *host, **permits = *permitsp;
260 1.18 christos size_t npermits = *npermitsp;
261 1.18 christos const char *errstr = "unknown error";
262 1.18 christos
263 1.21 christos if (npermits > SSH_AUTHOPT_PERMIT_MAX) {
264 1.18 christos *errstrp = "too many permission directives";
265 1.18 christos return -1;
266 1.18 christos }
267 1.18 christos if ((opt = opt_dequote(optsp, &errstr)) == NULL) {
268 1.18 christos return -1;
269 1.18 christos }
270 1.18 christos if (allow_bare_port && strchr(opt, ':') == NULL) {
271 1.18 christos /*
272 1.18 christos * Allow a bare port number in permitlisten to indicate a
273 1.18 christos * listen_host wildcard.
274 1.18 christos */
275 1.21 christos if (asprintf(&tmp, "*:%s", opt) == -1) {
276 1.21 christos free(opt);
277 1.18 christos *errstrp = "memory allocation failed";
278 1.18 christos return -1;
279 1.18 christos }
280 1.18 christos free(opt);
281 1.18 christos opt = tmp;
282 1.18 christos }
283 1.18 christos if ((tmp = strdup(opt)) == NULL) {
284 1.18 christos free(opt);
285 1.18 christos *errstrp = "memory allocation failed";
286 1.18 christos return -1;
287 1.18 christos }
288 1.18 christos cp = tmp;
289 1.18 christos /* validate syntax before recording it. */
290 1.28 christos host = hpdelim2(&cp, NULL);
291 1.18 christos if (host == NULL || strlen(host) >= NI_MAXHOST) {
292 1.18 christos free(tmp);
293 1.18 christos free(opt);
294 1.18 christos *errstrp = "invalid permission hostname";
295 1.18 christos return -1;
296 1.18 christos }
297 1.18 christos /*
298 1.18 christos * don't want to use permitopen_port to avoid
299 1.18 christos * dependency on channels.[ch] here.
300 1.18 christos */
301 1.18 christos if (cp == NULL ||
302 1.18 christos (strcmp(cp, "*") != 0 && a2port(cp) <= 0)) {
303 1.18 christos free(tmp);
304 1.18 christos free(opt);
305 1.18 christos *errstrp = "invalid permission port";
306 1.18 christos return -1;
307 1.18 christos }
308 1.18 christos /* XXX - add streamlocal support */
309 1.18 christos free(tmp);
310 1.18 christos /* Record it */
311 1.18 christos if ((permits = recallocarray(permits, npermits, npermits + 1,
312 1.18 christos sizeof(*permits))) == NULL) {
313 1.18 christos free(opt);
314 1.18 christos /* NB. don't update *permitsp if alloc fails */
315 1.18 christos *errstrp = "memory allocation failed";
316 1.18 christos return -1;
317 1.18 christos }
318 1.18 christos permits[npermits++] = opt;
319 1.18 christos *permitsp = permits;
320 1.18 christos *npermitsp = npermits;
321 1.18 christos return 0;
322 1.18 christos }
323 1.18 christos
324 1.17 christos struct sshauthopt *
325 1.17 christos sshauthopt_parse(const char *opts, const char **errstrp)
326 1.17 christos {
327 1.18 christos char **oarray, *opt, *cp, *tmp;
328 1.17 christos int r;
329 1.17 christos struct sshauthopt *ret = NULL;
330 1.17 christos const char *errstr = "unknown error";
331 1.17 christos uint64_t valid_before;
332 1.27 christos size_t i, l;
333 1.17 christos
334 1.17 christos if (errstrp != NULL)
335 1.17 christos *errstrp = NULL;
336 1.17 christos if ((ret = sshauthopt_new_with_keys_defaults()) == NULL)
337 1.17 christos goto alloc_fail;
338 1.17 christos
339 1.17 christos if (opts == NULL)
340 1.17 christos return ret;
341 1.17 christos
342 1.17 christos while (*opts && *opts != ' ' && *opts != '\t') {
343 1.17 christos /* flag options */
344 1.17 christos if ((r = opt_flag("restrict", 0, &opts)) != -1) {
345 1.17 christos ret->restricted = 1;
346 1.17 christos ret->permit_port_forwarding_flag = 0;
347 1.17 christos ret->permit_agent_forwarding_flag = 0;
348 1.17 christos ret->permit_x11_forwarding_flag = 0;
349 1.17 christos ret->permit_pty_flag = 0;
350 1.17 christos ret->permit_user_rc = 0;
351 1.17 christos } else if ((r = opt_flag("cert-authority", 0, &opts)) != -1) {
352 1.17 christos ret->cert_authority = r;
353 1.17 christos } else if ((r = opt_flag("port-forwarding", 1, &opts)) != -1) {
354 1.17 christos ret->permit_port_forwarding_flag = r == 1;
355 1.17 christos } else if ((r = opt_flag("agent-forwarding", 1, &opts)) != -1) {
356 1.17 christos ret->permit_agent_forwarding_flag = r == 1;
357 1.17 christos } else if ((r = opt_flag("x11-forwarding", 1, &opts)) != -1) {
358 1.17 christos ret->permit_x11_forwarding_flag = r == 1;
359 1.22 christos } else if ((r = opt_flag("touch-required", 1, &opts)) != -1) {
360 1.22 christos ret->no_require_user_presence = r != 1; /* NB. flip */
361 1.24 christos } else if ((r = opt_flag("verify-required", 1, &opts)) != -1) {
362 1.24 christos ret->require_verify = r == 1;
363 1.17 christos } else if ((r = opt_flag("pty", 1, &opts)) != -1) {
364 1.17 christos ret->permit_pty_flag = r == 1;
365 1.17 christos } else if ((r = opt_flag("user-rc", 1, &opts)) != -1) {
366 1.17 christos ret->permit_user_rc = r == 1;
367 1.17 christos } else if (opt_match(&opts, "command")) {
368 1.17 christos if (ret->force_command != NULL) {
369 1.17 christos errstr = "multiple \"command\" clauses";
370 1.17 christos goto fail;
371 1.17 christos }
372 1.17 christos ret->force_command = opt_dequote(&opts, &errstr);
373 1.17 christos if (ret->force_command == NULL)
374 1.17 christos goto fail;
375 1.17 christos } else if (opt_match(&opts, "principals")) {
376 1.17 christos if (ret->cert_principals != NULL) {
377 1.17 christos errstr = "multiple \"principals\" clauses";
378 1.17 christos goto fail;
379 1.17 christos }
380 1.17 christos ret->cert_principals = opt_dequote(&opts, &errstr);
381 1.17 christos if (ret->cert_principals == NULL)
382 1.17 christos goto fail;
383 1.17 christos } else if (opt_match(&opts, "from")) {
384 1.17 christos if (ret->required_from_host_keys != NULL) {
385 1.17 christos errstr = "multiple \"from\" clauses";
386 1.17 christos goto fail;
387 1.17 christos }
388 1.17 christos ret->required_from_host_keys = opt_dequote(&opts,
389 1.17 christos &errstr);
390 1.17 christos if (ret->required_from_host_keys == NULL)
391 1.17 christos goto fail;
392 1.17 christos } else if (opt_match(&opts, "expiry-time")) {
393 1.17 christos if ((opt = opt_dequote(&opts, &errstr)) == NULL)
394 1.17 christos goto fail;
395 1.17 christos if (parse_absolute_time(opt, &valid_before) != 0 ||
396 1.17 christos valid_before == 0) {
397 1.17 christos free(opt);
398 1.17 christos errstr = "invalid expires time";
399 1.17 christos goto fail;
400 1.17 christos }
401 1.17 christos free(opt);
402 1.17 christos if (ret->valid_before == 0 ||
403 1.17 christos valid_before < ret->valid_before)
404 1.17 christos ret->valid_before = valid_before;
405 1.17 christos } else if (opt_match(&opts, "environment")) {
406 1.27 christos if (ret->nenv > SSH_AUTHOPT_ENV_MAX) {
407 1.17 christos errstr = "too many environment strings";
408 1.17 christos goto fail;
409 1.17 christos }
410 1.17 christos if ((opt = opt_dequote(&opts, &errstr)) == NULL)
411 1.17 christos goto fail;
412 1.17 christos /* env name must be alphanumeric and followed by '=' */
413 1.17 christos if ((tmp = strchr(opt, '=')) == NULL) {
414 1.17 christos free(opt);
415 1.17 christos errstr = "invalid environment string";
416 1.17 christos goto fail;
417 1.17 christos }
418 1.27 christos if ((cp = strdup(opt)) == NULL) {
419 1.27 christos free(opt);
420 1.20 christos goto alloc_fail;
421 1.27 christos }
422 1.27 christos l = (size_t)(tmp - opt);
423 1.27 christos cp[l] = '\0'; /* truncate at '=' */
424 1.20 christos if (!valid_env_name(cp)) {
425 1.20 christos free(cp);
426 1.20 christos free(opt);
427 1.20 christos errstr = "invalid environment string";
428 1.20 christos goto fail;
429 1.17 christos }
430 1.27 christos /* Check for duplicates; XXX O(n*log(n)) */
431 1.27 christos for (i = 0; i < ret->nenv; i++) {
432 1.27 christos if (strncmp(ret->env[i], cp, l) == 0 &&
433 1.27 christos ret->env[i][l] == '=')
434 1.27 christos break;
435 1.27 christos }
436 1.20 christos free(cp);
437 1.27 christos /* First match wins */
438 1.27 christos if (i >= ret->nenv) {
439 1.27 christos /* Append it. */
440 1.27 christos oarray = ret->env;
441 1.27 christos if ((ret->env = recallocarray(ret->env,
442 1.27 christos ret->nenv, ret->nenv + 1,
443 1.27 christos sizeof(*ret->env))) == NULL) {
444 1.27 christos free(opt);
445 1.27 christos /* put it back for cleanup */
446 1.27 christos ret->env = oarray;
447 1.27 christos goto alloc_fail;
448 1.27 christos }
449 1.27 christos ret->env[ret->nenv++] = opt;
450 1.27 christos opt = NULL; /* transferred */
451 1.17 christos }
452 1.27 christos free(opt);
453 1.17 christos } else if (opt_match(&opts, "permitopen")) {
454 1.18 christos if (handle_permit(&opts, 0, &ret->permitopen,
455 1.18 christos &ret->npermitopen, &errstr) != 0)
456 1.17 christos goto fail;
457 1.18 christos } else if (opt_match(&opts, "permitlisten")) {
458 1.18 christos if (handle_permit(&opts, 1, &ret->permitlisten,
459 1.18 christos &ret->npermitlisten, &errstr) != 0)
460 1.17 christos goto fail;
461 1.17 christos } else if (opt_match(&opts, "tunnel")) {
462 1.17 christos if ((opt = opt_dequote(&opts, &errstr)) == NULL)
463 1.17 christos goto fail;
464 1.17 christos ret->force_tun_device = a2tun(opt, NULL);
465 1.17 christos free(opt);
466 1.17 christos if (ret->force_tun_device == SSH_TUNID_ERR) {
467 1.17 christos errstr = "invalid tun device";
468 1.17 christos goto fail;
469 1.17 christos }
470 1.17 christos }
471 1.17 christos /*
472 1.17 christos * Skip the comma, and move to the next option
473 1.17 christos * (or break out if there are no more).
474 1.17 christos */
475 1.17 christos if (*opts == '\0' || *opts == ' ' || *opts == '\t')
476 1.17 christos break; /* End of options. */
477 1.17 christos /* Anything other than a comma is an unknown option */
478 1.17 christos if (*opts != ',') {
479 1.17 christos errstr = "unknown key option";
480 1.17 christos goto fail;
481 1.17 christos }
482 1.17 christos opts++;
483 1.17 christos if (*opts == '\0') {
484 1.17 christos errstr = "unexpected end-of-options";
485 1.17 christos goto fail;
486 1.17 christos }
487 1.17 christos }
488 1.17 christos
489 1.17 christos /* success */
490 1.17 christos if (errstrp != NULL)
491 1.17 christos *errstrp = NULL;
492 1.17 christos return ret;
493 1.17 christos
494 1.17 christos alloc_fail:
495 1.17 christos errstr = "memory allocation failed";
496 1.17 christos fail:
497 1.17 christos sshauthopt_free(ret);
498 1.17 christos if (errstrp != NULL)
499 1.17 christos *errstrp = errstr;
500 1.17 christos return NULL;
501 1.17 christos }
502 1.17 christos
503 1.17 christos struct sshauthopt *
504 1.17 christos sshauthopt_from_cert(struct sshkey *k)
505 1.17 christos {
506 1.17 christos struct sshauthopt *ret;
507 1.17 christos
508 1.17 christos if (k == NULL || !sshkey_type_is_cert(k->type) || k->cert == NULL ||
509 1.17 christos k->cert->type != SSH2_CERT_TYPE_USER)
510 1.17 christos return NULL;
511 1.17 christos
512 1.17 christos if ((ret = sshauthopt_new()) == NULL)
513 1.17 christos return NULL;
514 1.17 christos
515 1.17 christos /* Handle options and critical extensions separately */
516 1.17 christos if (cert_option_list(ret, k->cert->critical,
517 1.17 christos OPTIONS_CRITICAL, 1) == -1) {
518 1.17 christos sshauthopt_free(ret);
519 1.17 christos return NULL;
520 1.17 christos }
521 1.17 christos if (cert_option_list(ret, k->cert->extensions,
522 1.17 christos OPTIONS_EXTENSIONS, 0) == -1) {
523 1.17 christos sshauthopt_free(ret);
524 1.17 christos return NULL;
525 1.17 christos }
526 1.17 christos /* success */
527 1.17 christos return ret;
528 1.17 christos }
529 1.17 christos
530 1.3 adam /*
531 1.17 christos * Merges "additional" options to "primary" and returns the result.
532 1.17 christos * NB. Some options from primary have primacy.
533 1.3 adam */
534 1.17 christos struct sshauthopt *
535 1.17 christos sshauthopt_merge(const struct sshauthopt *primary,
536 1.17 christos const struct sshauthopt *additional, const char **errstrp)
537 1.3 adam {
538 1.17 christos struct sshauthopt *ret;
539 1.17 christos const char *errstr = "internal error";
540 1.17 christos const char *tmp;
541 1.17 christos
542 1.17 christos if (errstrp != NULL)
543 1.17 christos *errstrp = NULL;
544 1.17 christos
545 1.17 christos if ((ret = sshauthopt_new()) == NULL)
546 1.17 christos goto alloc_fail;
547 1.17 christos
548 1.17 christos /* cert_authority and cert_principals are cleared in result */
549 1.17 christos
550 1.17 christos /* Prefer access lists from primary. */
551 1.17 christos /* XXX err is both set and mismatch? */
552 1.17 christos tmp = primary->required_from_host_cert;
553 1.17 christos if (tmp == NULL)
554 1.17 christos tmp = additional->required_from_host_cert;
555 1.17 christos if (tmp != NULL && (ret->required_from_host_cert = strdup(tmp)) == NULL)
556 1.17 christos goto alloc_fail;
557 1.17 christos tmp = primary->required_from_host_keys;
558 1.17 christos if (tmp == NULL)
559 1.17 christos tmp = additional->required_from_host_keys;
560 1.17 christos if (tmp != NULL && (ret->required_from_host_keys = strdup(tmp)) == NULL)
561 1.17 christos goto alloc_fail;
562 1.17 christos
563 1.18 christos /*
564 1.18 christos * force_tun_device, permitopen/permitlisten and environment all
565 1.18 christos * prefer the primary.
566 1.18 christos */
567 1.17 christos ret->force_tun_device = primary->force_tun_device;
568 1.17 christos if (ret->force_tun_device == -1)
569 1.17 christos ret->force_tun_device = additional->force_tun_device;
570 1.17 christos if (primary->nenv > 0) {
571 1.17 christos if (dup_strings(&ret->env, &ret->nenv,
572 1.17 christos primary->env, primary->nenv) != 0)
573 1.17 christos goto alloc_fail;
574 1.17 christos } else if (additional->nenv) {
575 1.17 christos if (dup_strings(&ret->env, &ret->nenv,
576 1.17 christos additional->env, additional->nenv) != 0)
577 1.17 christos goto alloc_fail;
578 1.17 christos }
579 1.17 christos if (primary->npermitopen > 0) {
580 1.17 christos if (dup_strings(&ret->permitopen, &ret->npermitopen,
581 1.17 christos primary->permitopen, primary->npermitopen) != 0)
582 1.17 christos goto alloc_fail;
583 1.17 christos } else if (additional->npermitopen > 0) {
584 1.17 christos if (dup_strings(&ret->permitopen, &ret->npermitopen,
585 1.17 christos additional->permitopen, additional->npermitopen) != 0)
586 1.17 christos goto alloc_fail;
587 1.17 christos }
588 1.17 christos
589 1.18 christos if (primary->npermitlisten > 0) {
590 1.18 christos if (dup_strings(&ret->permitlisten, &ret->npermitlisten,
591 1.18 christos primary->permitlisten, primary->npermitlisten) != 0)
592 1.18 christos goto alloc_fail;
593 1.18 christos } else if (additional->npermitlisten > 0) {
594 1.18 christos if (dup_strings(&ret->permitlisten, &ret->npermitlisten,
595 1.18 christos additional->permitlisten, additional->npermitlisten) != 0)
596 1.18 christos goto alloc_fail;
597 1.18 christos }
598 1.18 christos
599 1.22 christos #define OPTFLAG_AND(x) ret->x = (primary->x == 1) && (additional->x == 1)
600 1.24 christos #define OPTFLAG_OR(x) ret->x = (primary->x == 1) || (additional->x == 1)
601 1.22 christos /* Permissive flags are logical-AND (i.e. must be set in both) */
602 1.22 christos OPTFLAG_AND(permit_port_forwarding_flag);
603 1.22 christos OPTFLAG_AND(permit_agent_forwarding_flag);
604 1.22 christos OPTFLAG_AND(permit_x11_forwarding_flag);
605 1.22 christos OPTFLAG_AND(permit_pty_flag);
606 1.22 christos OPTFLAG_AND(permit_user_rc);
607 1.22 christos OPTFLAG_AND(no_require_user_presence);
608 1.24 christos /* Restrictive flags are logical-OR (i.e. must be set in either) */
609 1.24 christos OPTFLAG_OR(require_verify);
610 1.22 christos #undef OPTFLAG_AND
611 1.17 christos
612 1.17 christos /* Earliest expiry time should win */
613 1.17 christos if (primary->valid_before != 0)
614 1.17 christos ret->valid_before = primary->valid_before;
615 1.17 christos if (additional->valid_before != 0 &&
616 1.17 christos additional->valid_before < ret->valid_before)
617 1.17 christos ret->valid_before = additional->valid_before;
618 1.1 christos
619 1.14 christos /*
620 1.17 christos * When both multiple forced-command are specified, only
621 1.17 christos * proceed if they are identical, otherwise fail.
622 1.14 christos */
623 1.17 christos if (primary->force_command != NULL &&
624 1.17 christos additional->force_command != NULL) {
625 1.17 christos if (strcmp(primary->force_command,
626 1.17 christos additional->force_command) == 0) {
627 1.17 christos /* ok */
628 1.17 christos ret->force_command = strdup(primary->force_command);
629 1.17 christos if (ret->force_command == NULL)
630 1.17 christos goto alloc_fail;
631 1.14 christos } else {
632 1.17 christos errstr = "forced command options do not match";
633 1.17 christos goto fail;
634 1.14 christos }
635 1.17 christos } else if (primary->force_command != NULL) {
636 1.17 christos if ((ret->force_command = strdup(
637 1.17 christos primary->force_command)) == NULL)
638 1.17 christos goto alloc_fail;
639 1.17 christos } else if (additional->force_command != NULL) {
640 1.17 christos if ((ret->force_command = strdup(
641 1.17 christos additional->force_command)) == NULL)
642 1.17 christos goto alloc_fail;
643 1.17 christos }
644 1.14 christos /* success */
645 1.17 christos if (errstrp != NULL)
646 1.17 christos *errstrp = NULL;
647 1.17 christos return ret;
648 1.17 christos
649 1.17 christos alloc_fail:
650 1.17 christos errstr = "memory allocation failed";
651 1.17 christos fail:
652 1.17 christos if (errstrp != NULL)
653 1.17 christos *errstrp = errstr;
654 1.17 christos sshauthopt_free(ret);
655 1.17 christos return NULL;
656 1.17 christos }
657 1.17 christos
658 1.17 christos /*
659 1.17 christos * Copy options
660 1.17 christos */
661 1.17 christos struct sshauthopt *
662 1.17 christos sshauthopt_copy(const struct sshauthopt *orig)
663 1.17 christos {
664 1.17 christos struct sshauthopt *ret;
665 1.17 christos
666 1.17 christos if ((ret = sshauthopt_new()) == NULL)
667 1.17 christos return NULL;
668 1.17 christos
669 1.17 christos #define OPTSCALAR(x) ret->x = orig->x
670 1.17 christos OPTSCALAR(permit_port_forwarding_flag);
671 1.17 christos OPTSCALAR(permit_agent_forwarding_flag);
672 1.17 christos OPTSCALAR(permit_x11_forwarding_flag);
673 1.17 christos OPTSCALAR(permit_pty_flag);
674 1.17 christos OPTSCALAR(permit_user_rc);
675 1.17 christos OPTSCALAR(restricted);
676 1.17 christos OPTSCALAR(cert_authority);
677 1.17 christos OPTSCALAR(force_tun_device);
678 1.17 christos OPTSCALAR(valid_before);
679 1.22 christos OPTSCALAR(no_require_user_presence);
680 1.24 christos OPTSCALAR(require_verify);
681 1.17 christos #undef OPTSCALAR
682 1.17 christos #define OPTSTRING(x) \
683 1.17 christos do { \
684 1.17 christos if (orig->x != NULL && (ret->x = strdup(orig->x)) == NULL) { \
685 1.17 christos sshauthopt_free(ret); \
686 1.17 christos return NULL; \
687 1.17 christos } \
688 1.17 christos } while (0)
689 1.17 christos OPTSTRING(cert_principals);
690 1.17 christos OPTSTRING(force_command);
691 1.17 christos OPTSTRING(required_from_host_cert);
692 1.17 christos OPTSTRING(required_from_host_keys);
693 1.17 christos #undef OPTSTRING
694 1.17 christos
695 1.17 christos if (dup_strings(&ret->env, &ret->nenv, orig->env, orig->nenv) != 0 ||
696 1.17 christos dup_strings(&ret->permitopen, &ret->npermitopen,
697 1.18 christos orig->permitopen, orig->npermitopen) != 0 ||
698 1.18 christos dup_strings(&ret->permitlisten, &ret->npermitlisten,
699 1.18 christos orig->permitlisten, orig->npermitlisten) != 0) {
700 1.17 christos sshauthopt_free(ret);
701 1.17 christos return NULL;
702 1.17 christos }
703 1.17 christos return ret;
704 1.17 christos }
705 1.17 christos
706 1.17 christos static int
707 1.17 christos serialise_array(struct sshbuf *m, char **a, size_t n)
708 1.17 christos {
709 1.17 christos struct sshbuf *b;
710 1.17 christos size_t i;
711 1.29 christos int r = SSH_ERR_INTERNAL_ERROR;
712 1.17 christos
713 1.17 christos if (n > INT_MAX)
714 1.17 christos return SSH_ERR_INTERNAL_ERROR;
715 1.17 christos
716 1.17 christos if ((b = sshbuf_new()) == NULL) {
717 1.17 christos return SSH_ERR_ALLOC_FAIL;
718 1.17 christos }
719 1.17 christos for (i = 0; i < n; i++) {
720 1.29 christos if ((r = sshbuf_put_cstring(b, a[i])) != 0)
721 1.29 christos goto out;
722 1.17 christos }
723 1.17 christos if ((r = sshbuf_put_u32(m, n)) != 0 ||
724 1.29 christos (r = sshbuf_put_stringb(m, b)) != 0)
725 1.29 christos goto out;
726 1.17 christos /* success */
727 1.29 christos r = 0;
728 1.29 christos out:
729 1.29 christos sshbuf_free(b);
730 1.29 christos return r;
731 1.17 christos }
732 1.17 christos
733 1.17 christos static int
734 1.17 christos deserialise_array(struct sshbuf *m, char ***ap, size_t *np)
735 1.17 christos {
736 1.17 christos char **a = NULL;
737 1.17 christos size_t i, n = 0;
738 1.17 christos struct sshbuf *b = NULL;
739 1.17 christos u_int tmp;
740 1.17 christos int r = SSH_ERR_INTERNAL_ERROR;
741 1.17 christos
742 1.17 christos if ((r = sshbuf_get_u32(m, &tmp)) != 0 ||
743 1.17 christos (r = sshbuf_froms(m, &b)) != 0)
744 1.17 christos goto out;
745 1.17 christos if (tmp > INT_MAX) {
746 1.17 christos r = SSH_ERR_INVALID_FORMAT;
747 1.17 christos goto out;
748 1.17 christos }
749 1.17 christos n = tmp;
750 1.17 christos if (n > 0 && (a = calloc(n, sizeof(*a))) == NULL) {
751 1.17 christos r = SSH_ERR_ALLOC_FAIL;
752 1.17 christos goto out;
753 1.17 christos }
754 1.17 christos for (i = 0; i < n; i++) {
755 1.17 christos if ((r = sshbuf_get_cstring(b, &a[i], NULL)) != 0)
756 1.17 christos goto out;
757 1.17 christos }
758 1.17 christos /* success */
759 1.17 christos r = 0;
760 1.17 christos *ap = a;
761 1.17 christos a = NULL;
762 1.17 christos *np = n;
763 1.17 christos n = 0;
764 1.17 christos out:
765 1.23 christos if (a != NULL) {
766 1.23 christos for (i = 0; i < n; i++)
767 1.23 christos free(a[i]);
768 1.23 christos free(a);
769 1.23 christos }
770 1.17 christos sshbuf_free(b);
771 1.17 christos return r;
772 1.17 christos }
773 1.17 christos
774 1.17 christos static int
775 1.17 christos serialise_nullable_string(struct sshbuf *m, const char *s)
776 1.17 christos {
777 1.17 christos int r;
778 1.17 christos
779 1.17 christos if ((r = sshbuf_put_u8(m, s == NULL)) != 0 ||
780 1.17 christos (r = sshbuf_put_cstring(m, s)) != 0)
781 1.17 christos return r;
782 1.17 christos return 0;
783 1.17 christos }
784 1.17 christos
785 1.17 christos static int
786 1.17 christos deserialise_nullable_string(struct sshbuf *m, char **sp)
787 1.17 christos {
788 1.17 christos int r;
789 1.17 christos u_char flag;
790 1.17 christos
791 1.17 christos *sp = NULL;
792 1.17 christos if ((r = sshbuf_get_u8(m, &flag)) != 0 ||
793 1.17 christos (r = sshbuf_get_cstring(m, flag ? NULL : sp, NULL)) != 0)
794 1.17 christos return r;
795 1.1 christos return 0;
796 1.1 christos }
797 1.3 adam
798 1.17 christos int
799 1.17 christos sshauthopt_serialise(const struct sshauthopt *opts, struct sshbuf *m,
800 1.17 christos int untrusted)
801 1.17 christos {
802 1.17 christos int r = SSH_ERR_INTERNAL_ERROR;
803 1.17 christos
804 1.22 christos /* Flag options */
805 1.17 christos if ((r = sshbuf_put_u8(m, opts->permit_port_forwarding_flag)) != 0 ||
806 1.17 christos (r = sshbuf_put_u8(m, opts->permit_agent_forwarding_flag)) != 0 ||
807 1.17 christos (r = sshbuf_put_u8(m, opts->permit_x11_forwarding_flag)) != 0 ||
808 1.17 christos (r = sshbuf_put_u8(m, opts->permit_pty_flag)) != 0 ||
809 1.17 christos (r = sshbuf_put_u8(m, opts->permit_user_rc)) != 0 ||
810 1.17 christos (r = sshbuf_put_u8(m, opts->restricted)) != 0 ||
811 1.17 christos (r = sshbuf_put_u8(m, opts->cert_authority)) != 0 ||
812 1.24 christos (r = sshbuf_put_u8(m, opts->no_require_user_presence)) != 0 ||
813 1.24 christos (r = sshbuf_put_u8(m, opts->require_verify)) != 0)
814 1.22 christos return r;
815 1.22 christos
816 1.22 christos /* Simple integer options */
817 1.22 christos if ((r = sshbuf_put_u64(m, opts->valid_before)) != 0)
818 1.17 christos return r;
819 1.17 christos
820 1.17 christos /* tunnel number can be negative to indicate "unset" */
821 1.17 christos if ((r = sshbuf_put_u8(m, opts->force_tun_device == -1)) != 0 ||
822 1.17 christos (r = sshbuf_put_u32(m, (opts->force_tun_device < 0) ?
823 1.17 christos 0 : (u_int)opts->force_tun_device)) != 0)
824 1.17 christos return r;
825 1.17 christos
826 1.17 christos /* String options; these may be NULL */
827 1.17 christos if ((r = serialise_nullable_string(m,
828 1.17 christos untrusted ? "yes" : opts->cert_principals)) != 0 ||
829 1.17 christos (r = serialise_nullable_string(m,
830 1.17 christos untrusted ? "true" : opts->force_command)) != 0 ||
831 1.17 christos (r = serialise_nullable_string(m,
832 1.17 christos untrusted ? NULL : opts->required_from_host_cert)) != 0 ||
833 1.17 christos (r = serialise_nullable_string(m,
834 1.26 christos untrusted ? NULL : opts->required_from_host_keys)) != 0)
835 1.17 christos return r;
836 1.17 christos
837 1.17 christos /* Array options */
838 1.17 christos if ((r = serialise_array(m, opts->env,
839 1.17 christos untrusted ? 0 : opts->nenv)) != 0 ||
840 1.17 christos (r = serialise_array(m, opts->permitopen,
841 1.18 christos untrusted ? 0 : opts->npermitopen)) != 0 ||
842 1.18 christos (r = serialise_array(m, opts->permitlisten,
843 1.18 christos untrusted ? 0 : opts->npermitlisten)) != 0)
844 1.17 christos return r;
845 1.17 christos
846 1.17 christos /* success */
847 1.17 christos return 0;
848 1.17 christos }
849 1.17 christos
850 1.17 christos int
851 1.17 christos sshauthopt_deserialise(struct sshbuf *m, struct sshauthopt **optsp)
852 1.17 christos {
853 1.17 christos struct sshauthopt *opts = NULL;
854 1.17 christos int r = SSH_ERR_INTERNAL_ERROR;
855 1.17 christos u_char f;
856 1.17 christos u_int tmp;
857 1.17 christos
858 1.17 christos if ((opts = calloc(1, sizeof(*opts))) == NULL)
859 1.17 christos return SSH_ERR_ALLOC_FAIL;
860 1.17 christos
861 1.22 christos /* Flag options */
862 1.17 christos #define OPT_FLAG(x) \
863 1.17 christos do { \
864 1.17 christos if ((r = sshbuf_get_u8(m, &f)) != 0) \
865 1.17 christos goto out; \
866 1.17 christos opts->x = f; \
867 1.17 christos } while (0)
868 1.17 christos OPT_FLAG(permit_port_forwarding_flag);
869 1.17 christos OPT_FLAG(permit_agent_forwarding_flag);
870 1.17 christos OPT_FLAG(permit_x11_forwarding_flag);
871 1.17 christos OPT_FLAG(permit_pty_flag);
872 1.17 christos OPT_FLAG(permit_user_rc);
873 1.17 christos OPT_FLAG(restricted);
874 1.17 christos OPT_FLAG(cert_authority);
875 1.22 christos OPT_FLAG(no_require_user_presence);
876 1.24 christos OPT_FLAG(require_verify);
877 1.17 christos #undef OPT_FLAG
878 1.17 christos
879 1.22 christos /* Simple integer options */
880 1.17 christos if ((r = sshbuf_get_u64(m, &opts->valid_before)) != 0)
881 1.17 christos goto out;
882 1.17 christos
883 1.17 christos /* tunnel number can be negative to indicate "unset" */
884 1.17 christos if ((r = sshbuf_get_u8(m, &f)) != 0 ||
885 1.17 christos (r = sshbuf_get_u32(m, &tmp)) != 0)
886 1.17 christos goto out;
887 1.17 christos opts->force_tun_device = f ? -1 : (int)tmp;
888 1.17 christos
889 1.17 christos /* String options may be NULL */
890 1.17 christos if ((r = deserialise_nullable_string(m, &opts->cert_principals)) != 0 ||
891 1.17 christos (r = deserialise_nullable_string(m, &opts->force_command)) != 0 ||
892 1.17 christos (r = deserialise_nullable_string(m,
893 1.17 christos &opts->required_from_host_cert)) != 0 ||
894 1.17 christos (r = deserialise_nullable_string(m,
895 1.17 christos &opts->required_from_host_keys)) != 0)
896 1.17 christos goto out;
897 1.17 christos
898 1.17 christos /* Array options */
899 1.17 christos if ((r = deserialise_array(m, &opts->env, &opts->nenv)) != 0 ||
900 1.17 christos (r = deserialise_array(m,
901 1.18 christos &opts->permitopen, &opts->npermitopen)) != 0 ||
902 1.18 christos (r = deserialise_array(m,
903 1.18 christos &opts->permitlisten, &opts->npermitlisten)) != 0)
904 1.17 christos goto out;
905 1.17 christos
906 1.17 christos /* success */
907 1.17 christos r = 0;
908 1.17 christos *optsp = opts;
909 1.17 christos opts = NULL;
910 1.17 christos out:
911 1.17 christos sshauthopt_free(opts);
912 1.17 christos return r;
913 1.17 christos }
914