Annotation of src/usr.bin/ssh/readconf.c, Revision 1.119
1.1 deraadt 1: /*
1.18 deraadt 2: * Author: Tatu Ylonen <ylo@cs.hut.fi>
3: * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
4: * All rights reserved
5: * Functions for reading the configuration files.
1.26 markus 6: *
1.46 deraadt 7: * As far as I am concerned, the code I have written for this software
8: * can be used freely for any purpose. Any derived versions of this
9: * software must be clearly marked as such, and if the derived work is
10: * incompatible with the protocol description in the RFC file, it must be
11: * called by a name other than "ssh" or "Secure Shell".
1.18 deraadt 12: */
1.1 deraadt 13:
14: #include "includes.h"
1.119 ! markus 15: RCSID("$OpenBSD: readconf.c,v 1.118 2003/08/22 10:56:09 markus Exp $");
1.1 deraadt 16:
17: #include "ssh.h"
18: #include "xmalloc.h"
1.25 markus 19: #include "compat.h"
1.58 markus 20: #include "cipher.h"
1.55 markus 21: #include "pathnames.h"
1.58 markus 22: #include "log.h"
23: #include "readconf.h"
24: #include "match.h"
25: #include "misc.h"
1.62 markus 26: #include "kex.h"
27: #include "mac.h"
1.1 deraadt 28:
29: /* Format of the configuration file:
30:
31: # Configuration data is parsed as follows:
32: # 1. command line options
33: # 2. user-specific file
34: # 3. system-wide file
35: # Any configuration value is only changed the first time it is set.
36: # Thus, host-specific definitions should be at the beginning of the
37: # configuration file, and defaults at the end.
38:
39: # Host-specific declarations. These may override anything above. A single
40: # host may match multiple declarations; these are processed in the order
41: # that they are given in.
42:
43: Host *.ngs.fi ngs.fi
1.96 markus 44: User foo
1.1 deraadt 45:
46: Host fake.com
47: HostName another.host.name.real.org
48: User blaah
49: Port 34289
50: ForwardX11 no
51: ForwardAgent no
52:
53: Host books.com
54: RemoteForward 9999 shadows.cs.hut.fi:9999
55: Cipher 3des
56:
57: Host fascist.blob.com
58: Port 23123
59: User tylonen
60: PasswordAuthentication no
61:
62: Host puukko.hut.fi
63: User t35124p
64: ProxyCommand ssh-proxy %h %p
65:
66: Host *.fr
1.96 markus 67: PublicKeyAuthentication no
1.1 deraadt 68:
69: Host *.su
70: Cipher none
71: PasswordAuthentication no
72:
73: # Defaults for various options
74: Host *
75: ForwardAgent no
1.50 markus 76: ForwardX11 no
1.1 deraadt 77: PasswordAuthentication yes
78: RSAAuthentication yes
79: RhostsRSAAuthentication yes
80: StrictHostKeyChecking yes
81: KeepAlives no
82: IdentityFile ~/.ssh/identity
83: Port 22
84: EscapeChar ~
85:
86: */
87:
88: /* Keyword tokens. */
89:
1.17 markus 90: typedef enum {
91: oBadOption,
1.116 markus 92: oForwardAgent, oForwardX11, oGatewayPorts,
1.100 deraadt 93: oPasswordAuthentication, oRSAAuthentication,
1.59 markus 94: oChallengeResponseAuthentication, oXAuthLocation,
1.115 markus 95: oKerberosAuthentication, oKerberosTgtPassing,
1.17 markus 96: oIdentityFile, oHostName, oPort, oCipher, oRemoteForward, oLocalForward,
97: oUser, oHost, oEscapeChar, oRhostsRSAAuthentication, oProxyCommand,
98: oGlobalKnownHostsFile, oUserKnownHostsFile, oConnectionAttempts,
99: oBatchMode, oCheckHostIP, oStrictHostKeyChecking, oCompression,
1.59 markus 100: oCompressionLevel, oKeepAlives, oNumberOfPasswordPrompts,
1.62 markus 101: oUsePrivilegedPort, oLogLevel, oCiphers, oProtocol, oMacs,
1.50 markus 102: oGlobalKnownHostsFile2, oUserKnownHostsFile2, oPubkeyAuthentication,
1.67 markus 103: oKbdInteractiveAuthentication, oKbdInteractiveDevices, oHostKeyAlias,
1.76 markus 104: oDynamicForward, oPreferredAuthentications, oHostbasedAuthentication,
1.90 stevesk 105: oHostKeyAlgorithms, oBindAddress, oSmartcardDevice,
1.96 markus 106: oClearAllForwardings, oNoHostAuthenticationForLocalhost,
1.111 djm 107: oEnableSSHKeysign, oRekeyLimit, oVerifyHostKeyDNS, oConnectTimeout,
1.118 markus 108: oAddressFamily, oGssAuthentication, oGssDelegateCreds,
1.110 jakob 109: oDeprecated, oUnsupported
1.1 deraadt 110: } OpCodes;
111:
112: /* Textual representations of the tokens. */
113:
1.17 markus 114: static struct {
115: const char *name;
116: OpCodes opcode;
117: } keywords[] = {
118: { "forwardagent", oForwardAgent },
119: { "forwardx11", oForwardX11 },
1.34 markus 120: { "xauthlocation", oXAuthLocation },
1.17 markus 121: { "gatewayports", oGatewayPorts },
122: { "useprivilegedport", oUsePrivilegedPort },
1.116 markus 123: { "rhostsauthentication", oDeprecated },
1.17 markus 124: { "passwordauthentication", oPasswordAuthentication },
1.48 markus 125: { "kbdinteractiveauthentication", oKbdInteractiveAuthentication },
126: { "kbdinteractivedevices", oKbdInteractiveDevices },
1.17 markus 127: { "rsaauthentication", oRSAAuthentication },
1.50 markus 128: { "pubkeyauthentication", oPubkeyAuthentication },
1.59 markus 129: { "dsaauthentication", oPubkeyAuthentication }, /* alias */
1.72 markus 130: { "rhostsrsaauthentication", oRhostsRSAAuthentication },
1.73 markus 131: { "hostbasedauthentication", oHostbasedAuthentication },
1.59 markus 132: { "challengeresponseauthentication", oChallengeResponseAuthentication },
133: { "skeyauthentication", oChallengeResponseAuthentication }, /* alias */
134: { "tisauthentication", oChallengeResponseAuthentication }, /* alias */
1.110 jakob 135: { "kerberosauthentication", oUnsupported },
136: { "kerberostgtpassing", oUnsupported },
137: { "afstokenpassing", oUnsupported },
1.118 markus 138: #if defined(GSSAPI)
139: { "gssapiauthentication", oGssAuthentication },
140: { "gssapidelegatecreds", oGssDelegateCreds },
141: { "gssapidelegatecredentials", oGssDelegateCreds },
142: #else
143: { "gssapiauthentication", oUnsupported },
144: { "gssapidelegatecredentials", oUnsupported },
145: #endif
1.96 markus 146: { "fallbacktorsh", oDeprecated },
147: { "usersh", oDeprecated },
1.17 markus 148: { "identityfile", oIdentityFile },
1.50 markus 149: { "identityfile2", oIdentityFile }, /* alias */
1.17 markus 150: { "hostname", oHostName },
1.52 markus 151: { "hostkeyalias", oHostKeyAlias },
1.17 markus 152: { "proxycommand", oProxyCommand },
153: { "port", oPort },
154: { "cipher", oCipher },
1.25 markus 155: { "ciphers", oCiphers },
1.62 markus 156: { "macs", oMacs },
1.25 markus 157: { "protocol", oProtocol },
1.17 markus 158: { "remoteforward", oRemoteForward },
159: { "localforward", oLocalForward },
160: { "user", oUser },
161: { "host", oHost },
162: { "escapechar", oEscapeChar },
163: { "globalknownhostsfile", oGlobalKnownHostsFile },
1.81 markus 164: { "userknownhostsfile", oUserKnownHostsFile }, /* obsolete */
1.27 markus 165: { "globalknownhostsfile2", oGlobalKnownHostsFile2 },
1.81 markus 166: { "userknownhostsfile2", oUserKnownHostsFile2 }, /* obsolete */
1.17 markus 167: { "connectionattempts", oConnectionAttempts },
168: { "batchmode", oBatchMode },
169: { "checkhostip", oCheckHostIP },
170: { "stricthostkeychecking", oStrictHostKeyChecking },
171: { "compression", oCompression },
172: { "compressionlevel", oCompressionLevel },
173: { "keepalive", oKeepAlives },
174: { "numberofpasswordprompts", oNumberOfPasswordPrompts },
175: { "loglevel", oLogLevel },
1.71 markus 176: { "dynamicforward", oDynamicForward },
1.67 markus 177: { "preferredauthentications", oPreferredAuthentications },
1.76 markus 178: { "hostkeyalgorithms", oHostKeyAlgorithms },
1.77 markus 179: { "bindaddress", oBindAddress },
1.110 jakob 180: #ifdef SMARTCARD
1.85 jakob 181: { "smartcarddevice", oSmartcardDevice },
1.110 jakob 182: #else
183: { "smartcarddevice", oUnsupported },
184: #endif
1.93 deraadt 185: { "clearallforwardings", oClearAllForwardings },
1.101 markus 186: { "enablesshkeysign", oEnableSSHKeysign },
1.110 jakob 187: #ifdef DNS
1.107 jakob 188: { "verifyhostkeydns", oVerifyHostKeyDNS },
1.110 jakob 189: #else
190: { "verifyhostkeydns", oUnsupported },
191: #endif
1.93 deraadt 192: { "nohostauthenticationforlocalhost", oNoHostAuthenticationForLocalhost },
1.105 markus 193: { "rekeylimit", oRekeyLimit },
1.111 djm 194: { "connecttimeout", oConnectTimeout },
1.112 djm 195: { "addressfamily", oAddressFamily },
1.92 stevesk 196: { NULL, oBadOption }
1.13 markus 197: };
198:
1.19 markus 199: /*
200: * Adds a local TCP/IP port forward to options. Never returns if there is an
201: * error.
202: */
1.1 deraadt 203:
1.26 markus 204: void
1.22 markus 205: add_local_forward(Options *options, u_short port, const char *host,
206: u_short host_port)
1.1 deraadt 207: {
1.17 markus 208: Forward *fwd;
209: extern uid_t original_real_uid;
210: if (port < IPPORT_RESERVED && original_real_uid != 0)
1.64 millert 211: fatal("Privileged ports can only be forwarded by root.");
1.17 markus 212: if (options->num_local_forwards >= SSH_MAX_FORWARDS_PER_DIRECTION)
213: fatal("Too many local forwards (max %d).", SSH_MAX_FORWARDS_PER_DIRECTION);
214: fwd = &options->local_forwards[options->num_local_forwards++];
215: fwd->port = port;
216: fwd->host = xstrdup(host);
217: fwd->host_port = host_port;
1.1 deraadt 218: }
219:
1.19 markus 220: /*
221: * Adds a remote TCP/IP port forward to options. Never returns if there is
222: * an error.
223: */
1.1 deraadt 224:
1.26 markus 225: void
1.22 markus 226: add_remote_forward(Options *options, u_short port, const char *host,
227: u_short host_port)
1.1 deraadt 228: {
1.17 markus 229: Forward *fwd;
230: if (options->num_remote_forwards >= SSH_MAX_FORWARDS_PER_DIRECTION)
231: fatal("Too many remote forwards (max %d).",
1.93 deraadt 232: SSH_MAX_FORWARDS_PER_DIRECTION);
1.17 markus 233: fwd = &options->remote_forwards[options->num_remote_forwards++];
234: fwd->port = port;
235: fwd->host = xstrdup(host);
236: fwd->host_port = host_port;
1.1 deraadt 237: }
238:
1.90 stevesk 239: static void
240: clear_forwardings(Options *options)
241: {
242: int i;
243:
244: for (i = 0; i < options->num_local_forwards; i++)
245: xfree(options->local_forwards[i].host);
246: options->num_local_forwards = 0;
247: for (i = 0; i < options->num_remote_forwards; i++)
248: xfree(options->remote_forwards[i].host);
249: options->num_remote_forwards = 0;
250: }
251:
1.19 markus 252: /*
1.70 stevesk 253: * Returns the number of the token pointed to by cp or oBadOption.
1.19 markus 254: */
1.1 deraadt 255:
1.26 markus 256: static OpCodes
1.17 markus 257: parse_token(const char *cp, const char *filename, int linenum)
1.1 deraadt 258: {
1.51 markus 259: u_int i;
1.1 deraadt 260:
1.17 markus 261: for (i = 0; keywords[i].name; i++)
1.20 markus 262: if (strcasecmp(cp, keywords[i].name) == 0)
1.17 markus 263: return keywords[i].opcode;
264:
1.75 stevesk 265: error("%s: line %d: Bad configuration option: %s",
266: filename, linenum, cp);
1.17 markus 267: return oBadOption;
1.1 deraadt 268: }
269:
1.19 markus 270: /*
271: * Processes a single option line as used in the configuration files. This
272: * only sets those values that have not already been set.
273: */
1.102 markus 274: #define WHITESPACE " \t\r\n"
1.1 deraadt 275:
1.14 markus 276: int
277: process_config_line(Options *options, const char *host,
1.17 markus 278: char *line, const char *filename, int linenum,
279: int *activep)
1.1 deraadt 280: {
1.102 markus 281: char buf[256], *s, **charptr, *endofnumber, *keyword, *arg;
1.22 markus 282: int opcode, *intptr, value;
1.102 markus 283: size_t len;
1.22 markus 284: u_short fwd_port, fwd_host_port;
1.88 stevesk 285: char sfwd_host_port[6];
1.106 djm 286:
287: /* Strip trailing whitespace */
288: for(len = strlen(line) - 1; len > 0; len--) {
289: if (strchr(WHITESPACE, line[len]) == NULL)
290: break;
291: line[len] = '\0';
292: }
1.1 deraadt 293:
1.42 provos 294: s = line;
295: /* Get the keyword. (Each line is supposed to begin with a keyword). */
296: keyword = strdelim(&s);
297: /* Ignore leading whitespace. */
298: if (*keyword == '\0')
1.43 markus 299: keyword = strdelim(&s);
1.56 deraadt 300: if (keyword == NULL || !*keyword || *keyword == '\n' || *keyword == '#')
1.17 markus 301: return 0;
302:
1.38 provos 303: opcode = parse_token(keyword, filename, linenum);
1.17 markus 304:
305: switch (opcode) {
306: case oBadOption:
1.19 markus 307: /* don't panic, but count bad options */
308: return -1;
1.17 markus 309: /* NOTREACHED */
1.111 djm 310: case oConnectTimeout:
311: intptr = &options->connection_timeout;
312: /* parse_time: */
313: arg = strdelim(&s);
314: if (!arg || *arg == '\0')
315: fatal("%s line %d: missing time value.",
316: filename, linenum);
317: if ((value = convtime(arg)) == -1)
318: fatal("%s line %d: invalid time value.",
319: filename, linenum);
320: if (*intptr == -1)
321: *intptr = value;
322: break;
323:
1.17 markus 324: case oForwardAgent:
325: intptr = &options->forward_agent;
326: parse_flag:
1.42 provos 327: arg = strdelim(&s);
1.40 ho 328: if (!arg || *arg == '\0')
1.17 markus 329: fatal("%.200s line %d: Missing yes/no argument.", filename, linenum);
330: value = 0; /* To avoid compiler warning... */
1.38 provos 331: if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0)
1.17 markus 332: value = 1;
1.38 provos 333: else if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0)
1.17 markus 334: value = 0;
335: else
336: fatal("%.200s line %d: Bad yes/no argument.", filename, linenum);
337: if (*activep && *intptr == -1)
338: *intptr = value;
339: break;
340:
341: case oForwardX11:
342: intptr = &options->forward_x11;
343: goto parse_flag;
344:
345: case oGatewayPorts:
346: intptr = &options->gateway_ports;
347: goto parse_flag;
348:
349: case oUsePrivilegedPort:
350: intptr = &options->use_privileged_port;
351: goto parse_flag;
352:
353: case oPasswordAuthentication:
354: intptr = &options->password_authentication;
355: goto parse_flag;
356:
1.48 markus 357: case oKbdInteractiveAuthentication:
358: intptr = &options->kbd_interactive_authentication;
359: goto parse_flag;
360:
361: case oKbdInteractiveDevices:
362: charptr = &options->kbd_interactive_devices;
363: goto parse_string;
364:
1.50 markus 365: case oPubkeyAuthentication:
366: intptr = &options->pubkey_authentication;
1.30 markus 367: goto parse_flag;
368:
1.17 markus 369: case oRSAAuthentication:
370: intptr = &options->rsa_authentication;
371: goto parse_flag;
372:
373: case oRhostsRSAAuthentication:
374: intptr = &options->rhosts_rsa_authentication;
375: goto parse_flag;
376:
1.72 markus 377: case oHostbasedAuthentication:
378: intptr = &options->hostbased_authentication;
379: goto parse_flag;
380:
1.59 markus 381: case oChallengeResponseAuthentication:
1.78 markus 382: intptr = &options->challenge_response_authentication;
1.17 markus 383: goto parse_flag;
1.108 jakob 384:
1.17 markus 385: case oKerberosAuthentication:
386: intptr = &options->kerberos_authentication;
387: goto parse_flag;
1.108 jakob 388:
1.17 markus 389: case oKerberosTgtPassing:
390: intptr = &options->kerberos_tgt_passing;
391: goto parse_flag;
1.108 jakob 392:
1.118 markus 393: case oGssAuthentication:
394: intptr = &options->gss_authentication;
395: goto parse_flag;
396:
397: case oGssDelegateCreds:
398: intptr = &options->gss_deleg_creds;
399: goto parse_flag;
400:
1.17 markus 401: case oBatchMode:
402: intptr = &options->batch_mode;
403: goto parse_flag;
404:
405: case oCheckHostIP:
406: intptr = &options->check_host_ip;
407: goto parse_flag;
408:
1.107 jakob 409: case oVerifyHostKeyDNS:
410: intptr = &options->verify_host_key_dns;
411: goto parse_flag;
412:
1.17 markus 413: case oStrictHostKeyChecking:
414: intptr = &options->strict_host_key_checking;
1.42 provos 415: arg = strdelim(&s);
1.40 ho 416: if (!arg || *arg == '\0')
1.60 stevesk 417: fatal("%.200s line %d: Missing yes/no/ask argument.",
1.93 deraadt 418: filename, linenum);
1.17 markus 419: value = 0; /* To avoid compiler warning... */
1.38 provos 420: if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0)
1.17 markus 421: value = 1;
1.38 provos 422: else if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0)
1.17 markus 423: value = 0;
1.38 provos 424: else if (strcmp(arg, "ask") == 0)
1.17 markus 425: value = 2;
426: else
427: fatal("%.200s line %d: Bad yes/no/ask argument.", filename, linenum);
428: if (*activep && *intptr == -1)
429: *intptr = value;
430: break;
431:
432: case oCompression:
433: intptr = &options->compression;
434: goto parse_flag;
435:
436: case oKeepAlives:
437: intptr = &options->keepalives;
438: goto parse_flag;
439:
1.91 markus 440: case oNoHostAuthenticationForLocalhost:
441: intptr = &options->no_host_authentication_for_localhost;
442: goto parse_flag;
443:
1.17 markus 444: case oNumberOfPasswordPrompts:
445: intptr = &options->number_of_password_prompts;
446: goto parse_int;
447:
448: case oCompressionLevel:
449: intptr = &options->compression_level;
450: goto parse_int;
451:
1.105 markus 452: case oRekeyLimit:
453: intptr = &options->rekey_limit;
454: arg = strdelim(&s);
455: if (!arg || *arg == '\0')
456: fatal("%.200s line %d: Missing argument.", filename, linenum);
457: if (arg[0] < '0' || arg[0] > '9')
458: fatal("%.200s line %d: Bad number.", filename, linenum);
459: value = strtol(arg, &endofnumber, 10);
460: if (arg == endofnumber)
461: fatal("%.200s line %d: Bad number.", filename, linenum);
462: switch (toupper(*endofnumber)) {
463: case 'K':
464: value *= 1<<10;
465: break;
466: case 'M':
467: value *= 1<<20;
468: break;
469: case 'G':
470: value *= 1<<30;
471: break;
472: }
473: if (*activep && *intptr == -1)
474: *intptr = value;
475: break;
476:
1.17 markus 477: case oIdentityFile:
1.42 provos 478: arg = strdelim(&s);
1.40 ho 479: if (!arg || *arg == '\0')
1.17 markus 480: fatal("%.200s line %d: Missing argument.", filename, linenum);
481: if (*activep) {
1.50 markus 482: intptr = &options->num_identity_files;
1.27 markus 483: if (*intptr >= SSH_MAX_IDENTITY_FILES)
1.17 markus 484: fatal("%.200s line %d: Too many identity files specified (max %d).",
1.93 deraadt 485: filename, linenum, SSH_MAX_IDENTITY_FILES);
1.50 markus 486: charptr = &options->identity_files[*intptr];
1.38 provos 487: *charptr = xstrdup(arg);
1.27 markus 488: *intptr = *intptr + 1;
1.17 markus 489: }
490: break;
491:
1.34 markus 492: case oXAuthLocation:
493: charptr=&options->xauth_location;
494: goto parse_string;
495:
1.17 markus 496: case oUser:
497: charptr = &options->user;
498: parse_string:
1.42 provos 499: arg = strdelim(&s);
1.40 ho 500: if (!arg || *arg == '\0')
1.17 markus 501: fatal("%.200s line %d: Missing argument.", filename, linenum);
502: if (*activep && *charptr == NULL)
1.38 provos 503: *charptr = xstrdup(arg);
1.17 markus 504: break;
505:
506: case oGlobalKnownHostsFile:
507: charptr = &options->system_hostfile;
508: goto parse_string;
509:
510: case oUserKnownHostsFile:
511: charptr = &options->user_hostfile;
512: goto parse_string;
513:
1.27 markus 514: case oGlobalKnownHostsFile2:
515: charptr = &options->system_hostfile2;
516: goto parse_string;
517:
518: case oUserKnownHostsFile2:
519: charptr = &options->user_hostfile2;
520: goto parse_string;
521:
1.17 markus 522: case oHostName:
523: charptr = &options->hostname;
524: goto parse_string;
525:
1.52 markus 526: case oHostKeyAlias:
527: charptr = &options->host_key_alias;
528: goto parse_string;
529:
1.67 markus 530: case oPreferredAuthentications:
531: charptr = &options->preferred_authentications;
532: goto parse_string;
533:
1.77 markus 534: case oBindAddress:
535: charptr = &options->bind_address;
536: goto parse_string;
537:
1.85 jakob 538: case oSmartcardDevice:
1.86 markus 539: charptr = &options->smartcard_device;
540: goto parse_string;
1.85 jakob 541:
1.17 markus 542: case oProxyCommand:
1.113 markus 543: if (s == NULL)
544: fatal("%.200s line %d: Missing argument.", filename, linenum);
1.17 markus 545: charptr = &options->proxy_command;
1.102 markus 546: len = strspn(s, WHITESPACE "=");
1.17 markus 547: if (*activep && *charptr == NULL)
1.102 markus 548: *charptr = xstrdup(s + len);
1.17 markus 549: return 0;
550:
551: case oPort:
552: intptr = &options->port;
553: parse_int:
1.42 provos 554: arg = strdelim(&s);
1.40 ho 555: if (!arg || *arg == '\0')
1.17 markus 556: fatal("%.200s line %d: Missing argument.", filename, linenum);
1.38 provos 557: if (arg[0] < '0' || arg[0] > '9')
1.17 markus 558: fatal("%.200s line %d: Bad number.", filename, linenum);
1.21 markus 559:
560: /* Octal, decimal, or hex format? */
1.38 provos 561: value = strtol(arg, &endofnumber, 0);
562: if (arg == endofnumber)
1.21 markus 563: fatal("%.200s line %d: Bad number.", filename, linenum);
1.17 markus 564: if (*activep && *intptr == -1)
565: *intptr = value;
566: break;
567:
568: case oConnectionAttempts:
569: intptr = &options->connection_attempts;
570: goto parse_int;
571:
572: case oCipher:
573: intptr = &options->cipher;
1.42 provos 574: arg = strdelim(&s);
1.40 ho 575: if (!arg || *arg == '\0')
1.32 markus 576: fatal("%.200s line %d: Missing argument.", filename, linenum);
1.38 provos 577: value = cipher_number(arg);
1.17 markus 578: if (value == -1)
579: fatal("%.200s line %d: Bad cipher '%s'.",
1.93 deraadt 580: filename, linenum, arg ? arg : "<NONE>");
1.17 markus 581: if (*activep && *intptr == -1)
582: *intptr = value;
583: break;
584:
1.25 markus 585: case oCiphers:
1.42 provos 586: arg = strdelim(&s);
1.40 ho 587: if (!arg || *arg == '\0')
1.32 markus 588: fatal("%.200s line %d: Missing argument.", filename, linenum);
1.38 provos 589: if (!ciphers_valid(arg))
1.31 markus 590: fatal("%.200s line %d: Bad SSH2 cipher spec '%s'.",
1.93 deraadt 591: filename, linenum, arg ? arg : "<NONE>");
1.25 markus 592: if (*activep && options->ciphers == NULL)
1.38 provos 593: options->ciphers = xstrdup(arg);
1.25 markus 594: break;
595:
1.62 markus 596: case oMacs:
597: arg = strdelim(&s);
598: if (!arg || *arg == '\0')
599: fatal("%.200s line %d: Missing argument.", filename, linenum);
600: if (!mac_valid(arg))
601: fatal("%.200s line %d: Bad SSH2 Mac spec '%s'.",
1.93 deraadt 602: filename, linenum, arg ? arg : "<NONE>");
1.62 markus 603: if (*activep && options->macs == NULL)
604: options->macs = xstrdup(arg);
605: break;
606:
1.76 markus 607: case oHostKeyAlgorithms:
608: arg = strdelim(&s);
609: if (!arg || *arg == '\0')
610: fatal("%.200s line %d: Missing argument.", filename, linenum);
611: if (!key_names_valid2(arg))
612: fatal("%.200s line %d: Bad protocol 2 host key algorithms '%s'.",
1.93 deraadt 613: filename, linenum, arg ? arg : "<NONE>");
1.76 markus 614: if (*activep && options->hostkeyalgorithms == NULL)
615: options->hostkeyalgorithms = xstrdup(arg);
616: break;
617:
1.25 markus 618: case oProtocol:
619: intptr = &options->protocol;
1.42 provos 620: arg = strdelim(&s);
1.40 ho 621: if (!arg || *arg == '\0')
1.32 markus 622: fatal("%.200s line %d: Missing argument.", filename, linenum);
1.38 provos 623: value = proto_spec(arg);
1.25 markus 624: if (value == SSH_PROTO_UNKNOWN)
625: fatal("%.200s line %d: Bad protocol spec '%s'.",
1.93 deraadt 626: filename, linenum, arg ? arg : "<NONE>");
1.25 markus 627: if (*activep && *intptr == SSH_PROTO_UNKNOWN)
628: *intptr = value;
629: break;
630:
1.17 markus 631: case oLogLevel:
632: intptr = (int *) &options->log_level;
1.42 provos 633: arg = strdelim(&s);
1.38 provos 634: value = log_level_number(arg);
1.95 markus 635: if (value == SYSLOG_LEVEL_NOT_SET)
1.64 millert 636: fatal("%.200s line %d: unsupported log level '%s'",
1.93 deraadt 637: filename, linenum, arg ? arg : "<NONE>");
1.95 markus 638: if (*activep && (LogLevel) *intptr == SYSLOG_LEVEL_NOT_SET)
1.17 markus 639: *intptr = (LogLevel) value;
640: break;
641:
1.88 stevesk 642: case oLocalForward:
1.17 markus 643: case oRemoteForward:
1.42 provos 644: arg = strdelim(&s);
1.40 ho 645: if (!arg || *arg == '\0')
1.88 stevesk 646: fatal("%.200s line %d: Missing port argument.",
647: filename, linenum);
648: if ((fwd_port = a2port(arg)) == 0)
649: fatal("%.200s line %d: Bad listen port.",
650: filename, linenum);
1.42 provos 651: arg = strdelim(&s);
1.40 ho 652: if (!arg || *arg == '\0')
1.17 markus 653: fatal("%.200s line %d: Missing second argument.",
1.88 stevesk 654: filename, linenum);
655: if (sscanf(arg, "%255[^:]:%5[0-9]", buf, sfwd_host_port) != 2 &&
656: sscanf(arg, "%255[^/]/%5[0-9]", buf, sfwd_host_port) != 2)
657: fatal("%.200s line %d: Bad forwarding specification.",
658: filename, linenum);
659: if ((fwd_host_port = a2port(sfwd_host_port)) == 0)
660: fatal("%.200s line %d: Bad forwarding port.",
661: filename, linenum);
662: if (*activep) {
663: if (opcode == oLocalForward)
664: add_local_forward(options, fwd_port, buf,
665: fwd_host_port);
666: else if (opcode == oRemoteForward)
667: add_remote_forward(options, fwd_port, buf,
668: fwd_host_port);
669: }
1.17 markus 670: break;
1.71 markus 671:
672: case oDynamicForward:
673: arg = strdelim(&s);
674: if (!arg || *arg == '\0')
675: fatal("%.200s line %d: Missing port argument.",
676: filename, linenum);
1.74 stevesk 677: fwd_port = a2port(arg);
678: if (fwd_port == 0)
1.71 markus 679: fatal("%.200s line %d: Badly formatted port number.",
680: filename, linenum);
1.87 markus 681: if (*activep)
1.117 markus 682: add_local_forward(options, fwd_port, "socks", 0);
1.72 markus 683: break;
1.17 markus 684:
1.90 stevesk 685: case oClearAllForwardings:
686: intptr = &options->clear_forwardings;
687: goto parse_flag;
688:
1.17 markus 689: case oHost:
690: *activep = 0;
1.42 provos 691: while ((arg = strdelim(&s)) != NULL && *arg != '\0')
1.38 provos 692: if (match_pattern(host, arg)) {
693: debug("Applying options for %.100s", arg);
1.17 markus 694: *activep = 1;
695: break;
696: }
1.42 provos 697: /* Avoid garbage check below, as strdelim is done. */
1.17 markus 698: return 0;
699:
700: case oEscapeChar:
701: intptr = &options->escape_char;
1.42 provos 702: arg = strdelim(&s);
1.40 ho 703: if (!arg || *arg == '\0')
1.17 markus 704: fatal("%.200s line %d: Missing argument.", filename, linenum);
1.38 provos 705: if (arg[0] == '^' && arg[2] == 0 &&
1.51 markus 706: (u_char) arg[1] >= 64 && (u_char) arg[1] < 128)
707: value = (u_char) arg[1] & 31;
1.38 provos 708: else if (strlen(arg) == 1)
1.51 markus 709: value = (u_char) arg[0];
1.38 provos 710: else if (strcmp(arg, "none") == 0)
1.79 stevesk 711: value = SSH_ESCAPECHAR_NONE;
1.17 markus 712: else {
713: fatal("%.200s line %d: Bad escape character.",
1.93 deraadt 714: filename, linenum);
1.17 markus 715: /* NOTREACHED */
716: value = 0; /* Avoid compiler warning. */
717: }
718: if (*activep && *intptr == -1)
719: *intptr = value;
1.112 djm 720: break;
721:
722: case oAddressFamily:
723: arg = strdelim(&s);
1.114 djm 724: intptr = &options->address_family;
1.112 djm 725: if (strcasecmp(arg, "inet") == 0)
1.114 djm 726: value = AF_INET;
1.112 djm 727: else if (strcasecmp(arg, "inet6") == 0)
1.114 djm 728: value = AF_INET6;
1.112 djm 729: else if (strcasecmp(arg, "any") == 0)
1.114 djm 730: value = AF_UNSPEC;
1.112 djm 731: else
732: fatal("Unsupported AddressFamily \"%s\"", arg);
1.114 djm 733: if (*activep && *intptr == -1)
734: *intptr = value;
1.17 markus 735: break;
736:
1.101 markus 737: case oEnableSSHKeysign:
738: intptr = &options->enable_ssh_keysign;
739: goto parse_flag;
740:
1.96 markus 741: case oDeprecated:
1.98 markus 742: debug("%s line %d: Deprecated option \"%s\"",
1.96 markus 743: filename, linenum, keyword);
1.98 markus 744: return 0;
1.96 markus 745:
1.110 jakob 746: case oUnsupported:
747: error("%s line %d: Unsupported option \"%s\"",
748: filename, linenum, keyword);
749: return 0;
750:
1.17 markus 751: default:
752: fatal("process_config_line: Unimplemented opcode %d", opcode);
753: }
754:
755: /* Check that there is no garbage at end of line. */
1.57 djm 756: if ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1.39 ho 757: fatal("%.200s line %d: garbage at end of line; \"%.200s\".",
1.93 deraadt 758: filename, linenum, arg);
1.39 ho 759: }
1.17 markus 760: return 0;
1.1 deraadt 761: }
762:
763:
1.19 markus 764: /*
765: * Reads the config file and modifies the options accordingly. Options
766: * should already be initialized before this call. This never returns if
1.89 stevesk 767: * there is an error. If the file does not exist, this returns 0.
1.19 markus 768: */
1.1 deraadt 769:
1.89 stevesk 770: int
1.17 markus 771: read_config_file(const char *filename, const char *host, Options *options)
1.1 deraadt 772: {
1.17 markus 773: FILE *f;
774: char line[1024];
775: int active, linenum;
776: int bad_options = 0;
777:
778: /* Open the file. */
779: f = fopen(filename, "r");
780: if (!f)
1.89 stevesk 781: return 0;
1.17 markus 782:
783: debug("Reading configuration data %.200s", filename);
784:
1.19 markus 785: /*
786: * Mark that we are now processing the options. This flag is turned
787: * on/off by Host specifications.
788: */
1.17 markus 789: active = 1;
790: linenum = 0;
791: while (fgets(line, sizeof(line), f)) {
792: /* Update line number counter. */
793: linenum++;
794: if (process_config_line(options, host, line, filename, linenum, &active) != 0)
795: bad_options++;
796: }
797: fclose(f);
798: if (bad_options > 0)
1.64 millert 799: fatal("%s: terminating, %d bad configuration options",
1.93 deraadt 800: filename, bad_options);
1.89 stevesk 801: return 1;
1.1 deraadt 802: }
803:
1.19 markus 804: /*
805: * Initializes options to special values that indicate that they have not yet
806: * been set. Read_config_file will only set options with this value. Options
807: * are processed in the following order: command line, user config file,
808: * system config file. Last, fill_default_options is called.
809: */
1.1 deraadt 810:
1.26 markus 811: void
1.17 markus 812: initialize_options(Options * options)
1.1 deraadt 813: {
1.17 markus 814: memset(options, 'X', sizeof(*options));
815: options->forward_agent = -1;
816: options->forward_x11 = -1;
1.34 markus 817: options->xauth_location = NULL;
1.17 markus 818: options->gateway_ports = -1;
819: options->use_privileged_port = -1;
820: options->rsa_authentication = -1;
1.50 markus 821: options->pubkey_authentication = -1;
1.78 markus 822: options->challenge_response_authentication = -1;
1.17 markus 823: options->kerberos_authentication = -1;
1.82 dugsong 824: options->kerberos_tgt_passing = -1;
1.118 markus 825: options->gss_authentication = -1;
826: options->gss_deleg_creds = -1;
1.17 markus 827: options->password_authentication = -1;
1.48 markus 828: options->kbd_interactive_authentication = -1;
829: options->kbd_interactive_devices = NULL;
1.17 markus 830: options->rhosts_rsa_authentication = -1;
1.72 markus 831: options->hostbased_authentication = -1;
1.17 markus 832: options->batch_mode = -1;
833: options->check_host_ip = -1;
834: options->strict_host_key_checking = -1;
835: options->compression = -1;
836: options->keepalives = -1;
837: options->compression_level = -1;
838: options->port = -1;
1.114 djm 839: options->address_family = -1;
1.17 markus 840: options->connection_attempts = -1;
1.111 djm 841: options->connection_timeout = -1;
1.17 markus 842: options->number_of_password_prompts = -1;
843: options->cipher = -1;
1.25 markus 844: options->ciphers = NULL;
1.62 markus 845: options->macs = NULL;
1.76 markus 846: options->hostkeyalgorithms = NULL;
1.25 markus 847: options->protocol = SSH_PROTO_UNKNOWN;
1.17 markus 848: options->num_identity_files = 0;
849: options->hostname = NULL;
1.52 markus 850: options->host_key_alias = NULL;
1.17 markus 851: options->proxy_command = NULL;
852: options->user = NULL;
853: options->escape_char = -1;
854: options->system_hostfile = NULL;
855: options->user_hostfile = NULL;
1.27 markus 856: options->system_hostfile2 = NULL;
857: options->user_hostfile2 = NULL;
1.17 markus 858: options->num_local_forwards = 0;
859: options->num_remote_forwards = 0;
1.90 stevesk 860: options->clear_forwardings = -1;
1.95 markus 861: options->log_level = SYSLOG_LEVEL_NOT_SET;
1.67 markus 862: options->preferred_authentications = NULL;
1.77 markus 863: options->bind_address = NULL;
1.86 markus 864: options->smartcard_device = NULL;
1.101 markus 865: options->enable_ssh_keysign = - 1;
1.91 markus 866: options->no_host_authentication_for_localhost = - 1;
1.105 markus 867: options->rekey_limit = - 1;
1.107 jakob 868: options->verify_host_key_dns = -1;
1.1 deraadt 869: }
870:
1.19 markus 871: /*
872: * Called after processing other sources of option data, this fills those
873: * options for which no value has been specified with their default values.
874: */
1.1 deraadt 875:
1.26 markus 876: void
1.17 markus 877: fill_default_options(Options * options)
1.1 deraadt 878: {
1.61 deraadt 879: int len;
880:
1.17 markus 881: if (options->forward_agent == -1)
1.33 markus 882: options->forward_agent = 0;
1.17 markus 883: if (options->forward_x11 == -1)
1.23 markus 884: options->forward_x11 = 0;
1.34 markus 885: if (options->xauth_location == NULL)
1.80 markus 886: options->xauth_location = _PATH_XAUTH;
1.17 markus 887: if (options->gateway_ports == -1)
888: options->gateway_ports = 0;
889: if (options->use_privileged_port == -1)
1.65 markus 890: options->use_privileged_port = 0;
1.17 markus 891: if (options->rsa_authentication == -1)
892: options->rsa_authentication = 1;
1.50 markus 893: if (options->pubkey_authentication == -1)
894: options->pubkey_authentication = 1;
1.78 markus 895: if (options->challenge_response_authentication == -1)
1.83 markus 896: options->challenge_response_authentication = 1;
1.17 markus 897: if (options->kerberos_authentication == -1)
1.45 provos 898: options->kerberos_authentication = 1;
1.17 markus 899: if (options->kerberos_tgt_passing == -1)
900: options->kerberos_tgt_passing = 1;
1.118 markus 901: if (options->gss_authentication == -1)
902: options->gss_authentication = 1;
903: if (options->gss_deleg_creds == -1)
904: options->gss_deleg_creds = 0;
1.17 markus 905: if (options->password_authentication == -1)
906: options->password_authentication = 1;
1.48 markus 907: if (options->kbd_interactive_authentication == -1)
1.59 markus 908: options->kbd_interactive_authentication = 1;
1.17 markus 909: if (options->rhosts_rsa_authentication == -1)
1.99 stevesk 910: options->rhosts_rsa_authentication = 0;
1.72 markus 911: if (options->hostbased_authentication == -1)
912: options->hostbased_authentication = 0;
1.17 markus 913: if (options->batch_mode == -1)
914: options->batch_mode = 0;
915: if (options->check_host_ip == -1)
916: options->check_host_ip = 1;
917: if (options->strict_host_key_checking == -1)
918: options->strict_host_key_checking = 2; /* 2 is default */
919: if (options->compression == -1)
920: options->compression = 0;
921: if (options->keepalives == -1)
922: options->keepalives = 1;
923: if (options->compression_level == -1)
924: options->compression_level = 6;
925: if (options->port == -1)
926: options->port = 0; /* Filled in ssh_connect. */
1.114 djm 927: if (options->address_family == -1)
928: options->address_family = AF_UNSPEC;
1.17 markus 929: if (options->connection_attempts == -1)
1.84 markus 930: options->connection_attempts = 1;
1.17 markus 931: if (options->number_of_password_prompts == -1)
932: options->number_of_password_prompts = 3;
933: /* Selected in ssh_login(). */
934: if (options->cipher == -1)
935: options->cipher = SSH_CIPHER_NOT_SET;
1.31 markus 936: /* options->ciphers, default set in myproposals.h */
1.62 markus 937: /* options->macs, default set in myproposals.h */
1.76 markus 938: /* options->hostkeyalgorithms, default set in myproposals.h */
1.25 markus 939: if (options->protocol == SSH_PROTO_UNKNOWN)
1.69 markus 940: options->protocol = SSH_PROTO_1|SSH_PROTO_2;
1.17 markus 941: if (options->num_identity_files == 0) {
1.50 markus 942: if (options->protocol & SSH_PROTO_1) {
1.61 deraadt 943: len = 2 + strlen(_PATH_SSH_CLIENT_IDENTITY) + 1;
1.50 markus 944: options->identity_files[options->num_identity_files] =
1.61 deraadt 945: xmalloc(len);
946: snprintf(options->identity_files[options->num_identity_files++],
947: len, "~/%.100s", _PATH_SSH_CLIENT_IDENTITY);
1.50 markus 948: }
949: if (options->protocol & SSH_PROTO_2) {
1.63 deraadt 950: len = 2 + strlen(_PATH_SSH_CLIENT_ID_RSA) + 1;
951: options->identity_files[options->num_identity_files] =
952: xmalloc(len);
953: snprintf(options->identity_files[options->num_identity_files++],
954: len, "~/%.100s", _PATH_SSH_CLIENT_ID_RSA);
955:
1.61 deraadt 956: len = 2 + strlen(_PATH_SSH_CLIENT_ID_DSA) + 1;
1.50 markus 957: options->identity_files[options->num_identity_files] =
1.61 deraadt 958: xmalloc(len);
959: snprintf(options->identity_files[options->num_identity_files++],
960: len, "~/%.100s", _PATH_SSH_CLIENT_ID_DSA);
1.50 markus 961: }
1.27 markus 962: }
1.17 markus 963: if (options->escape_char == -1)
964: options->escape_char = '~';
965: if (options->system_hostfile == NULL)
1.55 markus 966: options->system_hostfile = _PATH_SSH_SYSTEM_HOSTFILE;
1.17 markus 967: if (options->user_hostfile == NULL)
1.55 markus 968: options->user_hostfile = _PATH_SSH_USER_HOSTFILE;
1.27 markus 969: if (options->system_hostfile2 == NULL)
1.55 markus 970: options->system_hostfile2 = _PATH_SSH_SYSTEM_HOSTFILE2;
1.27 markus 971: if (options->user_hostfile2 == NULL)
1.55 markus 972: options->user_hostfile2 = _PATH_SSH_USER_HOSTFILE2;
1.95 markus 973: if (options->log_level == SYSLOG_LEVEL_NOT_SET)
1.54 markus 974: options->log_level = SYSLOG_LEVEL_INFO;
1.90 stevesk 975: if (options->clear_forwardings == 1)
976: clear_forwardings(options);
1.91 markus 977: if (options->no_host_authentication_for_localhost == - 1)
978: options->no_host_authentication_for_localhost = 0;
1.101 markus 979: if (options->enable_ssh_keysign == -1)
980: options->enable_ssh_keysign = 0;
1.105 markus 981: if (options->rekey_limit == -1)
982: options->rekey_limit = 0;
1.107 jakob 983: if (options->verify_host_key_dns == -1)
984: options->verify_host_key_dns = 0;
1.17 markus 985: /* options->proxy_command should not be set by default */
986: /* options->user will be set in the main program if appropriate */
987: /* options->hostname will be set in the main program if appropriate */
1.52 markus 988: /* options->host_key_alias should not be set by default */
1.67 markus 989: /* options->preferred_authentications will be set in ssh */
1.1 deraadt 990: }