Coverage Report

Created: 2025-11-17 06:37

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
22
#include <stdlib.h>
23
#include <netdb.h>
24
#include <pwd.h>
25
#include <string.h>
26
#include <stdio.h>
27
#include <stdint.h>
28
#include <stdarg.h>
29
#include <ctype.h>
30
#include <limits.h>
31
32
#include "openbsd-compat/sys-queue.h"
33
34
#include "xmalloc.h"
35
#include "ssherr.h"
36
#include "log.h"
37
#include "sshbuf.h"
38
#include "misc.h"
39
#include "sshkey.h"
40
#include "match.h"
41
#include "ssh2.h"
42
#include "auth-options.h"
43
44
static int
45
dup_strings(char ***dstp, size_t *ndstp, char **src, size_t nsrc)
46
286
{
47
286
  char **dst;
48
286
  size_t i, j;
49
50
286
  *dstp = NULL;
51
286
  *ndstp = 0;
52
53
286
  if (nsrc == 0)
54
0
    return 0;
55
286
  if (nsrc >= SIZE_MAX / sizeof(*src) ||
56
286
      (dst = calloc(nsrc, sizeof(*src))) == NULL)
57
0
    return -1;
58
61.8k
  for (i = 0; i < nsrc; i++) {
59
61.5k
    if ((dst[i] = strdup(src[i])) == NULL) {
60
0
      for (j = 0; j < i; j++)
61
0
        free(dst[j]);
62
0
      free(dst);
63
0
      return -1;
64
0
    }
65
61.5k
  }
66
  /* success */
67
286
  *dstp = dst;
68
286
  *ndstp = nsrc;
69
286
  return 0;
70
286
}
71
72
0
#define OPTIONS_CRITICAL  1
73
0
#define OPTIONS_EXTENSIONS  2
74
static int
75
cert_option_list(struct sshauthopt *opts, struct sshbuf *oblob,
76
    u_int which, int crit)
77
0
{
78
0
  char *command, *allowed;
79
0
  char *name = NULL;
80
0
  struct sshbuf *c = NULL, *data = NULL;
81
0
  int r, ret = -1, found;
82
83
0
  if ((c = sshbuf_fromb(oblob)) == NULL) {
84
0
    error_f("sshbuf_fromb failed");
85
0
    goto out;
86
0
  }
87
88
0
  while (sshbuf_len(c) > 0) {
89
0
    sshbuf_free(data);
90
0
    data = NULL;
91
0
    if ((r = sshbuf_get_cstring(c, &name, NULL)) != 0 ||
92
0
        (r = sshbuf_froms(c, &data)) != 0) {
93
0
      error_r(r, "Unable to parse certificate options");
94
0
      goto out;
95
0
    }
96
0
    debug3("found certificate option \"%.100s\" len %zu",
97
0
        name, sshbuf_len(data));
98
0
    found = 0;
99
0
    if ((which & OPTIONS_EXTENSIONS) != 0) {
100
0
      if (strcmp(name, "no-touch-required") == 0) {
101
0
        opts->no_require_user_presence = 1;
102
0
        found = 1;
103
0
      } else if (strcmp(name, "permit-X11-forwarding") == 0) {
104
0
        opts->permit_x11_forwarding_flag = 1;
105
0
        found = 1;
106
0
      } else if (strcmp(name,
107
0
          "permit-agent-forwarding") == 0) {
108
0
        opts->permit_agent_forwarding_flag = 1;
109
0
        found = 1;
110
0
      } else if (strcmp(name,
111
0
          "permit-port-forwarding") == 0) {
112
0
        opts->permit_port_forwarding_flag = 1;
113
0
        found = 1;
114
0
      } else if (strcmp(name, "permit-pty") == 0) {
115
0
        opts->permit_pty_flag = 1;
116
0
        found = 1;
117
0
      } else if (strcmp(name, "permit-user-rc") == 0) {
118
0
        opts->permit_user_rc = 1;
119
0
        found = 1;
120
0
      }
121
0
    }
122
0
    if (!found && (which & OPTIONS_CRITICAL) != 0) {
123
0
      if (strcmp(name, "verify-required") == 0) {
124
0
        opts->require_verify = 1;
125
0
        found = 1;
126
0
      } else if (strcmp(name, "force-command") == 0) {
127
0
        if ((r = sshbuf_get_cstring(data, &command,
128
0
            NULL)) != 0) {
129
0
          error_r(r, "Unable to parse \"%s\" "
130
0
              "section", name);
131
0
          goto out;
132
0
        }
133
0
        if (opts->force_command != NULL) {
134
0
          error("Certificate has multiple "
135
0
              "force-command options");
136
0
          free(command);
137
0
          goto out;
138
0
        }
139
0
        opts->force_command = command;
140
0
        found = 1;
141
0
      } else if (strcmp(name, "source-address") == 0) {
142
0
        if ((r = sshbuf_get_cstring(data, &allowed,
143
0
            NULL)) != 0) {
144
0
          error_r(r, "Unable to parse \"%s\" "
145
0
              "section", name);
146
0
          goto out;
147
0
        }
148
0
        if (opts->required_from_host_cert != NULL) {
149
0
          error("Certificate has multiple "
150
0
              "source-address options");
151
0
          free(allowed);
152
0
          goto out;
153
0
        }
154
        /* Check syntax */
155
0
        if (addr_match_cidr_list(NULL, allowed) == -1) {
156
0
          error("Certificate source-address "
157
0
              "contents invalid");
158
0
          free(allowed);
159
0
          goto out;
160
0
        }
161
0
        opts->required_from_host_cert = allowed;
162
0
        found = 1;
163
0
      }
164
0
    }
165
166
0
    if (!found) {
167
0
      if (crit) {
168
0
        error("Certificate critical option \"%s\" "
169
0
            "is not supported", name);
170
0
        goto out;
171
0
      } else {
172
0
        logit("Certificate extension \"%s\" "
173
0
            "is not supported", name);
174
0
      }
175
0
    } else if (sshbuf_len(data) != 0) {
176
0
      error("Certificate option \"%s\" corrupt "
177
0
          "(extra data)", name);
178
0
      goto out;
179
0
    }
180
0
    free(name);
181
0
    name = NULL;
182
0
  }
183
  /* successfully parsed all options */
184
0
  ret = 0;
185
186
0
 out:
187
0
  free(name);
188
0
  sshbuf_free(data);
189
0
  sshbuf_free(c);
190
0
  return ret;
191
0
}
192
193
struct sshauthopt *
194
sshauthopt_new(void)
195
4.16k
{
196
4.16k
  struct sshauthopt *ret;
197
198
4.16k
  if ((ret = calloc(1, sizeof(*ret))) == NULL)
199
0
    return NULL;
200
4.16k
  ret->force_tun_device = -1;
201
4.16k
  return ret;
202
4.16k
}
203
204
void
205
sshauthopt_free(struct sshauthopt *opts)
206
6.41k
{
207
6.41k
  size_t i;
208
209
6.41k
  if (opts == NULL)
210
2.24k
    return;
211
212
4.16k
  free(opts->cert_principals);
213
4.16k
  free(opts->force_command);
214
4.16k
  free(opts->required_from_host_cert);
215
4.16k
  free(opts->required_from_host_keys);
216
217
7.38k
  for (i = 0; i < opts->nenv; i++)
218
3.22k
    free(opts->env[i]);
219
4.16k
  free(opts->env);
220
221
112k
  for (i = 0; i < opts->npermitopen; i++)
222
108k
    free(opts->permitopen[i]);
223
4.16k
  free(opts->permitopen);
224
225
42.2k
  for (i = 0; i < opts->npermitlisten; i++)
226
38.0k
    free(opts->permitlisten[i]);
227
4.16k
  free(opts->permitlisten);
228
229
4.16k
  freezero(opts, sizeof(*opts));
230
4.16k
}
231
232
struct sshauthopt *
233
sshauthopt_new_with_keys_defaults(void)
234
1.76k
{
235
1.76k
  struct sshauthopt *ret = NULL;
236
237
1.76k
  if ((ret = sshauthopt_new()) == NULL)
238
0
    return NULL;
239
240
  /* Defaults for authorized_keys flags */
241
1.76k
  ret->permit_port_forwarding_flag = 1;
242
1.76k
  ret->permit_agent_forwarding_flag = 1;
243
1.76k
  ret->permit_x11_forwarding_flag = 1;
244
1.76k
  ret->permit_pty_flag = 1;
245
1.76k
  ret->permit_user_rc = 1;
246
1.76k
  return ret;
247
1.76k
}
248
249
/*
250
 * Parse and record a permitopen/permitlisten directive.
251
 * Return 0 on success. Return -1 on failure and sets *errstrp to error reason.
252
 */
253
static int
254
handle_permit(const char **optsp, int allow_bare_port,
255
    char ***permitsp, size_t *npermitsp, const char **errstrp)
256
86.3k
{
257
86.3k
  char *opt, *tmp, *cp, *host, **permits = *permitsp;
258
86.3k
  size_t npermits = *npermitsp;
259
86.3k
  const char *errstr = "unknown error";
260
261
86.3k
  if (npermits > SSH_AUTHOPT_PERMIT_MAX) {
262
1
    *errstrp = "too many permission directives";
263
1
    return -1;
264
1
  }
265
86.3k
  if ((opt = opt_dequote(optsp, &errstr)) == NULL) {
266
15
    return -1;
267
15
  }
268
86.3k
  if (allow_bare_port && strchr(opt, ':') == NULL) {
269
    /*
270
     * Allow a bare port number in permitlisten to indicate a
271
     * listen_host wildcard.
272
     */
273
21.0k
    if (asprintf(&tmp, "*:%s", opt) == -1) {
274
0
      free(opt);
275
0
      *errstrp = "memory allocation failed";
276
0
      return -1;
277
0
    }
278
21.0k
    free(opt);
279
21.0k
    opt = tmp;
280
21.0k
  }
281
86.3k
  if ((tmp = strdup(opt)) == NULL) {
282
0
    free(opt);
283
0
    *errstrp = "memory allocation failed";
284
0
    return -1;
285
0
  }
286
86.3k
  cp = tmp;
287
  /* validate syntax before recording it. */
288
86.3k
  host = hpdelim2(&cp, NULL);
289
86.3k
  if (host == NULL || strlen(host) >= NI_MAXHOST) {
290
11
    free(tmp);
291
11
    free(opt);
292
11
    *errstrp = "invalid permission hostname";
293
11
    return -1;
294
11
  }
295
  /*
296
   * don't want to use permitopen_port to avoid
297
   * dependency on channels.[ch] here.
298
   */
299
86.3k
  if (cp == NULL ||
300
86.3k
      (strcmp(cp, "*") != 0 && a2port(cp) <= 0)) {
301
214
    free(tmp);
302
214
    free(opt);
303
214
    *errstrp = "invalid permission port";
304
214
    return -1;
305
214
  }
306
  /* XXX - add streamlocal support */
307
86.1k
  free(tmp);
308
  /* Record it */
309
86.1k
  if ((permits = recallocarray(permits, npermits, npermits + 1,
310
86.1k
      sizeof(*permits))) == NULL) {
311
0
    free(opt);
312
    /* NB. don't update *permitsp if alloc fails */
313
0
    *errstrp = "memory allocation failed";
314
0
    return -1;
315
0
  }
316
86.1k
  permits[npermits++] = opt;
317
86.1k
  *permitsp = permits;
318
86.1k
  *npermitsp = npermits;
319
86.1k
  return 0;
320
86.1k
}
321
322
struct sshauthopt *
323
sshauthopt_parse(const char *opts, const char **errstrp)
324
1.76k
{
325
1.76k
  char **oarray, *opt, *cp, *tmp;
326
1.76k
  int r;
327
1.76k
  struct sshauthopt *ret = NULL;
328
1.76k
  const char *errstr = "unknown error";
329
1.76k
  uint64_t valid_before;
330
1.76k
  size_t i, l;
331
332
1.76k
  if (errstrp != NULL)
333
0
    *errstrp = NULL;
334
1.76k
  if ((ret = sshauthopt_new_with_keys_defaults()) == NULL)
335
0
    goto alloc_fail;
336
337
1.76k
  if (opts == NULL)
338
0
    return ret;
339
340
124k
  while (*opts && *opts != ' ' && *opts != '\t') {
341
    /* flag options */
342
124k
    if ((r = opt_flag("restrict", 0, &opts)) != -1) {
343
195
      ret->restricted = 1;
344
195
      ret->permit_port_forwarding_flag = 0;
345
195
      ret->permit_agent_forwarding_flag = 0;
346
195
      ret->permit_x11_forwarding_flag = 0;
347
195
      ret->permit_pty_flag = 0;
348
195
      ret->permit_user_rc = 0;
349
124k
    } else if ((r = opt_flag("cert-authority", 0, &opts)) != -1) {
350
194
      ret->cert_authority = r;
351
123k
    } else if ((r = opt_flag("port-forwarding", 1, &opts)) != -1) {
352
195
      ret->permit_port_forwarding_flag = r == 1;
353
123k
    } else if ((r = opt_flag("agent-forwarding", 1, &opts)) != -1) {
354
197
      ret->permit_agent_forwarding_flag = r == 1;
355
123k
    } else if ((r = opt_flag("x11-forwarding", 1, &opts)) != -1) {
356
195
      ret->permit_x11_forwarding_flag = r == 1;
357
123k
    } else if ((r = opt_flag("touch-required", 1, &opts)) != -1) {
358
195
      ret->no_require_user_presence = r != 1; /* NB. flip */
359
123k
    } else if ((r = opt_flag("verify-required", 1, &opts)) != -1) {
360
197
      ret->require_verify = r == 1;
361
122k
    } else if ((r = opt_flag("pty", 1, &opts)) != -1) {
362
222
      ret->permit_pty_flag = r == 1;
363
122k
    } else if ((r = opt_flag("user-rc", 1, &opts)) != -1) {
364
197
      ret->permit_user_rc = r == 1;
365
122k
    } else if (opt_match(&opts, "command")) {
366
4
      if (ret->force_command != NULL) {
367
1
        errstr = "multiple \"command\" clauses";
368
1
        goto fail;
369
1
      }
370
3
      ret->force_command = opt_dequote(&opts, &errstr);
371
3
      if (ret->force_command == NULL)
372
1
        goto fail;
373
122k
    } else if (opt_match(&opts, "principals")) {
374
6
      if (ret->cert_principals != NULL) {
375
1
        errstr = "multiple \"principals\" clauses";
376
1
        goto fail;
377
1
      }
378
5
      ret->cert_principals = opt_dequote(&opts, &errstr);
379
5
      if (ret->cert_principals == NULL)
380
1
        goto fail;
381
122k
    } else if (opt_match(&opts, "from")) {
382
81
      if (ret->required_from_host_keys != NULL) {
383
5
        errstr = "multiple \"from\" clauses";
384
5
        goto fail;
385
5
      }
386
76
      ret->required_from_host_keys = opt_dequote(&opts,
387
76
          &errstr);
388
76
      if (ret->required_from_host_keys == NULL)
389
58
        goto fail;
390
122k
    } else if (opt_match(&opts, "expiry-time")) {
391
2.36k
      if ((opt = opt_dequote(&opts, &errstr)) == NULL)
392
1
        goto fail;
393
2.36k
      if (parse_absolute_time(opt, &valid_before) != 0 ||
394
2.14k
          valid_before == 0) {
395
222
        free(opt);
396
222
        errstr = "invalid expires time";
397
222
        goto fail;
398
222
      }
399
2.13k
      free(opt);
400
2.13k
      if (ret->valid_before == 0 ||
401
1.92k
          valid_before < ret->valid_before)
402
368
        ret->valid_before = valid_before;
403
120k
    } else if (opt_match(&opts, "environment")) {
404
32.0k
      if (ret->nenv > SSH_AUTHOPT_ENV_MAX) {
405
0
        errstr = "too many environment strings";
406
0
        goto fail;
407
0
      }
408
32.0k
      if ((opt = opt_dequote(&opts, &errstr)) == NULL)
409
4
        goto fail;
410
      /* env name must be alphanumeric and followed by '=' */
411
32.0k
      if ((tmp = strchr(opt, '=')) == NULL) {
412
1
        free(opt);
413
1
        errstr = "invalid environment string";
414
1
        goto fail;
415
1
      }
416
32.0k
      if ((cp = strdup(opt)) == NULL) {
417
0
        free(opt);
418
0
        goto alloc_fail;
419
0
      }
420
32.0k
      l = (size_t)(tmp - opt);
421
32.0k
      cp[l] = '\0'; /* truncate at '=' */
422
32.0k
      if (!valid_env_name(cp)) {
423
18
        free(cp);
424
18
        free(opt);
425
18
        errstr = "invalid environment string";
426
18
        goto fail;
427
18
      }
428
      /* Check for duplicates; XXX O(n*log(n)) */
429
100k
      for (i = 0; i < ret->nenv; i++) {
430
98.4k
        if (strncmp(ret->env[i], cp, l) == 0 &&
431
30.9k
            ret->env[i][l] == '=')
432
30.3k
          break;
433
98.4k
      }
434
31.9k
      free(cp);
435
      /* First match wins */
436
31.9k
      if (i >= ret->nenv) {
437
        /* Append it. */
438
1.62k
        oarray = ret->env;
439
1.62k
        if ((ret->env = recallocarray(ret->env,
440
1.62k
            ret->nenv, ret->nenv + 1,
441
1.62k
            sizeof(*ret->env))) == NULL) {
442
0
          free(opt);
443
          /* put it back for cleanup */
444
0
          ret->env = oarray;
445
0
          goto alloc_fail;
446
0
        }
447
1.62k
        ret->env[ret->nenv++] = opt;
448
1.62k
        opt = NULL; /* transferred */
449
1.62k
      }
450
31.9k
      free(opt);
451
88.0k
    } else if (opt_match(&opts, "permitopen")) {
452
65.0k
      if (handle_permit(&opts, 0, &ret->permitopen,
453
65.0k
          &ret->npermitopen, &errstr) != 0)
454
216
        goto fail;
455
65.0k
    } else if (opt_match(&opts, "permitlisten")) {
456
21.2k
      if (handle_permit(&opts, 1, &ret->permitlisten,
457
21.2k
          &ret->npermitlisten, &errstr) != 0)
458
25
        goto fail;
459
21.2k
    } else if (opt_match(&opts, "tunnel")) {
460
917
      if ((opt = opt_dequote(&opts, &errstr)) == NULL)
461
7
        goto fail;
462
910
      ret->force_tun_device = a2tun(opt, NULL);
463
910
      free(opt);
464
910
      if (ret->force_tun_device == SSH_TUNID_ERR) {
465
245
        errstr = "invalid tun device";
466
245
        goto fail;
467
245
      }
468
910
    }
469
    /*
470
     * Skip the comma, and move to the next option
471
     * (or break out if there are no more).
472
     */
473
123k
    if (*opts == '\0' || *opts == ' ' || *opts == '\t')
474
639
      break;    /* End of options. */
475
    /* Anything other than a comma is an unknown option */
476
122k
    if (*opts != ',') {
477
300
      errstr = "unknown key option";
478
300
      goto fail;
479
300
    }
480
122k
    opts++;
481
122k
    if (*opts == '\0') {
482
15
      errstr = "unexpected end-of-options";
483
15
      goto fail;
484
15
    }
485
122k
  }
486
487
  /* success */
488
642
  if (errstrp != NULL)
489
0
    *errstrp = NULL;
490
642
  return ret;
491
492
0
alloc_fail:
493
0
  errstr = "memory allocation failed";
494
1.12k
fail:
495
1.12k
  sshauthopt_free(ret);
496
1.12k
  if (errstrp != NULL)
497
0
    *errstrp = errstr;
498
1.12k
  return NULL;
499
0
}
500
501
struct sshauthopt *
502
sshauthopt_from_cert(struct sshkey *k)
503
0
{
504
0
  struct sshauthopt *ret;
505
506
0
  if (k == NULL || !sshkey_type_is_cert(k->type) || k->cert == NULL ||
507
0
      k->cert->type != SSH2_CERT_TYPE_USER)
508
0
    return NULL;
509
510
0
  if ((ret = sshauthopt_new()) == NULL)
511
0
    return NULL;
512
513
  /* Handle options and critical extensions separately */
514
0
  if (cert_option_list(ret, k->cert->critical,
515
0
      OPTIONS_CRITICAL, 1) == -1) {
516
0
    sshauthopt_free(ret);
517
0
    return NULL;
518
0
  }
519
0
  if (cert_option_list(ret, k->cert->extensions,
520
0
      OPTIONS_EXTENSIONS, 0) == -1) {
521
0
    sshauthopt_free(ret);
522
0
    return NULL;
523
0
  }
524
  /* success */
525
0
  return ret;
526
0
}
527
528
/*
529
 * Merges "additional" options to "primary" and returns the result.
530
 * NB. Some options from primary have primacy.
531
 */
532
struct sshauthopt *
533
sshauthopt_merge(const struct sshauthopt *primary,
534
    const struct sshauthopt *additional, const char **errstrp)
535
642
{
536
642
  struct sshauthopt *ret;
537
642
  const char *errstr = "internal error";
538
642
  const char *tmp;
539
540
642
  if (errstrp != NULL)
541
0
    *errstrp = NULL;
542
543
642
  if ((ret = sshauthopt_new()) == NULL)
544
0
    goto alloc_fail;
545
546
  /* cert_authority and cert_principals are cleared in result */
547
548
  /* Prefer access lists from primary. */
549
  /* XXX err is both set and mismatch? */
550
642
  tmp = primary->required_from_host_cert;
551
642
  if (tmp == NULL)
552
642
    tmp = additional->required_from_host_cert;
553
642
  if (tmp != NULL && (ret->required_from_host_cert = strdup(tmp)) == NULL)
554
0
    goto alloc_fail;
555
642
  tmp = primary->required_from_host_keys;
556
642
  if (tmp == NULL)
557
641
    tmp = additional->required_from_host_keys;
558
642
  if (tmp != NULL && (ret->required_from_host_keys = strdup(tmp)) == NULL)
559
0
    goto alloc_fail;
560
561
  /*
562
   * force_tun_device, permitopen/permitlisten and environment all
563
   * prefer the primary.
564
   */
565
642
  ret->force_tun_device = primary->force_tun_device;
566
642
  if (ret->force_tun_device == -1)
567
540
    ret->force_tun_device = additional->force_tun_device;
568
642
  if (primary->nenv > 0) {
569
183
    if (dup_strings(&ret->env, &ret->nenv,
570
183
        primary->env, primary->nenv) != 0)
571
0
      goto alloc_fail;
572
459
  } else if (additional->nenv) {
573
0
    if (dup_strings(&ret->env, &ret->nenv,
574
0
        additional->env, additional->nenv) != 0)
575
0
      goto alloc_fail;
576
0
  }
577
642
  if (primary->npermitopen > 0) {
578
67
    if (dup_strings(&ret->permitopen, &ret->npermitopen,
579
67
        primary->permitopen, primary->npermitopen) != 0)
580
0
      goto alloc_fail;
581
575
  } else if (additional->npermitopen > 0) {
582
0
    if (dup_strings(&ret->permitopen, &ret->npermitopen,
583
0
        additional->permitopen, additional->npermitopen) != 0)
584
0
      goto alloc_fail;
585
0
  }
586
587
642
  if (primary->npermitlisten > 0) {
588
36
    if (dup_strings(&ret->permitlisten, &ret->npermitlisten,
589
36
        primary->permitlisten, primary->npermitlisten) != 0)
590
0
      goto alloc_fail;
591
606
  } else if (additional->npermitlisten > 0) {
592
0
    if (dup_strings(&ret->permitlisten, &ret->npermitlisten,
593
0
        additional->permitlisten, additional->npermitlisten) != 0)
594
0
      goto alloc_fail;
595
0
  }
596
597
3.85k
#define OPTFLAG_AND(x) ret->x = (primary->x == 1) && (additional->x == 1)
598
642
#define OPTFLAG_OR(x) ret->x = (primary->x == 1) || (additional->x == 1)
599
  /* Permissive flags are logical-AND (i.e. must be set in both) */
600
642
  OPTFLAG_AND(permit_port_forwarding_flag);
601
642
  OPTFLAG_AND(permit_agent_forwarding_flag);
602
642
  OPTFLAG_AND(permit_x11_forwarding_flag);
603
642
  OPTFLAG_AND(permit_pty_flag);
604
642
  OPTFLAG_AND(permit_user_rc);
605
642
  OPTFLAG_AND(no_require_user_presence);
606
  /* Restrictive flags are logical-OR (i.e. must be set in either) */
607
642
  OPTFLAG_OR(require_verify);
608
642
#undef OPTFLAG_AND
609
610
  /* Earliest expiry time should win */
611
642
  if (primary->valid_before != 0)
612
169
    ret->valid_before = primary->valid_before;
613
642
  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
642
  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
642
  } 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
641
  } 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
642
  if (errstrp != NULL)
644
0
    *errstrp = NULL;
645
642
  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
}