blob: 8df6a6dfcabbd910313e620f770503c02432b2ed [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"
Damien Millerc5d86352002-02-05 12:13:41 +110013RCSID("$OpenBSD: auth-options.c,v 1.21 2002/01/29 14:32:03 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 Lindstromd71ba572001-09-12 18:03:31 +000023#include "misc.h"
Damien Millerf6d9e222000-06-18 14:50:44 +100024
25/* Flags set authorized_keys flags */
26int no_port_forwarding_flag = 0;
27int no_agent_forwarding_flag = 0;
28int no_x11_forwarding_flag = 0;
29int no_pty_flag = 0;
30
31/* "command=" option. */
32char *forced_command = NULL;
33
34/* "environment=" options. */
35struct envstring *custom_environment = NULL;
36
Damien Miller33804262001-02-04 23:20:18 +110037extern ServerOptions options;
38
Damien Miller874d77b2000-10-14 16:23:11 +110039void
40auth_clear_options(void)
41{
42 no_agent_forwarding_flag = 0;
43 no_port_forwarding_flag = 0;
44 no_pty_flag = 0;
45 no_x11_forwarding_flag = 0;
46 while (custom_environment) {
47 struct envstring *ce = custom_environment;
48 custom_environment = ce->next;
49 xfree(ce->s);
50 xfree(ce);
51 }
52 if (forced_command) {
53 xfree(forced_command);
54 forced_command = NULL;
55 }
Ben Lindstrom7bb8b492001-03-17 00:47:54 +000056 channel_clear_permitted_opens();
Damien Miller874d77b2000-10-14 16:23:11 +110057}
58
Ben Lindstrom226cfa02001-01-22 05:34:40 +000059/*
60 * return 1 if access is granted, 0 if not.
61 * side effect: sets key option flags
62 */
Damien Millerf6d9e222000-06-18 14:50:44 +100063int
Damien Miller33804262001-02-04 23:20:18 +110064auth_parse_options(struct passwd *pw, char *opts, char *file, u_long linenum)
Damien Millerf6d9e222000-06-18 14:50:44 +100065{
66 const char *cp;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +000067 int i;
Damien Miller874d77b2000-10-14 16:23:11 +110068
69 /* reset options */
70 auth_clear_options();
71
Ben Lindstrom36d7bd02001-02-10 22:27:19 +000072 if (!opts)
73 return 1;
74
Damien Miller33804262001-02-04 23:20:18 +110075 while (*opts && *opts != ' ' && *opts != '\t') {
Damien Millerf6d9e222000-06-18 14:50:44 +100076 cp = "no-port-forwarding";
Damien Miller33804262001-02-04 23:20:18 +110077 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
Damien Millerf6d9e222000-06-18 14:50:44 +100078 packet_send_debug("Port forwarding disabled.");
79 no_port_forwarding_flag = 1;
Damien Miller33804262001-02-04 23:20:18 +110080 opts += strlen(cp);
Damien Millerf6d9e222000-06-18 14:50:44 +100081 goto next_option;
82 }
83 cp = "no-agent-forwarding";
Damien Miller33804262001-02-04 23:20:18 +110084 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
Damien Millerf6d9e222000-06-18 14:50:44 +100085 packet_send_debug("Agent forwarding disabled.");
86 no_agent_forwarding_flag = 1;
Damien Miller33804262001-02-04 23:20:18 +110087 opts += strlen(cp);
Damien Millerf6d9e222000-06-18 14:50:44 +100088 goto next_option;
89 }
90 cp = "no-X11-forwarding";
Damien Miller33804262001-02-04 23:20:18 +110091 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
Damien Millerf6d9e222000-06-18 14:50:44 +100092 packet_send_debug("X11 forwarding disabled.");
93 no_x11_forwarding_flag = 1;
Damien Miller33804262001-02-04 23:20:18 +110094 opts += strlen(cp);
Damien Millerf6d9e222000-06-18 14:50:44 +100095 goto next_option;
96 }
97 cp = "no-pty";
Damien Miller33804262001-02-04 23:20:18 +110098 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
Damien Millerf6d9e222000-06-18 14:50:44 +100099 packet_send_debug("Pty allocation disabled.");
100 no_pty_flag = 1;
Damien Miller33804262001-02-04 23:20:18 +1100101 opts += strlen(cp);
Damien Millerf6d9e222000-06-18 14:50:44 +1000102 goto next_option;
103 }
104 cp = "command=\"";
Damien Miller33804262001-02-04 23:20:18 +1100105 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
Damien Miller33804262001-02-04 23:20:18 +1100106 opts += strlen(cp);
107 forced_command = xmalloc(strlen(opts) + 1);
Damien Millerf6d9e222000-06-18 14:50:44 +1000108 i = 0;
Damien Miller33804262001-02-04 23:20:18 +1100109 while (*opts) {
110 if (*opts == '"')
Damien Millerf6d9e222000-06-18 14:50:44 +1000111 break;
Damien Miller33804262001-02-04 23:20:18 +1100112 if (*opts == '\\' && opts[1] == '"') {
113 opts += 2;
Damien Millerf6d9e222000-06-18 14:50:44 +1000114 forced_command[i++] = '"';
115 continue;
116 }
Damien Miller33804262001-02-04 23:20:18 +1100117 forced_command[i++] = *opts++;
Damien Millerf6d9e222000-06-18 14:50:44 +1000118 }
Damien Miller33804262001-02-04 23:20:18 +1100119 if (!*opts) {
Damien Millerf6d9e222000-06-18 14:50:44 +1000120 debug("%.100s, line %lu: missing end quote",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000121 file, linenum);
Damien Millerf6d9e222000-06-18 14:50:44 +1000122 packet_send_debug("%.100s, line %lu: missing end quote",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000123 file, linenum);
Damien Miller056ddf72001-03-14 10:15:20 +1100124 xfree(forced_command);
125 forced_command = NULL;
126 goto bad_option;
Damien Millerf6d9e222000-06-18 14:50:44 +1000127 }
128 forced_command[i] = 0;
129 packet_send_debug("Forced command: %.900s", forced_command);
Damien Miller33804262001-02-04 23:20:18 +1100130 opts++;
Damien Millerf6d9e222000-06-18 14:50:44 +1000131 goto next_option;
132 }
133 cp = "environment=\"";
Damien Miller33804262001-02-04 23:20:18 +1100134 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
Damien Millerf6d9e222000-06-18 14:50:44 +1000135 char *s;
136 struct envstring *new_envstring;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +0000137
Damien Miller33804262001-02-04 23:20:18 +1100138 opts += strlen(cp);
139 s = xmalloc(strlen(opts) + 1);
Damien Millerf6d9e222000-06-18 14:50:44 +1000140 i = 0;
Damien Miller33804262001-02-04 23:20:18 +1100141 while (*opts) {
142 if (*opts == '"')
Damien Millerf6d9e222000-06-18 14:50:44 +1000143 break;
Damien Miller33804262001-02-04 23:20:18 +1100144 if (*opts == '\\' && opts[1] == '"') {
145 opts += 2;
Damien Millerf6d9e222000-06-18 14:50:44 +1000146 s[i++] = '"';
147 continue;
148 }
Damien Miller33804262001-02-04 23:20:18 +1100149 s[i++] = *opts++;
Damien Millerf6d9e222000-06-18 14:50:44 +1000150 }
Damien Miller33804262001-02-04 23:20:18 +1100151 if (!*opts) {
Damien Millerf6d9e222000-06-18 14:50:44 +1000152 debug("%.100s, line %lu: missing end quote",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000153 file, linenum);
Damien Millerf6d9e222000-06-18 14:50:44 +1000154 packet_send_debug("%.100s, line %lu: missing end quote",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000155 file, linenum);
Damien Miller056ddf72001-03-14 10:15:20 +1100156 xfree(s);
157 goto bad_option;
Damien Millerf6d9e222000-06-18 14:50:44 +1000158 }
159 s[i] = 0;
160 packet_send_debug("Adding to environment: %.900s", s);
161 debug("Adding to environment: %.900s", s);
Damien Miller33804262001-02-04 23:20:18 +1100162 opts++;
Damien Millerf6d9e222000-06-18 14:50:44 +1000163 new_envstring = xmalloc(sizeof(struct envstring));
164 new_envstring->s = s;
165 new_envstring->next = custom_environment;
166 custom_environment = new_envstring;
167 goto next_option;
168 }
169 cp = "from=\"";
Damien Miller33804262001-02-04 23:20:18 +1100170 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
Damien Miller33804262001-02-04 23:20:18 +1100171 const char *remote_ip = get_remote_ipaddr();
172 const char *remote_host = get_canonical_hostname(
Damien Millerc5d86352002-02-05 12:13:41 +1100173 options.verify_reverse_mapping);
Damien Miller33804262001-02-04 23:20:18 +1100174 char *patterns = xmalloc(strlen(opts) + 1);
Ben Lindstrom7bb8b492001-03-17 00:47:54 +0000175
Damien Miller33804262001-02-04 23:20:18 +1100176 opts += strlen(cp);
Damien Millerf6d9e222000-06-18 14:50:44 +1000177 i = 0;
Damien Miller33804262001-02-04 23:20:18 +1100178 while (*opts) {
179 if (*opts == '"')
Damien Millerf6d9e222000-06-18 14:50:44 +1000180 break;
Damien Miller33804262001-02-04 23:20:18 +1100181 if (*opts == '\\' && opts[1] == '"') {
182 opts += 2;
Damien Millerf6d9e222000-06-18 14:50:44 +1000183 patterns[i++] = '"';
184 continue;
185 }
Damien Miller33804262001-02-04 23:20:18 +1100186 patterns[i++] = *opts++;
Damien Millerf6d9e222000-06-18 14:50:44 +1000187 }
Damien Miller33804262001-02-04 23:20:18 +1100188 if (!*opts) {
Damien Millerf6d9e222000-06-18 14:50:44 +1000189 debug("%.100s, line %lu: missing end quote",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000190 file, linenum);
Damien Millerf6d9e222000-06-18 14:50:44 +1000191 packet_send_debug("%.100s, line %lu: missing end quote",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000192 file, linenum);
Damien Miller056ddf72001-03-14 10:15:20 +1100193 xfree(patterns);
194 goto bad_option;
Damien Millerf6d9e222000-06-18 14:50:44 +1000195 }
196 patterns[i] = 0;
Damien Miller33804262001-02-04 23:20:18 +1100197 opts++;
Ben Lindstromf0c50292001-06-25 05:17:53 +0000198 if (match_host_and_ip(remote_host, remote_ip,
199 patterns) != 1) {
200 xfree(patterns);
Damien Miller33804262001-02-04 23:20:18 +1100201 log("Authentication tried for %.100s with "
202 "correct key but not from a permitted "
203 "host (host=%.200s, ip=%.200s).",
204 pw->pw_name, remote_host, remote_ip);
205 packet_send_debug("Your host '%.200s' is not "
206 "permitted to use this key for login.",
207 remote_host);
Damien Millerf6d9e222000-06-18 14:50:44 +1000208 /* deny access */
209 return 0;
210 }
Ben Lindstromf0c50292001-06-25 05:17:53 +0000211 xfree(patterns);
Damien Millerf6d9e222000-06-18 14:50:44 +1000212 /* Host name matches. */
213 goto next_option;
214 }
Ben Lindstrom7bb8b492001-03-17 00:47:54 +0000215 cp = "permitopen=\"";
216 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
Ben Lindstromd71ba572001-09-12 18:03:31 +0000217 char host[256], sport[6];
Ben Lindstrom7bb8b492001-03-17 00:47:54 +0000218 u_short port;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +0000219 char *patterns = xmalloc(strlen(opts) + 1);
220
221 opts += strlen(cp);
222 i = 0;
223 while (*opts) {
224 if (*opts == '"')
225 break;
226 if (*opts == '\\' && opts[1] == '"') {
227 opts += 2;
228 patterns[i++] = '"';
229 continue;
230 }
231 patterns[i++] = *opts++;
232 }
233 if (!*opts) {
234 debug("%.100s, line %lu: missing end quote",
235 file, linenum);
236 packet_send_debug("%.100s, line %lu: missing end quote",
237 file, linenum);
238 xfree(patterns);
239 goto bad_option;
240 }
241 patterns[i] = 0;
242 opts++;
Ben Lindstromd71ba572001-09-12 18:03:31 +0000243 if (sscanf(patterns, "%255[^:]:%5[0-9]", host, sport) != 2 &&
244 sscanf(patterns, "%255[^/]/%5[0-9]", host, sport) != 2) {
245 debug("%.100s, line %lu: Bad permitopen specification "
246 "<%.100s>", file, linenum, patterns);
247 packet_send_debug("%.100s, line %lu: "
248 "Bad permitopen specification", file, linenum);
Ben Lindstrom7bb8b492001-03-17 00:47:54 +0000249 xfree(patterns);
250 goto bad_option;
251 }
Ben Lindstromd71ba572001-09-12 18:03:31 +0000252 if ((port = a2port(sport)) == 0) {
253 debug("%.100s, line %lu: Bad permitopen port <%.100s>",
254 file, linenum, sport);
255 packet_send_debug("%.100s, line %lu: "
256 "Bad permitopen port", file, linenum);
Ben Lindstrom7bb8b492001-03-17 00:47:54 +0000257 xfree(patterns);
258 goto bad_option;
259 }
Ben Lindstrom2d70f982001-03-19 00:13:46 +0000260 if (options.allow_tcp_forwarding)
Ben Lindstromd71ba572001-09-12 18:03:31 +0000261 channel_add_permitted_opens(host, port);
Ben Lindstrom7bb8b492001-03-17 00:47:54 +0000262 xfree(patterns);
263 goto next_option;
264 }
Damien Millerf6d9e222000-06-18 14:50:44 +1000265next_option:
266 /*
267 * Skip the comma, and move to the next option
268 * (or break out if there are no more).
269 */
Damien Miller33804262001-02-04 23:20:18 +1100270 if (!*opts)
Damien Millerf6d9e222000-06-18 14:50:44 +1000271 fatal("Bugs in auth-options.c option processing.");
Damien Miller33804262001-02-04 23:20:18 +1100272 if (*opts == ' ' || *opts == '\t')
Damien Millerf6d9e222000-06-18 14:50:44 +1000273 break; /* End of options. */
Damien Miller33804262001-02-04 23:20:18 +1100274 if (*opts != ',')
Damien Millerf6d9e222000-06-18 14:50:44 +1000275 goto bad_option;
Damien Miller33804262001-02-04 23:20:18 +1100276 opts++;
Damien Millerf6d9e222000-06-18 14:50:44 +1000277 /* Process the next option. */
278 }
279 /* grant access */
280 return 1;
281
282bad_option:
283 log("Bad options in %.100s file, line %lu: %.50s",
Damien Miller33804262001-02-04 23:20:18 +1100284 file, linenum, opts);
Damien Millerf6d9e222000-06-18 14:50:44 +1000285 packet_send_debug("Bad options in %.100s file, line %lu: %.50s",
Damien Miller33804262001-02-04 23:20:18 +1100286 file, linenum, opts);
Damien Millerf6d9e222000-06-18 14:50:44 +1000287 /* deny access */
288 return 0;
289}