Author: lidl
Date: Tue Sep  6 14:52:14 2016
New Revision: 305476
URL: https://svnweb.freebsd.org/changeset/base/305476

Log:
  MFC r305065: Add refactored blacklist support to sshd
  
  Change the calls to of blacklist_init() and blacklist_notify to be
  macros defined in the blacklist_client.h file.  This avoids
  the need for #ifdef USE_BLACKLIST / #endif except in the
  blacklist.c file.
  
  Remove redundent initialization attempts from within
  blacklist_notify - everything always goes through
  blacklistd_init().
  
  Added UseBlacklist option to sshd, which defaults to off.
  To enable the functionality, use '-o UseBlacklist=yes' on
  the command line, or uncomment in the sshd_config file.
  
  Approved by:  des
  Sponsored by: The FreeBSD Foundation

Added:
  stable/11/crypto/openssh/blacklist.c
     - copied unchanged from r305065, head/crypto/openssh/blacklist.c
  stable/11/crypto/openssh/blacklist_client.h
     - copied unchanged from r305065, head/crypto/openssh/blacklist_client.h
Modified:
  stable/11/crypto/openssh/auth-pam.c
  stable/11/crypto/openssh/auth.c
  stable/11/crypto/openssh/auth1.c
  stable/11/crypto/openssh/auth2.c
  stable/11/crypto/openssh/packet.c
  stable/11/crypto/openssh/servconf.c
  stable/11/crypto/openssh/servconf.h
  stable/11/crypto/openssh/sshd.c
  stable/11/crypto/openssh/sshd_config
  stable/11/crypto/openssh/sshd_config.5
  stable/11/secure/usr.sbin/sshd/Makefile
  stable/11/secure/usr.sbin/sshd/Makefile.depend
Directory Properties:
  stable/11/   (props changed)

Modified: stable/11/crypto/openssh/auth-pam.c
==============================================================================
--- stable/11/crypto/openssh/auth-pam.c Tue Sep  6 14:26:41 2016        
(r305475)
+++ stable/11/crypto/openssh/auth-pam.c Tue Sep  6 14:52:14 2016        
(r305476)
@@ -98,6 +98,7 @@
 #include "ssh-gss.h"
 #endif
 #include "monitor_wrap.h"
+#include "blacklist_client.h"
 
 extern ServerOptions options;
 extern Buffer loginmsg;
@@ -794,6 +795,7 @@ sshpam_query(void *ctx, char **name, cha
                                free(msg);
                                return (0);
                        }
+                       BLACKLIST_NOTIFY(BLACKLIST_AUTH_FAIL);
                        error("PAM: %s for %s%.100s from %.100s", msg,
                            sshpam_authctxt->valid ? "" : "illegal user ",
                            sshpam_authctxt->user,

Modified: stable/11/crypto/openssh/auth.c
==============================================================================
--- stable/11/crypto/openssh/auth.c     Tue Sep  6 14:26:41 2016        
(r305475)
+++ stable/11/crypto/openssh/auth.c     Tue Sep  6 14:52:14 2016        
(r305476)
@@ -75,6 +75,7 @@ __RCSID("$FreeBSD$");
 #include "authfile.h"
 #include "ssherr.h"
 #include "compat.h"
+#include "blacklist_client.h"
 
 /* import */
 extern ServerOptions options;
@@ -292,8 +293,11 @@ auth_log(Authctxt *authctxt, int authent
                authmsg = "Postponed";
        else if (partial)
                authmsg = "Partial";
-       else
+       else {
                authmsg = authenticated ? "Accepted" : "Failed";
+               BLACKLIST_NOTIFY(authenticated ?
+                   BLACKLIST_AUTH_OK : BLACKLIST_AUTH_FAIL);
+       }
 
        authlog("%s %s%s%s for %s%.100s from %.200s port %d %s%s%s",
            authmsg,
@@ -640,6 +644,7 @@ getpwnamallow(const char *user)
        }
 #endif
        if (pw == NULL) {
+               BLACKLIST_NOTIFY(BLACKLIST_AUTH_FAIL);
                logit("Invalid user %.100s from %.100s",
                    user, get_remote_ipaddr());
 #ifdef CUSTOM_FAILED_LOGIN

Modified: stable/11/crypto/openssh/auth1.c
==============================================================================
--- stable/11/crypto/openssh/auth1.c    Tue Sep  6 14:26:41 2016        
(r305475)
+++ stable/11/crypto/openssh/auth1.c    Tue Sep  6 14:52:14 2016        
(r305476)
@@ -43,6 +43,7 @@
 #endif
 #include "monitor_wrap.h"
 #include "buffer.h"
+#include "blacklist_client.h"
 
 /* import */
 extern ServerOptions options;
@@ -337,6 +338,7 @@ do_authloop(Authctxt *authctxt)
                        char *msg;
                        size_t len;
 
+                       BLACKLIST_NOTIFY(BLACKLIST_AUTH_FAIL);
                        error("Access denied for user %s by PAM account "
                            "configuration", authctxt->user);
                        len = buffer_len(&loginmsg);
@@ -404,6 +406,7 @@ do_authentication(Authctxt *authctxt)
        else {
                debug("do_authentication: invalid user %s", user);
                authctxt->pw = fakepw();
+               BLACKLIST_NOTIFY(BLACKLIST_AUTH_FAIL);
        }
 
        /* Configuration may have changed as a result of Match */

Modified: stable/11/crypto/openssh/auth2.c
==============================================================================
--- stable/11/crypto/openssh/auth2.c    Tue Sep  6 14:26:41 2016        
(r305475)
+++ stable/11/crypto/openssh/auth2.c    Tue Sep  6 14:52:14 2016        
(r305476)
@@ -52,6 +52,7 @@ __RCSID("$FreeBSD$");
 #include "pathnames.h"
 #include "buffer.h"
 #include "canohost.h"
+#include "blacklist_client.h"
 
 #ifdef GSSAPI
 #include "ssh-gss.h"
@@ -248,6 +249,7 @@ input_userauth_request(int type, u_int32
                } else {
                        logit("input_userauth_request: invalid user %s", user);
                        authctxt->pw = fakepw();
+                       BLACKLIST_NOTIFY(BLACKLIST_AUTH_FAIL);
 #ifdef SSH_AUDIT_EVENTS
                        PRIVSEP(audit_event(SSH_INVALID_USER));
 #endif

Copied: stable/11/crypto/openssh/blacklist.c (from r305065, 
head/crypto/openssh/blacklist.c)
==============================================================================
--- /dev/null   00:00:00 1970   (empty, because file is newly added)
+++ stable/11/crypto/openssh/blacklist.c        Tue Sep  6 14:52:14 2016        
(r305476, copy of r305065, head/crypto/openssh/blacklist.c)
@@ -0,0 +1,97 @@
+/*-
+ * Copyright (c) 2015 The NetBSD Foundation, Inc.
+ * Copyright (c) 2016 The FreeBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * Portions of this software were developed by Kurt Lidl
+ * under sponsorship from the FreeBSD Foundation.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by Christos Zoulas.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "includes.h"
+
+#include <ctype.h>
+#include <stdarg.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <syslog.h>
+#include <unistd.h>
+
+#include "ssh.h"
+#include "packet.h"
+#include "log.h"
+#include "misc.h"
+#include "servconf.h"
+#include "blacklist_client.h"
+#include <blacklist.h>
+
+static struct blacklist *blstate = NULL;
+
+/* import */
+extern ServerOptions options;
+
+/* internal definition from bl.h */
+struct blacklist *bl_create(bool, char *, void (*)(int, const char *, 
va_list));
+
+/* impedence match vsyslog() to sshd's internal logging levels */
+void
+im_log(int priority, const char *message, va_list args)
+{
+       LogLevel imlevel;
+
+       switch (priority) {
+       case LOG_ERR:
+               imlevel = SYSLOG_LEVEL_ERROR;
+               break;
+       case LOG_DEBUG:
+               imlevel = SYSLOG_LEVEL_DEBUG1;
+               break;
+       case LOG_INFO:
+               imlevel = SYSLOG_LEVEL_INFO;
+               break;
+       default:
+               imlevel = SYSLOG_LEVEL_DEBUG2;
+       }
+       do_log(imlevel, message, args);
+}
+
+void
+blacklist_init(void)
+{
+
+       if (options.use_blacklist)
+               blstate = bl_create(false, NULL, im_log);
+}
+
+void
+blacklist_notify(int action)
+{
+
+       if (blstate != NULL && packet_connection_is_on_socket())
+               (void)blacklist_r(blstate, action,
+               packet_get_connection_in(), "ssh");
+}

Copied: stable/11/crypto/openssh/blacklist_client.h (from r305065, 
head/crypto/openssh/blacklist_client.h)
==============================================================================
--- /dev/null   00:00:00 1970   (empty, because file is newly added)
+++ stable/11/crypto/openssh/blacklist_client.h Tue Sep  6 14:52:14 2016        
(r305476, copy of r305065, head/crypto/openssh/blacklist_client.h)
@@ -0,0 +1,57 @@
+/*-
+ * Copyright (c) 2015 The NetBSD Foundation, Inc.
+ * Copyright (c) 2016 The FreeBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * Portions of this software were developed by Kurt Lidl
+ * under sponsorship from the FreeBSD Foundation.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by Christos Zoulas.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef BLACKLIST_CLIENT_H
+#define BLACKLIST_CLIENT_H
+
+enum {
+       BLACKLIST_AUTH_OK = 0,
+       BLACKLIST_AUTH_FAIL
+};
+
+#ifdef USE_BLACKLIST
+void blacklist_init(void);
+void blacklist_notify(int);
+
+#define BLACKLIST_INIT() blacklist_init()
+#define BLACKLIST_NOTIFY(x) blacklist_notify(x)
+
+#else
+
+#define BLACKLIST_INIT()
+#define BLACKLIST_NOTIFY(x)
+
+#endif
+
+
+#endif /* BLACKLIST_CLIENT_H */

Modified: stable/11/crypto/openssh/packet.c
==============================================================================
--- stable/11/crypto/openssh/packet.c   Tue Sep  6 14:26:41 2016        
(r305475)
+++ stable/11/crypto/openssh/packet.c   Tue Sep  6 14:52:14 2016        
(r305476)
@@ -86,6 +86,7 @@ __RCSID("$FreeBSD$");
 #include "packet.h"
 #include "ssherr.h"
 #include "sshbuf.h"
+#include "blacklist_client.h"
 
 #ifdef PACKET_DEBUG
 #define DBG(x) x
@@ -2071,6 +2072,7 @@ sshpkt_fatal(struct ssh *ssh, const char
        case SSH_ERR_NO_KEX_ALG_MATCH:
        case SSH_ERR_NO_HOSTKEY_ALG_MATCH:
                if (ssh && ssh->kex && ssh->kex->failed_choice) {
+                       BLACKLIST_NOTIFY(BLACKLIST_AUTH_FAIL);
                        fatal("Unable to negotiate with %.200s port %d: %s. "
                            "Their offer: %s", ssh_remote_ipaddr(ssh),
                            ssh_remote_port(ssh), ssh_err(r),

Modified: stable/11/crypto/openssh/servconf.c
==============================================================================
--- stable/11/crypto/openssh/servconf.c Tue Sep  6 14:26:41 2016        
(r305475)
+++ stable/11/crypto/openssh/servconf.c Tue Sep  6 14:52:14 2016        
(r305476)
@@ -172,6 +172,7 @@ initialize_server_options(ServerOptions 
        options->ip_qos_bulk = -1;
        options->version_addendum = NULL;
        options->fingerprint_hash = -1;
+       options->use_blacklist = -1;
 }
 
 /* Returns 1 if a string option is unset or set to "none" or 0 otherwise. */
@@ -360,6 +361,8 @@ fill_default_server_options(ServerOption
                options->fwd_opts.streamlocal_bind_unlink = 0;
        if (options->fingerprint_hash == -1)
                options->fingerprint_hash = SSH_FP_HASH_DEFAULT;
+       if (options->use_blacklist == -1)
+               options->use_blacklist = 0;
 
        assemble_algorithms(options);
 
@@ -437,6 +440,7 @@ typedef enum {
        sAuthenticationMethods, sHostKeyAgent, sPermitUserRC,
        sStreamLocalBindMask, sStreamLocalBindUnlink,
        sAllowStreamLocalForwarding, sFingerprintHash,
+       sUseBlacklist,
        sDeprecated, sUnsupported
 } ServerOpCodes;
 
@@ -579,6 +583,7 @@ static struct {
        { "streamlocalbindunlink", sStreamLocalBindUnlink, SSHCFG_ALL },
        { "allowstreamlocalforwarding", sAllowStreamLocalForwarding, SSHCFG_ALL 
},
        { "fingerprinthash", sFingerprintHash, SSHCFG_GLOBAL },
+       { "useblacklist", sUseBlacklist, SSHCFG_GLOBAL },
        { "noneenabled", sUnsupported, SSHCFG_ALL },
        { "hpndisabled", sDeprecated, SSHCFG_ALL },
        { "hpnbuffersize", sDeprecated, SSHCFG_ALL },
@@ -1861,6 +1866,10 @@ process_server_config_line(ServerOptions
                        options->fingerprint_hash = value;
                break;
 
+       case sUseBlacklist:
+               intptr = &options->use_blacklist;
+               goto parse_flag;
+
        case sDeprecated:
                logit("%s line %d: Deprecated option %s",
                    filename, linenum, arg);
@@ -2304,6 +2313,7 @@ dump_config(ServerOptions *o)
        dump_cfg_fmtint(sAllowStreamLocalForwarding, 
o->allow_streamlocal_forwarding);
        dump_cfg_fmtint(sUsePrivilegeSeparation, use_privsep);
        dump_cfg_fmtint(sFingerprintHash, o->fingerprint_hash);
+       dump_cfg_fmtint(sUseBlacklist, o->use_blacklist);
 
        /* string arguments */
        dump_cfg_string(sPidFile, o->pid_file);

Modified: stable/11/crypto/openssh/servconf.h
==============================================================================
--- stable/11/crypto/openssh/servconf.h Tue Sep  6 14:26:41 2016        
(r305475)
+++ stable/11/crypto/openssh/servconf.h Tue Sep  6 14:52:14 2016        
(r305476)
@@ -195,6 +195,7 @@ typedef struct {
        char   *auth_methods[MAX_AUTH_METHODS];
 
        int     fingerprint_hash;
+       int     use_blacklist;
 }       ServerOptions;
 
 /* Information about the incoming connection as used by Match */

Modified: stable/11/crypto/openssh/sshd.c
==============================================================================
--- stable/11/crypto/openssh/sshd.c     Tue Sep  6 14:26:41 2016        
(r305475)
+++ stable/11/crypto/openssh/sshd.c     Tue Sep  6 14:52:14 2016        
(r305476)
@@ -135,6 +135,7 @@ __RCSID("$FreeBSD$");
 #include "ssh-sandbox.h"
 #include "version.h"
 #include "ssherr.h"
+#include "blacklist_client.h"
 
 #ifdef LIBWRAP
 #include <tcpd.h>
@@ -388,6 +389,8 @@ grace_alarm_handler(int sig)
                kill(0, SIGTERM);
        }
 
+       BLACKLIST_NOTIFY(BLACKLIST_AUTH_FAIL);
+
        /* Log error and exit. */
        sigdie("Timeout before authentication for %s", get_remote_ipaddr());
 }
@@ -2251,6 +2254,8 @@ main(int ac, char **av)
        buffer_init(&loginmsg);
        auth_debug_reset();
 
+       BLACKLIST_INIT();
+
        if (use_privsep) {
                if (privsep_preauth(authctxt) == 1)
                        goto authenticated;

Modified: stable/11/crypto/openssh/sshd_config
==============================================================================
--- stable/11/crypto/openssh/sshd_config        Tue Sep  6 14:26:41 2016        
(r305475)
+++ stable/11/crypto/openssh/sshd_config        Tue Sep  6 14:52:14 2016        
(r305476)
@@ -120,6 +120,7 @@
 #MaxStartups 10:30:100
 #PermitTunnel no
 #ChrootDirectory none
+#UseBlacklist no
 #VersionAddendum FreeBSD-20160310
 
 # no default banner path

Modified: stable/11/crypto/openssh/sshd_config.5
==============================================================================
--- stable/11/crypto/openssh/sshd_config.5      Tue Sep  6 14:26:41 2016        
(r305475)
+++ stable/11/crypto/openssh/sshd_config.5      Tue Sep  6 14:52:14 2016        
(r305476)
@@ -1537,6 +1537,15 @@ for authentication using
 .Cm TrustedUserCAKeys .
 For more details on certificates, see the CERTIFICATES section in
 .Xr ssh-keygen 1 .
+.It Cm UseBlacklist
+Specifies whether
+.Xr sshd 8
+attempts to send authentication success and failure messages
+to the
+.Xr blacklistd 8
+daemon.
+The default is
+.Dq no .
 .It Cm UseDNS
 Specifies whether
 .Xr sshd 8

Modified: stable/11/secure/usr.sbin/sshd/Makefile
==============================================================================
--- stable/11/secure/usr.sbin/sshd/Makefile     Tue Sep  6 14:26:41 2016        
(r305475)
+++ stable/11/secure/usr.sbin/sshd/Makefile     Tue Sep  6 14:52:14 2016        
(r305476)
@@ -40,6 +40,13 @@ CFLAGS+= -DUSE_BSM_AUDIT -DHAVE_GETAUDIT
 LIBADD+=       bsm
 .endif
 
+.if ${MK_BLACKLIST_SUPPORT} != "no"
+CFLAGS+= -DUSE_BLACKLIST -I${SRCTOP}/contrib/blacklist/include
+SRCS+= blacklist.c
+LIBADD+= blacklist
+LDFLAGS+=-L${LIBBLACKLISTDIR}
+.endif
+
 .if ${MK_KERBEROS_SUPPORT} != "no"
 CFLAGS+= -include krb5_config.h
 SRCS+= krb5_config.h

Modified: stable/11/secure/usr.sbin/sshd/Makefile.depend
==============================================================================
--- stable/11/secure/usr.sbin/sshd/Makefile.depend      Tue Sep  6 14:26:41 
2016        (r305475)
+++ stable/11/secure/usr.sbin/sshd/Makefile.depend      Tue Sep  6 14:52:14 
2016        (r305476)
@@ -17,6 +17,7 @@ DIRDEPS = \
        kerberos5/lib/libroken \
        kerberos5/lib/libwind \
        lib/${CSU_DIR} \
+       lib/libblacklist \
        lib/libbsm \
        lib/libc \
        lib/libcom_err \
_______________________________________________
svn-src-all@freebsd.org mailing list
https://lists.freebsd.org/mailman/listinfo/svn-src-all
To unsubscribe, send any mail to "svn-src-all-unsubscr...@freebsd.org"

Reply via email to