blob: 48be6d8e0f46ea06e4aa69a4c9c8403674b4802b [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 Lindstrom7a2073c2002-03-22 02:30:41 +000013RCSID("$OpenBSD: auth-options.c,v 1.22 2002/03/18 17:50:31 provos 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"
26
27/* Debugging messages */
28Buffer auth_debug;
29int auth_debug_init;
Damien Millerf6d9e222000-06-18 14:50:44 +100030
31/* Flags set authorized_keys flags */
32int no_port_forwarding_flag = 0;
33int no_agent_forwarding_flag = 0;
34int no_x11_forwarding_flag = 0;
35int no_pty_flag = 0;
36
37/* "command=" option. */
38char *forced_command = NULL;
39
40/* "environment=" options. */
41struct envstring *custom_environment = NULL;
42
Damien Miller33804262001-02-04 23:20:18 +110043extern ServerOptions options;
44
Damien Miller874d77b2000-10-14 16:23:11 +110045void
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000046auth_send_debug(Buffer *m)
47{
48 char *msg;
49
50 while (buffer_len(m)) {
51 msg = buffer_get_string(m, NULL);
52 packet_send_debug("%s", msg);
53 xfree(msg);
54 }
55}
56
57void
Damien Miller874d77b2000-10-14 16:23:11 +110058auth_clear_options(void)
59{
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000060 if (auth_debug_init)
61 buffer_clear(&auth_debug);
62 else {
63 buffer_init(&auth_debug);
64 auth_debug_init = 1;
65 }
66
Damien Miller874d77b2000-10-14 16:23:11 +110067 no_agent_forwarding_flag = 0;
68 no_port_forwarding_flag = 0;
69 no_pty_flag = 0;
70 no_x11_forwarding_flag = 0;
71 while (custom_environment) {
72 struct envstring *ce = custom_environment;
73 custom_environment = ce->next;
74 xfree(ce->s);
75 xfree(ce);
76 }
77 if (forced_command) {
78 xfree(forced_command);
79 forced_command = NULL;
80 }
Ben Lindstrom7bb8b492001-03-17 00:47:54 +000081 channel_clear_permitted_opens();
Damien Miller874d77b2000-10-14 16:23:11 +110082}
83
Ben Lindstrom226cfa02001-01-22 05:34:40 +000084/*
85 * return 1 if access is granted, 0 if not.
86 * side effect: sets key option flags
87 */
Damien Millerf6d9e222000-06-18 14:50:44 +100088int
Damien Miller33804262001-02-04 23:20:18 +110089auth_parse_options(struct passwd *pw, char *opts, char *file, u_long linenum)
Damien Millerf6d9e222000-06-18 14:50:44 +100090{
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000091 char tmp[1024];
Damien Millerf6d9e222000-06-18 14:50:44 +100092 const char *cp;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +000093 int i;
Damien Miller874d77b2000-10-14 16:23:11 +110094
95 /* reset options */
96 auth_clear_options();
97
Ben Lindstrom36d7bd02001-02-10 22:27:19 +000098 if (!opts)
99 return 1;
100
Damien Miller33804262001-02-04 23:20:18 +1100101 while (*opts && *opts != ' ' && *opts != '\t') {
Damien Millerf6d9e222000-06-18 14:50:44 +1000102 cp = "no-port-forwarding";
Damien Miller33804262001-02-04 23:20:18 +1100103 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000104 snprintf(tmp, sizeof(tmp), "Port forwarding disabled.");
105 buffer_put_cstring(&auth_debug, tmp);
Damien Millerf6d9e222000-06-18 14:50:44 +1000106 no_port_forwarding_flag = 1;
Damien Miller33804262001-02-04 23:20:18 +1100107 opts += strlen(cp);
Damien Millerf6d9e222000-06-18 14:50:44 +1000108 goto next_option;
109 }
110 cp = "no-agent-forwarding";
Damien Miller33804262001-02-04 23:20:18 +1100111 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000112 snprintf(tmp, sizeof(tmp), "Agent forwarding disabled.");
113 buffer_put_cstring(&auth_debug, tmp);
Damien Millerf6d9e222000-06-18 14:50:44 +1000114 no_agent_forwarding_flag = 1;
Damien Miller33804262001-02-04 23:20:18 +1100115 opts += strlen(cp);
Damien Millerf6d9e222000-06-18 14:50:44 +1000116 goto next_option;
117 }
118 cp = "no-X11-forwarding";
Damien Miller33804262001-02-04 23:20:18 +1100119 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000120 snprintf(tmp, sizeof(tmp), "X11 forwarding disabled.");
121 buffer_put_cstring(&auth_debug, tmp);
Damien Millerf6d9e222000-06-18 14:50:44 +1000122 no_x11_forwarding_flag = 1;
Damien Miller33804262001-02-04 23:20:18 +1100123 opts += strlen(cp);
Damien Millerf6d9e222000-06-18 14:50:44 +1000124 goto next_option;
125 }
126 cp = "no-pty";
Damien Miller33804262001-02-04 23:20:18 +1100127 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000128 snprintf(tmp, sizeof(tmp), "Pty allocation disabled.");
129 buffer_put_cstring(&auth_debug, tmp);
Damien Millerf6d9e222000-06-18 14:50:44 +1000130 no_pty_flag = 1;
Damien Miller33804262001-02-04 23:20:18 +1100131 opts += strlen(cp);
Damien Millerf6d9e222000-06-18 14:50:44 +1000132 goto next_option;
133 }
134 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 Lindstrom7a2073c2002-03-22 02:30:41 +0000152 snprintf(tmp, sizeof(tmp), "%.100s, line %lu: missing end quote",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000153 file, linenum);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000154 buffer_put_cstring(&auth_debug, tmp);
Damien Miller056ddf72001-03-14 10:15:20 +1100155 xfree(forced_command);
156 forced_command = NULL;
157 goto bad_option;
Damien Millerf6d9e222000-06-18 14:50:44 +1000158 }
159 forced_command[i] = 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000160 snprintf(tmp, sizeof(tmp), "Forced command: %.900s", forced_command);
161 buffer_put_cstring(&auth_debug, tmp);
Damien Miller33804262001-02-04 23:20:18 +1100162 opts++;
Damien Millerf6d9e222000-06-18 14:50:44 +1000163 goto next_option;
164 }
165 cp = "environment=\"";
Damien Miller33804262001-02-04 23:20:18 +1100166 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
Damien Millerf6d9e222000-06-18 14:50:44 +1000167 char *s;
168 struct envstring *new_envstring;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +0000169
Damien Miller33804262001-02-04 23:20:18 +1100170 opts += strlen(cp);
171 s = xmalloc(strlen(opts) + 1);
Damien Millerf6d9e222000-06-18 14:50:44 +1000172 i = 0;
Damien Miller33804262001-02-04 23:20:18 +1100173 while (*opts) {
174 if (*opts == '"')
Damien Millerf6d9e222000-06-18 14:50:44 +1000175 break;
Damien Miller33804262001-02-04 23:20:18 +1100176 if (*opts == '\\' && opts[1] == '"') {
177 opts += 2;
Damien Millerf6d9e222000-06-18 14:50:44 +1000178 s[i++] = '"';
179 continue;
180 }
Damien Miller33804262001-02-04 23:20:18 +1100181 s[i++] = *opts++;
Damien Millerf6d9e222000-06-18 14:50:44 +1000182 }
Damien Miller33804262001-02-04 23:20:18 +1100183 if (!*opts) {
Damien Millerf6d9e222000-06-18 14:50:44 +1000184 debug("%.100s, line %lu: missing end quote",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000185 file, linenum);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000186 snprintf(tmp, sizeof(tmp), "%.100s, line %lu: missing end quote",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000187 file, linenum);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000188 buffer_put_cstring(&auth_debug, tmp);
Damien Miller056ddf72001-03-14 10:15:20 +1100189 xfree(s);
190 goto bad_option;
Damien Millerf6d9e222000-06-18 14:50:44 +1000191 }
192 s[i] = 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000193 snprintf(tmp, sizeof(tmp), "Adding to environment: %.900s", s);
194 buffer_put_cstring(&auth_debug, tmp);
Damien Millerf6d9e222000-06-18 14:50:44 +1000195 debug("Adding to environment: %.900s", s);
Damien Miller33804262001-02-04 23:20:18 +1100196 opts++;
Damien Millerf6d9e222000-06-18 14:50:44 +1000197 new_envstring = xmalloc(sizeof(struct envstring));
198 new_envstring->s = s;
199 new_envstring->next = custom_environment;
200 custom_environment = new_envstring;
201 goto next_option;
202 }
203 cp = "from=\"";
Damien Miller33804262001-02-04 23:20:18 +1100204 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
Damien Miller33804262001-02-04 23:20:18 +1100205 const char *remote_ip = get_remote_ipaddr();
206 const char *remote_host = get_canonical_hostname(
Damien Millerc5d86352002-02-05 12:13:41 +1100207 options.verify_reverse_mapping);
Damien Miller33804262001-02-04 23:20:18 +1100208 char *patterns = xmalloc(strlen(opts) + 1);
Ben Lindstrom7bb8b492001-03-17 00:47:54 +0000209
Damien Miller33804262001-02-04 23:20:18 +1100210 opts += strlen(cp);
Damien Millerf6d9e222000-06-18 14:50:44 +1000211 i = 0;
Damien Miller33804262001-02-04 23:20:18 +1100212 while (*opts) {
213 if (*opts == '"')
Damien Millerf6d9e222000-06-18 14:50:44 +1000214 break;
Damien Miller33804262001-02-04 23:20:18 +1100215 if (*opts == '\\' && opts[1] == '"') {
216 opts += 2;
Damien Millerf6d9e222000-06-18 14:50:44 +1000217 patterns[i++] = '"';
218 continue;
219 }
Damien Miller33804262001-02-04 23:20:18 +1100220 patterns[i++] = *opts++;
Damien Millerf6d9e222000-06-18 14:50:44 +1000221 }
Damien Miller33804262001-02-04 23:20:18 +1100222 if (!*opts) {
Damien Millerf6d9e222000-06-18 14:50:44 +1000223 debug("%.100s, line %lu: missing end quote",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000224 file, linenum);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000225 snprintf(tmp, sizeof(tmp), "%.100s, line %lu: missing end quote",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000226 file, linenum);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000227 buffer_put_cstring(&auth_debug, tmp);
Damien Miller056ddf72001-03-14 10:15:20 +1100228 xfree(patterns);
229 goto bad_option;
Damien Millerf6d9e222000-06-18 14:50:44 +1000230 }
231 patterns[i] = 0;
Damien Miller33804262001-02-04 23:20:18 +1100232 opts++;
Ben Lindstromf0c50292001-06-25 05:17:53 +0000233 if (match_host_and_ip(remote_host, remote_ip,
234 patterns) != 1) {
235 xfree(patterns);
Damien Miller33804262001-02-04 23:20:18 +1100236 log("Authentication tried for %.100s with "
237 "correct key but not from a permitted "
238 "host (host=%.200s, ip=%.200s).",
239 pw->pw_name, remote_host, remote_ip);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000240 snprintf(tmp, sizeof(tmp),
241 "Your host '%.200s' is not "
Damien Miller33804262001-02-04 23:20:18 +1100242 "permitted to use this key for login.",
243 remote_host);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000244 buffer_put_cstring(&auth_debug, tmp);
Damien Millerf6d9e222000-06-18 14:50:44 +1000245 /* deny access */
246 return 0;
247 }
Ben Lindstromf0c50292001-06-25 05:17:53 +0000248 xfree(patterns);
Damien Millerf6d9e222000-06-18 14:50:44 +1000249 /* Host name matches. */
250 goto next_option;
251 }
Ben Lindstrom7bb8b492001-03-17 00:47:54 +0000252 cp = "permitopen=\"";
253 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
Ben Lindstromd71ba572001-09-12 18:03:31 +0000254 char host[256], sport[6];
Ben Lindstrom7bb8b492001-03-17 00:47:54 +0000255 u_short port;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +0000256 char *patterns = xmalloc(strlen(opts) + 1);
257
258 opts += strlen(cp);
259 i = 0;
260 while (*opts) {
261 if (*opts == '"')
262 break;
263 if (*opts == '\\' && opts[1] == '"') {
264 opts += 2;
265 patterns[i++] = '"';
266 continue;
267 }
268 patterns[i++] = *opts++;
269 }
270 if (!*opts) {
271 debug("%.100s, line %lu: missing end quote",
272 file, linenum);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000273 snprintf(tmp, sizeof(tmp), "%.100s, line %lu: missing end quote",
Ben Lindstrom7bb8b492001-03-17 00:47:54 +0000274 file, linenum);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000275 buffer_put_cstring(&auth_debug, tmp);
Ben Lindstrom7bb8b492001-03-17 00:47:54 +0000276 xfree(patterns);
277 goto bad_option;
278 }
279 patterns[i] = 0;
280 opts++;
Ben Lindstromd71ba572001-09-12 18:03:31 +0000281 if (sscanf(patterns, "%255[^:]:%5[0-9]", host, sport) != 2 &&
282 sscanf(patterns, "%255[^/]/%5[0-9]", host, sport) != 2) {
283 debug("%.100s, line %lu: Bad permitopen specification "
284 "<%.100s>", file, linenum, patterns);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000285 snprintf(tmp, sizeof(tmp), "%.100s, line %lu: "
Ben Lindstromd71ba572001-09-12 18:03:31 +0000286 "Bad permitopen specification", file, linenum);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000287 buffer_put_cstring(&auth_debug, tmp);
Ben Lindstrom7bb8b492001-03-17 00:47:54 +0000288 xfree(patterns);
289 goto bad_option;
290 }
Ben Lindstromd71ba572001-09-12 18:03:31 +0000291 if ((port = a2port(sport)) == 0) {
292 debug("%.100s, line %lu: Bad permitopen port <%.100s>",
293 file, linenum, sport);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000294 snprintf(tmp, sizeof(tmp), "%.100s, line %lu: "
Ben Lindstromd71ba572001-09-12 18:03:31 +0000295 "Bad permitopen port", file, linenum);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000296 buffer_put_cstring(&auth_debug, tmp);
Ben Lindstrom7bb8b492001-03-17 00:47:54 +0000297 xfree(patterns);
298 goto bad_option;
299 }
Ben Lindstrom2d70f982001-03-19 00:13:46 +0000300 if (options.allow_tcp_forwarding)
Ben Lindstromd71ba572001-09-12 18:03:31 +0000301 channel_add_permitted_opens(host, port);
Ben Lindstrom7bb8b492001-03-17 00:47:54 +0000302 xfree(patterns);
303 goto next_option;
304 }
Damien Millerf6d9e222000-06-18 14:50:44 +1000305next_option:
306 /*
307 * Skip the comma, and move to the next option
308 * (or break out if there are no more).
309 */
Damien Miller33804262001-02-04 23:20:18 +1100310 if (!*opts)
Damien Millerf6d9e222000-06-18 14:50:44 +1000311 fatal("Bugs in auth-options.c option processing.");
Damien Miller33804262001-02-04 23:20:18 +1100312 if (*opts == ' ' || *opts == '\t')
Damien Millerf6d9e222000-06-18 14:50:44 +1000313 break; /* End of options. */
Damien Miller33804262001-02-04 23:20:18 +1100314 if (*opts != ',')
Damien Millerf6d9e222000-06-18 14:50:44 +1000315 goto bad_option;
Damien Miller33804262001-02-04 23:20:18 +1100316 opts++;
Damien Millerf6d9e222000-06-18 14:50:44 +1000317 /* Process the next option. */
318 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000319
320 if (!use_privsep)
321 auth_send_debug(&auth_debug);
322
Damien Millerf6d9e222000-06-18 14:50:44 +1000323 /* grant access */
324 return 1;
325
326bad_option:
327 log("Bad options in %.100s file, line %lu: %.50s",
Damien Miller33804262001-02-04 23:20:18 +1100328 file, linenum, opts);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000329 snprintf(tmp, sizeof(tmp),
330 "Bad options in %.100s file, line %lu: %.50s",
Damien Miller33804262001-02-04 23:20:18 +1100331 file, linenum, opts);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000332 buffer_put_cstring(&auth_debug, tmp);
333
334 if (!use_privsep)
335 auth_send_debug(&auth_debug);
336
Damien Millerf6d9e222000-06-18 14:50:44 +1000337 /* deny access */
338 return 0;
339}