blob: 473fb8bf7b2fe4d7b058e15e0f147f3c52a66880 [file] [log] [blame]
Damien Miller9f2abc42006-07-10 20:53:08 +10001/* $OpenBSD: auth-options.c,v 1.36 2006/07/06 16:03:53 stevesk 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
17#include <pwd.h>
18
Damien Millerf6d9e222000-06-18 14:50:44 +100019#include "xmalloc.h"
20#include "match.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000021#include "log.h"
22#include "canohost.h"
Ben Lindstromc7637672001-06-09 00:36:26 +000023#include "channels.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000024#include "auth-options.h"
Damien Miller33804262001-02-04 23:20:18 +110025#include "servconf.h"
Ben Lindstromd71ba572001-09-12 18:03:31 +000026#include "misc.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000027#include "monitor_wrap.h"
Ben Lindstroma574cda2002-05-15 16:16:14 +000028#include "auth.h"
Damien Millerf6d9e222000-06-18 14:50:44 +100029
30/* Flags set authorized_keys flags */
31int no_port_forwarding_flag = 0;
32int no_agent_forwarding_flag = 0;
33int no_x11_forwarding_flag = 0;
34int no_pty_flag = 0;
35
36/* "command=" option. */
37char *forced_command = NULL;
38
39/* "environment=" options. */
40struct envstring *custom_environment = NULL;
41
Damien Millerd27b9472005-12-13 19:29:02 +110042/* "tunnel=" option. */
43int forced_tun_device = -1;
44
Damien Miller33804262001-02-04 23:20:18 +110045extern ServerOptions options;
46
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000047void
Damien Miller874d77b2000-10-14 16:23:11 +110048auth_clear_options(void)
49{
50 no_agent_forwarding_flag = 0;
51 no_port_forwarding_flag = 0;
52 no_pty_flag = 0;
53 no_x11_forwarding_flag = 0;
54 while (custom_environment) {
55 struct envstring *ce = custom_environment;
56 custom_environment = ce->next;
57 xfree(ce->s);
58 xfree(ce);
59 }
60 if (forced_command) {
61 xfree(forced_command);
62 forced_command = NULL;
63 }
Damien Millerd27b9472005-12-13 19:29:02 +110064 forced_tun_device = -1;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +000065 channel_clear_permitted_opens();
Ben Lindstroma574cda2002-05-15 16:16:14 +000066 auth_debug_reset();
Damien Miller874d77b2000-10-14 16:23:11 +110067}
68
Ben Lindstrom226cfa02001-01-22 05:34:40 +000069/*
70 * return 1 if access is granted, 0 if not.
71 * side effect: sets key option flags
72 */
Damien Millerf6d9e222000-06-18 14:50:44 +100073int
Damien Miller33804262001-02-04 23:20:18 +110074auth_parse_options(struct passwd *pw, char *opts, char *file, u_long linenum)
Damien Millerf6d9e222000-06-18 14:50:44 +100075{
76 const char *cp;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +000077 int i;
Damien Miller874d77b2000-10-14 16:23:11 +110078
79 /* reset options */
80 auth_clear_options();
81
Ben Lindstrom36d7bd02001-02-10 22:27:19 +000082 if (!opts)
83 return 1;
84
Damien Miller33804262001-02-04 23:20:18 +110085 while (*opts && *opts != ' ' && *opts != '\t') {
Damien Millerf6d9e222000-06-18 14:50:44 +100086 cp = "no-port-forwarding";
Damien Miller33804262001-02-04 23:20:18 +110087 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
Ben Lindstroma574cda2002-05-15 16:16:14 +000088 auth_debug_add("Port forwarding disabled.");
Damien Millerf6d9e222000-06-18 14:50:44 +100089 no_port_forwarding_flag = 1;
Damien Miller33804262001-02-04 23:20:18 +110090 opts += strlen(cp);
Damien Millerf6d9e222000-06-18 14:50:44 +100091 goto next_option;
92 }
93 cp = "no-agent-forwarding";
Damien Miller33804262001-02-04 23:20:18 +110094 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
Ben Lindstroma574cda2002-05-15 16:16:14 +000095 auth_debug_add("Agent forwarding disabled.");
Damien Millerf6d9e222000-06-18 14:50:44 +100096 no_agent_forwarding_flag = 1;
Damien Miller33804262001-02-04 23:20:18 +110097 opts += strlen(cp);
Damien Millerf6d9e222000-06-18 14:50:44 +100098 goto next_option;
99 }
100 cp = "no-X11-forwarding";
Damien Miller33804262001-02-04 23:20:18 +1100101 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
Ben Lindstroma574cda2002-05-15 16:16:14 +0000102 auth_debug_add("X11 forwarding disabled.");
Damien Millerf6d9e222000-06-18 14:50:44 +1000103 no_x11_forwarding_flag = 1;
Damien Miller33804262001-02-04 23:20:18 +1100104 opts += strlen(cp);
Damien Millerf6d9e222000-06-18 14:50:44 +1000105 goto next_option;
106 }
107 cp = "no-pty";
Damien Miller33804262001-02-04 23:20:18 +1100108 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
Ben Lindstroma574cda2002-05-15 16:16:14 +0000109 auth_debug_add("Pty allocation disabled.");
Damien Millerf6d9e222000-06-18 14:50:44 +1000110 no_pty_flag = 1;
Damien Miller33804262001-02-04 23:20:18 +1100111 opts += strlen(cp);
Damien Millerf6d9e222000-06-18 14:50:44 +1000112 goto next_option;
113 }
114 cp = "command=\"";
Damien Miller33804262001-02-04 23:20:18 +1100115 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
Damien Miller33804262001-02-04 23:20:18 +1100116 opts += strlen(cp);
117 forced_command = xmalloc(strlen(opts) + 1);
Damien Millerf6d9e222000-06-18 14:50:44 +1000118 i = 0;
Damien Miller33804262001-02-04 23:20:18 +1100119 while (*opts) {
120 if (*opts == '"')
Damien Millerf6d9e222000-06-18 14:50:44 +1000121 break;
Damien Miller33804262001-02-04 23:20:18 +1100122 if (*opts == '\\' && opts[1] == '"') {
123 opts += 2;
Damien Millerf6d9e222000-06-18 14:50:44 +1000124 forced_command[i++] = '"';
125 continue;
126 }
Damien Miller33804262001-02-04 23:20:18 +1100127 forced_command[i++] = *opts++;
Damien Millerf6d9e222000-06-18 14:50:44 +1000128 }
Damien Miller33804262001-02-04 23:20:18 +1100129 if (!*opts) {
Damien Millerf6d9e222000-06-18 14:50:44 +1000130 debug("%.100s, line %lu: missing end quote",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000131 file, linenum);
Ben Lindstroma574cda2002-05-15 16:16:14 +0000132 auth_debug_add("%.100s, line %lu: missing end quote",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000133 file, linenum);
Damien Miller056ddf72001-03-14 10:15:20 +1100134 xfree(forced_command);
135 forced_command = NULL;
136 goto bad_option;
Damien Millerf6d9e222000-06-18 14:50:44 +1000137 }
138 forced_command[i] = 0;
Ben Lindstroma574cda2002-05-15 16:16:14 +0000139 auth_debug_add("Forced command: %.900s", forced_command);
Damien Miller33804262001-02-04 23:20:18 +1100140 opts++;
Damien Millerf6d9e222000-06-18 14:50:44 +1000141 goto next_option;
142 }
143 cp = "environment=\"";
Ben Lindstrom5d860f02002-08-01 01:28:38 +0000144 if (options.permit_user_env &&
145 strncasecmp(opts, cp, strlen(cp)) == 0) {
Damien Millerf6d9e222000-06-18 14:50:44 +1000146 char *s;
147 struct envstring *new_envstring;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +0000148
Damien Miller33804262001-02-04 23:20:18 +1100149 opts += strlen(cp);
150 s = xmalloc(strlen(opts) + 1);
Damien Millerf6d9e222000-06-18 14:50:44 +1000151 i = 0;
Damien Miller33804262001-02-04 23:20:18 +1100152 while (*opts) {
153 if (*opts == '"')
Damien Millerf6d9e222000-06-18 14:50:44 +1000154 break;
Damien Miller33804262001-02-04 23:20:18 +1100155 if (*opts == '\\' && opts[1] == '"') {
156 opts += 2;
Damien Millerf6d9e222000-06-18 14:50:44 +1000157 s[i++] = '"';
158 continue;
159 }
Damien Miller33804262001-02-04 23:20:18 +1100160 s[i++] = *opts++;
Damien Millerf6d9e222000-06-18 14:50:44 +1000161 }
Damien Miller33804262001-02-04 23:20:18 +1100162 if (!*opts) {
Damien Millerf6d9e222000-06-18 14:50:44 +1000163 debug("%.100s, line %lu: missing end quote",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000164 file, linenum);
Ben Lindstroma574cda2002-05-15 16:16:14 +0000165 auth_debug_add("%.100s, line %lu: missing end quote",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000166 file, linenum);
Damien Miller056ddf72001-03-14 10:15:20 +1100167 xfree(s);
168 goto bad_option;
Damien Millerf6d9e222000-06-18 14:50:44 +1000169 }
170 s[i] = 0;
Ben Lindstroma574cda2002-05-15 16:16:14 +0000171 auth_debug_add("Adding to environment: %.900s", s);
Damien Millerf6d9e222000-06-18 14:50:44 +1000172 debug("Adding to environment: %.900s", s);
Damien Miller33804262001-02-04 23:20:18 +1100173 opts++;
Damien Millerf6d9e222000-06-18 14:50:44 +1000174 new_envstring = xmalloc(sizeof(struct envstring));
175 new_envstring->s = s;
176 new_envstring->next = custom_environment;
177 custom_environment = new_envstring;
178 goto next_option;
179 }
180 cp = "from=\"";
Damien Miller33804262001-02-04 23:20:18 +1100181 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
Damien Miller33804262001-02-04 23:20:18 +1100182 const char *remote_ip = get_remote_ipaddr();
183 const char *remote_host = get_canonical_hostname(
Damien Miller3a961dc2003-06-03 10:25:48 +1000184 options.use_dns);
Damien Miller33804262001-02-04 23:20:18 +1100185 char *patterns = xmalloc(strlen(opts) + 1);
Ben Lindstrom7bb8b492001-03-17 00:47:54 +0000186
Damien Miller33804262001-02-04 23:20:18 +1100187 opts += strlen(cp);
Damien Millerf6d9e222000-06-18 14:50:44 +1000188 i = 0;
Damien Miller33804262001-02-04 23:20:18 +1100189 while (*opts) {
190 if (*opts == '"')
Damien Millerf6d9e222000-06-18 14:50:44 +1000191 break;
Damien Miller33804262001-02-04 23:20:18 +1100192 if (*opts == '\\' && opts[1] == '"') {
193 opts += 2;
Damien Millerf6d9e222000-06-18 14:50:44 +1000194 patterns[i++] = '"';
195 continue;
196 }
Damien Miller33804262001-02-04 23:20:18 +1100197 patterns[i++] = *opts++;
Damien Millerf6d9e222000-06-18 14:50:44 +1000198 }
Damien Miller33804262001-02-04 23:20:18 +1100199 if (!*opts) {
Damien Millerf6d9e222000-06-18 14:50:44 +1000200 debug("%.100s, line %lu: missing end quote",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000201 file, linenum);
Ben Lindstroma574cda2002-05-15 16:16:14 +0000202 auth_debug_add("%.100s, line %lu: missing end quote",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000203 file, linenum);
Damien Miller056ddf72001-03-14 10:15:20 +1100204 xfree(patterns);
205 goto bad_option;
Damien Millerf6d9e222000-06-18 14:50:44 +1000206 }
207 patterns[i] = 0;
Damien Miller33804262001-02-04 23:20:18 +1100208 opts++;
Ben Lindstromf0c50292001-06-25 05:17:53 +0000209 if (match_host_and_ip(remote_host, remote_ip,
210 patterns) != 1) {
211 xfree(patterns);
Damien Miller996acd22003-04-09 20:59:48 +1000212 logit("Authentication tried for %.100s with "
Damien Miller33804262001-02-04 23:20:18 +1100213 "correct key but not from a permitted "
214 "host (host=%.200s, ip=%.200s).",
215 pw->pw_name, remote_host, remote_ip);
Ben Lindstroma574cda2002-05-15 16:16:14 +0000216 auth_debug_add("Your host '%.200s' is not "
Damien Miller33804262001-02-04 23:20:18 +1100217 "permitted to use this key for login.",
218 remote_host);
Damien Millerf6d9e222000-06-18 14:50:44 +1000219 /* deny access */
220 return 0;
221 }
Ben Lindstromf0c50292001-06-25 05:17:53 +0000222 xfree(patterns);
Damien Millerf6d9e222000-06-18 14:50:44 +1000223 /* Host name matches. */
224 goto next_option;
225 }
Ben Lindstrom7bb8b492001-03-17 00:47:54 +0000226 cp = "permitopen=\"";
227 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
Damien Millerf91ee4c2005-03-01 21:24:33 +1100228 char *host, *p;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +0000229 u_short port;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +0000230 char *patterns = xmalloc(strlen(opts) + 1);
231
232 opts += strlen(cp);
233 i = 0;
234 while (*opts) {
235 if (*opts == '"')
236 break;
237 if (*opts == '\\' && opts[1] == '"') {
238 opts += 2;
239 patterns[i++] = '"';
240 continue;
241 }
242 patterns[i++] = *opts++;
243 }
244 if (!*opts) {
245 debug("%.100s, line %lu: missing end quote",
246 file, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100247 auth_debug_add("%.100s, line %lu: missing "
248 "end quote", file, linenum);
Ben Lindstrom7bb8b492001-03-17 00:47:54 +0000249 xfree(patterns);
250 goto bad_option;
251 }
252 patterns[i] = 0;
253 opts++;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100254 p = patterns;
255 host = hpdelim(&p);
256 if (host == NULL || strlen(host) >= NI_MAXHOST) {
257 debug("%.100s, line %lu: Bad permitopen "
Darren Tucker90b9e022005-03-14 23:08:50 +1100258 "specification <%.100s>", file, linenum,
Damien Millerf91ee4c2005-03-01 21:24:33 +1100259 patterns);
Ben Lindstroma574cda2002-05-15 16:16:14 +0000260 auth_debug_add("%.100s, line %lu: "
Damien Millerf91ee4c2005-03-01 21:24:33 +1100261 "Bad permitopen specification", file,
262 linenum);
Ben Lindstrom7bb8b492001-03-17 00:47:54 +0000263 xfree(patterns);
264 goto bad_option;
265 }
Darren Tucker47eede72005-03-14 23:08:12 +1100266 host = cleanhostname(host);
267 if (p == NULL || (port = a2port(p)) == 0) {
Damien Millerf91ee4c2005-03-01 21:24:33 +1100268 debug("%.100s, line %lu: Bad permitopen port "
269 "<%.100s>", file, linenum, p ? p : "");
Ben Lindstroma574cda2002-05-15 16:16:14 +0000270 auth_debug_add("%.100s, line %lu: "
Ben Lindstromd71ba572001-09-12 18:03:31 +0000271 "Bad permitopen port", file, linenum);
Ben Lindstrom7bb8b492001-03-17 00:47:54 +0000272 xfree(patterns);
273 goto bad_option;
274 }
Ben Lindstrom2d70f982001-03-19 00:13:46 +0000275 if (options.allow_tcp_forwarding)
Ben Lindstromd71ba572001-09-12 18:03:31 +0000276 channel_add_permitted_opens(host, port);
Ben Lindstrom7bb8b492001-03-17 00:47:54 +0000277 xfree(patterns);
278 goto next_option;
279 }
Damien Millerd27b9472005-12-13 19:29:02 +1100280 cp = "tunnel=\"";
281 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
282 char *tun = NULL;
283 opts += strlen(cp);
284 tun = xmalloc(strlen(opts) + 1);
285 i = 0;
286 while (*opts) {
287 if (*opts == '"')
288 break;
289 tun[i++] = *opts++;
290 }
291 if (!*opts) {
292 debug("%.100s, line %lu: missing end quote",
293 file, linenum);
294 auth_debug_add("%.100s, line %lu: missing end quote",
295 file, linenum);
296 xfree(tun);
297 forced_tun_device = -1;
298 goto bad_option;
299 }
300 tun[i] = 0;
301 forced_tun_device = a2tun(tun, NULL);
302 xfree(tun);
Damien Miller7b58e802005-12-13 19:33:19 +1100303 if (forced_tun_device == SSH_TUNID_ERR) {
Damien Millerd27b9472005-12-13 19:29:02 +1100304 debug("%.100s, line %lu: invalid tun device",
305 file, linenum);
306 auth_debug_add("%.100s, line %lu: invalid tun device",
307 file, linenum);
308 forced_tun_device = -1;
309 goto bad_option;
310 }
311 auth_debug_add("Forced tun device: %d", forced_tun_device);
312 opts++;
313 goto next_option;
314 }
Damien Millerf6d9e222000-06-18 14:50:44 +1000315next_option:
316 /*
317 * Skip the comma, and move to the next option
318 * (or break out if there are no more).
319 */
Damien Miller33804262001-02-04 23:20:18 +1100320 if (!*opts)
Damien Millerf6d9e222000-06-18 14:50:44 +1000321 fatal("Bugs in auth-options.c option processing.");
Damien Miller33804262001-02-04 23:20:18 +1100322 if (*opts == ' ' || *opts == '\t')
Damien Millerf6d9e222000-06-18 14:50:44 +1000323 break; /* End of options. */
Damien Miller33804262001-02-04 23:20:18 +1100324 if (*opts != ',')
Damien Millerf6d9e222000-06-18 14:50:44 +1000325 goto bad_option;
Damien Miller33804262001-02-04 23:20:18 +1100326 opts++;
Damien Millerf6d9e222000-06-18 14:50:44 +1000327 /* Process the next option. */
328 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000329
330 if (!use_privsep)
Ben Lindstroma574cda2002-05-15 16:16:14 +0000331 auth_debug_send();
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000332
Damien Millerf6d9e222000-06-18 14:50:44 +1000333 /* grant access */
334 return 1;
335
336bad_option:
Damien Miller996acd22003-04-09 20:59:48 +1000337 logit("Bad options in %.100s file, line %lu: %.50s",
Damien Miller33804262001-02-04 23:20:18 +1100338 file, linenum, opts);
Ben Lindstroma574cda2002-05-15 16:16:14 +0000339 auth_debug_add("Bad options in %.100s file, line %lu: %.50s",
Damien Miller33804262001-02-04 23:20:18 +1100340 file, linenum, opts);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000341
342 if (!use_privsep)
Ben Lindstroma574cda2002-05-15 16:16:14 +0000343 auth_debug_send();
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000344
Damien Millerf6d9e222000-06-18 14:50:44 +1000345 /* deny access */
346 return 0;
347}