blob: bed00eef0fe3619b982a06dce82f3fe894f2eb00 [file] [log] [blame]
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001/* $OpenBSD: auth-options.c,v 1.74 2017/09/12 06:32:07 djm Exp $ */
Damien Millere4340be2000-09-16 13:29:08 +11002/*
3 * Author: Tatu Ylonen <ylo@cs.hut.fi>
4 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5 * All rights reserved
Damien Millere4340be2000-09-16 13:29:08 +11006 * As far as I am concerned, the code I have written for this software
7 * can be used freely for any purpose. Any derived versions of this
8 * software must be clearly marked as such, and if the derived work is
9 * incompatible with the protocol description in the RFC file, it must be
10 * called by a name other than "ssh" or "Secure Shell".
11 */
12
Damien Millerf6d9e222000-06-18 14:50:44 +100013#include "includes.h"
Damien Millerf6d9e222000-06-18 14:50:44 +100014
Damien Miller9f2abc42006-07-10 20:53:08 +100015#include <sys/types.h>
16
Damien Millerb8fe89c2006-07-24 14:51:00 +100017#include <netdb.h>
Damien Miller9f2abc42006-07-10 20:53:08 +100018#include <pwd.h>
Damien Millere3476ed2006-07-24 14:13:33 +100019#include <string.h>
Damien Millerd7834352006-08-05 12:39:39 +100020#include <stdio.h>
21#include <stdarg.h>
Damien Miller9f2abc42006-07-10 20:53:08 +100022
Damien Millerb84886b2008-05-19 15:05:07 +100023#include "openbsd-compat/sys-queue.h"
markus@openbsd.orgae8b4632015-01-14 10:30:34 +000024
25#include "key.h" /* XXX for typedef */
26#include "buffer.h" /* XXX for typedef */
Damien Millerf6d9e222000-06-18 14:50:44 +100027#include "xmalloc.h"
28#include "match.h"
markus@openbsd.orgae8b4632015-01-14 10:30:34 +000029#include "ssherr.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000030#include "log.h"
31#include "canohost.h"
djm@openbsd.org95767262016-03-07 19:02:43 +000032#include "packet.h"
markus@openbsd.orgae8b4632015-01-14 10:30:34 +000033#include "sshbuf.h"
Damien Miller7acefbb2014-07-18 14:11:24 +100034#include "misc.h"
Ben Lindstromc7637672001-06-09 00:36:26 +000035#include "channels.h"
Damien Miller33804262001-02-04 23:20:18 +110036#include "servconf.h"
markus@openbsd.orgae8b4632015-01-14 10:30:34 +000037#include "sshkey.h"
Damien Miller4e270b02010-04-16 15:56:21 +100038#include "auth-options.h"
Damien Millerd7834352006-08-05 12:39:39 +100039#include "hostfile.h"
Ben Lindstroma574cda2002-05-15 16:16:14 +000040#include "auth.h"
Damien Millerf6d9e222000-06-18 14:50:44 +100041
42/* Flags set authorized_keys flags */
43int no_port_forwarding_flag = 0;
44int no_agent_forwarding_flag = 0;
45int no_x11_forwarding_flag = 0;
46int no_pty_flag = 0;
Damien Miller95e80952008-03-27 11:03:05 +110047int no_user_rc = 0;
Damien Miller0a80ca12010-02-27 07:55:05 +110048int key_is_cert_authority = 0;
Damien Millerf6d9e222000-06-18 14:50:44 +100049
50/* "command=" option. */
51char *forced_command = NULL;
52
53/* "environment=" options. */
54struct envstring *custom_environment = NULL;
55
Damien Millerd27b9472005-12-13 19:29:02 +110056/* "tunnel=" option. */
57int forced_tun_device = -1;
58
Damien Miller30da3442010-05-10 11:58:03 +100059/* "principals=" option. */
60char *authorized_principals = NULL;
61
Damien Miller33804262001-02-04 23:20:18 +110062extern ServerOptions options;
63
djm@openbsd.orgdbee4112017-09-12 06:32:07 +000064/* XXX refactor to be stateless */
65
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000066void
Damien Miller874d77b2000-10-14 16:23:11 +110067auth_clear_options(void)
68{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +000069 struct ssh *ssh = active_state; /* XXX */
70
Damien Miller874d77b2000-10-14 16:23:11 +110071 no_agent_forwarding_flag = 0;
72 no_port_forwarding_flag = 0;
73 no_pty_flag = 0;
74 no_x11_forwarding_flag = 0;
Damien Miller95e80952008-03-27 11:03:05 +110075 no_user_rc = 0;
Damien Miller0a80ca12010-02-27 07:55:05 +110076 key_is_cert_authority = 0;
Damien Miller874d77b2000-10-14 16:23:11 +110077 while (custom_environment) {
78 struct envstring *ce = custom_environment;
79 custom_environment = ce->next;
Darren Tuckera627d422013-06-02 07:31:17 +100080 free(ce->s);
81 free(ce);
Damien Miller874d77b2000-10-14 16:23:11 +110082 }
mmcc@openbsd.orgd59ce082015-12-10 17:08:40 +000083 free(forced_command);
84 forced_command = NULL;
85 free(authorized_principals);
86 authorized_principals = NULL;
Damien Millerd27b9472005-12-13 19:29:02 +110087 forced_tun_device = -1;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +000088 channel_clear_permitted_opens(ssh);
Damien Miller874d77b2000-10-14 16:23:11 +110089}
90
Ben Lindstrom226cfa02001-01-22 05:34:40 +000091/*
djm@openbsd.org383f10f2015-11-16 00:30:02 +000092 * Match flag 'opt' in *optsp, and if allow_negate is set then also match
93 * 'no-opt'. Returns -1 if option not matched, 1 if option matches or 0
94 * if negated option matches.
95 * If the option or negated option matches, then *optsp is updated to
96 * point to the first character after the option and, if 'msg' is not NULL
97 * then a message based on it added via auth_debug_add().
98 */
99static int
100match_flag(const char *opt, int allow_negate, char **optsp, const char *msg)
101{
102 size_t opt_len = strlen(opt);
103 char *opts = *optsp;
104 int negate = 0;
105
106 if (allow_negate && strncasecmp(opts, "no-", 3) == 0) {
107 opts += 3;
108 negate = 1;
109 }
110 if (strncasecmp(opts, opt, opt_len) == 0) {
111 *optsp = opts + opt_len;
112 if (msg != NULL) {
113 auth_debug_add("%s %s.", msg,
114 negate ? "disabled" : "enabled");
115 }
116 return negate ? 0 : 1;
117 }
118 return -1;
119}
120
121/*
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000122 * return 1 if access is granted, 0 if not.
123 * side effect: sets key option flags
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000124 * XXX remove side effects; fill structure instead.
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000125 */
Damien Millerf6d9e222000-06-18 14:50:44 +1000126int
markus@openbsd.org75b8af82017-05-31 10:54:00 +0000127auth_parse_options(struct passwd *pw, char *opts, const char *file,
128 u_long linenum)
Damien Millerf6d9e222000-06-18 14:50:44 +1000129{
djm@openbsd.org95767262016-03-07 19:02:43 +0000130 struct ssh *ssh = active_state; /* XXX */
Damien Millerf6d9e222000-06-18 14:50:44 +1000131 const char *cp;
djm@openbsd.org383f10f2015-11-16 00:30:02 +0000132 int i, r;
Damien Miller874d77b2000-10-14 16:23:11 +1100133
134 /* reset options */
135 auth_clear_options();
136
Ben Lindstrom36d7bd02001-02-10 22:27:19 +0000137 if (!opts)
138 return 1;
139
Damien Miller33804262001-02-04 23:20:18 +1100140 while (*opts && *opts != ' ' && *opts != '\t') {
djm@openbsd.org383f10f2015-11-16 00:30:02 +0000141 if ((r = match_flag("cert-authority", 0, &opts, NULL)) != -1) {
142 key_is_cert_authority = r;
Damien Miller0a80ca12010-02-27 07:55:05 +1100143 goto next_option;
144 }
djm@openbsd.org383f10f2015-11-16 00:30:02 +0000145 if ((r = match_flag("restrict", 0, &opts, NULL)) != -1) {
146 auth_debug_add("Key is restricted.");
Damien Millerf6d9e222000-06-18 14:50:44 +1000147 no_port_forwarding_flag = 1;
Damien Millerf6d9e222000-06-18 14:50:44 +1000148 no_agent_forwarding_flag = 1;
Damien Millerf6d9e222000-06-18 14:50:44 +1000149 no_x11_forwarding_flag = 1;
Damien Millerf6d9e222000-06-18 14:50:44 +1000150 no_pty_flag = 1;
djm@openbsd.org383f10f2015-11-16 00:30:02 +0000151 no_user_rc = 1;
Damien Millerf6d9e222000-06-18 14:50:44 +1000152 goto next_option;
153 }
djm@openbsd.org383f10f2015-11-16 00:30:02 +0000154 if ((r = match_flag("port-forwarding", 1, &opts,
155 "Port forwarding")) != -1) {
156 no_port_forwarding_flag = r != 1;
157 goto next_option;
158 }
159 if ((r = match_flag("agent-forwarding", 1, &opts,
160 "Agent forwarding")) != -1) {
161 no_agent_forwarding_flag = r != 1;
162 goto next_option;
163 }
164 if ((r = match_flag("x11-forwarding", 1, &opts,
165 "X11 forwarding")) != -1) {
166 no_x11_forwarding_flag = r != 1;
167 goto next_option;
168 }
169 if ((r = match_flag("pty", 1, &opts,
170 "PTY allocation")) != -1) {
171 no_pty_flag = r != 1;
172 goto next_option;
173 }
174 if ((r = match_flag("user-rc", 1, &opts,
175 "User rc execution")) != -1) {
176 no_user_rc = r != 1;
Damien Miller95e80952008-03-27 11:03:05 +1100177 goto next_option;
178 }
Damien Millerf6d9e222000-06-18 14:50:44 +1000179 cp = "command=\"";
Damien Miller33804262001-02-04 23:20:18 +1100180 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
Damien Miller33804262001-02-04 23:20:18 +1100181 opts += strlen(cp);
mmcc@openbsd.orgd59ce082015-12-10 17:08:40 +0000182 free(forced_command);
Damien Miller33804262001-02-04 23:20:18 +1100183 forced_command = xmalloc(strlen(opts) + 1);
Damien Millerf6d9e222000-06-18 14:50:44 +1000184 i = 0;
Damien Miller33804262001-02-04 23:20:18 +1100185 while (*opts) {
186 if (*opts == '"')
Damien Millerf6d9e222000-06-18 14:50:44 +1000187 break;
Damien Miller33804262001-02-04 23:20:18 +1100188 if (*opts == '\\' && opts[1] == '"') {
189 opts += 2;
Damien Millerf6d9e222000-06-18 14:50:44 +1000190 forced_command[i++] = '"';
191 continue;
192 }
Damien Miller33804262001-02-04 23:20:18 +1100193 forced_command[i++] = *opts++;
Damien Millerf6d9e222000-06-18 14:50:44 +1000194 }
Damien Miller33804262001-02-04 23:20:18 +1100195 if (!*opts) {
Damien Millerf6d9e222000-06-18 14:50:44 +1000196 debug("%.100s, line %lu: missing end quote",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000197 file, linenum);
Ben Lindstroma574cda2002-05-15 16:16:14 +0000198 auth_debug_add("%.100s, line %lu: missing end quote",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000199 file, linenum);
Darren Tuckera627d422013-06-02 07:31:17 +1000200 free(forced_command);
Damien Miller056ddf72001-03-14 10:15:20 +1100201 forced_command = NULL;
202 goto bad_option;
Damien Millerf6d9e222000-06-18 14:50:44 +1000203 }
Damien Miller98299262006-07-24 14:01:43 +1000204 forced_command[i] = '\0';
Damien Millerde53fd02011-01-06 22:44:18 +1100205 auth_debug_add("Forced command.");
Damien Miller33804262001-02-04 23:20:18 +1100206 opts++;
Damien Millerf6d9e222000-06-18 14:50:44 +1000207 goto next_option;
208 }
Damien Miller30da3442010-05-10 11:58:03 +1000209 cp = "principals=\"";
210 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
211 opts += strlen(cp);
mmcc@openbsd.orgd59ce082015-12-10 17:08:40 +0000212 free(authorized_principals);
Damien Miller30da3442010-05-10 11:58:03 +1000213 authorized_principals = xmalloc(strlen(opts) + 1);
214 i = 0;
215 while (*opts) {
216 if (*opts == '"')
217 break;
218 if (*opts == '\\' && opts[1] == '"') {
219 opts += 2;
220 authorized_principals[i++] = '"';
221 continue;
222 }
223 authorized_principals[i++] = *opts++;
224 }
225 if (!*opts) {
226 debug("%.100s, line %lu: missing end quote",
227 file, linenum);
228 auth_debug_add("%.100s, line %lu: missing end quote",
229 file, linenum);
Darren Tuckera627d422013-06-02 07:31:17 +1000230 free(authorized_principals);
Damien Miller30da3442010-05-10 11:58:03 +1000231 authorized_principals = NULL;
232 goto bad_option;
233 }
234 authorized_principals[i] = '\0';
235 auth_debug_add("principals: %.900s",
236 authorized_principals);
237 opts++;
238 goto next_option;
239 }
Damien Millerf6d9e222000-06-18 14:50:44 +1000240 cp = "environment=\"";
djm@openbsd.orga42d67b2015-05-01 03:20:54 +0000241 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
Damien Millerf6d9e222000-06-18 14:50:44 +1000242 char *s;
243 struct envstring *new_envstring;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +0000244
Damien Miller33804262001-02-04 23:20:18 +1100245 opts += strlen(cp);
246 s = xmalloc(strlen(opts) + 1);
Damien Millerf6d9e222000-06-18 14:50:44 +1000247 i = 0;
Damien Miller33804262001-02-04 23:20:18 +1100248 while (*opts) {
249 if (*opts == '"')
Damien Millerf6d9e222000-06-18 14:50:44 +1000250 break;
Damien Miller33804262001-02-04 23:20:18 +1100251 if (*opts == '\\' && opts[1] == '"') {
252 opts += 2;
Damien Millerf6d9e222000-06-18 14:50:44 +1000253 s[i++] = '"';
254 continue;
255 }
Damien Miller33804262001-02-04 23:20:18 +1100256 s[i++] = *opts++;
Damien Millerf6d9e222000-06-18 14:50:44 +1000257 }
Damien Miller33804262001-02-04 23:20:18 +1100258 if (!*opts) {
Damien Millerf6d9e222000-06-18 14:50:44 +1000259 debug("%.100s, line %lu: missing end quote",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000260 file, linenum);
Ben Lindstroma574cda2002-05-15 16:16:14 +0000261 auth_debug_add("%.100s, line %lu: missing end quote",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000262 file, linenum);
Darren Tuckera627d422013-06-02 07:31:17 +1000263 free(s);
Damien Miller056ddf72001-03-14 10:15:20 +1100264 goto bad_option;
Damien Millerf6d9e222000-06-18 14:50:44 +1000265 }
Damien Miller98299262006-07-24 14:01:43 +1000266 s[i] = '\0';
Damien Miller33804262001-02-04 23:20:18 +1100267 opts++;
djm@openbsd.orga42d67b2015-05-01 03:20:54 +0000268 if (options.permit_user_env) {
269 auth_debug_add("Adding to environment: "
270 "%.900s", s);
271 debug("Adding to environment: %.900s", s);
272 new_envstring = xcalloc(1,
273 sizeof(*new_envstring));
274 new_envstring->s = s;
275 new_envstring->next = custom_environment;
276 custom_environment = new_envstring;
277 s = NULL;
278 }
279 free(s);
Damien Millerf6d9e222000-06-18 14:50:44 +1000280 goto next_option;
281 }
282 cp = "from=\"";
Damien Miller33804262001-02-04 23:20:18 +1100283 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
djm@openbsd.org95767262016-03-07 19:02:43 +0000284 const char *remote_ip = ssh_remote_ipaddr(ssh);
285 const char *remote_host = auth_get_canonical_hostname(
286 ssh, options.use_dns);
Damien Miller33804262001-02-04 23:20:18 +1100287 char *patterns = xmalloc(strlen(opts) + 1);
Ben Lindstrom7bb8b492001-03-17 00:47:54 +0000288
Damien Miller33804262001-02-04 23:20:18 +1100289 opts += strlen(cp);
Damien Millerf6d9e222000-06-18 14:50:44 +1000290 i = 0;
Damien Miller33804262001-02-04 23:20:18 +1100291 while (*opts) {
292 if (*opts == '"')
Damien Millerf6d9e222000-06-18 14:50:44 +1000293 break;
Damien Miller33804262001-02-04 23:20:18 +1100294 if (*opts == '\\' && opts[1] == '"') {
295 opts += 2;
Damien Millerf6d9e222000-06-18 14:50:44 +1000296 patterns[i++] = '"';
297 continue;
298 }
Damien Miller33804262001-02-04 23:20:18 +1100299 patterns[i++] = *opts++;
Damien Millerf6d9e222000-06-18 14:50:44 +1000300 }
Damien Miller33804262001-02-04 23:20:18 +1100301 if (!*opts) {
Damien Millerf6d9e222000-06-18 14:50:44 +1000302 debug("%.100s, line %lu: missing end quote",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000303 file, linenum);
Ben Lindstroma574cda2002-05-15 16:16:14 +0000304 auth_debug_add("%.100s, line %lu: missing end quote",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000305 file, linenum);
Darren Tuckera627d422013-06-02 07:31:17 +1000306 free(patterns);
Damien Miller056ddf72001-03-14 10:15:20 +1100307 goto bad_option;
Damien Millerf6d9e222000-06-18 14:50:44 +1000308 }
Damien Miller98299262006-07-24 14:01:43 +1000309 patterns[i] = '\0';
Damien Miller33804262001-02-04 23:20:18 +1100310 opts++;
Darren Tucker896ad5a2008-06-11 09:34:46 +1000311 switch (match_host_and_ip(remote_host, remote_ip,
312 patterns)) {
313 case 1:
Darren Tuckera627d422013-06-02 07:31:17 +1000314 free(patterns);
Darren Tucker896ad5a2008-06-11 09:34:46 +1000315 /* Host name matches. */
316 goto next_option;
317 case -1:
318 debug("%.100s, line %lu: invalid criteria",
319 file, linenum);
320 auth_debug_add("%.100s, line %lu: "
321 "invalid criteria", file, linenum);
322 /* FALLTHROUGH */
323 case 0:
Darren Tuckera627d422013-06-02 07:31:17 +1000324 free(patterns);
Damien Miller996acd22003-04-09 20:59:48 +1000325 logit("Authentication tried for %.100s with "
Damien Miller33804262001-02-04 23:20:18 +1100326 "correct key but not from a permitted "
327 "host (host=%.200s, ip=%.200s).",
328 pw->pw_name, remote_host, remote_ip);
Ben Lindstroma574cda2002-05-15 16:16:14 +0000329 auth_debug_add("Your host '%.200s' is not "
Damien Miller33804262001-02-04 23:20:18 +1100330 "permitted to use this key for login.",
331 remote_host);
Darren Tucker896ad5a2008-06-11 09:34:46 +1000332 break;
Damien Millerf6d9e222000-06-18 14:50:44 +1000333 }
Darren Tucker896ad5a2008-06-11 09:34:46 +1000334 /* deny access */
335 return 0;
Damien Millerf6d9e222000-06-18 14:50:44 +1000336 }
Ben Lindstrom7bb8b492001-03-17 00:47:54 +0000337 cp = "permitopen=\"";
338 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
Damien Millerf91ee4c2005-03-01 21:24:33 +1100339 char *host, *p;
Damien Millere37dde02009-01-28 16:33:01 +1100340 int port;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +0000341 char *patterns = xmalloc(strlen(opts) + 1);
342
343 opts += strlen(cp);
344 i = 0;
345 while (*opts) {
346 if (*opts == '"')
347 break;
348 if (*opts == '\\' && opts[1] == '"') {
349 opts += 2;
350 patterns[i++] = '"';
351 continue;
352 }
353 patterns[i++] = *opts++;
354 }
355 if (!*opts) {
356 debug("%.100s, line %lu: missing end quote",
357 file, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100358 auth_debug_add("%.100s, line %lu: missing "
359 "end quote", file, linenum);
Darren Tuckera627d422013-06-02 07:31:17 +1000360 free(patterns);
Ben Lindstrom7bb8b492001-03-17 00:47:54 +0000361 goto bad_option;
362 }
Damien Miller98299262006-07-24 14:01:43 +1000363 patterns[i] = '\0';
Ben Lindstrom7bb8b492001-03-17 00:47:54 +0000364 opts++;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100365 p = patterns;
Damien Miller7acefbb2014-07-18 14:11:24 +1000366 /* XXX - add streamlocal support */
Damien Millerf91ee4c2005-03-01 21:24:33 +1100367 host = hpdelim(&p);
368 if (host == NULL || strlen(host) >= NI_MAXHOST) {
369 debug("%.100s, line %lu: Bad permitopen "
Darren Tucker90b9e022005-03-14 23:08:50 +1100370 "specification <%.100s>", file, linenum,
Damien Millerf91ee4c2005-03-01 21:24:33 +1100371 patterns);
Ben Lindstroma574cda2002-05-15 16:16:14 +0000372 auth_debug_add("%.100s, line %lu: "
Damien Millerf91ee4c2005-03-01 21:24:33 +1100373 "Bad permitopen specification", file,
374 linenum);
Darren Tuckera627d422013-06-02 07:31:17 +1000375 free(patterns);
Ben Lindstrom7bb8b492001-03-17 00:47:54 +0000376 goto bad_option;
377 }
Darren Tucker47eede72005-03-14 23:08:12 +1100378 host = cleanhostname(host);
Darren Tucker1338b9e2011-10-02 18:57:35 +1100379 if (p == NULL || (port = permitopen_port(p)) < 0) {
Damien Millerf91ee4c2005-03-01 21:24:33 +1100380 debug("%.100s, line %lu: Bad permitopen port "
381 "<%.100s>", file, linenum, p ? p : "");
Ben Lindstroma574cda2002-05-15 16:16:14 +0000382 auth_debug_add("%.100s, line %lu: "
Ben Lindstromd71ba572001-09-12 18:03:31 +0000383 "Bad permitopen port", file, linenum);
Darren Tuckera627d422013-06-02 07:31:17 +1000384 free(patterns);
Ben Lindstrom7bb8b492001-03-17 00:47:54 +0000385 goto bad_option;
386 }
Damien Milleraa5b3f82012-12-03 09:50:54 +1100387 if ((options.allow_tcp_forwarding & FORWARD_LOCAL) != 0)
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000388 channel_add_permitted_opens(ssh, host, port);
Darren Tuckera627d422013-06-02 07:31:17 +1000389 free(patterns);
Ben Lindstrom7bb8b492001-03-17 00:47:54 +0000390 goto next_option;
391 }
Damien Millerd27b9472005-12-13 19:29:02 +1100392 cp = "tunnel=\"";
393 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
394 char *tun = NULL;
395 opts += strlen(cp);
396 tun = xmalloc(strlen(opts) + 1);
397 i = 0;
398 while (*opts) {
399 if (*opts == '"')
400 break;
401 tun[i++] = *opts++;
402 }
403 if (!*opts) {
404 debug("%.100s, line %lu: missing end quote",
405 file, linenum);
406 auth_debug_add("%.100s, line %lu: missing end quote",
407 file, linenum);
Darren Tuckera627d422013-06-02 07:31:17 +1000408 free(tun);
Damien Millerd27b9472005-12-13 19:29:02 +1100409 forced_tun_device = -1;
410 goto bad_option;
411 }
Damien Miller98299262006-07-24 14:01:43 +1000412 tun[i] = '\0';
Damien Millerd27b9472005-12-13 19:29:02 +1100413 forced_tun_device = a2tun(tun, NULL);
Darren Tuckera627d422013-06-02 07:31:17 +1000414 free(tun);
Damien Miller7b58e802005-12-13 19:33:19 +1100415 if (forced_tun_device == SSH_TUNID_ERR) {
Damien Millerd27b9472005-12-13 19:29:02 +1100416 debug("%.100s, line %lu: invalid tun device",
417 file, linenum);
418 auth_debug_add("%.100s, line %lu: invalid tun device",
419 file, linenum);
420 forced_tun_device = -1;
421 goto bad_option;
422 }
423 auth_debug_add("Forced tun device: %d", forced_tun_device);
424 opts++;
425 goto next_option;
426 }
Damien Millerf6d9e222000-06-18 14:50:44 +1000427next_option:
428 /*
429 * Skip the comma, and move to the next option
430 * (or break out if there are no more).
431 */
Damien Miller33804262001-02-04 23:20:18 +1100432 if (!*opts)
Damien Millerf6d9e222000-06-18 14:50:44 +1000433 fatal("Bugs in auth-options.c option processing.");
Damien Miller33804262001-02-04 23:20:18 +1100434 if (*opts == ' ' || *opts == '\t')
Damien Millerf6d9e222000-06-18 14:50:44 +1000435 break; /* End of options. */
Damien Miller33804262001-02-04 23:20:18 +1100436 if (*opts != ',')
Damien Millerf6d9e222000-06-18 14:50:44 +1000437 goto bad_option;
Damien Miller33804262001-02-04 23:20:18 +1100438 opts++;
Damien Millerf6d9e222000-06-18 14:50:44 +1000439 /* Process the next option. */
440 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000441
Damien Millerf6d9e222000-06-18 14:50:44 +1000442 /* grant access */
443 return 1;
444
445bad_option:
Damien Miller996acd22003-04-09 20:59:48 +1000446 logit("Bad options in %.100s file, line %lu: %.50s",
Damien Miller33804262001-02-04 23:20:18 +1100447 file, linenum, opts);
Ben Lindstroma574cda2002-05-15 16:16:14 +0000448 auth_debug_add("Bad options in %.100s file, line %lu: %.50s",
Damien Miller33804262001-02-04 23:20:18 +1100449 file, linenum, opts);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000450
Damien Millerf6d9e222000-06-18 14:50:44 +1000451 /* deny access */
452 return 0;
453}
Damien Miller0a80ca12010-02-27 07:55:05 +1100454
Damien Millerd0e4a8e2010-05-21 14:58:32 +1000455#define OPTIONS_CRITICAL 1
456#define OPTIONS_EXTENSIONS 2
457static int
markus@openbsd.orgae8b4632015-01-14 10:30:34 +0000458parse_option_list(struct sshbuf *oblob, struct passwd *pw,
Damien Millerd0e4a8e2010-05-21 14:58:32 +1000459 u_int which, int crit,
460 int *cert_no_port_forwarding_flag,
461 int *cert_no_agent_forwarding_flag,
462 int *cert_no_x11_forwarding_flag,
463 int *cert_no_pty_flag,
464 int *cert_no_user_rc,
465 char **cert_forced_command,
466 int *cert_source_address_done)
Damien Miller0a80ca12010-02-27 07:55:05 +1100467{
djm@openbsd.org95767262016-03-07 19:02:43 +0000468 struct ssh *ssh = active_state; /* XXX */
Damien Millerd0e4a8e2010-05-21 14:58:32 +1000469 char *command, *allowed;
470 const char *remote_ip;
Damien Millerce986542013-07-18 16:12:44 +1000471 char *name = NULL;
markus@openbsd.orgae8b4632015-01-14 10:30:34 +0000472 struct sshbuf *c = NULL, *data = NULL;
473 int r, ret = -1, result, found;
Damien Miller0a80ca12010-02-27 07:55:05 +1100474
markus@openbsd.orgae8b4632015-01-14 10:30:34 +0000475 if ((c = sshbuf_fromb(oblob)) == NULL) {
476 error("%s: sshbuf_fromb failed", __func__);
477 goto out;
478 }
Damien Miller0a80ca12010-02-27 07:55:05 +1100479
markus@openbsd.orgae8b4632015-01-14 10:30:34 +0000480 while (sshbuf_len(c) > 0) {
481 sshbuf_free(data);
482 data = NULL;
483 if ((r = sshbuf_get_cstring(c, &name, NULL)) != 0 ||
484 (r = sshbuf_froms(c, &data)) != 0) {
485 error("Unable to parse certificate options: %s",
486 ssh_err(r));
Damien Miller0a80ca12010-02-27 07:55:05 +1100487 goto out;
488 }
markus@openbsd.orgae8b4632015-01-14 10:30:34 +0000489 debug3("found certificate option \"%.100s\" len %zu",
490 name, sshbuf_len(data));
Damien Millerd0e4a8e2010-05-21 14:58:32 +1000491 found = 0;
492 if ((which & OPTIONS_EXTENSIONS) != 0) {
493 if (strcmp(name, "permit-X11-forwarding") == 0) {
494 *cert_no_x11_forwarding_flag = 0;
495 found = 1;
496 } else if (strcmp(name,
497 "permit-agent-forwarding") == 0) {
498 *cert_no_agent_forwarding_flag = 0;
499 found = 1;
500 } else if (strcmp(name,
501 "permit-port-forwarding") == 0) {
502 *cert_no_port_forwarding_flag = 0;
503 found = 1;
504 } else if (strcmp(name, "permit-pty") == 0) {
505 *cert_no_pty_flag = 0;
506 found = 1;
507 } else if (strcmp(name, "permit-user-rc") == 0) {
508 *cert_no_user_rc = 0;
509 found = 1;
Damien Miller0a80ca12010-02-27 07:55:05 +1100510 }
Damien Millerd0e4a8e2010-05-21 14:58:32 +1000511 }
512 if (!found && (which & OPTIONS_CRITICAL) != 0) {
513 if (strcmp(name, "force-command") == 0) {
markus@openbsd.orgae8b4632015-01-14 10:30:34 +0000514 if ((r = sshbuf_get_cstring(data, &command,
515 NULL)) != 0) {
516 error("Unable to parse \"%s\" "
517 "section: %s", name, ssh_err(r));
Damien Millerd0e4a8e2010-05-21 14:58:32 +1000518 goto out;
519 }
Damien Millerd0e4a8e2010-05-21 14:58:32 +1000520 if (*cert_forced_command != NULL) {
521 error("Certificate has multiple "
522 "force-command options");
Darren Tuckera627d422013-06-02 07:31:17 +1000523 free(command);
Damien Millerd0e4a8e2010-05-21 14:58:32 +1000524 goto out;
525 }
526 *cert_forced_command = command;
527 found = 1;
Damien Miller41396572010-03-04 21:51:11 +1100528 }
Damien Millerd0e4a8e2010-05-21 14:58:32 +1000529 if (strcmp(name, "source-address") == 0) {
markus@openbsd.orgae8b4632015-01-14 10:30:34 +0000530 if ((r = sshbuf_get_cstring(data, &allowed,
531 NULL)) != 0) {
532 error("Unable to parse \"%s\" "
533 "section: %s", name, ssh_err(r));
Damien Millerd0e4a8e2010-05-21 14:58:32 +1000534 goto out;
535 }
Damien Millerd0e4a8e2010-05-21 14:58:32 +1000536 if ((*cert_source_address_done)++) {
537 error("Certificate has multiple "
538 "source-address options");
Darren Tuckera627d422013-06-02 07:31:17 +1000539 free(allowed);
Damien Millerd0e4a8e2010-05-21 14:58:32 +1000540 goto out;
541 }
djm@openbsd.org95767262016-03-07 19:02:43 +0000542 remote_ip = ssh_remote_ipaddr(ssh);
Damien Millerbf25d112013-12-29 17:44:56 +1100543 result = addr_match_cidr_list(remote_ip,
544 allowed);
545 free(allowed);
546 switch (result) {
Damien Millerd0e4a8e2010-05-21 14:58:32 +1000547 case 1:
548 /* accepted */
Damien Millerd0e4a8e2010-05-21 14:58:32 +1000549 break;
550 case 0:
551 /* no match */
552 logit("Authentication tried for %.100s "
553 "with valid certificate but not "
554 "from a permitted host "
555 "(ip=%.200s).", pw->pw_name,
556 remote_ip);
557 auth_debug_add("Your address '%.200s' "
558 "is not permitted to use this "
559 "certificate for login.",
560 remote_ip);
Damien Millerd0e4a8e2010-05-21 14:58:32 +1000561 goto out;
562 case -1:
Damien Millerbf25d112013-12-29 17:44:56 +1100563 default:
Damien Millerd0e4a8e2010-05-21 14:58:32 +1000564 error("Certificate source-address "
565 "contents invalid");
Damien Millerd0e4a8e2010-05-21 14:58:32 +1000566 goto out;
567 }
568 found = 1;
Damien Miller0a80ca12010-02-27 07:55:05 +1100569 }
Damien Miller0a80ca12010-02-27 07:55:05 +1100570 }
571
Damien Millerd0e4a8e2010-05-21 14:58:32 +1000572 if (!found) {
573 if (crit) {
574 error("Certificate critical option \"%s\" "
575 "is not supported", name);
576 goto out;
577 } else {
578 logit("Certificate extension \"%s\" "
579 "is not supported", name);
580 }
markus@openbsd.orgae8b4632015-01-14 10:30:34 +0000581 } else if (sshbuf_len(data) != 0) {
Damien Millerd0e4a8e2010-05-21 14:58:32 +1000582 error("Certificate option \"%s\" corrupt "
Damien Miller0a80ca12010-02-27 07:55:05 +1100583 "(extra data)", name);
584 goto out;
585 }
Darren Tuckera627d422013-06-02 07:31:17 +1000586 free(name);
Damien Millerce986542013-07-18 16:12:44 +1000587 name = NULL;
Damien Miller0a80ca12010-02-27 07:55:05 +1100588 }
Damien Miller4e270b02010-04-16 15:56:21 +1000589 /* successfully parsed all options */
Damien Miller0a80ca12010-02-27 07:55:05 +1100590 ret = 0;
591
Damien Millerd0e4a8e2010-05-21 14:58:32 +1000592 out:
593 if (ret != 0 &&
594 cert_forced_command != NULL &&
595 *cert_forced_command != NULL) {
Darren Tuckera627d422013-06-02 07:31:17 +1000596 free(*cert_forced_command);
Damien Millerd0e4a8e2010-05-21 14:58:32 +1000597 *cert_forced_command = NULL;
598 }
mmcc@openbsd.orgd59ce082015-12-10 17:08:40 +0000599 free(name);
markus@openbsd.orgae8b4632015-01-14 10:30:34 +0000600 sshbuf_free(data);
601 sshbuf_free(c);
Damien Millerd0e4a8e2010-05-21 14:58:32 +1000602 return ret;
603}
604
605/*
606 * Set options from critical certificate options. These supersede user key
607 * options so this must be called after auth_parse_options().
608 */
609int
djm@openbsd.orgfd6dcef2016-11-30 02:57:40 +0000610auth_cert_options(struct sshkey *k, struct passwd *pw, const char **reason)
Damien Millerd0e4a8e2010-05-21 14:58:32 +1000611{
612 int cert_no_port_forwarding_flag = 1;
613 int cert_no_agent_forwarding_flag = 1;
614 int cert_no_x11_forwarding_flag = 1;
615 int cert_no_pty_flag = 1;
616 int cert_no_user_rc = 1;
617 char *cert_forced_command = NULL;
618 int cert_source_address_done = 0;
619
djm@openbsd.orgfd6dcef2016-11-30 02:57:40 +0000620 *reason = "invalid certificate options";
621
djm@openbsd.orgc28fc622015-07-03 03:43:18 +0000622 /* Separate options and extensions for v01 certs */
623 if (parse_option_list(k->cert->critical, pw,
624 OPTIONS_CRITICAL, 1, NULL, NULL, NULL, NULL, NULL,
625 &cert_forced_command,
626 &cert_source_address_done) == -1)
627 return -1;
628 if (parse_option_list(k->cert->extensions, pw,
629 OPTIONS_EXTENSIONS, 0,
630 &cert_no_port_forwarding_flag,
631 &cert_no_agent_forwarding_flag,
632 &cert_no_x11_forwarding_flag,
633 &cert_no_pty_flag,
634 &cert_no_user_rc,
635 NULL, NULL) == -1)
636 return -1;
Damien Millerd0e4a8e2010-05-21 14:58:32 +1000637
Damien Miller0a80ca12010-02-27 07:55:05 +1100638 no_port_forwarding_flag |= cert_no_port_forwarding_flag;
639 no_agent_forwarding_flag |= cert_no_agent_forwarding_flag;
640 no_x11_forwarding_flag |= cert_no_x11_forwarding_flag;
641 no_pty_flag |= cert_no_pty_flag;
642 no_user_rc |= cert_no_user_rc;
djm@openbsd.orgfd6dcef2016-11-30 02:57:40 +0000643 /*
644 * Only permit both CA and key option forced-command if they match.
645 * Otherwise refuse the certificate.
646 */
647 if (cert_forced_command != NULL && forced_command != NULL) {
648 if (strcmp(forced_command, cert_forced_command) == 0) {
649 free(forced_command);
650 forced_command = cert_forced_command;
651 } else {
652 *reason = "certificate and key options forced command "
653 "do not match";
654 free(cert_forced_command);
655 return -1;
656 }
657 } else if (cert_forced_command != NULL)
Damien Miller0a80ca12010-02-27 07:55:05 +1100658 forced_command = cert_forced_command;
djm@openbsd.orgfd6dcef2016-11-30 02:57:40 +0000659 /* success */
660 *reason = NULL;
Damien Millerd0e4a8e2010-05-21 14:58:32 +1000661 return 0;
Damien Miller0a80ca12010-02-27 07:55:05 +1100662}
663