Coverage Report

Created: 2023-06-07 07:08

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