From 45427285f4c6946b5fee1202f69389572d9086ad Mon Sep 17 00:00:00 2001 From: Timothy Pearson Date: Tue, 5 Jun 2012 01:04:35 -0500 Subject: [PATCH] Add error likely cause routine --- src/libtdeldap.cpp | 61 +++++++++++++++++++++++++++++++++++++++++----- src/libtdeldap.h | 1 + 2 files changed, 56 insertions(+), 6 deletions(-) diff --git a/src/libtdeldap.cpp b/src/libtdeldap.cpp index f7afe0d..3b8f2af 100644 --- a/src/libtdeldap.cpp +++ b/src/libtdeldap.cpp @@ -18,6 +18,10 @@ * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/ +#include +#include +#include + #include #include @@ -46,6 +50,10 @@ int requested_ldap_version = LDAP_VERSION3; int requested_ldap_auth_method = LDAP_AUTH_SIMPLE; // Is this safe and secure over an untrusted connection? char* ldap_user_and_operational_attributes[2] = {"*", "+"}; +enum ErrorCauseLocation { + ERRORCAUSE_LOCATION_BIND = 0 +}; + LDAPManager::LDAPManager(TQString realm, TQString host, TQObject *parent, const char *name) : TQObject(parent, name), m_realm(realm), m_host(host), m_port(0), m_creds(0), m_ldap(0) { TQStringList domainChunks = TQStringList::split(".", realm.lower()); @@ -79,6 +87,22 @@ LDAPCredentials LDAPManager::currentLDAPCredentials() { } } +TQString ldapLikelyErrorCause(int errcode, int location) { + TQString ret; + + if (location == ERRORCAUSE_LOCATION_BIND) { + if (LDAP_NAME_ERROR(errcode)) { + ret = "Unknown user name or incorrect user name format"; + } + } + + if (ret != "") { + ret.prepend("

" + i18n("Likely cause") + ":
"); + } + + return ret; +} + int LDAPManager::bind(TQString* errstr) { printf("[RAJA DEBUG 600.0] In LDAPManager::bind(%p)\n\r", errstr); fflush(stdout); if (m_ldap) { @@ -111,14 +135,14 @@ printf("[RAJA DEBUG 600.1] URI: %s\n\r", uri.ascii()); fflush(stdout); int retcode = ldap_initialize(&m_ldap, uri.ascii()); if (retcode < 0) { - if (errstr) *errstr = i18n("Unable to connect to LDAP server %1 on port %2

Reason: [%3] %4").arg(m_host).arg(m_port).arg(retcode).arg(ldap_err2string(retcode)); - else KMessageBox::error(0, i18n("Unable to connect to LDAP server %1 on port %2

Reason: [%3] %4").arg(m_host).arg(m_port).arg(retcode).arg(ldap_err2string(retcode)), i18n("Unable to connect to server!")); + if (errstr) *errstr = i18n("Unable to connect to LDAP server %1 on port %2

Reason: [%3] %4%5").arg(m_host).arg(m_port).arg(retcode).arg(ldap_err2string(retcode)).arg(ldapLikelyErrorCause(retcode, ERRORCAUSE_LOCATION_BIND)); + else KMessageBox::error(0, i18n("Unable to connect to LDAP server %1 on port %2

Reason: [%3] %4%5").arg(m_host).arg(m_port).arg(retcode).arg(ldap_err2string(retcode)).arg(ldapLikelyErrorCause(retcode, ERRORCAUSE_LOCATION_BIND)), i18n("Unable to connect to server!")); return -1; } retcode = ldap_set_option(m_ldap, LDAP_OPT_PROTOCOL_VERSION, &requested_ldap_version); if (retcode != LDAP_OPT_SUCCESS) { - if (errstr) *errstr = i18n("Unable to connect to LDAP server %1 on port %2

Reason: [%3] %4").arg(m_host).arg(m_port).arg(retcode).arg(ldap_err2string(retcode)); - else KMessageBox::error(0, i18n("Unable to connect to LDAP server %1 on port %2

Reason: [%3] %4").arg(m_host).arg(m_port).arg(retcode).arg(ldap_err2string(retcode)), i18n("Unable to connect to server!")); + if (errstr) *errstr = i18n("Unable to connect to LDAP server %1 on port %2

Reason: [%3] %4%5").arg(m_host).arg(m_port).arg(retcode).arg(ldap_err2string(retcode)).arg(ldapLikelyErrorCause(retcode, ERRORCAUSE_LOCATION_BIND)); + else KMessageBox::error(0, i18n("Unable to connect to LDAP server %1 on port %2

Reason: [%3] %4%5").arg(m_host).arg(m_port).arg(retcode).arg(ldap_err2string(retcode)).arg(ldapLikelyErrorCause(retcode, ERRORCAUSE_LOCATION_BIND)), i18n("Unable to connect to server!")); return -1; } printf("[RAJA DEBUG 660.0]\n\r"); fflush(stdout); @@ -219,8 +243,8 @@ printf("[RAJA DEBUG 660.2]\n\r"); fflush(stdout); printf("[RAJA DEBUG 600.2] ldap_dn: %s\n\r", ldap_dn.ascii()); fflush(stdout); if (retcode != LDAP_SUCCESS ) { - if (errstr) *errstr = i18n("Unable to connect to LDAP server %1 on port %2

Reason: [%3] %4").arg(m_host).arg(m_port).arg(retcode).arg(ldap_err2string(retcode)); - else KMessageBox::error(0, i18n("Unable to connect to LDAP server %1 on port %2

Reason: [%3] %4").arg(m_host).arg(m_port).arg(retcode).arg(ldap_err2string(retcode)), i18n("Unable to connect to server!")); + if (errstr) *errstr = i18n("Unable to connect to LDAP server %1 on port %2

Reason: [%3] %4%5").arg(m_host).arg(m_port).arg(retcode).arg(ldap_err2string(retcode)).arg(ldapLikelyErrorCause(retcode, ERRORCAUSE_LOCATION_BIND)); + else KMessageBox::error(0, i18n("Unable to connect to LDAP server %1 on port %2

Reason: [%3] %4%5").arg(m_host).arg(m_port).arg(retcode).arg(ldap_err2string(retcode)).arg(ldapLikelyErrorCause(retcode, ERRORCAUSE_LOCATION_BIND)), i18n("Unable to connect to server!")); return -1; } @@ -1341,6 +1365,31 @@ void LDAPManager::writeTDERealmList(LDAPRealmConfigList realms, KSimpleConfig* c } } +TQString LDAPManager::getMachineFQDN() { + struct addrinfo hints, *info, *p; + int gai_result; + + char hostname[1024]; + hostname[1023] = '\0'; + gethostname(hostname, 1023); + + memset(&hints, 0, sizeof hints); + hints.ai_family = AF_UNSPEC; // IPV4 or IPV6 + hints.ai_socktype = SOCK_STREAM; + hints.ai_flags = AI_CANONNAME; + + if ((gai_result = getaddrinfo(hostname, NULL, &hints, &info)) != 0) { + return TQString(hostname); + } + TQString fqdn = TQString(hostname); + for (p=info; p!=NULL; p=p->ai_next) { + fqdn = TQString(p->ai_canonname); + } + freeaddrinfo(info); + + return fqdn; +} + // =============================================================================================================== // // DATA CLASS CONSTRUCTORS AND DESTRUCTORS diff --git a/src/libtdeldap.h b/src/libtdeldap.h index 2579902..f205b79 100644 --- a/src/libtdeldap.h +++ b/src/libtdeldap.h @@ -235,6 +235,7 @@ class LDAPManager : public TQObject { int moveKerberosEntries(TQString newSuffix, TQString* errstr=0); + static TQString getMachineFQDN(); static void writeLDAPConfFile(LDAPRealmConfig realmcfg); static void writeTDERealmList(LDAPRealmConfigList realms, KSimpleConfig* config);