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