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