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