1 /* $OpenBSD: auth-options.c,v 1.48 2010/03/07 11:57:13 dtucker Exp $ */
3 * Author: Tatu Ylonen <ylo@cs.hut.fi>
4 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
6 * 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".
15 #include <sys/types.h>
23 #include "openbsd-compat/sys-queue.h"
30 #include "auth-options.h"
39 #include "monitor_wrap.h"
41 /* Flags set authorized_keys flags */
42 int no_port_forwarding_flag = 0;
43 int no_agent_forwarding_flag = 0;
44 int no_x11_forwarding_flag = 0;
47 int key_is_cert_authority = 0;
49 /* "command=" option. */
50 char *forced_command = NULL;
52 /* "environment=" options. */
53 struct envstring *custom_environment = NULL;
55 /* "tunnel=" option. */
56 int forced_tun_device = -1;
58 extern ServerOptions options;
61 auth_clear_options(void)
63 no_agent_forwarding_flag = 0;
64 no_port_forwarding_flag = 0;
66 no_x11_forwarding_flag = 0;
68 key_is_cert_authority = 0;
69 while (custom_environment) {
70 struct envstring *ce = custom_environment;
71 custom_environment = ce->next;
76 xfree(forced_command);
77 forced_command = NULL;
79 forced_tun_device = -1;
80 channel_clear_permitted_opens();
84 * return 1 if access is granted, 0 if not.
85 * side effect: sets key option flags
88 auth_parse_options(struct passwd *pw, char *opts, char *file, u_long linenum)
99 while (*opts && *opts != ' ' && *opts != '\t') {
100 cp = "cert-authority";
101 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
102 key_is_cert_authority = 1;
106 cp = "no-port-forwarding";
107 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
108 auth_debug_add("Port forwarding disabled.");
109 no_port_forwarding_flag = 1;
113 cp = "no-agent-forwarding";
114 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
115 auth_debug_add("Agent forwarding disabled.");
116 no_agent_forwarding_flag = 1;
120 cp = "no-X11-forwarding";
121 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
122 auth_debug_add("X11 forwarding disabled.");
123 no_x11_forwarding_flag = 1;
128 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
129 auth_debug_add("Pty allocation disabled.");
135 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
136 auth_debug_add("User rc file execution disabled.");
142 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
144 forced_command = xmalloc(strlen(opts) + 1);
149 if (*opts == '\\' && opts[1] == '"') {
151 forced_command[i++] = '"';
154 forced_command[i++] = *opts++;
157 debug("%.100s, line %lu: missing end quote",
159 auth_debug_add("%.100s, line %lu: missing end quote",
161 xfree(forced_command);
162 forced_command = NULL;
165 forced_command[i] = '\0';
166 auth_debug_add("Forced command: %.900s", forced_command);
170 cp = "environment=\"";
171 if (options.permit_user_env &&
172 strncasecmp(opts, cp, strlen(cp)) == 0) {
174 struct envstring *new_envstring;
177 s = xmalloc(strlen(opts) + 1);
182 if (*opts == '\\' && opts[1] == '"') {
190 debug("%.100s, line %lu: missing end quote",
192 auth_debug_add("%.100s, line %lu: missing end quote",
198 auth_debug_add("Adding to environment: %.900s", s);
199 debug("Adding to environment: %.900s", s);
201 new_envstring = xmalloc(sizeof(struct envstring));
202 new_envstring->s = s;
203 new_envstring->next = custom_environment;
204 custom_environment = new_envstring;
208 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
209 const char *remote_ip = get_remote_ipaddr();
210 const char *remote_host = get_canonical_hostname(
212 char *patterns = xmalloc(strlen(opts) + 1);
219 if (*opts == '\\' && opts[1] == '"') {
224 patterns[i++] = *opts++;
227 debug("%.100s, line %lu: missing end quote",
229 auth_debug_add("%.100s, line %lu: missing end quote",
236 switch (match_host_and_ip(remote_host, remote_ip,
240 /* Host name matches. */
243 debug("%.100s, line %lu: invalid criteria",
245 auth_debug_add("%.100s, line %lu: "
246 "invalid criteria", file, linenum);
250 logit("Authentication tried for %.100s with "
251 "correct key but not from a permitted "
252 "host (host=%.200s, ip=%.200s).",
253 pw->pw_name, remote_host, remote_ip);
254 auth_debug_add("Your host '%.200s' is not "
255 "permitted to use this key for login.",
262 cp = "permitopen=\"";
263 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
266 char *patterns = xmalloc(strlen(opts) + 1);
273 if (*opts == '\\' && opts[1] == '"') {
278 patterns[i++] = *opts++;
281 debug("%.100s, line %lu: missing end quote",
283 auth_debug_add("%.100s, line %lu: missing "
284 "end quote", file, linenum);
292 if (host == NULL || strlen(host) >= NI_MAXHOST) {
293 debug("%.100s, line %lu: Bad permitopen "
294 "specification <%.100s>", file, linenum,
296 auth_debug_add("%.100s, line %lu: "
297 "Bad permitopen specification", file,
302 host = cleanhostname(host);
303 if (p == NULL || (port = a2port(p)) <= 0) {
304 debug("%.100s, line %lu: Bad permitopen port "
305 "<%.100s>", file, linenum, p ? p : "");
306 auth_debug_add("%.100s, line %lu: "
307 "Bad permitopen port", file, linenum);
311 if (options.allow_tcp_forwarding)
312 channel_add_permitted_opens(host, port);
317 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
320 tun = xmalloc(strlen(opts) + 1);
328 debug("%.100s, line %lu: missing end quote",
330 auth_debug_add("%.100s, line %lu: missing end quote",
333 forced_tun_device = -1;
337 forced_tun_device = a2tun(tun, NULL);
339 if (forced_tun_device == SSH_TUNID_ERR) {
340 debug("%.100s, line %lu: invalid tun device",
342 auth_debug_add("%.100s, line %lu: invalid tun device",
344 forced_tun_device = -1;
347 auth_debug_add("Forced tun device: %d", forced_tun_device);
353 * Skip the comma, and move to the next option
354 * (or break out if there are no more).
357 fatal("Bugs in auth-options.c option processing.");
358 if (*opts == ' ' || *opts == '\t')
359 break; /* End of options. */
363 /* Process the next option. */
370 logit("Bad options in %.100s file, line %lu: %.50s",
371 file, linenum, opts);
372 auth_debug_add("Bad options in %.100s file, line %lu: %.50s",
373 file, linenum, opts);
380 * Set options from certificate constraints. These supersede user key options
381 * so this must be called after auth_parse_options().
384 auth_cert_constraints(Buffer *c_orig, struct passwd *pw)
386 u_char *name = NULL, *data_blob = NULL;
387 u_int nlen, dlen, clen;
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;
401 /* Make copy to avoid altering original */
403 buffer_append(&c, buffer_ptr(c_orig), buffer_len(c_orig));
405 while (buffer_len(&c) > 0) {
406 if ((name = buffer_get_string_ret(&c, &nlen)) == NULL ||
407 (data_blob = buffer_get_string_ret(&c, &dlen)) == NULL) {
408 error("Certificate constraints corrupt");
411 buffer_append(&data, data_blob, dlen);
412 debug3("found certificate constraint \"%.100s\" len %u",
414 if (strlen(name) != nlen) {
415 error("Certificate constraint name contains \\0");
418 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)
428 else if (strcmp(name, "force-command") == 0) {
429 char *command = buffer_get_string_ret(&data, &clen);
431 if (command == NULL) {
432 error("Certificate constraint \"%s\" corrupt",
436 if (strlen(command) != clen) {
437 error("force-command constrain contains \\0");
440 if (cert_forced_command != NULL) {
441 error("Certificate has multiple "
442 "force-command constraints");
446 cert_forced_command = command;
447 } else if (strcmp(name, "source-address") == 0) {
448 char *allowed = buffer_get_string_ret(&data, &clen);
449 const char *remote_ip = get_remote_ipaddr();
451 if (allowed == NULL) {
452 error("Certificate constraint \"%s\" corrupt",
456 if (strlen(allowed) != clen) {
457 error("source-address constrain contains \\0");
460 if (cert_source_address_done++) {
461 error("Certificate has multiple "
462 "source-address constraints");
466 switch (addr_match_cidr_list(remote_ip, allowed)) {
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);
483 error("Certificate source-address contents "
489 error("Certificate constraint \"%s\" is not supported",
494 if (buffer_len(&data) != 0) {
495 error("Certificate constraint \"%s\" corrupt "
496 "(extra data)", name);
502 name = data_blob = NULL;
505 /* successfully parsed all constraints */
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;
523 if (data_blob != NULL)