Coverage Report

Created: 2026-10-17 06:39

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