Blame SOURCES/0052-Fix-rDNS-with-IPv6.patch

90e381
From 01b646d2af0ed885d01d31a6479898a3c423a630 Mon Sep 17 00:00:00 2001
90e381
From: =?UTF-8?q?Ond=C5=99ej=20Lyson=C4=9Bk?= <olysonek@redhat.com>
90e381
Date: Thu, 26 Apr 2018 10:00:19 +0200
90e381
Subject: [PATCH 52/59] Fix rDNS with IPv6
90e381
90e381
Previously IPv6 addresses were not translated to hostnames for PAM to use.
90e381
---
90e381
 privops.c    |  3 ++-
90e381
 sysdeputil.c | 28 +++++++++++++++-------------
90e381
 sysdeputil.h |  5 ++++-
90e381
 sysutil.c    | 35 +++++++++++++++++++++++++++++++++++
90e381
 sysutil.h    |  4 ++++
90e381
 5 files changed, 60 insertions(+), 15 deletions(-)
90e381
90e381
diff --git a/privops.c b/privops.c
90e381
index f27c5c4..e577a27 100644
90e381
--- a/privops.c
90e381
+++ b/privops.c
90e381
@@ -383,7 +383,8 @@ handle_local_login(struct vsf_session* p_sess,
90e381
                    struct mystr* p_user_str,
90e381
                    const struct mystr* p_pass_str)
90e381
 {
90e381
-  if (!vsf_sysdep_check_auth(p_user_str, p_pass_str, &p_sess->remote_ip_str))
90e381
+  if (!vsf_sysdep_check_auth(p_sess, p_user_str, p_pass_str,
90e381
+                             &p_sess->remote_ip_str))
90e381
   {
90e381
     return kVSFLoginFail;
90e381
   }
90e381
diff --git a/sysdeputil.c b/sysdeputil.c
90e381
index 2063c87..4fe56c2 100644
90e381
--- a/sysdeputil.c
90e381
+++ b/sysdeputil.c
90e381
@@ -16,10 +16,6 @@
90e381
 #include "tunables.h"
90e381
 #include "builddefs.h"
90e381
 
90e381
-/* For gethostbyaddr, inet_addr */
90e381
-#include <netdb.h>
90e381
-#include <arpa/inet.h>
90e381
-
90e381
 /* For Linux, this adds nothing :-) */
90e381
 #include "port/porting_junk.h"
90e381
 
90e381
@@ -242,13 +238,15 @@ void vsf_remove_uwtmp(void);
90e381
 
90e381
 #ifndef VSF_SYSDEP_HAVE_PAM
90e381
 int
90e381
-vsf_sysdep_check_auth(struct mystr* p_user_str,
90e381
+vsf_sysdep_check_auth(struct vsf_session* p_sess,
90e381
+                      struct mystr* p_user_str,
90e381
                       const struct mystr* p_pass_str,
90e381
                       const struct mystr* p_remote_host)
90e381
 {
90e381
   const char* p_crypted;
90e381
   const struct passwd* p_pwd = getpwnam(str_getbuf(p_user_str));
90e381
   (void) p_remote_host;
90e381
+  (void) p_sess;
90e381
   if (p_pwd == NULL)
90e381
   {
90e381
     return 0;
90e381
@@ -322,14 +320,14 @@ static int pam_conv_func(int nmsg, const struct pam_message** p_msg,
90e381
 static void vsf_auth_shutdown(void);
90e381
 
90e381
 int
90e381
-vsf_sysdep_check_auth(struct mystr* p_user_str,
90e381
+vsf_sysdep_check_auth(struct vsf_session* p_sess,
90e381
+                      struct mystr* p_user_str,
90e381
                       const struct mystr* p_pass_str,
90e381
                       const struct mystr* p_remote_host)
90e381
 {
90e381
   int retval = -1;
90e381
 #ifdef PAM_RHOST
90e381
-  struct sockaddr_in sin;
90e381
-  struct hostent *host;
90e381
+  struct mystr hostname = INIT_MYSTR;
90e381
 #endif
90e381
   pam_item_t item;
90e381
   const char* pam_user_name = 0;
90e381
@@ -354,13 +352,17 @@ vsf_sysdep_check_auth(struct mystr* p_user_str,
90e381
     return 0;
90e381
   }
90e381
 #ifdef PAM_RHOST
90e381
-  if (tunable_reverse_lookup_enable) {
90e381
-    sin.sin_addr.s_addr = inet_addr(str_getbuf(p_remote_host));
90e381
-    host = gethostbyaddr((char*)&sin.sin_addr.s_addr,sizeof(struct in_addr),AF_INET);
90e381
-    if (host != (struct hostent*)0)
90e381
-      retval = pam_set_item(s_pamh, PAM_RHOST, host->h_name);
90e381
+  if (tunable_reverse_lookup_enable)
90e381
+  {
90e381
+    if (vsf_sysutil_get_hostname(p_sess->p_remote_addr, &hostname) == 0)
90e381
+    {
90e381
+      retval = pam_set_item(s_pamh, PAM_RHOST, str_getbuf(&hostname));
90e381
+      str_free(&hostname);
90e381
+    }
90e381
     else
90e381
+    {
90e381
       retval = pam_set_item(s_pamh, PAM_RHOST, str_getbuf(p_remote_host));
90e381
+    }
90e381
   } else {
90e381
     retval = pam_set_item(s_pamh, PAM_RHOST, str_getbuf(p_remote_host));
90e381
   }
90e381
diff --git a/sysdeputil.h b/sysdeputil.h
90e381
index 3b6b30a..6f2aa0a 100644
90e381
--- a/sysdeputil.h
90e381
+++ b/sysdeputil.h
90e381
@@ -5,6 +5,8 @@
90e381
 #include "filesize.h"
90e381
 #endif
90e381
 
90e381
+#include "session.h"
90e381
+
90e381
 /* VSF_SYSDEPUTIL_H:
90e381
  * Support for highly system dependent features, and querying for support
90e381
  * or lack thereof
90e381
@@ -15,7 +17,8 @@ struct mystr;
90e381
 
90e381
 /* Authentication of local users */
90e381
 /* Return 0 for fail, 1 for success */
90e381
-int vsf_sysdep_check_auth(struct mystr* p_user,
90e381
+int vsf_sysdep_check_auth(struct vsf_session* p_sess,
90e381
+                          struct mystr* p_user,
90e381
                           const struct mystr* p_pass,
90e381
                           const struct mystr* p_remote_host);
90e381
 
90e381
diff --git a/sysutil.c b/sysutil.c
90e381
index e847650..b68583b 100644
90e381
--- a/sysutil.c
90e381
+++ b/sysutil.c
90e381
@@ -2356,6 +2356,41 @@ vsf_sysutil_dns_resolve(struct vsf_sysutil_sockaddr** p_sockptr,
90e381
   }
90e381
 }
90e381
 
90e381
+int
90e381
+vsf_sysutil_get_hostname(struct vsf_sysutil_sockaddr *p_addr,
90e381
+                         struct mystr* p_str)
90e381
+{
90e381
+  struct sockaddr *sa;
90e381
+  socklen_t sa_len = 0;
90e381
+  char hostname[NI_MAXHOST];
90e381
+  int res;
90e381
+
90e381
+  sa = &p_addr->u.u_sockaddr;
90e381
+  if (sa->sa_family == AF_INET)
90e381
+  {
90e381
+    sa_len = sizeof(struct sockaddr_in);
90e381
+  }
90e381
+  else if (sa->sa_family == AF_INET6)
90e381
+  {
90e381
+    sa_len = sizeof(struct sockaddr_in6);
90e381
+  }
90e381
+  else
90e381
+  {
90e381
+    die("can only support ipv4 and ipv6 currently");
90e381
+  }
90e381
+  res = getnameinfo(sa, sa_len, hostname, sizeof(hostname), NULL, 0,
90e381
+                    NI_NAMEREQD);
90e381
+  if (res == 0)
90e381
+  {
90e381
+    str_alloc_text(p_str, hostname);
90e381
+    return 0;
90e381
+  }
90e381
+  else
90e381
+  {
90e381
+    return -1;
90e381
+  }
90e381
+}
90e381
+
90e381
 struct vsf_sysutil_user*
90e381
 vsf_sysutil_getpwuid(const unsigned int uid)
90e381
 {
90e381
diff --git a/sysutil.h b/sysutil.h
90e381
index 7a59f13..2df14ed 100644
90e381
--- a/sysutil.h
90e381
+++ b/sysutil.h
90e381
@@ -7,6 +7,8 @@
90e381
 #include "filesize.h"
90e381
 #endif
90e381
 
90e381
+#include "str.h"
90e381
+
90e381
 /* Return value queries */
90e381
 int vsf_sysutil_retval_is_error(int retval);
90e381
 enum EVSFSysUtilError
90e381
@@ -266,6 +268,8 @@ int vsf_sysutil_connect_timeout(int fd,
90e381
                                 unsigned int wait_seconds);
90e381
 void vsf_sysutil_dns_resolve(struct vsf_sysutil_sockaddr** p_sockptr,
90e381
                              const char* p_name);
90e381
+int vsf_sysutil_get_hostname(struct vsf_sysutil_sockaddr *p_addr,
90e381
+                             struct mystr* p_str);
90e381
 /* Option setting on sockets */
90e381
 void vsf_sysutil_activate_keepalive(int fd);
90e381
 void vsf_sysutil_rcvtimeo(int fd);
90e381
-- 
90e381
2.14.4
90e381