Annotation of src/usr.bin/ssh/servconf.c, Revision 1.178
1.178 ! pyr 1: /* $OpenBSD: servconf.c,v 1.177 2008/02/10 10:54:28 djm Exp $ */
1.1 deraadt 2: /*
1.26 deraadt 3: * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
4: * All rights reserved
1.34 markus 5: *
1.51 deraadt 6: * As far as I am concerned, the code I have written for this software
7: * can be used freely for any purpose. Any derived versions of this
8: * software must be clearly marked as such, and if the derived work is
9: * incompatible with the protocol description in the RFC file, it must be
10: * called by a name other than "ssh" or "Secure Shell".
1.26 deraadt 11: */
1.1 deraadt 12:
1.152 stevesk 13: #include <sys/types.h>
14: #include <sys/socket.h>
1.154 stevesk 15:
16: #include <netdb.h>
1.165 dtucker 17: #include <pwd.h>
1.162 stevesk 18: #include <stdio.h>
1.161 stevesk 19: #include <stdlib.h>
1.160 stevesk 20: #include <string.h>
1.164 deraadt 21: #include <signal.h>
1.155 stevesk 22: #include <unistd.h>
1.164 deraadt 23: #include <stdarg.h>
1.1 deraadt 24:
1.164 deraadt 25: #include "xmalloc.h"
1.1 deraadt 26: #include "ssh.h"
1.62 markus 27: #include "log.h"
1.164 deraadt 28: #include "buffer.h"
1.1 deraadt 29: #include "servconf.h"
1.33 markus 30: #include "compat.h"
1.60 markus 31: #include "pathnames.h"
1.62 markus 32: #include "misc.h"
33: #include "cipher.h"
1.164 deraadt 34: #include "key.h"
1.66 markus 35: #include "kex.h"
36: #include "mac.h"
1.153 dtucker 37: #include "match.h"
1.156 dtucker 38: #include "channels.h"
1.165 dtucker 39: #include "groupaccess.h"
1.1 deraadt 40:
1.84 itojun 41: static void add_listen_addr(ServerOptions *, char *, u_short);
42: static void add_one_listen_addr(ServerOptions *, char *, u_short);
1.29 markus 43:
1.102 provos 44: /* Use of privilege separation or not */
45: extern int use_privsep;
1.153 dtucker 46: extern Buffer cfg;
1.62 markus 47:
1.1 deraadt 48: /* Initializes the server options to their default values. */
49:
1.34 markus 50: void
1.25 markus 51: initialize_server_options(ServerOptions *options)
1.1 deraadt 52: {
1.25 markus 53: memset(options, 0, sizeof(*options));
1.29 markus 54: options->num_ports = 0;
55: options->ports_from_cmdline = 0;
56: options->listen_addrs = NULL;
1.138 djm 57: options->address_family = -1;
1.54 markus 58: options->num_host_key_files = 0;
1.36 markus 59: options->pid_file = NULL;
1.25 markus 60: options->server_key_bits = -1;
61: options->login_grace_time = -1;
62: options->key_regeneration_time = -1;
1.67 markus 63: options->permit_root_login = PERMIT_NOT_SET;
1.25 markus 64: options->ignore_rhosts = -1;
65: options->ignore_user_known_hosts = -1;
66: options->print_motd = -1;
1.72 stevesk 67: options->print_lastlog = -1;
1.25 markus 68: options->x11_forwarding = -1;
69: options->x11_display_offset = -1;
1.99 stevesk 70: options->x11_use_localhost = -1;
1.42 markus 71: options->xauth_location = NULL;
1.25 markus 72: options->strict_modes = -1;
1.129 markus 73: options->tcp_keep_alive = -1;
1.101 markus 74: options->log_facility = SYSLOG_FACILITY_NOT_SET;
75: options->log_level = SYSLOG_LEVEL_NOT_SET;
1.25 markus 76: options->rhosts_rsa_authentication = -1;
1.75 markus 77: options->hostbased_authentication = -1;
78: options->hostbased_uses_name_from_packet_only = -1;
1.25 markus 79: options->rsa_authentication = -1;
1.54 markus 80: options->pubkey_authentication = -1;
1.25 markus 81: options->kerberos_authentication = -1;
82: options->kerberos_or_local_passwd = -1;
83: options->kerberos_ticket_cleanup = -1;
1.130 jakob 84: options->kerberos_get_afs_token = -1;
1.125 markus 85: options->gss_authentication=-1;
86: options->gss_cleanup_creds = -1;
1.25 markus 87: options->password_authentication = -1;
1.52 markus 88: options->kbd_interactive_authentication = -1;
1.80 markus 89: options->challenge_response_authentication = -1;
1.25 markus 90: options->permit_empty_passwd = -1;
1.113 markus 91: options->permit_user_env = -1;
1.25 markus 92: options->use_login = -1;
1.111 markus 93: options->compression = -1;
1.53 markus 94: options->allow_tcp_forwarding = -1;
1.178 ! pyr 95: options->allow_agent_forwarding = -1;
1.25 markus 96: options->num_allow_users = 0;
97: options->num_deny_users = 0;
98: options->num_allow_groups = 0;
99: options->num_deny_groups = 0;
1.33 markus 100: options->ciphers = NULL;
1.66 markus 101: options->macs = NULL;
1.33 markus 102: options->protocol = SSH_PROTO_UNKNOWN;
1.38 markus 103: options->gateway_ports = -1;
1.43 jakob 104: options->num_subsystems = 0;
1.50 markus 105: options->max_startups_begin = -1;
106: options->max_startups_rate = -1;
1.46 markus 107: options->max_startups = -1;
1.133 dtucker 108: options->max_authtries = -1;
1.57 markus 109: options->banner = NULL;
1.122 markus 110: options->use_dns = -1;
1.77 beck 111: options->client_alive_interval = -1;
112: options->client_alive_count_max = -1;
1.82 markus 113: options->authorized_keys_file = NULL;
114: options->authorized_keys_file2 = NULL;
1.131 djm 115: options->num_accept_env = 0;
1.145 reyk 116: options->permit_tun = -1;
1.159 dtucker 117: options->num_permitted_opens = -1;
1.158 dtucker 118: options->adm_forced_command = NULL;
1.176 djm 119: options->chroot_directory = NULL;
1.1 deraadt 120: }
121:
1.34 markus 122: void
1.25 markus 123: fill_default_server_options(ServerOptions *options)
1.1 deraadt 124: {
1.54 markus 125: if (options->protocol == SSH_PROTO_UNKNOWN)
126: options->protocol = SSH_PROTO_1|SSH_PROTO_2;
127: if (options->num_host_key_files == 0) {
128: /* fill default hostkeys for protocols */
129: if (options->protocol & SSH_PROTO_1)
1.97 stevesk 130: options->host_key_files[options->num_host_key_files++] =
131: _PATH_HOST_KEY_FILE;
132: if (options->protocol & SSH_PROTO_2) {
133: options->host_key_files[options->num_host_key_files++] =
134: _PATH_HOST_RSA_KEY_FILE;
135: options->host_key_files[options->num_host_key_files++] =
136: _PATH_HOST_DSA_KEY_FILE;
137: }
1.54 markus 138: }
1.29 markus 139: if (options->num_ports == 0)
140: options->ports[options->num_ports++] = SSH_DEFAULT_PORT;
141: if (options->listen_addrs == NULL)
1.76 stevesk 142: add_listen_addr(options, NULL, 0);
1.36 markus 143: if (options->pid_file == NULL)
1.60 markus 144: options->pid_file = _PATH_SSH_DAEMON_PID_FILE;
1.25 markus 145: if (options->server_key_bits == -1)
146: options->server_key_bits = 768;
147: if (options->login_grace_time == -1)
1.115 stevesk 148: options->login_grace_time = 120;
1.25 markus 149: if (options->key_regeneration_time == -1)
150: options->key_regeneration_time = 3600;
1.67 markus 151: if (options->permit_root_login == PERMIT_NOT_SET)
152: options->permit_root_login = PERMIT_YES;
1.25 markus 153: if (options->ignore_rhosts == -1)
1.30 markus 154: options->ignore_rhosts = 1;
1.25 markus 155: if (options->ignore_user_known_hosts == -1)
156: options->ignore_user_known_hosts = 0;
157: if (options->print_motd == -1)
158: options->print_motd = 1;
1.72 stevesk 159: if (options->print_lastlog == -1)
160: options->print_lastlog = 1;
1.25 markus 161: if (options->x11_forwarding == -1)
1.30 markus 162: options->x11_forwarding = 0;
1.25 markus 163: if (options->x11_display_offset == -1)
1.30 markus 164: options->x11_display_offset = 10;
1.99 stevesk 165: if (options->x11_use_localhost == -1)
166: options->x11_use_localhost = 1;
1.42 markus 167: if (options->xauth_location == NULL)
1.83 markus 168: options->xauth_location = _PATH_XAUTH;
1.25 markus 169: if (options->strict_modes == -1)
170: options->strict_modes = 1;
1.129 markus 171: if (options->tcp_keep_alive == -1)
172: options->tcp_keep_alive = 1;
1.101 markus 173: if (options->log_facility == SYSLOG_FACILITY_NOT_SET)
1.25 markus 174: options->log_facility = SYSLOG_FACILITY_AUTH;
1.101 markus 175: if (options->log_level == SYSLOG_LEVEL_NOT_SET)
1.58 markus 176: options->log_level = SYSLOG_LEVEL_INFO;
1.25 markus 177: if (options->rhosts_rsa_authentication == -1)
1.30 markus 178: options->rhosts_rsa_authentication = 0;
1.75 markus 179: if (options->hostbased_authentication == -1)
180: options->hostbased_authentication = 0;
181: if (options->hostbased_uses_name_from_packet_only == -1)
182: options->hostbased_uses_name_from_packet_only = 0;
1.25 markus 183: if (options->rsa_authentication == -1)
184: options->rsa_authentication = 1;
1.54 markus 185: if (options->pubkey_authentication == -1)
186: options->pubkey_authentication = 1;
1.25 markus 187: if (options->kerberos_authentication == -1)
1.107 markus 188: options->kerberos_authentication = 0;
1.25 markus 189: if (options->kerberos_or_local_passwd == -1)
190: options->kerberos_or_local_passwd = 1;
191: if (options->kerberos_ticket_cleanup == -1)
192: options->kerberos_ticket_cleanup = 1;
1.130 jakob 193: if (options->kerberos_get_afs_token == -1)
194: options->kerberos_get_afs_token = 0;
1.125 markus 195: if (options->gss_authentication == -1)
196: options->gss_authentication = 0;
197: if (options->gss_cleanup_creds == -1)
198: options->gss_cleanup_creds = 1;
1.25 markus 199: if (options->password_authentication == -1)
200: options->password_authentication = 1;
1.52 markus 201: if (options->kbd_interactive_authentication == -1)
202: options->kbd_interactive_authentication = 0;
1.80 markus 203: if (options->challenge_response_authentication == -1)
204: options->challenge_response_authentication = 1;
1.25 markus 205: if (options->permit_empty_passwd == -1)
1.30 markus 206: options->permit_empty_passwd = 0;
1.113 markus 207: if (options->permit_user_env == -1)
208: options->permit_user_env = 0;
1.25 markus 209: if (options->use_login == -1)
210: options->use_login = 0;
1.111 markus 211: if (options->compression == -1)
1.143 markus 212: options->compression = COMP_DELAYED;
1.53 markus 213: if (options->allow_tcp_forwarding == -1)
214: options->allow_tcp_forwarding = 1;
1.178 ! pyr 215: if (options->allow_agent_forwarding == -1)
! 216: options->allow_agent_forwarding = 1;
1.38 markus 217: if (options->gateway_ports == -1)
218: options->gateway_ports = 0;
1.46 markus 219: if (options->max_startups == -1)
220: options->max_startups = 10;
1.50 markus 221: if (options->max_startups_rate == -1)
222: options->max_startups_rate = 100; /* 100% */
223: if (options->max_startups_begin == -1)
224: options->max_startups_begin = options->max_startups;
1.133 dtucker 225: if (options->max_authtries == -1)
226: options->max_authtries = DEFAULT_AUTH_FAIL_MAX;
1.122 markus 227: if (options->use_dns == -1)
228: options->use_dns = 1;
1.77 beck 229: if (options->client_alive_interval == -1)
1.95 deraadt 230: options->client_alive_interval = 0;
1.77 beck 231: if (options->client_alive_count_max == -1)
232: options->client_alive_count_max = 3;
1.90 markus 233: if (options->authorized_keys_file2 == NULL) {
234: /* authorized_keys_file2 falls back to authorized_keys_file */
235: if (options->authorized_keys_file != NULL)
236: options->authorized_keys_file2 = options->authorized_keys_file;
237: else
238: options->authorized_keys_file2 = _PATH_SSH_USER_PERMITTED_KEYS2;
239: }
240: if (options->authorized_keys_file == NULL)
241: options->authorized_keys_file = _PATH_SSH_USER_PERMITTED_KEYS;
1.145 reyk 242: if (options->permit_tun == -1)
1.146 reyk 243: options->permit_tun = SSH_TUNMODE_NO;
1.102 provos 244:
1.110 markus 245: /* Turn privilege separation on by default */
1.102 provos 246: if (use_privsep == -1)
1.110 markus 247: use_privsep = 1;
1.1 deraadt 248: }
249:
250: /* Keyword tokens. */
1.25 markus 251: typedef enum {
252: sBadOption, /* == unknown option */
253: sPort, sHostKeyFile, sServerKeyBits, sLoginGraceTime, sKeyRegenerationTime,
254: sPermitRootLogin, sLogFacility, sLogLevel,
1.124 markus 255: sRhostsRSAAuthentication, sRSAAuthentication,
1.25 markus 256: sKerberosAuthentication, sKerberosOrLocalPasswd, sKerberosTicketCleanup,
1.130 jakob 257: sKerberosGetAFSToken,
1.123 markus 258: sKerberosTgtPassing, sChallengeResponseAuthentication,
1.138 djm 259: sPasswordAuthentication, sKbdInteractiveAuthentication,
260: sListenAddress, sAddressFamily,
1.72 stevesk 261: sPrintMotd, sPrintLastLog, sIgnoreRhosts,
1.99 stevesk 262: sX11Forwarding, sX11DisplayOffset, sX11UseLocalhost,
1.129 markus 263: sStrictModes, sEmptyPasswd, sTCPKeepAlive,
1.113 markus 264: sPermitUserEnvironment, sUseLogin, sAllowTcpForwarding, sCompression,
1.53 markus 265: sAllowUsers, sDenyUsers, sAllowGroups, sDenyGroups,
1.66 markus 266: sIgnoreUserKnownHosts, sCiphers, sMacs, sProtocol, sPidFile,
1.133 dtucker 267: sGatewayPorts, sPubkeyAuthentication, sXAuthLocation, sSubsystem,
268: sMaxStartups, sMaxAuthTries,
1.122 markus 269: sBanner, sUseDNS, sHostbasedAuthentication,
1.95 deraadt 270: sHostbasedUsesNameFromPacketOnly, sClientAliveInterval,
1.89 jakob 271: sClientAliveCountMax, sAuthorizedKeysFile, sAuthorizedKeysFile2,
1.145 reyk 272: sGssAuthentication, sGssCleanupCreds, sAcceptEnv, sPermitTunnel,
1.176 djm 273: sMatch, sPermitOpen, sForceCommand, sChrootDirectory,
1.178 ! pyr 274: sUsePrivilegeSeparation, sAllowAgentForwarding,
1.121 jakob 275: sDeprecated, sUnsupported
1.1 deraadt 276: } ServerOpCodes;
277:
1.153 dtucker 278: #define SSHCFG_GLOBAL 0x01 /* allowed in main section of sshd_config */
279: #define SSHCFG_MATCH 0x02 /* allowed inside a Match section */
280: #define SSHCFG_ALL (SSHCFG_GLOBAL|SSHCFG_MATCH)
281:
1.1 deraadt 282: /* Textual representation of the tokens. */
1.25 markus 283: static struct {
284: const char *name;
285: ServerOpCodes opcode;
1.153 dtucker 286: u_int flags;
1.25 markus 287: } keywords[] = {
1.153 dtucker 288: { "port", sPort, SSHCFG_GLOBAL },
289: { "hostkey", sHostKeyFile, SSHCFG_GLOBAL },
290: { "hostdsakey", sHostKeyFile, SSHCFG_GLOBAL }, /* alias */
291: { "pidfile", sPidFile, SSHCFG_GLOBAL },
292: { "serverkeybits", sServerKeyBits, SSHCFG_GLOBAL },
293: { "logingracetime", sLoginGraceTime, SSHCFG_GLOBAL },
294: { "keyregenerationinterval", sKeyRegenerationTime, SSHCFG_GLOBAL },
1.175 dtucker 295: { "permitrootlogin", sPermitRootLogin, SSHCFG_ALL },
1.153 dtucker 296: { "syslogfacility", sLogFacility, SSHCFG_GLOBAL },
297: { "loglevel", sLogLevel, SSHCFG_GLOBAL },
298: { "rhostsauthentication", sDeprecated, SSHCFG_GLOBAL },
1.168 dtucker 299: { "rhostsrsaauthentication", sRhostsRSAAuthentication, SSHCFG_ALL },
300: { "hostbasedauthentication", sHostbasedAuthentication, SSHCFG_ALL },
1.153 dtucker 301: { "hostbasedusesnamefrompacketonly", sHostbasedUsesNameFromPacketOnly, SSHCFG_GLOBAL },
1.168 dtucker 302: { "rsaauthentication", sRSAAuthentication, SSHCFG_ALL },
303: { "pubkeyauthentication", sPubkeyAuthentication, SSHCFG_ALL },
1.153 dtucker 304: { "dsaauthentication", sPubkeyAuthentication, SSHCFG_GLOBAL }, /* alias */
1.123 markus 305: #ifdef KRB5
1.168 dtucker 306: { "kerberosauthentication", sKerberosAuthentication, SSHCFG_ALL },
1.153 dtucker 307: { "kerberosorlocalpasswd", sKerberosOrLocalPasswd, SSHCFG_GLOBAL },
308: { "kerberosticketcleanup", sKerberosTicketCleanup, SSHCFG_GLOBAL },
309: { "kerberosgetafstoken", sKerberosGetAFSToken, SSHCFG_GLOBAL },
1.121 jakob 310: #else
1.168 dtucker 311: { "kerberosauthentication", sUnsupported, SSHCFG_ALL },
1.153 dtucker 312: { "kerberosorlocalpasswd", sUnsupported, SSHCFG_GLOBAL },
313: { "kerberosticketcleanup", sUnsupported, SSHCFG_GLOBAL },
314: { "kerberosgetafstoken", sUnsupported, SSHCFG_GLOBAL },
1.126 markus 315: #endif
1.153 dtucker 316: { "kerberostgtpassing", sUnsupported, SSHCFG_GLOBAL },
317: { "afstokenpassing", sUnsupported, SSHCFG_GLOBAL },
1.125 markus 318: #ifdef GSSAPI
1.168 dtucker 319: { "gssapiauthentication", sGssAuthentication, SSHCFG_ALL },
1.153 dtucker 320: { "gssapicleanupcredentials", sGssCleanupCreds, SSHCFG_GLOBAL },
1.125 markus 321: #else
1.168 dtucker 322: { "gssapiauthentication", sUnsupported, SSHCFG_ALL },
1.153 dtucker 323: { "gssapicleanupcredentials", sUnsupported, SSHCFG_GLOBAL },
1.125 markus 324: #endif
1.168 dtucker 325: { "passwordauthentication", sPasswordAuthentication, SSHCFG_ALL },
326: { "kbdinteractiveauthentication", sKbdInteractiveAuthentication, SSHCFG_ALL },
1.170 dtucker 327: { "challengeresponseauthentication", sChallengeResponseAuthentication, SSHCFG_GLOBAL },
1.153 dtucker 328: { "skeyauthentication", sChallengeResponseAuthentication, SSHCFG_GLOBAL }, /* alias */
329: { "checkmail", sDeprecated, SSHCFG_GLOBAL },
330: { "listenaddress", sListenAddress, SSHCFG_GLOBAL },
331: { "addressfamily", sAddressFamily, SSHCFG_GLOBAL },
332: { "printmotd", sPrintMotd, SSHCFG_GLOBAL },
333: { "printlastlog", sPrintLastLog, SSHCFG_GLOBAL },
334: { "ignorerhosts", sIgnoreRhosts, SSHCFG_GLOBAL },
335: { "ignoreuserknownhosts", sIgnoreUserKnownHosts, SSHCFG_GLOBAL },
1.157 dtucker 336: { "x11forwarding", sX11Forwarding, SSHCFG_ALL },
337: { "x11displayoffset", sX11DisplayOffset, SSHCFG_ALL },
338: { "x11uselocalhost", sX11UseLocalhost, SSHCFG_ALL },
1.153 dtucker 339: { "xauthlocation", sXAuthLocation, SSHCFG_GLOBAL },
340: { "strictmodes", sStrictModes, SSHCFG_GLOBAL },
341: { "permitemptypasswords", sEmptyPasswd, SSHCFG_GLOBAL },
342: { "permituserenvironment", sPermitUserEnvironment, SSHCFG_GLOBAL },
343: { "uselogin", sUseLogin, SSHCFG_GLOBAL },
344: { "compression", sCompression, SSHCFG_GLOBAL },
345: { "tcpkeepalive", sTCPKeepAlive, SSHCFG_GLOBAL },
346: { "keepalive", sTCPKeepAlive, SSHCFG_GLOBAL }, /* obsolete alias */
347: { "allowtcpforwarding", sAllowTcpForwarding, SSHCFG_ALL },
1.178 ! pyr 348: { "allowagentforwarding", sAllowAgentForwarding, SSHCFG_ALL },
1.153 dtucker 349: { "allowusers", sAllowUsers, SSHCFG_GLOBAL },
350: { "denyusers", sDenyUsers, SSHCFG_GLOBAL },
351: { "allowgroups", sAllowGroups, SSHCFG_GLOBAL },
352: { "denygroups", sDenyGroups, SSHCFG_GLOBAL },
353: { "ciphers", sCiphers, SSHCFG_GLOBAL },
354: { "macs", sMacs, SSHCFG_GLOBAL },
355: { "protocol", sProtocol, SSHCFG_GLOBAL },
356: { "gatewayports", sGatewayPorts, SSHCFG_ALL },
357: { "subsystem", sSubsystem, SSHCFG_GLOBAL },
358: { "maxstartups", sMaxStartups, SSHCFG_GLOBAL },
359: { "maxauthtries", sMaxAuthTries, SSHCFG_GLOBAL },
1.168 dtucker 360: { "banner", sBanner, SSHCFG_ALL },
1.153 dtucker 361: { "usedns", sUseDNS, SSHCFG_GLOBAL },
362: { "verifyreversemapping", sDeprecated, SSHCFG_GLOBAL },
363: { "reversemappingcheck", sDeprecated, SSHCFG_GLOBAL },
364: { "clientaliveinterval", sClientAliveInterval, SSHCFG_GLOBAL },
365: { "clientalivecountmax", sClientAliveCountMax, SSHCFG_GLOBAL },
366: { "authorizedkeysfile", sAuthorizedKeysFile, SSHCFG_GLOBAL },
367: { "authorizedkeysfile2", sAuthorizedKeysFile2, SSHCFG_GLOBAL },
368: { "useprivilegeseparation", sUsePrivilegeSeparation, SSHCFG_GLOBAL},
369: { "acceptenv", sAcceptEnv, SSHCFG_GLOBAL },
370: { "permittunnel", sPermitTunnel, SSHCFG_GLOBAL },
371: { "match", sMatch, SSHCFG_ALL },
1.156 dtucker 372: { "permitopen", sPermitOpen, SSHCFG_ALL },
1.158 dtucker 373: { "forcecommand", sForceCommand, SSHCFG_ALL },
1.176 djm 374: { "chrootdirectory", sChrootDirectory, SSHCFG_ALL },
1.153 dtucker 375: { NULL, sBadOption, 0 }
1.1 deraadt 376: };
377:
1.27 markus 378: /*
1.73 stevesk 379: * Returns the number of the token pointed to by cp or sBadOption.
1.27 markus 380: */
1.1 deraadt 381:
1.34 markus 382: static ServerOpCodes
1.25 markus 383: parse_token(const char *cp, const char *filename,
1.153 dtucker 384: int linenum, u_int *flags)
1.1 deraadt 385: {
1.55 markus 386: u_int i;
1.1 deraadt 387:
1.25 markus 388: for (i = 0; keywords[i].name; i++)
1.153 dtucker 389: if (strcasecmp(cp, keywords[i].name) == 0) {
390: *flags = keywords[i].flags;
1.25 markus 391: return keywords[i].opcode;
1.153 dtucker 392: }
1.25 markus 393:
1.78 stevesk 394: error("%s: line %d: Bad configuration option: %s",
395: filename, linenum, cp);
1.25 markus 396: return sBadOption;
1.1 deraadt 397: }
398:
1.84 itojun 399: static void
1.76 stevesk 400: add_listen_addr(ServerOptions *options, char *addr, u_short port)
1.74 stevesk 401: {
1.142 djm 402: u_int i;
1.74 stevesk 403:
404: if (options->num_ports == 0)
405: options->ports[options->num_ports++] = SSH_DEFAULT_PORT;
1.138 djm 406: if (options->address_family == -1)
407: options->address_family = AF_UNSPEC;
1.76 stevesk 408: if (port == 0)
1.74 stevesk 409: for (i = 0; i < options->num_ports; i++)
410: add_one_listen_addr(options, addr, options->ports[i]);
411: else
1.76 stevesk 412: add_one_listen_addr(options, addr, port);
1.74 stevesk 413: }
414:
1.84 itojun 415: static void
1.74 stevesk 416: add_one_listen_addr(ServerOptions *options, char *addr, u_short port)
1.29 markus 417: {
418: struct addrinfo hints, *ai, *aitop;
419: char strport[NI_MAXSERV];
420: int gaierr;
421:
1.74 stevesk 422: memset(&hints, 0, sizeof(hints));
1.138 djm 423: hints.ai_family = options->address_family;
1.74 stevesk 424: hints.ai_socktype = SOCK_STREAM;
425: hints.ai_flags = (addr == NULL) ? AI_PASSIVE : 0;
1.112 deraadt 426: snprintf(strport, sizeof strport, "%u", port);
1.74 stevesk 427: if ((gaierr = getaddrinfo(addr, strport, &hints, &aitop)) != 0)
428: fatal("bad addr or host: %s (%s)",
429: addr ? addr : "<NULL>",
1.173 dtucker 430: ssh_gai_strerror(gaierr));
1.74 stevesk 431: for (ai = aitop; ai->ai_next; ai = ai->ai_next)
432: ;
433: ai->ai_next = options->listen_addrs;
434: options->listen_addrs = aitop;
1.29 markus 435: }
436:
1.153 dtucker 437: /*
438: * The strategy for the Match blocks is that the config file is parsed twice.
439: *
440: * The first time is at startup. activep is initialized to 1 and the
441: * directives in the global context are processed and acted on. Hitting a
442: * Match directive unsets activep and the directives inside the block are
443: * checked for syntax only.
444: *
445: * The second time is after a connection has been established but before
446: * authentication. activep is initialized to 2 and global config directives
447: * are ignored since they have already been processed. If the criteria in a
448: * Match block is met, activep is set and the subsequent directives
449: * processed and actioned until EOF or another Match block unsets it. Any
450: * options set are copied into the main server config.
451: *
452: * Potential additions/improvements:
453: * - Add Match support for pre-kex directives, eg Protocol, Ciphers.
454: *
455: * - Add a Tag directive (idea from David Leonard) ala pf, eg:
456: * Match Address 192.168.0.*
457: * Tag trusted
458: * Match Group wheel
459: * Tag trusted
460: * Match Tag trusted
461: * AllowTcpForwarding yes
462: * GatewayPorts clientspecified
463: * [...]
464: *
465: * - Add a PermittedChannelRequests directive
466: * Match Group shell
467: * PermittedChannelRequests session,forwarded-tcpip
468: */
469:
470: static int
1.165 dtucker 471: match_cfg_line_group(const char *grps, int line, const char *user)
472: {
473: int result = 0;
474: u_int ngrps = 0;
475: char *arg, *p, *cp, *grplist[MAX_MATCH_GROUPS];
476: struct passwd *pw;
477:
478: /*
479: * Even if we do not have a user yet, we still need to check for
480: * valid syntax.
481: */
482: arg = cp = xstrdup(grps);
483: while ((p = strsep(&cp, ",")) != NULL && *p != '\0') {
484: if (ngrps >= MAX_MATCH_GROUPS) {
485: error("line %d: too many groups in Match Group", line);
486: result = -1;
487: goto out;
488: }
489: grplist[ngrps++] = p;
490: }
491:
492: if (user == NULL)
493: goto out;
494:
495: if ((pw = getpwnam(user)) == NULL) {
496: debug("Can't match group at line %d because user %.100s does "
497: "not exist", line, user);
498: } else if (ga_init(pw->pw_name, pw->pw_gid) == 0) {
499: debug("Can't Match group because user %.100s not in any group "
500: "at line %d", user, line);
501: } else if (ga_match(grplist, ngrps) != 1) {
502: debug("user %.100s does not match group %.100s at line %d",
503: user, arg, line);
504: } else {
505: debug("user %.100s matched group %.100s at line %d", user,
506: arg, line);
507: result = 1;
508: }
509: out:
510: ga_free();
511: xfree(arg);
512: return result;
513: }
514:
515: static int
1.153 dtucker 516: match_cfg_line(char **condition, int line, const char *user, const char *host,
517: const char *address)
518: {
519: int result = 1;
520: char *arg, *attrib, *cp = *condition;
521: size_t len;
522:
523: if (user == NULL)
524: debug3("checking syntax for 'Match %s'", cp);
525: else
526: debug3("checking match for '%s' user %s host %s addr %s", cp,
527: user ? user : "(null)", host ? host : "(null)",
528: address ? address : "(null)");
529:
530: while ((attrib = strdelim(&cp)) && *attrib != '\0') {
531: if ((arg = strdelim(&cp)) == NULL || *arg == '\0') {
532: error("Missing Match criteria for %s", attrib);
533: return -1;
534: }
535: len = strlen(arg);
536: if (strcasecmp(attrib, "user") == 0) {
537: if (!user) {
538: result = 0;
539: continue;
540: }
541: if (match_pattern_list(user, arg, len, 0) != 1)
542: result = 0;
543: else
544: debug("user %.100s matched 'User %.100s' at "
545: "line %d", user, arg, line);
1.165 dtucker 546: } else if (strcasecmp(attrib, "group") == 0) {
547: switch (match_cfg_line_group(arg, line, user)) {
548: case -1:
549: return -1;
550: case 0:
551: result = 0;
552: }
1.153 dtucker 553: } else if (strcasecmp(attrib, "host") == 0) {
554: if (!host) {
555: result = 0;
556: continue;
557: }
558: if (match_hostname(host, arg, len) != 1)
559: result = 0;
560: else
561: debug("connection from %.100s matched 'Host "
562: "%.100s' at line %d", host, arg, line);
563: } else if (strcasecmp(attrib, "address") == 0) {
564: if (!address) {
565: result = 0;
566: continue;
567: }
568: if (match_hostname(address, arg, len) != 1)
569: result = 0;
570: else
571: debug("connection from %.100s matched 'Address "
572: "%.100s' at line %d", address, arg, line);
573: } else {
574: error("Unsupported Match attribute %s", attrib);
575: return -1;
576: }
577: }
578: if (user != NULL)
579: debug3("match %sfound", result ? "" : "not ");
580: *condition = cp;
581: return result;
582: }
583:
1.158 dtucker 584: #define WHITESPACE " \t\r\n"
585:
1.94 markus 586: int
587: process_server_config_line(ServerOptions *options, char *line,
1.153 dtucker 588: const char *filename, int linenum, int *activep, const char *user,
589: const char *host, const char *address)
1.1 deraadt 590: {
1.74 stevesk 591: char *cp, **charptr, *arg, *p;
1.153 dtucker 592: int cmdline = 0, *intptr, value, n;
1.174 dtucker 593: SyslogFacility *log_facility_ptr;
594: LogLevel *log_level_ptr;
1.25 markus 595: ServerOpCodes opcode;
1.139 djm 596: u_short port;
1.153 dtucker 597: u_int i, flags = 0;
1.151 djm 598: size_t len;
1.25 markus 599:
1.94 markus 600: cp = line;
1.148 dtucker 601: if ((arg = strdelim(&cp)) == NULL)
1.147 djm 602: return 0;
1.94 markus 603: /* Ignore leading whitespace */
604: if (*arg == '\0')
605: arg = strdelim(&cp);
606: if (!arg || !*arg || *arg == '#')
607: return 0;
608: intptr = NULL;
609: charptr = NULL;
1.153 dtucker 610: opcode = parse_token(arg, filename, linenum, &flags);
611:
612: if (activep == NULL) { /* We are processing a command line directive */
613: cmdline = 1;
614: activep = &cmdline;
615: }
616: if (*activep && opcode != sMatch)
617: debug3("%s:%d setting %s %s", filename, linenum, arg, cp);
618: if (*activep == 0 && !(flags & SSHCFG_MATCH)) {
619: if (user == NULL) {
620: fatal("%s line %d: Directive '%s' is not allowed "
621: "within a Match block", filename, linenum, arg);
622: } else { /* this is a directive we have already processed */
623: while (arg)
624: arg = strdelim(&cp);
625: return 0;
626: }
627: }
628:
1.94 markus 629: switch (opcode) {
630: case sBadOption:
631: return -1;
632: case sPort:
633: /* ignore ports from configfile if cmdline specifies ports */
634: if (options->ports_from_cmdline)
635: return 0;
636: if (options->listen_addrs != NULL)
637: fatal("%s line %d: ports must be specified before "
1.98 stevesk 638: "ListenAddress.", filename, linenum);
1.94 markus 639: if (options->num_ports >= MAX_PORTS)
640: fatal("%s line %d: too many ports.",
641: filename, linenum);
1.48 provos 642: arg = strdelim(&cp);
1.94 markus 643: if (!arg || *arg == '\0')
644: fatal("%s line %d: missing port number.",
645: filename, linenum);
646: options->ports[options->num_ports++] = a2port(arg);
647: if (options->ports[options->num_ports-1] == 0)
648: fatal("%s line %d: Badly formatted port number.",
649: filename, linenum);
650: break;
1.29 markus 651:
1.94 markus 652: case sServerKeyBits:
653: intptr = &options->server_key_bits;
1.25 markus 654: parse_int:
1.94 markus 655: arg = strdelim(&cp);
656: if (!arg || *arg == '\0')
657: fatal("%s line %d: missing integer value.",
658: filename, linenum);
659: value = atoi(arg);
1.153 dtucker 660: if (*activep && *intptr == -1)
1.94 markus 661: *intptr = value;
662: break;
1.25 markus 663:
1.94 markus 664: case sLoginGraceTime:
665: intptr = &options->login_grace_time;
1.81 stevesk 666: parse_time:
1.94 markus 667: arg = strdelim(&cp);
668: if (!arg || *arg == '\0')
669: fatal("%s line %d: missing time value.",
670: filename, linenum);
671: if ((value = convtime(arg)) == -1)
672: fatal("%s line %d: invalid time value.",
673: filename, linenum);
674: if (*intptr == -1)
675: *intptr = value;
676: break;
677:
678: case sKeyRegenerationTime:
679: intptr = &options->key_regeneration_time;
680: goto parse_time;
681:
682: case sListenAddress:
683: arg = strdelim(&cp);
1.139 djm 684: if (arg == NULL || *arg == '\0')
685: fatal("%s line %d: missing address",
1.94 markus 686: filename, linenum);
1.144 dtucker 687: /* check for bare IPv6 address: no "[]" and 2 or more ":" */
688: if (strchr(arg, '[') == NULL && (p = strchr(arg, ':')) != NULL
689: && strchr(p+1, ':') != NULL) {
690: add_listen_addr(options, arg, 0);
691: break;
692: }
1.139 djm 693: p = hpdelim(&arg);
694: if (p == NULL)
695: fatal("%s line %d: bad address:port usage",
696: filename, linenum);
697: p = cleanhostname(p);
698: if (arg == NULL)
699: port = 0;
700: else if ((port = a2port(arg)) == 0)
701: fatal("%s line %d: bad port number", filename, linenum);
702:
703: add_listen_addr(options, p, port);
1.25 markus 704:
1.138 djm 705: break;
706:
707: case sAddressFamily:
708: arg = strdelim(&cp);
1.141 markus 709: if (!arg || *arg == '\0')
710: fatal("%s line %d: missing address family.",
711: filename, linenum);
1.138 djm 712: intptr = &options->address_family;
713: if (options->listen_addrs != NULL)
714: fatal("%s line %d: address family must be specified before "
715: "ListenAddress.", filename, linenum);
716: if (strcasecmp(arg, "inet") == 0)
717: value = AF_INET;
718: else if (strcasecmp(arg, "inet6") == 0)
719: value = AF_INET6;
720: else if (strcasecmp(arg, "any") == 0)
721: value = AF_UNSPEC;
722: else
723: fatal("%s line %d: unsupported address family \"%s\".",
724: filename, linenum, arg);
725: if (*intptr == -1)
726: *intptr = value;
1.94 markus 727: break;
728:
729: case sHostKeyFile:
730: intptr = &options->num_host_key_files;
731: if (*intptr >= MAX_HOSTKEYS)
732: fatal("%s line %d: too many host keys specified (max %d).",
733: filename, linenum, MAX_HOSTKEYS);
734: charptr = &options->host_key_files[*intptr];
735: parse_filename:
736: arg = strdelim(&cp);
737: if (!arg || *arg == '\0')
738: fatal("%s line %d: missing file name.",
739: filename, linenum);
1.153 dtucker 740: if (*activep && *charptr == NULL) {
1.94 markus 741: *charptr = tilde_expand_filename(arg, getuid());
742: /* increase optional counter */
743: if (intptr != NULL)
744: *intptr = *intptr + 1;
745: }
746: break;
1.76 stevesk 747:
1.94 markus 748: case sPidFile:
749: charptr = &options->pid_file;
750: goto parse_filename;
1.25 markus 751:
1.94 markus 752: case sPermitRootLogin:
753: intptr = &options->permit_root_login;
754: arg = strdelim(&cp);
755: if (!arg || *arg == '\0')
756: fatal("%s line %d: missing yes/"
757: "without-password/forced-commands-only/no "
758: "argument.", filename, linenum);
759: value = 0; /* silence compiler */
760: if (strcmp(arg, "without-password") == 0)
761: value = PERMIT_NO_PASSWD;
762: else if (strcmp(arg, "forced-commands-only") == 0)
763: value = PERMIT_FORCED_ONLY;
764: else if (strcmp(arg, "yes") == 0)
765: value = PERMIT_YES;
766: else if (strcmp(arg, "no") == 0)
767: value = PERMIT_NO;
768: else
769: fatal("%s line %d: Bad yes/"
770: "without-password/forced-commands-only/no "
771: "argument: %s", filename, linenum, arg);
1.175 dtucker 772: if (*activep && *intptr == -1)
1.94 markus 773: *intptr = value;
774: break;
1.36 markus 775:
1.94 markus 776: case sIgnoreRhosts:
777: intptr = &options->ignore_rhosts;
1.25 markus 778: parse_flag:
1.94 markus 779: arg = strdelim(&cp);
780: if (!arg || *arg == '\0')
781: fatal("%s line %d: missing yes/no argument.",
782: filename, linenum);
783: value = 0; /* silence compiler */
784: if (strcmp(arg, "yes") == 0)
785: value = 1;
786: else if (strcmp(arg, "no") == 0)
787: value = 0;
788: else
789: fatal("%s line %d: Bad yes/no argument: %s",
790: filename, linenum, arg);
1.153 dtucker 791: if (*activep && *intptr == -1)
1.94 markus 792: *intptr = value;
793: break;
794:
795: case sIgnoreUserKnownHosts:
796: intptr = &options->ignore_user_known_hosts;
797: goto parse_flag;
798:
799: case sRhostsRSAAuthentication:
800: intptr = &options->rhosts_rsa_authentication;
801: goto parse_flag;
802:
803: case sHostbasedAuthentication:
804: intptr = &options->hostbased_authentication;
805: goto parse_flag;
806:
807: case sHostbasedUsesNameFromPacketOnly:
808: intptr = &options->hostbased_uses_name_from_packet_only;
809: goto parse_flag;
810:
811: case sRSAAuthentication:
812: intptr = &options->rsa_authentication;
813: goto parse_flag;
814:
815: case sPubkeyAuthentication:
816: intptr = &options->pubkey_authentication;
817: goto parse_flag;
1.119 jakob 818:
1.94 markus 819: case sKerberosAuthentication:
820: intptr = &options->kerberos_authentication;
821: goto parse_flag;
822:
823: case sKerberosOrLocalPasswd:
824: intptr = &options->kerberos_or_local_passwd;
825: goto parse_flag;
826:
827: case sKerberosTicketCleanup:
828: intptr = &options->kerberos_ticket_cleanup;
1.130 jakob 829: goto parse_flag;
830:
831: case sKerberosGetAFSToken:
832: intptr = &options->kerberos_get_afs_token;
1.125 markus 833: goto parse_flag;
834:
835: case sGssAuthentication:
836: intptr = &options->gss_authentication;
837: goto parse_flag;
838:
839: case sGssCleanupCreds:
840: intptr = &options->gss_cleanup_creds;
1.94 markus 841: goto parse_flag;
842:
843: case sPasswordAuthentication:
844: intptr = &options->password_authentication;
845: goto parse_flag;
846:
847: case sKbdInteractiveAuthentication:
848: intptr = &options->kbd_interactive_authentication;
849: goto parse_flag;
850:
851: case sChallengeResponseAuthentication:
852: intptr = &options->challenge_response_authentication;
853: goto parse_flag;
854:
855: case sPrintMotd:
856: intptr = &options->print_motd;
857: goto parse_flag;
858:
859: case sPrintLastLog:
860: intptr = &options->print_lastlog;
861: goto parse_flag;
862:
863: case sX11Forwarding:
864: intptr = &options->x11_forwarding;
865: goto parse_flag;
866:
867: case sX11DisplayOffset:
868: intptr = &options->x11_display_offset;
869: goto parse_int;
1.99 stevesk 870:
871: case sX11UseLocalhost:
872: intptr = &options->x11_use_localhost;
873: goto parse_flag;
1.94 markus 874:
875: case sXAuthLocation:
876: charptr = &options->xauth_location;
877: goto parse_filename;
878:
879: case sStrictModes:
880: intptr = &options->strict_modes;
881: goto parse_flag;
882:
1.129 markus 883: case sTCPKeepAlive:
884: intptr = &options->tcp_keep_alive;
1.94 markus 885: goto parse_flag;
886:
887: case sEmptyPasswd:
888: intptr = &options->permit_empty_passwd;
1.113 markus 889: goto parse_flag;
890:
891: case sPermitUserEnvironment:
892: intptr = &options->permit_user_env;
1.94 markus 893: goto parse_flag;
894:
895: case sUseLogin:
896: intptr = &options->use_login;
1.111 markus 897: goto parse_flag;
898:
899: case sCompression:
900: intptr = &options->compression;
1.143 markus 901: arg = strdelim(&cp);
902: if (!arg || *arg == '\0')
903: fatal("%s line %d: missing yes/no/delayed "
904: "argument.", filename, linenum);
905: value = 0; /* silence compiler */
906: if (strcmp(arg, "delayed") == 0)
907: value = COMP_DELAYED;
908: else if (strcmp(arg, "yes") == 0)
909: value = COMP_ZLIB;
910: else if (strcmp(arg, "no") == 0)
911: value = COMP_NONE;
912: else
913: fatal("%s line %d: Bad yes/no/delayed "
914: "argument: %s", filename, linenum, arg);
915: if (*intptr == -1)
916: *intptr = value;
917: break;
1.94 markus 918:
919: case sGatewayPorts:
920: intptr = &options->gateway_ports;
1.139 djm 921: arg = strdelim(&cp);
922: if (!arg || *arg == '\0')
923: fatal("%s line %d: missing yes/no/clientspecified "
924: "argument.", filename, linenum);
925: value = 0; /* silence compiler */
926: if (strcmp(arg, "clientspecified") == 0)
927: value = 2;
928: else if (strcmp(arg, "yes") == 0)
929: value = 1;
930: else if (strcmp(arg, "no") == 0)
931: value = 0;
932: else
933: fatal("%s line %d: Bad yes/no/clientspecified "
934: "argument: %s", filename, linenum, arg);
1.169 dtucker 935: if (*activep && *intptr == -1)
1.139 djm 936: *intptr = value;
937: break;
1.25 markus 938:
1.122 markus 939: case sUseDNS:
940: intptr = &options->use_dns;
1.94 markus 941: goto parse_flag;
1.53 markus 942:
1.94 markus 943: case sLogFacility:
1.174 dtucker 944: log_facility_ptr = &options->log_facility;
1.94 markus 945: arg = strdelim(&cp);
946: value = log_facility_number(arg);
1.101 markus 947: if (value == SYSLOG_FACILITY_NOT_SET)
1.94 markus 948: fatal("%.200s line %d: unsupported log facility '%s'",
949: filename, linenum, arg ? arg : "<NONE>");
1.174 dtucker 950: if (*log_facility_ptr == -1)
951: *log_facility_ptr = (SyslogFacility) value;
1.94 markus 952: break;
1.25 markus 953:
1.94 markus 954: case sLogLevel:
1.174 dtucker 955: log_level_ptr = &options->log_level;
1.94 markus 956: arg = strdelim(&cp);
957: value = log_level_number(arg);
1.101 markus 958: if (value == SYSLOG_LEVEL_NOT_SET)
1.94 markus 959: fatal("%.200s line %d: unsupported log level '%s'",
960: filename, linenum, arg ? arg : "<NONE>");
1.174 dtucker 961: if (*log_level_ptr == -1)
962: *log_level_ptr = (LogLevel) value;
1.94 markus 963: break;
964:
965: case sAllowTcpForwarding:
966: intptr = &options->allow_tcp_forwarding;
967: goto parse_flag;
1.102 provos 968:
1.178 ! pyr 969: case sAllowAgentForwarding:
! 970: intptr = &options->allow_agent_forwarding;
! 971: goto parse_flag;
! 972:
1.102 provos 973: case sUsePrivilegeSeparation:
974: intptr = &use_privsep;
975: goto parse_flag;
1.94 markus 976:
977: case sAllowUsers:
978: while ((arg = strdelim(&cp)) && *arg != '\0') {
979: if (options->num_allow_users >= MAX_ALLOW_USERS)
980: fatal("%s line %d: too many allow users.",
981: filename, linenum);
1.112 deraadt 982: options->allow_users[options->num_allow_users++] =
983: xstrdup(arg);
1.94 markus 984: }
985: break;
1.25 markus 986:
1.94 markus 987: case sDenyUsers:
988: while ((arg = strdelim(&cp)) && *arg != '\0') {
989: if (options->num_deny_users >= MAX_DENY_USERS)
1.163 stevesk 990: fatal("%s line %d: too many deny users.",
1.94 markus 991: filename, linenum);
1.112 deraadt 992: options->deny_users[options->num_deny_users++] =
993: xstrdup(arg);
1.94 markus 994: }
995: break;
1.25 markus 996:
1.94 markus 997: case sAllowGroups:
998: while ((arg = strdelim(&cp)) && *arg != '\0') {
999: if (options->num_allow_groups >= MAX_ALLOW_GROUPS)
1000: fatal("%s line %d: too many allow groups.",
1001: filename, linenum);
1.112 deraadt 1002: options->allow_groups[options->num_allow_groups++] =
1003: xstrdup(arg);
1.94 markus 1004: }
1005: break;
1.33 markus 1006:
1.94 markus 1007: case sDenyGroups:
1008: while ((arg = strdelim(&cp)) && *arg != '\0') {
1009: if (options->num_deny_groups >= MAX_DENY_GROUPS)
1010: fatal("%s line %d: too many deny groups.",
1011: filename, linenum);
1012: options->deny_groups[options->num_deny_groups++] = xstrdup(arg);
1013: }
1014: break;
1.66 markus 1015:
1.94 markus 1016: case sCiphers:
1017: arg = strdelim(&cp);
1018: if (!arg || *arg == '\0')
1019: fatal("%s line %d: Missing argument.", filename, linenum);
1020: if (!ciphers_valid(arg))
1021: fatal("%s line %d: Bad SSH2 cipher spec '%s'.",
1022: filename, linenum, arg ? arg : "<NONE>");
1023: if (options->ciphers == NULL)
1024: options->ciphers = xstrdup(arg);
1025: break;
1.33 markus 1026:
1.94 markus 1027: case sMacs:
1028: arg = strdelim(&cp);
1029: if (!arg || *arg == '\0')
1030: fatal("%s line %d: Missing argument.", filename, linenum);
1031: if (!mac_valid(arg))
1032: fatal("%s line %d: Bad SSH2 mac spec '%s'.",
1033: filename, linenum, arg ? arg : "<NONE>");
1034: if (options->macs == NULL)
1035: options->macs = xstrdup(arg);
1036: break;
1.43 jakob 1037:
1.94 markus 1038: case sProtocol:
1039: intptr = &options->protocol;
1040: arg = strdelim(&cp);
1041: if (!arg || *arg == '\0')
1042: fatal("%s line %d: Missing argument.", filename, linenum);
1043: value = proto_spec(arg);
1044: if (value == SSH_PROTO_UNKNOWN)
1045: fatal("%s line %d: Bad protocol spec '%s'.",
1.95 deraadt 1046: filename, linenum, arg ? arg : "<NONE>");
1.94 markus 1047: if (*intptr == SSH_PROTO_UNKNOWN)
1048: *intptr = value;
1049: break;
1050:
1051: case sSubsystem:
1052: if (options->num_subsystems >= MAX_SUBSYSTEMS) {
1053: fatal("%s line %d: too many subsystems defined.",
1.95 deraadt 1054: filename, linenum);
1.94 markus 1055: }
1056: arg = strdelim(&cp);
1057: if (!arg || *arg == '\0')
1058: fatal("%s line %d: Missing subsystem name.",
1.95 deraadt 1059: filename, linenum);
1.153 dtucker 1060: if (!*activep) {
1061: arg = strdelim(&cp);
1062: break;
1063: }
1.94 markus 1064: for (i = 0; i < options->num_subsystems; i++)
1065: if (strcmp(arg, options->subsystem_name[i]) == 0)
1066: fatal("%s line %d: Subsystem '%s' already defined.",
1.95 deraadt 1067: filename, linenum, arg);
1.94 markus 1068: options->subsystem_name[options->num_subsystems] = xstrdup(arg);
1069: arg = strdelim(&cp);
1070: if (!arg || *arg == '\0')
1071: fatal("%s line %d: Missing subsystem command.",
1.95 deraadt 1072: filename, linenum);
1.94 markus 1073: options->subsystem_command[options->num_subsystems] = xstrdup(arg);
1.151 djm 1074:
1075: /* Collect arguments (separate to executable) */
1076: p = xstrdup(arg);
1077: len = strlen(p) + 1;
1078: while ((arg = strdelim(&cp)) != NULL && *arg != '\0') {
1079: len += 1 + strlen(arg);
1080: p = xrealloc(p, 1, len);
1081: strlcat(p, " ", len);
1082: strlcat(p, arg, len);
1083: }
1084: options->subsystem_args[options->num_subsystems] = p;
1.94 markus 1085: options->num_subsystems++;
1086: break;
1.46 markus 1087:
1.94 markus 1088: case sMaxStartups:
1089: arg = strdelim(&cp);
1090: if (!arg || *arg == '\0')
1091: fatal("%s line %d: Missing MaxStartups spec.",
1.95 deraadt 1092: filename, linenum);
1.94 markus 1093: if ((n = sscanf(arg, "%d:%d:%d",
1094: &options->max_startups_begin,
1095: &options->max_startups_rate,
1096: &options->max_startups)) == 3) {
1097: if (options->max_startups_begin >
1098: options->max_startups ||
1099: options->max_startups_rate > 100 ||
1100: options->max_startups_rate < 1)
1.50 markus 1101: fatal("%s line %d: Illegal MaxStartups spec.",
1.87 stevesk 1102: filename, linenum);
1.94 markus 1103: } else if (n != 1)
1104: fatal("%s line %d: Illegal MaxStartups spec.",
1105: filename, linenum);
1106: else
1107: options->max_startups = options->max_startups_begin;
1108: break;
1.133 dtucker 1109:
1110: case sMaxAuthTries:
1111: intptr = &options->max_authtries;
1112: goto parse_int;
1.94 markus 1113:
1114: case sBanner:
1115: charptr = &options->banner;
1116: goto parse_filename;
1.176 djm 1117:
1.94 markus 1118: /*
1119: * These options can contain %X options expanded at
1120: * connect time, so that you can specify paths like:
1121: *
1122: * AuthorizedKeysFile /etc/ssh_keys/%u
1123: */
1124: case sAuthorizedKeysFile:
1125: case sAuthorizedKeysFile2:
1.163 stevesk 1126: charptr = (opcode == sAuthorizedKeysFile) ?
1.94 markus 1127: &options->authorized_keys_file :
1128: &options->authorized_keys_file2;
1129: goto parse_filename;
1130:
1131: case sClientAliveInterval:
1132: intptr = &options->client_alive_interval;
1133: goto parse_time;
1134:
1135: case sClientAliveCountMax:
1136: intptr = &options->client_alive_count_max;
1137: goto parse_int;
1.131 djm 1138:
1139: case sAcceptEnv:
1140: while ((arg = strdelim(&cp)) && *arg != '\0') {
1141: if (strchr(arg, '=') != NULL)
1142: fatal("%s line %d: Invalid environment name.",
1143: filename, linenum);
1144: if (options->num_accept_env >= MAX_ACCEPT_ENV)
1145: fatal("%s line %d: too many allow env.",
1146: filename, linenum);
1.153 dtucker 1147: if (!*activep)
1148: break;
1.131 djm 1149: options->accept_env[options->num_accept_env++] =
1150: xstrdup(arg);
1151: }
1152: break;
1.145 reyk 1153:
1154: case sPermitTunnel:
1155: intptr = &options->permit_tun;
1.146 reyk 1156: arg = strdelim(&cp);
1157: if (!arg || *arg == '\0')
1158: fatal("%s line %d: Missing yes/point-to-point/"
1159: "ethernet/no argument.", filename, linenum);
1160: value = 0; /* silence compiler */
1161: if (strcasecmp(arg, "ethernet") == 0)
1162: value = SSH_TUNMODE_ETHERNET;
1163: else if (strcasecmp(arg, "point-to-point") == 0)
1164: value = SSH_TUNMODE_POINTOPOINT;
1165: else if (strcasecmp(arg, "yes") == 0)
1166: value = SSH_TUNMODE_YES;
1167: else if (strcasecmp(arg, "no") == 0)
1168: value = SSH_TUNMODE_NO;
1169: else
1170: fatal("%s line %d: Bad yes/point-to-point/ethernet/"
1171: "no argument: %s", filename, linenum, arg);
1172: if (*intptr == -1)
1173: *intptr = value;
1174: break;
1.94 markus 1175:
1.153 dtucker 1176: case sMatch:
1177: if (cmdline)
1178: fatal("Match directive not supported as a command-line "
1179: "option");
1180: value = match_cfg_line(&cp, linenum, user, host, address);
1181: if (value < 0)
1182: fatal("%s line %d: Bad Match condition", filename,
1183: linenum);
1184: *activep = value;
1.156 dtucker 1185: break;
1186:
1187: case sPermitOpen:
1188: arg = strdelim(&cp);
1189: if (!arg || *arg == '\0')
1190: fatal("%s line %d: missing PermitOpen specification",
1191: filename, linenum);
1.167 dtucker 1192: n = options->num_permitted_opens; /* modified later */
1.156 dtucker 1193: if (strcmp(arg, "any") == 0) {
1.167 dtucker 1194: if (*activep && n == -1) {
1.156 dtucker 1195: channel_clear_adm_permitted_opens();
1.159 dtucker 1196: options->num_permitted_opens = 0;
1197: }
1.156 dtucker 1198: break;
1199: }
1.166 dtucker 1200: if (*activep && n == -1)
1201: channel_clear_adm_permitted_opens();
1.159 dtucker 1202: for (; arg != NULL && *arg != '\0'; arg = strdelim(&cp)) {
1203: p = hpdelim(&arg);
1204: if (p == NULL)
1205: fatal("%s line %d: missing host in PermitOpen",
1206: filename, linenum);
1207: p = cleanhostname(p);
1208: if (arg == NULL || (port = a2port(arg)) == 0)
1209: fatal("%s line %d: bad port number in "
1210: "PermitOpen", filename, linenum);
1.166 dtucker 1211: if (*activep && n == -1)
1.159 dtucker 1212: options->num_permitted_opens =
1213: channel_add_adm_permitted_opens(p, port);
1214: }
1.153 dtucker 1215: break;
1216:
1.158 dtucker 1217: case sForceCommand:
1218: if (cp == NULL)
1219: fatal("%.200s line %d: Missing argument.", filename,
1220: linenum);
1221: len = strspn(cp, WHITESPACE);
1222: if (*activep && options->adm_forced_command == NULL)
1223: options->adm_forced_command = xstrdup(cp + len);
1224: return 0;
1225:
1.176 djm 1226: case sChrootDirectory:
1227: charptr = &options->chroot_directory;
1.177 djm 1228:
1229: arg = strdelim(&cp);
1230: if (!arg || *arg == '\0')
1231: fatal("%s line %d: missing file name.",
1232: filename, linenum);
1233: if (*activep && *charptr == NULL)
1234: *charptr = xstrdup(arg);
1235: break;
1.176 djm 1236:
1.94 markus 1237: case sDeprecated:
1.117 itojun 1238: logit("%s line %d: Deprecated option %s",
1.121 jakob 1239: filename, linenum, arg);
1240: while (arg)
1241: arg = strdelim(&cp);
1242: break;
1243:
1244: case sUnsupported:
1245: logit("%s line %d: Unsupported option %s",
1.94 markus 1246: filename, linenum, arg);
1247: while (arg)
1248: arg = strdelim(&cp);
1249: break;
1250:
1251: default:
1252: fatal("%s line %d: Missing handler for opcode %s (%d)",
1253: filename, linenum, arg, opcode);
1254: }
1255: if ((arg = strdelim(&cp)) != NULL && *arg != '\0')
1256: fatal("%s line %d: garbage at end of line; \"%.200s\".",
1257: filename, linenum, arg);
1258: return 0;
1259: }
1260:
1261: /* Reads the server configuration file. */
1.25 markus 1262:
1.94 markus 1263: void
1.134 djm 1264: load_server_config(const char *filename, Buffer *conf)
1.94 markus 1265: {
1.134 djm 1266: char line[1024], *cp;
1.94 markus 1267: FILE *f;
1.81 stevesk 1268:
1.134 djm 1269: debug2("%s: filename %s", __func__, filename);
1270: if ((f = fopen(filename, "r")) == NULL) {
1.94 markus 1271: perror(filename);
1272: exit(1);
1273: }
1.134 djm 1274: buffer_clear(conf);
1275: while (fgets(line, sizeof(line), f)) {
1276: /*
1277: * Trim out comments and strip whitespace
1.135 deraadt 1278: * NB - preserve newlines, they are needed to reproduce
1.134 djm 1279: * line numbers later for error messages
1280: */
1281: if ((cp = strchr(line, '#')) != NULL)
1282: memcpy(cp, "\n", 2);
1283: cp = line + strspn(line, " \t\r");
1284:
1285: buffer_append(conf, cp, strlen(cp));
1286: }
1287: buffer_append(conf, "\0", 1);
1288: fclose(f);
1289: debug2("%s: done config len = %d", __func__, buffer_len(conf));
1290: }
1291:
1292: void
1.153 dtucker 1293: parse_server_match_config(ServerOptions *options, const char *user,
1294: const char *host, const char *address)
1295: {
1296: ServerOptions mo;
1297:
1298: initialize_server_options(&mo);
1299: parse_server_config(&mo, "reprocess config", &cfg, user, host, address);
1.168 dtucker 1300: copy_set_server_options(options, &mo, 0);
1.153 dtucker 1301: }
1302:
1.168 dtucker 1303: /* Helper macros */
1304: #define M_CP_INTOPT(n) do {\
1305: if (src->n != -1) \
1306: dst->n = src->n; \
1307: } while (0)
1308: #define M_CP_STROPT(n) do {\
1309: if (src->n != NULL) { \
1310: if (dst->n != NULL) \
1311: xfree(dst->n); \
1312: dst->n = src->n; \
1313: } \
1314: } while(0)
1315:
1316: /*
1317: * Copy any supported values that are set.
1318: *
1319: * If the preauth flag is set, we do not bother copying the the string or
1320: * array values that are not used pre-authentication, because any that we
1321: * do use must be explictly sent in mm_getpwnamallow().
1322: */
1.153 dtucker 1323: void
1.168 dtucker 1324: copy_set_server_options(ServerOptions *dst, ServerOptions *src, int preauth)
1.153 dtucker 1325: {
1.168 dtucker 1326: M_CP_INTOPT(password_authentication);
1327: M_CP_INTOPT(gss_authentication);
1328: M_CP_INTOPT(rsa_authentication);
1329: M_CP_INTOPT(pubkey_authentication);
1330: M_CP_INTOPT(kerberos_authentication);
1331: M_CP_INTOPT(hostbased_authentication);
1332: M_CP_INTOPT(kbd_interactive_authentication);
1.175 dtucker 1333: M_CP_INTOPT(permit_root_login);
1.168 dtucker 1334:
1335: M_CP_INTOPT(allow_tcp_forwarding);
1.178 ! pyr 1336: M_CP_INTOPT(allow_agent_forwarding);
1.168 dtucker 1337: M_CP_INTOPT(gateway_ports);
1338: M_CP_INTOPT(x11_display_offset);
1339: M_CP_INTOPT(x11_forwarding);
1340: M_CP_INTOPT(x11_use_localhost);
1341:
1342: M_CP_STROPT(banner);
1343: if (preauth)
1344: return;
1345: M_CP_STROPT(adm_forced_command);
1.176 djm 1346: M_CP_STROPT(chroot_directory);
1.153 dtucker 1347: }
1.168 dtucker 1348:
1349: #undef M_CP_INTOPT
1350: #undef M_CP_STROPT
1.153 dtucker 1351:
1352: void
1353: parse_server_config(ServerOptions *options, const char *filename, Buffer *conf,
1354: const char *user, const char *host, const char *address)
1.134 djm 1355: {
1.153 dtucker 1356: int active, linenum, bad_options = 0;
1.136 dtucker 1357: char *cp, *obuf, *cbuf;
1.134 djm 1358:
1359: debug2("%s: config %s len %d", __func__, filename, buffer_len(conf));
1360:
1.136 dtucker 1361: obuf = cbuf = xstrdup(buffer_ptr(conf));
1.153 dtucker 1362: active = user ? 0 : 1;
1.137 dtucker 1363: linenum = 1;
1.140 deraadt 1364: while ((cp = strsep(&cbuf, "\n")) != NULL) {
1.134 djm 1365: if (process_server_config_line(options, cp, filename,
1.153 dtucker 1366: linenum++, &active, user, host, address) != 0)
1.94 markus 1367: bad_options++;
1.1 deraadt 1368: }
1.136 dtucker 1369: xfree(obuf);
1.78 stevesk 1370: if (bad_options > 0)
1371: fatal("%s: terminating, %d bad configuration options",
1372: filename, bad_options);
1.1 deraadt 1373: }