Annotation of src/usr.bin/ssh/readconf.c, Revision 1.84
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.84 ! markus 15: RCSID("$OpenBSD: readconf.c,v 1.83 2001/07/22 22:04:19 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.77 markus 117: oHostKeyAlgorithms, oBindAddress
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.17 markus 186: { NULL, 0 }
1.13 markus 187: };
188:
1.19 markus 189: /*
190: * Adds a local TCP/IP port forward to options. Never returns if there is an
191: * error.
192: */
1.1 deraadt 193:
1.26 markus 194: void
1.22 markus 195: add_local_forward(Options *options, u_short port, const char *host,
196: u_short host_port)
1.1 deraadt 197: {
1.17 markus 198: Forward *fwd;
199: extern uid_t original_real_uid;
200: if (port < IPPORT_RESERVED && original_real_uid != 0)
1.64 millert 201: fatal("Privileged ports can only be forwarded by root.");
1.17 markus 202: if (options->num_local_forwards >= SSH_MAX_FORWARDS_PER_DIRECTION)
203: fatal("Too many local forwards (max %d).", SSH_MAX_FORWARDS_PER_DIRECTION);
204: fwd = &options->local_forwards[options->num_local_forwards++];
205: fwd->port = port;
206: fwd->host = xstrdup(host);
207: fwd->host_port = host_port;
1.1 deraadt 208: }
209:
1.19 markus 210: /*
211: * Adds a remote TCP/IP port forward to options. Never returns if there is
212: * an error.
213: */
1.1 deraadt 214:
1.26 markus 215: void
1.22 markus 216: add_remote_forward(Options *options, u_short port, const char *host,
217: u_short host_port)
1.1 deraadt 218: {
1.17 markus 219: Forward *fwd;
220: if (options->num_remote_forwards >= SSH_MAX_FORWARDS_PER_DIRECTION)
221: fatal("Too many remote forwards (max %d).",
222: SSH_MAX_FORWARDS_PER_DIRECTION);
223: fwd = &options->remote_forwards[options->num_remote_forwards++];
224: fwd->port = port;
225: fwd->host = xstrdup(host);
226: fwd->host_port = host_port;
1.1 deraadt 227: }
228:
1.19 markus 229: /*
1.70 stevesk 230: * Returns the number of the token pointed to by cp or oBadOption.
1.19 markus 231: */
1.1 deraadt 232:
1.26 markus 233: static OpCodes
1.17 markus 234: parse_token(const char *cp, const char *filename, int linenum)
1.1 deraadt 235: {
1.51 markus 236: u_int i;
1.1 deraadt 237:
1.17 markus 238: for (i = 0; keywords[i].name; i++)
1.20 markus 239: if (strcasecmp(cp, keywords[i].name) == 0)
1.17 markus 240: return keywords[i].opcode;
241:
1.75 stevesk 242: error("%s: line %d: Bad configuration option: %s",
243: filename, linenum, cp);
1.17 markus 244: return oBadOption;
1.1 deraadt 245: }
246:
1.19 markus 247: /*
248: * Processes a single option line as used in the configuration files. This
249: * only sets those values that have not already been set.
250: */
1.1 deraadt 251:
1.14 markus 252: int
253: process_config_line(Options *options, const char *host,
1.17 markus 254: char *line, const char *filename, int linenum,
255: int *activep)
1.1 deraadt 256: {
1.38 provos 257: char buf[256], *s, *string, **charptr, *endofnumber, *keyword, *arg;
1.22 markus 258: int opcode, *intptr, value;
259: u_short fwd_port, fwd_host_port;
1.1 deraadt 260:
1.42 provos 261: s = line;
262: /* Get the keyword. (Each line is supposed to begin with a keyword). */
263: keyword = strdelim(&s);
264: /* Ignore leading whitespace. */
265: if (*keyword == '\0')
1.43 markus 266: keyword = strdelim(&s);
1.56 deraadt 267: if (keyword == NULL || !*keyword || *keyword == '\n' || *keyword == '#')
1.17 markus 268: return 0;
269:
1.38 provos 270: opcode = parse_token(keyword, filename, linenum);
1.17 markus 271:
272: switch (opcode) {
273: case oBadOption:
1.19 markus 274: /* don't panic, but count bad options */
275: return -1;
1.17 markus 276: /* NOTREACHED */
277: case oForwardAgent:
278: intptr = &options->forward_agent;
279: parse_flag:
1.42 provos 280: arg = strdelim(&s);
1.40 ho 281: if (!arg || *arg == '\0')
1.17 markus 282: fatal("%.200s line %d: Missing yes/no argument.", filename, linenum);
283: value = 0; /* To avoid compiler warning... */
1.38 provos 284: if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0)
1.17 markus 285: value = 1;
1.38 provos 286: else if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0)
1.17 markus 287: value = 0;
288: else
289: fatal("%.200s line %d: Bad yes/no argument.", filename, linenum);
290: if (*activep && *intptr == -1)
291: *intptr = value;
292: break;
293:
294: case oForwardX11:
295: intptr = &options->forward_x11;
296: goto parse_flag;
297:
298: case oGatewayPorts:
299: intptr = &options->gateway_ports;
300: goto parse_flag;
301:
302: case oUsePrivilegedPort:
303: intptr = &options->use_privileged_port;
304: goto parse_flag;
305:
306: case oRhostsAuthentication:
307: intptr = &options->rhosts_authentication;
308: goto parse_flag;
309:
310: case oPasswordAuthentication:
311: intptr = &options->password_authentication;
312: goto parse_flag;
313:
1.48 markus 314: case oKbdInteractiveAuthentication:
315: intptr = &options->kbd_interactive_authentication;
316: goto parse_flag;
317:
318: case oKbdInteractiveDevices:
319: charptr = &options->kbd_interactive_devices;
320: goto parse_string;
321:
1.50 markus 322: case oPubkeyAuthentication:
323: intptr = &options->pubkey_authentication;
1.30 markus 324: goto parse_flag;
325:
1.17 markus 326: case oRSAAuthentication:
327: intptr = &options->rsa_authentication;
328: goto parse_flag;
329:
330: case oRhostsRSAAuthentication:
331: intptr = &options->rhosts_rsa_authentication;
332: goto parse_flag;
333:
1.72 markus 334: case oHostbasedAuthentication:
335: intptr = &options->hostbased_authentication;
336: goto parse_flag;
337:
1.59 markus 338: case oChallengeResponseAuthentication:
1.78 markus 339: intptr = &options->challenge_response_authentication;
1.17 markus 340: goto parse_flag;
1.82 dugsong 341: #if defined(KRB4) || defined(KRB5)
1.17 markus 342: case oKerberosAuthentication:
343: intptr = &options->kerberos_authentication;
344: goto parse_flag;
1.82 dugsong 345: #endif
346: #if defined(AFS) || defined(KRB5)
1.17 markus 347: case oKerberosTgtPassing:
348: intptr = &options->kerberos_tgt_passing;
349: goto parse_flag;
1.82 dugsong 350: #endif
351: #ifdef AFS
1.17 markus 352: case oAFSTokenPassing:
353: intptr = &options->afs_token_passing;
354: goto parse_flag;
1.1 deraadt 355: #endif
1.17 markus 356: case oFallBackToRsh:
357: intptr = &options->fallback_to_rsh;
358: goto parse_flag;
359:
360: case oUseRsh:
361: intptr = &options->use_rsh;
362: goto parse_flag;
363:
364: case oBatchMode:
365: intptr = &options->batch_mode;
366: goto parse_flag;
367:
368: case oCheckHostIP:
369: intptr = &options->check_host_ip;
370: goto parse_flag;
371:
372: case oStrictHostKeyChecking:
373: intptr = &options->strict_host_key_checking;
1.42 provos 374: arg = strdelim(&s);
1.40 ho 375: if (!arg || *arg == '\0')
1.60 stevesk 376: fatal("%.200s line %d: Missing yes/no/ask argument.",
1.17 markus 377: filename, linenum);
378: value = 0; /* To avoid compiler warning... */
1.38 provos 379: if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0)
1.17 markus 380: value = 1;
1.38 provos 381: else if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0)
1.17 markus 382: value = 0;
1.38 provos 383: else if (strcmp(arg, "ask") == 0)
1.17 markus 384: value = 2;
385: else
386: fatal("%.200s line %d: Bad yes/no/ask argument.", filename, linenum);
387: if (*activep && *intptr == -1)
388: *intptr = value;
389: break;
390:
391: case oCompression:
392: intptr = &options->compression;
393: goto parse_flag;
394:
395: case oKeepAlives:
396: intptr = &options->keepalives;
397: goto parse_flag;
398:
399: case oNumberOfPasswordPrompts:
400: intptr = &options->number_of_password_prompts;
401: goto parse_int;
402:
403: case oCompressionLevel:
404: intptr = &options->compression_level;
405: goto parse_int;
406:
407: case oIdentityFile:
1.42 provos 408: arg = strdelim(&s);
1.40 ho 409: if (!arg || *arg == '\0')
1.17 markus 410: fatal("%.200s line %d: Missing argument.", filename, linenum);
411: if (*activep) {
1.50 markus 412: intptr = &options->num_identity_files;
1.27 markus 413: if (*intptr >= SSH_MAX_IDENTITY_FILES)
1.17 markus 414: fatal("%.200s line %d: Too many identity files specified (max %d).",
415: filename, linenum, SSH_MAX_IDENTITY_FILES);
1.50 markus 416: charptr = &options->identity_files[*intptr];
1.38 provos 417: *charptr = xstrdup(arg);
1.27 markus 418: *intptr = *intptr + 1;
1.17 markus 419: }
420: break;
421:
1.34 markus 422: case oXAuthLocation:
423: charptr=&options->xauth_location;
424: goto parse_string;
425:
1.17 markus 426: case oUser:
427: charptr = &options->user;
428: parse_string:
1.42 provos 429: arg = strdelim(&s);
1.40 ho 430: if (!arg || *arg == '\0')
1.17 markus 431: fatal("%.200s line %d: Missing argument.", filename, linenum);
432: if (*activep && *charptr == NULL)
1.38 provos 433: *charptr = xstrdup(arg);
1.17 markus 434: break;
435:
436: case oGlobalKnownHostsFile:
437: charptr = &options->system_hostfile;
438: goto parse_string;
439:
440: case oUserKnownHostsFile:
441: charptr = &options->user_hostfile;
442: goto parse_string;
443:
1.27 markus 444: case oGlobalKnownHostsFile2:
445: charptr = &options->system_hostfile2;
446: goto parse_string;
447:
448: case oUserKnownHostsFile2:
449: charptr = &options->user_hostfile2;
450: goto parse_string;
451:
1.17 markus 452: case oHostName:
453: charptr = &options->hostname;
454: goto parse_string;
455:
1.52 markus 456: case oHostKeyAlias:
457: charptr = &options->host_key_alias;
458: goto parse_string;
459:
1.67 markus 460: case oPreferredAuthentications:
461: charptr = &options->preferred_authentications;
462: goto parse_string;
463:
1.77 markus 464: case oBindAddress:
465: charptr = &options->bind_address;
466: goto parse_string;
467:
1.17 markus 468: case oProxyCommand:
469: charptr = &options->proxy_command;
470: string = xstrdup("");
1.42 provos 471: while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1.38 provos 472: string = xrealloc(string, strlen(string) + strlen(arg) + 2);
1.17 markus 473: strcat(string, " ");
1.38 provos 474: strcat(string, arg);
1.17 markus 475: }
476: if (*activep && *charptr == NULL)
477: *charptr = string;
478: else
479: xfree(string);
480: return 0;
481:
482: case oPort:
483: intptr = &options->port;
484: parse_int:
1.42 provos 485: arg = strdelim(&s);
1.40 ho 486: if (!arg || *arg == '\0')
1.17 markus 487: fatal("%.200s line %d: Missing argument.", filename, linenum);
1.38 provos 488: if (arg[0] < '0' || arg[0] > '9')
1.17 markus 489: fatal("%.200s line %d: Bad number.", filename, linenum);
1.21 markus 490:
491: /* Octal, decimal, or hex format? */
1.38 provos 492: value = strtol(arg, &endofnumber, 0);
493: if (arg == endofnumber)
1.21 markus 494: fatal("%.200s line %d: Bad number.", filename, linenum);
1.17 markus 495: if (*activep && *intptr == -1)
496: *intptr = value;
497: break;
498:
499: case oConnectionAttempts:
500: intptr = &options->connection_attempts;
501: goto parse_int;
502:
503: case oCipher:
504: intptr = &options->cipher;
1.42 provos 505: arg = strdelim(&s);
1.40 ho 506: if (!arg || *arg == '\0')
1.32 markus 507: fatal("%.200s line %d: Missing argument.", filename, linenum);
1.38 provos 508: value = cipher_number(arg);
1.17 markus 509: if (value == -1)
510: fatal("%.200s line %d: Bad cipher '%s'.",
1.38 provos 511: filename, linenum, arg ? arg : "<NONE>");
1.17 markus 512: if (*activep && *intptr == -1)
513: *intptr = value;
514: break;
515:
1.25 markus 516: case oCiphers:
1.42 provos 517: arg = strdelim(&s);
1.40 ho 518: if (!arg || *arg == '\0')
1.32 markus 519: fatal("%.200s line %d: Missing argument.", filename, linenum);
1.38 provos 520: if (!ciphers_valid(arg))
1.31 markus 521: fatal("%.200s line %d: Bad SSH2 cipher spec '%s'.",
1.38 provos 522: filename, linenum, arg ? arg : "<NONE>");
1.25 markus 523: if (*activep && options->ciphers == NULL)
1.38 provos 524: options->ciphers = xstrdup(arg);
1.25 markus 525: break;
526:
1.62 markus 527: case oMacs:
528: arg = strdelim(&s);
529: if (!arg || *arg == '\0')
530: fatal("%.200s line %d: Missing argument.", filename, linenum);
531: if (!mac_valid(arg))
532: fatal("%.200s line %d: Bad SSH2 Mac spec '%s'.",
533: filename, linenum, arg ? arg : "<NONE>");
534: if (*activep && options->macs == NULL)
535: options->macs = xstrdup(arg);
536: break;
537:
1.76 markus 538: case oHostKeyAlgorithms:
539: arg = strdelim(&s);
540: if (!arg || *arg == '\0')
541: fatal("%.200s line %d: Missing argument.", filename, linenum);
542: if (!key_names_valid2(arg))
543: fatal("%.200s line %d: Bad protocol 2 host key algorithms '%s'.",
544: filename, linenum, arg ? arg : "<NONE>");
545: if (*activep && options->hostkeyalgorithms == NULL)
546: options->hostkeyalgorithms = xstrdup(arg);
547: break;
548:
1.25 markus 549: case oProtocol:
550: intptr = &options->protocol;
1.42 provos 551: arg = strdelim(&s);
1.40 ho 552: if (!arg || *arg == '\0')
1.32 markus 553: fatal("%.200s line %d: Missing argument.", filename, linenum);
1.38 provos 554: value = proto_spec(arg);
1.25 markus 555: if (value == SSH_PROTO_UNKNOWN)
556: fatal("%.200s line %d: Bad protocol spec '%s'.",
1.38 provos 557: filename, linenum, arg ? arg : "<NONE>");
1.25 markus 558: if (*activep && *intptr == SSH_PROTO_UNKNOWN)
559: *intptr = value;
560: break;
561:
1.17 markus 562: case oLogLevel:
563: intptr = (int *) &options->log_level;
1.42 provos 564: arg = strdelim(&s);
1.38 provos 565: value = log_level_number(arg);
1.17 markus 566: if (value == (LogLevel) - 1)
1.64 millert 567: fatal("%.200s line %d: unsupported log level '%s'",
1.38 provos 568: filename, linenum, arg ? arg : "<NONE>");
1.17 markus 569: if (*activep && (LogLevel) * intptr == -1)
570: *intptr = (LogLevel) value;
571: break;
572:
573: case oRemoteForward:
1.42 provos 574: arg = strdelim(&s);
1.40 ho 575: if (!arg || *arg == '\0')
1.17 markus 576: fatal("%.200s line %d: Missing argument.", filename, linenum);
1.74 stevesk 577: fwd_port = a2port(arg);
578: if (fwd_port == 0)
1.17 markus 579: fatal("%.200s line %d: Badly formatted port number.",
580: filename, linenum);
1.42 provos 581: arg = strdelim(&s);
1.40 ho 582: if (!arg || *arg == '\0')
1.17 markus 583: fatal("%.200s line %d: Missing second argument.",
584: filename, linenum);
1.38 provos 585: if (sscanf(arg, "%255[^:]:%hu", buf, &fwd_host_port) != 2)
1.17 markus 586: fatal("%.200s line %d: Badly formatted host:port.",
587: filename, linenum);
588: if (*activep)
589: add_remote_forward(options, fwd_port, buf, fwd_host_port);
590: break;
591:
592: case oLocalForward:
1.42 provos 593: arg = strdelim(&s);
1.40 ho 594: if (!arg || *arg == '\0')
1.17 markus 595: fatal("%.200s line %d: Missing argument.", filename, linenum);
1.74 stevesk 596: fwd_port = a2port(arg);
597: if (fwd_port == 0)
1.17 markus 598: fatal("%.200s line %d: Badly formatted port number.",
599: filename, linenum);
1.42 provos 600: arg = strdelim(&s);
1.40 ho 601: if (!arg || *arg == '\0')
1.17 markus 602: fatal("%.200s line %d: Missing second argument.",
603: filename, linenum);
1.38 provos 604: if (sscanf(arg, "%255[^:]:%hu", buf, &fwd_host_port) != 2)
1.17 markus 605: fatal("%.200s line %d: Badly formatted host:port.",
606: filename, linenum);
607: if (*activep)
608: add_local_forward(options, fwd_port, buf, fwd_host_port);
609: break;
1.71 markus 610:
611: case oDynamicForward:
612: arg = strdelim(&s);
613: if (!arg || *arg == '\0')
614: fatal("%.200s line %d: Missing port argument.",
615: filename, linenum);
1.74 stevesk 616: fwd_port = a2port(arg);
617: if (fwd_port == 0)
1.71 markus 618: fatal("%.200s line %d: Badly formatted port number.",
619: filename, linenum);
620: add_local_forward(options, fwd_port, "socks4", 0);
1.72 markus 621: break;
1.17 markus 622:
623: case oHost:
624: *activep = 0;
1.42 provos 625: while ((arg = strdelim(&s)) != NULL && *arg != '\0')
1.38 provos 626: if (match_pattern(host, arg)) {
627: debug("Applying options for %.100s", arg);
1.17 markus 628: *activep = 1;
629: break;
630: }
1.42 provos 631: /* Avoid garbage check below, as strdelim is done. */
1.17 markus 632: return 0;
633:
634: case oEscapeChar:
635: intptr = &options->escape_char;
1.42 provos 636: arg = strdelim(&s);
1.40 ho 637: if (!arg || *arg == '\0')
1.17 markus 638: fatal("%.200s line %d: Missing argument.", filename, linenum);
1.38 provos 639: if (arg[0] == '^' && arg[2] == 0 &&
1.51 markus 640: (u_char) arg[1] >= 64 && (u_char) arg[1] < 128)
641: value = (u_char) arg[1] & 31;
1.38 provos 642: else if (strlen(arg) == 1)
1.51 markus 643: value = (u_char) arg[0];
1.38 provos 644: else if (strcmp(arg, "none") == 0)
1.79 stevesk 645: value = SSH_ESCAPECHAR_NONE;
1.17 markus 646: else {
647: fatal("%.200s line %d: Bad escape character.",
648: filename, linenum);
649: /* NOTREACHED */
650: value = 0; /* Avoid compiler warning. */
651: }
652: if (*activep && *intptr == -1)
653: *intptr = value;
654: break;
655:
656: default:
657: fatal("process_config_line: Unimplemented opcode %d", opcode);
658: }
659:
660: /* Check that there is no garbage at end of line. */
1.57 djm 661: if ((arg = strdelim(&s)) != NULL && *arg != '\0') {
1.39 ho 662: fatal("%.200s line %d: garbage at end of line; \"%.200s\".",
663: filename, linenum, arg);
664: }
1.17 markus 665: return 0;
1.1 deraadt 666: }
667:
668:
1.19 markus 669: /*
670: * Reads the config file and modifies the options accordingly. Options
671: * should already be initialized before this call. This never returns if
672: * there is an error. If the file does not exist, this returns immediately.
673: */
1.1 deraadt 674:
1.26 markus 675: void
1.17 markus 676: read_config_file(const char *filename, const char *host, Options *options)
1.1 deraadt 677: {
1.17 markus 678: FILE *f;
679: char line[1024];
680: int active, linenum;
681: int bad_options = 0;
682:
683: /* Open the file. */
684: f = fopen(filename, "r");
685: if (!f)
686: return;
687:
688: debug("Reading configuration data %.200s", filename);
689:
1.19 markus 690: /*
691: * Mark that we are now processing the options. This flag is turned
692: * on/off by Host specifications.
693: */
1.17 markus 694: active = 1;
695: linenum = 0;
696: while (fgets(line, sizeof(line), f)) {
697: /* Update line number counter. */
698: linenum++;
699: if (process_config_line(options, host, line, filename, linenum, &active) != 0)
700: bad_options++;
701: }
702: fclose(f);
703: if (bad_options > 0)
1.64 millert 704: fatal("%s: terminating, %d bad configuration options",
1.17 markus 705: filename, bad_options);
1.1 deraadt 706: }
707:
1.19 markus 708: /*
709: * Initializes options to special values that indicate that they have not yet
710: * been set. Read_config_file will only set options with this value. Options
711: * are processed in the following order: command line, user config file,
712: * system config file. Last, fill_default_options is called.
713: */
1.1 deraadt 714:
1.26 markus 715: void
1.17 markus 716: initialize_options(Options * options)
1.1 deraadt 717: {
1.17 markus 718: memset(options, 'X', sizeof(*options));
719: options->forward_agent = -1;
720: options->forward_x11 = -1;
1.34 markus 721: options->xauth_location = NULL;
1.17 markus 722: options->gateway_ports = -1;
723: options->use_privileged_port = -1;
724: options->rhosts_authentication = -1;
725: options->rsa_authentication = -1;
1.50 markus 726: options->pubkey_authentication = -1;
1.78 markus 727: options->challenge_response_authentication = -1;
1.82 dugsong 728: #if defined(KRB4) || defined(KRB5)
1.17 markus 729: options->kerberos_authentication = -1;
1.1 deraadt 730: #endif
1.82 dugsong 731: #if defined(AFS) || defined(KRB5)
732: options->kerberos_tgt_passing = -1;
733: #endif
1.5 dugsong 734: #ifdef AFS
1.17 markus 735: options->afs_token_passing = -1;
1.1 deraadt 736: #endif
1.17 markus 737: options->password_authentication = -1;
1.48 markus 738: options->kbd_interactive_authentication = -1;
739: options->kbd_interactive_devices = NULL;
1.17 markus 740: options->rhosts_rsa_authentication = -1;
1.72 markus 741: options->hostbased_authentication = -1;
1.17 markus 742: options->fallback_to_rsh = -1;
743: options->use_rsh = -1;
744: options->batch_mode = -1;
745: options->check_host_ip = -1;
746: options->strict_host_key_checking = -1;
747: options->compression = -1;
748: options->keepalives = -1;
749: options->compression_level = -1;
750: options->port = -1;
751: options->connection_attempts = -1;
752: options->number_of_password_prompts = -1;
753: options->cipher = -1;
1.25 markus 754: options->ciphers = NULL;
1.62 markus 755: options->macs = NULL;
1.76 markus 756: options->hostkeyalgorithms = NULL;
1.25 markus 757: options->protocol = SSH_PROTO_UNKNOWN;
1.17 markus 758: options->num_identity_files = 0;
759: options->hostname = NULL;
1.52 markus 760: options->host_key_alias = NULL;
1.17 markus 761: options->proxy_command = NULL;
762: options->user = NULL;
763: options->escape_char = -1;
764: options->system_hostfile = NULL;
765: options->user_hostfile = NULL;
1.27 markus 766: options->system_hostfile2 = NULL;
767: options->user_hostfile2 = NULL;
1.17 markus 768: options->num_local_forwards = 0;
769: options->num_remote_forwards = 0;
770: options->log_level = (LogLevel) - 1;
1.67 markus 771: options->preferred_authentications = NULL;
1.77 markus 772: options->bind_address = 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: }