blob: 396bda62f67c242efb4a53901b63b5b42b93c49d [file] [log] [blame]
Damien Miller0a80ca12010-02-27 07:55:05 +11001/* $OpenBSD: auth-options.c,v 1.45 2010/02/26 20:29:54 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"
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();
Ben Lindstroma574cda2002-05-15 16:16:14 +000081 auth_debug_reset();
Damien Miller874d77b2000-10-14 16:23:11 +110082}
83
Ben Lindstrom226cfa02001-01-22 05:34:40 +000084/*
85 * return 1 if access is granted, 0 if not.
86 * side effect: sets key option flags
87 */
Damien Millerf6d9e222000-06-18 14:50:44 +100088int
Damien Miller33804262001-02-04 23:20:18 +110089auth_parse_options(struct passwd *pw, char *opts, char *file, u_long linenum)
Damien Millerf6d9e222000-06-18 14:50:44 +100090{
91 const char *cp;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +000092 int i;
Damien Miller874d77b2000-10-14 16:23:11 +110093
94 /* reset options */
95 auth_clear_options();
96
Ben Lindstrom36d7bd02001-02-10 22:27:19 +000097 if (!opts)
98 return 1;
99
Damien Miller33804262001-02-04 23:20:18 +1100100 while (*opts && *opts != ' ' && *opts != '\t') {
Damien Miller0a80ca12010-02-27 07:55:05 +1100101 cp = "cert-authority";
102 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
103 key_is_cert_authority = 1;
104 opts += strlen(cp);
105 goto next_option;
106 }
Damien Millerf6d9e222000-06-18 14:50:44 +1000107 cp = "no-port-forwarding";
Damien Miller33804262001-02-04 23:20:18 +1100108 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
Ben Lindstroma574cda2002-05-15 16:16:14 +0000109 auth_debug_add("Port forwarding disabled.");
Damien Millerf6d9e222000-06-18 14:50:44 +1000110 no_port_forwarding_flag = 1;
Damien Miller33804262001-02-04 23:20:18 +1100111 opts += strlen(cp);
Damien Millerf6d9e222000-06-18 14:50:44 +1000112 goto next_option;
113 }
114 cp = "no-agent-forwarding";
Damien Miller33804262001-02-04 23:20:18 +1100115 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
Ben Lindstroma574cda2002-05-15 16:16:14 +0000116 auth_debug_add("Agent forwarding disabled.");
Damien Millerf6d9e222000-06-18 14:50:44 +1000117 no_agent_forwarding_flag = 1;
Damien Miller33804262001-02-04 23:20:18 +1100118 opts += strlen(cp);
Damien Millerf6d9e222000-06-18 14:50:44 +1000119 goto next_option;
120 }
121 cp = "no-X11-forwarding";
Damien Miller33804262001-02-04 23:20:18 +1100122 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
Ben Lindstroma574cda2002-05-15 16:16:14 +0000123 auth_debug_add("X11 forwarding disabled.");
Damien Millerf6d9e222000-06-18 14:50:44 +1000124 no_x11_forwarding_flag = 1;
Damien Miller33804262001-02-04 23:20:18 +1100125 opts += strlen(cp);
Damien Millerf6d9e222000-06-18 14:50:44 +1000126 goto next_option;
127 }
128 cp = "no-pty";
Damien Miller33804262001-02-04 23:20:18 +1100129 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
Ben Lindstroma574cda2002-05-15 16:16:14 +0000130 auth_debug_add("Pty allocation disabled.");
Damien Millerf6d9e222000-06-18 14:50:44 +1000131 no_pty_flag = 1;
Damien Miller33804262001-02-04 23:20:18 +1100132 opts += strlen(cp);
Damien Millerf6d9e222000-06-18 14:50:44 +1000133 goto next_option;
134 }
Damien Miller95e80952008-03-27 11:03:05 +1100135 cp = "no-user-rc";
136 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
137 auth_debug_add("User rc file execution disabled.");
138 no_user_rc = 1;
139 opts += strlen(cp);
140 goto next_option;
141 }
Damien Millerf6d9e222000-06-18 14:50:44 +1000142 cp = "command=\"";
Damien Miller33804262001-02-04 23:20:18 +1100143 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
Damien Miller33804262001-02-04 23:20:18 +1100144 opts += strlen(cp);
145 forced_command = xmalloc(strlen(opts) + 1);
Damien Millerf6d9e222000-06-18 14:50:44 +1000146 i = 0;
Damien Miller33804262001-02-04 23:20:18 +1100147 while (*opts) {
148 if (*opts == '"')
Damien Millerf6d9e222000-06-18 14:50:44 +1000149 break;
Damien Miller33804262001-02-04 23:20:18 +1100150 if (*opts == '\\' && opts[1] == '"') {
151 opts += 2;
Damien Millerf6d9e222000-06-18 14:50:44 +1000152 forced_command[i++] = '"';
153 continue;
154 }
Damien Miller33804262001-02-04 23:20:18 +1100155 forced_command[i++] = *opts++;
Damien Millerf6d9e222000-06-18 14:50:44 +1000156 }
Damien Miller33804262001-02-04 23:20:18 +1100157 if (!*opts) {
Damien Millerf6d9e222000-06-18 14:50:44 +1000158 debug("%.100s, line %lu: missing end quote",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000159 file, linenum);
Ben Lindstroma574cda2002-05-15 16:16:14 +0000160 auth_debug_add("%.100s, line %lu: missing end quote",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000161 file, linenum);
Damien Miller056ddf72001-03-14 10:15:20 +1100162 xfree(forced_command);
163 forced_command = NULL;
164 goto bad_option;
Damien Millerf6d9e222000-06-18 14:50:44 +1000165 }
Damien Miller98299262006-07-24 14:01:43 +1000166 forced_command[i] = '\0';
Ben Lindstroma574cda2002-05-15 16:16:14 +0000167 auth_debug_add("Forced command: %.900s", forced_command);
Damien Miller33804262001-02-04 23:20:18 +1100168 opts++;
Damien Millerf6d9e222000-06-18 14:50:44 +1000169 goto next_option;
170 }
171 cp = "environment=\"";
Ben Lindstrom5d860f02002-08-01 01:28:38 +0000172 if (options.permit_user_env &&
173 strncasecmp(opts, cp, strlen(cp)) == 0) {
Damien Millerf6d9e222000-06-18 14:50:44 +1000174 char *s;
175 struct envstring *new_envstring;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +0000176
Damien Miller33804262001-02-04 23:20:18 +1100177 opts += strlen(cp);
178 s = xmalloc(strlen(opts) + 1);
Damien Millerf6d9e222000-06-18 14:50:44 +1000179 i = 0;
Damien Miller33804262001-02-04 23:20:18 +1100180 while (*opts) {
181 if (*opts == '"')
Damien Millerf6d9e222000-06-18 14:50:44 +1000182 break;
Damien Miller33804262001-02-04 23:20:18 +1100183 if (*opts == '\\' && opts[1] == '"') {
184 opts += 2;
Damien Millerf6d9e222000-06-18 14:50:44 +1000185 s[i++] = '"';
186 continue;
187 }
Damien Miller33804262001-02-04 23:20:18 +1100188 s[i++] = *opts++;
Damien Millerf6d9e222000-06-18 14:50:44 +1000189 }
Damien Miller33804262001-02-04 23:20:18 +1100190 if (!*opts) {
Damien Millerf6d9e222000-06-18 14:50:44 +1000191 debug("%.100s, line %lu: missing end quote",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000192 file, linenum);
Ben Lindstroma574cda2002-05-15 16:16:14 +0000193 auth_debug_add("%.100s, line %lu: missing end quote",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000194 file, linenum);
Damien Miller056ddf72001-03-14 10:15:20 +1100195 xfree(s);
196 goto bad_option;
Damien Millerf6d9e222000-06-18 14:50:44 +1000197 }
Damien Miller98299262006-07-24 14:01:43 +1000198 s[i] = '\0';
Ben Lindstroma574cda2002-05-15 16:16:14 +0000199 auth_debug_add("Adding to environment: %.900s", s);
Damien Millerf6d9e222000-06-18 14:50:44 +1000200 debug("Adding to environment: %.900s", s);
Damien Miller33804262001-02-04 23:20:18 +1100201 opts++;
Damien Millerf6d9e222000-06-18 14:50:44 +1000202 new_envstring = xmalloc(sizeof(struct envstring));
203 new_envstring->s = s;
204 new_envstring->next = custom_environment;
205 custom_environment = new_envstring;
206 goto next_option;
207 }
208 cp = "from=\"";
Damien Miller33804262001-02-04 23:20:18 +1100209 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
Damien Miller33804262001-02-04 23:20:18 +1100210 const char *remote_ip = get_remote_ipaddr();
211 const char *remote_host = get_canonical_hostname(
Damien Miller3a961dc2003-06-03 10:25:48 +1000212 options.use_dns);
Damien Miller33804262001-02-04 23:20:18 +1100213 char *patterns = xmalloc(strlen(opts) + 1);
Ben Lindstrom7bb8b492001-03-17 00:47:54 +0000214
Damien Miller33804262001-02-04 23:20:18 +1100215 opts += strlen(cp);
Damien Millerf6d9e222000-06-18 14:50:44 +1000216 i = 0;
Damien Miller33804262001-02-04 23:20:18 +1100217 while (*opts) {
218 if (*opts == '"')
Damien Millerf6d9e222000-06-18 14:50:44 +1000219 break;
Damien Miller33804262001-02-04 23:20:18 +1100220 if (*opts == '\\' && opts[1] == '"') {
221 opts += 2;
Damien Millerf6d9e222000-06-18 14:50:44 +1000222 patterns[i++] = '"';
223 continue;
224 }
Damien Miller33804262001-02-04 23:20:18 +1100225 patterns[i++] = *opts++;
Damien Millerf6d9e222000-06-18 14:50:44 +1000226 }
Damien Miller33804262001-02-04 23:20:18 +1100227 if (!*opts) {
Damien Millerf6d9e222000-06-18 14:50:44 +1000228 debug("%.100s, line %lu: missing end quote",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000229 file, linenum);
Ben Lindstroma574cda2002-05-15 16:16:14 +0000230 auth_debug_add("%.100s, line %lu: missing end quote",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000231 file, linenum);
Damien Miller056ddf72001-03-14 10:15:20 +1100232 xfree(patterns);
233 goto bad_option;
Damien Millerf6d9e222000-06-18 14:50:44 +1000234 }
Damien Miller98299262006-07-24 14:01:43 +1000235 patterns[i] = '\0';
Damien Miller33804262001-02-04 23:20:18 +1100236 opts++;
Darren Tucker896ad5a2008-06-11 09:34:46 +1000237 switch (match_host_and_ip(remote_host, remote_ip,
238 patterns)) {
239 case 1:
240 xfree(patterns);
241 /* Host name matches. */
242 goto next_option;
243 case -1:
244 debug("%.100s, line %lu: invalid criteria",
245 file, linenum);
246 auth_debug_add("%.100s, line %lu: "
247 "invalid criteria", file, linenum);
248 /* FALLTHROUGH */
249 case 0:
Ben Lindstromf0c50292001-06-25 05:17:53 +0000250 xfree(patterns);
Damien Miller996acd22003-04-09 20:59:48 +1000251 logit("Authentication tried for %.100s with "
Damien Miller33804262001-02-04 23:20:18 +1100252 "correct key but not from a permitted "
253 "host (host=%.200s, ip=%.200s).",
254 pw->pw_name, remote_host, remote_ip);
Ben Lindstroma574cda2002-05-15 16:16:14 +0000255 auth_debug_add("Your host '%.200s' is not "
Damien Miller33804262001-02-04 23:20:18 +1100256 "permitted to use this key for login.",
257 remote_host);
Darren Tucker896ad5a2008-06-11 09:34:46 +1000258 break;
Damien Millerf6d9e222000-06-18 14:50:44 +1000259 }
Darren Tucker896ad5a2008-06-11 09:34:46 +1000260 /* deny access */
261 return 0;
Damien Millerf6d9e222000-06-18 14:50:44 +1000262 }
Ben Lindstrom7bb8b492001-03-17 00:47:54 +0000263 cp = "permitopen=\"";
264 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
Damien Millerf91ee4c2005-03-01 21:24:33 +1100265 char *host, *p;
Damien Millere37dde02009-01-28 16:33:01 +1100266 int port;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +0000267 char *patterns = xmalloc(strlen(opts) + 1);
268
269 opts += strlen(cp);
270 i = 0;
271 while (*opts) {
272 if (*opts == '"')
273 break;
274 if (*opts == '\\' && opts[1] == '"') {
275 opts += 2;
276 patterns[i++] = '"';
277 continue;
278 }
279 patterns[i++] = *opts++;
280 }
281 if (!*opts) {
282 debug("%.100s, line %lu: missing end quote",
283 file, linenum);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100284 auth_debug_add("%.100s, line %lu: missing "
285 "end quote", file, linenum);
Ben Lindstrom7bb8b492001-03-17 00:47:54 +0000286 xfree(patterns);
287 goto bad_option;
288 }
Damien Miller98299262006-07-24 14:01:43 +1000289 patterns[i] = '\0';
Ben Lindstrom7bb8b492001-03-17 00:47:54 +0000290 opts++;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100291 p = patterns;
292 host = hpdelim(&p);
293 if (host == NULL || strlen(host) >= NI_MAXHOST) {
294 debug("%.100s, line %lu: Bad permitopen "
Darren Tucker90b9e022005-03-14 23:08:50 +1100295 "specification <%.100s>", file, linenum,
Damien Millerf91ee4c2005-03-01 21:24:33 +1100296 patterns);
Ben Lindstroma574cda2002-05-15 16:16:14 +0000297 auth_debug_add("%.100s, line %lu: "
Damien Millerf91ee4c2005-03-01 21:24:33 +1100298 "Bad permitopen specification", file,
299 linenum);
Ben Lindstrom7bb8b492001-03-17 00:47:54 +0000300 xfree(patterns);
301 goto bad_option;
302 }
Darren Tucker47eede72005-03-14 23:08:12 +1100303 host = cleanhostname(host);
Damien Millere37dde02009-01-28 16:33:01 +1100304 if (p == NULL || (port = a2port(p)) <= 0) {
Damien Millerf91ee4c2005-03-01 21:24:33 +1100305 debug("%.100s, line %lu: Bad permitopen port "
306 "<%.100s>", file, linenum, p ? p : "");
Ben Lindstroma574cda2002-05-15 16:16:14 +0000307 auth_debug_add("%.100s, line %lu: "
Ben Lindstromd71ba572001-09-12 18:03:31 +0000308 "Bad permitopen port", file, linenum);
Ben Lindstrom7bb8b492001-03-17 00:47:54 +0000309 xfree(patterns);
310 goto bad_option;
311 }
Ben Lindstrom2d70f982001-03-19 00:13:46 +0000312 if (options.allow_tcp_forwarding)
Ben Lindstromd71ba572001-09-12 18:03:31 +0000313 channel_add_permitted_opens(host, port);
Ben Lindstrom7bb8b492001-03-17 00:47:54 +0000314 xfree(patterns);
315 goto next_option;
316 }
Damien Millerd27b9472005-12-13 19:29:02 +1100317 cp = "tunnel=\"";
318 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
319 char *tun = NULL;
320 opts += strlen(cp);
321 tun = xmalloc(strlen(opts) + 1);
322 i = 0;
323 while (*opts) {
324 if (*opts == '"')
325 break;
326 tun[i++] = *opts++;
327 }
328 if (!*opts) {
329 debug("%.100s, line %lu: missing end quote",
330 file, linenum);
331 auth_debug_add("%.100s, line %lu: missing end quote",
332 file, linenum);
333 xfree(tun);
334 forced_tun_device = -1;
335 goto bad_option;
336 }
Damien Miller98299262006-07-24 14:01:43 +1000337 tun[i] = '\0';
Damien Millerd27b9472005-12-13 19:29:02 +1100338 forced_tun_device = a2tun(tun, NULL);
339 xfree(tun);
Damien Miller7b58e802005-12-13 19:33:19 +1100340 if (forced_tun_device == SSH_TUNID_ERR) {
Damien Millerd27b9472005-12-13 19:29:02 +1100341 debug("%.100s, line %lu: invalid tun device",
342 file, linenum);
343 auth_debug_add("%.100s, line %lu: invalid tun device",
344 file, linenum);
345 forced_tun_device = -1;
346 goto bad_option;
347 }
348 auth_debug_add("Forced tun device: %d", forced_tun_device);
349 opts++;
350 goto next_option;
351 }
Damien Millerf6d9e222000-06-18 14:50:44 +1000352next_option:
353 /*
354 * Skip the comma, and move to the next option
355 * (or break out if there are no more).
356 */
Damien Miller33804262001-02-04 23:20:18 +1100357 if (!*opts)
Damien Millerf6d9e222000-06-18 14:50:44 +1000358 fatal("Bugs in auth-options.c option processing.");
Damien Miller33804262001-02-04 23:20:18 +1100359 if (*opts == ' ' || *opts == '\t')
Damien Millerf6d9e222000-06-18 14:50:44 +1000360 break; /* End of options. */
Damien Miller33804262001-02-04 23:20:18 +1100361 if (*opts != ',')
Damien Millerf6d9e222000-06-18 14:50:44 +1000362 goto bad_option;
Damien Miller33804262001-02-04 23:20:18 +1100363 opts++;
Damien Millerf6d9e222000-06-18 14:50:44 +1000364 /* Process the next option. */
365 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000366
367 if (!use_privsep)
Ben Lindstroma574cda2002-05-15 16:16:14 +0000368 auth_debug_send();
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000369
Damien Millerf6d9e222000-06-18 14:50:44 +1000370 /* grant access */
371 return 1;
372
373bad_option:
Damien Miller996acd22003-04-09 20:59:48 +1000374 logit("Bad options in %.100s file, line %lu: %.50s",
Damien Miller33804262001-02-04 23:20:18 +1100375 file, linenum, opts);
Ben Lindstroma574cda2002-05-15 16:16:14 +0000376 auth_debug_add("Bad options in %.100s file, line %lu: %.50s",
Damien Miller33804262001-02-04 23:20:18 +1100377 file, linenum, opts);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000378
379 if (!use_privsep)
Ben Lindstroma574cda2002-05-15 16:16:14 +0000380 auth_debug_send();
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000381
Damien Millerf6d9e222000-06-18 14:50:44 +1000382 /* deny access */
383 return 0;
384}
Damien Miller0a80ca12010-02-27 07:55:05 +1100385
386/*
387 * Set options from certificate constraints. These supersede user key options
388 * so this must be called after auth_parse_options().
389 */
390int
391auth_cert_constraints(Buffer *c_orig, struct passwd *pw)
392{
393 u_char *name = NULL, *data_blob = NULL;
394 u_int len;
395 Buffer c, data;
396 int ret = -1;
397
398 int cert_no_port_forwarding_flag = 1;
399 int cert_no_agent_forwarding_flag = 1;
400 int cert_no_x11_forwarding_flag = 1;
401 int cert_no_pty_flag = 1;
402 int cert_no_user_rc = 1;
403 char *cert_forced_command = NULL;
404 int cert_source_address_done = 0;
405
406 buffer_init(&data);
407
408 /* Make copy to avoid altering original */
409 buffer_init(&c);
410 buffer_append(&c, buffer_ptr(c_orig), buffer_len(c_orig));
411
412 while (buffer_len(&c) > 0) {
413 if ((name = buffer_get_string_ret(&c, NULL)) == NULL ||
414 (data_blob = buffer_get_string_ret(&c, &len)) == NULL) {
415 error("Certificate constraints corrupt");
416 goto out;
417 }
418 buffer_append(&data, data_blob, len);
419 debug3("found certificate constraint \"%.100s\" len %u",
420 name, len);
421 if (strcmp(name, "permit-X11-forwarding") == 0)
422 cert_no_x11_forwarding_flag = 0;
423 else if (strcmp(name, "permit-agent-forwarding") == 0)
424 cert_no_agent_forwarding_flag = 0;
425 else if (strcmp(name, "permit-port-forwarding") == 0)
426 cert_no_port_forwarding_flag = 0;
427 else if (strcmp(name, "permit-pty") == 0)
428 cert_no_pty_flag = 0;
429 else if (strcmp(name, "permit-user-rc") == 0)
430 cert_no_user_rc = 0;
431 else if (strcmp(name, "force-command") == 0) {
432 char *command = buffer_get_string_ret(&data, NULL);
433
434 if (command == NULL) {
435 error("Certificate constraint \"%s\" corrupt",
436 name);
437 goto out;
438 }
439 if (cert_forced_command != NULL) {
440 error("Certificate has multiple "
441 "forced-command constraints");
442 xfree(command);
443 goto out;
444 }
445 cert_forced_command = command;
446 } else if (strcmp(name, "source-address") == 0) {
447 char *allowed = buffer_get_string_ret(&data, NULL);
448 const char *remote_ip = get_remote_ipaddr();
449
450 if (allowed == NULL) {
451 error("Certificate constraint \"%s\" corrupt",
452 name);
453 goto out;
454 }
455 if (cert_source_address_done++) {
456 error("Certificate has multiple "
457 "source-address constraints");
458 xfree(allowed);
459 goto out;
460 }
461 switch (addr_match_cidr_list(remote_ip, allowed)) {
462 case 1:
463 /* accepted */
464 xfree(allowed);
465 break;
466 case 0:
467 /* no match */
468 logit("Authentication tried for %.100s with "
469 "valid certificate but not from a "
470 "permitted host (ip=%.200s).",
471 pw->pw_name, remote_ip);
472 auth_debug_add("Your address '%.200s' is not "
473 "permitted to use this certificate for "
474 "login.", remote_ip);
475 xfree(allowed);
476 goto out;
477 case -1:
478 error("Certificate source-address contents "
479 "invalid");
480 xfree(allowed);
481 goto out;
482 }
483 } else {
484 error("Certificate constraint \"%s\" is not supported",
485 name);
486 goto out;
487 }
488
489 if (buffer_len(&data) != 0) {
490 error("Certificate constraint \"%s\" corrupt "
491 "(extra data)", name);
492 goto out;
493 }
494 buffer_clear(&data);
495 xfree(name);
496 xfree(data_blob);
497 name = data_blob = NULL;
498 }
499
500 /* successfully parsed all constraints */
501 ret = 0;
502
503 no_port_forwarding_flag |= cert_no_port_forwarding_flag;
504 no_agent_forwarding_flag |= cert_no_agent_forwarding_flag;
505 no_x11_forwarding_flag |= cert_no_x11_forwarding_flag;
506 no_pty_flag |= cert_no_pty_flag;
507 no_user_rc |= cert_no_user_rc;
508 /* CA-specified forced command supersedes key option */
509 if (cert_forced_command != NULL) {
510 if (forced_command != NULL)
511 xfree(forced_command);
512 forced_command = cert_forced_command;
513 }
514
515 out:
516 if (name != NULL)
517 xfree(name);
518 if (data_blob != NULL)
519 xfree(data_blob);
520 buffer_free(&data);
521 buffer_free(&c);
522 return ret;
523}
524