blob: 60d5f749b0d068a0b697131c87ebb5cb63d0beca [file] [log] [blame]
Damien Miller4e270b02010-04-16 15:56:21 +10001/* $OpenBSD: auth-options.c,v 1.50 2010/04/16 01:47:26 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 Miller33804262001-02-04 23:20:18 +110058extern ServerOptions options;
59
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000060void
Damien Miller874d77b2000-10-14 16:23:11 +110061auth_clear_options(void)
62{
63 no_agent_forwarding_flag = 0;
64 no_port_forwarding_flag = 0;
65 no_pty_flag = 0;
66 no_x11_forwarding_flag = 0;
Damien Miller95e80952008-03-27 11:03:05 +110067 no_user_rc = 0;
Damien Miller0a80ca12010-02-27 07:55:05 +110068 key_is_cert_authority = 0;
Damien Miller874d77b2000-10-14 16:23:11 +110069 while (custom_environment) {
70 struct envstring *ce = custom_environment;
71 custom_environment = ce->next;
72 xfree(ce->s);
73 xfree(ce);
74 }
75 if (forced_command) {
76 xfree(forced_command);
77 forced_command = NULL;
78 }
Damien Millerd27b9472005-12-13 19:29:02 +110079 forced_tun_device = -1;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +000080 channel_clear_permitted_opens();
Damien Miller874d77b2000-10-14 16:23:11 +110081}
82
Ben Lindstrom226cfa02001-01-22 05:34:40 +000083/*
84 * return 1 if access is granted, 0 if not.
85 * side effect: sets key option flags
86 */
Damien Millerf6d9e222000-06-18 14:50:44 +100087int
Damien Miller33804262001-02-04 23:20:18 +110088auth_parse_options(struct passwd *pw, char *opts, char *file, u_long linenum)
Damien Millerf6d9e222000-06-18 14:50:44 +100089{
90 const char *cp;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +000091 int i;
Damien Miller874d77b2000-10-14 16:23:11 +110092
93 /* reset options */
94 auth_clear_options();
95
Ben Lindstrom36d7bd02001-02-10 22:27:19 +000096 if (!opts)
97 return 1;
98
Damien Miller33804262001-02-04 23:20:18 +110099 while (*opts && *opts != ' ' && *opts != '\t') {
Damien Miller0a80ca12010-02-27 07:55:05 +1100100 cp = "cert-authority";
101 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
102 key_is_cert_authority = 1;
103 opts += strlen(cp);
104 goto next_option;
105 }
Damien Millerf6d9e222000-06-18 14:50:44 +1000106 cp = "no-port-forwarding";
Damien Miller33804262001-02-04 23:20:18 +1100107 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
Ben Lindstroma574cda2002-05-15 16:16:14 +0000108 auth_debug_add("Port forwarding disabled.");
Damien Millerf6d9e222000-06-18 14:50:44 +1000109 no_port_forwarding_flag = 1;
Damien Miller33804262001-02-04 23:20:18 +1100110 opts += strlen(cp);
Damien Millerf6d9e222000-06-18 14:50:44 +1000111 goto next_option;
112 }
113 cp = "no-agent-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("Agent forwarding disabled.");
Damien Millerf6d9e222000-06-18 14:50:44 +1000116 no_agent_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-X11-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("X11 forwarding disabled.");
Damien Millerf6d9e222000-06-18 14:50:44 +1000123 no_x11_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-pty";
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("Pty allocation disabled.");
Damien Millerf6d9e222000-06-18 14:50:44 +1000130 no_pty_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 }
Damien Miller95e80952008-03-27 11:03:05 +1100134 cp = "no-user-rc";
135 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
136 auth_debug_add("User rc file execution disabled.");
137 no_user_rc = 1;
138 opts += strlen(cp);
139 goto next_option;
140 }
Damien Millerf6d9e222000-06-18 14:50:44 +1000141 cp = "command=\"";
Damien Miller33804262001-02-04 23:20:18 +1100142 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
Damien Miller33804262001-02-04 23:20:18 +1100143 opts += strlen(cp);
144 forced_command = xmalloc(strlen(opts) + 1);
Damien Millerf6d9e222000-06-18 14:50:44 +1000145 i = 0;
Damien Miller33804262001-02-04 23:20:18 +1100146 while (*opts) {
147 if (*opts == '"')
Damien Millerf6d9e222000-06-18 14:50:44 +1000148 break;
Damien Miller33804262001-02-04 23:20:18 +1100149 if (*opts == '\\' && opts[1] == '"') {
150 opts += 2;
Damien Millerf6d9e222000-06-18 14:50:44 +1000151 forced_command[i++] = '"';
152 continue;
153 }
Damien Miller33804262001-02-04 23:20:18 +1100154 forced_command[i++] = *opts++;
Damien Millerf6d9e222000-06-18 14:50:44 +1000155 }
Damien Miller33804262001-02-04 23:20:18 +1100156 if (!*opts) {
Damien Millerf6d9e222000-06-18 14:50:44 +1000157 debug("%.100s, line %lu: missing end quote",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000158 file, linenum);
Ben Lindstroma574cda2002-05-15 16:16:14 +0000159 auth_debug_add("%.100s, line %lu: missing end quote",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000160 file, linenum);
Damien Miller056ddf72001-03-14 10:15:20 +1100161 xfree(forced_command);
162 forced_command = NULL;
163 goto bad_option;
Damien Millerf6d9e222000-06-18 14:50:44 +1000164 }
Damien Miller98299262006-07-24 14:01:43 +1000165 forced_command[i] = '\0';
Ben Lindstroma574cda2002-05-15 16:16:14 +0000166 auth_debug_add("Forced command: %.900s", forced_command);
Damien Miller33804262001-02-04 23:20:18 +1100167 opts++;
Damien Millerf6d9e222000-06-18 14:50:44 +1000168 goto next_option;
169 }
170 cp = "environment=\"";
Ben Lindstrom5d860f02002-08-01 01:28:38 +0000171 if (options.permit_user_env &&
172 strncasecmp(opts, cp, strlen(cp)) == 0) {
Damien Millerf6d9e222000-06-18 14:50:44 +1000173 char *s;
174 struct envstring *new_envstring;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +0000175
Damien Miller33804262001-02-04 23:20:18 +1100176 opts += strlen(cp);
177 s = xmalloc(strlen(opts) + 1);
Damien Millerf6d9e222000-06-18 14:50:44 +1000178 i = 0;
Damien Miller33804262001-02-04 23:20:18 +1100179 while (*opts) {
180 if (*opts == '"')
Damien Millerf6d9e222000-06-18 14:50:44 +1000181 break;
Damien Miller33804262001-02-04 23:20:18 +1100182 if (*opts == '\\' && opts[1] == '"') {
183 opts += 2;
Damien Millerf6d9e222000-06-18 14:50:44 +1000184 s[i++] = '"';
185 continue;
186 }
Damien Miller33804262001-02-04 23:20:18 +1100187 s[i++] = *opts++;
Damien Millerf6d9e222000-06-18 14:50:44 +1000188 }
Damien Miller33804262001-02-04 23:20:18 +1100189 if (!*opts) {
Damien Millerf6d9e222000-06-18 14:50:44 +1000190 debug("%.100s, line %lu: missing end quote",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000191 file, linenum);
Ben Lindstroma574cda2002-05-15 16:16:14 +0000192 auth_debug_add("%.100s, line %lu: missing end quote",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000193 file, linenum);
Damien Miller056ddf72001-03-14 10:15:20 +1100194 xfree(s);
195 goto bad_option;
Damien Millerf6d9e222000-06-18 14:50:44 +1000196 }
Damien Miller98299262006-07-24 14:01:43 +1000197 s[i] = '\0';
Ben Lindstroma574cda2002-05-15 16:16:14 +0000198 auth_debug_add("Adding to environment: %.900s", s);
Damien Millerf6d9e222000-06-18 14:50:44 +1000199 debug("Adding to environment: %.900s", s);
Damien Miller33804262001-02-04 23:20:18 +1100200 opts++;
Damien Millerf6d9e222000-06-18 14:50:44 +1000201 new_envstring = xmalloc(sizeof(struct envstring));
202 new_envstring->s = s;
203 new_envstring->next = custom_environment;
204 custom_environment = new_envstring;
205 goto next_option;
206 }
207 cp = "from=\"";
Damien Miller33804262001-02-04 23:20:18 +1100208 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
Damien Miller33804262001-02-04 23:20:18 +1100209 const char *remote_ip = get_remote_ipaddr();
210 const char *remote_host = get_canonical_hostname(
Damien Miller3a961dc2003-06-03 10:25:48 +1000211 options.use_dns);
Damien Miller33804262001-02-04 23:20:18 +1100212 char *patterns = xmalloc(strlen(opts) + 1);
Ben Lindstrom7bb8b492001-03-17 00:47:54 +0000213
Damien Miller33804262001-02-04 23:20:18 +1100214 opts += strlen(cp);
Damien Millerf6d9e222000-06-18 14:50:44 +1000215 i = 0;
Damien Miller33804262001-02-04 23:20:18 +1100216 while (*opts) {
217 if (*opts == '"')
Damien Millerf6d9e222000-06-18 14:50:44 +1000218 break;
Damien Miller33804262001-02-04 23:20:18 +1100219 if (*opts == '\\' && opts[1] == '"') {
220 opts += 2;
Damien Millerf6d9e222000-06-18 14:50:44 +1000221 patterns[i++] = '"';
222 continue;
223 }
Damien Miller33804262001-02-04 23:20:18 +1100224 patterns[i++] = *opts++;
Damien Millerf6d9e222000-06-18 14:50:44 +1000225 }
Damien Miller33804262001-02-04 23:20:18 +1100226 if (!*opts) {
Damien Millerf6d9e222000-06-18 14:50:44 +1000227 debug("%.100s, line %lu: missing end quote",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000228 file, linenum);
Ben Lindstroma574cda2002-05-15 16:16:14 +0000229 auth_debug_add("%.100s, line %lu: missing end quote",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000230 file, linenum);
Damien Miller056ddf72001-03-14 10:15:20 +1100231 xfree(patterns);
232 goto bad_option;
Damien Millerf6d9e222000-06-18 14:50:44 +1000233 }
Damien Miller98299262006-07-24 14:01:43 +1000234 patterns[i] = '\0';
Damien Miller33804262001-02-04 23:20:18 +1100235 opts++;
Darren Tucker896ad5a2008-06-11 09:34:46 +1000236 switch (match_host_and_ip(remote_host, remote_ip,
237 patterns)) {
238 case 1:
239 xfree(patterns);
240 /* Host name matches. */
241 goto next_option;
242 case -1:
243 debug("%.100s, line %lu: invalid criteria",
244 file, linenum);
245 auth_debug_add("%.100s, line %lu: "
246 "invalid criteria", file, linenum);
247 /* FALLTHROUGH */
248 case 0:
Ben Lindstromf0c50292001-06-25 05:17:53 +0000249 xfree(patterns);
Damien Miller996acd22003-04-09 20:59:48 +1000250 logit("Authentication tried for %.100s with "
Damien Miller33804262001-02-04 23:20:18 +1100251 "correct key but not from a permitted "
252 "host (host=%.200s, ip=%.200s).",
253 pw->pw_name, remote_host, remote_ip);
Ben Lindstroma574cda2002-05-15 16:16:14 +0000254 auth_debug_add("Your host '%.200s' is not "
Damien Miller33804262001-02-04 23:20:18 +1100255 "permitted to use this key for login.",
256 remote_host);
Darren Tucker896ad5a2008-06-11 09:34:46 +1000257 break;
Damien Millerf6d9e222000-06-18 14:50:44 +1000258 }
Darren Tucker896ad5a2008-06-11 09:34:46 +1000259 /* deny access */
260 return 0;
Damien Millerf6d9e222000-06-18 14:50:44 +1000261 }
Ben Lindstrom7bb8b492001-03-17 00:47:54 +0000262 cp = "permitopen=\"";
263 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
Damien Millerf91ee4c2005-03-01 21:24:33 +1100264 char *host, *p;
Damien Millere37dde02009-01-28 16:33:01 +1100265 int port;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +0000266 char *patterns = xmalloc(strlen(opts) + 1);
267
268 opts += strlen(cp);
269 i = 0;
270 while (*opts) {
271 if (*opts == '"')
272 break;
273 if (*opts == '\\' && opts[1] == '"') {
274 opts += 2;
275 patterns[i++] = '"';
276 continue;
277 }
278 patterns[i++] = *opts++;
279 }
280 if (!*opts) {
281 debug("%.100s, line %lu: missing end quote",
282 file, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100283 auth_debug_add("%.100s, line %lu: missing "
284 "end quote", file, linenum);
Ben Lindstrom7bb8b492001-03-17 00:47:54 +0000285 xfree(patterns);
286 goto bad_option;
287 }
Damien Miller98299262006-07-24 14:01:43 +1000288 patterns[i] = '\0';
Ben Lindstrom7bb8b492001-03-17 00:47:54 +0000289 opts++;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100290 p = patterns;
291 host = hpdelim(&p);
292 if (host == NULL || strlen(host) >= NI_MAXHOST) {
293 debug("%.100s, line %lu: Bad permitopen "
Darren Tucker90b9e022005-03-14 23:08:50 +1100294 "specification <%.100s>", file, linenum,
Damien Millerf91ee4c2005-03-01 21:24:33 +1100295 patterns);
Ben Lindstroma574cda2002-05-15 16:16:14 +0000296 auth_debug_add("%.100s, line %lu: "
Damien Millerf91ee4c2005-03-01 21:24:33 +1100297 "Bad permitopen specification", file,
298 linenum);
Ben Lindstrom7bb8b492001-03-17 00:47:54 +0000299 xfree(patterns);
300 goto bad_option;
301 }
Darren Tucker47eede72005-03-14 23:08:12 +1100302 host = cleanhostname(host);
Damien Millere37dde02009-01-28 16:33:01 +1100303 if (p == NULL || (port = a2port(p)) <= 0) {
Damien Millerf91ee4c2005-03-01 21:24:33 +1100304 debug("%.100s, line %lu: Bad permitopen port "
305 "<%.100s>", file, linenum, p ? p : "");
Ben Lindstroma574cda2002-05-15 16:16:14 +0000306 auth_debug_add("%.100s, line %lu: "
Ben Lindstromd71ba572001-09-12 18:03:31 +0000307 "Bad permitopen port", file, linenum);
Ben Lindstrom7bb8b492001-03-17 00:47:54 +0000308 xfree(patterns);
309 goto bad_option;
310 }
Ben Lindstrom2d70f982001-03-19 00:13:46 +0000311 if (options.allow_tcp_forwarding)
Ben Lindstromd71ba572001-09-12 18:03:31 +0000312 channel_add_permitted_opens(host, port);
Ben Lindstrom7bb8b492001-03-17 00:47:54 +0000313 xfree(patterns);
314 goto next_option;
315 }
Damien Millerd27b9472005-12-13 19:29:02 +1100316 cp = "tunnel=\"";
317 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
318 char *tun = NULL;
319 opts += strlen(cp);
320 tun = xmalloc(strlen(opts) + 1);
321 i = 0;
322 while (*opts) {
323 if (*opts == '"')
324 break;
325 tun[i++] = *opts++;
326 }
327 if (!*opts) {
328 debug("%.100s, line %lu: missing end quote",
329 file, linenum);
330 auth_debug_add("%.100s, line %lu: missing end quote",
331 file, linenum);
332 xfree(tun);
333 forced_tun_device = -1;
334 goto bad_option;
335 }
Damien Miller98299262006-07-24 14:01:43 +1000336 tun[i] = '\0';
Damien Millerd27b9472005-12-13 19:29:02 +1100337 forced_tun_device = a2tun(tun, NULL);
338 xfree(tun);
Damien Miller7b58e802005-12-13 19:33:19 +1100339 if (forced_tun_device == SSH_TUNID_ERR) {
Damien Millerd27b9472005-12-13 19:29:02 +1100340 debug("%.100s, line %lu: invalid tun device",
341 file, linenum);
342 auth_debug_add("%.100s, line %lu: invalid tun device",
343 file, linenum);
344 forced_tun_device = -1;
345 goto bad_option;
346 }
347 auth_debug_add("Forced tun device: %d", forced_tun_device);
348 opts++;
349 goto next_option;
350 }
Damien Millerf6d9e222000-06-18 14:50:44 +1000351next_option:
352 /*
353 * Skip the comma, and move to the next option
354 * (or break out if there are no more).
355 */
Damien Miller33804262001-02-04 23:20:18 +1100356 if (!*opts)
Damien Millerf6d9e222000-06-18 14:50:44 +1000357 fatal("Bugs in auth-options.c option processing.");
Damien Miller33804262001-02-04 23:20:18 +1100358 if (*opts == ' ' || *opts == '\t')
Damien Millerf6d9e222000-06-18 14:50:44 +1000359 break; /* End of options. */
Damien Miller33804262001-02-04 23:20:18 +1100360 if (*opts != ',')
Damien Millerf6d9e222000-06-18 14:50:44 +1000361 goto bad_option;
Damien Miller33804262001-02-04 23:20:18 +1100362 opts++;
Damien Millerf6d9e222000-06-18 14:50:44 +1000363 /* Process the next option. */
364 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000365
Damien Millerf6d9e222000-06-18 14:50:44 +1000366 /* grant access */
367 return 1;
368
369bad_option:
Damien Miller996acd22003-04-09 20:59:48 +1000370 logit("Bad options in %.100s file, line %lu: %.50s",
Damien Miller33804262001-02-04 23:20:18 +1100371 file, linenum, opts);
Ben Lindstroma574cda2002-05-15 16:16:14 +0000372 auth_debug_add("Bad options in %.100s file, line %lu: %.50s",
Damien Miller33804262001-02-04 23:20:18 +1100373 file, linenum, opts);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000374
Damien Millerf6d9e222000-06-18 14:50:44 +1000375 /* deny access */
376 return 0;
377}
Damien Miller0a80ca12010-02-27 07:55:05 +1100378
379/*
Damien Miller4e270b02010-04-16 15:56:21 +1000380 * Set options from critical certificate options. These supersede user key
381 * options so this must be called after auth_parse_options().
Damien Miller0a80ca12010-02-27 07:55:05 +1100382 */
383int
Damien Miller4e270b02010-04-16 15:56:21 +1000384auth_cert_options(Key *k, struct passwd *pw)
Damien Miller0a80ca12010-02-27 07:55:05 +1100385{
386 u_char *name = NULL, *data_blob = NULL;
Damien Miller41396572010-03-04 21:51:11 +1100387 u_int nlen, dlen, clen;
Damien Miller0a80ca12010-02-27 07:55:05 +1100388 Buffer c, data;
389 int ret = -1;
390
391 int cert_no_port_forwarding_flag = 1;
392 int cert_no_agent_forwarding_flag = 1;
393 int cert_no_x11_forwarding_flag = 1;
394 int cert_no_pty_flag = 1;
395 int cert_no_user_rc = 1;
396 char *cert_forced_command = NULL;
397 int cert_source_address_done = 0;
398
399 buffer_init(&data);
400
401 /* Make copy to avoid altering original */
402 buffer_init(&c);
Damien Miller4e270b02010-04-16 15:56:21 +1000403 buffer_append(&c,
404 buffer_ptr(&k->cert->critical), buffer_len(&k->cert->critical));
Damien Miller0a80ca12010-02-27 07:55:05 +1100405
406 while (buffer_len(&c) > 0) {
Damien Miller41396572010-03-04 21:51:11 +1100407 if ((name = buffer_get_string_ret(&c, &nlen)) == NULL ||
408 (data_blob = buffer_get_string_ret(&c, &dlen)) == NULL) {
Damien Miller4e270b02010-04-16 15:56:21 +1000409 error("Certificate options corrupt");
Damien Miller0a80ca12010-02-27 07:55:05 +1100410 goto out;
411 }
Damien Miller41396572010-03-04 21:51:11 +1100412 buffer_append(&data, data_blob, dlen);
Damien Miller0a80ca12010-02-27 07:55:05 +1100413 debug3("found certificate constraint \"%.100s\" len %u",
Damien Miller41396572010-03-04 21:51:11 +1100414 name, dlen);
415 if (strlen(name) != nlen) {
416 error("Certificate constraint name contains \\0");
417 goto out;
418 }
Damien Miller0a80ca12010-02-27 07:55:05 +1100419 if (strcmp(name, "permit-X11-forwarding") == 0)
420 cert_no_x11_forwarding_flag = 0;
421 else if (strcmp(name, "permit-agent-forwarding") == 0)
422 cert_no_agent_forwarding_flag = 0;
423 else if (strcmp(name, "permit-port-forwarding") == 0)
424 cert_no_port_forwarding_flag = 0;
425 else if (strcmp(name, "permit-pty") == 0)
426 cert_no_pty_flag = 0;
427 else if (strcmp(name, "permit-user-rc") == 0)
428 cert_no_user_rc = 0;
429 else if (strcmp(name, "force-command") == 0) {
Damien Miller41396572010-03-04 21:51:11 +1100430 char *command = buffer_get_string_ret(&data, &clen);
Damien Miller0a80ca12010-02-27 07:55:05 +1100431
432 if (command == NULL) {
433 error("Certificate constraint \"%s\" corrupt",
434 name);
435 goto out;
436 }
Damien Miller41396572010-03-04 21:51:11 +1100437 if (strlen(command) != clen) {
Damien Miller33334b22010-03-22 05:59:02 +1100438 error("force-command constraint contains \\0");
Damien Miller41396572010-03-04 21:51:11 +1100439 goto out;
440 }
Damien Miller0a80ca12010-02-27 07:55:05 +1100441 if (cert_forced_command != NULL) {
442 error("Certificate has multiple "
Damien Miller4e270b02010-04-16 15:56:21 +1000443 "force-command options");
Damien Miller0a80ca12010-02-27 07:55:05 +1100444 xfree(command);
445 goto out;
446 }
447 cert_forced_command = command;
448 } else if (strcmp(name, "source-address") == 0) {
Damien Miller41396572010-03-04 21:51:11 +1100449 char *allowed = buffer_get_string_ret(&data, &clen);
Damien Miller0a80ca12010-02-27 07:55:05 +1100450 const char *remote_ip = get_remote_ipaddr();
451
452 if (allowed == NULL) {
453 error("Certificate constraint \"%s\" corrupt",
454 name);
455 goto out;
456 }
Damien Miller41396572010-03-04 21:51:11 +1100457 if (strlen(allowed) != clen) {
Damien Miller33334b22010-03-22 05:59:02 +1100458 error("source-address constraint contains \\0");
Damien Miller41396572010-03-04 21:51:11 +1100459 goto out;
460 }
Damien Miller0a80ca12010-02-27 07:55:05 +1100461 if (cert_source_address_done++) {
462 error("Certificate has multiple "
Damien Miller4e270b02010-04-16 15:56:21 +1000463 "source-address options");
Damien Miller0a80ca12010-02-27 07:55:05 +1100464 xfree(allowed);
465 goto out;
466 }
467 switch (addr_match_cidr_list(remote_ip, allowed)) {
468 case 1:
469 /* accepted */
470 xfree(allowed);
471 break;
472 case 0:
473 /* no match */
474 logit("Authentication tried for %.100s with "
475 "valid certificate but not from a "
476 "permitted host (ip=%.200s).",
477 pw->pw_name, remote_ip);
478 auth_debug_add("Your address '%.200s' is not "
479 "permitted to use this certificate for "
480 "login.", remote_ip);
481 xfree(allowed);
482 goto out;
483 case -1:
484 error("Certificate source-address contents "
485 "invalid");
486 xfree(allowed);
487 goto out;
488 }
489 } else {
490 error("Certificate constraint \"%s\" is not supported",
491 name);
492 goto out;
493 }
494
495 if (buffer_len(&data) != 0) {
496 error("Certificate constraint \"%s\" corrupt "
497 "(extra data)", name);
498 goto out;
499 }
500 buffer_clear(&data);
501 xfree(name);
502 xfree(data_blob);
503 name = data_blob = NULL;
504 }
505
Damien Miller4e270b02010-04-16 15:56:21 +1000506 /* successfully parsed all options */
Damien Miller0a80ca12010-02-27 07:55:05 +1100507 ret = 0;
508
509 no_port_forwarding_flag |= cert_no_port_forwarding_flag;
510 no_agent_forwarding_flag |= cert_no_agent_forwarding_flag;
511 no_x11_forwarding_flag |= cert_no_x11_forwarding_flag;
512 no_pty_flag |= cert_no_pty_flag;
513 no_user_rc |= cert_no_user_rc;
514 /* CA-specified forced command supersedes key option */
515 if (cert_forced_command != NULL) {
516 if (forced_command != NULL)
517 xfree(forced_command);
518 forced_command = cert_forced_command;
519 }
520
521 out:
522 if (name != NULL)
523 xfree(name);
524 if (data_blob != NULL)
525 xfree(data_blob);
526 buffer_free(&data);
527 buffer_free(&c);
528 return ret;
529}
530