auth-options.c revision 1.13 1 1.7 christos /* $NetBSD: auth-options.c,v 1.13 2016/08/02 13:45:12 christos Exp $ */
2 1.13 christos /* $OpenBSD: auth-options.c,v 1.71 2016/03/07 19:02:43 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.7 christos __RCSID("$NetBSD: auth-options.c,v 1.13 2016/08/02 13:45:12 christos 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.9 christos #include "key.h" /* XXX for typedef */
27 1.9 christos #include "buffer.h" /* XXX for typedef */
28 1.1 christos #include "xmalloc.h"
29 1.1 christos #include "match.h"
30 1.9 christos #include "ssherr.h"
31 1.1 christos #include "log.h"
32 1.1 christos #include "canohost.h"
33 1.13 christos #include "packet.h"
34 1.9 christos #include "sshbuf.h"
35 1.8 christos #include "misc.h"
36 1.1 christos #include "channels.h"
37 1.1 christos #include "servconf.h"
38 1.9 christos #include "sshkey.h"
39 1.3 adam #include "auth-options.h"
40 1.1 christos #include "hostfile.h"
41 1.1 christos #include "auth.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.7 christos free(ce->s);
78 1.7 christos free(ce);
79 1.1 christos }
80 1.12 christos free(forced_command);
81 1.12 christos forced_command = NULL;
82 1.12 christos free(authorized_principals);
83 1.12 christos authorized_principals = NULL;
84 1.12 christos forced_tun_device = -1;
85 1.12 christos channel_clear_permitted_opens();
86 1.12 christos }
87 1.12 christos
88 1.12 christos /*
89 1.12 christos * Match flag 'opt' in *optsp, and if allow_negate is set then also match
90 1.12 christos * 'no-opt'. Returns -1 if option not matched, 1 if option matches or 0
91 1.12 christos * if negated option matches.
92 1.12 christos * If the option or negated option matches, then *optsp is updated to
93 1.12 christos * point to the first character after the option and, if 'msg' is not NULL
94 1.12 christos * then a message based on it added via auth_debug_add().
95 1.12 christos */
96 1.12 christos static int
97 1.12 christos match_flag(const char *opt, int allow_negate, const char **optsp, const char *msg)
98 1.12 christos {
99 1.12 christos size_t opt_len = strlen(opt);
100 1.12 christos const char *opts = *optsp;
101 1.12 christos int negate = 0;
102 1.12 christos
103 1.12 christos if (allow_negate && strncasecmp(opts, "no-", 3) == 0) {
104 1.12 christos opts += 3;
105 1.12 christos negate = 1;
106 1.1 christos }
107 1.12 christos if (strncasecmp(opts, opt, opt_len) == 0) {
108 1.12 christos *optsp = opts + opt_len;
109 1.12 christos if (msg != NULL) {
110 1.12 christos auth_debug_add("%s %s.", msg,
111 1.12 christos negate ? "disabled" : "enabled");
112 1.12 christos }
113 1.12 christos return negate ? 0 : 1;
114 1.3 adam }
115 1.12 christos return -1;
116 1.1 christos }
117 1.1 christos
118 1.1 christos /*
119 1.1 christos * return 1 if access is granted, 0 if not.
120 1.1 christos * side effect: sets key option flags
121 1.1 christos */
122 1.1 christos int
123 1.4 christos auth_parse_options(struct passwd *pw, const char *opts, const char *file,
124 1.4 christos u_long linenum)
125 1.1 christos {
126 1.13 christos struct ssh *ssh = active_state; /* XXX */
127 1.1 christos const char *cp;
128 1.12 christos int i, r;
129 1.1 christos
130 1.1 christos /* reset options */
131 1.1 christos auth_clear_options();
132 1.1 christos
133 1.1 christos if (!opts)
134 1.1 christos return 1;
135 1.1 christos
136 1.1 christos while (*opts && *opts != ' ' && *opts != '\t') {
137 1.12 christos if ((r = match_flag("cert-authority", 0, &opts, NULL)) != -1) {
138 1.12 christos key_is_cert_authority = r;
139 1.3 adam goto next_option;
140 1.3 adam }
141 1.12 christos if ((r = match_flag("restrict", 0, &opts, NULL)) != -1) {
142 1.12 christos auth_debug_add("Key is restricted.");
143 1.1 christos no_port_forwarding_flag = 1;
144 1.12 christos no_agent_forwarding_flag = 1;
145 1.12 christos no_x11_forwarding_flag = 1;
146 1.12 christos no_pty_flag = 1;
147 1.12 christos no_user_rc = 1;
148 1.12 christos goto next_option;
149 1.12 christos }
150 1.12 christos if ((r = match_flag("port-forwarding", 1, &opts,
151 1.12 christos "Port forwarding")) != -1) {
152 1.12 christos no_port_forwarding_flag = r != 1;
153 1.1 christos goto next_option;
154 1.1 christos }
155 1.12 christos if ((r = match_flag("agent-forwarding", 1, &opts,
156 1.12 christos "Agent forwarding")) != -1) {
157 1.12 christos no_agent_forwarding_flag = r != 1;
158 1.1 christos goto next_option;
159 1.1 christos }
160 1.12 christos if ((r = match_flag("x11-forwarding", 1, &opts,
161 1.12 christos "X11 forwarding")) != -1) {
162 1.12 christos no_x11_forwarding_flag = r != 1;
163 1.1 christos goto next_option;
164 1.1 christos }
165 1.12 christos if ((r = match_flag("pty", 1, &opts,
166 1.12 christos "PTY allocation")) != -1) {
167 1.12 christos no_pty_flag = r != 1;
168 1.1 christos goto next_option;
169 1.1 christos }
170 1.12 christos if ((r = match_flag("user-rc", 1, &opts,
171 1.12 christos "User rc execution")) != -1) {
172 1.12 christos no_user_rc = r != 1;
173 1.1 christos goto next_option;
174 1.1 christos }
175 1.1 christos cp = "command=\"";
176 1.1 christos if (strncasecmp(opts, cp, strlen(cp)) == 0) {
177 1.1 christos opts += strlen(cp);
178 1.12 christos free(forced_command);
179 1.1 christos forced_command = xmalloc(strlen(opts) + 1);
180 1.1 christos i = 0;
181 1.1 christos while (*opts) {
182 1.1 christos if (*opts == '"')
183 1.1 christos break;
184 1.1 christos if (*opts == '\\' && opts[1] == '"') {
185 1.1 christos opts += 2;
186 1.1 christos forced_command[i++] = '"';
187 1.1 christos continue;
188 1.1 christos }
189 1.1 christos forced_command[i++] = *opts++;
190 1.1 christos }
191 1.1 christos if (!*opts) {
192 1.1 christos debug("%.100s, line %lu: missing end quote",
193 1.1 christos file, linenum);
194 1.1 christos auth_debug_add("%.100s, line %lu: missing end quote",
195 1.1 christos file, linenum);
196 1.7 christos free(forced_command);
197 1.1 christos forced_command = NULL;
198 1.1 christos goto bad_option;
199 1.1 christos }
200 1.1 christos forced_command[i] = '\0';
201 1.4 christos auth_debug_add("Forced command.");
202 1.1 christos opts++;
203 1.1 christos goto next_option;
204 1.1 christos }
205 1.3 adam cp = "principals=\"";
206 1.3 adam if (strncasecmp(opts, cp, strlen(cp)) == 0) {
207 1.3 adam opts += strlen(cp);
208 1.12 christos free(authorized_principals);
209 1.3 adam authorized_principals = xmalloc(strlen(opts) + 1);
210 1.3 adam i = 0;
211 1.3 adam while (*opts) {
212 1.3 adam if (*opts == '"')
213 1.3 adam break;
214 1.3 adam if (*opts == '\\' && opts[1] == '"') {
215 1.3 adam opts += 2;
216 1.3 adam authorized_principals[i++] = '"';
217 1.3 adam continue;
218 1.3 adam }
219 1.3 adam authorized_principals[i++] = *opts++;
220 1.3 adam }
221 1.3 adam if (!*opts) {
222 1.3 adam debug("%.100s, line %lu: missing end quote",
223 1.3 adam file, linenum);
224 1.3 adam auth_debug_add("%.100s, line %lu: missing end quote",
225 1.3 adam file, linenum);
226 1.7 christos free(authorized_principals);
227 1.3 adam authorized_principals = NULL;
228 1.3 adam goto bad_option;
229 1.3 adam }
230 1.3 adam authorized_principals[i] = '\0';
231 1.3 adam auth_debug_add("principals: %.900s",
232 1.3 adam authorized_principals);
233 1.3 adam opts++;
234 1.3 adam goto next_option;
235 1.3 adam }
236 1.1 christos cp = "environment=\"";
237 1.10 christos if (strncasecmp(opts, cp, strlen(cp)) == 0) {
238 1.1 christos char *s;
239 1.1 christos struct envstring *new_envstring;
240 1.1 christos
241 1.1 christos opts += strlen(cp);
242 1.1 christos s = xmalloc(strlen(opts) + 1);
243 1.1 christos i = 0;
244 1.1 christos while (*opts) {
245 1.1 christos if (*opts == '"')
246 1.1 christos break;
247 1.1 christos if (*opts == '\\' && opts[1] == '"') {
248 1.1 christos opts += 2;
249 1.1 christos s[i++] = '"';
250 1.1 christos continue;
251 1.1 christos }
252 1.1 christos s[i++] = *opts++;
253 1.1 christos }
254 1.1 christos if (!*opts) {
255 1.1 christos debug("%.100s, line %lu: missing end quote",
256 1.1 christos file, linenum);
257 1.1 christos auth_debug_add("%.100s, line %lu: missing end quote",
258 1.1 christos file, linenum);
259 1.7 christos free(s);
260 1.1 christos goto bad_option;
261 1.1 christos }
262 1.1 christos s[i] = '\0';
263 1.1 christos opts++;
264 1.10 christos if (options.permit_user_env) {
265 1.10 christos auth_debug_add("Adding to environment: "
266 1.10 christos "%.900s", s);
267 1.10 christos debug("Adding to environment: %.900s", s);
268 1.10 christos new_envstring = xcalloc(1,
269 1.10 christos sizeof(*new_envstring));
270 1.10 christos new_envstring->s = s;
271 1.10 christos new_envstring->next = custom_environment;
272 1.10 christos custom_environment = new_envstring;
273 1.10 christos s = NULL;
274 1.10 christos }
275 1.10 christos free(s);
276 1.1 christos goto next_option;
277 1.1 christos }
278 1.1 christos cp = "from=\"";
279 1.1 christos if (strncasecmp(opts, cp, strlen(cp)) == 0) {
280 1.13 christos const char *remote_ip = ssh_remote_ipaddr(ssh);
281 1.13 christos const char *remote_host = auth_get_canonical_hostname(
282 1.13 christos ssh, options.use_dns);
283 1.1 christos char *patterns = xmalloc(strlen(opts) + 1);
284 1.1 christos
285 1.1 christos opts += strlen(cp);
286 1.1 christos i = 0;
287 1.1 christos while (*opts) {
288 1.1 christos if (*opts == '"')
289 1.1 christos break;
290 1.1 christos if (*opts == '\\' && opts[1] == '"') {
291 1.1 christos opts += 2;
292 1.1 christos patterns[i++] = '"';
293 1.1 christos continue;
294 1.1 christos }
295 1.1 christos patterns[i++] = *opts++;
296 1.1 christos }
297 1.1 christos if (!*opts) {
298 1.1 christos debug("%.100s, line %lu: missing end quote",
299 1.1 christos file, linenum);
300 1.1 christos auth_debug_add("%.100s, line %lu: missing end quote",
301 1.1 christos file, linenum);
302 1.7 christos free(patterns);
303 1.1 christos goto bad_option;
304 1.1 christos }
305 1.1 christos patterns[i] = '\0';
306 1.1 christos opts++;
307 1.1 christos switch (match_host_and_ip(remote_host, remote_ip,
308 1.1 christos patterns)) {
309 1.1 christos case 1:
310 1.7 christos free(patterns);
311 1.1 christos /* Host name matches. */
312 1.1 christos goto next_option;
313 1.1 christos case -1:
314 1.1 christos debug("%.100s, line %lu: invalid criteria",
315 1.1 christos file, linenum);
316 1.1 christos auth_debug_add("%.100s, line %lu: "
317 1.1 christos "invalid criteria", file, linenum);
318 1.1 christos /* FALLTHROUGH */
319 1.1 christos case 0:
320 1.7 christos free(patterns);
321 1.1 christos logit("Authentication tried for %.100s with "
322 1.1 christos "correct key but not from a permitted "
323 1.1 christos "host (host=%.200s, ip=%.200s).",
324 1.1 christos pw->pw_name, remote_host, remote_ip);
325 1.1 christos auth_debug_add("Your host '%.200s' is not "
326 1.1 christos "permitted to use this key for login.",
327 1.1 christos remote_host);
328 1.1 christos break;
329 1.1 christos }
330 1.1 christos /* deny access */
331 1.1 christos return 0;
332 1.1 christos }
333 1.1 christos cp = "permitopen=\"";
334 1.1 christos if (strncasecmp(opts, cp, strlen(cp)) == 0) {
335 1.1 christos char *host, *p;
336 1.1 christos int port;
337 1.1 christos char *patterns = xmalloc(strlen(opts) + 1);
338 1.1 christos
339 1.1 christos opts += strlen(cp);
340 1.1 christos i = 0;
341 1.1 christos while (*opts) {
342 1.1 christos if (*opts == '"')
343 1.1 christos break;
344 1.1 christos if (*opts == '\\' && opts[1] == '"') {
345 1.1 christos opts += 2;
346 1.1 christos patterns[i++] = '"';
347 1.1 christos continue;
348 1.1 christos }
349 1.1 christos patterns[i++] = *opts++;
350 1.1 christos }
351 1.1 christos if (!*opts) {
352 1.1 christos debug("%.100s, line %lu: missing end quote",
353 1.1 christos file, linenum);
354 1.1 christos auth_debug_add("%.100s, line %lu: missing "
355 1.1 christos "end quote", file, linenum);
356 1.7 christos free(patterns);
357 1.1 christos goto bad_option;
358 1.1 christos }
359 1.1 christos patterns[i] = '\0';
360 1.1 christos opts++;
361 1.1 christos p = patterns;
362 1.8 christos /* XXX - add streamlocal support */
363 1.1 christos host = hpdelim(&p);
364 1.1 christos if (host == NULL || strlen(host) >= NI_MAXHOST) {
365 1.1 christos debug("%.100s, line %lu: Bad permitopen "
366 1.1 christos "specification <%.100s>", file, linenum,
367 1.1 christos patterns);
368 1.1 christos auth_debug_add("%.100s, line %lu: "
369 1.1 christos "Bad permitopen specification", file,
370 1.1 christos linenum);
371 1.7 christos free(patterns);
372 1.1 christos goto bad_option;
373 1.1 christos }
374 1.1 christos host = cleanhostname(host);
375 1.5 christos if (p == NULL || (port = permitopen_port(p)) < 0) {
376 1.1 christos debug("%.100s, line %lu: Bad permitopen port "
377 1.1 christos "<%.100s>", file, linenum, p ? p : "");
378 1.1 christos auth_debug_add("%.100s, line %lu: "
379 1.1 christos "Bad permitopen port", file, linenum);
380 1.7 christos free(patterns);
381 1.1 christos goto bad_option;
382 1.1 christos }
383 1.6 christos if ((options.allow_tcp_forwarding & FORWARD_LOCAL) != 0)
384 1.1 christos channel_add_permitted_opens(host, port);
385 1.7 christos free(patterns);
386 1.1 christos goto next_option;
387 1.1 christos }
388 1.1 christos cp = "tunnel=\"";
389 1.1 christos if (strncasecmp(opts, cp, strlen(cp)) == 0) {
390 1.1 christos char *tun = NULL;
391 1.1 christos opts += strlen(cp);
392 1.1 christos tun = xmalloc(strlen(opts) + 1);
393 1.1 christos i = 0;
394 1.1 christos while (*opts) {
395 1.1 christos if (*opts == '"')
396 1.1 christos break;
397 1.1 christos tun[i++] = *opts++;
398 1.1 christos }
399 1.1 christos if (!*opts) {
400 1.1 christos debug("%.100s, line %lu: missing end quote",
401 1.1 christos file, linenum);
402 1.1 christos auth_debug_add("%.100s, line %lu: missing end quote",
403 1.1 christos file, linenum);
404 1.7 christos free(tun);
405 1.1 christos forced_tun_device = -1;
406 1.1 christos goto bad_option;
407 1.1 christos }
408 1.1 christos tun[i] = '\0';
409 1.1 christos forced_tun_device = a2tun(tun, NULL);
410 1.7 christos free(tun);
411 1.1 christos if (forced_tun_device == SSH_TUNID_ERR) {
412 1.1 christos debug("%.100s, line %lu: invalid tun device",
413 1.1 christos file, linenum);
414 1.1 christos auth_debug_add("%.100s, line %lu: invalid tun device",
415 1.1 christos file, linenum);
416 1.1 christos forced_tun_device = -1;
417 1.1 christos goto bad_option;
418 1.1 christos }
419 1.1 christos auth_debug_add("Forced tun device: %d", forced_tun_device);
420 1.1 christos opts++;
421 1.1 christos goto next_option;
422 1.1 christos }
423 1.1 christos next_option:
424 1.1 christos /*
425 1.1 christos * Skip the comma, and move to the next option
426 1.1 christos * (or break out if there are no more).
427 1.1 christos */
428 1.1 christos if (!*opts)
429 1.1 christos fatal("Bugs in auth-options.c option processing.");
430 1.1 christos if (*opts == ' ' || *opts == '\t')
431 1.1 christos break; /* End of options. */
432 1.1 christos if (*opts != ',')
433 1.1 christos goto bad_option;
434 1.1 christos opts++;
435 1.1 christos /* Process the next option. */
436 1.1 christos }
437 1.1 christos
438 1.1 christos /* grant access */
439 1.1 christos return 1;
440 1.1 christos
441 1.1 christos bad_option:
442 1.1 christos logit("Bad options in %.100s file, line %lu: %.50s",
443 1.1 christos file, linenum, opts);
444 1.1 christos auth_debug_add("Bad options in %.100s file, line %lu: %.50s",
445 1.1 christos file, linenum, opts);
446 1.1 christos
447 1.3 adam /* deny access */
448 1.3 adam return 0;
449 1.3 adam }
450 1.3 adam
451 1.3 adam #define OPTIONS_CRITICAL 1
452 1.3 adam #define OPTIONS_EXTENSIONS 2
453 1.3 adam static int
454 1.9 christos parse_option_list(struct sshbuf *oblob, struct passwd *pw,
455 1.3 adam u_int which, int crit,
456 1.3 adam int *cert_no_port_forwarding_flag,
457 1.3 adam int *cert_no_agent_forwarding_flag,
458 1.3 adam int *cert_no_x11_forwarding_flag,
459 1.3 adam int *cert_no_pty_flag,
460 1.3 adam int *cert_no_user_rc,
461 1.3 adam char **cert_forced_command,
462 1.3 adam int *cert_source_address_done)
463 1.3 adam {
464 1.13 christos struct ssh *ssh = active_state; /* XXX */
465 1.3 adam char *command, *allowed;
466 1.3 adam const char *remote_ip;
467 1.7 christos char *name = NULL;
468 1.9 christos struct sshbuf *c = NULL, *data = NULL;
469 1.9 christos int r, ret = -1, result, found;
470 1.9 christos
471 1.9 christos if ((c = sshbuf_fromb(oblob)) == NULL) {
472 1.9 christos error("%s: sshbuf_fromb failed", __func__);
473 1.9 christos goto out;
474 1.9 christos }
475 1.9 christos
476 1.9 christos while (sshbuf_len(c) > 0) {
477 1.9 christos sshbuf_free(data);
478 1.9 christos data = NULL;
479 1.9 christos if ((r = sshbuf_get_cstring(c, &name, NULL)) != 0 ||
480 1.9 christos (r = sshbuf_froms(c, &data)) != 0) {
481 1.9 christos error("Unable to parse certificate options: %s",
482 1.9 christos ssh_err(r));
483 1.3 adam goto out;
484 1.3 adam }
485 1.9 christos debug3("found certificate option \"%.100s\" len %zu",
486 1.9 christos name, sshbuf_len(data));
487 1.3 adam found = 0;
488 1.3 adam if ((which & OPTIONS_EXTENSIONS) != 0) {
489 1.3 adam if (strcmp(name, "permit-X11-forwarding") == 0) {
490 1.3 adam *cert_no_x11_forwarding_flag = 0;
491 1.3 adam found = 1;
492 1.3 adam } else if (strcmp(name,
493 1.3 adam "permit-agent-forwarding") == 0) {
494 1.3 adam *cert_no_agent_forwarding_flag = 0;
495 1.3 adam found = 1;
496 1.3 adam } else if (strcmp(name,
497 1.3 adam "permit-port-forwarding") == 0) {
498 1.3 adam *cert_no_port_forwarding_flag = 0;
499 1.3 adam found = 1;
500 1.3 adam } else if (strcmp(name, "permit-pty") == 0) {
501 1.3 adam *cert_no_pty_flag = 0;
502 1.3 adam found = 1;
503 1.3 adam } else if (strcmp(name, "permit-user-rc") == 0) {
504 1.3 adam *cert_no_user_rc = 0;
505 1.3 adam found = 1;
506 1.3 adam }
507 1.3 adam }
508 1.3 adam if (!found && (which & OPTIONS_CRITICAL) != 0) {
509 1.3 adam if (strcmp(name, "force-command") == 0) {
510 1.9 christos if ((r = sshbuf_get_cstring(data, &command,
511 1.9 christos NULL)) != 0) {
512 1.9 christos error("Unable to parse \"%s\" "
513 1.9 christos "section: %s", name, ssh_err(r));
514 1.3 adam goto out;
515 1.3 adam }
516 1.3 adam if (*cert_forced_command != NULL) {
517 1.3 adam error("Certificate has multiple "
518 1.3 adam "force-command options");
519 1.7 christos free(command);
520 1.3 adam goto out;
521 1.3 adam }
522 1.3 adam *cert_forced_command = command;
523 1.3 adam found = 1;
524 1.3 adam }
525 1.3 adam if (strcmp(name, "source-address") == 0) {
526 1.9 christos if ((r = sshbuf_get_cstring(data, &allowed,
527 1.9 christos NULL)) != 0) {
528 1.9 christos error("Unable to parse \"%s\" "
529 1.9 christos "section: %s", name, ssh_err(r));
530 1.3 adam goto out;
531 1.3 adam }
532 1.3 adam if ((*cert_source_address_done)++) {
533 1.3 adam error("Certificate has multiple "
534 1.3 adam "source-address options");
535 1.7 christos free(allowed);
536 1.3 adam goto out;
537 1.3 adam }
538 1.13 christos remote_ip = ssh_remote_ipaddr(ssh);
539 1.8 christos result = addr_match_cidr_list(remote_ip,
540 1.8 christos allowed);
541 1.8 christos free(allowed);
542 1.8 christos switch (result) {
543 1.3 adam case 1:
544 1.3 adam /* accepted */
545 1.3 adam break;
546 1.3 adam case 0:
547 1.3 adam /* no match */
548 1.3 adam logit("Authentication tried for %.100s "
549 1.3 adam "with valid certificate but not "
550 1.3 adam "from a permitted host "
551 1.3 adam "(ip=%.200s).", pw->pw_name,
552 1.3 adam remote_ip);
553 1.3 adam auth_debug_add("Your address '%.200s' "
554 1.3 adam "is not permitted to use this "
555 1.3 adam "certificate for login.",
556 1.3 adam remote_ip);
557 1.3 adam goto out;
558 1.3 adam case -1:
559 1.8 christos default:
560 1.3 adam error("Certificate source-address "
561 1.3 adam "contents invalid");
562 1.3 adam goto out;
563 1.3 adam }
564 1.3 adam found = 1;
565 1.3 adam }
566 1.3 adam }
567 1.3 adam
568 1.3 adam if (!found) {
569 1.3 adam if (crit) {
570 1.3 adam error("Certificate critical option \"%s\" "
571 1.3 adam "is not supported", name);
572 1.3 adam goto out;
573 1.3 adam } else {
574 1.3 adam logit("Certificate extension \"%s\" "
575 1.3 adam "is not supported", name);
576 1.3 adam }
577 1.9 christos } else if (sshbuf_len(data) != 0) {
578 1.3 adam error("Certificate option \"%s\" corrupt "
579 1.3 adam "(extra data)", name);
580 1.3 adam goto out;
581 1.3 adam }
582 1.7 christos free(name);
583 1.7 christos name = NULL;
584 1.3 adam }
585 1.3 adam /* successfully parsed all options */
586 1.3 adam ret = 0;
587 1.3 adam
588 1.3 adam out:
589 1.3 adam if (ret != 0 &&
590 1.3 adam cert_forced_command != NULL &&
591 1.3 adam *cert_forced_command != NULL) {
592 1.7 christos free(*cert_forced_command);
593 1.3 adam *cert_forced_command = NULL;
594 1.3 adam }
595 1.12 christos free(name);
596 1.9 christos sshbuf_free(data);
597 1.9 christos sshbuf_free(c);
598 1.3 adam return ret;
599 1.3 adam }
600 1.3 adam
601 1.3 adam /*
602 1.3 adam * Set options from critical certificate options. These supersede user key
603 1.3 adam * options so this must be called after auth_parse_options().
604 1.3 adam */
605 1.3 adam int
606 1.9 christos auth_cert_options(struct sshkey *k, struct passwd *pw)
607 1.3 adam {
608 1.3 adam int cert_no_port_forwarding_flag = 1;
609 1.3 adam int cert_no_agent_forwarding_flag = 1;
610 1.3 adam int cert_no_x11_forwarding_flag = 1;
611 1.3 adam int cert_no_pty_flag = 1;
612 1.3 adam int cert_no_user_rc = 1;
613 1.3 adam char *cert_forced_command = NULL;
614 1.3 adam int cert_source_address_done = 0;
615 1.3 adam
616 1.11 christos /* Separate options and extensions for v01 certs */
617 1.11 christos if (parse_option_list(k->cert->critical, pw,
618 1.11 christos OPTIONS_CRITICAL, 1, NULL, NULL, NULL, NULL, NULL,
619 1.11 christos &cert_forced_command,
620 1.11 christos &cert_source_address_done) == -1)
621 1.11 christos return -1;
622 1.11 christos if (parse_option_list(k->cert->extensions, pw,
623 1.11 christos OPTIONS_EXTENSIONS, 0,
624 1.11 christos &cert_no_port_forwarding_flag,
625 1.11 christos &cert_no_agent_forwarding_flag,
626 1.11 christos &cert_no_x11_forwarding_flag,
627 1.11 christos &cert_no_pty_flag,
628 1.11 christos &cert_no_user_rc,
629 1.11 christos NULL, NULL) == -1)
630 1.11 christos return -1;
631 1.1 christos
632 1.3 adam no_port_forwarding_flag |= cert_no_port_forwarding_flag;
633 1.3 adam no_agent_forwarding_flag |= cert_no_agent_forwarding_flag;
634 1.3 adam no_x11_forwarding_flag |= cert_no_x11_forwarding_flag;
635 1.3 adam no_pty_flag |= cert_no_pty_flag;
636 1.3 adam no_user_rc |= cert_no_user_rc;
637 1.3 adam /* CA-specified forced command supersedes key option */
638 1.3 adam if (cert_forced_command != NULL) {
639 1.12 christos free(forced_command);
640 1.3 adam forced_command = cert_forced_command;
641 1.3 adam }
642 1.1 christos return 0;
643 1.1 christos }
644 1.3 adam
645