blob: ab085c2339cc7bba259619d7181b1cce3c151a43 [file] [log] [blame]
Damien Millere37dde02009-01-28 16:33:01 +11001/* $OpenBSD: auth-options.c,v 1.44 2009/01/22 10:09:16 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 Miller9f2abc42006-07-10 20:53:08 +100015#include <sys/types.h>
16
Damien Millerb8fe89c2006-07-24 14:51:00 +100017#include <netdb.h>
Damien Miller9f2abc42006-07-10 20:53:08 +100018#include <pwd.h>
Damien Millere3476ed2006-07-24 14:13:33 +100019#include <string.h>
Damien Millerd7834352006-08-05 12:39:39 +100020#include <stdio.h>
21#include <stdarg.h>
Damien Miller9f2abc42006-07-10 20:53:08 +100022
Damien Millerb84886b2008-05-19 15:05:07 +100023#include "openbsd-compat/sys-queue.h"
Damien Millerf6d9e222000-06-18 14:50:44 +100024#include "xmalloc.h"
25#include "match.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000026#include "log.h"
27#include "canohost.h"
Damien Millerd7834352006-08-05 12:39:39 +100028#include "buffer.h"
Ben Lindstromc7637672001-06-09 00:36:26 +000029#include "channels.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000030#include "auth-options.h"
Damien Miller33804262001-02-04 23:20:18 +110031#include "servconf.h"
Ben Lindstromd71ba572001-09-12 18:03:31 +000032#include "misc.h"
Damien Millerd7834352006-08-05 12:39:39 +100033#include "key.h"
34#include "hostfile.h"
Ben Lindstroma574cda2002-05-15 16:16:14 +000035#include "auth.h"
Damien Millerd7834352006-08-05 12:39:39 +100036#ifdef GSSAPI
37#include "ssh-gss.h"
38#endif
39#include "monitor_wrap.h"
Damien Millerf6d9e222000-06-18 14:50:44 +100040
41/* Flags set authorized_keys flags */
42int no_port_forwarding_flag = 0;
43int no_agent_forwarding_flag = 0;
44int no_x11_forwarding_flag = 0;
45int no_pty_flag = 0;
Damien Miller95e80952008-03-27 11:03:05 +110046int no_user_rc = 0;
Damien Millerf6d9e222000-06-18 14:50:44 +100047
48/* "command=" option. */
49char *forced_command = NULL;
50
51/* "environment=" options. */
52struct envstring *custom_environment = NULL;
53
Damien Millerd27b9472005-12-13 19:29:02 +110054/* "tunnel=" option. */
55int forced_tun_device = -1;
56
Damien Miller33804262001-02-04 23:20:18 +110057extern ServerOptions options;
58
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000059void
Damien Miller874d77b2000-10-14 16:23:11 +110060auth_clear_options(void)
61{
62 no_agent_forwarding_flag = 0;
63 no_port_forwarding_flag = 0;
64 no_pty_flag = 0;
65 no_x11_forwarding_flag = 0;
Damien Miller95e80952008-03-27 11:03:05 +110066 no_user_rc = 0;
Damien Miller874d77b2000-10-14 16:23:11 +110067 while (custom_environment) {
68 struct envstring *ce = custom_environment;
69 custom_environment = ce->next;
70 xfree(ce->s);
71 xfree(ce);
72 }
73 if (forced_command) {
74 xfree(forced_command);
75 forced_command = NULL;
76 }
Damien Millerd27b9472005-12-13 19:29:02 +110077 forced_tun_device = -1;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +000078 channel_clear_permitted_opens();
Ben Lindstroma574cda2002-05-15 16:16:14 +000079 auth_debug_reset();
Damien Miller874d77b2000-10-14 16:23:11 +110080}
81
Ben Lindstrom226cfa02001-01-22 05:34:40 +000082/*
83 * return 1 if access is granted, 0 if not.
84 * side effect: sets key option flags
85 */
Damien Millerf6d9e222000-06-18 14:50:44 +100086int
Damien Miller33804262001-02-04 23:20:18 +110087auth_parse_options(struct passwd *pw, char *opts, char *file, u_long linenum)
Damien Millerf6d9e222000-06-18 14:50:44 +100088{
89 const char *cp;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +000090 int i;
Damien Miller874d77b2000-10-14 16:23:11 +110091
92 /* reset options */
93 auth_clear_options();
94
Ben Lindstrom36d7bd02001-02-10 22:27:19 +000095 if (!opts)
96 return 1;
97
Damien Miller33804262001-02-04 23:20:18 +110098 while (*opts && *opts != ' ' && *opts != '\t') {
Damien Millerf6d9e222000-06-18 14:50:44 +100099 cp = "no-port-forwarding";
Damien Miller33804262001-02-04 23:20:18 +1100100 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
Ben Lindstroma574cda2002-05-15 16:16:14 +0000101 auth_debug_add("Port forwarding disabled.");
Damien Millerf6d9e222000-06-18 14:50:44 +1000102 no_port_forwarding_flag = 1;
Damien Miller33804262001-02-04 23:20:18 +1100103 opts += strlen(cp);
Damien Millerf6d9e222000-06-18 14:50:44 +1000104 goto next_option;
105 }
106 cp = "no-agent-forwarding";
Damien Miller33804262001-02-04 23:20:18 +1100107 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
Ben Lindstroma574cda2002-05-15 16:16:14 +0000108 auth_debug_add("Agent forwarding disabled.");
Damien Millerf6d9e222000-06-18 14:50:44 +1000109 no_agent_forwarding_flag = 1;
Damien Miller33804262001-02-04 23:20:18 +1100110 opts += strlen(cp);
Damien Millerf6d9e222000-06-18 14:50:44 +1000111 goto next_option;
112 }
113 cp = "no-X11-forwarding";
Damien Miller33804262001-02-04 23:20:18 +1100114 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
Ben Lindstroma574cda2002-05-15 16:16:14 +0000115 auth_debug_add("X11 forwarding disabled.");
Damien Millerf6d9e222000-06-18 14:50:44 +1000116 no_x11_forwarding_flag = 1;
Damien Miller33804262001-02-04 23:20:18 +1100117 opts += strlen(cp);
Damien Millerf6d9e222000-06-18 14:50:44 +1000118 goto next_option;
119 }
120 cp = "no-pty";
Damien Miller33804262001-02-04 23:20:18 +1100121 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
Ben Lindstroma574cda2002-05-15 16:16:14 +0000122 auth_debug_add("Pty allocation disabled.");
Damien Millerf6d9e222000-06-18 14:50:44 +1000123 no_pty_flag = 1;
Damien Miller33804262001-02-04 23:20:18 +1100124 opts += strlen(cp);
Damien Millerf6d9e222000-06-18 14:50:44 +1000125 goto next_option;
126 }
Damien Miller95e80952008-03-27 11:03:05 +1100127 cp = "no-user-rc";
128 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
129 auth_debug_add("User rc file execution disabled.");
130 no_user_rc = 1;
131 opts += strlen(cp);
132 goto next_option;
133 }
Damien Millerf6d9e222000-06-18 14:50:44 +1000134 cp = "command=\"";
Damien Miller33804262001-02-04 23:20:18 +1100135 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
Damien Miller33804262001-02-04 23:20:18 +1100136 opts += strlen(cp);
137 forced_command = xmalloc(strlen(opts) + 1);
Damien Millerf6d9e222000-06-18 14:50:44 +1000138 i = 0;
Damien Miller33804262001-02-04 23:20:18 +1100139 while (*opts) {
140 if (*opts == '"')
Damien Millerf6d9e222000-06-18 14:50:44 +1000141 break;
Damien Miller33804262001-02-04 23:20:18 +1100142 if (*opts == '\\' && opts[1] == '"') {
143 opts += 2;
Damien Millerf6d9e222000-06-18 14:50:44 +1000144 forced_command[i++] = '"';
145 continue;
146 }
Damien Miller33804262001-02-04 23:20:18 +1100147 forced_command[i++] = *opts++;
Damien Millerf6d9e222000-06-18 14:50:44 +1000148 }
Damien Miller33804262001-02-04 23:20:18 +1100149 if (!*opts) {
Damien Millerf6d9e222000-06-18 14:50:44 +1000150 debug("%.100s, line %lu: missing end quote",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000151 file, linenum);
Ben Lindstroma574cda2002-05-15 16:16:14 +0000152 auth_debug_add("%.100s, line %lu: missing end quote",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000153 file, linenum);
Damien Miller056ddf72001-03-14 10:15:20 +1100154 xfree(forced_command);
155 forced_command = NULL;
156 goto bad_option;
Damien Millerf6d9e222000-06-18 14:50:44 +1000157 }
Damien Miller98299262006-07-24 14:01:43 +1000158 forced_command[i] = '\0';
Ben Lindstroma574cda2002-05-15 16:16:14 +0000159 auth_debug_add("Forced command: %.900s", forced_command);
Damien Miller33804262001-02-04 23:20:18 +1100160 opts++;
Damien Millerf6d9e222000-06-18 14:50:44 +1000161 goto next_option;
162 }
163 cp = "environment=\"";
Ben Lindstrom5d860f02002-08-01 01:28:38 +0000164 if (options.permit_user_env &&
165 strncasecmp(opts, cp, strlen(cp)) == 0) {
Damien Millerf6d9e222000-06-18 14:50:44 +1000166 char *s;
167 struct envstring *new_envstring;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +0000168
Damien Miller33804262001-02-04 23:20:18 +1100169 opts += strlen(cp);
170 s = xmalloc(strlen(opts) + 1);
Damien Millerf6d9e222000-06-18 14:50:44 +1000171 i = 0;
Damien Miller33804262001-02-04 23:20:18 +1100172 while (*opts) {
173 if (*opts == '"')
Damien Millerf6d9e222000-06-18 14:50:44 +1000174 break;
Damien Miller33804262001-02-04 23:20:18 +1100175 if (*opts == '\\' && opts[1] == '"') {
176 opts += 2;
Damien Millerf6d9e222000-06-18 14:50:44 +1000177 s[i++] = '"';
178 continue;
179 }
Damien Miller33804262001-02-04 23:20:18 +1100180 s[i++] = *opts++;
Damien Millerf6d9e222000-06-18 14:50:44 +1000181 }
Damien Miller33804262001-02-04 23:20:18 +1100182 if (!*opts) {
Damien Millerf6d9e222000-06-18 14:50:44 +1000183 debug("%.100s, line %lu: missing end quote",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000184 file, linenum);
Ben Lindstroma574cda2002-05-15 16:16:14 +0000185 auth_debug_add("%.100s, line %lu: missing end quote",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000186 file, linenum);
Damien Miller056ddf72001-03-14 10:15:20 +1100187 xfree(s);
188 goto bad_option;
Damien Millerf6d9e222000-06-18 14:50:44 +1000189 }
Damien Miller98299262006-07-24 14:01:43 +1000190 s[i] = '\0';
Ben Lindstroma574cda2002-05-15 16:16:14 +0000191 auth_debug_add("Adding to environment: %.900s", s);
Damien Millerf6d9e222000-06-18 14:50:44 +1000192 debug("Adding to environment: %.900s", s);
Damien Miller33804262001-02-04 23:20:18 +1100193 opts++;
Damien Millerf6d9e222000-06-18 14:50:44 +1000194 new_envstring = xmalloc(sizeof(struct envstring));
195 new_envstring->s = s;
196 new_envstring->next = custom_environment;
197 custom_environment = new_envstring;
198 goto next_option;
199 }
200 cp = "from=\"";
Damien Miller33804262001-02-04 23:20:18 +1100201 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
Damien Miller33804262001-02-04 23:20:18 +1100202 const char *remote_ip = get_remote_ipaddr();
203 const char *remote_host = get_canonical_hostname(
Damien Miller3a961dc2003-06-03 10:25:48 +1000204 options.use_dns);
Damien Miller33804262001-02-04 23:20:18 +1100205 char *patterns = xmalloc(strlen(opts) + 1);
Ben Lindstrom7bb8b492001-03-17 00:47:54 +0000206
Damien Miller33804262001-02-04 23:20:18 +1100207 opts += strlen(cp);
Damien Millerf6d9e222000-06-18 14:50:44 +1000208 i = 0;
Damien Miller33804262001-02-04 23:20:18 +1100209 while (*opts) {
210 if (*opts == '"')
Damien Millerf6d9e222000-06-18 14:50:44 +1000211 break;
Damien Miller33804262001-02-04 23:20:18 +1100212 if (*opts == '\\' && opts[1] == '"') {
213 opts += 2;
Damien Millerf6d9e222000-06-18 14:50:44 +1000214 patterns[i++] = '"';
215 continue;
216 }
Damien Miller33804262001-02-04 23:20:18 +1100217 patterns[i++] = *opts++;
Damien Millerf6d9e222000-06-18 14:50:44 +1000218 }
Damien Miller33804262001-02-04 23:20:18 +1100219 if (!*opts) {
Damien Millerf6d9e222000-06-18 14:50:44 +1000220 debug("%.100s, line %lu: missing end quote",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000221 file, linenum);
Ben Lindstroma574cda2002-05-15 16:16:14 +0000222 auth_debug_add("%.100s, line %lu: missing end quote",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000223 file, linenum);
Damien Miller056ddf72001-03-14 10:15:20 +1100224 xfree(patterns);
225 goto bad_option;
Damien Millerf6d9e222000-06-18 14:50:44 +1000226 }
Damien Miller98299262006-07-24 14:01:43 +1000227 patterns[i] = '\0';
Damien Miller33804262001-02-04 23:20:18 +1100228 opts++;
Darren Tucker896ad5a2008-06-11 09:34:46 +1000229 switch (match_host_and_ip(remote_host, remote_ip,
230 patterns)) {
231 case 1:
232 xfree(patterns);
233 /* Host name matches. */
234 goto next_option;
235 case -1:
236 debug("%.100s, line %lu: invalid criteria",
237 file, linenum);
238 auth_debug_add("%.100s, line %lu: "
239 "invalid criteria", file, linenum);
240 /* FALLTHROUGH */
241 case 0:
Ben Lindstromf0c50292001-06-25 05:17:53 +0000242 xfree(patterns);
Damien Miller996acd22003-04-09 20:59:48 +1000243 logit("Authentication tried for %.100s with "
Damien Miller33804262001-02-04 23:20:18 +1100244 "correct key but not from a permitted "
245 "host (host=%.200s, ip=%.200s).",
246 pw->pw_name, remote_host, remote_ip);
Ben Lindstroma574cda2002-05-15 16:16:14 +0000247 auth_debug_add("Your host '%.200s' is not "
Damien Miller33804262001-02-04 23:20:18 +1100248 "permitted to use this key for login.",
249 remote_host);
Darren Tucker896ad5a2008-06-11 09:34:46 +1000250 break;
Damien Millerf6d9e222000-06-18 14:50:44 +1000251 }
Darren Tucker896ad5a2008-06-11 09:34:46 +1000252 /* deny access */
253 return 0;
Damien Millerf6d9e222000-06-18 14:50:44 +1000254 }
Ben Lindstrom7bb8b492001-03-17 00:47:54 +0000255 cp = "permitopen=\"";
256 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
Damien Millerf91ee4c2005-03-01 21:24:33 +1100257 char *host, *p;
Damien Millere37dde02009-01-28 16:33:01 +1100258 int port;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +0000259 char *patterns = xmalloc(strlen(opts) + 1);
260
261 opts += strlen(cp);
262 i = 0;
263 while (*opts) {
264 if (*opts == '"')
265 break;
266 if (*opts == '\\' && opts[1] == '"') {
267 opts += 2;
268 patterns[i++] = '"';
269 continue;
270 }
271 patterns[i++] = *opts++;
272 }
273 if (!*opts) {
274 debug("%.100s, line %lu: missing end quote",
275 file, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100276 auth_debug_add("%.100s, line %lu: missing "
277 "end quote", file, linenum);
Ben Lindstrom7bb8b492001-03-17 00:47:54 +0000278 xfree(patterns);
279 goto bad_option;
280 }
Damien Miller98299262006-07-24 14:01:43 +1000281 patterns[i] = '\0';
Ben Lindstrom7bb8b492001-03-17 00:47:54 +0000282 opts++;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100283 p = patterns;
284 host = hpdelim(&p);
285 if (host == NULL || strlen(host) >= NI_MAXHOST) {
286 debug("%.100s, line %lu: Bad permitopen "
Darren Tucker90b9e022005-03-14 23:08:50 +1100287 "specification <%.100s>", file, linenum,
Damien Millerf91ee4c2005-03-01 21:24:33 +1100288 patterns);
Ben Lindstroma574cda2002-05-15 16:16:14 +0000289 auth_debug_add("%.100s, line %lu: "
Damien Millerf91ee4c2005-03-01 21:24:33 +1100290 "Bad permitopen specification", file,
291 linenum);
Ben Lindstrom7bb8b492001-03-17 00:47:54 +0000292 xfree(patterns);
293 goto bad_option;
294 }
Darren Tucker47eede72005-03-14 23:08:12 +1100295 host = cleanhostname(host);
Damien Millere37dde02009-01-28 16:33:01 +1100296 if (p == NULL || (port = a2port(p)) <= 0) {
Damien Millerf91ee4c2005-03-01 21:24:33 +1100297 debug("%.100s, line %lu: Bad permitopen port "
298 "<%.100s>", file, linenum, p ? p : "");
Ben Lindstroma574cda2002-05-15 16:16:14 +0000299 auth_debug_add("%.100s, line %lu: "
Ben Lindstromd71ba572001-09-12 18:03:31 +0000300 "Bad permitopen port", file, linenum);
Ben Lindstrom7bb8b492001-03-17 00:47:54 +0000301 xfree(patterns);
302 goto bad_option;
303 }
Ben Lindstrom2d70f982001-03-19 00:13:46 +0000304 if (options.allow_tcp_forwarding)
Ben Lindstromd71ba572001-09-12 18:03:31 +0000305 channel_add_permitted_opens(host, port);
Ben Lindstrom7bb8b492001-03-17 00:47:54 +0000306 xfree(patterns);
307 goto next_option;
308 }
Damien Millerd27b9472005-12-13 19:29:02 +1100309 cp = "tunnel=\"";
310 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
311 char *tun = NULL;
312 opts += strlen(cp);
313 tun = xmalloc(strlen(opts) + 1);
314 i = 0;
315 while (*opts) {
316 if (*opts == '"')
317 break;
318 tun[i++] = *opts++;
319 }
320 if (!*opts) {
321 debug("%.100s, line %lu: missing end quote",
322 file, linenum);
323 auth_debug_add("%.100s, line %lu: missing end quote",
324 file, linenum);
325 xfree(tun);
326 forced_tun_device = -1;
327 goto bad_option;
328 }
Damien Miller98299262006-07-24 14:01:43 +1000329 tun[i] = '\0';
Damien Millerd27b9472005-12-13 19:29:02 +1100330 forced_tun_device = a2tun(tun, NULL);
331 xfree(tun);
Damien Miller7b58e802005-12-13 19:33:19 +1100332 if (forced_tun_device == SSH_TUNID_ERR) {
Damien Millerd27b9472005-12-13 19:29:02 +1100333 debug("%.100s, line %lu: invalid tun device",
334 file, linenum);
335 auth_debug_add("%.100s, line %lu: invalid tun device",
336 file, linenum);
337 forced_tun_device = -1;
338 goto bad_option;
339 }
340 auth_debug_add("Forced tun device: %d", forced_tun_device);
341 opts++;
342 goto next_option;
343 }
Damien Millerf6d9e222000-06-18 14:50:44 +1000344next_option:
345 /*
346 * Skip the comma, and move to the next option
347 * (or break out if there are no more).
348 */
Damien Miller33804262001-02-04 23:20:18 +1100349 if (!*opts)
Damien Millerf6d9e222000-06-18 14:50:44 +1000350 fatal("Bugs in auth-options.c option processing.");
Damien Miller33804262001-02-04 23:20:18 +1100351 if (*opts == ' ' || *opts == '\t')
Damien Millerf6d9e222000-06-18 14:50:44 +1000352 break; /* End of options. */
Damien Miller33804262001-02-04 23:20:18 +1100353 if (*opts != ',')
Damien Millerf6d9e222000-06-18 14:50:44 +1000354 goto bad_option;
Damien Miller33804262001-02-04 23:20:18 +1100355 opts++;
Damien Millerf6d9e222000-06-18 14:50:44 +1000356 /* Process the next option. */
357 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000358
359 if (!use_privsep)
Ben Lindstroma574cda2002-05-15 16:16:14 +0000360 auth_debug_send();
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000361
Damien Millerf6d9e222000-06-18 14:50:44 +1000362 /* grant access */
363 return 1;
364
365bad_option:
Damien Miller996acd22003-04-09 20:59:48 +1000366 logit("Bad options in %.100s file, line %lu: %.50s",
Damien Miller33804262001-02-04 23:20:18 +1100367 file, linenum, opts);
Ben Lindstroma574cda2002-05-15 16:16:14 +0000368 auth_debug_add("Bad options in %.100s file, line %lu: %.50s",
Damien Miller33804262001-02-04 23:20:18 +1100369 file, linenum, opts);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000370
371 if (!use_privsep)
Ben Lindstroma574cda2002-05-15 16:16:14 +0000372 auth_debug_send();
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000373
Damien Millerf6d9e222000-06-18 14:50:44 +1000374 /* deny access */
375 return 0;
376}