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