Coverage Report

Created: 2026-10-13 06:56

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
0
{
46
0
  char **dst;
47
0
  size_t i, j;
48
49
0
  *dstp = NULL;
50
0
  *ndstp = 0;
51
52
0
  if (nsrc == 0)
53
0
    return 0;
54
0
  if (nsrc >= SIZE_MAX / sizeof(*src) ||
55
0
      (dst = calloc(nsrc, sizeof(*src))) == NULL)
56
0
    return -1;
57
0
  for (i = 0; i < nsrc; i++) {
58
0
    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
0
  }
65
  /* success */
66
0
  *dstp = dst;
67
0
  *ndstp = nsrc;
68
0
  return 0;
69
0
}
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
0
{
195
0
  struct sshauthopt *ret;
196
197
0
  if ((ret = calloc(1, sizeof(*ret))) == NULL)
198
0
    return NULL;
199
0
  ret->force_tun_device = -1;
200
0
  return ret;
201
0
}
202
203
void
204
sshauthopt_free(struct sshauthopt *opts)
205
0
{
206
0
  size_t i;
207
208
0
  if (opts == NULL)
209
0
    return;
210
211
0
  free(opts->cert_principals);
212
0
  free(opts->force_command);
213
0
  free(opts->required_from_host_cert);
214
0
  free(opts->required_from_host_keys);
215
216
0
  for (i = 0; i < opts->nenv; i++)
217
0
    free(opts->env[i]);
218
0
  free(opts->env);
219
220
0
  for (i = 0; i < opts->npermitopen; i++)
221
0
    free(opts->permitopen[i]);
222
0
  free(opts->permitopen);
223
224
0
  for (i = 0; i < opts->npermitlisten; i++)
225
0
    free(opts->permitlisten[i]);
226
0
  free(opts->permitlisten);
227
228
0
  freezero(opts, sizeof(*opts));
229
0
}
230
231
struct sshauthopt *
232
sshauthopt_new_with_keys_defaults(void)
233
0
{
234
0
  struct sshauthopt *ret = NULL;
235
236
0
  if ((ret = sshauthopt_new()) == NULL)
237
0
    return NULL;
238
239
  /* Defaults for authorized_keys flags */
240
0
  ret->permit_port_forwarding_flag = 1;
241
0
  ret->permit_agent_forwarding_flag = 1;
242
0
  ret->permit_x11_forwarding_flag = 1;
243
0
  ret->permit_pty_flag = 1;
244
0
  ret->permit_user_rc = 1;
245
0
  return ret;
246
0
}
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
0
{
256
0
  char *opt, *tmp, *cp, *host, **permits = *permitsp;
257
0
  size_t npermits = *npermitsp;
258
0
  const char *errstr = "unknown error";
259
260
0
  if (npermits > SSH_AUTHOPT_PERMIT_MAX) {
261
0
    *errstrp = "too many permission directives";
262
0
    return -1;
263
0
  }
264
0
  if ((opt = opt_dequote(optsp, &errstr)) == NULL) {
265
0
    return -1;
266
0
  }
267
0
  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
0
    if (asprintf(&tmp, "*:%s", opt) == -1) {
273
0
      free(opt);
274
0
      *errstrp = "memory allocation failed";
275
0
      return -1;
276
0
    }
277
0
    free(opt);
278
0
    opt = tmp;
279
0
  }
280
0
  if ((tmp = strdup(opt)) == NULL) {
281
0
    free(opt);
282
0
    *errstrp = "memory allocation failed";
283
0
    return -1;
284
0
  }
285
0
  cp = tmp;
286
  /* validate syntax before recording it. */
287
0
  host = hpdelim2(&cp, NULL);
288
0
  if (host == NULL || strlen(host) >= NI_MAXHOST) {
289
0
    free(tmp);
290
0
    free(opt);
291
0
    *errstrp = "invalid permission hostname";
292
0
    return -1;
293
0
  }
294
  /*
295
   * don't want to use permitopen_port to avoid
296
   * dependency on channels.[ch] here.
297
   */
298
0
  if (cp == NULL ||
299
0
      (strcmp(cp, "*") != 0 && a2port(cp) <= 0)) {
300
0
    free(tmp);
301
0
    free(opt);
302
0
    *errstrp = "invalid permission port";
303
0
    return -1;
304
0
  }
305
  /* XXX - add streamlocal support */
306
0
  free(tmp);
307
  /* Record it */
308
0
  if ((permits = recallocarray(permits, npermits, npermits + 1,
309
0
      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
0
  permits[npermits++] = opt;
316
0
  *permitsp = permits;
317
0
  *npermitsp = npermits;
318
0
  return 0;
319
0
}
320
321
struct sshauthopt *
322
sshauthopt_parse(const char *opts, const char **errstrp)
323
0
{
324
0
  char **oarray, *opt, *cp, *tmp;
325
0
  int r;
326
0
  struct sshauthopt *ret = NULL;
327
0
  const char *errstr = "unknown error";
328
0
  uint64_t valid_before;
329
0
  size_t i, l;
330
331
0
  if (errstrp != NULL)
332
0
    *errstrp = NULL;
333
0
  if ((ret = sshauthopt_new_with_keys_defaults()) == NULL)
334
0
    goto alloc_fail;
335
336
0
  if (opts == NULL)
337
0
    return ret;
338
339
0
  while (*opts && *opts != ' ' && *opts != '\t') {
340
    /* flag options */
341
0
    if ((r = opt_flag("restrict", 0, &opts)) != -1) {
342
0
      ret->restricted = 1;
343
0
      ret->permit_port_forwarding_flag = 0;
344
0
      ret->permit_agent_forwarding_flag = 0;
345
0
      ret->permit_x11_forwarding_flag = 0;
346
0
      ret->permit_pty_flag = 0;
347
0
      ret->permit_user_rc = 0;
348
0
    } else if ((r = opt_flag("cert-authority", 0, &opts)) != -1) {
349
0
      ret->cert_authority = r;
350
0
    } else if ((r = opt_flag("port-forwarding", 1, &opts)) != -1) {
351
0
      ret->permit_port_forwarding_flag = r == 1;
352
0
    } else if ((r = opt_flag("agent-forwarding", 1, &opts)) != -1) {
353
0
      ret->permit_agent_forwarding_flag = r == 1;
354
0
    } else if ((r = opt_flag("x11-forwarding", 1, &opts)) != -1) {
355
0
      ret->permit_x11_forwarding_flag = r == 1;
356
0
    } else if ((r = opt_flag("touch-required", 1, &opts)) != -1) {
357
0
      ret->no_require_user_presence = r != 1; /* NB. flip */
358
0
    } else if ((r = opt_flag("verify-required", 1, &opts)) != -1) {
359
0
      ret->require_verify = r == 1;
360
0
    } else if ((r = opt_flag("pty", 1, &opts)) != -1) {
361
0
      ret->permit_pty_flag = r == 1;
362
0
    } else if ((r = opt_flag("user-rc", 1, &opts)) != -1) {
363
0
      ret->permit_user_rc = r == 1;
364
0
    } else if (opt_match(&opts, "command")) {
365
0
      if (ret->force_command != NULL) {
366
0
        errstr = "multiple \"command\" clauses";
367
0
        goto fail;
368
0
      }
369
0
      ret->force_command = opt_dequote(&opts, &errstr);
370
0
      if (ret->force_command == NULL)
371
0
        goto fail;
372
0
    } else if (opt_match(&opts, "principals")) {
373
0
      if (ret->cert_principals != NULL) {
374
0
        errstr = "multiple \"principals\" clauses";
375
0
        goto fail;
376
0
      }
377
0
      ret->cert_principals = opt_dequote(&opts, &errstr);
378
0
      if (ret->cert_principals == NULL)
379
0
        goto fail;
380
0
    } else if (opt_match(&opts, "from")) {
381
0
      if (ret->required_from_host_keys != NULL) {
382
0
        errstr = "multiple \"from\" clauses";
383
0
        goto fail;
384
0
      }
385
0
      ret->required_from_host_keys = opt_dequote(&opts,
386
0
          &errstr);
387
0
      if (ret->required_from_host_keys == NULL)
388
0
        goto fail;
389
0
    } else if (opt_match(&opts, "expiry-time")) {
390
0
      if ((opt = opt_dequote(&opts, &errstr)) == NULL)
391
0
        goto fail;
392
0
      if (parse_absolute_time(opt, &valid_before) != 0 ||
393
0
          valid_before == 0) {
394
0
        free(opt);
395
0
        errstr = "invalid expires time";
396
0
        goto fail;
397
0
      }
398
0
      free(opt);
399
0
      if (ret->valid_before == 0 ||
400
0
          valid_before < ret->valid_before)
401
0
        ret->valid_before = valid_before;
402
0
    } else if (opt_match(&opts, "environment")) {
403
0
      if (ret->nenv > SSH_AUTHOPT_ENV_MAX) {
404
0
        errstr = "too many environment strings";
405
0
        goto fail;
406
0
      }
407
0
      if ((opt = opt_dequote(&opts, &errstr)) == NULL)
408
0
        goto fail;
409
      /* env name must be alphanumeric and followed by '=' */
410
0
      if ((tmp = strchr(opt, '=')) == NULL) {
411
0
        free(opt);
412
0
        errstr = "invalid environment string";
413
0
        goto fail;
414
0
      }
415
0
      if ((cp = strdup(opt)) == NULL) {
416
0
        free(opt);
417
0
        goto alloc_fail;
418
0
      }
419
0
      l = (size_t)(tmp - opt);
420
0
      cp[l] = '\0'; /* truncate at '=' */
421
0
      if (!valid_env_name(cp)) {
422
0
        free(cp);
423
0
        free(opt);
424
0
        errstr = "invalid environment string";
425
0
        goto fail;
426
0
      }
427
      /* Check for duplicates; XXX O(n*log(n)) */
428
0
      for (i = 0; i < ret->nenv; i++) {
429
0
        if (strncmp(ret->env[i], cp, l) == 0 &&
430
0
            ret->env[i][l] == '=')
431
0
          break;
432
0
      }
433
0
      free(cp);
434
      /* First match wins */
435
0
      if (i >= ret->nenv) {
436
        /* Append it. */
437
0
        oarray = ret->env;
438
0
        if ((ret->env = recallocarray(ret->env,
439
0
            ret->nenv, ret->nenv + 1,
440
0
            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
0
        ret->env[ret->nenv++] = opt;
447
0
        opt = NULL; /* transferred */
448
0
      }
449
0
      free(opt);
450
0
    } else if (opt_match(&opts, "permitopen")) {
451
0
      if (handle_permit(&opts, 0, &ret->permitopen,
452
0
          &ret->npermitopen, &errstr) != 0)
453
0
        goto fail;
454
0
    } else if (opt_match(&opts, "permitlisten")) {
455
0
      if (handle_permit(&opts, 1, &ret->permitlisten,
456
0
          &ret->npermitlisten, &errstr) != 0)
457
0
        goto fail;
458
0
    } else if (opt_match(&opts, "tunnel")) {
459
0
      if ((opt = opt_dequote(&opts, &errstr)) == NULL)
460
0
        goto fail;
461
0
      ret->force_tun_device = a2tun(opt, NULL);
462
0
      free(opt);
463
0
      if (ret->force_tun_device == SSH_TUNID_ERR) {
464
0
        errstr = "invalid tun device";
465
0
        goto fail;
466
0
      }
467
0
    }
468
    /*
469
     * Skip the comma, and move to the next option
470
     * (or break out if there are no more).
471
     */
472
0
    if (*opts == '\0' || *opts == ' ' || *opts == '\t')
473
0
      break;   /* End of options. */
474
    /* Anything other than a comma is an unknown option */
475
0
    if (*opts != ',') {
476
0
      errstr = "unknown key option";
477
0
      goto fail;
478
0
    }
479
0
    opts++;
480
0
    if (*opts == '\0') {
481
0
      errstr = "unexpected end-of-options";
482
0
      goto fail;
483
0
    }
484
0
  }
485
486
  /* success */
487
0
  if (errstrp != NULL)
488
0
    *errstrp = NULL;
489
0
  return ret;
490
491
0
alloc_fail:
492
0
  errstr = "memory allocation failed";
493
0
fail:
494
0
  sshauthopt_free(ret);
495
0
  if (errstrp != NULL)
496
0
    *errstrp = errstr;
497
0
  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
0
{
535
0
  struct sshauthopt *ret;
536
0
  const char *errstr = "internal error";
537
0
  const char *tmp;
538
539
0
  if (errstrp != NULL)
540
0
    *errstrp = NULL;
541
542
0
  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
0
  tmp = primary->required_from_host_cert;
550
0
  if (tmp == NULL)
551
0
    tmp = additional->required_from_host_cert;
552
0
  if (tmp != NULL && (ret->required_from_host_cert = strdup(tmp)) == NULL)
553
0
    goto alloc_fail;
554
0
  tmp = primary->required_from_host_keys;
555
0
  if (tmp == NULL)
556
0
    tmp = additional->required_from_host_keys;
557
0
  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
0
  ret->force_tun_device = primary->force_tun_device;
565
0
  if (ret->force_tun_device == -1)
566
0
    ret->force_tun_device = additional->force_tun_device;
567
0
  if (primary->nenv > 0) {
568
0
    if (dup_strings(&ret->env, &ret->nenv,
569
0
        primary->env, primary->nenv) != 0)
570
0
      goto alloc_fail;
571
0
  } 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
0
  if (primary->npermitopen > 0) {
577
0
    if (dup_strings(&ret->permitopen, &ret->npermitopen,
578
0
        primary->permitopen, primary->npermitopen) != 0)
579
0
      goto alloc_fail;
580
0
  } 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
0
  if (primary->npermitlisten > 0) {
587
0
    if (dup_strings(&ret->permitlisten, &ret->npermitlisten,
588
0
        primary->permitlisten, primary->npermitlisten) != 0)
589
0
      goto alloc_fail;
590
0
  } 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
0
#define OPTFLAG_AND(x) ret->x = (primary->x == 1) && (additional->x == 1)
597
0
#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
0
  OPTFLAG_AND(permit_port_forwarding_flag);
600
0
  OPTFLAG_AND(permit_agent_forwarding_flag);
601
0
  OPTFLAG_AND(permit_x11_forwarding_flag);
602
0
  OPTFLAG_AND(permit_pty_flag);
603
0
  OPTFLAG_AND(permit_user_rc);
604
0
  OPTFLAG_AND(no_require_user_presence);
605
  /* Restrictive flags are logical-OR (i.e. must be set in either) */
606
0
  OPTFLAG_OR(require_verify);
607
0
  OPTFLAG_OR(restricted);
608
0
#undef OPTFLAG_AND
609
610
  /* Earliest expiry time should win */
611
0
  if (primary->valid_before != 0)
612
0
    ret->valid_before = primary->valid_before;
613
0
  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
0
  if (primary->force_command != NULL &&
622
0
      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
0
  } else if (primary->force_command != NULL) {
634
0
    if ((ret->force_command = strdup(
635
0
        primary->force_command)) == NULL)
636
0
      goto alloc_fail;
637
0
  } 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
0
  if (errstrp != NULL)
644
0
    *errstrp = NULL;
645
0
  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
}