blob: 69b314fbd11ab00cf6d1af7c428a40ce47d3efc2 [file] [log] [blame]
Damien Miller33334b22010-03-22 05:59:02 +11001/* $OpenBSD: auth-options.c,v 1.49 2010/03/16 15:46:52 stevesk 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"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000030#include "auth-options.h"
Damien Miller33804262001-02-04 23:20:18 +110031#include "servconf.h"
Ben Lindstromd71ba572001-09-12 18:03:31 +000032#include "misc.h"
Damien Millerd7834352006-08-05 12:39:39 +100033#include "key.h"
34#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/*
380 * Set options from certificate constraints. These supersede user key options
381 * so this must be called after auth_parse_options().
382 */
383int
384auth_cert_constraints(Buffer *c_orig, struct passwd *pw)
385{
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);
403 buffer_append(&c, buffer_ptr(c_orig), buffer_len(c_orig));
404
405 while (buffer_len(&c) > 0) {
Damien Miller41396572010-03-04 21:51:11 +1100406 if ((name = buffer_get_string_ret(&c, &nlen)) == NULL ||
407 (data_blob = buffer_get_string_ret(&c, &dlen)) == NULL) {
Damien Miller0a80ca12010-02-27 07:55:05 +1100408 error("Certificate constraints corrupt");
409 goto out;
410 }
Damien Miller41396572010-03-04 21:51:11 +1100411 buffer_append(&data, data_blob, dlen);
Damien Miller0a80ca12010-02-27 07:55:05 +1100412 debug3("found certificate constraint \"%.100s\" len %u",
Damien Miller41396572010-03-04 21:51:11 +1100413 name, dlen);
414 if (strlen(name) != nlen) {
415 error("Certificate constraint name contains \\0");
416 goto out;
417 }
Damien Miller0a80ca12010-02-27 07:55:05 +1100418 if (strcmp(name, "permit-X11-forwarding") == 0)
419 cert_no_x11_forwarding_flag = 0;
420 else if (strcmp(name, "permit-agent-forwarding") == 0)
421 cert_no_agent_forwarding_flag = 0;
422 else if (strcmp(name, "permit-port-forwarding") == 0)
423 cert_no_port_forwarding_flag = 0;
424 else if (strcmp(name, "permit-pty") == 0)
425 cert_no_pty_flag = 0;
426 else if (strcmp(name, "permit-user-rc") == 0)
427 cert_no_user_rc = 0;
428 else if (strcmp(name, "force-command") == 0) {
Damien Miller41396572010-03-04 21:51:11 +1100429 char *command = buffer_get_string_ret(&data, &clen);
Damien Miller0a80ca12010-02-27 07:55:05 +1100430
431 if (command == NULL) {
432 error("Certificate constraint \"%s\" corrupt",
433 name);
434 goto out;
435 }
Damien Miller41396572010-03-04 21:51:11 +1100436 if (strlen(command) != clen) {
Damien Miller33334b22010-03-22 05:59:02 +1100437 error("force-command constraint contains \\0");
Damien Miller41396572010-03-04 21:51:11 +1100438 goto out;
439 }
Damien Miller0a80ca12010-02-27 07:55:05 +1100440 if (cert_forced_command != NULL) {
441 error("Certificate has multiple "
Damien Miller689b8722010-03-05 10:42:24 +1100442 "force-command constraints");
Damien Miller0a80ca12010-02-27 07:55:05 +1100443 xfree(command);
444 goto out;
445 }
446 cert_forced_command = command;
447 } else if (strcmp(name, "source-address") == 0) {
Damien Miller41396572010-03-04 21:51:11 +1100448 char *allowed = buffer_get_string_ret(&data, &clen);
Damien Miller0a80ca12010-02-27 07:55:05 +1100449 const char *remote_ip = get_remote_ipaddr();
450
451 if (allowed == NULL) {
452 error("Certificate constraint \"%s\" corrupt",
453 name);
454 goto out;
455 }
Damien Miller41396572010-03-04 21:51:11 +1100456 if (strlen(allowed) != clen) {
Damien Miller33334b22010-03-22 05:59:02 +1100457 error("source-address constraint contains \\0");
Damien Miller41396572010-03-04 21:51:11 +1100458 goto out;
459 }
Damien Miller0a80ca12010-02-27 07:55:05 +1100460 if (cert_source_address_done++) {
461 error("Certificate has multiple "
462 "source-address constraints");
463 xfree(allowed);
464 goto out;
465 }
466 switch (addr_match_cidr_list(remote_ip, allowed)) {
467 case 1:
468 /* accepted */
469 xfree(allowed);
470 break;
471 case 0:
472 /* no match */
473 logit("Authentication tried for %.100s with "
474 "valid certificate but not from a "
475 "permitted host (ip=%.200s).",
476 pw->pw_name, remote_ip);
477 auth_debug_add("Your address '%.200s' is not "
478 "permitted to use this certificate for "
479 "login.", remote_ip);
480 xfree(allowed);
481 goto out;
482 case -1:
483 error("Certificate source-address contents "
484 "invalid");
485 xfree(allowed);
486 goto out;
487 }
488 } else {
489 error("Certificate constraint \"%s\" is not supported",
490 name);
491 goto out;
492 }
493
494 if (buffer_len(&data) != 0) {
495 error("Certificate constraint \"%s\" corrupt "
496 "(extra data)", name);
497 goto out;
498 }
499 buffer_clear(&data);
500 xfree(name);
501 xfree(data_blob);
502 name = data_blob = NULL;
503 }
504
505 /* successfully parsed all constraints */
506 ret = 0;
507
508 no_port_forwarding_flag |= cert_no_port_forwarding_flag;
509 no_agent_forwarding_flag |= cert_no_agent_forwarding_flag;
510 no_x11_forwarding_flag |= cert_no_x11_forwarding_flag;
511 no_pty_flag |= cert_no_pty_flag;
512 no_user_rc |= cert_no_user_rc;
513 /* CA-specified forced command supersedes key option */
514 if (cert_forced_command != NULL) {
515 if (forced_command != NULL)
516 xfree(forced_command);
517 forced_command = cert_forced_command;
518 }
519
520 out:
521 if (name != NULL)
522 xfree(name);
523 if (data_blob != NULL)
524 xfree(data_blob);
525 buffer_free(&data);
526 buffer_free(&c);
527 return ret;
528}
529