Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 1 | /* |
| 2 | * Author: Tatu Ylonen <ylo@cs.hut.fi> |
| 3 | * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland |
| 4 | * All rights reserved |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 5 | * As far as I am concerned, the code I have written for this software |
| 6 | * can be used freely for any purpose. Any derived versions of this |
| 7 | * software must be clearly marked as such, and if the derived work is |
| 8 | * incompatible with the protocol description in the RFC file, it must be |
| 9 | * called by a name other than "ssh" or "Secure Shell". |
| 10 | */ |
| 11 | |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 12 | #include "includes.h" |
Ben Lindstrom | c763767 | 2001-06-09 00:36:26 +0000 | [diff] [blame] | 13 | RCSID("$OpenBSD: auth-options.c,v 1.18 2001/05/31 10:30:12 markus Exp $"); |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 14 | |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 15 | #include "packet.h" |
| 16 | #include "xmalloc.h" |
| 17 | #include "match.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 18 | #include "log.h" |
| 19 | #include "canohost.h" |
Ben Lindstrom | c763767 | 2001-06-09 00:36:26 +0000 | [diff] [blame] | 20 | #include "channels.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 21 | #include "auth-options.h" |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 22 | #include "servconf.h" |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 23 | |
| 24 | /* Flags set authorized_keys flags */ |
| 25 | int no_port_forwarding_flag = 0; |
| 26 | int no_agent_forwarding_flag = 0; |
| 27 | int no_x11_forwarding_flag = 0; |
| 28 | int no_pty_flag = 0; |
| 29 | |
| 30 | /* "command=" option. */ |
| 31 | char *forced_command = NULL; |
| 32 | |
| 33 | /* "environment=" options. */ |
| 34 | struct envstring *custom_environment = NULL; |
| 35 | |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 36 | extern ServerOptions options; |
| 37 | |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 38 | void |
| 39 | auth_clear_options(void) |
| 40 | { |
| 41 | no_agent_forwarding_flag = 0; |
| 42 | no_port_forwarding_flag = 0; |
| 43 | no_pty_flag = 0; |
| 44 | no_x11_forwarding_flag = 0; |
| 45 | while (custom_environment) { |
| 46 | struct envstring *ce = custom_environment; |
| 47 | custom_environment = ce->next; |
| 48 | xfree(ce->s); |
| 49 | xfree(ce); |
| 50 | } |
| 51 | if (forced_command) { |
| 52 | xfree(forced_command); |
| 53 | forced_command = NULL; |
| 54 | } |
Ben Lindstrom | 7bb8b49 | 2001-03-17 00:47:54 +0000 | [diff] [blame] | 55 | channel_clear_permitted_opens(); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 56 | } |
| 57 | |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 58 | /* |
| 59 | * return 1 if access is granted, 0 if not. |
| 60 | * side effect: sets key option flags |
| 61 | */ |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 62 | int |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 63 | 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] | 64 | { |
| 65 | const char *cp; |
Ben Lindstrom | 7bb8b49 | 2001-03-17 00:47:54 +0000 | [diff] [blame] | 66 | int i; |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 67 | |
| 68 | /* reset options */ |
| 69 | auth_clear_options(); |
| 70 | |
Ben Lindstrom | 36d7bd0 | 2001-02-10 22:27:19 +0000 | [diff] [blame] | 71 | if (!opts) |
| 72 | return 1; |
| 73 | |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 74 | while (*opts && *opts != ' ' && *opts != '\t') { |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 75 | cp = "no-port-forwarding"; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 76 | if (strncasecmp(opts, cp, strlen(cp)) == 0) { |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 77 | packet_send_debug("Port forwarding disabled."); |
| 78 | no_port_forwarding_flag = 1; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 79 | opts += strlen(cp); |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 80 | goto next_option; |
| 81 | } |
| 82 | cp = "no-agent-forwarding"; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 83 | if (strncasecmp(opts, cp, strlen(cp)) == 0) { |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 84 | packet_send_debug("Agent forwarding disabled."); |
| 85 | no_agent_forwarding_flag = 1; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 86 | opts += strlen(cp); |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 87 | goto next_option; |
| 88 | } |
| 89 | cp = "no-X11-forwarding"; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 90 | if (strncasecmp(opts, cp, strlen(cp)) == 0) { |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 91 | packet_send_debug("X11 forwarding disabled."); |
| 92 | no_x11_forwarding_flag = 1; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 93 | opts += strlen(cp); |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 94 | goto next_option; |
| 95 | } |
| 96 | cp = "no-pty"; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 97 | if (strncasecmp(opts, cp, strlen(cp)) == 0) { |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 98 | packet_send_debug("Pty allocation disabled."); |
| 99 | no_pty_flag = 1; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 100 | opts += strlen(cp); |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 101 | goto next_option; |
| 102 | } |
| 103 | cp = "command=\""; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 104 | if (strncasecmp(opts, cp, strlen(cp)) == 0) { |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 105 | opts += strlen(cp); |
| 106 | forced_command = xmalloc(strlen(opts) + 1); |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 107 | i = 0; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 108 | while (*opts) { |
| 109 | if (*opts == '"') |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 110 | break; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 111 | if (*opts == '\\' && opts[1] == '"') { |
| 112 | opts += 2; |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 113 | forced_command[i++] = '"'; |
| 114 | continue; |
| 115 | } |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 116 | forced_command[i++] = *opts++; |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 117 | } |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 118 | if (!*opts) { |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 119 | debug("%.100s, line %lu: missing end quote", |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 120 | file, linenum); |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 121 | packet_send_debug("%.100s, line %lu: missing end quote", |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 122 | file, linenum); |
Damien Miller | 056ddf7 | 2001-03-14 10:15:20 +1100 | [diff] [blame] | 123 | xfree(forced_command); |
| 124 | forced_command = NULL; |
| 125 | goto bad_option; |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 126 | } |
| 127 | forced_command[i] = 0; |
| 128 | packet_send_debug("Forced command: %.900s", forced_command); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 129 | opts++; |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 130 | goto next_option; |
| 131 | } |
| 132 | cp = "environment=\""; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 133 | if (strncasecmp(opts, cp, strlen(cp)) == 0) { |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 134 | char *s; |
| 135 | struct envstring *new_envstring; |
Ben Lindstrom | 7bb8b49 | 2001-03-17 00:47:54 +0000 | [diff] [blame] | 136 | |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 137 | opts += strlen(cp); |
| 138 | s = xmalloc(strlen(opts) + 1); |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 139 | i = 0; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 140 | while (*opts) { |
| 141 | if (*opts == '"') |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 142 | break; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 143 | if (*opts == '\\' && opts[1] == '"') { |
| 144 | opts += 2; |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 145 | s[i++] = '"'; |
| 146 | continue; |
| 147 | } |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 148 | s[i++] = *opts++; |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 149 | } |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 150 | if (!*opts) { |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 151 | debug("%.100s, line %lu: missing end quote", |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 152 | file, linenum); |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 153 | packet_send_debug("%.100s, line %lu: missing end quote", |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 154 | file, linenum); |
Damien Miller | 056ddf7 | 2001-03-14 10:15:20 +1100 | [diff] [blame] | 155 | xfree(s); |
| 156 | goto bad_option; |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 157 | } |
| 158 | s[i] = 0; |
| 159 | packet_send_debug("Adding to environment: %.900s", s); |
| 160 | debug("Adding to environment: %.900s", s); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 161 | opts++; |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 162 | new_envstring = xmalloc(sizeof(struct envstring)); |
| 163 | new_envstring->s = s; |
| 164 | new_envstring->next = custom_environment; |
| 165 | custom_environment = new_envstring; |
| 166 | goto next_option; |
| 167 | } |
| 168 | cp = "from=\""; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 169 | if (strncasecmp(opts, cp, strlen(cp)) == 0) { |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 170 | int mname, mip; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 171 | const char *remote_ip = get_remote_ipaddr(); |
| 172 | const char *remote_host = get_canonical_hostname( |
| 173 | options.reverse_mapping_check); |
| 174 | char *patterns = xmalloc(strlen(opts) + 1); |
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); |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 177 | i = 0; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 178 | while (*opts) { |
| 179 | if (*opts == '"') |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 180 | break; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 181 | if (*opts == '\\' && opts[1] == '"') { |
| 182 | opts += 2; |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 183 | patterns[i++] = '"'; |
| 184 | continue; |
| 185 | } |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 186 | patterns[i++] = *opts++; |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 187 | } |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 188 | if (!*opts) { |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 189 | debug("%.100s, line %lu: missing end quote", |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 190 | file, linenum); |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 191 | packet_send_debug("%.100s, line %lu: missing end quote", |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 192 | file, linenum); |
Damien Miller | 056ddf7 | 2001-03-14 10:15:20 +1100 | [diff] [blame] | 193 | xfree(patterns); |
| 194 | goto bad_option; |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 195 | } |
| 196 | patterns[i] = 0; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 197 | opts++; |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 198 | /* |
| 199 | * Deny access if we get a negative |
| 200 | * match for the hostname or the ip |
| 201 | * or if we get not match at all |
| 202 | */ |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 203 | mname = match_hostname(remote_host, patterns, |
| 204 | strlen(patterns)); |
| 205 | mip = match_hostname(remote_ip, patterns, |
| 206 | strlen(patterns)); |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 207 | xfree(patterns); |
| 208 | if (mname == -1 || mip == -1 || |
| 209 | (mname != 1 && mip != 1)) { |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 210 | log("Authentication tried for %.100s with " |
| 211 | "correct key but not from a permitted " |
| 212 | "host (host=%.200s, ip=%.200s).", |
| 213 | pw->pw_name, remote_host, remote_ip); |
| 214 | packet_send_debug("Your host '%.200s' is not " |
| 215 | "permitted to use this key for login.", |
| 216 | remote_host); |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 217 | /* deny access */ |
| 218 | return 0; |
| 219 | } |
| 220 | /* Host name matches. */ |
| 221 | goto next_option; |
| 222 | } |
Ben Lindstrom | 7bb8b49 | 2001-03-17 00:47:54 +0000 | [diff] [blame] | 223 | cp = "permitopen=\""; |
| 224 | if (strncasecmp(opts, cp, strlen(cp)) == 0) { |
| 225 | u_short port; |
| 226 | char *c, *ep; |
| 227 | char *patterns = xmalloc(strlen(opts) + 1); |
| 228 | |
| 229 | opts += strlen(cp); |
| 230 | i = 0; |
| 231 | while (*opts) { |
| 232 | if (*opts == '"') |
| 233 | break; |
| 234 | if (*opts == '\\' && opts[1] == '"') { |
| 235 | opts += 2; |
| 236 | patterns[i++] = '"'; |
| 237 | continue; |
| 238 | } |
| 239 | patterns[i++] = *opts++; |
| 240 | } |
| 241 | if (!*opts) { |
| 242 | debug("%.100s, line %lu: missing end quote", |
| 243 | file, linenum); |
| 244 | packet_send_debug("%.100s, line %lu: missing end quote", |
| 245 | file, linenum); |
| 246 | xfree(patterns); |
| 247 | goto bad_option; |
| 248 | } |
| 249 | patterns[i] = 0; |
| 250 | opts++; |
| 251 | c = strchr(patterns, ':'); |
| 252 | if (c == NULL) { |
| 253 | debug("%.100s, line %lu: permitopen: missing colon <%.100s>", |
| 254 | file, linenum, patterns); |
| 255 | packet_send_debug("%.100s, line %lu: missing colon", |
| 256 | file, linenum); |
| 257 | xfree(patterns); |
| 258 | goto bad_option; |
| 259 | } |
| 260 | *c = 0; |
| 261 | c++; |
| 262 | port = strtol(c, &ep, 0); |
| 263 | if (c == ep) { |
| 264 | debug("%.100s, line %lu: permitopen: missing port <%.100s>", |
| 265 | file, linenum, patterns); |
| 266 | packet_send_debug("%.100s, line %lu: missing port", |
| 267 | file, linenum); |
| 268 | xfree(patterns); |
| 269 | goto bad_option; |
| 270 | } |
Ben Lindstrom | 2d70f98 | 2001-03-19 00:13:46 +0000 | [diff] [blame] | 271 | if (options.allow_tcp_forwarding) |
| 272 | channel_add_permitted_opens(patterns, port); |
Ben Lindstrom | 7bb8b49 | 2001-03-17 00:47:54 +0000 | [diff] [blame] | 273 | xfree(patterns); |
| 274 | goto next_option; |
| 275 | } |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 276 | next_option: |
| 277 | /* |
| 278 | * Skip the comma, and move to the next option |
| 279 | * (or break out if there are no more). |
| 280 | */ |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 281 | if (!*opts) |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 282 | fatal("Bugs in auth-options.c option processing."); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 283 | if (*opts == ' ' || *opts == '\t') |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 284 | break; /* End of options. */ |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 285 | if (*opts != ',') |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 286 | goto bad_option; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 287 | opts++; |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 288 | /* Process the next option. */ |
| 289 | } |
| 290 | /* grant access */ |
| 291 | return 1; |
| 292 | |
| 293 | bad_option: |
| 294 | log("Bad options in %.100s file, line %lu: %.50s", |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 295 | file, linenum, opts); |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 296 | packet_send_debug("Bad options in %.100s file, line %lu: %.50s", |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 297 | file, linenum, opts); |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 298 | /* deny access */ |
| 299 | return 0; |
| 300 | } |