blob: a85e408359eeb96d866e5ea57c4780ffb25f9213 [file] [log] [blame]
Damien Millere4340be2000-09-16 13:29:08 +11001/*
2 * Author: Tatu Ylonen <ylo@cs.hut.fi>
3 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
4 * All rights reserved
Damien Millere4340be2000-09-16 13:29:08 +11005 * As far as I am concerned, the code I have written for this software
6 * can be used freely for any purpose. Any derived versions of this
7 * software must be clearly marked as such, and if the derived work is
8 * incompatible with the protocol description in the RFC file, it must be
9 * called by a name other than "ssh" or "Secure Shell".
10 */
11
Damien Millerf6d9e222000-06-18 14:50:44 +100012#include "includes.h"
Darren Tucker90b9e022005-03-14 23:08:50 +110013RCSID("$OpenBSD: auth-options.c,v 1.31 2005/03/10 22:40:38 deraadt Exp $");
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 Miller33804262001-02-04 23:20:18 +110038extern ServerOptions options;
39
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000040void
Damien Miller874d77b2000-10-14 16:23:11 +110041auth_clear_options(void)
42{
43 no_agent_forwarding_flag = 0;
44 no_port_forwarding_flag = 0;
45 no_pty_flag = 0;
46 no_x11_forwarding_flag = 0;
47 while (custom_environment) {
48 struct envstring *ce = custom_environment;
49 custom_environment = ce->next;
50 xfree(ce->s);
51 xfree(ce);
52 }
53 if (forced_command) {
54 xfree(forced_command);
55 forced_command = NULL;
56 }
Ben Lindstrom7bb8b492001-03-17 00:47:54 +000057 channel_clear_permitted_opens();
Ben Lindstroma574cda2002-05-15 16:16:14 +000058 auth_debug_reset();
Damien Miller874d77b2000-10-14 16:23:11 +110059}
60
Ben Lindstrom226cfa02001-01-22 05:34:40 +000061/*
62 * return 1 if access is granted, 0 if not.
63 * side effect: sets key option flags
64 */
Damien Millerf6d9e222000-06-18 14:50:44 +100065int
Damien Miller33804262001-02-04 23:20:18 +110066auth_parse_options(struct passwd *pw, char *opts, char *file, u_long linenum)
Damien Millerf6d9e222000-06-18 14:50:44 +100067{
68 const char *cp;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +000069 int i;
Damien Miller874d77b2000-10-14 16:23:11 +110070
71 /* reset options */
72 auth_clear_options();
73
Ben Lindstrom36d7bd02001-02-10 22:27:19 +000074 if (!opts)
75 return 1;
76
Damien Miller33804262001-02-04 23:20:18 +110077 while (*opts && *opts != ' ' && *opts != '\t') {
Damien Millerf6d9e222000-06-18 14:50:44 +100078 cp = "no-port-forwarding";
Damien Miller33804262001-02-04 23:20:18 +110079 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
Ben Lindstroma574cda2002-05-15 16:16:14 +000080 auth_debug_add("Port forwarding disabled.");
Damien Millerf6d9e222000-06-18 14:50:44 +100081 no_port_forwarding_flag = 1;
Damien Miller33804262001-02-04 23:20:18 +110082 opts += strlen(cp);
Damien Millerf6d9e222000-06-18 14:50:44 +100083 goto next_option;
84 }
85 cp = "no-agent-forwarding";
Damien Miller33804262001-02-04 23:20:18 +110086 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
Ben Lindstroma574cda2002-05-15 16:16:14 +000087 auth_debug_add("Agent forwarding disabled.");
Damien Millerf6d9e222000-06-18 14:50:44 +100088 no_agent_forwarding_flag = 1;
Damien Miller33804262001-02-04 23:20:18 +110089 opts += strlen(cp);
Damien Millerf6d9e222000-06-18 14:50:44 +100090 goto next_option;
91 }
92 cp = "no-X11-forwarding";
Damien Miller33804262001-02-04 23:20:18 +110093 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
Ben Lindstroma574cda2002-05-15 16:16:14 +000094 auth_debug_add("X11 forwarding disabled.");
Damien Millerf6d9e222000-06-18 14:50:44 +100095 no_x11_forwarding_flag = 1;
Damien Miller33804262001-02-04 23:20:18 +110096 opts += strlen(cp);
Damien Millerf6d9e222000-06-18 14:50:44 +100097 goto next_option;
98 }
99 cp = "no-pty";
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("Pty allocation disabled.");
Damien Millerf6d9e222000-06-18 14:50:44 +1000102 no_pty_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 = "command=\"";
Damien Miller33804262001-02-04 23:20:18 +1100107 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
Damien Miller33804262001-02-04 23:20:18 +1100108 opts += strlen(cp);
109 forced_command = xmalloc(strlen(opts) + 1);
Damien Millerf6d9e222000-06-18 14:50:44 +1000110 i = 0;
Damien Miller33804262001-02-04 23:20:18 +1100111 while (*opts) {
112 if (*opts == '"')
Damien Millerf6d9e222000-06-18 14:50:44 +1000113 break;
Damien Miller33804262001-02-04 23:20:18 +1100114 if (*opts == '\\' && opts[1] == '"') {
115 opts += 2;
Damien Millerf6d9e222000-06-18 14:50:44 +1000116 forced_command[i++] = '"';
117 continue;
118 }
Damien Miller33804262001-02-04 23:20:18 +1100119 forced_command[i++] = *opts++;
Damien Millerf6d9e222000-06-18 14:50:44 +1000120 }
Damien Miller33804262001-02-04 23:20:18 +1100121 if (!*opts) {
Damien Millerf6d9e222000-06-18 14:50:44 +1000122 debug("%.100s, line %lu: missing end quote",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000123 file, linenum);
Ben Lindstroma574cda2002-05-15 16:16:14 +0000124 auth_debug_add("%.100s, line %lu: missing end quote",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000125 file, linenum);
Damien Miller056ddf72001-03-14 10:15:20 +1100126 xfree(forced_command);
127 forced_command = NULL;
128 goto bad_option;
Damien Millerf6d9e222000-06-18 14:50:44 +1000129 }
130 forced_command[i] = 0;
Ben Lindstroma574cda2002-05-15 16:16:14 +0000131 auth_debug_add("Forced command: %.900s", forced_command);
Damien Miller33804262001-02-04 23:20:18 +1100132 opts++;
Damien Millerf6d9e222000-06-18 14:50:44 +1000133 goto next_option;
134 }
135 cp = "environment=\"";
Ben Lindstrom5d860f02002-08-01 01:28:38 +0000136 if (options.permit_user_env &&
137 strncasecmp(opts, cp, strlen(cp)) == 0) {
Damien Millerf6d9e222000-06-18 14:50:44 +1000138 char *s;
139 struct envstring *new_envstring;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +0000140
Damien Miller33804262001-02-04 23:20:18 +1100141 opts += strlen(cp);
142 s = xmalloc(strlen(opts) + 1);
Damien Millerf6d9e222000-06-18 14:50:44 +1000143 i = 0;
Damien Miller33804262001-02-04 23:20:18 +1100144 while (*opts) {
145 if (*opts == '"')
Damien Millerf6d9e222000-06-18 14:50:44 +1000146 break;
Damien Miller33804262001-02-04 23:20:18 +1100147 if (*opts == '\\' && opts[1] == '"') {
148 opts += 2;
Damien Millerf6d9e222000-06-18 14:50:44 +1000149 s[i++] = '"';
150 continue;
151 }
Damien Miller33804262001-02-04 23:20:18 +1100152 s[i++] = *opts++;
Damien Millerf6d9e222000-06-18 14:50:44 +1000153 }
Damien Miller33804262001-02-04 23:20:18 +1100154 if (!*opts) {
Damien Millerf6d9e222000-06-18 14:50:44 +1000155 debug("%.100s, line %lu: missing end quote",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000156 file, linenum);
Ben Lindstroma574cda2002-05-15 16:16:14 +0000157 auth_debug_add("%.100s, line %lu: missing end quote",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000158 file, linenum);
Damien Miller056ddf72001-03-14 10:15:20 +1100159 xfree(s);
160 goto bad_option;
Damien Millerf6d9e222000-06-18 14:50:44 +1000161 }
162 s[i] = 0;
Ben Lindstroma574cda2002-05-15 16:16:14 +0000163 auth_debug_add("Adding to environment: %.900s", s);
Damien Millerf6d9e222000-06-18 14:50:44 +1000164 debug("Adding to environment: %.900s", s);
Damien Miller33804262001-02-04 23:20:18 +1100165 opts++;
Damien Millerf6d9e222000-06-18 14:50:44 +1000166 new_envstring = xmalloc(sizeof(struct envstring));
167 new_envstring->s = s;
168 new_envstring->next = custom_environment;
169 custom_environment = new_envstring;
170 goto next_option;
171 }
172 cp = "from=\"";
Damien Miller33804262001-02-04 23:20:18 +1100173 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
Damien Miller33804262001-02-04 23:20:18 +1100174 const char *remote_ip = get_remote_ipaddr();
175 const char *remote_host = get_canonical_hostname(
Damien Miller3a961dc2003-06-03 10:25:48 +1000176 options.use_dns);
Damien Miller33804262001-02-04 23:20:18 +1100177 char *patterns = xmalloc(strlen(opts) + 1);
Ben Lindstrom7bb8b492001-03-17 00:47:54 +0000178
Damien Miller33804262001-02-04 23:20:18 +1100179 opts += strlen(cp);
Damien Millerf6d9e222000-06-18 14:50:44 +1000180 i = 0;
Damien Miller33804262001-02-04 23:20:18 +1100181 while (*opts) {
182 if (*opts == '"')
Damien Millerf6d9e222000-06-18 14:50:44 +1000183 break;
Damien Miller33804262001-02-04 23:20:18 +1100184 if (*opts == '\\' && opts[1] == '"') {
185 opts += 2;
Damien Millerf6d9e222000-06-18 14:50:44 +1000186 patterns[i++] = '"';
187 continue;
188 }
Damien Miller33804262001-02-04 23:20:18 +1100189 patterns[i++] = *opts++;
Damien Millerf6d9e222000-06-18 14:50:44 +1000190 }
Damien Miller33804262001-02-04 23:20:18 +1100191 if (!*opts) {
Damien Millerf6d9e222000-06-18 14:50:44 +1000192 debug("%.100s, line %lu: missing end quote",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000193 file, linenum);
Ben Lindstroma574cda2002-05-15 16:16:14 +0000194 auth_debug_add("%.100s, line %lu: missing end quote",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000195 file, linenum);
Damien Miller056ddf72001-03-14 10:15:20 +1100196 xfree(patterns);
197 goto bad_option;
Damien Millerf6d9e222000-06-18 14:50:44 +1000198 }
199 patterns[i] = 0;
Damien Miller33804262001-02-04 23:20:18 +1100200 opts++;
Ben Lindstromf0c50292001-06-25 05:17:53 +0000201 if (match_host_and_ip(remote_host, remote_ip,
202 patterns) != 1) {
203 xfree(patterns);
Damien Miller996acd22003-04-09 20:59:48 +1000204 logit("Authentication tried for %.100s with "
Damien Miller33804262001-02-04 23:20:18 +1100205 "correct key but not from a permitted "
206 "host (host=%.200s, ip=%.200s).",
207 pw->pw_name, remote_host, remote_ip);
Ben Lindstroma574cda2002-05-15 16:16:14 +0000208 auth_debug_add("Your host '%.200s' is not "
Damien Miller33804262001-02-04 23:20:18 +1100209 "permitted to use this key for login.",
210 remote_host);
Damien Millerf6d9e222000-06-18 14:50:44 +1000211 /* deny access */
212 return 0;
213 }
Ben Lindstromf0c50292001-06-25 05:17:53 +0000214 xfree(patterns);
Damien Millerf6d9e222000-06-18 14:50:44 +1000215 /* Host name matches. */
216 goto next_option;
217 }
Ben Lindstrom7bb8b492001-03-17 00:47:54 +0000218 cp = "permitopen=\"";
219 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
Damien Millerf91ee4c2005-03-01 21:24:33 +1100220 char *host, *p;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +0000221 u_short port;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +0000222 char *patterns = xmalloc(strlen(opts) + 1);
223
224 opts += strlen(cp);
225 i = 0;
226 while (*opts) {
227 if (*opts == '"')
228 break;
229 if (*opts == '\\' && opts[1] == '"') {
230 opts += 2;
231 patterns[i++] = '"';
232 continue;
233 }
234 patterns[i++] = *opts++;
235 }
236 if (!*opts) {
237 debug("%.100s, line %lu: missing end quote",
238 file, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100239 auth_debug_add("%.100s, line %lu: missing "
240 "end quote", file, linenum);
Ben Lindstrom7bb8b492001-03-17 00:47:54 +0000241 xfree(patterns);
242 goto bad_option;
243 }
244 patterns[i] = 0;
245 opts++;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100246 p = patterns;
247 host = hpdelim(&p);
248 if (host == NULL || strlen(host) >= NI_MAXHOST) {
249 debug("%.100s, line %lu: Bad permitopen "
Darren Tucker90b9e022005-03-14 23:08:50 +1100250 "specification <%.100s>", file, linenum,
Damien Millerf91ee4c2005-03-01 21:24:33 +1100251 patterns);
Ben Lindstroma574cda2002-05-15 16:16:14 +0000252 auth_debug_add("%.100s, line %lu: "
Damien Millerf91ee4c2005-03-01 21:24:33 +1100253 "Bad permitopen specification", file,
254 linenum);
Ben Lindstrom7bb8b492001-03-17 00:47:54 +0000255 xfree(patterns);
256 goto bad_option;
257 }
Darren Tucker47eede72005-03-14 23:08:12 +1100258 host = cleanhostname(host);
259 if (p == NULL || (port = a2port(p)) == 0) {
Damien Millerf91ee4c2005-03-01 21:24:33 +1100260 debug("%.100s, line %lu: Bad permitopen port "
261 "<%.100s>", file, linenum, p ? p : "");
Ben Lindstroma574cda2002-05-15 16:16:14 +0000262 auth_debug_add("%.100s, line %lu: "
Ben Lindstromd71ba572001-09-12 18:03:31 +0000263 "Bad permitopen port", file, linenum);
Ben Lindstrom7bb8b492001-03-17 00:47:54 +0000264 xfree(patterns);
265 goto bad_option;
266 }
Ben Lindstrom2d70f982001-03-19 00:13:46 +0000267 if (options.allow_tcp_forwarding)
Ben Lindstromd71ba572001-09-12 18:03:31 +0000268 channel_add_permitted_opens(host, port);
Ben Lindstrom7bb8b492001-03-17 00:47:54 +0000269 xfree(patterns);
270 goto next_option;
271 }
Damien Millerf6d9e222000-06-18 14:50:44 +1000272next_option:
273 /*
274 * Skip the comma, and move to the next option
275 * (or break out if there are no more).
276 */
Damien Miller33804262001-02-04 23:20:18 +1100277 if (!*opts)
Damien Millerf6d9e222000-06-18 14:50:44 +1000278 fatal("Bugs in auth-options.c option processing.");
Damien Miller33804262001-02-04 23:20:18 +1100279 if (*opts == ' ' || *opts == '\t')
Damien Millerf6d9e222000-06-18 14:50:44 +1000280 break; /* End of options. */
Damien Miller33804262001-02-04 23:20:18 +1100281 if (*opts != ',')
Damien Millerf6d9e222000-06-18 14:50:44 +1000282 goto bad_option;
Damien Miller33804262001-02-04 23:20:18 +1100283 opts++;
Damien Millerf6d9e222000-06-18 14:50:44 +1000284 /* Process the next option. */
285 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000286
287 if (!use_privsep)
Ben Lindstroma574cda2002-05-15 16:16:14 +0000288 auth_debug_send();
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000289
Damien Millerf6d9e222000-06-18 14:50:44 +1000290 /* grant access */
291 return 1;
292
293bad_option:
Damien Miller996acd22003-04-09 20:59:48 +1000294 logit("Bad options in %.100s file, line %lu: %.50s",
Damien Miller33804262001-02-04 23:20:18 +1100295 file, linenum, opts);
Ben Lindstroma574cda2002-05-15 16:16:14 +0000296 auth_debug_add("Bad options in %.100s file, line %lu: %.50s",
Damien Miller33804262001-02-04 23:20:18 +1100297 file, linenum, opts);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000298
299 if (!use_privsep)
Ben Lindstroma574cda2002-05-15 16:16:14 +0000300 auth_debug_send();
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000301
Damien Millerf6d9e222000-06-18 14:50:44 +1000302 /* deny access */
303 return 0;
304}