blob: 56d598412653d80bc30b897791acfc96818a4c6d [file] [log] [blame]
Damien Miller57c30112006-03-26 14:24:48 +11001/* $OpenBSD: auth-options.c,v 1.35 2006/03/25 13:17:01 djm Exp $ */
Damien Millere4340be2000-09-16 13:29:08 +11002/*
3 * Author: Tatu Ylonen <ylo@cs.hut.fi>
4 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5 * All rights reserved
Damien Millere4340be2000-09-16 13:29:08 +11006 * As far as I am concerned, the code I have written for this software
7 * can be used freely for any purpose. Any derived versions of this
8 * software must be clearly marked as such, and if the derived work is
9 * incompatible with the protocol description in the RFC file, it must be
10 * called by a name other than "ssh" or "Secure Shell".
11 */
12
Damien Millerf6d9e222000-06-18 14:50:44 +100013#include "includes.h"
Damien Millerf6d9e222000-06-18 14:50:44 +100014
Damien Millerf6d9e222000-06-18 14:50:44 +100015#include "xmalloc.h"
16#include "match.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000017#include "log.h"
18#include "canohost.h"
Ben Lindstromc7637672001-06-09 00:36:26 +000019#include "channels.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000020#include "auth-options.h"
Damien Miller33804262001-02-04 23:20:18 +110021#include "servconf.h"
Ben Lindstromd71ba572001-09-12 18:03:31 +000022#include "misc.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000023#include "monitor_wrap.h"
Ben Lindstroma574cda2002-05-15 16:16:14 +000024#include "auth.h"
Damien Millerf6d9e222000-06-18 14:50:44 +100025
26/* Flags set authorized_keys flags */
27int no_port_forwarding_flag = 0;
28int no_agent_forwarding_flag = 0;
29int no_x11_forwarding_flag = 0;
30int no_pty_flag = 0;
31
32/* "command=" option. */
33char *forced_command = NULL;
34
35/* "environment=" options. */
36struct envstring *custom_environment = NULL;
37
Damien Millerd27b9472005-12-13 19:29:02 +110038/* "tunnel=" option. */
39int forced_tun_device = -1;
40
Damien Miller33804262001-02-04 23:20:18 +110041extern ServerOptions options;
42
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000043void
Damien Miller874d77b2000-10-14 16:23:11 +110044auth_clear_options(void)
45{
46 no_agent_forwarding_flag = 0;
47 no_port_forwarding_flag = 0;
48 no_pty_flag = 0;
49 no_x11_forwarding_flag = 0;
50 while (custom_environment) {
51 struct envstring *ce = custom_environment;
52 custom_environment = ce->next;
53 xfree(ce->s);
54 xfree(ce);
55 }
56 if (forced_command) {
57 xfree(forced_command);
58 forced_command = NULL;
59 }
Damien Millerd27b9472005-12-13 19:29:02 +110060 forced_tun_device = -1;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +000061 channel_clear_permitted_opens();
Ben Lindstroma574cda2002-05-15 16:16:14 +000062 auth_debug_reset();
Damien Miller874d77b2000-10-14 16:23:11 +110063}
64
Ben Lindstrom226cfa02001-01-22 05:34:40 +000065/*
66 * return 1 if access is granted, 0 if not.
67 * side effect: sets key option flags
68 */
Damien Millerf6d9e222000-06-18 14:50:44 +100069int
Damien Miller33804262001-02-04 23:20:18 +110070auth_parse_options(struct passwd *pw, char *opts, char *file, u_long linenum)
Damien Millerf6d9e222000-06-18 14:50:44 +100071{
72 const char *cp;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +000073 int i;
Damien Miller874d77b2000-10-14 16:23:11 +110074
75 /* reset options */
76 auth_clear_options();
77
Ben Lindstrom36d7bd02001-02-10 22:27:19 +000078 if (!opts)
79 return 1;
80
Damien Miller33804262001-02-04 23:20:18 +110081 while (*opts && *opts != ' ' && *opts != '\t') {
Damien Millerf6d9e222000-06-18 14:50:44 +100082 cp = "no-port-forwarding";
Damien Miller33804262001-02-04 23:20:18 +110083 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
Ben Lindstroma574cda2002-05-15 16:16:14 +000084 auth_debug_add("Port forwarding disabled.");
Damien Millerf6d9e222000-06-18 14:50:44 +100085 no_port_forwarding_flag = 1;
Damien Miller33804262001-02-04 23:20:18 +110086 opts += strlen(cp);
Damien Millerf6d9e222000-06-18 14:50:44 +100087 goto next_option;
88 }
89 cp = "no-agent-forwarding";
Damien Miller33804262001-02-04 23:20:18 +110090 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
Ben Lindstroma574cda2002-05-15 16:16:14 +000091 auth_debug_add("Agent forwarding disabled.");
Damien Millerf6d9e222000-06-18 14:50:44 +100092 no_agent_forwarding_flag = 1;
Damien Miller33804262001-02-04 23:20:18 +110093 opts += strlen(cp);
Damien Millerf6d9e222000-06-18 14:50:44 +100094 goto next_option;
95 }
96 cp = "no-X11-forwarding";
Damien Miller33804262001-02-04 23:20:18 +110097 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
Ben Lindstroma574cda2002-05-15 16:16:14 +000098 auth_debug_add("X11 forwarding disabled.");
Damien Millerf6d9e222000-06-18 14:50:44 +100099 no_x11_forwarding_flag = 1;
Damien Miller33804262001-02-04 23:20:18 +1100100 opts += strlen(cp);
Damien Millerf6d9e222000-06-18 14:50:44 +1000101 goto next_option;
102 }
103 cp = "no-pty";
Damien Miller33804262001-02-04 23:20:18 +1100104 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
Ben Lindstroma574cda2002-05-15 16:16:14 +0000105 auth_debug_add("Pty allocation disabled.");
Damien Millerf6d9e222000-06-18 14:50:44 +1000106 no_pty_flag = 1;
Damien Miller33804262001-02-04 23:20:18 +1100107 opts += strlen(cp);
Damien Millerf6d9e222000-06-18 14:50:44 +1000108 goto next_option;
109 }
110 cp = "command=\"";
Damien Miller33804262001-02-04 23:20:18 +1100111 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
Damien Miller33804262001-02-04 23:20:18 +1100112 opts += strlen(cp);
113 forced_command = xmalloc(strlen(opts) + 1);
Damien Millerf6d9e222000-06-18 14:50:44 +1000114 i = 0;
Damien Miller33804262001-02-04 23:20:18 +1100115 while (*opts) {
116 if (*opts == '"')
Damien Millerf6d9e222000-06-18 14:50:44 +1000117 break;
Damien Miller33804262001-02-04 23:20:18 +1100118 if (*opts == '\\' && opts[1] == '"') {
119 opts += 2;
Damien Millerf6d9e222000-06-18 14:50:44 +1000120 forced_command[i++] = '"';
121 continue;
122 }
Damien Miller33804262001-02-04 23:20:18 +1100123 forced_command[i++] = *opts++;
Damien Millerf6d9e222000-06-18 14:50:44 +1000124 }
Damien Miller33804262001-02-04 23:20:18 +1100125 if (!*opts) {
Damien Millerf6d9e222000-06-18 14:50:44 +1000126 debug("%.100s, line %lu: missing end quote",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000127 file, linenum);
Ben Lindstroma574cda2002-05-15 16:16:14 +0000128 auth_debug_add("%.100s, line %lu: missing end quote",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000129 file, linenum);
Damien Miller056ddf72001-03-14 10:15:20 +1100130 xfree(forced_command);
131 forced_command = NULL;
132 goto bad_option;
Damien Millerf6d9e222000-06-18 14:50:44 +1000133 }
134 forced_command[i] = 0;
Ben Lindstroma574cda2002-05-15 16:16:14 +0000135 auth_debug_add("Forced command: %.900s", forced_command);
Damien Miller33804262001-02-04 23:20:18 +1100136 opts++;
Damien Millerf6d9e222000-06-18 14:50:44 +1000137 goto next_option;
138 }
139 cp = "environment=\"";
Ben Lindstrom5d860f02002-08-01 01:28:38 +0000140 if (options.permit_user_env &&
141 strncasecmp(opts, cp, strlen(cp)) == 0) {
Damien Millerf6d9e222000-06-18 14:50:44 +1000142 char *s;
143 struct envstring *new_envstring;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +0000144
Damien Miller33804262001-02-04 23:20:18 +1100145 opts += strlen(cp);
146 s = xmalloc(strlen(opts) + 1);
Damien Millerf6d9e222000-06-18 14:50:44 +1000147 i = 0;
Damien Miller33804262001-02-04 23:20:18 +1100148 while (*opts) {
149 if (*opts == '"')
Damien Millerf6d9e222000-06-18 14:50:44 +1000150 break;
Damien Miller33804262001-02-04 23:20:18 +1100151 if (*opts == '\\' && opts[1] == '"') {
152 opts += 2;
Damien Millerf6d9e222000-06-18 14:50:44 +1000153 s[i++] = '"';
154 continue;
155 }
Damien Miller33804262001-02-04 23:20:18 +1100156 s[i++] = *opts++;
Damien Millerf6d9e222000-06-18 14:50:44 +1000157 }
Damien Miller33804262001-02-04 23:20:18 +1100158 if (!*opts) {
Damien Millerf6d9e222000-06-18 14:50:44 +1000159 debug("%.100s, line %lu: missing end quote",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000160 file, linenum);
Ben Lindstroma574cda2002-05-15 16:16:14 +0000161 auth_debug_add("%.100s, line %lu: missing end quote",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000162 file, linenum);
Damien Miller056ddf72001-03-14 10:15:20 +1100163 xfree(s);
164 goto bad_option;
Damien Millerf6d9e222000-06-18 14:50:44 +1000165 }
166 s[i] = 0;
Ben Lindstroma574cda2002-05-15 16:16:14 +0000167 auth_debug_add("Adding to environment: %.900s", s);
Damien Millerf6d9e222000-06-18 14:50:44 +1000168 debug("Adding to environment: %.900s", s);
Damien Miller33804262001-02-04 23:20:18 +1100169 opts++;
Damien Millerf6d9e222000-06-18 14:50:44 +1000170 new_envstring = xmalloc(sizeof(struct envstring));
171 new_envstring->s = s;
172 new_envstring->next = custom_environment;
173 custom_environment = new_envstring;
174 goto next_option;
175 }
176 cp = "from=\"";
Damien Miller33804262001-02-04 23:20:18 +1100177 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
Damien Miller33804262001-02-04 23:20:18 +1100178 const char *remote_ip = get_remote_ipaddr();
179 const char *remote_host = get_canonical_hostname(
Damien Miller3a961dc2003-06-03 10:25:48 +1000180 options.use_dns);
Damien Miller33804262001-02-04 23:20:18 +1100181 char *patterns = xmalloc(strlen(opts) + 1);
Ben Lindstrom7bb8b492001-03-17 00:47:54 +0000182
Damien Miller33804262001-02-04 23:20:18 +1100183 opts += strlen(cp);
Damien Millerf6d9e222000-06-18 14:50:44 +1000184 i = 0;
Damien Miller33804262001-02-04 23:20:18 +1100185 while (*opts) {
186 if (*opts == '"')
Damien Millerf6d9e222000-06-18 14:50:44 +1000187 break;
Damien Miller33804262001-02-04 23:20:18 +1100188 if (*opts == '\\' && opts[1] == '"') {
189 opts += 2;
Damien Millerf6d9e222000-06-18 14:50:44 +1000190 patterns[i++] = '"';
191 continue;
192 }
Damien Miller33804262001-02-04 23:20:18 +1100193 patterns[i++] = *opts++;
Damien Millerf6d9e222000-06-18 14:50:44 +1000194 }
Damien Miller33804262001-02-04 23:20:18 +1100195 if (!*opts) {
Damien Millerf6d9e222000-06-18 14:50:44 +1000196 debug("%.100s, line %lu: missing end quote",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000197 file, linenum);
Ben Lindstroma574cda2002-05-15 16:16:14 +0000198 auth_debug_add("%.100s, line %lu: missing end quote",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000199 file, linenum);
Damien Miller056ddf72001-03-14 10:15:20 +1100200 xfree(patterns);
201 goto bad_option;
Damien Millerf6d9e222000-06-18 14:50:44 +1000202 }
203 patterns[i] = 0;
Damien Miller33804262001-02-04 23:20:18 +1100204 opts++;
Ben Lindstromf0c50292001-06-25 05:17:53 +0000205 if (match_host_and_ip(remote_host, remote_ip,
206 patterns) != 1) {
207 xfree(patterns);
Damien Miller996acd22003-04-09 20:59:48 +1000208 logit("Authentication tried for %.100s with "
Damien Miller33804262001-02-04 23:20:18 +1100209 "correct key but not from a permitted "
210 "host (host=%.200s, ip=%.200s).",
211 pw->pw_name, remote_host, remote_ip);
Ben Lindstroma574cda2002-05-15 16:16:14 +0000212 auth_debug_add("Your host '%.200s' is not "
Damien Miller33804262001-02-04 23:20:18 +1100213 "permitted to use this key for login.",
214 remote_host);
Damien Millerf6d9e222000-06-18 14:50:44 +1000215 /* deny access */
216 return 0;
217 }
Ben Lindstromf0c50292001-06-25 05:17:53 +0000218 xfree(patterns);
Damien Millerf6d9e222000-06-18 14:50:44 +1000219 /* Host name matches. */
220 goto next_option;
221 }
Ben Lindstrom7bb8b492001-03-17 00:47:54 +0000222 cp = "permitopen=\"";
223 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
Damien Millerf91ee4c2005-03-01 21:24:33 +1100224 char *host, *p;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +0000225 u_short port;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +0000226 char *patterns = xmalloc(strlen(opts) + 1);
227
228 opts += strlen(cp);
229 i = 0;
230 while (*opts) {
231 if (*opts == '"')
232 break;
233 if (*opts == '\\' && opts[1] == '"') {
234 opts += 2;
235 patterns[i++] = '"';
236 continue;
237 }
238 patterns[i++] = *opts++;
239 }
240 if (!*opts) {
241 debug("%.100s, line %lu: missing end quote",
242 file, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100243 auth_debug_add("%.100s, line %lu: missing "
244 "end quote", file, linenum);
Ben Lindstrom7bb8b492001-03-17 00:47:54 +0000245 xfree(patterns);
246 goto bad_option;
247 }
248 patterns[i] = 0;
249 opts++;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100250 p = patterns;
251 host = hpdelim(&p);
252 if (host == NULL || strlen(host) >= NI_MAXHOST) {
253 debug("%.100s, line %lu: Bad permitopen "
Darren Tucker90b9e022005-03-14 23:08:50 +1100254 "specification <%.100s>", file, linenum,
Damien Millerf91ee4c2005-03-01 21:24:33 +1100255 patterns);
Ben Lindstroma574cda2002-05-15 16:16:14 +0000256 auth_debug_add("%.100s, line %lu: "
Damien Millerf91ee4c2005-03-01 21:24:33 +1100257 "Bad permitopen specification", file,
258 linenum);
Ben Lindstrom7bb8b492001-03-17 00:47:54 +0000259 xfree(patterns);
260 goto bad_option;
261 }
Darren Tucker47eede72005-03-14 23:08:12 +1100262 host = cleanhostname(host);
263 if (p == NULL || (port = a2port(p)) == 0) {
Damien Millerf91ee4c2005-03-01 21:24:33 +1100264 debug("%.100s, line %lu: Bad permitopen port "
265 "<%.100s>", file, linenum, p ? p : "");
Ben Lindstroma574cda2002-05-15 16:16:14 +0000266 auth_debug_add("%.100s, line %lu: "
Ben Lindstromd71ba572001-09-12 18:03:31 +0000267 "Bad permitopen port", file, linenum);
Ben Lindstrom7bb8b492001-03-17 00:47:54 +0000268 xfree(patterns);
269 goto bad_option;
270 }
Ben Lindstrom2d70f982001-03-19 00:13:46 +0000271 if (options.allow_tcp_forwarding)
Ben Lindstromd71ba572001-09-12 18:03:31 +0000272 channel_add_permitted_opens(host, port);
Ben Lindstrom7bb8b492001-03-17 00:47:54 +0000273 xfree(patterns);
274 goto next_option;
275 }
Damien Millerd27b9472005-12-13 19:29:02 +1100276 cp = "tunnel=\"";
277 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
278 char *tun = NULL;
279 opts += strlen(cp);
280 tun = xmalloc(strlen(opts) + 1);
281 i = 0;
282 while (*opts) {
283 if (*opts == '"')
284 break;
285 tun[i++] = *opts++;
286 }
287 if (!*opts) {
288 debug("%.100s, line %lu: missing end quote",
289 file, linenum);
290 auth_debug_add("%.100s, line %lu: missing end quote",
291 file, linenum);
292 xfree(tun);
293 forced_tun_device = -1;
294 goto bad_option;
295 }
296 tun[i] = 0;
297 forced_tun_device = a2tun(tun, NULL);
298 xfree(tun);
Damien Miller7b58e802005-12-13 19:33:19 +1100299 if (forced_tun_device == SSH_TUNID_ERR) {
Damien Millerd27b9472005-12-13 19:29:02 +1100300 debug("%.100s, line %lu: invalid tun device",
301 file, linenum);
302 auth_debug_add("%.100s, line %lu: invalid tun device",
303 file, linenum);
304 forced_tun_device = -1;
305 goto bad_option;
306 }
307 auth_debug_add("Forced tun device: %d", forced_tun_device);
308 opts++;
309 goto next_option;
310 }
Damien Millerf6d9e222000-06-18 14:50:44 +1000311next_option:
312 /*
313 * Skip the comma, and move to the next option
314 * (or break out if there are no more).
315 */
Damien Miller33804262001-02-04 23:20:18 +1100316 if (!*opts)
Damien Millerf6d9e222000-06-18 14:50:44 +1000317 fatal("Bugs in auth-options.c option processing.");
Damien Miller33804262001-02-04 23:20:18 +1100318 if (*opts == ' ' || *opts == '\t')
Damien Millerf6d9e222000-06-18 14:50:44 +1000319 break; /* End of options. */
Damien Miller33804262001-02-04 23:20:18 +1100320 if (*opts != ',')
Damien Millerf6d9e222000-06-18 14:50:44 +1000321 goto bad_option;
Damien Miller33804262001-02-04 23:20:18 +1100322 opts++;
Damien Millerf6d9e222000-06-18 14:50:44 +1000323 /* Process the next option. */
324 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000325
326 if (!use_privsep)
Ben Lindstroma574cda2002-05-15 16:16:14 +0000327 auth_debug_send();
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000328
Damien Millerf6d9e222000-06-18 14:50:44 +1000329 /* grant access */
330 return 1;
331
332bad_option:
Damien Miller996acd22003-04-09 20:59:48 +1000333 logit("Bad options in %.100s file, line %lu: %.50s",
Damien Miller33804262001-02-04 23:20:18 +1100334 file, linenum, opts);
Ben Lindstroma574cda2002-05-15 16:16:14 +0000335 auth_debug_add("Bad options in %.100s file, line %lu: %.50s",
Damien Miller33804262001-02-04 23:20:18 +1100336 file, linenum, opts);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000337
338 if (!use_privsep)
Ben Lindstroma574cda2002-05-15 16:16:14 +0000339 auth_debug_send();
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000340
Damien Millerf6d9e222000-06-18 14:50:44 +1000341 /* deny access */
342 return 0;
343}