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