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