Coverage Report

Created: 2025-06-22 06:46

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