Annotation of src/usr.bin/ssh/readconf.c, Revision 1.101
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.101 ! markus 15: RCSID("$OpenBSD: readconf.c,v 1.100 2002/06/19 00:27:55 deraadt 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.1 deraadt 269:
1.14 markus 270: int
271: process_config_line(Options *options, const char *host,
1.17 markus 272: char *line, const char *filename, int linenum,
273: int *activep)
1.1 deraadt 274: {
1.38 provos 275: char buf[256], *s, *string, **charptr, *endofnumber, *keyword, *arg;
1.22 markus 276: int opcode, *intptr, value;
277: u_short fwd_port, fwd_host_port;
1.88 stevesk 278: char sfwd_host_port[6];
1.1 deraadt 279:
1.42 provos 280: s = line;
281: /* Get the keyword. (Each line is supposed to begin with a keyword). */
282: keyword = strdelim(&s);
283: /* Ignore leading whitespace. */
284: if (*keyword == '\0')
1.43 markus 285: keyword = strdelim(&s);
1.56 deraadt 286: if (keyword == NULL || !*keyword || *keyword == '\n' || *keyword == '#')
1.17 markus 287: return 0;
288:
1.38 provos 289: opcode = parse_token(keyword, filename, linenum);
1.17 markus 290:
291: switch (opcode) {
292: case oBadOption:
1.19 markus 293: /* don't panic, but count bad options */
294: return -1;
1.17 markus 295: /* NOTREACHED */
296: case oForwardAgent:
297: intptr = &options->forward_agent;
298: parse_flag:
1.42 provos 299: arg = strdelim(&s);
1.40 ho 300: if (!arg || *arg == '\0')
1.17 markus 301: fatal("%.200s line %d: Missing yes/no argument.", filename, linenum);
302: value = 0; /* To avoid compiler warning... */
1.38 provos 303: if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0)
1.17 markus 304: value = 1;
1.38 provos 305: else if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0)
1.17 markus 306: value = 0;
307: else
308: fatal("%.200s line %d: Bad yes/no argument.", filename, linenum);
309: if (*activep && *intptr == -1)
310: *intptr = value;
311: break;
312:
313: case oForwardX11:
314: intptr = &options->forward_x11;
315: goto parse_flag;
316:
317: case oGatewayPorts:
318: intptr = &options->gateway_ports;
319: goto parse_flag;
320:
321: case oUsePrivilegedPort:
322: intptr = &options->use_privileged_port;
323: goto parse_flag;
324:
325: case oRhostsAuthentication:
326: intptr = &options->rhosts_authentication;
327: goto parse_flag;
328:
329: case oPasswordAuthentication:
330: intptr = &options->password_authentication;
331: goto parse_flag;
332:
1.48 markus 333: case oKbdInteractiveAuthentication:
334: intptr = &options->kbd_interactive_authentication;
335: goto parse_flag;
336:
337: case oKbdInteractiveDevices:
338: charptr = &options->kbd_interactive_devices;
339: goto parse_string;
340:
1.50 markus 341: case oPubkeyAuthentication:
342: intptr = &options->pubkey_authentication;
1.30 markus 343: goto parse_flag;
344:
1.17 markus 345: case oRSAAuthentication:
346: intptr = &options->rsa_authentication;
347: goto parse_flag;
348:
349: case oRhostsRSAAuthentication:
350: intptr = &options->rhosts_rsa_authentication;
351: goto parse_flag;
352:
1.72 markus 353: case oHostbasedAuthentication:
354: intptr = &options->hostbased_authentication;
355: goto parse_flag;
356:
1.59 markus 357: case oChallengeResponseAuthentication:
1.78 markus 358: intptr = &options->challenge_response_authentication;
1.17 markus 359: goto parse_flag;
1.82 dugsong 360: #if defined(KRB4) || defined(KRB5)
1.17 markus 361: case oKerberosAuthentication:
362: intptr = &options->kerberos_authentication;
363: goto parse_flag;
1.82 dugsong 364: #endif
365: #if defined(AFS) || defined(KRB5)
1.17 markus 366: case oKerberosTgtPassing:
367: intptr = &options->kerberos_tgt_passing;
368: goto parse_flag;
1.82 dugsong 369: #endif
370: #ifdef AFS
1.17 markus 371: case oAFSTokenPassing:
372: intptr = &options->afs_token_passing;
373: goto parse_flag;
1.1 deraadt 374: #endif
1.17 markus 375: case oBatchMode:
376: intptr = &options->batch_mode;
377: goto parse_flag;
378:
379: case oCheckHostIP:
380: intptr = &options->check_host_ip;
381: goto parse_flag;
382:
383: case oStrictHostKeyChecking:
384: intptr = &options->strict_host_key_checking;
1.42 provos 385: arg = strdelim(&s);
1.40 ho 386: if (!arg || *arg == '\0')
1.60 stevesk 387: fatal("%.200s line %d: Missing yes/no/ask argument.",
1.93 deraadt 388: filename, linenum);
1.17 markus 389: value = 0; /* To avoid compiler warning... */
1.38 provos 390: if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0)
1.17 markus 391: value = 1;
1.38 provos 392: else if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0)
1.17 markus 393: value = 0;
1.38 provos 394: else if (strcmp(arg, "ask") == 0)
1.17 markus 395: value = 2;
396: else
397: fatal("%.200s line %d: Bad yes/no/ask argument.", filename, linenum);
398: if (*activep && *intptr == -1)
399: *intptr = value;
400: break;
401:
402: case oCompression:
403: intptr = &options->compression;
404: goto parse_flag;
405:
406: case oKeepAlives:
407: intptr = &options->keepalives;
408: goto parse_flag;
409:
1.91 markus 410: case oNoHostAuthenticationForLocalhost:
411: intptr = &options->no_host_authentication_for_localhost;
412: goto parse_flag;
413:
1.17 markus 414: case oNumberOfPasswordPrompts:
415: intptr = &options->number_of_password_prompts;
416: goto parse_int;
417:
418: case oCompressionLevel:
419: intptr = &options->compression_level;
420: goto parse_int;
421:
422: case oIdentityFile:
1.42 provos 423: arg = strdelim(&s);
1.40 ho 424: if (!arg || *arg == '\0')
1.17 markus 425: fatal("%.200s line %d: Missing argument.", filename, linenum);
426: if (*activep) {
1.50 markus 427: intptr = &options->num_identity_files;
1.27 markus 428: if (*intptr >= SSH_MAX_IDENTITY_FILES)
1.17 markus 429: fatal("%.200s line %d: Too many identity files specified (max %d).",
1.93 deraadt 430: filename, linenum, SSH_MAX_IDENTITY_FILES);
1.50 markus 431: charptr = &options->identity_files[*intptr];
1.38 provos 432: *charptr = xstrdup(arg);
1.27 markus 433: *intptr = *intptr + 1;
1.17 markus 434: }
435: break;
436:
1.34 markus 437: case oXAuthLocation:
438: charptr=&options->xauth_location;
439: goto parse_string;
440:
1.17 markus 441: case oUser:
442: charptr = &options->user;
443: parse_string:
1.42 provos 444: arg = strdelim(&s);
1.40 ho 445: if (!arg || *arg == '\0')
1.17 markus 446: fatal("%.200s line %d: Missing argument.", filename, linenum);
447: if (*activep && *charptr == NULL)
1.38 provos 448: *charptr = xstrdup(arg);
1.17 markus 449: break;
450:
451: case oGlobalKnownHostsFile:
452: charptr = &options->system_hostfile;
453: goto parse_string;
454:
455: case oUserKnownHostsFile:
456: charptr = &options->user_hostfile;
457: goto parse_string;
458:
1.27 markus 459: case oGlobalKnownHostsFile2:
460: charptr = &options->system_hostfile2;
461: goto parse_string;
462:
463: case oUserKnownHostsFile2:
464: charptr = &options->user_hostfile2;
465: goto parse_string;
466:
1.17 markus 467: case oHostName:
468: charptr = &options->hostname;
469: goto parse_string;
470:
1.52 markus 471: case oHostKeyAlias:
472: charptr = &options->host_key_alias;
473: goto parse_string;
474:
1.67 markus 475: case oPreferredAuthentications:
476: charptr = &options->preferred_authentications;
477: goto parse_string;
478:
1.77 markus 479: case oBindAddress:
480: charptr = &options->bind_address;
481: goto parse_string;
482:
1.85 jakob 483: case oSmartcardDevice:
1.86 markus 484: charptr = &options->smartcard_device;
485: goto parse_string;
1.85 jakob 486:
1.17 markus 487: case oProxyCommand:
488: charptr = &options->proxy_command;
489: string = xstrdup("");
1.42 provos 490: while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1.38 provos 491: string = xrealloc(string, strlen(string) + strlen(arg) + 2);
1.17 markus 492: strcat(string, " ");
1.38 provos 493: strcat(string, arg);
1.17 markus 494: }
495: if (*activep && *charptr == NULL)
496: *charptr = string;
497: else
498: xfree(string);
499: return 0;
500:
501: case oPort:
502: intptr = &options->port;
503: parse_int:
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);
1.38 provos 507: if (arg[0] < '0' || arg[0] > '9')
1.17 markus 508: fatal("%.200s line %d: Bad number.", filename, linenum);
1.21 markus 509:
510: /* Octal, decimal, or hex format? */
1.38 provos 511: value = strtol(arg, &endofnumber, 0);
512: if (arg == endofnumber)
1.21 markus 513: fatal("%.200s line %d: Bad number.", filename, linenum);
1.17 markus 514: if (*activep && *intptr == -1)
515: *intptr = value;
516: break;
517:
518: case oConnectionAttempts:
519: intptr = &options->connection_attempts;
520: goto parse_int;
521:
522: case oCipher:
523: intptr = &options->cipher;
1.42 provos 524: arg = strdelim(&s);
1.40 ho 525: if (!arg || *arg == '\0')
1.32 markus 526: fatal("%.200s line %d: Missing argument.", filename, linenum);
1.38 provos 527: value = cipher_number(arg);
1.17 markus 528: if (value == -1)
529: fatal("%.200s line %d: Bad cipher '%s'.",
1.93 deraadt 530: filename, linenum, arg ? arg : "<NONE>");
1.17 markus 531: if (*activep && *intptr == -1)
532: *intptr = value;
533: break;
534:
1.25 markus 535: case oCiphers:
1.42 provos 536: arg = strdelim(&s);
1.40 ho 537: if (!arg || *arg == '\0')
1.32 markus 538: fatal("%.200s line %d: Missing argument.", filename, linenum);
1.38 provos 539: if (!ciphers_valid(arg))
1.31 markus 540: fatal("%.200s line %d: Bad SSH2 cipher spec '%s'.",
1.93 deraadt 541: filename, linenum, arg ? arg : "<NONE>");
1.25 markus 542: if (*activep && options->ciphers == NULL)
1.38 provos 543: options->ciphers = xstrdup(arg);
1.25 markus 544: break;
545:
1.62 markus 546: case oMacs:
547: arg = strdelim(&s);
548: if (!arg || *arg == '\0')
549: fatal("%.200s line %d: Missing argument.", filename, linenum);
550: if (!mac_valid(arg))
551: fatal("%.200s line %d: Bad SSH2 Mac spec '%s'.",
1.93 deraadt 552: filename, linenum, arg ? arg : "<NONE>");
1.62 markus 553: if (*activep && options->macs == NULL)
554: options->macs = xstrdup(arg);
555: break;
556:
1.76 markus 557: case oHostKeyAlgorithms:
558: arg = strdelim(&s);
559: if (!arg || *arg == '\0')
560: fatal("%.200s line %d: Missing argument.", filename, linenum);
561: if (!key_names_valid2(arg))
562: fatal("%.200s line %d: Bad protocol 2 host key algorithms '%s'.",
1.93 deraadt 563: filename, linenum, arg ? arg : "<NONE>");
1.76 markus 564: if (*activep && options->hostkeyalgorithms == NULL)
565: options->hostkeyalgorithms = xstrdup(arg);
566: break;
567:
1.25 markus 568: case oProtocol:
569: intptr = &options->protocol;
1.42 provos 570: arg = strdelim(&s);
1.40 ho 571: if (!arg || *arg == '\0')
1.32 markus 572: fatal("%.200s line %d: Missing argument.", filename, linenum);
1.38 provos 573: value = proto_spec(arg);
1.25 markus 574: if (value == SSH_PROTO_UNKNOWN)
575: fatal("%.200s line %d: Bad protocol spec '%s'.",
1.93 deraadt 576: filename, linenum, arg ? arg : "<NONE>");
1.25 markus 577: if (*activep && *intptr == SSH_PROTO_UNKNOWN)
578: *intptr = value;
579: break;
580:
1.17 markus 581: case oLogLevel:
582: intptr = (int *) &options->log_level;
1.42 provos 583: arg = strdelim(&s);
1.38 provos 584: value = log_level_number(arg);
1.95 markus 585: if (value == SYSLOG_LEVEL_NOT_SET)
1.64 millert 586: fatal("%.200s line %d: unsupported log level '%s'",
1.93 deraadt 587: filename, linenum, arg ? arg : "<NONE>");
1.95 markus 588: if (*activep && (LogLevel) *intptr == SYSLOG_LEVEL_NOT_SET)
1.17 markus 589: *intptr = (LogLevel) value;
590: break;
591:
1.88 stevesk 592: case oLocalForward:
1.17 markus 593: case oRemoteForward:
1.42 provos 594: arg = strdelim(&s);
1.40 ho 595: if (!arg || *arg == '\0')
1.88 stevesk 596: fatal("%.200s line %d: Missing port argument.",
597: filename, linenum);
598: if ((fwd_port = a2port(arg)) == 0)
599: fatal("%.200s line %d: Bad listen port.",
600: filename, linenum);
1.42 provos 601: arg = strdelim(&s);
1.40 ho 602: if (!arg || *arg == '\0')
1.17 markus 603: fatal("%.200s line %d: Missing second argument.",
1.88 stevesk 604: filename, linenum);
605: if (sscanf(arg, "%255[^:]:%5[0-9]", buf, sfwd_host_port) != 2 &&
606: sscanf(arg, "%255[^/]/%5[0-9]", buf, sfwd_host_port) != 2)
607: fatal("%.200s line %d: Bad forwarding specification.",
608: filename, linenum);
609: if ((fwd_host_port = a2port(sfwd_host_port)) == 0)
610: fatal("%.200s line %d: Bad forwarding port.",
611: filename, linenum);
612: if (*activep) {
613: if (opcode == oLocalForward)
614: add_local_forward(options, fwd_port, buf,
615: fwd_host_port);
616: else if (opcode == oRemoteForward)
617: add_remote_forward(options, fwd_port, buf,
618: fwd_host_port);
619: }
1.17 markus 620: break;
1.71 markus 621:
622: case oDynamicForward:
623: arg = strdelim(&s);
624: if (!arg || *arg == '\0')
625: fatal("%.200s line %d: Missing port argument.",
626: filename, linenum);
1.74 stevesk 627: fwd_port = a2port(arg);
628: if (fwd_port == 0)
1.71 markus 629: fatal("%.200s line %d: Badly formatted port number.",
630: filename, linenum);
1.87 markus 631: if (*activep)
632: add_local_forward(options, fwd_port, "socks4", 0);
1.72 markus 633: break;
1.17 markus 634:
1.90 stevesk 635: case oClearAllForwardings:
636: intptr = &options->clear_forwardings;
637: goto parse_flag;
638:
1.17 markus 639: case oHost:
640: *activep = 0;
1.42 provos 641: while ((arg = strdelim(&s)) != NULL && *arg != '\0')
1.38 provos 642: if (match_pattern(host, arg)) {
643: debug("Applying options for %.100s", arg);
1.17 markus 644: *activep = 1;
645: break;
646: }
1.42 provos 647: /* Avoid garbage check below, as strdelim is done. */
1.17 markus 648: return 0;
649:
650: case oEscapeChar:
651: intptr = &options->escape_char;
1.42 provos 652: arg = strdelim(&s);
1.40 ho 653: if (!arg || *arg == '\0')
1.17 markus 654: fatal("%.200s line %d: Missing argument.", filename, linenum);
1.38 provos 655: if (arg[0] == '^' && arg[2] == 0 &&
1.51 markus 656: (u_char) arg[1] >= 64 && (u_char) arg[1] < 128)
657: value = (u_char) arg[1] & 31;
1.38 provos 658: else if (strlen(arg) == 1)
1.51 markus 659: value = (u_char) arg[0];
1.38 provos 660: else if (strcmp(arg, "none") == 0)
1.79 stevesk 661: value = SSH_ESCAPECHAR_NONE;
1.17 markus 662: else {
663: fatal("%.200s line %d: Bad escape character.",
1.93 deraadt 664: filename, linenum);
1.17 markus 665: /* NOTREACHED */
666: value = 0; /* Avoid compiler warning. */
667: }
668: if (*activep && *intptr == -1)
669: *intptr = value;
670: break;
671:
1.101 ! markus 672: case oEnableSSHKeysign:
! 673: intptr = &options->enable_ssh_keysign;
! 674: goto parse_flag;
! 675:
1.96 markus 676: case oDeprecated:
1.98 markus 677: debug("%s line %d: Deprecated option \"%s\"",
1.96 markus 678: filename, linenum, keyword);
1.98 markus 679: return 0;
1.96 markus 680:
1.17 markus 681: default:
682: fatal("process_config_line: Unimplemented opcode %d", opcode);
683: }
684:
685: /* Check that there is no garbage at end of line. */
1.57 djm 686: if ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1.39 ho 687: fatal("%.200s line %d: garbage at end of line; \"%.200s\".",
1.93 deraadt 688: filename, linenum, arg);
1.39 ho 689: }
1.17 markus 690: return 0;
1.1 deraadt 691: }
692:
693:
1.19 markus 694: /*
695: * Reads the config file and modifies the options accordingly. Options
696: * should already be initialized before this call. This never returns if
1.89 stevesk 697: * there is an error. If the file does not exist, this returns 0.
1.19 markus 698: */
1.1 deraadt 699:
1.89 stevesk 700: int
1.17 markus 701: read_config_file(const char *filename, const char *host, Options *options)
1.1 deraadt 702: {
1.17 markus 703: FILE *f;
704: char line[1024];
705: int active, linenum;
706: int bad_options = 0;
707:
708: /* Open the file. */
709: f = fopen(filename, "r");
710: if (!f)
1.89 stevesk 711: return 0;
1.17 markus 712:
713: debug("Reading configuration data %.200s", filename);
714:
1.19 markus 715: /*
716: * Mark that we are now processing the options. This flag is turned
717: * on/off by Host specifications.
718: */
1.17 markus 719: active = 1;
720: linenum = 0;
721: while (fgets(line, sizeof(line), f)) {
722: /* Update line number counter. */
723: linenum++;
724: if (process_config_line(options, host, line, filename, linenum, &active) != 0)
725: bad_options++;
726: }
727: fclose(f);
728: if (bad_options > 0)
1.64 millert 729: fatal("%s: terminating, %d bad configuration options",
1.93 deraadt 730: filename, bad_options);
1.89 stevesk 731: return 1;
1.1 deraadt 732: }
733:
1.19 markus 734: /*
735: * Initializes options to special values that indicate that they have not yet
736: * been set. Read_config_file will only set options with this value. Options
737: * are processed in the following order: command line, user config file,
738: * system config file. Last, fill_default_options is called.
739: */
1.1 deraadt 740:
1.26 markus 741: void
1.17 markus 742: initialize_options(Options * options)
1.1 deraadt 743: {
1.17 markus 744: memset(options, 'X', sizeof(*options));
745: options->forward_agent = -1;
746: options->forward_x11 = -1;
1.34 markus 747: options->xauth_location = NULL;
1.17 markus 748: options->gateway_ports = -1;
749: options->use_privileged_port = -1;
750: options->rhosts_authentication = -1;
751: options->rsa_authentication = -1;
1.50 markus 752: options->pubkey_authentication = -1;
1.78 markus 753: options->challenge_response_authentication = -1;
1.82 dugsong 754: #if defined(KRB4) || defined(KRB5)
1.17 markus 755: options->kerberos_authentication = -1;
1.1 deraadt 756: #endif
1.82 dugsong 757: #if defined(AFS) || defined(KRB5)
758: options->kerberos_tgt_passing = -1;
759: #endif
1.5 dugsong 760: #ifdef AFS
1.17 markus 761: options->afs_token_passing = -1;
1.1 deraadt 762: #endif
1.17 markus 763: options->password_authentication = -1;
1.48 markus 764: options->kbd_interactive_authentication = -1;
765: options->kbd_interactive_devices = NULL;
1.17 markus 766: options->rhosts_rsa_authentication = -1;
1.72 markus 767: options->hostbased_authentication = -1;
1.17 markus 768: options->batch_mode = -1;
769: options->check_host_ip = -1;
770: options->strict_host_key_checking = -1;
771: options->compression = -1;
772: options->keepalives = -1;
773: options->compression_level = -1;
774: options->port = -1;
775: options->connection_attempts = -1;
776: options->number_of_password_prompts = -1;
777: options->cipher = -1;
1.25 markus 778: options->ciphers = NULL;
1.62 markus 779: options->macs = NULL;
1.76 markus 780: options->hostkeyalgorithms = NULL;
1.25 markus 781: options->protocol = SSH_PROTO_UNKNOWN;
1.17 markus 782: options->num_identity_files = 0;
783: options->hostname = NULL;
1.52 markus 784: options->host_key_alias = NULL;
1.17 markus 785: options->proxy_command = NULL;
786: options->user = NULL;
787: options->escape_char = -1;
788: options->system_hostfile = NULL;
789: options->user_hostfile = NULL;
1.27 markus 790: options->system_hostfile2 = NULL;
791: options->user_hostfile2 = NULL;
1.17 markus 792: options->num_local_forwards = 0;
793: options->num_remote_forwards = 0;
1.90 stevesk 794: options->clear_forwardings = -1;
1.95 markus 795: options->log_level = SYSLOG_LEVEL_NOT_SET;
1.67 markus 796: options->preferred_authentications = NULL;
1.77 markus 797: options->bind_address = NULL;
1.86 markus 798: options->smartcard_device = NULL;
1.101 ! markus 799: options->enable_ssh_keysign = - 1;
1.91 markus 800: options->no_host_authentication_for_localhost = - 1;
1.1 deraadt 801: }
802:
1.19 markus 803: /*
804: * Called after processing other sources of option data, this fills those
805: * options for which no value has been specified with their default values.
806: */
1.1 deraadt 807:
1.26 markus 808: void
1.17 markus 809: fill_default_options(Options * options)
1.1 deraadt 810: {
1.61 deraadt 811: int len;
812:
1.17 markus 813: if (options->forward_agent == -1)
1.33 markus 814: options->forward_agent = 0;
1.17 markus 815: if (options->forward_x11 == -1)
1.23 markus 816: options->forward_x11 = 0;
1.34 markus 817: if (options->xauth_location == NULL)
1.80 markus 818: options->xauth_location = _PATH_XAUTH;
1.17 markus 819: if (options->gateway_ports == -1)
820: options->gateway_ports = 0;
821: if (options->use_privileged_port == -1)
1.65 markus 822: options->use_privileged_port = 0;
1.17 markus 823: if (options->rhosts_authentication == -1)
1.99 stevesk 824: options->rhosts_authentication = 0;
1.17 markus 825: if (options->rsa_authentication == -1)
826: options->rsa_authentication = 1;
1.50 markus 827: if (options->pubkey_authentication == -1)
828: options->pubkey_authentication = 1;
1.78 markus 829: if (options->challenge_response_authentication == -1)
1.83 markus 830: options->challenge_response_authentication = 1;
1.82 dugsong 831: #if defined(KRB4) || defined(KRB5)
1.17 markus 832: if (options->kerberos_authentication == -1)
1.45 provos 833: options->kerberos_authentication = 1;
1.82 dugsong 834: #endif
835: #if defined(AFS) || defined(KRB5)
1.17 markus 836: if (options->kerberos_tgt_passing == -1)
837: options->kerberos_tgt_passing = 1;
1.82 dugsong 838: #endif
839: #ifdef AFS
1.17 markus 840: if (options->afs_token_passing == -1)
841: options->afs_token_passing = 1;
1.82 dugsong 842: #endif
1.17 markus 843: if (options->password_authentication == -1)
844: options->password_authentication = 1;
1.48 markus 845: if (options->kbd_interactive_authentication == -1)
1.59 markus 846: options->kbd_interactive_authentication = 1;
1.17 markus 847: if (options->rhosts_rsa_authentication == -1)
1.99 stevesk 848: options->rhosts_rsa_authentication = 0;
1.72 markus 849: if (options->hostbased_authentication == -1)
850: options->hostbased_authentication = 0;
1.17 markus 851: if (options->batch_mode == -1)
852: options->batch_mode = 0;
853: if (options->check_host_ip == -1)
854: options->check_host_ip = 1;
855: if (options->strict_host_key_checking == -1)
856: options->strict_host_key_checking = 2; /* 2 is default */
857: if (options->compression == -1)
858: options->compression = 0;
859: if (options->keepalives == -1)
860: options->keepalives = 1;
861: if (options->compression_level == -1)
862: options->compression_level = 6;
863: if (options->port == -1)
864: options->port = 0; /* Filled in ssh_connect. */
865: if (options->connection_attempts == -1)
1.84 markus 866: options->connection_attempts = 1;
1.17 markus 867: if (options->number_of_password_prompts == -1)
868: options->number_of_password_prompts = 3;
869: /* Selected in ssh_login(). */
870: if (options->cipher == -1)
871: options->cipher = SSH_CIPHER_NOT_SET;
1.31 markus 872: /* options->ciphers, default set in myproposals.h */
1.62 markus 873: /* options->macs, default set in myproposals.h */
1.76 markus 874: /* options->hostkeyalgorithms, default set in myproposals.h */
1.25 markus 875: if (options->protocol == SSH_PROTO_UNKNOWN)
1.69 markus 876: options->protocol = SSH_PROTO_1|SSH_PROTO_2;
1.17 markus 877: if (options->num_identity_files == 0) {
1.50 markus 878: if (options->protocol & SSH_PROTO_1) {
1.61 deraadt 879: len = 2 + strlen(_PATH_SSH_CLIENT_IDENTITY) + 1;
1.50 markus 880: options->identity_files[options->num_identity_files] =
1.61 deraadt 881: xmalloc(len);
882: snprintf(options->identity_files[options->num_identity_files++],
883: len, "~/%.100s", _PATH_SSH_CLIENT_IDENTITY);
1.50 markus 884: }
885: if (options->protocol & SSH_PROTO_2) {
1.63 deraadt 886: len = 2 + strlen(_PATH_SSH_CLIENT_ID_RSA) + 1;
887: options->identity_files[options->num_identity_files] =
888: xmalloc(len);
889: snprintf(options->identity_files[options->num_identity_files++],
890: len, "~/%.100s", _PATH_SSH_CLIENT_ID_RSA);
891:
1.61 deraadt 892: len = 2 + strlen(_PATH_SSH_CLIENT_ID_DSA) + 1;
1.50 markus 893: options->identity_files[options->num_identity_files] =
1.61 deraadt 894: xmalloc(len);
895: snprintf(options->identity_files[options->num_identity_files++],
896: len, "~/%.100s", _PATH_SSH_CLIENT_ID_DSA);
1.50 markus 897: }
1.27 markus 898: }
1.17 markus 899: if (options->escape_char == -1)
900: options->escape_char = '~';
901: if (options->system_hostfile == NULL)
1.55 markus 902: options->system_hostfile = _PATH_SSH_SYSTEM_HOSTFILE;
1.17 markus 903: if (options->user_hostfile == NULL)
1.55 markus 904: options->user_hostfile = _PATH_SSH_USER_HOSTFILE;
1.27 markus 905: if (options->system_hostfile2 == NULL)
1.55 markus 906: options->system_hostfile2 = _PATH_SSH_SYSTEM_HOSTFILE2;
1.27 markus 907: if (options->user_hostfile2 == NULL)
1.55 markus 908: options->user_hostfile2 = _PATH_SSH_USER_HOSTFILE2;
1.95 markus 909: if (options->log_level == SYSLOG_LEVEL_NOT_SET)
1.54 markus 910: options->log_level = SYSLOG_LEVEL_INFO;
1.90 stevesk 911: if (options->clear_forwardings == 1)
912: clear_forwardings(options);
1.91 markus 913: if (options->no_host_authentication_for_localhost == - 1)
914: options->no_host_authentication_for_localhost = 0;
1.101 ! markus 915: if (options->enable_ssh_keysign == -1)
! 916: options->enable_ssh_keysign = 0;
1.17 markus 917: /* options->proxy_command should not be set by default */
918: /* options->user will be set in the main program if appropriate */
919: /* options->hostname will be set in the main program if appropriate */
1.52 markus 920: /* options->host_key_alias should not be set by default */
1.67 markus 921: /* options->preferred_authentications will be set in ssh */
1.1 deraadt 922: }