]> andersk Git - openssh.git/blob - readconf.c
1d6409fdb1faf76d8b6411d259a6f9d1dd19272c
[openssh.git] / readconf.c
1 /* $OpenBSD: readconf.c,v 1.164 2007/12/31 10:41:31 dtucker Exp $ */
2 /*
3  * Author: Tatu Ylonen <ylo@cs.hut.fi>
4  * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5  *                    All rights reserved
6  * Functions for reading the configuration files.
7  *
8  * As far as I am concerned, the code I have written for this software
9  * can be used freely for any purpose.  Any derived versions of this
10  * software must be clearly marked as such, and if the derived work is
11  * incompatible with the protocol description in the RFC file, it must be
12  * called by a name other than "ssh" or "Secure Shell".
13  */
14
15 #include "includes.h"
16
17 #include <sys/types.h>
18 #include <sys/stat.h>
19 #include <sys/socket.h>
20
21 #include <netinet/in.h>
22
23 #include <ctype.h>
24 #include <errno.h>
25 #include <netdb.h>
26 #include <signal.h>
27 #include <stdarg.h>
28 #include <stdio.h>
29 #include <string.h>
30 #include <unistd.h>
31
32 #include "xmalloc.h"
33 #include "ssh.h"
34 #include "compat.h"
35 #include "cipher.h"
36 #include "pathnames.h"
37 #include "log.h"
38 #include "key.h"
39 #include "readconf.h"
40 #include "match.h"
41 #include "misc.h"
42 #include "buffer.h"
43 #include "kex.h"
44 #include "mac.h"
45
46 /* Format of the configuration file:
47
48    # Configuration data is parsed as follows:
49    #  1. command line options
50    #  2. user-specific file
51    #  3. system-wide file
52    # Any configuration value is only changed the first time it is set.
53    # Thus, host-specific definitions should be at the beginning of the
54    # configuration file, and defaults at the end.
55
56    # Host-specific declarations.  These may override anything above.  A single
57    # host may match multiple declarations; these are processed in the order
58    # that they are given in.
59
60    Host *.ngs.fi ngs.fi
61      User foo
62
63    Host fake.com
64      HostName another.host.name.real.org
65      User blaah
66      Port 34289
67      ForwardX11 no
68      ForwardAgent no
69
70    Host books.com
71      RemoteForward 9999 shadows.cs.hut.fi:9999
72      Cipher 3des
73
74    Host fascist.blob.com
75      Port 23123
76      User tylonen
77      PasswordAuthentication no
78
79    Host puukko.hut.fi
80      User t35124p
81      ProxyCommand ssh-proxy %h %p
82
83    Host *.fr
84      PublicKeyAuthentication no
85
86    Host *.su
87      Cipher none
88      PasswordAuthentication no
89
90    Host vpn.fake.com
91      Tunnel yes
92      TunnelDevice 3
93
94    # Defaults for various options
95    Host *
96      ForwardAgent no
97      ForwardX11 no
98      PasswordAuthentication yes
99      RSAAuthentication yes
100      RhostsRSAAuthentication yes
101      StrictHostKeyChecking yes
102      TcpKeepAlive no
103      IdentityFile ~/.ssh/identity
104      Port 22
105      EscapeChar ~
106
107 */
108
109 /* Keyword tokens. */
110
111 typedef enum {
112         oBadOption,
113         oForwardAgent, oForwardX11, oForwardX11Trusted, oGatewayPorts,
114         oExitOnForwardFailure,
115         oPasswordAuthentication, oRSAAuthentication,
116         oChallengeResponseAuthentication, oXAuthLocation,
117         oIdentityFile, oHostName, oPort, oCipher, oRemoteForward, oLocalForward,
118         oUser, oHost, oEscapeChar, oRhostsRSAAuthentication, oProxyCommand,
119         oGlobalKnownHostsFile, oUserKnownHostsFile, oConnectionAttempts,
120         oBatchMode, oCheckHostIP, oStrictHostKeyChecking, oCompression,
121         oCompressionLevel, oTCPKeepAlive, oNumberOfPasswordPrompts,
122         oUsePrivilegedPort, oLogLevel, oCiphers, oProtocol, oMacs,
123         oGlobalKnownHostsFile2, oUserKnownHostsFile2, oPubkeyAuthentication,
124         oKbdInteractiveAuthentication, oKbdInteractiveDevices, oHostKeyAlias,
125         oDynamicForward, oPreferredAuthentications, oHostbasedAuthentication,
126         oHostKeyAlgorithms, oBindAddress, oSmartcardDevice,
127         oClearAllForwardings, oNoHostAuthenticationForLocalhost,
128         oEnableSSHKeysign, oRekeyLimit, oVerifyHostKeyDNS, oConnectTimeout,
129         oAddressFamily, oGssAuthentication, oGssDelegateCreds,
130         oServerAliveInterval, oServerAliveCountMax, oIdentitiesOnly,
131         oSendEnv, oControlPath, oControlMaster, oHashKnownHosts,
132         oTunnel, oTunnelDevice, oLocalCommand, oPermitLocalCommand,
133         oDeprecated, oUnsupported
134 } OpCodes;
135
136 /* Textual representations of the tokens. */
137
138 static struct {
139         const char *name;
140         OpCodes opcode;
141 } keywords[] = {
142         { "forwardagent", oForwardAgent },
143         { "forwardx11", oForwardX11 },
144         { "forwardx11trusted", oForwardX11Trusted },
145         { "exitonforwardfailure", oExitOnForwardFailure },
146         { "xauthlocation", oXAuthLocation },
147         { "gatewayports", oGatewayPorts },
148         { "useprivilegedport", oUsePrivilegedPort },
149         { "rhostsauthentication", oDeprecated },
150         { "passwordauthentication", oPasswordAuthentication },
151         { "kbdinteractiveauthentication", oKbdInteractiveAuthentication },
152         { "kbdinteractivedevices", oKbdInteractiveDevices },
153         { "rsaauthentication", oRSAAuthentication },
154         { "pubkeyauthentication", oPubkeyAuthentication },
155         { "dsaauthentication", oPubkeyAuthentication },             /* alias */
156         { "rhostsrsaauthentication", oRhostsRSAAuthentication },
157         { "hostbasedauthentication", oHostbasedAuthentication },
158         { "challengeresponseauthentication", oChallengeResponseAuthentication },
159         { "skeyauthentication", oChallengeResponseAuthentication }, /* alias */
160         { "tisauthentication", oChallengeResponseAuthentication },  /* alias */
161         { "kerberosauthentication", oUnsupported },
162         { "kerberostgtpassing", oUnsupported },
163         { "afstokenpassing", oUnsupported },
164 #if defined(GSSAPI)
165         { "gssapiauthentication", oGssAuthentication },
166         { "gssapidelegatecredentials", oGssDelegateCreds },
167 #else
168         { "gssapiauthentication", oUnsupported },
169         { "gssapidelegatecredentials", oUnsupported },
170 #endif
171         { "fallbacktorsh", oDeprecated },
172         { "usersh", oDeprecated },
173         { "identityfile", oIdentityFile },
174         { "identityfile2", oIdentityFile },                     /* alias */
175         { "identitiesonly", oIdentitiesOnly },
176         { "hostname", oHostName },
177         { "hostkeyalias", oHostKeyAlias },
178         { "proxycommand", oProxyCommand },
179         { "port", oPort },
180         { "cipher", oCipher },
181         { "ciphers", oCiphers },
182         { "macs", oMacs },
183         { "protocol", oProtocol },
184         { "remoteforward", oRemoteForward },
185         { "localforward", oLocalForward },
186         { "user", oUser },
187         { "host", oHost },
188         { "escapechar", oEscapeChar },
189         { "globalknownhostsfile", oGlobalKnownHostsFile },
190         { "userknownhostsfile", oUserKnownHostsFile },          /* obsolete */
191         { "globalknownhostsfile2", oGlobalKnownHostsFile2 },
192         { "userknownhostsfile2", oUserKnownHostsFile2 },        /* obsolete */
193         { "connectionattempts", oConnectionAttempts },
194         { "batchmode", oBatchMode },
195         { "checkhostip", oCheckHostIP },
196         { "stricthostkeychecking", oStrictHostKeyChecking },
197         { "compression", oCompression },
198         { "compressionlevel", oCompressionLevel },
199         { "tcpkeepalive", oTCPKeepAlive },
200         { "keepalive", oTCPKeepAlive },                         /* obsolete */
201         { "numberofpasswordprompts", oNumberOfPasswordPrompts },
202         { "loglevel", oLogLevel },
203         { "dynamicforward", oDynamicForward },
204         { "preferredauthentications", oPreferredAuthentications },
205         { "hostkeyalgorithms", oHostKeyAlgorithms },
206         { "bindaddress", oBindAddress },
207 #ifdef SMARTCARD
208         { "smartcarddevice", oSmartcardDevice },
209 #else
210         { "smartcarddevice", oUnsupported },
211 #endif
212         { "clearallforwardings", oClearAllForwardings },
213         { "enablesshkeysign", oEnableSSHKeysign },
214         { "verifyhostkeydns", oVerifyHostKeyDNS },
215         { "nohostauthenticationforlocalhost", oNoHostAuthenticationForLocalhost },
216         { "rekeylimit", oRekeyLimit },
217         { "connecttimeout", oConnectTimeout },
218         { "addressfamily", oAddressFamily },
219         { "serveraliveinterval", oServerAliveInterval },
220         { "serveralivecountmax", oServerAliveCountMax },
221         { "sendenv", oSendEnv },
222         { "controlpath", oControlPath },
223         { "controlmaster", oControlMaster },
224         { "hashknownhosts", oHashKnownHosts },
225         { "tunnel", oTunnel },
226         { "tunneldevice", oTunnelDevice },
227         { "localcommand", oLocalCommand },
228         { "permitlocalcommand", oPermitLocalCommand },
229         { NULL, oBadOption }
230 };
231
232 /*
233  * Adds a local TCP/IP port forward to options.  Never returns if there is an
234  * error.
235  */
236
237 void
238 add_local_forward(Options *options, const Forward *newfwd)
239 {
240         Forward *fwd;
241 #ifndef NO_IPPORT_RESERVED_CONCEPT
242         extern uid_t original_real_uid;
243         if (newfwd->listen_port < IPPORT_RESERVED && original_real_uid != 0)
244                 fatal("Privileged ports can only be forwarded by root.");
245 #endif
246         if (options->num_local_forwards >= SSH_MAX_FORWARDS_PER_DIRECTION)
247                 fatal("Too many local forwards (max %d).", SSH_MAX_FORWARDS_PER_DIRECTION);
248         fwd = &options->local_forwards[options->num_local_forwards++];
249
250         fwd->listen_host = (newfwd->listen_host == NULL) ?
251             NULL : xstrdup(newfwd->listen_host);
252         fwd->listen_port = newfwd->listen_port;
253         fwd->connect_host = xstrdup(newfwd->connect_host);
254         fwd->connect_port = newfwd->connect_port;
255 }
256
257 /*
258  * Adds a remote TCP/IP port forward to options.  Never returns if there is
259  * an error.
260  */
261
262 void
263 add_remote_forward(Options *options, const Forward *newfwd)
264 {
265         Forward *fwd;
266         if (options->num_remote_forwards >= SSH_MAX_FORWARDS_PER_DIRECTION)
267                 fatal("Too many remote forwards (max %d).",
268                     SSH_MAX_FORWARDS_PER_DIRECTION);
269         fwd = &options->remote_forwards[options->num_remote_forwards++];
270
271         fwd->listen_host = (newfwd->listen_host == NULL) ?
272             NULL : xstrdup(newfwd->listen_host);
273         fwd->listen_port = newfwd->listen_port;
274         fwd->connect_host = xstrdup(newfwd->connect_host);
275         fwd->connect_port = newfwd->connect_port;
276 }
277
278 static void
279 clear_forwardings(Options *options)
280 {
281         int i;
282
283         for (i = 0; i < options->num_local_forwards; i++) {
284                 if (options->local_forwards[i].listen_host != NULL)
285                         xfree(options->local_forwards[i].listen_host);
286                 xfree(options->local_forwards[i].connect_host);
287         }
288         options->num_local_forwards = 0;
289         for (i = 0; i < options->num_remote_forwards; i++) {
290                 if (options->remote_forwards[i].listen_host != NULL)
291                         xfree(options->remote_forwards[i].listen_host);
292                 xfree(options->remote_forwards[i].connect_host);
293         }
294         options->num_remote_forwards = 0;
295         options->tun_open = SSH_TUNMODE_NO;
296 }
297
298 /*
299  * Returns the number of the token pointed to by cp or oBadOption.
300  */
301
302 static OpCodes
303 parse_token(const char *cp, const char *filename, int linenum)
304 {
305         u_int i;
306
307         for (i = 0; keywords[i].name; i++)
308                 if (strcasecmp(cp, keywords[i].name) == 0)
309                         return keywords[i].opcode;
310
311         error("%s: line %d: Bad configuration option: %s",
312             filename, linenum, cp);
313         return oBadOption;
314 }
315
316 /*
317  * Processes a single option line as used in the configuration files. This
318  * only sets those values that have not already been set.
319  */
320 #define WHITESPACE " \t\r\n"
321
322 int
323 process_config_line(Options *options, const char *host,
324                     char *line, const char *filename, int linenum,
325                     int *activep)
326 {
327         char *s, **charptr, *endofnumber, *keyword, *arg, *arg2, fwdarg[256];
328         int opcode, *intptr, value, value2, scale;
329         LogLevel *log_level_ptr;
330         long long orig, val64;
331         size_t len;
332         Forward fwd;
333
334         /* Strip trailing whitespace */
335         for (len = strlen(line) - 1; len > 0; len--) {
336                 if (strchr(WHITESPACE, line[len]) == NULL)
337                         break;
338                 line[len] = '\0';
339         }
340
341         s = line;
342         /* Get the keyword. (Each line is supposed to begin with a keyword). */
343         if ((keyword = strdelim(&s)) == NULL)
344                 return 0;
345         /* Ignore leading whitespace. */
346         if (*keyword == '\0')
347                 keyword = strdelim(&s);
348         if (keyword == NULL || !*keyword || *keyword == '\n' || *keyword == '#')
349                 return 0;
350
351         opcode = parse_token(keyword, filename, linenum);
352
353         switch (opcode) {
354         case oBadOption:
355                 /* don't panic, but count bad options */
356                 return -1;
357                 /* NOTREACHED */
358         case oConnectTimeout:
359                 intptr = &options->connection_timeout;
360 parse_time:
361                 arg = strdelim(&s);
362                 if (!arg || *arg == '\0')
363                         fatal("%s line %d: missing time value.",
364                             filename, linenum);
365                 if ((value = convtime(arg)) == -1)
366                         fatal("%s line %d: invalid time value.",
367                             filename, linenum);
368                 if (*activep && *intptr == -1)
369                         *intptr = value;
370                 break;
371
372         case oForwardAgent:
373                 intptr = &options->forward_agent;
374 parse_flag:
375                 arg = strdelim(&s);
376                 if (!arg || *arg == '\0')
377                         fatal("%.200s line %d: Missing yes/no argument.", filename, linenum);
378                 value = 0;      /* To avoid compiler warning... */
379                 if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0)
380                         value = 1;
381                 else if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0)
382                         value = 0;
383                 else
384                         fatal("%.200s line %d: Bad yes/no argument.", filename, linenum);
385                 if (*activep && *intptr == -1)
386                         *intptr = value;
387                 break;
388
389         case oForwardX11:
390                 intptr = &options->forward_x11;
391                 goto parse_flag;
392
393         case oForwardX11Trusted:
394                 intptr = &options->forward_x11_trusted;
395                 goto parse_flag;
396
397         case oGatewayPorts:
398                 intptr = &options->gateway_ports;
399                 goto parse_flag;
400
401         case oExitOnForwardFailure:
402                 intptr = &options->exit_on_forward_failure;
403                 goto parse_flag;
404
405         case oUsePrivilegedPort:
406                 intptr = &options->use_privileged_port;
407                 goto parse_flag;
408
409         case oPasswordAuthentication:
410                 intptr = &options->password_authentication;
411                 goto parse_flag;
412
413         case oKbdInteractiveAuthentication:
414                 intptr = &options->kbd_interactive_authentication;
415                 goto parse_flag;
416
417         case oKbdInteractiveDevices:
418                 charptr = &options->kbd_interactive_devices;
419                 goto parse_string;
420
421         case oPubkeyAuthentication:
422                 intptr = &options->pubkey_authentication;
423                 goto parse_flag;
424
425         case oRSAAuthentication:
426                 intptr = &options->rsa_authentication;
427                 goto parse_flag;
428
429         case oRhostsRSAAuthentication:
430                 intptr = &options->rhosts_rsa_authentication;
431                 goto parse_flag;
432
433         case oHostbasedAuthentication:
434                 intptr = &options->hostbased_authentication;
435                 goto parse_flag;
436
437         case oChallengeResponseAuthentication:
438                 intptr = &options->challenge_response_authentication;
439                 goto parse_flag;
440
441         case oGssAuthentication:
442                 intptr = &options->gss_authentication;
443                 goto parse_flag;
444
445         case oGssDelegateCreds:
446                 intptr = &options->gss_deleg_creds;
447                 goto parse_flag;
448
449         case oBatchMode:
450                 intptr = &options->batch_mode;
451                 goto parse_flag;
452
453         case oCheckHostIP:
454                 intptr = &options->check_host_ip;
455                 goto parse_flag;
456
457         case oVerifyHostKeyDNS:
458                 intptr = &options->verify_host_key_dns;
459                 goto parse_yesnoask;
460
461         case oStrictHostKeyChecking:
462                 intptr = &options->strict_host_key_checking;
463 parse_yesnoask:
464                 arg = strdelim(&s);
465                 if (!arg || *arg == '\0')
466                         fatal("%.200s line %d: Missing yes/no/ask argument.",
467                             filename, linenum);
468                 value = 0;      /* To avoid compiler warning... */
469                 if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0)
470                         value = 1;
471                 else if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0)
472                         value = 0;
473                 else if (strcmp(arg, "ask") == 0)
474                         value = 2;
475                 else
476                         fatal("%.200s line %d: Bad yes/no/ask argument.", filename, linenum);
477                 if (*activep && *intptr == -1)
478                         *intptr = value;
479                 break;
480
481         case oCompression:
482                 intptr = &options->compression;
483                 goto parse_flag;
484
485         case oTCPKeepAlive:
486                 intptr = &options->tcp_keep_alive;
487                 goto parse_flag;
488
489         case oNoHostAuthenticationForLocalhost:
490                 intptr = &options->no_host_authentication_for_localhost;
491                 goto parse_flag;
492
493         case oNumberOfPasswordPrompts:
494                 intptr = &options->number_of_password_prompts;
495                 goto parse_int;
496
497         case oCompressionLevel:
498                 intptr = &options->compression_level;
499                 goto parse_int;
500
501         case oRekeyLimit:
502                 intptr = &options->rekey_limit;
503                 arg = strdelim(&s);
504                 if (!arg || *arg == '\0')
505                         fatal("%.200s line %d: Missing argument.", filename, linenum);
506                 if (arg[0] < '0' || arg[0] > '9')
507                         fatal("%.200s line %d: Bad number.", filename, linenum);
508                 orig = val64 = strtoll(arg, &endofnumber, 10);
509                 if (arg == endofnumber)
510                         fatal("%.200s line %d: Bad number.", filename, linenum);
511                 switch (toupper(*endofnumber)) {
512                 case '\0':
513                         scale = 1;
514                         break;
515                 case 'K':
516                         scale = 1<<10;
517                         break;
518                 case 'M':
519                         scale = 1<<20;
520                         break;
521                 case 'G':
522                         scale = 1<<30;
523                         break;
524                 default:
525                         fatal("%.200s line %d: Invalid RekeyLimit suffix",
526                             filename, linenum);
527                 }
528                 val64 *= scale;
529                 /* detect integer wrap and too-large limits */
530                 if ((val64 / scale) != orig || val64 > INT_MAX)
531                         fatal("%.200s line %d: RekeyLimit too large",
532                             filename, linenum);
533                 if (val64 < 16)
534                         fatal("%.200s line %d: RekeyLimit too small",
535                             filename, linenum);
536                 if (*activep && *intptr == -1)
537                         *intptr = (int)val64;
538                 break;
539
540         case oIdentityFile:
541                 arg = strdelim(&s);
542                 if (!arg || *arg == '\0')
543                         fatal("%.200s line %d: Missing argument.", filename, linenum);
544                 if (*activep) {
545                         intptr = &options->num_identity_files;
546                         if (*intptr >= SSH_MAX_IDENTITY_FILES)
547                                 fatal("%.200s line %d: Too many identity files specified (max %d).",
548                                     filename, linenum, SSH_MAX_IDENTITY_FILES);
549                         charptr = &options->identity_files[*intptr];
550                         *charptr = xstrdup(arg);
551                         *intptr = *intptr + 1;
552                 }
553                 break;
554
555         case oXAuthLocation:
556                 charptr=&options->xauth_location;
557                 goto parse_string;
558
559         case oUser:
560                 charptr = &options->user;
561 parse_string:
562                 arg = strdelim(&s);
563                 if (!arg || *arg == '\0')
564                         fatal("%.200s line %d: Missing argument.", filename, linenum);
565                 if (*activep && *charptr == NULL)
566                         *charptr = xstrdup(arg);
567                 break;
568
569         case oGlobalKnownHostsFile:
570                 charptr = &options->system_hostfile;
571                 goto parse_string;
572
573         case oUserKnownHostsFile:
574                 charptr = &options->user_hostfile;
575                 goto parse_string;
576
577         case oGlobalKnownHostsFile2:
578                 charptr = &options->system_hostfile2;
579                 goto parse_string;
580
581         case oUserKnownHostsFile2:
582                 charptr = &options->user_hostfile2;
583                 goto parse_string;
584
585         case oHostName:
586                 charptr = &options->hostname;
587                 goto parse_string;
588
589         case oHostKeyAlias:
590                 charptr = &options->host_key_alias;
591                 goto parse_string;
592
593         case oPreferredAuthentications:
594                 charptr = &options->preferred_authentications;
595                 goto parse_string;
596
597         case oBindAddress:
598                 charptr = &options->bind_address;
599                 goto parse_string;
600
601         case oSmartcardDevice:
602                 charptr = &options->smartcard_device;
603                 goto parse_string;
604
605         case oProxyCommand:
606                 charptr = &options->proxy_command;
607 parse_command:
608                 if (s == NULL)
609                         fatal("%.200s line %d: Missing argument.", filename, linenum);
610                 len = strspn(s, WHITESPACE "=");
611                 if (*activep && *charptr == NULL)
612                         *charptr = xstrdup(s + len);
613                 return 0;
614
615         case oPort:
616                 intptr = &options->port;
617 parse_int:
618                 arg = strdelim(&s);
619                 if (!arg || *arg == '\0')
620                         fatal("%.200s line %d: Missing argument.", filename, linenum);
621                 if (arg[0] < '0' || arg[0] > '9')
622                         fatal("%.200s line %d: Bad number.", filename, linenum);
623
624                 /* Octal, decimal, or hex format? */
625                 value = strtol(arg, &endofnumber, 0);
626                 if (arg == endofnumber)
627                         fatal("%.200s line %d: Bad number.", filename, linenum);
628                 if (*activep && *intptr == -1)
629                         *intptr = value;
630                 break;
631
632         case oConnectionAttempts:
633                 intptr = &options->connection_attempts;
634                 goto parse_int;
635
636         case oCipher:
637                 intptr = &options->cipher;
638                 arg = strdelim(&s);
639                 if (!arg || *arg == '\0')
640                         fatal("%.200s line %d: Missing argument.", filename, linenum);
641                 value = cipher_number(arg);
642                 if (value == -1)
643                         fatal("%.200s line %d: Bad cipher '%s'.",
644                             filename, linenum, arg ? arg : "<NONE>");
645                 if (*activep && *intptr == -1)
646                         *intptr = value;
647                 break;
648
649         case oCiphers:
650                 arg = strdelim(&s);
651                 if (!arg || *arg == '\0')
652                         fatal("%.200s line %d: Missing argument.", filename, linenum);
653                 if (!ciphers_valid(arg))
654                         fatal("%.200s line %d: Bad SSH2 cipher spec '%s'.",
655                             filename, linenum, arg ? arg : "<NONE>");
656                 if (*activep && options->ciphers == NULL)
657                         options->ciphers = xstrdup(arg);
658                 break;
659
660         case oMacs:
661                 arg = strdelim(&s);
662                 if (!arg || *arg == '\0')
663                         fatal("%.200s line %d: Missing argument.", filename, linenum);
664                 if (!mac_valid(arg))
665                         fatal("%.200s line %d: Bad SSH2 Mac spec '%s'.",
666                             filename, linenum, arg ? arg : "<NONE>");
667                 if (*activep && options->macs == NULL)
668                         options->macs = xstrdup(arg);
669                 break;
670
671         case oHostKeyAlgorithms:
672                 arg = strdelim(&s);
673                 if (!arg || *arg == '\0')
674                         fatal("%.200s line %d: Missing argument.", filename, linenum);
675                 if (!key_names_valid2(arg))
676                         fatal("%.200s line %d: Bad protocol 2 host key algorithms '%s'.",
677                             filename, linenum, arg ? arg : "<NONE>");
678                 if (*activep && options->hostkeyalgorithms == NULL)
679                         options->hostkeyalgorithms = xstrdup(arg);
680                 break;
681
682         case oProtocol:
683                 intptr = &options->protocol;
684                 arg = strdelim(&s);
685                 if (!arg || *arg == '\0')
686                         fatal("%.200s line %d: Missing argument.", filename, linenum);
687                 value = proto_spec(arg);
688                 if (value == SSH_PROTO_UNKNOWN)
689                         fatal("%.200s line %d: Bad protocol spec '%s'.",
690                             filename, linenum, arg ? arg : "<NONE>");
691                 if (*activep && *intptr == SSH_PROTO_UNKNOWN)
692                         *intptr = value;
693                 break;
694
695         case oLogLevel:
696                 log_level_ptr = &options->log_level;
697                 arg = strdelim(&s);
698                 value = log_level_number(arg);
699                 if (value == SYSLOG_LEVEL_NOT_SET)
700                         fatal("%.200s line %d: unsupported log level '%s'",
701                             filename, linenum, arg ? arg : "<NONE>");
702                 if (*activep && *log_level_ptr == SYSLOG_LEVEL_NOT_SET)
703                         *log_level_ptr = (LogLevel) value;
704                 break;
705
706         case oLocalForward:
707         case oRemoteForward:
708                 arg = strdelim(&s);
709                 if (arg == NULL || *arg == '\0')
710                         fatal("%.200s line %d: Missing port argument.",
711                             filename, linenum);
712                 arg2 = strdelim(&s);
713                 if (arg2 == NULL || *arg2 == '\0')
714                         fatal("%.200s line %d: Missing target argument.",
715                             filename, linenum);
716
717                 /* construct a string for parse_forward */
718                 snprintf(fwdarg, sizeof(fwdarg), "%s:%s", arg, arg2);
719
720                 if (parse_forward(&fwd, fwdarg) == 0)
721                         fatal("%.200s line %d: Bad forwarding specification.",
722                             filename, linenum);
723
724                 if (*activep) {
725                         if (opcode == oLocalForward)
726                                 add_local_forward(options, &fwd);
727                         else if (opcode == oRemoteForward)
728                                 add_remote_forward(options, &fwd);
729                 }
730                 break;
731
732         case oDynamicForward:
733                 arg = strdelim(&s);
734                 if (!arg || *arg == '\0')
735                         fatal("%.200s line %d: Missing port argument.",
736                             filename, linenum);
737                 memset(&fwd, '\0', sizeof(fwd));
738                 fwd.connect_host = "socks";
739                 fwd.listen_host = hpdelim(&arg);
740                 if (fwd.listen_host == NULL ||
741                     strlen(fwd.listen_host) >= NI_MAXHOST)
742                         fatal("%.200s line %d: Bad forwarding specification.",
743                             filename, linenum);
744                 if (arg) {
745                         fwd.listen_port = a2port(arg);
746                         fwd.listen_host = cleanhostname(fwd.listen_host);
747                 } else {
748                         fwd.listen_port = a2port(fwd.listen_host);
749                         fwd.listen_host = NULL;
750                 }
751                 if (fwd.listen_port == 0)
752                         fatal("%.200s line %d: Badly formatted port number.",
753                             filename, linenum);
754                 if (*activep)
755                         add_local_forward(options, &fwd);
756                 break;
757
758         case oClearAllForwardings:
759                 intptr = &options->clear_forwardings;
760                 goto parse_flag;
761
762         case oHost:
763                 *activep = 0;
764                 while ((arg = strdelim(&s)) != NULL && *arg != '\0')
765                         if (match_pattern(host, arg)) {
766                                 debug("Applying options for %.100s", arg);
767                                 *activep = 1;
768                                 break;
769                         }
770                 /* Avoid garbage check below, as strdelim is done. */
771                 return 0;
772
773         case oEscapeChar:
774                 intptr = &options->escape_char;
775                 arg = strdelim(&s);
776                 if (!arg || *arg == '\0')
777                         fatal("%.200s line %d: Missing argument.", filename, linenum);
778                 if (arg[0] == '^' && arg[2] == 0 &&
779                     (u_char) arg[1] >= 64 && (u_char) arg[1] < 128)
780                         value = (u_char) arg[1] & 31;
781                 else if (strlen(arg) == 1)
782                         value = (u_char) arg[0];
783                 else if (strcmp(arg, "none") == 0)
784                         value = SSH_ESCAPECHAR_NONE;
785                 else {
786                         fatal("%.200s line %d: Bad escape character.",
787                             filename, linenum);
788                         /* NOTREACHED */
789                         value = 0;      /* Avoid compiler warning. */
790                 }
791                 if (*activep && *intptr == -1)
792                         *intptr = value;
793                 break;
794
795         case oAddressFamily:
796                 arg = strdelim(&s);
797                 if (!arg || *arg == '\0')
798                         fatal("%s line %d: missing address family.",
799                             filename, linenum);
800                 intptr = &options->address_family;
801                 if (strcasecmp(arg, "inet") == 0)
802                         value = AF_INET;
803                 else if (strcasecmp(arg, "inet6") == 0)
804                         value = AF_INET6;
805                 else if (strcasecmp(arg, "any") == 0)
806                         value = AF_UNSPEC;
807                 else
808                         fatal("Unsupported AddressFamily \"%s\"", arg);
809                 if (*activep && *intptr == -1)
810                         *intptr = value;
811                 break;
812
813         case oEnableSSHKeysign:
814                 intptr = &options->enable_ssh_keysign;
815                 goto parse_flag;
816
817         case oIdentitiesOnly:
818                 intptr = &options->identities_only;
819                 goto parse_flag;
820
821         case oServerAliveInterval:
822                 intptr = &options->server_alive_interval;
823                 goto parse_time;
824
825         case oServerAliveCountMax:
826                 intptr = &options->server_alive_count_max;
827                 goto parse_int;
828
829         case oSendEnv:
830                 while ((arg = strdelim(&s)) != NULL && *arg != '\0') {
831                         if (strchr(arg, '=') != NULL)
832                                 fatal("%s line %d: Invalid environment name.",
833                                     filename, linenum);
834                         if (!*activep)
835                                 continue;
836                         if (options->num_send_env >= MAX_SEND_ENV)
837                                 fatal("%s line %d: too many send env.",
838                                     filename, linenum);
839                         options->send_env[options->num_send_env++] =
840                             xstrdup(arg);
841                 }
842                 break;
843
844         case oControlPath:
845                 charptr = &options->control_path;
846                 goto parse_string;
847
848         case oControlMaster:
849                 intptr = &options->control_master;
850                 arg = strdelim(&s);
851                 if (!arg || *arg == '\0')
852                         fatal("%.200s line %d: Missing ControlMaster argument.",
853                             filename, linenum);
854                 value = 0;      /* To avoid compiler warning... */
855                 if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0)
856                         value = SSHCTL_MASTER_YES;
857                 else if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0)
858                         value = SSHCTL_MASTER_NO;
859                 else if (strcmp(arg, "auto") == 0)
860                         value = SSHCTL_MASTER_AUTO;
861                 else if (strcmp(arg, "ask") == 0)
862                         value = SSHCTL_MASTER_ASK;
863                 else if (strcmp(arg, "autoask") == 0)
864                         value = SSHCTL_MASTER_AUTO_ASK;
865                 else
866                         fatal("%.200s line %d: Bad ControlMaster argument.",
867                             filename, linenum);
868                 if (*activep && *intptr == -1)
869                         *intptr = value;
870                 break;
871
872         case oHashKnownHosts:
873                 intptr = &options->hash_known_hosts;
874                 goto parse_flag;
875
876         case oTunnel:
877                 intptr = &options->tun_open;
878                 arg = strdelim(&s);
879                 if (!arg || *arg == '\0')
880                         fatal("%s line %d: Missing yes/point-to-point/"
881                             "ethernet/no argument.", filename, linenum);
882                 value = 0;      /* silence compiler */
883                 if (strcasecmp(arg, "ethernet") == 0)
884                         value = SSH_TUNMODE_ETHERNET;
885                 else if (strcasecmp(arg, "point-to-point") == 0)
886                         value = SSH_TUNMODE_POINTOPOINT;
887                 else if (strcasecmp(arg, "yes") == 0)
888                         value = SSH_TUNMODE_DEFAULT;
889                 else if (strcasecmp(arg, "no") == 0)
890                         value = SSH_TUNMODE_NO;
891                 else
892                         fatal("%s line %d: Bad yes/point-to-point/ethernet/"
893                             "no argument: %s", filename, linenum, arg);
894                 if (*activep)
895                         *intptr = value;
896                 break;
897
898         case oTunnelDevice:
899                 arg = strdelim(&s);
900                 if (!arg || *arg == '\0')
901                         fatal("%.200s line %d: Missing argument.", filename, linenum);
902                 value = a2tun(arg, &value2);
903                 if (value == SSH_TUNID_ERR)
904                         fatal("%.200s line %d: Bad tun device.", filename, linenum);
905                 if (*activep) {
906                         options->tun_local = value;
907                         options->tun_remote = value2;
908                 }
909                 break;
910
911         case oLocalCommand:
912                 charptr = &options->local_command;
913                 goto parse_command;
914
915         case oPermitLocalCommand:
916                 intptr = &options->permit_local_command;
917                 goto parse_flag;
918
919         case oDeprecated:
920                 debug("%s line %d: Deprecated option \"%s\"",
921                     filename, linenum, keyword);
922                 return 0;
923
924         case oUnsupported:
925                 error("%s line %d: Unsupported option \"%s\"",
926                     filename, linenum, keyword);
927                 return 0;
928
929         default:
930                 fatal("process_config_line: Unimplemented opcode %d", opcode);
931         }
932
933         /* Check that there is no garbage at end of line. */
934         if ((arg = strdelim(&s)) != NULL && *arg != '\0') {
935                 fatal("%.200s line %d: garbage at end of line; \"%.200s\".",
936                     filename, linenum, arg);
937         }
938         return 0;
939 }
940
941
942 /*
943  * Reads the config file and modifies the options accordingly.  Options
944  * should already be initialized before this call.  This never returns if
945  * there is an error.  If the file does not exist, this returns 0.
946  */
947
948 int
949 read_config_file(const char *filename, const char *host, Options *options,
950     int checkperm)
951 {
952         FILE *f;
953         char line[1024];
954         int active, linenum;
955         int bad_options = 0;
956
957         /* Open the file. */
958         if ((f = fopen(filename, "r")) == NULL)
959                 return 0;
960
961         if (checkperm) {
962                 struct stat sb;
963
964                 if (fstat(fileno(f), &sb) == -1)
965                         fatal("fstat %s: %s", filename, strerror(errno));
966                 if (((sb.st_uid != 0 && sb.st_uid != getuid()) ||
967                     (sb.st_mode & 022) != 0))
968                         fatal("Bad owner or permissions on %s", filename);
969         }
970
971         debug("Reading configuration data %.200s", filename);
972
973         /*
974          * Mark that we are now processing the options.  This flag is turned
975          * on/off by Host specifications.
976          */
977         active = 1;
978         linenum = 0;
979         while (fgets(line, sizeof(line), f)) {
980                 /* Update line number counter. */
981                 linenum++;
982                 if (process_config_line(options, host, line, filename, linenum, &active) != 0)
983                         bad_options++;
984         }
985         fclose(f);
986         if (bad_options > 0)
987                 fatal("%s: terminating, %d bad configuration options",
988                     filename, bad_options);
989         return 1;
990 }
991
992 /*
993  * Initializes options to special values that indicate that they have not yet
994  * been set.  Read_config_file will only set options with this value. Options
995  * are processed in the following order: command line, user config file,
996  * system config file.  Last, fill_default_options is called.
997  */
998
999 void
1000 initialize_options(Options * options)
1001 {
1002         memset(options, 'X', sizeof(*options));
1003         options->forward_agent = -1;
1004         options->forward_x11 = -1;
1005         options->forward_x11_trusted = -1;
1006         options->exit_on_forward_failure = -1;
1007         options->xauth_location = NULL;
1008         options->gateway_ports = -1;
1009         options->use_privileged_port = -1;
1010         options->rsa_authentication = -1;
1011         options->pubkey_authentication = -1;
1012         options->challenge_response_authentication = -1;
1013         options->gss_authentication = -1;
1014         options->gss_deleg_creds = -1;
1015         options->password_authentication = -1;
1016         options->kbd_interactive_authentication = -1;
1017         options->kbd_interactive_devices = NULL;
1018         options->rhosts_rsa_authentication = -1;
1019         options->hostbased_authentication = -1;
1020         options->batch_mode = -1;
1021         options->check_host_ip = -1;
1022         options->strict_host_key_checking = -1;
1023         options->compression = -1;
1024         options->tcp_keep_alive = -1;
1025         options->compression_level = -1;
1026         options->port = -1;
1027         options->address_family = -1;
1028         options->connection_attempts = -1;
1029         options->connection_timeout = -1;
1030         options->number_of_password_prompts = -1;
1031         options->cipher = -1;
1032         options->ciphers = NULL;
1033         options->macs = NULL;
1034         options->hostkeyalgorithms = NULL;
1035         options->protocol = SSH_PROTO_UNKNOWN;
1036         options->num_identity_files = 0;
1037         options->hostname = NULL;
1038         options->host_key_alias = NULL;
1039         options->proxy_command = NULL;
1040         options->user = NULL;
1041         options->escape_char = -1;
1042         options->system_hostfile = NULL;
1043         options->user_hostfile = NULL;
1044         options->system_hostfile2 = NULL;
1045         options->user_hostfile2 = NULL;
1046         options->num_local_forwards = 0;
1047         options->num_remote_forwards = 0;
1048         options->clear_forwardings = -1;
1049         options->log_level = SYSLOG_LEVEL_NOT_SET;
1050         options->preferred_authentications = NULL;
1051         options->bind_address = NULL;
1052         options->smartcard_device = NULL;
1053         options->enable_ssh_keysign = - 1;
1054         options->no_host_authentication_for_localhost = - 1;
1055         options->identities_only = - 1;
1056         options->rekey_limit = - 1;
1057         options->verify_host_key_dns = -1;
1058         options->server_alive_interval = -1;
1059         options->server_alive_count_max = -1;
1060         options->num_send_env = 0;
1061         options->control_path = NULL;
1062         options->control_master = -1;
1063         options->hash_known_hosts = -1;
1064         options->tun_open = -1;
1065         options->tun_local = -1;
1066         options->tun_remote = -1;
1067         options->local_command = NULL;
1068         options->permit_local_command = -1;
1069 }
1070
1071 /*
1072  * Called after processing other sources of option data, this fills those
1073  * options for which no value has been specified with their default values.
1074  */
1075
1076 void
1077 fill_default_options(Options * options)
1078 {
1079         int len;
1080
1081         if (options->forward_agent == -1)
1082                 options->forward_agent = 0;
1083         if (options->forward_x11 == -1)
1084                 options->forward_x11 = 0;
1085         if (options->forward_x11_trusted == -1)
1086                 options->forward_x11_trusted = 0;
1087         if (options->exit_on_forward_failure == -1)
1088                 options->exit_on_forward_failure = 0;
1089         if (options->xauth_location == NULL)
1090                 options->xauth_location = _PATH_XAUTH;
1091         if (options->gateway_ports == -1)
1092                 options->gateway_ports = 0;
1093         if (options->use_privileged_port == -1)
1094                 options->use_privileged_port = 0;
1095         if (options->rsa_authentication == -1)
1096                 options->rsa_authentication = 1;
1097         if (options->pubkey_authentication == -1)
1098                 options->pubkey_authentication = 1;
1099         if (options->challenge_response_authentication == -1)
1100                 options->challenge_response_authentication = 1;
1101         if (options->gss_authentication == -1)
1102                 options->gss_authentication = 0;
1103         if (options->gss_deleg_creds == -1)
1104                 options->gss_deleg_creds = 0;
1105         if (options->password_authentication == -1)
1106                 options->password_authentication = 1;
1107         if (options->kbd_interactive_authentication == -1)
1108                 options->kbd_interactive_authentication = 1;
1109         if (options->rhosts_rsa_authentication == -1)
1110                 options->rhosts_rsa_authentication = 0;
1111         if (options->hostbased_authentication == -1)
1112                 options->hostbased_authentication = 0;
1113         if (options->batch_mode == -1)
1114                 options->batch_mode = 0;
1115         if (options->check_host_ip == -1)
1116                 options->check_host_ip = 1;
1117         if (options->strict_host_key_checking == -1)
1118                 options->strict_host_key_checking = 2;  /* 2 is default */
1119         if (options->compression == -1)
1120                 options->compression = 0;
1121         if (options->tcp_keep_alive == -1)
1122                 options->tcp_keep_alive = 1;
1123         if (options->compression_level == -1)
1124                 options->compression_level = 6;
1125         if (options->port == -1)
1126                 options->port = 0;      /* Filled in ssh_connect. */
1127         if (options->address_family == -1)
1128                 options->address_family = AF_UNSPEC;
1129         if (options->connection_attempts == -1)
1130                 options->connection_attempts = 1;
1131         if (options->number_of_password_prompts == -1)
1132                 options->number_of_password_prompts = 3;
1133         /* Selected in ssh_login(). */
1134         if (options->cipher == -1)
1135                 options->cipher = SSH_CIPHER_NOT_SET;
1136         /* options->ciphers, default set in myproposals.h */
1137         /* options->macs, default set in myproposals.h */
1138         /* options->hostkeyalgorithms, default set in myproposals.h */
1139         if (options->protocol == SSH_PROTO_UNKNOWN)
1140                 options->protocol = SSH_PROTO_1|SSH_PROTO_2;
1141         if (options->num_identity_files == 0) {
1142                 if (options->protocol & SSH_PROTO_1) {
1143                         len = 2 + strlen(_PATH_SSH_CLIENT_IDENTITY) + 1;
1144                         options->identity_files[options->num_identity_files] =
1145                             xmalloc(len);
1146                         snprintf(options->identity_files[options->num_identity_files++],
1147                             len, "~/%.100s", _PATH_SSH_CLIENT_IDENTITY);
1148                 }
1149                 if (options->protocol & SSH_PROTO_2) {
1150                         len = 2 + strlen(_PATH_SSH_CLIENT_ID_RSA) + 1;
1151                         options->identity_files[options->num_identity_files] =
1152                             xmalloc(len);
1153                         snprintf(options->identity_files[options->num_identity_files++],
1154                             len, "~/%.100s", _PATH_SSH_CLIENT_ID_RSA);
1155
1156                         len = 2 + strlen(_PATH_SSH_CLIENT_ID_DSA) + 1;
1157                         options->identity_files[options->num_identity_files] =
1158                             xmalloc(len);
1159                         snprintf(options->identity_files[options->num_identity_files++],
1160                             len, "~/%.100s", _PATH_SSH_CLIENT_ID_DSA);
1161                 }
1162         }
1163         if (options->escape_char == -1)
1164                 options->escape_char = '~';
1165         if (options->system_hostfile == NULL)
1166                 options->system_hostfile = _PATH_SSH_SYSTEM_HOSTFILE;
1167         if (options->user_hostfile == NULL)
1168                 options->user_hostfile = _PATH_SSH_USER_HOSTFILE;
1169         if (options->system_hostfile2 == NULL)
1170                 options->system_hostfile2 = _PATH_SSH_SYSTEM_HOSTFILE2;
1171         if (options->user_hostfile2 == NULL)
1172                 options->user_hostfile2 = _PATH_SSH_USER_HOSTFILE2;
1173         if (options->log_level == SYSLOG_LEVEL_NOT_SET)
1174                 options->log_level = SYSLOG_LEVEL_INFO;
1175         if (options->clear_forwardings == 1)
1176                 clear_forwardings(options);
1177         if (options->no_host_authentication_for_localhost == - 1)
1178                 options->no_host_authentication_for_localhost = 0;
1179         if (options->identities_only == -1)
1180                 options->identities_only = 0;
1181         if (options->enable_ssh_keysign == -1)
1182                 options->enable_ssh_keysign = 0;
1183         if (options->rekey_limit == -1)
1184                 options->rekey_limit = 0;
1185         if (options->verify_host_key_dns == -1)
1186                 options->verify_host_key_dns = 0;
1187         if (options->server_alive_interval == -1)
1188                 options->server_alive_interval = 0;
1189         if (options->server_alive_count_max == -1)
1190                 options->server_alive_count_max = 3;
1191         if (options->control_master == -1)
1192                 options->control_master = 0;
1193         if (options->hash_known_hosts == -1)
1194                 options->hash_known_hosts = 0;
1195         if (options->tun_open == -1)
1196                 options->tun_open = SSH_TUNMODE_NO;
1197         if (options->tun_local == -1)
1198                 options->tun_local = SSH_TUNID_ANY;
1199         if (options->tun_remote == -1)
1200                 options->tun_remote = SSH_TUNID_ANY;
1201         if (options->permit_local_command == -1)
1202                 options->permit_local_command = 0;
1203         /* options->local_command should not be set by default */
1204         /* options->proxy_command should not be set by default */
1205         /* options->user will be set in the main program if appropriate */
1206         /* options->hostname will be set in the main program if appropriate */
1207         /* options->host_key_alias should not be set by default */
1208         /* options->preferred_authentications will be set in ssh */
1209 }
1210
1211 /*
1212  * parse_forward
1213  * parses a string containing a port forwarding specification of the form:
1214  *      [listenhost:]listenport:connecthost:connectport
1215  * returns number of arguments parsed or zero on error
1216  */
1217 int
1218 parse_forward(Forward *fwd, const char *fwdspec)
1219 {
1220         int i;
1221         char *p, *cp, *fwdarg[4];
1222
1223         memset(fwd, '\0', sizeof(*fwd));
1224
1225         cp = p = xstrdup(fwdspec);
1226
1227         /* skip leading spaces */
1228         while (isspace(*cp))
1229                 cp++;
1230
1231         for (i = 0; i < 4; ++i)
1232                 if ((fwdarg[i] = hpdelim(&cp)) == NULL)
1233                         break;
1234
1235         /* Check for trailing garbage in 4-arg case*/
1236         if (cp != NULL)
1237                 i = 0;  /* failure */
1238
1239         switch (i) {
1240         case 3:
1241                 fwd->listen_host = NULL;
1242                 fwd->listen_port = a2port(fwdarg[0]);
1243                 fwd->connect_host = xstrdup(cleanhostname(fwdarg[1]));
1244                 fwd->connect_port = a2port(fwdarg[2]);
1245                 break;
1246
1247         case 4:
1248                 fwd->listen_host = xstrdup(cleanhostname(fwdarg[0]));
1249                 fwd->listen_port = a2port(fwdarg[1]);
1250                 fwd->connect_host = xstrdup(cleanhostname(fwdarg[2]));
1251                 fwd->connect_port = a2port(fwdarg[3]);
1252                 break;
1253         default:
1254                 i = 0; /* failure */
1255         }
1256
1257         xfree(p);
1258
1259         if (fwd->listen_port == 0 || fwd->connect_port == 0)
1260                 goto fail_free;
1261
1262         if (fwd->connect_host != NULL &&
1263             strlen(fwd->connect_host) >= NI_MAXHOST)
1264                 goto fail_free;
1265
1266         return (i);
1267
1268  fail_free:
1269         if (fwd->connect_host != NULL)
1270                 xfree(fwd->connect_host);
1271         if (fwd->listen_host != NULL)
1272                 xfree(fwd->listen_host);
1273         return (0);
1274 }
This page took 0.261487 seconds and 3 git commands to generate.