Damien Miller | 4e270b0 | 2010-04-16 15:56:21 +1000 | [diff] [blame] | 1 | /* $OpenBSD: auth-options.c,v 1.50 2010/04/16 01:47:26 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" |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 24 | #include "xmalloc.h" |
| 25 | #include "match.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 26 | #include "log.h" |
| 27 | #include "canohost.h" |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 28 | #include "buffer.h" |
Ben Lindstrom | c763767 | 2001-06-09 00:36:26 +0000 | [diff] [blame] | 29 | #include "channels.h" |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 30 | #include "servconf.h" |
Ben Lindstrom | d71ba57 | 2001-09-12 18:03:31 +0000 | [diff] [blame] | 31 | #include "misc.h" |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 32 | #include "key.h" |
Damien Miller | 4e270b0 | 2010-04-16 15:56:21 +1000 | [diff] [blame] | 33 | #include "auth-options.h" |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 34 | #include "hostfile.h" |
Ben Lindstrom | a574cda | 2002-05-15 16:16:14 +0000 | [diff] [blame] | 35 | #include "auth.h" |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 36 | #ifdef GSSAPI |
| 37 | #include "ssh-gss.h" |
| 38 | #endif |
| 39 | #include "monitor_wrap.h" |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 40 | |
| 41 | /* Flags set authorized_keys flags */ |
| 42 | int no_port_forwarding_flag = 0; |
| 43 | int no_agent_forwarding_flag = 0; |
| 44 | int no_x11_forwarding_flag = 0; |
| 45 | int no_pty_flag = 0; |
Damien Miller | 95e8095 | 2008-03-27 11:03:05 +1100 | [diff] [blame] | 46 | int no_user_rc = 0; |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 47 | int key_is_cert_authority = 0; |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 48 | |
| 49 | /* "command=" option. */ |
| 50 | char *forced_command = NULL; |
| 51 | |
| 52 | /* "environment=" options. */ |
| 53 | struct envstring *custom_environment = NULL; |
| 54 | |
Damien Miller | d27b947 | 2005-12-13 19:29:02 +1100 | [diff] [blame] | 55 | /* "tunnel=" option. */ |
| 56 | int forced_tun_device = -1; |
| 57 | |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 58 | extern ServerOptions options; |
| 59 | |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 60 | void |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 61 | auth_clear_options(void) |
| 62 | { |
| 63 | no_agent_forwarding_flag = 0; |
| 64 | no_port_forwarding_flag = 0; |
| 65 | no_pty_flag = 0; |
| 66 | no_x11_forwarding_flag = 0; |
Damien Miller | 95e8095 | 2008-03-27 11:03:05 +1100 | [diff] [blame] | 67 | no_user_rc = 0; |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 68 | key_is_cert_authority = 0; |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 69 | while (custom_environment) { |
| 70 | struct envstring *ce = custom_environment; |
| 71 | custom_environment = ce->next; |
| 72 | xfree(ce->s); |
| 73 | xfree(ce); |
| 74 | } |
| 75 | if (forced_command) { |
| 76 | xfree(forced_command); |
| 77 | forced_command = NULL; |
| 78 | } |
Damien Miller | d27b947 | 2005-12-13 19:29:02 +1100 | [diff] [blame] | 79 | forced_tun_device = -1; |
Ben Lindstrom | 7bb8b49 | 2001-03-17 00:47:54 +0000 | [diff] [blame] | 80 | channel_clear_permitted_opens(); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 81 | } |
| 82 | |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 83 | /* |
| 84 | * return 1 if access is granted, 0 if not. |
| 85 | * side effect: sets key option flags |
| 86 | */ |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 87 | int |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 88 | auth_parse_options(struct passwd *pw, char *opts, char *file, u_long linenum) |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 89 | { |
| 90 | const char *cp; |
Ben Lindstrom | 7bb8b49 | 2001-03-17 00:47:54 +0000 | [diff] [blame] | 91 | int i; |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 92 | |
| 93 | /* reset options */ |
| 94 | auth_clear_options(); |
| 95 | |
Ben Lindstrom | 36d7bd0 | 2001-02-10 22:27:19 +0000 | [diff] [blame] | 96 | if (!opts) |
| 97 | return 1; |
| 98 | |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 99 | while (*opts && *opts != ' ' && *opts != '\t') { |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 100 | cp = "cert-authority"; |
| 101 | if (strncasecmp(opts, cp, strlen(cp)) == 0) { |
| 102 | key_is_cert_authority = 1; |
| 103 | opts += strlen(cp); |
| 104 | goto next_option; |
| 105 | } |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 106 | cp = "no-port-forwarding"; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 107 | if (strncasecmp(opts, cp, strlen(cp)) == 0) { |
Ben Lindstrom | a574cda | 2002-05-15 16:16:14 +0000 | [diff] [blame] | 108 | auth_debug_add("Port forwarding disabled."); |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 109 | no_port_forwarding_flag = 1; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 110 | opts += strlen(cp); |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 111 | goto next_option; |
| 112 | } |
| 113 | cp = "no-agent-forwarding"; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 114 | if (strncasecmp(opts, cp, strlen(cp)) == 0) { |
Ben Lindstrom | a574cda | 2002-05-15 16:16:14 +0000 | [diff] [blame] | 115 | auth_debug_add("Agent forwarding disabled."); |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 116 | no_agent_forwarding_flag = 1; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 117 | opts += strlen(cp); |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 118 | goto next_option; |
| 119 | } |
| 120 | cp = "no-X11-forwarding"; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 121 | if (strncasecmp(opts, cp, strlen(cp)) == 0) { |
Ben Lindstrom | a574cda | 2002-05-15 16:16:14 +0000 | [diff] [blame] | 122 | auth_debug_add("X11 forwarding disabled."); |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 123 | no_x11_forwarding_flag = 1; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 124 | opts += strlen(cp); |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 125 | goto next_option; |
| 126 | } |
| 127 | cp = "no-pty"; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 128 | if (strncasecmp(opts, cp, strlen(cp)) == 0) { |
Ben Lindstrom | a574cda | 2002-05-15 16:16:14 +0000 | [diff] [blame] | 129 | auth_debug_add("Pty allocation disabled."); |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 130 | no_pty_flag = 1; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 131 | opts += strlen(cp); |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 132 | goto next_option; |
| 133 | } |
Damien Miller | 95e8095 | 2008-03-27 11:03:05 +1100 | [diff] [blame] | 134 | cp = "no-user-rc"; |
| 135 | if (strncasecmp(opts, cp, strlen(cp)) == 0) { |
| 136 | auth_debug_add("User rc file execution disabled."); |
| 137 | no_user_rc = 1; |
| 138 | opts += strlen(cp); |
| 139 | goto next_option; |
| 140 | } |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 141 | cp = "command=\""; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 142 | if (strncasecmp(opts, cp, strlen(cp)) == 0) { |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 143 | opts += strlen(cp); |
| 144 | forced_command = xmalloc(strlen(opts) + 1); |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 145 | i = 0; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 146 | while (*opts) { |
| 147 | if (*opts == '"') |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 148 | break; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 149 | if (*opts == '\\' && opts[1] == '"') { |
| 150 | opts += 2; |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 151 | forced_command[i++] = '"'; |
| 152 | continue; |
| 153 | } |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 154 | forced_command[i++] = *opts++; |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 155 | } |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 156 | if (!*opts) { |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 157 | debug("%.100s, line %lu: missing end quote", |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 158 | file, linenum); |
Ben Lindstrom | a574cda | 2002-05-15 16:16:14 +0000 | [diff] [blame] | 159 | auth_debug_add("%.100s, line %lu: missing end quote", |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 160 | file, linenum); |
Damien Miller | 056ddf7 | 2001-03-14 10:15:20 +1100 | [diff] [blame] | 161 | xfree(forced_command); |
| 162 | forced_command = NULL; |
| 163 | goto bad_option; |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 164 | } |
Damien Miller | 9829926 | 2006-07-24 14:01:43 +1000 | [diff] [blame] | 165 | forced_command[i] = '\0'; |
Ben Lindstrom | a574cda | 2002-05-15 16:16:14 +0000 | [diff] [blame] | 166 | auth_debug_add("Forced command: %.900s", forced_command); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 167 | opts++; |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 168 | goto next_option; |
| 169 | } |
| 170 | cp = "environment=\""; |
Ben Lindstrom | 5d860f0 | 2002-08-01 01:28:38 +0000 | [diff] [blame] | 171 | if (options.permit_user_env && |
| 172 | strncasecmp(opts, cp, strlen(cp)) == 0) { |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 173 | char *s; |
| 174 | struct envstring *new_envstring; |
Ben Lindstrom | 7bb8b49 | 2001-03-17 00:47:54 +0000 | [diff] [blame] | 175 | |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 176 | opts += strlen(cp); |
| 177 | s = xmalloc(strlen(opts) + 1); |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 178 | i = 0; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 179 | while (*opts) { |
| 180 | if (*opts == '"') |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 181 | break; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 182 | if (*opts == '\\' && opts[1] == '"') { |
| 183 | opts += 2; |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 184 | s[i++] = '"'; |
| 185 | continue; |
| 186 | } |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 187 | s[i++] = *opts++; |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 188 | } |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 189 | if (!*opts) { |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 190 | debug("%.100s, line %lu: missing end quote", |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 191 | file, linenum); |
Ben Lindstrom | a574cda | 2002-05-15 16:16:14 +0000 | [diff] [blame] | 192 | auth_debug_add("%.100s, line %lu: missing end quote", |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 193 | file, linenum); |
Damien Miller | 056ddf7 | 2001-03-14 10:15:20 +1100 | [diff] [blame] | 194 | xfree(s); |
| 195 | goto bad_option; |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 196 | } |
Damien Miller | 9829926 | 2006-07-24 14:01:43 +1000 | [diff] [blame] | 197 | s[i] = '\0'; |
Ben Lindstrom | a574cda | 2002-05-15 16:16:14 +0000 | [diff] [blame] | 198 | auth_debug_add("Adding to environment: %.900s", s); |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 199 | debug("Adding to environment: %.900s", s); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 200 | opts++; |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 201 | new_envstring = xmalloc(sizeof(struct envstring)); |
| 202 | new_envstring->s = s; |
| 203 | new_envstring->next = custom_environment; |
| 204 | custom_environment = new_envstring; |
| 205 | goto next_option; |
| 206 | } |
| 207 | cp = "from=\""; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 208 | if (strncasecmp(opts, cp, strlen(cp)) == 0) { |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 209 | const char *remote_ip = get_remote_ipaddr(); |
| 210 | const char *remote_host = get_canonical_hostname( |
Damien Miller | 3a961dc | 2003-06-03 10:25:48 +1000 | [diff] [blame] | 211 | options.use_dns); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 212 | char *patterns = xmalloc(strlen(opts) + 1); |
Ben Lindstrom | 7bb8b49 | 2001-03-17 00:47:54 +0000 | [diff] [blame] | 213 | |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 214 | opts += strlen(cp); |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 215 | i = 0; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 216 | while (*opts) { |
| 217 | if (*opts == '"') |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 218 | break; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 219 | if (*opts == '\\' && opts[1] == '"') { |
| 220 | opts += 2; |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 221 | patterns[i++] = '"'; |
| 222 | continue; |
| 223 | } |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 224 | patterns[i++] = *opts++; |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 225 | } |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 226 | if (!*opts) { |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 227 | debug("%.100s, line %lu: missing end quote", |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 228 | file, linenum); |
Ben Lindstrom | a574cda | 2002-05-15 16:16:14 +0000 | [diff] [blame] | 229 | auth_debug_add("%.100s, line %lu: missing end quote", |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 230 | file, linenum); |
Damien Miller | 056ddf7 | 2001-03-14 10:15:20 +1100 | [diff] [blame] | 231 | xfree(patterns); |
| 232 | goto bad_option; |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 233 | } |
Damien Miller | 9829926 | 2006-07-24 14:01:43 +1000 | [diff] [blame] | 234 | patterns[i] = '\0'; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 235 | opts++; |
Darren Tucker | 896ad5a | 2008-06-11 09:34:46 +1000 | [diff] [blame] | 236 | switch (match_host_and_ip(remote_host, remote_ip, |
| 237 | patterns)) { |
| 238 | case 1: |
| 239 | xfree(patterns); |
| 240 | /* Host name matches. */ |
| 241 | goto next_option; |
| 242 | case -1: |
| 243 | debug("%.100s, line %lu: invalid criteria", |
| 244 | file, linenum); |
| 245 | auth_debug_add("%.100s, line %lu: " |
| 246 | "invalid criteria", file, linenum); |
| 247 | /* FALLTHROUGH */ |
| 248 | case 0: |
Ben Lindstrom | f0c5029 | 2001-06-25 05:17:53 +0000 | [diff] [blame] | 249 | xfree(patterns); |
Damien Miller | 996acd2 | 2003-04-09 20:59:48 +1000 | [diff] [blame] | 250 | logit("Authentication tried for %.100s with " |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 251 | "correct key but not from a permitted " |
| 252 | "host (host=%.200s, ip=%.200s).", |
| 253 | pw->pw_name, remote_host, remote_ip); |
Ben Lindstrom | a574cda | 2002-05-15 16:16:14 +0000 | [diff] [blame] | 254 | auth_debug_add("Your host '%.200s' is not " |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 255 | "permitted to use this key for login.", |
| 256 | remote_host); |
Darren Tucker | 896ad5a | 2008-06-11 09:34:46 +1000 | [diff] [blame] | 257 | break; |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 258 | } |
Darren Tucker | 896ad5a | 2008-06-11 09:34:46 +1000 | [diff] [blame] | 259 | /* deny access */ |
| 260 | return 0; |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 261 | } |
Ben Lindstrom | 7bb8b49 | 2001-03-17 00:47:54 +0000 | [diff] [blame] | 262 | cp = "permitopen=\""; |
| 263 | if (strncasecmp(opts, cp, strlen(cp)) == 0) { |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 264 | char *host, *p; |
Damien Miller | e37dde0 | 2009-01-28 16:33:01 +1100 | [diff] [blame] | 265 | int port; |
Ben Lindstrom | 7bb8b49 | 2001-03-17 00:47:54 +0000 | [diff] [blame] | 266 | char *patterns = xmalloc(strlen(opts) + 1); |
| 267 | |
| 268 | opts += strlen(cp); |
| 269 | i = 0; |
| 270 | while (*opts) { |
| 271 | if (*opts == '"') |
| 272 | break; |
| 273 | if (*opts == '\\' && opts[1] == '"') { |
| 274 | opts += 2; |
| 275 | patterns[i++] = '"'; |
| 276 | continue; |
| 277 | } |
| 278 | patterns[i++] = *opts++; |
| 279 | } |
| 280 | if (!*opts) { |
| 281 | debug("%.100s, line %lu: missing end quote", |
| 282 | file, linenum); |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 283 | auth_debug_add("%.100s, line %lu: missing " |
| 284 | "end quote", file, linenum); |
Ben Lindstrom | 7bb8b49 | 2001-03-17 00:47:54 +0000 | [diff] [blame] | 285 | xfree(patterns); |
| 286 | goto bad_option; |
| 287 | } |
Damien Miller | 9829926 | 2006-07-24 14:01:43 +1000 | [diff] [blame] | 288 | patterns[i] = '\0'; |
Ben Lindstrom | 7bb8b49 | 2001-03-17 00:47:54 +0000 | [diff] [blame] | 289 | opts++; |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 290 | p = patterns; |
| 291 | host = hpdelim(&p); |
| 292 | if (host == NULL || strlen(host) >= NI_MAXHOST) { |
| 293 | debug("%.100s, line %lu: Bad permitopen " |
Darren Tucker | 90b9e02 | 2005-03-14 23:08:50 +1100 | [diff] [blame] | 294 | "specification <%.100s>", file, linenum, |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 295 | patterns); |
Ben Lindstrom | a574cda | 2002-05-15 16:16:14 +0000 | [diff] [blame] | 296 | auth_debug_add("%.100s, line %lu: " |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 297 | "Bad permitopen specification", file, |
| 298 | linenum); |
Ben Lindstrom | 7bb8b49 | 2001-03-17 00:47:54 +0000 | [diff] [blame] | 299 | xfree(patterns); |
| 300 | goto bad_option; |
| 301 | } |
Darren Tucker | 47eede7 | 2005-03-14 23:08:12 +1100 | [diff] [blame] | 302 | host = cleanhostname(host); |
Damien Miller | e37dde0 | 2009-01-28 16:33:01 +1100 | [diff] [blame] | 303 | if (p == NULL || (port = a2port(p)) <= 0) { |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 304 | debug("%.100s, line %lu: Bad permitopen port " |
| 305 | "<%.100s>", file, linenum, p ? p : ""); |
Ben Lindstrom | a574cda | 2002-05-15 16:16:14 +0000 | [diff] [blame] | 306 | auth_debug_add("%.100s, line %lu: " |
Ben Lindstrom | d71ba57 | 2001-09-12 18:03:31 +0000 | [diff] [blame] | 307 | "Bad permitopen port", file, linenum); |
Ben Lindstrom | 7bb8b49 | 2001-03-17 00:47:54 +0000 | [diff] [blame] | 308 | xfree(patterns); |
| 309 | goto bad_option; |
| 310 | } |
Ben Lindstrom | 2d70f98 | 2001-03-19 00:13:46 +0000 | [diff] [blame] | 311 | if (options.allow_tcp_forwarding) |
Ben Lindstrom | d71ba57 | 2001-09-12 18:03:31 +0000 | [diff] [blame] | 312 | channel_add_permitted_opens(host, port); |
Ben Lindstrom | 7bb8b49 | 2001-03-17 00:47:54 +0000 | [diff] [blame] | 313 | xfree(patterns); |
| 314 | goto next_option; |
| 315 | } |
Damien Miller | d27b947 | 2005-12-13 19:29:02 +1100 | [diff] [blame] | 316 | cp = "tunnel=\""; |
| 317 | if (strncasecmp(opts, cp, strlen(cp)) == 0) { |
| 318 | char *tun = NULL; |
| 319 | opts += strlen(cp); |
| 320 | tun = xmalloc(strlen(opts) + 1); |
| 321 | i = 0; |
| 322 | while (*opts) { |
| 323 | if (*opts == '"') |
| 324 | break; |
| 325 | tun[i++] = *opts++; |
| 326 | } |
| 327 | if (!*opts) { |
| 328 | debug("%.100s, line %lu: missing end quote", |
| 329 | file, linenum); |
| 330 | auth_debug_add("%.100s, line %lu: missing end quote", |
| 331 | file, linenum); |
| 332 | xfree(tun); |
| 333 | forced_tun_device = -1; |
| 334 | goto bad_option; |
| 335 | } |
Damien Miller | 9829926 | 2006-07-24 14:01:43 +1000 | [diff] [blame] | 336 | tun[i] = '\0'; |
Damien Miller | d27b947 | 2005-12-13 19:29:02 +1100 | [diff] [blame] | 337 | forced_tun_device = a2tun(tun, NULL); |
| 338 | xfree(tun); |
Damien Miller | 7b58e80 | 2005-12-13 19:33:19 +1100 | [diff] [blame] | 339 | if (forced_tun_device == SSH_TUNID_ERR) { |
Damien Miller | d27b947 | 2005-12-13 19:29:02 +1100 | [diff] [blame] | 340 | debug("%.100s, line %lu: invalid tun device", |
| 341 | file, linenum); |
| 342 | auth_debug_add("%.100s, line %lu: invalid tun device", |
| 343 | file, linenum); |
| 344 | forced_tun_device = -1; |
| 345 | goto bad_option; |
| 346 | } |
| 347 | auth_debug_add("Forced tun device: %d", forced_tun_device); |
| 348 | opts++; |
| 349 | goto next_option; |
| 350 | } |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 351 | next_option: |
| 352 | /* |
| 353 | * Skip the comma, and move to the next option |
| 354 | * (or break out if there are no more). |
| 355 | */ |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 356 | if (!*opts) |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 357 | fatal("Bugs in auth-options.c option processing."); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 358 | if (*opts == ' ' || *opts == '\t') |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 359 | break; /* End of options. */ |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 360 | if (*opts != ',') |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 361 | goto bad_option; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 362 | opts++; |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 363 | /* Process the next option. */ |
| 364 | } |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 365 | |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 366 | /* grant access */ |
| 367 | return 1; |
| 368 | |
| 369 | bad_option: |
Damien Miller | 996acd2 | 2003-04-09 20:59:48 +1000 | [diff] [blame] | 370 | logit("Bad options in %.100s file, line %lu: %.50s", |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 371 | file, linenum, opts); |
Ben Lindstrom | a574cda | 2002-05-15 16:16:14 +0000 | [diff] [blame] | 372 | auth_debug_add("Bad options in %.100s file, line %lu: %.50s", |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 373 | file, linenum, opts); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 374 | |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 375 | /* deny access */ |
| 376 | return 0; |
| 377 | } |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 378 | |
| 379 | /* |
Damien Miller | 4e270b0 | 2010-04-16 15:56:21 +1000 | [diff] [blame] | 380 | * Set options from critical certificate options. These supersede user key |
| 381 | * options so this must be called after auth_parse_options(). |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 382 | */ |
| 383 | int |
Damien Miller | 4e270b0 | 2010-04-16 15:56:21 +1000 | [diff] [blame] | 384 | auth_cert_options(Key *k, struct passwd *pw) |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 385 | { |
| 386 | u_char *name = NULL, *data_blob = NULL; |
Damien Miller | 4139657 | 2010-03-04 21:51:11 +1100 | [diff] [blame] | 387 | u_int nlen, dlen, clen; |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 388 | Buffer c, data; |
| 389 | int ret = -1; |
| 390 | |
| 391 | int cert_no_port_forwarding_flag = 1; |
| 392 | int cert_no_agent_forwarding_flag = 1; |
| 393 | int cert_no_x11_forwarding_flag = 1; |
| 394 | int cert_no_pty_flag = 1; |
| 395 | int cert_no_user_rc = 1; |
| 396 | char *cert_forced_command = NULL; |
| 397 | int cert_source_address_done = 0; |
| 398 | |
| 399 | buffer_init(&data); |
| 400 | |
| 401 | /* Make copy to avoid altering original */ |
| 402 | buffer_init(&c); |
Damien Miller | 4e270b0 | 2010-04-16 15:56:21 +1000 | [diff] [blame] | 403 | buffer_append(&c, |
| 404 | buffer_ptr(&k->cert->critical), buffer_len(&k->cert->critical)); |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 405 | |
| 406 | while (buffer_len(&c) > 0) { |
Damien Miller | 4139657 | 2010-03-04 21:51:11 +1100 | [diff] [blame] | 407 | if ((name = buffer_get_string_ret(&c, &nlen)) == NULL || |
| 408 | (data_blob = buffer_get_string_ret(&c, &dlen)) == NULL) { |
Damien Miller | 4e270b0 | 2010-04-16 15:56:21 +1000 | [diff] [blame] | 409 | error("Certificate options corrupt"); |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 410 | goto out; |
| 411 | } |
Damien Miller | 4139657 | 2010-03-04 21:51:11 +1100 | [diff] [blame] | 412 | buffer_append(&data, data_blob, dlen); |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 413 | debug3("found certificate constraint \"%.100s\" len %u", |
Damien Miller | 4139657 | 2010-03-04 21:51:11 +1100 | [diff] [blame] | 414 | name, dlen); |
| 415 | if (strlen(name) != nlen) { |
| 416 | error("Certificate constraint name contains \\0"); |
| 417 | goto out; |
| 418 | } |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 419 | if (strcmp(name, "permit-X11-forwarding") == 0) |
| 420 | cert_no_x11_forwarding_flag = 0; |
| 421 | else if (strcmp(name, "permit-agent-forwarding") == 0) |
| 422 | cert_no_agent_forwarding_flag = 0; |
| 423 | else if (strcmp(name, "permit-port-forwarding") == 0) |
| 424 | cert_no_port_forwarding_flag = 0; |
| 425 | else if (strcmp(name, "permit-pty") == 0) |
| 426 | cert_no_pty_flag = 0; |
| 427 | else if (strcmp(name, "permit-user-rc") == 0) |
| 428 | cert_no_user_rc = 0; |
| 429 | else if (strcmp(name, "force-command") == 0) { |
Damien Miller | 4139657 | 2010-03-04 21:51:11 +1100 | [diff] [blame] | 430 | char *command = buffer_get_string_ret(&data, &clen); |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 431 | |
| 432 | if (command == NULL) { |
| 433 | error("Certificate constraint \"%s\" corrupt", |
| 434 | name); |
| 435 | goto out; |
| 436 | } |
Damien Miller | 4139657 | 2010-03-04 21:51:11 +1100 | [diff] [blame] | 437 | if (strlen(command) != clen) { |
Damien Miller | 33334b2 | 2010-03-22 05:59:02 +1100 | [diff] [blame] | 438 | error("force-command constraint contains \\0"); |
Damien Miller | 4139657 | 2010-03-04 21:51:11 +1100 | [diff] [blame] | 439 | goto out; |
| 440 | } |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 441 | if (cert_forced_command != NULL) { |
| 442 | error("Certificate has multiple " |
Damien Miller | 4e270b0 | 2010-04-16 15:56:21 +1000 | [diff] [blame] | 443 | "force-command options"); |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 444 | xfree(command); |
| 445 | goto out; |
| 446 | } |
| 447 | cert_forced_command = command; |
| 448 | } else if (strcmp(name, "source-address") == 0) { |
Damien Miller | 4139657 | 2010-03-04 21:51:11 +1100 | [diff] [blame] | 449 | char *allowed = buffer_get_string_ret(&data, &clen); |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 450 | const char *remote_ip = get_remote_ipaddr(); |
| 451 | |
| 452 | if (allowed == NULL) { |
| 453 | error("Certificate constraint \"%s\" corrupt", |
| 454 | name); |
| 455 | goto out; |
| 456 | } |
Damien Miller | 4139657 | 2010-03-04 21:51:11 +1100 | [diff] [blame] | 457 | if (strlen(allowed) != clen) { |
Damien Miller | 33334b2 | 2010-03-22 05:59:02 +1100 | [diff] [blame] | 458 | error("source-address constraint contains \\0"); |
Damien Miller | 4139657 | 2010-03-04 21:51:11 +1100 | [diff] [blame] | 459 | goto out; |
| 460 | } |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 461 | if (cert_source_address_done++) { |
| 462 | error("Certificate has multiple " |
Damien Miller | 4e270b0 | 2010-04-16 15:56:21 +1000 | [diff] [blame] | 463 | "source-address options"); |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 464 | xfree(allowed); |
| 465 | goto out; |
| 466 | } |
| 467 | switch (addr_match_cidr_list(remote_ip, allowed)) { |
| 468 | case 1: |
| 469 | /* accepted */ |
| 470 | xfree(allowed); |
| 471 | break; |
| 472 | case 0: |
| 473 | /* no match */ |
| 474 | logit("Authentication tried for %.100s with " |
| 475 | "valid certificate but not from a " |
| 476 | "permitted host (ip=%.200s).", |
| 477 | pw->pw_name, remote_ip); |
| 478 | auth_debug_add("Your address '%.200s' is not " |
| 479 | "permitted to use this certificate for " |
| 480 | "login.", remote_ip); |
| 481 | xfree(allowed); |
| 482 | goto out; |
| 483 | case -1: |
| 484 | error("Certificate source-address contents " |
| 485 | "invalid"); |
| 486 | xfree(allowed); |
| 487 | goto out; |
| 488 | } |
| 489 | } else { |
| 490 | error("Certificate constraint \"%s\" is not supported", |
| 491 | name); |
| 492 | goto out; |
| 493 | } |
| 494 | |
| 495 | if (buffer_len(&data) != 0) { |
| 496 | error("Certificate constraint \"%s\" corrupt " |
| 497 | "(extra data)", name); |
| 498 | goto out; |
| 499 | } |
| 500 | buffer_clear(&data); |
| 501 | xfree(name); |
| 502 | xfree(data_blob); |
| 503 | name = data_blob = NULL; |
| 504 | } |
| 505 | |
Damien Miller | 4e270b0 | 2010-04-16 15:56:21 +1000 | [diff] [blame] | 506 | /* successfully parsed all options */ |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 507 | ret = 0; |
| 508 | |
| 509 | no_port_forwarding_flag |= cert_no_port_forwarding_flag; |
| 510 | no_agent_forwarding_flag |= cert_no_agent_forwarding_flag; |
| 511 | no_x11_forwarding_flag |= cert_no_x11_forwarding_flag; |
| 512 | no_pty_flag |= cert_no_pty_flag; |
| 513 | no_user_rc |= cert_no_user_rc; |
| 514 | /* CA-specified forced command supersedes key option */ |
| 515 | if (cert_forced_command != NULL) { |
| 516 | if (forced_command != NULL) |
| 517 | xfree(forced_command); |
| 518 | forced_command = cert_forced_command; |
| 519 | } |
| 520 | |
| 521 | out: |
| 522 | if (name != NULL) |
| 523 | xfree(name); |
| 524 | if (data_blob != NULL) |
| 525 | xfree(data_blob); |
| 526 | buffer_free(&data); |
| 527 | buffer_free(&c); |
| 528 | return ret; |
| 529 | } |
| 530 | |