blob: 6e2256961005562349addbf3b165ec2cd0fe02f9 [file] [log] [blame]
Damien Miller95e80952008-03-27 11:03:05 +11001/* $OpenBSD: auth-options.c,v 1.41 2008/03/26 21:28:14 djm 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;
Damien Miller95e80952008-03-27 11:03:05 +110045int no_user_rc = 0;
Damien Millerf6d9e222000-06-18 14:50:44 +100046
47/* "command=" option. */
48char *forced_command = NULL;
49
50/* "environment=" options. */
51struct envstring *custom_environment = NULL;
52
Damien Millerd27b9472005-12-13 19:29:02 +110053/* "tunnel=" option. */
54int forced_tun_device = -1;
55
Damien Miller33804262001-02-04 23:20:18 +110056extern ServerOptions options;
57
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000058void
Damien Miller874d77b2000-10-14 16:23:11 +110059auth_clear_options(void)
60{
61 no_agent_forwarding_flag = 0;
62 no_port_forwarding_flag = 0;
63 no_pty_flag = 0;
64 no_x11_forwarding_flag = 0;
Damien Miller95e80952008-03-27 11:03:05 +110065 no_user_rc = 0;
Damien Miller874d77b2000-10-14 16:23:11 +110066 while (custom_environment) {
67 struct envstring *ce = custom_environment;
68 custom_environment = ce->next;
69 xfree(ce->s);
70 xfree(ce);
71 }
72 if (forced_command) {
73 xfree(forced_command);
74 forced_command = NULL;
75 }
Damien Millerd27b9472005-12-13 19:29:02 +110076 forced_tun_device = -1;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +000077 channel_clear_permitted_opens();
Ben Lindstroma574cda2002-05-15 16:16:14 +000078 auth_debug_reset();
Damien Miller874d77b2000-10-14 16:23:11 +110079}
80
Ben Lindstrom226cfa02001-01-22 05:34:40 +000081/*
82 * return 1 if access is granted, 0 if not.
83 * side effect: sets key option flags
84 */
Damien Millerf6d9e222000-06-18 14:50:44 +100085int
Damien Miller33804262001-02-04 23:20:18 +110086auth_parse_options(struct passwd *pw, char *opts, char *file, u_long linenum)
Damien Millerf6d9e222000-06-18 14:50:44 +100087{
88 const char *cp;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +000089 int i;
Damien Miller874d77b2000-10-14 16:23:11 +110090
91 /* reset options */
92 auth_clear_options();
93
Ben Lindstrom36d7bd02001-02-10 22:27:19 +000094 if (!opts)
95 return 1;
96
Damien Miller33804262001-02-04 23:20:18 +110097 while (*opts && *opts != ' ' && *opts != '\t') {
Damien Millerf6d9e222000-06-18 14:50:44 +100098 cp = "no-port-forwarding";
Damien Miller33804262001-02-04 23:20:18 +110099 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
Ben Lindstroma574cda2002-05-15 16:16:14 +0000100 auth_debug_add("Port forwarding disabled.");
Damien Millerf6d9e222000-06-18 14:50:44 +1000101 no_port_forwarding_flag = 1;
Damien Miller33804262001-02-04 23:20:18 +1100102 opts += strlen(cp);
Damien Millerf6d9e222000-06-18 14:50:44 +1000103 goto next_option;
104 }
105 cp = "no-agent-forwarding";
Damien Miller33804262001-02-04 23:20:18 +1100106 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
Ben Lindstroma574cda2002-05-15 16:16:14 +0000107 auth_debug_add("Agent forwarding disabled.");
Damien Millerf6d9e222000-06-18 14:50:44 +1000108 no_agent_forwarding_flag = 1;
Damien Miller33804262001-02-04 23:20:18 +1100109 opts += strlen(cp);
Damien Millerf6d9e222000-06-18 14:50:44 +1000110 goto next_option;
111 }
112 cp = "no-X11-forwarding";
Damien Miller33804262001-02-04 23:20:18 +1100113 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
Ben Lindstroma574cda2002-05-15 16:16:14 +0000114 auth_debug_add("X11 forwarding disabled.");
Damien Millerf6d9e222000-06-18 14:50:44 +1000115 no_x11_forwarding_flag = 1;
Damien Miller33804262001-02-04 23:20:18 +1100116 opts += strlen(cp);
Damien Millerf6d9e222000-06-18 14:50:44 +1000117 goto next_option;
118 }
119 cp = "no-pty";
Damien Miller33804262001-02-04 23:20:18 +1100120 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
Ben Lindstroma574cda2002-05-15 16:16:14 +0000121 auth_debug_add("Pty allocation disabled.");
Damien Millerf6d9e222000-06-18 14:50:44 +1000122 no_pty_flag = 1;
Damien Miller33804262001-02-04 23:20:18 +1100123 opts += strlen(cp);
Damien Millerf6d9e222000-06-18 14:50:44 +1000124 goto next_option;
125 }
Damien Miller95e80952008-03-27 11:03:05 +1100126 cp = "no-user-rc";
127 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
128 auth_debug_add("User rc file execution disabled.");
129 no_user_rc = 1;
130 opts += strlen(cp);
131 goto next_option;
132 }
Damien Millerf6d9e222000-06-18 14:50:44 +1000133 cp = "command=\"";
Damien Miller33804262001-02-04 23:20:18 +1100134 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
Damien Miller33804262001-02-04 23:20:18 +1100135 opts += strlen(cp);
136 forced_command = xmalloc(strlen(opts) + 1);
Damien Millerf6d9e222000-06-18 14:50:44 +1000137 i = 0;
Damien Miller33804262001-02-04 23:20:18 +1100138 while (*opts) {
139 if (*opts == '"')
Damien Millerf6d9e222000-06-18 14:50:44 +1000140 break;
Damien Miller33804262001-02-04 23:20:18 +1100141 if (*opts == '\\' && opts[1] == '"') {
142 opts += 2;
Damien Millerf6d9e222000-06-18 14:50:44 +1000143 forced_command[i++] = '"';
144 continue;
145 }
Damien Miller33804262001-02-04 23:20:18 +1100146 forced_command[i++] = *opts++;
Damien Millerf6d9e222000-06-18 14:50:44 +1000147 }
Damien Miller33804262001-02-04 23:20:18 +1100148 if (!*opts) {
Damien Millerf6d9e222000-06-18 14:50:44 +1000149 debug("%.100s, line %lu: missing end quote",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000150 file, linenum);
Ben Lindstroma574cda2002-05-15 16:16:14 +0000151 auth_debug_add("%.100s, line %lu: missing end quote",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000152 file, linenum);
Damien Miller056ddf72001-03-14 10:15:20 +1100153 xfree(forced_command);
154 forced_command = NULL;
155 goto bad_option;
Damien Millerf6d9e222000-06-18 14:50:44 +1000156 }
Damien Miller98299262006-07-24 14:01:43 +1000157 forced_command[i] = '\0';
Ben Lindstroma574cda2002-05-15 16:16:14 +0000158 auth_debug_add("Forced command: %.900s", forced_command);
Damien Miller33804262001-02-04 23:20:18 +1100159 opts++;
Damien Millerf6d9e222000-06-18 14:50:44 +1000160 goto next_option;
161 }
162 cp = "environment=\"";
Ben Lindstrom5d860f02002-08-01 01:28:38 +0000163 if (options.permit_user_env &&
164 strncasecmp(opts, cp, strlen(cp)) == 0) {
Damien Millerf6d9e222000-06-18 14:50:44 +1000165 char *s;
166 struct envstring *new_envstring;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +0000167
Damien Miller33804262001-02-04 23:20:18 +1100168 opts += strlen(cp);
169 s = xmalloc(strlen(opts) + 1);
Damien Millerf6d9e222000-06-18 14:50:44 +1000170 i = 0;
Damien Miller33804262001-02-04 23:20:18 +1100171 while (*opts) {
172 if (*opts == '"')
Damien Millerf6d9e222000-06-18 14:50:44 +1000173 break;
Damien Miller33804262001-02-04 23:20:18 +1100174 if (*opts == '\\' && opts[1] == '"') {
175 opts += 2;
Damien Millerf6d9e222000-06-18 14:50:44 +1000176 s[i++] = '"';
177 continue;
178 }
Damien Miller33804262001-02-04 23:20:18 +1100179 s[i++] = *opts++;
Damien Millerf6d9e222000-06-18 14:50:44 +1000180 }
Damien Miller33804262001-02-04 23:20:18 +1100181 if (!*opts) {
Damien Millerf6d9e222000-06-18 14:50:44 +1000182 debug("%.100s, line %lu: missing end quote",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000183 file, linenum);
Ben Lindstroma574cda2002-05-15 16:16:14 +0000184 auth_debug_add("%.100s, line %lu: missing end quote",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000185 file, linenum);
Damien Miller056ddf72001-03-14 10:15:20 +1100186 xfree(s);
187 goto bad_option;
Damien Millerf6d9e222000-06-18 14:50:44 +1000188 }
Damien Miller98299262006-07-24 14:01:43 +1000189 s[i] = '\0';
Ben Lindstroma574cda2002-05-15 16:16:14 +0000190 auth_debug_add("Adding to environment: %.900s", s);
Damien Millerf6d9e222000-06-18 14:50:44 +1000191 debug("Adding to environment: %.900s", s);
Damien Miller33804262001-02-04 23:20:18 +1100192 opts++;
Damien Millerf6d9e222000-06-18 14:50:44 +1000193 new_envstring = xmalloc(sizeof(struct envstring));
194 new_envstring->s = s;
195 new_envstring->next = custom_environment;
196 custom_environment = new_envstring;
197 goto next_option;
198 }
199 cp = "from=\"";
Damien Miller33804262001-02-04 23:20:18 +1100200 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
Damien Miller33804262001-02-04 23:20:18 +1100201 const char *remote_ip = get_remote_ipaddr();
202 const char *remote_host = get_canonical_hostname(
Damien Miller3a961dc2003-06-03 10:25:48 +1000203 options.use_dns);
Damien Miller33804262001-02-04 23:20:18 +1100204 char *patterns = xmalloc(strlen(opts) + 1);
Ben Lindstrom7bb8b492001-03-17 00:47:54 +0000205
Damien Miller33804262001-02-04 23:20:18 +1100206 opts += strlen(cp);
Damien Millerf6d9e222000-06-18 14:50:44 +1000207 i = 0;
Damien Miller33804262001-02-04 23:20:18 +1100208 while (*opts) {
209 if (*opts == '"')
Damien Millerf6d9e222000-06-18 14:50:44 +1000210 break;
Damien Miller33804262001-02-04 23:20:18 +1100211 if (*opts == '\\' && opts[1] == '"') {
212 opts += 2;
Damien Millerf6d9e222000-06-18 14:50:44 +1000213 patterns[i++] = '"';
214 continue;
215 }
Damien Miller33804262001-02-04 23:20:18 +1100216 patterns[i++] = *opts++;
Damien Millerf6d9e222000-06-18 14:50:44 +1000217 }
Damien Miller33804262001-02-04 23:20:18 +1100218 if (!*opts) {
Damien Millerf6d9e222000-06-18 14:50:44 +1000219 debug("%.100s, line %lu: missing end quote",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000220 file, linenum);
Ben Lindstroma574cda2002-05-15 16:16:14 +0000221 auth_debug_add("%.100s, line %lu: missing end quote",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000222 file, linenum);
Damien Miller056ddf72001-03-14 10:15:20 +1100223 xfree(patterns);
224 goto bad_option;
Damien Millerf6d9e222000-06-18 14:50:44 +1000225 }
Damien Miller98299262006-07-24 14:01:43 +1000226 patterns[i] = '\0';
Damien Miller33804262001-02-04 23:20:18 +1100227 opts++;
Ben Lindstromf0c50292001-06-25 05:17:53 +0000228 if (match_host_and_ip(remote_host, remote_ip,
229 patterns) != 1) {
230 xfree(patterns);
Damien Miller996acd22003-04-09 20:59:48 +1000231 logit("Authentication tried for %.100s with "
Damien Miller33804262001-02-04 23:20:18 +1100232 "correct key but not from a permitted "
233 "host (host=%.200s, ip=%.200s).",
234 pw->pw_name, remote_host, remote_ip);
Ben Lindstroma574cda2002-05-15 16:16:14 +0000235 auth_debug_add("Your host '%.200s' is not "
Damien Miller33804262001-02-04 23:20:18 +1100236 "permitted to use this key for login.",
237 remote_host);
Damien Millerf6d9e222000-06-18 14:50:44 +1000238 /* deny access */
239 return 0;
240 }
Ben Lindstromf0c50292001-06-25 05:17:53 +0000241 xfree(patterns);
Damien Millerf6d9e222000-06-18 14:50:44 +1000242 /* Host name matches. */
243 goto next_option;
244 }
Ben Lindstrom7bb8b492001-03-17 00:47:54 +0000245 cp = "permitopen=\"";
246 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
Damien Millerf91ee4c2005-03-01 21:24:33 +1100247 char *host, *p;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +0000248 u_short port;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +0000249 char *patterns = xmalloc(strlen(opts) + 1);
250
251 opts += strlen(cp);
252 i = 0;
253 while (*opts) {
254 if (*opts == '"')
255 break;
256 if (*opts == '\\' && opts[1] == '"') {
257 opts += 2;
258 patterns[i++] = '"';
259 continue;
260 }
261 patterns[i++] = *opts++;
262 }
263 if (!*opts) {
264 debug("%.100s, line %lu: missing end quote",
265 file, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100266 auth_debug_add("%.100s, line %lu: missing "
267 "end quote", file, linenum);
Ben Lindstrom7bb8b492001-03-17 00:47:54 +0000268 xfree(patterns);
269 goto bad_option;
270 }
Damien Miller98299262006-07-24 14:01:43 +1000271 patterns[i] = '\0';
Ben Lindstrom7bb8b492001-03-17 00:47:54 +0000272 opts++;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100273 p = patterns;
274 host = hpdelim(&p);
275 if (host == NULL || strlen(host) >= NI_MAXHOST) {
276 debug("%.100s, line %lu: Bad permitopen "
Darren Tucker90b9e022005-03-14 23:08:50 +1100277 "specification <%.100s>", file, linenum,
Damien Millerf91ee4c2005-03-01 21:24:33 +1100278 patterns);
Ben Lindstroma574cda2002-05-15 16:16:14 +0000279 auth_debug_add("%.100s, line %lu: "
Damien Millerf91ee4c2005-03-01 21:24:33 +1100280 "Bad permitopen specification", file,
281 linenum);
Ben Lindstrom7bb8b492001-03-17 00:47:54 +0000282 xfree(patterns);
283 goto bad_option;
284 }
Darren Tucker47eede72005-03-14 23:08:12 +1100285 host = cleanhostname(host);
286 if (p == NULL || (port = a2port(p)) == 0) {
Damien Millerf91ee4c2005-03-01 21:24:33 +1100287 debug("%.100s, line %lu: Bad permitopen port "
288 "<%.100s>", file, linenum, p ? p : "");
Ben Lindstroma574cda2002-05-15 16:16:14 +0000289 auth_debug_add("%.100s, line %lu: "
Ben Lindstromd71ba572001-09-12 18:03:31 +0000290 "Bad permitopen port", file, linenum);
Ben Lindstrom7bb8b492001-03-17 00:47:54 +0000291 xfree(patterns);
292 goto bad_option;
293 }
Ben Lindstrom2d70f982001-03-19 00:13:46 +0000294 if (options.allow_tcp_forwarding)
Ben Lindstromd71ba572001-09-12 18:03:31 +0000295 channel_add_permitted_opens(host, port);
Ben Lindstrom7bb8b492001-03-17 00:47:54 +0000296 xfree(patterns);
297 goto next_option;
298 }
Damien Millerd27b9472005-12-13 19:29:02 +1100299 cp = "tunnel=\"";
300 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
301 char *tun = NULL;
302 opts += strlen(cp);
303 tun = xmalloc(strlen(opts) + 1);
304 i = 0;
305 while (*opts) {
306 if (*opts == '"')
307 break;
308 tun[i++] = *opts++;
309 }
310 if (!*opts) {
311 debug("%.100s, line %lu: missing end quote",
312 file, linenum);
313 auth_debug_add("%.100s, line %lu: missing end quote",
314 file, linenum);
315 xfree(tun);
316 forced_tun_device = -1;
317 goto bad_option;
318 }
Damien Miller98299262006-07-24 14:01:43 +1000319 tun[i] = '\0';
Damien Millerd27b9472005-12-13 19:29:02 +1100320 forced_tun_device = a2tun(tun, NULL);
321 xfree(tun);
Damien Miller7b58e802005-12-13 19:33:19 +1100322 if (forced_tun_device == SSH_TUNID_ERR) {
Damien Millerd27b9472005-12-13 19:29:02 +1100323 debug("%.100s, line %lu: invalid tun device",
324 file, linenum);
325 auth_debug_add("%.100s, line %lu: invalid tun device",
326 file, linenum);
327 forced_tun_device = -1;
328 goto bad_option;
329 }
330 auth_debug_add("Forced tun device: %d", forced_tun_device);
331 opts++;
332 goto next_option;
333 }
Damien Millerf6d9e222000-06-18 14:50:44 +1000334next_option:
335 /*
336 * Skip the comma, and move to the next option
337 * (or break out if there are no more).
338 */
Damien Miller33804262001-02-04 23:20:18 +1100339 if (!*opts)
Damien Millerf6d9e222000-06-18 14:50:44 +1000340 fatal("Bugs in auth-options.c option processing.");
Damien Miller33804262001-02-04 23:20:18 +1100341 if (*opts == ' ' || *opts == '\t')
Damien Millerf6d9e222000-06-18 14:50:44 +1000342 break; /* End of options. */
Damien Miller33804262001-02-04 23:20:18 +1100343 if (*opts != ',')
Damien Millerf6d9e222000-06-18 14:50:44 +1000344 goto bad_option;
Damien Miller33804262001-02-04 23:20:18 +1100345 opts++;
Damien Millerf6d9e222000-06-18 14:50:44 +1000346 /* Process the next option. */
347 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000348
349 if (!use_privsep)
Ben Lindstroma574cda2002-05-15 16:16:14 +0000350 auth_debug_send();
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000351
Damien Millerf6d9e222000-06-18 14:50:44 +1000352 /* grant access */
353 return 1;
354
355bad_option:
Damien Miller996acd22003-04-09 20:59:48 +1000356 logit("Bad options in %.100s file, line %lu: %.50s",
Damien Miller33804262001-02-04 23:20:18 +1100357 file, linenum, opts);
Ben Lindstroma574cda2002-05-15 16:16:14 +0000358 auth_debug_add("Bad options in %.100s file, line %lu: %.50s",
Damien Miller33804262001-02-04 23:20:18 +1100359 file, linenum, opts);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000360
361 if (!use_privsep)
Ben Lindstroma574cda2002-05-15 16:16:14 +0000362 auth_debug_send();
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000363
Damien Millerf6d9e222000-06-18 14:50:44 +1000364 /* deny access */
365 return 0;
366}