blob: cd904967af44c4afb2f368e1010056d214749afd [file] [log] [blame]
Damien Millere3476ed2006-07-24 14:13:33 +10001/* $OpenBSD: auth-options.c,v 1.39 2006/07/22 20:48:22 stevesk Exp $ */
Damien Millere4340be2000-09-16 13:29:08 +11002/*
3 * Author: Tatu Ylonen <ylo@cs.hut.fi>
4 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5 * All rights reserved
Damien Millere4340be2000-09-16 13:29:08 +11006 * As far as I am concerned, the code I have written for this software
7 * can be used freely for any purpose. Any derived versions of this
8 * software must be clearly marked as such, and if the derived work is
9 * incompatible with the protocol description in the RFC file, it must be
10 * called by a name other than "ssh" or "Secure Shell".
11 */
12
Damien Millerf6d9e222000-06-18 14:50:44 +100013#include "includes.h"
Damien Millerf6d9e222000-06-18 14:50:44 +100014
Damien Miller9f2abc42006-07-10 20:53:08 +100015#include <sys/types.h>
16
Damien Millerb8fe89c2006-07-24 14:51:00 +100017#include <netdb.h>
Damien Miller9f2abc42006-07-10 20:53:08 +100018#include <pwd.h>
Damien Millere3476ed2006-07-24 14:13:33 +100019#include <string.h>
Damien Miller9f2abc42006-07-10 20:53:08 +100020
Damien Millerf6d9e222000-06-18 14:50:44 +100021#include "xmalloc.h"
22#include "match.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000023#include "log.h"
24#include "canohost.h"
Ben Lindstromc7637672001-06-09 00:36:26 +000025#include "channels.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000026#include "auth-options.h"
Damien Miller33804262001-02-04 23:20:18 +110027#include "servconf.h"
Ben Lindstromd71ba572001-09-12 18:03:31 +000028#include "misc.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000029#include "monitor_wrap.h"
Ben Lindstroma574cda2002-05-15 16:16:14 +000030#include "auth.h"
Damien Millerf6d9e222000-06-18 14:50:44 +100031
32/* Flags set authorized_keys flags */
33int no_port_forwarding_flag = 0;
34int no_agent_forwarding_flag = 0;
35int no_x11_forwarding_flag = 0;
36int no_pty_flag = 0;
37
38/* "command=" option. */
39char *forced_command = NULL;
40
41/* "environment=" options. */
42struct envstring *custom_environment = NULL;
43
Damien Millerd27b9472005-12-13 19:29:02 +110044/* "tunnel=" option. */
45int forced_tun_device = -1;
46
Damien Miller33804262001-02-04 23:20:18 +110047extern ServerOptions options;
48
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000049void
Damien Miller874d77b2000-10-14 16:23:11 +110050auth_clear_options(void)
51{
52 no_agent_forwarding_flag = 0;
53 no_port_forwarding_flag = 0;
54 no_pty_flag = 0;
55 no_x11_forwarding_flag = 0;
56 while (custom_environment) {
57 struct envstring *ce = custom_environment;
58 custom_environment = ce->next;
59 xfree(ce->s);
60 xfree(ce);
61 }
62 if (forced_command) {
63 xfree(forced_command);
64 forced_command = NULL;
65 }
Damien Millerd27b9472005-12-13 19:29:02 +110066 forced_tun_device = -1;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +000067 channel_clear_permitted_opens();
Ben Lindstroma574cda2002-05-15 16:16:14 +000068 auth_debug_reset();
Damien Miller874d77b2000-10-14 16:23:11 +110069}
70
Ben Lindstrom226cfa02001-01-22 05:34:40 +000071/*
72 * return 1 if access is granted, 0 if not.
73 * side effect: sets key option flags
74 */
Damien Millerf6d9e222000-06-18 14:50:44 +100075int
Damien Miller33804262001-02-04 23:20:18 +110076auth_parse_options(struct passwd *pw, char *opts, char *file, u_long linenum)
Damien Millerf6d9e222000-06-18 14:50:44 +100077{
78 const char *cp;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +000079 int i;
Damien Miller874d77b2000-10-14 16:23:11 +110080
81 /* reset options */
82 auth_clear_options();
83
Ben Lindstrom36d7bd02001-02-10 22:27:19 +000084 if (!opts)
85 return 1;
86
Damien Miller33804262001-02-04 23:20:18 +110087 while (*opts && *opts != ' ' && *opts != '\t') {
Damien Millerf6d9e222000-06-18 14:50:44 +100088 cp = "no-port-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("Port forwarding disabled.");
Damien Millerf6d9e222000-06-18 14:50:44 +100091 no_port_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-agent-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("Agent forwarding disabled.");
Damien Millerf6d9e222000-06-18 14:50:44 +100098 no_agent_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-X11-forwarding";
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("X11 forwarding disabled.");
Damien Millerf6d9e222000-06-18 14:50:44 +1000105 no_x11_forwarding_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 = "no-pty";
Damien Miller33804262001-02-04 23:20:18 +1100110 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
Ben Lindstroma574cda2002-05-15 16:16:14 +0000111 auth_debug_add("Pty allocation disabled.");
Damien Millerf6d9e222000-06-18 14:50:44 +1000112 no_pty_flag = 1;
Damien Miller33804262001-02-04 23:20:18 +1100113 opts += strlen(cp);
Damien Millerf6d9e222000-06-18 14:50:44 +1000114 goto next_option;
115 }
116 cp = "command=\"";
Damien Miller33804262001-02-04 23:20:18 +1100117 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
Damien Miller33804262001-02-04 23:20:18 +1100118 opts += strlen(cp);
119 forced_command = xmalloc(strlen(opts) + 1);
Damien Millerf6d9e222000-06-18 14:50:44 +1000120 i = 0;
Damien Miller33804262001-02-04 23:20:18 +1100121 while (*opts) {
122 if (*opts == '"')
Damien Millerf6d9e222000-06-18 14:50:44 +1000123 break;
Damien Miller33804262001-02-04 23:20:18 +1100124 if (*opts == '\\' && opts[1] == '"') {
125 opts += 2;
Damien Millerf6d9e222000-06-18 14:50:44 +1000126 forced_command[i++] = '"';
127 continue;
128 }
Damien Miller33804262001-02-04 23:20:18 +1100129 forced_command[i++] = *opts++;
Damien Millerf6d9e222000-06-18 14:50:44 +1000130 }
Damien Miller33804262001-02-04 23:20:18 +1100131 if (!*opts) {
Damien Millerf6d9e222000-06-18 14:50:44 +1000132 debug("%.100s, line %lu: missing end quote",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000133 file, linenum);
Ben Lindstroma574cda2002-05-15 16:16:14 +0000134 auth_debug_add("%.100s, line %lu: missing end quote",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000135 file, linenum);
Damien Miller056ddf72001-03-14 10:15:20 +1100136 xfree(forced_command);
137 forced_command = NULL;
138 goto bad_option;
Damien Millerf6d9e222000-06-18 14:50:44 +1000139 }
Damien Miller98299262006-07-24 14:01:43 +1000140 forced_command[i] = '\0';
Ben Lindstroma574cda2002-05-15 16:16:14 +0000141 auth_debug_add("Forced command: %.900s", forced_command);
Damien Miller33804262001-02-04 23:20:18 +1100142 opts++;
Damien Millerf6d9e222000-06-18 14:50:44 +1000143 goto next_option;
144 }
145 cp = "environment=\"";
Ben Lindstrom5d860f02002-08-01 01:28:38 +0000146 if (options.permit_user_env &&
147 strncasecmp(opts, cp, strlen(cp)) == 0) {
Damien Millerf6d9e222000-06-18 14:50:44 +1000148 char *s;
149 struct envstring *new_envstring;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +0000150
Damien Miller33804262001-02-04 23:20:18 +1100151 opts += strlen(cp);
152 s = xmalloc(strlen(opts) + 1);
Damien Millerf6d9e222000-06-18 14:50:44 +1000153 i = 0;
Damien Miller33804262001-02-04 23:20:18 +1100154 while (*opts) {
155 if (*opts == '"')
Damien Millerf6d9e222000-06-18 14:50:44 +1000156 break;
Damien Miller33804262001-02-04 23:20:18 +1100157 if (*opts == '\\' && opts[1] == '"') {
158 opts += 2;
Damien Millerf6d9e222000-06-18 14:50:44 +1000159 s[i++] = '"';
160 continue;
161 }
Damien Miller33804262001-02-04 23:20:18 +1100162 s[i++] = *opts++;
Damien Millerf6d9e222000-06-18 14:50:44 +1000163 }
Damien Miller33804262001-02-04 23:20:18 +1100164 if (!*opts) {
Damien Millerf6d9e222000-06-18 14:50:44 +1000165 debug("%.100s, line %lu: missing end quote",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000166 file, linenum);
Ben Lindstroma574cda2002-05-15 16:16:14 +0000167 auth_debug_add("%.100s, line %lu: missing end quote",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000168 file, linenum);
Damien Miller056ddf72001-03-14 10:15:20 +1100169 xfree(s);
170 goto bad_option;
Damien Millerf6d9e222000-06-18 14:50:44 +1000171 }
Damien Miller98299262006-07-24 14:01:43 +1000172 s[i] = '\0';
Ben Lindstroma574cda2002-05-15 16:16:14 +0000173 auth_debug_add("Adding to environment: %.900s", s);
Damien Millerf6d9e222000-06-18 14:50:44 +1000174 debug("Adding to environment: %.900s", s);
Damien Miller33804262001-02-04 23:20:18 +1100175 opts++;
Damien Millerf6d9e222000-06-18 14:50:44 +1000176 new_envstring = xmalloc(sizeof(struct envstring));
177 new_envstring->s = s;
178 new_envstring->next = custom_environment;
179 custom_environment = new_envstring;
180 goto next_option;
181 }
182 cp = "from=\"";
Damien Miller33804262001-02-04 23:20:18 +1100183 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
Damien Miller33804262001-02-04 23:20:18 +1100184 const char *remote_ip = get_remote_ipaddr();
185 const char *remote_host = get_canonical_hostname(
Damien Miller3a961dc2003-06-03 10:25:48 +1000186 options.use_dns);
Damien Miller33804262001-02-04 23:20:18 +1100187 char *patterns = xmalloc(strlen(opts) + 1);
Ben Lindstrom7bb8b492001-03-17 00:47:54 +0000188
Damien Miller33804262001-02-04 23:20:18 +1100189 opts += strlen(cp);
Damien Millerf6d9e222000-06-18 14:50:44 +1000190 i = 0;
Damien Miller33804262001-02-04 23:20:18 +1100191 while (*opts) {
192 if (*opts == '"')
Damien Millerf6d9e222000-06-18 14:50:44 +1000193 break;
Damien Miller33804262001-02-04 23:20:18 +1100194 if (*opts == '\\' && opts[1] == '"') {
195 opts += 2;
Damien Millerf6d9e222000-06-18 14:50:44 +1000196 patterns[i++] = '"';
197 continue;
198 }
Damien Miller33804262001-02-04 23:20:18 +1100199 patterns[i++] = *opts++;
Damien Millerf6d9e222000-06-18 14:50:44 +1000200 }
Damien Miller33804262001-02-04 23:20:18 +1100201 if (!*opts) {
Damien Millerf6d9e222000-06-18 14:50:44 +1000202 debug("%.100s, line %lu: missing end quote",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000203 file, linenum);
Ben Lindstroma574cda2002-05-15 16:16:14 +0000204 auth_debug_add("%.100s, line %lu: missing end quote",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000205 file, linenum);
Damien Miller056ddf72001-03-14 10:15:20 +1100206 xfree(patterns);
207 goto bad_option;
Damien Millerf6d9e222000-06-18 14:50:44 +1000208 }
Damien Miller98299262006-07-24 14:01:43 +1000209 patterns[i] = '\0';
Damien Miller33804262001-02-04 23:20:18 +1100210 opts++;
Ben Lindstromf0c50292001-06-25 05:17:53 +0000211 if (match_host_and_ip(remote_host, remote_ip,
212 patterns) != 1) {
213 xfree(patterns);
Damien Miller996acd22003-04-09 20:59:48 +1000214 logit("Authentication tried for %.100s with "
Damien Miller33804262001-02-04 23:20:18 +1100215 "correct key but not from a permitted "
216 "host (host=%.200s, ip=%.200s).",
217 pw->pw_name, remote_host, remote_ip);
Ben Lindstroma574cda2002-05-15 16:16:14 +0000218 auth_debug_add("Your host '%.200s' is not "
Damien Miller33804262001-02-04 23:20:18 +1100219 "permitted to use this key for login.",
220 remote_host);
Damien Millerf6d9e222000-06-18 14:50:44 +1000221 /* deny access */
222 return 0;
223 }
Ben Lindstromf0c50292001-06-25 05:17:53 +0000224 xfree(patterns);
Damien Millerf6d9e222000-06-18 14:50:44 +1000225 /* Host name matches. */
226 goto next_option;
227 }
Ben Lindstrom7bb8b492001-03-17 00:47:54 +0000228 cp = "permitopen=\"";
229 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
Damien Millerf91ee4c2005-03-01 21:24:33 +1100230 char *host, *p;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +0000231 u_short port;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +0000232 char *patterns = xmalloc(strlen(opts) + 1);
233
234 opts += strlen(cp);
235 i = 0;
236 while (*opts) {
237 if (*opts == '"')
238 break;
239 if (*opts == '\\' && opts[1] == '"') {
240 opts += 2;
241 patterns[i++] = '"';
242 continue;
243 }
244 patterns[i++] = *opts++;
245 }
246 if (!*opts) {
247 debug("%.100s, line %lu: missing end quote",
248 file, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100249 auth_debug_add("%.100s, line %lu: missing "
250 "end quote", file, linenum);
Ben Lindstrom7bb8b492001-03-17 00:47:54 +0000251 xfree(patterns);
252 goto bad_option;
253 }
Damien Miller98299262006-07-24 14:01:43 +1000254 patterns[i] = '\0';
Ben Lindstrom7bb8b492001-03-17 00:47:54 +0000255 opts++;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100256 p = patterns;
257 host = hpdelim(&p);
258 if (host == NULL || strlen(host) >= NI_MAXHOST) {
259 debug("%.100s, line %lu: Bad permitopen "
Darren Tucker90b9e022005-03-14 23:08:50 +1100260 "specification <%.100s>", file, linenum,
Damien Millerf91ee4c2005-03-01 21:24:33 +1100261 patterns);
Ben Lindstroma574cda2002-05-15 16:16:14 +0000262 auth_debug_add("%.100s, line %lu: "
Damien Millerf91ee4c2005-03-01 21:24:33 +1100263 "Bad permitopen specification", file,
264 linenum);
Ben Lindstrom7bb8b492001-03-17 00:47:54 +0000265 xfree(patterns);
266 goto bad_option;
267 }
Darren Tucker47eede72005-03-14 23:08:12 +1100268 host = cleanhostname(host);
269 if (p == NULL || (port = a2port(p)) == 0) {
Damien Millerf91ee4c2005-03-01 21:24:33 +1100270 debug("%.100s, line %lu: Bad permitopen port "
271 "<%.100s>", file, linenum, p ? p : "");
Ben Lindstroma574cda2002-05-15 16:16:14 +0000272 auth_debug_add("%.100s, line %lu: "
Ben Lindstromd71ba572001-09-12 18:03:31 +0000273 "Bad permitopen port", file, linenum);
Ben Lindstrom7bb8b492001-03-17 00:47:54 +0000274 xfree(patterns);
275 goto bad_option;
276 }
Ben Lindstrom2d70f982001-03-19 00:13:46 +0000277 if (options.allow_tcp_forwarding)
Ben Lindstromd71ba572001-09-12 18:03:31 +0000278 channel_add_permitted_opens(host, port);
Ben Lindstrom7bb8b492001-03-17 00:47:54 +0000279 xfree(patterns);
280 goto next_option;
281 }
Damien Millerd27b9472005-12-13 19:29:02 +1100282 cp = "tunnel=\"";
283 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
284 char *tun = NULL;
285 opts += strlen(cp);
286 tun = xmalloc(strlen(opts) + 1);
287 i = 0;
288 while (*opts) {
289 if (*opts == '"')
290 break;
291 tun[i++] = *opts++;
292 }
293 if (!*opts) {
294 debug("%.100s, line %lu: missing end quote",
295 file, linenum);
296 auth_debug_add("%.100s, line %lu: missing end quote",
297 file, linenum);
298 xfree(tun);
299 forced_tun_device = -1;
300 goto bad_option;
301 }
Damien Miller98299262006-07-24 14:01:43 +1000302 tun[i] = '\0';
Damien Millerd27b9472005-12-13 19:29:02 +1100303 forced_tun_device = a2tun(tun, NULL);
304 xfree(tun);
Damien Miller7b58e802005-12-13 19:33:19 +1100305 if (forced_tun_device == SSH_TUNID_ERR) {
Damien Millerd27b9472005-12-13 19:29:02 +1100306 debug("%.100s, line %lu: invalid tun device",
307 file, linenum);
308 auth_debug_add("%.100s, line %lu: invalid tun device",
309 file, linenum);
310 forced_tun_device = -1;
311 goto bad_option;
312 }
313 auth_debug_add("Forced tun device: %d", forced_tun_device);
314 opts++;
315 goto next_option;
316 }
Damien Millerf6d9e222000-06-18 14:50:44 +1000317next_option:
318 /*
319 * Skip the comma, and move to the next option
320 * (or break out if there are no more).
321 */
Damien Miller33804262001-02-04 23:20:18 +1100322 if (!*opts)
Damien Millerf6d9e222000-06-18 14:50:44 +1000323 fatal("Bugs in auth-options.c option processing.");
Damien Miller33804262001-02-04 23:20:18 +1100324 if (*opts == ' ' || *opts == '\t')
Damien Millerf6d9e222000-06-18 14:50:44 +1000325 break; /* End of options. */
Damien Miller33804262001-02-04 23:20:18 +1100326 if (*opts != ',')
Damien Millerf6d9e222000-06-18 14:50:44 +1000327 goto bad_option;
Damien Miller33804262001-02-04 23:20:18 +1100328 opts++;
Damien Millerf6d9e222000-06-18 14:50:44 +1000329 /* Process the next option. */
330 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000331
332 if (!use_privsep)
Ben Lindstroma574cda2002-05-15 16:16:14 +0000333 auth_debug_send();
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000334
Damien Millerf6d9e222000-06-18 14:50:44 +1000335 /* grant access */
336 return 1;
337
338bad_option:
Damien Miller996acd22003-04-09 20:59:48 +1000339 logit("Bad options in %.100s file, line %lu: %.50s",
Damien Miller33804262001-02-04 23:20:18 +1100340 file, linenum, opts);
Ben Lindstroma574cda2002-05-15 16:16:14 +0000341 auth_debug_add("Bad options in %.100s file, line %lu: %.50s",
Damien Miller33804262001-02-04 23:20:18 +1100342 file, linenum, opts);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000343
344 if (!use_privsep)
Ben Lindstroma574cda2002-05-15 16:16:14 +0000345 auth_debug_send();
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000346
Damien Millerf6d9e222000-06-18 14:50:44 +1000347 /* deny access */
348 return 0;
349}