]> andersk Git - openssh.git/blobdiff - servconf.c
20070326
[openssh.git] / servconf.c
index e2c1d4458af49d261dd1f5b8131310427924afa7..d9865025126bd88ed62c5af98d71961c4d03d750 100644 (file)
@@ -1,4 +1,4 @@
-/* $OpenBSD: servconf.c,v 1.158 2006/07/19 13:07:10 dtucker Exp $ */
+/* $OpenBSD: servconf.c,v 1.171 2007/03/09 05:20:06 dtucker Exp $ */
 /*
  * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
  *                    All rights reserved
 #include <sys/types.h>
 #include <sys/socket.h>
 
-#if defined(HAVE_NETDB_H)
-# include <netdb.h>
-#endif
+#include <netdb.h>
+#include <pwd.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <signal.h>
 #include <unistd.h>
+#include <stdarg.h>
 
+#include "xmalloc.h"
 #include "ssh.h"
 #include "log.h"
+#include "buffer.h"
 #include "servconf.h"
-#include "xmalloc.h"
 #include "compat.h"
 #include "pathnames.h"
 #include "misc.h"
 #include "cipher.h"
+#include "key.h"
 #include "kex.h"
 #include "mac.h"
 #include "match.h"
 #include "channels.h"
+#include "groupaccess.h"
 
 static void add_listen_addr(ServerOptions *, char *, u_short);
 static void add_one_listen_addr(ServerOptions *, char *, u_short);
@@ -113,6 +120,7 @@ initialize_server_options(ServerOptions *options)
        options->authorized_keys_file2 = NULL;
        options->num_accept_env = 0;
        options->permit_tun = -1;
+       options->num_permitted_opens = -1;
        options->adm_forced_command = NULL;
 }
 
@@ -317,14 +325,14 @@ static struct {
        { "syslogfacility", sLogFacility, SSHCFG_GLOBAL },
        { "loglevel", sLogLevel, SSHCFG_GLOBAL },
        { "rhostsauthentication", sDeprecated, SSHCFG_GLOBAL },
-       { "rhostsrsaauthentication", sRhostsRSAAuthentication, SSHCFG_GLOBAL },
-       { "hostbasedauthentication", sHostbasedAuthentication, SSHCFG_GLOBAL },
+       { "rhostsrsaauthentication", sRhostsRSAAuthentication, SSHCFG_ALL },
+       { "hostbasedauthentication", sHostbasedAuthentication, SSHCFG_ALL },
        { "hostbasedusesnamefrompacketonly", sHostbasedUsesNameFromPacketOnly, SSHCFG_GLOBAL },
-       { "rsaauthentication", sRSAAuthentication, SSHCFG_GLOBAL },
-       { "pubkeyauthentication", sPubkeyAuthentication, SSHCFG_GLOBAL },
+       { "rsaauthentication", sRSAAuthentication, SSHCFG_ALL },
+       { "pubkeyauthentication", sPubkeyAuthentication, SSHCFG_ALL },
        { "dsaauthentication", sPubkeyAuthentication, SSHCFG_GLOBAL },  /* alias */
 #ifdef KRB5
-       { "kerberosauthentication", sKerberosAuthentication, SSHCFG_GLOBAL },
+       { "kerberosauthentication", sKerberosAuthentication, SSHCFG_ALL },
        { "kerberosorlocalpasswd", sKerberosOrLocalPasswd, SSHCFG_GLOBAL },
        { "kerberosticketcleanup", sKerberosTicketCleanup, SSHCFG_GLOBAL },
 #ifdef USE_AFS
@@ -333,7 +341,7 @@ static struct {
        { "kerberosgetafstoken", sUnsupported, SSHCFG_GLOBAL },
 #endif
 #else
-       { "kerberosauthentication", sUnsupported, SSHCFG_GLOBAL },
+       { "kerberosauthentication", sUnsupported, SSHCFG_ALL },
        { "kerberosorlocalpasswd", sUnsupported, SSHCFG_GLOBAL },
        { "kerberosticketcleanup", sUnsupported, SSHCFG_GLOBAL },
        { "kerberosgetafstoken", sUnsupported, SSHCFG_GLOBAL },
@@ -341,14 +349,14 @@ static struct {
        { "kerberostgtpassing", sUnsupported, SSHCFG_GLOBAL },
        { "afstokenpassing", sUnsupported, SSHCFG_GLOBAL },
 #ifdef GSSAPI
-       { "gssapiauthentication", sGssAuthentication, SSHCFG_GLOBAL },
+       { "gssapiauthentication", sGssAuthentication, SSHCFG_ALL },
        { "gssapicleanupcredentials", sGssCleanupCreds, SSHCFG_GLOBAL },
 #else
-       { "gssapiauthentication", sUnsupported, SSHCFG_GLOBAL },
+       { "gssapiauthentication", sUnsupported, SSHCFG_ALL },
        { "gssapicleanupcredentials", sUnsupported, SSHCFG_GLOBAL },
 #endif
-       { "passwordauthentication", sPasswordAuthentication, SSHCFG_GLOBAL },
-       { "kbdinteractiveauthentication", sKbdInteractiveAuthentication, SSHCFG_GLOBAL },
+       { "passwordauthentication", sPasswordAuthentication, SSHCFG_ALL },
+       { "kbdinteractiveauthentication", sKbdInteractiveAuthentication, SSHCFG_ALL },
        { "challengeresponseauthentication", sChallengeResponseAuthentication, SSHCFG_GLOBAL },
        { "skeyauthentication", sChallengeResponseAuthentication, SSHCFG_GLOBAL }, /* alias */
        { "checkmail", sDeprecated, SSHCFG_GLOBAL },
@@ -381,7 +389,7 @@ static struct {
        { "subsystem", sSubsystem, SSHCFG_GLOBAL },
        { "maxstartups", sMaxStartups, SSHCFG_GLOBAL },
        { "maxauthtries", sMaxAuthTries, SSHCFG_GLOBAL },
-       { "banner", sBanner, SSHCFG_GLOBAL },
+       { "banner", sBanner, SSHCFG_ALL },
        { "usedns", sUseDNS, SSHCFG_GLOBAL },
        { "verifyreversemapping", sDeprecated, SSHCFG_GLOBAL },
        { "reversemappingcheck", sDeprecated, SSHCFG_GLOBAL },
@@ -490,6 +498,51 @@ add_one_listen_addr(ServerOptions *options, char *addr, u_short port)
  *             PermittedChannelRequests session,forwarded-tcpip
  */
 
+static int
+match_cfg_line_group(const char *grps, int line, const char *user)
+{
+       int result = 0;
+       u_int ngrps = 0;
+       char *arg, *p, *cp, *grplist[MAX_MATCH_GROUPS];
+       struct passwd *pw;
+
+       /*
+        * Even if we do not have a user yet, we still need to check for
+        * valid syntax.
+        */
+       arg = cp = xstrdup(grps);
+       while ((p = strsep(&cp, ",")) != NULL && *p != '\0') {
+               if (ngrps >= MAX_MATCH_GROUPS) {
+                       error("line %d: too many groups in Match Group", line);
+                       result = -1;
+                       goto out;
+               }
+               grplist[ngrps++] = p;
+       }
+
+       if (user == NULL)
+               goto out;
+
+       if ((pw = getpwnam(user)) == NULL) {
+               debug("Can't match group at line %d because user %.100s does "
+                   "not exist", line, user);
+       } else if (ga_init(pw->pw_name, pw->pw_gid) == 0) {
+               debug("Can't Match group because user %.100s not in any group "
+                   "at line %d", user, line);
+       } else if (ga_match(grplist, ngrps) != 1) {
+               debug("user %.100s does not match group %.100s at line %d",
+                   user, arg, line);
+       } else {
+               debug("user %.100s matched group %.100s at line %d", user,
+                   arg, line);
+               result = 1;
+       }
+out:
+       ga_free();
+       xfree(arg);
+       return result;
+}
+
 static int
 match_cfg_line(char **condition, int line, const char *user, const char *host,
     const char *address)
@@ -521,6 +574,13 @@ match_cfg_line(char **condition, int line, const char *user, const char *host,
                        else
                                debug("user %.100s matched 'User %.100s' at "
                                    "line %d", user, arg, line);
+               } else if (strcasecmp(attrib, "group") == 0) {
+                       switch (match_cfg_line_group(arg, line, user)) {
+                       case -1:
+                               return -1;
+                       case 0:
+                               result = 0;
+                       }
                } else if (strcasecmp(attrib, "host") == 0) {
                        if (!host) {
                                result = 0;
@@ -908,7 +968,7 @@ parse_flag:
                else
                        fatal("%s line %d: Bad yes/no/clientspecified "
                            "argument: %s", filename, linenum, arg);
-               if (*intptr == -1)
+               if (*activep && *intptr == -1)
                        *intptr = value;
                break;
 
@@ -959,7 +1019,7 @@ parse_flag:
        case sDenyUsers:
                while ((arg = strdelim(&cp)) && *arg != '\0') {
                        if (options->num_deny_users >= MAX_DENY_USERS)
-                               fatal( "%s line %d: too many deny users.",
+                               fatal("%s line %d: too many deny users.",
                                    filename, linenum);
                        options->deny_users[options->num_deny_users++] =
                            xstrdup(arg);
@@ -1094,7 +1154,7 @@ parse_flag:
         */
        case sAuthorizedKeysFile:
        case sAuthorizedKeysFile2:
-               charptr = (opcode == sAuthorizedKeysFile ) ?
+               charptr = (opcode == sAuthorizedKeysFile) ?
                    &options->authorized_keys_file :
                    &options->authorized_keys_file2;
                goto parse_filename;
@@ -1160,21 +1220,29 @@ parse_flag:
                if (!arg || *arg == '\0')
                        fatal("%s line %d: missing PermitOpen specification",
                            filename, linenum);
+               n = options->num_permitted_opens;       /* modified later */
                if (strcmp(arg, "any") == 0) {
-                       if (*activep)
+                       if (*activep && n == -1) {
                                channel_clear_adm_permitted_opens();
+                               options->num_permitted_opens = 0;
+                       }
                        break;
                }
-               p = hpdelim(&arg);
-               if (p == NULL)
-                       fatal("%s line %d: missing host in PermitOpen",
-                           filename, linenum);
-               p = cleanhostname(p);
-               if (arg == NULL || (port = a2port(arg)) == 0)
-                       fatal("%s line %d: bad port number in PermitOpen",
-                           filename, linenum);
-               if (*activep)
-                       channel_add_adm_permitted_opens(p, port);
+               if (*activep && n == -1)
+                       channel_clear_adm_permitted_opens();
+               for (; arg != NULL && *arg != '\0'; arg = strdelim(&cp)) {
+                       p = hpdelim(&arg);
+                       if (p == NULL)
+                               fatal("%s line %d: missing host in PermitOpen",
+                                   filename, linenum);
+                       p = cleanhostname(p);
+                       if (arg == NULL || (port = a2port(arg)) == 0)
+                               fatal("%s line %d: bad port number in "
+                                   "PermitOpen", filename, linenum);
+                       if (*activep && n == -1)
+                               options->num_permitted_opens =
+                                   channel_add_adm_permitted_opens(p, port);
+               }
                break;
 
        case sForceCommand:
@@ -1249,30 +1317,55 @@ parse_server_match_config(ServerOptions *options, const char *user,
 
        initialize_server_options(&mo);
        parse_server_config(&mo, "reprocess config", &cfg, user, host, address);
-       copy_set_server_options(options, &mo);
+       copy_set_server_options(options, &mo, 0);
 }
 
-/* Copy any (supported) values that are set */
+/* Helper macros */
+#define M_CP_INTOPT(n) do {\
+       if (src->n != -1) \
+               dst->n = src->n; \
+} while (0)
+#define M_CP_STROPT(n) do {\
+       if (src->n != NULL) { \
+               if (dst->n != NULL) \
+                       xfree(dst->n); \
+               dst->n = src->n; \
+       } \
+} while(0)
+
+/*
+ * Copy any supported values that are set.
+ *
+ * If the preauth flag is set, we do not bother copying the the string or
+ * array values that are not used pre-authentication, because any that we
+ * do use must be explictly sent in mm_getpwnamallow().
+ */
 void
-copy_set_server_options(ServerOptions *dst, ServerOptions *src)
+copy_set_server_options(ServerOptions *dst, ServerOptions *src, int preauth)
 {
-       if (src->allow_tcp_forwarding != -1)
-               dst->allow_tcp_forwarding = src->allow_tcp_forwarding;
-       if (src->gateway_ports != -1)
-               dst->gateway_ports = src->gateway_ports;
-       if (src->adm_forced_command != NULL) {
-               if (dst->adm_forced_command != NULL)
-                       xfree(dst->adm_forced_command);
-               dst->adm_forced_command = src->adm_forced_command;
-       }
-       if (src->x11_display_offset != -1)
-               dst->x11_display_offset = src->x11_display_offset;
-       if (src->x11_forwarding != -1)
-               dst->x11_forwarding = src->x11_forwarding;
-       if (src->x11_use_localhost != -1)
-               dst->x11_use_localhost = src->x11_use_localhost;
+       M_CP_INTOPT(password_authentication);
+       M_CP_INTOPT(gss_authentication);
+       M_CP_INTOPT(rsa_authentication);
+       M_CP_INTOPT(pubkey_authentication);
+       M_CP_INTOPT(kerberos_authentication);
+       M_CP_INTOPT(hostbased_authentication);
+       M_CP_INTOPT(kbd_interactive_authentication);
+
+       M_CP_INTOPT(allow_tcp_forwarding);
+       M_CP_INTOPT(gateway_ports);
+       M_CP_INTOPT(x11_display_offset);
+       M_CP_INTOPT(x11_forwarding);
+       M_CP_INTOPT(x11_use_localhost);
+
+       M_CP_STROPT(banner);
+       if (preauth)
+               return;
+       M_CP_STROPT(adm_forced_command);
 }
 
+#undef M_CP_INTOPT
+#undef M_CP_STROPT
+
 void
 parse_server_config(ServerOptions *options, const char *filename, Buffer *conf,
     const char *user, const char *host, const char *address)
This page took 0.046548 seconds and 4 git commands to generate.