]> andersk Git - gssapi-openssh.git/blob - openssh/servconf.c
merged OPENSSH_4_2P1_SIMON-20050926-2 to trunk
[gssapi-openssh.git] / openssh / servconf.c
1 /*
2  * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
3  *                    All rights reserved
4  *
5  * As far as I am concerned, the code I have written for this software
6  * can be used freely for any purpose.  Any derived versions of this
7  * software must be clearly marked as such, and if the derived work is
8  * incompatible with the protocol description in the RFC file, it must be
9  * called by a name other than "ssh" or "Secure Shell".
10  */
11
12 #include "includes.h"
13 RCSID("$OpenBSD: servconf.c,v 1.144 2005/08/06 10:03:12 dtucker Exp $");
14
15 #include "ssh.h"
16 #include "log.h"
17 #include "servconf.h"
18 #include "xmalloc.h"
19 #include "compat.h"
20 #include "pathnames.h"
21 #include "misc.h"
22 #include "cipher.h"
23 #include "kex.h"
24 #include "mac.h"
25
26 static void add_listen_addr(ServerOptions *, char *, u_short);
27 static void add_one_listen_addr(ServerOptions *, char *, u_short);
28
29 /* Use of privilege separation or not */
30 extern int use_privsep;
31
32 /* Initializes the server options to their default values. */
33
34 void
35 initialize_server_options(ServerOptions *options)
36 {
37         memset(options, 0, sizeof(*options));
38
39         /* Portable-specific options */
40         options->use_pam = -1;
41
42         /* Standard Options */
43         options->num_ports = 0;
44         options->ports_from_cmdline = 0;
45         options->listen_addrs = NULL;
46         options->address_family = -1;
47         options->num_host_key_files = 0;
48         options->pid_file = NULL;
49         options->server_key_bits = -1;
50         options->login_grace_time = -1;
51         options->key_regeneration_time = -1;
52         options->permit_root_login = PERMIT_NOT_SET;
53         options->ignore_rhosts = -1;
54         options->ignore_user_known_hosts = -1;
55         options->print_motd = -1;
56         options->print_lastlog = -1;
57         options->x11_forwarding = -1;
58         options->x11_display_offset = -1;
59         options->x11_use_localhost = -1;
60         options->xauth_location = NULL;
61         options->strict_modes = -1;
62         options->tcp_keep_alive = -1;
63         options->log_facility = SYSLOG_FACILITY_NOT_SET;
64         options->log_level = SYSLOG_LEVEL_NOT_SET;
65         options->rhosts_rsa_authentication = -1;
66         options->hostbased_authentication = -1;
67         options->hostbased_uses_name_from_packet_only = -1;
68         options->rsa_authentication = -1;
69         options->pubkey_authentication = -1;
70         options->kerberos_authentication = -1;
71         options->kerberos_or_local_passwd = -1;
72         options->kerberos_ticket_cleanup = -1;
73 #ifdef  SESSION_HOOKS
74         options->session_hooks_allow = -1;
75         options->session_hooks_startup_cmd = NULL;
76         options->session_hooks_shutdown_cmd = NULL;
77 #endif
78         options->kerberos_get_afs_token = -1;
79         options->gss_authentication=-1;
80         options->gss_keyex = -1;
81         options->gss_cleanup_creds = -1;
82         options->password_authentication = -1;
83         options->kbd_interactive_authentication = -1;
84         options->challenge_response_authentication = -1;
85         options->permit_empty_passwd = -1;
86         options->permit_user_env = -1;
87         options->use_login = -1;
88         options->compression = -1;
89         options->allow_tcp_forwarding = -1;
90         options->num_allow_users = 0;
91         options->num_deny_users = 0;
92         options->num_allow_groups = 0;
93         options->num_deny_groups = 0;
94         options->ciphers = NULL;
95         options->macs = NULL;
96         options->protocol = SSH_PROTO_UNKNOWN;
97         options->gateway_ports = -1;
98         options->num_subsystems = 0;
99         options->max_startups_begin = -1;
100         options->max_startups_rate = -1;
101         options->max_startups = -1;
102         options->max_authtries = -1;
103         options->banner = NULL;
104         options->use_dns = -1;
105         options->client_alive_interval = -1;
106         options->client_alive_count_max = -1;
107         options->authorized_keys_file = NULL;
108         options->authorized_keys_file2 = NULL;
109         options->num_accept_env = 0;
110
111         /* Needs to be accessable in many places */
112         use_privsep = -1;
113 }
114
115 void
116 fill_default_server_options(ServerOptions *options)
117 {
118         /* Portable-specific options */
119         if (options->use_pam == -1)
120                 options->use_pam = 0;
121
122         /* Standard Options */
123         if (options->protocol == SSH_PROTO_UNKNOWN)
124                 options->protocol = SSH_PROTO_1|SSH_PROTO_2;
125         if (options->num_host_key_files == 0) {
126                 /* fill default hostkeys for protocols */
127                 if (options->protocol & SSH_PROTO_1)
128                         options->host_key_files[options->num_host_key_files++] =
129                             _PATH_HOST_KEY_FILE;
130                 if (options->protocol & SSH_PROTO_2) {
131                         options->host_key_files[options->num_host_key_files++] =
132                             _PATH_HOST_RSA_KEY_FILE;
133                         options->host_key_files[options->num_host_key_files++] =
134                             _PATH_HOST_DSA_KEY_FILE;
135                 }
136         }
137         if (options->num_ports == 0)
138                 options->ports[options->num_ports++] = SSH_DEFAULT_PORT;
139         if (options->listen_addrs == NULL)
140                 add_listen_addr(options, NULL, 0);
141         if (options->pid_file == NULL)
142                 options->pid_file = _PATH_SSH_DAEMON_PID_FILE;
143         if (options->server_key_bits == -1)
144                 options->server_key_bits = 768;
145         if (options->login_grace_time == -1)
146                 options->login_grace_time = 120;
147         if (options->key_regeneration_time == -1)
148                 options->key_regeneration_time = 3600;
149         if (options->permit_root_login == PERMIT_NOT_SET)
150                 options->permit_root_login = PERMIT_YES;
151         if (options->ignore_rhosts == -1)
152                 options->ignore_rhosts = 1;
153         if (options->ignore_user_known_hosts == -1)
154                 options->ignore_user_known_hosts = 0;
155         if (options->print_motd == -1)
156                 options->print_motd = 1;
157         if (options->print_lastlog == -1)
158                 options->print_lastlog = 1;
159         if (options->x11_forwarding == -1)
160                 options->x11_forwarding = 0;
161         if (options->x11_display_offset == -1)
162                 options->x11_display_offset = 10;
163         if (options->x11_use_localhost == -1)
164                 options->x11_use_localhost = 1;
165         if (options->xauth_location == NULL)
166                 options->xauth_location = _PATH_XAUTH;
167         if (options->strict_modes == -1)
168                 options->strict_modes = 1;
169         if (options->tcp_keep_alive == -1)
170                 options->tcp_keep_alive = 1;
171         if (options->log_facility == SYSLOG_FACILITY_NOT_SET)
172                 options->log_facility = SYSLOG_FACILITY_AUTH;
173         if (options->log_level == SYSLOG_LEVEL_NOT_SET)
174                 options->log_level = SYSLOG_LEVEL_INFO;
175         if (options->rhosts_rsa_authentication == -1)
176                 options->rhosts_rsa_authentication = 0;
177         if (options->hostbased_authentication == -1)
178                 options->hostbased_authentication = 0;
179         if (options->hostbased_uses_name_from_packet_only == -1)
180                 options->hostbased_uses_name_from_packet_only = 0;
181         if (options->rsa_authentication == -1)
182                 options->rsa_authentication = 1;
183         if (options->pubkey_authentication == -1)
184                 options->pubkey_authentication = 1;
185         if (options->kerberos_authentication == -1)
186                 options->kerberos_authentication = 0;
187         if (options->kerberos_or_local_passwd == -1)
188                 options->kerberos_or_local_passwd = 1;
189         if (options->kerberos_ticket_cleanup == -1)
190                 options->kerberos_ticket_cleanup = 1;
191         if (options->kerberos_get_afs_token == -1)
192                 options->kerberos_get_afs_token = 0;
193         if (options->gss_authentication == -1)
194                 options->gss_authentication = 1;
195         if (options->gss_keyex == -1)
196                 options->gss_keyex = 1;
197         if (options->gss_cleanup_creds == -1)
198                 options->gss_cleanup_creds = 1;
199         if (options->password_authentication == -1)
200                 options->password_authentication = 1;
201         if (options->kbd_interactive_authentication == -1)
202                 options->kbd_interactive_authentication = 0;
203         if (options->challenge_response_authentication == -1)
204                 options->challenge_response_authentication = 1;
205         if (options->permit_empty_passwd == -1)
206                 options->permit_empty_passwd = 0;
207         if (options->permit_user_env == -1)
208                 options->permit_user_env = 0;
209         if (options->use_login == -1)
210                 options->use_login = 0;
211         if (options->compression == -1)
212                 options->compression = COMP_DELAYED;
213         if (options->allow_tcp_forwarding == -1)
214                 options->allow_tcp_forwarding = 1;
215         if (options->gateway_ports == -1)
216                 options->gateway_ports = 0;
217         if (options->max_startups == -1)
218                 options->max_startups = 10;
219         if (options->max_startups_rate == -1)
220                 options->max_startups_rate = 100;               /* 100% */
221         if (options->max_startups_begin == -1)
222                 options->max_startups_begin = options->max_startups;
223         if (options->max_authtries == -1)
224                 options->max_authtries = DEFAULT_AUTH_FAIL_MAX;
225         if (options->use_dns == -1)
226                 options->use_dns = 1;
227         if (options->client_alive_interval == -1)
228                 options->client_alive_interval = 0;
229         if (options->client_alive_count_max == -1)
230                 options->client_alive_count_max = 3;
231         if (options->authorized_keys_file2 == NULL) {
232                 /* authorized_keys_file2 falls back to authorized_keys_file */
233                 if (options->authorized_keys_file != NULL)
234                         options->authorized_keys_file2 = options->authorized_keys_file;
235                 else
236                         options->authorized_keys_file2 = _PATH_SSH_USER_PERMITTED_KEYS2;
237         }
238         if (options->authorized_keys_file == NULL)
239                 options->authorized_keys_file = _PATH_SSH_USER_PERMITTED_KEYS;
240
241         /* Turn privilege separation on by default */
242         if (use_privsep == -1)
243                 use_privsep = 1;
244
245 #ifndef HAVE_MMAP
246         if (use_privsep && options->compression == 1) {
247                 error("This platform does not support both privilege "
248                     "separation and compression");
249                 error("Compression disabled");
250                 options->compression = 0;
251         }
252 #endif
253
254 }
255
256 /* Keyword tokens. */
257 typedef enum {
258         sBadOption,             /* == unknown option */
259         /* Portable-specific options */
260         sUsePAM,
261         /* Standard Options */
262         sPort, sHostKeyFile, sServerKeyBits, sLoginGraceTime, sKeyRegenerationTime,
263         sPermitRootLogin, sLogFacility, sLogLevel,
264         sRhostsRSAAuthentication, sRSAAuthentication,
265         sKerberosAuthentication, sKerberosOrLocalPasswd, sKerberosTicketCleanup,
266         sKerberosGetAFSToken,
267         sKerberosTgtPassing, sChallengeResponseAuthentication,
268 #ifdef SESSION_HOOKS
269         sAllowSessionHooks, sSessionHookStartupCmd, sSessionHookShutdownCmd,
270 #endif
271         sPasswordAuthentication, sKbdInteractiveAuthentication,
272         sListenAddress, sAddressFamily,
273         sPrintMotd, sPrintLastLog, sIgnoreRhosts,
274         sX11Forwarding, sX11DisplayOffset, sX11UseLocalhost,
275         sStrictModes, sEmptyPasswd, sTCPKeepAlive,
276         sPermitUserEnvironment, sUseLogin, sAllowTcpForwarding, sCompression,
277         sAllowUsers, sDenyUsers, sAllowGroups, sDenyGroups,
278         sIgnoreUserKnownHosts, sCiphers, sMacs, sProtocol, sPidFile,
279         sGatewayPorts, sPubkeyAuthentication, sXAuthLocation, sSubsystem,
280         sMaxStartups, sMaxAuthTries,
281         sBanner, sUseDNS, sHostbasedAuthentication,
282         sHostbasedUsesNameFromPacketOnly, sClientAliveInterval,
283         sClientAliveCountMax, sAuthorizedKeysFile, sAuthorizedKeysFile2,
284         sGssAuthentication, sGssKeyEx, sGssCleanupCreds, sAcceptEnv,
285         sUsePrivilegeSeparation,
286         sDeprecated, sUnsupported
287 } ServerOpCodes;
288
289 /* Textual representation of the tokens. */
290 static struct {
291         const char *name;
292         ServerOpCodes opcode;
293 } keywords[] = {
294         /* Portable-specific options */
295 #ifdef USE_PAM
296         { "usepam", sUsePAM },
297 #else
298         { "usepam", sUnsupported },
299 #endif
300         { "pamauthenticationviakbdint", sDeprecated },
301         /* Standard Options */
302         { "port", sPort },
303         { "hostkey", sHostKeyFile },
304         { "hostdsakey", sHostKeyFile },                                 /* alias */
305         { "pidfile", sPidFile },
306         { "serverkeybits", sServerKeyBits },
307         { "logingracetime", sLoginGraceTime },
308         { "keyregenerationinterval", sKeyRegenerationTime },
309         { "permitrootlogin", sPermitRootLogin },
310         { "syslogfacility", sLogFacility },
311         { "loglevel", sLogLevel },
312         { "rhostsauthentication", sDeprecated },
313         { "rhostsrsaauthentication", sRhostsRSAAuthentication },
314         { "hostbasedauthentication", sHostbasedAuthentication },
315         { "hostbasedusesnamefrompacketonly", sHostbasedUsesNameFromPacketOnly },
316         { "rsaauthentication", sRSAAuthentication },
317         { "pubkeyauthentication", sPubkeyAuthentication },
318         { "dsaauthentication", sPubkeyAuthentication },                 /* alias */
319 #ifdef KRB5
320         { "kerberosauthentication", sKerberosAuthentication },
321         { "kerberosorlocalpasswd", sKerberosOrLocalPasswd },
322         { "kerberosticketcleanup", sKerberosTicketCleanup },
323 #ifdef USE_AFS
324         { "kerberosgetafstoken", sKerberosGetAFSToken },
325 #else
326         { "kerberosgetafstoken", sUnsupported },
327 #endif
328 #else
329         { "kerberosauthentication", sUnsupported },
330         { "kerberosorlocalpasswd", sUnsupported },
331         { "kerberosticketcleanup", sUnsupported },
332         { "kerberosgetafstoken", sUnsupported },
333 #endif
334         { "kerberostgtpassing", sUnsupported },
335         { "afstokenpassing", sUnsupported },
336 #ifdef GSSAPI
337         { "gssapiauthentication", sGssAuthentication },
338         { "gssapikeyexchange", sGssKeyEx },
339         { "gssapicleanupcredentials", sGssCleanupCreds },
340 #else
341         { "gssapiauthentication", sUnsupported },
342         { "gssapikeyexchange", sUnsupported },
343         { "gssapicleanupcredentials", sUnsupported },
344 #endif
345 #ifdef SESSION_HOOKS
346         { "allowsessionhooks", sAllowSessionHooks },
347         { "sessionhookstartupcmd", sSessionHookStartupCmd },
348         { "sessionhookshutdowncmd", sSessionHookShutdownCmd },
349 #endif        
350         { "passwordauthentication", sPasswordAuthentication },
351         { "kbdinteractiveauthentication", sKbdInteractiveAuthentication },
352         { "challengeresponseauthentication", sChallengeResponseAuthentication },
353         { "skeyauthentication", sChallengeResponseAuthentication }, /* alias */
354         { "checkmail", sDeprecated },
355         { "listenaddress", sListenAddress },
356         { "addressfamily", sAddressFamily },
357         { "printmotd", sPrintMotd },
358         { "printlastlog", sPrintLastLog },
359         { "ignorerhosts", sIgnoreRhosts },
360         { "ignoreuserknownhosts", sIgnoreUserKnownHosts },
361         { "x11forwarding", sX11Forwarding },
362         { "x11displayoffset", sX11DisplayOffset },
363         { "x11uselocalhost", sX11UseLocalhost },
364         { "xauthlocation", sXAuthLocation },
365         { "strictmodes", sStrictModes },
366         { "permitemptypasswords", sEmptyPasswd },
367         { "permituserenvironment", sPermitUserEnvironment },
368         { "uselogin", sUseLogin },
369         { "compression", sCompression },
370         { "tcpkeepalive", sTCPKeepAlive },
371         { "keepalive", sTCPKeepAlive },                         /* obsolete alias */
372         { "allowtcpforwarding", sAllowTcpForwarding },
373         { "allowusers", sAllowUsers },
374         { "denyusers", sDenyUsers },
375         { "allowgroups", sAllowGroups },
376         { "denygroups", sDenyGroups },
377         { "ciphers", sCiphers },
378         { "macs", sMacs },
379         { "protocol", sProtocol },
380         { "gatewayports", sGatewayPorts },
381         { "subsystem", sSubsystem },
382         { "maxstartups", sMaxStartups },
383         { "maxauthtries", sMaxAuthTries },
384         { "banner", sBanner },
385         { "usedns", sUseDNS },
386         { "verifyreversemapping", sDeprecated },
387         { "reversemappingcheck", sDeprecated },
388         { "clientaliveinterval", sClientAliveInterval },
389         { "clientalivecountmax", sClientAliveCountMax },
390         { "authorizedkeysfile", sAuthorizedKeysFile },
391         { "authorizedkeysfile2", sAuthorizedKeysFile2 },
392         { "useprivilegeseparation", sUsePrivilegeSeparation},
393         { "acceptenv", sAcceptEnv },
394         { NULL, sBadOption }
395 };
396
397 /*
398  * Returns the number of the token pointed to by cp or sBadOption.
399  */
400
401 static ServerOpCodes
402 parse_token(const char *cp, const char *filename,
403             int linenum)
404 {
405         u_int i;
406
407         for (i = 0; keywords[i].name; i++)
408                 if (strcasecmp(cp, keywords[i].name) == 0)
409                         return keywords[i].opcode;
410
411         error("%s: line %d: Bad configuration option: %s",
412             filename, linenum, cp);
413         return sBadOption;
414 }
415
416 static void
417 add_listen_addr(ServerOptions *options, char *addr, u_short port)
418 {
419         u_int i;
420
421         if (options->num_ports == 0)
422                 options->ports[options->num_ports++] = SSH_DEFAULT_PORT;
423         if (options->address_family == -1)
424                 options->address_family = AF_UNSPEC;
425         if (port == 0)
426                 for (i = 0; i < options->num_ports; i++)
427                         add_one_listen_addr(options, addr, options->ports[i]);
428         else
429                 add_one_listen_addr(options, addr, port);
430 }
431
432 static void
433 add_one_listen_addr(ServerOptions *options, char *addr, u_short port)
434 {
435         struct addrinfo hints, *ai, *aitop;
436         char strport[NI_MAXSERV];
437         int gaierr;
438
439         memset(&hints, 0, sizeof(hints));
440         hints.ai_family = options->address_family;
441         hints.ai_socktype = SOCK_STREAM;
442         hints.ai_flags = (addr == NULL) ? AI_PASSIVE : 0;
443         snprintf(strport, sizeof strport, "%u", port);
444         if ((gaierr = getaddrinfo(addr, strport, &hints, &aitop)) != 0)
445                 fatal("bad addr or host: %s (%s)",
446                     addr ? addr : "<NULL>",
447                     gai_strerror(gaierr));
448         for (ai = aitop; ai->ai_next; ai = ai->ai_next)
449                 ;
450         ai->ai_next = options->listen_addrs;
451         options->listen_addrs = aitop;
452 }
453
454 int
455 process_server_config_line(ServerOptions *options, char *line,
456     const char *filename, int linenum)
457 {
458         char *cp, **charptr, *arg, *p;
459         int *intptr, value, n;
460         ServerOpCodes opcode;
461         u_short port;
462         u_int i;
463
464         cp = line;
465         arg = strdelim(&cp);
466         /* Ignore leading whitespace */
467         if (*arg == '\0')
468                 arg = strdelim(&cp);
469         if (!arg || !*arg || *arg == '#')
470                 return 0;
471         intptr = NULL;
472         charptr = NULL;
473         opcode = parse_token(arg, filename, linenum);
474         switch (opcode) {
475         /* Portable-specific options */
476         case sUsePAM:
477                 intptr = &options->use_pam;
478                 goto parse_flag;
479
480         /* Standard Options */
481         case sBadOption:
482                 return -1;
483         case sPort:
484                 /* ignore ports from configfile if cmdline specifies ports */
485                 if (options->ports_from_cmdline)
486                         return 0;
487                 if (options->listen_addrs != NULL)
488                         fatal("%s line %d: ports must be specified before "
489                             "ListenAddress.", filename, linenum);
490                 if (options->num_ports >= MAX_PORTS)
491                         fatal("%s line %d: too many ports.",
492                             filename, linenum);
493                 arg = strdelim(&cp);
494                 if (!arg || *arg == '\0')
495                         fatal("%s line %d: missing port number.",
496                             filename, linenum);
497                 options->ports[options->num_ports++] = a2port(arg);
498                 if (options->ports[options->num_ports-1] == 0)
499                         fatal("%s line %d: Badly formatted port number.",
500                             filename, linenum);
501                 break;
502
503         case sServerKeyBits:
504                 intptr = &options->server_key_bits;
505 parse_int:
506                 arg = strdelim(&cp);
507                 if (!arg || *arg == '\0')
508                         fatal("%s line %d: missing integer value.",
509                             filename, linenum);
510                 value = atoi(arg);
511                 if (*intptr == -1)
512                         *intptr = value;
513                 break;
514
515         case sLoginGraceTime:
516                 intptr = &options->login_grace_time;
517 parse_time:
518                 arg = strdelim(&cp);
519                 if (!arg || *arg == '\0')
520                         fatal("%s line %d: missing time value.",
521                             filename, linenum);
522                 if ((value = convtime(arg)) == -1)
523                         fatal("%s line %d: invalid time value.",
524                             filename, linenum);
525                 if (*intptr == -1)
526                         *intptr = value;
527                 break;
528
529         case sKeyRegenerationTime:
530                 intptr = &options->key_regeneration_time;
531                 goto parse_time;
532
533         case sListenAddress:
534                 arg = strdelim(&cp);
535                 if (arg == NULL || *arg == '\0')
536                         fatal("%s line %d: missing address",
537                             filename, linenum);
538                 /* check for bare IPv6 address: no "[]" and 2 or more ":" */
539                 if (strchr(arg, '[') == NULL && (p = strchr(arg, ':')) != NULL
540                     && strchr(p+1, ':') != NULL) {
541                         add_listen_addr(options, arg, 0);
542                         break;
543                 }
544                 p = hpdelim(&arg);
545                 if (p == NULL)
546                         fatal("%s line %d: bad address:port usage",
547                             filename, linenum);
548                 p = cleanhostname(p);
549                 if (arg == NULL)
550                         port = 0;
551                 else if ((port = a2port(arg)) == 0)
552                         fatal("%s line %d: bad port number", filename, linenum);
553
554                 add_listen_addr(options, p, port);
555
556                 break;
557
558         case sAddressFamily:
559                 arg = strdelim(&cp);
560                 if (!arg || *arg == '\0')
561                         fatal("%s line %d: missing address family.",
562                             filename, linenum);
563                 intptr = &options->address_family;
564                 if (options->listen_addrs != NULL)
565                         fatal("%s line %d: address family must be specified before "
566                             "ListenAddress.", filename, linenum);
567                 if (strcasecmp(arg, "inet") == 0)
568                         value = AF_INET;
569                 else if (strcasecmp(arg, "inet6") == 0)
570                         value = AF_INET6;
571                 else if (strcasecmp(arg, "any") == 0)
572                         value = AF_UNSPEC;
573                 else
574                         fatal("%s line %d: unsupported address family \"%s\".",
575                             filename, linenum, arg);
576                 if (*intptr == -1)
577                         *intptr = value;
578                 break;
579
580         case sHostKeyFile:
581                 intptr = &options->num_host_key_files;
582                 if (*intptr >= MAX_HOSTKEYS)
583                         fatal("%s line %d: too many host keys specified (max %d).",
584                             filename, linenum, MAX_HOSTKEYS);
585                 charptr = &options->host_key_files[*intptr];
586 parse_filename:
587                 arg = strdelim(&cp);
588                 if (!arg || *arg == '\0')
589                         fatal("%s line %d: missing file name.",
590                             filename, linenum);
591                 if (*charptr == NULL) {
592                         *charptr = tilde_expand_filename(arg, getuid());
593                         /* increase optional counter */
594                         if (intptr != NULL)
595                                 *intptr = *intptr + 1;
596                 }
597                 break;
598
599         case sPidFile:
600                 charptr = &options->pid_file;
601                 goto parse_filename;
602
603         case sPermitRootLogin:
604                 intptr = &options->permit_root_login;
605                 arg = strdelim(&cp);
606                 if (!arg || *arg == '\0')
607                         fatal("%s line %d: missing yes/"
608                             "without-password/forced-commands-only/no "
609                             "argument.", filename, linenum);
610                 value = 0;      /* silence compiler */
611                 if (strcmp(arg, "without-password") == 0)
612                         value = PERMIT_NO_PASSWD;
613                 else if (strcmp(arg, "forced-commands-only") == 0)
614                         value = PERMIT_FORCED_ONLY;
615                 else if (strcmp(arg, "yes") == 0)
616                         value = PERMIT_YES;
617                 else if (strcmp(arg, "no") == 0)
618                         value = PERMIT_NO;
619                 else
620                         fatal("%s line %d: Bad yes/"
621                             "without-password/forced-commands-only/no "
622                             "argument: %s", filename, linenum, arg);
623                 if (*intptr == -1)
624                         *intptr = value;
625                 break;
626
627         case sIgnoreRhosts:
628                 intptr = &options->ignore_rhosts;
629 parse_flag:
630                 arg = strdelim(&cp);
631                 if (!arg || *arg == '\0')
632                         fatal("%s line %d: missing yes/no argument.",
633                             filename, linenum);
634                 value = 0;      /* silence compiler */
635                 if (strcmp(arg, "yes") == 0)
636                         value = 1;
637                 else if (strcmp(arg, "no") == 0)
638                         value = 0;
639                 else
640                         fatal("%s line %d: Bad yes/no argument: %s",
641                                 filename, linenum, arg);
642                 if (*intptr == -1)
643                         *intptr = value;
644                 break;
645
646         case sIgnoreUserKnownHosts:
647                 intptr = &options->ignore_user_known_hosts;
648                 goto parse_flag;
649
650         case sRhostsRSAAuthentication:
651                 intptr = &options->rhosts_rsa_authentication;
652                 goto parse_flag;
653
654         case sHostbasedAuthentication:
655                 intptr = &options->hostbased_authentication;
656                 goto parse_flag;
657
658         case sHostbasedUsesNameFromPacketOnly:
659                 intptr = &options->hostbased_uses_name_from_packet_only;
660                 goto parse_flag;
661
662         case sRSAAuthentication:
663                 intptr = &options->rsa_authentication;
664                 goto parse_flag;
665
666         case sPubkeyAuthentication:
667                 intptr = &options->pubkey_authentication;
668                 goto parse_flag;
669
670         case sKerberosAuthentication:
671                 intptr = &options->kerberos_authentication;
672                 goto parse_flag;
673
674         case sKerberosOrLocalPasswd:
675                 intptr = &options->kerberos_or_local_passwd;
676                 goto parse_flag;
677
678         case sKerberosTicketCleanup:
679                 intptr = &options->kerberos_ticket_cleanup;
680                 goto parse_flag;
681
682         case sKerberosGetAFSToken:
683                 intptr = &options->kerberos_get_afs_token;
684                 goto parse_flag;
685
686         case sGssAuthentication:
687                 intptr = &options->gss_authentication;
688                 goto parse_flag;
689
690         case sGssKeyEx:
691                 intptr = &options->gss_keyex;
692                 goto parse_flag;
693
694         case sGssCleanupCreds:
695                 intptr = &options->gss_cleanup_creds;
696                 goto parse_flag;
697
698 #ifdef SESSION_HOOKS
699         case sAllowSessionHooks:
700                 intptr = &options->session_hooks_allow;
701                 goto parse_flag;
702         case sSessionHookStartupCmd:
703         case sSessionHookShutdownCmd:
704                 arg = strdelim(&cp);
705                 if (!arg || *arg == '\0')
706                     fatal("%s line %d: empty session hook command",
707                           filename, linenum);
708                 if (opcode==sSessionHookStartupCmd)
709                     options->session_hooks_startup_cmd = strdup(arg);
710                 else
711                     options->session_hooks_shutdown_cmd = strdup(arg);
712                 break;
713 #endif                  
714
715         case sPasswordAuthentication:
716                 intptr = &options->password_authentication;
717                 goto parse_flag;
718
719         case sKbdInteractiveAuthentication:
720                 intptr = &options->kbd_interactive_authentication;
721                 goto parse_flag;
722
723         case sChallengeResponseAuthentication:
724                 intptr = &options->challenge_response_authentication;
725                 goto parse_flag;
726
727         case sPrintMotd:
728                 intptr = &options->print_motd;
729                 goto parse_flag;
730
731         case sPrintLastLog:
732                 intptr = &options->print_lastlog;
733                 goto parse_flag;
734
735         case sX11Forwarding:
736                 intptr = &options->x11_forwarding;
737                 goto parse_flag;
738
739         case sX11DisplayOffset:
740                 intptr = &options->x11_display_offset;
741                 goto parse_int;
742
743         case sX11UseLocalhost:
744                 intptr = &options->x11_use_localhost;
745                 goto parse_flag;
746
747         case sXAuthLocation:
748                 charptr = &options->xauth_location;
749                 goto parse_filename;
750
751         case sStrictModes:
752                 intptr = &options->strict_modes;
753                 goto parse_flag;
754
755         case sTCPKeepAlive:
756                 intptr = &options->tcp_keep_alive;
757                 goto parse_flag;
758
759         case sEmptyPasswd:
760                 intptr = &options->permit_empty_passwd;
761                 goto parse_flag;
762
763         case sPermitUserEnvironment:
764                 intptr = &options->permit_user_env;
765                 goto parse_flag;
766
767         case sUseLogin:
768                 intptr = &options->use_login;
769                 goto parse_flag;
770
771         case sCompression:
772                 intptr = &options->compression;
773                 arg = strdelim(&cp);
774                 if (!arg || *arg == '\0')
775                         fatal("%s line %d: missing yes/no/delayed "
776                             "argument.", filename, linenum);
777                 value = 0;      /* silence compiler */
778                 if (strcmp(arg, "delayed") == 0)
779                         value = COMP_DELAYED;
780                 else if (strcmp(arg, "yes") == 0)
781                         value = COMP_ZLIB;
782                 else if (strcmp(arg, "no") == 0)
783                         value = COMP_NONE;
784                 else
785                         fatal("%s line %d: Bad yes/no/delayed "
786                             "argument: %s", filename, linenum, arg);
787                 if (*intptr == -1)
788                         *intptr = value;
789                 break;
790
791         case sGatewayPorts:
792                 intptr = &options->gateway_ports;
793                 arg = strdelim(&cp);
794                 if (!arg || *arg == '\0')
795                         fatal("%s line %d: missing yes/no/clientspecified "
796                             "argument.", filename, linenum);
797                 value = 0;      /* silence compiler */
798                 if (strcmp(arg, "clientspecified") == 0)
799                         value = 2;
800                 else if (strcmp(arg, "yes") == 0)
801                         value = 1;
802                 else if (strcmp(arg, "no") == 0)
803                         value = 0;
804                 else
805                         fatal("%s line %d: Bad yes/no/clientspecified "
806                             "argument: %s", filename, linenum, arg);
807                 if (*intptr == -1)
808                         *intptr = value;
809                 break;
810
811         case sUseDNS:
812                 intptr = &options->use_dns;
813                 goto parse_flag;
814
815         case sLogFacility:
816                 intptr = (int *) &options->log_facility;
817                 arg = strdelim(&cp);
818                 value = log_facility_number(arg);
819                 if (value == SYSLOG_FACILITY_NOT_SET)
820                         fatal("%.200s line %d: unsupported log facility '%s'",
821                             filename, linenum, arg ? arg : "<NONE>");
822                 if (*intptr == -1)
823                         *intptr = (SyslogFacility) value;
824                 break;
825
826         case sLogLevel:
827                 intptr = (int *) &options->log_level;
828                 arg = strdelim(&cp);
829                 value = log_level_number(arg);
830                 if (value == SYSLOG_LEVEL_NOT_SET)
831                         fatal("%.200s line %d: unsupported log level '%s'",
832                             filename, linenum, arg ? arg : "<NONE>");
833                 if (*intptr == -1)
834                         *intptr = (LogLevel) value;
835                 break;
836
837         case sAllowTcpForwarding:
838                 intptr = &options->allow_tcp_forwarding;
839                 goto parse_flag;
840
841         case sUsePrivilegeSeparation:
842                 intptr = &use_privsep;
843                 goto parse_flag;
844
845         case sAllowUsers:
846                 while ((arg = strdelim(&cp)) && *arg != '\0') {
847                         if (options->num_allow_users >= MAX_ALLOW_USERS)
848                                 fatal("%s line %d: too many allow users.",
849                                     filename, linenum);
850                         options->allow_users[options->num_allow_users++] =
851                             xstrdup(arg);
852                 }
853                 break;
854
855         case sDenyUsers:
856                 while ((arg = strdelim(&cp)) && *arg != '\0') {
857                         if (options->num_deny_users >= MAX_DENY_USERS)
858                                 fatal( "%s line %d: too many deny users.",
859                                     filename, linenum);
860                         options->deny_users[options->num_deny_users++] =
861                             xstrdup(arg);
862                 }
863                 break;
864
865         case sAllowGroups:
866                 while ((arg = strdelim(&cp)) && *arg != '\0') {
867                         if (options->num_allow_groups >= MAX_ALLOW_GROUPS)
868                                 fatal("%s line %d: too many allow groups.",
869                                     filename, linenum);
870                         options->allow_groups[options->num_allow_groups++] =
871                             xstrdup(arg);
872                 }
873                 break;
874
875         case sDenyGroups:
876                 while ((arg = strdelim(&cp)) && *arg != '\0') {
877                         if (options->num_deny_groups >= MAX_DENY_GROUPS)
878                                 fatal("%s line %d: too many deny groups.",
879                                     filename, linenum);
880                         options->deny_groups[options->num_deny_groups++] = xstrdup(arg);
881                 }
882                 break;
883
884         case sCiphers:
885                 arg = strdelim(&cp);
886                 if (!arg || *arg == '\0')
887                         fatal("%s line %d: Missing argument.", filename, linenum);
888                 if (!ciphers_valid(arg))
889                         fatal("%s line %d: Bad SSH2 cipher spec '%s'.",
890                             filename, linenum, arg ? arg : "<NONE>");
891                 if (options->ciphers == NULL)
892                         options->ciphers = xstrdup(arg);
893                 break;
894
895         case sMacs:
896                 arg = strdelim(&cp);
897                 if (!arg || *arg == '\0')
898                         fatal("%s line %d: Missing argument.", filename, linenum);
899                 if (!mac_valid(arg))
900                         fatal("%s line %d: Bad SSH2 mac spec '%s'.",
901                             filename, linenum, arg ? arg : "<NONE>");
902                 if (options->macs == NULL)
903                         options->macs = xstrdup(arg);
904                 break;
905
906         case sProtocol:
907                 intptr = &options->protocol;
908                 arg = strdelim(&cp);
909                 if (!arg || *arg == '\0')
910                         fatal("%s line %d: Missing argument.", filename, linenum);
911                 value = proto_spec(arg);
912                 if (value == SSH_PROTO_UNKNOWN)
913                         fatal("%s line %d: Bad protocol spec '%s'.",
914                             filename, linenum, arg ? arg : "<NONE>");
915                 if (*intptr == SSH_PROTO_UNKNOWN)
916                         *intptr = value;
917                 break;
918
919         case sSubsystem:
920                 if (options->num_subsystems >= MAX_SUBSYSTEMS) {
921                         fatal("%s line %d: too many subsystems defined.",
922                             filename, linenum);
923                 }
924                 arg = strdelim(&cp);
925                 if (!arg || *arg == '\0')
926                         fatal("%s line %d: Missing subsystem name.",
927                             filename, linenum);
928                 for (i = 0; i < options->num_subsystems; i++)
929                         if (strcmp(arg, options->subsystem_name[i]) == 0)
930                                 fatal("%s line %d: Subsystem '%s' already defined.",
931                                     filename, linenum, arg);
932                 options->subsystem_name[options->num_subsystems] = xstrdup(arg);
933                 arg = strdelim(&cp);
934                 if (!arg || *arg == '\0')
935                         fatal("%s line %d: Missing subsystem command.",
936                             filename, linenum);
937                 options->subsystem_command[options->num_subsystems] = xstrdup(arg);
938                 options->num_subsystems++;
939                 break;
940
941         case sMaxStartups:
942                 arg = strdelim(&cp);
943                 if (!arg || *arg == '\0')
944                         fatal("%s line %d: Missing MaxStartups spec.",
945                             filename, linenum);
946                 if ((n = sscanf(arg, "%d:%d:%d",
947                     &options->max_startups_begin,
948                     &options->max_startups_rate,
949                     &options->max_startups)) == 3) {
950                         if (options->max_startups_begin >
951                             options->max_startups ||
952                             options->max_startups_rate > 100 ||
953                             options->max_startups_rate < 1)
954                                 fatal("%s line %d: Illegal MaxStartups spec.",
955                                     filename, linenum);
956                 } else if (n != 1)
957                         fatal("%s line %d: Illegal MaxStartups spec.",
958                             filename, linenum);
959                 else
960                         options->max_startups = options->max_startups_begin;
961                 break;
962
963         case sMaxAuthTries:
964                 intptr = &options->max_authtries;
965                 goto parse_int;
966
967         case sBanner:
968                 charptr = &options->banner;
969                 goto parse_filename;
970         /*
971          * These options can contain %X options expanded at
972          * connect time, so that you can specify paths like:
973          *
974          * AuthorizedKeysFile   /etc/ssh_keys/%u
975          */
976         case sAuthorizedKeysFile:
977         case sAuthorizedKeysFile2:
978                 charptr = (opcode == sAuthorizedKeysFile ) ?
979                     &options->authorized_keys_file :
980                     &options->authorized_keys_file2;
981                 goto parse_filename;
982
983         case sClientAliveInterval:
984                 intptr = &options->client_alive_interval;
985                 goto parse_time;
986
987         case sClientAliveCountMax:
988                 intptr = &options->client_alive_count_max;
989                 goto parse_int;
990
991         case sAcceptEnv:
992                 while ((arg = strdelim(&cp)) && *arg != '\0') {
993                         if (strchr(arg, '=') != NULL)
994                                 fatal("%s line %d: Invalid environment name.",
995                                     filename, linenum);
996                         if (options->num_accept_env >= MAX_ACCEPT_ENV)
997                                 fatal("%s line %d: too many allow env.",
998                                     filename, linenum);
999                         options->accept_env[options->num_accept_env++] =
1000                             xstrdup(arg);
1001                 }
1002                 break;
1003
1004         case sDeprecated:
1005                 logit("%s line %d: Deprecated option %s",
1006                     filename, linenum, arg);
1007                 while (arg)
1008                     arg = strdelim(&cp);
1009                 break;
1010
1011         case sUnsupported:
1012                 logit("%s line %d: Unsupported option %s",
1013                     filename, linenum, arg);
1014                 while (arg)
1015                     arg = strdelim(&cp);
1016                 break;
1017
1018         default:
1019                 fatal("%s line %d: Missing handler for opcode %s (%d)",
1020                     filename, linenum, arg, opcode);
1021         }
1022         if ((arg = strdelim(&cp)) != NULL && *arg != '\0')
1023                 fatal("%s line %d: garbage at end of line; \"%.200s\".",
1024                     filename, linenum, arg);
1025         return 0;
1026 }
1027
1028 /* Reads the server configuration file. */
1029
1030 void
1031 load_server_config(const char *filename, Buffer *conf)
1032 {
1033         char line[1024], *cp;
1034         FILE *f;
1035
1036         debug2("%s: filename %s", __func__, filename);
1037         if ((f = fopen(filename, "r")) == NULL) {
1038                 perror(filename);
1039                 exit(1);
1040         }
1041         buffer_clear(conf);
1042         while (fgets(line, sizeof(line), f)) {
1043                 /*
1044                  * Trim out comments and strip whitespace
1045                  * NB - preserve newlines, they are needed to reproduce
1046                  * line numbers later for error messages
1047                  */
1048                 if ((cp = strchr(line, '#')) != NULL)
1049                         memcpy(cp, "\n", 2);
1050                 cp = line + strspn(line, " \t\r");
1051
1052                 buffer_append(conf, cp, strlen(cp));
1053         }
1054         buffer_append(conf, "\0", 1);
1055         fclose(f);
1056         debug2("%s: done config len = %d", __func__, buffer_len(conf));
1057 }
1058
1059 void
1060 parse_server_config(ServerOptions *options, const char *filename, Buffer *conf)
1061 {
1062         int linenum, bad_options = 0;
1063         char *cp, *obuf, *cbuf;
1064
1065         debug2("%s: config %s len %d", __func__, filename, buffer_len(conf));
1066
1067         obuf = cbuf = xstrdup(buffer_ptr(conf));
1068         linenum = 1;
1069         while ((cp = strsep(&cbuf, "\n")) != NULL) {
1070                 if (process_server_config_line(options, cp, filename,
1071                     linenum++) != 0)
1072                         bad_options++;
1073         }
1074         xfree(obuf);
1075         if (bad_options > 0)
1076                 fatal("%s: terminating, %d bad configuration options",
1077                     filename, bad_options);
1078 }
This page took 0.198724 seconds and 5 git commands to generate.