Index: Makefile.in
--- Makefile.in.orig	2012-06-27 18:57:12.000000000 +0200
+++ Makefile.in	2012-07-15 13:34:59.000000000 +0200
@@ -40,7 +40,7 @@
 build_triplet = @build@
 host_triplet = @host@
 sbin_PROGRAMS = ns-slapd$(EXEEXT) ldap-agent-bin$(EXEEXT)
-bin_PROGRAMS = dbscan-bin$(EXEEXT) dsktune-bin$(EXEEXT) \
+bin_PROGRAMS = dbscan-bin$(EXEEXT) \
 	infadd-bin$(EXEEXT) ldclt-bin$(EXEEXT) ldif-bin$(EXEEXT) \
 	migratecred-bin$(EXEEXT) mmldif-bin$(EXEEXT) \
 	pwdhash-bin$(EXEEXT) rsearch-bin$(EXEEXT)
@@ -1331,21 +1331,21 @@
 # Linker Flags
 #------------------------
 NSPR_LINK = @nspr_lib@ -lplc4 -lplds4 -lnspr4
-NSS_LINK = @nss_lib@ -lssl3 -lnss3
+NSS_LINK = @nss_lib@ -lnssl -lnss
 @OPENLDAP_FALSE@LDAPSDK_LINK_NOTHR = $(LDAPSDK_LINK)
 # with recent versions of openldap - if you link with both ldap_r and ldap, the
 # shared lib _fini for one will stomp on the other, and the program will crash
 @OPENLDAP_TRUE@LDAPSDK_LINK_NOTHR = @openldap_lib@ -lldap@ol_libver@ @ldap_lib_ldif@ -llber@ol_libver@
-@OPENLDAP_FALSE@LDAPSDK_LINK = @ldapsdk_lib@ -lssldap60 -lprldap60 -lldap60 -lldif60
+@OPENLDAP_FALSE@LDAPSDK_LINK = @ldapsdk_lib@ -lssldap60 -lprldap60 -lldap60 -lldif60 -llber60
 @OPENLDAP_TRUE@LDAPSDK_LINK = @openldap_lib@ -lldap_r@ol_libver@ @ldap_lib_ldif@ -llber@ol_libver@
 @OPENLDAP_FALSE@ldaplib = mozldap
 @OPENLDAP_TRUE@ldaplib = openldap
 @OPENLDAP_FALSE@ldaplib_defs = 
 @OPENLDAP_TRUE@ldaplib_defs = -DUSE_OPENLDAP
-DB_LINK = @db_lib@ -ldb-@db_libver@
+DB_LINK = @db_lib@ -ldb
 SASL_LINK = @sasl_lib@ -lsasl2
 SVRCORE_LINK = @svrcore_lib@ -lsvrcore
-ICU_LINK = @icu_lib@ -licui18n -licuuc -licudata
+ICU_LINK = @icu_lib@ -lsicui18n -lsicuuc -lsicudata
 PCRE_LINK = @pcre_lib@ -lpcre
 NETSNMP_LINK = @netsnmp_lib@ @netsnmp_link@
 PAM_LINK = -lpam
Index: include/base/systems.h
--- include/base/systems.h.orig	2012-06-27 18:57:12.000000000 +0200
+++ include/base/systems.h	2012-07-15 13:34:21.000000000 +0200
@@ -339,6 +339,34 @@
 #ifndef NO_DOMAINNAME
 #define NO_DOMAINNAME
 #endif
+
+#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
+#define SA_HANDLER_T(x) (void (*)(int))(x)
+#undef NEED_CRYPT_PROTO
+#define NET_SOCKETS
+#ifndef NO_DOMAINNAME
+#define NO_DOMAINNAME
+#endif
+
 #elif defined(SOLARIS) || defined(SOLARISx86)
 
 #define ACCELERATOR_CACHE
Index: include/base/systhr.h
--- include/base/systhr.h.orig	2012-06-27 18:57:12.000000000 +0200
+++ include/base/systhr.h	2012-07-15 13:34:21.000000000 +0200
@@ -57,6 +57,7 @@
 #include "netsite.h"
 #endif /* !NETSITE_H */
 
+#define THREAD_ANY
 #ifdef THREAD_ANY
 
 /* --- Begin function prototypes --- */
Index: include/public/base/systems.h
--- include/public/base/systems.h.orig	2012-06-27 18:57:12.000000000 +0200
+++ include/public/base/systems.h	2012-07-15 13:34:21.000000000 +0200
@@ -212,6 +212,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)
+
 #elif defined (XP_WIN32)      /* Windows NT */
 
 #include <wtypes.h>
Index: ldap/include/portable.h
--- ldap/include/portable.h.orig	2012-06-27 18:57:12.000000000 +0200
+++ ldap/include/portable.h	2012-07-15 13:34:22.000000000 +0200
@@ -153,7 +153,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
@@ -279,7 +279,7 @@
     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
@@ -317,7 +317,7 @@
 #elif defined( hpux10 )
 #define CTIME( c, b, l )		nsldapi_compat_ctime_r( c, b, l )
 #elif defined( IRIX ) || defined(UNIXWARE) || defined(LINUX) \
-	|| defined(OSF1V4) || defined(AIX) || defined(UnixWare) || defined (HPUX11)
+	|| defined(OSF1V4) || defined(AIX) || defined(UnixWare) || defined (HPUX11) || defined(__FreeBSD__)
 #define CTIME( c, b, l )                ctime_r( c, b )
 #elif defined( OSF1V3 )
 #define CTIME( c, b, l )		(ctime_r( c, b, l ) ? NULL : b)
@@ -341,7 +341,7 @@
 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 */
 
Index: ldap/servers/plugins/pwdstorage/crypt_pwd.c
--- ldap/servers/plugins/pwdstorage/crypt_pwd.c.orig	2012-06-27 18:57:12.000000000 +0200
+++ ldap/servers/plugins/pwdstorage/crypt_pwd.c	2012-07-15 13:34:22.000000000 +0200
@@ -52,7 +52,7 @@
 char *crypt(char *key, char *salt);
 #else
 #include <sys/socket.h>
-#if defined( hpux ) || defined ( AIX ) || defined (LINUX) || defined (OSF1)
+#if defined( hpux ) || defined ( AIX ) || defined (LINUX) || defined (OSF1) || defined (__FreeBSD__)
 #ifndef __USE_XOPEN
 #define __USE_XOPEN     /* linux */
 #endif /* __USE_XOPEN */
Index: ldap/servers/slapd/back-ldbm/dblayer.c
--- ldap/servers/slapd/back-ldbm/dblayer.c.orig	2012-06-27 18:57:12.000000000 +0200
+++ ldap/servers/slapd/back-ldbm/dblayer.c	2012-07-15 13:34:22.000000000 +0200
@@ -96,6 +96,8 @@
 #include "back-ldbm.h"
 #include "dblayer.h"
 
+#include <sys/resource.h>
+
 #if 1000*DB_VERSION_MAJOR + 100*DB_VERSION_MINOR >= 4100
 #define DB_OPEN(oflags, db, txnid, file, database, type, flags, mode, rval)    \
 {                                                                              \
Index: ldap/servers/slapd/back-ldbm/ldbm_entryrdn.c
--- ldap/servers/slapd/back-ldbm/ldbm_entryrdn.c.orig	2012-06-27 18:57:12.000000000 +0200
+++ ldap/servers/slapd/back-ldbm/ldbm_entryrdn.c	2012-07-15 13:34:22.000000000 +0200
@@ -1656,7 +1656,11 @@
     /* Open the entryrdn index */
     ainfo_get(be, LDBM_ENTRYRDN_STR, ai);
     if (NULL == *ai) {
+#ifdef ENODATA
         rc = ENODATA;
+#else
+        rc = ENOENT;
+#endif
         goto bail;
     }
     inst = (ldbm_instance *)be->be_instance_info;
Index: ldap/servers/slapd/pw.c
--- ldap/servers/slapd/pw.c.orig	2012-06-27 18:57:12.000000000 +0200
+++ ldap/servers/slapd/pw.c	2012-07-15 13:34:22.000000000 +0200
@@ -800,7 +800,11 @@
 	 * In that case, no need to check the password syntax, but just returns 
 	 * PASS == 0.
 	 */
+#ifdef LDAP_MOD_OP
 	if (LDAP_MOD_DELETE == (mod_op & LDAP_MOD_OP)) {
+#else
+	if (mod_op & LDAP_MOD_DELETE) {
+#endif
 		return 0;
 	}
 	if (NULL == vals) {
Index: ldap/servers/slapd/uniqueidgen.c
--- ldap/servers/slapd/uniqueidgen.c.orig	2012-06-27 18:57:12.000000000 +0200
+++ ldap/servers/slapd/uniqueidgen.c	2012-07-15 13:34:22.000000000 +0200
@@ -47,7 +47,9 @@
 #ifndef _WIN32			 /* for ntoh* functions */
 #include <sys/types.h>
 #include <sys/time.h>
+#ifdef __linux__
 #include <sys/sysinfo.h>
+#endif
 #include <sys/utsname.h>
 #endif
 
Index: ldap/servers/slapd/uuid.c
--- ldap/servers/slapd/uuid.c.orig	2012-06-27 18:57:12.000000000 +0200
+++ ldap/servers/slapd/uuid.c	2012-07-15 13:34:22.000000000 +0200
@@ -73,7 +73,9 @@
 #else
 #include <sys/types.h>
 #include <sys/time.h>
+#ifdef __linux__
 #include <sys/sysinfo.h>
+#endif
 #include <sys/utsname.h>
 #include <unistd.h>     /* gethostname() */
 #endif
Index: lib/base/dnsdmain.cpp
--- lib/base/dnsdmain.cpp.orig	2012-06-27 18:57:12.000000000 +0200
+++ lib/base/dnsdmain.cpp	2012-07-15 13:34:22.000000000 +0200
@@ -75,7 +75,7 @@
 #else 
 extern int getdomainname(char *, int);
 #endif /* Linux */
-#if defined(HPUX) || defined (UnixWare) || defined(Linux) || defined(IRIX6_5) || defined(SOLARIS_GCC)
+#if defined(HPUX) || defined (UnixWare) || defined(Linux) || defined(IRIX6_5) || defined(SOLARIS_GCC) || defined(__FreeBSD__)
 extern int gethostname (char *name, size_t namelen);
 #else
 #ifndef AIX
Index: lib/base/file.cpp
--- lib/base/file.cpp.orig	2012-06-27 18:57:12.000000000 +0200
+++ lib/base/file.cpp	2012-07-15 13:34:22.000000000 +0200
@@ -50,6 +50,7 @@
 
 
 #include "base/file.h"
+#include "base/systhr.h"
 #ifdef BSD_RLIMIT
 #include <sys/time.h>
 #include <sys/resource.h>
@@ -504,7 +505,7 @@
 }
 
 #ifdef XP_UNIX
-#if !defined(SNI) && !defined(LINUX)
+#if !defined(SNI) && !defined(LINUX) && !defined(__FreeBSD__)
 extern char *sys_errlist[];
 #endif /* SNI */
 #endif
@@ -517,6 +518,7 @@
 #include "xp_error.h"
 */
 #else /* THREAD_ANY */
+static int errmsg_key = -1;
 static char errmsg[ERRMSG_SIZE];
 #endif /* THREAD_ANY */
 
Index: lib/ldaputil/cert.c
--- lib/ldaputil/cert.c.orig	2012-06-27 18:57:12.000000000 +0200
+++ lib/ldaputil/cert.c	2012-07-15 13:34:22.000000000 +0200
@@ -42,7 +42,7 @@
 
 
 #include <string.h>
-#include <malloc.h>
+#include <stdlib.h>
 
 /* removed for ns security integration
 #include <sec.h>
Index: lib/ldaputil/certmap.c
--- lib/ldaputil/certmap.c.orig	2012-06-27 18:57:12.000000000 +0200
+++ lib/ldaputil/certmap.c	2012-07-15 13:34:22.000000000 +0200
@@ -44,7 +44,7 @@
 #include <stdio.h>
 #include <string.h>
 #include <ctype.h>
-#include <malloc.h>
+#include <stdlib.h>
 
 /* removed for ns security integration
 #include <sec.h>
Index: lib/ldaputil/dbconf.c
--- lib/ldaputil/dbconf.c.orig	2012-06-27 18:57:12.000000000 +0200
+++ lib/ldaputil/dbconf.c	2012-07-15 13:34:22.000000000 +0200
@@ -42,7 +42,7 @@
 
 
 #include <string.h>
-#include <malloc.h>
+#include <stdlib.h>
 #include <ctype.h>
 
 #include <ldaputil/errors.h>
Index: lib/ldaputil/encode.c
--- lib/ldaputil/encode.c.orig	2012-06-27 18:57:12.000000000 +0200
+++ lib/ldaputil/encode.c	2012-07-15 13:34:22.000000000 +0200
@@ -41,7 +41,7 @@
 #endif
 
 
-#include <malloc.h>
+#include <stdlib.h>
 #include <string.h>
 #include <ldaputil/certmap.h>
 #include <ldaputil/encode.h>
Index: ldap/servers/slapd/tools/ldclt/ldclt.c
--- ldap/servers/slapd/tools/ldclt/ldclt.c.orig	2012-06-27 18:57:12.000000000 +0200
+++ ldap/servers/slapd/tools/ldclt/ldclt.c	2012-07-15 13:43:22.000000000 +0200
@@ -1328,7 +1328,7 @@
       oflags = O_APPEND|O_WRONLY|O_CREAT;			/*JLS 05-04-01*/
     else							/*JLS 05-04-01*/
       oflags = O_EXCL|O_WRONLY|O_CREAT;				/*JLS 05-04-01*/
-#if !defined(_WIN32) && !defined(OSF1) && !defined(__LP64__) && !defined(_LP64)				/*JLS 05-04-01*/
+#if defined (O_LARGEFILE) && !defined(_WIN32) && !defined(OSF1) && !defined(__LP64__) && !defined(_LP64)				/*JLS 05-04-01*/
     oflags |= O_LARGEFILE;					/*JLS 05-04-01*/
 #endif								/*JLS 03-04-01*/
     mctx.genldifFile = open (mctx.genldifName, oflags, 0666);	/*JLS 05-04-01*/
