blob: 3a6c3c0f35e48d8283654c26d2256b7173791888 [file] [log] [blame]
Damien Millerb84886b2008-05-19 15:05:07 +10001/* $OpenBSD: auth-options.c,v 1.42 2008/05/08 12:02:23 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++;
Ben Lindstromf0c50292001-06-25 05:17:53 +0000229 if (match_host_and_ip(remote_host, remote_ip,
230 patterns) != 1) {
231 xfree(patterns);
Damien Miller996acd22003-04-09 20:59:48 +1000232 logit("Authentication tried for %.100s with "
Damien Miller33804262001-02-04 23:20:18 +1100233 "correct key but not from a permitted "
234 "host (host=%.200s, ip=%.200s).",
235 pw->pw_name, remote_host, remote_ip);
Ben Lindstroma574cda2002-05-15 16:16:14 +0000236 auth_debug_add("Your host '%.200s' is not "
Damien Miller33804262001-02-04 23:20:18 +1100237 "permitted to use this key for login.",
238 remote_host);
Damien Millerf6d9e222000-06-18 14:50:44 +1000239 /* deny access */
240 return 0;
241 }
Ben Lindstromf0c50292001-06-25 05:17:53 +0000242 xfree(patterns);
Damien Millerf6d9e222000-06-18 14:50:44 +1000243 /* Host name matches. */
244 goto next_option;
245 }
Ben Lindstrom7bb8b492001-03-17 00:47:54 +0000246 cp = "permitopen=\"";
247 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
Damien Millerf91ee4c2005-03-01 21:24:33 +1100248 char *host, *p;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +0000249 u_short port;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +0000250 char *patterns = xmalloc(strlen(opts) + 1);
251
252 opts += strlen(cp);
253 i = 0;
254 while (*opts) {
255 if (*opts == '"')
256 break;
257 if (*opts == '\\' && opts[1] == '"') {
258 opts += 2;
259 patterns[i++] = '"';
260 continue;
261 }
262 patterns[i++] = *opts++;
263 }
264 if (!*opts) {
265 debug("%.100s, line %lu: missing end quote",
266 file, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100267 auth_debug_add("%.100s, line %lu: missing "
268 "end quote", file, linenum);
Ben Lindstrom7bb8b492001-03-17 00:47:54 +0000269 xfree(patterns);
270 goto bad_option;
271 }
Damien Miller98299262006-07-24 14:01:43 +1000272 patterns[i] = '\0';
Ben Lindstrom7bb8b492001-03-17 00:47:54 +0000273 opts++;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100274 p = patterns;
275 host = hpdelim(&p);
276 if (host == NULL || strlen(host) >= NI_MAXHOST) {
277 debug("%.100s, line %lu: Bad permitopen "
Darren Tucker90b9e022005-03-14 23:08:50 +1100278 "specification <%.100s>", file, linenum,
Damien Millerf91ee4c2005-03-01 21:24:33 +1100279 patterns);
Ben Lindstroma574cda2002-05-15 16:16:14 +0000280 auth_debug_add("%.100s, line %lu: "
Damien Millerf91ee4c2005-03-01 21:24:33 +1100281 "Bad permitopen specification", file,
282 linenum);
Ben Lindstrom7bb8b492001-03-17 00:47:54 +0000283 xfree(patterns);
284 goto bad_option;
285 }
Darren Tucker47eede72005-03-14 23:08:12 +1100286 host = cleanhostname(host);
287 if (p == NULL || (port = a2port(p)) == 0) {
Damien Millerf91ee4c2005-03-01 21:24:33 +1100288 debug("%.100s, line %lu: Bad permitopen port "
289 "<%.100s>", file, linenum, p ? p : "");
Ben Lindstroma574cda2002-05-15 16:16:14 +0000290 auth_debug_add("%.100s, line %lu: "
Ben Lindstromd71ba572001-09-12 18:03:31 +0000291 "Bad permitopen port", file, linenum);
Ben Lindstrom7bb8b492001-03-17 00:47:54 +0000292 xfree(patterns);
293 goto bad_option;
294 }
Ben Lindstrom2d70f982001-03-19 00:13:46 +0000295 if (options.allow_tcp_forwarding)
Ben Lindstromd71ba572001-09-12 18:03:31 +0000296 channel_add_permitted_opens(host, port);
Ben Lindstrom7bb8b492001-03-17 00:47:54 +0000297 xfree(patterns);
298 goto next_option;
299 }
Damien Millerd27b9472005-12-13 19:29:02 +1100300 cp = "tunnel=\"";
301 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
302 char *tun = NULL;
303 opts += strlen(cp);
304 tun = xmalloc(strlen(opts) + 1);
305 i = 0;
306 while (*opts) {
307 if (*opts == '"')
308 break;
309 tun[i++] = *opts++;
310 }
311 if (!*opts) {
312 debug("%.100s, line %lu: missing end quote",
313 file, linenum);
314 auth_debug_add("%.100s, line %lu: missing end quote",
315 file, linenum);
316 xfree(tun);
317 forced_tun_device = -1;
318 goto bad_option;
319 }
Damien Miller98299262006-07-24 14:01:43 +1000320 tun[i] = '\0';
Damien Millerd27b9472005-12-13 19:29:02 +1100321 forced_tun_device = a2tun(tun, NULL);
322 xfree(tun);
Damien Miller7b58e802005-12-13 19:33:19 +1100323 if (forced_tun_device == SSH_TUNID_ERR) {
Damien Millerd27b9472005-12-13 19:29:02 +1100324 debug("%.100s, line %lu: invalid tun device",
325 file, linenum);
326 auth_debug_add("%.100s, line %lu: invalid tun device",
327 file, linenum);
328 forced_tun_device = -1;
329 goto bad_option;
330 }
331 auth_debug_add("Forced tun device: %d", forced_tun_device);
332 opts++;
333 goto next_option;
334 }
Damien Millerf6d9e222000-06-18 14:50:44 +1000335next_option:
336 /*
337 * Skip the comma, and move to the next option
338 * (or break out if there are no more).
339 */
Damien Miller33804262001-02-04 23:20:18 +1100340 if (!*opts)
Damien Millerf6d9e222000-06-18 14:50:44 +1000341 fatal("Bugs in auth-options.c option processing.");
Damien Miller33804262001-02-04 23:20:18 +1100342 if (*opts == ' ' || *opts == '\t')
Damien Millerf6d9e222000-06-18 14:50:44 +1000343 break; /* End of options. */
Damien Miller33804262001-02-04 23:20:18 +1100344 if (*opts != ',')
Damien Millerf6d9e222000-06-18 14:50:44 +1000345 goto bad_option;
Damien Miller33804262001-02-04 23:20:18 +1100346 opts++;
Damien Millerf6d9e222000-06-18 14:50:44 +1000347 /* Process the next option. */
348 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000349
350 if (!use_privsep)
Ben Lindstroma574cda2002-05-15 16:16:14 +0000351 auth_debug_send();
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000352
Damien Millerf6d9e222000-06-18 14:50:44 +1000353 /* grant access */
354 return 1;
355
356bad_option:
Damien Miller996acd22003-04-09 20:59:48 +1000357 logit("Bad options in %.100s file, line %lu: %.50s",
Damien Miller33804262001-02-04 23:20:18 +1100358 file, linenum, opts);
Ben Lindstroma574cda2002-05-15 16:16:14 +0000359 auth_debug_add("Bad options in %.100s file, line %lu: %.50s",
Damien Miller33804262001-02-04 23:20:18 +1100360 file, linenum, opts);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000361
362 if (!use_privsep)
Ben Lindstroma574cda2002-05-15 16:16:14 +0000363 auth_debug_send();
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000364
Damien Millerf6d9e222000-06-18 14:50:44 +1000365 /* deny access */
366 return 0;
367}