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