blob: 23d0423e122df2064debee9f0c5570c6cde06e0a [file] [log] [blame]
Damien Milleraa5b3f82012-12-03 09:50:54 +11001/* $OpenBSD: auth-options.c,v 1.57 2012/12/02 20:46:11 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 Millerb84886b2008-05-19 15:05:07 +100023#include "openbsd-compat/sys-queue.h"
Damien Millerf6d9e222000-06-18 14:50:44 +100024#include "xmalloc.h"
25#include "match.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000026#include "log.h"
27#include "canohost.h"
Damien Millerd7834352006-08-05 12:39:39 +100028#include "buffer.h"
Ben Lindstromc7637672001-06-09 00:36:26 +000029#include "channels.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"
Damien Miller4e270b02010-04-16 15:56:21 +100033#include "auth-options.h"
Damien Millerd7834352006-08-05 12:39:39 +100034#include "hostfile.h"
Ben Lindstroma574cda2002-05-15 16:16:14 +000035#include "auth.h"
Damien Millerd7834352006-08-05 12:39:39 +100036#ifdef GSSAPI
37#include "ssh-gss.h"
38#endif
39#include "monitor_wrap.h"
Damien Millerf6d9e222000-06-18 14:50:44 +100040
41/* Flags set authorized_keys flags */
42int no_port_forwarding_flag = 0;
43int no_agent_forwarding_flag = 0;
44int no_x11_forwarding_flag = 0;
45int no_pty_flag = 0;
Damien Miller95e80952008-03-27 11:03:05 +110046int no_user_rc = 0;
Damien Miller0a80ca12010-02-27 07:55:05 +110047int key_is_cert_authority = 0;
Damien Millerf6d9e222000-06-18 14:50:44 +100048
49/* "command=" option. */
50char *forced_command = NULL;
51
52/* "environment=" options. */
53struct envstring *custom_environment = NULL;
54
Damien Millerd27b9472005-12-13 19:29:02 +110055/* "tunnel=" option. */
56int forced_tun_device = -1;
57
Damien Miller30da3442010-05-10 11:58:03 +100058/* "principals=" option. */
59char *authorized_principals = NULL;
60
Damien Miller33804262001-02-04 23:20:18 +110061extern ServerOptions options;
62
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000063void
Damien Miller874d77b2000-10-14 16:23:11 +110064auth_clear_options(void)
65{
66 no_agent_forwarding_flag = 0;
67 no_port_forwarding_flag = 0;
68 no_pty_flag = 0;
69 no_x11_forwarding_flag = 0;
Damien Miller95e80952008-03-27 11:03:05 +110070 no_user_rc = 0;
Damien Miller0a80ca12010-02-27 07:55:05 +110071 key_is_cert_authority = 0;
Damien Miller874d77b2000-10-14 16:23:11 +110072 while (custom_environment) {
73 struct envstring *ce = custom_environment;
74 custom_environment = ce->next;
75 xfree(ce->s);
76 xfree(ce);
77 }
78 if (forced_command) {
79 xfree(forced_command);
80 forced_command = NULL;
81 }
Damien Miller30da3442010-05-10 11:58:03 +100082 if (authorized_principals) {
83 xfree(authorized_principals);
84 authorized_principals = NULL;
85 }
Damien Millerd27b9472005-12-13 19:29:02 +110086 forced_tun_device = -1;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +000087 channel_clear_permitted_opens();
Damien Miller874d77b2000-10-14 16:23:11 +110088}
89
Ben Lindstrom226cfa02001-01-22 05:34:40 +000090/*
91 * return 1 if access is granted, 0 if not.
92 * side effect: sets key option flags
93 */
Damien Millerf6d9e222000-06-18 14:50:44 +100094int
Damien Miller33804262001-02-04 23:20:18 +110095auth_parse_options(struct passwd *pw, char *opts, char *file, u_long linenum)
Damien Millerf6d9e222000-06-18 14:50:44 +100096{
97 const char *cp;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +000098 int i;
Damien Miller874d77b2000-10-14 16:23:11 +110099
100 /* reset options */
101 auth_clear_options();
102
Ben Lindstrom36d7bd02001-02-10 22:27:19 +0000103 if (!opts)
104 return 1;
105
Damien Miller33804262001-02-04 23:20:18 +1100106 while (*opts && *opts != ' ' && *opts != '\t') {
Damien Miller0a80ca12010-02-27 07:55:05 +1100107 cp = "cert-authority";
108 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
109 key_is_cert_authority = 1;
110 opts += strlen(cp);
111 goto next_option;
112 }
Damien Millerf6d9e222000-06-18 14:50:44 +1000113 cp = "no-port-forwarding";
Damien Miller33804262001-02-04 23:20:18 +1100114 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
Ben Lindstroma574cda2002-05-15 16:16:14 +0000115 auth_debug_add("Port forwarding disabled.");
Damien Millerf6d9e222000-06-18 14:50:44 +1000116 no_port_forwarding_flag = 1;
Damien Miller33804262001-02-04 23:20:18 +1100117 opts += strlen(cp);
Damien Millerf6d9e222000-06-18 14:50:44 +1000118 goto next_option;
119 }
120 cp = "no-agent-forwarding";
Damien Miller33804262001-02-04 23:20:18 +1100121 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
Ben Lindstroma574cda2002-05-15 16:16:14 +0000122 auth_debug_add("Agent forwarding disabled.");
Damien Millerf6d9e222000-06-18 14:50:44 +1000123 no_agent_forwarding_flag = 1;
Damien Miller33804262001-02-04 23:20:18 +1100124 opts += strlen(cp);
Damien Millerf6d9e222000-06-18 14:50:44 +1000125 goto next_option;
126 }
127 cp = "no-X11-forwarding";
Damien Miller33804262001-02-04 23:20:18 +1100128 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
Ben Lindstroma574cda2002-05-15 16:16:14 +0000129 auth_debug_add("X11 forwarding disabled.");
Damien Millerf6d9e222000-06-18 14:50:44 +1000130 no_x11_forwarding_flag = 1;
Damien Miller33804262001-02-04 23:20:18 +1100131 opts += strlen(cp);
Damien Millerf6d9e222000-06-18 14:50:44 +1000132 goto next_option;
133 }
134 cp = "no-pty";
Damien Miller33804262001-02-04 23:20:18 +1100135 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
Ben Lindstroma574cda2002-05-15 16:16:14 +0000136 auth_debug_add("Pty allocation disabled.");
Damien Millerf6d9e222000-06-18 14:50:44 +1000137 no_pty_flag = 1;
Damien Miller33804262001-02-04 23:20:18 +1100138 opts += strlen(cp);
Damien Millerf6d9e222000-06-18 14:50:44 +1000139 goto next_option;
140 }
Damien Miller95e80952008-03-27 11:03:05 +1100141 cp = "no-user-rc";
142 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
143 auth_debug_add("User rc file execution disabled.");
144 no_user_rc = 1;
145 opts += strlen(cp);
146 goto next_option;
147 }
Damien Millerf6d9e222000-06-18 14:50:44 +1000148 cp = "command=\"";
Damien Miller33804262001-02-04 23:20:18 +1100149 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
Damien Miller33804262001-02-04 23:20:18 +1100150 opts += strlen(cp);
Damien Miller30da3442010-05-10 11:58:03 +1000151 if (forced_command != NULL)
152 xfree(forced_command);
Damien Miller33804262001-02-04 23:20:18 +1100153 forced_command = 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 forced_command[i++] = '"';
161 continue;
162 }
Damien Miller33804262001-02-04 23:20:18 +1100163 forced_command[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(forced_command);
171 forced_command = NULL;
172 goto bad_option;
Damien Millerf6d9e222000-06-18 14:50:44 +1000173 }
Damien Miller98299262006-07-24 14:01:43 +1000174 forced_command[i] = '\0';
Damien Millerde53fd02011-01-06 22:44:18 +1100175 auth_debug_add("Forced command.");
Damien Miller33804262001-02-04 23:20:18 +1100176 opts++;
Damien Millerf6d9e222000-06-18 14:50:44 +1000177 goto next_option;
178 }
Damien Miller30da3442010-05-10 11:58:03 +1000179 cp = "principals=\"";
180 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
181 opts += strlen(cp);
182 if (authorized_principals != NULL)
183 xfree(authorized_principals);
184 authorized_principals = xmalloc(strlen(opts) + 1);
185 i = 0;
186 while (*opts) {
187 if (*opts == '"')
188 break;
189 if (*opts == '\\' && opts[1] == '"') {
190 opts += 2;
191 authorized_principals[i++] = '"';
192 continue;
193 }
194 authorized_principals[i++] = *opts++;
195 }
196 if (!*opts) {
197 debug("%.100s, line %lu: missing end quote",
198 file, linenum);
199 auth_debug_add("%.100s, line %lu: missing end quote",
200 file, linenum);
201 xfree(authorized_principals);
202 authorized_principals = NULL;
203 goto bad_option;
204 }
205 authorized_principals[i] = '\0';
206 auth_debug_add("principals: %.900s",
207 authorized_principals);
208 opts++;
209 goto next_option;
210 }
Damien Millerf6d9e222000-06-18 14:50:44 +1000211 cp = "environment=\"";
Ben Lindstrom5d860f02002-08-01 01:28:38 +0000212 if (options.permit_user_env &&
213 strncasecmp(opts, cp, strlen(cp)) == 0) {
Damien Millerf6d9e222000-06-18 14:50:44 +1000214 char *s;
215 struct envstring *new_envstring;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +0000216
Damien Miller33804262001-02-04 23:20:18 +1100217 opts += strlen(cp);
218 s = xmalloc(strlen(opts) + 1);
Damien Millerf6d9e222000-06-18 14:50:44 +1000219 i = 0;
Damien Miller33804262001-02-04 23:20:18 +1100220 while (*opts) {
221 if (*opts == '"')
Damien Millerf6d9e222000-06-18 14:50:44 +1000222 break;
Damien Miller33804262001-02-04 23:20:18 +1100223 if (*opts == '\\' && opts[1] == '"') {
224 opts += 2;
Damien Millerf6d9e222000-06-18 14:50:44 +1000225 s[i++] = '"';
226 continue;
227 }
Damien Miller33804262001-02-04 23:20:18 +1100228 s[i++] = *opts++;
Damien Millerf6d9e222000-06-18 14:50:44 +1000229 }
Damien Miller33804262001-02-04 23:20:18 +1100230 if (!*opts) {
Damien Millerf6d9e222000-06-18 14:50:44 +1000231 debug("%.100s, line %lu: missing end quote",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000232 file, linenum);
Ben Lindstroma574cda2002-05-15 16:16:14 +0000233 auth_debug_add("%.100s, line %lu: missing end quote",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000234 file, linenum);
Damien Miller056ddf72001-03-14 10:15:20 +1100235 xfree(s);
236 goto bad_option;
Damien Millerf6d9e222000-06-18 14:50:44 +1000237 }
Damien Miller98299262006-07-24 14:01:43 +1000238 s[i] = '\0';
Ben Lindstroma574cda2002-05-15 16:16:14 +0000239 auth_debug_add("Adding to environment: %.900s", s);
Damien Millerf6d9e222000-06-18 14:50:44 +1000240 debug("Adding to environment: %.900s", s);
Damien Miller33804262001-02-04 23:20:18 +1100241 opts++;
Damien Millerf6d9e222000-06-18 14:50:44 +1000242 new_envstring = xmalloc(sizeof(struct envstring));
243 new_envstring->s = s;
244 new_envstring->next = custom_environment;
245 custom_environment = new_envstring;
246 goto next_option;
247 }
248 cp = "from=\"";
Damien Miller33804262001-02-04 23:20:18 +1100249 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
Damien Miller33804262001-02-04 23:20:18 +1100250 const char *remote_ip = get_remote_ipaddr();
251 const char *remote_host = get_canonical_hostname(
Damien Miller3a961dc2003-06-03 10:25:48 +1000252 options.use_dns);
Damien Miller33804262001-02-04 23:20:18 +1100253 char *patterns = xmalloc(strlen(opts) + 1);
Ben Lindstrom7bb8b492001-03-17 00:47:54 +0000254
Damien Miller33804262001-02-04 23:20:18 +1100255 opts += strlen(cp);
Damien Millerf6d9e222000-06-18 14:50:44 +1000256 i = 0;
Damien Miller33804262001-02-04 23:20:18 +1100257 while (*opts) {
258 if (*opts == '"')
Damien Millerf6d9e222000-06-18 14:50:44 +1000259 break;
Damien Miller33804262001-02-04 23:20:18 +1100260 if (*opts == '\\' && opts[1] == '"') {
261 opts += 2;
Damien Millerf6d9e222000-06-18 14:50:44 +1000262 patterns[i++] = '"';
263 continue;
264 }
Damien Miller33804262001-02-04 23:20:18 +1100265 patterns[i++] = *opts++;
Damien Millerf6d9e222000-06-18 14:50:44 +1000266 }
Damien Miller33804262001-02-04 23:20:18 +1100267 if (!*opts) {
Damien Millerf6d9e222000-06-18 14:50:44 +1000268 debug("%.100s, line %lu: missing end quote",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000269 file, linenum);
Ben Lindstroma574cda2002-05-15 16:16:14 +0000270 auth_debug_add("%.100s, line %lu: missing end quote",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000271 file, linenum);
Damien Miller056ddf72001-03-14 10:15:20 +1100272 xfree(patterns);
273 goto bad_option;
Damien Millerf6d9e222000-06-18 14:50:44 +1000274 }
Damien Miller98299262006-07-24 14:01:43 +1000275 patterns[i] = '\0';
Damien Miller33804262001-02-04 23:20:18 +1100276 opts++;
Darren Tucker896ad5a2008-06-11 09:34:46 +1000277 switch (match_host_and_ip(remote_host, remote_ip,
278 patterns)) {
279 case 1:
280 xfree(patterns);
281 /* Host name matches. */
282 goto next_option;
283 case -1:
284 debug("%.100s, line %lu: invalid criteria",
285 file, linenum);
286 auth_debug_add("%.100s, line %lu: "
287 "invalid criteria", file, linenum);
288 /* FALLTHROUGH */
289 case 0:
Ben Lindstromf0c50292001-06-25 05:17:53 +0000290 xfree(patterns);
Damien Miller996acd22003-04-09 20:59:48 +1000291 logit("Authentication tried for %.100s with "
Damien Miller33804262001-02-04 23:20:18 +1100292 "correct key but not from a permitted "
293 "host (host=%.200s, ip=%.200s).",
294 pw->pw_name, remote_host, remote_ip);
Ben Lindstroma574cda2002-05-15 16:16:14 +0000295 auth_debug_add("Your host '%.200s' is not "
Damien Miller33804262001-02-04 23:20:18 +1100296 "permitted to use this key for login.",
297 remote_host);
Darren Tucker896ad5a2008-06-11 09:34:46 +1000298 break;
Damien Millerf6d9e222000-06-18 14:50:44 +1000299 }
Darren Tucker896ad5a2008-06-11 09:34:46 +1000300 /* deny access */
301 return 0;
Damien Millerf6d9e222000-06-18 14:50:44 +1000302 }
Ben Lindstrom7bb8b492001-03-17 00:47:54 +0000303 cp = "permitopen=\"";
304 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
Damien Millerf91ee4c2005-03-01 21:24:33 +1100305 char *host, *p;
Damien Millere37dde02009-01-28 16:33:01 +1100306 int port;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +0000307 char *patterns = xmalloc(strlen(opts) + 1);
308
309 opts += strlen(cp);
310 i = 0;
311 while (*opts) {
312 if (*opts == '"')
313 break;
314 if (*opts == '\\' && opts[1] == '"') {
315 opts += 2;
316 patterns[i++] = '"';
317 continue;
318 }
319 patterns[i++] = *opts++;
320 }
321 if (!*opts) {
322 debug("%.100s, line %lu: missing end quote",
323 file, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100324 auth_debug_add("%.100s, line %lu: missing "
325 "end quote", file, linenum);
Ben Lindstrom7bb8b492001-03-17 00:47:54 +0000326 xfree(patterns);
327 goto bad_option;
328 }
Damien Miller98299262006-07-24 14:01:43 +1000329 patterns[i] = '\0';
Ben Lindstrom7bb8b492001-03-17 00:47:54 +0000330 opts++;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100331 p = patterns;
332 host = hpdelim(&p);
333 if (host == NULL || strlen(host) >= NI_MAXHOST) {
334 debug("%.100s, line %lu: Bad permitopen "
Darren Tucker90b9e022005-03-14 23:08:50 +1100335 "specification <%.100s>", file, linenum,
Damien Millerf91ee4c2005-03-01 21:24:33 +1100336 patterns);
Ben Lindstroma574cda2002-05-15 16:16:14 +0000337 auth_debug_add("%.100s, line %lu: "
Damien Millerf91ee4c2005-03-01 21:24:33 +1100338 "Bad permitopen specification", file,
339 linenum);
Ben Lindstrom7bb8b492001-03-17 00:47:54 +0000340 xfree(patterns);
341 goto bad_option;
342 }
Darren Tucker47eede72005-03-14 23:08:12 +1100343 host = cleanhostname(host);
Darren Tucker1338b9e2011-10-02 18:57:35 +1100344 if (p == NULL || (port = permitopen_port(p)) < 0) {
Damien Millerf91ee4c2005-03-01 21:24:33 +1100345 debug("%.100s, line %lu: Bad permitopen port "
346 "<%.100s>", file, linenum, p ? p : "");
Ben Lindstroma574cda2002-05-15 16:16:14 +0000347 auth_debug_add("%.100s, line %lu: "
Ben Lindstromd71ba572001-09-12 18:03:31 +0000348 "Bad permitopen port", file, linenum);
Ben Lindstrom7bb8b492001-03-17 00:47:54 +0000349 xfree(patterns);
350 goto bad_option;
351 }
Damien Milleraa5b3f82012-12-03 09:50:54 +1100352 if ((options.allow_tcp_forwarding & FORWARD_LOCAL) != 0)
Ben Lindstromd71ba572001-09-12 18:03:31 +0000353 channel_add_permitted_opens(host, port);
Ben Lindstrom7bb8b492001-03-17 00:47:54 +0000354 xfree(patterns);
355 goto next_option;
356 }
Damien Millerd27b9472005-12-13 19:29:02 +1100357 cp = "tunnel=\"";
358 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
359 char *tun = NULL;
360 opts += strlen(cp);
361 tun = xmalloc(strlen(opts) + 1);
362 i = 0;
363 while (*opts) {
364 if (*opts == '"')
365 break;
366 tun[i++] = *opts++;
367 }
368 if (!*opts) {
369 debug("%.100s, line %lu: missing end quote",
370 file, linenum);
371 auth_debug_add("%.100s, line %lu: missing end quote",
372 file, linenum);
373 xfree(tun);
374 forced_tun_device = -1;
375 goto bad_option;
376 }
Damien Miller98299262006-07-24 14:01:43 +1000377 tun[i] = '\0';
Damien Millerd27b9472005-12-13 19:29:02 +1100378 forced_tun_device = a2tun(tun, NULL);
379 xfree(tun);
Damien Miller7b58e802005-12-13 19:33:19 +1100380 if (forced_tun_device == SSH_TUNID_ERR) {
Damien Millerd27b9472005-12-13 19:29:02 +1100381 debug("%.100s, line %lu: invalid tun device",
382 file, linenum);
383 auth_debug_add("%.100s, line %lu: invalid tun device",
384 file, linenum);
385 forced_tun_device = -1;
386 goto bad_option;
387 }
388 auth_debug_add("Forced tun device: %d", forced_tun_device);
389 opts++;
390 goto next_option;
391 }
Damien Millerf6d9e222000-06-18 14:50:44 +1000392next_option:
393 /*
394 * Skip the comma, and move to the next option
395 * (or break out if there are no more).
396 */
Damien Miller33804262001-02-04 23:20:18 +1100397 if (!*opts)
Damien Millerf6d9e222000-06-18 14:50:44 +1000398 fatal("Bugs in auth-options.c option processing.");
Damien Miller33804262001-02-04 23:20:18 +1100399 if (*opts == ' ' || *opts == '\t')
Damien Millerf6d9e222000-06-18 14:50:44 +1000400 break; /* End of options. */
Damien Miller33804262001-02-04 23:20:18 +1100401 if (*opts != ',')
Damien Millerf6d9e222000-06-18 14:50:44 +1000402 goto bad_option;
Damien Miller33804262001-02-04 23:20:18 +1100403 opts++;
Damien Millerf6d9e222000-06-18 14:50:44 +1000404 /* Process the next option. */
405 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000406
Damien Millerf6d9e222000-06-18 14:50:44 +1000407 /* grant access */
408 return 1;
409
410bad_option:
Damien Miller996acd22003-04-09 20:59:48 +1000411 logit("Bad options in %.100s file, line %lu: %.50s",
Damien Miller33804262001-02-04 23:20:18 +1100412 file, linenum, opts);
Ben Lindstroma574cda2002-05-15 16:16:14 +0000413 auth_debug_add("Bad options in %.100s file, line %lu: %.50s",
Damien Miller33804262001-02-04 23:20:18 +1100414 file, linenum, opts);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000415
Damien Millerf6d9e222000-06-18 14:50:44 +1000416 /* deny access */
417 return 0;
418}
Damien Miller0a80ca12010-02-27 07:55:05 +1100419
Damien Millerd0e4a8e2010-05-21 14:58:32 +1000420#define OPTIONS_CRITICAL 1
421#define OPTIONS_EXTENSIONS 2
422static int
423parse_option_list(u_char *optblob, size_t optblob_len, struct passwd *pw,
424 u_int which, int crit,
425 int *cert_no_port_forwarding_flag,
426 int *cert_no_agent_forwarding_flag,
427 int *cert_no_x11_forwarding_flag,
428 int *cert_no_pty_flag,
429 int *cert_no_user_rc,
430 char **cert_forced_command,
431 int *cert_source_address_done)
Damien Miller0a80ca12010-02-27 07:55:05 +1100432{
Damien Millerd0e4a8e2010-05-21 14:58:32 +1000433 char *command, *allowed;
434 const char *remote_ip;
Damien Miller0a80ca12010-02-27 07:55:05 +1100435 u_char *name = NULL, *data_blob = NULL;
Damien Miller41396572010-03-04 21:51:11 +1100436 u_int nlen, dlen, clen;
Damien Miller0a80ca12010-02-27 07:55:05 +1100437 Buffer c, data;
Damien Millerd0e4a8e2010-05-21 14:58:32 +1000438 int ret = -1, found;
Damien Miller0a80ca12010-02-27 07:55:05 +1100439
440 buffer_init(&data);
441
442 /* Make copy to avoid altering original */
443 buffer_init(&c);
Damien Millerd0e4a8e2010-05-21 14:58:32 +1000444 buffer_append(&c, optblob, optblob_len);
Damien Miller0a80ca12010-02-27 07:55:05 +1100445
446 while (buffer_len(&c) > 0) {
Damien Millerda108ec2010-08-31 22:36:39 +1000447 if ((name = buffer_get_cstring_ret(&c, &nlen)) == NULL ||
Damien Miller41396572010-03-04 21:51:11 +1100448 (data_blob = buffer_get_string_ret(&c, &dlen)) == NULL) {
Damien Miller4e270b02010-04-16 15:56:21 +1000449 error("Certificate options corrupt");
Damien Miller0a80ca12010-02-27 07:55:05 +1100450 goto out;
451 }
Damien Miller41396572010-03-04 21:51:11 +1100452 buffer_append(&data, data_blob, dlen);
Damien Millerd0e4a8e2010-05-21 14:58:32 +1000453 debug3("found certificate option \"%.100s\" len %u",
Damien Miller41396572010-03-04 21:51:11 +1100454 name, dlen);
Damien Millerd0e4a8e2010-05-21 14:58:32 +1000455 found = 0;
456 if ((which & OPTIONS_EXTENSIONS) != 0) {
457 if (strcmp(name, "permit-X11-forwarding") == 0) {
458 *cert_no_x11_forwarding_flag = 0;
459 found = 1;
460 } else if (strcmp(name,
461 "permit-agent-forwarding") == 0) {
462 *cert_no_agent_forwarding_flag = 0;
463 found = 1;
464 } else if (strcmp(name,
465 "permit-port-forwarding") == 0) {
466 *cert_no_port_forwarding_flag = 0;
467 found = 1;
468 } else if (strcmp(name, "permit-pty") == 0) {
469 *cert_no_pty_flag = 0;
470 found = 1;
471 } else if (strcmp(name, "permit-user-rc") == 0) {
472 *cert_no_user_rc = 0;
473 found = 1;
Damien Miller0a80ca12010-02-27 07:55:05 +1100474 }
Damien Millerd0e4a8e2010-05-21 14:58:32 +1000475 }
476 if (!found && (which & OPTIONS_CRITICAL) != 0) {
477 if (strcmp(name, "force-command") == 0) {
Damien Millerda108ec2010-08-31 22:36:39 +1000478 if ((command = buffer_get_cstring_ret(&data,
Damien Millerd0e4a8e2010-05-21 14:58:32 +1000479 &clen)) == NULL) {
480 error("Certificate constraint \"%s\" "
481 "corrupt", name);
482 goto out;
483 }
Damien Millerd0e4a8e2010-05-21 14:58:32 +1000484 if (*cert_forced_command != NULL) {
485 error("Certificate has multiple "
486 "force-command options");
487 xfree(command);
488 goto out;
489 }
490 *cert_forced_command = command;
491 found = 1;
Damien Miller41396572010-03-04 21:51:11 +1100492 }
Damien Millerd0e4a8e2010-05-21 14:58:32 +1000493 if (strcmp(name, "source-address") == 0) {
Damien Millerda108ec2010-08-31 22:36:39 +1000494 if ((allowed = buffer_get_cstring_ret(&data,
Damien Millerd0e4a8e2010-05-21 14:58:32 +1000495 &clen)) == NULL) {
496 error("Certificate constraint "
497 "\"%s\" corrupt", name);
498 goto out;
499 }
Damien Millerd0e4a8e2010-05-21 14:58:32 +1000500 if ((*cert_source_address_done)++) {
501 error("Certificate has multiple "
502 "source-address options");
503 xfree(allowed);
504 goto out;
505 }
506 remote_ip = get_remote_ipaddr();
507 switch (addr_match_cidr_list(remote_ip,
508 allowed)) {
509 case 1:
510 /* accepted */
511 xfree(allowed);
512 break;
513 case 0:
514 /* no match */
515 logit("Authentication tried for %.100s "
516 "with valid certificate but not "
517 "from a permitted host "
518 "(ip=%.200s).", pw->pw_name,
519 remote_ip);
520 auth_debug_add("Your address '%.200s' "
521 "is not permitted to use this "
522 "certificate for login.",
523 remote_ip);
524 xfree(allowed);
525 goto out;
526 case -1:
527 error("Certificate source-address "
528 "contents invalid");
529 xfree(allowed);
530 goto out;
531 }
532 found = 1;
Damien Miller0a80ca12010-02-27 07:55:05 +1100533 }
Damien Miller0a80ca12010-02-27 07:55:05 +1100534 }
535
Damien Millerd0e4a8e2010-05-21 14:58:32 +1000536 if (!found) {
537 if (crit) {
538 error("Certificate critical option \"%s\" "
539 "is not supported", name);
540 goto out;
541 } else {
542 logit("Certificate extension \"%s\" "
543 "is not supported", name);
544 }
545 } else if (buffer_len(&data) != 0) {
546 error("Certificate option \"%s\" corrupt "
Damien Miller0a80ca12010-02-27 07:55:05 +1100547 "(extra data)", name);
548 goto out;
549 }
550 buffer_clear(&data);
551 xfree(name);
552 xfree(data_blob);
553 name = data_blob = NULL;
554 }
Damien Miller4e270b02010-04-16 15:56:21 +1000555 /* successfully parsed all options */
Damien Miller0a80ca12010-02-27 07:55:05 +1100556 ret = 0;
557
Damien Millerd0e4a8e2010-05-21 14:58:32 +1000558 out:
559 if (ret != 0 &&
560 cert_forced_command != NULL &&
561 *cert_forced_command != NULL) {
562 xfree(*cert_forced_command);
563 *cert_forced_command = NULL;
564 }
565 if (name != NULL)
566 xfree(name);
567 if (data_blob != NULL)
568 xfree(data_blob);
569 buffer_free(&data);
570 buffer_free(&c);
571 return ret;
572}
573
574/*
575 * Set options from critical certificate options. These supersede user key
576 * options so this must be called after auth_parse_options().
577 */
578int
579auth_cert_options(Key *k, struct passwd *pw)
580{
581 int cert_no_port_forwarding_flag = 1;
582 int cert_no_agent_forwarding_flag = 1;
583 int cert_no_x11_forwarding_flag = 1;
584 int cert_no_pty_flag = 1;
585 int cert_no_user_rc = 1;
586 char *cert_forced_command = NULL;
587 int cert_source_address_done = 0;
588
589 if (key_cert_is_legacy(k)) {
590 /* All options are in the one field for v00 certs */
591 if (parse_option_list(buffer_ptr(&k->cert->critical),
592 buffer_len(&k->cert->critical), pw,
593 OPTIONS_CRITICAL|OPTIONS_EXTENSIONS, 1,
594 &cert_no_port_forwarding_flag,
595 &cert_no_agent_forwarding_flag,
596 &cert_no_x11_forwarding_flag,
597 &cert_no_pty_flag,
598 &cert_no_user_rc,
599 &cert_forced_command,
600 &cert_source_address_done) == -1)
601 return -1;
602 } else {
603 /* Separate options and extensions for v01 certs */
604 if (parse_option_list(buffer_ptr(&k->cert->critical),
605 buffer_len(&k->cert->critical), pw,
606 OPTIONS_CRITICAL, 1, NULL, NULL, NULL, NULL, NULL,
607 &cert_forced_command,
608 &cert_source_address_done) == -1)
609 return -1;
610 if (parse_option_list(buffer_ptr(&k->cert->extensions),
611 buffer_len(&k->cert->extensions), pw,
612 OPTIONS_EXTENSIONS, 1,
613 &cert_no_port_forwarding_flag,
614 &cert_no_agent_forwarding_flag,
615 &cert_no_x11_forwarding_flag,
616 &cert_no_pty_flag,
617 &cert_no_user_rc,
618 NULL, NULL) == -1)
619 return -1;
620 }
621
Damien Miller0a80ca12010-02-27 07:55:05 +1100622 no_port_forwarding_flag |= cert_no_port_forwarding_flag;
623 no_agent_forwarding_flag |= cert_no_agent_forwarding_flag;
624 no_x11_forwarding_flag |= cert_no_x11_forwarding_flag;
625 no_pty_flag |= cert_no_pty_flag;
626 no_user_rc |= cert_no_user_rc;
627 /* CA-specified forced command supersedes key option */
628 if (cert_forced_command != NULL) {
629 if (forced_command != NULL)
630 xfree(forced_command);
631 forced_command = cert_forced_command;
632 }
Damien Millerd0e4a8e2010-05-21 14:58:32 +1000633 return 0;
Damien Miller0a80ca12010-02-27 07:55:05 +1100634}
635