summaryrefslogtreecommitdiffstats
path: root/ldap/libraries
diff options
context:
space:
mode:
authorcvsadm <cvsadm>2005-01-21 00:44:34 +0000
committercvsadm <cvsadm>2005-01-21 00:44:34 +0000
commitb2093e3016027d6b5cf06b3f91f30769bfc099e2 (patch)
treecf58939393a9032182c4fbc4441164a9456e82f8 /ldap/libraries
downloadds-b2093e3016027d6b5cf06b3f91f30769bfc099e2.tar.gz
ds-b2093e3016027d6b5cf06b3f91f30769bfc099e2.tar.xz
ds-b2093e3016027d6b5cf06b3f91f30769bfc099e2.zip
Moving NSCP Directory Server from DirectoryBranch to TRUNK, initial drop. (foxworth)ldapserver7x
Diffstat (limited to 'ldap/libraries')
-rw-r--r--ldap/libraries/Makefile370
-rw-r--r--ldap/libraries/Makefile.client37
-rw-r--r--ldap/libraries/libavl/Makefile52
-rw-r--r--ldap/libraries/libavl/avl.c773
-rw-r--r--ldap/libraries/libavl/testavl.c125
-rw-r--r--ldap/libraries/libldif/Makefile52
-rw-r--r--ldap/libraries/libldif/fileurl.c289
-rw-r--r--ldap/libraries/libldif/fileurl.h40
-rw-r--r--ldap/libraries/libldif/line64.c743
-rw-r--r--ldap/libraries/liblitekey/Makefile50
-rw-r--r--ldap/libraries/liblitekey/keycheck.c137
-rw-r--r--ldap/libraries/libutil/Makefile70
-rw-r--r--ldap/libraries/libutil/getopt.c109
-rw-r--r--ldap/libraries/libutil/ntdebug.c51
-rw-r--r--ldap/libraries/libutil/ntevent.c183
-rw-r--r--ldap/libraries/libutil/ntreg.c105
-rw-r--r--ldap/libraries/libutil/ntresource.h31
-rw-r--r--ldap/libraries/libutil/ntstubs.c40
18 files changed, 3257 insertions, 0 deletions
diff --git a/ldap/libraries/Makefile b/ldap/libraries/Makefile
new file mode 100644
index 00000000..d174d131
--- /dev/null
+++ b/ldap/libraries/Makefile
@@ -0,0 +1,370 @@
+#
+# BEGIN COPYRIGHT BLOCK
+# Copyright 2001 Sun Microsystems, Inc.
+# Portions copyright 1999, 2001-2003 Netscape Communications Corporation.
+# All rights reserved.
+# END COPYRIGHT BLOCK
+#
+#
+# GNU Makefile for Directory Server and LDAP SDK libraries
+#
+
+MCOM_ROOT = ../../..
+LDAP_SRC = $(MCOM_ROOT)/ldapserver/ldap
+
+NOSTDCLEAN=true # don't let nsconfig.mk define target clean
+NOSTDSTRIP=true # don't let nsconfig.mk define target strip
+NSPR20=true # probably should be defined somewhere else (not sure where)
+
+OBJDEST = $(OBJDIR)/lib
+LIBDIR = $(LDAP_LIBDIR)
+
+include $(MCOM_ROOT)/ldapserver/nsdefs.mk
+include $(MCOM_ROOT)/ldapserver/nsconfig.mk
+include $(LDAP_SRC)/nsldap.mk
+# the following lines are to make this work for
+# both db1.85 and db2.0
+ifndef LDAP_USE_OLD_DB
+include $(MCOM_ROOT)/ldapserver/ns_usedb.mk
+ldap_db_depend:=$(DB_LIB_DEP)
+CFLAGS+= -I$(DB_INCLUDE)
+else
+ldap_db_depend:=_berkeley_db
+endif
+
+ifeq ($(ARCH), WINNT)
+LDAPRES= $(addprefix $(LIBDIR)/, libldap/*.res)
+LCACHERES= $(addprefix $(LIBDIR)/, liblcache/*.res)
+endif
+
+########## Security #######################
+##########ifneq ($(SECURITY), none)
+
+# Where to find libsec and friends
+SECDIR=$(NSCP_DISTDIR)/lib
+
+ifeq ($(ARCH), WINNT)
+# Used by libsec in Win32
+SEC_SYSLIBS=rpcrt4.lib user32.lib
+# When pulling in libsec/nspr20, WinSock and WinMM must be linked in
+ifdef INCLUDE_SSL
+EXTRA_LIBS=winmm.lib wsock32.lib
+SDK_EXPORT_DEFS=ldapsdk.def
+SDK_EXPORT_DEPS=msdos/winsock/nsldap32.def msdos/winsock/ldapssl.def
+else
+SDK_EXPORT_DEFS=msdos/winsock/nsldap32.def
+endif
+SDK_EXPORT_FLAGS=/DEF:$(SDK_EXPORT_DEFS)
+LCACHE_EXPORT_DEFS=msdos/winsock/nslch32.def
+LCACHE_EXPORT_FLAGS=/DEF:$(LCACHE_EXPORT_DEFS)
+endif
+
+EXPLDAP=libldap
+EXPLCH=liblcache
+EXPLDSSL=libldap_ssl
+
+ifeq ($(ARCH), AIX)
+USE_DLL_EXPORTS_FILE=1
+else
+ifeq ($(ARCH), SOLARIS)
+USE_DLL_EXPORTS_FILE=1
+else
+ifeq ($(ARCH), IRIX)
+USE_DLL_EXPORTS_FILE=1
+endif
+endif
+endif
+
+
+ifeq ($(USE_DLL_EXPORTS_FILE), 1)
+ifdef INCLUDE_SSL
+SDK_EXPORT_DEFS=$(OBJDEST)/$(EXPLDAP).exp $(OBJDEST)/$(EXPLDSSL).exp
+DLL_EXPORT_FLAGS=$(SDK_EXPORT_DEFS)
+else
+SDK_EXPORT_DEFS=$(OBJDEST)/$(EXPLDAP).exp
+endif
+DLL_EXPORT_FLAGS=$(addprefix $(DLLEXPORTS_PREFIX), $(SDK_EXPORT_DEFS))
+LCACHE_EXPORT_DEFS=$(OBJDEST)/$(EXPLCH).exp
+DLL_EXPORT_FLAGS2=$(addprefix $(DLLEXPORTS_PREFIX), $(LCACHE_EXPORT_DEFS))
+endif
+
+
+EXTRA_SECLIBS= $(STATICLIBNSPR) $(LIBSEC) $(LIBXP) $(LIBDB) $(LIBARES) \
+ $(LIBSSLIO) $(SEC_SYSLIBS)
+EXTRA_SECLIBS_DEP += $(STATICLIBNSPR) $(LIBXP_DEP) \
+ $(LIBSSLIO_DEP)
+EXTRA_SECLIBS_DEP += $(LIBSEC_DEP) $(LIBDB_DEP) $(LIBARES_DEP)
+
+ifdef INCLUDE_SSL
+#
+# include security code in the LDAP DLL
+#
+EXTRA_LIBS += $(EXTRA_SECLIBS)
+EXTRA_LIBS_DEP += $(EXTRA_SECLIBS_DEP)
+ifneq ($(ARCH), WINNT)
+DLL_LDFLAGS += -$(LIBPATH)$(SECDIR)
+endif
+
+SSLOBJS= $(addprefix $(LIBDIR)/, libssldap/*.o)
+SSL_DEP= $(LDAPSDK_DEP)
+
+endif
+########## end Security ###################
+
+# The following libraries are built by this Makefile:
+# dynamic LDAP library
+DLIBLDAP= $(addprefix $(LIBDIR)/, $(LIBLDAP_DLL).$(DLL_SUFFIX))
+# static LDAP library
+SLIBLDAP= $(addprefix $(LIBDIR)/, $(LIBLDAP_LIB).$(LIB_SUFFIX))
+# dynamic LCACHE library
+DLIBLCACHE= $(addprefix $(LIBDIR)/, $(LIBLCACHE_DLL).$(DLL_SUFFIX))
+# static LCACHE library
+SLIBLCACHE= $(addprefix $(LIBDIR)/, $(LIBLCACHE_LIB).$(LIB_SUFFIX))
+
+# objects built by libavl/Makefile
+AVLOBJS= $(addprefix $(LIBDIR)/, libavl/*.o)
+# objects built by berkeley_db/Makefile
+# the following lines pull in the new db library with the liblcache dll is built
+# this is a hack, but can't see how to add it easier without a new LINK_DLL2.
+ifndef LDAP_USE_OLD_DB
+DLL_EXPORT_FLAGS2+=$(DB_STATIC_LIB)
+else
+BERKDBOBJS= $(addprefix $(LIBDIR)/, libdb/*.o)
+endif
+# objects built by liblber/Makefile
+LBEROBJS= $(addprefix $(LIBDIR)/, liblber/*.o)
+# objects built by liblcache/Makefile
+LCACHEOBJS= $(addprefix $(LIBDIR)/, liblcache/*.o)
+# objects built by libldap/Makefile
+LDAPOBJS= $(addprefix $(LIBDIR)/, libldap/*.o)
+# one object built by libldap/Makefile
+REGEXOBJ= $(addprefix $(LIBDIR)/, libldap/regex.o)
+# objects built by libldbm/Makefile
+LDBMOBJS= $(addprefix $(LIBDIR)/, libldbm/*.o)
+# objects built by libldif/Makefile
+LDIFOBJS= $(addprefix $(LIBDIR)/, libldif/*.o)
+
+all: $(LDAP_LIBDIR) clientSDK
+
+buildDirectory: $(LDAP_LIBDIR) _libavl _libldif _liblitekey _libutil
+
+ifdef LDAP_USE_OLD_DB
+_berkeley_db:
+ cd berkeley_db; $(MAKE) $(MFLAGS) all
+endif
+
+_libavl:
+ cd libavl; $(MAKE) $(MFLAGS) all
+
+_liblber:
+ cd liblber; $(MAKE) $(MFLAGS) all
+
+_libldbm:
+ cd libldbm; $(MAKE) $(MFLAGS) all
+
+_libldif:
+ cd libldif; $(MAKE) $(MFLAGS) all
+
+_liblitekey:
+ cd liblitekey; $(MAKE) $(MFLAGS) all
+
+_libssldap:
+ cd libssldap; $(MAKE) $(MFLAGS) all
+
+_libutil:
+ifeq ($(ARCH), WINNT)
+ cd libutil; $(MAKE) $(MFLAGS) all
+endif
+
+_libldap:
+ cd libldap; $(MAKE) $(MFLAGS) all
+
+_slapd:
+ cd $(LDAP_SRC)/servers/slapd; $(MAKE) $(MFLAGS) all
+
+_back-ldbm:
+ cd $(LDAP_SRC)/servers/slapd/back-ldbm; $(MAKE) $(MFLAGS) all
+
+_back-ldif:
+ cd $(LDAP_SRC)/servers/slapd/back-ldif; $(MAKE) $(MFLAGS) all
+
+_liblcache:
+ cd liblcache; $(MAKE) $(MFLAGS) clientSDK
+
+ifdef INCLUDE_SSL
+#
+# There is no easy way to include libsec and friends when making static libs
+# (at least on UNIX) so we don't bother making them when INCLUDE_SSL is set.
+# Also if LDAP_NO_LIBLCACHE is 1, we don't build liblcache. Simple, huh?
+#
+ifeq ($(LDAP_NO_LIBLCACHE),1)
+LIBS2BUILD=$(DLIBLDAP)
+else
+LIBS2BUILD=$(DLIBLDAP) $(DLIBLCACHE)
+endif
+else
+ifeq ($(LDAP_NO_LIBLCACHE),1)
+LIBS2BUILD=$(DLIBLDAP) $(SLIBLDAP)
+else
+LIBS2BUILD=$(DLIBLDAP) $(SLIBLDAP) $(DLIBLCACHE) $(SLIBLCACHE)
+endif
+
+endif
+
+clientSDK: $(LDAP_LIBDIR) _libavl _liblber _libldif \
+ _libssldap _libutil $(LIBS2BUILD)
+
+OBJS=$(LBEROBJS) $(LDAPOBJS) $(SSLOBJS) $(LDAPRES)
+
+$(DLIBLDAP): $(LDAP_OUT_DIR) _liblber _libldap $(EXTRA_LIBS_DEP) $(SSL_DEP) \
+ $(SDK_EXPORT_DEFS)
+ $(LINK_DLL) $(SDK_EXPORT_FLAGS) $(EXTRA_LIBS)
+
+$(SLIBLDAP): $(LDAP_OUT_DIR) _liblber _libldap $(EXTRA_LIBS_DEP) $(SSL_DEP)
+ $(LINK_LIB)
+
+OBJS2=$(LCACHEOBJS) $(AVLOBJS) $(LDBMOBJS) $(BERKDBOBJS) $(LDIFOBJS) \
+ $(REGEXOBJ) $(LCACHERES)
+
+ifeq ($(ARCH), WINNT)
+# liblber is included here only to pick up ber_err_print()
+# libsec and libxp are included here only to pick up SHA1_Hash()
+OTHER_SECLIBS= $(LIBSEC) $(LIBXP)
+EXTRA_LCACHE_LIBS=$(LDAP_SDK_LIBLDAP_DLL) $(LDAP_LIBLBER) $(OTHER_SECLIBS)
+EXTRA_LCACHE_LIBS_DEP= $(LDAP_SDK_LIBLDAP_DLL_DEP) $(LDAP_LIBLBER_DEP) \
+ $(EXTRA_SECLIBS_DEP)
+endif
+
+ifeq ($(ARCH), AIX)
+DLL_LDFLAGS += -$(LIBPATH)$(SECDIR)
+# extra libraries are needed on AIX as well
+EXTRA_LCACHE_LIBS=$(LDAP_SDK_LIBLDAP_DLL) $(LDAP_LIBLBER) $(EXTRA_SECLIBS)
+EXTRA_LCACHE_LIBS_DEP= $(LDAP_SDK_LIBLDAP_DLL_DEP) $(LDAP_LIBLBER_DEP) \
+ $(EXTRA_SECLIBS_DEP)
+endif
+
+$(DLIBLCACHE): $(LDAP_OUT_DIR) $(ldap_db_depend) _liblcache _libavl _libldbm \
+ _libldif $(EXTRA_LCACHE_LIBS_DEP) $(LCACHE_EXPORT_DEFS)
+ $(LINK_DLL2) $(LCACHE_EXPORT_FLAGS) $(EXTRA_LCACHE_LIBS) $(EXTRA_LIBS)
+
+$(SLIBLCACHE): $(LDAP_OUT_DIR) $(ldap_db_depend) _liblcache libavl _libldbm \
+ _libldif
+ $(LINK_LIB2)
+
+# Need to add exports if building with SSL in NT
+ifeq ($(ARCH), WINNT)
+ifdef INCLUDE_SSL
+$(SDK_EXPORT_DEFS) : $(SDK_EXPORT_DEPS)
+ cat $(SDK_EXPORT_DEPS) > $(SDK_EXPORT_DEFS)
+endif
+endif
+
+clean: FORCE
+ifdef LDAP_USE_OLD_DB
+ cd berkeley_db; $(MAKE) $(MFLAGS) clean
+endif
+ cd libavl; $(MAKE) $(MFLAGS) clean
+ cd libldif; $(MAKE) $(MFLAGS) clean
+ifeq ($(ARCH), WINNT)
+ cd libutil; $(MAKE) $(MFLAGS) clean
+ -(cd $(LDAP_SRC)/servers/slapd; $(MAKE) $(MFLAGS) clean)
+endif
+ -(cd $(LDAP_SRC)/servers/slapd/back-ldbm; $(MAKE) $(MFLAGS) clean)
+ -(cd $(LDAP_SRC)/servers/slapd/back-ldif; $(MAKE) $(MFLAGS) clean)
+ifeq ($(ARCH), WINNT)
+ -$(RM) $(LIBLDAP_DLL_OBJ)
+endif
+ -$(RM) $(LIBS2BUILD)
+
+veryclean: clean
+
+FORCE:
+
+$(LDAP_OUT_DIR):
+ $(MKDIR) $(LDAP_OUT_DIR)
+
+$(LDAP_LIBDIR):
+ $(MKDIR) $(LDAP_LIBDIR)
+
+
+GENEXPORTS=$(PERL) $(LDAP_SRC)/build/genexports.pl
+# WINDEFDIR is now defined below via a recursive make. This is a
+# bit of a hack to avoid collisions with SDK_EXPORT_DEFS (defined above).
+#WINDEFDIR=msdos/winsock
+MACDEFDIR=macintosh
+GENEXPARGS=$(BUILD_DEBUG) $(DIRSDK_VERSION_DLL_SUFFIX) $(DIRSDK_VERSION)
+
+DEFLDAPW32=$(WINDEFDIR)/nsldap32
+DEFLDAPW16REV=$(WINDEFDIR)/nsldap
+DEFLDAPW16=$(WINDEFDIR)/libldap
+DEFLDAPW32BC=$(WINDEFDIR)/nsldap32bc
+DEFLDSSLW32=$(WINDEFDIR)/ldapssl
+DEFLDSSLW32BC=$(WINDEFDIR)/ldapsslbc
+DEFLCHW32=$(WINDEFDIR)/nslch32
+DEFLCHW32BC=$(WINDEFDIR)/nslch32bc
+DEFLDAPMACPPC=$(MACDEFDIR)/libldap-PPC.u
+DEFLDAPMACCLIENTPPC=$(MACDEFDIR)/LDAPClientPPC.prj
+DEFLDAPMACCLIENT68K=$(MACDEFDIR)/LDAPClient68K.prj
+
+# nsdefs.mk contains version number definitions....
+OTHERDEFDEPS=$(MCOM_ROOT)/ldapserver/nsdefs.mk
+
+exportfiles:
+ $(MAKE) $(MFLAGS) WINDEFDIR=msdos/winsock doExportFiles
+
+doExportFiles: $(DEFLDAPW32).def $(DEFLDAPW16).def $(DEFLDAPW16REV).def \
+ $(DEFLDAPW32BC).def $(DEFLDSSLW32).def $(DEFLDSSLW32BC).def \
+ $(DEFLCHW32).def $(DEFLCHW32BC).def \
+ $(DEFLDAPMACPPC).exp $(DEFLDAPMACCLIENTPPC).exp \
+ $(DEFLDAPMACCLIENT68K).exp
+
+#
+# All the various flavors of Windows:
+#
+$(DEFLDAPW32).def: $(DEFLDAPW32).tdf libldap.ex $(OTHERDEFDEPS)
+ $(GENEXPORTS) Win32 $< libldap.ex $(GENEXPARGS) > $@
+
+$(DEFLDAPW16).def: $(DEFLDAPW16).tdf libldap.ex $(OTHERDEFDEPS)
+ $(GENEXPORTS) Win16 $< libldap.ex $(GENEXPARGS) > $@
+
+$(DEFLDAPW16REV).def: $(DEFLDAPW16REV).tdf libldap.ex $(OTHERDEFDEPS)
+ $(GENEXPORTS) Win16Rev $< libldap.ex $(GENEXPARGS) > $@
+
+$(DEFLDAPW32BC).def: $(DEFLDAPW32BC).tdf libldap.ex $(OTHERDEFDEPS)
+ $(GENEXPORTS) WinBC $< libldap.ex $(GENEXPARGS) > $@
+
+$(DEFLDSSLW32).def: $(DEFLDSSLW32).tdf libldap_ssl.ex $(OTHERDEFDEPS)
+ $(GENEXPORTS) Win32 $< libldap_ssl.ex $(GENEXPARGS) > $@
+
+$(DEFLDSSLW32BC).def: $(DEFLDSSLW32BC).tdf libldap_ssl.ex $(OTHERDEFDEPS)
+ $(GENEXPORTS) WinBC $< libldap_ssl.ex $(GENEXPARGS) > $@
+
+$(DEFLCHW32).def: $(DEFLCHW32).tdf liblcache.ex $(OTHERDEFDEPS)
+ $(GENEXPORTS) Win32 $< liblcache.ex $(GENEXPARGS) > $@
+
+$(DEFLCHW32BC).def: $(DEFLCHW32BC).tdf liblcache.ex $(OTHERDEFDEPS)
+ $(GENEXPORTS) WinBC $< liblcache.ex $(GENEXPARGS) > $@
+
+#
+# MacOS:
+#
+$(DEFLDAPMACPPC).exp: libldap.ex $(OTHERDEFDEPS)
+ $(GENEXPORTS) MacOS Standard $< $(GENEXPARGS) > $@
+
+$(DEFLDAPMACCLIENTPPC).exp: libldap.ex $(OTHERDEFDEPS)
+ $(GENEXPORTS) MacOS Standard $< $(GENEXPARGS) > $@
+
+$(DEFLDAPMACCLIENT68K).exp: libldap.ex $(OTHERDEFDEPS)
+ $(GENEXPORTS) MacOS Standard $< $(GENEXPARGS) > $@
+
+ifeq ($(USE_DLL_EXPORTS_FILE), 1)
+# generic rules to create standard export/map files
+$(OBJDEST)/$(EXPLDAP).exp: libldap.ex $(OTHERDEFDEPS)
+ $(GENEXPORTS) $(ARCH) Standard $< $(GENEXPARGS) > $@
+
+$(OBJDEST)/$(EXPLDSSL).exp: libldap_ssl.ex $(OTHERDEFDEPS)
+ $(GENEXPORTS) $(ARCH) Standard $< $(GENEXPARGS) > $@
+
+$(OBJDEST)/$(EXPLCH).exp: liblcache.ex $(OTHERDEFDEPS)
+ $(GENEXPORTS) $(ARCH) Standard $< $(GENEXPARGS) > $@
+endif
diff --git a/ldap/libraries/Makefile.client b/ldap/libraries/Makefile.client
new file mode 100644
index 00000000..5aa0ab20
--- /dev/null
+++ b/ldap/libraries/Makefile.client
@@ -0,0 +1,37 @@
+#
+# BEGIN COPYRIGHT BLOCK
+# Copyright 2001 Sun Microsystems, Inc.
+# Portions copyright 1999, 2001-2003 Netscape Communications Corporation.
+# All rights reserved.
+# END COPYRIGHT BLOCK
+#
+DEPTH = ../../..
+UNIXDIRLIST = liblber libldap
+
+include $(DEPTH)/config/rules.mk
+
+all export:: FORCE
+ @for i in $(UNIXDIRLIST); do \
+ echo " cd $$i; $(MAKE) -f Makefile.client $(MFLAGS) export"; \
+ ( cd $$i; $(MAKE) -f Makefile.client $(MFLAGS) export ); \
+ done
+
+libs install:: FORCE
+ @for i in $(UNIXDIRLIST); do \
+ echo " cd $$i; $(MAKE) -f Makefile.client $(MFLAGS) install"; \
+ ( cd $$i; $(MAKE) -f Makefile.client $(MFLAGS) install ); \
+ done
+
+clean clobber:: FORCE
+ @for i in $(UNIXDIRLIST); do \
+ echo " cd $$i; $(MAKE) -f Makefile.client $(MFLAGS) clean"; \
+ ( cd $$i; $(MAKE) -f Makefile.client $(MFLAGS) clean ); \
+ done
+
+realclean clobber_all:: FORCE
+ @for i in $(UNIXDIRLIST); do \
+ echo " cd $$i; $(MAKE) -f Makefile.client $(MFLAGS) realclean"; \
+ ( cd $$i; $(MAKE) -f Makefile.client $(MFLAGS) realclean ); \
+ done
+
+FORCE:
diff --git a/ldap/libraries/libavl/Makefile b/ldap/libraries/libavl/Makefile
new file mode 100644
index 00000000..28660d4d
--- /dev/null
+++ b/ldap/libraries/libavl/Makefile
@@ -0,0 +1,52 @@
+#
+# BEGIN COPYRIGHT BLOCK
+# Copyright 2001 Sun Microsystems, Inc.
+# Portions copyright 1999, 2001-2003 Netscape Communications Corporation.
+# All rights reserved.
+# END COPYRIGHT BLOCK
+#
+#
+# GNU Makefile for libavl
+#
+
+LDAP_SRC = ../..
+MCOM_ROOT = ../../../..
+
+NOSTDCLEAN=true # don't let nsconfig.mk define target clean
+NOSTDSTRIP=true # don't let nsconfig.mk define target strip
+NSPR20=true # probably should be defined somewhere else (not sure where)
+
+OBJDEST = $(OBJDIR)/lib/libavl
+LIBDIR = $(LDAP_LIBDIR)
+
+include $(MCOM_ROOT)/ldapserver/nsdefs.mk
+include $(MCOM_ROOT)/ldapserver/nsconfig.mk
+include $(LDAP_SRC)/nsldap.mk
+
+CFLAGS += $(SLCFLAGS)
+
+LIBAVL_OBJS= avl.o
+
+OBJS = $(addprefix $(OBJDEST)/, $(LIBAVL_OBJS))
+
+LIBAVL= $(addprefix $(LIBDIR)/, libavl.$(LIB_SUFFIX))
+
+clientSDK: all
+
+all: $(OBJDEST) $(LIBDIR) $(LIBAVL)
+
+$(LIBDIR):
+ $(MKDIR) $(LIBDIR)
+
+$(LIBAVL): $(OBJS)
+ $(LINK_LIB)
+
+veryclean: clean
+
+clean:
+ $(RM) $(OBJS)
+ $(RM) $(LIBAVL)
+
+$(OBJDEST):
+ $(MKDIR) $(OBJDEST)
+
diff --git a/ldap/libraries/libavl/avl.c b/ldap/libraries/libavl/avl.c
new file mode 100644
index 00000000..f6fb61ad
--- /dev/null
+++ b/ldap/libraries/libavl/avl.c
@@ -0,0 +1,773 @@
+/** BEGIN COPYRIGHT BLOCK
+ * Copyright 2001 Sun Microsystems, Inc.
+ * Portions copyright 1999, 2001-2003 Netscape Communications Corporation.
+ * All rights reserved.
+ * END COPYRIGHT BLOCK **/
+/* avl.c - routines to implement an avl tree */
+/*
+ * Copyright (c) 1993 Regents of the University of Michigan.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms are permitted
+ * provided that this notice is preserved and that due credit is given
+ * to the University of Michigan at Ann Arbor. The name of the University
+ * may not be used to endorse or promote products derived from this
+ * software without specific prior written permission. This software
+ * is provided ``as is'' without express or implied warranty.
+ */
+
+#if 0
+static char copyright[] = "@(#) Copyright (c) 1993 Regents of the University of Michigan.\nAll rights reserved.\n";
+static char avl_version[] = "AVL library version 1.0\n";
+#endif
+
+#ifdef _WIN32
+typedef char *caddr_t;
+#define WIN32_LEAN_AND_MEAN
+#include <windows.h>
+#endif
+
+#include <sys/types.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include "avl.h"
+
+#define ROTATERIGHT(x) { \
+ Avlnode *tmp;\
+ if ( *x == NULL || (*x)->avl_left == NULL ) {\
+ (void) printf("RR error\n"); exit(1); \
+ }\
+ tmp = (*x)->avl_left;\
+ (*x)->avl_left = tmp->avl_right;\
+ tmp->avl_right = *x;\
+ *x = tmp;\
+}
+#define ROTATELEFT(x) { \
+ Avlnode *tmp;\
+ if ( *x == NULL || (*x)->avl_right == NULL ) {\
+ (void) printf("RL error\n"); exit(1); \
+ }\
+ tmp = (*x)->avl_right;\
+ (*x)->avl_right = tmp->avl_left;\
+ tmp->avl_left = *x;\
+ *x = tmp;\
+}
+
+/*
+ * ravl_insert - called from avl_insert() to do a recursive insert into
+ * and balance of an avl tree.
+ */
+
+static int
+ravl_insert(
+ Avlnode **iroot,
+ caddr_t data,
+ int *taller,
+ IFP fcmp, /* comparison function */
+ IFP fdup, /* function to call for duplicates */
+ int depth
+)
+{
+ int rc, cmp, tallersub;
+ Avlnode *l, *r;
+
+ if ( *iroot == 0 ) {
+ if ( (*iroot = (Avlnode *) malloc( sizeof( Avlnode ) ))
+ == NULL ) {
+ return( -1 );
+ }
+ (*iroot)->avl_left = 0;
+ (*iroot)->avl_right = 0;
+ (*iroot)->avl_bf = 0;
+ (*iroot)->avl_data = data;
+ *taller = 1;
+ return( 0 );
+ }
+
+ cmp = (*fcmp)( data, (*iroot)->avl_data );
+
+ /* equal - duplicate name */
+ if ( cmp == 0 ) {
+ *taller = 0;
+ return( (*fdup)( (*iroot)->avl_data, data ) );
+ }
+
+ /* go right */
+ else if ( cmp > 0 ) {
+ rc = ravl_insert( &((*iroot)->avl_right), data, &tallersub,
+ fcmp, fdup, depth );
+ if ( tallersub )
+ switch ( (*iroot)->avl_bf ) {
+ case LH : /* left high - balance is restored */
+ (*iroot)->avl_bf = EH;
+ *taller = 0;
+ break;
+ case EH : /* equal height - now right heavy */
+ (*iroot)->avl_bf = RH;
+ *taller = 1;
+ break;
+ case RH : /* right heavy to start - right balance */
+ r = (*iroot)->avl_right;
+ switch ( r->avl_bf ) {
+ case LH : /* double rotation left */
+ l = r->avl_left;
+ switch ( l->avl_bf ) {
+ case LH : (*iroot)->avl_bf = EH;
+ r->avl_bf = RH;
+ break;
+ case EH : (*iroot)->avl_bf = EH;
+ r->avl_bf = EH;
+ break;
+ case RH : (*iroot)->avl_bf = LH;
+ r->avl_bf = EH;
+ break;
+ }
+ l->avl_bf = EH;
+ ROTATERIGHT( (&r) )
+ (*iroot)->avl_right = r;
+ ROTATELEFT( iroot )
+ *taller = 0;
+ break;
+ case EH : /* This should never happen */
+ break;
+ case RH : /* single rotation left */
+ (*iroot)->avl_bf = EH;
+ r->avl_bf = EH;
+ ROTATELEFT( iroot )
+ *taller = 0;
+ break;
+ }
+ break;
+ }
+ else
+ *taller = 0;
+ }
+
+ /* go left */
+ else {
+ rc = ravl_insert( &((*iroot)->avl_left), data, &tallersub,
+ fcmp, fdup, depth );
+ if ( tallersub )
+ switch ( (*iroot)->avl_bf ) {
+ case LH : /* left high to start - left balance */
+ l = (*iroot)->avl_left;
+ switch ( l->avl_bf ) {
+ case LH : /* single rotation right */
+ (*iroot)->avl_bf = EH;
+ l->avl_bf = EH;
+ ROTATERIGHT( iroot )
+ *taller = 0;
+ break;
+ case EH : /* this should never happen */
+ break;
+ case RH : /* double rotation right */
+ r = l->avl_right;
+ switch ( r->avl_bf ) {
+ case LH : (*iroot)->avl_bf = RH;
+ l->avl_bf = EH;
+ break;
+ case EH : (*iroot)->avl_bf = EH;
+ l->avl_bf = EH;
+ break;
+ case RH : (*iroot)->avl_bf = EH;
+ l->avl_bf = LH;
+ break;
+ }
+ r->avl_bf = EH;
+ ROTATELEFT( (&l) )
+ (*iroot)->avl_left = l;
+ ROTATERIGHT( iroot )
+ *taller = 0;
+ break;
+ }
+ break;
+ case EH : /* equal height - now left heavy */
+ (*iroot)->avl_bf = LH;
+ *taller = 1;
+ break;
+ case RH : /* right high - balance is restored */
+ (*iroot)->avl_bf = EH;
+ *taller = 0;
+ break;
+ }
+ else
+ *taller = 0;
+ }
+
+ return( rc );
+}
+
+/*
+ * avl_insert -- insert a node containing data data into the avl tree
+ * with root root. fcmp is a function to call to compare the data portion
+ * of two nodes. it should take two arguments and return <, >, or == 0,
+ * depending on whether its first argument is <, >, or == its second
+ * argument (like strcmp, e.g.). fdup is a function to call when a duplicate
+ * node is inserted. it should return 0, or -1 and its return value
+ * will be the return value from avl_insert in the case of a duplicate node.
+ * the function will be called with the original node's data as its first
+ * argument and with the incoming duplicate node's data as its second
+ * argument. this could be used, for example, to keep a count with each
+ * node.
+ *
+ * NOTE: this routine may malloc memory
+ */
+
+int
+avl_insert(
+ Avlnode **root,
+ caddr_t data,
+ IFP fcmp,
+ IFP fdup
+)
+{
+ int taller;
+
+ return( ravl_insert( root, data, &taller, fcmp, fdup, 0 ) );
+}
+
+/*
+ * right_balance() - called from delete when root's right subtree has
+ * been shortened because of a deletion.
+ */
+
+static int
+right_balance( Avlnode **root )
+{
+ int shorter= 0;
+ Avlnode *r, *l;
+
+ switch( (*root)->avl_bf ) {
+ case RH: /* was right high - equal now */
+ (*root)->avl_bf = EH;
+ shorter = 1;
+ break;
+ case EH: /* was equal - left high now */
+ (*root)->avl_bf = LH;
+ shorter = 0;
+ break;
+ case LH: /* was right high - balance */
+ l = (*root)->avl_left;
+ switch ( l->avl_bf ) {
+ case RH : /* double rotation left */
+ r = l->avl_right;
+ switch ( r->avl_bf ) {
+ case RH :
+ (*root)->avl_bf = EH;
+ l->avl_bf = LH;
+ break;
+ case EH :
+ (*root)->avl_bf = EH;
+ l->avl_bf = EH;
+ break;
+ case LH :
+ (*root)->avl_bf = RH;
+ l->avl_bf = EH;
+ break;
+ }
+ r->avl_bf = EH;
+ ROTATELEFT( (&l) )
+ (*root)->avl_left = l;
+ ROTATERIGHT( root )
+ shorter = 1;
+ break;
+ case EH : /* right rotation */
+ (*root)->avl_bf = LH;
+ l->avl_bf = RH;
+ ROTATERIGHT( root );
+ shorter = 0;
+ break;
+ case LH : /* single rotation right */
+ (*root)->avl_bf = EH;
+ l->avl_bf = EH;
+ ROTATERIGHT( root )
+ shorter = 1;
+ break;
+ }
+ break;
+ }
+
+ return( shorter );
+}
+
+/*
+ * left_balance() - called from delete when root's left subtree has
+ * been shortened because of a deletion.
+ */
+
+static int
+left_balance( Avlnode **root )
+{
+ int shorter= 0;
+ Avlnode *r, *l;
+
+ switch( (*root)->avl_bf ) {
+ case LH: /* was left high - equal now */
+ (*root)->avl_bf = EH;
+ shorter = 1;
+ break;
+ case EH: /* was equal - right high now */
+ (*root)->avl_bf = RH;
+ shorter = 0;
+ break;
+ case RH: /* was right high - balance */
+ r = (*root)->avl_right;
+ switch ( r->avl_bf ) {
+ case LH : /* double rotation left */
+ l = r->avl_left;
+ switch ( l->avl_bf ) {
+ case LH :
+ (*root)->avl_bf = EH;
+ r->avl_bf = RH;
+ break;
+ case EH :
+ (*root)->avl_bf = EH;
+ r->avl_bf = EH;
+ break;
+ case RH :
+ (*root)->avl_bf = LH;
+ r->avl_bf = EH;
+ break;
+ }
+ l->avl_bf = EH;
+ ROTATERIGHT( (&r) )
+ (*root)->avl_right = r;
+ ROTATELEFT( root )
+ shorter = 1;
+ break;
+ case EH : /* single rotation left */
+ (*root)->avl_bf = RH;
+ r->avl_bf = LH;
+ ROTATELEFT( root );
+ shorter = 0;
+ break;
+ case RH : /* single rotation left */
+ (*root)->avl_bf = EH;
+ r->avl_bf = EH;
+ ROTATELEFT( root )
+ shorter = 1;
+ break;
+ }
+ break;
+ }
+
+ return( shorter );
+}
+
+/*
+ * ravl_delete() - called from avl_delete to do recursive deletion of a
+ * node from an avl tree. It finds the node recursively, deletes it,
+ * and returns shorter if the tree is shorter after the deletion and
+ * rebalancing.
+ */
+
+static caddr_t
+ravl_delete(
+ Avlnode **root,
+ caddr_t data,
+ IFP fcmp,
+ int *shorter
+)
+{
+ int shortersubtree = 0;
+ int cmp;
+ caddr_t savedata;
+ Avlnode *minnode, *savenode;
+
+ if ( *root == NULLAVL )
+ return( 0 );
+
+ cmp = (*fcmp)( data, (*root)->avl_data );
+
+ /* found it! */
+ if ( cmp == 0 ) {
+ savenode = *root;
+ savedata = savenode->avl_data;
+
+ /* simple cases: no left child */
+ if ( (*root)->avl_left == 0 ) {
+ *root = (*root)->avl_right;
+ *shorter = 1;
+ free( (char *) savenode );
+ return( savedata );
+ /* no right child */
+ } else if ( (*root)->avl_right == 0 ) {
+ *root = (*root)->avl_left;
+ *shorter = 1;
+ free( (char *) savenode );
+ return( savedata );
+ }
+
+ /*
+ * avl_getmin will return to us the smallest node greater
+ * than the one we are trying to delete. deleting this node
+ * from the right subtree is guaranteed to end in one of the
+ * simple cases above.
+ */
+
+ minnode = (*root)->avl_right;
+ while ( minnode->avl_left != NULLAVL )
+ minnode = minnode->avl_left;
+
+ /* swap the data */
+ (*root)->avl_data = minnode->avl_data;
+ minnode->avl_data = savedata;
+
+ savedata = ravl_delete( &(*root)->avl_right, data, fcmp,
+ &shortersubtree );
+
+ if ( shortersubtree )
+ *shorter = right_balance( root );
+ else
+ *shorter = 0;
+ /* go left */
+ } else if ( cmp < 0 ) {
+ if ( (savedata = ravl_delete( &(*root)->avl_left, data, fcmp,
+ &shortersubtree )) == 0 ) {
+ *shorter = 0;
+ return( 0 );
+ }
+
+ /* left subtree shorter? */
+ if ( shortersubtree )
+ *shorter = left_balance( root );
+ else
+ *shorter = 0;
+ /* go right */
+ } else {
+ if ( (savedata = ravl_delete( &(*root)->avl_right, data, fcmp,
+ &shortersubtree )) == 0 ) {
+ *shorter = 0;
+ return( 0 );
+ }
+
+ if ( shortersubtree )
+ *shorter = right_balance( root );
+ else
+ *shorter = 0;
+ }
+
+ return( savedata );
+}
+
+/*
+ * avl_delete() - deletes the node containing data (according to fcmp) from
+ * the avl tree rooted at root.
+ */
+
+caddr_t
+avl_delete( Avlnode **root, caddr_t data, IFP fcmp )
+{
+ int shorter;
+
+ return( ravl_delete( root, data, fcmp, &shorter ) );
+}
+
+static int
+avl_inapply( Avlnode *root, IFP fn, caddr_t arg, int stopflag )
+{
+ if ( root == 0 )
+ return( AVL_NOMORE );
+
+ if ( root->avl_left != 0 )
+ if ( avl_inapply( root->avl_left, fn, arg, stopflag )
+ == stopflag )
+ return( stopflag );
+
+ if ( (*fn)( root->avl_data, arg ) == stopflag )
+ return( stopflag );
+
+ if ( root->avl_right == 0 )
+ return( AVL_NOMORE );
+ else
+ return( avl_inapply( root->avl_right, fn, arg, stopflag ) );
+}
+
+static int
+avl_postapply( Avlnode *root, IFP fn, caddr_t arg, int stopflag )
+{
+ if ( root == 0 )
+ return( AVL_NOMORE );
+
+ if ( root->avl_left != 0 )
+ if ( avl_postapply( root->avl_left, fn, arg, stopflag )
+ == stopflag )
+ return( stopflag );
+
+ if ( root->avl_right != 0 )
+ if ( avl_postapply( root->avl_right, fn, arg, stopflag )
+ == stopflag )
+ return( stopflag );
+
+ return( (*fn)( root->avl_data, arg ) );
+}
+
+static int
+avl_preapply( Avlnode *root, IFP fn, caddr_t arg, int stopflag )
+{
+ if ( root == 0 )
+ return( AVL_NOMORE );
+
+ if ( (*fn)( root->avl_data, arg ) == stopflag )
+ return( stopflag );
+
+ if ( root->avl_left != 0 )
+ if ( avl_preapply( root->avl_left, fn, arg, stopflag )
+ == stopflag )
+ return( stopflag );
+
+ if ( root->avl_right == 0 )
+ return( AVL_NOMORE );
+ else
+ return( avl_preapply( root->avl_right, fn, arg, stopflag ) );
+}
+
+/*
+ * avl_apply -- avl tree root is traversed, function fn is called with
+ * arguments arg and the data portion of each node. if fn returns stopflag,
+ * the traversal is cut short, otherwise it continues. Do not use -6 as
+ * a stopflag, as this is what is used to indicate the traversal ran out
+ * of nodes.
+ */
+
+int
+avl_apply(
+ Avlnode *root,
+ IFP fn,
+ caddr_t arg,
+ int stopflag,
+ int type
+)
+{
+ switch ( type ) {
+ case AVL_INORDER:
+ return( avl_inapply( root, fn, arg, stopflag ) );
+ case AVL_PREORDER:
+ return( avl_preapply( root, fn, arg, stopflag ) );
+ case AVL_POSTORDER:
+ return( avl_postapply( root, fn, arg, stopflag ) );
+ default:
+ fprintf( stderr, "Invalid traversal type %d\n", type );
+ return( -1 );
+ }
+
+ /* NOTREACHED */
+}
+
+/*
+ * avl_prefixapply - traverse avl tree root, applying function fprefix
+ * to any nodes that match. fcmp is called with data as its first arg
+ * and the current node's data as its second arg. it should return
+ * 0 if they match, < 0 if data is less, and > 0 if data is greater.
+ * the idea is to efficiently find all nodes that are prefixes of
+ * some key... Like avl_apply, this routine also takes a stopflag
+ * and will return prematurely if fmatch returns this value. Otherwise,
+ * AVL_NOMORE is returned.
+ */
+
+int
+avl_prefixapply(
+ Avlnode *root,
+ caddr_t data,
+ IFP fmatch,
+ caddr_t marg,
+ IFP fcmp,
+ caddr_t carg,
+ int stopflag
+)
+{
+ int cmp;
+
+ if ( root == 0 )
+ return( AVL_NOMORE );
+
+ cmp = (*fcmp)( data, root->avl_data, carg );
+ if ( cmp == 0 ) {
+ if ( (*fmatch)( root->avl_data, marg ) == stopflag )
+ return( stopflag );
+
+ if ( root->avl_left != 0 )
+ if ( avl_prefixapply( root->avl_left, data, fmatch,
+ marg, fcmp, carg, stopflag ) == stopflag )
+ return( stopflag );
+
+ if ( root->avl_right != 0 )
+ return( avl_prefixapply( root->avl_right, data, fmatch,
+ marg, fcmp, carg, stopflag ) );
+ else
+ return( AVL_NOMORE );
+
+ } else if ( cmp < 0 ) {
+ if ( root->avl_left != 0 )
+ return( avl_prefixapply( root->avl_left, data, fmatch,
+ marg, fcmp, carg, stopflag ) );
+ } else {
+ if ( root->avl_right != 0 )
+ return( avl_prefixapply( root->avl_right, data, fmatch,
+ marg, fcmp, carg, stopflag ) );
+ }
+
+ return( AVL_NOMORE );
+}
+
+/*
+ * avl_free -- traverse avltree root, freeing the memory it is using.
+ * the dfree() is called to free the data portion of each node. The
+ * number of items actually freed is returned.
+ */
+
+int
+avl_free( Avlnode *root, IFP dfree )
+{
+ int nleft, nright;
+
+ if ( root == 0 )
+ return( 0 );
+
+ nleft = nright = 0;
+ if ( root->avl_left != 0 )
+ nleft = avl_free( root->avl_left, dfree );
+
+ if ( root->avl_right != 0 )
+ nright = avl_free( root->avl_right, dfree );
+
+ if ( dfree )
+ (*dfree)( root->avl_data );
+
+ free( (char *)root );
+
+ return( nleft + nright + 1 );
+}
+
+/*
+ * avl_find -- search avltree root for a node with data data. the function
+ * cmp is used to compare things. it is called with data as its first arg
+ * and the current node data as its second. it should return 0 if they match,
+ * < 0 if arg1 is less than arg2 and > 0 if arg1 is greater than arg2.
+ */
+
+caddr_t
+avl_find( Avlnode *root, caddr_t data, IFP fcmp )
+{
+ int cmp;
+
+ while ( root != 0 && (cmp = (*fcmp)( data, root->avl_data )) != 0 ) {
+ if ( cmp < 0 )
+ root = root->avl_left;
+ else
+ root = root->avl_right;
+ }
+
+ return( root ? root->avl_data : 0 );
+}
+
+/*
+ * avl_find_lin -- search avltree root linearly for a node with data data.
+ * the function cmp is used to compare things. it is called with data as its
+ * first arg and the current node data as its second. it should return 0 if
+ * they match, non-zero otherwise.
+ */
+
+caddr_t
+avl_find_lin( Avlnode *root, caddr_t data, IFP fcmp )
+{
+ caddr_t res;
+
+ if ( root == 0 )
+ return( NULL );
+
+ if ( (*fcmp)( data, root->avl_data ) == 0 )
+ return( root->avl_data );
+
+ if ( root->avl_left != 0 )
+ if ( (res = avl_find_lin( root->avl_left, data, fcmp ))
+ != NULL )
+ return( res );
+
+ if ( root->avl_right == 0 )
+ return( NULL );
+ else
+ return( avl_find_lin( root->avl_right, data, fcmp ) );
+}
+
+static caddr_t *avl_list = (caddr_t *)0;
+static int avl_maxlist = 0;
+static int avl_nextlist = 0;
+
+#define AVL_GRABSIZE 100
+
+/* ARGSUSED */
+static int
+avl_buildlist( caddr_t data, int arg )
+{
+ static int slots = 0;
+
+ if ( avl_list == (caddr_t *) 0 ) {
+ avl_list = (caddr_t *) malloc(AVL_GRABSIZE * sizeof(caddr_t));
+ slots = AVL_GRABSIZE;
+ avl_maxlist = 0;
+ } else if ( avl_maxlist == slots ) {
+ slots += AVL_GRABSIZE;
+ avl_list = (caddr_t *) realloc( (char *) avl_list,
+ (unsigned) slots * sizeof(caddr_t));
+ }
+
+ avl_list[ avl_maxlist++ ] = data;
+
+ return( 0 );
+}
+
+/*
+ * avl_getfirst() and avl_getnext() are provided as alternate tree
+ * traversal methods, to be used when a single function cannot be
+ * provided to be called with every node in the tree. avl_getfirst()
+ * traverses the tree and builds a linear list of all the nodes,
+ * returning the first node. avl_getnext() returns the next thing
+ * on the list built by avl_getfirst(). This means that avl_getfirst()
+ * can take a while, and that the tree should not be messed with while
+ * being traversed in this way, and that multiple traversals (even of
+ * different trees) cannot be active at once.
+ */
+
+caddr_t
+avl_getfirst( Avlnode *root )
+{
+ if ( avl_list ) {
+ free( (char *) avl_list);
+ avl_list = (caddr_t *) 0;
+ }
+ avl_maxlist = 0;
+ avl_nextlist = 0;
+
+ if ( root == 0 )
+ return( 0 );
+
+ (void) avl_apply( root, avl_buildlist, (caddr_t) 0, -1, AVL_INORDER );
+
+ return( avl_list[ avl_nextlist++ ] );
+}
+
+caddr_t
+avl_getnext()
+{
+ if ( avl_list == 0 )
+ return( 0 );
+
+ if ( avl_nextlist == avl_maxlist ) {
+ free( (caddr_t) avl_list);
+ avl_list = (caddr_t *) 0;
+ return( 0 );
+ }
+
+ return( avl_list[ avl_nextlist++ ] );
+}
+
+int avl_dup_error()
+{
+ return( -1 );
+}
+
+int avl_dup_ok()
+{
+ return( 0 );
+}
diff --git a/ldap/libraries/libavl/testavl.c b/ldap/libraries/libavl/testavl.c
new file mode 100644
index 00000000..6efe8c84
--- /dev/null
+++ b/ldap/libraries/libavl/testavl.c
@@ -0,0 +1,125 @@
+/** BEGIN COPYRIGHT BLOCK
+ * Copyright 2001 Sun Microsystems, Inc.
+ * Portions copyright 1999, 2001-2003 Netscape Communications Corporation.
+ * All rights reserved.
+ * END COPYRIGHT BLOCK **/
+/* testavl.c - Test Tim Howes AVL code */
+#ifdef _WIN32
+#include <windows.h>
+#endif
+#include <sys/types.h>
+#include <stdio.h>
+#include "avl.h"
+
+char *strdup( s )
+char *s;
+{
+ char *new;
+
+ if ( (new = (char *) malloc( strlen( s ) + 1 )) == NULL )
+ return( NULL );
+
+ strcpy( new, s );
+
+ return( new );
+}
+
+main( argc, argv )
+int argc;
+char **argv;
+{
+ Avlnode *tree = NULLAVL;
+ char command[ 10 ];
+ char name[ 80 ];
+ char *p;
+ int free(), strcmp();
+
+ printf( "> " );
+ while ( fgets( command, sizeof( command ), stdin ) != NULL ) {
+ switch( *command ) {
+ case 'n': /* new tree */
+ ( void ) avl_free( tree, free );
+ tree = NULLAVL;
+ break;
+ case 'p': /* print */
+ ( void ) myprint( tree );
+ break;
+ case 't': /* traverse with first, next */
+ printf( "***\n" );
+ for ( p = (char * ) avl_getfirst( tree );
+ p != NULL; p = (char *) avl_getnext( tree, p ) )
+ printf( "%s\n", p );
+ printf( "***\n" );
+ break;
+ case 'f': /* find */
+ printf( "data? " );
+ if ( fgets( name, sizeof( name ), stdin ) == NULL )
+ exit( 0 );
+ name[ strlen( name ) - 1 ] = '\0';
+ if ( (p = (char *) avl_find( tree, name, strcmp ))
+ == NULL )
+ printf( "Not found.\n\n" );
+ else
+ printf( "%s\n\n", p );
+ break;
+ case 'i': /* insert */
+ printf( "data? " );
+ if ( fgets( name, sizeof( name ), stdin ) == NULL )
+ exit( 0 );
+ name[ strlen( name ) - 1 ] = '\0';
+ if ( avl_insert( &tree, strdup( name ), strcmp,
+ avl_dup_error ) != OK )
+ printf( "\nNot inserted!\n" );
+ break;
+ case 'd': /* delete */
+ printf( "data? " );
+ if ( fgets( name, sizeof( name ), stdin ) == NULL )
+ exit( 0 );
+ name[ strlen( name ) - 1 ] = '\0';
+ if ( avl_delete( &tree, name, strcmp ) == NULL )
+ printf( "\nNot found!\n" );
+ break;
+ case 'q': /* quit */
+ exit( 0 );
+ break;
+ case '\n':
+ break;
+ default:
+ printf("Commands: insert, delete, print, new, quit\n");
+ }
+
+ printf( "> " );
+ }
+ /* NOTREACHED */
+}
+
+static ravl_print( root, depth )
+Avlnode *root;
+int depth;
+{
+ int i;
+
+ if ( root == 0 )
+ return;
+
+ ravl_print( root->avl_right, depth+1 );
+
+ for ( i = 0; i < depth; i++ )
+ printf( " " );
+ printf( "%s %d\n", root->avl_data, root->avl_bf );
+
+ ravl_print( root->avl_left, depth+1 );
+}
+
+myprint( root )
+Avlnode *root;
+{
+ printf( "********\n" );
+
+ if ( root == 0 )
+ printf( "\tNULL\n" );
+ else
+ ( void ) ravl_print( root, 0 );
+
+ printf( "********\n" );
+}
diff --git a/ldap/libraries/libldif/Makefile b/ldap/libraries/libldif/Makefile
new file mode 100644
index 00000000..5420662c
--- /dev/null
+++ b/ldap/libraries/libldif/Makefile
@@ -0,0 +1,52 @@
+#
+# BEGIN COPYRIGHT BLOCK
+# Copyright 2001 Sun Microsystems, Inc.
+# Portions copyright 1999, 2001-2003 Netscape Communications Corporation.
+# All rights reserved.
+# END COPYRIGHT BLOCK
+#
+#
+# GNU Makefile for libldif
+#
+
+LDAP_SRC = ../..
+MCOM_ROOT = ../../../..
+
+NOSTDCLEAN=true # don't let nsconfig.mk define target clean
+NOSTDSTRIP=true # don't let nsconfig.mk define target strip
+NSPR20=true # probably should be defined somewhere else (not sure where)
+
+OBJDEST = $(OBJDIR)/lib/libldif
+LIBDIR = $(LDAP_LIBDIR)
+
+include $(MCOM_ROOT)/ldapserver/nsdefs.mk
+include $(MCOM_ROOT)/ldapserver/nsconfig.mk
+include $(LDAP_SRC)/nsldap.mk
+
+CFLAGS += $(SLCFLAGS)
+
+LIBLDIF_OBJS= line64.o fileurl.o
+
+OBJS = $(addprefix $(OBJDEST)/, $(LIBLDIF_OBJS))
+
+LIBLDIF= $(addprefix $(LIBDIR)/, libldif.$(LIB_SUFFIX))
+
+clientSDK: all
+
+all: $(OBJDEST) $(LIBDIR) $(OBJS) $(LIBLDIF)
+
+$(LIBDIR):
+ $(MKDIR) $(LIBDIR)
+
+$(LIBLDIF): $(OBJS)
+ $(LINK_LIB)
+
+veryclean: clean
+
+clean:
+ $(RM) $(OBJS)
+ $(RM) $(LIBLDIF)
+
+$(OBJDEST):
+ $(MKDIR) $(OBJDEST)
+
diff --git a/ldap/libraries/libldif/fileurl.c b/ldap/libraries/libldif/fileurl.c
new file mode 100644
index 00000000..b8c65aba
--- /dev/null
+++ b/ldap/libraries/libldif/fileurl.c
@@ -0,0 +1,289 @@
+/** BEGIN COPYRIGHT BLOCK
+ * Copyright 2001 Sun Microsystems, Inc.
+ * Portions copyright 1999, 2001-2003 Netscape Communications Corporation.
+ * All rights reserved.
+ * END COPYRIGHT BLOCK **/
+/*
+ * LDIF tools fileurl.c -- functions for handling file URLs.
+ * Used by ldif_parse_line.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <errno.h>
+#include "fileurl.h"
+#include <ctype.h> /* for isalpha() */
+
+static int str_starts_with( char *s, char *prefix );
+static void hex_unescape( char *s );
+static int unhex( char c );
+static void strcpy_escaped_and_convert( char *s1, char *s2 );
+
+/*
+ * Convert a file URL to a local path.
+ *
+ * If successful, LDIF_FILEURL_SUCCESS is returned and *localpathp is
+ * set point to an allocated string. If not, an different LDIF_FILEURL_
+ * error code is returned.
+ *
+ * See RFCs 1738 and 2396 for a specification for file URLs... but
+ * Netscape Navigator seems to be a bit more lenient in what it will
+ * accept, especially on Windows).
+ *
+ * This function parses file URLs of these three forms:
+ *
+ * file:///path
+ * file:/path
+ * file://localhost/path
+ * file://host/path (rejected with a ...NONLOCAL error)
+ *
+ * On Windows, we convert leading drive letters of the form C| to C:
+ * and if a drive letter is present we strip off the slash that precedes
+ * path. Otherwise, the leading slash is returned.
+ *
+ */
+int
+ldif_fileurl2path( char *fileurl, char **localpathp )
+{
+ char *path;
+
+ /*
+ * Make sure this is a file name or URL we can handle.
+ */
+ if ( *fileurl == '/' ||
+ ( isalpha( fileurl[0] ) && ( fileurl[1] == '|' || fileurl[1] == ':' ) ) ) {
+ path = fileurl;
+ goto path_ready;
+ } else if ( !str_starts_with( fileurl, "file:" )) {
+ return( LDIF_FILEURL_NOTAFILEURL );
+ }
+
+ path = fileurl + 5; /* skip past "file:" scheme prefix */
+
+ if ( *path != '/' ) {
+ return( LDIF_FILEURL_MISSINGPATH );
+ }
+
+ ++path; /* skip past '/' at end of "file:/" */
+
+ if ( *path == '/' ) {
+ ++path; /* remainder is now host/path or /path */
+ if ( *path != '/' ) {
+ /*
+ * Make sure it is for the local host.
+ */
+ if ( str_starts_with( path, "localhost/" )) {
+ path += 9;
+ } else {
+ return( LDIF_FILEURL_NONLOCAL );
+ }
+ }
+ } else { /* URL is of the form file:/path */
+ --path;
+ }
+
+ /*
+ * The remainder is now of the form /path. On Windows, skip past the
+ * leading slash if a drive letter is present.
+ */
+#ifdef _WIN32
+ if ( isalpha( path[1] ) && ( path[2] == '|' || path[2] == ':' )) {
+ ++path;
+ }
+#endif /* _WIN32 */
+
+
+ path_ready:
+ /*
+ * Duplicate the path so we can safely alter it.
+ * Unescape any %HH sequences.
+ */
+ if (( path = strdup( path )) == NULL ) {
+ return( LDIF_FILEURL_NOMEMORY );
+ }
+ hex_unescape( path );
+
+#ifdef _WIN32
+ /*
+ * Convert forward slashes to backslashes for Windows. Also,
+ * if we see a drive letter / vertical bar combination (e.g., c|)
+ * at the beginning of the path, replace the '|' with a ':'.
+ */
+ {
+ char *p;
+
+ for ( p = path; *p != '\0'; ++p ) {
+ if ( *p == '/' ) {
+ *p = '\\';
+ }
+ }
+ }
+
+ if ( isalpha( path[0] ) && path[1] == '|' ) {
+ path[1] = ':';
+ }
+#endif /* _WIN32 */
+
+ *localpathp = path;
+ return( LDIF_FILEURL_SUCCESS );
+}
+
+
+/*
+ * Convert a local path to a file URL.
+ *
+ * If successful, LDIF_FILEURL_SUCCESS is returned and *urlp is
+ * set point to an allocated string. If not, an different LDIF_FILEURL_
+ * error code is returned. At present, the only possible error is
+ * LDIF_FILEURL_NOMEMORY.
+ *
+ * This function produces file URLs of the form file:path.
+ *
+ * On Windows, we convert leading drive letters to C|.
+ *
+ */
+int
+ldif_path2fileurl( char *path, char **urlp )
+{
+ char *p, *url, *prefix ="file:";
+
+ if ( NULL == path ) {
+ path = "/";
+ }
+
+ /*
+ * Allocate space for the URL, taking into account that path may
+ * expand during the hex escaping process.
+ */
+ if (( url = malloc( strlen( prefix ) + 3 * strlen( path ) + 1 )) == NULL ) {
+ return( LDIF_FILEURL_NOMEMORY );
+ }
+
+ strcpy( url, prefix );
+ p = url + strlen( prefix );
+
+#ifdef _WIN32
+ /*
+ * On Windows, convert leading drive letters (e.g., C:) to the correct URL
+ * syntax (e.g., C|).
+ */
+ if ( isalpha( path[0] ) && path[1] == ':' ) {
+ *p++ = path[0];
+ *p++ = '|';
+ path += 2;
+ *p = '\0';
+ }
+#endif /* _WIN32 */
+
+ /*
+ * Append the path, encoding any URL-special characters using the %HH
+ * convention.
+ * On Windows, convert backwards slashes in the path to forward ones.
+ */
+ strcpy_escaped_and_convert( p, path );
+
+ *urlp = url;
+ return( LDIF_FILEURL_SUCCESS );
+}
+
+
+/*
+ * Return a non-zero value if the string s begins with prefix and zero if not.
+ */
+static int
+str_starts_with( char *s, char *prefix )
+{
+ size_t prefix_len;
+
+ if ( s == NULL || prefix == NULL ) {
+ return( 0 );
+ }
+
+ prefix_len = strlen( prefix );
+ if ( strlen( s ) < prefix_len ) {
+ return( 0 );
+ }
+
+ return( strncmp( s, prefix, prefix_len ) == 0 );
+}
+
+
+/*
+ * Remove URL hex escapes from s... done in place. The basic concept for
+ * this routine is borrowed from the WWW library HTUnEscape() routine.
+ *
+ */
+static void
+hex_unescape( char *s )
+{
+ char *p;
+
+ for ( p = s; *s != '\0'; ++s ) {
+ if ( *s == '%' ) {
+ if ( *++s != '\0' ) {
+ *p = unhex( *s ) << 4;
+ }
+ if ( *++s != '\0' ) {
+ *p++ += unhex( *s );
+ }
+ } else {
+ *p++ = *s;
+ }
+ }
+
+ *p = '\0';
+}
+
+
+/*
+ * Return the integer equivalent of one hex digit (in c).
+ *
+ */
+static int
+unhex( char c )
+{
+ return( c >= '0' && c <= '9' ? c - '0'
+ : c >= 'A' && c <= 'F' ? c - 'A' + 10
+ : c - 'a' + 10 );
+}
+
+
+#define HREF_CHAR_ACCEPTABLE( c ) (( c >= '-' && c <= '9' ) || \
+ ( c >= '@' && c <= 'Z' ) || \
+ ( c == '_' ) || \
+ ( c >= 'a' && c <= 'z' ))
+
+/*
+ * Like strcat(), except if any URL-special characters are found in s2
+ * they are escaped using the %HH convention and backslash characters are
+ * converted to forward slashes on Windows.
+ *
+ * Maximum space needed in s1 is 3 * strlen( s2 ) + 1.
+ *
+ */
+static void
+strcpy_escaped_and_convert( char *s1, char *s2 )
+{
+ char *p, *q;
+ char *hexdig = "0123456789ABCDEF";
+
+ p = s1 + strlen( s1 );
+ for ( q = s2; *q != '\0'; ++q ) {
+#ifdef _WIN32
+ if ( *q == '\\' ) {
+ *p++ = '/';
+ } else
+#endif /* _WIN32 */
+
+ if ( HREF_CHAR_ACCEPTABLE( *q )) {
+ *p++ = *q;
+ } else {
+ *p++ = '%';
+ *p++ = hexdig[ 0x0F & ((*(unsigned char*)q) >> 4) ];
+ *p++ = hexdig[ 0x0F & *q ];
+ }
+ }
+
+ *p = '\0';
+}
diff --git a/ldap/libraries/libldif/fileurl.h b/ldap/libraries/libldif/fileurl.h
new file mode 100644
index 00000000..727a641c
--- /dev/null
+++ b/ldap/libraries/libldif/fileurl.h
@@ -0,0 +1,40 @@
+/** BEGIN COPYRIGHT BLOCK
+ * Copyright 2001 Sun Microsystems, Inc.
+ * Portions copyright 1999, 2001-2003 Netscape Communications Corporation.
+ * All rights reserved.
+ * END COPYRIGHT BLOCK **/
+/*
+ * LDIF tools fileurl.h -- defines for file URL functions.
+ * Used by ldif_parse_line.
+ */
+
+/*
+ * ldif_fileurl2path() convert a file URL to a local path.
+ *
+ * If successful, LDIF_FILEURL_SUCCESS is returned and *localpathp is
+ * set point to an allocated string. If not, an differnet LDIF_FILEURL_
+ * error code is returned.
+ */
+int ldif_fileurl2path( char *fileurl, char **localpathp );
+
+
+/*
+ * Convert a local path to a file URL.
+ *
+ * If successful, LDIF_FILEURL_SUCCESS is returned and *urlp is
+ * set point to an allocated string. If not, an different LDIF_FILEURL_
+ * error code is returned. At present, the only possible error is
+ * LDIF_FILEURL_NOMEMORY.
+ *
+ */
+int ldif_path2fileurl( char *path, char **urlp );
+
+
+/*
+ * Possible return codes for ldif_fileurl2path and ldif_path2fileurl.
+ */
+#define LDIF_FILEURL_SUCCESS 0
+#define LDIF_FILEURL_NOTAFILEURL 1
+#define LDIF_FILEURL_MISSINGPATH 2
+#define LDIF_FILEURL_NONLOCAL 3
+#define LDIF_FILEURL_NOMEMORY 4
diff --git a/ldap/libraries/libldif/line64.c b/ldap/libraries/libldif/line64.c
new file mode 100644
index 00000000..ade33a91
--- /dev/null
+++ b/ldap/libraries/libldif/line64.c
@@ -0,0 +1,743 @@
+/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
+ *
+ * The contents of this file are subject to the Netscape Public License
+ * Version 1.0 (the "NPL"); you may not use this file except in
+ * compliance with the NPL. You may obtain a copy of the NPL at
+ * http://www.mozilla.org/NPL/
+ *
+ * Software distributed under the NPL is distributed on an "AS IS" basis,
+ * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
+ * for the specific language governing rights and limitations under the
+ * NPL.
+ *
+ * The Initial Developer of this code under the NPL is Netscape
+ * Communications Corporation. Portions created by Netscape are
+ * Copyright (C) 1998 Netscape Communications Corporation. All Rights
+ * Reserved.
+ */
+/* line64.c - routines for dealing with the slapd line format */
+
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include <ctype.h>
+#ifndef macintosh
+#include <sys/types.h>
+#endif
+#ifdef _WIN32
+#include <windows.h>
+#elif !defined( macintosh )
+#include <sys/socket.h>
+#endif
+#include "ldaplog.h"
+#include "ldif.h"
+#include <errno.h>
+#include "lber.h"
+#include <sys/stat.h>
+#include "fileurl.h"
+
+#ifndef isascii
+#define isascii( c ) (!((c) & ~0177))
+#endif
+
+#define RIGHT2 0x03
+#define RIGHT4 0x0f
+#define CONTINUED_LINE_MARKER '\001'
+
+#define ISBLANK(c) (c == ' ' || c == '\t' || c == '\n') /* not "\r\v\f" */
+
+#define LDIF_OPT_ISSET( value, opt ) (((value) & (opt)) != 0 )
+
+static char nib2b64[0x40] =
+ "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
+
+static unsigned char b642nib[0x80] = {
+ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
+ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
+ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
+ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
+ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
+ 0xff, 0xff, 0xff, 0x3e, 0xff, 0xff, 0xff, 0x3f,
+ 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b,
+ 0x3c, 0x3d, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
+ 0xff, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
+ 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e,
+ 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16,
+ 0x17, 0x18, 0x19, 0xff, 0xff, 0xff, 0xff, 0xff,
+ 0xff, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20,
+ 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28,
+ 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30,
+ 0x31, 0x32, 0x33, 0xff, 0xff, 0xff, 0xff, 0xff
+};
+
+static int ldif_base64_encode_internal( unsigned char *src, char *dst, int srclen,
+ int lenused, int wraplen );
+
+static int ldif_fromfile( char *path, struct berval *bv );
+
+extern int errno;
+/*
+ * ldif_parse_line - takes a line of the form "type:[:] value" and splits it
+ * into components "type" and "value". if a double colon separates type from
+ * value, then value is encoded in base 64, and parse_line un-decodes it
+ * (in place) before returning.
+ * Success return might be 0 (value is returned in place) or 1 (value has
+ * been malloc'ed)
+ */
+
+int
+ldif_parse_line(
+ char *line,
+ char **type,
+ char **value,
+ int *vlen,
+ char **errmsg
+)
+{
+ char *p, *s, *d;
+ int b64;
+ int url = 0;
+ int rc = 0;
+
+ *errmsg = NULL;
+
+ /* skip any leading space */
+ while ( ISBLANK( *line ) ) {
+ line++;
+ }
+ *type = line;
+
+ for ( s = line; *s && *s != ':'; s++ )
+ ; /* NULL */
+ if ( *s == '\0' ) {
+
+ /* Comment-out while we address calling libldif from ns-back-ldbm
+ on NT. 1 of 3 */
+#if defined( _WIN32 )
+ /*
+#endif
+ LDAPDebug( LDAP_DEBUG_PARSE, "ldif_parse_line: missing ':' "
+ "on line \"%s\"\n", line, 0, 0 );
+#if defined( _WIN32 )
+ */
+#endif
+ return( -1 );
+ }
+
+ /* trim any space between type and : */
+ for ( p = s - 1; p > line && ISBLANK( *p ); p-- ) {
+ *p = '\0';
+ }
+ *s++ = '\0';
+
+ /* check for double : - indicates base 64 encoded value */
+ if ( *s == ':' ) {
+ s++;
+ b64 = 1;
+
+ /* single : - normally encoded value */
+ } else {
+ /* check for ":<" - indicates value is actually an url */
+ if (*s == '<') {
+ s++;
+ url = 1;
+ }
+ b64 = 0;
+ }
+
+ /* skip space between : and value */
+ while ( ISBLANK( *s ) ) {
+ s++;
+ }
+
+ /*
+ * If no value is present, return a zero-length string for
+ * *value, with *vlen set to zero.
+ */
+ if ( *s == '\0' ) {
+ *value = s;
+ *vlen = 0;
+ return( 0 );
+ }
+
+ /* check for continued line markers that should be deleted */
+ for ( p = s, d = s; *p; p++ ) {
+ if ( *p != CONTINUED_LINE_MARKER )
+ *d++ = *p;
+ }
+ *d = '\0';
+
+ *value = s;
+ if ( b64 ) {
+ if (( *vlen = ldif_base64_decode( s, (unsigned char *)s ))
+ < 0 ) {
+ /* Comment-out while we address calling libldif from ns-back-ldbm
+ on NT. 3 of 3 */
+#if defined( _WIN32 )
+ /*
+#endif
+ LDAPDebug( LDAP_DEBUG_ANY,
+ "ldif_parse_line: invalid base 64 char on line \"%s\"\n",
+ line, 0, 0 );
+#if defined( _WIN32 )
+ */
+#endif
+ return( -1 );
+ }
+ s[ *vlen ] = '\0';
+
+ } else if (url) {
+
+ char *path;
+ struct stat fstats;
+ struct berval bv;
+
+ bv.bv_val = NULL;
+
+ if (( *errmsg = (char *)malloc( strlen(s) + 1024 )) == NULL ) {
+ return (-1);
+ }
+
+ /*
+ * We only support file:// URLs for now.
+ */
+
+ switch( ldif_fileurl2path( s, &path )) {
+ case LDIF_FILEURL_NOTAFILEURL:
+ sprintf(*errmsg,
+ "ldif_parse_line: unsupported URL \"%S\";"
+ " use a file:// URL instead.\n", s);
+ rc = -1;
+ break;
+
+ case LDIF_FILEURL_MISSINGPATH:
+ sprintf(*errmsg,
+ "ldif_parse_line: unable to process URL \"%S\" --"
+ " missing path..\n", s);
+ rc = -1;
+ break;
+
+ case LDIF_FILEURL_NONLOCAL:
+ sprintf(*errmsg,
+ "ldif_parse_line: unable to process URL \"%S\" --"
+ " only local file:// URLs are supported.\n", s);
+ rc = -1;
+ break;
+
+ case LDIF_FILEURL_NOMEMORY:
+ perror( "ldif_fileurl2path" );
+ rc = -1;
+ break;
+
+ case LDIF_FILEURL_SUCCESS:
+ if ( stat( path, &fstats ) != 0 ) {
+ perror( path );
+ rc = -1;
+ } else if ( fstats.st_mode & S_IFDIR ) {
+ sprintf(*errmsg,
+ "ldif_parse_line: %s is a directory, not a file.\n", path);
+ rc = -1;
+ } else if ( ldif_fromfile( path, &bv ) < 0 ) {
+ sprintf(*errmsg,
+ "ldif_parse_line: unable to retrieve information"
+ " from file %s.\n", path);
+ rc = -1;
+ }
+ free( path );
+ break;
+
+ default:
+ sprintf(*errmsg,
+ "ldif_parse_line: unable to process URL \"%S\" --"
+ " unknown error.\n", s);
+ rc = -1;
+ }
+
+
+ if ( rc != 0 ) {
+ if (bv.bv_val)
+ free(bv.bv_val);
+ } else {
+ *value = bv.bv_val;
+ *vlen = bv.bv_len;
+ rc = 1;
+ }
+ } else {
+ *vlen = (int) (d - s);
+ }
+
+ return( rc );
+}
+
+
+/*
+ * ldif_base64_decode - take the BASE64-encoded characters in "src"
+ * (a zero-terminated string) and decode them into the the buffer "dst".
+ * "src" and "dst" can be the same if in-place decoding is desired.
+ * "dst" must be large enough to hold the decoded octets. No more than
+ * 3 * strlen( src ) / 4 bytes will be produced.
+ * "dst" may contain zero octets anywhere within it, but it is not
+ * zero-terminated by this function.
+ *
+ * The number of bytes copied to "dst" is returned if all goes well.
+ * -1 is returned if the BASE64 encoding in "src" is invalid.
+ */
+
+int
+ldif_base64_decode( char *src, unsigned char *dst )
+{
+ char *p, *stop;
+ unsigned char nib, *byte;
+ int i, len;
+
+ stop = strchr( src, '\0' );
+ byte = dst;
+ for ( p = src, len = 0; p < stop; p += 4, len += 3 ) {
+ for ( i = 0; i < 4; i++ ) {
+ if ( p[i] != '=' && (p[i] & 0x80 ||
+ b642nib[ p[i] & 0x7f ] > 0x3f) ) {
+ return( -1 );
+ }
+ }
+
+ /* first digit */
+ nib = b642nib[ p[0] & 0x7f ];
+ byte[0] = nib << 2;
+
+ /* second digit */
+ nib = b642nib[ p[1] & 0x7f ];
+ byte[0] |= nib >> 4;
+
+ /* third digit */
+ if ( p[2] == '=' ) {
+ len += 1;
+ break;
+ }
+ byte[1] = (nib & RIGHT4) << 4;
+ nib = b642nib[ p[2] & 0x7f ];
+ byte[1] |= nib >> 2;
+
+ /* fourth digit */
+ if ( p[3] == '=' ) {
+ len += 2;
+ break;
+ }
+ byte[2] = (nib & RIGHT2) << 6;
+ nib = b642nib[ p[3] & 0x7f ];
+ byte[2] |= nib;
+
+ byte += 3;
+ }
+
+ return( len );
+}
+
+/*
+ * ldif_getline - return the next "line" (minus newline) of input from a
+ * string buffer of lines separated by newlines, terminated by \n\n
+ * or \0. this routine handles continued lines, bundling them into
+ * a single big line before returning. if a line begins with a white
+ * space character, it is a continuation of the previous line. the white
+ * space character (nb: only one char), and preceeding newline are changed
+ * into CONTINUED_LINE_MARKER chars, to be deleted later by the
+ * ldif_parse_line() routine above.
+ *
+ * it takes a pointer to a pointer to the buffer on the first call,
+ * which it updates and must be supplied on subsequent calls.
+ *
+ * XXX need to update this function to also support <CR><LF> as EOL.
+ * XXX supports <CR><LF> as of 07/29/1998 (richm)
+ */
+
+char *
+ldif_getline( char **next )
+{
+ char *l;
+ char c;
+ char *p;
+
+ if ( *next == NULL || **next == '\n' || **next == '\0' ) {
+ return( NULL );
+ }
+
+ while ( **next == '#' ) { /* skip comment lines */
+ if (( *next = strchr( *next, '\n' )) == NULL ) {
+ return( NULL );
+ }
+ (*next)++;
+ }
+
+ l = *next;
+ while ( (*next = strchr( *next, '\n' )) != NULL ) {
+ p = *next - 1; /* pointer to character previous to the newline */
+ c = *(*next + 1); /* character after the newline */
+ if ( ISBLANK( c ) && c != '\n' ) {
+ /* DOS EOL is \r\n, so if the character before */
+ /* the \n is \r, continue it too */
+ if (*p == '\r')
+ *p = CONTINUED_LINE_MARKER;
+ **next = CONTINUED_LINE_MARKER;
+ *(*next+1) = CONTINUED_LINE_MARKER;
+ } else {
+ /* DOS EOL is \r\n, so if the character before */
+ /* the \n is \r, null it too */
+ if (*p == '\r')
+ *p = '\0';
+ *(*next)++ = '\0';
+ break;
+ }
+ (*next)++;
+ }
+
+ return( l );
+}
+
+
+#define LDIF_SAFE_CHAR( c ) ( (c) != '\r' && (c) != '\n' )
+#define LDIF_CONSERVATIVE_CHAR( c ) ( LDIF_SAFE_CHAR(c) && isascii((c)) \
+ && ( isprint((c)) || (c) == '\t' ))
+#define LDIF_SAFE_INITCHAR( c ) ( LDIF_SAFE_CHAR(c) && (c) != ':' \
+ && (c) != ' ' && (c) != '<' )
+#define LDIF_CONSERVATIVE_INITCHAR( c ) ( LDIF_SAFE_INITCHAR( c ) && \
+ ! ( isascii((c)) && isspace((c))))
+#define LDIF_CONSERVATIVE_FINALCHAR( c ) ( (c) != ' ' )
+
+
+void
+ldif_put_type_and_value_with_options( char **out, char *t, char *val,
+ int vlen, unsigned long options )
+{
+ unsigned char *p, *byte, *stop;
+ char *save;
+ int b64, len, savelen, wraplen;
+ len = 0;
+
+ if ( LDIF_OPT_ISSET( options, LDIF_OPT_NOWRAP )) {
+ wraplen = -1;
+ } else {
+ wraplen = LDIF_MAX_LINE_WIDTH;
+ }
+
+ /* put the type + ": " */
+ for ( p = (unsigned char *) t; *p; p++, len++ ) {
+ *(*out)++ = *p;
+ }
+ *(*out)++ = ':';
+ len++;
+ if ( LDIF_OPT_ISSET( options, LDIF_OPT_VALUE_IS_URL )) {
+ *(*out)++ = '<'; /* add '<' for URLs */
+ len++;
+ }
+ save = *out;
+ savelen = len;
+ b64 = 0;
+
+ stop = (unsigned char *)val;
+ if ( val && vlen > 0 ) {
+ *(*out)++ = ' ';
+ stop = (unsigned char *) (val + vlen);
+ if ( LDIF_OPT_ISSET( options, LDIF_OPT_MINIMAL_ENCODING )) {
+ if ( !LDIF_SAFE_INITCHAR( val[0] )) {
+ b64 = 1;
+ }
+ } else {
+ if ( !LDIF_CONSERVATIVE_INITCHAR( val[0] ) ||
+ !LDIF_CONSERVATIVE_FINALCHAR( val[vlen-1] )) {
+ b64 = 1;
+ }
+ }
+ }
+
+ if ( !b64 ) {
+ for ( byte = (unsigned char *) val; byte < stop;
+ byte++, len++ ) {
+ if ( LDIF_OPT_ISSET( options,
+ LDIF_OPT_MINIMAL_ENCODING )) {
+ if ( !LDIF_SAFE_CHAR( *byte )) {
+ b64 = 1;
+ break;
+ }
+ } else if ( !LDIF_CONSERVATIVE_CHAR( *byte )) {
+ b64 = 1;
+ break;
+ }
+
+ if ( wraplen != -1 && len > wraplen ) {
+ *(*out)++ = '\n';
+ *(*out)++ = ' ';
+ len = 1;
+ }
+ *(*out)++ = *byte;
+ }
+ }
+
+ if ( b64 ) {
+ *out = save;
+ *(*out)++ = ':';
+ *(*out)++ = ' ';
+ len = ldif_base64_encode_internal( (unsigned char *)val, *out, vlen,
+ savelen + 2, wraplen );
+ *out += len;
+ }
+
+ *(*out)++ = '\n';
+}
+
+void
+ldif_put_type_and_value( char **out, char *t, char *val, int vlen )
+{
+ ldif_put_type_and_value_with_options( out, t, val, vlen, 0 );
+}
+
+void
+ldif_put_type_and_value_nowrap( char **out, char *t, char *val, int vlen )
+{
+ ldif_put_type_and_value_with_options( out, t, val, vlen, LDIF_OPT_NOWRAP );
+}
+
+/*
+ * ldif_base64_encode_internal - encode "srclen" bytes in "src", place BASE64
+ * encoded bytes in "dst" and return the length of the BASE64
+ * encoded string. "dst" is also zero-terminated by this function.
+ *
+ * If "lenused" >= 0, newlines will be included in "dst" and "lenused" if
+ * appropriate. "lenused" should be a count of characters already used
+ * on the current line. The LDIF lines we create will contain at most
+ * "wraplen" characters on each line, unless "wraplen" is -1, in which
+ * case output line length is unlimited.
+ *
+ * If "lenused" < 0, no newlines will be included, and the LDIF_BASE64_LEN()
+ * macro can be used to determine how many bytes will be placed in "dst."
+ */
+
+static int
+ldif_base64_encode_internal( unsigned char *src, char *dst, int srclen, int lenused, int wraplen )
+{
+ unsigned char *byte, *stop;
+ unsigned char buf[3];
+ char *out;
+ unsigned long bits;
+ int i, pad, len;
+
+ len = 0;
+ out = dst;
+ stop = src + srclen;
+
+ /* convert to base 64 (3 bytes => 4 base 64 digits) */
+ for ( byte = src; byte < stop - 2; byte += 3 ) {
+ bits = (byte[0] & 0xff) << 16;
+ bits |= (byte[1] & 0xff) << 8;
+ bits |= (byte[2] & 0xff);
+
+ for ( i = 0; i < 4; i++, bits <<= 6 ) {
+ if ( wraplen != -1 && lenused >= 0 && lenused++ > wraplen ) {
+ *out++ = '\n';
+ *out++ = ' ';
+ lenused = 2;
+ }
+
+ /* get b64 digit from high order 6 bits */
+ *out++ = nib2b64[ (bits & 0xfc0000L) >> 18 ];
+ }
+ }
+
+ /* add padding if necessary */
+ if ( byte < stop ) {
+ for ( i = 0; byte + i < stop; i++ ) {
+ buf[i] = byte[i];
+ }
+ for ( pad = 0; i < 3; i++, pad++ ) {
+ buf[i] = '\0';
+ }
+ byte = buf;
+ bits = (byte[0] & 0xff) << 16;
+ bits |= (byte[1] & 0xff) << 8;
+ bits |= (byte[2] & 0xff);
+
+ for ( i = 0; i < 4; i++, bits <<= 6 ) {
+ if ( wraplen != -1 && lenused >= 0 && lenused++ > wraplen ) {
+ *out++ = '\n';
+ *out++ = ' ';
+ lenused = 2;
+ }
+
+ if (( i == 3 && pad > 0 ) || ( i == 2 && pad == 2 )) {
+ /* Pad as appropriate */
+ *out++ = '=';
+ } else {
+ /* get b64 digit from low order 6 bits */
+ *out++ = nib2b64[ (bits & 0xfc0000L) >> 18 ];
+ }
+ }
+ }
+
+ *out = '\0';
+
+ return( out - dst );
+}
+
+int
+ldif_base64_encode( unsigned char *src, char *dst, int srclen, int lenused )
+{
+ return ldif_base64_encode_internal( src, dst, srclen, lenused, LDIF_MAX_LINE_WIDTH );
+}
+
+int
+ldif_base64_encode_nowrap( unsigned char *src, char *dst, int srclen, int lenused )
+{
+ return ldif_base64_encode_internal( src, dst, srclen, lenused, -1 );
+}
+
+
+/*
+ * return malloc'd, zero-terminated LDIF line
+ */
+char *
+ldif_type_and_value_with_options( char *type, char *val, int vlen,
+ unsigned long options )
+{
+ char *buf, *p;
+ int tlen;
+
+ tlen = strlen( type );
+ if (( buf = (char *)malloc( LDIF_SIZE_NEEDED( tlen, vlen ) + 1 )) !=
+ NULL ) {
+ p = buf;
+ ldif_put_type_and_value_with_options( &p, type, val, vlen, options );
+ *p = '\0';
+ }
+
+ return( buf );
+}
+
+char *
+ldif_type_and_value( char *type, char *val, int vlen )
+{
+ return ldif_type_and_value_with_options( type, val, vlen, 0 );
+}
+
+char *
+ldif_type_and_value_nowrap( char *type, char *val, int vlen )
+{
+ return ldif_type_and_value_with_options( type, val, vlen, LDIF_OPT_NOWRAP );
+}
+
+/*
+ * ldif_get_entry - read the next ldif entry from the FILE referenced
+ * by fp. return a pointer to a malloc'd, null-terminated buffer. also
+ * returned is the last line number read, in *lineno.
+ */
+char *
+ldif_get_entry( FILE *fp, int *lineno )
+{
+ char line[BUFSIZ];
+ char *buf;
+ int max, cur, len, gotsome;
+
+ buf = NULL;
+ max = cur = gotsome = 0;
+ while ( fgets( line, sizeof(line), fp ) != NULL ) {
+ if ( lineno != NULL ) {
+ (*lineno)++;
+ }
+ /* ldif entries are terminated by a \n on a line by itself */
+ if ( line[0] == '\0' || line[0] == '\n'
+#if !defined( XP_WIN32 )
+ || ( line[0] == '\r' && line[1] == '\n' ) /* DOS format */
+#endif
+ ) {
+ if ( gotsome ) {
+ break;
+ } else {
+ continue;
+ }
+ } else if ( line[0] == '#' ) {
+ continue;
+ }
+ gotsome = 1;
+ len = strlen( line );
+#if !defined( XP_WIN32 )
+ /* DOS format */
+ if ( len > 0 && line[len-1] == '\r' ) {
+ --len;
+ line[len] = '\0';
+ } else if ( len > 1 && line[len-2] == '\r' && line[len-1] == '\n' ) {
+ --len;
+ line[len-1] = line[len];
+ line[len] = '\0';
+ }
+#endif
+ while ( cur + (len + 1) > max ) {
+ if ( buf == NULL ) {
+ max += BUFSIZ;
+ buf = (char *) malloc( max );
+ } else {
+ max *= 2;
+ buf = (char *) realloc( buf, max );
+ }
+ if ( buf == NULL ) {
+ return( NULL );
+ }
+ }
+
+ memcpy( buf + cur, line, len + 1 );
+ cur += len;
+ }
+
+ return( buf );
+}
+
+
+
+static int
+ldif_fromfile( char *path, struct berval *bv )
+{
+ FILE *fp;
+ long rlen;
+ int eof;
+#if defined( XP_WIN32 )
+ char mode[20] = "r+b";
+#else
+ char mode[20] = "r";
+#endif
+
+ if (( fp = fopen( path, mode )) == NULL ) {
+ perror( path );
+ return( -1 );
+ }
+
+ if ( fseek( fp, 0L, SEEK_END ) != 0 ) {
+ perror( path );
+ fclose( fp );
+ return( -1 );
+ }
+
+ bv->bv_len = ftell( fp );
+
+ if (( bv->bv_val = (char *)malloc( bv->bv_len + 1 )) == NULL ) {
+ perror( "malloc" );
+ fclose( fp );
+ return( -1 );
+ }
+
+ if ( fseek( fp, 0L, SEEK_SET ) != 0 ) {
+ perror( path );
+ fclose( fp );
+ return( -1 );
+ }
+
+ rlen = fread( bv->bv_val, 1, bv->bv_len, fp );
+ eof = feof( fp );
+ fclose( fp );
+
+ if ( rlen != (long)bv->bv_len ) {
+ perror( path );
+ free( bv->bv_val );
+ return( -1 );
+ }
+
+ bv->bv_val[ bv->bv_len ] = '\0';
+ return( bv->bv_len );
+}
+
+
diff --git a/ldap/libraries/liblitekey/Makefile b/ldap/libraries/liblitekey/Makefile
new file mode 100644
index 00000000..27e6d942
--- /dev/null
+++ b/ldap/libraries/liblitekey/Makefile
@@ -0,0 +1,50 @@
+#
+# BEGIN COPYRIGHT BLOCK
+# Copyright 2001 Sun Microsystems, Inc.
+# Portions copyright 1999, 2001-2003 Netscape Communications Corporation.
+# All rights reserved.
+# END COPYRIGHT BLOCK
+#
+#
+# GNU Makefile for liblitekey
+#
+
+LDAP_SRC = ../..
+MCOM_ROOT = ../../../..
+
+NOSTDCLEAN=true # don't let nsconfig.mk define target clean
+NOSTDSTRIP=true # don't let nsconfig.mk define target strip
+NSPR20=true # probably should be defined somewhere else (not sure where)
+
+OBJDEST = $(OBJDIR)/lib/liblitekey
+LIBDIR = $(LDAP_LIBDIR)
+
+include $(MCOM_ROOT)/ldapserver/nsdefs.mk
+include $(MCOM_ROOT)/ldapserver/nsconfig.mk
+include $(LDAP_SRC)/nsldap.mk
+
+CFLAGS += $(SLCFLAGS)
+
+LIBLITEKEY_OBJS= keycheck.o
+
+OBJS = $(addprefix $(OBJDEST)/, $(LIBLITEKEY_OBJS))
+
+LIBLITEKEY = $(addprefix $(LIBDIR)/, liblitekey.$(LIB_SUFFIX))
+
+all: $(OBJDEST) $(LIBDIR) $(OBJS) $(LIBLITEKEY)
+
+$(LIBDIR):
+ $(MKDIR) $(LIBDIR)
+
+$(LIBLITEKEY): $(OBJS)
+ $(LINK_LIB)
+
+veryclean: clean
+
+clean:
+ $(RM) $(OBJS)
+ $(RM) $(LIBLITEKEY)
+
+$(OBJDEST):
+ $(MKDIR) $(OBJDEST)
+
diff --git a/ldap/libraries/liblitekey/keycheck.c b/ldap/libraries/liblitekey/keycheck.c
new file mode 100644
index 00000000..4f3ee505
--- /dev/null
+++ b/ldap/libraries/liblitekey/keycheck.c
@@ -0,0 +1,137 @@
+/** BEGIN COPYRIGHT BLOCK
+ * Copyright 2001 Sun Microsystems, Inc.
+ * Portions copyright 1999, 2001-2003 Netscape Communications Corporation.
+ * All rights reserved.
+ * END COPYRIGHT BLOCK **/
+/*
+ * keycheck.c
+ */
+
+
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include <litekey.h>
+
+#define DS_NORMAL_MAGIC_KEY 119
+#define DS_LITE_MAGIC_KEY 326
+#define FILE_PATHSEP '/'
+#define BUFSIZE 800
+
+/*
+ * is_directory_lite
+ *
+ * Checks if the directory server installation is a normal or a
+ * lite. The decision is made based on the key in the key file.
+ *
+ * Input:
+ * char *root; Pathname to install root
+ * Returns:
+ * 1 - yes, it's LITE server
+ * 0 - No; it's fully paid (normal) server.
+ *
+ */
+int is_directory_lite( char *root)
+{
+
+ char buf[40];
+ char *bufp = buf;
+ FILE *fp = NULL;
+ int key =0;
+ char *nsroot;
+ char pathname[BUFSIZE];
+
+ return DS_NORMAL_TYPE; /* richm: no more lite mode in DS 5.0 */
+#if 0 /* no more lite mode */
+ /* There are 3 ways to determine if the server is FULL or LITE.
+ * 1) Use NETSITE_ROOT variable
+ * 2) Use the root path provided
+ * 3) Look at the current directory
+ *
+ * If all of them fails, then it's LITE.
+ */
+ nsroot = getenv("NETSITE_ROOT");
+
+ if ( (NULL == root) && (NULL == nsroot)) {
+ /* case 3 */
+ sprintf ( pathname, "slapd.key" );
+ } else if (NULL == nsroot) {
+ /* case 2 */
+ sprintf ( pathname, "%s%cbin%cslapd%cserver%cslapd.key",
+ root, FILE_PATHSEP,FILE_PATHSEP,
+ FILE_PATHSEP, FILE_PATHSEP);
+ } else {
+ /* case 1 */
+ sprintf ( pathname, "%s%cbin%cslapd%cserver%cslapd.key",
+ nsroot, FILE_PATHSEP,FILE_PATHSEP,
+ FILE_PATHSEP, FILE_PATHSEP);
+ }
+
+
+ /* First read from the key file */
+ if ((fp = fopen ( pathname, "r")) == NULL )
+ return DS_LITE_TYPE;
+
+ if ( fgets(buf, 40, fp) == NULL)
+ return DS_LITE_TYPE;
+
+ fclose (fp );
+
+ /* The key is in the format: "key:123456" */
+ bufp +=4;
+ key = atoi ( (const char *) bufp );
+
+ /* Now we have the key. Determine which one it is */
+ if ( 0 == (key % DS_NORMAL_MAGIC_KEY))
+ return DS_NORMAL_TYPE;
+ else if ( 0 == (key % DS_LITE_MAGIC_KEY) )
+ return DS_LITE_TYPE;
+
+ /* By defualt, it's lite */
+ return DS_LITE_TYPE;
+#endif /* no more lite mode */
+}
+
+/*
+ * generate_lite_key
+ * Generate a key for the product that is being used.
+ *
+ * Input:
+ * type DS_NORMAL_TYPE - Normal
+ * DS_LITE_TYPE - Lite
+ * Returns:
+ * a int key.
+ *
+ */
+int generate_directory_key( int type)
+{
+
+ int key = 0;
+ int val;
+
+ val = rand();
+
+ if (type == DS_NORMAL_TYPE )
+ key = val * DS_NORMAL_MAGIC_KEY;
+ else if (type == DS_LITE_TYPE )
+ key = val * DS_LITE_MAGIC_KEY;
+
+ return key;
+}
+
+/*
+ * is_key_validNormalKey
+ *
+ * Check if the key ia a valid normal key or not.
+ */
+int
+is_key_validNormalKey ( int key )
+{
+
+ if (key <= 0 ) return 0;
+
+ if (0 == ( key % DS_NORMAL_MAGIC_KEY ))
+ return 1;
+
+ return 0;
+}
diff --git a/ldap/libraries/libutil/Makefile b/ldap/libraries/libutil/Makefile
new file mode 100644
index 00000000..6920ec86
--- /dev/null
+++ b/ldap/libraries/libutil/Makefile
@@ -0,0 +1,70 @@
+#
+# BEGIN COPYRIGHT BLOCK
+# Copyright 2001 Sun Microsystems, Inc.
+# Portions copyright 1999, 2001-2003 Netscape Communications Corporation.
+# All rights reserved.
+# END COPYRIGHT BLOCK
+#
+#
+# GNU Makefile for libutil
+#
+
+LDAP_SRC = ../..
+MCOM_ROOT = ../../../..
+
+NOSTDCLEAN=true # don't let nsconfig.mk define target clean
+NOSTDSTRIP=true # don't let nsconfig.mk define target strip
+NSPR20=true # probably should be defined somewhere else (not sure where)
+
+OBJDEST = $(OBJDIR)/lib/libutil
+LIBDIR = $(LDAP_LIBDIR)
+
+include $(MCOM_ROOT)/ldapserver/nsdefs.mk
+include $(MCOM_ROOT)/ldapserver/nsconfig.mk
+include $(LDAP_SRC)/nsldap.mk
+
+#
+# ntdebug.c currently not used
+#
+LIBUTIL_OBJS= getopt.o ntevent.o \
+ ntreg.o ntstubs.o
+
+ifeq ($(ARCH), WINNT)
+LIBUTIL_OBJS += crypt.o
+endif
+
+OBJS = $(addprefix $(OBJDEST)/, $(LIBUTIL_OBJS))
+
+LIBUTIL= $(addprefix $(LIBDIR)/, libutil.$(LIB_SUFFIX))
+
+INCLUDES += -I$(LDAP_SRC)/servers/slapd -I$(OBJDIR)/include
+
+SLAPDMESSAGES_H=$(MCOM_ROOT)/ldapserver/ldap/include/ntslapdmessages.h
+
+ifeq ($(LDAP_NO_LIBLCACHE),1)
+CFLAGS+=-DNO_LIBLCACHE
+endif
+
+clientSDK: all
+
+all: $(OBJDEST) $(SLAPDMESSAGES_H) $(LIBDIR) $(OBJS) $(LIBUTIL)
+
+$(LIBDIR):
+ $(MKDIR) $(LIBDIR)
+
+$(LIBUTIL): $(OBJS)
+ $(LINK_LIB)
+
+$(SLAPDMESSAGES_H):
+ @echo target: $@
+ cd $(MCOM_ROOT)/ldapserver/ldap/servers/slapd/ntmsgdll; $(MAKE) $(MFLAGS)
+
+veryclean: clean
+
+clean:
+ $(RM) $(OBJS)
+ $(RM) $(LIBUTIL)
+
+$(OBJDEST):
+ $(MKDIR) $(OBJDEST)
+
diff --git a/ldap/libraries/libutil/getopt.c b/ldap/libraries/libutil/getopt.c
new file mode 100644
index 00000000..612d78fe
--- /dev/null
+++ b/ldap/libraries/libutil/getopt.c
@@ -0,0 +1,109 @@
+/*
+ * Copyright (c) 1987 Regents of the University of California.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms are permitted
+ * provided that: (1) source distributions retain this entire copyright
+ * notice and comment, and (2) distributions including binaries display
+ * the following acknowledgement: ``This product includes software
+ * developed by the University of California, Berkeley and its contributors''
+ * in the documentation or other materials provided with the distribution
+ * and in all advertising materials mentioning features or use of this
+ * software. Neither the name of the University nor the names of its
+ * contributors may be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
+ */
+
+#ifdef _WINDOWS
+
+#if defined(LIBC_SCCS) && !defined(lint)
+static char sccsid[] = "@(#)getopt.c 4.12 (Berkeley) 6/1/90";
+#endif /* LIBC_SCCS and not lint */
+
+#include <windows.h>
+#include <stdio.h>
+#include <string.h>
+#include "lber.h"
+#define index strchr
+#define rindex strrchr
+
+/*
+ * get option letter from argument vector
+ */
+int opterr = 1, /* if error message should be printed */
+ optind = 1, /* index into parent argv vector */
+ optopt; /* character checked for validity */
+char *optarg; /* argument associated with option */
+
+#define BADCH (int)'?'
+#define EMSG ""
+
+int getopt(int nargc, char *const *nargv, const char *ostr)
+{
+ static char *place = EMSG; /* option letter processing */
+ register char *oli; /* option letter list index */
+ char *p;
+
+ if (!*place) { /* update scanning pointer */
+ if (optind >= nargc || *(place = nargv[optind]) != '-') {
+ place = EMSG;
+ return(EOF);
+ }
+ if (place[1] && *++place == '-') { /* found "--" */
+ ++optind;
+ place = EMSG;
+ return(EOF);
+ }
+ } /* option letter okay? */
+ if ((optopt = (int)*place++) == (int)':' ||
+ !(oli = index(ostr, optopt))) {
+ /*
+ * if the user didn't specify '-' as an option,
+ * assume it means EOF.
+ */
+ if (optopt == (int)'-')
+ return(EOF);
+ if (!*place)
+ ++optind;
+ if (opterr) {
+ if (!(p = rindex(*nargv, '/')))
+ p = *nargv;
+ else
+ ++p;
+ (void)fprintf(stderr, "%s: illegal option -- %c\n",
+ p, optopt);
+ }
+ return(BADCH);
+ }
+ if (*++oli != ':') { /* don't need argument */
+ optarg = NULL;
+ if (!*place)
+ ++optind;
+ }
+ else { /* need an argument */
+ if (*place) /* no white space */
+ optarg = place;
+ else if (nargc <= ++optind) { /* no arg */
+ place = EMSG;
+ if (!(p = rindex(*nargv, '/')))
+ p = *nargv;
+ else
+ ++p;
+ if (opterr)
+ (void)fprintf(stderr,
+ "%s: option requires an argument -- %c\n",
+ p, optopt);
+ return(BADCH);
+ }
+ else /* white space */
+ optarg = nargv[optind];
+ place = EMSG;
+ ++optind;
+ }
+ return(optopt); /* dump back option letter */
+}
+
+#endif
diff --git a/ldap/libraries/libutil/ntdebug.c b/ldap/libraries/libutil/ntdebug.c
new file mode 100644
index 00000000..d0347e2d
--- /dev/null
+++ b/ldap/libraries/libutil/ntdebug.c
@@ -0,0 +1,51 @@
+/** BEGIN COPYRIGHT BLOCK
+ * Copyright 2001 Sun Microsystems, Inc.
+ * Portions copyright 1999, 2001-2003 Netscape Communications Corporation.
+ * All rights reserved.
+ * END COPYRIGHT BLOCK **/
+/******************************************************
+ *
+ * ntdebug.c - Sends debug output to window and stdout
+ * on Win32 platforms.
+ *
+ ******************************************************/
+
+#if defined( _WIN32 )
+#include <windows.h>
+#include <time.h>
+#include <stdio.h>
+#if defined( SLAPD_LOGGING )
+#include "slap.h"
+#include "proto-slap.h"
+#else
+#include "ldap.h"
+#include "ldaplog.h"
+#endif
+int slapd_ldap_debug = LDAP_DEBUG_ANY;
+FILE *error_logfp = NULL;
+
+void LDAPDebug( int level, char *fmt, ... )
+{
+ va_list arg_ptr;
+ va_start( arg_ptr, fmt );
+ if ( slapd_ldap_debug & level )
+ {
+ char szFormattedString[512];
+ _vsnprintf( szFormattedString, sizeof( szFormattedString ), fmt, arg_ptr );
+
+#if defined( LDAP_DEBUG )
+ /* Send to debug window ...*/
+ OutputDebugString( szFormattedString );
+
+ /* ... and to stderr */
+ fprintf( stderr, szFormattedString );
+#endif
+#if defined( SLAPD_LOGGING )
+ if ( error_logfp != NULL )
+ slapd_log_error( error_logfp, szFormattedString );
+#endif
+ }
+ va_end( arg_ptr );
+
+}
+#endif
diff --git a/ldap/libraries/libutil/ntevent.c b/ldap/libraries/libutil/ntevent.c
new file mode 100644
index 00000000..2b05dd9d
--- /dev/null
+++ b/ldap/libraries/libutil/ntevent.c
@@ -0,0 +1,183 @@
+/** BEGIN COPYRIGHT BLOCK
+ * Copyright 2001 Sun Microsystems, Inc.
+ * Portions copyright 1999, 2001-2003 Netscape Communications Corporation.
+ * All rights reserved.
+ * END COPYRIGHT BLOCK **/
+
+#ifdef _WIN32
+
+#include <windows.h>
+#include <stdio.h>
+#include "ldap.h"
+#include "regparms.h"
+
+HANDLE hSlapdEventSource;
+LPTSTR pszServerName;
+
+void ReportSlapdEvent(WORD wEventType, DWORD dwIdEvent, WORD wNumInsertStrings,
+ char *pszStrings)
+{
+ LPCTSTR lpszStrings[64];
+ BOOL bSuccess;
+
+ if( hSlapdEventSource )
+ {
+ if( pszServerName )
+ lpszStrings[0] = (LPCTSTR)pszServerName;
+
+ if( pszStrings != NULL)
+ lpszStrings[1] = (LPCTSTR)pszStrings;
+
+ wNumInsertStrings++;
+
+ /* Now report the event, which will add this event to the event log */
+ bSuccess = ReportEvent(hSlapdEventSource, /* event-log handle */
+ wEventType, /* event type */
+ 0, /* category zero */
+ dwIdEvent, /* event ID */
+ NULL, /* no user SID */
+ wNumInsertStrings, /* number of substr */
+ 0, /* no binary data */
+ lpszStrings, /* string array */
+ NULL); /* address of data */
+ }
+
+} /* ReportSlapdEvent */
+
+BOOL ReportSlapdStatusToSCMgr(
+ SERVICE_STATUS *serviceStatus,
+ SERVICE_STATUS_HANDLE serviceStatusHandle,
+ HANDLE Event,
+ DWORD dwCurrentState,
+ DWORD dwWin32ExitCode,
+ DWORD dwCheckPoint,
+ DWORD dwWaitHint)
+{
+ /* Disable control requests until the service is started. */
+ if (dwCurrentState == SERVICE_START_PENDING)
+ serviceStatus->dwControlsAccepted = 0;
+ else
+ serviceStatus->dwControlsAccepted = SERVICE_ACCEPT_STOP |
+ SERVICE_ACCEPT_PAUSE_CONTINUE;
+
+ serviceStatus->dwCurrentState = dwCurrentState;
+ serviceStatus->dwWin32ExitCode = dwWin32ExitCode;
+ serviceStatus->dwCheckPoint = dwCheckPoint;
+
+ serviceStatus->dwWaitHint = dwWaitHint;
+
+ /* Report the status of the service to the service control manager. */
+ return SetServiceStatus( serviceStatusHandle, serviceStatus);
+
+} /* ReportSlapdStatusToSCMgr */
+
+// This is a routine that we use to check for multiple instances of a server with
+// the same id. We cannot use a shared data section to keep count of instances since
+// there will be multiple instances of the server running. MS recommends using a
+// sync object to do this. Thus we attempt to create an object with same NAME
+// but different TYPE as the server "Done" event.We have a small race condition
+// between the check and the creation of the "Done" event.
+
+BOOL
+MultipleInstances()
+{
+ HANDLE hServDoneSemaphore;
+ DWORD result;
+ CHAR ErrMsg[1024];
+ char szDoneEvent[256];
+
+ if( !pszServerName )
+ return FALSE;
+
+ sprintf(szDoneEvent, "NS_%s", pszServerName);
+
+ hServDoneSemaphore = CreateSemaphore(
+ NULL, // security attributes
+ 0, // initial count for semaphore
+ 1, // maximum count for semaphore
+ szDoneEvent);
+
+ if ( hServDoneSemaphore == NULL) {
+
+ result = GetLastError();
+ if (result == ERROR_INVALID_HANDLE) {
+
+ sprintf(ErrMsg, "Netscape Server %s is already"
+ " running. Terminating this instance.", pszServerName);
+
+ MessageBox(GetDesktopWindow(), ErrMsg,
+ "SERVER ALREADY RUNNING", MB_ICONEXCLAMATION | MB_OK);
+ return TRUE;
+
+ } else {
+ /* We aren't too interested in why the creation failed
+ * if it is not because of another instance */
+
+ return FALSE;
+ }
+ } // hServDoneSemaphore == NULL
+
+ CloseHandle(hServDoneSemaphore);
+ return FALSE;
+}
+
+BOOL SlapdIsAService()
+{
+ // May change in V2.0
+ return FALSE;
+}
+
+BOOL SlapdGetServerNameFromCmdline(char *szServerName, char *szCmdLine, int dirname)
+{
+ BOOL bReturn = FALSE;
+ char *szChar = NULL;
+ char szCmdCopy[_MAX_PATH];
+
+ if( szCmdLine )
+ {
+ memset(szCmdCopy, 0, _MAX_PATH );
+ strcpy( szCmdCopy, szCmdLine );
+ }
+ else
+ return(bReturn);
+
+ // szCmdCopy should be something like
+ // c:\navgold\server\slapd-kennedy\config\slapd.conf
+ // unless dirname is TRUE in which case it should be
+ // c:\navgold\server\slapd-kennedy
+ if(szChar = strrchr(szCmdCopy, '\\'))
+ {
+ *szChar = 0;
+ if(dirname)
+ {
+ strcpy(szServerName, szChar+1);
+ bReturn = TRUE;
+ }
+ else if(szChar = strrchr(szCmdCopy, '\\'))
+ {
+ // szCmdCopy should be c:\navgold\server\slapd-kennedy\config
+ *szChar = 0;
+ // szCmdCopy should be c:\navgold\server\slapd-kennedy
+ if(szChar = strrchr(szCmdCopy, '\\'))
+ {
+ szChar++;
+ // szChar should point to slapd-kennedy
+ strcpy(szServerName, szChar);
+ bReturn = TRUE;
+ }
+ }
+ }
+ else
+ {
+ // szCmdCopy should be something like slapd-kennedy
+ strcpy(szServerName, szCmdCopy);
+ bReturn = TRUE;
+ }
+
+ if(strlen(szServerName) == 0)
+ bReturn = FALSE;
+
+ return(bReturn);
+}
+
+#endif _WIN32
diff --git a/ldap/libraries/libutil/ntreg.c b/ldap/libraries/libutil/ntreg.c
new file mode 100644
index 00000000..53313075
--- /dev/null
+++ b/ldap/libraries/libutil/ntreg.c
@@ -0,0 +1,105 @@
+/** BEGIN COPYRIGHT BLOCK
+ * Copyright 2001 Sun Microsystems, Inc.
+ * Portions copyright 1999, 2001-2003 Netscape Communications Corporation.
+ * All rights reserved.
+ * END COPYRIGHT BLOCK **/
+
+#ifdef _WIN32
+
+#include <windows.h>
+#include <stdio.h>
+#include "ldap.h"
+
+int SlapdGetRegSZ( LPTSTR lpszRegKey, LPSTR lpszValueName, LPTSTR lpszValue )
+{
+ HKEY hKey;
+ DWORD dwType, dwNumBytes;
+ LONG lResult;
+
+ /* Open the registry, get the required key handle. */
+ lResult = RegOpenKeyEx( HKEY_LOCAL_MACHINE, lpszRegKey,
+ 0L, KEY_QUERY_VALUE, &hKey );
+ if (lResult == ERROR_SUCCESS)
+ {
+ dwNumBytes = sizeof( DWORD );
+ lResult = RegQueryValueEx( hKey, lpszValueName, 0,
+ &dwType, NULL, &dwNumBytes );
+ if( lResult == ERROR_SUCCESS )
+ {
+ RegQueryValueEx( hKey, lpszValueName, 0, &dwType,
+ (LPBYTE)lpszValue, &dwNumBytes );
+ *(lpszValue+dwNumBytes) = 0;
+
+ /* Close the Registry. */
+ RegCloseKey(hKey);
+ return 0;
+ }
+ else
+ {
+ /* No config file location stored in the Registry. */
+ RegCloseKey(hKey);
+ return 1;
+ }
+ }
+ else
+ {
+ return 1;
+ }
+} /* SlapdGetRegSZ */
+
+
+int SlapdSetRegSZ( LPTSTR lpszKey, LPSTR lpszValueName, LPTSTR lpszValue )
+{
+ HKEY hKey;
+ LONG lResult;
+
+ /* Open the registry, get a handle to the desired key. */
+ lResult = RegOpenKeyEx( HKEY_LOCAL_MACHINE, lpszKey, 0,
+ KEY_ALL_ACCESS, &hKey );
+ if (lResult == ERROR_SUCCESS)
+ {
+ /* Set the value to the value-name at the key location. */
+ RegSetValueEx( hKey, lpszValueName, 0, REG_SZ,
+ (CONST BYTE*)lpszValue, strlen(lpszValue) );
+
+ /* Close the registry */
+ RegCloseKey(hKey);
+ return 0;
+ }
+ else
+ {
+ return 1;
+ }
+} /* SlapdSetRegSZ */
+
+/* converts '/' chars to '\' */
+void
+unixtodospath(char *szText)
+{
+ if(szText)
+ {
+ while(*szText)
+ {
+ if( *szText == '/' )
+ *szText = '\\';
+ szText++;
+ }
+ }
+}
+
+/* converts '\' chars to '/' */
+void
+dostounixpath(char *szText)
+{
+ if(szText)
+ {
+ while(*szText)
+ {
+ if( *szText == '\\' )
+ *szText = '/';
+ szText++;
+ }
+ }
+}
+
+#endif /* _WIN32 */
diff --git a/ldap/libraries/libutil/ntresource.h b/ldap/libraries/libutil/ntresource.h
new file mode 100644
index 00000000..c2e375c5
--- /dev/null
+++ b/ldap/libraries/libutil/ntresource.h
@@ -0,0 +1,31 @@
+/** BEGIN COPYRIGHT BLOCK
+ * Copyright 2001 Sun Microsystems, Inc.
+ * Portions copyright 1999, 2001-2003 Netscape Communications Corporation.
+ * All rights reserved.
+ * END COPYRIGHT BLOCK **/
+//{{NO_DEPENDENCIES}}
+// Microsoft Developer Studio generated include file.
+// Used by ntslapd.rc
+//
+#define IDD_DATABASE_PASSWORD 101
+#define IDD_FORTEZZA_PIN 102
+#define IDI_KEY 103
+#define IDI_LOGO 105
+#define IDR_MENU 205
+#define IDEDIT 1000
+#define ID_SERVER_RESTART 40001
+#define ID_FILE_EXIT 40003
+#define ID_SERVER_SHUTDOWN 40004
+#define ID_SERVER_SUSPEND 40005
+#define ID_SERVER_RESUME 40006
+
+// Next default values for new objects
+//
+#ifdef APSTUDIO_INVOKED
+#ifndef APSTUDIO_READONLY_SYMBOLS
+#define _APS_NEXT_RESOURCE_VALUE 106
+#define _APS_NEXT_COMMAND_VALUE 40001
+#define _APS_NEXT_CONTROL_VALUE 1001
+#define _APS_NEXT_SYMED_VALUE 101
+#endif
+#endif
diff --git a/ldap/libraries/libutil/ntstubs.c b/ldap/libraries/libutil/ntstubs.c
new file mode 100644
index 00000000..5d18ad38
--- /dev/null
+++ b/ldap/libraries/libutil/ntstubs.c
@@ -0,0 +1,40 @@
+/** BEGIN COPYRIGHT BLOCK
+ * Copyright 2001 Sun Microsystems, Inc.
+ * Portions copyright 1999, 2001-2003 Netscape Communications Corporation.
+ * All rights reserved.
+ * END COPYRIGHT BLOCK **/
+/******************************************************
+ *
+ * ntstubs.c - Stubs needed on NT when linking in
+ * the SSL code. If these stubs were not here, the
+ * named functions below would not be located at link
+ * time, because there is no implementation of the
+ * functions for Win32 in cross-platform libraries.
+ *
+ ******************************************************/
+
+#if defined( _WIN32 ) && defined ( NET_SSL )
+
+#include <windows.h>
+#include <nspr.h>
+
+/*
+char* XP_FileName (const char* name, XP_FileType type)
+{
+ return NULL;
+}
+
+XP_File XP_FileOpen(const char* name, XP_FileType type,
+ const XP_FilePerm permissions)
+{
+ return NULL;
+}
+*/
+
+char *
+WH_FileName (const char *name, PRFileType type)
+{
+ return NULL;
+}
+#endif /* WIN32 && NET_SSL */
+