blob: ca5e1c931499934b9eec24b4cceaeae693912e5f [file] [log] [blame]
Damien Millerd7834352006-08-05 12:39:39 +10001/* $OpenBSD: auth-options.c,v 1.40 2006/08/03 03:34:41 deraadt 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 Millerd7834352006-08-05 12:39:39 +100020#include <stdio.h>
21#include <stdarg.h>
Damien Miller9f2abc42006-07-10 20:53:08 +100022
Damien Millerf6d9e222000-06-18 14:50:44 +100023#include "xmalloc.h"
24#include "match.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000025#include "log.h"
26#include "canohost.h"
Damien Millerd7834352006-08-05 12:39:39 +100027#include "buffer.h"
Ben Lindstromc7637672001-06-09 00:36:26 +000028#include "channels.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000029#include "auth-options.h"
Damien Miller33804262001-02-04 23:20:18 +110030#include "servconf.h"
Ben Lindstromd71ba572001-09-12 18:03:31 +000031#include "misc.h"
Damien Millerd7834352006-08-05 12:39:39 +100032#include "key.h"
33#include "hostfile.h"
Ben Lindstroma574cda2002-05-15 16:16:14 +000034#include "auth.h"
Damien Millerd7834352006-08-05 12:39:39 +100035#ifdef GSSAPI
36#include "ssh-gss.h"
37#endif
38#include "monitor_wrap.h"
Damien Millerf6d9e222000-06-18 14:50:44 +100039
40/* Flags set authorized_keys flags */
41int no_port_forwarding_flag = 0;
42int no_agent_forwarding_flag = 0;
43int no_x11_forwarding_flag = 0;
44int no_pty_flag = 0;
45
46/* "command=" option. */
47char *forced_command = NULL;
48
49/* "environment=" options. */
50struct envstring *custom_environment = NULL;
51
Damien Millerd27b9472005-12-13 19:29:02 +110052/* "tunnel=" option. */
53int forced_tun_device = -1;
54
Damien Miller33804262001-02-04 23:20:18 +110055extern ServerOptions options;
56
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000057void
Damien Miller874d77b2000-10-14 16:23:11 +110058auth_clear_options(void)
59{
60 no_agent_forwarding_flag = 0;
61 no_port_forwarding_flag = 0;
62 no_pty_flag = 0;
63 no_x11_forwarding_flag = 0;
64 while (custom_environment) {
65 struct envstring *ce = custom_environment;
66 custom_environment = ce->next;
67 xfree(ce->s);
68 xfree(ce);
69 }
70 if (forced_command) {
71 xfree(forced_command);
72 forced_command = NULL;
73 }
Damien Millerd27b9472005-12-13 19:29:02 +110074 forced_tun_device = -1;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +000075 channel_clear_permitted_opens();
Ben Lindstroma574cda2002-05-15 16:16:14 +000076 auth_debug_reset();
Damien Miller874d77b2000-10-14 16:23:11 +110077}
78
Ben Lindstrom226cfa02001-01-22 05:34:40 +000079/*
80 * return 1 if access is granted, 0 if not.
81 * side effect: sets key option flags
82 */
Damien Millerf6d9e222000-06-18 14:50:44 +100083int
Damien Miller33804262001-02-04 23:20:18 +110084auth_parse_options(struct passwd *pw, char *opts, char *file, u_long linenum)
Damien Millerf6d9e222000-06-18 14:50:44 +100085{
86 const char *cp;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +000087 int i;
Damien Miller874d77b2000-10-14 16:23:11 +110088
89 /* reset options */
90 auth_clear_options();
91
Ben Lindstrom36d7bd02001-02-10 22:27:19 +000092 if (!opts)
93 return 1;
94
Damien Miller33804262001-02-04 23:20:18 +110095 while (*opts && *opts != ' ' && *opts != '\t') {
Damien Millerf6d9e222000-06-18 14:50:44 +100096 cp = "no-port-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("Port forwarding disabled.");
Damien Millerf6d9e222000-06-18 14:50:44 +100099 no_port_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-agent-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("Agent forwarding disabled.");
Damien Millerf6d9e222000-06-18 14:50:44 +1000106 no_agent_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-X11-forwarding";
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("X11 forwarding disabled.");
Damien Millerf6d9e222000-06-18 14:50:44 +1000113 no_x11_forwarding_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 = "no-pty";
Damien Miller33804262001-02-04 23:20:18 +1100118 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
Ben Lindstroma574cda2002-05-15 16:16:14 +0000119 auth_debug_add("Pty allocation disabled.");
Damien Millerf6d9e222000-06-18 14:50:44 +1000120 no_pty_flag = 1;
Damien Miller33804262001-02-04 23:20:18 +1100121 opts += strlen(cp);
Damien Millerf6d9e222000-06-18 14:50:44 +1000122 goto next_option;
123 }
124 cp = "command=\"";
Damien Miller33804262001-02-04 23:20:18 +1100125 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
Damien Miller33804262001-02-04 23:20:18 +1100126 opts += strlen(cp);
127 forced_command = xmalloc(strlen(opts) + 1);
Damien Millerf6d9e222000-06-18 14:50:44 +1000128 i = 0;
Damien Miller33804262001-02-04 23:20:18 +1100129 while (*opts) {
130 if (*opts == '"')
Damien Millerf6d9e222000-06-18 14:50:44 +1000131 break;
Damien Miller33804262001-02-04 23:20:18 +1100132 if (*opts == '\\' && opts[1] == '"') {
133 opts += 2;
Damien Millerf6d9e222000-06-18 14:50:44 +1000134 forced_command[i++] = '"';
135 continue;
136 }
Damien Miller33804262001-02-04 23:20:18 +1100137 forced_command[i++] = *opts++;
Damien Millerf6d9e222000-06-18 14:50:44 +1000138 }
Damien Miller33804262001-02-04 23:20:18 +1100139 if (!*opts) {
Damien Millerf6d9e222000-06-18 14:50:44 +1000140 debug("%.100s, line %lu: missing end quote",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000141 file, linenum);
Ben Lindstroma574cda2002-05-15 16:16:14 +0000142 auth_debug_add("%.100s, line %lu: missing end quote",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000143 file, linenum);
Damien Miller056ddf72001-03-14 10:15:20 +1100144 xfree(forced_command);
145 forced_command = NULL;
146 goto bad_option;
Damien Millerf6d9e222000-06-18 14:50:44 +1000147 }
Damien Miller98299262006-07-24 14:01:43 +1000148 forced_command[i] = '\0';
Ben Lindstroma574cda2002-05-15 16:16:14 +0000149 auth_debug_add("Forced command: %.900s", forced_command);
Damien Miller33804262001-02-04 23:20:18 +1100150 opts++;
Damien Millerf6d9e222000-06-18 14:50:44 +1000151 goto next_option;
152 }
153 cp = "environment=\"";
Ben Lindstrom5d860f02002-08-01 01:28:38 +0000154 if (options.permit_user_env &&
155 strncasecmp(opts, cp, strlen(cp)) == 0) {
Damien Millerf6d9e222000-06-18 14:50:44 +1000156 char *s;
157 struct envstring *new_envstring;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +0000158
Damien Miller33804262001-02-04 23:20:18 +1100159 opts += strlen(cp);
160 s = xmalloc(strlen(opts) + 1);
Damien Millerf6d9e222000-06-18 14:50:44 +1000161 i = 0;
Damien Miller33804262001-02-04 23:20:18 +1100162 while (*opts) {
163 if (*opts == '"')
Damien Millerf6d9e222000-06-18 14:50:44 +1000164 break;
Damien Miller33804262001-02-04 23:20:18 +1100165 if (*opts == '\\' && opts[1] == '"') {
166 opts += 2;
Damien Millerf6d9e222000-06-18 14:50:44 +1000167 s[i++] = '"';
168 continue;
169 }
Damien Miller33804262001-02-04 23:20:18 +1100170 s[i++] = *opts++;
Damien Millerf6d9e222000-06-18 14:50:44 +1000171 }
Damien Miller33804262001-02-04 23:20:18 +1100172 if (!*opts) {
Damien Millerf6d9e222000-06-18 14:50:44 +1000173 debug("%.100s, line %lu: missing end quote",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000174 file, linenum);
Ben Lindstroma574cda2002-05-15 16:16:14 +0000175 auth_debug_add("%.100s, line %lu: missing end quote",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000176 file, linenum);
Damien Miller056ddf72001-03-14 10:15:20 +1100177 xfree(s);
178 goto bad_option;
Damien Millerf6d9e222000-06-18 14:50:44 +1000179 }
Damien Miller98299262006-07-24 14:01:43 +1000180 s[i] = '\0';
Ben Lindstroma574cda2002-05-15 16:16:14 +0000181 auth_debug_add("Adding to environment: %.900s", s);
Damien Millerf6d9e222000-06-18 14:50:44 +1000182 debug("Adding to environment: %.900s", s);
Damien Miller33804262001-02-04 23:20:18 +1100183 opts++;
Damien Millerf6d9e222000-06-18 14:50:44 +1000184 new_envstring = xmalloc(sizeof(struct envstring));
185 new_envstring->s = s;
186 new_envstring->next = custom_environment;
187 custom_environment = new_envstring;
188 goto next_option;
189 }
190 cp = "from=\"";
Damien Miller33804262001-02-04 23:20:18 +1100191 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
Damien Miller33804262001-02-04 23:20:18 +1100192 const char *remote_ip = get_remote_ipaddr();
193 const char *remote_host = get_canonical_hostname(
Damien Miller3a961dc2003-06-03 10:25:48 +1000194 options.use_dns);
Damien Miller33804262001-02-04 23:20:18 +1100195 char *patterns = xmalloc(strlen(opts) + 1);
Ben Lindstrom7bb8b492001-03-17 00:47:54 +0000196
Damien Miller33804262001-02-04 23:20:18 +1100197 opts += strlen(cp);
Damien Millerf6d9e222000-06-18 14:50:44 +1000198 i = 0;
Damien Miller33804262001-02-04 23:20:18 +1100199 while (*opts) {
200 if (*opts == '"')
Damien Millerf6d9e222000-06-18 14:50:44 +1000201 break;
Damien Miller33804262001-02-04 23:20:18 +1100202 if (*opts == '\\' && opts[1] == '"') {
203 opts += 2;
Damien Millerf6d9e222000-06-18 14:50:44 +1000204 patterns[i++] = '"';
205 continue;
206 }
Damien Miller33804262001-02-04 23:20:18 +1100207 patterns[i++] = *opts++;
Damien Millerf6d9e222000-06-18 14:50:44 +1000208 }
Damien Miller33804262001-02-04 23:20:18 +1100209 if (!*opts) {
Damien Millerf6d9e222000-06-18 14:50:44 +1000210 debug("%.100s, line %lu: missing end quote",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000211 file, linenum);
Ben Lindstroma574cda2002-05-15 16:16:14 +0000212 auth_debug_add("%.100s, line %lu: missing end quote",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000213 file, linenum);
Damien Miller056ddf72001-03-14 10:15:20 +1100214 xfree(patterns);
215 goto bad_option;
Damien Millerf6d9e222000-06-18 14:50:44 +1000216 }
Damien Miller98299262006-07-24 14:01:43 +1000217 patterns[i] = '\0';
Damien Miller33804262001-02-04 23:20:18 +1100218 opts++;
Ben Lindstromf0c50292001-06-25 05:17:53 +0000219 if (match_host_and_ip(remote_host, remote_ip,
220 patterns) != 1) {
221 xfree(patterns);
Damien Miller996acd22003-04-09 20:59:48 +1000222 logit("Authentication tried for %.100s with "
Damien Miller33804262001-02-04 23:20:18 +1100223 "correct key but not from a permitted "
224 "host (host=%.200s, ip=%.200s).",
225 pw->pw_name, remote_host, remote_ip);
Ben Lindstroma574cda2002-05-15 16:16:14 +0000226 auth_debug_add("Your host '%.200s' is not "
Damien Miller33804262001-02-04 23:20:18 +1100227 "permitted to use this key for login.",
228 remote_host);
Damien Millerf6d9e222000-06-18 14:50:44 +1000229 /* deny access */
230 return 0;
231 }
Ben Lindstromf0c50292001-06-25 05:17:53 +0000232 xfree(patterns);
Damien Millerf6d9e222000-06-18 14:50:44 +1000233 /* Host name matches. */
234 goto next_option;
235 }
Ben Lindstrom7bb8b492001-03-17 00:47:54 +0000236 cp = "permitopen=\"";
237 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
Damien Millerf91ee4c2005-03-01 21:24:33 +1100238 char *host, *p;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +0000239 u_short port;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +0000240 char *patterns = xmalloc(strlen(opts) + 1);
241
242 opts += strlen(cp);
243 i = 0;
244 while (*opts) {
245 if (*opts == '"')
246 break;
247 if (*opts == '\\' && opts[1] == '"') {
248 opts += 2;
249 patterns[i++] = '"';
250 continue;
251 }
252 patterns[i++] = *opts++;
253 }
254 if (!*opts) {
255 debug("%.100s, line %lu: missing end quote",
256 file, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100257 auth_debug_add("%.100s, line %lu: missing "
258 "end quote", file, linenum);
Ben Lindstrom7bb8b492001-03-17 00:47:54 +0000259 xfree(patterns);
260 goto bad_option;
261 }
Damien Miller98299262006-07-24 14:01:43 +1000262 patterns[i] = '\0';
Ben Lindstrom7bb8b492001-03-17 00:47:54 +0000263 opts++;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100264 p = patterns;
265 host = hpdelim(&p);
266 if (host == NULL || strlen(host) >= NI_MAXHOST) {
267 debug("%.100s, line %lu: Bad permitopen "
Darren Tucker90b9e022005-03-14 23:08:50 +1100268 "specification <%.100s>", file, linenum,
Damien Millerf91ee4c2005-03-01 21:24:33 +1100269 patterns);
Ben Lindstroma574cda2002-05-15 16:16:14 +0000270 auth_debug_add("%.100s, line %lu: "
Damien Millerf91ee4c2005-03-01 21:24:33 +1100271 "Bad permitopen specification", file,
272 linenum);
Ben Lindstrom7bb8b492001-03-17 00:47:54 +0000273 xfree(patterns);
274 goto bad_option;
275 }
Darren Tucker47eede72005-03-14 23:08:12 +1100276 host = cleanhostname(host);
277 if (p == NULL || (port = a2port(p)) == 0) {
Damien Millerf91ee4c2005-03-01 21:24:33 +1100278 debug("%.100s, line %lu: Bad permitopen port "
279 "<%.100s>", file, linenum, p ? p : "");
Ben Lindstroma574cda2002-05-15 16:16:14 +0000280 auth_debug_add("%.100s, line %lu: "
Ben Lindstromd71ba572001-09-12 18:03:31 +0000281 "Bad permitopen port", file, linenum);
Ben Lindstrom7bb8b492001-03-17 00:47:54 +0000282 xfree(patterns);
283 goto bad_option;
284 }
Ben Lindstrom2d70f982001-03-19 00:13:46 +0000285 if (options.allow_tcp_forwarding)
Ben Lindstromd71ba572001-09-12 18:03:31 +0000286 channel_add_permitted_opens(host, port);
Ben Lindstrom7bb8b492001-03-17 00:47:54 +0000287 xfree(patterns);
288 goto next_option;
289 }
Damien Millerd27b9472005-12-13 19:29:02 +1100290 cp = "tunnel=\"";
291 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
292 char *tun = NULL;
293 opts += strlen(cp);
294 tun = xmalloc(strlen(opts) + 1);
295 i = 0;
296 while (*opts) {
297 if (*opts == '"')
298 break;
299 tun[i++] = *opts++;
300 }
301 if (!*opts) {
302 debug("%.100s, line %lu: missing end quote",
303 file, linenum);
304 auth_debug_add("%.100s, line %lu: missing end quote",
305 file, linenum);
306 xfree(tun);
307 forced_tun_device = -1;
308 goto bad_option;
309 }
Damien Miller98299262006-07-24 14:01:43 +1000310 tun[i] = '\0';
Damien Millerd27b9472005-12-13 19:29:02 +1100311 forced_tun_device = a2tun(tun, NULL);
312 xfree(tun);
Damien Miller7b58e802005-12-13 19:33:19 +1100313 if (forced_tun_device == SSH_TUNID_ERR) {
Damien Millerd27b9472005-12-13 19:29:02 +1100314 debug("%.100s, line %lu: invalid tun device",
315 file, linenum);
316 auth_debug_add("%.100s, line %lu: invalid tun device",
317 file, linenum);
318 forced_tun_device = -1;
319 goto bad_option;
320 }
321 auth_debug_add("Forced tun device: %d", forced_tun_device);
322 opts++;
323 goto next_option;
324 }
Damien Millerf6d9e222000-06-18 14:50:44 +1000325next_option:
326 /*
327 * Skip the comma, and move to the next option
328 * (or break out if there are no more).
329 */
Damien Miller33804262001-02-04 23:20:18 +1100330 if (!*opts)
Damien Millerf6d9e222000-06-18 14:50:44 +1000331 fatal("Bugs in auth-options.c option processing.");
Damien Miller33804262001-02-04 23:20:18 +1100332 if (*opts == ' ' || *opts == '\t')
Damien Millerf6d9e222000-06-18 14:50:44 +1000333 break; /* End of options. */
Damien Miller33804262001-02-04 23:20:18 +1100334 if (*opts != ',')
Damien Millerf6d9e222000-06-18 14:50:44 +1000335 goto bad_option;
Damien Miller33804262001-02-04 23:20:18 +1100336 opts++;
Damien Millerf6d9e222000-06-18 14:50:44 +1000337 /* Process the next option. */
338 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000339
340 if (!use_privsep)
Ben Lindstroma574cda2002-05-15 16:16:14 +0000341 auth_debug_send();
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000342
Damien Millerf6d9e222000-06-18 14:50:44 +1000343 /* grant access */
344 return 1;
345
346bad_option:
Damien Miller996acd22003-04-09 20:59:48 +1000347 logit("Bad options in %.100s file, line %lu: %.50s",
Damien Miller33804262001-02-04 23:20:18 +1100348 file, linenum, opts);
Ben Lindstroma574cda2002-05-15 16:16:14 +0000349 auth_debug_add("Bad options in %.100s file, line %lu: %.50s",
Damien Miller33804262001-02-04 23:20:18 +1100350 file, linenum, opts);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000351
352 if (!use_privsep)
Ben Lindstroma574cda2002-05-15 16:16:14 +0000353 auth_debug_send();
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000354
Damien Millerf6d9e222000-06-18 14:50:44 +1000355 /* deny access */
356 return 0;
357}