From bbea3827d35fb168442d4ffefba7ffa4e59648d5 Mon Sep 17 00:00:00 2001
From: William Brown <wibrown@redhat.com>
Date: Tue, 12 Apr 2016 17:34:27 +1000
Subject: [PATCH] Ticket 48797 - Add freebsd support to ns-slapd

Description: This adds support to ns-slapd to run on FreeBSD. It is an
initial build of this functionality, and may yet have issues. It will
likely rely on OpenSSL for Openldap also.

This fixes kerberos library detection and support, number of portability issues
in the headers, and much more.

https://fedorahosted.org/389/ticket/48797

Author: wibrown

Reviewed by: ???
---
 Makefile.am                                 | 12 +++++++---
 configure.ac                                | 10 ++++++++
 include/base/systems.h                      | 26 +++++++++++++++++++++
 include/public/base/systems.h               |  9 ++++++++
 ldap/include/portable.h                     |  8 +++----
 ldap/servers/plugins/pwdstorage/crypt_pwd.c |  2 +-
 ldap/servers/slapd/back-ldbm/back-ldbm.h    |  6 +++++
 ldap/servers/slapd/daemon.c                 | 23 +++++++++++++-----
 ldap/servers/slapd/getsocketpeer.c          |  7 ++++++
 ldap/servers/slapd/ldaputil.c               | 26 +++++++++++++++++++++
 ldap/servers/slapd/localhost.c              |  2 +-
 ldap/servers/slapd/main.c                   |  6 ++++-
 ldap/servers/slapd/tools/ldclt/ldclt.h      |  5 ++++
 ldap/servers/slapd/tools/pwenc.c            |  2 +-
 ldap/servers/slapd/uniqueidgen.c            |  5 ++++
 ldap/servers/slapd/uuid.c                   |  4 ++++
 ldap/systools/idsktune.c                    |  4 ++++
 lib/base/crit.cpp                           | 36 ++++++++++++++---------------
 lib/base/dnsdmain.cpp                       |  2 +-
 lib/base/file.cpp                           |  2 +-
 lib/ldaputil/cert.c                         |  4 +++-
 lib/ldaputil/certmap.c                      |  4 +++-
 lib/ldaputil/dbconf.c                       |  4 +++-
 lib/ldaputil/encode.c                       |  4 +++-
 m4/kerberos.m4                              | 22 ++++++++++--------
 25 files changed, 184 insertions(+), 51 deletions(-)

diff --git a/Makefile.am b/Makefile.am
index 57b6b52..0c2de22 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -49,7 +49,7 @@ AM_LDFLAGS = -lpthread
 else
 #AM_LDFLAGS = -Wl,-z,defs
 AM_LDFLAGS = $(ASAN_DEFINES)
-endif
+endif #end hpux
 
 #------------------------
 # Linker Flags
@@ -77,7 +77,13 @@ PCRE_LINK = @pcre_lib@ -lpcre
 NETSNMP_LINK = @netsnmp_lib@ @netsnmp_link@
 PAM_LINK = -lpam
 KERBEROS_LINK = $(kerberos_lib)
+
+if FREEBSD
+DLOPEN_LINK = 
+else
 DLOPEN_LINK = -ldl
+endif
+
 SYSTEMD_LINK = @systemd_lib@
 
 
@@ -1901,7 +1907,7 @@ endif
 if SYSTEMD
 	$(fixupcmd) $^ | sed -e 's/@preamble@/# This file is in systemd EnvironmentFile format - see man systemd.exec/' > $@
 else
-	$(fixupcmd) $^ | sed -n -e 's/@preamble@//' -e '/^#/{p;d}' -e '/^$$/{p;d}' -e 's/^\([^=]*\)\(=.*\)$$/\1\2 ; export \1/ ; p' > $@
+	$(fixupcmd) $^ | sed -n -e 's/@preamble@//' -e '/^#/{p;d;}' -e '/^$$/{p;d;}' -e 's/^\([^=]*\)\(=.*\)$$/\1\2 ; export \1/ ; p' > $@
 	$(fixupcmd) $(srcdir)/ldap/admin/src/initconfig.in >> $@
 endif
 
@@ -1910,7 +1916,7 @@ endif
 if SYSTEMD
 	$(fixupcmd) $^ | sed -e 's/@preamble@/# This file is in systemd EnvironmentFile format - see man systemd.exec/' > $@
 else
-	$(fixupcmd) $^ | sed -n -e 's/@preamble@//' -e '/^#/{p;d}' -e '/^$$/{p;d}' -e 's/^\([^=]*\)\(=.*\)$$/\1\2 ; export \1/ ; p' > $@
+	$(fixupcmd) $^ | sed -n -e 's/@preamble@//' -e '/^#/{p;d;}' -e '/^$$/{p;d;}' -e 's/^\([^=]*\)\(=.*\)$$/\1\2 ; export \1/ ; p' > $@
 endif
 
 %/$(PACKAGE_NAME).pc: %/dirsrv.pc.in
diff --git a/configure.ac b/configure.ac
index 4be4613..b4d35d0 100644
--- a/configure.ac
+++ b/configure.ac
@@ -536,6 +536,15 @@ case $host in
     AC_SUBST([LIBCRYPT], [$LIBCRYPT])
     AC_DEFINE([USE_POSIX_RWLOCKS], [1], [POSIX rwlocks])
     ;;
+  *-*-freebsd*)
+    AC_DEFINE([FREEBSD], [1], [FreeBSD])
+    platform="freebsd"
+    initdir='$(sysconfdir)/rc.d'
+    THREADLIB=-lthr
+    AC_SUBST([THREADLIB], [$THREADLIB])
+    AC_DEFINE([USE_POSIX_RWLOCKS], [1], [POSIX rwlocks])
+    LIBDL=
+    ;;
   ia64-hp-hpux*)
     AC_DEFINE([hpux], [1], [HP-UX])
     AC_DEFINE([HPUX], [1], [HP-UX])
@@ -638,6 +647,7 @@ AC_SUBST(initconfigdir)
 
 AM_CONDITIONAL([HPUX],[test "$platform" = "hpux"])
 AM_CONDITIONAL([SOLARIS],[test "$platform" = "solaris"])
+AM_CONDITIONAL([FREEBSD],[test "$platform" = "freebsd"])
 
 # Check for library dependencies
 m4_include(m4/nspr.m4)
diff --git a/include/base/systems.h b/include/base/systems.h
index 5f14969..1597359 100644
--- a/include/base/systems.h
+++ b/include/base/systems.h
@@ -142,6 +142,32 @@
 #define NET_SOCKETS
 #define SHMEM_MMAP_FLAGS MAP_SHARED
 
+#elif defined(__FreeBSD__)
+
+#define ACCELERATOR_CACHE
+#define DNS_CACHE
+#define FILE_INHERIT_FCNTL
+#define DAEMON_UNIX_MOBRULE
+#define BSD_RLIMIT
+#define BSD_SIGNALS
+#define FILE_UNIX_MMAP
+#define FILE_MMAP_FLAGS (MAP_FILE | MAP_SHARED)
+#define SHMEM_UNIX_MMAP
+#define SHMEM_MMAP_FLAGS MAP_SHARED
+#define AUTH_DBM
+#define SEM_FLOCK
+#define DLL_CAPABLE
+#define DLL_DLOPEN
+#define DLL_DLOPEN_FLAGS RTLD_NOW
+#define HAVE_ATEXIT
+#define HAS_STATFS
+#define JAVA_STATIC_LINK
+#undef NEED_CRYPT_PROTO
+#define NET_SOCKETS
+#ifndef NO_DOMAINNAME
+#define NO_DOMAINNAME
+#endif
+
 #else
 #error "Missing defines in ns/netsite/include/base/systems.h"
 #endif
diff --git a/include/public/base/systems.h b/include/public/base/systems.h
index 535a35c..ba36502 100644
--- a/include/public/base/systems.h
+++ b/include/public/base/systems.h
@@ -65,6 +65,15 @@
 #define SHMEM_UNIX_MMAP
 #define ZERO(ptr,len) memset(ptr,0,len)
 
+#elif defined(__FreeBSD__)
+
+#define FILE_UNIX
+#define FILE_UNIX_MMAP
+#define MALLOC_POOLS
+#define SEM_FLOCK
+#define SHMEM_UNIX_MMAP
+#define ZERO(ptr,len) memset(ptr,0,len)
+
 #else
 #error "Missing defines in ns/netsite/include/public/base/systems.h"
 #endif
diff --git a/ldap/include/portable.h b/ldap/include/portable.h
index ea164c0..0f454ce 100644
--- a/ldap/include/portable.h
+++ b/ldap/include/portable.h
@@ -124,7 +124,7 @@
  * Are sys_errlist and sys_nerr declared in stdio.h?
  */
 #ifndef SYSERRLIST_IN_STDIO
-#if defined( freebsd ) || defined(Linux)
+#if defined( __FreeBSD__ ) || defined(Linux)
 #define SYSERRLIST_IN_STDIO
 #endif
 #endif
@@ -250,7 +250,7 @@ int strncasecmp(const char *, const char *, size_t);
     defined(UNIXWARE) || defined(SUNOS4) || defined(SNI) || defined(BSDI) || \
     defined(NCR) || defined(OSF1) || defined(NEC) || \
     ( defined(HPUX10) && !defined(_REENTRANT)) || defined(HPUX11) || \
-    defined(UnixWare) || defined(LINUX2_0)
+    defined(UnixWare) || defined(LINUX2_0) || defined (__FreeBSD__)
 #define GETHOSTBYNAME( n, r, b, l, e )  gethostbyname( n )
 #elif defined(AIX)
 #define GETHOSTBYNAME_BUF_T struct hostent_data
@@ -282,7 +282,7 @@ typedef char GETHOSTBYADDR_buf_t [BUFSIZ];
 
 
 #if defined(HPUX9) || defined(LINUX1_2) || defined(SUNOS4) || defined(SNI) || \
-    defined(SCOOS) || defined(BSDI) || defined(NCR) || \
+    defined(SCOOS) || defined(BSDI) || defined(NCR) || defined (__FreeBSD__) || \
     defined(NEC) || ( defined(HPUX10) && !defined(_REENTRANT)) 
 #define CTIME( c, b, l )		ctime( c )
 #elif defined( hpux10 )
@@ -312,7 +312,7 @@ char *strtok_r(char *, const char *, char **);
 extern char *strdup();
 #endif /* ultrix || nextstep */
 
-#if defined( sunos4 ) || defined( OSF1 )
+#if defined( sunos4 ) || defined( OSF1 ) || defined (__FreeBSD__)
 #define	BSD_TIME	1	/* for servers/slapd/log.h */
 #endif /* sunos4 || osf */
 
diff --git a/ldap/servers/plugins/pwdstorage/crypt_pwd.c b/ldap/servers/plugins/pwdstorage/crypt_pwd.c
index 29355a2..8b031ed 100644
--- a/ldap/servers/plugins/pwdstorage/crypt_pwd.c
+++ b/ldap/servers/plugins/pwdstorage/crypt_pwd.c
@@ -20,7 +20,7 @@
 #include <string.h>
 #include <sys/types.h>
 #include <sys/socket.h>
-#if defined( hpux ) || defined (LINUX)
+#if defined( hpux ) || defined (LINUX) || defined (__FreeBSD__)
 #ifndef __USE_XOPEN
 #define __USE_XOPEN     /* linux */
 #endif /* __USE_XOPEN */
diff --git a/ldap/servers/slapd/back-ldbm/back-ldbm.h b/ldap/servers/slapd/back-ldbm/back-ldbm.h
index 9499292..7807564 100644
--- a/ldap/servers/slapd/back-ldbm/back-ldbm.h
+++ b/ldap/servers/slapd/back-ldbm/back-ldbm.h
@@ -16,6 +16,12 @@
 #ifndef _BACK_LDBM_H_
 #define _BACK_LDBM_H_
 
+/* This is not avaliable in BSD errno, copy linux */
+/* Is it possible to define this as ELAST? */
+#ifdef __FreeBSD__
+#define ENODATA 96
+#endif
+
 #if defined(HPUX11) || defined(OS_solaris) || defined(linux)
 /* built-in 64-bit file I/O support */
 #define DB_USE_64LFS
diff --git a/ldap/servers/slapd/daemon.c b/ldap/servers/slapd/daemon.c
index 91ad13e..70f858d 100644
--- a/ldap/servers/slapd/daemon.c
+++ b/ldap/servers/slapd/daemon.c
@@ -38,10 +38,14 @@
 #endif /* NEED_FILIO */
 /* for some reason, linux tty stuff defines CTIME */
 #include <stdio.h>
+#if defined(LINUX) || defined(__FreeBSD__)
 #ifdef LINUX
 #undef CTIME
 #include <sys/statfs.h>
-#else
+#endif /* linux*/
+#include <sys/param.h>
+#include <sys/mount.h>
+#else  /* Linux or fbsd */
 #include <sys/statvfs.h>
 #include <sys/mnttab.h>
 #endif
@@ -283,7 +287,7 @@ disk_mon_get_mount_point(char *dir)
 
     return NULL;
 }
-#else /* Linux */
+#elif LINUX /* Linux */
 char *
 disk_mon_get_mount_point(char *dir)
 {
@@ -313,6 +317,13 @@ disk_mon_get_mount_point(char *dir)
 
     return NULL;
 }
+#elif __FreeBSD__
+char *
+disk_mon_get_mount_point(char *dir)
+{
+	/* CRITICAL TO FINISH THIS */
+	return NULL;
+}
 #endif
 
 /*
@@ -381,7 +392,7 @@ disk_mon_get_dirs(char ***list, int logs_critical){
 char *
 disk_mon_check_diskspace(char **dirs, PRUint64 threshold, PRUint64 *disk_space)
 {
-#ifdef LINUX
+#if defined(LINUX) || defined(__FreeBSD__)
     struct statfs buf;
 #else
     struct statvfs buf;
@@ -394,10 +405,10 @@ disk_mon_check_diskspace(char **dirs, PRUint64 threshold, PRUint64 *disk_space)
     int i = 0;
 
     for(i = 0; dirs && dirs[i]; i++){
-#ifndef LINUX
-        if (statvfs(dirs[i], &buf) != -1)
-#else
+#if defined(LINUX) || defined(__FreeBSD__)
         if (statfs(dirs[i], &buf) != -1)
+#else
+        if (statvfs(dirs[i], &buf) != -1)
 #endif
         {
             LL_UI2L(freeBytes, buf.f_bavail);
diff --git a/ldap/servers/slapd/getsocketpeer.c b/ldap/servers/slapd/getsocketpeer.c
index 255322b..062db83 100644
--- a/ldap/servers/slapd/getsocketpeer.c
+++ b/ldap/servers/slapd/getsocketpeer.c
@@ -96,10 +96,13 @@ int slapd_get_socket_peer(PRFileDesc *nspr_fd, uid_t *uid, gid_t *gid)
 	iov.iov_len = sizeof(dummy);
 	msg.msg_iov = &iov;
 	msg.msg_iovlen = 1;
+#ifndef __FreeBSD__
 	msg.msg_accrights = (caddr_t)&pass_sd;
 	msg.msg_accrightslen = sizeof(pass_sd); /* Initialize it with 8 bytes. 
 											   If recvmsg is successful,
 											   4 is supposed to be returned. */
+
+#endif
 	/* 
 	   Since PR_SockOpt_Nonblocking is set to the socket,
 	   recvmsg returns immediately if no data is waiting to be received.
@@ -109,7 +112,11 @@ int slapd_get_socket_peer(PRFileDesc *nspr_fd, uid_t *uid, gid_t *gid)
 	while ((rc = recvmsg(fd, &msg, MSG_PEEK)) < 0 && (EAGAIN == (myerrno = errno)) && retrycnt-- >= 0)
 		;
 
+#ifdef __FreeBSD__
+	if (rc >= 0)
+#else
 	if (rc >= 0 && msg.msg_accrightslen == sizeof(int))
+#endif
 	{
 		struct stat st;
 
diff --git a/ldap/servers/slapd/ldaputil.c b/ldap/servers/slapd/ldaputil.c
index 3851be5..50df1e7 100644
--- a/ldap/servers/slapd/ldaputil.c
+++ b/ldap/servers/slapd/ldaputil.c
@@ -66,6 +66,15 @@
 #include <ldap_ssl.h>
 #include <ldappr.h>
 #else
+
+#ifdef HAVE_HEIMDAL_KERBEROS
+#include <com_err.h>
+#endif
+
+#ifndef MAX_KEYTAB_NAME_LEN
+#define MAX_KEYTAB_NAME_LEN 1100
+#endif
+
 /* need mutex around ldap_initialize - see https://fedorahosted.org/389/ticket/348 */
 static PRCallOnceType ol_init_callOnce = {0,0};
 static PRLock *ol_init_lock = NULL;
@@ -1581,7 +1590,11 @@ show_one_credential(int authtracelevel,
     char *logname = "show_one_credential";
     krb5_error_code rc;
     char *name = NULL, *sname = NULL;
+#ifdef HAVE_HEIMDAL_KERBEROS
+    krb5_timestamp startts, endts, renewts;
+#else
     char startts[BUFSIZ], endts[BUFSIZ], renewts[BUFSIZ];
+#endif
 
     if ((rc = krb5_unparse_name(ctx, cred->client, &name))) {
         slapi_log_error(SLAPI_LOG_FATAL, logname,
@@ -1598,6 +1611,13 @@ show_one_credential(int authtracelevel,
     if (!cred->times.starttime) {
         cred->times.starttime = cred->times.authtime;
     }
+#ifdef HAVE_HEIMDAL_KERBEROS
+    slapi_log_error(authtracelevel, logname,
+                    "\tKerberos credential: client [%s] server [%s] "
+                    "start time [%s] end time [%s] renew time [%s] "
+                    "flags [0x%x]\n", name, sname, ctime(&startts), ctime(&endts),
+                    ctime(&renewts), (uint32_t)cred->flags.i);
+#else
     krb5_timestamp_to_sfstring((krb5_timestamp)cred->times.starttime,
                                startts, sizeof(startts), NULL);
     krb5_timestamp_to_sfstring((krb5_timestamp)cred->times.endtime,
@@ -1610,6 +1630,7 @@ show_one_credential(int authtracelevel,
                     "start time [%s] end time [%s] renew time [%s] "
                     "flags [0x%x]\n", name, sname, startts, endts,
                     renewts, (uint32_t)cred->ticket_flags);
+#endif
 
 cleanup:
     krb5_free_unparsed_name(ctx, name);
@@ -1712,8 +1733,13 @@ credentials_are_valid(
        order to set mcreds.server required in order
        to use krb5_cc_retrieve_creds() */
     /* get default realm first */
+#ifdef HAVE_HEIMDAL_KERBEROS
+    realm_str = krb5_principal_get_realm(ctx, princ);
+    realm_len = krb5_realm_length(realm_str);
+#else
     realm_len = krb5_princ_realm(ctx, princ)->length;
     realm_str = krb5_princ_realm(ctx, princ)->data;
+#endif
     tgs_princ_name = slapi_ch_smprintf("%s/%*s@%*s", KRB5_TGS_NAME,
                                        realm_len, realm_str,
                                        realm_len, realm_str);
diff --git a/ldap/servers/slapd/localhost.c b/ldap/servers/slapd/localhost.c
index 2c40493..bedd7a3 100644
--- a/ldap/servers/slapd/localhost.c
+++ b/ldap/servers/slapd/localhost.c
@@ -24,7 +24,7 @@
 #include <resolv.h>
 #include <errno.h>
 #include "slap.h"
-#if defined(USE_SYSCONF) || defined(LINUX)
+#if defined(USE_SYSCONF) || defined(LINUX) || defined( __FreeBSD__ )
 #include <unistd.h>
 #endif /* USE_SYSCONF */
 
diff --git a/ldap/servers/slapd/main.c b/ldap/servers/slapd/main.c
index c04408c..e089113 100644
--- a/ldap/servers/slapd/main.c
+++ b/ldap/servers/slapd/main.c
@@ -11,7 +11,11 @@
 #  include <config.h>
 #endif
 
+#ifdef __FreeBSD__
+#include <stdlib.h>
+#else
 #include <malloc.h>
+#endif
 #include <ldap.h>
 #undef OFF
 #undef LITTLE_ENDIAN
@@ -31,7 +35,7 @@
 #include <arpa/inet.h>
 #include <netdb.h>
 #include <pwd.h> /* getpwnam */
-#if !defined(LINUX)
+#if !defined(LINUX) && !defined(__FreeBSD__)
 union semun {
     int val;
     struct semid_ds *buf;
diff --git a/ldap/servers/slapd/tools/ldclt/ldclt.h b/ldap/servers/slapd/tools/ldclt/ldclt.h
index 4f8f485..228ac19 100644
--- a/ldap/servers/slapd/tools/ldclt/ldclt.h
+++ b/ldap/servers/slapd/tools/ldclt/ldclt.h
@@ -172,6 +172,11 @@ dd/mm/yy | Author	| Comments
 #if defined(USE_OPENLDAP)
 #define ABS(x) ((x > 0) ? (x) : (-x))
 #endif
+
+#if defined(__FreeBSD__)
+#include <sys/time.h>
+#endif
+
 /*
  * Misc constant definitions
  */
diff --git a/ldap/servers/slapd/tools/pwenc.c b/ldap/servers/slapd/tools/pwenc.c
index c5ed37c..6968d0e 100644
--- a/ldap/servers/slapd/tools/pwenc.c
+++ b/ldap/servers/slapd/tools/pwenc.c
@@ -16,7 +16,7 @@
 #include <sys/errno.h>
 #include <sys/param.h>
 #include <sys/types.h>
-#if defined(LINUX) /* I bet other Unix would like
+#if defined(LINUX) || defined(__FreeBSD__) /* I bet other Unix would like
 					* this flag. But don't want to
 					* break other builds so far */
 #include <unistd.h>
diff --git a/ldap/servers/slapd/uniqueidgen.c b/ldap/servers/slapd/uniqueidgen.c
index 3fbe53c..9664890 100644
--- a/ldap/servers/slapd/uniqueidgen.c
+++ b/ldap/servers/slapd/uniqueidgen.c
@@ -16,7 +16,12 @@
 #include <string.h>
 #include <sys/types.h>
 #include <sys/time.h>
+
+/* What platforms actually need this? */
+#ifndef __FreeBSD__
 #include <sys/sysinfo.h>
+#endif
+
 #include <sys/utsname.h>
 #include "nspr.h"
 #include "slap.h"
diff --git a/ldap/servers/slapd/uuid.c b/ldap/servers/slapd/uuid.c
index 83415b4..4db4c55 100644
--- a/ldap/servers/slapd/uuid.c
+++ b/ldap/servers/slapd/uuid.c
@@ -40,7 +40,11 @@
 #include <pk11func.h>
 #include <sys/types.h>
 #include <sys/time.h>
+
+#ifndef __FreeBSD__
 #include <sys/sysinfo.h>
+#endif
+
 #include <sys/utsname.h>
 #include <unistd.h>     /* gethostname() */
 #include "slap.h"
diff --git a/ldap/systools/idsktune.c b/ldap/systools/idsktune.c
index 6a24b0e..3442135 100644
--- a/ldap/systools/idsktune.c
+++ b/ldap/systools/idsktune.c
@@ -26,6 +26,10 @@ static char *build_date = "23-FEBRUARY-2012";
 #define IDDS_SYSV_INCLUDE 1
 #endif
 
+#if defined(__FreeBSD__)
+#define IDDS_BSD_INCLUDE 1
+#endif
+
 #include <sys/types.h>
 
 #if !defined(__VMS)
diff --git a/lib/base/crit.cpp b/lib/base/crit.cpp
index e7daf17..ccc1de4 100644
--- a/lib/base/crit.cpp
+++ b/lib/base/crit.cpp
@@ -42,12 +42,12 @@ typedef struct critical {
 	PRLock		*lock;
 	PRUint32	count;
 	PRThread	*owner;
-} critical_t;
+} ns_critical_t;
 
 typedef struct condvar {
-	critical_t	*lock;
+	ns_critical_t	*lock;
 	PRCondVar	*cvar;
-} condvar_t;
+} ns_condvar_t;
 
 #endif
 /* -------------------------- critical sections --------------------------- */
@@ -57,7 +57,7 @@ typedef struct condvar {
 NSAPI_PUBLIC int crit_owner_is_me(CRITICAL id)
 {
 #ifdef USE_NSPR
-    critical_t *crit = (critical_t*)id;
+    ns_critical_t *crit = (ns_critical_t*)id;
 
     return (crit->owner == PR_GetCurrentThread());
 #else
@@ -68,7 +68,7 @@ NSAPI_PUBLIC int crit_owner_is_me(CRITICAL id)
 NSAPI_PUBLIC CRITICAL crit_init(void)
 {
 #ifdef USE_NSPR
-    critical_t *crit = (critical_t*)PERM_MALLOC(sizeof(critical_t)) ;
+    ns_critical_t *crit = (ns_critical_t*)PERM_MALLOC(sizeof(ns_critical_t)) ;
 
     if (crit) {
         if (!(crit->lock = PR_NewLock())) {
@@ -87,7 +87,7 @@ NSAPI_PUBLIC CRITICAL crit_init(void)
 NSAPI_PUBLIC void crit_enter(CRITICAL id)
 {
 #ifdef USE_NSPR
-    critical_t *crit = (critical_t*)id;
+    ns_critical_t *crit = (ns_critical_t*)id;
     PRThread *me = PR_GetCurrentThread();
 
     if ( crit->owner == me) {
@@ -106,7 +106,7 @@ NSAPI_PUBLIC void crit_enter(CRITICAL id)
 NSAPI_PUBLIC void crit_exit(CRITICAL id)
 {
 #ifdef USE_NSPR
-    critical_t	*crit = (critical_t*)id;
+    ns_critical_t	*crit = (ns_critical_t*)id;
 
     if (crit->owner != PR_GetCurrentThread()) 
         return;
@@ -121,7 +121,7 @@ NSAPI_PUBLIC void crit_exit(CRITICAL id)
 NSAPI_PUBLIC void crit_terminate(CRITICAL id)
 {
 #ifdef USE_NSPR
-    critical_t	*crit = (critical_t*)id;
+    ns_critical_t	*crit = (ns_critical_t*)id;
 
     PR_DestroyLock((PRLock*)crit->lock);
     PERM_FREE(crit);
@@ -135,9 +135,9 @@ NSAPI_PUBLIC void crit_terminate(CRITICAL id)
 NSAPI_PUBLIC CONDVAR condvar_init(CRITICAL id)
 {
 #ifdef USE_NSPR
-    critical_t	*crit = (critical_t*)id;
+    ns_critical_t	*crit = (ns_critical_t*)id;
 
-    condvar_t *cvar = (condvar_t*)PERM_MALLOC(sizeof(condvar_t)) ;
+    ns_condvar_t *cvar = (ns_condvar_t*)PERM_MALLOC(sizeof(ns_condvar_t)) ;
 
     if (crit) {
         cvar->lock = crit;
@@ -153,7 +153,7 @@ NSAPI_PUBLIC CONDVAR condvar_init(CRITICAL id)
 NSAPI_PUBLIC void condvar_wait(CONDVAR _cv)
 {
 #ifdef USE_NSPR
-    condvar_t *cv = (condvar_t *)_cv;
+    ns_condvar_t *cv = (ns_condvar_t *)_cv;
     /* Save away recursion count so we can restore it after the wait */
     int saveCount = cv->lock->count;
     PRThread *saveOwner = cv->lock->owner;
@@ -170,10 +170,10 @@ NSAPI_PUBLIC void condvar_wait(CONDVAR _cv)
 }
 
 
-NSAPI_PUBLIC void condvar_timed_wait(CONDVAR _cv, long secs)
+NSAPI_PUBLIC void ns_condvar_timed_wait(CONDVAR _cv, long secs)
 {
 #ifdef USE_NSPR
-    condvar_t *cv = (condvar_t *)_cv;
+    ns_condvar_t *cv = (ns_condvar_t *)_cv;
     /* Save away recursion count so we can restore it after the wait */
     int saveCount = cv->lock->count;
     PRThread *saveOwner = cv->lock->owner;
@@ -197,7 +197,7 @@ NSAPI_PUBLIC void condvar_timed_wait(CONDVAR _cv, long secs)
 NSAPI_PUBLIC void condvar_notify(CONDVAR _cv)
 {
 #ifdef USE_NSPR
-    condvar_t *cv = (condvar_t *)_cv;
+    ns_condvar_t *cv = (ns_condvar_t *)_cv;
     PR_ASSERT(cv->lock->owner == PR_GetCurrentThread());
     PR_NotifyCondVar(cv->cvar);
 #endif
@@ -206,16 +206,16 @@ NSAPI_PUBLIC void condvar_notify(CONDVAR _cv)
 NSAPI_PUBLIC void condvar_notifyAll(CONDVAR _cv)
 {
 #ifdef USE_NSPR
-    condvar_t *cv = (condvar_t *)_cv;
+    ns_condvar_t *cv = (ns_condvar_t *)_cv;
     PR_ASSERT(cv->lock->owner == PR_GetCurrentThread());
     PR_NotifyAllCondVar(cv->cvar);
 #endif
 }
 
-NSAPI_PUBLIC void condvar_terminate(CONDVAR _cv)
+NSAPI_PUBLIC void ns_condvar_terminate(CONDVAR _cv)
 {
 #ifdef USE_NSPR
-    condvar_t *cv = (condvar_t *)_cv;
+    ns_condvar_t *cv = (ns_condvar_t *)_cv;
     PR_DestroyCondVar(cv->cvar);
     PERM_FREE(cv);
 #endif
@@ -297,7 +297,7 @@ cs_terminate(COUNTING_SEMAPHORE csp)
 	/* usfreesema() */
 	return;
 #else
-	condvar_terminate(cs->cv);
+	ns_condvar_terminate(cs->cv);
 	crit_terminate(cs->cv_lock);
 	crit_terminate(cs->lock);
 	PERM_FREE(cs);
diff --git a/lib/base/dnsdmain.cpp b/lib/base/dnsdmain.cpp
index 545f617..0b522ad 100644
--- a/lib/base/dnsdmain.cpp
+++ b/lib/base/dnsdmain.cpp
@@ -40,7 +40,7 @@ extern int getdomainname(char *, size_t);
 #else 
 extern int getdomainname(char *, int);
 #endif /* Linux */
-#if defined(HPUX) || defined(Linux) || defined(SOLARIS_GCC)
+#if defined(HPUX) || defined(Linux) || defined(SOLARIS_GCC) || defined(__FreeBSD__)
 extern int gethostname (char *name, size_t namelen);
 #else
 extern int gethostname (char *name, int namelen);
diff --git a/lib/base/file.cpp b/lib/base/file.cpp
index 8c9274a..169c780 100644
--- a/lib/base/file.cpp
+++ b/lib/base/file.cpp
@@ -214,7 +214,7 @@ NSAPI_PUBLIC int file_notfound(void)
     return (errno == ENOENT);
 }
 
-#if !defined(LINUX)
+#if !defined(LINUX) && !defined(__FreeBSD__)
 extern char *sys_errlist[];
 #endif
 
diff --git a/lib/ldaputil/cert.c b/lib/ldaputil/cert.c
index dfe77c6..aca4f98 100644
--- a/lib/ldaputil/cert.c
+++ b/lib/ldaputil/cert.c
@@ -13,7 +13,9 @@
 
 
 #include <string.h>
-#include <malloc.h>
+/* This was malloc.h - but it's moved to stdlib.h on most platforms, and FBSD is strict */
+/* Make it stdlib.h, and revert to malloc.h with ifdefs if we have issues here. WB 2016 */
+#include <stdlib.h>
 
 /* removed for ns security integration
 #include <sec.h>
diff --git a/lib/ldaputil/certmap.c b/lib/ldaputil/certmap.c
index b05bbba..4534bac 100644
--- a/lib/ldaputil/certmap.c
+++ b/lib/ldaputil/certmap.c
@@ -15,7 +15,9 @@
 #include <stdio.h>
 #include <string.h>
 #include <ctype.h>
-#include <malloc.h>
+/* This was malloc.h - but it's moved to stdlib.h on most platforms, and FBSD is strict */
+/* Make it stdlib.h, and revert to malloc.h with ifdefs if we have issues here. WB 2016 */
+#include <stdlib.h>
 
 /* removed for ns security integration
 #include <sec.h>
diff --git a/lib/ldaputil/dbconf.c b/lib/ldaputil/dbconf.c
index 048bc56..d5c315b 100644
--- a/lib/ldaputil/dbconf.c
+++ b/lib/ldaputil/dbconf.c
@@ -13,7 +13,9 @@
 
 
 #include <string.h>
-#include <malloc.h>
+/* This was malloc.h - but it's moved to stdlib.h on most platforms, and FBSD is strict */
+/* Make it stdlib.h, and revert to malloc.h with ifdefs if we have issues here. WB 2016 */
+#include <stdlib.h>
 #include <ctype.h>
 
 #include <ldaputil/errors.h>
diff --git a/lib/ldaputil/encode.c b/lib/ldaputil/encode.c
index 267d795..eea3edd 100644
--- a/lib/ldaputil/encode.c
+++ b/lib/ldaputil/encode.c
@@ -12,7 +12,9 @@
 #endif
 
 
-#include <malloc.h>
+/* This was malloc.h - but it's moved to stdlib.h on most platforms, and FBSD is strict */
+/* Make it stdlib.h, and revert to malloc.h with ifdefs if we have issues here. WB 2016 */
+#include <stdlib.h>
 #include <string.h>
 #include <ldaputil/certmap.h>
 #include <ldaputil/encode.h>
diff --git a/m4/kerberos.m4 b/m4/kerberos.m4
index a82f57a..0c51828 100644
--- a/m4/kerberos.m4
+++ b/m4/kerberos.m4
@@ -102,7 +102,19 @@ if test -n "$with_kerberos" ; then
     fi
 fi
 
+dnl check for Heimdal Kerberos
+AC_CHECK_HEADERS(heim_err.h)
+if test $ac_cv_header_heim_err_h = yes ; then
+    krb5_impl=heimdal
+    AC_DEFINE(HAVE_HEIMDAL_KERBEROS, 1,
+        [define if you have HEIMDAL Kerberos])
+
+else
+    krb5_impl=mit
+fi
+
 if test -n "$with_kerberos" -a -z "$kerberos_lib" ; then
+    AC_MSG_RESULT([Checking with kerberos and kerberos_lib])
     # save these in order to set them to use the check macros below
     # like AC_CHECK_HEADERS, AC_CHECK_LIB, and AC_CHECK_FUNCS
     save_CPPFLAGS="$CPPFLAGS"
@@ -113,13 +125,6 @@ if test -n "$with_kerberos" -a -z "$kerberos_lib" ; then
     if test -n "$kerberos_libdir" ; then
         LDFLAGS="-L$kerberos_libdir $LDFLAGS"
     fi
-    krb5_impl=mit
-
-    dnl check for Heimdal Kerberos
-    AC_CHECK_HEADERS(heim_err.h)
-    if test $ac_cv_header_heim_err_h = yes ; then
-        krb5_impl=heimdal
-    fi
 
     if test "x$krb5_impl" = "xmit"; then
         AC_CHECK_LIB(k5crypto, main,
@@ -143,9 +148,6 @@ if test -n "$with_kerberos" -a -z "$kerberos_lib" ; then
             [have_krb5=no],
             [-l$krb5crypto -lasn1 -lroken -lcom_err])
 
-        AC_DEFINE(HAVE_HEIMDAL_KERBEROS, 1,
-            [define if you have HEIMDAL Kerberos])
-
     else
         have_krb5=no
         AC_MSG_WARN([Unrecognized Kerberos5 Implementation])
-- 
2.5.5

