blob: 83ef02c42447ef8ebaf27a7c18167903e48f07d8 [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 Lindstromf0c50292001-06-25 05:17:53 +000013RCSID("$OpenBSD: auth-options.c,v 1.19 2001/06/24 05:25:09 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"
Damien Millerf6d9e222000-06-18 14:50:44 +100023
24/* Flags set authorized_keys flags */
25int no_port_forwarding_flag = 0;
26int no_agent_forwarding_flag = 0;
27int no_x11_forwarding_flag = 0;
28int no_pty_flag = 0;
29
30/* "command=" option. */
31char *forced_command = NULL;
32
33/* "environment=" options. */
34struct envstring *custom_environment = NULL;
35
Damien Miller33804262001-02-04 23:20:18 +110036extern ServerOptions options;
37
Damien Miller874d77b2000-10-14 16:23:11 +110038void
39auth_clear_options(void)
40{
41 no_agent_forwarding_flag = 0;
42 no_port_forwarding_flag = 0;
43 no_pty_flag = 0;
44 no_x11_forwarding_flag = 0;
45 while (custom_environment) {
46 struct envstring *ce = custom_environment;
47 custom_environment = ce->next;
48 xfree(ce->s);
49 xfree(ce);
50 }
51 if (forced_command) {
52 xfree(forced_command);
53 forced_command = NULL;
54 }
Ben Lindstrom7bb8b492001-03-17 00:47:54 +000055 channel_clear_permitted_opens();
Damien Miller874d77b2000-10-14 16:23:11 +110056}
57
Ben Lindstrom226cfa02001-01-22 05:34:40 +000058/*
59 * return 1 if access is granted, 0 if not.
60 * side effect: sets key option flags
61 */
Damien Millerf6d9e222000-06-18 14:50:44 +100062int
Damien Miller33804262001-02-04 23:20:18 +110063auth_parse_options(struct passwd *pw, char *opts, char *file, u_long linenum)
Damien Millerf6d9e222000-06-18 14:50:44 +100064{
65 const char *cp;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +000066 int i;
Damien Miller874d77b2000-10-14 16:23:11 +110067
68 /* reset options */
69 auth_clear_options();
70
Ben Lindstrom36d7bd02001-02-10 22:27:19 +000071 if (!opts)
72 return 1;
73
Damien Miller33804262001-02-04 23:20:18 +110074 while (*opts && *opts != ' ' && *opts != '\t') {
Damien Millerf6d9e222000-06-18 14:50:44 +100075 cp = "no-port-forwarding";
Damien Miller33804262001-02-04 23:20:18 +110076 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
Damien Millerf6d9e222000-06-18 14:50:44 +100077 packet_send_debug("Port forwarding disabled.");
78 no_port_forwarding_flag = 1;
Damien Miller33804262001-02-04 23:20:18 +110079 opts += strlen(cp);
Damien Millerf6d9e222000-06-18 14:50:44 +100080 goto next_option;
81 }
82 cp = "no-agent-forwarding";
Damien Miller33804262001-02-04 23:20:18 +110083 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
Damien Millerf6d9e222000-06-18 14:50:44 +100084 packet_send_debug("Agent forwarding disabled.");
85 no_agent_forwarding_flag = 1;
Damien Miller33804262001-02-04 23:20:18 +110086 opts += strlen(cp);
Damien Millerf6d9e222000-06-18 14:50:44 +100087 goto next_option;
88 }
89 cp = "no-X11-forwarding";
Damien Miller33804262001-02-04 23:20:18 +110090 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
Damien Millerf6d9e222000-06-18 14:50:44 +100091 packet_send_debug("X11 forwarding disabled.");
92 no_x11_forwarding_flag = 1;
Damien Miller33804262001-02-04 23:20:18 +110093 opts += strlen(cp);
Damien Millerf6d9e222000-06-18 14:50:44 +100094 goto next_option;
95 }
96 cp = "no-pty";
Damien Miller33804262001-02-04 23:20:18 +110097 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
Damien Millerf6d9e222000-06-18 14:50:44 +100098 packet_send_debug("Pty allocation disabled.");
99 no_pty_flag = 1;
Damien Miller33804262001-02-04 23:20:18 +1100100 opts += strlen(cp);
Damien Millerf6d9e222000-06-18 14:50:44 +1000101 goto next_option;
102 }
103 cp = "command=\"";
Damien Miller33804262001-02-04 23:20:18 +1100104 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
Damien Miller33804262001-02-04 23:20:18 +1100105 opts += strlen(cp);
106 forced_command = xmalloc(strlen(opts) + 1);
Damien Millerf6d9e222000-06-18 14:50:44 +1000107 i = 0;
Damien Miller33804262001-02-04 23:20:18 +1100108 while (*opts) {
109 if (*opts == '"')
Damien Millerf6d9e222000-06-18 14:50:44 +1000110 break;
Damien Miller33804262001-02-04 23:20:18 +1100111 if (*opts == '\\' && opts[1] == '"') {
112 opts += 2;
Damien Millerf6d9e222000-06-18 14:50:44 +1000113 forced_command[i++] = '"';
114 continue;
115 }
Damien Miller33804262001-02-04 23:20:18 +1100116 forced_command[i++] = *opts++;
Damien Millerf6d9e222000-06-18 14:50:44 +1000117 }
Damien Miller33804262001-02-04 23:20:18 +1100118 if (!*opts) {
Damien Millerf6d9e222000-06-18 14:50:44 +1000119 debug("%.100s, line %lu: missing end quote",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000120 file, linenum);
Damien Millerf6d9e222000-06-18 14:50:44 +1000121 packet_send_debug("%.100s, line %lu: missing end quote",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000122 file, linenum);
Damien Miller056ddf72001-03-14 10:15:20 +1100123 xfree(forced_command);
124 forced_command = NULL;
125 goto bad_option;
Damien Millerf6d9e222000-06-18 14:50:44 +1000126 }
127 forced_command[i] = 0;
128 packet_send_debug("Forced command: %.900s", forced_command);
Damien Miller33804262001-02-04 23:20:18 +1100129 opts++;
Damien Millerf6d9e222000-06-18 14:50:44 +1000130 goto next_option;
131 }
132 cp = "environment=\"";
Damien Miller33804262001-02-04 23:20:18 +1100133 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
Damien Millerf6d9e222000-06-18 14:50:44 +1000134 char *s;
135 struct envstring *new_envstring;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +0000136
Damien Miller33804262001-02-04 23:20:18 +1100137 opts += strlen(cp);
138 s = xmalloc(strlen(opts) + 1);
Damien Millerf6d9e222000-06-18 14:50:44 +1000139 i = 0;
Damien Miller33804262001-02-04 23:20:18 +1100140 while (*opts) {
141 if (*opts == '"')
Damien Millerf6d9e222000-06-18 14:50:44 +1000142 break;
Damien Miller33804262001-02-04 23:20:18 +1100143 if (*opts == '\\' && opts[1] == '"') {
144 opts += 2;
Damien Millerf6d9e222000-06-18 14:50:44 +1000145 s[i++] = '"';
146 continue;
147 }
Damien Miller33804262001-02-04 23:20:18 +1100148 s[i++] = *opts++;
Damien Millerf6d9e222000-06-18 14:50:44 +1000149 }
Damien Miller33804262001-02-04 23:20:18 +1100150 if (!*opts) {
Damien Millerf6d9e222000-06-18 14:50:44 +1000151 debug("%.100s, line %lu: missing end quote",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000152 file, linenum);
Damien Millerf6d9e222000-06-18 14:50:44 +1000153 packet_send_debug("%.100s, line %lu: missing end quote",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000154 file, linenum);
Damien Miller056ddf72001-03-14 10:15:20 +1100155 xfree(s);
156 goto bad_option;
Damien Millerf6d9e222000-06-18 14:50:44 +1000157 }
158 s[i] = 0;
159 packet_send_debug("Adding to environment: %.900s", s);
160 debug("Adding to environment: %.900s", s);
Damien Miller33804262001-02-04 23:20:18 +1100161 opts++;
Damien Millerf6d9e222000-06-18 14:50:44 +1000162 new_envstring = xmalloc(sizeof(struct envstring));
163 new_envstring->s = s;
164 new_envstring->next = custom_environment;
165 custom_environment = new_envstring;
166 goto next_option;
167 }
168 cp = "from=\"";
Damien Miller33804262001-02-04 23:20:18 +1100169 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
Damien Miller33804262001-02-04 23:20:18 +1100170 const char *remote_ip = get_remote_ipaddr();
171 const char *remote_host = get_canonical_hostname(
172 options.reverse_mapping_check);
173 char *patterns = xmalloc(strlen(opts) + 1);
Ben Lindstrom7bb8b492001-03-17 00:47:54 +0000174
Damien Miller33804262001-02-04 23:20:18 +1100175 opts += strlen(cp);
Damien Millerf6d9e222000-06-18 14:50:44 +1000176 i = 0;
Damien Miller33804262001-02-04 23:20:18 +1100177 while (*opts) {
178 if (*opts == '"')
Damien Millerf6d9e222000-06-18 14:50:44 +1000179 break;
Damien Miller33804262001-02-04 23:20:18 +1100180 if (*opts == '\\' && opts[1] == '"') {
181 opts += 2;
Damien Millerf6d9e222000-06-18 14:50:44 +1000182 patterns[i++] = '"';
183 continue;
184 }
Damien Miller33804262001-02-04 23:20:18 +1100185 patterns[i++] = *opts++;
Damien Millerf6d9e222000-06-18 14:50:44 +1000186 }
Damien Miller33804262001-02-04 23:20:18 +1100187 if (!*opts) {
Damien Millerf6d9e222000-06-18 14:50:44 +1000188 debug("%.100s, line %lu: missing end quote",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000189 file, linenum);
Damien Millerf6d9e222000-06-18 14:50:44 +1000190 packet_send_debug("%.100s, line %lu: missing end quote",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000191 file, linenum);
Damien Miller056ddf72001-03-14 10:15:20 +1100192 xfree(patterns);
193 goto bad_option;
Damien Millerf6d9e222000-06-18 14:50:44 +1000194 }
195 patterns[i] = 0;
Damien Miller33804262001-02-04 23:20:18 +1100196 opts++;
Ben Lindstromf0c50292001-06-25 05:17:53 +0000197 if (match_host_and_ip(remote_host, remote_ip,
198 patterns) != 1) {
199 xfree(patterns);
Damien Miller33804262001-02-04 23:20:18 +1100200 log("Authentication tried for %.100s with "
201 "correct key but not from a permitted "
202 "host (host=%.200s, ip=%.200s).",
203 pw->pw_name, remote_host, remote_ip);
204 packet_send_debug("Your host '%.200s' is not "
205 "permitted to use this key for login.",
206 remote_host);
Damien Millerf6d9e222000-06-18 14:50:44 +1000207 /* deny access */
208 return 0;
209 }
Ben Lindstromf0c50292001-06-25 05:17:53 +0000210 xfree(patterns);
Damien Millerf6d9e222000-06-18 14:50:44 +1000211 /* Host name matches. */
212 goto next_option;
213 }
Ben Lindstrom7bb8b492001-03-17 00:47:54 +0000214 cp = "permitopen=\"";
215 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
216 u_short port;
217 char *c, *ep;
218 char *patterns = xmalloc(strlen(opts) + 1);
219
220 opts += strlen(cp);
221 i = 0;
222 while (*opts) {
223 if (*opts == '"')
224 break;
225 if (*opts == '\\' && opts[1] == '"') {
226 opts += 2;
227 patterns[i++] = '"';
228 continue;
229 }
230 patterns[i++] = *opts++;
231 }
232 if (!*opts) {
233 debug("%.100s, line %lu: missing end quote",
234 file, linenum);
235 packet_send_debug("%.100s, line %lu: missing end quote",
236 file, linenum);
237 xfree(patterns);
238 goto bad_option;
239 }
240 patterns[i] = 0;
241 opts++;
242 c = strchr(patterns, ':');
243 if (c == NULL) {
244 debug("%.100s, line %lu: permitopen: missing colon <%.100s>",
245 file, linenum, patterns);
246 packet_send_debug("%.100s, line %lu: missing colon",
247 file, linenum);
248 xfree(patterns);
249 goto bad_option;
250 }
251 *c = 0;
252 c++;
253 port = strtol(c, &ep, 0);
254 if (c == ep) {
255 debug("%.100s, line %lu: permitopen: missing port <%.100s>",
256 file, linenum, patterns);
257 packet_send_debug("%.100s, line %lu: missing port",
258 file, linenum);
259 xfree(patterns);
260 goto bad_option;
261 }
Ben Lindstrom2d70f982001-03-19 00:13:46 +0000262 if (options.allow_tcp_forwarding)
263 channel_add_permitted_opens(patterns, port);
Ben Lindstrom7bb8b492001-03-17 00:47:54 +0000264 xfree(patterns);
265 goto next_option;
266 }
Damien Millerf6d9e222000-06-18 14:50:44 +1000267next_option:
268 /*
269 * Skip the comma, and move to the next option
270 * (or break out if there are no more).
271 */
Damien Miller33804262001-02-04 23:20:18 +1100272 if (!*opts)
Damien Millerf6d9e222000-06-18 14:50:44 +1000273 fatal("Bugs in auth-options.c option processing.");
Damien Miller33804262001-02-04 23:20:18 +1100274 if (*opts == ' ' || *opts == '\t')
Damien Millerf6d9e222000-06-18 14:50:44 +1000275 break; /* End of options. */
Damien Miller33804262001-02-04 23:20:18 +1100276 if (*opts != ',')
Damien Millerf6d9e222000-06-18 14:50:44 +1000277 goto bad_option;
Damien Miller33804262001-02-04 23:20:18 +1100278 opts++;
Damien Millerf6d9e222000-06-18 14:50:44 +1000279 /* Process the next option. */
280 }
281 /* grant access */
282 return 1;
283
284bad_option:
285 log("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 packet_send_debug("Bad options in %.100s file, line %lu: %.50s",
Damien Miller33804262001-02-04 23:20:18 +1100288 file, linenum, opts);
Damien Millerf6d9e222000-06-18 14:50:44 +1000289 /* deny access */
290 return 0;
291}