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