]>
Commit | Line | Data |
---|---|---|
00146caa | 1 | /* $OpenBSD: auth-options.c,v 1.39 2006/07/22 20:48:22 stevesk Exp $ */ |
bcbf86ec | 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 | |
bcbf86ec | 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 | ||
38c295d6 | 13 | #include "includes.h" |
38c295d6 | 14 | |
b1842393 | 15 | #include <sys/types.h> |
16 | ||
28cb0a43 | 17 | #include <netdb.h> |
b1842393 | 18 | #include <pwd.h> |
00146caa | 19 | #include <string.h> |
b1842393 | 20 | |
38c295d6 | 21 | #include "xmalloc.h" |
22 | #include "match.h" | |
42f11eb2 | 23 | #include "log.h" |
24 | #include "canohost.h" | |
a5efa1bb | 25 | #include "channels.h" |
42f11eb2 | 26 | #include "auth-options.h" |
61e96248 | 27 | #include "servconf.h" |
ed787d14 | 28 | #include "misc.h" |
1853d1ef | 29 | #include "monitor_wrap.h" |
01dafcb5 | 30 | #include "auth.h" |
38c295d6 | 31 | |
32 | /* Flags set authorized_keys flags */ | |
33 | int no_port_forwarding_flag = 0; | |
34 | int no_agent_forwarding_flag = 0; | |
35 | int no_x11_forwarding_flag = 0; | |
36 | int no_pty_flag = 0; | |
37 | ||
38 | /* "command=" option. */ | |
39 | char *forced_command = NULL; | |
40 | ||
41 | /* "environment=" options. */ | |
42 | struct envstring *custom_environment = NULL; | |
43 | ||
d20f3c9e | 44 | /* "tunnel=" option. */ |
45 | int forced_tun_device = -1; | |
46 | ||
61e96248 | 47 | extern ServerOptions options; |
48 | ||
94ec8c6b | 49 | void |
50 | auth_clear_options(void) | |
51 | { | |
52 | no_agent_forwarding_flag = 0; | |
53 | no_port_forwarding_flag = 0; | |
54 | no_pty_flag = 0; | |
55 | no_x11_forwarding_flag = 0; | |
56 | while (custom_environment) { | |
57 | struct envstring *ce = custom_environment; | |
58 | custom_environment = ce->next; | |
59 | xfree(ce->s); | |
60 | xfree(ce); | |
61 | } | |
62 | if (forced_command) { | |
63 | xfree(forced_command); | |
64 | forced_command = NULL; | |
65 | } | |
d20f3c9e | 66 | forced_tun_device = -1; |
01794848 | 67 | channel_clear_permitted_opens(); |
01dafcb5 | 68 | auth_debug_reset(); |
94ec8c6b | 69 | } |
70 | ||
42f11eb2 | 71 | /* |
72 | * return 1 if access is granted, 0 if not. | |
73 | * side effect: sets key option flags | |
74 | */ | |
38c295d6 | 75 | int |
61e96248 | 76 | auth_parse_options(struct passwd *pw, char *opts, char *file, u_long linenum) |
38c295d6 | 77 | { |
78 | const char *cp; | |
01794848 | 79 | int i; |
94ec8c6b | 80 | |
81 | /* reset options */ | |
82 | auth_clear_options(); | |
83 | ||
e0b2cf6b | 84 | if (!opts) |
85 | return 1; | |
86 | ||
61e96248 | 87 | while (*opts && *opts != ' ' && *opts != '\t') { |
38c295d6 | 88 | cp = "no-port-forwarding"; |
61e96248 | 89 | if (strncasecmp(opts, cp, strlen(cp)) == 0) { |
01dafcb5 | 90 | auth_debug_add("Port forwarding disabled."); |
38c295d6 | 91 | no_port_forwarding_flag = 1; |
61e96248 | 92 | opts += strlen(cp); |
38c295d6 | 93 | goto next_option; |
94 | } | |
95 | cp = "no-agent-forwarding"; | |
61e96248 | 96 | if (strncasecmp(opts, cp, strlen(cp)) == 0) { |
01dafcb5 | 97 | auth_debug_add("Agent forwarding disabled."); |
38c295d6 | 98 | no_agent_forwarding_flag = 1; |
61e96248 | 99 | opts += strlen(cp); |
38c295d6 | 100 | goto next_option; |
101 | } | |
102 | cp = "no-X11-forwarding"; | |
61e96248 | 103 | if (strncasecmp(opts, cp, strlen(cp)) == 0) { |
01dafcb5 | 104 | auth_debug_add("X11 forwarding disabled."); |
38c295d6 | 105 | no_x11_forwarding_flag = 1; |
61e96248 | 106 | opts += strlen(cp); |
38c295d6 | 107 | goto next_option; |
108 | } | |
109 | cp = "no-pty"; | |
61e96248 | 110 | if (strncasecmp(opts, cp, strlen(cp)) == 0) { |
01dafcb5 | 111 | auth_debug_add("Pty allocation disabled."); |
38c295d6 | 112 | no_pty_flag = 1; |
61e96248 | 113 | opts += strlen(cp); |
38c295d6 | 114 | goto next_option; |
115 | } | |
116 | cp = "command=\""; | |
61e96248 | 117 | if (strncasecmp(opts, cp, strlen(cp)) == 0) { |
61e96248 | 118 | opts += strlen(cp); |
119 | forced_command = xmalloc(strlen(opts) + 1); | |
38c295d6 | 120 | i = 0; |
61e96248 | 121 | while (*opts) { |
122 | if (*opts == '"') | |
38c295d6 | 123 | break; |
61e96248 | 124 | if (*opts == '\\' && opts[1] == '"') { |
125 | opts += 2; | |
38c295d6 | 126 | forced_command[i++] = '"'; |
127 | continue; | |
128 | } | |
61e96248 | 129 | forced_command[i++] = *opts++; |
38c295d6 | 130 | } |
61e96248 | 131 | if (!*opts) { |
38c295d6 | 132 | debug("%.100s, line %lu: missing end quote", |
42f11eb2 | 133 | file, linenum); |
01dafcb5 | 134 | auth_debug_add("%.100s, line %lu: missing end quote", |
42f11eb2 | 135 | file, linenum); |
acc9d6d7 | 136 | xfree(forced_command); |
137 | forced_command = NULL; | |
138 | goto bad_option; | |
38c295d6 | 139 | } |
774de098 | 140 | forced_command[i] = '\0'; |
01dafcb5 | 141 | auth_debug_add("Forced command: %.900s", forced_command); |
61e96248 | 142 | opts++; |
38c295d6 | 143 | goto next_option; |
144 | } | |
145 | cp = "environment=\""; | |
f00bab84 | 146 | if (options.permit_user_env && |
147 | strncasecmp(opts, cp, strlen(cp)) == 0) { | |
38c295d6 | 148 | char *s; |
149 | struct envstring *new_envstring; | |
01794848 | 150 | |
61e96248 | 151 | opts += strlen(cp); |
152 | s = xmalloc(strlen(opts) + 1); | |
38c295d6 | 153 | i = 0; |
61e96248 | 154 | while (*opts) { |
155 | if (*opts == '"') | |
38c295d6 | 156 | break; |
61e96248 | 157 | if (*opts == '\\' && opts[1] == '"') { |
158 | opts += 2; | |
38c295d6 | 159 | s[i++] = '"'; |
160 | continue; | |
161 | } | |
61e96248 | 162 | s[i++] = *opts++; |
38c295d6 | 163 | } |
61e96248 | 164 | if (!*opts) { |
38c295d6 | 165 | debug("%.100s, line %lu: missing end quote", |
42f11eb2 | 166 | file, linenum); |
01dafcb5 | 167 | auth_debug_add("%.100s, line %lu: missing end quote", |
42f11eb2 | 168 | file, linenum); |
acc9d6d7 | 169 | xfree(s); |
170 | goto bad_option; | |
38c295d6 | 171 | } |
774de098 | 172 | s[i] = '\0'; |
01dafcb5 | 173 | auth_debug_add("Adding to environment: %.900s", s); |
38c295d6 | 174 | debug("Adding to environment: %.900s", s); |
61e96248 | 175 | opts++; |
38c295d6 | 176 | new_envstring = xmalloc(sizeof(struct envstring)); |
177 | new_envstring->s = s; | |
178 | new_envstring->next = custom_environment; | |
179 | custom_environment = new_envstring; | |
180 | goto next_option; | |
181 | } | |
182 | cp = "from=\""; | |
61e96248 | 183 | if (strncasecmp(opts, cp, strlen(cp)) == 0) { |
61e96248 | 184 | const char *remote_ip = get_remote_ipaddr(); |
185 | const char *remote_host = get_canonical_hostname( | |
c5a7d788 | 186 | options.use_dns); |
61e96248 | 187 | char *patterns = xmalloc(strlen(opts) + 1); |
01794848 | 188 | |
61e96248 | 189 | opts += strlen(cp); |
38c295d6 | 190 | i = 0; |
61e96248 | 191 | while (*opts) { |
192 | if (*opts == '"') | |
38c295d6 | 193 | break; |
61e96248 | 194 | if (*opts == '\\' && opts[1] == '"') { |
195 | opts += 2; | |
38c295d6 | 196 | patterns[i++] = '"'; |
197 | continue; | |
198 | } | |
61e96248 | 199 | patterns[i++] = *opts++; |
38c295d6 | 200 | } |
61e96248 | 201 | if (!*opts) { |
38c295d6 | 202 | debug("%.100s, line %lu: missing end quote", |
42f11eb2 | 203 | file, linenum); |
01dafcb5 | 204 | auth_debug_add("%.100s, line %lu: missing end quote", |
42f11eb2 | 205 | file, linenum); |
acc9d6d7 | 206 | xfree(patterns); |
207 | goto bad_option; | |
38c295d6 | 208 | } |
774de098 | 209 | patterns[i] = '\0'; |
61e96248 | 210 | opts++; |
2cee8a25 | 211 | if (match_host_and_ip(remote_host, remote_ip, |
212 | patterns) != 1) { | |
213 | xfree(patterns); | |
bbe88b6d | 214 | logit("Authentication tried for %.100s with " |
61e96248 | 215 | "correct key but not from a permitted " |
216 | "host (host=%.200s, ip=%.200s).", | |
217 | pw->pw_name, remote_host, remote_ip); | |
01dafcb5 | 218 | auth_debug_add("Your host '%.200s' is not " |
61e96248 | 219 | "permitted to use this key for login.", |
220 | remote_host); | |
38c295d6 | 221 | /* deny access */ |
222 | return 0; | |
223 | } | |
2cee8a25 | 224 | xfree(patterns); |
38c295d6 | 225 | /* Host name matches. */ |
226 | goto next_option; | |
227 | } | |
01794848 | 228 | cp = "permitopen=\""; |
229 | if (strncasecmp(opts, cp, strlen(cp)) == 0) { | |
3867aa0a | 230 | char *host, *p; |
01794848 | 231 | u_short port; |
01794848 | 232 | char *patterns = xmalloc(strlen(opts) + 1); |
233 | ||
234 | opts += strlen(cp); | |
235 | i = 0; | |
236 | while (*opts) { | |
237 | if (*opts == '"') | |
238 | break; | |
239 | if (*opts == '\\' && opts[1] == '"') { | |
240 | opts += 2; | |
241 | patterns[i++] = '"'; | |
242 | continue; | |
243 | } | |
244 | patterns[i++] = *opts++; | |
245 | } | |
246 | if (!*opts) { | |
247 | debug("%.100s, line %lu: missing end quote", | |
248 | file, linenum); | |
3867aa0a | 249 | auth_debug_add("%.100s, line %lu: missing " |
250 | "end quote", file, linenum); | |
01794848 | 251 | xfree(patterns); |
252 | goto bad_option; | |
253 | } | |
774de098 | 254 | patterns[i] = '\0'; |
01794848 | 255 | opts++; |
3867aa0a | 256 | p = patterns; |
257 | host = hpdelim(&p); | |
258 | if (host == NULL || strlen(host) >= NI_MAXHOST) { | |
259 | debug("%.100s, line %lu: Bad permitopen " | |
16d3d2bc | 260 | "specification <%.100s>", file, linenum, |
3867aa0a | 261 | patterns); |
01dafcb5 | 262 | auth_debug_add("%.100s, line %lu: " |
3867aa0a | 263 | "Bad permitopen specification", file, |
264 | linenum); | |
01794848 | 265 | xfree(patterns); |
266 | goto bad_option; | |
267 | } | |
f8cc7664 | 268 | host = cleanhostname(host); |
269 | if (p == NULL || (port = a2port(p)) == 0) { | |
3867aa0a | 270 | debug("%.100s, line %lu: Bad permitopen port " |
271 | "<%.100s>", file, linenum, p ? p : ""); | |
01dafcb5 | 272 | auth_debug_add("%.100s, line %lu: " |
ed787d14 | 273 | "Bad permitopen port", file, linenum); |
01794848 | 274 | xfree(patterns); |
275 | goto bad_option; | |
276 | } | |
b1ed8313 | 277 | if (options.allow_tcp_forwarding) |
ed787d14 | 278 | channel_add_permitted_opens(host, port); |
01794848 | 279 | xfree(patterns); |
280 | goto next_option; | |
281 | } | |
d20f3c9e | 282 | cp = "tunnel=\""; |
283 | if (strncasecmp(opts, cp, strlen(cp)) == 0) { | |
284 | char *tun = NULL; | |
285 | opts += strlen(cp); | |
286 | tun = xmalloc(strlen(opts) + 1); | |
287 | i = 0; | |
288 | while (*opts) { | |
289 | if (*opts == '"') | |
290 | break; | |
291 | tun[i++] = *opts++; | |
292 | } | |
293 | if (!*opts) { | |
294 | debug("%.100s, line %lu: missing end quote", | |
295 | file, linenum); | |
296 | auth_debug_add("%.100s, line %lu: missing end quote", | |
297 | file, linenum); | |
298 | xfree(tun); | |
299 | forced_tun_device = -1; | |
300 | goto bad_option; | |
301 | } | |
774de098 | 302 | tun[i] = '\0'; |
d20f3c9e | 303 | forced_tun_device = a2tun(tun, NULL); |
304 | xfree(tun); | |
a4f24bf8 | 305 | if (forced_tun_device == SSH_TUNID_ERR) { |
d20f3c9e | 306 | debug("%.100s, line %lu: invalid tun device", |
307 | file, linenum); | |
308 | auth_debug_add("%.100s, line %lu: invalid tun device", | |
309 | file, linenum); | |
310 | forced_tun_device = -1; | |
311 | goto bad_option; | |
312 | } | |
313 | auth_debug_add("Forced tun device: %d", forced_tun_device); | |
314 | opts++; | |
315 | goto next_option; | |
316 | } | |
38c295d6 | 317 | next_option: |
318 | /* | |
319 | * Skip the comma, and move to the next option | |
320 | * (or break out if there are no more). | |
321 | */ | |
61e96248 | 322 | if (!*opts) |
38c295d6 | 323 | fatal("Bugs in auth-options.c option processing."); |
61e96248 | 324 | if (*opts == ' ' || *opts == '\t') |
38c295d6 | 325 | break; /* End of options. */ |
61e96248 | 326 | if (*opts != ',') |
38c295d6 | 327 | goto bad_option; |
61e96248 | 328 | opts++; |
38c295d6 | 329 | /* Process the next option. */ |
330 | } | |
1853d1ef | 331 | |
332 | if (!use_privsep) | |
01dafcb5 | 333 | auth_debug_send(); |
1853d1ef | 334 | |
38c295d6 | 335 | /* grant access */ |
336 | return 1; | |
337 | ||
338 | bad_option: | |
bbe88b6d | 339 | logit("Bad options in %.100s file, line %lu: %.50s", |
61e96248 | 340 | file, linenum, opts); |
01dafcb5 | 341 | auth_debug_add("Bad options in %.100s file, line %lu: %.50s", |
61e96248 | 342 | file, linenum, opts); |
1853d1ef | 343 | |
344 | if (!use_privsep) | |
01dafcb5 | 345 | auth_debug_send(); |
1853d1ef | 346 | |
38c295d6 | 347 | /* deny access */ |
348 | return 0; | |
349 | } |