xref: /titanic_50/usr/src/cmd/ssh/sshd/servconf.c (revision 56e7e6c6e6c82bcc8d19a12a3bd8f9fd529ba5b4)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
37c478bd9Sstevel@tonic-gate  *                    All rights reserved
47c478bd9Sstevel@tonic-gate  *
57c478bd9Sstevel@tonic-gate  * As far as I am concerned, the code I have written for this software
67c478bd9Sstevel@tonic-gate  * can be used freely for any purpose.  Any derived versions of this
77c478bd9Sstevel@tonic-gate  * software must be clearly marked as such, and if the derived work is
87c478bd9Sstevel@tonic-gate  * incompatible with the protocol description in the RFC file, it must be
97c478bd9Sstevel@tonic-gate  * called by a name other than "ssh" or "Secure Shell".
107c478bd9Sstevel@tonic-gate  */
117c478bd9Sstevel@tonic-gate /*
126f8d59d8SJan Pechanec  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
137c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
147c478bd9Sstevel@tonic-gate  */
157c478bd9Sstevel@tonic-gate 
167c478bd9Sstevel@tonic-gate #include "includes.h"
177c478bd9Sstevel@tonic-gate RCSID("$OpenBSD: servconf.c,v 1.115 2002/09/04 18:52:42 stevesk Exp $");
187c478bd9Sstevel@tonic-gate 
197c478bd9Sstevel@tonic-gate #ifdef HAVE_DEFOPEN
207c478bd9Sstevel@tonic-gate #include <deflt.h>
217c478bd9Sstevel@tonic-gate #endif /* HAVE_DEFOPEN */
227c478bd9Sstevel@tonic-gate 
237c478bd9Sstevel@tonic-gate #if defined(KRB4)
247c478bd9Sstevel@tonic-gate #include <krb.h>
257c478bd9Sstevel@tonic-gate #endif
267c478bd9Sstevel@tonic-gate #if defined(KRB5)
277c478bd9Sstevel@tonic-gate #ifdef HEIMDAL
287c478bd9Sstevel@tonic-gate #include <krb.h>
297c478bd9Sstevel@tonic-gate #else
307c478bd9Sstevel@tonic-gate /* Bodge - but then, so is using the kerberos IV KEYFILE to get a Kerberos V
317c478bd9Sstevel@tonic-gate  * keytab */
327c478bd9Sstevel@tonic-gate #define KEYFILE "/etc/krb5.keytab"
337c478bd9Sstevel@tonic-gate #endif
347c478bd9Sstevel@tonic-gate #endif
357c478bd9Sstevel@tonic-gate #ifdef AFS
367c478bd9Sstevel@tonic-gate #include <kafs.h>
377c478bd9Sstevel@tonic-gate #endif
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate #include "ssh.h"
407c478bd9Sstevel@tonic-gate #include "log.h"
41b07b2f5cSHuie-Ying Lee #include "buffer.h"
427c478bd9Sstevel@tonic-gate #include "servconf.h"
437c478bd9Sstevel@tonic-gate #include "xmalloc.h"
447c478bd9Sstevel@tonic-gate #include "compat.h"
457c478bd9Sstevel@tonic-gate #include "pathnames.h"
467c478bd9Sstevel@tonic-gate #include "tildexpand.h"
477c478bd9Sstevel@tonic-gate #include "misc.h"
487c478bd9Sstevel@tonic-gate #include "cipher.h"
497c478bd9Sstevel@tonic-gate #include "kex.h"
507c478bd9Sstevel@tonic-gate #include "mac.h"
517c478bd9Sstevel@tonic-gate #include "auth.h"
52b07b2f5cSHuie-Ying Lee #include "match.h"
53b07b2f5cSHuie-Ying Lee #include "groupaccess.h"
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate static void add_listen_addr(ServerOptions *, char *, u_short);
567c478bd9Sstevel@tonic-gate static void add_one_listen_addr(ServerOptions *, char *, u_short);
577c478bd9Sstevel@tonic-gate 
58b07b2f5cSHuie-Ying Lee extern Buffer cfg;
59b07b2f5cSHuie-Ying Lee 
607c478bd9Sstevel@tonic-gate /* AF_UNSPEC or AF_INET or AF_INET6 */
617c478bd9Sstevel@tonic-gate extern int IPv4or6;
627c478bd9Sstevel@tonic-gate 
636f8d59d8SJan Pechanec /*
646f8d59d8SJan Pechanec  * Initializes the server options to their initial (unset) values. Some of those
656f8d59d8SJan Pechanec  * that stay unset after the command line options and configuration files are
666f8d59d8SJan Pechanec  * read are set to their default values in fill_default_server_options().
676f8d59d8SJan Pechanec  */
687c478bd9Sstevel@tonic-gate void
697c478bd9Sstevel@tonic-gate initialize_server_options(ServerOptions *options)
707c478bd9Sstevel@tonic-gate {
717c478bd9Sstevel@tonic-gate 	(void) memset(options, 0, sizeof(*options));
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate 	/* Portable-specific options */
747c478bd9Sstevel@tonic-gate 	options->pam_authentication_via_kbd_int = -1;
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate 	/* Standard Options */
777c478bd9Sstevel@tonic-gate 	options->num_ports = 0;
787c478bd9Sstevel@tonic-gate 	options->ports_from_cmdline = 0;
797c478bd9Sstevel@tonic-gate 	options->listen_addrs = NULL;
807c478bd9Sstevel@tonic-gate 	options->num_host_key_files = 0;
817c478bd9Sstevel@tonic-gate 	options->pid_file = NULL;
827c478bd9Sstevel@tonic-gate 	options->server_key_bits = -1;
837c478bd9Sstevel@tonic-gate 	options->login_grace_time = -1;
847c478bd9Sstevel@tonic-gate 	options->key_regeneration_time = -1;
857c478bd9Sstevel@tonic-gate 	options->permit_root_login = PERMIT_NOT_SET;
867c478bd9Sstevel@tonic-gate 	options->ignore_rhosts = -1;
877c478bd9Sstevel@tonic-gate 	options->ignore_user_known_hosts = -1;
887c478bd9Sstevel@tonic-gate 	options->print_motd = -1;
897c478bd9Sstevel@tonic-gate 	options->print_lastlog = -1;
907c478bd9Sstevel@tonic-gate 	options->x11_forwarding = -1;
917c478bd9Sstevel@tonic-gate 	options->x11_display_offset = -1;
927c478bd9Sstevel@tonic-gate 	options->x11_use_localhost = -1;
937c478bd9Sstevel@tonic-gate 	options->xauth_location = NULL;
947c478bd9Sstevel@tonic-gate 	options->strict_modes = -1;
957c478bd9Sstevel@tonic-gate 	options->keepalives = -1;
967c478bd9Sstevel@tonic-gate 	options->log_facility = SYSLOG_FACILITY_NOT_SET;
977c478bd9Sstevel@tonic-gate 	options->log_level = SYSLOG_LEVEL_NOT_SET;
987c478bd9Sstevel@tonic-gate 	options->rhosts_authentication = -1;
997c478bd9Sstevel@tonic-gate 	options->rhosts_rsa_authentication = -1;
1007c478bd9Sstevel@tonic-gate 	options->hostbased_authentication = -1;
1017c478bd9Sstevel@tonic-gate 	options->hostbased_uses_name_from_packet_only = -1;
1027c478bd9Sstevel@tonic-gate 	options->rsa_authentication = -1;
1037c478bd9Sstevel@tonic-gate 	options->pubkey_authentication = -1;
1047c478bd9Sstevel@tonic-gate #ifdef GSSAPI
1057c478bd9Sstevel@tonic-gate 	options->gss_authentication = -1;
1067c478bd9Sstevel@tonic-gate 	options->gss_keyex = -1;
1077c478bd9Sstevel@tonic-gate 	options->gss_store_creds = -1;
1087c478bd9Sstevel@tonic-gate 	options->gss_use_session_ccache = -1;
1097c478bd9Sstevel@tonic-gate 	options->gss_cleanup_creds = -1;
1107c478bd9Sstevel@tonic-gate #endif
1117c478bd9Sstevel@tonic-gate #if defined(KRB4) || defined(KRB5)
1127c478bd9Sstevel@tonic-gate 	options->kerberos_authentication = -1;
1137c478bd9Sstevel@tonic-gate 	options->kerberos_or_local_passwd = -1;
1147c478bd9Sstevel@tonic-gate 	options->kerberos_ticket_cleanup = -1;
1157c478bd9Sstevel@tonic-gate #endif
1167c478bd9Sstevel@tonic-gate #if defined(AFS) || defined(KRB5)
1177c478bd9Sstevel@tonic-gate 	options->kerberos_tgt_passing = -1;
1187c478bd9Sstevel@tonic-gate #endif
1197c478bd9Sstevel@tonic-gate #ifdef AFS
1207c478bd9Sstevel@tonic-gate 	options->afs_token_passing = -1;
1217c478bd9Sstevel@tonic-gate #endif
1227c478bd9Sstevel@tonic-gate 	options->password_authentication = -1;
1237c478bd9Sstevel@tonic-gate 	options->kbd_interactive_authentication = -1;
1247c478bd9Sstevel@tonic-gate 	options->challenge_response_authentication = -1;
1257c478bd9Sstevel@tonic-gate 	options->permit_empty_passwd = -1;
1267c478bd9Sstevel@tonic-gate 	options->permit_user_env = -1;
1277c478bd9Sstevel@tonic-gate 	options->compression = -1;
1287c478bd9Sstevel@tonic-gate 	options->allow_tcp_forwarding = -1;
1297c478bd9Sstevel@tonic-gate 	options->num_allow_users = 0;
1307c478bd9Sstevel@tonic-gate 	options->num_deny_users = 0;
1317c478bd9Sstevel@tonic-gate 	options->num_allow_groups = 0;
1327c478bd9Sstevel@tonic-gate 	options->num_deny_groups = 0;
1337c478bd9Sstevel@tonic-gate 	options->ciphers = NULL;
1347c478bd9Sstevel@tonic-gate 	options->macs = NULL;
1357c478bd9Sstevel@tonic-gate 	options->protocol = SSH_PROTO_UNKNOWN;
1367c478bd9Sstevel@tonic-gate 	options->gateway_ports = -1;
1377c478bd9Sstevel@tonic-gate 	options->num_subsystems = 0;
1387c478bd9Sstevel@tonic-gate 	options->max_startups_begin = -1;
1397c478bd9Sstevel@tonic-gate 	options->max_startups_rate = -1;
1407c478bd9Sstevel@tonic-gate 	options->max_startups = -1;
1417c478bd9Sstevel@tonic-gate 	options->banner = NULL;
1427c478bd9Sstevel@tonic-gate 	options->verify_reverse_mapping = -1;
1437c478bd9Sstevel@tonic-gate 	options->client_alive_interval = -1;
1447c478bd9Sstevel@tonic-gate 	options->client_alive_count_max = -1;
1457c478bd9Sstevel@tonic-gate 	options->authorized_keys_file = NULL;
1467c478bd9Sstevel@tonic-gate 	options->authorized_keys_file2 = NULL;
1477c478bd9Sstevel@tonic-gate 
1487c478bd9Sstevel@tonic-gate 	options->max_auth_tries = -1;
1497c478bd9Sstevel@tonic-gate 	options->max_auth_tries_log = -1;
1507c478bd9Sstevel@tonic-gate 
1517c478bd9Sstevel@tonic-gate 	options->max_init_auth_tries = -1;
1527c478bd9Sstevel@tonic-gate 	options->max_init_auth_tries_log = -1;
1537c478bd9Sstevel@tonic-gate 
1547c478bd9Sstevel@tonic-gate 	options->lookup_client_hostnames = -1;
155cd7d5fafSJan Pechanec 	options->use_openssl_engine = -1;
1566f8d59d8SJan Pechanec 	options->chroot_directory = NULL;
1577c478bd9Sstevel@tonic-gate }
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate #ifdef HAVE_DEFOPEN
1607c478bd9Sstevel@tonic-gate /*
1617c478bd9Sstevel@tonic-gate  * Reads /etc/default/login and defaults several ServerOptions:
1627c478bd9Sstevel@tonic-gate  *
1637c478bd9Sstevel@tonic-gate  * PermitRootLogin
1647c478bd9Sstevel@tonic-gate  * PermitEmptyPasswords
1657c478bd9Sstevel@tonic-gate  * LoginGraceTime
1667c478bd9Sstevel@tonic-gate  *
1677c478bd9Sstevel@tonic-gate  * CONSOLE=*      -> PermitRootLogin=without-password
1687c478bd9Sstevel@tonic-gate  * #CONSOLE=*     -> PermitRootLogin=yes
1697c478bd9Sstevel@tonic-gate  *
1707c478bd9Sstevel@tonic-gate  * PASSREQ=YES    -> PermitEmptyPasswords=no
1717c478bd9Sstevel@tonic-gate  * PASSREQ=NO     -> PermitEmptyPasswords=yes
1727c478bd9Sstevel@tonic-gate  * #PASSREQ=*     -> PermitEmptyPasswords=no
1737c478bd9Sstevel@tonic-gate  *
1747c478bd9Sstevel@tonic-gate  * TIMEOUT=<secs> -> LoginGraceTime=<secs>
1757c478bd9Sstevel@tonic-gate  * #TIMEOUT=<secs> -> LoginGraceTime=300
1767c478bd9Sstevel@tonic-gate  */
1777c478bd9Sstevel@tonic-gate static
1787c478bd9Sstevel@tonic-gate void
1797c478bd9Sstevel@tonic-gate deflt_fill_default_server_options(ServerOptions *options)
1807c478bd9Sstevel@tonic-gate {
1817c478bd9Sstevel@tonic-gate 	int	flags;
1827c478bd9Sstevel@tonic-gate 	char	*ptr;
1837c478bd9Sstevel@tonic-gate 
1847c478bd9Sstevel@tonic-gate 	if (defopen(_PATH_DEFAULT_LOGIN))
1857c478bd9Sstevel@tonic-gate 		return;
1867c478bd9Sstevel@tonic-gate 
1877c478bd9Sstevel@tonic-gate 	/* Ignore case */
1887c478bd9Sstevel@tonic-gate 	flags = defcntl(DC_GETFLAGS, 0);
1897c478bd9Sstevel@tonic-gate 	TURNOFF(flags, DC_CASE);
1907c478bd9Sstevel@tonic-gate 	(void) defcntl(DC_SETFLAGS, flags);
1917c478bd9Sstevel@tonic-gate 
1927c478bd9Sstevel@tonic-gate 	if (options->permit_root_login == PERMIT_NOT_SET &&
1937c478bd9Sstevel@tonic-gate 	    (ptr = defread("CONSOLE=")) != NULL)
1947c478bd9Sstevel@tonic-gate 		options->permit_root_login = PERMIT_NO_PASSWD;
1957c478bd9Sstevel@tonic-gate 
1967c478bd9Sstevel@tonic-gate 	if (options->permit_empty_passwd == -1 &&
1977c478bd9Sstevel@tonic-gate 	    (ptr = defread("PASSREQ=")) != NULL) {
1987c478bd9Sstevel@tonic-gate 		if (strcasecmp("YES", ptr) == 0)
1997c478bd9Sstevel@tonic-gate 			options->permit_empty_passwd = 0;
2007c478bd9Sstevel@tonic-gate 		else if (strcasecmp("NO", ptr) == 0)
2017c478bd9Sstevel@tonic-gate 			options->permit_empty_passwd = 1;
2027c478bd9Sstevel@tonic-gate 	}
2037c478bd9Sstevel@tonic-gate 
2047c478bd9Sstevel@tonic-gate 	if (options->max_init_auth_tries == -1 &&
2057c478bd9Sstevel@tonic-gate 	    (ptr = defread("RETRIES=")) != NULL) {
2067c478bd9Sstevel@tonic-gate 		options->max_init_auth_tries = atoi(ptr);
2077c478bd9Sstevel@tonic-gate 	}
2087c478bd9Sstevel@tonic-gate 
2097c478bd9Sstevel@tonic-gate 	if (options->max_init_auth_tries_log == -1 &&
2107c478bd9Sstevel@tonic-gate 	    (ptr = defread("SYSLOG_FAILED_LOGINS=")) != NULL) {
2117c478bd9Sstevel@tonic-gate 		options->max_init_auth_tries_log = atoi(ptr);
2127c478bd9Sstevel@tonic-gate 	}
2137c478bd9Sstevel@tonic-gate 
2147c478bd9Sstevel@tonic-gate 	if (options->login_grace_time == -1) {
2157c478bd9Sstevel@tonic-gate 		if ((ptr = defread("TIMEOUT=")) != NULL)
2167c478bd9Sstevel@tonic-gate 			options->login_grace_time = (unsigned)atoi(ptr);
2177c478bd9Sstevel@tonic-gate 		else
2187c478bd9Sstevel@tonic-gate 			options->login_grace_time = 300;
2197c478bd9Sstevel@tonic-gate 	}
2207c478bd9Sstevel@tonic-gate 
2217c478bd9Sstevel@tonic-gate 	(void) defopen((char *)NULL);
2227c478bd9Sstevel@tonic-gate }
2237c478bd9Sstevel@tonic-gate #endif /* HAVE_DEFOPEN */
2247c478bd9Sstevel@tonic-gate 
2257c478bd9Sstevel@tonic-gate void
2267c478bd9Sstevel@tonic-gate fill_default_server_options(ServerOptions *options)
2277c478bd9Sstevel@tonic-gate {
2287c478bd9Sstevel@tonic-gate 
2297c478bd9Sstevel@tonic-gate #ifdef HAVE_DEFOPEN
2307c478bd9Sstevel@tonic-gate 	deflt_fill_default_server_options(options);
2317c478bd9Sstevel@tonic-gate #endif /* HAVE_DEFOPEN */
2327c478bd9Sstevel@tonic-gate 
2337c478bd9Sstevel@tonic-gate 	/* Portable-specific options */
2347c478bd9Sstevel@tonic-gate 	if (options->pam_authentication_via_kbd_int == -1)
2357c478bd9Sstevel@tonic-gate 		options->pam_authentication_via_kbd_int = 0;
2367c478bd9Sstevel@tonic-gate 
2377c478bd9Sstevel@tonic-gate 	/* Standard Options */
2387c478bd9Sstevel@tonic-gate 	if (options->protocol == SSH_PROTO_UNKNOWN)
2397c478bd9Sstevel@tonic-gate 		options->protocol = SSH_PROTO_1|SSH_PROTO_2;
2407c478bd9Sstevel@tonic-gate 	if (options->num_host_key_files == 0) {
2417c478bd9Sstevel@tonic-gate 		/* fill default hostkeys for protocols */
2427c478bd9Sstevel@tonic-gate 		if (options->protocol & SSH_PROTO_1)
2437c478bd9Sstevel@tonic-gate 			options->host_key_files[options->num_host_key_files++] =
2447c478bd9Sstevel@tonic-gate 			    _PATH_HOST_KEY_FILE;
2457c478bd9Sstevel@tonic-gate #ifndef GSSAPI
2467c478bd9Sstevel@tonic-gate 		/* With GSS keyex we can run v2 w/ no host keys */
2477c478bd9Sstevel@tonic-gate 		if (options->protocol & SSH_PROTO_2) {
2487c478bd9Sstevel@tonic-gate 			options->host_key_files[options->num_host_key_files++] =
2497c478bd9Sstevel@tonic-gate 			    _PATH_HOST_RSA_KEY_FILE;
2507c478bd9Sstevel@tonic-gate 			options->host_key_files[options->num_host_key_files++] =
2517c478bd9Sstevel@tonic-gate 			    _PATH_HOST_DSA_KEY_FILE;
2527c478bd9Sstevel@tonic-gate 		}
2537c478bd9Sstevel@tonic-gate #endif /* GSSAPI */
2547c478bd9Sstevel@tonic-gate 	}
2557c478bd9Sstevel@tonic-gate 	if (options->num_ports == 0)
2567c478bd9Sstevel@tonic-gate 		options->ports[options->num_ports++] = SSH_DEFAULT_PORT;
2577c478bd9Sstevel@tonic-gate 	if (options->listen_addrs == NULL)
2587c478bd9Sstevel@tonic-gate 		add_listen_addr(options, NULL, 0);
2597c478bd9Sstevel@tonic-gate 	if (options->pid_file == NULL)
2607c478bd9Sstevel@tonic-gate 		options->pid_file = _PATH_SSH_DAEMON_PID_FILE;
2617c478bd9Sstevel@tonic-gate 	if (options->server_key_bits == -1)
2627c478bd9Sstevel@tonic-gate 		options->server_key_bits = 768;
2637c478bd9Sstevel@tonic-gate 	if (options->login_grace_time == -1)
2647c478bd9Sstevel@tonic-gate 		options->login_grace_time = 120;
2657c478bd9Sstevel@tonic-gate 	if (options->key_regeneration_time == -1)
2667c478bd9Sstevel@tonic-gate 		options->key_regeneration_time = 3600;
2677c478bd9Sstevel@tonic-gate 	if (options->permit_root_login == PERMIT_NOT_SET)
2687c478bd9Sstevel@tonic-gate 		options->permit_root_login = PERMIT_YES;
2697c478bd9Sstevel@tonic-gate 	if (options->ignore_rhosts == -1)
2707c478bd9Sstevel@tonic-gate 		options->ignore_rhosts = 1;
2717c478bd9Sstevel@tonic-gate 	if (options->ignore_user_known_hosts == -1)
2727c478bd9Sstevel@tonic-gate 		options->ignore_user_known_hosts = 0;
2737c478bd9Sstevel@tonic-gate 	if (options->print_motd == -1)
2747c478bd9Sstevel@tonic-gate 		options->print_motd = 1;
2757c478bd9Sstevel@tonic-gate 	if (options->print_lastlog == -1)
2767c478bd9Sstevel@tonic-gate 		options->print_lastlog = 1;
2777c478bd9Sstevel@tonic-gate 	if (options->x11_forwarding == -1)
2787c478bd9Sstevel@tonic-gate 		options->x11_forwarding = 1;
2797c478bd9Sstevel@tonic-gate 	if (options->x11_display_offset == -1)
2807c478bd9Sstevel@tonic-gate 		options->x11_display_offset = 10;
2817c478bd9Sstevel@tonic-gate 	if (options->x11_use_localhost == -1)
2827c478bd9Sstevel@tonic-gate 		options->x11_use_localhost = 1;
2837c478bd9Sstevel@tonic-gate 	if (options->xauth_location == NULL)
2847c478bd9Sstevel@tonic-gate 		options->xauth_location = _PATH_XAUTH;
2857c478bd9Sstevel@tonic-gate 	if (options->strict_modes == -1)
2867c478bd9Sstevel@tonic-gate 		options->strict_modes = 1;
2877c478bd9Sstevel@tonic-gate 	if (options->keepalives == -1)
2887c478bd9Sstevel@tonic-gate 		options->keepalives = 1;
2897c478bd9Sstevel@tonic-gate 	if (options->log_facility == SYSLOG_FACILITY_NOT_SET)
2907c478bd9Sstevel@tonic-gate 		options->log_facility = SYSLOG_FACILITY_AUTH;
2917c478bd9Sstevel@tonic-gate 	if (options->log_level == SYSLOG_LEVEL_NOT_SET)
2927c478bd9Sstevel@tonic-gate 		options->log_level = SYSLOG_LEVEL_INFO;
2937c478bd9Sstevel@tonic-gate 	if (options->rhosts_authentication == -1)
2947c478bd9Sstevel@tonic-gate 		options->rhosts_authentication = 0;
2957c478bd9Sstevel@tonic-gate 	if (options->rhosts_rsa_authentication == -1)
2967c478bd9Sstevel@tonic-gate 		options->rhosts_rsa_authentication = 0;
2977c478bd9Sstevel@tonic-gate 	if (options->hostbased_authentication == -1)
2987c478bd9Sstevel@tonic-gate 		options->hostbased_authentication = 0;
2997c478bd9Sstevel@tonic-gate 	if (options->hostbased_uses_name_from_packet_only == -1)
3007c478bd9Sstevel@tonic-gate 		options->hostbased_uses_name_from_packet_only = 0;
3017c478bd9Sstevel@tonic-gate 	if (options->rsa_authentication == -1)
3027c478bd9Sstevel@tonic-gate 		options->rsa_authentication = 1;
3037c478bd9Sstevel@tonic-gate 	if (options->pubkey_authentication == -1)
3047c478bd9Sstevel@tonic-gate 		options->pubkey_authentication = 1;
3057c478bd9Sstevel@tonic-gate #ifdef GSSAPI
3067c478bd9Sstevel@tonic-gate 	if (options->gss_authentication == -1)
3077c478bd9Sstevel@tonic-gate 		options->gss_authentication = 1;
3087c478bd9Sstevel@tonic-gate 	if (options->gss_keyex == -1)
3097c478bd9Sstevel@tonic-gate 		options->gss_keyex = 1;
3107c478bd9Sstevel@tonic-gate 	if (options->gss_store_creds == -1)
3117c478bd9Sstevel@tonic-gate 		options->gss_store_creds = 1;
3127c478bd9Sstevel@tonic-gate 	if (options->gss_use_session_ccache == -1)
3137c478bd9Sstevel@tonic-gate 		options->gss_use_session_ccache = 1;
3147c478bd9Sstevel@tonic-gate 	if (options->gss_cleanup_creds == -1)
3157c478bd9Sstevel@tonic-gate 		options->gss_cleanup_creds = 1;
3167c478bd9Sstevel@tonic-gate #endif
3177c478bd9Sstevel@tonic-gate #if defined(KRB4) || defined(KRB5)
3187c478bd9Sstevel@tonic-gate 	if (options->kerberos_authentication == -1)
3197c478bd9Sstevel@tonic-gate 		options->kerberos_authentication = 0;
3207c478bd9Sstevel@tonic-gate 	if (options->kerberos_or_local_passwd == -1)
3217c478bd9Sstevel@tonic-gate 		options->kerberos_or_local_passwd = 1;
3227c478bd9Sstevel@tonic-gate 	if (options->kerberos_ticket_cleanup == -1)
3237c478bd9Sstevel@tonic-gate 		options->kerberos_ticket_cleanup = 1;
3247c478bd9Sstevel@tonic-gate #endif
3257c478bd9Sstevel@tonic-gate #if defined(AFS) || defined(KRB5)
3267c478bd9Sstevel@tonic-gate 	if (options->kerberos_tgt_passing == -1)
3277c478bd9Sstevel@tonic-gate 		options->kerberos_tgt_passing = 0;
3287c478bd9Sstevel@tonic-gate #endif
3297c478bd9Sstevel@tonic-gate #ifdef AFS
3307c478bd9Sstevel@tonic-gate 	if (options->afs_token_passing == -1)
3317c478bd9Sstevel@tonic-gate 		options->afs_token_passing = 0;
3327c478bd9Sstevel@tonic-gate #endif
3337c478bd9Sstevel@tonic-gate 	if (options->password_authentication == -1)
3347c478bd9Sstevel@tonic-gate 		options->password_authentication = 1;
3357c478bd9Sstevel@tonic-gate 	if (options->kbd_interactive_authentication == -1)
3367c478bd9Sstevel@tonic-gate 		options->kbd_interactive_authentication = 0;
3377c478bd9Sstevel@tonic-gate 	if (options->challenge_response_authentication == -1)
3387c478bd9Sstevel@tonic-gate 		options->challenge_response_authentication = 1;
3397c478bd9Sstevel@tonic-gate 	if (options->permit_empty_passwd == -1)
3407c478bd9Sstevel@tonic-gate 		options->permit_empty_passwd = 0;
3417c478bd9Sstevel@tonic-gate 	if (options->permit_user_env == -1)
3427c478bd9Sstevel@tonic-gate 		options->permit_user_env = 0;
3437c478bd9Sstevel@tonic-gate 	if (options->compression == -1)
3447c478bd9Sstevel@tonic-gate 		options->compression = 1;
3457c478bd9Sstevel@tonic-gate 	if (options->allow_tcp_forwarding == -1)
3467c478bd9Sstevel@tonic-gate 		options->allow_tcp_forwarding = 1;
3477c478bd9Sstevel@tonic-gate 	if (options->gateway_ports == -1)
3487c478bd9Sstevel@tonic-gate 		options->gateway_ports = 0;
3497c478bd9Sstevel@tonic-gate 	if (options->max_startups == -1)
3507c478bd9Sstevel@tonic-gate 		options->max_startups = 10;
3517c478bd9Sstevel@tonic-gate 	if (options->max_startups_rate == -1)
3527c478bd9Sstevel@tonic-gate 		options->max_startups_rate = 100;		/* 100% */
3537c478bd9Sstevel@tonic-gate 	if (options->max_startups_begin == -1)
3547c478bd9Sstevel@tonic-gate 		options->max_startups_begin = options->max_startups;
3557c478bd9Sstevel@tonic-gate 	if (options->verify_reverse_mapping == -1)
3567c478bd9Sstevel@tonic-gate 		options->verify_reverse_mapping = 0;
3577c478bd9Sstevel@tonic-gate 	if (options->client_alive_interval == -1)
3587c478bd9Sstevel@tonic-gate 		options->client_alive_interval = 0;
3597c478bd9Sstevel@tonic-gate 	if (options->client_alive_count_max == -1)
3607c478bd9Sstevel@tonic-gate 		options->client_alive_count_max = 3;
3617c478bd9Sstevel@tonic-gate 	if (options->authorized_keys_file2 == NULL) {
3627c478bd9Sstevel@tonic-gate 		/* authorized_keys_file2 falls back to authorized_keys_file */
3637c478bd9Sstevel@tonic-gate 		if (options->authorized_keys_file != NULL)
3647c478bd9Sstevel@tonic-gate 			options->authorized_keys_file2 = options->authorized_keys_file;
3657c478bd9Sstevel@tonic-gate 		else
3667c478bd9Sstevel@tonic-gate 			options->authorized_keys_file2 = _PATH_SSH_USER_PERMITTED_KEYS2;
3677c478bd9Sstevel@tonic-gate 	}
3687c478bd9Sstevel@tonic-gate 	if (options->authorized_keys_file == NULL)
3697c478bd9Sstevel@tonic-gate 		options->authorized_keys_file = _PATH_SSH_USER_PERMITTED_KEYS;
3707c478bd9Sstevel@tonic-gate 
3717c478bd9Sstevel@tonic-gate 	if (options->max_auth_tries == -1)
3727c478bd9Sstevel@tonic-gate 		options->max_auth_tries = AUTH_FAIL_MAX;
3737c478bd9Sstevel@tonic-gate 	if (options->max_auth_tries_log == -1)
3747c478bd9Sstevel@tonic-gate 		options->max_auth_tries_log = options->max_auth_tries / 2;
3757c478bd9Sstevel@tonic-gate 
3767c478bd9Sstevel@tonic-gate 	if (options->max_init_auth_tries == -1)
3777c478bd9Sstevel@tonic-gate 		options->max_init_auth_tries = AUTH_FAIL_MAX;
3787c478bd9Sstevel@tonic-gate 	if (options->max_init_auth_tries_log == -1)
3797c478bd9Sstevel@tonic-gate 		options->max_init_auth_tries_log = options->max_init_auth_tries / 2;
3807c478bd9Sstevel@tonic-gate 
3817c478bd9Sstevel@tonic-gate 	if (options->lookup_client_hostnames == -1)
3827c478bd9Sstevel@tonic-gate 		options->lookup_client_hostnames = 1;
383cd7d5fafSJan Pechanec 	if (options->use_openssl_engine == -1)
384cd7d5fafSJan Pechanec 		options->use_openssl_engine = 1;
3857c478bd9Sstevel@tonic-gate }
3867c478bd9Sstevel@tonic-gate 
3877c478bd9Sstevel@tonic-gate /* Keyword tokens. */
3887c478bd9Sstevel@tonic-gate typedef enum {
3897c478bd9Sstevel@tonic-gate 	sBadOption,		/* == unknown option */
3907c478bd9Sstevel@tonic-gate 	/* Portable-specific options */
3917c478bd9Sstevel@tonic-gate 	sPAMAuthenticationViaKbdInt,
3927c478bd9Sstevel@tonic-gate 	/* Standard Options */
3937c478bd9Sstevel@tonic-gate 	sPort, sHostKeyFile, sServerKeyBits, sLoginGraceTime, sKeyRegenerationTime,
3947c478bd9Sstevel@tonic-gate 	sPermitRootLogin, sLogFacility, sLogLevel,
3957c478bd9Sstevel@tonic-gate 	sRhostsAuthentication, sRhostsRSAAuthentication, sRSAAuthentication,
3967c478bd9Sstevel@tonic-gate #ifdef GSSAPI
3977c478bd9Sstevel@tonic-gate 	sGssAuthentication, sGssKeyEx, sGssStoreDelegCreds,
3987c478bd9Sstevel@tonic-gate 	sGssUseSessionCredCache, sGssCleanupCreds,
3997c478bd9Sstevel@tonic-gate #endif /* GSSAPI */
4007c478bd9Sstevel@tonic-gate #if defined(KRB4) || defined(KRB5)
4017c478bd9Sstevel@tonic-gate 	sKerberosAuthentication, sKerberosOrLocalPasswd, sKerberosTicketCleanup,
4027c478bd9Sstevel@tonic-gate #endif
4037c478bd9Sstevel@tonic-gate #if defined(AFS) || defined(KRB5)
4047c478bd9Sstevel@tonic-gate 	sKerberosTgtPassing,
4057c478bd9Sstevel@tonic-gate #endif
4067c478bd9Sstevel@tonic-gate #ifdef AFS
4077c478bd9Sstevel@tonic-gate 	sAFSTokenPassing,
4087c478bd9Sstevel@tonic-gate #endif
4097c478bd9Sstevel@tonic-gate 	sChallengeResponseAuthentication,
4107c478bd9Sstevel@tonic-gate 	sPasswordAuthentication, sKbdInteractiveAuthentication, sListenAddress,
4117c478bd9Sstevel@tonic-gate 	sPrintMotd, sPrintLastLog, sIgnoreRhosts,
4127c478bd9Sstevel@tonic-gate 	sX11Forwarding, sX11DisplayOffset, sX11UseLocalhost,
4137c478bd9Sstevel@tonic-gate 	sStrictModes, sEmptyPasswd, sKeepAlives,
4147c478bd9Sstevel@tonic-gate 	sPermitUserEnvironment, sUseLogin, sAllowTcpForwarding, sCompression,
4157c478bd9Sstevel@tonic-gate 	sAllowUsers, sDenyUsers, sAllowGroups, sDenyGroups,
4167c478bd9Sstevel@tonic-gate 	sIgnoreUserKnownHosts, sCiphers, sMacs, sProtocol, sPidFile,
4177c478bd9Sstevel@tonic-gate 	sGatewayPorts, sPubkeyAuthentication, sXAuthLocation, sSubsystem, sMaxStartups,
4187c478bd9Sstevel@tonic-gate 	sBanner, sVerifyReverseMapping, sHostbasedAuthentication,
4197c478bd9Sstevel@tonic-gate 	sHostbasedUsesNameFromPacketOnly, sClientAliveInterval,
4207c478bd9Sstevel@tonic-gate 	sClientAliveCountMax, sAuthorizedKeysFile, sAuthorizedKeysFile2,
4217c478bd9Sstevel@tonic-gate 	sMaxAuthTries, sMaxAuthTriesLog, sUsePrivilegeSeparation,
4226f8d59d8SJan Pechanec 	sLookupClientHostnames, sUseOpenSSLEngine, sChrootDirectory,
423b07b2f5cSHuie-Ying Lee 	sMatch,
4247c478bd9Sstevel@tonic-gate 	sDeprecated
4257c478bd9Sstevel@tonic-gate } ServerOpCodes;
4267c478bd9Sstevel@tonic-gate 
427b07b2f5cSHuie-Ying Lee #define SSHCFG_GLOBAL	0x01	/* allowed in main section of sshd_config */
428b07b2f5cSHuie-Ying Lee #define SSHCFG_MATCH	0x02	/* allowed inside a Match section */
429b07b2f5cSHuie-Ying Lee #define SSHCFG_ALL	(SSHCFG_GLOBAL|SSHCFG_MATCH)
430b07b2f5cSHuie-Ying Lee 
4317c478bd9Sstevel@tonic-gate /* Textual representation of the tokens. */
4327c478bd9Sstevel@tonic-gate static struct {
4337c478bd9Sstevel@tonic-gate 	const char *name;
4347c478bd9Sstevel@tonic-gate 	ServerOpCodes opcode;
435b07b2f5cSHuie-Ying Lee 	u_int flags;
4367c478bd9Sstevel@tonic-gate } keywords[] = {
4377c478bd9Sstevel@tonic-gate 	/* Portable-specific options */
438b07b2f5cSHuie-Ying Lee 	{ "PAMAuthenticationViaKbdInt", sPAMAuthenticationViaKbdInt, SSHCFG_GLOBAL },
4397c478bd9Sstevel@tonic-gate 	/* Standard Options */
440b07b2f5cSHuie-Ying Lee 	{ "port", sPort, SSHCFG_GLOBAL },
441b07b2f5cSHuie-Ying Lee 	{ "hostkey", sHostKeyFile, SSHCFG_GLOBAL },
442b07b2f5cSHuie-Ying Lee 	{ "hostdsakey", sHostKeyFile, SSHCFG_GLOBAL },			/* alias */
443b07b2f5cSHuie-Ying Lee 	{ "pidfile", sPidFile, SSHCFG_GLOBAL },
444b07b2f5cSHuie-Ying Lee 	{ "serverkeybits", sServerKeyBits, SSHCFG_GLOBAL },
445b07b2f5cSHuie-Ying Lee 	{ "logingracetime", sLoginGraceTime, SSHCFG_GLOBAL },
446b07b2f5cSHuie-Ying Lee 	{ "keyregenerationinterval", sKeyRegenerationTime, SSHCFG_GLOBAL },
447b07b2f5cSHuie-Ying Lee 	{ "permitrootlogin", sPermitRootLogin, SSHCFG_ALL },
448b07b2f5cSHuie-Ying Lee 	{ "syslogfacility", sLogFacility, SSHCFG_GLOBAL },
449b07b2f5cSHuie-Ying Lee 	{ "loglevel", sLogLevel, SSHCFG_GLOBAL },
450b07b2f5cSHuie-Ying Lee 	{ "rhostsauthentication", sRhostsAuthentication, SSHCFG_GLOBAL },
451b07b2f5cSHuie-Ying Lee 	{ "rhostsrsaauthentication", sRhostsRSAAuthentication, SSHCFG_ALL },
452b07b2f5cSHuie-Ying Lee 	{ "hostbasedauthentication", sHostbasedAuthentication, SSHCFG_ALL },
4537c478bd9Sstevel@tonic-gate 	{ "hostbasedusesnamefrompacketonly", sHostbasedUsesNameFromPacketOnly },
454b07b2f5cSHuie-Ying Lee 	{ "rsaauthentication", sRSAAuthentication, SSHCFG_ALL },
455b07b2f5cSHuie-Ying Lee 	{ "pubkeyauthentication", sPubkeyAuthentication, SSHCFG_ALL },
456b07b2f5cSHuie-Ying Lee 	{ "dsaauthentication", sPubkeyAuthentication, SSHCFG_GLOBAL },	/* alias */
4577c478bd9Sstevel@tonic-gate #ifdef GSSAPI
458b07b2f5cSHuie-Ying Lee 	{ "gssapiauthentication", sGssAuthentication, SSHCFG_ALL },
459b07b2f5cSHuie-Ying Lee 	{ "gssapikeyexchange", sGssKeyEx,   SSHCFG_GLOBAL },
460b07b2f5cSHuie-Ying Lee 	{ "gssapistoredelegatedcredentials", sGssStoreDelegCreds, SSHCFG_GLOBAL },
461b07b2f5cSHuie-Ying Lee 	{ "gssauthentication", sGssAuthentication, SSHCFG_GLOBAL },	/* alias */
462b07b2f5cSHuie-Ying Lee 	{ "gsskeyex", sGssKeyEx, SSHCFG_GLOBAL },	/* alias */
463b07b2f5cSHuie-Ying Lee 	{ "gssstoredelegcreds", sGssStoreDelegCreds, SSHCFG_GLOBAL },	/* alias */
4647c478bd9Sstevel@tonic-gate #ifndef SUNW_GSSAPI
465b07b2f5cSHuie-Ying Lee 	{ "gssusesessionccache", sGssUseSessionCredCache, SSHCFG_GLOBAL },
466b07b2f5cSHuie-Ying Lee 	{ "gssusesessioncredcache", sGssUseSessionCredCache, SSHCFG_GLOBAL },
467b07b2f5cSHuie-Ying Lee 	{ "gsscleanupcreds", sGssCleanupCreds, SSHCFG_GLOBAL },
4687c478bd9Sstevel@tonic-gate #endif /* SUNW_GSSAPI */
4697c478bd9Sstevel@tonic-gate #endif
4707c478bd9Sstevel@tonic-gate #if defined(KRB4) || defined(KRB5)
471b07b2f5cSHuie-Ying Lee 	{ "kerberosauthentication", sKerberosAuthentication, SSHCFG_ALL },
472b07b2f5cSHuie-Ying Lee 	{ "kerberosorlocalpasswd", sKerberosOrLocalPasswd, SSHCFG_GLOBAL },
473b07b2f5cSHuie-Ying Lee 	{ "kerberosticketcleanup", sKerberosTicketCleanup, SSHCFG_GLOBAL },
4747c478bd9Sstevel@tonic-gate #endif
4757c478bd9Sstevel@tonic-gate #if defined(AFS) || defined(KRB5)
476b07b2f5cSHuie-Ying Lee 	{ "kerberostgtpassing", sKerberosTgtPassing, SSHCFG_GLOBAL },
4777c478bd9Sstevel@tonic-gate #endif
4787c478bd9Sstevel@tonic-gate #ifdef AFS
479b07b2f5cSHuie-Ying Lee 	{ "afstokenpassing", sAFSTokenPassing, SSHCFG_GLOBAL },
4807c478bd9Sstevel@tonic-gate #endif
481b07b2f5cSHuie-Ying Lee 	{ "passwordauthentication", sPasswordAuthentication, SSHCFG_ALL },
482b07b2f5cSHuie-Ying Lee 	{ "kbdinteractiveauthentication", sKbdInteractiveAuthentication, SSHCFG_ALL },
483b07b2f5cSHuie-Ying Lee 	{ "challengeresponseauthentication", sChallengeResponseAuthentication, SSHCFG_GLOBAL },
484b07b2f5cSHuie-Ying Lee 	{ "skeyauthentication", sChallengeResponseAuthentication, SSHCFG_GLOBAL }, /* alias */
485b07b2f5cSHuie-Ying Lee 	{ "checkmail", sDeprecated, SSHCFG_GLOBAL },
486b07b2f5cSHuie-Ying Lee 	{ "listenaddress", sListenAddress, SSHCFG_GLOBAL },
487b07b2f5cSHuie-Ying Lee 	{ "printmotd", sPrintMotd, SSHCFG_GLOBAL },
488b07b2f5cSHuie-Ying Lee 	{ "printlastlog", sPrintLastLog, SSHCFG_GLOBAL },
489b07b2f5cSHuie-Ying Lee 	{ "ignorerhosts", sIgnoreRhosts, SSHCFG_GLOBAL },
490b07b2f5cSHuie-Ying Lee 	{ "ignoreuserknownhosts", sIgnoreUserKnownHosts, SSHCFG_GLOBAL },
491b07b2f5cSHuie-Ying Lee 	{ "x11forwarding", sX11Forwarding, SSHCFG_ALL },
492b07b2f5cSHuie-Ying Lee 	{ "x11displayoffset", sX11DisplayOffset, SSHCFG_ALL },
493b07b2f5cSHuie-Ying Lee 	{ "x11uselocalhost", sX11UseLocalhost, SSHCFG_ALL },
494b07b2f5cSHuie-Ying Lee 	{ "xauthlocation", sXAuthLocation, SSHCFG_GLOBAL },
495b07b2f5cSHuie-Ying Lee 	{ "strictmodes", sStrictModes, SSHCFG_GLOBAL },
496b07b2f5cSHuie-Ying Lee 	{ "permitemptypasswords", sEmptyPasswd, SSHCFG_ALL },
497b07b2f5cSHuie-Ying Lee 	{ "permituserenvironment", sPermitUserEnvironment, SSHCFG_GLOBAL },
498b07b2f5cSHuie-Ying Lee 	{ "uselogin", sUseLogin, SSHCFG_GLOBAL },
499b07b2f5cSHuie-Ying Lee 	{ "compression", sCompression, SSHCFG_GLOBAL },
500b07b2f5cSHuie-Ying Lee 	{ "keepalive", sKeepAlives, SSHCFG_GLOBAL },
501b07b2f5cSHuie-Ying Lee 	{ "allowtcpforwarding", sAllowTcpForwarding, SSHCFG_ALL },
502b07b2f5cSHuie-Ying Lee 	{ "allowusers", sAllowUsers, SSHCFG_GLOBAL },
503b07b2f5cSHuie-Ying Lee 	{ "denyusers", sDenyUsers, SSHCFG_GLOBAL },
504b07b2f5cSHuie-Ying Lee 	{ "allowgroups", sAllowGroups, SSHCFG_GLOBAL },
505b07b2f5cSHuie-Ying Lee 	{ "denygroups", sDenyGroups, SSHCFG_GLOBAL },
506b07b2f5cSHuie-Ying Lee 	{ "ciphers", sCiphers, SSHCFG_GLOBAL },
507b07b2f5cSHuie-Ying Lee 	{ "macs", sMacs, SSHCFG_GLOBAL},
508b07b2f5cSHuie-Ying Lee 	{ "protocol", sProtocol,SSHCFG_GLOBAL },
509b07b2f5cSHuie-Ying Lee 	{ "gatewayports", sGatewayPorts, SSHCFG_ALL },
510b07b2f5cSHuie-Ying Lee 	{ "subsystem", sSubsystem, SSHCFG_GLOBAL},
511b07b2f5cSHuie-Ying Lee 	{ "maxstartups", sMaxStartups, SSHCFG_GLOBAL },
512b07b2f5cSHuie-Ying Lee 	{ "banner", sBanner, SSHCFG_ALL },
513b07b2f5cSHuie-Ying Lee 	{ "verifyreversemapping", sVerifyReverseMapping, SSHCFG_GLOBAL },
514b07b2f5cSHuie-Ying Lee 	{ "reversemappingcheck", sVerifyReverseMapping,SSHCFG_GLOBAL },
515b07b2f5cSHuie-Ying Lee 	{ "clientaliveinterval", sClientAliveInterval, SSHCFG_GLOBAL },
516b07b2f5cSHuie-Ying Lee 	{ "clientalivecountmax", sClientAliveCountMax, SSHCFG_GLOBAL },
517b07b2f5cSHuie-Ying Lee 	{ "authorizedkeysfile", sAuthorizedKeysFile, SSHCFG_GLOBAL },
518b07b2f5cSHuie-Ying Lee 	{ "authorizedkeysfile2", sAuthorizedKeysFile2, SSHCFG_GLOBAL },
519b07b2f5cSHuie-Ying Lee 	{ "maxauthtries", sMaxAuthTries, SSHCFG_ALL },
520b07b2f5cSHuie-Ying Lee 	{ "maxauthtrieslog", sMaxAuthTriesLog, SSHCFG_GLOBAL },
521b07b2f5cSHuie-Ying Lee 	{ "useprivilegeseparation", sUsePrivilegeSeparation, SSHCFG_GLOBAL },
522b07b2f5cSHuie-Ying Lee 	{ "lookupclienthostnames", sLookupClientHostnames, SSHCFG_GLOBAL },
523b07b2f5cSHuie-Ying Lee 	{ "useopensslengine", sUseOpenSSLEngine, SSHCFG_GLOBAL },
524b07b2f5cSHuie-Ying Lee 	{ "chrootdirectory", sChrootDirectory, SSHCFG_ALL },
525b07b2f5cSHuie-Ying Lee 	{ "match", sMatch, SSHCFG_ALL },
526b07b2f5cSHuie-Ying Lee 
527b07b2f5cSHuie-Ying Lee 	{ NULL, sBadOption, 0 }
5287c478bd9Sstevel@tonic-gate };
5297c478bd9Sstevel@tonic-gate 
5307c478bd9Sstevel@tonic-gate /*
5317c478bd9Sstevel@tonic-gate  * Returns the number of the token pointed to by cp or sBadOption.
5327c478bd9Sstevel@tonic-gate  */
5337c478bd9Sstevel@tonic-gate 
5347c478bd9Sstevel@tonic-gate static ServerOpCodes
5357c478bd9Sstevel@tonic-gate parse_token(const char *cp, const char *filename,
536b07b2f5cSHuie-Ying Lee 	    int linenum, u_int *flags)
5377c478bd9Sstevel@tonic-gate {
5387c478bd9Sstevel@tonic-gate 	u_int i;
5397c478bd9Sstevel@tonic-gate 
5407c478bd9Sstevel@tonic-gate 	for (i = 0; keywords[i].name; i++)
541b07b2f5cSHuie-Ying Lee 		if (strcasecmp(cp, keywords[i].name) == 0) {
542b07b2f5cSHuie-Ying Lee 			*flags = keywords[i].flags;
5437c478bd9Sstevel@tonic-gate 			return keywords[i].opcode;
544b07b2f5cSHuie-Ying Lee 		}
5457c478bd9Sstevel@tonic-gate 
5467c478bd9Sstevel@tonic-gate 	error("%s: line %d: Bad configuration option: %s",
5477c478bd9Sstevel@tonic-gate 	    filename, linenum, cp);
5487c478bd9Sstevel@tonic-gate 	return sBadOption;
5497c478bd9Sstevel@tonic-gate }
5507c478bd9Sstevel@tonic-gate 
5517c478bd9Sstevel@tonic-gate static void
5527c478bd9Sstevel@tonic-gate add_listen_addr(ServerOptions *options, char *addr, u_short port)
5537c478bd9Sstevel@tonic-gate {
5547c478bd9Sstevel@tonic-gate 	int i;
5557c478bd9Sstevel@tonic-gate 
5567c478bd9Sstevel@tonic-gate 	if (options->num_ports == 0)
5577c478bd9Sstevel@tonic-gate 		options->ports[options->num_ports++] = SSH_DEFAULT_PORT;
5587c478bd9Sstevel@tonic-gate 	if (port == 0)
5597c478bd9Sstevel@tonic-gate 		for (i = 0; i < options->num_ports; i++)
5607c478bd9Sstevel@tonic-gate 			add_one_listen_addr(options, addr, options->ports[i]);
5617c478bd9Sstevel@tonic-gate 	else
5627c478bd9Sstevel@tonic-gate 		add_one_listen_addr(options, addr, port);
5637c478bd9Sstevel@tonic-gate }
5647c478bd9Sstevel@tonic-gate 
5657c478bd9Sstevel@tonic-gate static void
5667c478bd9Sstevel@tonic-gate add_one_listen_addr(ServerOptions *options, char *addr, u_short port)
5677c478bd9Sstevel@tonic-gate {
5687c478bd9Sstevel@tonic-gate 	struct addrinfo hints, *ai, *aitop;
5697c478bd9Sstevel@tonic-gate 	char strport[NI_MAXSERV];
5707c478bd9Sstevel@tonic-gate 	int gaierr;
5717c478bd9Sstevel@tonic-gate 
5727c478bd9Sstevel@tonic-gate 	(void) memset(&hints, 0, sizeof(hints));
5737c478bd9Sstevel@tonic-gate 	hints.ai_family = IPv4or6;
5747c478bd9Sstevel@tonic-gate 	hints.ai_socktype = SOCK_STREAM;
5757c478bd9Sstevel@tonic-gate 	hints.ai_flags = (addr == NULL) ? AI_PASSIVE : 0;
5767c478bd9Sstevel@tonic-gate 	(void) snprintf(strport, sizeof strport, "%u", port);
5777c478bd9Sstevel@tonic-gate 	if ((gaierr = getaddrinfo(addr, strport, &hints, &aitop)) != 0)
5787c478bd9Sstevel@tonic-gate 		fatal("bad addr or host: %s (%s)",
5797c478bd9Sstevel@tonic-gate 		    addr ? addr : "<NULL>",
5807c478bd9Sstevel@tonic-gate 		    gai_strerror(gaierr));
5817c478bd9Sstevel@tonic-gate 	for (ai = aitop; ai->ai_next; ai = ai->ai_next)
5827c478bd9Sstevel@tonic-gate 		;
5837c478bd9Sstevel@tonic-gate 	ai->ai_next = options->listen_addrs;
5847c478bd9Sstevel@tonic-gate 	options->listen_addrs = aitop;
5857c478bd9Sstevel@tonic-gate }
5867c478bd9Sstevel@tonic-gate 
587b07b2f5cSHuie-Ying Lee /*
588b07b2f5cSHuie-Ying Lee  * The strategy for the Match blocks is that the config file is parsed twice.
589b07b2f5cSHuie-Ying Lee  *
590b07b2f5cSHuie-Ying Lee  * The first time is at startup.  activep is initialized to 1 and the
591b07b2f5cSHuie-Ying Lee  * directives in the global context are processed and acted on.  Hitting a
592b07b2f5cSHuie-Ying Lee  * Match directive unsets activep and the directives inside the block are
593b07b2f5cSHuie-Ying Lee  * checked for syntax only.
594b07b2f5cSHuie-Ying Lee  *
595b07b2f5cSHuie-Ying Lee  * The second time is after a connection has been established but before
596b07b2f5cSHuie-Ying Lee  * authentication.  activep is initialized to 2 and global config directives
597b07b2f5cSHuie-Ying Lee  * are ignored since they have already been processed.  If the criteria in a
598b07b2f5cSHuie-Ying Lee  * Match block is met, activep is set and the subsequent directives
599b07b2f5cSHuie-Ying Lee  * processed and actioned until EOF or another Match block unsets it.  Any
600b07b2f5cSHuie-Ying Lee  * options set are copied into the main server config.
601b07b2f5cSHuie-Ying Lee  *
602b07b2f5cSHuie-Ying Lee  * Potential additions/improvements:
603b07b2f5cSHuie-Ying Lee  *  - Add Match support for pre-kex directives, eg Protocol, Ciphers.
604b07b2f5cSHuie-Ying Lee  *
605b07b2f5cSHuie-Ying Lee  *  - Add a Tag directive (idea from David Leonard) ala pf, eg:
606b07b2f5cSHuie-Ying Lee  *	Match Address 192.168.0.*
607b07b2f5cSHuie-Ying Lee  *		Tag trusted
608b07b2f5cSHuie-Ying Lee  *	Match Group wheel
609b07b2f5cSHuie-Ying Lee  *		Tag trusted
610b07b2f5cSHuie-Ying Lee  *	Match Tag trusted
611b07b2f5cSHuie-Ying Lee  *		AllowTcpForwarding yes
612b07b2f5cSHuie-Ying Lee  *		GatewayPorts clientspecified
613b07b2f5cSHuie-Ying Lee  *		[...]
614b07b2f5cSHuie-Ying Lee  *
615b07b2f5cSHuie-Ying Lee  *  - Add a PermittedChannelRequests directive
616b07b2f5cSHuie-Ying Lee  *	Match Group shell
617b07b2f5cSHuie-Ying Lee  *		PermittedChannelRequests session,forwarded-tcpip
618b07b2f5cSHuie-Ying Lee  */
619b07b2f5cSHuie-Ying Lee 
620b07b2f5cSHuie-Ying Lee static int
621b07b2f5cSHuie-Ying Lee match_cfg_line_group(const char *grps, int line, const char *user)
622b07b2f5cSHuie-Ying Lee {
623b07b2f5cSHuie-Ying Lee 	int result = 0;
624b07b2f5cSHuie-Ying Lee 	struct passwd *pw;
625b07b2f5cSHuie-Ying Lee 
626b07b2f5cSHuie-Ying Lee 	if (user == NULL)
627b07b2f5cSHuie-Ying Lee 		goto out;
628b07b2f5cSHuie-Ying Lee 
629b07b2f5cSHuie-Ying Lee 	if ((pw = getpwnam(user)) == NULL) {
630b07b2f5cSHuie-Ying Lee 		debug("Can't match group at line %d because user %.100s does "
631b07b2f5cSHuie-Ying Lee 		    "not exist", line, user);
632b07b2f5cSHuie-Ying Lee 	} else if (ga_init(pw->pw_name, pw->pw_gid) == 0) {
633b07b2f5cSHuie-Ying Lee 		debug("Can't Match group because user %.100s not in any group "
634b07b2f5cSHuie-Ying Lee 		    "at line %d", user, line);
635b07b2f5cSHuie-Ying Lee 	} else if (ga_match_pattern_list(grps) != 1) {
636b07b2f5cSHuie-Ying Lee 		debug("user %.100s does not match group list %.100s at line %d",
637b07b2f5cSHuie-Ying Lee 		    user, grps, line);
638b07b2f5cSHuie-Ying Lee 	} else {
639b07b2f5cSHuie-Ying Lee 		debug("user %.100s matched group list %.100s at line %d", user,
640b07b2f5cSHuie-Ying Lee 		    grps, line);
641b07b2f5cSHuie-Ying Lee 		result = 1;
642b07b2f5cSHuie-Ying Lee 	}
643b07b2f5cSHuie-Ying Lee out:
644b07b2f5cSHuie-Ying Lee 	ga_free();
645b07b2f5cSHuie-Ying Lee 	return result;
646b07b2f5cSHuie-Ying Lee }
647b07b2f5cSHuie-Ying Lee 
648b07b2f5cSHuie-Ying Lee static int
649b07b2f5cSHuie-Ying Lee match_cfg_line(char **condition, int line, const char *user, const char *host,
650b07b2f5cSHuie-Ying Lee     const char *address)
651b07b2f5cSHuie-Ying Lee {
652b07b2f5cSHuie-Ying Lee 	int result = 1;
653b07b2f5cSHuie-Ying Lee 	char *arg, *attrib, *cp = *condition;
654b07b2f5cSHuie-Ying Lee 	size_t len;
655b07b2f5cSHuie-Ying Lee 
656b07b2f5cSHuie-Ying Lee 	if (user == NULL)
657b07b2f5cSHuie-Ying Lee 		debug3("checking syntax for 'Match %s'", cp);
658b07b2f5cSHuie-Ying Lee 	else
659b07b2f5cSHuie-Ying Lee 		debug3("checking match for '%s' user %s host %s addr %s", cp,
660b07b2f5cSHuie-Ying Lee 		    user ? user : "(null)", host ? host : "(null)",
661b07b2f5cSHuie-Ying Lee 		    address ? address : "(null)");
662b07b2f5cSHuie-Ying Lee 
663*56e7e6c6SHuie-Ying Lee 	while ((attrib = strdelim(&cp)) != NULL && *attrib != '\0') {
664b07b2f5cSHuie-Ying Lee 		if ((arg = strdelim(&cp)) == NULL || *arg == '\0') {
665b07b2f5cSHuie-Ying Lee 			error("Missing Match criteria for %s", attrib);
666b07b2f5cSHuie-Ying Lee 			return -1;
667b07b2f5cSHuie-Ying Lee 		}
668b07b2f5cSHuie-Ying Lee 		len = strlen(arg);
669b07b2f5cSHuie-Ying Lee 		if (strcasecmp(attrib, "user") == 0) {
670b07b2f5cSHuie-Ying Lee 			if (!user) {
671b07b2f5cSHuie-Ying Lee 				result = 0;
672b07b2f5cSHuie-Ying Lee 				continue;
673b07b2f5cSHuie-Ying Lee 			}
674b07b2f5cSHuie-Ying Lee 			if (match_pattern_list(user, arg, len, 0) != 1)
675b07b2f5cSHuie-Ying Lee 				result = 0;
676b07b2f5cSHuie-Ying Lee 			else
677b07b2f5cSHuie-Ying Lee 				debug("user %.100s matched 'User %.100s' at "
678b07b2f5cSHuie-Ying Lee 				    "line %d", user, arg, line);
679b07b2f5cSHuie-Ying Lee 		} else if (strcasecmp(attrib, "group") == 0) {
680b07b2f5cSHuie-Ying Lee 			switch (match_cfg_line_group(arg, line, user)) {
681b07b2f5cSHuie-Ying Lee 			case -1:
682b07b2f5cSHuie-Ying Lee 				return -1;
683b07b2f5cSHuie-Ying Lee 			case 0:
684b07b2f5cSHuie-Ying Lee 				result = 0;
685b07b2f5cSHuie-Ying Lee 			}
686b07b2f5cSHuie-Ying Lee 		} else if (strcasecmp(attrib, "host") == 0) {
687b07b2f5cSHuie-Ying Lee 			if (!host) {
688b07b2f5cSHuie-Ying Lee 				result = 0;
689b07b2f5cSHuie-Ying Lee 				continue;
690b07b2f5cSHuie-Ying Lee 			}
691b07b2f5cSHuie-Ying Lee 			if (match_hostname(host, arg, len) != 1)
692b07b2f5cSHuie-Ying Lee 				result = 0;
693b07b2f5cSHuie-Ying Lee 			else
694b07b2f5cSHuie-Ying Lee 				debug("connection from %.100s matched 'Host "
695b07b2f5cSHuie-Ying Lee 				    "%.100s' at line %d", host, arg, line);
696b07b2f5cSHuie-Ying Lee 		} else if (strcasecmp(attrib, "address") == 0) {
697b07b2f5cSHuie-Ying Lee 			switch (addr_match_list(address, arg)) {
698b07b2f5cSHuie-Ying Lee 			case 1:
699b07b2f5cSHuie-Ying Lee 				debug("connection from %.100s matched 'Address "
700b07b2f5cSHuie-Ying Lee 				    "%.100s' at line %d", address, arg, line);
701b07b2f5cSHuie-Ying Lee 				break;
702b07b2f5cSHuie-Ying Lee 			case 0:
703b07b2f5cSHuie-Ying Lee 			case -1:
704b07b2f5cSHuie-Ying Lee 				result = 0;
705b07b2f5cSHuie-Ying Lee 				break;
706b07b2f5cSHuie-Ying Lee 			case -2:
707b07b2f5cSHuie-Ying Lee 				return -1;
708b07b2f5cSHuie-Ying Lee 			}
709b07b2f5cSHuie-Ying Lee 		} else {
710b07b2f5cSHuie-Ying Lee 			error("Unsupported Match attribute %s", attrib);
711b07b2f5cSHuie-Ying Lee 			return -1;
712b07b2f5cSHuie-Ying Lee 		}
713b07b2f5cSHuie-Ying Lee 	}
714b07b2f5cSHuie-Ying Lee 	if (user != NULL)
715b07b2f5cSHuie-Ying Lee 		debug3("match %sfound", result ? "" : "not ");
716b07b2f5cSHuie-Ying Lee 	*condition = cp;
717b07b2f5cSHuie-Ying Lee 	return result;
718b07b2f5cSHuie-Ying Lee }
719b07b2f5cSHuie-Ying Lee 
720b07b2f5cSHuie-Ying Lee #define WHITESPACE " \t\r\n"
721b07b2f5cSHuie-Ying Lee 
7227c478bd9Sstevel@tonic-gate int
7237c478bd9Sstevel@tonic-gate process_server_config_line(ServerOptions *options, char *line,
724b07b2f5cSHuie-Ying Lee     const char *filename, int linenum, int *activep, const char *user,
725b07b2f5cSHuie-Ying Lee     const char *host, const char *address)
7267c478bd9Sstevel@tonic-gate {
7277c478bd9Sstevel@tonic-gate 	char *cp, **charptr, *arg, *p;
728b07b2f5cSHuie-Ying Lee 	int cmdline = 0, *intptr, value, n;
7297c478bd9Sstevel@tonic-gate 	ServerOpCodes opcode;
730b07b2f5cSHuie-Ying Lee 	u_int i, flags = 0;
7316f8d59d8SJan Pechanec 	size_t len;
7327c478bd9Sstevel@tonic-gate 
7337c478bd9Sstevel@tonic-gate 	cp = line;
7347c478bd9Sstevel@tonic-gate 	arg = strdelim(&cp);
7357c478bd9Sstevel@tonic-gate 	/* Ignore leading whitespace */
7367c478bd9Sstevel@tonic-gate 	if (*arg == '\0')
7377c478bd9Sstevel@tonic-gate 		arg = strdelim(&cp);
7387c478bd9Sstevel@tonic-gate 	if (!arg || !*arg || *arg == '#')
7397c478bd9Sstevel@tonic-gate 		return 0;
7407c478bd9Sstevel@tonic-gate 	intptr = NULL;
7417c478bd9Sstevel@tonic-gate 	charptr = NULL;
742b07b2f5cSHuie-Ying Lee 	opcode = parse_token(arg, filename, linenum, &flags);
743b07b2f5cSHuie-Ying Lee 
744b07b2f5cSHuie-Ying Lee 	if (activep == NULL) { /* We are processing a command line directive */
745b07b2f5cSHuie-Ying Lee 		cmdline = 1;
746b07b2f5cSHuie-Ying Lee 		activep = &cmdline;
747b07b2f5cSHuie-Ying Lee 	}
748b07b2f5cSHuie-Ying Lee 	if (*activep && opcode != sMatch)
749b07b2f5cSHuie-Ying Lee 		debug3("%s:%d setting %s %s", filename, linenum, arg, cp);
750b07b2f5cSHuie-Ying Lee 	if (*activep == 0 && !(flags & SSHCFG_MATCH)) {
751b07b2f5cSHuie-Ying Lee 		if (user == NULL) {
752b07b2f5cSHuie-Ying Lee 			fatal("%s line %d: Directive '%s' is not allowed "
753b07b2f5cSHuie-Ying Lee 			    "within a Match block", filename, linenum, arg);
754b07b2f5cSHuie-Ying Lee 		} else { /* this is a directive we have already processed */
755b07b2f5cSHuie-Ying Lee 			while (arg)
756b07b2f5cSHuie-Ying Lee 				arg = strdelim(&cp);
757b07b2f5cSHuie-Ying Lee 			return 0;
758b07b2f5cSHuie-Ying Lee 		}
759b07b2f5cSHuie-Ying Lee 	}
760b07b2f5cSHuie-Ying Lee 
7617c478bd9Sstevel@tonic-gate 	switch (opcode) {
7627c478bd9Sstevel@tonic-gate 	/* Portable-specific options */
7637c478bd9Sstevel@tonic-gate 	case sPAMAuthenticationViaKbdInt:
7647c478bd9Sstevel@tonic-gate 		intptr = &options->pam_authentication_via_kbd_int;
7657c478bd9Sstevel@tonic-gate 		goto parse_flag;
7667c478bd9Sstevel@tonic-gate 
7677c478bd9Sstevel@tonic-gate 	/* Standard Options */
7687c478bd9Sstevel@tonic-gate 	case sBadOption:
7697c478bd9Sstevel@tonic-gate 		return -1;
7707c478bd9Sstevel@tonic-gate 	case sPort:
7717c478bd9Sstevel@tonic-gate 		/* ignore ports from configfile if cmdline specifies ports */
7727c478bd9Sstevel@tonic-gate 		if (options->ports_from_cmdline)
7737c478bd9Sstevel@tonic-gate 			return 0;
7747c478bd9Sstevel@tonic-gate 		if (options->listen_addrs != NULL)
7757c478bd9Sstevel@tonic-gate 			fatal("%s line %d: ports must be specified before "
7767c478bd9Sstevel@tonic-gate 			    "ListenAddress.", filename, linenum);
7777c478bd9Sstevel@tonic-gate 		if (options->num_ports >= MAX_PORTS)
7787c478bd9Sstevel@tonic-gate 			fatal("%s line %d: too many ports.",
7797c478bd9Sstevel@tonic-gate 			    filename, linenum);
7807c478bd9Sstevel@tonic-gate 		arg = strdelim(&cp);
7817c478bd9Sstevel@tonic-gate 		if (!arg || *arg == '\0')
7827c478bd9Sstevel@tonic-gate 			fatal("%s line %d: missing port number.",
7837c478bd9Sstevel@tonic-gate 			    filename, linenum);
7847c478bd9Sstevel@tonic-gate 		options->ports[options->num_ports++] = a2port(arg);
7857c478bd9Sstevel@tonic-gate 		if (options->ports[options->num_ports-1] == 0)
7867c478bd9Sstevel@tonic-gate 			fatal("%s line %d: Badly formatted port number.",
7877c478bd9Sstevel@tonic-gate 			    filename, linenum);
7887c478bd9Sstevel@tonic-gate 		break;
7897c478bd9Sstevel@tonic-gate 
7907c478bd9Sstevel@tonic-gate 	case sServerKeyBits:
7917c478bd9Sstevel@tonic-gate 		intptr = &options->server_key_bits;
7927c478bd9Sstevel@tonic-gate parse_int:
7937c478bd9Sstevel@tonic-gate 		arg = strdelim(&cp);
7947c478bd9Sstevel@tonic-gate 		if (!arg || *arg == '\0')
7957c478bd9Sstevel@tonic-gate 			fatal("%s line %d: missing integer value.",
7967c478bd9Sstevel@tonic-gate 			    filename, linenum);
7977c478bd9Sstevel@tonic-gate 		value = atoi(arg);
798b07b2f5cSHuie-Ying Lee 		if (*activep && *intptr == -1)
7997c478bd9Sstevel@tonic-gate 			*intptr = value;
8007c478bd9Sstevel@tonic-gate 		break;
8017c478bd9Sstevel@tonic-gate 
8027c478bd9Sstevel@tonic-gate 	case sLoginGraceTime:
8037c478bd9Sstevel@tonic-gate 		intptr = &options->login_grace_time;
8047c478bd9Sstevel@tonic-gate parse_time:
8057c478bd9Sstevel@tonic-gate 		arg = strdelim(&cp);
8067c478bd9Sstevel@tonic-gate 		if (!arg || *arg == '\0')
8077c478bd9Sstevel@tonic-gate 			fatal("%s line %d: missing time value.",
8087c478bd9Sstevel@tonic-gate 			    filename, linenum);
8097c478bd9Sstevel@tonic-gate 		if ((value = convtime(arg)) == -1)
8107c478bd9Sstevel@tonic-gate 			fatal("%s line %d: invalid time value.",
8117c478bd9Sstevel@tonic-gate 			    filename, linenum);
8127c478bd9Sstevel@tonic-gate 		if (*intptr == -1)
8137c478bd9Sstevel@tonic-gate 			*intptr = value;
8147c478bd9Sstevel@tonic-gate 		break;
8157c478bd9Sstevel@tonic-gate 
8167c478bd9Sstevel@tonic-gate 	case sKeyRegenerationTime:
8177c478bd9Sstevel@tonic-gate 		intptr = &options->key_regeneration_time;
8187c478bd9Sstevel@tonic-gate 		goto parse_time;
8197c478bd9Sstevel@tonic-gate 
8207c478bd9Sstevel@tonic-gate 	case sListenAddress:
8217c478bd9Sstevel@tonic-gate 		arg = strdelim(&cp);
8227c478bd9Sstevel@tonic-gate 		if (!arg || *arg == '\0' || strncmp(arg, "[]", 2) == 0)
8237c478bd9Sstevel@tonic-gate 			fatal("%s line %d: missing inet addr.",
8247c478bd9Sstevel@tonic-gate 			    filename, linenum);
8257c478bd9Sstevel@tonic-gate 		if (*arg == '[') {
8267c478bd9Sstevel@tonic-gate 			if ((p = strchr(arg, ']')) == NULL)
8277c478bd9Sstevel@tonic-gate 				fatal("%s line %d: bad ipv6 inet addr usage.",
8287c478bd9Sstevel@tonic-gate 				    filename, linenum);
8297c478bd9Sstevel@tonic-gate 			arg++;
8307c478bd9Sstevel@tonic-gate 			(void) memmove(p, p+1, strlen(p+1)+1);
8317c478bd9Sstevel@tonic-gate 		} else if (((p = strchr(arg, ':')) == NULL) ||
8327c478bd9Sstevel@tonic-gate 			    (strchr(p+1, ':') != NULL)) {
8337c478bd9Sstevel@tonic-gate 			add_listen_addr(options, arg, 0);
8347c478bd9Sstevel@tonic-gate 			break;
8357c478bd9Sstevel@tonic-gate 		}
8367c478bd9Sstevel@tonic-gate 		if (*p == ':') {
8377c478bd9Sstevel@tonic-gate 			u_short port;
8387c478bd9Sstevel@tonic-gate 
8397c478bd9Sstevel@tonic-gate 			p++;
8407c478bd9Sstevel@tonic-gate 			if (*p == '\0')
8417c478bd9Sstevel@tonic-gate 				fatal("%s line %d: bad inet addr:port usage.",
8427c478bd9Sstevel@tonic-gate 				    filename, linenum);
8437c478bd9Sstevel@tonic-gate 			else {
8447c478bd9Sstevel@tonic-gate 				*(p-1) = '\0';
8457c478bd9Sstevel@tonic-gate 				if ((port = a2port(p)) == 0)
8467c478bd9Sstevel@tonic-gate 					fatal("%s line %d: bad port number.",
8477c478bd9Sstevel@tonic-gate 					    filename, linenum);
8487c478bd9Sstevel@tonic-gate 				add_listen_addr(options, arg, port);
8497c478bd9Sstevel@tonic-gate 			}
8507c478bd9Sstevel@tonic-gate 		} else if (*p == '\0')
8517c478bd9Sstevel@tonic-gate 			add_listen_addr(options, arg, 0);
8527c478bd9Sstevel@tonic-gate 		else
8537c478bd9Sstevel@tonic-gate 			fatal("%s line %d: bad inet addr usage.",
8547c478bd9Sstevel@tonic-gate 			    filename, linenum);
8557c478bd9Sstevel@tonic-gate 		break;
8567c478bd9Sstevel@tonic-gate 
8577c478bd9Sstevel@tonic-gate 	case sHostKeyFile:
8587c478bd9Sstevel@tonic-gate 		intptr = &options->num_host_key_files;
8597c478bd9Sstevel@tonic-gate 		if (*intptr >= MAX_HOSTKEYS)
8607c478bd9Sstevel@tonic-gate 			fatal("%s line %d: too many host keys specified (max %d).",
8617c478bd9Sstevel@tonic-gate 			    filename, linenum, MAX_HOSTKEYS);
8627c478bd9Sstevel@tonic-gate 		charptr = &options->host_key_files[*intptr];
8637c478bd9Sstevel@tonic-gate parse_filename:
8647c478bd9Sstevel@tonic-gate 		arg = strdelim(&cp);
8657c478bd9Sstevel@tonic-gate 		if (!arg || *arg == '\0')
8667c478bd9Sstevel@tonic-gate 			fatal("%s line %d: missing file name.",
8677c478bd9Sstevel@tonic-gate 			    filename, linenum);
868b07b2f5cSHuie-Ying Lee 		if (*activep && *charptr == NULL) {
8697c478bd9Sstevel@tonic-gate 			*charptr = tilde_expand_filename(arg, getuid());
8707c478bd9Sstevel@tonic-gate 			/* increase optional counter */
8717c478bd9Sstevel@tonic-gate 			if (intptr != NULL)
8727c478bd9Sstevel@tonic-gate 				*intptr = *intptr + 1;
8737c478bd9Sstevel@tonic-gate 		}
8747c478bd9Sstevel@tonic-gate 		break;
8757c478bd9Sstevel@tonic-gate 
8767c478bd9Sstevel@tonic-gate 	case sPidFile:
8777c478bd9Sstevel@tonic-gate 		charptr = &options->pid_file;
8787c478bd9Sstevel@tonic-gate 		goto parse_filename;
8797c478bd9Sstevel@tonic-gate 
8807c478bd9Sstevel@tonic-gate 	case sPermitRootLogin:
8817c478bd9Sstevel@tonic-gate 		intptr = &options->permit_root_login;
8827c478bd9Sstevel@tonic-gate 		arg = strdelim(&cp);
8837c478bd9Sstevel@tonic-gate 		if (!arg || *arg == '\0')
8847c478bd9Sstevel@tonic-gate 			fatal("%s line %d: missing yes/"
8857c478bd9Sstevel@tonic-gate 			    "without-password/forced-commands-only/no "
8867c478bd9Sstevel@tonic-gate 			    "argument.", filename, linenum);
8877c478bd9Sstevel@tonic-gate 		value = 0;	/* silence compiler */
8887c478bd9Sstevel@tonic-gate 		if (strcmp(arg, "without-password") == 0)
8897c478bd9Sstevel@tonic-gate 			value = PERMIT_NO_PASSWD;
8907c478bd9Sstevel@tonic-gate 		else if (strcmp(arg, "forced-commands-only") == 0)
8917c478bd9Sstevel@tonic-gate 			value = PERMIT_FORCED_ONLY;
8927c478bd9Sstevel@tonic-gate 		else if (strcmp(arg, "yes") == 0)
8937c478bd9Sstevel@tonic-gate 			value = PERMIT_YES;
8947c478bd9Sstevel@tonic-gate 		else if (strcmp(arg, "no") == 0)
8957c478bd9Sstevel@tonic-gate 			value = PERMIT_NO;
8967c478bd9Sstevel@tonic-gate 		else
8977c478bd9Sstevel@tonic-gate 			fatal("%s line %d: Bad yes/"
8987c478bd9Sstevel@tonic-gate 			    "without-password/forced-commands-only/no "
8997c478bd9Sstevel@tonic-gate 			    "argument: %s", filename, linenum, arg);
900b07b2f5cSHuie-Ying Lee 		if (*activep && *intptr == -1)
9017c478bd9Sstevel@tonic-gate 			*intptr = value;
9027c478bd9Sstevel@tonic-gate 		break;
9037c478bd9Sstevel@tonic-gate 
9047c478bd9Sstevel@tonic-gate 	case sIgnoreRhosts:
9057c478bd9Sstevel@tonic-gate 		intptr = &options->ignore_rhosts;
9067c478bd9Sstevel@tonic-gate parse_flag:
9077c478bd9Sstevel@tonic-gate 		arg = strdelim(&cp);
9087c478bd9Sstevel@tonic-gate 		if (!arg || *arg == '\0')
9097c478bd9Sstevel@tonic-gate 			fatal("%s line %d: missing yes/no argument.",
9107c478bd9Sstevel@tonic-gate 			    filename, linenum);
9117c478bd9Sstevel@tonic-gate 		value = 0;	/* silence compiler */
9127c478bd9Sstevel@tonic-gate 		if (strcmp(arg, "yes") == 0)
9137c478bd9Sstevel@tonic-gate 			value = 1;
9147c478bd9Sstevel@tonic-gate 		else if (strcmp(arg, "no") == 0)
9157c478bd9Sstevel@tonic-gate 			value = 0;
9167c478bd9Sstevel@tonic-gate 		else
9177c478bd9Sstevel@tonic-gate 			fatal("%s line %d: Bad yes/no argument: %s",
9187c478bd9Sstevel@tonic-gate 				filename, linenum, arg);
919b07b2f5cSHuie-Ying Lee 		if (*activep && *intptr == -1)
9207c478bd9Sstevel@tonic-gate 			*intptr = value;
9217c478bd9Sstevel@tonic-gate 		break;
9227c478bd9Sstevel@tonic-gate 
9237c478bd9Sstevel@tonic-gate 	case sIgnoreUserKnownHosts:
9247c478bd9Sstevel@tonic-gate 		intptr = &options->ignore_user_known_hosts;
9257c478bd9Sstevel@tonic-gate 		goto parse_flag;
9267c478bd9Sstevel@tonic-gate 
9277c478bd9Sstevel@tonic-gate 	case sRhostsAuthentication:
9287c478bd9Sstevel@tonic-gate 		intptr = &options->rhosts_authentication;
9297c478bd9Sstevel@tonic-gate 		goto parse_flag;
9307c478bd9Sstevel@tonic-gate 
9317c478bd9Sstevel@tonic-gate 	case sRhostsRSAAuthentication:
9327c478bd9Sstevel@tonic-gate 		intptr = &options->rhosts_rsa_authentication;
9337c478bd9Sstevel@tonic-gate 		goto parse_flag;
9347c478bd9Sstevel@tonic-gate 
9357c478bd9Sstevel@tonic-gate 	case sHostbasedAuthentication:
9367c478bd9Sstevel@tonic-gate 		intptr = &options->hostbased_authentication;
9377c478bd9Sstevel@tonic-gate 		goto parse_flag;
9387c478bd9Sstevel@tonic-gate 
9397c478bd9Sstevel@tonic-gate 	case sHostbasedUsesNameFromPacketOnly:
9407c478bd9Sstevel@tonic-gate 		intptr = &options->hostbased_uses_name_from_packet_only;
9417c478bd9Sstevel@tonic-gate 		goto parse_flag;
9427c478bd9Sstevel@tonic-gate 
9437c478bd9Sstevel@tonic-gate 	case sRSAAuthentication:
9447c478bd9Sstevel@tonic-gate 		intptr = &options->rsa_authentication;
9457c478bd9Sstevel@tonic-gate 		goto parse_flag;
9467c478bd9Sstevel@tonic-gate 
9477c478bd9Sstevel@tonic-gate 	case sPubkeyAuthentication:
9487c478bd9Sstevel@tonic-gate 		intptr = &options->pubkey_authentication;
9497c478bd9Sstevel@tonic-gate 		goto parse_flag;
9507c478bd9Sstevel@tonic-gate #ifdef GSSAPI
9517c478bd9Sstevel@tonic-gate 	case sGssAuthentication:
9527c478bd9Sstevel@tonic-gate 		intptr = &options->gss_authentication;
9537c478bd9Sstevel@tonic-gate 		goto parse_flag;
9547c478bd9Sstevel@tonic-gate 	case sGssKeyEx:
9557c478bd9Sstevel@tonic-gate 		intptr = &options->gss_keyex;
9567c478bd9Sstevel@tonic-gate 		goto parse_flag;
9577c478bd9Sstevel@tonic-gate 	case sGssStoreDelegCreds:
9587c478bd9Sstevel@tonic-gate 		intptr = &options->gss_keyex;
9597c478bd9Sstevel@tonic-gate 		goto parse_flag;
9607c478bd9Sstevel@tonic-gate #ifndef SUNW_GSSAPI
9617c478bd9Sstevel@tonic-gate 	case sGssUseSessionCredCache:
9627c478bd9Sstevel@tonic-gate 		intptr = &options->gss_use_session_ccache;
9637c478bd9Sstevel@tonic-gate 		goto parse_flag;
9647c478bd9Sstevel@tonic-gate 	case sGssCleanupCreds:
9657c478bd9Sstevel@tonic-gate 		intptr = &options->gss_cleanup_creds;
9667c478bd9Sstevel@tonic-gate 		goto parse_flag;
9677c478bd9Sstevel@tonic-gate #endif /* SUNW_GSSAPI */
9687c478bd9Sstevel@tonic-gate #endif /* GSSAPI */
9697c478bd9Sstevel@tonic-gate #if defined(KRB4) || defined(KRB5)
9707c478bd9Sstevel@tonic-gate 	case sKerberosAuthentication:
9717c478bd9Sstevel@tonic-gate 		intptr = &options->kerberos_authentication;
9727c478bd9Sstevel@tonic-gate 		goto parse_flag;
9737c478bd9Sstevel@tonic-gate 
9747c478bd9Sstevel@tonic-gate 	case sKerberosOrLocalPasswd:
9757c478bd9Sstevel@tonic-gate 		intptr = &options->kerberos_or_local_passwd;
9767c478bd9Sstevel@tonic-gate 		goto parse_flag;
9777c478bd9Sstevel@tonic-gate 
9787c478bd9Sstevel@tonic-gate 	case sKerberosTicketCleanup:
9797c478bd9Sstevel@tonic-gate 		intptr = &options->kerberos_ticket_cleanup;
9807c478bd9Sstevel@tonic-gate 		goto parse_flag;
9817c478bd9Sstevel@tonic-gate #endif
9827c478bd9Sstevel@tonic-gate #if defined(AFS) || defined(KRB5)
9837c478bd9Sstevel@tonic-gate 	case sKerberosTgtPassing:
9847c478bd9Sstevel@tonic-gate 		intptr = &options->kerberos_tgt_passing;
9857c478bd9Sstevel@tonic-gate 		goto parse_flag;
9867c478bd9Sstevel@tonic-gate #endif
9877c478bd9Sstevel@tonic-gate #ifdef AFS
9887c478bd9Sstevel@tonic-gate 	case sAFSTokenPassing:
9897c478bd9Sstevel@tonic-gate 		intptr = &options->afs_token_passing;
9907c478bd9Sstevel@tonic-gate 		goto parse_flag;
9917c478bd9Sstevel@tonic-gate #endif
9927c478bd9Sstevel@tonic-gate 
9937c478bd9Sstevel@tonic-gate 	case sPasswordAuthentication:
9947c478bd9Sstevel@tonic-gate 		intptr = &options->password_authentication;
9957c478bd9Sstevel@tonic-gate 		goto parse_flag;
9967c478bd9Sstevel@tonic-gate 
9977c478bd9Sstevel@tonic-gate 	case sKbdInteractiveAuthentication:
9987c478bd9Sstevel@tonic-gate 		intptr = &options->kbd_interactive_authentication;
9997c478bd9Sstevel@tonic-gate 		goto parse_flag;
10007c478bd9Sstevel@tonic-gate 
10017c478bd9Sstevel@tonic-gate 	case sChallengeResponseAuthentication:
10027c478bd9Sstevel@tonic-gate 		intptr = &options->challenge_response_authentication;
10037c478bd9Sstevel@tonic-gate 		goto parse_flag;
10047c478bd9Sstevel@tonic-gate 
10057c478bd9Sstevel@tonic-gate 	case sPrintMotd:
10067c478bd9Sstevel@tonic-gate 		intptr = &options->print_motd;
10077c478bd9Sstevel@tonic-gate 		goto parse_flag;
10087c478bd9Sstevel@tonic-gate 
10097c478bd9Sstevel@tonic-gate 	case sPrintLastLog:
10107c478bd9Sstevel@tonic-gate 		intptr = &options->print_lastlog;
10117c478bd9Sstevel@tonic-gate 		goto parse_flag;
10127c478bd9Sstevel@tonic-gate 
10137c478bd9Sstevel@tonic-gate 	case sX11Forwarding:
10147c478bd9Sstevel@tonic-gate 		intptr = &options->x11_forwarding;
10157c478bd9Sstevel@tonic-gate 		goto parse_flag;
10167c478bd9Sstevel@tonic-gate 
10177c478bd9Sstevel@tonic-gate 	case sX11DisplayOffset:
10187c478bd9Sstevel@tonic-gate 		intptr = &options->x11_display_offset;
10197c478bd9Sstevel@tonic-gate 		goto parse_int;
10207c478bd9Sstevel@tonic-gate 
10217c478bd9Sstevel@tonic-gate 	case sX11UseLocalhost:
10227c478bd9Sstevel@tonic-gate 		intptr = &options->x11_use_localhost;
10237c478bd9Sstevel@tonic-gate 		goto parse_flag;
10247c478bd9Sstevel@tonic-gate 
10257c478bd9Sstevel@tonic-gate 	case sXAuthLocation:
10267c478bd9Sstevel@tonic-gate 		charptr = &options->xauth_location;
10277c478bd9Sstevel@tonic-gate 		goto parse_filename;
10287c478bd9Sstevel@tonic-gate 
10297c478bd9Sstevel@tonic-gate 	case sStrictModes:
10307c478bd9Sstevel@tonic-gate 		intptr = &options->strict_modes;
10317c478bd9Sstevel@tonic-gate 		goto parse_flag;
10327c478bd9Sstevel@tonic-gate 
10337c478bd9Sstevel@tonic-gate 	case sKeepAlives:
10347c478bd9Sstevel@tonic-gate 		intptr = &options->keepalives;
10357c478bd9Sstevel@tonic-gate 		goto parse_flag;
10367c478bd9Sstevel@tonic-gate 
10377c478bd9Sstevel@tonic-gate 	case sEmptyPasswd:
10387c478bd9Sstevel@tonic-gate 		intptr = &options->permit_empty_passwd;
10397c478bd9Sstevel@tonic-gate 		goto parse_flag;
10407c478bd9Sstevel@tonic-gate 
10417c478bd9Sstevel@tonic-gate 	case sPermitUserEnvironment:
10427c478bd9Sstevel@tonic-gate 		intptr = &options->permit_user_env;
10437c478bd9Sstevel@tonic-gate 		goto parse_flag;
10447c478bd9Sstevel@tonic-gate 
10457c478bd9Sstevel@tonic-gate 	case sUseLogin:
1046b9aa66a7SJan Pechanec 		log("%s line %d: ignoring UseLogin option value."
1047b9aa66a7SJan Pechanec 		    " This option is always off.", filename, linenum);
1048b9aa66a7SJan Pechanec 		while (arg)
1049b9aa66a7SJan Pechanec 			arg = strdelim(&cp);
1050b9aa66a7SJan Pechanec 		break;
10517c478bd9Sstevel@tonic-gate 
10527c478bd9Sstevel@tonic-gate 	case sCompression:
10537c478bd9Sstevel@tonic-gate 		intptr = &options->compression;
10547c478bd9Sstevel@tonic-gate 		goto parse_flag;
10557c478bd9Sstevel@tonic-gate 
10567c478bd9Sstevel@tonic-gate 	case sGatewayPorts:
1057b07b2f5cSHuie-Ying Lee 		intptr = &options->gateway_ports;
10589b03ea0fSjp161948 		arg = strdelim(&cp);
1059b07b2f5cSHuie-Ying Lee 		if (!arg || *arg == '\0')
1060b07b2f5cSHuie-Ying Lee 			fatal("%s line %d: missing yes/no/clientspecified "
10619b03ea0fSjp161948 			    "argument.", filename, linenum);
1062b07b2f5cSHuie-Ying Lee 		value = 0;	/* silence compiler */
1063b07b2f5cSHuie-Ying Lee 		if (strcmp(arg, "clientspecified") == 0)
1064b07b2f5cSHuie-Ying Lee 			value = 2;
1065b07b2f5cSHuie-Ying Lee 		else if (strcmp(arg, "yes") == 0)
1066b07b2f5cSHuie-Ying Lee 			value = 1;
1067b07b2f5cSHuie-Ying Lee 		else if (strcmp(arg, "no") == 0)
1068b07b2f5cSHuie-Ying Lee 			value = 0;
1069b07b2f5cSHuie-Ying Lee 		else
1070b07b2f5cSHuie-Ying Lee 			fatal("%s line %d: Bad yes/no/clientspecified "
1071b07b2f5cSHuie-Ying Lee 			    "argument: %s", filename, linenum, arg);
1072b07b2f5cSHuie-Ying Lee 		if (*activep && *intptr == -1)
1073b07b2f5cSHuie-Ying Lee 			*intptr = value;
10749b03ea0fSjp161948 		break;
10757c478bd9Sstevel@tonic-gate 
10767c478bd9Sstevel@tonic-gate 	case sVerifyReverseMapping:
10777c478bd9Sstevel@tonic-gate 		intptr = &options->verify_reverse_mapping;
10787c478bd9Sstevel@tonic-gate 		goto parse_flag;
10797c478bd9Sstevel@tonic-gate 
10807c478bd9Sstevel@tonic-gate 	case sLogFacility:
10817c478bd9Sstevel@tonic-gate 		intptr = (int *) &options->log_facility;
10827c478bd9Sstevel@tonic-gate 		arg = strdelim(&cp);
10837c478bd9Sstevel@tonic-gate 		value = log_facility_number(arg);
10847c478bd9Sstevel@tonic-gate 		if (value == SYSLOG_FACILITY_NOT_SET)
10857c478bd9Sstevel@tonic-gate 			fatal("%.200s line %d: unsupported log facility '%s'",
10867c478bd9Sstevel@tonic-gate 			    filename, linenum, arg ? arg : "<NONE>");
10877c478bd9Sstevel@tonic-gate 		if (*intptr == -1)
10887c478bd9Sstevel@tonic-gate 			*intptr = (SyslogFacility) value;
10897c478bd9Sstevel@tonic-gate 		break;
10907c478bd9Sstevel@tonic-gate 
10917c478bd9Sstevel@tonic-gate 	case sLogLevel:
10927c478bd9Sstevel@tonic-gate 		intptr = (int *) &options->log_level;
10937c478bd9Sstevel@tonic-gate 		arg = strdelim(&cp);
10947c478bd9Sstevel@tonic-gate 		value = log_level_number(arg);
10957c478bd9Sstevel@tonic-gate 		if (value == SYSLOG_LEVEL_NOT_SET)
10967c478bd9Sstevel@tonic-gate 			fatal("%.200s line %d: unsupported log level '%s'",
10977c478bd9Sstevel@tonic-gate 			    filename, linenum, arg ? arg : "<NONE>");
10987c478bd9Sstevel@tonic-gate 		if (*intptr == -1)
10997c478bd9Sstevel@tonic-gate 			*intptr = (LogLevel) value;
11007c478bd9Sstevel@tonic-gate 		break;
11017c478bd9Sstevel@tonic-gate 
11027c478bd9Sstevel@tonic-gate 	case sAllowTcpForwarding:
11037c478bd9Sstevel@tonic-gate 		intptr = &options->allow_tcp_forwarding;
11047c478bd9Sstevel@tonic-gate 		goto parse_flag;
11057c478bd9Sstevel@tonic-gate 
11067c478bd9Sstevel@tonic-gate 	case sUsePrivilegeSeparation:
11079a8058b5Sjp161948 		log("%s line %d: ignoring UsePrivilegeSeparation option value."
11089a8058b5Sjp161948 		    " This option is always on.", filename, linenum);
11099a8058b5Sjp161948 		while (arg)
11109a8058b5Sjp161948 			arg = strdelim(&cp);
11119a8058b5Sjp161948 		break;
11127c478bd9Sstevel@tonic-gate 
11137c478bd9Sstevel@tonic-gate 	case sAllowUsers:
11147c478bd9Sstevel@tonic-gate 		while (((arg = strdelim(&cp)) != NULL) && *arg != '\0') {
11157c478bd9Sstevel@tonic-gate 			if (options->num_allow_users >= MAX_ALLOW_USERS)
11167c478bd9Sstevel@tonic-gate 				fatal("%s line %d: too many allow users.",
11177c478bd9Sstevel@tonic-gate 				    filename, linenum);
11187c478bd9Sstevel@tonic-gate 			options->allow_users[options->num_allow_users++] =
11197c478bd9Sstevel@tonic-gate 			    xstrdup(arg);
11207c478bd9Sstevel@tonic-gate 		}
11217c478bd9Sstevel@tonic-gate 		break;
11227c478bd9Sstevel@tonic-gate 
11237c478bd9Sstevel@tonic-gate 	case sDenyUsers:
11247c478bd9Sstevel@tonic-gate 		while (((arg = strdelim(&cp)) != NULL) && *arg != '\0') {
11257c478bd9Sstevel@tonic-gate 			if (options->num_deny_users >= MAX_DENY_USERS)
11267c478bd9Sstevel@tonic-gate 				fatal( "%s line %d: too many deny users.",
11277c478bd9Sstevel@tonic-gate 				    filename, linenum);
11287c478bd9Sstevel@tonic-gate 			options->deny_users[options->num_deny_users++] =
11297c478bd9Sstevel@tonic-gate 			    xstrdup(arg);
11307c478bd9Sstevel@tonic-gate 		}
11317c478bd9Sstevel@tonic-gate 		break;
11327c478bd9Sstevel@tonic-gate 
11337c478bd9Sstevel@tonic-gate 	case sAllowGroups:
11347c478bd9Sstevel@tonic-gate 		while (((arg = strdelim(&cp)) != NULL) && *arg != '\0') {
11357c478bd9Sstevel@tonic-gate 			if (options->num_allow_groups >= MAX_ALLOW_GROUPS)
11367c478bd9Sstevel@tonic-gate 				fatal("%s line %d: too many allow groups.",
11377c478bd9Sstevel@tonic-gate 				    filename, linenum);
11387c478bd9Sstevel@tonic-gate 			options->allow_groups[options->num_allow_groups++] =
11397c478bd9Sstevel@tonic-gate 			    xstrdup(arg);
11407c478bd9Sstevel@tonic-gate 		}
11417c478bd9Sstevel@tonic-gate 		break;
11427c478bd9Sstevel@tonic-gate 
11437c478bd9Sstevel@tonic-gate 	case sDenyGroups:
11447c478bd9Sstevel@tonic-gate 		while (((arg = strdelim(&cp)) != NULL) && *arg != '\0') {
11457c478bd9Sstevel@tonic-gate 			if (options->num_deny_groups >= MAX_DENY_GROUPS)
11467c478bd9Sstevel@tonic-gate 				fatal("%s line %d: too many deny groups.",
11477c478bd9Sstevel@tonic-gate 				    filename, linenum);
11487c478bd9Sstevel@tonic-gate 			options->deny_groups[options->num_deny_groups++] = xstrdup(arg);
11497c478bd9Sstevel@tonic-gate 		}
11507c478bd9Sstevel@tonic-gate 		break;
11517c478bd9Sstevel@tonic-gate 
11527c478bd9Sstevel@tonic-gate 	case sCiphers:
11537c478bd9Sstevel@tonic-gate 		arg = strdelim(&cp);
11547c478bd9Sstevel@tonic-gate 		if (!arg || *arg == '\0')
11557c478bd9Sstevel@tonic-gate 			fatal("%s line %d: Missing argument.", filename, linenum);
11567c478bd9Sstevel@tonic-gate 		if (!ciphers_valid(arg))
11577c478bd9Sstevel@tonic-gate 			fatal("%s line %d: Bad SSH2 cipher spec '%s'.",
11587c478bd9Sstevel@tonic-gate 			    filename, linenum, arg ? arg : "<NONE>");
11597c478bd9Sstevel@tonic-gate 		if (options->ciphers == NULL)
11607c478bd9Sstevel@tonic-gate 			options->ciphers = xstrdup(arg);
11617c478bd9Sstevel@tonic-gate 		break;
11627c478bd9Sstevel@tonic-gate 
11637c478bd9Sstevel@tonic-gate 	case sMacs:
11647c478bd9Sstevel@tonic-gate 		arg = strdelim(&cp);
11657c478bd9Sstevel@tonic-gate 		if (!arg || *arg == '\0')
11667c478bd9Sstevel@tonic-gate 			fatal("%s line %d: Missing argument.", filename, linenum);
11677c478bd9Sstevel@tonic-gate 		if (!mac_valid(arg))
11687c478bd9Sstevel@tonic-gate 			fatal("%s line %d: Bad SSH2 mac spec '%s'.",
11697c478bd9Sstevel@tonic-gate 			    filename, linenum, arg ? arg : "<NONE>");
11707c478bd9Sstevel@tonic-gate 		if (options->macs == NULL)
11717c478bd9Sstevel@tonic-gate 			options->macs = xstrdup(arg);
11727c478bd9Sstevel@tonic-gate 		break;
11737c478bd9Sstevel@tonic-gate 
11747c478bd9Sstevel@tonic-gate 	case sProtocol:
11757c478bd9Sstevel@tonic-gate 		intptr = &options->protocol;
11767c478bd9Sstevel@tonic-gate 		arg = strdelim(&cp);
11777c478bd9Sstevel@tonic-gate 		if (!arg || *arg == '\0')
11787c478bd9Sstevel@tonic-gate 			fatal("%s line %d: Missing argument.", filename, linenum);
11797c478bd9Sstevel@tonic-gate 		value = proto_spec(arg);
11807c478bd9Sstevel@tonic-gate 		if (value == SSH_PROTO_UNKNOWN)
11817c478bd9Sstevel@tonic-gate 			fatal("%s line %d: Bad protocol spec '%s'.",
11827c478bd9Sstevel@tonic-gate 			    filename, linenum, arg ? arg : "<NONE>");
11837c478bd9Sstevel@tonic-gate 		if (*intptr == SSH_PROTO_UNKNOWN)
11847c478bd9Sstevel@tonic-gate 			*intptr = value;
11857c478bd9Sstevel@tonic-gate 		break;
11867c478bd9Sstevel@tonic-gate 
11877c478bd9Sstevel@tonic-gate 	case sSubsystem:
11887c478bd9Sstevel@tonic-gate 		if (options->num_subsystems >= MAX_SUBSYSTEMS) {
11897c478bd9Sstevel@tonic-gate 			fatal("%s line %d: too many subsystems defined.",
11907c478bd9Sstevel@tonic-gate 			    filename, linenum);
11917c478bd9Sstevel@tonic-gate 		}
11927c478bd9Sstevel@tonic-gate 		arg = strdelim(&cp);
11937c478bd9Sstevel@tonic-gate 		if (!arg || *arg == '\0')
11947c478bd9Sstevel@tonic-gate 			fatal("%s line %d: Missing subsystem name.",
11957c478bd9Sstevel@tonic-gate 			    filename, linenum);
1196b07b2f5cSHuie-Ying Lee 		if (!*activep) {
1197b07b2f5cSHuie-Ying Lee 			arg = strdelim(&cp);
1198b07b2f5cSHuie-Ying Lee 			break;
1199b07b2f5cSHuie-Ying Lee 		}
12007c478bd9Sstevel@tonic-gate 		for (i = 0; i < options->num_subsystems; i++)
12017c478bd9Sstevel@tonic-gate 			if (strcmp(arg, options->subsystem_name[i]) == 0)
12027c478bd9Sstevel@tonic-gate 				fatal("%s line %d: Subsystem '%s' already defined.",
12037c478bd9Sstevel@tonic-gate 				    filename, linenum, arg);
12047c478bd9Sstevel@tonic-gate 		options->subsystem_name[options->num_subsystems] = xstrdup(arg);
12057c478bd9Sstevel@tonic-gate 		arg = strdelim(&cp);
12067c478bd9Sstevel@tonic-gate 		if (!arg || *arg == '\0')
12077c478bd9Sstevel@tonic-gate 			fatal("%s line %d: Missing subsystem command.",
12087c478bd9Sstevel@tonic-gate 			    filename, linenum);
12097c478bd9Sstevel@tonic-gate 		options->subsystem_command[options->num_subsystems] = xstrdup(arg);
12106f8d59d8SJan Pechanec 
12116f8d59d8SJan Pechanec 		/*
12126f8d59d8SJan Pechanec 		 * Collect arguments (separate to executable), including the
12136f8d59d8SJan Pechanec 		 * name of the executable, in a way that is easier to parse
12146f8d59d8SJan Pechanec 		 * later.
12156f8d59d8SJan Pechanec 		 */
12166f8d59d8SJan Pechanec 		p = xstrdup(arg);
12176f8d59d8SJan Pechanec 		len = strlen(p) + 1;
12186f8d59d8SJan Pechanec 		while ((arg = strdelim(&cp)) != NULL && *arg != '\0') {
12196f8d59d8SJan Pechanec 			len += 1 + strlen(arg);
12206f8d59d8SJan Pechanec 			p = xrealloc(p, len);
12216f8d59d8SJan Pechanec 			strlcat(p, " ", len);
12226f8d59d8SJan Pechanec 			strlcat(p, arg, len);
12236f8d59d8SJan Pechanec 		}
12246f8d59d8SJan Pechanec 		options->subsystem_args[options->num_subsystems] = p;
12257c478bd9Sstevel@tonic-gate 		options->num_subsystems++;
12267c478bd9Sstevel@tonic-gate 		break;
12277c478bd9Sstevel@tonic-gate 
12287c478bd9Sstevel@tonic-gate 	case sMaxStartups:
12297c478bd9Sstevel@tonic-gate 		arg = strdelim(&cp);
12307c478bd9Sstevel@tonic-gate 		if (!arg || *arg == '\0')
12317c478bd9Sstevel@tonic-gate 			fatal("%s line %d: Missing MaxStartups spec.",
12327c478bd9Sstevel@tonic-gate 			    filename, linenum);
12337c478bd9Sstevel@tonic-gate 		if ((n = sscanf(arg, "%d:%d:%d",
12347c478bd9Sstevel@tonic-gate 		    &options->max_startups_begin,
12357c478bd9Sstevel@tonic-gate 		    &options->max_startups_rate,
12367c478bd9Sstevel@tonic-gate 		    &options->max_startups)) == 3) {
12377c478bd9Sstevel@tonic-gate 			if (options->max_startups_begin >
12387c478bd9Sstevel@tonic-gate 			    options->max_startups ||
12397c478bd9Sstevel@tonic-gate 			    options->max_startups_rate > 100 ||
12407c478bd9Sstevel@tonic-gate 			    options->max_startups_rate < 1)
12417c478bd9Sstevel@tonic-gate 				fatal("%s line %d: Illegal MaxStartups spec.",
12427c478bd9Sstevel@tonic-gate 				    filename, linenum);
12437c478bd9Sstevel@tonic-gate 		} else if (n != 1)
12447c478bd9Sstevel@tonic-gate 			fatal("%s line %d: Illegal MaxStartups spec.",
12457c478bd9Sstevel@tonic-gate 			    filename, linenum);
12467c478bd9Sstevel@tonic-gate 		else
12477c478bd9Sstevel@tonic-gate 			options->max_startups = options->max_startups_begin;
12487c478bd9Sstevel@tonic-gate 		break;
12497c478bd9Sstevel@tonic-gate 
12507c478bd9Sstevel@tonic-gate 	case sBanner:
12517c478bd9Sstevel@tonic-gate 		charptr = &options->banner;
12527c478bd9Sstevel@tonic-gate 		goto parse_filename;
12537c478bd9Sstevel@tonic-gate 	/*
12547c478bd9Sstevel@tonic-gate 	 * These options can contain %X options expanded at
12557c478bd9Sstevel@tonic-gate 	 * connect time, so that you can specify paths like:
12567c478bd9Sstevel@tonic-gate 	 *
12577c478bd9Sstevel@tonic-gate 	 * AuthorizedKeysFile	/etc/ssh_keys/%u
12587c478bd9Sstevel@tonic-gate 	 */
12597c478bd9Sstevel@tonic-gate 	case sAuthorizedKeysFile:
12607c478bd9Sstevel@tonic-gate 	case sAuthorizedKeysFile2:
12617c478bd9Sstevel@tonic-gate 		charptr = (opcode == sAuthorizedKeysFile) ?
12627c478bd9Sstevel@tonic-gate 		    &options->authorized_keys_file :
12637c478bd9Sstevel@tonic-gate 		    &options->authorized_keys_file2;
12647c478bd9Sstevel@tonic-gate 		goto parse_filename;
12657c478bd9Sstevel@tonic-gate 
12667c478bd9Sstevel@tonic-gate 	case sClientAliveInterval:
12677c478bd9Sstevel@tonic-gate 		intptr = &options->client_alive_interval;
12687c478bd9Sstevel@tonic-gate 		goto parse_time;
12697c478bd9Sstevel@tonic-gate 
12707c478bd9Sstevel@tonic-gate 	case sClientAliveCountMax:
12717c478bd9Sstevel@tonic-gate 		intptr = &options->client_alive_count_max;
12727c478bd9Sstevel@tonic-gate 		goto parse_int;
12737c478bd9Sstevel@tonic-gate 
12747c478bd9Sstevel@tonic-gate 	case sMaxAuthTries:
12757c478bd9Sstevel@tonic-gate 		intptr = &options->max_auth_tries;
12767c478bd9Sstevel@tonic-gate 		goto parse_int;
12777c478bd9Sstevel@tonic-gate 
12787c478bd9Sstevel@tonic-gate 	case sMaxAuthTriesLog:
12797c478bd9Sstevel@tonic-gate 		intptr = &options->max_auth_tries_log;
12807c478bd9Sstevel@tonic-gate 		goto parse_int;
12817c478bd9Sstevel@tonic-gate 
12827c478bd9Sstevel@tonic-gate 	case sLookupClientHostnames:
12837c478bd9Sstevel@tonic-gate 		intptr = &options->lookup_client_hostnames;
12847c478bd9Sstevel@tonic-gate 		goto parse_flag;
12856f8d59d8SJan Pechanec 
1286cd7d5fafSJan Pechanec 	case sUseOpenSSLEngine:
1287cd7d5fafSJan Pechanec 		intptr = &options->use_openssl_engine;
1288cd7d5fafSJan Pechanec 		goto parse_flag;
12897c478bd9Sstevel@tonic-gate 
12906f8d59d8SJan Pechanec 	case sChrootDirectory:
12916f8d59d8SJan Pechanec 		charptr = &options->chroot_directory;
12926f8d59d8SJan Pechanec 
12936f8d59d8SJan Pechanec 		arg = strdelim(&cp);
12946f8d59d8SJan Pechanec 		if (arg == NULL || *arg == '\0')
12956f8d59d8SJan Pechanec 			fatal("%s line %d: missing directory name for "
12966f8d59d8SJan Pechanec 			    "ChrootDirectory.", filename, linenum);
1297b07b2f5cSHuie-Ying Lee 		if (*activep && *charptr == NULL)
12986f8d59d8SJan Pechanec 			*charptr = xstrdup(arg);
12996f8d59d8SJan Pechanec 		break;
13006f8d59d8SJan Pechanec 
1301b07b2f5cSHuie-Ying Lee 	case sMatch:
1302b07b2f5cSHuie-Ying Lee 		if (cmdline)
1303b07b2f5cSHuie-Ying Lee 			fatal("Match directive not supported as a command-line "
1304b07b2f5cSHuie-Ying Lee 			   "option");
1305b07b2f5cSHuie-Ying Lee 		value = match_cfg_line(&cp, linenum, user, host, address);
1306b07b2f5cSHuie-Ying Lee 		if (value < 0)
1307b07b2f5cSHuie-Ying Lee 			fatal("%s line %d: Bad Match condition", filename,
1308b07b2f5cSHuie-Ying Lee 			    linenum);
1309b07b2f5cSHuie-Ying Lee 		*activep = value;
1310b07b2f5cSHuie-Ying Lee 		break;
1311b07b2f5cSHuie-Ying Lee 
13127c478bd9Sstevel@tonic-gate 	case sDeprecated:
13137c478bd9Sstevel@tonic-gate 		log("%s line %d: Deprecated option %s",
13147c478bd9Sstevel@tonic-gate 		    filename, linenum, arg);
13157c478bd9Sstevel@tonic-gate 		while (arg)
13167c478bd9Sstevel@tonic-gate 		    arg = strdelim(&cp);
13177c478bd9Sstevel@tonic-gate 		break;
13187c478bd9Sstevel@tonic-gate 
13197c478bd9Sstevel@tonic-gate 	default:
13207c478bd9Sstevel@tonic-gate 		fatal("%s line %d: Missing handler for opcode %s (%d)",
13217c478bd9Sstevel@tonic-gate 		    filename, linenum, arg, opcode);
13227c478bd9Sstevel@tonic-gate 	}
13237c478bd9Sstevel@tonic-gate 	if ((arg = strdelim(&cp)) != NULL && *arg != '\0')
13247c478bd9Sstevel@tonic-gate 		fatal("%s line %d: garbage at end of line; \"%.200s\".",
13257c478bd9Sstevel@tonic-gate 		    filename, linenum, arg);
13267c478bd9Sstevel@tonic-gate 	return 0;
13277c478bd9Sstevel@tonic-gate }
13287c478bd9Sstevel@tonic-gate 
1329b07b2f5cSHuie-Ying Lee 
13307c478bd9Sstevel@tonic-gate /* Reads the server configuration file. */
13317c478bd9Sstevel@tonic-gate 
13327c478bd9Sstevel@tonic-gate void
1333b07b2f5cSHuie-Ying Lee load_server_config(const char *filename, Buffer *conf)
13347c478bd9Sstevel@tonic-gate {
1335b07b2f5cSHuie-Ying Lee 	char line[1024], *cp;
13367c478bd9Sstevel@tonic-gate 	FILE *f;
13377c478bd9Sstevel@tonic-gate 
1338b07b2f5cSHuie-Ying Lee 	debug2("%s: filename %s", __func__, filename);
1339b07b2f5cSHuie-Ying Lee 	if ((f = fopen(filename, "r")) == NULL) {
13407c478bd9Sstevel@tonic-gate 		perror(filename);
13417c478bd9Sstevel@tonic-gate 		exit(1);
13427c478bd9Sstevel@tonic-gate 	}
1343b07b2f5cSHuie-Ying Lee 	buffer_clear(conf);
13447c478bd9Sstevel@tonic-gate 	while (fgets(line, sizeof(line), f)) {
1345b07b2f5cSHuie-Ying Lee 		/*
1346b07b2f5cSHuie-Ying Lee 		 * Trim out comments and strip whitespace
1347b07b2f5cSHuie-Ying Lee 		 * NB - preserve newlines, they are needed to reproduce
1348b07b2f5cSHuie-Ying Lee 		 * line numbers later for error messages
1349b07b2f5cSHuie-Ying Lee 		 */
1350b07b2f5cSHuie-Ying Lee 		if ((cp = strchr(line, '#')) != NULL)
1351b07b2f5cSHuie-Ying Lee 			memcpy(cp, "\n", 2);
1352b07b2f5cSHuie-Ying Lee 		cp = line + strspn(line, " \t\r");
1353b07b2f5cSHuie-Ying Lee 
1354b07b2f5cSHuie-Ying Lee 		buffer_append(conf, cp, strlen(cp));
1355b07b2f5cSHuie-Ying Lee 	}
1356b07b2f5cSHuie-Ying Lee 	buffer_append(conf, "\0", 1);
1357b07b2f5cSHuie-Ying Lee 	fclose(f);
1358b07b2f5cSHuie-Ying Lee 	debug2("%s: done config len = %d", __func__, buffer_len(conf));
1359b07b2f5cSHuie-Ying Lee }
1360b07b2f5cSHuie-Ying Lee 
1361b07b2f5cSHuie-Ying Lee void
1362b07b2f5cSHuie-Ying Lee parse_server_match_config(ServerOptions *options, const char *user,
1363b07b2f5cSHuie-Ying Lee     const char *host, const char *address)
1364b07b2f5cSHuie-Ying Lee {
1365b07b2f5cSHuie-Ying Lee 	ServerOptions mo;
1366b07b2f5cSHuie-Ying Lee 
1367b07b2f5cSHuie-Ying Lee 	initialize_server_options(&mo);
1368b07b2f5cSHuie-Ying Lee 	parse_server_config(&mo, "reprocess config", &cfg, user, host, address);
1369b07b2f5cSHuie-Ying Lee 	copy_set_server_options(options, &mo, 0);
1370b07b2f5cSHuie-Ying Lee }
1371b07b2f5cSHuie-Ying Lee 
1372b07b2f5cSHuie-Ying Lee 
1373b07b2f5cSHuie-Ying Lee 
1374b07b2f5cSHuie-Ying Lee /* Helper macros */
1375b07b2f5cSHuie-Ying Lee #define M_CP_INTOPT(n) do {\
1376b07b2f5cSHuie-Ying Lee 	if (src->n != -1) \
1377b07b2f5cSHuie-Ying Lee 		dst->n = src->n; \
1378b07b2f5cSHuie-Ying Lee } while (0)
1379b07b2f5cSHuie-Ying Lee #define M_CP_STROPT(n) do {\
1380b07b2f5cSHuie-Ying Lee 	if (src->n != NULL) { \
1381b07b2f5cSHuie-Ying Lee 		if (dst->n != NULL) \
1382b07b2f5cSHuie-Ying Lee 			xfree(dst->n); \
1383b07b2f5cSHuie-Ying Lee 		dst->n = src->n; \
1384b07b2f5cSHuie-Ying Lee 	} \
1385b07b2f5cSHuie-Ying Lee } while(0)
1386b07b2f5cSHuie-Ying Lee 
1387b07b2f5cSHuie-Ying Lee /*
1388b07b2f5cSHuie-Ying Lee  * Copy any supported values that are set.
1389b07b2f5cSHuie-Ying Lee  *
1390b07b2f5cSHuie-Ying Lee  * If the preauth flag is set, we do not bother copying the the string or
1391b07b2f5cSHuie-Ying Lee  * array values that are not used pre-authentication, because any that we
1392b07b2f5cSHuie-Ying Lee  * do use must be explictly sent in mm_getpwnamallow().
1393b07b2f5cSHuie-Ying Lee  */
1394b07b2f5cSHuie-Ying Lee void
1395b07b2f5cSHuie-Ying Lee copy_set_server_options(ServerOptions *dst, ServerOptions *src, int preauth)
1396b07b2f5cSHuie-Ying Lee {
1397b07b2f5cSHuie-Ying Lee 	M_CP_INTOPT(password_authentication);
1398b07b2f5cSHuie-Ying Lee 	M_CP_INTOPT(gss_authentication);
1399b07b2f5cSHuie-Ying Lee 	M_CP_INTOPT(rsa_authentication);
1400b07b2f5cSHuie-Ying Lee 	M_CP_INTOPT(pubkey_authentication);
1401b07b2f5cSHuie-Ying Lee 	M_CP_INTOPT(hostbased_authentication);
1402b07b2f5cSHuie-Ying Lee 	M_CP_INTOPT(kbd_interactive_authentication);
1403b07b2f5cSHuie-Ying Lee 	M_CP_INTOPT(permit_root_login);
1404b07b2f5cSHuie-Ying Lee 	M_CP_INTOPT(permit_empty_passwd);
1405b07b2f5cSHuie-Ying Lee 	M_CP_INTOPT(allow_tcp_forwarding);
1406b07b2f5cSHuie-Ying Lee 	M_CP_INTOPT(gateway_ports);
1407b07b2f5cSHuie-Ying Lee 	M_CP_INTOPT(x11_display_offset);
1408b07b2f5cSHuie-Ying Lee 	M_CP_INTOPT(x11_forwarding);
1409b07b2f5cSHuie-Ying Lee 	M_CP_INTOPT(x11_use_localhost);
1410b07b2f5cSHuie-Ying Lee 	M_CP_INTOPT(max_auth_tries);
1411b07b2f5cSHuie-Ying Lee 	M_CP_STROPT(banner);
1412b07b2f5cSHuie-Ying Lee 
1413b07b2f5cSHuie-Ying Lee 	if (preauth)
1414b07b2f5cSHuie-Ying Lee 		return;
1415b07b2f5cSHuie-Ying Lee 	M_CP_STROPT(chroot_directory);
1416b07b2f5cSHuie-Ying Lee }
1417b07b2f5cSHuie-Ying Lee 
1418b07b2f5cSHuie-Ying Lee #undef M_CP_INTOPT
1419b07b2f5cSHuie-Ying Lee #undef M_CP_STROPT
1420b07b2f5cSHuie-Ying Lee 
1421b07b2f5cSHuie-Ying Lee void
1422b07b2f5cSHuie-Ying Lee parse_server_config(ServerOptions *options, const char *filename, Buffer *conf,
1423b07b2f5cSHuie-Ying Lee     const char *user, const char *host, const char *address)
1424b07b2f5cSHuie-Ying Lee {
1425b07b2f5cSHuie-Ying Lee 	int active, linenum, bad_options = 0;
1426b07b2f5cSHuie-Ying Lee 	char *cp, *obuf, *cbuf;
1427b07b2f5cSHuie-Ying Lee 
1428b07b2f5cSHuie-Ying Lee 	debug2("%s: config %s len %d", __func__, filename, buffer_len(conf));
1429b07b2f5cSHuie-Ying Lee 
1430b07b2f5cSHuie-Ying Lee 	obuf = cbuf = xstrdup(buffer_ptr(conf));
1431b07b2f5cSHuie-Ying Lee 	active = user ? 0 : 1;
1432b07b2f5cSHuie-Ying Lee 	linenum = 1;
1433b07b2f5cSHuie-Ying Lee 	while ((cp = strsep(&cbuf, "\n")) != NULL) {
1434b07b2f5cSHuie-Ying Lee 		if (process_server_config_line(options, cp, filename,
1435b07b2f5cSHuie-Ying Lee 		    linenum++, &active, user, host, address) != 0)
14367c478bd9Sstevel@tonic-gate 			bad_options++;
14377c478bd9Sstevel@tonic-gate 	}
1438b07b2f5cSHuie-Ying Lee 	xfree(obuf);
14397c478bd9Sstevel@tonic-gate 	if (bad_options > 0)
14407c478bd9Sstevel@tonic-gate 		fatal("%s: terminating, %d bad configuration options",
14417c478bd9Sstevel@tonic-gate 		    filename, bad_options);
14427c478bd9Sstevel@tonic-gate }
14436f8d59d8SJan Pechanec 
1444b07b2f5cSHuie-Ying Lee 
14456f8d59d8SJan Pechanec /*
14466f8d59d8SJan Pechanec  * Note that "none" is a special path having the same affect on sshd
14476f8d59d8SJan Pechanec  * configuration as not specifying ChrootDirectory at all.
14486f8d59d8SJan Pechanec  */
14496f8d59d8SJan Pechanec int
14506f8d59d8SJan Pechanec chroot_requested(char *chroot_directory)
14516f8d59d8SJan Pechanec {
14526f8d59d8SJan Pechanec 	return (chroot_directory != NULL &&
14536f8d59d8SJan Pechanec 	    strcasecmp(chroot_directory, "none") != 0);
14546f8d59d8SJan Pechanec }
1455