Unify access to FQDN

FreeIPA's Python and C code used different approaches to get the FQDN of
the host. Some places assumed that gethostname() returns a FQDN. Other
code paths used glibc's resolver to resolve the current node name to a
FQDN.

Python code now uses the ipalib.constants.FQDN where a fully qualified
domain name is expected. The variable is initialized only once and avoids
potential DNS lookups.

C code uses a new helper function ipa_gethostfqdn() in util package. The
function implements similar logic as gethostfqdn() except it uses more
modern getaddrinfo(). The result is cached as well.

Fixes: https://pagure.io/freeipa/issue/8501
Signed-off-by: Christian Heimes <cheimes@redhat.com>
Reviewed-By: Fraser Tweedale <ftweedal@redhat.com>
This commit is contained in:
Christian Heimes authored and Fraser Tweedale committed 2020-10-26 17:11:19 +11:00
1 parent 5155280bb4
commit e28ec76898
23 files changed
+226 -67

No files matched your search

+2
View File
@@ -1,9 +1,11 @@
AM_CPPFLAGS := -I$(top_srcdir)/util
AM_CFLAGS := @LDAP_CFLAGS@ @LIBVERTO_CFLAGS@ @KRB5_CFLAGS@ @NSPR_CFLAGS@
AM_LDFLAGS := @LDAP_LIBS@ @LIBVERTO_LIBS@ @KRAD_LIBS@ @KRB5_LIBS@
noinst_HEADERS = internal.h
appdir = $(libexecdir)/ipa/
app_PROGRAMS = ipa-otpd
ipa_otpd_LDADD = $(top_builddir)/util/libutil.la
dist_noinst_DATA = ipa-otpd.socket.in ipa-otpd@.service.in test.py
systemdsystemunit_DATA = ipa-otpd.socket ipa-otpd@.service
+3 -2
View File
@@ -32,6 +32,7 @@
#include <signal.h>
#include <stdbool.h>
#include "ipa_hostname.h"
/* Our global state. */
struct otpd_context ctx;
@@ -212,7 +213,7 @@ static krb5_error_code setup_ldap(const char *uri, krb5_boolean bind,
int main(int argc, char **argv)
{
char hostname[HOST_NAME_MAX + 1];
char hostname[IPA_HOST_NAME_LEN];
krb5_error_code retval;
krb5_data hndata;
verto_ev *sig;
@@ -227,7 +228,7 @@ int main(int argc, char **argv)
memset(&ctx, 0, sizeof(ctx));
ctx.exitstatus = 1;
if (gethostname(hostname, sizeof(hostname)) < 0) {
if (ipa_gethostfqdn(hostname) < 0) {
otpd_log_err(errno, "Unable to get hostname");
goto error;
}
+5 -6
View File
@@ -36,6 +36,7 @@ char *smb_xstrdup(const char *s);
#include <sasl/sasl.h>
#include <krb5/krb5.h>
#include <sss_idmap.h>
#include "ipa_hostname.h"
#include "ipa_asn1.h"
#include "ipa_pwd.h"
#include "ipa_mspac.h"
@@ -4440,7 +4441,7 @@ static char *sec_key(TALLOC_CTX *mem_ctx, const char *d)
static NTSTATUS save_sid_to_secret(struct ipasam_private *ipasam_state)
{
char hostname[255];
char hostname[IPA_HOST_NAME_LEN];
int ret;
char *p;
TALLOC_CTX *tmp_ctx;
@@ -4466,13 +4467,12 @@ static NTSTATUS save_sid_to_secret(struct ipasam_private *ipasam_state)
goto done;
}
ret = gethostname(hostname, sizeof(hostname));
ret = ipa_gethostfqdn(hostname);
if (ret == -1) {
DEBUG(1, ("gethostname failed.\n"));
status = NT_STATUS_UNSUCCESSFUL;
goto done;
}
hostname[sizeof(hostname)-1] = '\0';
p = strchr(hostname, '.');
if (p != NULL) {
*p = '\0';
@@ -4724,7 +4724,7 @@ static NTSTATUS ipasam_generate_principals(struct ipasam_private *ipasam_state)
int ret;
krb5_context context;
NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
char hostname[255];
char hostname[IPA_HOST_NAME_LEN];
char *default_realm = NULL;
if (!ipasam_state) {
@@ -4736,12 +4736,11 @@ static NTSTATUS ipasam_generate_principals(struct ipasam_private *ipasam_state)
return status;
}
ret = gethostname(hostname, sizeof(hostname));
ret = ipa_gethostfqdn(hostname);
if (ret == -1) {
DEBUG(1, ("gethostname failed.\n"));
goto done;
}
hostname[sizeof(hostname)-1] = '\0';
rc = krb5_get_default_realm(context, &default_realm);
if (rc) {
@@ -5,6 +5,7 @@ PLUGIN_COMMON_DIR = $(srcdir)/../common
AM_CPPFLAGS = \
-I$(srcdir) \
-I$(PLUGIN_COMMON_DIR) \
-I$(top_srcdir)/util \
-DPREFIX=\""$(prefix)"\" \
-DBINDIR=\""$(bindir)"\" \
-DLIBDIR=\""$(libdir)"\" \
@@ -31,6 +32,7 @@ libipa_cldap_la_SOURCES = \
libipa_cldap_la_LDFLAGS = -avoid-version
libipa_cldap_la_LIBADD = \
$(top_builddir)/util/libutil.la \
$(LDAP_LIBS) \
$(NDRNBT_LIBS) \
$(NULL)
@@ -49,6 +51,7 @@ ipa_cldap_tests_LDFLAGS = \
-rpath $(shell pkg-config --libs-only-L dirsrv | sed -e 's/-L//') \
$(NULL)
ipa_cldap_tests_LDADD = \
$(top_builddir)/util/libutil.la \
$(CMOCKA_LIBS) \
$(NDRNBT_LIBS) \
$(DIRSRV_LIBS) \
@@ -38,6 +38,7 @@
* END COPYRIGHT BLOCK **/
#include "ipa_cldap.h"
#include "ipa_hostname.h"
#include <endian.h>
#include <talloc.h>
#include <ctype.h>
@@ -236,7 +237,7 @@ int ipa_cldap_netlogon(struct ipa_cldap_ctx *ctx,
struct ipa_cldap_req *req,
struct berval *reply)
{
char hostname[MAXHOSTNAMELEN + 1]; /* NOTE: lenght hardcoded in kernel */
char hostname[IPA_HOST_NAME_LEN];
char *domain = NULL;
char *our_domain = NULL;
char *guid = NULL;
@@ -321,13 +322,11 @@ int ipa_cldap_netlogon(struct ipa_cldap_ctx *ctx,
goto done;
}
ret = gethostname(hostname, MAXHOSTNAMELEN);
ret = ipa_gethostfqdn(hostname);
if (ret == -1) {
ret = errno;
goto done;
}
/* Make double sure it is terminated */
hostname[MAXHOSTNAMELEN] = '\0';
dot = strchr(hostname, '.');
if (!dot) {
/* this name is not fully qualified, therefore invalid */