djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1 | /* $OpenBSD: auth-options.c,v 1.74 2017/09/12 06:32:07 djm Exp $ */ |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 2 | /* |
| 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 Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 6 | * 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 Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 13 | #include "includes.h" |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 14 | |
Damien Miller | 9f2abc4 | 2006-07-10 20:53:08 +1000 | [diff] [blame] | 15 | #include <sys/types.h> |
| 16 | |
Damien Miller | b8fe89c | 2006-07-24 14:51:00 +1000 | [diff] [blame] | 17 | #include <netdb.h> |
Damien Miller | 9f2abc4 | 2006-07-10 20:53:08 +1000 | [diff] [blame] | 18 | #include <pwd.h> |
Damien Miller | e3476ed | 2006-07-24 14:13:33 +1000 | [diff] [blame] | 19 | #include <string.h> |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 20 | #include <stdio.h> |
| 21 | #include <stdarg.h> |
Damien Miller | 9f2abc4 | 2006-07-10 20:53:08 +1000 | [diff] [blame] | 22 | |
Damien Miller | b84886b | 2008-05-19 15:05:07 +1000 | [diff] [blame] | 23 | #include "openbsd-compat/sys-queue.h" |
markus@openbsd.org | ae8b463 | 2015-01-14 10:30:34 +0000 | [diff] [blame] | 24 | |
| 25 | #include "key.h" /* XXX for typedef */ |
| 26 | #include "buffer.h" /* XXX for typedef */ |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 27 | #include "xmalloc.h" |
| 28 | #include "match.h" |
markus@openbsd.org | ae8b463 | 2015-01-14 10:30:34 +0000 | [diff] [blame] | 29 | #include "ssherr.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 30 | #include "log.h" |
| 31 | #include "canohost.h" |
djm@openbsd.org | 9576726 | 2016-03-07 19:02:43 +0000 | [diff] [blame] | 32 | #include "packet.h" |
markus@openbsd.org | ae8b463 | 2015-01-14 10:30:34 +0000 | [diff] [blame] | 33 | #include "sshbuf.h" |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 34 | #include "misc.h" |
Ben Lindstrom | c763767 | 2001-06-09 00:36:26 +0000 | [diff] [blame] | 35 | #include "channels.h" |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 36 | #include "servconf.h" |
markus@openbsd.org | ae8b463 | 2015-01-14 10:30:34 +0000 | [diff] [blame] | 37 | #include "sshkey.h" |
Damien Miller | 4e270b0 | 2010-04-16 15:56:21 +1000 | [diff] [blame] | 38 | #include "auth-options.h" |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 39 | #include "hostfile.h" |
Ben Lindstrom | a574cda | 2002-05-15 16:16:14 +0000 | [diff] [blame] | 40 | #include "auth.h" |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 41 | |
| 42 | /* Flags set authorized_keys flags */ |
| 43 | int no_port_forwarding_flag = 0; |
| 44 | int no_agent_forwarding_flag = 0; |
| 45 | int no_x11_forwarding_flag = 0; |
| 46 | int no_pty_flag = 0; |
Damien Miller | 95e8095 | 2008-03-27 11:03:05 +1100 | [diff] [blame] | 47 | int no_user_rc = 0; |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 48 | int key_is_cert_authority = 0; |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 49 | |
| 50 | /* "command=" option. */ |
| 51 | char *forced_command = NULL; |
| 52 | |
| 53 | /* "environment=" options. */ |
| 54 | struct envstring *custom_environment = NULL; |
| 55 | |
Damien Miller | d27b947 | 2005-12-13 19:29:02 +1100 | [diff] [blame] | 56 | /* "tunnel=" option. */ |
| 57 | int forced_tun_device = -1; |
| 58 | |
Damien Miller | 30da344 | 2010-05-10 11:58:03 +1000 | [diff] [blame] | 59 | /* "principals=" option. */ |
| 60 | char *authorized_principals = NULL; |
| 61 | |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 62 | extern ServerOptions options; |
| 63 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 64 | /* XXX refactor to be stateless */ |
| 65 | |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 66 | void |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 67 | auth_clear_options(void) |
| 68 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 69 | struct ssh *ssh = active_state; /* XXX */ |
| 70 | |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 71 | no_agent_forwarding_flag = 0; |
| 72 | no_port_forwarding_flag = 0; |
| 73 | no_pty_flag = 0; |
| 74 | no_x11_forwarding_flag = 0; |
Damien Miller | 95e8095 | 2008-03-27 11:03:05 +1100 | [diff] [blame] | 75 | no_user_rc = 0; |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 76 | key_is_cert_authority = 0; |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 77 | while (custom_environment) { |
| 78 | struct envstring *ce = custom_environment; |
| 79 | custom_environment = ce->next; |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 80 | free(ce->s); |
| 81 | free(ce); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 82 | } |
mmcc@openbsd.org | d59ce08 | 2015-12-10 17:08:40 +0000 | [diff] [blame] | 83 | free(forced_command); |
| 84 | forced_command = NULL; |
| 85 | free(authorized_principals); |
| 86 | authorized_principals = NULL; |
Damien Miller | d27b947 | 2005-12-13 19:29:02 +1100 | [diff] [blame] | 87 | forced_tun_device = -1; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 88 | channel_clear_permitted_opens(ssh); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 89 | } |
| 90 | |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 91 | /* |
djm@openbsd.org | 383f10f | 2015-11-16 00:30:02 +0000 | [diff] [blame] | 92 | * 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 | */ |
| 99 | static int |
| 100 | match_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 Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 122 | * return 1 if access is granted, 0 if not. |
| 123 | * side effect: sets key option flags |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 124 | * XXX remove side effects; fill structure instead. |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 125 | */ |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 126 | int |
markus@openbsd.org | 75b8af8 | 2017-05-31 10:54:00 +0000 | [diff] [blame] | 127 | auth_parse_options(struct passwd *pw, char *opts, const char *file, |
| 128 | u_long linenum) |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 129 | { |
djm@openbsd.org | 9576726 | 2016-03-07 19:02:43 +0000 | [diff] [blame] | 130 | struct ssh *ssh = active_state; /* XXX */ |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 131 | const char *cp; |
djm@openbsd.org | 383f10f | 2015-11-16 00:30:02 +0000 | [diff] [blame] | 132 | int i, r; |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 133 | |
| 134 | /* reset options */ |
| 135 | auth_clear_options(); |
| 136 | |
Ben Lindstrom | 36d7bd0 | 2001-02-10 22:27:19 +0000 | [diff] [blame] | 137 | if (!opts) |
| 138 | return 1; |
| 139 | |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 140 | while (*opts && *opts != ' ' && *opts != '\t') { |
djm@openbsd.org | 383f10f | 2015-11-16 00:30:02 +0000 | [diff] [blame] | 141 | if ((r = match_flag("cert-authority", 0, &opts, NULL)) != -1) { |
| 142 | key_is_cert_authority = r; |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 143 | goto next_option; |
| 144 | } |
djm@openbsd.org | 383f10f | 2015-11-16 00:30:02 +0000 | [diff] [blame] | 145 | if ((r = match_flag("restrict", 0, &opts, NULL)) != -1) { |
| 146 | auth_debug_add("Key is restricted."); |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 147 | no_port_forwarding_flag = 1; |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 148 | no_agent_forwarding_flag = 1; |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 149 | no_x11_forwarding_flag = 1; |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 150 | no_pty_flag = 1; |
djm@openbsd.org | 383f10f | 2015-11-16 00:30:02 +0000 | [diff] [blame] | 151 | no_user_rc = 1; |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 152 | goto next_option; |
| 153 | } |
djm@openbsd.org | 383f10f | 2015-11-16 00:30:02 +0000 | [diff] [blame] | 154 | 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 Miller | 95e8095 | 2008-03-27 11:03:05 +1100 | [diff] [blame] | 177 | goto next_option; |
| 178 | } |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 179 | cp = "command=\""; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 180 | if (strncasecmp(opts, cp, strlen(cp)) == 0) { |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 181 | opts += strlen(cp); |
mmcc@openbsd.org | d59ce08 | 2015-12-10 17:08:40 +0000 | [diff] [blame] | 182 | free(forced_command); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 183 | forced_command = xmalloc(strlen(opts) + 1); |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 184 | i = 0; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 185 | while (*opts) { |
| 186 | if (*opts == '"') |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 187 | break; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 188 | if (*opts == '\\' && opts[1] == '"') { |
| 189 | opts += 2; |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 190 | forced_command[i++] = '"'; |
| 191 | continue; |
| 192 | } |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 193 | forced_command[i++] = *opts++; |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 194 | } |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 195 | if (!*opts) { |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 196 | debug("%.100s, line %lu: missing end quote", |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 197 | file, linenum); |
Ben Lindstrom | a574cda | 2002-05-15 16:16:14 +0000 | [diff] [blame] | 198 | auth_debug_add("%.100s, line %lu: missing end quote", |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 199 | file, linenum); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 200 | free(forced_command); |
Damien Miller | 056ddf7 | 2001-03-14 10:15:20 +1100 | [diff] [blame] | 201 | forced_command = NULL; |
| 202 | goto bad_option; |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 203 | } |
Damien Miller | 9829926 | 2006-07-24 14:01:43 +1000 | [diff] [blame] | 204 | forced_command[i] = '\0'; |
Damien Miller | de53fd0 | 2011-01-06 22:44:18 +1100 | [diff] [blame] | 205 | auth_debug_add("Forced command."); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 206 | opts++; |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 207 | goto next_option; |
| 208 | } |
Damien Miller | 30da344 | 2010-05-10 11:58:03 +1000 | [diff] [blame] | 209 | cp = "principals=\""; |
| 210 | if (strncasecmp(opts, cp, strlen(cp)) == 0) { |
| 211 | opts += strlen(cp); |
mmcc@openbsd.org | d59ce08 | 2015-12-10 17:08:40 +0000 | [diff] [blame] | 212 | free(authorized_principals); |
Damien Miller | 30da344 | 2010-05-10 11:58:03 +1000 | [diff] [blame] | 213 | 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 Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 230 | free(authorized_principals); |
Damien Miller | 30da344 | 2010-05-10 11:58:03 +1000 | [diff] [blame] | 231 | 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 Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 240 | cp = "environment=\""; |
djm@openbsd.org | a42d67b | 2015-05-01 03:20:54 +0000 | [diff] [blame] | 241 | if (strncasecmp(opts, cp, strlen(cp)) == 0) { |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 242 | char *s; |
| 243 | struct envstring *new_envstring; |
Ben Lindstrom | 7bb8b49 | 2001-03-17 00:47:54 +0000 | [diff] [blame] | 244 | |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 245 | opts += strlen(cp); |
| 246 | s = xmalloc(strlen(opts) + 1); |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 247 | i = 0; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 248 | while (*opts) { |
| 249 | if (*opts == '"') |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 250 | break; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 251 | if (*opts == '\\' && opts[1] == '"') { |
| 252 | opts += 2; |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 253 | s[i++] = '"'; |
| 254 | continue; |
| 255 | } |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 256 | s[i++] = *opts++; |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 257 | } |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 258 | if (!*opts) { |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 259 | debug("%.100s, line %lu: missing end quote", |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 260 | file, linenum); |
Ben Lindstrom | a574cda | 2002-05-15 16:16:14 +0000 | [diff] [blame] | 261 | auth_debug_add("%.100s, line %lu: missing end quote", |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 262 | file, linenum); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 263 | free(s); |
Damien Miller | 056ddf7 | 2001-03-14 10:15:20 +1100 | [diff] [blame] | 264 | goto bad_option; |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 265 | } |
Damien Miller | 9829926 | 2006-07-24 14:01:43 +1000 | [diff] [blame] | 266 | s[i] = '\0'; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 267 | opts++; |
djm@openbsd.org | a42d67b | 2015-05-01 03:20:54 +0000 | [diff] [blame] | 268 | 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 Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 280 | goto next_option; |
| 281 | } |
| 282 | cp = "from=\""; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 283 | if (strncasecmp(opts, cp, strlen(cp)) == 0) { |
djm@openbsd.org | 9576726 | 2016-03-07 19:02:43 +0000 | [diff] [blame] | 284 | const char *remote_ip = ssh_remote_ipaddr(ssh); |
| 285 | const char *remote_host = auth_get_canonical_hostname( |
| 286 | ssh, options.use_dns); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 287 | char *patterns = xmalloc(strlen(opts) + 1); |
Ben Lindstrom | 7bb8b49 | 2001-03-17 00:47:54 +0000 | [diff] [blame] | 288 | |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 289 | opts += strlen(cp); |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 290 | i = 0; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 291 | while (*opts) { |
| 292 | if (*opts == '"') |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 293 | break; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 294 | if (*opts == '\\' && opts[1] == '"') { |
| 295 | opts += 2; |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 296 | patterns[i++] = '"'; |
| 297 | continue; |
| 298 | } |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 299 | patterns[i++] = *opts++; |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 300 | } |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 301 | if (!*opts) { |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 302 | debug("%.100s, line %lu: missing end quote", |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 303 | file, linenum); |
Ben Lindstrom | a574cda | 2002-05-15 16:16:14 +0000 | [diff] [blame] | 304 | auth_debug_add("%.100s, line %lu: missing end quote", |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 305 | file, linenum); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 306 | free(patterns); |
Damien Miller | 056ddf7 | 2001-03-14 10:15:20 +1100 | [diff] [blame] | 307 | goto bad_option; |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 308 | } |
Damien Miller | 9829926 | 2006-07-24 14:01:43 +1000 | [diff] [blame] | 309 | patterns[i] = '\0'; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 310 | opts++; |
Darren Tucker | 896ad5a | 2008-06-11 09:34:46 +1000 | [diff] [blame] | 311 | switch (match_host_and_ip(remote_host, remote_ip, |
| 312 | patterns)) { |
| 313 | case 1: |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 314 | free(patterns); |
Darren Tucker | 896ad5a | 2008-06-11 09:34:46 +1000 | [diff] [blame] | 315 | /* 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 Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 324 | free(patterns); |
Damien Miller | 996acd2 | 2003-04-09 20:59:48 +1000 | [diff] [blame] | 325 | logit("Authentication tried for %.100s with " |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 326 | "correct key but not from a permitted " |
| 327 | "host (host=%.200s, ip=%.200s).", |
| 328 | pw->pw_name, remote_host, remote_ip); |
Ben Lindstrom | a574cda | 2002-05-15 16:16:14 +0000 | [diff] [blame] | 329 | auth_debug_add("Your host '%.200s' is not " |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 330 | "permitted to use this key for login.", |
| 331 | remote_host); |
Darren Tucker | 896ad5a | 2008-06-11 09:34:46 +1000 | [diff] [blame] | 332 | break; |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 333 | } |
Darren Tucker | 896ad5a | 2008-06-11 09:34:46 +1000 | [diff] [blame] | 334 | /* deny access */ |
| 335 | return 0; |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 336 | } |
Ben Lindstrom | 7bb8b49 | 2001-03-17 00:47:54 +0000 | [diff] [blame] | 337 | cp = "permitopen=\""; |
| 338 | if (strncasecmp(opts, cp, strlen(cp)) == 0) { |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 339 | char *host, *p; |
Damien Miller | e37dde0 | 2009-01-28 16:33:01 +1100 | [diff] [blame] | 340 | int port; |
Ben Lindstrom | 7bb8b49 | 2001-03-17 00:47:54 +0000 | [diff] [blame] | 341 | 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 Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 358 | auth_debug_add("%.100s, line %lu: missing " |
| 359 | "end quote", file, linenum); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 360 | free(patterns); |
Ben Lindstrom | 7bb8b49 | 2001-03-17 00:47:54 +0000 | [diff] [blame] | 361 | goto bad_option; |
| 362 | } |
Damien Miller | 9829926 | 2006-07-24 14:01:43 +1000 | [diff] [blame] | 363 | patterns[i] = '\0'; |
Ben Lindstrom | 7bb8b49 | 2001-03-17 00:47:54 +0000 | [diff] [blame] | 364 | opts++; |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 365 | p = patterns; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 366 | /* XXX - add streamlocal support */ |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 367 | host = hpdelim(&p); |
| 368 | if (host == NULL || strlen(host) >= NI_MAXHOST) { |
| 369 | debug("%.100s, line %lu: Bad permitopen " |
Darren Tucker | 90b9e02 | 2005-03-14 23:08:50 +1100 | [diff] [blame] | 370 | "specification <%.100s>", file, linenum, |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 371 | patterns); |
Ben Lindstrom | a574cda | 2002-05-15 16:16:14 +0000 | [diff] [blame] | 372 | auth_debug_add("%.100s, line %lu: " |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 373 | "Bad permitopen specification", file, |
| 374 | linenum); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 375 | free(patterns); |
Ben Lindstrom | 7bb8b49 | 2001-03-17 00:47:54 +0000 | [diff] [blame] | 376 | goto bad_option; |
| 377 | } |
Darren Tucker | 47eede7 | 2005-03-14 23:08:12 +1100 | [diff] [blame] | 378 | host = cleanhostname(host); |
Darren Tucker | 1338b9e | 2011-10-02 18:57:35 +1100 | [diff] [blame] | 379 | if (p == NULL || (port = permitopen_port(p)) < 0) { |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 380 | debug("%.100s, line %lu: Bad permitopen port " |
| 381 | "<%.100s>", file, linenum, p ? p : ""); |
Ben Lindstrom | a574cda | 2002-05-15 16:16:14 +0000 | [diff] [blame] | 382 | auth_debug_add("%.100s, line %lu: " |
Ben Lindstrom | d71ba57 | 2001-09-12 18:03:31 +0000 | [diff] [blame] | 383 | "Bad permitopen port", file, linenum); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 384 | free(patterns); |
Ben Lindstrom | 7bb8b49 | 2001-03-17 00:47:54 +0000 | [diff] [blame] | 385 | goto bad_option; |
| 386 | } |
Damien Miller | aa5b3f8 | 2012-12-03 09:50:54 +1100 | [diff] [blame] | 387 | if ((options.allow_tcp_forwarding & FORWARD_LOCAL) != 0) |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 388 | channel_add_permitted_opens(ssh, host, port); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 389 | free(patterns); |
Ben Lindstrom | 7bb8b49 | 2001-03-17 00:47:54 +0000 | [diff] [blame] | 390 | goto next_option; |
| 391 | } |
Damien Miller | d27b947 | 2005-12-13 19:29:02 +1100 | [diff] [blame] | 392 | 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 Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 408 | free(tun); |
Damien Miller | d27b947 | 2005-12-13 19:29:02 +1100 | [diff] [blame] | 409 | forced_tun_device = -1; |
| 410 | goto bad_option; |
| 411 | } |
Damien Miller | 9829926 | 2006-07-24 14:01:43 +1000 | [diff] [blame] | 412 | tun[i] = '\0'; |
Damien Miller | d27b947 | 2005-12-13 19:29:02 +1100 | [diff] [blame] | 413 | forced_tun_device = a2tun(tun, NULL); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 414 | free(tun); |
Damien Miller | 7b58e80 | 2005-12-13 19:33:19 +1100 | [diff] [blame] | 415 | if (forced_tun_device == SSH_TUNID_ERR) { |
Damien Miller | d27b947 | 2005-12-13 19:29:02 +1100 | [diff] [blame] | 416 | 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 Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 427 | next_option: |
| 428 | /* |
| 429 | * Skip the comma, and move to the next option |
| 430 | * (or break out if there are no more). |
| 431 | */ |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 432 | if (!*opts) |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 433 | fatal("Bugs in auth-options.c option processing."); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 434 | if (*opts == ' ' || *opts == '\t') |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 435 | break; /* End of options. */ |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 436 | if (*opts != ',') |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 437 | goto bad_option; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 438 | opts++; |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 439 | /* Process the next option. */ |
| 440 | } |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 441 | |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 442 | /* grant access */ |
| 443 | return 1; |
| 444 | |
| 445 | bad_option: |
Damien Miller | 996acd2 | 2003-04-09 20:59:48 +1000 | [diff] [blame] | 446 | logit("Bad options in %.100s file, line %lu: %.50s", |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 447 | file, linenum, opts); |
Ben Lindstrom | a574cda | 2002-05-15 16:16:14 +0000 | [diff] [blame] | 448 | auth_debug_add("Bad options in %.100s file, line %lu: %.50s", |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 449 | file, linenum, opts); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 450 | |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 451 | /* deny access */ |
| 452 | return 0; |
| 453 | } |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 454 | |
Damien Miller | d0e4a8e | 2010-05-21 14:58:32 +1000 | [diff] [blame] | 455 | #define OPTIONS_CRITICAL 1 |
| 456 | #define OPTIONS_EXTENSIONS 2 |
| 457 | static int |
markus@openbsd.org | ae8b463 | 2015-01-14 10:30:34 +0000 | [diff] [blame] | 458 | parse_option_list(struct sshbuf *oblob, struct passwd *pw, |
Damien Miller | d0e4a8e | 2010-05-21 14:58:32 +1000 | [diff] [blame] | 459 | 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 Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 467 | { |
djm@openbsd.org | 9576726 | 2016-03-07 19:02:43 +0000 | [diff] [blame] | 468 | struct ssh *ssh = active_state; /* XXX */ |
Damien Miller | d0e4a8e | 2010-05-21 14:58:32 +1000 | [diff] [blame] | 469 | char *command, *allowed; |
| 470 | const char *remote_ip; |
Damien Miller | ce98654 | 2013-07-18 16:12:44 +1000 | [diff] [blame] | 471 | char *name = NULL; |
markus@openbsd.org | ae8b463 | 2015-01-14 10:30:34 +0000 | [diff] [blame] | 472 | struct sshbuf *c = NULL, *data = NULL; |
| 473 | int r, ret = -1, result, found; |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 474 | |
markus@openbsd.org | ae8b463 | 2015-01-14 10:30:34 +0000 | [diff] [blame] | 475 | if ((c = sshbuf_fromb(oblob)) == NULL) { |
| 476 | error("%s: sshbuf_fromb failed", __func__); |
| 477 | goto out; |
| 478 | } |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 479 | |
markus@openbsd.org | ae8b463 | 2015-01-14 10:30:34 +0000 | [diff] [blame] | 480 | 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 Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 487 | goto out; |
| 488 | } |
markus@openbsd.org | ae8b463 | 2015-01-14 10:30:34 +0000 | [diff] [blame] | 489 | debug3("found certificate option \"%.100s\" len %zu", |
| 490 | name, sshbuf_len(data)); |
Damien Miller | d0e4a8e | 2010-05-21 14:58:32 +1000 | [diff] [blame] | 491 | 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 Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 510 | } |
Damien Miller | d0e4a8e | 2010-05-21 14:58:32 +1000 | [diff] [blame] | 511 | } |
| 512 | if (!found && (which & OPTIONS_CRITICAL) != 0) { |
| 513 | if (strcmp(name, "force-command") == 0) { |
markus@openbsd.org | ae8b463 | 2015-01-14 10:30:34 +0000 | [diff] [blame] | 514 | if ((r = sshbuf_get_cstring(data, &command, |
| 515 | NULL)) != 0) { |
| 516 | error("Unable to parse \"%s\" " |
| 517 | "section: %s", name, ssh_err(r)); |
Damien Miller | d0e4a8e | 2010-05-21 14:58:32 +1000 | [diff] [blame] | 518 | goto out; |
| 519 | } |
Damien Miller | d0e4a8e | 2010-05-21 14:58:32 +1000 | [diff] [blame] | 520 | if (*cert_forced_command != NULL) { |
| 521 | error("Certificate has multiple " |
| 522 | "force-command options"); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 523 | free(command); |
Damien Miller | d0e4a8e | 2010-05-21 14:58:32 +1000 | [diff] [blame] | 524 | goto out; |
| 525 | } |
| 526 | *cert_forced_command = command; |
| 527 | found = 1; |
Damien Miller | 4139657 | 2010-03-04 21:51:11 +1100 | [diff] [blame] | 528 | } |
Damien Miller | d0e4a8e | 2010-05-21 14:58:32 +1000 | [diff] [blame] | 529 | if (strcmp(name, "source-address") == 0) { |
markus@openbsd.org | ae8b463 | 2015-01-14 10:30:34 +0000 | [diff] [blame] | 530 | if ((r = sshbuf_get_cstring(data, &allowed, |
| 531 | NULL)) != 0) { |
| 532 | error("Unable to parse \"%s\" " |
| 533 | "section: %s", name, ssh_err(r)); |
Damien Miller | d0e4a8e | 2010-05-21 14:58:32 +1000 | [diff] [blame] | 534 | goto out; |
| 535 | } |
Damien Miller | d0e4a8e | 2010-05-21 14:58:32 +1000 | [diff] [blame] | 536 | if ((*cert_source_address_done)++) { |
| 537 | error("Certificate has multiple " |
| 538 | "source-address options"); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 539 | free(allowed); |
Damien Miller | d0e4a8e | 2010-05-21 14:58:32 +1000 | [diff] [blame] | 540 | goto out; |
| 541 | } |
djm@openbsd.org | 9576726 | 2016-03-07 19:02:43 +0000 | [diff] [blame] | 542 | remote_ip = ssh_remote_ipaddr(ssh); |
Damien Miller | bf25d11 | 2013-12-29 17:44:56 +1100 | [diff] [blame] | 543 | result = addr_match_cidr_list(remote_ip, |
| 544 | allowed); |
| 545 | free(allowed); |
| 546 | switch (result) { |
Damien Miller | d0e4a8e | 2010-05-21 14:58:32 +1000 | [diff] [blame] | 547 | case 1: |
| 548 | /* accepted */ |
Damien Miller | d0e4a8e | 2010-05-21 14:58:32 +1000 | [diff] [blame] | 549 | 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 Miller | d0e4a8e | 2010-05-21 14:58:32 +1000 | [diff] [blame] | 561 | goto out; |
| 562 | case -1: |
Damien Miller | bf25d11 | 2013-12-29 17:44:56 +1100 | [diff] [blame] | 563 | default: |
Damien Miller | d0e4a8e | 2010-05-21 14:58:32 +1000 | [diff] [blame] | 564 | error("Certificate source-address " |
| 565 | "contents invalid"); |
Damien Miller | d0e4a8e | 2010-05-21 14:58:32 +1000 | [diff] [blame] | 566 | goto out; |
| 567 | } |
| 568 | found = 1; |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 569 | } |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 570 | } |
| 571 | |
Damien Miller | d0e4a8e | 2010-05-21 14:58:32 +1000 | [diff] [blame] | 572 | 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.org | ae8b463 | 2015-01-14 10:30:34 +0000 | [diff] [blame] | 581 | } else if (sshbuf_len(data) != 0) { |
Damien Miller | d0e4a8e | 2010-05-21 14:58:32 +1000 | [diff] [blame] | 582 | error("Certificate option \"%s\" corrupt " |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 583 | "(extra data)", name); |
| 584 | goto out; |
| 585 | } |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 586 | free(name); |
Damien Miller | ce98654 | 2013-07-18 16:12:44 +1000 | [diff] [blame] | 587 | name = NULL; |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 588 | } |
Damien Miller | 4e270b0 | 2010-04-16 15:56:21 +1000 | [diff] [blame] | 589 | /* successfully parsed all options */ |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 590 | ret = 0; |
| 591 | |
Damien Miller | d0e4a8e | 2010-05-21 14:58:32 +1000 | [diff] [blame] | 592 | out: |
| 593 | if (ret != 0 && |
| 594 | cert_forced_command != NULL && |
| 595 | *cert_forced_command != NULL) { |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 596 | free(*cert_forced_command); |
Damien Miller | d0e4a8e | 2010-05-21 14:58:32 +1000 | [diff] [blame] | 597 | *cert_forced_command = NULL; |
| 598 | } |
mmcc@openbsd.org | d59ce08 | 2015-12-10 17:08:40 +0000 | [diff] [blame] | 599 | free(name); |
markus@openbsd.org | ae8b463 | 2015-01-14 10:30:34 +0000 | [diff] [blame] | 600 | sshbuf_free(data); |
| 601 | sshbuf_free(c); |
Damien Miller | d0e4a8e | 2010-05-21 14:58:32 +1000 | [diff] [blame] | 602 | 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 | */ |
| 609 | int |
djm@openbsd.org | fd6dcef | 2016-11-30 02:57:40 +0000 | [diff] [blame] | 610 | auth_cert_options(struct sshkey *k, struct passwd *pw, const char **reason) |
Damien Miller | d0e4a8e | 2010-05-21 14:58:32 +1000 | [diff] [blame] | 611 | { |
| 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.org | fd6dcef | 2016-11-30 02:57:40 +0000 | [diff] [blame] | 620 | *reason = "invalid certificate options"; |
| 621 | |
djm@openbsd.org | c28fc62 | 2015-07-03 03:43:18 +0000 | [diff] [blame] | 622 | /* 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 Miller | d0e4a8e | 2010-05-21 14:58:32 +1000 | [diff] [blame] | 637 | |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 638 | 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.org | fd6dcef | 2016-11-30 02:57:40 +0000 | [diff] [blame] | 643 | /* |
| 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 Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 658 | forced_command = cert_forced_command; |
djm@openbsd.org | fd6dcef | 2016-11-30 02:57:40 +0000 | [diff] [blame] | 659 | /* success */ |
| 660 | *reason = NULL; |
Damien Miller | d0e4a8e | 2010-05-21 14:58:32 +1000 | [diff] [blame] | 661 | return 0; |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 662 | } |
| 663 | |