blob: 0a191dbbac4ad1df84ca76f114ab82c292c66635 [file] [log] [blame]
markus@openbsd.org75b8af82017-05-31 10:54:00 +00001/* $OpenBSD: auth-options.c,v 1.73 2017/05/31 10:54:00 markus 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
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000064void
Damien Miller874d77b2000-10-14 16:23:11 +110065auth_clear_options(void)
66{
67 no_agent_forwarding_flag = 0;
68 no_port_forwarding_flag = 0;
69 no_pty_flag = 0;
70 no_x11_forwarding_flag = 0;
Damien Miller95e80952008-03-27 11:03:05 +110071 no_user_rc = 0;
Damien Miller0a80ca12010-02-27 07:55:05 +110072 key_is_cert_authority = 0;
Damien Miller874d77b2000-10-14 16:23:11 +110073 while (custom_environment) {
74 struct envstring *ce = custom_environment;
75 custom_environment = ce->next;
Darren Tuckera627d422013-06-02 07:31:17 +100076 free(ce->s);
77 free(ce);
Damien Miller874d77b2000-10-14 16:23:11 +110078 }
mmcc@openbsd.orgd59ce082015-12-10 17:08:40 +000079 free(forced_command);
80 forced_command = NULL;
81 free(authorized_principals);
82 authorized_principals = NULL;
Damien Millerd27b9472005-12-13 19:29:02 +110083 forced_tun_device = -1;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +000084 channel_clear_permitted_opens();
Damien Miller874d77b2000-10-14 16:23:11 +110085}
86
Ben Lindstrom226cfa02001-01-22 05:34:40 +000087/*
djm@openbsd.org383f10f2015-11-16 00:30:02 +000088 * Match flag 'opt' in *optsp, and if allow_negate is set then also match
89 * 'no-opt'. Returns -1 if option not matched, 1 if option matches or 0
90 * if negated option matches.
91 * If the option or negated option matches, then *optsp is updated to
92 * point to the first character after the option and, if 'msg' is not NULL
93 * then a message based on it added via auth_debug_add().
94 */
95static int
96match_flag(const char *opt, int allow_negate, char **optsp, const char *msg)
97{
98 size_t opt_len = strlen(opt);
99 char *opts = *optsp;
100 int negate = 0;
101
102 if (allow_negate && strncasecmp(opts, "no-", 3) == 0) {
103 opts += 3;
104 negate = 1;
105 }
106 if (strncasecmp(opts, opt, opt_len) == 0) {
107 *optsp = opts + opt_len;
108 if (msg != NULL) {
109 auth_debug_add("%s %s.", msg,
110 negate ? "disabled" : "enabled");
111 }
112 return negate ? 0 : 1;
113 }
114 return -1;
115}
116
117/*
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000118 * return 1 if access is granted, 0 if not.
119 * side effect: sets key option flags
120 */
Damien Millerf6d9e222000-06-18 14:50:44 +1000121int
markus@openbsd.org75b8af82017-05-31 10:54:00 +0000122auth_parse_options(struct passwd *pw, char *opts, const char *file,
123 u_long linenum)
Damien Millerf6d9e222000-06-18 14:50:44 +1000124{
djm@openbsd.org95767262016-03-07 19:02:43 +0000125 struct ssh *ssh = active_state; /* XXX */
Damien Millerf6d9e222000-06-18 14:50:44 +1000126 const char *cp;
djm@openbsd.org383f10f2015-11-16 00:30:02 +0000127 int i, r;
Damien Miller874d77b2000-10-14 16:23:11 +1100128
129 /* reset options */
130 auth_clear_options();
131
Ben Lindstrom36d7bd02001-02-10 22:27:19 +0000132 if (!opts)
133 return 1;
134
Damien Miller33804262001-02-04 23:20:18 +1100135 while (*opts && *opts != ' ' && *opts != '\t') {
djm@openbsd.org383f10f2015-11-16 00:30:02 +0000136 if ((r = match_flag("cert-authority", 0, &opts, NULL)) != -1) {
137 key_is_cert_authority = r;
Damien Miller0a80ca12010-02-27 07:55:05 +1100138 goto next_option;
139 }
djm@openbsd.org383f10f2015-11-16 00:30:02 +0000140 if ((r = match_flag("restrict", 0, &opts, NULL)) != -1) {
141 auth_debug_add("Key is restricted.");
Damien Millerf6d9e222000-06-18 14:50:44 +1000142 no_port_forwarding_flag = 1;
Damien Millerf6d9e222000-06-18 14:50:44 +1000143 no_agent_forwarding_flag = 1;
Damien Millerf6d9e222000-06-18 14:50:44 +1000144 no_x11_forwarding_flag = 1;
Damien Millerf6d9e222000-06-18 14:50:44 +1000145 no_pty_flag = 1;
djm@openbsd.org383f10f2015-11-16 00:30:02 +0000146 no_user_rc = 1;
Damien Millerf6d9e222000-06-18 14:50:44 +1000147 goto next_option;
148 }
djm@openbsd.org383f10f2015-11-16 00:30:02 +0000149 if ((r = match_flag("port-forwarding", 1, &opts,
150 "Port forwarding")) != -1) {
151 no_port_forwarding_flag = r != 1;
152 goto next_option;
153 }
154 if ((r = match_flag("agent-forwarding", 1, &opts,
155 "Agent forwarding")) != -1) {
156 no_agent_forwarding_flag = r != 1;
157 goto next_option;
158 }
159 if ((r = match_flag("x11-forwarding", 1, &opts,
160 "X11 forwarding")) != -1) {
161 no_x11_forwarding_flag = r != 1;
162 goto next_option;
163 }
164 if ((r = match_flag("pty", 1, &opts,
165 "PTY allocation")) != -1) {
166 no_pty_flag = r != 1;
167 goto next_option;
168 }
169 if ((r = match_flag("user-rc", 1, &opts,
170 "User rc execution")) != -1) {
171 no_user_rc = r != 1;
Damien Miller95e80952008-03-27 11:03:05 +1100172 goto next_option;
173 }
Damien Millerf6d9e222000-06-18 14:50:44 +1000174 cp = "command=\"";
Damien Miller33804262001-02-04 23:20:18 +1100175 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
Damien Miller33804262001-02-04 23:20:18 +1100176 opts += strlen(cp);
mmcc@openbsd.orgd59ce082015-12-10 17:08:40 +0000177 free(forced_command);
Damien Miller33804262001-02-04 23:20:18 +1100178 forced_command = xmalloc(strlen(opts) + 1);
Damien Millerf6d9e222000-06-18 14:50:44 +1000179 i = 0;
Damien Miller33804262001-02-04 23:20:18 +1100180 while (*opts) {
181 if (*opts == '"')
Damien Millerf6d9e222000-06-18 14:50:44 +1000182 break;
Damien Miller33804262001-02-04 23:20:18 +1100183 if (*opts == '\\' && opts[1] == '"') {
184 opts += 2;
Damien Millerf6d9e222000-06-18 14:50:44 +1000185 forced_command[i++] = '"';
186 continue;
187 }
Damien Miller33804262001-02-04 23:20:18 +1100188 forced_command[i++] = *opts++;
Damien Millerf6d9e222000-06-18 14:50:44 +1000189 }
Damien Miller33804262001-02-04 23:20:18 +1100190 if (!*opts) {
Damien Millerf6d9e222000-06-18 14:50:44 +1000191 debug("%.100s, line %lu: missing end quote",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000192 file, linenum);
Ben Lindstroma574cda2002-05-15 16:16:14 +0000193 auth_debug_add("%.100s, line %lu: missing end quote",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000194 file, linenum);
Darren Tuckera627d422013-06-02 07:31:17 +1000195 free(forced_command);
Damien Miller056ddf72001-03-14 10:15:20 +1100196 forced_command = NULL;
197 goto bad_option;
Damien Millerf6d9e222000-06-18 14:50:44 +1000198 }
Damien Miller98299262006-07-24 14:01:43 +1000199 forced_command[i] = '\0';
Damien Millerde53fd02011-01-06 22:44:18 +1100200 auth_debug_add("Forced command.");
Damien Miller33804262001-02-04 23:20:18 +1100201 opts++;
Damien Millerf6d9e222000-06-18 14:50:44 +1000202 goto next_option;
203 }
Damien Miller30da3442010-05-10 11:58:03 +1000204 cp = "principals=\"";
205 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
206 opts += strlen(cp);
mmcc@openbsd.orgd59ce082015-12-10 17:08:40 +0000207 free(authorized_principals);
Damien Miller30da3442010-05-10 11:58:03 +1000208 authorized_principals = xmalloc(strlen(opts) + 1);
209 i = 0;
210 while (*opts) {
211 if (*opts == '"')
212 break;
213 if (*opts == '\\' && opts[1] == '"') {
214 opts += 2;
215 authorized_principals[i++] = '"';
216 continue;
217 }
218 authorized_principals[i++] = *opts++;
219 }
220 if (!*opts) {
221 debug("%.100s, line %lu: missing end quote",
222 file, linenum);
223 auth_debug_add("%.100s, line %lu: missing end quote",
224 file, linenum);
Darren Tuckera627d422013-06-02 07:31:17 +1000225 free(authorized_principals);
Damien Miller30da3442010-05-10 11:58:03 +1000226 authorized_principals = NULL;
227 goto bad_option;
228 }
229 authorized_principals[i] = '\0';
230 auth_debug_add("principals: %.900s",
231 authorized_principals);
232 opts++;
233 goto next_option;
234 }
Damien Millerf6d9e222000-06-18 14:50:44 +1000235 cp = "environment=\"";
djm@openbsd.orga42d67b2015-05-01 03:20:54 +0000236 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
Damien Millerf6d9e222000-06-18 14:50:44 +1000237 char *s;
238 struct envstring *new_envstring;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +0000239
Damien Miller33804262001-02-04 23:20:18 +1100240 opts += strlen(cp);
241 s = xmalloc(strlen(opts) + 1);
Damien Millerf6d9e222000-06-18 14:50:44 +1000242 i = 0;
Damien Miller33804262001-02-04 23:20:18 +1100243 while (*opts) {
244 if (*opts == '"')
Damien Millerf6d9e222000-06-18 14:50:44 +1000245 break;
Damien Miller33804262001-02-04 23:20:18 +1100246 if (*opts == '\\' && opts[1] == '"') {
247 opts += 2;
Damien Millerf6d9e222000-06-18 14:50:44 +1000248 s[i++] = '"';
249 continue;
250 }
Damien Miller33804262001-02-04 23:20:18 +1100251 s[i++] = *opts++;
Damien Millerf6d9e222000-06-18 14:50:44 +1000252 }
Damien Miller33804262001-02-04 23:20:18 +1100253 if (!*opts) {
Damien Millerf6d9e222000-06-18 14:50:44 +1000254 debug("%.100s, line %lu: missing end quote",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000255 file, linenum);
Ben Lindstroma574cda2002-05-15 16:16:14 +0000256 auth_debug_add("%.100s, line %lu: missing end quote",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000257 file, linenum);
Darren Tuckera627d422013-06-02 07:31:17 +1000258 free(s);
Damien Miller056ddf72001-03-14 10:15:20 +1100259 goto bad_option;
Damien Millerf6d9e222000-06-18 14:50:44 +1000260 }
Damien Miller98299262006-07-24 14:01:43 +1000261 s[i] = '\0';
Damien Miller33804262001-02-04 23:20:18 +1100262 opts++;
djm@openbsd.orga42d67b2015-05-01 03:20:54 +0000263 if (options.permit_user_env) {
264 auth_debug_add("Adding to environment: "
265 "%.900s", s);
266 debug("Adding to environment: %.900s", s);
267 new_envstring = xcalloc(1,
268 sizeof(*new_envstring));
269 new_envstring->s = s;
270 new_envstring->next = custom_environment;
271 custom_environment = new_envstring;
272 s = NULL;
273 }
274 free(s);
Damien Millerf6d9e222000-06-18 14:50:44 +1000275 goto next_option;
276 }
277 cp = "from=\"";
Damien Miller33804262001-02-04 23:20:18 +1100278 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
djm@openbsd.org95767262016-03-07 19:02:43 +0000279 const char *remote_ip = ssh_remote_ipaddr(ssh);
280 const char *remote_host = auth_get_canonical_hostname(
281 ssh, options.use_dns);
Damien Miller33804262001-02-04 23:20:18 +1100282 char *patterns = xmalloc(strlen(opts) + 1);
Ben Lindstrom7bb8b492001-03-17 00:47:54 +0000283
Damien Miller33804262001-02-04 23:20:18 +1100284 opts += strlen(cp);
Damien Millerf6d9e222000-06-18 14:50:44 +1000285 i = 0;
Damien Miller33804262001-02-04 23:20:18 +1100286 while (*opts) {
287 if (*opts == '"')
Damien Millerf6d9e222000-06-18 14:50:44 +1000288 break;
Damien Miller33804262001-02-04 23:20:18 +1100289 if (*opts == '\\' && opts[1] == '"') {
290 opts += 2;
Damien Millerf6d9e222000-06-18 14:50:44 +1000291 patterns[i++] = '"';
292 continue;
293 }
Damien Miller33804262001-02-04 23:20:18 +1100294 patterns[i++] = *opts++;
Damien Millerf6d9e222000-06-18 14:50:44 +1000295 }
Damien Miller33804262001-02-04 23:20:18 +1100296 if (!*opts) {
Damien Millerf6d9e222000-06-18 14:50:44 +1000297 debug("%.100s, line %lu: missing end quote",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000298 file, linenum);
Ben Lindstroma574cda2002-05-15 16:16:14 +0000299 auth_debug_add("%.100s, line %lu: missing end quote",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000300 file, linenum);
Darren Tuckera627d422013-06-02 07:31:17 +1000301 free(patterns);
Damien Miller056ddf72001-03-14 10:15:20 +1100302 goto bad_option;
Damien Millerf6d9e222000-06-18 14:50:44 +1000303 }
Damien Miller98299262006-07-24 14:01:43 +1000304 patterns[i] = '\0';
Damien Miller33804262001-02-04 23:20:18 +1100305 opts++;
Darren Tucker896ad5a2008-06-11 09:34:46 +1000306 switch (match_host_and_ip(remote_host, remote_ip,
307 patterns)) {
308 case 1:
Darren Tuckera627d422013-06-02 07:31:17 +1000309 free(patterns);
Darren Tucker896ad5a2008-06-11 09:34:46 +1000310 /* Host name matches. */
311 goto next_option;
312 case -1:
313 debug("%.100s, line %lu: invalid criteria",
314 file, linenum);
315 auth_debug_add("%.100s, line %lu: "
316 "invalid criteria", file, linenum);
317 /* FALLTHROUGH */
318 case 0:
Darren Tuckera627d422013-06-02 07:31:17 +1000319 free(patterns);
Damien Miller996acd22003-04-09 20:59:48 +1000320 logit("Authentication tried for %.100s with "
Damien Miller33804262001-02-04 23:20:18 +1100321 "correct key but not from a permitted "
322 "host (host=%.200s, ip=%.200s).",
323 pw->pw_name, remote_host, remote_ip);
Ben Lindstroma574cda2002-05-15 16:16:14 +0000324 auth_debug_add("Your host '%.200s' is not "
Damien Miller33804262001-02-04 23:20:18 +1100325 "permitted to use this key for login.",
326 remote_host);
Darren Tucker896ad5a2008-06-11 09:34:46 +1000327 break;
Damien Millerf6d9e222000-06-18 14:50:44 +1000328 }
Darren Tucker896ad5a2008-06-11 09:34:46 +1000329 /* deny access */
330 return 0;
Damien Millerf6d9e222000-06-18 14:50:44 +1000331 }
Ben Lindstrom7bb8b492001-03-17 00:47:54 +0000332 cp = "permitopen=\"";
333 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
Damien Millerf91ee4c2005-03-01 21:24:33 +1100334 char *host, *p;
Damien Millere37dde02009-01-28 16:33:01 +1100335 int port;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +0000336 char *patterns = xmalloc(strlen(opts) + 1);
337
338 opts += strlen(cp);
339 i = 0;
340 while (*opts) {
341 if (*opts == '"')
342 break;
343 if (*opts == '\\' && opts[1] == '"') {
344 opts += 2;
345 patterns[i++] = '"';
346 continue;
347 }
348 patterns[i++] = *opts++;
349 }
350 if (!*opts) {
351 debug("%.100s, line %lu: missing end quote",
352 file, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100353 auth_debug_add("%.100s, line %lu: missing "
354 "end quote", file, linenum);
Darren Tuckera627d422013-06-02 07:31:17 +1000355 free(patterns);
Ben Lindstrom7bb8b492001-03-17 00:47:54 +0000356 goto bad_option;
357 }
Damien Miller98299262006-07-24 14:01:43 +1000358 patterns[i] = '\0';
Ben Lindstrom7bb8b492001-03-17 00:47:54 +0000359 opts++;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100360 p = patterns;
Damien Miller7acefbb2014-07-18 14:11:24 +1000361 /* XXX - add streamlocal support */
Damien Millerf91ee4c2005-03-01 21:24:33 +1100362 host = hpdelim(&p);
363 if (host == NULL || strlen(host) >= NI_MAXHOST) {
364 debug("%.100s, line %lu: Bad permitopen "
Darren Tucker90b9e022005-03-14 23:08:50 +1100365 "specification <%.100s>", file, linenum,
Damien Millerf91ee4c2005-03-01 21:24:33 +1100366 patterns);
Ben Lindstroma574cda2002-05-15 16:16:14 +0000367 auth_debug_add("%.100s, line %lu: "
Damien Millerf91ee4c2005-03-01 21:24:33 +1100368 "Bad permitopen specification", file,
369 linenum);
Darren Tuckera627d422013-06-02 07:31:17 +1000370 free(patterns);
Ben Lindstrom7bb8b492001-03-17 00:47:54 +0000371 goto bad_option;
372 }
Darren Tucker47eede72005-03-14 23:08:12 +1100373 host = cleanhostname(host);
Darren Tucker1338b9e2011-10-02 18:57:35 +1100374 if (p == NULL || (port = permitopen_port(p)) < 0) {
Damien Millerf91ee4c2005-03-01 21:24:33 +1100375 debug("%.100s, line %lu: Bad permitopen port "
376 "<%.100s>", file, linenum, p ? p : "");
Ben Lindstroma574cda2002-05-15 16:16:14 +0000377 auth_debug_add("%.100s, line %lu: "
Ben Lindstromd71ba572001-09-12 18:03:31 +0000378 "Bad permitopen port", file, linenum);
Darren Tuckera627d422013-06-02 07:31:17 +1000379 free(patterns);
Ben Lindstrom7bb8b492001-03-17 00:47:54 +0000380 goto bad_option;
381 }
Damien Milleraa5b3f82012-12-03 09:50:54 +1100382 if ((options.allow_tcp_forwarding & FORWARD_LOCAL) != 0)
Ben Lindstromd71ba572001-09-12 18:03:31 +0000383 channel_add_permitted_opens(host, port);
Darren Tuckera627d422013-06-02 07:31:17 +1000384 free(patterns);
Ben Lindstrom7bb8b492001-03-17 00:47:54 +0000385 goto next_option;
386 }
Damien Millerd27b9472005-12-13 19:29:02 +1100387 cp = "tunnel=\"";
388 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
389 char *tun = NULL;
390 opts += strlen(cp);
391 tun = xmalloc(strlen(opts) + 1);
392 i = 0;
393 while (*opts) {
394 if (*opts == '"')
395 break;
396 tun[i++] = *opts++;
397 }
398 if (!*opts) {
399 debug("%.100s, line %lu: missing end quote",
400 file, linenum);
401 auth_debug_add("%.100s, line %lu: missing end quote",
402 file, linenum);
Darren Tuckera627d422013-06-02 07:31:17 +1000403 free(tun);
Damien Millerd27b9472005-12-13 19:29:02 +1100404 forced_tun_device = -1;
405 goto bad_option;
406 }
Damien Miller98299262006-07-24 14:01:43 +1000407 tun[i] = '\0';
Damien Millerd27b9472005-12-13 19:29:02 +1100408 forced_tun_device = a2tun(tun, NULL);
Darren Tuckera627d422013-06-02 07:31:17 +1000409 free(tun);
Damien Miller7b58e802005-12-13 19:33:19 +1100410 if (forced_tun_device == SSH_TUNID_ERR) {
Damien Millerd27b9472005-12-13 19:29:02 +1100411 debug("%.100s, line %lu: invalid tun device",
412 file, linenum);
413 auth_debug_add("%.100s, line %lu: invalid tun device",
414 file, linenum);
415 forced_tun_device = -1;
416 goto bad_option;
417 }
418 auth_debug_add("Forced tun device: %d", forced_tun_device);
419 opts++;
420 goto next_option;
421 }
Damien Millerf6d9e222000-06-18 14:50:44 +1000422next_option:
423 /*
424 * Skip the comma, and move to the next option
425 * (or break out if there are no more).
426 */
Damien Miller33804262001-02-04 23:20:18 +1100427 if (!*opts)
Damien Millerf6d9e222000-06-18 14:50:44 +1000428 fatal("Bugs in auth-options.c option processing.");
Damien Miller33804262001-02-04 23:20:18 +1100429 if (*opts == ' ' || *opts == '\t')
Damien Millerf6d9e222000-06-18 14:50:44 +1000430 break; /* End of options. */
Damien Miller33804262001-02-04 23:20:18 +1100431 if (*opts != ',')
Damien Millerf6d9e222000-06-18 14:50:44 +1000432 goto bad_option;
Damien Miller33804262001-02-04 23:20:18 +1100433 opts++;
Damien Millerf6d9e222000-06-18 14:50:44 +1000434 /* Process the next option. */
435 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000436
Damien Millerf6d9e222000-06-18 14:50:44 +1000437 /* grant access */
438 return 1;
439
440bad_option:
Damien Miller996acd22003-04-09 20:59:48 +1000441 logit("Bad options in %.100s file, line %lu: %.50s",
Damien Miller33804262001-02-04 23:20:18 +1100442 file, linenum, opts);
Ben Lindstroma574cda2002-05-15 16:16:14 +0000443 auth_debug_add("Bad options in %.100s file, line %lu: %.50s",
Damien Miller33804262001-02-04 23:20:18 +1100444 file, linenum, opts);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000445
Damien Millerf6d9e222000-06-18 14:50:44 +1000446 /* deny access */
447 return 0;
448}
Damien Miller0a80ca12010-02-27 07:55:05 +1100449
Damien Millerd0e4a8e2010-05-21 14:58:32 +1000450#define OPTIONS_CRITICAL 1
451#define OPTIONS_EXTENSIONS 2
452static int
markus@openbsd.orgae8b4632015-01-14 10:30:34 +0000453parse_option_list(struct sshbuf *oblob, struct passwd *pw,
Damien Millerd0e4a8e2010-05-21 14:58:32 +1000454 u_int which, int crit,
455 int *cert_no_port_forwarding_flag,
456 int *cert_no_agent_forwarding_flag,
457 int *cert_no_x11_forwarding_flag,
458 int *cert_no_pty_flag,
459 int *cert_no_user_rc,
460 char **cert_forced_command,
461 int *cert_source_address_done)
Damien Miller0a80ca12010-02-27 07:55:05 +1100462{
djm@openbsd.org95767262016-03-07 19:02:43 +0000463 struct ssh *ssh = active_state; /* XXX */
Damien Millerd0e4a8e2010-05-21 14:58:32 +1000464 char *command, *allowed;
465 const char *remote_ip;
Damien Millerce986542013-07-18 16:12:44 +1000466 char *name = NULL;
markus@openbsd.orgae8b4632015-01-14 10:30:34 +0000467 struct sshbuf *c = NULL, *data = NULL;
468 int r, ret = -1, result, found;
Damien Miller0a80ca12010-02-27 07:55:05 +1100469
markus@openbsd.orgae8b4632015-01-14 10:30:34 +0000470 if ((c = sshbuf_fromb(oblob)) == NULL) {
471 error("%s: sshbuf_fromb failed", __func__);
472 goto out;
473 }
Damien Miller0a80ca12010-02-27 07:55:05 +1100474
markus@openbsd.orgae8b4632015-01-14 10:30:34 +0000475 while (sshbuf_len(c) > 0) {
476 sshbuf_free(data);
477 data = NULL;
478 if ((r = sshbuf_get_cstring(c, &name, NULL)) != 0 ||
479 (r = sshbuf_froms(c, &data)) != 0) {
480 error("Unable to parse certificate options: %s",
481 ssh_err(r));
Damien Miller0a80ca12010-02-27 07:55:05 +1100482 goto out;
483 }
markus@openbsd.orgae8b4632015-01-14 10:30:34 +0000484 debug3("found certificate option \"%.100s\" len %zu",
485 name, sshbuf_len(data));
Damien Millerd0e4a8e2010-05-21 14:58:32 +1000486 found = 0;
487 if ((which & OPTIONS_EXTENSIONS) != 0) {
488 if (strcmp(name, "permit-X11-forwarding") == 0) {
489 *cert_no_x11_forwarding_flag = 0;
490 found = 1;
491 } else if (strcmp(name,
492 "permit-agent-forwarding") == 0) {
493 *cert_no_agent_forwarding_flag = 0;
494 found = 1;
495 } else if (strcmp(name,
496 "permit-port-forwarding") == 0) {
497 *cert_no_port_forwarding_flag = 0;
498 found = 1;
499 } else if (strcmp(name, "permit-pty") == 0) {
500 *cert_no_pty_flag = 0;
501 found = 1;
502 } else if (strcmp(name, "permit-user-rc") == 0) {
503 *cert_no_user_rc = 0;
504 found = 1;
Damien Miller0a80ca12010-02-27 07:55:05 +1100505 }
Damien Millerd0e4a8e2010-05-21 14:58:32 +1000506 }
507 if (!found && (which & OPTIONS_CRITICAL) != 0) {
508 if (strcmp(name, "force-command") == 0) {
markus@openbsd.orgae8b4632015-01-14 10:30:34 +0000509 if ((r = sshbuf_get_cstring(data, &command,
510 NULL)) != 0) {
511 error("Unable to parse \"%s\" "
512 "section: %s", name, ssh_err(r));
Damien Millerd0e4a8e2010-05-21 14:58:32 +1000513 goto out;
514 }
Damien Millerd0e4a8e2010-05-21 14:58:32 +1000515 if (*cert_forced_command != NULL) {
516 error("Certificate has multiple "
517 "force-command options");
Darren Tuckera627d422013-06-02 07:31:17 +1000518 free(command);
Damien Millerd0e4a8e2010-05-21 14:58:32 +1000519 goto out;
520 }
521 *cert_forced_command = command;
522 found = 1;
Damien Miller41396572010-03-04 21:51:11 +1100523 }
Damien Millerd0e4a8e2010-05-21 14:58:32 +1000524 if (strcmp(name, "source-address") == 0) {
markus@openbsd.orgae8b4632015-01-14 10:30:34 +0000525 if ((r = sshbuf_get_cstring(data, &allowed,
526 NULL)) != 0) {
527 error("Unable to parse \"%s\" "
528 "section: %s", name, ssh_err(r));
Damien Millerd0e4a8e2010-05-21 14:58:32 +1000529 goto out;
530 }
Damien Millerd0e4a8e2010-05-21 14:58:32 +1000531 if ((*cert_source_address_done)++) {
532 error("Certificate has multiple "
533 "source-address options");
Darren Tuckera627d422013-06-02 07:31:17 +1000534 free(allowed);
Damien Millerd0e4a8e2010-05-21 14:58:32 +1000535 goto out;
536 }
djm@openbsd.org95767262016-03-07 19:02:43 +0000537 remote_ip = ssh_remote_ipaddr(ssh);
Damien Millerbf25d112013-12-29 17:44:56 +1100538 result = addr_match_cidr_list(remote_ip,
539 allowed);
540 free(allowed);
541 switch (result) {
Damien Millerd0e4a8e2010-05-21 14:58:32 +1000542 case 1:
543 /* accepted */
Damien Millerd0e4a8e2010-05-21 14:58:32 +1000544 break;
545 case 0:
546 /* no match */
547 logit("Authentication tried for %.100s "
548 "with valid certificate but not "
549 "from a permitted host "
550 "(ip=%.200s).", pw->pw_name,
551 remote_ip);
552 auth_debug_add("Your address '%.200s' "
553 "is not permitted to use this "
554 "certificate for login.",
555 remote_ip);
Damien Millerd0e4a8e2010-05-21 14:58:32 +1000556 goto out;
557 case -1:
Damien Millerbf25d112013-12-29 17:44:56 +1100558 default:
Damien Millerd0e4a8e2010-05-21 14:58:32 +1000559 error("Certificate source-address "
560 "contents invalid");
Damien Millerd0e4a8e2010-05-21 14:58:32 +1000561 goto out;
562 }
563 found = 1;
Damien Miller0a80ca12010-02-27 07:55:05 +1100564 }
Damien Miller0a80ca12010-02-27 07:55:05 +1100565 }
566
Damien Millerd0e4a8e2010-05-21 14:58:32 +1000567 if (!found) {
568 if (crit) {
569 error("Certificate critical option \"%s\" "
570 "is not supported", name);
571 goto out;
572 } else {
573 logit("Certificate extension \"%s\" "
574 "is not supported", name);
575 }
markus@openbsd.orgae8b4632015-01-14 10:30:34 +0000576 } else if (sshbuf_len(data) != 0) {
Damien Millerd0e4a8e2010-05-21 14:58:32 +1000577 error("Certificate option \"%s\" corrupt "
Damien Miller0a80ca12010-02-27 07:55:05 +1100578 "(extra data)", name);
579 goto out;
580 }
Darren Tuckera627d422013-06-02 07:31:17 +1000581 free(name);
Damien Millerce986542013-07-18 16:12:44 +1000582 name = NULL;
Damien Miller0a80ca12010-02-27 07:55:05 +1100583 }
Damien Miller4e270b02010-04-16 15:56:21 +1000584 /* successfully parsed all options */
Damien Miller0a80ca12010-02-27 07:55:05 +1100585 ret = 0;
586
Damien Millerd0e4a8e2010-05-21 14:58:32 +1000587 out:
588 if (ret != 0 &&
589 cert_forced_command != NULL &&
590 *cert_forced_command != NULL) {
Darren Tuckera627d422013-06-02 07:31:17 +1000591 free(*cert_forced_command);
Damien Millerd0e4a8e2010-05-21 14:58:32 +1000592 *cert_forced_command = NULL;
593 }
mmcc@openbsd.orgd59ce082015-12-10 17:08:40 +0000594 free(name);
markus@openbsd.orgae8b4632015-01-14 10:30:34 +0000595 sshbuf_free(data);
596 sshbuf_free(c);
Damien Millerd0e4a8e2010-05-21 14:58:32 +1000597 return ret;
598}
599
600/*
601 * Set options from critical certificate options. These supersede user key
602 * options so this must be called after auth_parse_options().
603 */
604int
djm@openbsd.orgfd6dcef2016-11-30 02:57:40 +0000605auth_cert_options(struct sshkey *k, struct passwd *pw, const char **reason)
Damien Millerd0e4a8e2010-05-21 14:58:32 +1000606{
607 int cert_no_port_forwarding_flag = 1;
608 int cert_no_agent_forwarding_flag = 1;
609 int cert_no_x11_forwarding_flag = 1;
610 int cert_no_pty_flag = 1;
611 int cert_no_user_rc = 1;
612 char *cert_forced_command = NULL;
613 int cert_source_address_done = 0;
614
djm@openbsd.orgfd6dcef2016-11-30 02:57:40 +0000615 *reason = "invalid certificate options";
616
djm@openbsd.orgc28fc622015-07-03 03:43:18 +0000617 /* Separate options and extensions for v01 certs */
618 if (parse_option_list(k->cert->critical, pw,
619 OPTIONS_CRITICAL, 1, NULL, NULL, NULL, NULL, NULL,
620 &cert_forced_command,
621 &cert_source_address_done) == -1)
622 return -1;
623 if (parse_option_list(k->cert->extensions, pw,
624 OPTIONS_EXTENSIONS, 0,
625 &cert_no_port_forwarding_flag,
626 &cert_no_agent_forwarding_flag,
627 &cert_no_x11_forwarding_flag,
628 &cert_no_pty_flag,
629 &cert_no_user_rc,
630 NULL, NULL) == -1)
631 return -1;
Damien Millerd0e4a8e2010-05-21 14:58:32 +1000632
Damien Miller0a80ca12010-02-27 07:55:05 +1100633 no_port_forwarding_flag |= cert_no_port_forwarding_flag;
634 no_agent_forwarding_flag |= cert_no_agent_forwarding_flag;
635 no_x11_forwarding_flag |= cert_no_x11_forwarding_flag;
636 no_pty_flag |= cert_no_pty_flag;
637 no_user_rc |= cert_no_user_rc;
djm@openbsd.orgfd6dcef2016-11-30 02:57:40 +0000638 /*
639 * Only permit both CA and key option forced-command if they match.
640 * Otherwise refuse the certificate.
641 */
642 if (cert_forced_command != NULL && forced_command != NULL) {
643 if (strcmp(forced_command, cert_forced_command) == 0) {
644 free(forced_command);
645 forced_command = cert_forced_command;
646 } else {
647 *reason = "certificate and key options forced command "
648 "do not match";
649 free(cert_forced_command);
650 return -1;
651 }
652 } else if (cert_forced_command != NULL)
Damien Miller0a80ca12010-02-27 07:55:05 +1100653 forced_command = cert_forced_command;
djm@openbsd.orgfd6dcef2016-11-30 02:57:40 +0000654 /* success */
655 *reason = NULL;
Damien Millerd0e4a8e2010-05-21 14:58:32 +1000656 return 0;
Damien Miller0a80ca12010-02-27 07:55:05 +1100657}
658