blob: 7e6bfeb40c02e5767c63976010395c46e090a33e [file] [log] [blame]
Damien Miller98299262006-07-24 14:01:43 +10001/* $OpenBSD: auth-options.c,v 1.38 2006/07/17 12:02:24 dtucker 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 Millerbe43ebf2006-07-24 13:51:51 +100017#if defined(HAVE_NETDB_H)
18# include <netdb.h>
19#endif
Damien Miller9f2abc42006-07-10 20:53:08 +100020#include <pwd.h>
21
Damien Millerf6d9e222000-06-18 14:50:44 +100022#include "xmalloc.h"
23#include "match.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000024#include "log.h"
25#include "canohost.h"
Ben Lindstromc7637672001-06-09 00:36:26 +000026#include "channels.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000027#include "auth-options.h"
Damien Miller33804262001-02-04 23:20:18 +110028#include "servconf.h"
Ben Lindstromd71ba572001-09-12 18:03:31 +000029#include "misc.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000030#include "monitor_wrap.h"
Ben Lindstroma574cda2002-05-15 16:16:14 +000031#include "auth.h"
Damien Millerf6d9e222000-06-18 14:50:44 +100032
33/* Flags set authorized_keys flags */
34int no_port_forwarding_flag = 0;
35int no_agent_forwarding_flag = 0;
36int no_x11_forwarding_flag = 0;
37int no_pty_flag = 0;
38
39/* "command=" option. */
40char *forced_command = NULL;
41
42/* "environment=" options. */
43struct envstring *custom_environment = NULL;
44
Damien Millerd27b9472005-12-13 19:29:02 +110045/* "tunnel=" option. */
46int forced_tun_device = -1;
47
Damien Miller33804262001-02-04 23:20:18 +110048extern ServerOptions options;
49
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000050void
Damien Miller874d77b2000-10-14 16:23:11 +110051auth_clear_options(void)
52{
53 no_agent_forwarding_flag = 0;
54 no_port_forwarding_flag = 0;
55 no_pty_flag = 0;
56 no_x11_forwarding_flag = 0;
57 while (custom_environment) {
58 struct envstring *ce = custom_environment;
59 custom_environment = ce->next;
60 xfree(ce->s);
61 xfree(ce);
62 }
63 if (forced_command) {
64 xfree(forced_command);
65 forced_command = NULL;
66 }
Damien Millerd27b9472005-12-13 19:29:02 +110067 forced_tun_device = -1;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +000068 channel_clear_permitted_opens();
Ben Lindstroma574cda2002-05-15 16:16:14 +000069 auth_debug_reset();
Damien Miller874d77b2000-10-14 16:23:11 +110070}
71
Ben Lindstrom226cfa02001-01-22 05:34:40 +000072/*
73 * return 1 if access is granted, 0 if not.
74 * side effect: sets key option flags
75 */
Damien Millerf6d9e222000-06-18 14:50:44 +100076int
Damien Miller33804262001-02-04 23:20:18 +110077auth_parse_options(struct passwd *pw, char *opts, char *file, u_long linenum)
Damien Millerf6d9e222000-06-18 14:50:44 +100078{
79 const char *cp;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +000080 int i;
Damien Miller874d77b2000-10-14 16:23:11 +110081
82 /* reset options */
83 auth_clear_options();
84
Ben Lindstrom36d7bd02001-02-10 22:27:19 +000085 if (!opts)
86 return 1;
87
Damien Miller33804262001-02-04 23:20:18 +110088 while (*opts && *opts != ' ' && *opts != '\t') {
Damien Millerf6d9e222000-06-18 14:50:44 +100089 cp = "no-port-forwarding";
Damien Miller33804262001-02-04 23:20:18 +110090 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
Ben Lindstroma574cda2002-05-15 16:16:14 +000091 auth_debug_add("Port forwarding disabled.");
Damien Millerf6d9e222000-06-18 14:50:44 +100092 no_port_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-agent-forwarding";
Damien Miller33804262001-02-04 23:20:18 +110097 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
Ben Lindstroma574cda2002-05-15 16:16:14 +000098 auth_debug_add("Agent forwarding disabled.");
Damien Millerf6d9e222000-06-18 14:50:44 +100099 no_agent_forwarding_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 = "no-X11-forwarding";
Damien Miller33804262001-02-04 23:20:18 +1100104 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
Ben Lindstroma574cda2002-05-15 16:16:14 +0000105 auth_debug_add("X11 forwarding disabled.");
Damien Millerf6d9e222000-06-18 14:50:44 +1000106 no_x11_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-pty";
Damien Miller33804262001-02-04 23:20:18 +1100111 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
Ben Lindstroma574cda2002-05-15 16:16:14 +0000112 auth_debug_add("Pty allocation disabled.");
Damien Millerf6d9e222000-06-18 14:50:44 +1000113 no_pty_flag = 1;
Damien Miller33804262001-02-04 23:20:18 +1100114 opts += strlen(cp);
Damien Millerf6d9e222000-06-18 14:50:44 +1000115 goto next_option;
116 }
117 cp = "command=\"";
Damien Miller33804262001-02-04 23:20:18 +1100118 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
Damien Miller33804262001-02-04 23:20:18 +1100119 opts += strlen(cp);
120 forced_command = xmalloc(strlen(opts) + 1);
Damien Millerf6d9e222000-06-18 14:50:44 +1000121 i = 0;
Damien Miller33804262001-02-04 23:20:18 +1100122 while (*opts) {
123 if (*opts == '"')
Damien Millerf6d9e222000-06-18 14:50:44 +1000124 break;
Damien Miller33804262001-02-04 23:20:18 +1100125 if (*opts == '\\' && opts[1] == '"') {
126 opts += 2;
Damien Millerf6d9e222000-06-18 14:50:44 +1000127 forced_command[i++] = '"';
128 continue;
129 }
Damien Miller33804262001-02-04 23:20:18 +1100130 forced_command[i++] = *opts++;
Damien Millerf6d9e222000-06-18 14:50:44 +1000131 }
Damien Miller33804262001-02-04 23:20:18 +1100132 if (!*opts) {
Damien Millerf6d9e222000-06-18 14:50:44 +1000133 debug("%.100s, line %lu: missing end quote",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000134 file, linenum);
Ben Lindstroma574cda2002-05-15 16:16:14 +0000135 auth_debug_add("%.100s, line %lu: missing end quote",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000136 file, linenum);
Damien Miller056ddf72001-03-14 10:15:20 +1100137 xfree(forced_command);
138 forced_command = NULL;
139 goto bad_option;
Damien Millerf6d9e222000-06-18 14:50:44 +1000140 }
Damien Miller98299262006-07-24 14:01:43 +1000141 forced_command[i] = '\0';
Ben Lindstroma574cda2002-05-15 16:16:14 +0000142 auth_debug_add("Forced command: %.900s", forced_command);
Damien Miller33804262001-02-04 23:20:18 +1100143 opts++;
Damien Millerf6d9e222000-06-18 14:50:44 +1000144 goto next_option;
145 }
146 cp = "environment=\"";
Ben Lindstrom5d860f02002-08-01 01:28:38 +0000147 if (options.permit_user_env &&
148 strncasecmp(opts, cp, strlen(cp)) == 0) {
Damien Millerf6d9e222000-06-18 14:50:44 +1000149 char *s;
150 struct envstring *new_envstring;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +0000151
Damien Miller33804262001-02-04 23:20:18 +1100152 opts += strlen(cp);
153 s = xmalloc(strlen(opts) + 1);
Damien Millerf6d9e222000-06-18 14:50:44 +1000154 i = 0;
Damien Miller33804262001-02-04 23:20:18 +1100155 while (*opts) {
156 if (*opts == '"')
Damien Millerf6d9e222000-06-18 14:50:44 +1000157 break;
Damien Miller33804262001-02-04 23:20:18 +1100158 if (*opts == '\\' && opts[1] == '"') {
159 opts += 2;
Damien Millerf6d9e222000-06-18 14:50:44 +1000160 s[i++] = '"';
161 continue;
162 }
Damien Miller33804262001-02-04 23:20:18 +1100163 s[i++] = *opts++;
Damien Millerf6d9e222000-06-18 14:50:44 +1000164 }
Damien Miller33804262001-02-04 23:20:18 +1100165 if (!*opts) {
Damien Millerf6d9e222000-06-18 14:50:44 +1000166 debug("%.100s, line %lu: missing end quote",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000167 file, linenum);
Ben Lindstroma574cda2002-05-15 16:16:14 +0000168 auth_debug_add("%.100s, line %lu: missing end quote",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000169 file, linenum);
Damien Miller056ddf72001-03-14 10:15:20 +1100170 xfree(s);
171 goto bad_option;
Damien Millerf6d9e222000-06-18 14:50:44 +1000172 }
Damien Miller98299262006-07-24 14:01:43 +1000173 s[i] = '\0';
Ben Lindstroma574cda2002-05-15 16:16:14 +0000174 auth_debug_add("Adding to environment: %.900s", s);
Damien Millerf6d9e222000-06-18 14:50:44 +1000175 debug("Adding to environment: %.900s", s);
Damien Miller33804262001-02-04 23:20:18 +1100176 opts++;
Damien Millerf6d9e222000-06-18 14:50:44 +1000177 new_envstring = xmalloc(sizeof(struct envstring));
178 new_envstring->s = s;
179 new_envstring->next = custom_environment;
180 custom_environment = new_envstring;
181 goto next_option;
182 }
183 cp = "from=\"";
Damien Miller33804262001-02-04 23:20:18 +1100184 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
Damien Miller33804262001-02-04 23:20:18 +1100185 const char *remote_ip = get_remote_ipaddr();
186 const char *remote_host = get_canonical_hostname(
Damien Miller3a961dc2003-06-03 10:25:48 +1000187 options.use_dns);
Damien Miller33804262001-02-04 23:20:18 +1100188 char *patterns = xmalloc(strlen(opts) + 1);
Ben Lindstrom7bb8b492001-03-17 00:47:54 +0000189
Damien Miller33804262001-02-04 23:20:18 +1100190 opts += strlen(cp);
Damien Millerf6d9e222000-06-18 14:50:44 +1000191 i = 0;
Damien Miller33804262001-02-04 23:20:18 +1100192 while (*opts) {
193 if (*opts == '"')
Damien Millerf6d9e222000-06-18 14:50:44 +1000194 break;
Damien Miller33804262001-02-04 23:20:18 +1100195 if (*opts == '\\' && opts[1] == '"') {
196 opts += 2;
Damien Millerf6d9e222000-06-18 14:50:44 +1000197 patterns[i++] = '"';
198 continue;
199 }
Damien Miller33804262001-02-04 23:20:18 +1100200 patterns[i++] = *opts++;
Damien Millerf6d9e222000-06-18 14:50:44 +1000201 }
Damien Miller33804262001-02-04 23:20:18 +1100202 if (!*opts) {
Damien Millerf6d9e222000-06-18 14:50:44 +1000203 debug("%.100s, line %lu: missing end quote",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000204 file, linenum);
Ben Lindstroma574cda2002-05-15 16:16:14 +0000205 auth_debug_add("%.100s, line %lu: missing end quote",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000206 file, linenum);
Damien Miller056ddf72001-03-14 10:15:20 +1100207 xfree(patterns);
208 goto bad_option;
Damien Millerf6d9e222000-06-18 14:50:44 +1000209 }
Damien Miller98299262006-07-24 14:01:43 +1000210 patterns[i] = '\0';
Damien Miller33804262001-02-04 23:20:18 +1100211 opts++;
Ben Lindstromf0c50292001-06-25 05:17:53 +0000212 if (match_host_and_ip(remote_host, remote_ip,
213 patterns) != 1) {
214 xfree(patterns);
Damien Miller996acd22003-04-09 20:59:48 +1000215 logit("Authentication tried for %.100s with "
Damien Miller33804262001-02-04 23:20:18 +1100216 "correct key but not from a permitted "
217 "host (host=%.200s, ip=%.200s).",
218 pw->pw_name, remote_host, remote_ip);
Ben Lindstroma574cda2002-05-15 16:16:14 +0000219 auth_debug_add("Your host '%.200s' is not "
Damien Miller33804262001-02-04 23:20:18 +1100220 "permitted to use this key for login.",
221 remote_host);
Damien Millerf6d9e222000-06-18 14:50:44 +1000222 /* deny access */
223 return 0;
224 }
Ben Lindstromf0c50292001-06-25 05:17:53 +0000225 xfree(patterns);
Damien Millerf6d9e222000-06-18 14:50:44 +1000226 /* Host name matches. */
227 goto next_option;
228 }
Ben Lindstrom7bb8b492001-03-17 00:47:54 +0000229 cp = "permitopen=\"";
230 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
Damien Millerf91ee4c2005-03-01 21:24:33 +1100231 char *host, *p;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +0000232 u_short port;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +0000233 char *patterns = xmalloc(strlen(opts) + 1);
234
235 opts += strlen(cp);
236 i = 0;
237 while (*opts) {
238 if (*opts == '"')
239 break;
240 if (*opts == '\\' && opts[1] == '"') {
241 opts += 2;
242 patterns[i++] = '"';
243 continue;
244 }
245 patterns[i++] = *opts++;
246 }
247 if (!*opts) {
248 debug("%.100s, line %lu: missing end quote",
249 file, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100250 auth_debug_add("%.100s, line %lu: missing "
251 "end quote", file, linenum);
Ben Lindstrom7bb8b492001-03-17 00:47:54 +0000252 xfree(patterns);
253 goto bad_option;
254 }
Damien Miller98299262006-07-24 14:01:43 +1000255 patterns[i] = '\0';
Ben Lindstrom7bb8b492001-03-17 00:47:54 +0000256 opts++;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100257 p = patterns;
258 host = hpdelim(&p);
259 if (host == NULL || strlen(host) >= NI_MAXHOST) {
260 debug("%.100s, line %lu: Bad permitopen "
Darren Tucker90b9e022005-03-14 23:08:50 +1100261 "specification <%.100s>", file, linenum,
Damien Millerf91ee4c2005-03-01 21:24:33 +1100262 patterns);
Ben Lindstroma574cda2002-05-15 16:16:14 +0000263 auth_debug_add("%.100s, line %lu: "
Damien Millerf91ee4c2005-03-01 21:24:33 +1100264 "Bad permitopen specification", file,
265 linenum);
Ben Lindstrom7bb8b492001-03-17 00:47:54 +0000266 xfree(patterns);
267 goto bad_option;
268 }
Darren Tucker47eede72005-03-14 23:08:12 +1100269 host = cleanhostname(host);
270 if (p == NULL || (port = a2port(p)) == 0) {
Damien Millerf91ee4c2005-03-01 21:24:33 +1100271 debug("%.100s, line %lu: Bad permitopen port "
272 "<%.100s>", file, linenum, p ? p : "");
Ben Lindstroma574cda2002-05-15 16:16:14 +0000273 auth_debug_add("%.100s, line %lu: "
Ben Lindstromd71ba572001-09-12 18:03:31 +0000274 "Bad permitopen port", file, linenum);
Ben Lindstrom7bb8b492001-03-17 00:47:54 +0000275 xfree(patterns);
276 goto bad_option;
277 }
Ben Lindstrom2d70f982001-03-19 00:13:46 +0000278 if (options.allow_tcp_forwarding)
Ben Lindstromd71ba572001-09-12 18:03:31 +0000279 channel_add_permitted_opens(host, port);
Ben Lindstrom7bb8b492001-03-17 00:47:54 +0000280 xfree(patterns);
281 goto next_option;
282 }
Damien Millerd27b9472005-12-13 19:29:02 +1100283 cp = "tunnel=\"";
284 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
285 char *tun = NULL;
286 opts += strlen(cp);
287 tun = xmalloc(strlen(opts) + 1);
288 i = 0;
289 while (*opts) {
290 if (*opts == '"')
291 break;
292 tun[i++] = *opts++;
293 }
294 if (!*opts) {
295 debug("%.100s, line %lu: missing end quote",
296 file, linenum);
297 auth_debug_add("%.100s, line %lu: missing end quote",
298 file, linenum);
299 xfree(tun);
300 forced_tun_device = -1;
301 goto bad_option;
302 }
Damien Miller98299262006-07-24 14:01:43 +1000303 tun[i] = '\0';
Damien Millerd27b9472005-12-13 19:29:02 +1100304 forced_tun_device = a2tun(tun, NULL);
305 xfree(tun);
Damien Miller7b58e802005-12-13 19:33:19 +1100306 if (forced_tun_device == SSH_TUNID_ERR) {
Damien Millerd27b9472005-12-13 19:29:02 +1100307 debug("%.100s, line %lu: invalid tun device",
308 file, linenum);
309 auth_debug_add("%.100s, line %lu: invalid tun device",
310 file, linenum);
311 forced_tun_device = -1;
312 goto bad_option;
313 }
314 auth_debug_add("Forced tun device: %d", forced_tun_device);
315 opts++;
316 goto next_option;
317 }
Damien Millerf6d9e222000-06-18 14:50:44 +1000318next_option:
319 /*
320 * Skip the comma, and move to the next option
321 * (or break out if there are no more).
322 */
Damien Miller33804262001-02-04 23:20:18 +1100323 if (!*opts)
Damien Millerf6d9e222000-06-18 14:50:44 +1000324 fatal("Bugs in auth-options.c option processing.");
Damien Miller33804262001-02-04 23:20:18 +1100325 if (*opts == ' ' || *opts == '\t')
Damien Millerf6d9e222000-06-18 14:50:44 +1000326 break; /* End of options. */
Damien Miller33804262001-02-04 23:20:18 +1100327 if (*opts != ',')
Damien Millerf6d9e222000-06-18 14:50:44 +1000328 goto bad_option;
Damien Miller33804262001-02-04 23:20:18 +1100329 opts++;
Damien Millerf6d9e222000-06-18 14:50:44 +1000330 /* Process the next option. */
331 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000332
333 if (!use_privsep)
Ben Lindstroma574cda2002-05-15 16:16:14 +0000334 auth_debug_send();
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000335
Damien Millerf6d9e222000-06-18 14:50:44 +1000336 /* grant access */
337 return 1;
338
339bad_option:
Damien Miller996acd22003-04-09 20:59:48 +1000340 logit("Bad options in %.100s file, line %lu: %.50s",
Damien Miller33804262001-02-04 23:20:18 +1100341 file, linenum, opts);
Ben Lindstroma574cda2002-05-15 16:16:14 +0000342 auth_debug_add("Bad options in %.100s file, line %lu: %.50s",
Damien Miller33804262001-02-04 23:20:18 +1100343 file, linenum, opts);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000344
345 if (!use_privsep)
Ben Lindstroma574cda2002-05-15 16:16:14 +0000346 auth_debug_send();
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000347
Damien Millerf6d9e222000-06-18 14:50:44 +1000348 /* deny access */
349 return 0;
350}