blob: 77488a49dfbe6815e0a3a61ba4b87d4985b4f130 [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 Millerf6d9e222000-06-18 14:50:44 +100013
Damien Millerf6d9e222000-06-18 14:50:44 +100014#include "xmalloc.h"
15#include "match.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000016#include "log.h"
17#include "canohost.h"
Ben Lindstromc7637672001-06-09 00:36:26 +000018#include "channels.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000019#include "auth-options.h"
Damien Miller33804262001-02-04 23:20:18 +110020#include "servconf.h"
Ben Lindstromd71ba572001-09-12 18:03:31 +000021#include "misc.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000022#include "monitor_wrap.h"
Ben Lindstroma574cda2002-05-15 16:16:14 +000023#include "auth.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 Millerd27b9472005-12-13 19:29:02 +110037/* "tunnel=" option. */
38int forced_tun_device = -1;
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 }
Damien Millerd27b9472005-12-13 19:29:02 +110059 forced_tun_device = -1;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +000060 channel_clear_permitted_opens();
Ben Lindstroma574cda2002-05-15 16:16:14 +000061 auth_debug_reset();
Damien Miller874d77b2000-10-14 16:23:11 +110062}
63
Ben Lindstrom226cfa02001-01-22 05:34:40 +000064/*
65 * return 1 if access is granted, 0 if not.
66 * side effect: sets key option flags
67 */
Damien Millerf6d9e222000-06-18 14:50:44 +100068int
Damien Miller33804262001-02-04 23:20:18 +110069auth_parse_options(struct passwd *pw, char *opts, char *file, u_long linenum)
Damien Millerf6d9e222000-06-18 14:50:44 +100070{
71 const char *cp;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +000072 int i;
Damien Miller874d77b2000-10-14 16:23:11 +110073
74 /* reset options */
75 auth_clear_options();
76
Ben Lindstrom36d7bd02001-02-10 22:27:19 +000077 if (!opts)
78 return 1;
79
Damien Miller33804262001-02-04 23:20:18 +110080 while (*opts && *opts != ' ' && *opts != '\t') {
Damien Millerf6d9e222000-06-18 14:50:44 +100081 cp = "no-port-forwarding";
Damien Miller33804262001-02-04 23:20:18 +110082 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
Ben Lindstroma574cda2002-05-15 16:16:14 +000083 auth_debug_add("Port forwarding disabled.");
Damien Millerf6d9e222000-06-18 14:50:44 +100084 no_port_forwarding_flag = 1;
Damien Miller33804262001-02-04 23:20:18 +110085 opts += strlen(cp);
Damien Millerf6d9e222000-06-18 14:50:44 +100086 goto next_option;
87 }
88 cp = "no-agent-forwarding";
Damien Miller33804262001-02-04 23:20:18 +110089 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
Ben Lindstroma574cda2002-05-15 16:16:14 +000090 auth_debug_add("Agent forwarding disabled.");
Damien Millerf6d9e222000-06-18 14:50:44 +100091 no_agent_forwarding_flag = 1;
Damien Miller33804262001-02-04 23:20:18 +110092 opts += strlen(cp);
Damien Millerf6d9e222000-06-18 14:50:44 +100093 goto next_option;
94 }
95 cp = "no-X11-forwarding";
Damien Miller33804262001-02-04 23:20:18 +110096 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
Ben Lindstroma574cda2002-05-15 16:16:14 +000097 auth_debug_add("X11 forwarding disabled.");
Damien Millerf6d9e222000-06-18 14:50:44 +100098 no_x11_forwarding_flag = 1;
Damien Miller33804262001-02-04 23:20:18 +110099 opts += strlen(cp);
Damien Millerf6d9e222000-06-18 14:50:44 +1000100 goto next_option;
101 }
102 cp = "no-pty";
Damien Miller33804262001-02-04 23:20:18 +1100103 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
Ben Lindstroma574cda2002-05-15 16:16:14 +0000104 auth_debug_add("Pty allocation disabled.");
Damien Millerf6d9e222000-06-18 14:50:44 +1000105 no_pty_flag = 1;
Damien Miller33804262001-02-04 23:20:18 +1100106 opts += strlen(cp);
Damien Millerf6d9e222000-06-18 14:50:44 +1000107 goto next_option;
108 }
109 cp = "command=\"";
Damien Miller33804262001-02-04 23:20:18 +1100110 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
Damien Miller33804262001-02-04 23:20:18 +1100111 opts += strlen(cp);
112 forced_command = xmalloc(strlen(opts) + 1);
Damien Millerf6d9e222000-06-18 14:50:44 +1000113 i = 0;
Damien Miller33804262001-02-04 23:20:18 +1100114 while (*opts) {
115 if (*opts == '"')
Damien Millerf6d9e222000-06-18 14:50:44 +1000116 break;
Damien Miller33804262001-02-04 23:20:18 +1100117 if (*opts == '\\' && opts[1] == '"') {
118 opts += 2;
Damien Millerf6d9e222000-06-18 14:50:44 +1000119 forced_command[i++] = '"';
120 continue;
121 }
Damien Miller33804262001-02-04 23:20:18 +1100122 forced_command[i++] = *opts++;
Damien Millerf6d9e222000-06-18 14:50:44 +1000123 }
Damien Miller33804262001-02-04 23:20:18 +1100124 if (!*opts) {
Damien Millerf6d9e222000-06-18 14:50:44 +1000125 debug("%.100s, line %lu: missing end quote",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000126 file, linenum);
Ben Lindstroma574cda2002-05-15 16:16:14 +0000127 auth_debug_add("%.100s, line %lu: missing end quote",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000128 file, linenum);
Damien Miller056ddf72001-03-14 10:15:20 +1100129 xfree(forced_command);
130 forced_command = NULL;
131 goto bad_option;
Damien Millerf6d9e222000-06-18 14:50:44 +1000132 }
133 forced_command[i] = 0;
Ben Lindstroma574cda2002-05-15 16:16:14 +0000134 auth_debug_add("Forced command: %.900s", forced_command);
Damien Miller33804262001-02-04 23:20:18 +1100135 opts++;
Damien Millerf6d9e222000-06-18 14:50:44 +1000136 goto next_option;
137 }
138 cp = "environment=\"";
Ben Lindstrom5d860f02002-08-01 01:28:38 +0000139 if (options.permit_user_env &&
140 strncasecmp(opts, cp, strlen(cp)) == 0) {
Damien Millerf6d9e222000-06-18 14:50:44 +1000141 char *s;
142 struct envstring *new_envstring;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +0000143
Damien Miller33804262001-02-04 23:20:18 +1100144 opts += strlen(cp);
145 s = xmalloc(strlen(opts) + 1);
Damien Millerf6d9e222000-06-18 14:50:44 +1000146 i = 0;
Damien Miller33804262001-02-04 23:20:18 +1100147 while (*opts) {
148 if (*opts == '"')
Damien Millerf6d9e222000-06-18 14:50:44 +1000149 break;
Damien Miller33804262001-02-04 23:20:18 +1100150 if (*opts == '\\' && opts[1] == '"') {
151 opts += 2;
Damien Millerf6d9e222000-06-18 14:50:44 +1000152 s[i++] = '"';
153 continue;
154 }
Damien Miller33804262001-02-04 23:20:18 +1100155 s[i++] = *opts++;
Damien Millerf6d9e222000-06-18 14:50:44 +1000156 }
Damien Miller33804262001-02-04 23:20:18 +1100157 if (!*opts) {
Damien Millerf6d9e222000-06-18 14:50:44 +1000158 debug("%.100s, line %lu: missing end quote",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000159 file, linenum);
Ben Lindstroma574cda2002-05-15 16:16:14 +0000160 auth_debug_add("%.100s, line %lu: missing end quote",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000161 file, linenum);
Damien Miller056ddf72001-03-14 10:15:20 +1100162 xfree(s);
163 goto bad_option;
Damien Millerf6d9e222000-06-18 14:50:44 +1000164 }
165 s[i] = 0;
Ben Lindstroma574cda2002-05-15 16:16:14 +0000166 auth_debug_add("Adding to environment: %.900s", s);
Damien Millerf6d9e222000-06-18 14:50:44 +1000167 debug("Adding to environment: %.900s", s);
Damien Miller33804262001-02-04 23:20:18 +1100168 opts++;
Damien Millerf6d9e222000-06-18 14:50:44 +1000169 new_envstring = xmalloc(sizeof(struct envstring));
170 new_envstring->s = s;
171 new_envstring->next = custom_environment;
172 custom_environment = new_envstring;
173 goto next_option;
174 }
175 cp = "from=\"";
Damien Miller33804262001-02-04 23:20:18 +1100176 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
Damien Miller33804262001-02-04 23:20:18 +1100177 const char *remote_ip = get_remote_ipaddr();
178 const char *remote_host = get_canonical_hostname(
Damien Miller3a961dc2003-06-03 10:25:48 +1000179 options.use_dns);
Damien Miller33804262001-02-04 23:20:18 +1100180 char *patterns = xmalloc(strlen(opts) + 1);
Ben Lindstrom7bb8b492001-03-17 00:47:54 +0000181
Damien Miller33804262001-02-04 23:20:18 +1100182 opts += strlen(cp);
Damien Millerf6d9e222000-06-18 14:50:44 +1000183 i = 0;
Damien Miller33804262001-02-04 23:20:18 +1100184 while (*opts) {
185 if (*opts == '"')
Damien Millerf6d9e222000-06-18 14:50:44 +1000186 break;
Damien Miller33804262001-02-04 23:20:18 +1100187 if (*opts == '\\' && opts[1] == '"') {
188 opts += 2;
Damien Millerf6d9e222000-06-18 14:50:44 +1000189 patterns[i++] = '"';
190 continue;
191 }
Damien Miller33804262001-02-04 23:20:18 +1100192 patterns[i++] = *opts++;
Damien Millerf6d9e222000-06-18 14:50:44 +1000193 }
Damien Miller33804262001-02-04 23:20:18 +1100194 if (!*opts) {
Damien Millerf6d9e222000-06-18 14:50:44 +1000195 debug("%.100s, line %lu: missing end quote",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000196 file, linenum);
Ben Lindstroma574cda2002-05-15 16:16:14 +0000197 auth_debug_add("%.100s, line %lu: missing end quote",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000198 file, linenum);
Damien Miller056ddf72001-03-14 10:15:20 +1100199 xfree(patterns);
200 goto bad_option;
Damien Millerf6d9e222000-06-18 14:50:44 +1000201 }
202 patterns[i] = 0;
Damien Miller33804262001-02-04 23:20:18 +1100203 opts++;
Ben Lindstromf0c50292001-06-25 05:17:53 +0000204 if (match_host_and_ip(remote_host, remote_ip,
205 patterns) != 1) {
206 xfree(patterns);
Damien Miller996acd22003-04-09 20:59:48 +1000207 logit("Authentication tried for %.100s with "
Damien Miller33804262001-02-04 23:20:18 +1100208 "correct key but not from a permitted "
209 "host (host=%.200s, ip=%.200s).",
210 pw->pw_name, remote_host, remote_ip);
Ben Lindstroma574cda2002-05-15 16:16:14 +0000211 auth_debug_add("Your host '%.200s' is not "
Damien Miller33804262001-02-04 23:20:18 +1100212 "permitted to use this key for login.",
213 remote_host);
Damien Millerf6d9e222000-06-18 14:50:44 +1000214 /* deny access */
215 return 0;
216 }
Ben Lindstromf0c50292001-06-25 05:17:53 +0000217 xfree(patterns);
Damien Millerf6d9e222000-06-18 14:50:44 +1000218 /* Host name matches. */
219 goto next_option;
220 }
Ben Lindstrom7bb8b492001-03-17 00:47:54 +0000221 cp = "permitopen=\"";
222 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
Damien Millerf91ee4c2005-03-01 21:24:33 +1100223 char *host, *p;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +0000224 u_short port;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +0000225 char *patterns = xmalloc(strlen(opts) + 1);
226
227 opts += strlen(cp);
228 i = 0;
229 while (*opts) {
230 if (*opts == '"')
231 break;
232 if (*opts == '\\' && opts[1] == '"') {
233 opts += 2;
234 patterns[i++] = '"';
235 continue;
236 }
237 patterns[i++] = *opts++;
238 }
239 if (!*opts) {
240 debug("%.100s, line %lu: missing end quote",
241 file, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100242 auth_debug_add("%.100s, line %lu: missing "
243 "end quote", file, linenum);
Ben Lindstrom7bb8b492001-03-17 00:47:54 +0000244 xfree(patterns);
245 goto bad_option;
246 }
247 patterns[i] = 0;
248 opts++;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100249 p = patterns;
250 host = hpdelim(&p);
251 if (host == NULL || strlen(host) >= NI_MAXHOST) {
252 debug("%.100s, line %lu: Bad permitopen "
Darren Tucker90b9e022005-03-14 23:08:50 +1100253 "specification <%.100s>", file, linenum,
Damien Millerf91ee4c2005-03-01 21:24:33 +1100254 patterns);
Ben Lindstroma574cda2002-05-15 16:16:14 +0000255 auth_debug_add("%.100s, line %lu: "
Damien Millerf91ee4c2005-03-01 21:24:33 +1100256 "Bad permitopen specification", file,
257 linenum);
Ben Lindstrom7bb8b492001-03-17 00:47:54 +0000258 xfree(patterns);
259 goto bad_option;
260 }
Darren Tucker47eede72005-03-14 23:08:12 +1100261 host = cleanhostname(host);
262 if (p == NULL || (port = a2port(p)) == 0) {
Damien Millerf91ee4c2005-03-01 21:24:33 +1100263 debug("%.100s, line %lu: Bad permitopen port "
264 "<%.100s>", file, linenum, p ? p : "");
Ben Lindstroma574cda2002-05-15 16:16:14 +0000265 auth_debug_add("%.100s, line %lu: "
Ben Lindstromd71ba572001-09-12 18:03:31 +0000266 "Bad permitopen port", file, linenum);
Ben Lindstrom7bb8b492001-03-17 00:47:54 +0000267 xfree(patterns);
268 goto bad_option;
269 }
Ben Lindstrom2d70f982001-03-19 00:13:46 +0000270 if (options.allow_tcp_forwarding)
Ben Lindstromd71ba572001-09-12 18:03:31 +0000271 channel_add_permitted_opens(host, port);
Ben Lindstrom7bb8b492001-03-17 00:47:54 +0000272 xfree(patterns);
273 goto next_option;
274 }
Damien Millerd27b9472005-12-13 19:29:02 +1100275 cp = "tunnel=\"";
276 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
277 char *tun = NULL;
278 opts += strlen(cp);
279 tun = xmalloc(strlen(opts) + 1);
280 i = 0;
281 while (*opts) {
282 if (*opts == '"')
283 break;
284 tun[i++] = *opts++;
285 }
286 if (!*opts) {
287 debug("%.100s, line %lu: missing end quote",
288 file, linenum);
289 auth_debug_add("%.100s, line %lu: missing end quote",
290 file, linenum);
291 xfree(tun);
292 forced_tun_device = -1;
293 goto bad_option;
294 }
295 tun[i] = 0;
296 forced_tun_device = a2tun(tun, NULL);
297 xfree(tun);
Damien Miller7b58e802005-12-13 19:33:19 +1100298 if (forced_tun_device == SSH_TUNID_ERR) {
Damien Millerd27b9472005-12-13 19:29:02 +1100299 debug("%.100s, line %lu: invalid tun device",
300 file, linenum);
301 auth_debug_add("%.100s, line %lu: invalid tun device",
302 file, linenum);
303 forced_tun_device = -1;
304 goto bad_option;
305 }
306 auth_debug_add("Forced tun device: %d", forced_tun_device);
307 opts++;
308 goto next_option;
309 }
Damien Millerf6d9e222000-06-18 14:50:44 +1000310next_option:
311 /*
312 * Skip the comma, and move to the next option
313 * (or break out if there are no more).
314 */
Damien Miller33804262001-02-04 23:20:18 +1100315 if (!*opts)
Damien Millerf6d9e222000-06-18 14:50:44 +1000316 fatal("Bugs in auth-options.c option processing.");
Damien Miller33804262001-02-04 23:20:18 +1100317 if (*opts == ' ' || *opts == '\t')
Damien Millerf6d9e222000-06-18 14:50:44 +1000318 break; /* End of options. */
Damien Miller33804262001-02-04 23:20:18 +1100319 if (*opts != ',')
Damien Millerf6d9e222000-06-18 14:50:44 +1000320 goto bad_option;
Damien Miller33804262001-02-04 23:20:18 +1100321 opts++;
Damien Millerf6d9e222000-06-18 14:50:44 +1000322 /* Process the next option. */
323 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000324
325 if (!use_privsep)
Ben Lindstroma574cda2002-05-15 16:16:14 +0000326 auth_debug_send();
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000327
Damien Millerf6d9e222000-06-18 14:50:44 +1000328 /* grant access */
329 return 1;
330
331bad_option:
Damien Miller996acd22003-04-09 20:59:48 +1000332 logit("Bad options in %.100s file, line %lu: %.50s",
Damien Miller33804262001-02-04 23:20:18 +1100333 file, linenum, opts);
Ben Lindstroma574cda2002-05-15 16:16:14 +0000334 auth_debug_add("Bad options in %.100s file, line %lu: %.50s",
Damien Miller33804262001-02-04 23:20:18 +1100335 file, linenum, opts);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000336
337 if (!use_privsep)
Ben Lindstroma574cda2002-05-15 16:16:14 +0000338 auth_debug_send();
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000339
Damien Millerf6d9e222000-06-18 14:50:44 +1000340 /* deny access */
341 return 0;
342}