Coverage Report

Created: 2026-05-30 06:22

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.102 2025/09/15 04:38:00 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
291
{
46
291
  char **dst;
47
291
  size_t i, j;
48
49
291
  *dstp = NULL;
50
291
  *ndstp = 0;
51
52
291
  if (nsrc == 0)
53
0
    return 0;
54
291
  if (nsrc >= SIZE_MAX / sizeof(*src) ||
55
291
      (dst = calloc(nsrc, sizeof(*src))) == NULL)
56
0
    return -1;
57
60.7k
  for (i = 0; i < nsrc; i++) {
58
60.4k
    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
60.4k
  }
65
  /* success */
66
291
  *dstp = dst;
67
291
  *ndstp = nsrc;
68
291
  return 0;
69
291
}
70
71
0
#define OPTIONS_CRITICAL  1
72
0
#define OPTIONS_EXTENSIONS  2
73
static int
74
cert_option_list(struct sshauthopt *opts, struct sshbuf *oblob,
75
    u_int which, int crit)
76
0
{
77
0
  char *command, *allowed;
78
0
  char *name = NULL;
79
0
  struct sshbuf *c = NULL, *data = NULL;
80
0
  int r, ret = -1, found;
81
82
0
  if ((c = sshbuf_fromb(oblob)) == NULL) {
83
0
    error_f("sshbuf_fromb failed");
84
0
    goto out;
85
0
  }
86
87
0
  while (sshbuf_len(c) > 0) {
88
0
    sshbuf_free(data);
89
0
    data = NULL;
90
0
    if ((r = sshbuf_get_cstring(c, &name, NULL)) != 0 ||
91
0
        (r = sshbuf_froms(c, &data)) != 0) {
92
0
      error_r(r, "Unable to parse certificate options");
93
0
      goto out;
94
0
    }
95
0
    debug3("found certificate option \"%.100s\" len %zu",
96
0
        name, sshbuf_len(data));
97
0
    found = 0;
98
0
    if ((which & OPTIONS_EXTENSIONS) != 0) {
99
0
      if (strcmp(name, "no-touch-required") == 0) {
100
0
        opts->no_require_user_presence = 1;
101
0
        found = 1;
102
0
      } else if (strcmp(name, "permit-X11-forwarding") == 0) {
103
0
        opts->permit_x11_forwarding_flag = 1;
104
0
        found = 1;
105
0
      } else if (strcmp(name,
106
0
          "permit-agent-forwarding") == 0) {
107
0
        opts->permit_agent_forwarding_flag = 1;
108
0
        found = 1;
109
0
      } else if (strcmp(name,
110
0
          "permit-port-forwarding") == 0) {
111
0
        opts->permit_port_forwarding_flag = 1;
112
0
        found = 1;
113
0
      } else if (strcmp(name, "permit-pty") == 0) {
114
0
        opts->permit_pty_flag = 1;
115
0
        found = 1;
116
0
      } else if (strcmp(name, "permit-user-rc") == 0) {
117
0
        opts->permit_user_rc = 1;
118
0
        found = 1;
119
0
      }
120
0
    }
121
0
    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
0
    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
0
    } 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
0
    free(name);
180
0
    name = NULL;
181
0
  }
182
  /* successfully parsed all options */
183
0
  ret = 0;
184
185
0
 out:
186
0
  free(name);
187
0
  sshbuf_free(data);
188
0
  sshbuf_free(c);
189
0
  return ret;
190
0
}
191
192
struct sshauthopt *
193
sshauthopt_new(void)
194
4.55k
{
195
4.55k
  struct sshauthopt *ret;
196
197
4.55k
  if ((ret = calloc(1, sizeof(*ret))) == NULL)
198
0
    return NULL;
199
4.55k
  ret->force_tun_device = -1;
200
4.55k
  return ret;
201
4.55k
}
202
203
void
204
sshauthopt_free(struct sshauthopt *opts)
205
7.05k
{
206
7.05k
  size_t i;
207
208
7.05k
  if (opts == NULL)
209
2.50k
    return;
210
211
4.55k
  free(opts->cert_principals);
212
4.55k
  free(opts->force_command);
213
4.55k
  free(opts->required_from_host_cert);
214
4.55k
  free(opts->required_from_host_keys);
215
216
7.81k
  for (i = 0; i < opts->nenv; i++)
217
3.26k
    free(opts->env[i]);
218
4.55k
  free(opts->env);
219
220
123k
  for (i = 0; i < opts->npermitopen; i++)
221
119k
    free(opts->permitopen[i]);
222
4.55k
  free(opts->permitopen);
223
224
42.6k
  for (i = 0; i < opts->npermitlisten; i++)
225
38.0k
    free(opts->permitlisten[i]);
226
4.55k
  free(opts->permitlisten);
227
228
4.55k
  freezero(opts, sizeof(*opts));
229
4.55k
}
230
231
struct sshauthopt *
232
sshauthopt_new_with_keys_defaults(void)
233
1.93k
{
234
1.93k
  struct sshauthopt *ret = NULL;
235
236
1.93k
  if ((ret = sshauthopt_new()) == NULL)
237
0
    return NULL;
238
239
  /* Defaults for authorized_keys flags */
240
1.93k
  ret->permit_port_forwarding_flag = 1;
241
1.93k
  ret->permit_agent_forwarding_flag = 1;
242
1.93k
  ret->permit_x11_forwarding_flag = 1;
243
1.93k
  ret->permit_pty_flag = 1;
244
1.93k
  ret->permit_user_rc = 1;
245
1.93k
  return ret;
246
1.93k
}
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
98.5k
{
256
98.5k
  char *opt, *tmp, *cp, *host, **permits = *permitsp;
257
98.5k
  size_t npermits = *npermitsp;
258
98.5k
  const char *errstr = "unknown error";
259
260
98.5k
  if (npermits > SSH_AUTHOPT_PERMIT_MAX) {
261
1
    *errstrp = "too many permission directives";
262
1
    return -1;
263
1
  }
264
98.5k
  if ((opt = opt_dequote(optsp, &errstr)) == NULL) {
265
17
    return -1;
266
17
  }
267
98.5k
  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
21.0k
    if (asprintf(&tmp, "*:%s", opt) == -1) {
273
0
      free(opt);
274
0
      *errstrp = "memory allocation failed";
275
0
      return -1;
276
0
    }
277
21.0k
    free(opt);
278
21.0k
    opt = tmp;
279
21.0k
  }
280
98.5k
  if ((tmp = strdup(opt)) == NULL) {
281
0
    free(opt);
282
0
    *errstrp = "memory allocation failed";
283
0
    return -1;
284
0
  }
285
98.5k
  cp = tmp;
286
  /* validate syntax before recording it. */
287
98.5k
  host = hpdelim2(&cp, NULL);
288
98.5k
  if (host == NULL || strlen(host) >= NI_MAXHOST) {
289
11
    free(tmp);
290
11
    free(opt);
291
11
    *errstrp = "invalid permission hostname";
292
11
    return -1;
293
11
  }
294
  /*
295
   * don't want to use permitopen_port to avoid
296
   * dependency on channels.[ch] here.
297
   */
298
98.5k
  if (cp == NULL ||
299
98.4k
      (strcmp(cp, "*") != 0 && a2port(cp) <= 0)) {
300
289
    free(tmp);
301
289
    free(opt);
302
289
    *errstrp = "invalid permission port";
303
289
    return -1;
304
289
  }
305
  /* XXX - add streamlocal support */
306
98.2k
  free(tmp);
307
  /* Record it */
308
98.2k
  if ((permits = recallocarray(permits, npermits, npermits + 1,
309
98.2k
      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
98.2k
  permits[npermits++] = opt;
316
98.2k
  *permitsp = permits;
317
98.2k
  *npermitsp = npermits;
318
98.2k
  return 0;
319
98.2k
}
320
321
struct sshauthopt *
322
sshauthopt_parse(const char *opts, const char **errstrp)
323
1.93k
{
324
1.93k
  char **oarray, *opt, *cp, *tmp;
325
1.93k
  int r;
326
1.93k
  struct sshauthopt *ret = NULL;
327
1.93k
  const char *errstr = "unknown error";
328
1.93k
  uint64_t valid_before;
329
1.93k
  size_t i, l;
330
331
1.93k
  if (errstrp != NULL)
332
0
    *errstrp = NULL;
333
1.93k
  if ((ret = sshauthopt_new_with_keys_defaults()) == NULL)
334
0
    goto alloc_fail;
335
336
1.93k
  if (opts == NULL)
337
0
    return ret;
338
339
136k
  while (*opts && *opts != ' ' && *opts != '\t') {
340
    /* flag options */
341
136k
    if ((r = opt_flag("restrict", 0, &opts)) != -1) {
342
195
      ret->restricted = 1;
343
195
      ret->permit_port_forwarding_flag = 0;
344
195
      ret->permit_agent_forwarding_flag = 0;
345
195
      ret->permit_x11_forwarding_flag = 0;
346
195
      ret->permit_pty_flag = 0;
347
195
      ret->permit_user_rc = 0;
348
136k
    } else if ((r = opt_flag("cert-authority", 0, &opts)) != -1) {
349
194
      ret->cert_authority = r;
350
136k
    } else if ((r = opt_flag("port-forwarding", 1, &opts)) != -1) {
351
195
      ret->permit_port_forwarding_flag = r == 1;
352
136k
    } else if ((r = opt_flag("agent-forwarding", 1, &opts)) != -1) {
353
196
      ret->permit_agent_forwarding_flag = r == 1;
354
135k
    } else if ((r = opt_flag("x11-forwarding", 1, &opts)) != -1) {
355
195
      ret->permit_x11_forwarding_flag = r == 1;
356
135k
    } else if ((r = opt_flag("touch-required", 1, &opts)) != -1) {
357
195
      ret->no_require_user_presence = r != 1; /* NB. flip */
358
135k
    } else if ((r = opt_flag("verify-required", 1, &opts)) != -1) {
359
196
      ret->require_verify = r == 1;
360
135k
    } else if ((r = opt_flag("pty", 1, &opts)) != -1) {
361
222
      ret->permit_pty_flag = r == 1;
362
135k
    } else if ((r = opt_flag("user-rc", 1, &opts)) != -1) {
363
197
      ret->permit_user_rc = r == 1;
364
134k
    } else if (opt_match(&opts, "command")) {
365
4
      if (ret->force_command != NULL) {
366
1
        errstr = "multiple \"command\" clauses";
367
1
        goto fail;
368
1
      }
369
3
      ret->force_command = opt_dequote(&opts, &errstr);
370
3
      if (ret->force_command == NULL)
371
1
        goto fail;
372
134k
    } else if (opt_match(&opts, "principals")) {
373
6
      if (ret->cert_principals != NULL) {
374
1
        errstr = "multiple \"principals\" clauses";
375
1
        goto fail;
376
1
      }
377
5
      ret->cert_principals = opt_dequote(&opts, &errstr);
378
5
      if (ret->cert_principals == NULL)
379
1
        goto fail;
380
134k
    } else if (opt_match(&opts, "from")) {
381
86
      if (ret->required_from_host_keys != NULL) {
382
5
        errstr = "multiple \"from\" clauses";
383
5
        goto fail;
384
5
      }
385
81
      ret->required_from_host_keys = opt_dequote(&opts,
386
81
          &errstr);
387
81
      if (ret->required_from_host_keys == NULL)
388
63
        goto fail;
389
134k
    } else if (opt_match(&opts, "expiry-time")) {
390
2.40k
      if ((opt = opt_dequote(&opts, &errstr)) == NULL)
391
1
        goto fail;
392
2.39k
      if (parse_absolute_time(opt, &valid_before) != 0 ||
393
2.17k
          valid_before == 0) {
394
227
        free(opt);
395
227
        errstr = "invalid expires time";
396
227
        goto fail;
397
227
      }
398
2.17k
      free(opt);
399
2.17k
      if (ret->valid_before == 0 ||
400
1.94k
          valid_before < ret->valid_before)
401
386
        ret->valid_before = valid_before;
402
132k
    } else if (opt_match(&opts, "environment")) {
403
32.0k
      if (ret->nenv > SSH_AUTHOPT_ENV_MAX) {
404
0
        errstr = "too many environment strings";
405
0
        goto fail;
406
0
      }
407
32.0k
      if ((opt = opt_dequote(&opts, &errstr)) == NULL)
408
6
        goto fail;
409
      /* env name must be alphanumeric and followed by '=' */
410
32.0k
      if ((tmp = strchr(opt, '=')) == NULL) {
411
1
        free(opt);
412
1
        errstr = "invalid environment string";
413
1
        goto fail;
414
1
      }
415
32.0k
      if ((cp = strdup(opt)) == NULL) {
416
0
        free(opt);
417
0
        goto alloc_fail;
418
0
      }
419
32.0k
      l = (size_t)(tmp - opt);
420
32.0k
      cp[l] = '\0'; /* truncate at '=' */
421
32.0k
      if (!valid_env_name(cp)) {
422
17
        free(cp);
423
17
        free(opt);
424
17
        errstr = "invalid environment string";
425
17
        goto fail;
426
17
      }
427
      /* Check for duplicates; XXX O(n*log(n)) */
428
100k
      for (i = 0; i < ret->nenv; i++) {
429
98.6k
        if (strncmp(ret->env[i], cp, l) == 0 &&
430
30.9k
            ret->env[i][l] == '=')
431
30.3k
          break;
432
98.6k
      }
433
32.0k
      free(cp);
434
      /* First match wins */
435
32.0k
      if (i >= ret->nenv) {
436
        /* Append it. */
437
1.65k
        oarray = ret->env;
438
1.65k
        if ((ret->env = recallocarray(ret->env,
439
1.65k
            ret->nenv, ret->nenv + 1,
440
1.65k
            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.65k
        ret->env[ret->nenv++] = opt;
447
1.65k
        opt = NULL; /* transferred */
448
1.65k
      }
449
32.0k
      free(opt);
450
100k
    } else if (opt_match(&opts, "permitopen")) {
451
77.2k
      if (handle_permit(&opts, 0, &ret->permitopen,
452
77.2k
          &ret->npermitopen, &errstr) != 0)
453
293
        goto fail;
454
77.2k
    } else if (opt_match(&opts, "permitlisten")) {
455
21.2k
      if (handle_permit(&opts, 1, &ret->permitlisten,
456
21.2k
          &ret->npermitlisten, &errstr) != 0)
457
25
        goto fail;
458
21.2k
    } else if (opt_match(&opts, "tunnel")) {
459
1.08k
      if ((opt = opt_dequote(&opts, &errstr)) == NULL)
460
7
        goto fail;
461
1.07k
      ret->force_tun_device = a2tun(opt, NULL);
462
1.07k
      free(opt);
463
1.07k
      if (ret->force_tun_device == SSH_TUNID_ERR) {
464
269
        errstr = "invalid tun device";
465
269
        goto fail;
466
269
      }
467
1.07k
    }
468
    /*
469
     * Skip the comma, and move to the next option
470
     * (or break out if there are no more).
471
     */
472
135k
    if (*opts == '\0' || *opts == ' ' || *opts == '\t')
473
681
      break;    /* End of options. */
474
    /* Anything other than a comma is an unknown option */
475
135k
    if (*opts != ',') {
476
327
      errstr = "unknown key option";
477
327
      goto fail;
478
327
    }
479
134k
    opts++;
480
134k
    if (*opts == '\0') {
481
5
      errstr = "unexpected end-of-options";
482
5
      goto fail;
483
5
    }
484
134k
  }
485
486
  /* success */
487
685
  if (errstrp != NULL)
488
0
    *errstrp = NULL;
489
685
  return ret;
490
491
0
alloc_fail:
492
0
  errstr = "memory allocation failed";
493
1.25k
fail:
494
1.25k
  sshauthopt_free(ret);
495
1.25k
  if (errstrp != NULL)
496
0
    *errstrp = errstr;
497
1.25k
  return NULL;
498
0
}
499
500
struct sshauthopt *
501
sshauthopt_from_cert(struct sshkey *k)
502
0
{
503
0
  struct sshauthopt *ret;
504
505
0
  if (k == NULL || !sshkey_type_is_cert(k->type) || k->cert == NULL ||
506
0
      k->cert->type != SSH2_CERT_TYPE_USER)
507
0
    return NULL;
508
509
0
  if ((ret = sshauthopt_new()) == NULL)
510
0
    return NULL;
511
512
  /* Handle options and critical extensions separately */
513
0
  if (cert_option_list(ret, k->cert->critical,
514
0
      OPTIONS_CRITICAL, 1) == -1) {
515
0
    sshauthopt_free(ret);
516
0
    return NULL;
517
0
  }
518
0
  if (cert_option_list(ret, k->cert->extensions,
519
0
      OPTIONS_EXTENSIONS, 0) == -1) {
520
0
    sshauthopt_free(ret);
521
0
    return NULL;
522
0
  }
523
  /* success */
524
0
  return ret;
525
0
}
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
685
{
535
685
  struct sshauthopt *ret;
536
685
  const char *errstr = "internal error";
537
685
  const char *tmp;
538
539
685
  if (errstrp != NULL)
540
0
    *errstrp = NULL;
541
542
685
  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
685
  tmp = primary->required_from_host_cert;
550
685
  if (tmp == NULL)
551
685
    tmp = additional->required_from_host_cert;
552
685
  if (tmp != NULL && (ret->required_from_host_cert = strdup(tmp)) == NULL)
553
0
    goto alloc_fail;
554
685
  tmp = primary->required_from_host_keys;
555
685
  if (tmp == NULL)
556
684
    tmp = additional->required_from_host_keys;
557
685
  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
685
  ret->force_tun_device = primary->force_tun_device;
565
685
  if (ret->force_tun_device == -1)
566
564
    ret->force_tun_device = additional->force_tun_device;
567
685
  if (primary->nenv > 0) {
568
187
    if (dup_strings(&ret->env, &ret->nenv,
569
187
        primary->env, primary->nenv) != 0)
570
0
      goto alloc_fail;
571
498
  } 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
685
  if (primary->npermitopen > 0) {
577
68
    if (dup_strings(&ret->permitopen, &ret->npermitopen,
578
68
        primary->permitopen, primary->npermitopen) != 0)
579
0
      goto alloc_fail;
580
617
  } 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
685
  if (primary->npermitlisten > 0) {
587
36
    if (dup_strings(&ret->permitlisten, &ret->npermitlisten,
588
36
        primary->permitlisten, primary->npermitlisten) != 0)
589
0
      goto alloc_fail;
590
649
  } 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
4.11k
#define OPTFLAG_AND(x) ret->x = (primary->x == 1) && (additional->x == 1)
597
685
#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
685
  OPTFLAG_AND(permit_port_forwarding_flag);
600
685
  OPTFLAG_AND(permit_agent_forwarding_flag);
601
685
  OPTFLAG_AND(permit_x11_forwarding_flag);
602
685
  OPTFLAG_AND(permit_pty_flag);
603
685
  OPTFLAG_AND(permit_user_rc);
604
685
  OPTFLAG_AND(no_require_user_presence);
605
  /* Restrictive flags are logical-OR (i.e. must be set in either) */
606
685
  OPTFLAG_OR(require_verify);
607
685
#undef OPTFLAG_AND
608
609
  /* Earliest expiry time should win */
610
685
  if (primary->valid_before != 0)
611
187
    ret->valid_before = primary->valid_before;
612
685
  if (additional->valid_before != 0 &&
613
0
      additional->valid_before < ret->valid_before)
614
0
    ret->valid_before = additional->valid_before;
615
616
  /*
617
   * When both multiple forced-command are specified, only
618
   * proceed if they are identical, otherwise fail.
619
   */
620
685
  if (primary->force_command != NULL &&
621
1
      additional->force_command != NULL) {
622
0
    if (strcmp(primary->force_command,
623
0
        additional->force_command) == 0) {
624
      /* ok */
625
0
      ret->force_command = strdup(primary->force_command);
626
0
      if (ret->force_command == NULL)
627
0
        goto alloc_fail;
628
0
    } else {
629
0
      errstr = "forced command options do not match";
630
0
      goto fail;
631
0
    }
632
685
  } else if (primary->force_command != NULL) {
633
1
    if ((ret->force_command = strdup(
634
1
        primary->force_command)) == NULL)
635
0
      goto alloc_fail;
636
684
  } else if (additional->force_command != NULL) {
637
0
    if ((ret->force_command = strdup(
638
0
        additional->force_command)) == NULL)
639
0
      goto alloc_fail;
640
0
  }
641
  /* success */
642
685
  if (errstrp != NULL)
643
0
    *errstrp = NULL;
644
685
  return ret;
645
646
0
 alloc_fail:
647
0
  errstr = "memory allocation failed";
648
0
 fail:
649
0
  if (errstrp != NULL)
650
0
    *errstrp = errstr;
651
0
  sshauthopt_free(ret);
652
0
  return NULL;
653
0
}
654
655
/*
656
 * Copy options
657
 */
658
struct sshauthopt *
659
sshauthopt_copy(const struct sshauthopt *orig)
660
0
{
661
0
  struct sshauthopt *ret;
662
663
0
  if ((ret = sshauthopt_new()) == NULL)
664
0
    return NULL;
665
666
0
#define OPTSCALAR(x) ret->x = orig->x
667
0
  OPTSCALAR(permit_port_forwarding_flag);
668
0
  OPTSCALAR(permit_agent_forwarding_flag);
669
0
  OPTSCALAR(permit_x11_forwarding_flag);
670
0
  OPTSCALAR(permit_pty_flag);
671
0
  OPTSCALAR(permit_user_rc);
672
0
  OPTSCALAR(restricted);
673
0
  OPTSCALAR(cert_authority);
674
0
  OPTSCALAR(force_tun_device);
675
0
  OPTSCALAR(valid_before);
676
0
  OPTSCALAR(no_require_user_presence);
677
0
  OPTSCALAR(require_verify);
678
0
#undef OPTSCALAR
679
0
#define OPTSTRING(x) \
680
0
  do { \
681
0
    if (orig->x != NULL && (ret->x = strdup(orig->x)) == NULL) { \
682
0
      sshauthopt_free(ret); \
683
0
      return NULL; \
684
0
    } \
685
0
  } while (0)
686
0
  OPTSTRING(cert_principals);
687
0
  OPTSTRING(force_command);
688
0
  OPTSTRING(required_from_host_cert);
689
0
  OPTSTRING(required_from_host_keys);
690
0
#undef OPTSTRING
691
692
0
  if (dup_strings(&ret->env, &ret->nenv, orig->env, orig->nenv) != 0 ||
693
0
      dup_strings(&ret->permitopen, &ret->npermitopen,
694
0
      orig->permitopen, orig->npermitopen) != 0 ||
695
0
      dup_strings(&ret->permitlisten, &ret->npermitlisten,
696
0
      orig->permitlisten, orig->npermitlisten) != 0) {
697
0
    sshauthopt_free(ret);
698
0
    return NULL;
699
0
  }
700
0
  return ret;
701
0
}
702
703
static int
704
serialise_array(struct sshbuf *m, char **a, size_t n)
705
0
{
706
0
  struct sshbuf *b;
707
0
  size_t i;
708
0
  int r = SSH_ERR_INTERNAL_ERROR;
709
710
0
  if (n > INT_MAX)
711
0
    return SSH_ERR_INTERNAL_ERROR;
712
713
0
  if ((b = sshbuf_new()) == NULL) {
714
0
    return SSH_ERR_ALLOC_FAIL;
715
0
  }
716
0
  for (i = 0; i < n; i++) {
717
0
    if ((r = sshbuf_put_cstring(b, a[i])) != 0)
718
0
      goto out;
719
0
  }
720
0
  if ((r = sshbuf_put_u32(m, n)) != 0 ||
721
0
      (r = sshbuf_put_stringb(m, b)) != 0)
722
0
    goto out;
723
  /* success */
724
0
  r = 0;
725
0
 out:
726
0
  sshbuf_free(b);
727
0
  return r;
728
0
}
729
730
static int
731
deserialise_array(struct sshbuf *m, char ***ap, size_t *np)
732
0
{
733
0
  char **a = NULL;
734
0
  size_t i, n = 0;
735
0
  struct sshbuf *b = NULL;
736
0
  u_int tmp;
737
0
  int r = SSH_ERR_INTERNAL_ERROR;
738
739
0
  if ((r = sshbuf_get_u32(m, &tmp)) != 0 ||
740
0
      (r = sshbuf_froms(m, &b)) != 0)
741
0
    goto out;
742
0
  if (tmp > INT_MAX) {
743
0
    r = SSH_ERR_INVALID_FORMAT;
744
0
    goto out;
745
0
  }
746
0
  n = tmp;
747
0
  if (n > 0 && (a = calloc(n, sizeof(*a))) == NULL) {
748
0
    r = SSH_ERR_ALLOC_FAIL;
749
0
    goto out;
750
0
  }
751
0
  for (i = 0; i < n; i++) {
752
0
    if ((r = sshbuf_get_cstring(b, &a[i], NULL)) != 0)
753
0
      goto out;
754
0
  }
755
  /* success */
756
0
  r = 0;
757
0
  *ap = a;
758
0
  a = NULL;
759
0
  *np = n;
760
0
  n = 0;
761
0
 out:
762
0
  if (a != NULL) {
763
0
    for (i = 0; i < n; i++)
764
0
      free(a[i]);
765
0
    free(a);
766
0
  }
767
0
  sshbuf_free(b);
768
0
  return r;
769
0
}
770
771
static int
772
serialise_nullable_string(struct sshbuf *m, const char *s)
773
0
{
774
0
  int r;
775
776
0
  if ((r = sshbuf_put_u8(m, s == NULL)) != 0 ||
777
0
      (r = sshbuf_put_cstring(m, s)) != 0)
778
0
    return r;
779
0
  return 0;
780
0
}
781
782
static int
783
deserialise_nullable_string(struct sshbuf *m, char **sp)
784
0
{
785
0
  int r;
786
0
  u_char flag;
787
788
0
  *sp = NULL;
789
0
  if ((r = sshbuf_get_u8(m, &flag)) != 0 ||
790
0
      (r = sshbuf_get_cstring(m, flag ? NULL : sp, NULL)) != 0)
791
0
    return r;
792
0
  return 0;
793
0
}
794
795
int
796
sshauthopt_serialise(const struct sshauthopt *opts, struct sshbuf *m,
797
    int untrusted)
798
0
{
799
0
  int r = SSH_ERR_INTERNAL_ERROR;
800
801
  /* Flag options */
802
0
  if ((r = sshbuf_put_u8(m, opts->permit_port_forwarding_flag)) != 0 ||
803
0
      (r = sshbuf_put_u8(m, opts->permit_agent_forwarding_flag)) != 0 ||
804
0
      (r = sshbuf_put_u8(m, opts->permit_x11_forwarding_flag)) != 0 ||
805
0
      (r = sshbuf_put_u8(m, opts->permit_pty_flag)) != 0 ||
806
0
      (r = sshbuf_put_u8(m, opts->permit_user_rc)) != 0 ||
807
0
      (r = sshbuf_put_u8(m, opts->restricted)) != 0 ||
808
0
      (r = sshbuf_put_u8(m, opts->cert_authority)) != 0 ||
809
0
      (r = sshbuf_put_u8(m, opts->no_require_user_presence)) != 0 ||
810
0
      (r = sshbuf_put_u8(m, opts->require_verify)) != 0)
811
0
    return r;
812
813
  /* Simple integer options */
814
0
  if ((r = sshbuf_put_u64(m, opts->valid_before)) != 0)
815
0
    return r;
816
817
  /* tunnel number can be negative to indicate "unset" */
818
0
  if ((r = sshbuf_put_u8(m, opts->force_tun_device == -1)) != 0 ||
819
0
      (r = sshbuf_put_u32(m, (opts->force_tun_device < 0) ?
820
0
      0 : (u_int)opts->force_tun_device)) != 0)
821
0
    return r;
822
823
  /* String options; these may be NULL */
824
0
  if ((r = serialise_nullable_string(m,
825
0
      untrusted ? "yes" : opts->cert_principals)) != 0 ||
826
0
      (r = serialise_nullable_string(m,
827
0
      untrusted ? "true" : opts->force_command)) != 0 ||
828
0
      (r = serialise_nullable_string(m,
829
0
      untrusted ? NULL : opts->required_from_host_cert)) != 0 ||
830
0
      (r = serialise_nullable_string(m,
831
0
      untrusted ? NULL : opts->required_from_host_keys)) != 0)
832
0
    return r;
833
834
  /* Array options */
835
0
  if ((r = serialise_array(m, opts->env,
836
0
      untrusted ? 0 : opts->nenv)) != 0 ||
837
0
      (r = serialise_array(m, opts->permitopen,
838
0
      untrusted ? 0 : opts->npermitopen)) != 0 ||
839
0
      (r = serialise_array(m, opts->permitlisten,
840
0
      untrusted ? 0 : opts->npermitlisten)) != 0)
841
0
    return r;
842
843
  /* success */
844
0
  return 0;
845
0
}
846
847
int
848
sshauthopt_deserialise(struct sshbuf *m, struct sshauthopt **optsp)
849
0
{
850
0
  struct sshauthopt *opts = NULL;
851
0
  int r = SSH_ERR_INTERNAL_ERROR;
852
0
  u_char f;
853
0
  u_int tmp;
854
855
0
  if ((opts = calloc(1, sizeof(*opts))) == NULL)
856
0
    return SSH_ERR_ALLOC_FAIL;
857
858
  /* Flag options */
859
0
#define OPT_FLAG(x) \
860
0
  do { \
861
0
    if ((r = sshbuf_get_u8(m, &f)) != 0) \
862
0
      goto out; \
863
0
    opts->x = f; \
864
0
  } while (0)
865
0
  OPT_FLAG(permit_port_forwarding_flag);
866
0
  OPT_FLAG(permit_agent_forwarding_flag);
867
0
  OPT_FLAG(permit_x11_forwarding_flag);
868
0
  OPT_FLAG(permit_pty_flag);
869
0
  OPT_FLAG(permit_user_rc);
870
0
  OPT_FLAG(restricted);
871
0
  OPT_FLAG(cert_authority);
872
0
  OPT_FLAG(no_require_user_presence);
873
0
  OPT_FLAG(require_verify);
874
0
#undef OPT_FLAG
875
876
  /* Simple integer options */
877
0
  if ((r = sshbuf_get_u64(m, &opts->valid_before)) != 0)
878
0
    goto out;
879
880
  /* tunnel number can be negative to indicate "unset" */
881
0
  if ((r = sshbuf_get_u8(m, &f)) != 0 ||
882
0
      (r = sshbuf_get_u32(m, &tmp)) != 0)
883
0
    goto out;
884
0
  opts->force_tun_device = f ? -1 : (int)tmp;
885
886
  /* String options may be NULL */
887
0
  if ((r = deserialise_nullable_string(m, &opts->cert_principals)) != 0 ||
888
0
      (r = deserialise_nullable_string(m, &opts->force_command)) != 0 ||
889
0
      (r = deserialise_nullable_string(m,
890
0
      &opts->required_from_host_cert)) != 0 ||
891
0
      (r = deserialise_nullable_string(m,
892
0
      &opts->required_from_host_keys)) != 0)
893
0
    goto out;
894
895
  /* Array options */
896
0
  if ((r = deserialise_array(m, &opts->env, &opts->nenv)) != 0 ||
897
0
      (r = deserialise_array(m,
898
0
      &opts->permitopen, &opts->npermitopen)) != 0 ||
899
0
      (r = deserialise_array(m,
900
0
      &opts->permitlisten, &opts->npermitlisten)) != 0)
901
0
    goto out;
902
903
  /* success */
904
0
  r = 0;
905
0
  *optsp = opts;
906
0
  opts = NULL;
907
0
 out:
908
0
  sshauthopt_free(opts);
909
0
  return r;
910
0
}