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