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