Annotation of src/usr.bin/ssh/readconf.c, Revision 1.198
1.198 ! dtucker 1: /* $OpenBSD: readconf.c,v 1.197 2013/03/06 23:36:53 djm Exp $ */
1.1 deraadt 2: /*
1.18 deraadt 3: * Author: Tatu Ylonen <ylo@cs.hut.fi>
4: * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5: * All rights reserved
6: * Functions for reading the configuration files.
1.26 markus 7: *
1.46 deraadt 8: * As far as I am concerned, the code I have written for this software
9: * can be used freely for any purpose. Any derived versions of this
10: * software must be clearly marked as such, and if the derived work is
11: * incompatible with the protocol description in the RFC file, it must be
12: * called by a name other than "ssh" or "Secure Shell".
1.18 deraadt 13: */
1.1 deraadt 14:
1.147 stevesk 15: #include <sys/types.h>
16: #include <sys/stat.h>
1.152 stevesk 17: #include <sys/socket.h>
18:
19: #include <netinet/in.h>
1.190 djm 20: #include <netinet/in_systm.h>
21: #include <netinet/ip.h>
1.148 stevesk 22:
23: #include <ctype.h>
1.154 stevesk 24: #include <errno.h>
1.155 stevesk 25: #include <netdb.h>
1.159 deraadt 26: #include <signal.h>
1.158 stevesk 27: #include <stdio.h>
1.157 stevesk 28: #include <string.h>
1.156 stevesk 29: #include <unistd.h>
1.1 deraadt 30:
1.159 deraadt 31: #include "xmalloc.h"
1.1 deraadt 32: #include "ssh.h"
1.25 markus 33: #include "compat.h"
1.58 markus 34: #include "cipher.h"
1.55 markus 35: #include "pathnames.h"
1.58 markus 36: #include "log.h"
1.159 deraadt 37: #include "key.h"
1.58 markus 38: #include "readconf.h"
39: #include "match.h"
40: #include "misc.h"
1.159 deraadt 41: #include "buffer.h"
1.62 markus 42: #include "kex.h"
43: #include "mac.h"
1.1 deraadt 44:
45: /* Format of the configuration file:
46:
47: # Configuration data is parsed as follows:
48: # 1. command line options
49: # 2. user-specific file
50: # 3. system-wide file
51: # Any configuration value is only changed the first time it is set.
52: # Thus, host-specific definitions should be at the beginning of the
53: # configuration file, and defaults at the end.
54:
55: # Host-specific declarations. These may override anything above. A single
56: # host may match multiple declarations; these are processed in the order
57: # that they are given in.
58:
59: Host *.ngs.fi ngs.fi
1.96 markus 60: User foo
1.1 deraadt 61:
62: Host fake.com
63: HostName another.host.name.real.org
64: User blaah
65: Port 34289
66: ForwardX11 no
67: ForwardAgent no
68:
69: Host books.com
70: RemoteForward 9999 shadows.cs.hut.fi:9999
71: Cipher 3des
72:
73: Host fascist.blob.com
74: Port 23123
75: User tylonen
76: PasswordAuthentication no
77:
78: Host puukko.hut.fi
79: User t35124p
80: ProxyCommand ssh-proxy %h %p
81:
82: Host *.fr
1.96 markus 83: PublicKeyAuthentication no
1.1 deraadt 84:
85: Host *.su
86: Cipher none
87: PasswordAuthentication no
88:
1.144 reyk 89: Host vpn.fake.com
90: Tunnel yes
91: TunnelDevice 3
92:
1.1 deraadt 93: # Defaults for various options
94: Host *
95: ForwardAgent no
1.50 markus 96: ForwardX11 no
1.1 deraadt 97: PasswordAuthentication yes
98: RSAAuthentication yes
99: RhostsRSAAuthentication yes
100: StrictHostKeyChecking yes
1.126 markus 101: TcpKeepAlive no
1.1 deraadt 102: IdentityFile ~/.ssh/identity
103: Port 22
104: EscapeChar ~
105:
106: */
107:
108: /* Keyword tokens. */
109:
1.17 markus 110: typedef enum {
111: oBadOption,
1.186 djm 112: oForwardAgent, oForwardX11, oForwardX11Trusted, oForwardX11Timeout,
113: oGatewayPorts, oExitOnForwardFailure,
1.100 deraadt 114: oPasswordAuthentication, oRSAAuthentication,
1.59 markus 115: oChallengeResponseAuthentication, oXAuthLocation,
1.17 markus 116: oIdentityFile, oHostName, oPort, oCipher, oRemoteForward, oLocalForward,
117: oUser, oHost, oEscapeChar, oRhostsRSAAuthentication, oProxyCommand,
118: oGlobalKnownHostsFile, oUserKnownHostsFile, oConnectionAttempts,
119: oBatchMode, oCheckHostIP, oStrictHostKeyChecking, oCompression,
1.126 markus 120: oCompressionLevel, oTCPKeepAlive, oNumberOfPasswordPrompts,
1.62 markus 121: oUsePrivilegedPort, oLogLevel, oCiphers, oProtocol, oMacs,
1.50 markus 122: oGlobalKnownHostsFile2, oUserKnownHostsFile2, oPubkeyAuthentication,
1.67 markus 123: oKbdInteractiveAuthentication, oKbdInteractiveDevices, oHostKeyAlias,
1.76 markus 124: oDynamicForward, oPreferredAuthentications, oHostbasedAuthentication,
1.183 markus 125: oHostKeyAlgorithms, oBindAddress, oPKCS11Provider,
1.96 markus 126: oClearAllForwardings, oNoHostAuthenticationForLocalhost,
1.111 djm 127: oEnableSSHKeysign, oRekeyLimit, oVerifyHostKeyDNS, oConnectTimeout,
1.118 markus 128: oAddressFamily, oGssAuthentication, oGssDelegateCreds,
1.128 markus 129: oServerAliveInterval, oServerAliveCountMax, oIdentitiesOnly,
1.187 djm 130: oSendEnv, oControlPath, oControlMaster, oControlPersist,
131: oHashKnownHosts,
1.144 reyk 132: oTunnel, oTunnelDevice, oLocalCommand, oPermitLocalCommand,
1.182 dtucker 133: oVisualHostKey, oUseRoaming, oZeroKnowledgePasswordAuthentication,
1.192 djm 134: oKexAlgorithms, oIPQoS, oRequestTTY,
1.182 dtucker 135: oDeprecated, oUnsupported
1.1 deraadt 136: } OpCodes;
137:
138: /* Textual representations of the tokens. */
139:
1.17 markus 140: static struct {
141: const char *name;
142: OpCodes opcode;
143: } keywords[] = {
144: { "forwardagent", oForwardAgent },
145: { "forwardx11", oForwardX11 },
1.123 markus 146: { "forwardx11trusted", oForwardX11Trusted },
1.186 djm 147: { "forwardx11timeout", oForwardX11Timeout },
1.153 markus 148: { "exitonforwardfailure", oExitOnForwardFailure },
1.34 markus 149: { "xauthlocation", oXAuthLocation },
1.17 markus 150: { "gatewayports", oGatewayPorts },
151: { "useprivilegedport", oUsePrivilegedPort },
1.116 markus 152: { "rhostsauthentication", oDeprecated },
1.17 markus 153: { "passwordauthentication", oPasswordAuthentication },
1.48 markus 154: { "kbdinteractiveauthentication", oKbdInteractiveAuthentication },
155: { "kbdinteractivedevices", oKbdInteractiveDevices },
1.17 markus 156: { "rsaauthentication", oRSAAuthentication },
1.50 markus 157: { "pubkeyauthentication", oPubkeyAuthentication },
1.59 markus 158: { "dsaauthentication", oPubkeyAuthentication }, /* alias */
1.72 markus 159: { "rhostsrsaauthentication", oRhostsRSAAuthentication },
1.73 markus 160: { "hostbasedauthentication", oHostbasedAuthentication },
1.59 markus 161: { "challengeresponseauthentication", oChallengeResponseAuthentication },
162: { "skeyauthentication", oChallengeResponseAuthentication }, /* alias */
163: { "tisauthentication", oChallengeResponseAuthentication }, /* alias */
1.110 jakob 164: { "kerberosauthentication", oUnsupported },
165: { "kerberostgtpassing", oUnsupported },
166: { "afstokenpassing", oUnsupported },
1.118 markus 167: #if defined(GSSAPI)
168: { "gssapiauthentication", oGssAuthentication },
169: { "gssapidelegatecredentials", oGssDelegateCreds },
170: #else
171: { "gssapiauthentication", oUnsupported },
172: { "gssapidelegatecredentials", oUnsupported },
173: #endif
1.96 markus 174: { "fallbacktorsh", oDeprecated },
175: { "usersh", oDeprecated },
1.17 markus 176: { "identityfile", oIdentityFile },
1.174 stevesk 177: { "identityfile2", oIdentityFile }, /* obsolete */
1.128 markus 178: { "identitiesonly", oIdentitiesOnly },
1.17 markus 179: { "hostname", oHostName },
1.52 markus 180: { "hostkeyalias", oHostKeyAlias },
1.17 markus 181: { "proxycommand", oProxyCommand },
182: { "port", oPort },
183: { "cipher", oCipher },
1.25 markus 184: { "ciphers", oCiphers },
1.62 markus 185: { "macs", oMacs },
1.25 markus 186: { "protocol", oProtocol },
1.17 markus 187: { "remoteforward", oRemoteForward },
188: { "localforward", oLocalForward },
189: { "user", oUser },
190: { "host", oHost },
191: { "escapechar", oEscapeChar },
192: { "globalknownhostsfile", oGlobalKnownHostsFile },
1.193 djm 193: { "globalknownhostsfile2", oDeprecated },
1.174 stevesk 194: { "userknownhostsfile", oUserKnownHostsFile },
1.193 djm 195: { "userknownhostsfile2", oDeprecated },
1.17 markus 196: { "connectionattempts", oConnectionAttempts },
197: { "batchmode", oBatchMode },
198: { "checkhostip", oCheckHostIP },
199: { "stricthostkeychecking", oStrictHostKeyChecking },
200: { "compression", oCompression },
201: { "compressionlevel", oCompressionLevel },
1.126 markus 202: { "tcpkeepalive", oTCPKeepAlive },
203: { "keepalive", oTCPKeepAlive }, /* obsolete */
1.17 markus 204: { "numberofpasswordprompts", oNumberOfPasswordPrompts },
205: { "loglevel", oLogLevel },
1.71 markus 206: { "dynamicforward", oDynamicForward },
1.67 markus 207: { "preferredauthentications", oPreferredAuthentications },
1.76 markus 208: { "hostkeyalgorithms", oHostKeyAlgorithms },
1.77 markus 209: { "bindaddress", oBindAddress },
1.183 markus 210: #ifdef ENABLE_PKCS11
211: { "smartcarddevice", oPKCS11Provider },
212: { "pkcs11provider", oPKCS11Provider },
1.110 jakob 213: #else
214: { "smartcarddevice", oUnsupported },
1.183 markus 215: { "pkcs11provider", oUnsupported },
1.110 jakob 216: #endif
1.93 deraadt 217: { "clearallforwardings", oClearAllForwardings },
1.101 markus 218: { "enablesshkeysign", oEnableSSHKeysign },
1.107 jakob 219: { "verifyhostkeydns", oVerifyHostKeyDNS },
1.93 deraadt 220: { "nohostauthenticationforlocalhost", oNoHostAuthenticationForLocalhost },
1.105 markus 221: { "rekeylimit", oRekeyLimit },
1.111 djm 222: { "connecttimeout", oConnectTimeout },
1.112 djm 223: { "addressfamily", oAddressFamily },
1.127 markus 224: { "serveraliveinterval", oServerAliveInterval },
225: { "serveralivecountmax", oServerAliveCountMax },
1.130 djm 226: { "sendenv", oSendEnv },
1.132 djm 227: { "controlpath", oControlPath },
228: { "controlmaster", oControlMaster },
1.187 djm 229: { "controlpersist", oControlPersist },
1.136 djm 230: { "hashknownhosts", oHashKnownHosts },
1.144 reyk 231: { "tunnel", oTunnel },
232: { "tunneldevice", oTunnelDevice },
233: { "localcommand", oLocalCommand },
234: { "permitlocalcommand", oPermitLocalCommand },
1.167 grunk 235: { "visualhostkey", oVisualHostKey },
1.177 andreas 236: { "useroaming", oUseRoaming },
1.171 djm 237: #ifdef JPAKE
238: { "zeroknowledgepasswordauthentication",
239: oZeroKnowledgePasswordAuthentication },
240: #else
241: { "zeroknowledgepasswordauthentication", oUnsupported },
242: #endif
1.189 djm 243: { "kexalgorithms", oKexAlgorithms },
1.190 djm 244: { "ipqos", oIPQoS },
1.192 djm 245: { "requesttty", oRequestTTY },
1.171 djm 246:
1.92 stevesk 247: { NULL, oBadOption }
1.13 markus 248: };
249:
1.19 markus 250: /*
251: * Adds a local TCP/IP port forward to options. Never returns if there is an
252: * error.
253: */
1.1 deraadt 254:
1.26 markus 255: void
1.135 djm 256: add_local_forward(Options *options, const Forward *newfwd)
1.1 deraadt 257: {
1.17 markus 258: Forward *fwd;
259: extern uid_t original_real_uid;
1.185 djm 260:
1.135 djm 261: if (newfwd->listen_port < IPPORT_RESERVED && original_real_uid != 0)
1.64 millert 262: fatal("Privileged ports can only be forwarded by root.");
1.185 djm 263: options->local_forwards = xrealloc(options->local_forwards,
264: options->num_local_forwards + 1,
265: sizeof(*options->local_forwards));
1.17 markus 266: fwd = &options->local_forwards[options->num_local_forwards++];
1.135 djm 267:
1.172 stevesk 268: fwd->listen_host = newfwd->listen_host;
1.135 djm 269: fwd->listen_port = newfwd->listen_port;
1.172 stevesk 270: fwd->connect_host = newfwd->connect_host;
1.135 djm 271: fwd->connect_port = newfwd->connect_port;
1.1 deraadt 272: }
273:
1.19 markus 274: /*
275: * Adds a remote TCP/IP port forward to options. Never returns if there is
276: * an error.
277: */
1.1 deraadt 278:
1.26 markus 279: void
1.135 djm 280: add_remote_forward(Options *options, const Forward *newfwd)
1.1 deraadt 281: {
1.17 markus 282: Forward *fwd;
1.185 djm 283:
284: options->remote_forwards = xrealloc(options->remote_forwards,
285: options->num_remote_forwards + 1,
286: sizeof(*options->remote_forwards));
1.17 markus 287: fwd = &options->remote_forwards[options->num_remote_forwards++];
1.135 djm 288:
1.172 stevesk 289: fwd->listen_host = newfwd->listen_host;
1.135 djm 290: fwd->listen_port = newfwd->listen_port;
1.172 stevesk 291: fwd->connect_host = newfwd->connect_host;
1.135 djm 292: fwd->connect_port = newfwd->connect_port;
1.194 markus 293: fwd->handle = newfwd->handle;
1.184 markus 294: fwd->allocated_port = 0;
1.1 deraadt 295: }
296:
1.90 stevesk 297: static void
298: clear_forwardings(Options *options)
299: {
300: int i;
301:
1.135 djm 302: for (i = 0; i < options->num_local_forwards; i++) {
1.138 dtucker 303: if (options->local_forwards[i].listen_host != NULL)
304: xfree(options->local_forwards[i].listen_host);
1.135 djm 305: xfree(options->local_forwards[i].connect_host);
306: }
1.185 djm 307: if (options->num_local_forwards > 0) {
308: xfree(options->local_forwards);
309: options->local_forwards = NULL;
310: }
1.90 stevesk 311: options->num_local_forwards = 0;
1.135 djm 312: for (i = 0; i < options->num_remote_forwards; i++) {
1.138 dtucker 313: if (options->remote_forwards[i].listen_host != NULL)
314: xfree(options->remote_forwards[i].listen_host);
1.135 djm 315: xfree(options->remote_forwards[i].connect_host);
316: }
1.185 djm 317: if (options->num_remote_forwards > 0) {
318: xfree(options->remote_forwards);
319: options->remote_forwards = NULL;
320: }
1.90 stevesk 321: options->num_remote_forwards = 0;
1.145 reyk 322: options->tun_open = SSH_TUNMODE_NO;
1.90 stevesk 323: }
324:
1.195 dtucker 325: void
326: add_identity_file(Options *options, const char *dir, const char *filename,
327: int userprovided)
328: {
329: char *path;
330:
331: if (options->num_identity_files >= SSH_MAX_IDENTITY_FILES)
332: fatal("Too many identity files specified (max %d)",
333: SSH_MAX_IDENTITY_FILES);
334:
335: if (dir == NULL) /* no dir, filename is absolute */
336: path = xstrdup(filename);
337: else
338: (void)xasprintf(&path, "%.100s%.100s", dir, filename);
339:
340: options->identity_file_userprovided[options->num_identity_files] =
341: userprovided;
342: options->identity_files[options->num_identity_files++] = path;
343: }
344:
1.19 markus 345: /*
1.70 stevesk 346: * Returns the number of the token pointed to by cp or oBadOption.
1.19 markus 347: */
1.1 deraadt 348:
1.26 markus 349: static OpCodes
1.17 markus 350: parse_token(const char *cp, const char *filename, int linenum)
1.1 deraadt 351: {
1.51 markus 352: u_int i;
1.1 deraadt 353:
1.17 markus 354: for (i = 0; keywords[i].name; i++)
1.20 markus 355: if (strcasecmp(cp, keywords[i].name) == 0)
1.17 markus 356: return keywords[i].opcode;
357:
1.75 stevesk 358: error("%s: line %d: Bad configuration option: %s",
359: filename, linenum, cp);
1.17 markus 360: return oBadOption;
1.1 deraadt 361: }
362:
1.19 markus 363: /*
364: * Processes a single option line as used in the configuration files. This
365: * only sets those values that have not already been set.
366: */
1.102 markus 367: #define WHITESPACE " \t\r\n"
1.1 deraadt 368:
1.14 markus 369: int
370: process_config_line(Options *options, const char *host,
1.17 markus 371: char *line, const char *filename, int linenum,
1.196 dtucker 372: int *activep, int userconfig)
1.1 deraadt 373: {
1.193 djm 374: char *s, **charptr, *endofnumber, *keyword, *arg, *arg2;
375: char **cpptr, fwdarg[256];
376: u_int *uintptr, max_entries = 0;
1.191 djm 377: int negated, opcode, *intptr, value, value2, scale;
1.164 dtucker 378: LogLevel *log_level_ptr;
1.146 djm 379: long long orig, val64;
1.102 markus 380: size_t len;
1.135 djm 381: Forward fwd;
1.106 djm 382:
383: /* Strip trailing whitespace */
1.139 deraadt 384: for (len = strlen(line) - 1; len > 0; len--) {
1.106 djm 385: if (strchr(WHITESPACE, line[len]) == NULL)
386: break;
387: line[len] = '\0';
388: }
1.1 deraadt 389:
1.42 provos 390: s = line;
391: /* Get the keyword. (Each line is supposed to begin with a keyword). */
1.149 djm 392: if ((keyword = strdelim(&s)) == NULL)
393: return 0;
1.42 provos 394: /* Ignore leading whitespace. */
395: if (*keyword == '\0')
1.43 markus 396: keyword = strdelim(&s);
1.56 deraadt 397: if (keyword == NULL || !*keyword || *keyword == '\n' || *keyword == '#')
1.17 markus 398: return 0;
399:
1.38 provos 400: opcode = parse_token(keyword, filename, linenum);
1.17 markus 401:
402: switch (opcode) {
403: case oBadOption:
1.19 markus 404: /* don't panic, but count bad options */
405: return -1;
1.17 markus 406: /* NOTREACHED */
1.111 djm 407: case oConnectTimeout:
408: intptr = &options->connection_timeout;
1.127 markus 409: parse_time:
1.111 djm 410: arg = strdelim(&s);
411: if (!arg || *arg == '\0')
412: fatal("%s line %d: missing time value.",
413: filename, linenum);
414: if ((value = convtime(arg)) == -1)
415: fatal("%s line %d: invalid time value.",
416: filename, linenum);
1.160 dtucker 417: if (*activep && *intptr == -1)
1.111 djm 418: *intptr = value;
419: break;
420:
1.17 markus 421: case oForwardAgent:
422: intptr = &options->forward_agent;
423: parse_flag:
1.42 provos 424: arg = strdelim(&s);
1.40 ho 425: if (!arg || *arg == '\0')
1.17 markus 426: fatal("%.200s line %d: Missing yes/no argument.", filename, linenum);
427: value = 0; /* To avoid compiler warning... */
1.38 provos 428: if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0)
1.17 markus 429: value = 1;
1.38 provos 430: else if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0)
1.17 markus 431: value = 0;
432: else
433: fatal("%.200s line %d: Bad yes/no argument.", filename, linenum);
434: if (*activep && *intptr == -1)
435: *intptr = value;
436: break;
437:
438: case oForwardX11:
439: intptr = &options->forward_x11;
440: goto parse_flag;
441:
1.123 markus 442: case oForwardX11Trusted:
443: intptr = &options->forward_x11_trusted;
444: goto parse_flag;
1.186 djm 445:
446: case oForwardX11Timeout:
447: intptr = &options->forward_x11_timeout;
448: goto parse_time;
1.123 markus 449:
1.17 markus 450: case oGatewayPorts:
451: intptr = &options->gateway_ports;
452: goto parse_flag;
453:
1.153 markus 454: case oExitOnForwardFailure:
455: intptr = &options->exit_on_forward_failure;
456: goto parse_flag;
457:
1.17 markus 458: case oUsePrivilegedPort:
459: intptr = &options->use_privileged_port;
460: goto parse_flag;
461:
462: case oPasswordAuthentication:
463: intptr = &options->password_authentication;
464: goto parse_flag;
465:
1.171 djm 466: case oZeroKnowledgePasswordAuthentication:
467: intptr = &options->zero_knowledge_password_authentication;
468: goto parse_flag;
469:
1.48 markus 470: case oKbdInteractiveAuthentication:
471: intptr = &options->kbd_interactive_authentication;
472: goto parse_flag;
473:
474: case oKbdInteractiveDevices:
475: charptr = &options->kbd_interactive_devices;
476: goto parse_string;
477:
1.50 markus 478: case oPubkeyAuthentication:
479: intptr = &options->pubkey_authentication;
1.30 markus 480: goto parse_flag;
481:
1.17 markus 482: case oRSAAuthentication:
483: intptr = &options->rsa_authentication;
484: goto parse_flag;
485:
486: case oRhostsRSAAuthentication:
487: intptr = &options->rhosts_rsa_authentication;
488: goto parse_flag;
489:
1.72 markus 490: case oHostbasedAuthentication:
491: intptr = &options->hostbased_authentication;
492: goto parse_flag;
493:
1.59 markus 494: case oChallengeResponseAuthentication:
1.78 markus 495: intptr = &options->challenge_response_authentication;
1.17 markus 496: goto parse_flag;
1.108 jakob 497:
1.118 markus 498: case oGssAuthentication:
499: intptr = &options->gss_authentication;
500: goto parse_flag;
501:
502: case oGssDelegateCreds:
503: intptr = &options->gss_deleg_creds;
504: goto parse_flag;
505:
1.17 markus 506: case oBatchMode:
507: intptr = &options->batch_mode;
508: goto parse_flag;
509:
510: case oCheckHostIP:
511: intptr = &options->check_host_ip;
1.167 grunk 512: goto parse_flag;
1.17 markus 513:
1.107 jakob 514: case oVerifyHostKeyDNS:
515: intptr = &options->verify_host_key_dns;
1.125 jakob 516: goto parse_yesnoask;
1.107 jakob 517:
1.17 markus 518: case oStrictHostKeyChecking:
519: intptr = &options->strict_host_key_checking;
1.125 jakob 520: parse_yesnoask:
1.42 provos 521: arg = strdelim(&s);
1.40 ho 522: if (!arg || *arg == '\0')
1.60 stevesk 523: fatal("%.200s line %d: Missing yes/no/ask argument.",
1.93 deraadt 524: filename, linenum);
1.17 markus 525: value = 0; /* To avoid compiler warning... */
1.38 provos 526: if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0)
1.17 markus 527: value = 1;
1.38 provos 528: else if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0)
1.17 markus 529: value = 0;
1.38 provos 530: else if (strcmp(arg, "ask") == 0)
1.17 markus 531: value = 2;
532: else
533: fatal("%.200s line %d: Bad yes/no/ask argument.", filename, linenum);
534: if (*activep && *intptr == -1)
535: *intptr = value;
536: break;
537:
538: case oCompression:
539: intptr = &options->compression;
540: goto parse_flag;
541:
1.126 markus 542: case oTCPKeepAlive:
543: intptr = &options->tcp_keep_alive;
1.17 markus 544: goto parse_flag;
545:
1.91 markus 546: case oNoHostAuthenticationForLocalhost:
547: intptr = &options->no_host_authentication_for_localhost;
548: goto parse_flag;
549:
1.17 markus 550: case oNumberOfPasswordPrompts:
551: intptr = &options->number_of_password_prompts;
552: goto parse_int;
553:
554: case oCompressionLevel:
555: intptr = &options->compression_level;
556: goto parse_int;
557:
1.105 markus 558: case oRekeyLimit:
559: arg = strdelim(&s);
560: if (!arg || *arg == '\0')
1.198 ! dtucker 561: fatal("%.200s line %d: Missing argument.", filename,
! 562: linenum);
! 563: if (strcmp(arg, "default") == 0) {
! 564: val64 = 0;
! 565: } else {
! 566: if (arg[0] < '0' || arg[0] > '9')
! 567: fatal("%.200s line %d: Bad number.", filename,
! 568: linenum);
! 569: orig = val64 = strtoll(arg, &endofnumber, 10);
! 570: if (arg == endofnumber)
! 571: fatal("%.200s line %d: Bad number.", filename,
! 572: linenum);
! 573: switch (toupper(*endofnumber)) {
! 574: case '\0':
! 575: scale = 1;
! 576: break;
! 577: case 'K':
! 578: scale = 1<<10;
! 579: break;
! 580: case 'M':
! 581: scale = 1<<20;
! 582: break;
! 583: case 'G':
! 584: scale = 1<<30;
! 585: break;
! 586: default:
! 587: fatal("%.200s line %d: Invalid RekeyLimit "
! 588: "suffix", filename, linenum);
! 589: }
! 590: val64 *= scale;
! 591: /* detect integer wrap and too-large limits */
! 592: if ((val64 / scale) != orig || val64 > UINT_MAX)
! 593: fatal("%.200s line %d: RekeyLimit too large",
! 594: filename, linenum);
! 595: if (val64 != 0 && val64 < 16)
! 596: fatal("%.200s line %d: RekeyLimit too small",
! 597: filename, linenum);
1.105 markus 598: }
1.165 djm 599: if (*activep && options->rekey_limit == -1)
600: options->rekey_limit = (u_int32_t)val64;
1.198 ! dtucker 601: if (s != NULL) { /* optional rekey interval present */
! 602: if (strcmp(s, "none") == 0) {
! 603: (void)strdelim(&s); /* discard */
! 604: break;
! 605: }
! 606: intptr = &options->rekey_interval;
! 607: goto parse_time;
! 608: }
1.105 markus 609: break;
610:
1.17 markus 611: case oIdentityFile:
1.42 provos 612: arg = strdelim(&s);
1.40 ho 613: if (!arg || *arg == '\0')
1.17 markus 614: fatal("%.200s line %d: Missing argument.", filename, linenum);
615: if (*activep) {
1.50 markus 616: intptr = &options->num_identity_files;
1.27 markus 617: if (*intptr >= SSH_MAX_IDENTITY_FILES)
1.17 markus 618: fatal("%.200s line %d: Too many identity files specified (max %d).",
1.93 deraadt 619: filename, linenum, SSH_MAX_IDENTITY_FILES);
1.196 dtucker 620: add_identity_file(options, NULL, arg, userconfig);
1.17 markus 621: }
622: break;
623:
1.34 markus 624: case oXAuthLocation:
625: charptr=&options->xauth_location;
626: goto parse_string;
627:
1.17 markus 628: case oUser:
629: charptr = &options->user;
630: parse_string:
1.42 provos 631: arg = strdelim(&s);
1.40 ho 632: if (!arg || *arg == '\0')
1.193 djm 633: fatal("%.200s line %d: Missing argument.",
634: filename, linenum);
1.17 markus 635: if (*activep && *charptr == NULL)
1.38 provos 636: *charptr = xstrdup(arg);
1.17 markus 637: break;
638:
639: case oGlobalKnownHostsFile:
1.193 djm 640: cpptr = (char **)&options->system_hostfiles;
641: uintptr = &options->num_system_hostfiles;
642: max_entries = SSH_MAX_HOSTS_FILES;
643: parse_char_array:
644: if (*activep && *uintptr == 0) {
645: while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
646: if ((*uintptr) >= max_entries)
647: fatal("%s line %d: "
648: "too many authorized keys files.",
649: filename, linenum);
650: cpptr[(*uintptr)++] = xstrdup(arg);
651: }
652: }
653: return 0;
1.17 markus 654:
655: case oUserKnownHostsFile:
1.193 djm 656: cpptr = (char **)&options->user_hostfiles;
657: uintptr = &options->num_user_hostfiles;
658: max_entries = SSH_MAX_HOSTS_FILES;
659: goto parse_char_array;
1.27 markus 660:
1.17 markus 661: case oHostName:
662: charptr = &options->hostname;
663: goto parse_string;
664:
1.52 markus 665: case oHostKeyAlias:
666: charptr = &options->host_key_alias;
667: goto parse_string;
668:
1.67 markus 669: case oPreferredAuthentications:
670: charptr = &options->preferred_authentications;
671: goto parse_string;
672:
1.77 markus 673: case oBindAddress:
674: charptr = &options->bind_address;
675: goto parse_string;
676:
1.183 markus 677: case oPKCS11Provider:
678: charptr = &options->pkcs11_provider;
1.86 markus 679: goto parse_string;
1.85 jakob 680:
1.17 markus 681: case oProxyCommand:
1.144 reyk 682: charptr = &options->proxy_command;
683: parse_command:
1.113 markus 684: if (s == NULL)
685: fatal("%.200s line %d: Missing argument.", filename, linenum);
1.102 markus 686: len = strspn(s, WHITESPACE "=");
1.17 markus 687: if (*activep && *charptr == NULL)
1.102 markus 688: *charptr = xstrdup(s + len);
1.17 markus 689: return 0;
690:
691: case oPort:
692: intptr = &options->port;
693: parse_int:
1.42 provos 694: arg = strdelim(&s);
1.40 ho 695: if (!arg || *arg == '\0')
1.17 markus 696: fatal("%.200s line %d: Missing argument.", filename, linenum);
1.38 provos 697: if (arg[0] < '0' || arg[0] > '9')
1.17 markus 698: fatal("%.200s line %d: Bad number.", filename, linenum);
1.21 markus 699:
700: /* Octal, decimal, or hex format? */
1.38 provos 701: value = strtol(arg, &endofnumber, 0);
702: if (arg == endofnumber)
1.21 markus 703: fatal("%.200s line %d: Bad number.", filename, linenum);
1.17 markus 704: if (*activep && *intptr == -1)
705: *intptr = value;
706: break;
707:
708: case oConnectionAttempts:
709: intptr = &options->connection_attempts;
710: goto parse_int;
711:
712: case oCipher:
713: intptr = &options->cipher;
1.42 provos 714: arg = strdelim(&s);
1.40 ho 715: if (!arg || *arg == '\0')
1.32 markus 716: fatal("%.200s line %d: Missing argument.", filename, linenum);
1.38 provos 717: value = cipher_number(arg);
1.17 markus 718: if (value == -1)
719: fatal("%.200s line %d: Bad cipher '%s'.",
1.93 deraadt 720: filename, linenum, arg ? arg : "<NONE>");
1.17 markus 721: if (*activep && *intptr == -1)
722: *intptr = value;
723: break;
724:
1.25 markus 725: case oCiphers:
1.42 provos 726: arg = strdelim(&s);
1.40 ho 727: if (!arg || *arg == '\0')
1.32 markus 728: fatal("%.200s line %d: Missing argument.", filename, linenum);
1.38 provos 729: if (!ciphers_valid(arg))
1.31 markus 730: fatal("%.200s line %d: Bad SSH2 cipher spec '%s'.",
1.93 deraadt 731: filename, linenum, arg ? arg : "<NONE>");
1.25 markus 732: if (*activep && options->ciphers == NULL)
1.38 provos 733: options->ciphers = xstrdup(arg);
1.25 markus 734: break;
735:
1.62 markus 736: case oMacs:
737: arg = strdelim(&s);
738: if (!arg || *arg == '\0')
739: fatal("%.200s line %d: Missing argument.", filename, linenum);
740: if (!mac_valid(arg))
741: fatal("%.200s line %d: Bad SSH2 Mac spec '%s'.",
1.93 deraadt 742: filename, linenum, arg ? arg : "<NONE>");
1.62 markus 743: if (*activep && options->macs == NULL)
744: options->macs = xstrdup(arg);
745: break;
746:
1.189 djm 747: case oKexAlgorithms:
748: arg = strdelim(&s);
749: if (!arg || *arg == '\0')
750: fatal("%.200s line %d: Missing argument.",
751: filename, linenum);
752: if (!kex_names_valid(arg))
753: fatal("%.200s line %d: Bad SSH2 KexAlgorithms '%s'.",
754: filename, linenum, arg ? arg : "<NONE>");
755: if (*activep && options->kex_algorithms == NULL)
756: options->kex_algorithms = xstrdup(arg);
757: break;
758:
1.76 markus 759: case oHostKeyAlgorithms:
760: arg = strdelim(&s);
761: if (!arg || *arg == '\0')
762: fatal("%.200s line %d: Missing argument.", filename, linenum);
763: if (!key_names_valid2(arg))
764: fatal("%.200s line %d: Bad protocol 2 host key algorithms '%s'.",
1.93 deraadt 765: filename, linenum, arg ? arg : "<NONE>");
1.76 markus 766: if (*activep && options->hostkeyalgorithms == NULL)
767: options->hostkeyalgorithms = xstrdup(arg);
768: break;
769:
1.25 markus 770: case oProtocol:
771: intptr = &options->protocol;
1.42 provos 772: arg = strdelim(&s);
1.40 ho 773: if (!arg || *arg == '\0')
1.32 markus 774: fatal("%.200s line %d: Missing argument.", filename, linenum);
1.38 provos 775: value = proto_spec(arg);
1.25 markus 776: if (value == SSH_PROTO_UNKNOWN)
777: fatal("%.200s line %d: Bad protocol spec '%s'.",
1.93 deraadt 778: filename, linenum, arg ? arg : "<NONE>");
1.25 markus 779: if (*activep && *intptr == SSH_PROTO_UNKNOWN)
780: *intptr = value;
781: break;
782:
1.17 markus 783: case oLogLevel:
1.164 dtucker 784: log_level_ptr = &options->log_level;
1.42 provos 785: arg = strdelim(&s);
1.38 provos 786: value = log_level_number(arg);
1.95 markus 787: if (value == SYSLOG_LEVEL_NOT_SET)
1.64 millert 788: fatal("%.200s line %d: unsupported log level '%s'",
1.93 deraadt 789: filename, linenum, arg ? arg : "<NONE>");
1.164 dtucker 790: if (*activep && *log_level_ptr == SYSLOG_LEVEL_NOT_SET)
791: *log_level_ptr = (LogLevel) value;
1.17 markus 792: break;
793:
1.88 stevesk 794: case oLocalForward:
1.17 markus 795: case oRemoteForward:
1.168 stevesk 796: case oDynamicForward:
1.42 provos 797: arg = strdelim(&s);
1.135 djm 798: if (arg == NULL || *arg == '\0')
1.88 stevesk 799: fatal("%.200s line %d: Missing port argument.",
800: filename, linenum);
1.135 djm 801:
1.168 stevesk 802: if (opcode == oLocalForward ||
803: opcode == oRemoteForward) {
804: arg2 = strdelim(&s);
805: if (arg2 == NULL || *arg2 == '\0')
806: fatal("%.200s line %d: Missing target argument.",
807: filename, linenum);
1.135 djm 808:
1.168 stevesk 809: /* construct a string for parse_forward */
810: snprintf(fwdarg, sizeof(fwdarg), "%s:%s", arg, arg2);
811: } else if (opcode == oDynamicForward) {
812: strlcpy(fwdarg, arg, sizeof(fwdarg));
813: }
814:
815: if (parse_forward(&fwd, fwdarg,
1.176 djm 816: opcode == oDynamicForward ? 1 : 0,
817: opcode == oRemoteForward ? 1 : 0) == 0)
1.88 stevesk 818: fatal("%.200s line %d: Bad forwarding specification.",
819: filename, linenum);
1.135 djm 820:
1.88 stevesk 821: if (*activep) {
1.168 stevesk 822: if (opcode == oLocalForward ||
823: opcode == oDynamicForward)
1.135 djm 824: add_local_forward(options, &fwd);
1.88 stevesk 825: else if (opcode == oRemoteForward)
1.135 djm 826: add_remote_forward(options, &fwd);
1.88 stevesk 827: }
1.17 markus 828: break;
1.71 markus 829:
1.90 stevesk 830: case oClearAllForwardings:
831: intptr = &options->clear_forwardings;
832: goto parse_flag;
833:
1.17 markus 834: case oHost:
835: *activep = 0;
1.191 djm 836: arg2 = NULL;
837: while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
838: negated = *arg == '!';
839: if (negated)
840: arg++;
1.38 provos 841: if (match_pattern(host, arg)) {
1.191 djm 842: if (negated) {
843: debug("%.200s line %d: Skipping Host "
844: "block because of negated match "
845: "for %.100s", filename, linenum,
846: arg);
847: *activep = 0;
848: break;
849: }
850: if (!*activep)
851: arg2 = arg; /* logged below */
1.17 markus 852: *activep = 1;
853: }
1.191 djm 854: }
855: if (*activep)
856: debug("%.200s line %d: Applying options for %.100s",
857: filename, linenum, arg2);
1.42 provos 858: /* Avoid garbage check below, as strdelim is done. */
1.17 markus 859: return 0;
860:
861: case oEscapeChar:
862: intptr = &options->escape_char;
1.42 provos 863: arg = strdelim(&s);
1.40 ho 864: if (!arg || *arg == '\0')
1.17 markus 865: fatal("%.200s line %d: Missing argument.", filename, linenum);
1.38 provos 866: if (arg[0] == '^' && arg[2] == 0 &&
1.51 markus 867: (u_char) arg[1] >= 64 && (u_char) arg[1] < 128)
868: value = (u_char) arg[1] & 31;
1.38 provos 869: else if (strlen(arg) == 1)
1.51 markus 870: value = (u_char) arg[0];
1.38 provos 871: else if (strcmp(arg, "none") == 0)
1.79 stevesk 872: value = SSH_ESCAPECHAR_NONE;
1.17 markus 873: else {
874: fatal("%.200s line %d: Bad escape character.",
1.93 deraadt 875: filename, linenum);
1.17 markus 876: /* NOTREACHED */
877: value = 0; /* Avoid compiler warning. */
878: }
879: if (*activep && *intptr == -1)
880: *intptr = value;
1.112 djm 881: break;
882:
883: case oAddressFamily:
884: arg = strdelim(&s);
1.140 markus 885: if (!arg || *arg == '\0')
886: fatal("%s line %d: missing address family.",
887: filename, linenum);
1.114 djm 888: intptr = &options->address_family;
1.112 djm 889: if (strcasecmp(arg, "inet") == 0)
1.114 djm 890: value = AF_INET;
1.112 djm 891: else if (strcasecmp(arg, "inet6") == 0)
1.114 djm 892: value = AF_INET6;
1.112 djm 893: else if (strcasecmp(arg, "any") == 0)
1.114 djm 894: value = AF_UNSPEC;
1.112 djm 895: else
896: fatal("Unsupported AddressFamily \"%s\"", arg);
1.114 djm 897: if (*activep && *intptr == -1)
898: *intptr = value;
1.17 markus 899: break;
900:
1.101 markus 901: case oEnableSSHKeysign:
902: intptr = &options->enable_ssh_keysign;
903: goto parse_flag;
904:
1.128 markus 905: case oIdentitiesOnly:
906: intptr = &options->identities_only;
907: goto parse_flag;
908:
1.127 markus 909: case oServerAliveInterval:
910: intptr = &options->server_alive_interval;
911: goto parse_time;
912:
913: case oServerAliveCountMax:
914: intptr = &options->server_alive_count_max;
915: goto parse_int;
916:
1.130 djm 917: case oSendEnv:
918: while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
919: if (strchr(arg, '=') != NULL)
920: fatal("%s line %d: Invalid environment name.",
921: filename, linenum);
1.137 djm 922: if (!*activep)
923: continue;
1.130 djm 924: if (options->num_send_env >= MAX_SEND_ENV)
925: fatal("%s line %d: too many send env.",
926: filename, linenum);
927: options->send_env[options->num_send_env++] =
928: xstrdup(arg);
929: }
930: break;
931:
1.132 djm 932: case oControlPath:
933: charptr = &options->control_path;
934: goto parse_string;
935:
936: case oControlMaster:
937: intptr = &options->control_master;
1.141 djm 938: arg = strdelim(&s);
939: if (!arg || *arg == '\0')
940: fatal("%.200s line %d: Missing ControlMaster argument.",
941: filename, linenum);
942: value = 0; /* To avoid compiler warning... */
943: if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0)
944: value = SSHCTL_MASTER_YES;
945: else if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0)
946: value = SSHCTL_MASTER_NO;
947: else if (strcmp(arg, "auto") == 0)
948: value = SSHCTL_MASTER_AUTO;
949: else if (strcmp(arg, "ask") == 0)
950: value = SSHCTL_MASTER_ASK;
951: else if (strcmp(arg, "autoask") == 0)
952: value = SSHCTL_MASTER_AUTO_ASK;
953: else
954: fatal("%.200s line %d: Bad ControlMaster argument.",
955: filename, linenum);
956: if (*activep && *intptr == -1)
957: *intptr = value;
958: break;
1.132 djm 959:
1.187 djm 960: case oControlPersist:
961: /* no/false/yes/true, or a time spec */
962: intptr = &options->control_persist;
963: arg = strdelim(&s);
964: if (!arg || *arg == '\0')
965: fatal("%.200s line %d: Missing ControlPersist"
966: " argument.", filename, linenum);
967: value = 0;
968: value2 = 0; /* timeout */
969: if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0)
970: value = 0;
971: else if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0)
972: value = 1;
973: else if ((value2 = convtime(arg)) >= 0)
974: value = 1;
975: else
976: fatal("%.200s line %d: Bad ControlPersist argument.",
977: filename, linenum);
978: if (*activep && *intptr == -1) {
979: *intptr = value;
980: options->control_persist_timeout = value2;
981: }
982: break;
983:
1.136 djm 984: case oHashKnownHosts:
985: intptr = &options->hash_known_hosts;
986: goto parse_flag;
987:
1.144 reyk 988: case oTunnel:
989: intptr = &options->tun_open;
1.145 reyk 990: arg = strdelim(&s);
991: if (!arg || *arg == '\0')
992: fatal("%s line %d: Missing yes/point-to-point/"
993: "ethernet/no argument.", filename, linenum);
994: value = 0; /* silence compiler */
995: if (strcasecmp(arg, "ethernet") == 0)
996: value = SSH_TUNMODE_ETHERNET;
997: else if (strcasecmp(arg, "point-to-point") == 0)
998: value = SSH_TUNMODE_POINTOPOINT;
999: else if (strcasecmp(arg, "yes") == 0)
1000: value = SSH_TUNMODE_DEFAULT;
1001: else if (strcasecmp(arg, "no") == 0)
1002: value = SSH_TUNMODE_NO;
1003: else
1004: fatal("%s line %d: Bad yes/point-to-point/ethernet/"
1005: "no argument: %s", filename, linenum, arg);
1006: if (*activep)
1007: *intptr = value;
1008: break;
1.144 reyk 1009:
1010: case oTunnelDevice:
1011: arg = strdelim(&s);
1012: if (!arg || *arg == '\0')
1013: fatal("%.200s line %d: Missing argument.", filename, linenum);
1014: value = a2tun(arg, &value2);
1.145 reyk 1015: if (value == SSH_TUNID_ERR)
1.144 reyk 1016: fatal("%.200s line %d: Bad tun device.", filename, linenum);
1017: if (*activep) {
1018: options->tun_local = value;
1019: options->tun_remote = value2;
1020: }
1021: break;
1022:
1023: case oLocalCommand:
1024: charptr = &options->local_command;
1025: goto parse_command;
1026:
1027: case oPermitLocalCommand:
1028: intptr = &options->permit_local_command;
1029: goto parse_flag;
1030:
1.167 grunk 1031: case oVisualHostKey:
1032: intptr = &options->visual_host_key;
1033: goto parse_flag;
1034:
1.190 djm 1035: case oIPQoS:
1036: arg = strdelim(&s);
1037: if ((value = parse_ipqos(arg)) == -1)
1038: fatal("%s line %d: Bad IPQoS value: %s",
1039: filename, linenum, arg);
1040: arg = strdelim(&s);
1041: if (arg == NULL)
1042: value2 = value;
1043: else if ((value2 = parse_ipqos(arg)) == -1)
1044: fatal("%s line %d: Bad IPQoS value: %s",
1045: filename, linenum, arg);
1046: if (*activep) {
1047: options->ip_qos_interactive = value;
1048: options->ip_qos_bulk = value2;
1049: }
1050: break;
1051:
1.177 andreas 1052: case oUseRoaming:
1053: intptr = &options->use_roaming;
1054: goto parse_flag;
1055:
1.192 djm 1056: case oRequestTTY:
1057: arg = strdelim(&s);
1058: if (!arg || *arg == '\0')
1059: fatal("%s line %d: missing argument.",
1060: filename, linenum);
1061: intptr = &options->request_tty;
1062: if (strcasecmp(arg, "yes") == 0)
1063: value = REQUEST_TTY_YES;
1064: else if (strcasecmp(arg, "no") == 0)
1065: value = REQUEST_TTY_NO;
1066: else if (strcasecmp(arg, "force") == 0)
1067: value = REQUEST_TTY_FORCE;
1068: else if (strcasecmp(arg, "auto") == 0)
1069: value = REQUEST_TTY_AUTO;
1070: else
1071: fatal("Unsupported RequestTTY \"%s\"", arg);
1072: if (*activep && *intptr == -1)
1073: *intptr = value;
1074: break;
1075:
1.96 markus 1076: case oDeprecated:
1.98 markus 1077: debug("%s line %d: Deprecated option \"%s\"",
1.96 markus 1078: filename, linenum, keyword);
1.98 markus 1079: return 0;
1.96 markus 1080:
1.110 jakob 1081: case oUnsupported:
1082: error("%s line %d: Unsupported option \"%s\"",
1083: filename, linenum, keyword);
1084: return 0;
1085:
1.17 markus 1086: default:
1087: fatal("process_config_line: Unimplemented opcode %d", opcode);
1088: }
1089:
1090: /* Check that there is no garbage at end of line. */
1.57 djm 1091: if ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1.39 ho 1092: fatal("%.200s line %d: garbage at end of line; \"%.200s\".",
1.142 djm 1093: filename, linenum, arg);
1.39 ho 1094: }
1.17 markus 1095: return 0;
1.1 deraadt 1096: }
1097:
1098:
1.19 markus 1099: /*
1100: * Reads the config file and modifies the options accordingly. Options
1101: * should already be initialized before this call. This never returns if
1.89 stevesk 1102: * there is an error. If the file does not exist, this returns 0.
1.19 markus 1103: */
1.1 deraadt 1104:
1.89 stevesk 1105: int
1.134 deraadt 1106: read_config_file(const char *filename, const char *host, Options *options,
1.196 dtucker 1107: int flags)
1.1 deraadt 1108: {
1.17 markus 1109: FILE *f;
1110: char line[1024];
1111: int active, linenum;
1112: int bad_options = 0;
1113:
1.129 djm 1114: if ((f = fopen(filename, "r")) == NULL)
1.89 stevesk 1115: return 0;
1.129 djm 1116:
1.196 dtucker 1117: if (flags & SSHCONF_CHECKPERM) {
1.129 djm 1118: struct stat sb;
1.134 deraadt 1119:
1.131 dtucker 1120: if (fstat(fileno(f), &sb) == -1)
1.129 djm 1121: fatal("fstat %s: %s", filename, strerror(errno));
1122: if (((sb.st_uid != 0 && sb.st_uid != getuid()) ||
1.131 dtucker 1123: (sb.st_mode & 022) != 0))
1.129 djm 1124: fatal("Bad owner or permissions on %s", filename);
1125: }
1.17 markus 1126:
1127: debug("Reading configuration data %.200s", filename);
1128:
1.19 markus 1129: /*
1130: * Mark that we are now processing the options. This flag is turned
1131: * on/off by Host specifications.
1132: */
1.17 markus 1133: active = 1;
1134: linenum = 0;
1135: while (fgets(line, sizeof(line), f)) {
1136: /* Update line number counter. */
1137: linenum++;
1.196 dtucker 1138: if (process_config_line(options, host, line, filename, linenum,
1139: &active, flags & SSHCONF_USERCONF) != 0)
1.17 markus 1140: bad_options++;
1141: }
1142: fclose(f);
1143: if (bad_options > 0)
1.64 millert 1144: fatal("%s: terminating, %d bad configuration options",
1.93 deraadt 1145: filename, bad_options);
1.89 stevesk 1146: return 1;
1.1 deraadt 1147: }
1148:
1.19 markus 1149: /*
1150: * Initializes options to special values that indicate that they have not yet
1151: * been set. Read_config_file will only set options with this value. Options
1152: * are processed in the following order: command line, user config file,
1153: * system config file. Last, fill_default_options is called.
1154: */
1.1 deraadt 1155:
1.26 markus 1156: void
1.17 markus 1157: initialize_options(Options * options)
1.1 deraadt 1158: {
1.17 markus 1159: memset(options, 'X', sizeof(*options));
1160: options->forward_agent = -1;
1161: options->forward_x11 = -1;
1.123 markus 1162: options->forward_x11_trusted = -1;
1.186 djm 1163: options->forward_x11_timeout = -1;
1.153 markus 1164: options->exit_on_forward_failure = -1;
1.34 markus 1165: options->xauth_location = NULL;
1.17 markus 1166: options->gateway_ports = -1;
1167: options->use_privileged_port = -1;
1168: options->rsa_authentication = -1;
1.50 markus 1169: options->pubkey_authentication = -1;
1.78 markus 1170: options->challenge_response_authentication = -1;
1.118 markus 1171: options->gss_authentication = -1;
1172: options->gss_deleg_creds = -1;
1.17 markus 1173: options->password_authentication = -1;
1.48 markus 1174: options->kbd_interactive_authentication = -1;
1175: options->kbd_interactive_devices = NULL;
1.17 markus 1176: options->rhosts_rsa_authentication = -1;
1.72 markus 1177: options->hostbased_authentication = -1;
1.17 markus 1178: options->batch_mode = -1;
1179: options->check_host_ip = -1;
1180: options->strict_host_key_checking = -1;
1181: options->compression = -1;
1.126 markus 1182: options->tcp_keep_alive = -1;
1.17 markus 1183: options->compression_level = -1;
1184: options->port = -1;
1.114 djm 1185: options->address_family = -1;
1.17 markus 1186: options->connection_attempts = -1;
1.111 djm 1187: options->connection_timeout = -1;
1.17 markus 1188: options->number_of_password_prompts = -1;
1189: options->cipher = -1;
1.25 markus 1190: options->ciphers = NULL;
1.62 markus 1191: options->macs = NULL;
1.189 djm 1192: options->kex_algorithms = NULL;
1.76 markus 1193: options->hostkeyalgorithms = NULL;
1.25 markus 1194: options->protocol = SSH_PROTO_UNKNOWN;
1.17 markus 1195: options->num_identity_files = 0;
1196: options->hostname = NULL;
1.52 markus 1197: options->host_key_alias = NULL;
1.17 markus 1198: options->proxy_command = NULL;
1199: options->user = NULL;
1200: options->escape_char = -1;
1.193 djm 1201: options->num_system_hostfiles = 0;
1202: options->num_user_hostfiles = 0;
1.185 djm 1203: options->local_forwards = NULL;
1.17 markus 1204: options->num_local_forwards = 0;
1.185 djm 1205: options->remote_forwards = NULL;
1.17 markus 1206: options->num_remote_forwards = 0;
1.90 stevesk 1207: options->clear_forwardings = -1;
1.95 markus 1208: options->log_level = SYSLOG_LEVEL_NOT_SET;
1.67 markus 1209: options->preferred_authentications = NULL;
1.77 markus 1210: options->bind_address = NULL;
1.183 markus 1211: options->pkcs11_provider = NULL;
1.101 markus 1212: options->enable_ssh_keysign = - 1;
1.91 markus 1213: options->no_host_authentication_for_localhost = - 1;
1.128 markus 1214: options->identities_only = - 1;
1.105 markus 1215: options->rekey_limit = - 1;
1.198 ! dtucker 1216: options->rekey_interval = -1;
1.107 jakob 1217: options->verify_host_key_dns = -1;
1.127 markus 1218: options->server_alive_interval = -1;
1219: options->server_alive_count_max = -1;
1.130 djm 1220: options->num_send_env = 0;
1.132 djm 1221: options->control_path = NULL;
1222: options->control_master = -1;
1.187 djm 1223: options->control_persist = -1;
1224: options->control_persist_timeout = 0;
1.136 djm 1225: options->hash_known_hosts = -1;
1.144 reyk 1226: options->tun_open = -1;
1227: options->tun_local = -1;
1228: options->tun_remote = -1;
1229: options->local_command = NULL;
1230: options->permit_local_command = -1;
1.177 andreas 1231: options->use_roaming = -1;
1.167 grunk 1232: options->visual_host_key = -1;
1.171 djm 1233: options->zero_knowledge_password_authentication = -1;
1.190 djm 1234: options->ip_qos_interactive = -1;
1235: options->ip_qos_bulk = -1;
1.192 djm 1236: options->request_tty = -1;
1.1 deraadt 1237: }
1238:
1.19 markus 1239: /*
1240: * Called after processing other sources of option data, this fills those
1241: * options for which no value has been specified with their default values.
1242: */
1.1 deraadt 1243:
1.26 markus 1244: void
1.17 markus 1245: fill_default_options(Options * options)
1.1 deraadt 1246: {
1.17 markus 1247: if (options->forward_agent == -1)
1.33 markus 1248: options->forward_agent = 0;
1.17 markus 1249: if (options->forward_x11 == -1)
1.23 markus 1250: options->forward_x11 = 0;
1.123 markus 1251: if (options->forward_x11_trusted == -1)
1252: options->forward_x11_trusted = 0;
1.186 djm 1253: if (options->forward_x11_timeout == -1)
1254: options->forward_x11_timeout = 1200;
1.153 markus 1255: if (options->exit_on_forward_failure == -1)
1256: options->exit_on_forward_failure = 0;
1.34 markus 1257: if (options->xauth_location == NULL)
1.80 markus 1258: options->xauth_location = _PATH_XAUTH;
1.17 markus 1259: if (options->gateway_ports == -1)
1260: options->gateway_ports = 0;
1261: if (options->use_privileged_port == -1)
1.65 markus 1262: options->use_privileged_port = 0;
1.17 markus 1263: if (options->rsa_authentication == -1)
1264: options->rsa_authentication = 1;
1.50 markus 1265: if (options->pubkey_authentication == -1)
1266: options->pubkey_authentication = 1;
1.78 markus 1267: if (options->challenge_response_authentication == -1)
1.83 markus 1268: options->challenge_response_authentication = 1;
1.118 markus 1269: if (options->gss_authentication == -1)
1.122 markus 1270: options->gss_authentication = 0;
1.118 markus 1271: if (options->gss_deleg_creds == -1)
1272: options->gss_deleg_creds = 0;
1.17 markus 1273: if (options->password_authentication == -1)
1274: options->password_authentication = 1;
1.48 markus 1275: if (options->kbd_interactive_authentication == -1)
1.59 markus 1276: options->kbd_interactive_authentication = 1;
1.17 markus 1277: if (options->rhosts_rsa_authentication == -1)
1.99 stevesk 1278: options->rhosts_rsa_authentication = 0;
1.72 markus 1279: if (options->hostbased_authentication == -1)
1280: options->hostbased_authentication = 0;
1.17 markus 1281: if (options->batch_mode == -1)
1282: options->batch_mode = 0;
1283: if (options->check_host_ip == -1)
1284: options->check_host_ip = 1;
1285: if (options->strict_host_key_checking == -1)
1286: options->strict_host_key_checking = 2; /* 2 is default */
1287: if (options->compression == -1)
1288: options->compression = 0;
1.126 markus 1289: if (options->tcp_keep_alive == -1)
1290: options->tcp_keep_alive = 1;
1.17 markus 1291: if (options->compression_level == -1)
1292: options->compression_level = 6;
1293: if (options->port == -1)
1294: options->port = 0; /* Filled in ssh_connect. */
1.114 djm 1295: if (options->address_family == -1)
1296: options->address_family = AF_UNSPEC;
1.17 markus 1297: if (options->connection_attempts == -1)
1.84 markus 1298: options->connection_attempts = 1;
1.17 markus 1299: if (options->number_of_password_prompts == -1)
1300: options->number_of_password_prompts = 3;
1301: /* Selected in ssh_login(). */
1302: if (options->cipher == -1)
1303: options->cipher = SSH_CIPHER_NOT_SET;
1.31 markus 1304: /* options->ciphers, default set in myproposals.h */
1.62 markus 1305: /* options->macs, default set in myproposals.h */
1.189 djm 1306: /* options->kex_algorithms, default set in myproposals.h */
1.76 markus 1307: /* options->hostkeyalgorithms, default set in myproposals.h */
1.25 markus 1308: if (options->protocol == SSH_PROTO_UNKNOWN)
1.178 markus 1309: options->protocol = SSH_PROTO_2;
1.17 markus 1310: if (options->num_identity_files == 0) {
1.50 markus 1311: if (options->protocol & SSH_PROTO_1) {
1.195 dtucker 1312: add_identity_file(options, "~/",
1313: _PATH_SSH_CLIENT_IDENTITY, 0);
1.50 markus 1314: }
1315: if (options->protocol & SSH_PROTO_2) {
1.195 dtucker 1316: add_identity_file(options, "~/",
1317: _PATH_SSH_CLIENT_ID_RSA, 0);
1318: add_identity_file(options, "~/",
1319: _PATH_SSH_CLIENT_ID_DSA, 0);
1320: add_identity_file(options, "~/",
1321: _PATH_SSH_CLIENT_ID_ECDSA, 0);
1.50 markus 1322: }
1.27 markus 1323: }
1.17 markus 1324: if (options->escape_char == -1)
1325: options->escape_char = '~';
1.193 djm 1326: if (options->num_system_hostfiles == 0) {
1327: options->system_hostfiles[options->num_system_hostfiles++] =
1328: xstrdup(_PATH_SSH_SYSTEM_HOSTFILE);
1329: options->system_hostfiles[options->num_system_hostfiles++] =
1330: xstrdup(_PATH_SSH_SYSTEM_HOSTFILE2);
1331: }
1332: if (options->num_user_hostfiles == 0) {
1333: options->user_hostfiles[options->num_user_hostfiles++] =
1334: xstrdup(_PATH_SSH_USER_HOSTFILE);
1335: options->user_hostfiles[options->num_user_hostfiles++] =
1336: xstrdup(_PATH_SSH_USER_HOSTFILE2);
1337: }
1.95 markus 1338: if (options->log_level == SYSLOG_LEVEL_NOT_SET)
1.54 markus 1339: options->log_level = SYSLOG_LEVEL_INFO;
1.90 stevesk 1340: if (options->clear_forwardings == 1)
1341: clear_forwardings(options);
1.91 markus 1342: if (options->no_host_authentication_for_localhost == - 1)
1343: options->no_host_authentication_for_localhost = 0;
1.128 markus 1344: if (options->identities_only == -1)
1345: options->identities_only = 0;
1.101 markus 1346: if (options->enable_ssh_keysign == -1)
1347: options->enable_ssh_keysign = 0;
1.105 markus 1348: if (options->rekey_limit == -1)
1349: options->rekey_limit = 0;
1.198 ! dtucker 1350: if (options->rekey_interval == -1)
! 1351: options->rekey_interval = 0;
1.107 jakob 1352: if (options->verify_host_key_dns == -1)
1353: options->verify_host_key_dns = 0;
1.127 markus 1354: if (options->server_alive_interval == -1)
1355: options->server_alive_interval = 0;
1356: if (options->server_alive_count_max == -1)
1357: options->server_alive_count_max = 3;
1.132 djm 1358: if (options->control_master == -1)
1359: options->control_master = 0;
1.187 djm 1360: if (options->control_persist == -1) {
1361: options->control_persist = 0;
1362: options->control_persist_timeout = 0;
1363: }
1.136 djm 1364: if (options->hash_known_hosts == -1)
1365: options->hash_known_hosts = 0;
1.144 reyk 1366: if (options->tun_open == -1)
1.145 reyk 1367: options->tun_open = SSH_TUNMODE_NO;
1368: if (options->tun_local == -1)
1369: options->tun_local = SSH_TUNID_ANY;
1370: if (options->tun_remote == -1)
1371: options->tun_remote = SSH_TUNID_ANY;
1.144 reyk 1372: if (options->permit_local_command == -1)
1373: options->permit_local_command = 0;
1.177 andreas 1374: if (options->use_roaming == -1)
1375: options->use_roaming = 1;
1.167 grunk 1376: if (options->visual_host_key == -1)
1377: options->visual_host_key = 0;
1.171 djm 1378: if (options->zero_knowledge_password_authentication == -1)
1379: options->zero_knowledge_password_authentication = 0;
1.190 djm 1380: if (options->ip_qos_interactive == -1)
1381: options->ip_qos_interactive = IPTOS_LOWDELAY;
1382: if (options->ip_qos_bulk == -1)
1383: options->ip_qos_bulk = IPTOS_THROUGHPUT;
1.192 djm 1384: if (options->request_tty == -1)
1385: options->request_tty = REQUEST_TTY_AUTO;
1.144 reyk 1386: /* options->local_command should not be set by default */
1.17 markus 1387: /* options->proxy_command should not be set by default */
1388: /* options->user will be set in the main program if appropriate */
1389: /* options->hostname will be set in the main program if appropriate */
1.52 markus 1390: /* options->host_key_alias should not be set by default */
1.67 markus 1391: /* options->preferred_authentications will be set in ssh */
1.135 djm 1392: }
1393:
1394: /*
1395: * parse_forward
1396: * parses a string containing a port forwarding specification of the form:
1.168 stevesk 1397: * dynamicfwd == 0
1.135 djm 1398: * [listenhost:]listenport:connecthost:connectport
1.168 stevesk 1399: * dynamicfwd == 1
1400: * [listenhost:]listenport
1.135 djm 1401: * returns number of arguments parsed or zero on error
1402: */
1403: int
1.176 djm 1404: parse_forward(Forward *fwd, const char *fwdspec, int dynamicfwd, int remotefwd)
1.135 djm 1405: {
1406: int i;
1407: char *p, *cp, *fwdarg[4];
1408:
1409: memset(fwd, '\0', sizeof(*fwd));
1410:
1411: cp = p = xstrdup(fwdspec);
1412:
1413: /* skip leading spaces */
1.162 tedu 1414: while (isspace(*cp))
1.135 djm 1415: cp++;
1416:
1417: for (i = 0; i < 4; ++i)
1418: if ((fwdarg[i] = hpdelim(&cp)) == NULL)
1419: break;
1420:
1.170 stevesk 1421: /* Check for trailing garbage */
1.135 djm 1422: if (cp != NULL)
1423: i = 0; /* failure */
1424:
1425: switch (i) {
1.168 stevesk 1426: case 1:
1427: fwd->listen_host = NULL;
1428: fwd->listen_port = a2port(fwdarg[0]);
1429: fwd->connect_host = xstrdup("socks");
1430: break;
1431:
1432: case 2:
1433: fwd->listen_host = xstrdup(cleanhostname(fwdarg[0]));
1434: fwd->listen_port = a2port(fwdarg[1]);
1435: fwd->connect_host = xstrdup("socks");
1436: break;
1437:
1.135 djm 1438: case 3:
1439: fwd->listen_host = NULL;
1440: fwd->listen_port = a2port(fwdarg[0]);
1441: fwd->connect_host = xstrdup(cleanhostname(fwdarg[1]));
1442: fwd->connect_port = a2port(fwdarg[2]);
1443: break;
1444:
1445: case 4:
1446: fwd->listen_host = xstrdup(cleanhostname(fwdarg[0]));
1447: fwd->listen_port = a2port(fwdarg[1]);
1448: fwd->connect_host = xstrdup(cleanhostname(fwdarg[2]));
1449: fwd->connect_port = a2port(fwdarg[3]);
1450: break;
1451: default:
1452: i = 0; /* failure */
1453: }
1454:
1455: xfree(p);
1456:
1.168 stevesk 1457: if (dynamicfwd) {
1458: if (!(i == 1 || i == 2))
1459: goto fail_free;
1460: } else {
1461: if (!(i == 3 || i == 4))
1462: goto fail_free;
1.175 djm 1463: if (fwd->connect_port <= 0)
1.168 stevesk 1464: goto fail_free;
1465: }
1466:
1.176 djm 1467: if (fwd->listen_port < 0 || (!remotefwd && fwd->listen_port == 0))
1.135 djm 1468: goto fail_free;
1469:
1470: if (fwd->connect_host != NULL &&
1471: strlen(fwd->connect_host) >= NI_MAXHOST)
1472: goto fail_free;
1.176 djm 1473: if (fwd->listen_host != NULL &&
1474: strlen(fwd->listen_host) >= NI_MAXHOST)
1475: goto fail_free;
1476:
1.135 djm 1477:
1478: return (i);
1479:
1480: fail_free:
1.173 djm 1481: if (fwd->connect_host != NULL) {
1.135 djm 1482: xfree(fwd->connect_host);
1.173 djm 1483: fwd->connect_host = NULL;
1484: }
1485: if (fwd->listen_host != NULL) {
1.135 djm 1486: xfree(fwd->listen_host);
1.173 djm 1487: fwd->listen_host = NULL;
1488: }
1.135 djm 1489: return (0);
1.1 deraadt 1490: }