]> git.ipfire.org Git - thirdparty/squid.git/commitdiff
Helpers: upgrade digest helpers to C++
authorAmos Jeffries <squid3@treenet.co.nz>
Thu, 8 Jul 2010 11:01:47 +0000 (23:01 +1200)
committerAmos Jeffries <squid3@treenet.co.nz>
Thu, 8 Jul 2010 11:01:47 +0000 (23:01 +1200)
12 files changed:
configure.in
helpers/digest_auth/LDAP/Makefile.am [moved from helpers/digest_auth/ldap/Makefile.am with 51% similarity]
helpers/digest_auth/LDAP/config.test [moved from helpers/digest_auth/ldap/config.test with 100% similarity]
helpers/digest_auth/LDAP/digest_common.h [moved from helpers/digest_auth/ldap/digest_common.h with 100% similarity]
helpers/digest_auth/LDAP/digest_pw_auth.cc [moved from helpers/digest_auth/eDirectory/digest_pw_auth.c with 92% similarity]
helpers/digest_auth/LDAP/ldap_backend.cc [moved from helpers/digest_auth/ldap/ldap_backend.c with 96% similarity]
helpers/digest_auth/LDAP/ldap_backend.h [moved from helpers/digest_auth/ldap/ldap_backend.h with 100% similarity]
helpers/digest_auth/Makefile.am
helpers/digest_auth/eDirectory/Makefile.am
helpers/digest_auth/eDirectory/digest_pw_auth.cc [moved from helpers/digest_auth/ldap/digest_pw_auth.c with 91% similarity]
helpers/digest_auth/eDirectory/edir_ldapext.cc [moved from helpers/digest_auth/eDirectory/edir_ldapext.c with 84% similarity]
helpers/digest_auth/eDirectory/ldap_backend.cc [moved from helpers/digest_auth/eDirectory/ldap_backend.c with 93% similarity]

index ae8e88528420da90b0f2dcae60f5892d27874965..a91957e2911860758690fd3547dce8f66cf2b981 100644 (file)
@@ -3821,7 +3821,7 @@ AC_CONFIG_FILES([\
        helpers/digest_auth/Makefile \
        helpers/digest_auth/eDirectory/Makefile \
        helpers/digest_auth/file/Makefile \
-       helpers/digest_auth/ldap/Makefile \
+       helpers/digest_auth/LDAP/Makefile \
        helpers/ntlm_auth/Makefile \
        helpers/ntlm_auth/fake/Makefile \
        helpers/ntlm_auth/smb_lm/Makefile \
similarity index 51%
rename from helpers/digest_auth/ldap/Makefile.am
rename to helpers/digest_auth/LDAP/Makefile.am
index a16d39fa475ef73d80735e20d76603f459f2c4e0..cb7ec9f5224c4ef1d3b2d02a2c657959ec65c48b 100644 (file)
@@ -1,26 +1,16 @@
-#
-#  Makefile for the Squid Object Cache server
-#
-#  $Id$
-#
-#  Uncomment and customize the following to suit your needs:
-#
-
 include $(top_srcdir)/src/Common.am
 
 ## we need our local files too (but avoid -I. at all costs)
 INCLUDES += -I$(srcdir)
 
-
 libexec_PROGRAMS = digest_ldap_auth
-digest_ldap_auth_SOURCES = digest_pw_auth.c \
+digest_ldap_auth_SOURCES = digest_pw_auth.cc \
        digest_common.h \
-       ldap_backend.c \
+       ldap_backend.cc \
        ldap_backend.h
 
-LDADD = \
-       $(top_builddir)/compat/libcompat.la \
-       -L$(top_builddir)/lib -lmiscutil \
+digest_ldap_auth_LDADD = \
+       $(COMPAT_LIB) \
        $(LDAPLIB) \
        $(LBERLIB) \
        $(CRYPTLIB) \
similarity index 92%
rename from helpers/digest_auth/eDirectory/digest_pw_auth.c
rename to helpers/digest_auth/LDAP/digest_pw_auth.cc
index 2f5f2016dbfa9cef44c41739ce388f6dff027817..a83ac45bd442da8d6f323eca3ddc092fdaadf173 100644 (file)
  *
  * Copyright (c) 2003  Robert Collins  <robertc@squid-cache.org>
  */
-
+#include "config.h"
 #include "digest_common.h"
+#include "helpers/defines.h"
 #include "ldap_backend.h"
+
 #define PROGRAM_NAME "digest_ldap_auth"
 
 
@@ -63,7 +65,7 @@ OutputHHA1(RequestData * requestData)
     requestData->error = 0;
     GetHHA1(requestData);
     if (requestData->error) {
-        printf("ERR No such user\n");
+        SEND_ERR("No such user");
         return;
     }
     printf("%s\n", requestData->HHA1);
@@ -75,7 +77,7 @@ DoOneRequest(char *buf)
     RequestData requestData;
     ParseBuffer(buf, &requestData);
     if (!requestData.parsed) {
-        printf("ERR\n");
+        SEND_ERR("");
         return;
     }
     OutputHHA1(&requestData);
@@ -93,10 +95,10 @@ ProcessArguments(int argc, char **argv)
 int
 main(int argc, char **argv)
 {
-    char buf[256];
+    char buf[HELPER_INPUT_BUFFER];
     setbuf(stdout, NULL);
     ProcessArguments(argc, argv);
-    while (fgets(buf, 256, stdin) != NULL)
+    while (fgets(buf, HELPER_INPUT_BUFFER, stdin) != NULL)
         DoOneRequest(buf);
     exit(0);
 }
similarity index 96%
rename from helpers/digest_auth/ldap/ldap_backend.c
rename to helpers/digest_auth/LDAP/ldap_backend.cc
index ccb70afba2d326881c83869cc81689c4d882a0a7..d305c01a8f92c50f9763d73a04f6b7c6f7dd180c 100644 (file)
@@ -63,7 +63,6 @@ static int encrpass = 0;
 static int searchscope = LDAP_SCOPE_SUBTREE;
 static int persistent = 0;
 static int noreferrals = 0;
-static int show_debug_messages = 0;
 static int port = LDAP_PORT;
 static int strip_nt_domain = 0;
 static int aliasderef = LDAP_DEREF_NEVER;
@@ -94,7 +93,7 @@ squid_ldap_set_aliasderef(int deref)
 static void
 squid_ldap_set_referrals(int referrals)
 {
-    int *value = referrals ? LDAP_OPT_ON :LDAP_OPT_OFF;
+    int *value = static_cast<int*>(referrals ? LDAP_OPT_ON :LDAP_OPT_OFF);
     ldap_set_option(ld, LDAP_OPT_REFERRALS, value);
 }
 static void
@@ -207,8 +206,7 @@ getpassword(char *login, char *realm)
             snprintf(filter, sizeof(filter), usersearchfilter, escaped_login, escaped_login, escaped_login, escaped_login, escaped_login, escaped_login, escaped_login, escaped_login, escaped_login, escaped_login, escaped_login, escaped_login, escaped_login, escaped_login, escaped_login, escaped_login);
 
 retrysrch:
-            if (show_debug_messages)
-                fprintf(stderr, "user filter '%s', searchbase '%s'\n", filter, searchbase);
+            debug("user filter '%s', searchbase '%s'\n", filter, searchbase);
 
             rc = ldap_search_s(ld, searchbase, searchscope, filter, NULL, 0, &res);
             if (rc != LDAP_SUCCESS) {
@@ -243,8 +241,7 @@ retrysrch:
             snprintf(searchbase, 8192, "%s=%s, %s", userdnattr, login, userbasedn);
 
 retrydnattr:
-            if (show_debug_messages)
-                fprintf(stderr, "searchbase '%s'\n", searchbase);
+            debug("searchbase '%s'\n", searchbase);
             rc = ldap_search_s(ld, searchbase, searchscope, NULL, NULL, 0, &res);
         }
         if (rc == LDAP_SUCCESS) {
@@ -256,8 +253,7 @@ retrydnattr:
                 return NULL;
             }
             if (!values) {
-                if (show_debug_messages)
-                    printf("No attribute value found\n");
+                debug("No attribute value found\n");
                 ldap_msgfree(res);
                 return NULL;
             }
@@ -274,8 +270,7 @@ retrydnattr:
                 }
                 value++;
             }
-            if (show_debug_messages)
-                printf("password: %s\n", password);
+            debug("password: %s\n", password);
             if (password)
                 password = xstrdup(password);
             ldap_value_free(values);
@@ -389,8 +384,7 @@ ldapconnect(void)
                 ld = NULL;
             }
         }
-        if (show_debug_messages)
-            fprintf(stderr, "Connected OK\n");
+        debug("Connected OK\n");
     }
 }
 int
@@ -433,7 +427,7 @@ LDAPArguments(int argc, char **argv)
         case 'h':
             if (ldapServer) {
                 int len = strlen(ldapServer) + 1 + strlen(value) + 1;
-                char *newhost = malloc(len);
+                char *newhost = (char*)malloc(len);
                 snprintf(newhost, len, "%s %s", ldapServer, value);
                 free(ldapServer);
                 ldapServer = newhost;
@@ -544,7 +538,7 @@ LDAPArguments(int argc, char **argv)
             break;
 #endif
         case 'd':
-            show_debug_messages = 1;
+            debug_enabled = 1;
             break;
         case 'E':
             strip_nt_domain = 1;
@@ -559,7 +553,7 @@ LDAPArguments(int argc, char **argv)
         char *value = argv[1];
         if (ldapServer) {
             int len = strlen(ldapServer) + 1 + strlen(value) + 1;
-            char *newhost = malloc(len);
+            char *newhost = (char*)malloc(len);
             snprintf(newhost, len, "%s %s", ldapServer, value);
             free(ldapServer);
             ldapServer = newhost;
index 56dfd86e1f47989d725e620d943fbbe3f42be9dc..89e7bbda56adc439f89c61bf0bcbd794e20e595e 100644 (file)
@@ -2,6 +2,6 @@
 DIST_SUBDIRS= \
        eDirectory \
        file \
-       ldap
+       LDAP
 
 SUBDIRS= $(DIGEST_AUTH_HELPERS)
index 0a32097fb6d0ebdcb29a7a9f5d64b1a49f42fd61..0997f2aca45c915fb9325c62584b252a136731d9 100644 (file)
@@ -1,28 +1,19 @@
-#
-#  Makefile for the Squid Object Cache server
-#
-#  $Id$
-#
-#  Uncomment and customize the following to suit your needs:
-#
-
 include $(top_srcdir)/src/Common.am
 
 ## we need our local files too (but avoid -I. at all costs)
 INCLUDES += -I$(srcdir)
 
 
-libexec_PROGRAMS = digest_edir_auth
-digest_edir_auth_SOURCES = digest_pw_auth.c \
+libexec_PROGRAMS = digest_edirectory_auth
+digest_edirectory_auth_SOURCES = digest_pw_auth.cc \
        digest_common.h \
-       ldap_backend.c \
+       ldap_backend.cc \
        ldap_backend.h \
-       edir_ldapext.c \
+       edir_ldapext.cc \
        edir_ldapext.h
 
-LDADD = \
-       $(top_builddir)/compat/libcompat.la \
-       -L$(top_builddir)/lib -lmiscutil \
+digest_edirectory_auth_LDADD = \
+       $(COMPAT_LIB) \
        $(LDAPLIB) \
        $(LBERLIB) \
        $(CRYPTLIB) \
similarity index 91%
rename from helpers/digest_auth/ldap/digest_pw_auth.c
rename to helpers/digest_auth/eDirectory/digest_pw_auth.cc
index 2f5f2016dbfa9cef44c41739ce388f6dff027817..e2359a6bc069cf9a465ebb07665f403fc38f38c5 100644 (file)
  *
  * Copyright (c) 2003  Robert Collins  <robertc@squid-cache.org>
  */
-
+#include "config.h"
 #include "digest_common.h"
+#include "helpers/defines.h"
 #include "ldap_backend.h"
-#define PROGRAM_NAME "digest_ldap_auth"
 
+#define PROGRAM_NAME "digest_edirectory_auth"
 
 static void
 GetHHA1(RequestData * requestData)
@@ -63,7 +64,7 @@ OutputHHA1(RequestData * requestData)
     requestData->error = 0;
     GetHHA1(requestData);
     if (requestData->error) {
-        printf("ERR No such user\n");
+        SEND_ERR("No such user");
         return;
     }
     printf("%s\n", requestData->HHA1);
@@ -75,7 +76,7 @@ DoOneRequest(char *buf)
     RequestData requestData;
     ParseBuffer(buf, &requestData);
     if (!requestData.parsed) {
-        printf("ERR\n");
+        SEND_ERR("");
         return;
     }
     OutputHHA1(&requestData);
@@ -93,10 +94,10 @@ ProcessArguments(int argc, char **argv)
 int
 main(int argc, char **argv)
 {
-    char buf[256];
+    char buf[HELPER_INPUT_BUFFER];
     setbuf(stdout, NULL);
     ProcessArguments(argc, argv);
-    while (fgets(buf, 256, stdin) != NULL)
+    while (fgets(buf, HELPER_INPUT_BUFFER, stdin) != NULL)
         DoOneRequest(buf);
     exit(0);
 }
similarity index 84%
rename from helpers/digest_auth/eDirectory/edir_ldapext.c
rename to helpers/digest_auth/eDirectory/edir_ldapext.cc
index f8a611afb11159f6872b6be9a99b158f8158fd6e..713edf2cafdb040b133e70c1d124c7e0ff8551bc 100644 (file)
@@ -104,7 +104,8 @@ static int berEncodePasswordData(
     /* Allocate a BerElement for the request parameters. */
     if ((requestBer = ber_alloc()) == NULL) {
         err = LDAP_ENCODING_ERROR;
-        goto Cleanup;
+        ber_free(requestBer, 1);
+        return err;
     }
 
     if (password != NULL && password2 != NULL) {
@@ -120,19 +121,14 @@ static int berEncodePasswordData(
 
     if (rc < 0) {
         err = LDAP_ENCODING_ERROR;
-        goto Cleanup;
     } else {
         err = 0;
+        /* Convert the BER we just built to a berval that we'll send with the extended request. */
+        if ((ber_tag_t)ber_flatten(requestBer, requestBV) == LBER_ERROR) {
+            err = LDAP_ENCODING_ERROR;
+        }
     }
 
-    /* Convert the BER we just built to a berval that we'll send with the extended request. */
-    if (ber_flatten(requestBer, requestBV) == LBER_ERROR) {
-        err = LDAP_ENCODING_ERROR;
-        goto Cleanup;
-    }
-
-Cleanup:
-
     if (requestBer) {
         ber_free(requestBer, 1);
     }
@@ -175,7 +171,7 @@ static int berEncodeLoginData(
     /* Allocate a BerElement for the request parameters. */
     if ((requestBer = ber_alloc()) == NULL) {
         err = LDAP_ENCODING_ERROR;
-        goto Cleanup;
+        return err;
     }
 
     /* BER encode the NMAS Version and the objectDN */
@@ -202,18 +198,11 @@ static int berEncodeLoginData(
         err = (ber_printf(requestBer, "o}", utf8TagPtr, utf8TagSize) < 0) ? LDAP_ENCODING_ERROR : 0;
     }
 
-    if (err) {
-        goto Cleanup;
-    }
-
     /* Convert the BER we just built to a berval that we'll send with the extended request. */
-    if (ber_flatten(requestBer, requestBV) == LBER_ERROR) {
+    if (!err && (ber_tag_t)ber_flatten(requestBer, requestBV) == LBER_ERROR) {
         err = LDAP_ENCODING_ERROR;
-        goto Cleanup;
     }
 
-Cleanup:
-
     if (requestBer) {
         ber_free(requestBer, 1);
     }
@@ -240,18 +229,14 @@ static int berDecodeLoginData(
 
     if ((replyBer = ber_init(replyBV)) == NULL) {
         err = LDAP_OPERATIONS_ERROR;
-        goto Cleanup;
     }
-
-    if (retData) {
+    else if (retData) {
         retOctStrLen = *retDataLen + 1;
-        retOctStr = SMB_MALLOC_ARRAY(char, retOctStrLen);
+        retOctStr = (char*)SMB_MALLOC_ARRAY(char, retOctStrLen);
         if (!retOctStr) {
             err = LDAP_OPERATIONS_ERROR;
-            goto Cleanup;
         }
-
-        if (ber_scanf(replyBer, "{iis}", serverVersion, &err, retOctStr, &retOctStrLen) != -1) {
+        else if (ber_scanf(replyBer, "{iis}", serverVersion, &err, retOctStr, &retOctStrLen) != LBER_ERROR) {
             if (*retDataLen >= retOctStrLen) {
                 memcpy(retData, retOctStr, retOctStrLen);
             } else if (!err) {
@@ -263,15 +248,13 @@ static int berDecodeLoginData(
             err = LDAP_DECODING_ERROR;
         }
     } else {
-        if (ber_scanf(replyBer, "{ii}", serverVersion, &err) == -1) {
+        if (ber_scanf(replyBer, "{ii}", serverVersion, &err) == LBER_ERROR) {
             if (!err) {
                 err = LDAP_DECODING_ERROR;
             }
         }
     }
 
-Cleanup:
-
     if (replyBer) {
         ber_free(replyBer, 1);
     }
@@ -311,44 +294,32 @@ static int getLoginConfig(
 
     err = berEncodeLoginData(&requestBV, objectDN, methodIDLen, methodID, tag, 0, NULL);
     if (err) {
-        goto Cleanup;
-    }
-
-    /* Call the ldap_extended_operation (synchronously) */
-    if ((err = ldap_extended_operation_s(ld, NMASLDAP_GET_LOGIN_CONFIG_REQUEST,
+        ;
+    } else if (!err && (err = ldap_extended_operation_s(ld, NMASLDAP_GET_LOGIN_CONFIG_REQUEST,
                                          requestBV, NULL, NULL, &replyOID, &replyBV))) {
-        goto Cleanup;
-    }
-
-    /* Make sure there is a return OID */
-    if (!replyOID) {
+        /* Call the ldap_extended_operation (synchronously) */
+        ;
+    } else if (!replyOID) {
+        /* Make sure there is a return OID */
         err = LDAP_NOT_SUPPORTED;
-        goto Cleanup;
-    }
-
-    /* Is this what we were expecting to get back. */
-    if (strcmp(replyOID, NMASLDAP_GET_LOGIN_CONFIG_RESPONSE)) {
+    } else if (strcmp(replyOID, NMASLDAP_GET_LOGIN_CONFIG_RESPONSE)) {
+        /* Is this what we were expecting to get back. */
         err = LDAP_NOT_SUPPORTED;
-        goto Cleanup;
-    }
+    } else if (!replyBV) {
+        /* Do we have a good returned berval? */
 
-    /* Do we have a good returned berval? */
-    if (!replyBV) {
         /* No; returned berval means we experienced a rather drastic error. */
         /* Return operations error. */
         err = LDAP_OPERATIONS_ERROR;
-        goto Cleanup;
-    }
+    } else {
 
-    err = berDecodeLoginData(replyBV, &serverVersion, dataLen, data);
+        err = berDecodeLoginData(replyBV, &serverVersion, dataLen, data);
 
-    if (serverVersion != NMAS_LDAP_EXT_VERSION) {
-        err = LDAP_OPERATIONS_ERROR;
-        goto Cleanup;
+        if (serverVersion != NMAS_LDAP_EXT_VERSION) {
+            err = LDAP_OPERATIONS_ERROR;
+        }
     }
 
-Cleanup:
-
     if (replyBV) {
         ber_bvfree(replyBV);
     }
@@ -385,7 +356,7 @@ static int nmasldap_get_simple_pwd(
     size_t  pwdBufLen, bufferLen;
 
     bufferLen = pwdBufLen = pwdLen+2;
-    pwdBuf = SMB_MALLOC_ARRAY(char, pwdBufLen); /* digest and null */
+    pwdBuf = (char*)SMB_MALLOC_ARRAY(char, pwdBufLen); /* digest and null */
     if (pwdBuf == NULL) {
         return LDAP_NO_MEMORY;
     }
@@ -451,58 +422,42 @@ static int nmasldap_get_password(
     }
 
     bufferLen = pwdBufLen = *pwdSize;
-    pwdBuf = SMB_MALLOC_ARRAY(char, pwdBufLen+2);
+    pwdBuf = (char*)SMB_MALLOC_ARRAY(char, pwdBufLen+2);
     if (pwdBuf == NULL) {
         return LDAP_NO_MEMORY;
     }
 
     err = berEncodePasswordData(&requestBV, objectDN, NULL, NULL);
     if (err) {
-        goto Cleanup;
-    }
-
-    /* Call the ldap_extended_operation (synchronously) */
-    if ((err = ldap_extended_operation_s(ld, NMASLDAP_GET_PASSWORD_REQUEST, requestBV, NULL, NULL, &replyOID, &replyBV))) {
-        goto Cleanup;
-    }
-
-    /* Make sure there is a return OID */
-    if (!replyOID) {
+        ;
+    } else if ((err = ldap_extended_operation_s(ld, NMASLDAP_GET_PASSWORD_REQUEST, requestBV, NULL, NULL, &replyOID, &replyBV))) {
+        ; /* Call the ldap_extended_operation (synchronously) */
+    } else if (!replyOID) {
+        /* Make sure there is a return OID */
         err = LDAP_NOT_SUPPORTED;
-        goto Cleanup;
-    }
-
-    /* Is this what we were expecting to get back. */
-    if (strcmp(replyOID, NMASLDAP_GET_PASSWORD_RESPONSE)) {
+    } else if (strcmp(replyOID, NMASLDAP_GET_PASSWORD_RESPONSE)) {
+        /* Is this what we were expecting to get back. */
         err = LDAP_NOT_SUPPORTED;
-        goto Cleanup;
-    }
-
-    /* Do we have a good returned berval? */
-    if (!replyBV) {
+    } else if (!replyBV) {
+        /* Do we have a good returned berval? */
         /* No; returned berval means we experienced a rather drastic error. */
         /* Return operations error. */
         err = LDAP_OPERATIONS_ERROR;
-        goto Cleanup;
-    }
-
-    err = berDecodeLoginData(replyBV, &serverVersion, &pwdBufLen, pwdBuf);
+    } else {
+        err = berDecodeLoginData(replyBV, &serverVersion, &pwdBufLen, pwdBuf);
 
-    if (serverVersion != NMAS_LDAP_EXT_VERSION) {
-        err = LDAP_OPERATIONS_ERROR;
-        goto Cleanup;
-    }
+        if (serverVersion != NMAS_LDAP_EXT_VERSION) {
+            err = LDAP_OPERATIONS_ERROR;
 
-    if (!err && pwdBufLen != 0) {
-        if (*pwdSize >= pwdBufLen+1 && pwd != NULL) {
-            memcpy(pwd, pwdBuf, pwdBufLen);
-            pwd[pwdBufLen] = 0; /* add null termination */
+        } else if (!err && pwdBufLen != 0) {
+            if (*pwdSize >= pwdBufLen+1 && pwd != NULL) {
+                memcpy(pwd, pwdBuf, pwdBufLen);
+                pwd[pwdBufLen] = 0; /* add null termination */
+            }
+            *pwdSize = pwdBufLen; /* does not include null termination */
         }
-        *pwdSize = pwdBufLen; /* does not include null termination */
     }
 
-Cleanup:
-
     if (replyBV) {
         ber_bvfree(replyBV);
     }
similarity index 93%
rename from helpers/digest_auth/eDirectory/ldap_backend.c
rename to helpers/digest_auth/eDirectory/ldap_backend.cc
index 1b1591234a4bb7ff61f994c8a7645ac35827fa43..6d69aa9af03a463c25439690f43eaa8353bdeafc 100644 (file)
@@ -63,7 +63,6 @@ static int encrpass = 0;
 static int searchscope = LDAP_SCOPE_SUBTREE;
 static int persistent = 0;
 static int noreferrals = 0;
-static int show_debug_messages = 0;
 static int port = LDAP_PORT;
 static int strip_nt_domain = 0;
 static int edir_universal_passwd = 0;
@@ -95,7 +94,7 @@ squid_ldap_set_aliasderef(int deref)
 static void
 squid_ldap_set_referrals(int referrals)
 {
-    int *value = referrals ? LDAP_OPT_ON :LDAP_OPT_OFF;
+    int *value = static_cast<int*>(referrals ? LDAP_OPT_ON :LDAP_OPT_OFF);
     ldap_set_option(ld, LDAP_OPT_REFERRALS, value);
 }
 static void
@@ -144,7 +143,7 @@ squid_ldap_set_timelimit(int aTimeLimit)
 static void
 squid_ldap_set_connect_timeout(int aTimeLimit)
 {
-    fprintf(stderr, "Connect timeouts not supported in your LDAP library\n");
+    fprintf(stderr, "ERROR: Connect timeouts not supported in your LDAP library\n");
 }
 static void
 squid_ldap_memfree(char *p)
@@ -211,8 +210,7 @@ getpassword(char *login, char *realm)
             snprintf(filter, sizeof(filter), usersearchfilter, escaped_login, escaped_login, escaped_login, escaped_login, escaped_login, escaped_login, escaped_login, escaped_login, escaped_login, escaped_login, escaped_login, escaped_login, escaped_login, escaped_login, escaped_login, escaped_login);
 
 retrysrch:
-            if (show_debug_messages)
-                fprintf(stderr, "user filter '%s', searchbase '%s'\n", filter, searchbase);
+            debug("user filter '%s', searchbase '%s'\n", filter, searchbase);
 
             rc = ldap_search_s(ld, searchbase, searchscope, filter, NULL, 0, &res);
             if (rc != LDAP_SUCCESS) {
@@ -247,31 +245,26 @@ retrysrch:
             snprintf(searchbase, 8192, "%s=%s, %s", userdnattr, login, userbasedn);
 
 retrydnattr:
-            if (show_debug_messages)
-                fprintf(stderr, "searchbase '%s'\n", searchbase);
+            debug("searchbase '%s'\n", searchbase);
             rc = ldap_search_s(ld, searchbase, searchscope, NULL, NULL, 0, &res);
         }
         if (rc == LDAP_SUCCESS) {
             entry = ldap_first_entry(ld, res);
             if (entry) {
-                if (show_debug_messages)
-                    printf("ldap dn: %s\n", ldap_get_dn(ld, entry));
+                debug("ldap dn: %s\n", ldap_get_dn(ld, entry));
                 if (edir_universal_passwd) {
 
                     /* allocate some memory for the universal password returned by NMAS */
-                    universal_password = malloc(universal_password_len);
-                    memset(universal_password, 0, universal_password_len);
-                    values = malloc(sizeof(char *));
+                    universal_password = (char*)calloc(1, universal_password_len);
+                    values = (char**)calloc(1, sizeof(char *));
 
                     /* actually talk to NMAS to get a password */
                     nmas_res = nds_get_password(ld, ldap_get_dn(ld, entry), &universal_password_len, universal_password);
                     if (nmas_res == LDAP_SUCCESS && universal_password) {
-                        if (show_debug_messages)
-                            printf("NMAS returned value %s\n", universal_password);
+                        debug("NMAS returned value %s\n", universal_password);
                         values[0] = universal_password;
                     } else {
-                        if (show_debug_messages)
-                            printf("Error reading Universal Password: %d = %s\n", nmas_res, ldap_err2string(nmas_res));
+                        debug("Error reading Universal Password: %d = %s\n", nmas_res, ldap_err2string(nmas_res));
                     }
                 } else {
                     values = ldap_get_values(ld, entry, passattr);
@@ -281,8 +274,7 @@ retrydnattr:
                 return NULL;
             }
             if (!values) {
-                if (show_debug_messages)
-                    printf("No attribute value found\n");
+                debug("No attribute value found\n");
                 if (edir_universal_passwd)
                     free(universal_password);
                 ldap_msgfree(res);
@@ -301,8 +293,7 @@ retrydnattr:
                 }
                 value++;
             }
-            if (show_debug_messages)
-                printf("password: %s\n", password);
+            debug("password: %s\n", password);
             if (password)
                 password = xstrdup(password);
             if (edir_universal_passwd) {
@@ -419,8 +410,7 @@ ldapconnect(void)
                 ld = NULL;
             }
         }
-        if (show_debug_messages)
-            fprintf(stderr, "Connected OK\n");
+        debug("Connected OK\n");
     }
 }
 int
@@ -465,7 +455,7 @@ LDAPArguments(int argc, char **argv)
         case 'h':
             if (ldapServer) {
                 int len = strlen(ldapServer) + 1 + strlen(value) + 1;
-                char *newhost = malloc(len);
+                char *newhost = (char*)malloc(len);
                 snprintf(newhost, len, "%s %s", ldapServer, value);
                 free(ldapServer);
                 ldapServer = newhost;
@@ -576,7 +566,7 @@ LDAPArguments(int argc, char **argv)
             break;
 #endif
         case 'd':
-            show_debug_messages = 1;
+            debug_enabled = 1;
             break;
         case 'E':
             strip_nt_domain = 1;
@@ -594,7 +584,7 @@ LDAPArguments(int argc, char **argv)
         char *value = argv[1];
         if (ldapServer) {
             int len = strlen(ldapServer) + 1 + strlen(value) + 1;
-            char *newhost = malloc(len);
+            char *newhost = (char*)malloc(len);
             snprintf(newhost, len, "%s %s", ldapServer, value);
             free(ldapServer);
             ldapServer = newhost;