Annotation of src/usr.bin/ssh/readconf.c, Revision 1.130
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.130 ! djm 15: RCSID("$OpenBSD: readconf.c,v 1.129 2004/04/18 23:10:26 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: PasswordAuthentication no
61:
62: Host puukko.hut.fi
63: User t35124p
64: ProxyCommand ssh-proxy %h %p
65:
66: Host *.fr
1.96 markus 67: PublicKeyAuthentication no
1.1 deraadt 68:
69: Host *.su
70: Cipher none
71: PasswordAuthentication no
72:
73: # Defaults for various options
74: Host *
75: ForwardAgent no
1.50 markus 76: ForwardX11 no
1.1 deraadt 77: PasswordAuthentication yes
78: RSAAuthentication yes
79: RhostsRSAAuthentication yes
80: StrictHostKeyChecking yes
1.126 markus 81: TcpKeepAlive no
1.1 deraadt 82: IdentityFile ~/.ssh/identity
83: Port 22
84: EscapeChar ~
85:
86: */
87:
88: /* Keyword tokens. */
89:
1.17 markus 90: typedef enum {
91: oBadOption,
1.123 markus 92: oForwardAgent, oForwardX11, oForwardX11Trusted, oGatewayPorts,
1.100 deraadt 93: oPasswordAuthentication, oRSAAuthentication,
1.59 markus 94: oChallengeResponseAuthentication, oXAuthLocation,
1.17 markus 95: oIdentityFile, oHostName, oPort, oCipher, oRemoteForward, oLocalForward,
96: oUser, oHost, oEscapeChar, oRhostsRSAAuthentication, oProxyCommand,
97: oGlobalKnownHostsFile, oUserKnownHostsFile, oConnectionAttempts,
98: oBatchMode, oCheckHostIP, oStrictHostKeyChecking, oCompression,
1.126 markus 99: oCompressionLevel, oTCPKeepAlive, oNumberOfPasswordPrompts,
1.62 markus 100: oUsePrivilegedPort, oLogLevel, oCiphers, oProtocol, oMacs,
1.50 markus 101: oGlobalKnownHostsFile2, oUserKnownHostsFile2, oPubkeyAuthentication,
1.67 markus 102: oKbdInteractiveAuthentication, oKbdInteractiveDevices, oHostKeyAlias,
1.76 markus 103: oDynamicForward, oPreferredAuthentications, oHostbasedAuthentication,
1.90 stevesk 104: oHostKeyAlgorithms, oBindAddress, oSmartcardDevice,
1.96 markus 105: oClearAllForwardings, oNoHostAuthenticationForLocalhost,
1.111 djm 106: oEnableSSHKeysign, oRekeyLimit, oVerifyHostKeyDNS, oConnectTimeout,
1.118 markus 107: oAddressFamily, oGssAuthentication, oGssDelegateCreds,
1.128 markus 108: oServerAliveInterval, oServerAliveCountMax, oIdentitiesOnly,
1.130 ! djm 109: oSendEnv,
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.123 markus 121: { "forwardx11trusted", oForwardX11Trusted },
1.34 markus 122: { "xauthlocation", oXAuthLocation },
1.17 markus 123: { "gatewayports", oGatewayPorts },
124: { "useprivilegedport", oUsePrivilegedPort },
1.116 markus 125: { "rhostsauthentication", oDeprecated },
1.17 markus 126: { "passwordauthentication", oPasswordAuthentication },
1.48 markus 127: { "kbdinteractiveauthentication", oKbdInteractiveAuthentication },
128: { "kbdinteractivedevices", oKbdInteractiveDevices },
1.17 markus 129: { "rsaauthentication", oRSAAuthentication },
1.50 markus 130: { "pubkeyauthentication", oPubkeyAuthentication },
1.59 markus 131: { "dsaauthentication", oPubkeyAuthentication }, /* alias */
1.72 markus 132: { "rhostsrsaauthentication", oRhostsRSAAuthentication },
1.73 markus 133: { "hostbasedauthentication", oHostbasedAuthentication },
1.59 markus 134: { "challengeresponseauthentication", oChallengeResponseAuthentication },
135: { "skeyauthentication", oChallengeResponseAuthentication }, /* alias */
136: { "tisauthentication", oChallengeResponseAuthentication }, /* alias */
1.110 jakob 137: { "kerberosauthentication", oUnsupported },
138: { "kerberostgtpassing", oUnsupported },
139: { "afstokenpassing", oUnsupported },
1.118 markus 140: #if defined(GSSAPI)
141: { "gssapiauthentication", oGssAuthentication },
142: { "gssapidelegatecredentials", oGssDelegateCreds },
143: #else
144: { "gssapiauthentication", oUnsupported },
145: { "gssapidelegatecredentials", oUnsupported },
146: #endif
1.96 markus 147: { "fallbacktorsh", oDeprecated },
148: { "usersh", oDeprecated },
1.17 markus 149: { "identityfile", oIdentityFile },
1.50 markus 150: { "identityfile2", oIdentityFile }, /* alias */
1.128 markus 151: { "identitiesonly", oIdentitiesOnly },
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 },
1.126 markus 175: { "tcpkeepalive", oTCPKeepAlive },
176: { "keepalive", oTCPKeepAlive }, /* obsolete */
1.17 markus 177: { "numberofpasswordprompts", oNumberOfPasswordPrompts },
178: { "loglevel", oLogLevel },
1.71 markus 179: { "dynamicforward", oDynamicForward },
1.67 markus 180: { "preferredauthentications", oPreferredAuthentications },
1.76 markus 181: { "hostkeyalgorithms", oHostKeyAlgorithms },
1.77 markus 182: { "bindaddress", oBindAddress },
1.110 jakob 183: #ifdef SMARTCARD
1.85 jakob 184: { "smartcarddevice", oSmartcardDevice },
1.110 jakob 185: #else
186: { "smartcarddevice", oUnsupported },
187: #endif
1.93 deraadt 188: { "clearallforwardings", oClearAllForwardings },
1.101 markus 189: { "enablesshkeysign", oEnableSSHKeysign },
1.107 jakob 190: { "verifyhostkeydns", oVerifyHostKeyDNS },
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.127 markus 195: { "serveraliveinterval", oServerAliveInterval },
196: { "serveralivecountmax", oServerAliveCountMax },
1.130 ! djm 197: { "sendenv", oSendEnv },
1.92 stevesk 198: { NULL, oBadOption }
1.13 markus 199: };
200:
1.19 markus 201: /*
202: * Adds a local TCP/IP port forward to options. Never returns if there is an
203: * error.
204: */
1.1 deraadt 205:
1.26 markus 206: void
1.22 markus 207: add_local_forward(Options *options, u_short port, const char *host,
208: u_short host_port)
1.1 deraadt 209: {
1.17 markus 210: Forward *fwd;
211: extern uid_t original_real_uid;
212: if (port < IPPORT_RESERVED && original_real_uid != 0)
1.64 millert 213: fatal("Privileged ports can only be forwarded by root.");
1.17 markus 214: if (options->num_local_forwards >= SSH_MAX_FORWARDS_PER_DIRECTION)
215: fatal("Too many local forwards (max %d).", SSH_MAX_FORWARDS_PER_DIRECTION);
216: fwd = &options->local_forwards[options->num_local_forwards++];
217: fwd->port = port;
218: fwd->host = xstrdup(host);
219: fwd->host_port = host_port;
1.1 deraadt 220: }
221:
1.19 markus 222: /*
223: * Adds a remote TCP/IP port forward to options. Never returns if there is
224: * an error.
225: */
1.1 deraadt 226:
1.26 markus 227: void
1.22 markus 228: add_remote_forward(Options *options, u_short port, const char *host,
229: u_short host_port)
1.1 deraadt 230: {
1.17 markus 231: Forward *fwd;
232: if (options->num_remote_forwards >= SSH_MAX_FORWARDS_PER_DIRECTION)
233: fatal("Too many remote forwards (max %d).",
1.93 deraadt 234: SSH_MAX_FORWARDS_PER_DIRECTION);
1.17 markus 235: fwd = &options->remote_forwards[options->num_remote_forwards++];
236: fwd->port = port;
237: fwd->host = xstrdup(host);
238: fwd->host_port = host_port;
1.1 deraadt 239: }
240:
1.90 stevesk 241: static void
242: clear_forwardings(Options *options)
243: {
244: int i;
245:
246: for (i = 0; i < options->num_local_forwards; i++)
247: xfree(options->local_forwards[i].host);
248: options->num_local_forwards = 0;
249: for (i = 0; i < options->num_remote_forwards; i++)
250: xfree(options->remote_forwards[i].host);
251: options->num_remote_forwards = 0;
252: }
253:
1.19 markus 254: /*
1.70 stevesk 255: * Returns the number of the token pointed to by cp or oBadOption.
1.19 markus 256: */
1.1 deraadt 257:
1.26 markus 258: static OpCodes
1.17 markus 259: parse_token(const char *cp, const char *filename, int linenum)
1.1 deraadt 260: {
1.51 markus 261: u_int i;
1.1 deraadt 262:
1.17 markus 263: for (i = 0; keywords[i].name; i++)
1.20 markus 264: if (strcasecmp(cp, keywords[i].name) == 0)
1.17 markus 265: return keywords[i].opcode;
266:
1.75 stevesk 267: error("%s: line %d: Bad configuration option: %s",
268: filename, linenum, cp);
1.17 markus 269: return oBadOption;
1.1 deraadt 270: }
271:
1.19 markus 272: /*
273: * Processes a single option line as used in the configuration files. This
274: * only sets those values that have not already been set.
275: */
1.102 markus 276: #define WHITESPACE " \t\r\n"
1.1 deraadt 277:
1.14 markus 278: int
279: process_config_line(Options *options, const char *host,
1.17 markus 280: char *line, const char *filename, int linenum,
281: int *activep)
1.1 deraadt 282: {
1.102 markus 283: char buf[256], *s, **charptr, *endofnumber, *keyword, *arg;
1.22 markus 284: int opcode, *intptr, value;
1.102 markus 285: size_t len;
1.22 markus 286: u_short fwd_port, fwd_host_port;
1.88 stevesk 287: char sfwd_host_port[6];
1.106 djm 288:
289: /* Strip trailing whitespace */
290: for(len = strlen(line) - 1; len > 0; len--) {
291: if (strchr(WHITESPACE, line[len]) == NULL)
292: break;
293: line[len] = '\0';
294: }
1.1 deraadt 295:
1.42 provos 296: s = line;
297: /* Get the keyword. (Each line is supposed to begin with a keyword). */
298: keyword = strdelim(&s);
299: /* Ignore leading whitespace. */
300: if (*keyword == '\0')
1.43 markus 301: keyword = strdelim(&s);
1.56 deraadt 302: if (keyword == NULL || !*keyword || *keyword == '\n' || *keyword == '#')
1.17 markus 303: return 0;
304:
1.38 provos 305: opcode = parse_token(keyword, filename, linenum);
1.17 markus 306:
307: switch (opcode) {
308: case oBadOption:
1.19 markus 309: /* don't panic, but count bad options */
310: return -1;
1.17 markus 311: /* NOTREACHED */
1.111 djm 312: case oConnectTimeout:
313: intptr = &options->connection_timeout;
1.127 markus 314: parse_time:
1.111 djm 315: arg = strdelim(&s);
316: if (!arg || *arg == '\0')
317: fatal("%s line %d: missing time value.",
318: filename, linenum);
319: if ((value = convtime(arg)) == -1)
320: fatal("%s line %d: invalid time value.",
321: filename, linenum);
322: if (*intptr == -1)
323: *intptr = value;
324: break;
325:
1.17 markus 326: case oForwardAgent:
327: intptr = &options->forward_agent;
328: parse_flag:
1.42 provos 329: arg = strdelim(&s);
1.40 ho 330: if (!arg || *arg == '\0')
1.17 markus 331: fatal("%.200s line %d: Missing yes/no argument.", filename, linenum);
332: value = 0; /* To avoid compiler warning... */
1.38 provos 333: if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0)
1.17 markus 334: value = 1;
1.38 provos 335: else if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0)
1.17 markus 336: value = 0;
337: else
338: fatal("%.200s line %d: Bad yes/no argument.", filename, linenum);
339: if (*activep && *intptr == -1)
340: *intptr = value;
341: break;
342:
343: case oForwardX11:
344: intptr = &options->forward_x11;
345: goto parse_flag;
346:
1.123 markus 347: case oForwardX11Trusted:
348: intptr = &options->forward_x11_trusted;
349: goto parse_flag;
350:
1.17 markus 351: case oGatewayPorts:
352: intptr = &options->gateway_ports;
353: goto parse_flag;
354:
355: case oUsePrivilegedPort:
356: intptr = &options->use_privileged_port;
357: goto parse_flag;
358:
359: case oPasswordAuthentication:
360: intptr = &options->password_authentication;
361: goto parse_flag;
362:
1.48 markus 363: case oKbdInteractiveAuthentication:
364: intptr = &options->kbd_interactive_authentication;
365: goto parse_flag;
366:
367: case oKbdInteractiveDevices:
368: charptr = &options->kbd_interactive_devices;
369: goto parse_string;
370:
1.50 markus 371: case oPubkeyAuthentication:
372: intptr = &options->pubkey_authentication;
1.30 markus 373: goto parse_flag;
374:
1.17 markus 375: case oRSAAuthentication:
376: intptr = &options->rsa_authentication;
377: goto parse_flag;
378:
379: case oRhostsRSAAuthentication:
380: intptr = &options->rhosts_rsa_authentication;
381: goto parse_flag;
382:
1.72 markus 383: case oHostbasedAuthentication:
384: intptr = &options->hostbased_authentication;
385: goto parse_flag;
386:
1.59 markus 387: case oChallengeResponseAuthentication:
1.78 markus 388: intptr = &options->challenge_response_authentication;
1.17 markus 389: goto parse_flag;
1.108 jakob 390:
1.118 markus 391: case oGssAuthentication:
392: intptr = &options->gss_authentication;
393: goto parse_flag;
394:
395: case oGssDelegateCreds:
396: intptr = &options->gss_deleg_creds;
397: goto parse_flag;
398:
1.17 markus 399: case oBatchMode:
400: intptr = &options->batch_mode;
401: goto parse_flag;
402:
403: case oCheckHostIP:
404: intptr = &options->check_host_ip;
405: goto parse_flag;
406:
1.107 jakob 407: case oVerifyHostKeyDNS:
408: intptr = &options->verify_host_key_dns;
1.125 jakob 409: goto parse_yesnoask;
1.107 jakob 410:
1.17 markus 411: case oStrictHostKeyChecking:
412: intptr = &options->strict_host_key_checking;
1.125 jakob 413: parse_yesnoask:
1.42 provos 414: arg = strdelim(&s);
1.40 ho 415: if (!arg || *arg == '\0')
1.60 stevesk 416: fatal("%.200s line %d: Missing yes/no/ask argument.",
1.93 deraadt 417: filename, linenum);
1.17 markus 418: value = 0; /* To avoid compiler warning... */
1.38 provos 419: if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0)
1.17 markus 420: value = 1;
1.38 provos 421: else if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0)
1.17 markus 422: value = 0;
1.38 provos 423: else if (strcmp(arg, "ask") == 0)
1.17 markus 424: value = 2;
425: else
426: fatal("%.200s line %d: Bad yes/no/ask argument.", filename, linenum);
427: if (*activep && *intptr == -1)
428: *intptr = value;
429: break;
430:
431: case oCompression:
432: intptr = &options->compression;
433: goto parse_flag;
434:
1.126 markus 435: case oTCPKeepAlive:
436: intptr = &options->tcp_keep_alive;
1.17 markus 437: goto parse_flag;
438:
1.91 markus 439: case oNoHostAuthenticationForLocalhost:
440: intptr = &options->no_host_authentication_for_localhost;
441: goto parse_flag;
442:
1.17 markus 443: case oNumberOfPasswordPrompts:
444: intptr = &options->number_of_password_prompts;
445: goto parse_int;
446:
447: case oCompressionLevel:
448: intptr = &options->compression_level;
449: goto parse_int;
450:
1.105 markus 451: case oRekeyLimit:
452: intptr = &options->rekey_limit;
453: arg = strdelim(&s);
454: if (!arg || *arg == '\0')
455: fatal("%.200s line %d: Missing argument.", filename, linenum);
456: if (arg[0] < '0' || arg[0] > '9')
457: fatal("%.200s line %d: Bad number.", filename, linenum);
458: value = strtol(arg, &endofnumber, 10);
459: if (arg == endofnumber)
460: fatal("%.200s line %d: Bad number.", filename, linenum);
461: switch (toupper(*endofnumber)) {
462: case 'K':
463: value *= 1<<10;
464: break;
465: case 'M':
466: value *= 1<<20;
467: break;
468: case 'G':
469: value *= 1<<30;
470: break;
471: }
472: if (*activep && *intptr == -1)
473: *intptr = value;
474: break;
475:
1.17 markus 476: case oIdentityFile:
1.42 provos 477: arg = strdelim(&s);
1.40 ho 478: if (!arg || *arg == '\0')
1.17 markus 479: fatal("%.200s line %d: Missing argument.", filename, linenum);
480: if (*activep) {
1.50 markus 481: intptr = &options->num_identity_files;
1.27 markus 482: if (*intptr >= SSH_MAX_IDENTITY_FILES)
1.17 markus 483: fatal("%.200s line %d: Too many identity files specified (max %d).",
1.93 deraadt 484: filename, linenum, SSH_MAX_IDENTITY_FILES);
1.50 markus 485: charptr = &options->identity_files[*intptr];
1.38 provos 486: *charptr = xstrdup(arg);
1.27 markus 487: *intptr = *intptr + 1;
1.17 markus 488: }
489: break;
490:
1.34 markus 491: case oXAuthLocation:
492: charptr=&options->xauth_location;
493: goto parse_string;
494:
1.17 markus 495: case oUser:
496: charptr = &options->user;
497: parse_string:
1.42 provos 498: arg = strdelim(&s);
1.40 ho 499: if (!arg || *arg == '\0')
1.17 markus 500: fatal("%.200s line %d: Missing argument.", filename, linenum);
501: if (*activep && *charptr == NULL)
1.38 provos 502: *charptr = xstrdup(arg);
1.17 markus 503: break;
504:
505: case oGlobalKnownHostsFile:
506: charptr = &options->system_hostfile;
507: goto parse_string;
508:
509: case oUserKnownHostsFile:
510: charptr = &options->user_hostfile;
511: goto parse_string;
512:
1.27 markus 513: case oGlobalKnownHostsFile2:
514: charptr = &options->system_hostfile2;
515: goto parse_string;
516:
517: case oUserKnownHostsFile2:
518: charptr = &options->user_hostfile2;
519: goto parse_string;
520:
1.17 markus 521: case oHostName:
522: charptr = &options->hostname;
523: goto parse_string;
524:
1.52 markus 525: case oHostKeyAlias:
526: charptr = &options->host_key_alias;
527: goto parse_string;
528:
1.67 markus 529: case oPreferredAuthentications:
530: charptr = &options->preferred_authentications;
531: goto parse_string;
532:
1.77 markus 533: case oBindAddress:
534: charptr = &options->bind_address;
535: goto parse_string;
536:
1.85 jakob 537: case oSmartcardDevice:
1.86 markus 538: charptr = &options->smartcard_device;
539: goto parse_string;
1.85 jakob 540:
1.17 markus 541: case oProxyCommand:
1.113 markus 542: if (s == NULL)
543: fatal("%.200s line %d: Missing argument.", filename, linenum);
1.17 markus 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)
1.117 markus 681: add_local_forward(options, fwd_port, "socks", 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;
1.112 djm 719: break;
720:
721: case oAddressFamily:
722: arg = strdelim(&s);
1.114 djm 723: intptr = &options->address_family;
1.112 djm 724: if (strcasecmp(arg, "inet") == 0)
1.114 djm 725: value = AF_INET;
1.112 djm 726: else if (strcasecmp(arg, "inet6") == 0)
1.114 djm 727: value = AF_INET6;
1.112 djm 728: else if (strcasecmp(arg, "any") == 0)
1.114 djm 729: value = AF_UNSPEC;
1.112 djm 730: else
731: fatal("Unsupported AddressFamily \"%s\"", arg);
1.114 djm 732: if (*activep && *intptr == -1)
733: *intptr = value;
1.17 markus 734: break;
735:
1.101 markus 736: case oEnableSSHKeysign:
737: intptr = &options->enable_ssh_keysign;
738: goto parse_flag;
739:
1.128 markus 740: case oIdentitiesOnly:
741: intptr = &options->identities_only;
742: goto parse_flag;
743:
1.127 markus 744: case oServerAliveInterval:
745: intptr = &options->server_alive_interval;
746: goto parse_time;
747:
748: case oServerAliveCountMax:
749: intptr = &options->server_alive_count_max;
750: goto parse_int;
751:
1.130 ! djm 752: case oSendEnv:
! 753: while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
! 754: if (strchr(arg, '=') != NULL)
! 755: fatal("%s line %d: Invalid environment name.",
! 756: filename, linenum);
! 757: if (options->num_send_env >= MAX_SEND_ENV)
! 758: fatal("%s line %d: too many send env.",
! 759: filename, linenum);
! 760: options->send_env[options->num_send_env++] =
! 761: xstrdup(arg);
! 762: }
! 763: break;
! 764:
1.96 markus 765: case oDeprecated:
1.98 markus 766: debug("%s line %d: Deprecated option \"%s\"",
1.96 markus 767: filename, linenum, keyword);
1.98 markus 768: return 0;
1.96 markus 769:
1.110 jakob 770: case oUnsupported:
771: error("%s line %d: Unsupported option \"%s\"",
772: filename, linenum, keyword);
773: return 0;
774:
1.17 markus 775: default:
776: fatal("process_config_line: Unimplemented opcode %d", opcode);
777: }
778:
779: /* Check that there is no garbage at end of line. */
1.57 djm 780: if ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1.39 ho 781: fatal("%.200s line %d: garbage at end of line; \"%.200s\".",
1.93 deraadt 782: filename, linenum, arg);
1.39 ho 783: }
1.17 markus 784: return 0;
1.1 deraadt 785: }
786:
787:
1.19 markus 788: /*
789: * Reads the config file and modifies the options accordingly. Options
790: * should already be initialized before this call. This never returns if
1.89 stevesk 791: * there is an error. If the file does not exist, this returns 0.
1.19 markus 792: */
1.1 deraadt 793:
1.89 stevesk 794: int
1.129 djm 795: read_config_file(const char *filename, const char *host, Options *options,
796: int checkperm)
1.1 deraadt 797: {
1.17 markus 798: FILE *f;
799: char line[1024];
800: int active, linenum;
801: int bad_options = 0;
802:
803: /* Open the file. */
1.129 djm 804: if ((f = fopen(filename, "r")) == NULL)
1.89 stevesk 805: return 0;
1.129 djm 806:
807: if (checkperm) {
808: struct stat sb;
809:
810: if (fstat(fileno(f), &sb) == -1) {
811: fatal("fstat %s: %s", filename, strerror(errno));
812: fclose(f);
813: return (0);
814: }
815: if (((sb.st_uid != 0 && sb.st_uid != getuid()) ||
816: (sb.st_mode & 022) != 0)) {
817: fatal("Bad owner or permissions on %s", filename);
818: return 0;
819: }
820: }
1.17 markus 821:
822: debug("Reading configuration data %.200s", filename);
823:
1.19 markus 824: /*
825: * Mark that we are now processing the options. This flag is turned
826: * on/off by Host specifications.
827: */
1.17 markus 828: active = 1;
829: linenum = 0;
830: while (fgets(line, sizeof(line), f)) {
831: /* Update line number counter. */
832: linenum++;
833: if (process_config_line(options, host, line, filename, linenum, &active) != 0)
834: bad_options++;
835: }
836: fclose(f);
837: if (bad_options > 0)
1.64 millert 838: fatal("%s: terminating, %d bad configuration options",
1.93 deraadt 839: filename, bad_options);
1.89 stevesk 840: return 1;
1.1 deraadt 841: }
842:
1.19 markus 843: /*
844: * Initializes options to special values that indicate that they have not yet
845: * been set. Read_config_file will only set options with this value. Options
846: * are processed in the following order: command line, user config file,
847: * system config file. Last, fill_default_options is called.
848: */
1.1 deraadt 849:
1.26 markus 850: void
1.17 markus 851: initialize_options(Options * options)
1.1 deraadt 852: {
1.17 markus 853: memset(options, 'X', sizeof(*options));
854: options->forward_agent = -1;
855: options->forward_x11 = -1;
1.123 markus 856: options->forward_x11_trusted = -1;
1.34 markus 857: options->xauth_location = NULL;
1.17 markus 858: options->gateway_ports = -1;
859: options->use_privileged_port = -1;
860: options->rsa_authentication = -1;
1.50 markus 861: options->pubkey_authentication = -1;
1.78 markus 862: options->challenge_response_authentication = -1;
1.118 markus 863: options->gss_authentication = -1;
864: options->gss_deleg_creds = -1;
1.17 markus 865: options->password_authentication = -1;
1.48 markus 866: options->kbd_interactive_authentication = -1;
867: options->kbd_interactive_devices = NULL;
1.17 markus 868: options->rhosts_rsa_authentication = -1;
1.72 markus 869: options->hostbased_authentication = -1;
1.17 markus 870: options->batch_mode = -1;
871: options->check_host_ip = -1;
872: options->strict_host_key_checking = -1;
873: options->compression = -1;
1.126 markus 874: options->tcp_keep_alive = -1;
1.17 markus 875: options->compression_level = -1;
876: options->port = -1;
1.114 djm 877: options->address_family = -1;
1.17 markus 878: options->connection_attempts = -1;
1.111 djm 879: options->connection_timeout = -1;
1.17 markus 880: options->number_of_password_prompts = -1;
881: options->cipher = -1;
1.25 markus 882: options->ciphers = NULL;
1.62 markus 883: options->macs = NULL;
1.76 markus 884: options->hostkeyalgorithms = NULL;
1.25 markus 885: options->protocol = SSH_PROTO_UNKNOWN;
1.17 markus 886: options->num_identity_files = 0;
887: options->hostname = NULL;
1.52 markus 888: options->host_key_alias = NULL;
1.17 markus 889: options->proxy_command = NULL;
890: options->user = NULL;
891: options->escape_char = -1;
892: options->system_hostfile = NULL;
893: options->user_hostfile = NULL;
1.27 markus 894: options->system_hostfile2 = NULL;
895: options->user_hostfile2 = NULL;
1.17 markus 896: options->num_local_forwards = 0;
897: options->num_remote_forwards = 0;
1.90 stevesk 898: options->clear_forwardings = -1;
1.95 markus 899: options->log_level = SYSLOG_LEVEL_NOT_SET;
1.67 markus 900: options->preferred_authentications = NULL;
1.77 markus 901: options->bind_address = NULL;
1.86 markus 902: options->smartcard_device = NULL;
1.101 markus 903: options->enable_ssh_keysign = - 1;
1.91 markus 904: options->no_host_authentication_for_localhost = - 1;
1.128 markus 905: options->identities_only = - 1;
1.105 markus 906: options->rekey_limit = - 1;
1.107 jakob 907: options->verify_host_key_dns = -1;
1.127 markus 908: options->server_alive_interval = -1;
909: options->server_alive_count_max = -1;
1.130 ! djm 910: options->num_send_env = 0;
1.1 deraadt 911: }
912:
1.19 markus 913: /*
914: * Called after processing other sources of option data, this fills those
915: * options for which no value has been specified with their default values.
916: */
1.1 deraadt 917:
1.26 markus 918: void
1.17 markus 919: fill_default_options(Options * options)
1.1 deraadt 920: {
1.61 deraadt 921: int len;
922:
1.17 markus 923: if (options->forward_agent == -1)
1.33 markus 924: options->forward_agent = 0;
1.17 markus 925: if (options->forward_x11 == -1)
1.23 markus 926: options->forward_x11 = 0;
1.123 markus 927: if (options->forward_x11_trusted == -1)
928: options->forward_x11_trusted = 0;
1.34 markus 929: if (options->xauth_location == NULL)
1.80 markus 930: options->xauth_location = _PATH_XAUTH;
1.17 markus 931: if (options->gateway_ports == -1)
932: options->gateway_ports = 0;
933: if (options->use_privileged_port == -1)
1.65 markus 934: options->use_privileged_port = 0;
1.17 markus 935: if (options->rsa_authentication == -1)
936: options->rsa_authentication = 1;
1.50 markus 937: if (options->pubkey_authentication == -1)
938: options->pubkey_authentication = 1;
1.78 markus 939: if (options->challenge_response_authentication == -1)
1.83 markus 940: options->challenge_response_authentication = 1;
1.118 markus 941: if (options->gss_authentication == -1)
1.122 markus 942: options->gss_authentication = 0;
1.118 markus 943: if (options->gss_deleg_creds == -1)
944: options->gss_deleg_creds = 0;
1.17 markus 945: if (options->password_authentication == -1)
946: options->password_authentication = 1;
1.48 markus 947: if (options->kbd_interactive_authentication == -1)
1.59 markus 948: options->kbd_interactive_authentication = 1;
1.17 markus 949: if (options->rhosts_rsa_authentication == -1)
1.99 stevesk 950: options->rhosts_rsa_authentication = 0;
1.72 markus 951: if (options->hostbased_authentication == -1)
952: options->hostbased_authentication = 0;
1.17 markus 953: if (options->batch_mode == -1)
954: options->batch_mode = 0;
955: if (options->check_host_ip == -1)
956: options->check_host_ip = 1;
957: if (options->strict_host_key_checking == -1)
958: options->strict_host_key_checking = 2; /* 2 is default */
959: if (options->compression == -1)
960: options->compression = 0;
1.126 markus 961: if (options->tcp_keep_alive == -1)
962: options->tcp_keep_alive = 1;
1.17 markus 963: if (options->compression_level == -1)
964: options->compression_level = 6;
965: if (options->port == -1)
966: options->port = 0; /* Filled in ssh_connect. */
1.114 djm 967: if (options->address_family == -1)
968: options->address_family = AF_UNSPEC;
1.17 markus 969: if (options->connection_attempts == -1)
1.84 markus 970: options->connection_attempts = 1;
1.17 markus 971: if (options->number_of_password_prompts == -1)
972: options->number_of_password_prompts = 3;
973: /* Selected in ssh_login(). */
974: if (options->cipher == -1)
975: options->cipher = SSH_CIPHER_NOT_SET;
1.31 markus 976: /* options->ciphers, default set in myproposals.h */
1.62 markus 977: /* options->macs, default set in myproposals.h */
1.76 markus 978: /* options->hostkeyalgorithms, default set in myproposals.h */
1.25 markus 979: if (options->protocol == SSH_PROTO_UNKNOWN)
1.69 markus 980: options->protocol = SSH_PROTO_1|SSH_PROTO_2;
1.17 markus 981: if (options->num_identity_files == 0) {
1.50 markus 982: if (options->protocol & SSH_PROTO_1) {
1.61 deraadt 983: len = 2 + strlen(_PATH_SSH_CLIENT_IDENTITY) + 1;
1.50 markus 984: options->identity_files[options->num_identity_files] =
1.61 deraadt 985: xmalloc(len);
986: snprintf(options->identity_files[options->num_identity_files++],
987: len, "~/%.100s", _PATH_SSH_CLIENT_IDENTITY);
1.50 markus 988: }
989: if (options->protocol & SSH_PROTO_2) {
1.63 deraadt 990: len = 2 + strlen(_PATH_SSH_CLIENT_ID_RSA) + 1;
991: options->identity_files[options->num_identity_files] =
992: xmalloc(len);
993: snprintf(options->identity_files[options->num_identity_files++],
994: len, "~/%.100s", _PATH_SSH_CLIENT_ID_RSA);
995:
1.61 deraadt 996: len = 2 + strlen(_PATH_SSH_CLIENT_ID_DSA) + 1;
1.50 markus 997: options->identity_files[options->num_identity_files] =
1.61 deraadt 998: xmalloc(len);
999: snprintf(options->identity_files[options->num_identity_files++],
1000: len, "~/%.100s", _PATH_SSH_CLIENT_ID_DSA);
1.50 markus 1001: }
1.27 markus 1002: }
1.17 markus 1003: if (options->escape_char == -1)
1004: options->escape_char = '~';
1005: if (options->system_hostfile == NULL)
1.55 markus 1006: options->system_hostfile = _PATH_SSH_SYSTEM_HOSTFILE;
1.17 markus 1007: if (options->user_hostfile == NULL)
1.55 markus 1008: options->user_hostfile = _PATH_SSH_USER_HOSTFILE;
1.27 markus 1009: if (options->system_hostfile2 == NULL)
1.55 markus 1010: options->system_hostfile2 = _PATH_SSH_SYSTEM_HOSTFILE2;
1.27 markus 1011: if (options->user_hostfile2 == NULL)
1.55 markus 1012: options->user_hostfile2 = _PATH_SSH_USER_HOSTFILE2;
1.95 markus 1013: if (options->log_level == SYSLOG_LEVEL_NOT_SET)
1.54 markus 1014: options->log_level = SYSLOG_LEVEL_INFO;
1.90 stevesk 1015: if (options->clear_forwardings == 1)
1016: clear_forwardings(options);
1.91 markus 1017: if (options->no_host_authentication_for_localhost == - 1)
1018: options->no_host_authentication_for_localhost = 0;
1.128 markus 1019: if (options->identities_only == -1)
1020: options->identities_only = 0;
1.101 markus 1021: if (options->enable_ssh_keysign == -1)
1022: options->enable_ssh_keysign = 0;
1.105 markus 1023: if (options->rekey_limit == -1)
1024: options->rekey_limit = 0;
1.107 jakob 1025: if (options->verify_host_key_dns == -1)
1026: options->verify_host_key_dns = 0;
1.127 markus 1027: if (options->server_alive_interval == -1)
1028: options->server_alive_interval = 0;
1029: if (options->server_alive_count_max == -1)
1030: options->server_alive_count_max = 3;
1.17 markus 1031: /* options->proxy_command should not be set by default */
1032: /* options->user will be set in the main program if appropriate */
1033: /* options->hostname will be set in the main program if appropriate */
1.52 markus 1034: /* options->host_key_alias should not be set by default */
1.67 markus 1035: /* options->preferred_authentications will be set in ssh */
1.1 deraadt 1036: }