Annotation of src/usr.bin/ssh/servconf.c, Revision 1.181
1.181 ! djm 1: /* $OpenBSD: servconf.c,v 1.180 2008/05/08 12:21:16 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) {
1.181 ! djm 569: switch (addr_match_list(address, arg)) {
! 570: case 1:
! 571: debug("connection from %.100s matched 'Address "
! 572: "%.100s' at line %d", address, arg, line);
! 573: break;
! 574: case 0:
1.153 dtucker 575: result = 0;
1.181 ! djm 576: break;
! 577: case -1:
! 578: return -1;
1.153 dtucker 579: }
580: } else {
581: error("Unsupported Match attribute %s", attrib);
582: return -1;
583: }
584: }
585: if (user != NULL)
586: debug3("match %sfound", result ? "" : "not ");
587: *condition = cp;
588: return result;
589: }
590:
1.158 dtucker 591: #define WHITESPACE " \t\r\n"
592:
1.94 markus 593: int
594: process_server_config_line(ServerOptions *options, char *line,
1.153 dtucker 595: const char *filename, int linenum, int *activep, const char *user,
596: const char *host, const char *address)
1.1 deraadt 597: {
1.74 stevesk 598: char *cp, **charptr, *arg, *p;
1.153 dtucker 599: int cmdline = 0, *intptr, value, n;
1.174 dtucker 600: SyslogFacility *log_facility_ptr;
601: LogLevel *log_level_ptr;
1.25 markus 602: ServerOpCodes opcode;
1.139 djm 603: u_short port;
1.153 dtucker 604: u_int i, flags = 0;
1.151 djm 605: size_t len;
1.25 markus 606:
1.94 markus 607: cp = line;
1.148 dtucker 608: if ((arg = strdelim(&cp)) == NULL)
1.147 djm 609: return 0;
1.94 markus 610: /* Ignore leading whitespace */
611: if (*arg == '\0')
612: arg = strdelim(&cp);
613: if (!arg || !*arg || *arg == '#')
614: return 0;
615: intptr = NULL;
616: charptr = NULL;
1.153 dtucker 617: opcode = parse_token(arg, filename, linenum, &flags);
618:
619: if (activep == NULL) { /* We are processing a command line directive */
620: cmdline = 1;
621: activep = &cmdline;
622: }
623: if (*activep && opcode != sMatch)
624: debug3("%s:%d setting %s %s", filename, linenum, arg, cp);
625: if (*activep == 0 && !(flags & SSHCFG_MATCH)) {
626: if (user == NULL) {
627: fatal("%s line %d: Directive '%s' is not allowed "
628: "within a Match block", filename, linenum, arg);
629: } else { /* this is a directive we have already processed */
630: while (arg)
631: arg = strdelim(&cp);
632: return 0;
633: }
634: }
635:
1.94 markus 636: switch (opcode) {
637: case sBadOption:
638: return -1;
639: case sPort:
640: /* ignore ports from configfile if cmdline specifies ports */
641: if (options->ports_from_cmdline)
642: return 0;
643: if (options->listen_addrs != NULL)
644: fatal("%s line %d: ports must be specified before "
1.98 stevesk 645: "ListenAddress.", filename, linenum);
1.94 markus 646: if (options->num_ports >= MAX_PORTS)
647: fatal("%s line %d: too many ports.",
648: filename, linenum);
1.48 provos 649: arg = strdelim(&cp);
1.94 markus 650: if (!arg || *arg == '\0')
651: fatal("%s line %d: missing port number.",
652: filename, linenum);
653: options->ports[options->num_ports++] = a2port(arg);
654: if (options->ports[options->num_ports-1] == 0)
655: fatal("%s line %d: Badly formatted port number.",
656: filename, linenum);
657: break;
1.29 markus 658:
1.94 markus 659: case sServerKeyBits:
660: intptr = &options->server_key_bits;
1.180 djm 661: parse_int:
1.94 markus 662: arg = strdelim(&cp);
663: if (!arg || *arg == '\0')
664: fatal("%s line %d: missing integer value.",
665: filename, linenum);
666: value = atoi(arg);
1.153 dtucker 667: if (*activep && *intptr == -1)
1.94 markus 668: *intptr = value;
669: break;
1.25 markus 670:
1.94 markus 671: case sLoginGraceTime:
672: intptr = &options->login_grace_time;
1.180 djm 673: parse_time:
1.94 markus 674: arg = strdelim(&cp);
675: if (!arg || *arg == '\0')
676: fatal("%s line %d: missing time value.",
677: filename, linenum);
678: if ((value = convtime(arg)) == -1)
679: fatal("%s line %d: invalid time value.",
680: filename, linenum);
681: if (*intptr == -1)
682: *intptr = value;
683: break;
684:
685: case sKeyRegenerationTime:
686: intptr = &options->key_regeneration_time;
687: goto parse_time;
688:
689: case sListenAddress:
690: arg = strdelim(&cp);
1.139 djm 691: if (arg == NULL || *arg == '\0')
692: fatal("%s line %d: missing address",
1.94 markus 693: filename, linenum);
1.144 dtucker 694: /* check for bare IPv6 address: no "[]" and 2 or more ":" */
695: if (strchr(arg, '[') == NULL && (p = strchr(arg, ':')) != NULL
696: && strchr(p+1, ':') != NULL) {
697: add_listen_addr(options, arg, 0);
698: break;
699: }
1.139 djm 700: p = hpdelim(&arg);
701: if (p == NULL)
702: fatal("%s line %d: bad address:port usage",
703: filename, linenum);
704: p = cleanhostname(p);
705: if (arg == NULL)
706: port = 0;
707: else if ((port = a2port(arg)) == 0)
708: fatal("%s line %d: bad port number", filename, linenum);
709:
710: add_listen_addr(options, p, port);
1.25 markus 711:
1.138 djm 712: break;
713:
714: case sAddressFamily:
715: arg = strdelim(&cp);
1.141 markus 716: if (!arg || *arg == '\0')
717: fatal("%s line %d: missing address family.",
718: filename, linenum);
1.138 djm 719: intptr = &options->address_family;
720: if (options->listen_addrs != NULL)
721: fatal("%s line %d: address family must be specified before "
722: "ListenAddress.", filename, linenum);
723: if (strcasecmp(arg, "inet") == 0)
724: value = AF_INET;
725: else if (strcasecmp(arg, "inet6") == 0)
726: value = AF_INET6;
727: else if (strcasecmp(arg, "any") == 0)
728: value = AF_UNSPEC;
729: else
730: fatal("%s line %d: unsupported address family \"%s\".",
731: filename, linenum, arg);
732: if (*intptr == -1)
733: *intptr = value;
1.94 markus 734: break;
735:
736: case sHostKeyFile:
737: intptr = &options->num_host_key_files;
738: if (*intptr >= MAX_HOSTKEYS)
739: fatal("%s line %d: too many host keys specified (max %d).",
740: filename, linenum, MAX_HOSTKEYS);
741: charptr = &options->host_key_files[*intptr];
1.180 djm 742: parse_filename:
1.94 markus 743: arg = strdelim(&cp);
744: if (!arg || *arg == '\0')
745: fatal("%s line %d: missing file name.",
746: filename, linenum);
1.153 dtucker 747: if (*activep && *charptr == NULL) {
1.94 markus 748: *charptr = tilde_expand_filename(arg, getuid());
749: /* increase optional counter */
750: if (intptr != NULL)
751: *intptr = *intptr + 1;
752: }
753: break;
1.76 stevesk 754:
1.94 markus 755: case sPidFile:
756: charptr = &options->pid_file;
757: goto parse_filename;
1.25 markus 758:
1.94 markus 759: case sPermitRootLogin:
760: intptr = &options->permit_root_login;
761: arg = strdelim(&cp);
762: if (!arg || *arg == '\0')
763: fatal("%s line %d: missing yes/"
764: "without-password/forced-commands-only/no "
765: "argument.", filename, linenum);
766: value = 0; /* silence compiler */
767: if (strcmp(arg, "without-password") == 0)
768: value = PERMIT_NO_PASSWD;
769: else if (strcmp(arg, "forced-commands-only") == 0)
770: value = PERMIT_FORCED_ONLY;
771: else if (strcmp(arg, "yes") == 0)
772: value = PERMIT_YES;
773: else if (strcmp(arg, "no") == 0)
774: value = PERMIT_NO;
775: else
776: fatal("%s line %d: Bad yes/"
777: "without-password/forced-commands-only/no "
778: "argument: %s", filename, linenum, arg);
1.175 dtucker 779: if (*activep && *intptr == -1)
1.94 markus 780: *intptr = value;
781: break;
1.36 markus 782:
1.94 markus 783: case sIgnoreRhosts:
784: intptr = &options->ignore_rhosts;
1.180 djm 785: parse_flag:
1.94 markus 786: arg = strdelim(&cp);
787: if (!arg || *arg == '\0')
788: fatal("%s line %d: missing yes/no argument.",
789: filename, linenum);
790: value = 0; /* silence compiler */
791: if (strcmp(arg, "yes") == 0)
792: value = 1;
793: else if (strcmp(arg, "no") == 0)
794: value = 0;
795: else
796: fatal("%s line %d: Bad yes/no argument: %s",
797: filename, linenum, arg);
1.153 dtucker 798: if (*activep && *intptr == -1)
1.94 markus 799: *intptr = value;
800: break;
801:
802: case sIgnoreUserKnownHosts:
803: intptr = &options->ignore_user_known_hosts;
804: goto parse_flag;
805:
806: case sRhostsRSAAuthentication:
807: intptr = &options->rhosts_rsa_authentication;
808: goto parse_flag;
809:
810: case sHostbasedAuthentication:
811: intptr = &options->hostbased_authentication;
812: goto parse_flag;
813:
814: case sHostbasedUsesNameFromPacketOnly:
815: intptr = &options->hostbased_uses_name_from_packet_only;
816: goto parse_flag;
817:
818: case sRSAAuthentication:
819: intptr = &options->rsa_authentication;
820: goto parse_flag;
821:
822: case sPubkeyAuthentication:
823: intptr = &options->pubkey_authentication;
824: goto parse_flag;
1.119 jakob 825:
1.94 markus 826: case sKerberosAuthentication:
827: intptr = &options->kerberos_authentication;
828: goto parse_flag;
829:
830: case sKerberosOrLocalPasswd:
831: intptr = &options->kerberos_or_local_passwd;
832: goto parse_flag;
833:
834: case sKerberosTicketCleanup:
835: intptr = &options->kerberos_ticket_cleanup;
1.130 jakob 836: goto parse_flag;
837:
838: case sKerberosGetAFSToken:
839: intptr = &options->kerberos_get_afs_token;
1.125 markus 840: goto parse_flag;
841:
842: case sGssAuthentication:
843: intptr = &options->gss_authentication;
844: goto parse_flag;
845:
846: case sGssCleanupCreds:
847: intptr = &options->gss_cleanup_creds;
1.94 markus 848: goto parse_flag;
849:
850: case sPasswordAuthentication:
851: intptr = &options->password_authentication;
852: goto parse_flag;
853:
854: case sKbdInteractiveAuthentication:
855: intptr = &options->kbd_interactive_authentication;
856: goto parse_flag;
857:
858: case sChallengeResponseAuthentication:
859: intptr = &options->challenge_response_authentication;
860: goto parse_flag;
861:
862: case sPrintMotd:
863: intptr = &options->print_motd;
864: goto parse_flag;
865:
866: case sPrintLastLog:
867: intptr = &options->print_lastlog;
868: goto parse_flag;
869:
870: case sX11Forwarding:
871: intptr = &options->x11_forwarding;
872: goto parse_flag;
873:
874: case sX11DisplayOffset:
875: intptr = &options->x11_display_offset;
876: goto parse_int;
1.99 stevesk 877:
878: case sX11UseLocalhost:
879: intptr = &options->x11_use_localhost;
880: goto parse_flag;
1.94 markus 881:
882: case sXAuthLocation:
883: charptr = &options->xauth_location;
884: goto parse_filename;
885:
886: case sStrictModes:
887: intptr = &options->strict_modes;
888: goto parse_flag;
889:
1.129 markus 890: case sTCPKeepAlive:
891: intptr = &options->tcp_keep_alive;
1.94 markus 892: goto parse_flag;
893:
894: case sEmptyPasswd:
895: intptr = &options->permit_empty_passwd;
1.113 markus 896: goto parse_flag;
897:
898: case sPermitUserEnvironment:
899: intptr = &options->permit_user_env;
1.94 markus 900: goto parse_flag;
901:
902: case sUseLogin:
903: intptr = &options->use_login;
1.111 markus 904: goto parse_flag;
905:
906: case sCompression:
907: intptr = &options->compression;
1.143 markus 908: arg = strdelim(&cp);
909: if (!arg || *arg == '\0')
910: fatal("%s line %d: missing yes/no/delayed "
911: "argument.", filename, linenum);
912: value = 0; /* silence compiler */
913: if (strcmp(arg, "delayed") == 0)
914: value = COMP_DELAYED;
915: else if (strcmp(arg, "yes") == 0)
916: value = COMP_ZLIB;
917: else if (strcmp(arg, "no") == 0)
918: value = COMP_NONE;
919: else
920: fatal("%s line %d: Bad yes/no/delayed "
921: "argument: %s", filename, linenum, arg);
922: if (*intptr == -1)
923: *intptr = value;
924: break;
1.94 markus 925:
926: case sGatewayPorts:
927: intptr = &options->gateway_ports;
1.139 djm 928: arg = strdelim(&cp);
929: if (!arg || *arg == '\0')
930: fatal("%s line %d: missing yes/no/clientspecified "
931: "argument.", filename, linenum);
932: value = 0; /* silence compiler */
933: if (strcmp(arg, "clientspecified") == 0)
934: value = 2;
935: else if (strcmp(arg, "yes") == 0)
936: value = 1;
937: else if (strcmp(arg, "no") == 0)
938: value = 0;
939: else
940: fatal("%s line %d: Bad yes/no/clientspecified "
941: "argument: %s", filename, linenum, arg);
1.169 dtucker 942: if (*activep && *intptr == -1)
1.139 djm 943: *intptr = value;
944: break;
1.25 markus 945:
1.122 markus 946: case sUseDNS:
947: intptr = &options->use_dns;
1.94 markus 948: goto parse_flag;
1.53 markus 949:
1.94 markus 950: case sLogFacility:
1.174 dtucker 951: log_facility_ptr = &options->log_facility;
1.94 markus 952: arg = strdelim(&cp);
953: value = log_facility_number(arg);
1.101 markus 954: if (value == SYSLOG_FACILITY_NOT_SET)
1.94 markus 955: fatal("%.200s line %d: unsupported log facility '%s'",
956: filename, linenum, arg ? arg : "<NONE>");
1.174 dtucker 957: if (*log_facility_ptr == -1)
958: *log_facility_ptr = (SyslogFacility) value;
1.94 markus 959: break;
1.25 markus 960:
1.94 markus 961: case sLogLevel:
1.174 dtucker 962: log_level_ptr = &options->log_level;
1.94 markus 963: arg = strdelim(&cp);
964: value = log_level_number(arg);
1.101 markus 965: if (value == SYSLOG_LEVEL_NOT_SET)
1.94 markus 966: fatal("%.200s line %d: unsupported log level '%s'",
967: filename, linenum, arg ? arg : "<NONE>");
1.174 dtucker 968: if (*log_level_ptr == -1)
969: *log_level_ptr = (LogLevel) value;
1.94 markus 970: break;
971:
972: case sAllowTcpForwarding:
973: intptr = &options->allow_tcp_forwarding;
974: goto parse_flag;
1.102 provos 975:
1.178 pyr 976: case sAllowAgentForwarding:
977: intptr = &options->allow_agent_forwarding;
978: goto parse_flag;
979:
1.102 provos 980: case sUsePrivilegeSeparation:
981: intptr = &use_privsep;
982: goto parse_flag;
1.94 markus 983:
984: case sAllowUsers:
985: while ((arg = strdelim(&cp)) && *arg != '\0') {
986: if (options->num_allow_users >= MAX_ALLOW_USERS)
987: fatal("%s line %d: too many allow users.",
988: filename, linenum);
1.112 deraadt 989: options->allow_users[options->num_allow_users++] =
990: xstrdup(arg);
1.94 markus 991: }
992: break;
1.25 markus 993:
1.94 markus 994: case sDenyUsers:
995: while ((arg = strdelim(&cp)) && *arg != '\0') {
996: if (options->num_deny_users >= MAX_DENY_USERS)
1.163 stevesk 997: fatal("%s line %d: too many deny users.",
1.94 markus 998: filename, linenum);
1.112 deraadt 999: options->deny_users[options->num_deny_users++] =
1000: xstrdup(arg);
1.94 markus 1001: }
1002: break;
1.25 markus 1003:
1.94 markus 1004: case sAllowGroups:
1005: while ((arg = strdelim(&cp)) && *arg != '\0') {
1006: if (options->num_allow_groups >= MAX_ALLOW_GROUPS)
1007: fatal("%s line %d: too many allow groups.",
1008: filename, linenum);
1.112 deraadt 1009: options->allow_groups[options->num_allow_groups++] =
1010: xstrdup(arg);
1.94 markus 1011: }
1012: break;
1.33 markus 1013:
1.94 markus 1014: case sDenyGroups:
1015: while ((arg = strdelim(&cp)) && *arg != '\0') {
1016: if (options->num_deny_groups >= MAX_DENY_GROUPS)
1017: fatal("%s line %d: too many deny groups.",
1018: filename, linenum);
1019: options->deny_groups[options->num_deny_groups++] = xstrdup(arg);
1020: }
1021: break;
1.66 markus 1022:
1.94 markus 1023: case sCiphers:
1024: arg = strdelim(&cp);
1025: if (!arg || *arg == '\0')
1026: fatal("%s line %d: Missing argument.", filename, linenum);
1027: if (!ciphers_valid(arg))
1028: fatal("%s line %d: Bad SSH2 cipher spec '%s'.",
1029: filename, linenum, arg ? arg : "<NONE>");
1030: if (options->ciphers == NULL)
1031: options->ciphers = xstrdup(arg);
1032: break;
1.33 markus 1033:
1.94 markus 1034: case sMacs:
1035: arg = strdelim(&cp);
1036: if (!arg || *arg == '\0')
1037: fatal("%s line %d: Missing argument.", filename, linenum);
1038: if (!mac_valid(arg))
1039: fatal("%s line %d: Bad SSH2 mac spec '%s'.",
1040: filename, linenum, arg ? arg : "<NONE>");
1041: if (options->macs == NULL)
1042: options->macs = xstrdup(arg);
1043: break;
1.43 jakob 1044:
1.94 markus 1045: case sProtocol:
1046: intptr = &options->protocol;
1047: arg = strdelim(&cp);
1048: if (!arg || *arg == '\0')
1049: fatal("%s line %d: Missing argument.", filename, linenum);
1050: value = proto_spec(arg);
1051: if (value == SSH_PROTO_UNKNOWN)
1052: fatal("%s line %d: Bad protocol spec '%s'.",
1.95 deraadt 1053: filename, linenum, arg ? arg : "<NONE>");
1.94 markus 1054: if (*intptr == SSH_PROTO_UNKNOWN)
1055: *intptr = value;
1056: break;
1057:
1058: case sSubsystem:
1059: if (options->num_subsystems >= MAX_SUBSYSTEMS) {
1060: fatal("%s line %d: too many subsystems defined.",
1.95 deraadt 1061: filename, linenum);
1.94 markus 1062: }
1063: arg = strdelim(&cp);
1064: if (!arg || *arg == '\0')
1065: fatal("%s line %d: Missing subsystem name.",
1.95 deraadt 1066: filename, linenum);
1.153 dtucker 1067: if (!*activep) {
1068: arg = strdelim(&cp);
1069: break;
1070: }
1.94 markus 1071: for (i = 0; i < options->num_subsystems; i++)
1072: if (strcmp(arg, options->subsystem_name[i]) == 0)
1073: fatal("%s line %d: Subsystem '%s' already defined.",
1.95 deraadt 1074: filename, linenum, arg);
1.94 markus 1075: options->subsystem_name[options->num_subsystems] = xstrdup(arg);
1076: arg = strdelim(&cp);
1077: if (!arg || *arg == '\0')
1078: fatal("%s line %d: Missing subsystem command.",
1.95 deraadt 1079: filename, linenum);
1.94 markus 1080: options->subsystem_command[options->num_subsystems] = xstrdup(arg);
1.151 djm 1081:
1082: /* Collect arguments (separate to executable) */
1083: p = xstrdup(arg);
1084: len = strlen(p) + 1;
1085: while ((arg = strdelim(&cp)) != NULL && *arg != '\0') {
1086: len += 1 + strlen(arg);
1087: p = xrealloc(p, 1, len);
1088: strlcat(p, " ", len);
1089: strlcat(p, arg, len);
1090: }
1091: options->subsystem_args[options->num_subsystems] = p;
1.94 markus 1092: options->num_subsystems++;
1093: break;
1.46 markus 1094:
1.94 markus 1095: case sMaxStartups:
1096: arg = strdelim(&cp);
1097: if (!arg || *arg == '\0')
1098: fatal("%s line %d: Missing MaxStartups spec.",
1.95 deraadt 1099: filename, linenum);
1.94 markus 1100: if ((n = sscanf(arg, "%d:%d:%d",
1101: &options->max_startups_begin,
1102: &options->max_startups_rate,
1103: &options->max_startups)) == 3) {
1104: if (options->max_startups_begin >
1105: options->max_startups ||
1106: options->max_startups_rate > 100 ||
1107: options->max_startups_rate < 1)
1.50 markus 1108: fatal("%s line %d: Illegal MaxStartups spec.",
1.87 stevesk 1109: filename, linenum);
1.94 markus 1110: } else if (n != 1)
1111: fatal("%s line %d: Illegal MaxStartups spec.",
1112: filename, linenum);
1113: else
1114: options->max_startups = options->max_startups_begin;
1115: break;
1.133 dtucker 1116:
1117: case sMaxAuthTries:
1118: intptr = &options->max_authtries;
1119: goto parse_int;
1.94 markus 1120:
1.180 djm 1121: case sMaxSessions:
1122: intptr = &options->max_sessions;
1123: goto parse_int;
1124:
1.94 markus 1125: case sBanner:
1126: charptr = &options->banner;
1127: goto parse_filename;
1.176 djm 1128:
1.94 markus 1129: /*
1130: * These options can contain %X options expanded at
1131: * connect time, so that you can specify paths like:
1132: *
1133: * AuthorizedKeysFile /etc/ssh_keys/%u
1134: */
1135: case sAuthorizedKeysFile:
1136: case sAuthorizedKeysFile2:
1.163 stevesk 1137: charptr = (opcode == sAuthorizedKeysFile) ?
1.94 markus 1138: &options->authorized_keys_file :
1139: &options->authorized_keys_file2;
1140: goto parse_filename;
1141:
1142: case sClientAliveInterval:
1143: intptr = &options->client_alive_interval;
1144: goto parse_time;
1145:
1146: case sClientAliveCountMax:
1147: intptr = &options->client_alive_count_max;
1148: goto parse_int;
1.131 djm 1149:
1150: case sAcceptEnv:
1151: while ((arg = strdelim(&cp)) && *arg != '\0') {
1152: if (strchr(arg, '=') != NULL)
1153: fatal("%s line %d: Invalid environment name.",
1154: filename, linenum);
1155: if (options->num_accept_env >= MAX_ACCEPT_ENV)
1156: fatal("%s line %d: too many allow env.",
1157: filename, linenum);
1.153 dtucker 1158: if (!*activep)
1159: break;
1.131 djm 1160: options->accept_env[options->num_accept_env++] =
1161: xstrdup(arg);
1162: }
1163: break;
1.145 reyk 1164:
1165: case sPermitTunnel:
1166: intptr = &options->permit_tun;
1.146 reyk 1167: arg = strdelim(&cp);
1168: if (!arg || *arg == '\0')
1169: fatal("%s line %d: Missing yes/point-to-point/"
1170: "ethernet/no argument.", filename, linenum);
1171: value = 0; /* silence compiler */
1172: if (strcasecmp(arg, "ethernet") == 0)
1173: value = SSH_TUNMODE_ETHERNET;
1174: else if (strcasecmp(arg, "point-to-point") == 0)
1175: value = SSH_TUNMODE_POINTOPOINT;
1176: else if (strcasecmp(arg, "yes") == 0)
1177: value = SSH_TUNMODE_YES;
1178: else if (strcasecmp(arg, "no") == 0)
1179: value = SSH_TUNMODE_NO;
1180: else
1181: fatal("%s line %d: Bad yes/point-to-point/ethernet/"
1182: "no argument: %s", filename, linenum, arg);
1183: if (*intptr == -1)
1184: *intptr = value;
1185: break;
1.94 markus 1186:
1.153 dtucker 1187: case sMatch:
1188: if (cmdline)
1189: fatal("Match directive not supported as a command-line "
1190: "option");
1191: value = match_cfg_line(&cp, linenum, user, host, address);
1192: if (value < 0)
1193: fatal("%s line %d: Bad Match condition", filename,
1194: linenum);
1195: *activep = value;
1.156 dtucker 1196: break;
1197:
1198: case sPermitOpen:
1199: arg = strdelim(&cp);
1200: if (!arg || *arg == '\0')
1201: fatal("%s line %d: missing PermitOpen specification",
1202: filename, linenum);
1.167 dtucker 1203: n = options->num_permitted_opens; /* modified later */
1.156 dtucker 1204: if (strcmp(arg, "any") == 0) {
1.167 dtucker 1205: if (*activep && n == -1) {
1.156 dtucker 1206: channel_clear_adm_permitted_opens();
1.159 dtucker 1207: options->num_permitted_opens = 0;
1208: }
1.156 dtucker 1209: break;
1210: }
1.166 dtucker 1211: if (*activep && n == -1)
1212: channel_clear_adm_permitted_opens();
1.159 dtucker 1213: for (; arg != NULL && *arg != '\0'; arg = strdelim(&cp)) {
1214: p = hpdelim(&arg);
1215: if (p == NULL)
1216: fatal("%s line %d: missing host in PermitOpen",
1217: filename, linenum);
1218: p = cleanhostname(p);
1219: if (arg == NULL || (port = a2port(arg)) == 0)
1220: fatal("%s line %d: bad port number in "
1221: "PermitOpen", filename, linenum);
1.166 dtucker 1222: if (*activep && n == -1)
1.159 dtucker 1223: options->num_permitted_opens =
1224: channel_add_adm_permitted_opens(p, port);
1225: }
1.153 dtucker 1226: break;
1227:
1.158 dtucker 1228: case sForceCommand:
1229: if (cp == NULL)
1230: fatal("%.200s line %d: Missing argument.", filename,
1231: linenum);
1232: len = strspn(cp, WHITESPACE);
1233: if (*activep && options->adm_forced_command == NULL)
1234: options->adm_forced_command = xstrdup(cp + len);
1235: return 0;
1236:
1.176 djm 1237: case sChrootDirectory:
1238: charptr = &options->chroot_directory;
1.177 djm 1239:
1240: arg = strdelim(&cp);
1241: if (!arg || *arg == '\0')
1242: fatal("%s line %d: missing file name.",
1243: filename, linenum);
1244: if (*activep && *charptr == NULL)
1245: *charptr = xstrdup(arg);
1246: break;
1.176 djm 1247:
1.94 markus 1248: case sDeprecated:
1.117 itojun 1249: logit("%s line %d: Deprecated option %s",
1.121 jakob 1250: filename, linenum, arg);
1251: while (arg)
1252: arg = strdelim(&cp);
1253: break;
1254:
1255: case sUnsupported:
1256: logit("%s line %d: Unsupported option %s",
1.94 markus 1257: filename, linenum, arg);
1258: while (arg)
1259: arg = strdelim(&cp);
1260: break;
1261:
1262: default:
1263: fatal("%s line %d: Missing handler for opcode %s (%d)",
1264: filename, linenum, arg, opcode);
1265: }
1266: if ((arg = strdelim(&cp)) != NULL && *arg != '\0')
1267: fatal("%s line %d: garbage at end of line; \"%.200s\".",
1268: filename, linenum, arg);
1269: return 0;
1270: }
1271:
1272: /* Reads the server configuration file. */
1.25 markus 1273:
1.94 markus 1274: void
1.134 djm 1275: load_server_config(const char *filename, Buffer *conf)
1.94 markus 1276: {
1.134 djm 1277: char line[1024], *cp;
1.94 markus 1278: FILE *f;
1.81 stevesk 1279:
1.134 djm 1280: debug2("%s: filename %s", __func__, filename);
1281: if ((f = fopen(filename, "r")) == NULL) {
1.94 markus 1282: perror(filename);
1283: exit(1);
1284: }
1.134 djm 1285: buffer_clear(conf);
1286: while (fgets(line, sizeof(line), f)) {
1287: /*
1288: * Trim out comments and strip whitespace
1.135 deraadt 1289: * NB - preserve newlines, they are needed to reproduce
1.134 djm 1290: * line numbers later for error messages
1291: */
1292: if ((cp = strchr(line, '#')) != NULL)
1293: memcpy(cp, "\n", 2);
1294: cp = line + strspn(line, " \t\r");
1295:
1296: buffer_append(conf, cp, strlen(cp));
1297: }
1298: buffer_append(conf, "\0", 1);
1299: fclose(f);
1300: debug2("%s: done config len = %d", __func__, buffer_len(conf));
1301: }
1302:
1303: void
1.153 dtucker 1304: parse_server_match_config(ServerOptions *options, const char *user,
1305: const char *host, const char *address)
1306: {
1307: ServerOptions mo;
1308:
1309: initialize_server_options(&mo);
1310: parse_server_config(&mo, "reprocess config", &cfg, user, host, address);
1.168 dtucker 1311: copy_set_server_options(options, &mo, 0);
1.153 dtucker 1312: }
1313:
1.168 dtucker 1314: /* Helper macros */
1315: #define M_CP_INTOPT(n) do {\
1316: if (src->n != -1) \
1317: dst->n = src->n; \
1318: } while (0)
1319: #define M_CP_STROPT(n) do {\
1320: if (src->n != NULL) { \
1321: if (dst->n != NULL) \
1322: xfree(dst->n); \
1323: dst->n = src->n; \
1324: } \
1325: } while(0)
1326:
1327: /*
1328: * Copy any supported values that are set.
1329: *
1330: * If the preauth flag is set, we do not bother copying the the string or
1331: * array values that are not used pre-authentication, because any that we
1332: * do use must be explictly sent in mm_getpwnamallow().
1333: */
1.153 dtucker 1334: void
1.168 dtucker 1335: copy_set_server_options(ServerOptions *dst, ServerOptions *src, int preauth)
1.153 dtucker 1336: {
1.168 dtucker 1337: M_CP_INTOPT(password_authentication);
1338: M_CP_INTOPT(gss_authentication);
1339: M_CP_INTOPT(rsa_authentication);
1340: M_CP_INTOPT(pubkey_authentication);
1341: M_CP_INTOPT(kerberos_authentication);
1342: M_CP_INTOPT(hostbased_authentication);
1343: M_CP_INTOPT(kbd_interactive_authentication);
1.175 dtucker 1344: M_CP_INTOPT(permit_root_login);
1.168 dtucker 1345:
1346: M_CP_INTOPT(allow_tcp_forwarding);
1.178 pyr 1347: M_CP_INTOPT(allow_agent_forwarding);
1.168 dtucker 1348: M_CP_INTOPT(gateway_ports);
1349: M_CP_INTOPT(x11_display_offset);
1350: M_CP_INTOPT(x11_forwarding);
1351: M_CP_INTOPT(x11_use_localhost);
1.180 djm 1352: M_CP_INTOPT(max_sessions);
1.168 dtucker 1353:
1354: M_CP_STROPT(banner);
1355: if (preauth)
1356: return;
1357: M_CP_STROPT(adm_forced_command);
1.176 djm 1358: M_CP_STROPT(chroot_directory);
1.153 dtucker 1359: }
1.168 dtucker 1360:
1361: #undef M_CP_INTOPT
1362: #undef M_CP_STROPT
1.153 dtucker 1363:
1364: void
1365: parse_server_config(ServerOptions *options, const char *filename, Buffer *conf,
1366: const char *user, const char *host, const char *address)
1.134 djm 1367: {
1.153 dtucker 1368: int active, linenum, bad_options = 0;
1.136 dtucker 1369: char *cp, *obuf, *cbuf;
1.134 djm 1370:
1371: debug2("%s: config %s len %d", __func__, filename, buffer_len(conf));
1372:
1.136 dtucker 1373: obuf = cbuf = xstrdup(buffer_ptr(conf));
1.153 dtucker 1374: active = user ? 0 : 1;
1.137 dtucker 1375: linenum = 1;
1.140 deraadt 1376: while ((cp = strsep(&cbuf, "\n")) != NULL) {
1.134 djm 1377: if (process_server_config_line(options, cp, filename,
1.153 dtucker 1378: linenum++, &active, user, host, address) != 0)
1.94 markus 1379: bad_options++;
1.1 deraadt 1380: }
1.136 dtucker 1381: xfree(obuf);
1.78 stevesk 1382: if (bad_options > 0)
1383: fatal("%s: terminating, %d bad configuration options",
1384: filename, bad_options);
1.1 deraadt 1385: }