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