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