blob: 2787d2948b5062e5c0dac0bf1742d9ca3451a8da [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"
Ben Lindstroma574cda2002-05-15 16:16:14 +000013RCSID("$OpenBSD: auth-options.c,v 1.24 2002/05/13 20:44:58 markus Exp $");
Damien Millerf6d9e222000-06-18 14:50:44 +100014
Damien Millerf6d9e222000-06-18 14:50:44 +100015#include "packet.h"
16#include "xmalloc.h"
17#include "match.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000018#include "log.h"
19#include "canohost.h"
Ben Lindstromc7637672001-06-09 00:36:26 +000020#include "channels.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000021#include "auth-options.h"
Damien Miller33804262001-02-04 23:20:18 +110022#include "servconf.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000023#include "bufaux.h"
Ben Lindstromd71ba572001-09-12 18:03:31 +000024#include "misc.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000025#include "monitor_wrap.h"
Ben Lindstroma574cda2002-05-15 16:16:14 +000026#include "auth.h"
Damien Millerf6d9e222000-06-18 14:50:44 +100027
28/* Flags set authorized_keys flags */
29int no_port_forwarding_flag = 0;
30int no_agent_forwarding_flag = 0;
31int no_x11_forwarding_flag = 0;
32int no_pty_flag = 0;
33
34/* "command=" option. */
35char *forced_command = NULL;
36
37/* "environment=" options. */
38struct envstring *custom_environment = NULL;
39
Damien Miller33804262001-02-04 23:20:18 +110040extern ServerOptions options;
41
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000042void
Damien Miller874d77b2000-10-14 16:23:11 +110043auth_clear_options(void)
44{
45 no_agent_forwarding_flag = 0;
46 no_port_forwarding_flag = 0;
47 no_pty_flag = 0;
48 no_x11_forwarding_flag = 0;
49 while (custom_environment) {
50 struct envstring *ce = custom_environment;
51 custom_environment = ce->next;
52 xfree(ce->s);
53 xfree(ce);
54 }
55 if (forced_command) {
56 xfree(forced_command);
57 forced_command = NULL;
58 }
Ben Lindstrom7bb8b492001-03-17 00:47:54 +000059 channel_clear_permitted_opens();
Ben Lindstroma574cda2002-05-15 16:16:14 +000060 auth_debug_reset();
Damien Miller874d77b2000-10-14 16:23:11 +110061}
62
Ben Lindstrom226cfa02001-01-22 05:34:40 +000063/*
64 * return 1 if access is granted, 0 if not.
65 * side effect: sets key option flags
66 */
Damien Millerf6d9e222000-06-18 14:50:44 +100067int
Damien Miller33804262001-02-04 23:20:18 +110068auth_parse_options(struct passwd *pw, char *opts, char *file, u_long linenum)
Damien Millerf6d9e222000-06-18 14:50:44 +100069{
70 const char *cp;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +000071 int i;
Damien Miller874d77b2000-10-14 16:23:11 +110072
73 /* reset options */
74 auth_clear_options();
75
Ben Lindstrom36d7bd02001-02-10 22:27:19 +000076 if (!opts)
77 return 1;
78
Damien Miller33804262001-02-04 23:20:18 +110079 while (*opts && *opts != ' ' && *opts != '\t') {
Damien Millerf6d9e222000-06-18 14:50:44 +100080 cp = "no-port-forwarding";
Damien Miller33804262001-02-04 23:20:18 +110081 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
Ben Lindstroma574cda2002-05-15 16:16:14 +000082 auth_debug_add("Port forwarding disabled.");
Damien Millerf6d9e222000-06-18 14:50:44 +100083 no_port_forwarding_flag = 1;
Damien Miller33804262001-02-04 23:20:18 +110084 opts += strlen(cp);
Damien Millerf6d9e222000-06-18 14:50:44 +100085 goto next_option;
86 }
87 cp = "no-agent-forwarding";
Damien Miller33804262001-02-04 23:20:18 +110088 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
Ben Lindstroma574cda2002-05-15 16:16:14 +000089 auth_debug_add("Agent forwarding disabled.");
Damien Millerf6d9e222000-06-18 14:50:44 +100090 no_agent_forwarding_flag = 1;
Damien Miller33804262001-02-04 23:20:18 +110091 opts += strlen(cp);
Damien Millerf6d9e222000-06-18 14:50:44 +100092 goto next_option;
93 }
94 cp = "no-X11-forwarding";
Damien Miller33804262001-02-04 23:20:18 +110095 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
Ben Lindstroma574cda2002-05-15 16:16:14 +000096 auth_debug_add("X11 forwarding disabled.");
Damien Millerf6d9e222000-06-18 14:50:44 +100097 no_x11_forwarding_flag = 1;
Damien Miller33804262001-02-04 23:20:18 +110098 opts += strlen(cp);
Damien Millerf6d9e222000-06-18 14:50:44 +100099 goto next_option;
100 }
101 cp = "no-pty";
Damien Miller33804262001-02-04 23:20:18 +1100102 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
Ben Lindstroma574cda2002-05-15 16:16:14 +0000103 auth_debug_add("Pty allocation disabled.");
Damien Millerf6d9e222000-06-18 14:50:44 +1000104 no_pty_flag = 1;
Damien Miller33804262001-02-04 23:20:18 +1100105 opts += strlen(cp);
Damien Millerf6d9e222000-06-18 14:50:44 +1000106 goto next_option;
107 }
108 cp = "command=\"";
Damien Miller33804262001-02-04 23:20:18 +1100109 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
Damien Miller33804262001-02-04 23:20:18 +1100110 opts += strlen(cp);
111 forced_command = xmalloc(strlen(opts) + 1);
Damien Millerf6d9e222000-06-18 14:50:44 +1000112 i = 0;
Damien Miller33804262001-02-04 23:20:18 +1100113 while (*opts) {
114 if (*opts == '"')
Damien Millerf6d9e222000-06-18 14:50:44 +1000115 break;
Damien Miller33804262001-02-04 23:20:18 +1100116 if (*opts == '\\' && opts[1] == '"') {
117 opts += 2;
Damien Millerf6d9e222000-06-18 14:50:44 +1000118 forced_command[i++] = '"';
119 continue;
120 }
Damien Miller33804262001-02-04 23:20:18 +1100121 forced_command[i++] = *opts++;
Damien Millerf6d9e222000-06-18 14:50:44 +1000122 }
Damien Miller33804262001-02-04 23:20:18 +1100123 if (!*opts) {
Damien Millerf6d9e222000-06-18 14:50:44 +1000124 debug("%.100s, line %lu: missing end quote",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000125 file, linenum);
Ben Lindstroma574cda2002-05-15 16:16:14 +0000126 auth_debug_add("%.100s, line %lu: missing end quote",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000127 file, linenum);
Damien Miller056ddf72001-03-14 10:15:20 +1100128 xfree(forced_command);
129 forced_command = NULL;
130 goto bad_option;
Damien Millerf6d9e222000-06-18 14:50:44 +1000131 }
132 forced_command[i] = 0;
Ben Lindstroma574cda2002-05-15 16:16:14 +0000133 auth_debug_add("Forced command: %.900s", forced_command);
Damien Miller33804262001-02-04 23:20:18 +1100134 opts++;
Damien Millerf6d9e222000-06-18 14:50:44 +1000135 goto next_option;
136 }
137 cp = "environment=\"";
Damien Miller33804262001-02-04 23:20:18 +1100138 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
Damien Millerf6d9e222000-06-18 14:50:44 +1000139 char *s;
140 struct envstring *new_envstring;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +0000141
Damien Miller33804262001-02-04 23:20:18 +1100142 opts += strlen(cp);
143 s = xmalloc(strlen(opts) + 1);
Damien Millerf6d9e222000-06-18 14:50:44 +1000144 i = 0;
Damien Miller33804262001-02-04 23:20:18 +1100145 while (*opts) {
146 if (*opts == '"')
Damien Millerf6d9e222000-06-18 14:50:44 +1000147 break;
Damien Miller33804262001-02-04 23:20:18 +1100148 if (*opts == '\\' && opts[1] == '"') {
149 opts += 2;
Damien Millerf6d9e222000-06-18 14:50:44 +1000150 s[i++] = '"';
151 continue;
152 }
Damien Miller33804262001-02-04 23:20:18 +1100153 s[i++] = *opts++;
Damien Millerf6d9e222000-06-18 14:50:44 +1000154 }
Damien Miller33804262001-02-04 23:20:18 +1100155 if (!*opts) {
Damien Millerf6d9e222000-06-18 14:50:44 +1000156 debug("%.100s, line %lu: missing end quote",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000157 file, linenum);
Ben Lindstroma574cda2002-05-15 16:16:14 +0000158 auth_debug_add("%.100s, line %lu: missing end quote",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000159 file, linenum);
Damien Miller056ddf72001-03-14 10:15:20 +1100160 xfree(s);
161 goto bad_option;
Damien Millerf6d9e222000-06-18 14:50:44 +1000162 }
163 s[i] = 0;
Ben Lindstroma574cda2002-05-15 16:16:14 +0000164 auth_debug_add("Adding to environment: %.900s", s);
Damien Millerf6d9e222000-06-18 14:50:44 +1000165 debug("Adding to environment: %.900s", s);
Damien Miller33804262001-02-04 23:20:18 +1100166 opts++;
Damien Millerf6d9e222000-06-18 14:50:44 +1000167 new_envstring = xmalloc(sizeof(struct envstring));
168 new_envstring->s = s;
169 new_envstring->next = custom_environment;
170 custom_environment = new_envstring;
171 goto next_option;
172 }
173 cp = "from=\"";
Damien Miller33804262001-02-04 23:20:18 +1100174 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
Damien Miller33804262001-02-04 23:20:18 +1100175 const char *remote_ip = get_remote_ipaddr();
176 const char *remote_host = get_canonical_hostname(
Damien Millerc5d86352002-02-05 12:13:41 +1100177 options.verify_reverse_mapping);
Damien Miller33804262001-02-04 23:20:18 +1100178 char *patterns = xmalloc(strlen(opts) + 1);
Ben Lindstrom7bb8b492001-03-17 00:47:54 +0000179
Damien Miller33804262001-02-04 23:20:18 +1100180 opts += strlen(cp);
Damien Millerf6d9e222000-06-18 14:50:44 +1000181 i = 0;
Damien Miller33804262001-02-04 23:20:18 +1100182 while (*opts) {
183 if (*opts == '"')
Damien Millerf6d9e222000-06-18 14:50:44 +1000184 break;
Damien Miller33804262001-02-04 23:20:18 +1100185 if (*opts == '\\' && opts[1] == '"') {
186 opts += 2;
Damien Millerf6d9e222000-06-18 14:50:44 +1000187 patterns[i++] = '"';
188 continue;
189 }
Damien Miller33804262001-02-04 23:20:18 +1100190 patterns[i++] = *opts++;
Damien Millerf6d9e222000-06-18 14:50:44 +1000191 }
Damien Miller33804262001-02-04 23:20:18 +1100192 if (!*opts) {
Damien Millerf6d9e222000-06-18 14:50:44 +1000193 debug("%.100s, line %lu: missing end quote",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000194 file, linenum);
Ben Lindstroma574cda2002-05-15 16:16:14 +0000195 auth_debug_add("%.100s, line %lu: missing end quote",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000196 file, linenum);
Damien Miller056ddf72001-03-14 10:15:20 +1100197 xfree(patterns);
198 goto bad_option;
Damien Millerf6d9e222000-06-18 14:50:44 +1000199 }
200 patterns[i] = 0;
Damien Miller33804262001-02-04 23:20:18 +1100201 opts++;
Ben Lindstromf0c50292001-06-25 05:17:53 +0000202 if (match_host_and_ip(remote_host, remote_ip,
203 patterns) != 1) {
204 xfree(patterns);
Damien Miller33804262001-02-04 23:20:18 +1100205 log("Authentication tried for %.100s with "
206 "correct key but not from a permitted "
207 "host (host=%.200s, ip=%.200s).",
208 pw->pw_name, remote_host, remote_ip);
Ben Lindstroma574cda2002-05-15 16:16:14 +0000209 auth_debug_add("Your host '%.200s' is not "
Damien Miller33804262001-02-04 23:20:18 +1100210 "permitted to use this key for login.",
211 remote_host);
Damien Millerf6d9e222000-06-18 14:50:44 +1000212 /* deny access */
213 return 0;
214 }
Ben Lindstromf0c50292001-06-25 05:17:53 +0000215 xfree(patterns);
Damien Millerf6d9e222000-06-18 14:50:44 +1000216 /* Host name matches. */
217 goto next_option;
218 }
Ben Lindstrom7bb8b492001-03-17 00:47:54 +0000219 cp = "permitopen=\"";
220 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
Ben Lindstromd71ba572001-09-12 18:03:31 +0000221 char host[256], sport[6];
Ben Lindstrom7bb8b492001-03-17 00:47:54 +0000222 u_short port;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +0000223 char *patterns = xmalloc(strlen(opts) + 1);
224
225 opts += strlen(cp);
226 i = 0;
227 while (*opts) {
228 if (*opts == '"')
229 break;
230 if (*opts == '\\' && opts[1] == '"') {
231 opts += 2;
232 patterns[i++] = '"';
233 continue;
234 }
235 patterns[i++] = *opts++;
236 }
237 if (!*opts) {
238 debug("%.100s, line %lu: missing end quote",
239 file, linenum);
Ben Lindstroma574cda2002-05-15 16:16:14 +0000240 auth_debug_add("%.100s, line %lu: missing end quote",
Ben Lindstrom7bb8b492001-03-17 00:47:54 +0000241 file, linenum);
242 xfree(patterns);
243 goto bad_option;
244 }
245 patterns[i] = 0;
246 opts++;
Ben Lindstromd71ba572001-09-12 18:03:31 +0000247 if (sscanf(patterns, "%255[^:]:%5[0-9]", host, sport) != 2 &&
248 sscanf(patterns, "%255[^/]/%5[0-9]", host, sport) != 2) {
249 debug("%.100s, line %lu: Bad permitopen specification "
250 "<%.100s>", file, linenum, patterns);
Ben Lindstroma574cda2002-05-15 16:16:14 +0000251 auth_debug_add("%.100s, line %lu: "
Ben Lindstromd71ba572001-09-12 18:03:31 +0000252 "Bad permitopen specification", file, linenum);
Ben Lindstrom7bb8b492001-03-17 00:47:54 +0000253 xfree(patterns);
254 goto bad_option;
255 }
Ben Lindstromd71ba572001-09-12 18:03:31 +0000256 if ((port = a2port(sport)) == 0) {
257 debug("%.100s, line %lu: Bad permitopen port <%.100s>",
258 file, linenum, sport);
Ben Lindstroma574cda2002-05-15 16:16:14 +0000259 auth_debug_add("%.100s, line %lu: "
Ben Lindstromd71ba572001-09-12 18:03:31 +0000260 "Bad permitopen port", file, linenum);
Ben Lindstrom7bb8b492001-03-17 00:47:54 +0000261 xfree(patterns);
262 goto bad_option;
263 }
Ben Lindstrom2d70f982001-03-19 00:13:46 +0000264 if (options.allow_tcp_forwarding)
Ben Lindstromd71ba572001-09-12 18:03:31 +0000265 channel_add_permitted_opens(host, port);
Ben Lindstrom7bb8b492001-03-17 00:47:54 +0000266 xfree(patterns);
267 goto next_option;
268 }
Damien Millerf6d9e222000-06-18 14:50:44 +1000269next_option:
270 /*
271 * Skip the comma, and move to the next option
272 * (or break out if there are no more).
273 */
Damien Miller33804262001-02-04 23:20:18 +1100274 if (!*opts)
Damien Millerf6d9e222000-06-18 14:50:44 +1000275 fatal("Bugs in auth-options.c option processing.");
Damien Miller33804262001-02-04 23:20:18 +1100276 if (*opts == ' ' || *opts == '\t')
Damien Millerf6d9e222000-06-18 14:50:44 +1000277 break; /* End of options. */
Damien Miller33804262001-02-04 23:20:18 +1100278 if (*opts != ',')
Damien Millerf6d9e222000-06-18 14:50:44 +1000279 goto bad_option;
Damien Miller33804262001-02-04 23:20:18 +1100280 opts++;
Damien Millerf6d9e222000-06-18 14:50:44 +1000281 /* Process the next option. */
282 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000283
284 if (!use_privsep)
Ben Lindstroma574cda2002-05-15 16:16:14 +0000285 auth_debug_send();
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000286
Damien Millerf6d9e222000-06-18 14:50:44 +1000287 /* grant access */
288 return 1;
289
290bad_option:
291 log("Bad options in %.100s file, line %lu: %.50s",
Damien Miller33804262001-02-04 23:20:18 +1100292 file, linenum, opts);
Ben Lindstroma574cda2002-05-15 16:16:14 +0000293 auth_debug_add("Bad options in %.100s file, line %lu: %.50s",
Damien Miller33804262001-02-04 23:20:18 +1100294 file, linenum, opts);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000295
296 if (!use_privsep)
Ben Lindstroma574cda2002-05-15 16:16:14 +0000297 auth_debug_send();
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000298
Damien Millerf6d9e222000-06-18 14:50:44 +1000299 /* deny access */
300 return 0;
301}