auth-options.c revision 1.3 1 1.3 adam /* $NetBSD: auth-options.c,v 1.3 2010/11/21 18:29:48 adam Exp $ */
2 1.3 adam /* $OpenBSD: auth-options.c,v 1.52 2010/05/20 23:46:02 djm Exp $ */
3 1.1 christos /*
4 1.1 christos * Author: Tatu Ylonen <ylo (at) cs.hut.fi>
5 1.1 christos * Copyright (c) 1995 Tatu Ylonen <ylo (at) cs.hut.fi>, Espoo, Finland
6 1.1 christos * All rights reserved
7 1.1 christos * As far as I am concerned, the code I have written for this software
8 1.1 christos * can be used freely for any purpose. Any derived versions of this
9 1.1 christos * software must be clearly marked as such, and if the derived work is
10 1.1 christos * incompatible with the protocol description in the RFC file, it must be
11 1.1 christos * called by a name other than "ssh" or "Secure Shell".
12 1.1 christos */
13 1.1 christos
14 1.2 christos #include "includes.h"
15 1.3 adam __RCSID("$NetBSD: auth-options.c,v 1.3 2010/11/21 18:29:48 adam Exp $");
16 1.1 christos #include <sys/types.h>
17 1.1 christos #include <sys/queue.h>
18 1.1 christos
19 1.1 christos #include <netdb.h>
20 1.1 christos #include <pwd.h>
21 1.1 christos #include <string.h>
22 1.1 christos #include <stdio.h>
23 1.1 christos #include <stdarg.h>
24 1.2 christos #include <time.h>
25 1.1 christos
26 1.1 christos #include "xmalloc.h"
27 1.1 christos #include "match.h"
28 1.1 christos #include "log.h"
29 1.1 christos #include "canohost.h"
30 1.1 christos #include "buffer.h"
31 1.1 christos #include "channels.h"
32 1.1 christos #include "servconf.h"
33 1.1 christos #include "misc.h"
34 1.1 christos #include "key.h"
35 1.3 adam #include "auth-options.h"
36 1.1 christos #include "hostfile.h"
37 1.1 christos #include "auth.h"
38 1.1 christos #ifdef GSSAPI
39 1.1 christos #include "ssh-gss.h"
40 1.1 christos #endif
41 1.1 christos #include "monitor_wrap.h"
42 1.1 christos
43 1.1 christos /* Flags set authorized_keys flags */
44 1.1 christos int no_port_forwarding_flag = 0;
45 1.1 christos int no_agent_forwarding_flag = 0;
46 1.1 christos int no_x11_forwarding_flag = 0;
47 1.1 christos int no_pty_flag = 0;
48 1.1 christos int no_user_rc = 0;
49 1.3 adam int key_is_cert_authority = 0;
50 1.1 christos
51 1.1 christos /* "command=" option. */
52 1.1 christos char *forced_command = NULL;
53 1.1 christos
54 1.1 christos /* "environment=" options. */
55 1.1 christos struct envstring *custom_environment = NULL;
56 1.1 christos
57 1.1 christos /* "tunnel=" option. */
58 1.1 christos int forced_tun_device = -1;
59 1.1 christos
60 1.3 adam /* "principals=" option. */
61 1.3 adam char *authorized_principals = NULL;
62 1.3 adam
63 1.1 christos extern ServerOptions options;
64 1.1 christos
65 1.1 christos void
66 1.1 christos auth_clear_options(void)
67 1.1 christos {
68 1.1 christos no_agent_forwarding_flag = 0;
69 1.1 christos no_port_forwarding_flag = 0;
70 1.1 christos no_pty_flag = 0;
71 1.1 christos no_x11_forwarding_flag = 0;
72 1.1 christos no_user_rc = 0;
73 1.3 adam key_is_cert_authority = 0;
74 1.1 christos while (custom_environment) {
75 1.1 christos struct envstring *ce = custom_environment;
76 1.1 christos custom_environment = ce->next;
77 1.1 christos xfree(ce->s);
78 1.1 christos xfree(ce);
79 1.1 christos }
80 1.1 christos if (forced_command) {
81 1.1 christos xfree(forced_command);
82 1.1 christos forced_command = NULL;
83 1.1 christos }
84 1.3 adam if (authorized_principals) {
85 1.3 adam xfree(authorized_principals);
86 1.3 adam authorized_principals = NULL;
87 1.3 adam }
88 1.1 christos forced_tun_device = -1;
89 1.1 christos channel_clear_permitted_opens();
90 1.1 christos }
91 1.1 christos
92 1.1 christos /*
93 1.1 christos * return 1 if access is granted, 0 if not.
94 1.1 christos * side effect: sets key option flags
95 1.1 christos */
96 1.1 christos int
97 1.1 christos auth_parse_options(struct passwd *pw, char *opts, char *file, u_long linenum)
98 1.1 christos {
99 1.1 christos const char *cp;
100 1.1 christos int i;
101 1.1 christos
102 1.1 christos /* reset options */
103 1.1 christos auth_clear_options();
104 1.1 christos
105 1.1 christos if (!opts)
106 1.1 christos return 1;
107 1.1 christos
108 1.1 christos while (*opts && *opts != ' ' && *opts != '\t') {
109 1.3 adam cp = "cert-authority";
110 1.3 adam if (strncasecmp(opts, cp, strlen(cp)) == 0) {
111 1.3 adam key_is_cert_authority = 1;
112 1.3 adam opts += strlen(cp);
113 1.3 adam goto next_option;
114 1.3 adam }
115 1.1 christos cp = "no-port-forwarding";
116 1.1 christos if (strncasecmp(opts, cp, strlen(cp)) == 0) {
117 1.1 christos auth_debug_add("Port forwarding disabled.");
118 1.1 christos no_port_forwarding_flag = 1;
119 1.1 christos opts += strlen(cp);
120 1.1 christos goto next_option;
121 1.1 christos }
122 1.1 christos cp = "no-agent-forwarding";
123 1.1 christos if (strncasecmp(opts, cp, strlen(cp)) == 0) {
124 1.1 christos auth_debug_add("Agent forwarding disabled.");
125 1.1 christos no_agent_forwarding_flag = 1;
126 1.1 christos opts += strlen(cp);
127 1.1 christos goto next_option;
128 1.1 christos }
129 1.1 christos cp = "no-X11-forwarding";
130 1.1 christos if (strncasecmp(opts, cp, strlen(cp)) == 0) {
131 1.1 christos auth_debug_add("X11 forwarding disabled.");
132 1.1 christos no_x11_forwarding_flag = 1;
133 1.1 christos opts += strlen(cp);
134 1.1 christos goto next_option;
135 1.1 christos }
136 1.1 christos cp = "no-pty";
137 1.1 christos if (strncasecmp(opts, cp, strlen(cp)) == 0) {
138 1.1 christos auth_debug_add("Pty allocation disabled.");
139 1.1 christos no_pty_flag = 1;
140 1.1 christos opts += strlen(cp);
141 1.1 christos goto next_option;
142 1.1 christos }
143 1.1 christos cp = "no-user-rc";
144 1.1 christos if (strncasecmp(opts, cp, strlen(cp)) == 0) {
145 1.1 christos auth_debug_add("User rc file execution disabled.");
146 1.1 christos no_user_rc = 1;
147 1.1 christos opts += strlen(cp);
148 1.1 christos goto next_option;
149 1.1 christos }
150 1.1 christos cp = "command=\"";
151 1.1 christos if (strncasecmp(opts, cp, strlen(cp)) == 0) {
152 1.1 christos opts += strlen(cp);
153 1.3 adam if (forced_command != NULL)
154 1.3 adam xfree(forced_command);
155 1.1 christos forced_command = xmalloc(strlen(opts) + 1);
156 1.1 christos i = 0;
157 1.1 christos while (*opts) {
158 1.1 christos if (*opts == '"')
159 1.1 christos break;
160 1.1 christos if (*opts == '\\' && opts[1] == '"') {
161 1.1 christos opts += 2;
162 1.1 christos forced_command[i++] = '"';
163 1.1 christos continue;
164 1.1 christos }
165 1.1 christos forced_command[i++] = *opts++;
166 1.1 christos }
167 1.1 christos if (!*opts) {
168 1.1 christos debug("%.100s, line %lu: missing end quote",
169 1.1 christos file, linenum);
170 1.1 christos auth_debug_add("%.100s, line %lu: missing end quote",
171 1.1 christos file, linenum);
172 1.1 christos xfree(forced_command);
173 1.1 christos forced_command = NULL;
174 1.1 christos goto bad_option;
175 1.1 christos }
176 1.1 christos forced_command[i] = '\0';
177 1.1 christos auth_debug_add("Forced command: %.900s", forced_command);
178 1.1 christos opts++;
179 1.1 christos goto next_option;
180 1.1 christos }
181 1.3 adam cp = "principals=\"";
182 1.3 adam if (strncasecmp(opts, cp, strlen(cp)) == 0) {
183 1.3 adam opts += strlen(cp);
184 1.3 adam if (authorized_principals != NULL)
185 1.3 adam xfree(authorized_principals);
186 1.3 adam authorized_principals = xmalloc(strlen(opts) + 1);
187 1.3 adam i = 0;
188 1.3 adam while (*opts) {
189 1.3 adam if (*opts == '"')
190 1.3 adam break;
191 1.3 adam if (*opts == '\\' && opts[1] == '"') {
192 1.3 adam opts += 2;
193 1.3 adam authorized_principals[i++] = '"';
194 1.3 adam continue;
195 1.3 adam }
196 1.3 adam authorized_principals[i++] = *opts++;
197 1.3 adam }
198 1.3 adam if (!*opts) {
199 1.3 adam debug("%.100s, line %lu: missing end quote",
200 1.3 adam file, linenum);
201 1.3 adam auth_debug_add("%.100s, line %lu: missing end quote",
202 1.3 adam file, linenum);
203 1.3 adam xfree(authorized_principals);
204 1.3 adam authorized_principals = NULL;
205 1.3 adam goto bad_option;
206 1.3 adam }
207 1.3 adam authorized_principals[i] = '\0';
208 1.3 adam auth_debug_add("principals: %.900s",
209 1.3 adam authorized_principals);
210 1.3 adam opts++;
211 1.3 adam goto next_option;
212 1.3 adam }
213 1.1 christos cp = "environment=\"";
214 1.1 christos if (options.permit_user_env &&
215 1.1 christos strncasecmp(opts, cp, strlen(cp)) == 0) {
216 1.1 christos char *s;
217 1.1 christos struct envstring *new_envstring;
218 1.1 christos
219 1.1 christos opts += strlen(cp);
220 1.1 christos s = xmalloc(strlen(opts) + 1);
221 1.1 christos i = 0;
222 1.1 christos while (*opts) {
223 1.1 christos if (*opts == '"')
224 1.1 christos break;
225 1.1 christos if (*opts == '\\' && opts[1] == '"') {
226 1.1 christos opts += 2;
227 1.1 christos s[i++] = '"';
228 1.1 christos continue;
229 1.1 christos }
230 1.1 christos s[i++] = *opts++;
231 1.1 christos }
232 1.1 christos if (!*opts) {
233 1.1 christos debug("%.100s, line %lu: missing end quote",
234 1.1 christos file, linenum);
235 1.1 christos auth_debug_add("%.100s, line %lu: missing end quote",
236 1.1 christos file, linenum);
237 1.1 christos xfree(s);
238 1.1 christos goto bad_option;
239 1.1 christos }
240 1.1 christos s[i] = '\0';
241 1.1 christos auth_debug_add("Adding to environment: %.900s", s);
242 1.1 christos debug("Adding to environment: %.900s", s);
243 1.1 christos opts++;
244 1.1 christos new_envstring = xmalloc(sizeof(struct envstring));
245 1.1 christos new_envstring->s = s;
246 1.1 christos new_envstring->next = custom_environment;
247 1.1 christos custom_environment = new_envstring;
248 1.1 christos goto next_option;
249 1.1 christos }
250 1.1 christos cp = "from=\"";
251 1.1 christos if (strncasecmp(opts, cp, strlen(cp)) == 0) {
252 1.1 christos const char *remote_ip = get_remote_ipaddr();
253 1.1 christos const char *remote_host = get_canonical_hostname(
254 1.1 christos options.use_dns);
255 1.1 christos char *patterns = xmalloc(strlen(opts) + 1);
256 1.1 christos
257 1.1 christos opts += strlen(cp);
258 1.1 christos i = 0;
259 1.1 christos while (*opts) {
260 1.1 christos if (*opts == '"')
261 1.1 christos break;
262 1.1 christos if (*opts == '\\' && opts[1] == '"') {
263 1.1 christos opts += 2;
264 1.1 christos patterns[i++] = '"';
265 1.1 christos continue;
266 1.1 christos }
267 1.1 christos patterns[i++] = *opts++;
268 1.1 christos }
269 1.1 christos if (!*opts) {
270 1.1 christos debug("%.100s, line %lu: missing end quote",
271 1.1 christos file, linenum);
272 1.1 christos auth_debug_add("%.100s, line %lu: missing end quote",
273 1.1 christos file, linenum);
274 1.1 christos xfree(patterns);
275 1.1 christos goto bad_option;
276 1.1 christos }
277 1.1 christos patterns[i] = '\0';
278 1.1 christos opts++;
279 1.1 christos switch (match_host_and_ip(remote_host, remote_ip,
280 1.1 christos patterns)) {
281 1.1 christos case 1:
282 1.1 christos xfree(patterns);
283 1.1 christos /* Host name matches. */
284 1.1 christos goto next_option;
285 1.1 christos case -1:
286 1.1 christos debug("%.100s, line %lu: invalid criteria",
287 1.1 christos file, linenum);
288 1.1 christos auth_debug_add("%.100s, line %lu: "
289 1.1 christos "invalid criteria", file, linenum);
290 1.1 christos /* FALLTHROUGH */
291 1.1 christos case 0:
292 1.1 christos xfree(patterns);
293 1.1 christos logit("Authentication tried for %.100s with "
294 1.1 christos "correct key but not from a permitted "
295 1.1 christos "host (host=%.200s, ip=%.200s).",
296 1.1 christos pw->pw_name, remote_host, remote_ip);
297 1.1 christos auth_debug_add("Your host '%.200s' is not "
298 1.1 christos "permitted to use this key for login.",
299 1.1 christos remote_host);
300 1.1 christos break;
301 1.1 christos }
302 1.1 christos /* deny access */
303 1.1 christos return 0;
304 1.1 christos }
305 1.1 christos cp = "permitopen=\"";
306 1.1 christos if (strncasecmp(opts, cp, strlen(cp)) == 0) {
307 1.1 christos char *host, *p;
308 1.1 christos int port;
309 1.1 christos char *patterns = xmalloc(strlen(opts) + 1);
310 1.1 christos
311 1.1 christos opts += strlen(cp);
312 1.1 christos i = 0;
313 1.1 christos while (*opts) {
314 1.1 christos if (*opts == '"')
315 1.1 christos break;
316 1.1 christos if (*opts == '\\' && opts[1] == '"') {
317 1.1 christos opts += 2;
318 1.1 christos patterns[i++] = '"';
319 1.1 christos continue;
320 1.1 christos }
321 1.1 christos patterns[i++] = *opts++;
322 1.1 christos }
323 1.1 christos if (!*opts) {
324 1.1 christos debug("%.100s, line %lu: missing end quote",
325 1.1 christos file, linenum);
326 1.1 christos auth_debug_add("%.100s, line %lu: missing "
327 1.1 christos "end quote", file, linenum);
328 1.1 christos xfree(patterns);
329 1.1 christos goto bad_option;
330 1.1 christos }
331 1.1 christos patterns[i] = '\0';
332 1.1 christos opts++;
333 1.1 christos p = patterns;
334 1.1 christos host = hpdelim(&p);
335 1.1 christos if (host == NULL || strlen(host) >= NI_MAXHOST) {
336 1.1 christos debug("%.100s, line %lu: Bad permitopen "
337 1.1 christos "specification <%.100s>", file, linenum,
338 1.1 christos patterns);
339 1.1 christos auth_debug_add("%.100s, line %lu: "
340 1.1 christos "Bad permitopen specification", file,
341 1.1 christos linenum);
342 1.1 christos xfree(patterns);
343 1.1 christos goto bad_option;
344 1.1 christos }
345 1.1 christos host = cleanhostname(host);
346 1.1 christos if (p == NULL || (port = a2port(p)) <= 0) {
347 1.1 christos debug("%.100s, line %lu: Bad permitopen port "
348 1.1 christos "<%.100s>", file, linenum, p ? p : "");
349 1.1 christos auth_debug_add("%.100s, line %lu: "
350 1.1 christos "Bad permitopen port", file, linenum);
351 1.1 christos xfree(patterns);
352 1.1 christos goto bad_option;
353 1.1 christos }
354 1.1 christos if (options.allow_tcp_forwarding)
355 1.1 christos channel_add_permitted_opens(host, port);
356 1.1 christos xfree(patterns);
357 1.1 christos goto next_option;
358 1.1 christos }
359 1.1 christos cp = "tunnel=\"";
360 1.1 christos if (strncasecmp(opts, cp, strlen(cp)) == 0) {
361 1.1 christos char *tun = NULL;
362 1.1 christos opts += strlen(cp);
363 1.1 christos tun = xmalloc(strlen(opts) + 1);
364 1.1 christos i = 0;
365 1.1 christos while (*opts) {
366 1.1 christos if (*opts == '"')
367 1.1 christos break;
368 1.1 christos tun[i++] = *opts++;
369 1.1 christos }
370 1.1 christos if (!*opts) {
371 1.1 christos debug("%.100s, line %lu: missing end quote",
372 1.1 christos file, linenum);
373 1.1 christos auth_debug_add("%.100s, line %lu: missing end quote",
374 1.1 christos file, linenum);
375 1.1 christos xfree(tun);
376 1.1 christos forced_tun_device = -1;
377 1.1 christos goto bad_option;
378 1.1 christos }
379 1.1 christos tun[i] = '\0';
380 1.1 christos forced_tun_device = a2tun(tun, NULL);
381 1.1 christos xfree(tun);
382 1.1 christos if (forced_tun_device == SSH_TUNID_ERR) {
383 1.1 christos debug("%.100s, line %lu: invalid tun device",
384 1.1 christos file, linenum);
385 1.1 christos auth_debug_add("%.100s, line %lu: invalid tun device",
386 1.1 christos file, linenum);
387 1.1 christos forced_tun_device = -1;
388 1.1 christos goto bad_option;
389 1.1 christos }
390 1.1 christos auth_debug_add("Forced tun device: %d", forced_tun_device);
391 1.1 christos opts++;
392 1.1 christos goto next_option;
393 1.1 christos }
394 1.1 christos next_option:
395 1.1 christos /*
396 1.1 christos * Skip the comma, and move to the next option
397 1.1 christos * (or break out if there are no more).
398 1.1 christos */
399 1.1 christos if (!*opts)
400 1.1 christos fatal("Bugs in auth-options.c option processing.");
401 1.1 christos if (*opts == ' ' || *opts == '\t')
402 1.1 christos break; /* End of options. */
403 1.1 christos if (*opts != ',')
404 1.1 christos goto bad_option;
405 1.1 christos opts++;
406 1.1 christos /* Process the next option. */
407 1.1 christos }
408 1.1 christos
409 1.1 christos /* grant access */
410 1.1 christos return 1;
411 1.1 christos
412 1.1 christos bad_option:
413 1.1 christos logit("Bad options in %.100s file, line %lu: %.50s",
414 1.1 christos file, linenum, opts);
415 1.1 christos auth_debug_add("Bad options in %.100s file, line %lu: %.50s",
416 1.1 christos file, linenum, opts);
417 1.1 christos
418 1.3 adam /* deny access */
419 1.3 adam return 0;
420 1.3 adam }
421 1.3 adam
422 1.3 adam #define OPTIONS_CRITICAL 1
423 1.3 adam #define OPTIONS_EXTENSIONS 2
424 1.3 adam static int
425 1.3 adam parse_option_list(u_char *optblob, size_t optblob_len, struct passwd *pw,
426 1.3 adam u_int which, int crit,
427 1.3 adam int *cert_no_port_forwarding_flag,
428 1.3 adam int *cert_no_agent_forwarding_flag,
429 1.3 adam int *cert_no_x11_forwarding_flag,
430 1.3 adam int *cert_no_pty_flag,
431 1.3 adam int *cert_no_user_rc,
432 1.3 adam char **cert_forced_command,
433 1.3 adam int *cert_source_address_done)
434 1.3 adam {
435 1.3 adam char *command, *allowed;
436 1.3 adam const char *remote_ip;
437 1.3 adam u_char *name = NULL, *data_blob = NULL;
438 1.3 adam u_int nlen, dlen, clen;
439 1.3 adam Buffer c, data;
440 1.3 adam int ret = -1, found;
441 1.3 adam
442 1.3 adam buffer_init(&data);
443 1.3 adam
444 1.3 adam /* Make copy to avoid altering original */
445 1.3 adam buffer_init(&c);
446 1.3 adam buffer_append(&c, optblob, optblob_len);
447 1.3 adam
448 1.3 adam while (buffer_len(&c) > 0) {
449 1.3 adam if ((name = buffer_get_string_ret(&c, &nlen)) == NULL ||
450 1.3 adam (data_blob = buffer_get_string_ret(&c, &dlen)) == NULL) {
451 1.3 adam error("Certificate options corrupt");
452 1.3 adam goto out;
453 1.3 adam }
454 1.3 adam buffer_append(&data, data_blob, dlen);
455 1.3 adam debug3("found certificate option \"%.100s\" len %u",
456 1.3 adam name, dlen);
457 1.3 adam if (strlen(name) != nlen) {
458 1.3 adam error("Certificate constraint name contains \\0");
459 1.3 adam goto out;
460 1.3 adam }
461 1.3 adam found = 0;
462 1.3 adam if ((which & OPTIONS_EXTENSIONS) != 0) {
463 1.3 adam if (strcmp(name, "permit-X11-forwarding") == 0) {
464 1.3 adam *cert_no_x11_forwarding_flag = 0;
465 1.3 adam found = 1;
466 1.3 adam } else if (strcmp(name,
467 1.3 adam "permit-agent-forwarding") == 0) {
468 1.3 adam *cert_no_agent_forwarding_flag = 0;
469 1.3 adam found = 1;
470 1.3 adam } else if (strcmp(name,
471 1.3 adam "permit-port-forwarding") == 0) {
472 1.3 adam *cert_no_port_forwarding_flag = 0;
473 1.3 adam found = 1;
474 1.3 adam } else if (strcmp(name, "permit-pty") == 0) {
475 1.3 adam *cert_no_pty_flag = 0;
476 1.3 adam found = 1;
477 1.3 adam } else if (strcmp(name, "permit-user-rc") == 0) {
478 1.3 adam *cert_no_user_rc = 0;
479 1.3 adam found = 1;
480 1.3 adam }
481 1.3 adam }
482 1.3 adam if (!found && (which & OPTIONS_CRITICAL) != 0) {
483 1.3 adam if (strcmp(name, "force-command") == 0) {
484 1.3 adam if ((command = buffer_get_string_ret(&data,
485 1.3 adam &clen)) == NULL) {
486 1.3 adam error("Certificate constraint \"%s\" "
487 1.3 adam "corrupt", name);
488 1.3 adam goto out;
489 1.3 adam }
490 1.3 adam if (strlen(command) != clen) {
491 1.3 adam error("force-command constraint "
492 1.3 adam "contains \\0");
493 1.3 adam goto out;
494 1.3 adam }
495 1.3 adam if (*cert_forced_command != NULL) {
496 1.3 adam error("Certificate has multiple "
497 1.3 adam "force-command options");
498 1.3 adam xfree(command);
499 1.3 adam goto out;
500 1.3 adam }
501 1.3 adam *cert_forced_command = command;
502 1.3 adam found = 1;
503 1.3 adam }
504 1.3 adam if (strcmp(name, "source-address") == 0) {
505 1.3 adam if ((allowed = buffer_get_string_ret(&data,
506 1.3 adam &clen)) == NULL) {
507 1.3 adam error("Certificate constraint "
508 1.3 adam "\"%s\" corrupt", name);
509 1.3 adam goto out;
510 1.3 adam }
511 1.3 adam if (strlen(allowed) != clen) {
512 1.3 adam error("source-address constraint "
513 1.3 adam "contains \\0");
514 1.3 adam goto out;
515 1.3 adam }
516 1.3 adam if ((*cert_source_address_done)++) {
517 1.3 adam error("Certificate has multiple "
518 1.3 adam "source-address options");
519 1.3 adam xfree(allowed);
520 1.3 adam goto out;
521 1.3 adam }
522 1.3 adam remote_ip = get_remote_ipaddr();
523 1.3 adam switch (addr_match_cidr_list(remote_ip,
524 1.3 adam allowed)) {
525 1.3 adam case 1:
526 1.3 adam /* accepted */
527 1.3 adam xfree(allowed);
528 1.3 adam break;
529 1.3 adam case 0:
530 1.3 adam /* no match */
531 1.3 adam logit("Authentication tried for %.100s "
532 1.3 adam "with valid certificate but not "
533 1.3 adam "from a permitted host "
534 1.3 adam "(ip=%.200s).", pw->pw_name,
535 1.3 adam remote_ip);
536 1.3 adam auth_debug_add("Your address '%.200s' "
537 1.3 adam "is not permitted to use this "
538 1.3 adam "certificate for login.",
539 1.3 adam remote_ip);
540 1.3 adam xfree(allowed);
541 1.3 adam goto out;
542 1.3 adam case -1:
543 1.3 adam error("Certificate source-address "
544 1.3 adam "contents invalid");
545 1.3 adam xfree(allowed);
546 1.3 adam goto out;
547 1.3 adam }
548 1.3 adam found = 1;
549 1.3 adam }
550 1.3 adam }
551 1.3 adam
552 1.3 adam if (!found) {
553 1.3 adam if (crit) {
554 1.3 adam error("Certificate critical option \"%s\" "
555 1.3 adam "is not supported", name);
556 1.3 adam goto out;
557 1.3 adam } else {
558 1.3 adam logit("Certificate extension \"%s\" "
559 1.3 adam "is not supported", name);
560 1.3 adam }
561 1.3 adam } else if (buffer_len(&data) != 0) {
562 1.3 adam error("Certificate option \"%s\" corrupt "
563 1.3 adam "(extra data)", name);
564 1.3 adam goto out;
565 1.3 adam }
566 1.3 adam buffer_clear(&data);
567 1.3 adam xfree(name);
568 1.3 adam xfree(data_blob);
569 1.3 adam name = data_blob = NULL;
570 1.3 adam }
571 1.3 adam /* successfully parsed all options */
572 1.3 adam ret = 0;
573 1.3 adam
574 1.3 adam out:
575 1.3 adam if (ret != 0 &&
576 1.3 adam cert_forced_command != NULL &&
577 1.3 adam *cert_forced_command != NULL) {
578 1.3 adam xfree(*cert_forced_command);
579 1.3 adam *cert_forced_command = NULL;
580 1.3 adam }
581 1.3 adam if (name != NULL)
582 1.3 adam xfree(name);
583 1.3 adam if (data_blob != NULL)
584 1.3 adam xfree(data_blob);
585 1.3 adam buffer_free(&data);
586 1.3 adam buffer_free(&c);
587 1.3 adam return ret;
588 1.3 adam }
589 1.3 adam
590 1.3 adam /*
591 1.3 adam * Set options from critical certificate options. These supersede user key
592 1.3 adam * options so this must be called after auth_parse_options().
593 1.3 adam */
594 1.3 adam int
595 1.3 adam auth_cert_options(Key *k, struct passwd *pw)
596 1.3 adam {
597 1.3 adam int cert_no_port_forwarding_flag = 1;
598 1.3 adam int cert_no_agent_forwarding_flag = 1;
599 1.3 adam int cert_no_x11_forwarding_flag = 1;
600 1.3 adam int cert_no_pty_flag = 1;
601 1.3 adam int cert_no_user_rc = 1;
602 1.3 adam char *cert_forced_command = NULL;
603 1.3 adam int cert_source_address_done = 0;
604 1.3 adam
605 1.3 adam if (key_cert_is_legacy(k)) {
606 1.3 adam /* All options are in the one field for v00 certs */
607 1.3 adam if (parse_option_list(buffer_ptr(&k->cert->critical),
608 1.3 adam buffer_len(&k->cert->critical), pw,
609 1.3 adam OPTIONS_CRITICAL|OPTIONS_EXTENSIONS, 1,
610 1.3 adam &cert_no_port_forwarding_flag,
611 1.3 adam &cert_no_agent_forwarding_flag,
612 1.3 adam &cert_no_x11_forwarding_flag,
613 1.3 adam &cert_no_pty_flag,
614 1.3 adam &cert_no_user_rc,
615 1.3 adam &cert_forced_command,
616 1.3 adam &cert_source_address_done) == -1)
617 1.3 adam return -1;
618 1.3 adam } else {
619 1.3 adam /* Separate options and extensions for v01 certs */
620 1.3 adam if (parse_option_list(buffer_ptr(&k->cert->critical),
621 1.3 adam buffer_len(&k->cert->critical), pw,
622 1.3 adam OPTIONS_CRITICAL, 1, NULL, NULL, NULL, NULL, NULL,
623 1.3 adam &cert_forced_command,
624 1.3 adam &cert_source_address_done) == -1)
625 1.3 adam return -1;
626 1.3 adam if (parse_option_list(buffer_ptr(&k->cert->extensions),
627 1.3 adam buffer_len(&k->cert->extensions), pw,
628 1.3 adam OPTIONS_EXTENSIONS, 1,
629 1.3 adam &cert_no_port_forwarding_flag,
630 1.3 adam &cert_no_agent_forwarding_flag,
631 1.3 adam &cert_no_x11_forwarding_flag,
632 1.3 adam &cert_no_pty_flag,
633 1.3 adam &cert_no_user_rc,
634 1.3 adam NULL, NULL) == -1)
635 1.3 adam return -1;
636 1.3 adam }
637 1.1 christos
638 1.3 adam no_port_forwarding_flag |= cert_no_port_forwarding_flag;
639 1.3 adam no_agent_forwarding_flag |= cert_no_agent_forwarding_flag;
640 1.3 adam no_x11_forwarding_flag |= cert_no_x11_forwarding_flag;
641 1.3 adam no_pty_flag |= cert_no_pty_flag;
642 1.3 adam no_user_rc |= cert_no_user_rc;
643 1.3 adam /* CA-specified forced command supersedes key option */
644 1.3 adam if (cert_forced_command != NULL) {
645 1.3 adam if (forced_command != NULL)
646 1.3 adam xfree(forced_command);
647 1.3 adam forced_command = cert_forced_command;
648 1.3 adam }
649 1.1 christos return 0;
650 1.1 christos }
651 1.3 adam
652