xref: /onnv-gate/usr/src/cmd/ssh/sshd/auth-options.c (revision 5562:0f12179b71ab)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * Author: Tatu Ylonen <ylo@cs.hut.fi>
30Sstevel@tonic-gate  * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
40Sstevel@tonic-gate  *                    All rights reserved
50Sstevel@tonic-gate  * As far as I am concerned, the code I have written for this software
60Sstevel@tonic-gate  * can be used freely for any purpose.  Any derived versions of this
70Sstevel@tonic-gate  * software must be clearly marked as such, and if the derived work is
80Sstevel@tonic-gate  * incompatible with the protocol description in the RFC file, it must be
90Sstevel@tonic-gate  * called by a name other than "ssh" or "Secure Shell".
100Sstevel@tonic-gate  */
110Sstevel@tonic-gate 
120Sstevel@tonic-gate #include "includes.h"
130Sstevel@tonic-gate RCSID("$OpenBSD: auth-options.c,v 1.26 2002/07/30 17:03:55 markus Exp $");
140Sstevel@tonic-gate 
150Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
160Sstevel@tonic-gate 
170Sstevel@tonic-gate #include "xmalloc.h"
180Sstevel@tonic-gate #include "match.h"
190Sstevel@tonic-gate #include "log.h"
200Sstevel@tonic-gate #include "canohost.h"
210Sstevel@tonic-gate #include "channels.h"
220Sstevel@tonic-gate #include "auth-options.h"
230Sstevel@tonic-gate #include "servconf.h"
240Sstevel@tonic-gate #include "misc.h"
250Sstevel@tonic-gate #include "auth.h"
260Sstevel@tonic-gate 
270Sstevel@tonic-gate /* Flags set authorized_keys flags */
280Sstevel@tonic-gate int no_port_forwarding_flag = 0;
290Sstevel@tonic-gate int no_agent_forwarding_flag = 0;
300Sstevel@tonic-gate int no_x11_forwarding_flag = 0;
310Sstevel@tonic-gate int no_pty_flag = 0;
320Sstevel@tonic-gate 
330Sstevel@tonic-gate /* "command=" option. */
340Sstevel@tonic-gate char *forced_command = NULL;
350Sstevel@tonic-gate 
360Sstevel@tonic-gate /* "environment=" options. */
370Sstevel@tonic-gate struct envstring *custom_environment = NULL;
380Sstevel@tonic-gate 
390Sstevel@tonic-gate extern ServerOptions options;
400Sstevel@tonic-gate 
410Sstevel@tonic-gate void
auth_clear_options(void)420Sstevel@tonic-gate auth_clear_options(void)
430Sstevel@tonic-gate {
440Sstevel@tonic-gate 	no_agent_forwarding_flag = 0;
450Sstevel@tonic-gate 	no_port_forwarding_flag = 0;
460Sstevel@tonic-gate 	no_pty_flag = 0;
470Sstevel@tonic-gate 	no_x11_forwarding_flag = 0;
480Sstevel@tonic-gate 	while (custom_environment) {
490Sstevel@tonic-gate 		struct envstring *ce = custom_environment;
500Sstevel@tonic-gate 		custom_environment = ce->next;
510Sstevel@tonic-gate 		xfree(ce->s);
520Sstevel@tonic-gate 		xfree(ce);
530Sstevel@tonic-gate 	}
540Sstevel@tonic-gate 	if (forced_command) {
550Sstevel@tonic-gate 		xfree(forced_command);
560Sstevel@tonic-gate 		forced_command = NULL;
570Sstevel@tonic-gate 	}
580Sstevel@tonic-gate 	channel_clear_permitted_opens();
590Sstevel@tonic-gate 	auth_debug_reset();
600Sstevel@tonic-gate }
610Sstevel@tonic-gate 
620Sstevel@tonic-gate /*
630Sstevel@tonic-gate  * return 1 if access is granted, 0 if not.
640Sstevel@tonic-gate  * side effect: sets key option flags
650Sstevel@tonic-gate  */
660Sstevel@tonic-gate int
auth_parse_options(struct passwd * pw,char * opts,char * file,u_long linenum)670Sstevel@tonic-gate auth_parse_options(struct passwd *pw, char *opts, char *file, u_long linenum)
680Sstevel@tonic-gate {
690Sstevel@tonic-gate 	const char *cp;
700Sstevel@tonic-gate 	int i;
710Sstevel@tonic-gate 
720Sstevel@tonic-gate 	/* reset options */
730Sstevel@tonic-gate 	auth_clear_options();
740Sstevel@tonic-gate 
750Sstevel@tonic-gate 	if (!opts)
760Sstevel@tonic-gate 		return 1;
770Sstevel@tonic-gate 
780Sstevel@tonic-gate 	while (*opts && *opts != ' ' && *opts != '\t') {
790Sstevel@tonic-gate 		cp = "no-port-forwarding";
800Sstevel@tonic-gate 		if (strncasecmp(opts, cp, strlen(cp)) == 0) {
810Sstevel@tonic-gate 			auth_debug_add("Port forwarding disabled.");
820Sstevel@tonic-gate 			no_port_forwarding_flag = 1;
830Sstevel@tonic-gate 			opts += strlen(cp);
840Sstevel@tonic-gate 			goto next_option;
850Sstevel@tonic-gate 		}
860Sstevel@tonic-gate 		cp = "no-agent-forwarding";
870Sstevel@tonic-gate 		if (strncasecmp(opts, cp, strlen(cp)) == 0) {
880Sstevel@tonic-gate 			auth_debug_add("Agent forwarding disabled.");
890Sstevel@tonic-gate 			no_agent_forwarding_flag = 1;
900Sstevel@tonic-gate 			opts += strlen(cp);
910Sstevel@tonic-gate 			goto next_option;
920Sstevel@tonic-gate 		}
930Sstevel@tonic-gate 		cp = "no-X11-forwarding";
940Sstevel@tonic-gate 		if (strncasecmp(opts, cp, strlen(cp)) == 0) {
950Sstevel@tonic-gate 			auth_debug_add("X11 forwarding disabled.");
960Sstevel@tonic-gate 			no_x11_forwarding_flag = 1;
970Sstevel@tonic-gate 			opts += strlen(cp);
980Sstevel@tonic-gate 			goto next_option;
990Sstevel@tonic-gate 		}
1000Sstevel@tonic-gate 		cp = "no-pty";
1010Sstevel@tonic-gate 		if (strncasecmp(opts, cp, strlen(cp)) == 0) {
1020Sstevel@tonic-gate 			auth_debug_add("Pty allocation disabled.");
1030Sstevel@tonic-gate 			no_pty_flag = 1;
1040Sstevel@tonic-gate 			opts += strlen(cp);
1050Sstevel@tonic-gate 			goto next_option;
1060Sstevel@tonic-gate 		}
1070Sstevel@tonic-gate 		cp = "command=\"";
1080Sstevel@tonic-gate 		if (strncasecmp(opts, cp, strlen(cp)) == 0) {
1090Sstevel@tonic-gate 			opts += strlen(cp);
1100Sstevel@tonic-gate 			forced_command = xmalloc(strlen(opts) + 1);
1110Sstevel@tonic-gate 			i = 0;
1120Sstevel@tonic-gate 			while (*opts) {
1130Sstevel@tonic-gate 				if (*opts == '"')
1140Sstevel@tonic-gate 					break;
1150Sstevel@tonic-gate 				if (*opts == '\\' && opts[1] == '"') {
1160Sstevel@tonic-gate 					opts += 2;
1170Sstevel@tonic-gate 					forced_command[i++] = '"';
1180Sstevel@tonic-gate 					continue;
1190Sstevel@tonic-gate 				}
1200Sstevel@tonic-gate 				forced_command[i++] = *opts++;
1210Sstevel@tonic-gate 			}
1220Sstevel@tonic-gate 			if (!*opts) {
1230Sstevel@tonic-gate 				debug("%.100s, line %lu: missing end quote",
1240Sstevel@tonic-gate 				    file, linenum);
1250Sstevel@tonic-gate 				auth_debug_add("%.100s, line %lu: missing end quote",
1260Sstevel@tonic-gate 				    file, linenum);
1270Sstevel@tonic-gate 				xfree(forced_command);
1280Sstevel@tonic-gate 				forced_command = NULL;
1290Sstevel@tonic-gate 				goto bad_option;
1300Sstevel@tonic-gate 			}
1310Sstevel@tonic-gate 			forced_command[i] = 0;
1320Sstevel@tonic-gate 			auth_debug_add("Forced command: %.900s", forced_command);
1330Sstevel@tonic-gate 			opts++;
1340Sstevel@tonic-gate 			goto next_option;
1350Sstevel@tonic-gate 		}
1360Sstevel@tonic-gate 		cp = "environment=\"";
1370Sstevel@tonic-gate 		if (options.permit_user_env &&
1380Sstevel@tonic-gate 		    strncasecmp(opts, cp, strlen(cp)) == 0) {
1390Sstevel@tonic-gate 			char *s;
1400Sstevel@tonic-gate 			struct envstring *new_envstring;
1410Sstevel@tonic-gate 
1420Sstevel@tonic-gate 			opts += strlen(cp);
1430Sstevel@tonic-gate 			s = xmalloc(strlen(opts) + 1);
1440Sstevel@tonic-gate 			i = 0;
1450Sstevel@tonic-gate 			while (*opts) {
1460Sstevel@tonic-gate 				if (*opts == '"')
1470Sstevel@tonic-gate 					break;
1480Sstevel@tonic-gate 				if (*opts == '\\' && opts[1] == '"') {
1490Sstevel@tonic-gate 					opts += 2;
1500Sstevel@tonic-gate 					s[i++] = '"';
1510Sstevel@tonic-gate 					continue;
1520Sstevel@tonic-gate 				}
1530Sstevel@tonic-gate 				s[i++] = *opts++;
1540Sstevel@tonic-gate 			}
1550Sstevel@tonic-gate 			if (!*opts) {
1560Sstevel@tonic-gate 				debug("%.100s, line %lu: missing end quote",
1570Sstevel@tonic-gate 				    file, linenum);
1580Sstevel@tonic-gate 				auth_debug_add("%.100s, line %lu: missing end quote",
1590Sstevel@tonic-gate 				    file, linenum);
1600Sstevel@tonic-gate 				xfree(s);
1610Sstevel@tonic-gate 				goto bad_option;
1620Sstevel@tonic-gate 			}
1630Sstevel@tonic-gate 			s[i] = 0;
1640Sstevel@tonic-gate 			auth_debug_add("Adding to environment: %.900s", s);
1650Sstevel@tonic-gate 			debug("Adding to environment: %.900s", s);
1660Sstevel@tonic-gate 			opts++;
1670Sstevel@tonic-gate 			new_envstring = xmalloc(sizeof(struct envstring));
1680Sstevel@tonic-gate 			new_envstring->s = s;
1690Sstevel@tonic-gate 			new_envstring->next = custom_environment;
1700Sstevel@tonic-gate 			custom_environment = new_envstring;
1710Sstevel@tonic-gate 			goto next_option;
1720Sstevel@tonic-gate 		}
1730Sstevel@tonic-gate 		cp = "from=\"";
1740Sstevel@tonic-gate 		if (strncasecmp(opts, cp, strlen(cp)) == 0) {
1750Sstevel@tonic-gate 			const char *remote_ip = get_remote_ipaddr();
1760Sstevel@tonic-gate 			const char *remote_host = get_canonical_hostname(
1770Sstevel@tonic-gate 			    options.verify_reverse_mapping);
1780Sstevel@tonic-gate 			char *patterns = xmalloc(strlen(opts) + 1);
1790Sstevel@tonic-gate 
1800Sstevel@tonic-gate 			opts += strlen(cp);
1810Sstevel@tonic-gate 			i = 0;
1820Sstevel@tonic-gate 			while (*opts) {
1830Sstevel@tonic-gate 				if (*opts == '"')
1840Sstevel@tonic-gate 					break;
1850Sstevel@tonic-gate 				if (*opts == '\\' && opts[1] == '"') {
1860Sstevel@tonic-gate 					opts += 2;
1870Sstevel@tonic-gate 					patterns[i++] = '"';
1880Sstevel@tonic-gate 					continue;
1890Sstevel@tonic-gate 				}
1900Sstevel@tonic-gate 				patterns[i++] = *opts++;
1910Sstevel@tonic-gate 			}
1920Sstevel@tonic-gate 			if (!*opts) {
1930Sstevel@tonic-gate 				debug("%.100s, line %lu: missing end quote",
1940Sstevel@tonic-gate 				    file, linenum);
1950Sstevel@tonic-gate 				auth_debug_add("%.100s, line %lu: missing end quote",
1960Sstevel@tonic-gate 				    file, linenum);
1970Sstevel@tonic-gate 				xfree(patterns);
1980Sstevel@tonic-gate 				goto bad_option;
1990Sstevel@tonic-gate 			}
2000Sstevel@tonic-gate 			patterns[i] = 0;
2010Sstevel@tonic-gate 			opts++;
2020Sstevel@tonic-gate 			if (match_host_and_ip(remote_host, remote_ip,
2030Sstevel@tonic-gate 			    patterns) != 1) {
2040Sstevel@tonic-gate 				xfree(patterns);
2050Sstevel@tonic-gate 				log("Authentication tried for %.100s with "
2060Sstevel@tonic-gate 				    "correct key but not from a permitted "
2070Sstevel@tonic-gate 				    "host (host=%.200s, ip=%.200s).",
2080Sstevel@tonic-gate 				    pw->pw_name, remote_host, remote_ip);
2090Sstevel@tonic-gate 				auth_debug_add("Your host '%.200s' is not "
2100Sstevel@tonic-gate 				    "permitted to use this key for login.",
2110Sstevel@tonic-gate 				    remote_host);
2120Sstevel@tonic-gate 				/* deny access */
2130Sstevel@tonic-gate 				return 0;
2140Sstevel@tonic-gate 			}
2150Sstevel@tonic-gate 			xfree(patterns);
2160Sstevel@tonic-gate 			/* Host name matches. */
2170Sstevel@tonic-gate 			goto next_option;
2180Sstevel@tonic-gate 		}
2190Sstevel@tonic-gate 		cp = "permitopen=\"";
2200Sstevel@tonic-gate 		if (strncasecmp(opts, cp, strlen(cp)) == 0) {
2210Sstevel@tonic-gate 			char host[256], sport[6];
2220Sstevel@tonic-gate 			u_short port;
2230Sstevel@tonic-gate 			char *patterns = xmalloc(strlen(opts) + 1);
2240Sstevel@tonic-gate 
2250Sstevel@tonic-gate 			opts += strlen(cp);
2260Sstevel@tonic-gate 			i = 0;
2270Sstevel@tonic-gate 			while (*opts) {
2280Sstevel@tonic-gate 				if (*opts == '"')
2290Sstevel@tonic-gate 					break;
2300Sstevel@tonic-gate 				if (*opts == '\\' && opts[1] == '"') {
2310Sstevel@tonic-gate 					opts += 2;
2320Sstevel@tonic-gate 					patterns[i++] = '"';
2330Sstevel@tonic-gate 					continue;
2340Sstevel@tonic-gate 				}
2350Sstevel@tonic-gate 				patterns[i++] = *opts++;
2360Sstevel@tonic-gate 			}
2370Sstevel@tonic-gate 			if (!*opts) {
2380Sstevel@tonic-gate 				debug("%.100s, line %lu: missing end quote",
2390Sstevel@tonic-gate 				    file, linenum);
2400Sstevel@tonic-gate 				auth_debug_add("%.100s, line %lu: missing end quote",
2410Sstevel@tonic-gate 				    file, linenum);
2420Sstevel@tonic-gate 				xfree(patterns);
2430Sstevel@tonic-gate 				goto bad_option;
2440Sstevel@tonic-gate 			}
2450Sstevel@tonic-gate 			patterns[i] = 0;
2460Sstevel@tonic-gate 			opts++;
2470Sstevel@tonic-gate 			if (sscanf(patterns, "%255[^:]:%5[0-9]", host, sport) != 2 &&
2480Sstevel@tonic-gate 			    sscanf(patterns, "%255[^/]/%5[0-9]", host, sport) != 2) {
2490Sstevel@tonic-gate 				debug("%.100s, line %lu: Bad permitopen specification "
2500Sstevel@tonic-gate 				    "<%.100s>", file, linenum, patterns);
2510Sstevel@tonic-gate 				auth_debug_add("%.100s, line %lu: "
2520Sstevel@tonic-gate 				    "Bad permitopen specification", file, linenum);
2530Sstevel@tonic-gate 				xfree(patterns);
2540Sstevel@tonic-gate 				goto bad_option;
2550Sstevel@tonic-gate 			}
2560Sstevel@tonic-gate 			if ((port = a2port(sport)) == 0) {
2570Sstevel@tonic-gate 				debug("%.100s, line %lu: Bad permitopen port <%.100s>",
2580Sstevel@tonic-gate 				    file, linenum, sport);
2590Sstevel@tonic-gate 				auth_debug_add("%.100s, line %lu: "
2600Sstevel@tonic-gate 				    "Bad permitopen port", file, linenum);
2610Sstevel@tonic-gate 				xfree(patterns);
2620Sstevel@tonic-gate 				goto bad_option;
2630Sstevel@tonic-gate 			}
2640Sstevel@tonic-gate 			if (options.allow_tcp_forwarding)
2650Sstevel@tonic-gate 				channel_add_permitted_opens(host, port);
2660Sstevel@tonic-gate 			xfree(patterns);
2670Sstevel@tonic-gate 			goto next_option;
2680Sstevel@tonic-gate 		}
2690Sstevel@tonic-gate next_option:
2700Sstevel@tonic-gate 		/*
2710Sstevel@tonic-gate 		 * Skip the comma, and move to the next option
2720Sstevel@tonic-gate 		 * (or break out if there are no more).
2730Sstevel@tonic-gate 		 */
2740Sstevel@tonic-gate 		if (!*opts)
2750Sstevel@tonic-gate 			fatal("Bugs in auth-options.c option processing.");
2760Sstevel@tonic-gate 		if (*opts == ' ' || *opts == '\t')
2770Sstevel@tonic-gate 			break;		/* End of options. */
2780Sstevel@tonic-gate 		if (*opts != ',')
2790Sstevel@tonic-gate 			goto bad_option;
2800Sstevel@tonic-gate 		opts++;
2810Sstevel@tonic-gate 		/* Process the next option. */
2820Sstevel@tonic-gate 	}
2830Sstevel@tonic-gate 
284*5562Sjp161948 	auth_debug_send();
2850Sstevel@tonic-gate 
2860Sstevel@tonic-gate 	/* grant access */
2870Sstevel@tonic-gate 	return 1;
2880Sstevel@tonic-gate 
2890Sstevel@tonic-gate bad_option:
2900Sstevel@tonic-gate 	log("Bad options in %.100s file, line %lu: %.50s",
2910Sstevel@tonic-gate 	    file, linenum, opts);
2920Sstevel@tonic-gate 	auth_debug_add("Bad options in %.100s file, line %lu: %.50s",
2930Sstevel@tonic-gate 	    file, linenum, opts);
2940Sstevel@tonic-gate 
295*5562Sjp161948 	auth_debug_send();
2960Sstevel@tonic-gate 
2970Sstevel@tonic-gate 	/* deny access */
2980Sstevel@tonic-gate 	return 0;
2990Sstevel@tonic-gate }
300