blob: edbaf80bbf0921b6597eb28c131e8fbef18a72ab [file] [log] [blame]
mmcc@openbsd.orgd59ce082015-12-10 17:08:40 +00001/* $OpenBSD: auth-options.c,v 1.70 2015/12/10 17:08:40 mmcc 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"
markus@openbsd.orgae8b4632015-01-14 10:30:34 +000032#include "sshbuf.h"
Damien Miller7acefbb2014-07-18 14:11:24 +100033#include "misc.h"
Ben Lindstromc7637672001-06-09 00:36:26 +000034#include "channels.h"
Damien Miller33804262001-02-04 23:20:18 +110035#include "servconf.h"
markus@openbsd.orgae8b4632015-01-14 10:30:34 +000036#include "sshkey.h"
Damien Miller4e270b02010-04-16 15:56:21 +100037#include "auth-options.h"
Damien Millerd7834352006-08-05 12:39:39 +100038#include "hostfile.h"
Ben Lindstroma574cda2002-05-15 16:16:14 +000039#include "auth.h"
Damien Millerf6d9e222000-06-18 14:50:44 +100040
41/* Flags set authorized_keys flags */
42int no_port_forwarding_flag = 0;
43int no_agent_forwarding_flag = 0;
44int no_x11_forwarding_flag = 0;
45int no_pty_flag = 0;
Damien Miller95e80952008-03-27 11:03:05 +110046int no_user_rc = 0;
Damien Miller0a80ca12010-02-27 07:55:05 +110047int key_is_cert_authority = 0;
Damien Millerf6d9e222000-06-18 14:50:44 +100048
49/* "command=" option. */
50char *forced_command = NULL;
51
52/* "environment=" options. */
53struct envstring *custom_environment = NULL;
54
Damien Millerd27b9472005-12-13 19:29:02 +110055/* "tunnel=" option. */
56int forced_tun_device = -1;
57
Damien Miller30da3442010-05-10 11:58:03 +100058/* "principals=" option. */
59char *authorized_principals = NULL;
60
Damien Miller33804262001-02-04 23:20:18 +110061extern ServerOptions options;
62
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000063void
Damien Miller874d77b2000-10-14 16:23:11 +110064auth_clear_options(void)
65{
66 no_agent_forwarding_flag = 0;
67 no_port_forwarding_flag = 0;
68 no_pty_flag = 0;
69 no_x11_forwarding_flag = 0;
Damien Miller95e80952008-03-27 11:03:05 +110070 no_user_rc = 0;
Damien Miller0a80ca12010-02-27 07:55:05 +110071 key_is_cert_authority = 0;
Damien Miller874d77b2000-10-14 16:23:11 +110072 while (custom_environment) {
73 struct envstring *ce = custom_environment;
74 custom_environment = ce->next;
Darren Tuckera627d422013-06-02 07:31:17 +100075 free(ce->s);
76 free(ce);
Damien Miller874d77b2000-10-14 16:23:11 +110077 }
mmcc@openbsd.orgd59ce082015-12-10 17:08:40 +000078 free(forced_command);
79 forced_command = NULL;
80 free(authorized_principals);
81 authorized_principals = NULL;
Damien Millerd27b9472005-12-13 19:29:02 +110082 forced_tun_device = -1;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +000083 channel_clear_permitted_opens();
Damien Miller874d77b2000-10-14 16:23:11 +110084}
85
Ben Lindstrom226cfa02001-01-22 05:34:40 +000086/*
djm@openbsd.org383f10f2015-11-16 00:30:02 +000087 * Match flag 'opt' in *optsp, and if allow_negate is set then also match
88 * 'no-opt'. Returns -1 if option not matched, 1 if option matches or 0
89 * if negated option matches.
90 * If the option or negated option matches, then *optsp is updated to
91 * point to the first character after the option and, if 'msg' is not NULL
92 * then a message based on it added via auth_debug_add().
93 */
94static int
95match_flag(const char *opt, int allow_negate, char **optsp, const char *msg)
96{
97 size_t opt_len = strlen(opt);
98 char *opts = *optsp;
99 int negate = 0;
100
101 if (allow_negate && strncasecmp(opts, "no-", 3) == 0) {
102 opts += 3;
103 negate = 1;
104 }
105 if (strncasecmp(opts, opt, opt_len) == 0) {
106 *optsp = opts + opt_len;
107 if (msg != NULL) {
108 auth_debug_add("%s %s.", msg,
109 negate ? "disabled" : "enabled");
110 }
111 return negate ? 0 : 1;
112 }
113 return -1;
114}
115
116/*
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000117 * return 1 if access is granted, 0 if not.
118 * side effect: sets key option flags
119 */
Damien Millerf6d9e222000-06-18 14:50:44 +1000120int
Damien Miller33804262001-02-04 23:20:18 +1100121auth_parse_options(struct passwd *pw, char *opts, char *file, u_long linenum)
Damien Millerf6d9e222000-06-18 14:50:44 +1000122{
123 const char *cp;
djm@openbsd.org383f10f2015-11-16 00:30:02 +0000124 int i, r;
Damien Miller874d77b2000-10-14 16:23:11 +1100125
126 /* reset options */
127 auth_clear_options();
128
Ben Lindstrom36d7bd02001-02-10 22:27:19 +0000129 if (!opts)
130 return 1;
131
Damien Miller33804262001-02-04 23:20:18 +1100132 while (*opts && *opts != ' ' && *opts != '\t') {
djm@openbsd.org383f10f2015-11-16 00:30:02 +0000133 if ((r = match_flag("cert-authority", 0, &opts, NULL)) != -1) {
134 key_is_cert_authority = r;
Damien Miller0a80ca12010-02-27 07:55:05 +1100135 goto next_option;
136 }
djm@openbsd.org383f10f2015-11-16 00:30:02 +0000137 if ((r = match_flag("restrict", 0, &opts, NULL)) != -1) {
138 auth_debug_add("Key is restricted.");
Damien Millerf6d9e222000-06-18 14:50:44 +1000139 no_port_forwarding_flag = 1;
Damien Millerf6d9e222000-06-18 14:50:44 +1000140 no_agent_forwarding_flag = 1;
Damien Millerf6d9e222000-06-18 14:50:44 +1000141 no_x11_forwarding_flag = 1;
Damien Millerf6d9e222000-06-18 14:50:44 +1000142 no_pty_flag = 1;
djm@openbsd.org383f10f2015-11-16 00:30:02 +0000143 no_user_rc = 1;
Damien Millerf6d9e222000-06-18 14:50:44 +1000144 goto next_option;
145 }
djm@openbsd.org383f10f2015-11-16 00:30:02 +0000146 if ((r = match_flag("port-forwarding", 1, &opts,
147 "Port forwarding")) != -1) {
148 no_port_forwarding_flag = r != 1;
149 goto next_option;
150 }
151 if ((r = match_flag("agent-forwarding", 1, &opts,
152 "Agent forwarding")) != -1) {
153 no_agent_forwarding_flag = r != 1;
154 goto next_option;
155 }
156 if ((r = match_flag("x11-forwarding", 1, &opts,
157 "X11 forwarding")) != -1) {
158 no_x11_forwarding_flag = r != 1;
159 goto next_option;
160 }
161 if ((r = match_flag("pty", 1, &opts,
162 "PTY allocation")) != -1) {
163 no_pty_flag = r != 1;
164 goto next_option;
165 }
166 if ((r = match_flag("user-rc", 1, &opts,
167 "User rc execution")) != -1) {
168 no_user_rc = r != 1;
Damien Miller95e80952008-03-27 11:03:05 +1100169 goto next_option;
170 }
Damien Millerf6d9e222000-06-18 14:50:44 +1000171 cp = "command=\"";
Damien Miller33804262001-02-04 23:20:18 +1100172 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
Damien Miller33804262001-02-04 23:20:18 +1100173 opts += strlen(cp);
mmcc@openbsd.orgd59ce082015-12-10 17:08:40 +0000174 free(forced_command);
Damien Miller33804262001-02-04 23:20:18 +1100175 forced_command = xmalloc(strlen(opts) + 1);
Damien Millerf6d9e222000-06-18 14:50:44 +1000176 i = 0;
Damien Miller33804262001-02-04 23:20:18 +1100177 while (*opts) {
178 if (*opts == '"')
Damien Millerf6d9e222000-06-18 14:50:44 +1000179 break;
Damien Miller33804262001-02-04 23:20:18 +1100180 if (*opts == '\\' && opts[1] == '"') {
181 opts += 2;
Damien Millerf6d9e222000-06-18 14:50:44 +1000182 forced_command[i++] = '"';
183 continue;
184 }
Damien Miller33804262001-02-04 23:20:18 +1100185 forced_command[i++] = *opts++;
Damien Millerf6d9e222000-06-18 14:50:44 +1000186 }
Damien Miller33804262001-02-04 23:20:18 +1100187 if (!*opts) {
Damien Millerf6d9e222000-06-18 14:50:44 +1000188 debug("%.100s, line %lu: missing end quote",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000189 file, linenum);
Ben Lindstroma574cda2002-05-15 16:16:14 +0000190 auth_debug_add("%.100s, line %lu: missing end quote",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000191 file, linenum);
Darren Tuckera627d422013-06-02 07:31:17 +1000192 free(forced_command);
Damien Miller056ddf72001-03-14 10:15:20 +1100193 forced_command = NULL;
194 goto bad_option;
Damien Millerf6d9e222000-06-18 14:50:44 +1000195 }
Damien Miller98299262006-07-24 14:01:43 +1000196 forced_command[i] = '\0';
Damien Millerde53fd02011-01-06 22:44:18 +1100197 auth_debug_add("Forced command.");
Damien Miller33804262001-02-04 23:20:18 +1100198 opts++;
Damien Millerf6d9e222000-06-18 14:50:44 +1000199 goto next_option;
200 }
Damien Miller30da3442010-05-10 11:58:03 +1000201 cp = "principals=\"";
202 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
203 opts += strlen(cp);
mmcc@openbsd.orgd59ce082015-12-10 17:08:40 +0000204 free(authorized_principals);
Damien Miller30da3442010-05-10 11:58:03 +1000205 authorized_principals = xmalloc(strlen(opts) + 1);
206 i = 0;
207 while (*opts) {
208 if (*opts == '"')
209 break;
210 if (*opts == '\\' && opts[1] == '"') {
211 opts += 2;
212 authorized_principals[i++] = '"';
213 continue;
214 }
215 authorized_principals[i++] = *opts++;
216 }
217 if (!*opts) {
218 debug("%.100s, line %lu: missing end quote",
219 file, linenum);
220 auth_debug_add("%.100s, line %lu: missing end quote",
221 file, linenum);
Darren Tuckera627d422013-06-02 07:31:17 +1000222 free(authorized_principals);
Damien Miller30da3442010-05-10 11:58:03 +1000223 authorized_principals = NULL;
224 goto bad_option;
225 }
226 authorized_principals[i] = '\0';
227 auth_debug_add("principals: %.900s",
228 authorized_principals);
229 opts++;
230 goto next_option;
231 }
Damien Millerf6d9e222000-06-18 14:50:44 +1000232 cp = "environment=\"";
djm@openbsd.orga42d67b2015-05-01 03:20:54 +0000233 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
Damien Millerf6d9e222000-06-18 14:50:44 +1000234 char *s;
235 struct envstring *new_envstring;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +0000236
Damien Miller33804262001-02-04 23:20:18 +1100237 opts += strlen(cp);
238 s = xmalloc(strlen(opts) + 1);
Damien Millerf6d9e222000-06-18 14:50:44 +1000239 i = 0;
Damien Miller33804262001-02-04 23:20:18 +1100240 while (*opts) {
241 if (*opts == '"')
Damien Millerf6d9e222000-06-18 14:50:44 +1000242 break;
Damien Miller33804262001-02-04 23:20:18 +1100243 if (*opts == '\\' && opts[1] == '"') {
244 opts += 2;
Damien Millerf6d9e222000-06-18 14:50:44 +1000245 s[i++] = '"';
246 continue;
247 }
Damien Miller33804262001-02-04 23:20:18 +1100248 s[i++] = *opts++;
Damien Millerf6d9e222000-06-18 14:50:44 +1000249 }
Damien Miller33804262001-02-04 23:20:18 +1100250 if (!*opts) {
Damien Millerf6d9e222000-06-18 14:50:44 +1000251 debug("%.100s, line %lu: missing end quote",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000252 file, linenum);
Ben Lindstroma574cda2002-05-15 16:16:14 +0000253 auth_debug_add("%.100s, line %lu: missing end quote",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000254 file, linenum);
Darren Tuckera627d422013-06-02 07:31:17 +1000255 free(s);
Damien Miller056ddf72001-03-14 10:15:20 +1100256 goto bad_option;
Damien Millerf6d9e222000-06-18 14:50:44 +1000257 }
Damien Miller98299262006-07-24 14:01:43 +1000258 s[i] = '\0';
Damien Miller33804262001-02-04 23:20:18 +1100259 opts++;
djm@openbsd.orga42d67b2015-05-01 03:20:54 +0000260 if (options.permit_user_env) {
261 auth_debug_add("Adding to environment: "
262 "%.900s", s);
263 debug("Adding to environment: %.900s", s);
264 new_envstring = xcalloc(1,
265 sizeof(*new_envstring));
266 new_envstring->s = s;
267 new_envstring->next = custom_environment;
268 custom_environment = new_envstring;
269 s = NULL;
270 }
271 free(s);
Damien Millerf6d9e222000-06-18 14:50:44 +1000272 goto next_option;
273 }
274 cp = "from=\"";
Damien Miller33804262001-02-04 23:20:18 +1100275 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
Damien Miller33804262001-02-04 23:20:18 +1100276 const char *remote_ip = get_remote_ipaddr();
277 const char *remote_host = get_canonical_hostname(
Damien Miller3a961dc2003-06-03 10:25:48 +1000278 options.use_dns);
Damien Miller33804262001-02-04 23:20:18 +1100279 char *patterns = xmalloc(strlen(opts) + 1);
Ben Lindstrom7bb8b492001-03-17 00:47:54 +0000280
Damien Miller33804262001-02-04 23:20:18 +1100281 opts += strlen(cp);
Damien Millerf6d9e222000-06-18 14:50:44 +1000282 i = 0;
Damien Miller33804262001-02-04 23:20:18 +1100283 while (*opts) {
284 if (*opts == '"')
Damien Millerf6d9e222000-06-18 14:50:44 +1000285 break;
Damien Miller33804262001-02-04 23:20:18 +1100286 if (*opts == '\\' && opts[1] == '"') {
287 opts += 2;
Damien Millerf6d9e222000-06-18 14:50:44 +1000288 patterns[i++] = '"';
289 continue;
290 }
Damien Miller33804262001-02-04 23:20:18 +1100291 patterns[i++] = *opts++;
Damien Millerf6d9e222000-06-18 14:50:44 +1000292 }
Damien Miller33804262001-02-04 23:20:18 +1100293 if (!*opts) {
Damien Millerf6d9e222000-06-18 14:50:44 +1000294 debug("%.100s, line %lu: missing end quote",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000295 file, linenum);
Ben Lindstroma574cda2002-05-15 16:16:14 +0000296 auth_debug_add("%.100s, line %lu: missing end quote",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000297 file, linenum);
Darren Tuckera627d422013-06-02 07:31:17 +1000298 free(patterns);
Damien Miller056ddf72001-03-14 10:15:20 +1100299 goto bad_option;
Damien Millerf6d9e222000-06-18 14:50:44 +1000300 }
Damien Miller98299262006-07-24 14:01:43 +1000301 patterns[i] = '\0';
Damien Miller33804262001-02-04 23:20:18 +1100302 opts++;
Darren Tucker896ad5a2008-06-11 09:34:46 +1000303 switch (match_host_and_ip(remote_host, remote_ip,
304 patterns)) {
305 case 1:
Darren Tuckera627d422013-06-02 07:31:17 +1000306 free(patterns);
Darren Tucker896ad5a2008-06-11 09:34:46 +1000307 /* Host name matches. */
308 goto next_option;
309 case -1:
310 debug("%.100s, line %lu: invalid criteria",
311 file, linenum);
312 auth_debug_add("%.100s, line %lu: "
313 "invalid criteria", file, linenum);
314 /* FALLTHROUGH */
315 case 0:
Darren Tuckera627d422013-06-02 07:31:17 +1000316 free(patterns);
Damien Miller996acd22003-04-09 20:59:48 +1000317 logit("Authentication tried for %.100s with "
Damien Miller33804262001-02-04 23:20:18 +1100318 "correct key but not from a permitted "
319 "host (host=%.200s, ip=%.200s).",
320 pw->pw_name, remote_host, remote_ip);
Ben Lindstroma574cda2002-05-15 16:16:14 +0000321 auth_debug_add("Your host '%.200s' is not "
Damien Miller33804262001-02-04 23:20:18 +1100322 "permitted to use this key for login.",
323 remote_host);
Darren Tucker896ad5a2008-06-11 09:34:46 +1000324 break;
Damien Millerf6d9e222000-06-18 14:50:44 +1000325 }
Darren Tucker896ad5a2008-06-11 09:34:46 +1000326 /* deny access */
327 return 0;
Damien Millerf6d9e222000-06-18 14:50:44 +1000328 }
Ben Lindstrom7bb8b492001-03-17 00:47:54 +0000329 cp = "permitopen=\"";
330 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
Damien Millerf91ee4c2005-03-01 21:24:33 +1100331 char *host, *p;
Damien Millere37dde02009-01-28 16:33:01 +1100332 int port;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +0000333 char *patterns = xmalloc(strlen(opts) + 1);
334
335 opts += strlen(cp);
336 i = 0;
337 while (*opts) {
338 if (*opts == '"')
339 break;
340 if (*opts == '\\' && opts[1] == '"') {
341 opts += 2;
342 patterns[i++] = '"';
343 continue;
344 }
345 patterns[i++] = *opts++;
346 }
347 if (!*opts) {
348 debug("%.100s, line %lu: missing end quote",
349 file, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100350 auth_debug_add("%.100s, line %lu: missing "
351 "end quote", file, linenum);
Darren Tuckera627d422013-06-02 07:31:17 +1000352 free(patterns);
Ben Lindstrom7bb8b492001-03-17 00:47:54 +0000353 goto bad_option;
354 }
Damien Miller98299262006-07-24 14:01:43 +1000355 patterns[i] = '\0';
Ben Lindstrom7bb8b492001-03-17 00:47:54 +0000356 opts++;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100357 p = patterns;
Damien Miller7acefbb2014-07-18 14:11:24 +1000358 /* XXX - add streamlocal support */
Damien Millerf91ee4c2005-03-01 21:24:33 +1100359 host = hpdelim(&p);
360 if (host == NULL || strlen(host) >= NI_MAXHOST) {
361 debug("%.100s, line %lu: Bad permitopen "
Darren Tucker90b9e022005-03-14 23:08:50 +1100362 "specification <%.100s>", file, linenum,
Damien Millerf91ee4c2005-03-01 21:24:33 +1100363 patterns);
Ben Lindstroma574cda2002-05-15 16:16:14 +0000364 auth_debug_add("%.100s, line %lu: "
Damien Millerf91ee4c2005-03-01 21:24:33 +1100365 "Bad permitopen specification", file,
366 linenum);
Darren Tuckera627d422013-06-02 07:31:17 +1000367 free(patterns);
Ben Lindstrom7bb8b492001-03-17 00:47:54 +0000368 goto bad_option;
369 }
Darren Tucker47eede72005-03-14 23:08:12 +1100370 host = cleanhostname(host);
Darren Tucker1338b9e2011-10-02 18:57:35 +1100371 if (p == NULL || (port = permitopen_port(p)) < 0) {
Damien Millerf91ee4c2005-03-01 21:24:33 +1100372 debug("%.100s, line %lu: Bad permitopen port "
373 "<%.100s>", file, linenum, p ? p : "");
Ben Lindstroma574cda2002-05-15 16:16:14 +0000374 auth_debug_add("%.100s, line %lu: "
Ben Lindstromd71ba572001-09-12 18:03:31 +0000375 "Bad permitopen port", file, linenum);
Darren Tuckera627d422013-06-02 07:31:17 +1000376 free(patterns);
Ben Lindstrom7bb8b492001-03-17 00:47:54 +0000377 goto bad_option;
378 }
Damien Milleraa5b3f82012-12-03 09:50:54 +1100379 if ((options.allow_tcp_forwarding & FORWARD_LOCAL) != 0)
Ben Lindstromd71ba572001-09-12 18:03:31 +0000380 channel_add_permitted_opens(host, port);
Darren Tuckera627d422013-06-02 07:31:17 +1000381 free(patterns);
Ben Lindstrom7bb8b492001-03-17 00:47:54 +0000382 goto next_option;
383 }
Damien Millerd27b9472005-12-13 19:29:02 +1100384 cp = "tunnel=\"";
385 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
386 char *tun = NULL;
387 opts += strlen(cp);
388 tun = xmalloc(strlen(opts) + 1);
389 i = 0;
390 while (*opts) {
391 if (*opts == '"')
392 break;
393 tun[i++] = *opts++;
394 }
395 if (!*opts) {
396 debug("%.100s, line %lu: missing end quote",
397 file, linenum);
398 auth_debug_add("%.100s, line %lu: missing end quote",
399 file, linenum);
Darren Tuckera627d422013-06-02 07:31:17 +1000400 free(tun);
Damien Millerd27b9472005-12-13 19:29:02 +1100401 forced_tun_device = -1;
402 goto bad_option;
403 }
Damien Miller98299262006-07-24 14:01:43 +1000404 tun[i] = '\0';
Damien Millerd27b9472005-12-13 19:29:02 +1100405 forced_tun_device = a2tun(tun, NULL);
Darren Tuckera627d422013-06-02 07:31:17 +1000406 free(tun);
Damien Miller7b58e802005-12-13 19:33:19 +1100407 if (forced_tun_device == SSH_TUNID_ERR) {
Damien Millerd27b9472005-12-13 19:29:02 +1100408 debug("%.100s, line %lu: invalid tun device",
409 file, linenum);
410 auth_debug_add("%.100s, line %lu: invalid tun device",
411 file, linenum);
412 forced_tun_device = -1;
413 goto bad_option;
414 }
415 auth_debug_add("Forced tun device: %d", forced_tun_device);
416 opts++;
417 goto next_option;
418 }
Damien Millerf6d9e222000-06-18 14:50:44 +1000419next_option:
420 /*
421 * Skip the comma, and move to the next option
422 * (or break out if there are no more).
423 */
Damien Miller33804262001-02-04 23:20:18 +1100424 if (!*opts)
Damien Millerf6d9e222000-06-18 14:50:44 +1000425 fatal("Bugs in auth-options.c option processing.");
Damien Miller33804262001-02-04 23:20:18 +1100426 if (*opts == ' ' || *opts == '\t')
Damien Millerf6d9e222000-06-18 14:50:44 +1000427 break; /* End of options. */
Damien Miller33804262001-02-04 23:20:18 +1100428 if (*opts != ',')
Damien Millerf6d9e222000-06-18 14:50:44 +1000429 goto bad_option;
Damien Miller33804262001-02-04 23:20:18 +1100430 opts++;
Damien Millerf6d9e222000-06-18 14:50:44 +1000431 /* Process the next option. */
432 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000433
Damien Millerf6d9e222000-06-18 14:50:44 +1000434 /* grant access */
435 return 1;
436
437bad_option:
Damien Miller996acd22003-04-09 20:59:48 +1000438 logit("Bad options in %.100s file, line %lu: %.50s",
Damien Miller33804262001-02-04 23:20:18 +1100439 file, linenum, opts);
Ben Lindstroma574cda2002-05-15 16:16:14 +0000440 auth_debug_add("Bad options in %.100s file, line %lu: %.50s",
Damien Miller33804262001-02-04 23:20:18 +1100441 file, linenum, opts);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000442
Damien Millerf6d9e222000-06-18 14:50:44 +1000443 /* deny access */
444 return 0;
445}
Damien Miller0a80ca12010-02-27 07:55:05 +1100446
Damien Millerd0e4a8e2010-05-21 14:58:32 +1000447#define OPTIONS_CRITICAL 1
448#define OPTIONS_EXTENSIONS 2
449static int
markus@openbsd.orgae8b4632015-01-14 10:30:34 +0000450parse_option_list(struct sshbuf *oblob, struct passwd *pw,
Damien Millerd0e4a8e2010-05-21 14:58:32 +1000451 u_int which, int crit,
452 int *cert_no_port_forwarding_flag,
453 int *cert_no_agent_forwarding_flag,
454 int *cert_no_x11_forwarding_flag,
455 int *cert_no_pty_flag,
456 int *cert_no_user_rc,
457 char **cert_forced_command,
458 int *cert_source_address_done)
Damien Miller0a80ca12010-02-27 07:55:05 +1100459{
Damien Millerd0e4a8e2010-05-21 14:58:32 +1000460 char *command, *allowed;
461 const char *remote_ip;
Damien Millerce986542013-07-18 16:12:44 +1000462 char *name = NULL;
markus@openbsd.orgae8b4632015-01-14 10:30:34 +0000463 struct sshbuf *c = NULL, *data = NULL;
464 int r, ret = -1, result, found;
Damien Miller0a80ca12010-02-27 07:55:05 +1100465
markus@openbsd.orgae8b4632015-01-14 10:30:34 +0000466 if ((c = sshbuf_fromb(oblob)) == NULL) {
467 error("%s: sshbuf_fromb failed", __func__);
468 goto out;
469 }
Damien Miller0a80ca12010-02-27 07:55:05 +1100470
markus@openbsd.orgae8b4632015-01-14 10:30:34 +0000471 while (sshbuf_len(c) > 0) {
472 sshbuf_free(data);
473 data = NULL;
474 if ((r = sshbuf_get_cstring(c, &name, NULL)) != 0 ||
475 (r = sshbuf_froms(c, &data)) != 0) {
476 error("Unable to parse certificate options: %s",
477 ssh_err(r));
Damien Miller0a80ca12010-02-27 07:55:05 +1100478 goto out;
479 }
markus@openbsd.orgae8b4632015-01-14 10:30:34 +0000480 debug3("found certificate option \"%.100s\" len %zu",
481 name, sshbuf_len(data));
Damien Millerd0e4a8e2010-05-21 14:58:32 +1000482 found = 0;
483 if ((which & OPTIONS_EXTENSIONS) != 0) {
484 if (strcmp(name, "permit-X11-forwarding") == 0) {
485 *cert_no_x11_forwarding_flag = 0;
486 found = 1;
487 } else if (strcmp(name,
488 "permit-agent-forwarding") == 0) {
489 *cert_no_agent_forwarding_flag = 0;
490 found = 1;
491 } else if (strcmp(name,
492 "permit-port-forwarding") == 0) {
493 *cert_no_port_forwarding_flag = 0;
494 found = 1;
495 } else if (strcmp(name, "permit-pty") == 0) {
496 *cert_no_pty_flag = 0;
497 found = 1;
498 } else if (strcmp(name, "permit-user-rc") == 0) {
499 *cert_no_user_rc = 0;
500 found = 1;
Damien Miller0a80ca12010-02-27 07:55:05 +1100501 }
Damien Millerd0e4a8e2010-05-21 14:58:32 +1000502 }
503 if (!found && (which & OPTIONS_CRITICAL) != 0) {
504 if (strcmp(name, "force-command") == 0) {
markus@openbsd.orgae8b4632015-01-14 10:30:34 +0000505 if ((r = sshbuf_get_cstring(data, &command,
506 NULL)) != 0) {
507 error("Unable to parse \"%s\" "
508 "section: %s", name, ssh_err(r));
Damien Millerd0e4a8e2010-05-21 14:58:32 +1000509 goto out;
510 }
Damien Millerd0e4a8e2010-05-21 14:58:32 +1000511 if (*cert_forced_command != NULL) {
512 error("Certificate has multiple "
513 "force-command options");
Darren Tuckera627d422013-06-02 07:31:17 +1000514 free(command);
Damien Millerd0e4a8e2010-05-21 14:58:32 +1000515 goto out;
516 }
517 *cert_forced_command = command;
518 found = 1;
Damien Miller41396572010-03-04 21:51:11 +1100519 }
Damien Millerd0e4a8e2010-05-21 14:58:32 +1000520 if (strcmp(name, "source-address") == 0) {
markus@openbsd.orgae8b4632015-01-14 10:30:34 +0000521 if ((r = sshbuf_get_cstring(data, &allowed,
522 NULL)) != 0) {
523 error("Unable to parse \"%s\" "
524 "section: %s", name, ssh_err(r));
Damien Millerd0e4a8e2010-05-21 14:58:32 +1000525 goto out;
526 }
Damien Millerd0e4a8e2010-05-21 14:58:32 +1000527 if ((*cert_source_address_done)++) {
528 error("Certificate has multiple "
529 "source-address options");
Darren Tuckera627d422013-06-02 07:31:17 +1000530 free(allowed);
Damien Millerd0e4a8e2010-05-21 14:58:32 +1000531 goto out;
532 }
533 remote_ip = get_remote_ipaddr();
Damien Millerbf25d112013-12-29 17:44:56 +1100534 result = addr_match_cidr_list(remote_ip,
535 allowed);
536 free(allowed);
537 switch (result) {
Damien Millerd0e4a8e2010-05-21 14:58:32 +1000538 case 1:
539 /* accepted */
Damien Millerd0e4a8e2010-05-21 14:58:32 +1000540 break;
541 case 0:
542 /* no match */
543 logit("Authentication tried for %.100s "
544 "with valid certificate but not "
545 "from a permitted host "
546 "(ip=%.200s).", pw->pw_name,
547 remote_ip);
548 auth_debug_add("Your address '%.200s' "
549 "is not permitted to use this "
550 "certificate for login.",
551 remote_ip);
Damien Millerd0e4a8e2010-05-21 14:58:32 +1000552 goto out;
553 case -1:
Damien Millerbf25d112013-12-29 17:44:56 +1100554 default:
Damien Millerd0e4a8e2010-05-21 14:58:32 +1000555 error("Certificate source-address "
556 "contents invalid");
Damien Millerd0e4a8e2010-05-21 14:58:32 +1000557 goto out;
558 }
559 found = 1;
Damien Miller0a80ca12010-02-27 07:55:05 +1100560 }
Damien Miller0a80ca12010-02-27 07:55:05 +1100561 }
562
Damien Millerd0e4a8e2010-05-21 14:58:32 +1000563 if (!found) {
564 if (crit) {
565 error("Certificate critical option \"%s\" "
566 "is not supported", name);
567 goto out;
568 } else {
569 logit("Certificate extension \"%s\" "
570 "is not supported", name);
571 }
markus@openbsd.orgae8b4632015-01-14 10:30:34 +0000572 } else if (sshbuf_len(data) != 0) {
Damien Millerd0e4a8e2010-05-21 14:58:32 +1000573 error("Certificate option \"%s\" corrupt "
Damien Miller0a80ca12010-02-27 07:55:05 +1100574 "(extra data)", name);
575 goto out;
576 }
Darren Tuckera627d422013-06-02 07:31:17 +1000577 free(name);
Damien Millerce986542013-07-18 16:12:44 +1000578 name = NULL;
Damien Miller0a80ca12010-02-27 07:55:05 +1100579 }
Damien Miller4e270b02010-04-16 15:56:21 +1000580 /* successfully parsed all options */
Damien Miller0a80ca12010-02-27 07:55:05 +1100581 ret = 0;
582
Damien Millerd0e4a8e2010-05-21 14:58:32 +1000583 out:
584 if (ret != 0 &&
585 cert_forced_command != NULL &&
586 *cert_forced_command != NULL) {
Darren Tuckera627d422013-06-02 07:31:17 +1000587 free(*cert_forced_command);
Damien Millerd0e4a8e2010-05-21 14:58:32 +1000588 *cert_forced_command = NULL;
589 }
mmcc@openbsd.orgd59ce082015-12-10 17:08:40 +0000590 free(name);
markus@openbsd.orgae8b4632015-01-14 10:30:34 +0000591 sshbuf_free(data);
592 sshbuf_free(c);
Damien Millerd0e4a8e2010-05-21 14:58:32 +1000593 return ret;
594}
595
596/*
597 * Set options from critical certificate options. These supersede user key
598 * options so this must be called after auth_parse_options().
599 */
600int
markus@openbsd.orgae8b4632015-01-14 10:30:34 +0000601auth_cert_options(struct sshkey *k, struct passwd *pw)
Damien Millerd0e4a8e2010-05-21 14:58:32 +1000602{
603 int cert_no_port_forwarding_flag = 1;
604 int cert_no_agent_forwarding_flag = 1;
605 int cert_no_x11_forwarding_flag = 1;
606 int cert_no_pty_flag = 1;
607 int cert_no_user_rc = 1;
608 char *cert_forced_command = NULL;
609 int cert_source_address_done = 0;
610
djm@openbsd.orgc28fc622015-07-03 03:43:18 +0000611 /* Separate options and extensions for v01 certs */
612 if (parse_option_list(k->cert->critical, pw,
613 OPTIONS_CRITICAL, 1, NULL, NULL, NULL, NULL, NULL,
614 &cert_forced_command,
615 &cert_source_address_done) == -1)
616 return -1;
617 if (parse_option_list(k->cert->extensions, pw,
618 OPTIONS_EXTENSIONS, 0,
619 &cert_no_port_forwarding_flag,
620 &cert_no_agent_forwarding_flag,
621 &cert_no_x11_forwarding_flag,
622 &cert_no_pty_flag,
623 &cert_no_user_rc,
624 NULL, NULL) == -1)
625 return -1;
Damien Millerd0e4a8e2010-05-21 14:58:32 +1000626
Damien Miller0a80ca12010-02-27 07:55:05 +1100627 no_port_forwarding_flag |= cert_no_port_forwarding_flag;
628 no_agent_forwarding_flag |= cert_no_agent_forwarding_flag;
629 no_x11_forwarding_flag |= cert_no_x11_forwarding_flag;
630 no_pty_flag |= cert_no_pty_flag;
631 no_user_rc |= cert_no_user_rc;
632 /* CA-specified forced command supersedes key option */
633 if (cert_forced_command != NULL) {
mmcc@openbsd.orgd59ce082015-12-10 17:08:40 +0000634 free(forced_command);
Damien Miller0a80ca12010-02-27 07:55:05 +1100635 forced_command = cert_forced_command;
636 }
Damien Millerd0e4a8e2010-05-21 14:58:32 +1000637 return 0;
Damien Miller0a80ca12010-02-27 07:55:05 +1100638}
639