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