diff --git a/lib/NTMakefile b/lib/NTMakefile
index 7a85763..ba3bdcd 100755
--- a/lib/NTMakefile
+++ b/lib/NTMakefile
@@ -1,5 +1,9 @@
 !INCLUDE ..\win32\common.mak
 
+!IF "$(STATIC_PLUGIN)" == "yes"
+!INCLUDE ..\plugins\plugin.mak
+!ENDIF
+
 # WS2tcpip.h included in Visual Studio 7 provides getaddrinfo, ...
 # emulation on Windows, so there is no need to build getaddrinfo.c
 
@@ -8,17 +12,27 @@ compat_sources = getaddrinfo.c getnameinfo.c
 compat_objs = getaddrinfo.obj getnameinfo.obj
 !ENDIF
 
-
 libsasl_sources = auxprop.c canonusr.c checkpw.c client.c common.c config.c external.c md5.c saslutil.c server.c seterror.c windlopen.c getsubopt.c plugin_common.c plugin_common.h $(compat_sources)
 libsasl_objs = auxprop.obj canonusr.obj checkpw.obj client.obj common.obj config.obj external.obj md5.obj saslutil.obj server.obj seterror.obj windlopen.obj getsubopt.obj plugin_common.obj $(compat_objs)
 libsasl_res = libsasl.res
 libsasl_out = libsasl.dll libsasl.exp libsasl.lib $(libsasl_res)
 
-CPPFLAGS = /wd4996 /Wp64 /D NEED_GETOPT /I "..\win32\include" /I "." /I "..\include" /D "WIN32" /D "_WINDOWS" /D "_MBCS" /D "_USRDLL" /D "LIBSASL_EXPORTS"
+# add sources for static-linked plugins
+!IF "$(STATIC_PLUGIN)" == "yes"
+libsasl_sources = $(libsasl_sources) $(STATIC_PLUGIN_SRC)
+libsasl_objs = $(libsasl_objs) $(STATIC_PLUGIN_OBJ)
+!ENDIF
+
+CPPFLAGS = /wd4996 /D NEED_GETOPT /I "..\win32\include" /I "." /I "..\include" /D "WIN32" /D "_WINDOWS" /D "_MBCS" /D "_USRDLL" /D "LIBSASL_EXPORTS" /D "_CRT_SECURE_NO_WARNINGS" /D "_SCL_SECURE_NO_WARNINGS"
 
 !IF $(TARGET_WIN_SYSTEM) >= 51
 CPPFLAGS = /D TARGET_WIN_SYSTEM=$(TARGET_WIN_SYSTEM) $(CPPFLAGS)
-!ENDIF 
+!ENDIF
+
+!IF "$(STATIC_PLUGIN)" == "yes"
+CPPFLAGS = $(CPPFLAGS) $(EXTRA_FLAGS) /D "STATIC_PLUGIN" $(STATIC_PLUGIN_CFLAGS)
+LINK32DLL_FLAGS = $(LINK32DLL_FLAGS) $(STATIC_PLUGIN_EXTLIBS)
+!ENDIF
 
 all_objs = $(libsasl_objs)
 all_out = $(libsasl_out)
@@ -70,6 +84,7 @@ CLEAN :
 	-@erase $(all_out)
 	-@erase plugin_common.h
 	-@erase plugin_common.c
+	-@erase $(STATIC_PLUGIN_SRCS)
 	-@erase $(exclude_list)
 
 $(libsasl_res): NTMakefile
diff --git a/lib/windlopen.c b/lib/windlopen.c
index f514e0d..d0c8442 100644
--- a/lib/windlopen.c
+++ b/lib/windlopen.c
@@ -50,6 +50,11 @@
 #include <sasl.h>
 #include "saslint.h"
 
+#ifdef STATIC_PLUGIN
+#include <saslplug.h>
+#include "staticopen.h"
+#endif
+
 #define DLL_SUFFIX	".dll"
 #define DLL_MASK	"*" DLL_SUFFIX
 #define DLL_MASK_LEN	5
@@ -168,19 +173,27 @@ int _sasl_load_plugins(const add_plugin_list_t *entrypoints,
 		       const sasl_callback_t *verifyfile_cb)
 {
     int result;
+    const add_plugin_list_t *cur_ep;
+
+#ifdef STATIC_PLUGIN /* STATIC_PLUGIN 2 */
+    add_plugin_t *add_plugin;
+    _sasl_plug_type type;
+    _sasl_plug_rec *p;
+#else /* STATIC_PLUGIN 2 */
     char cur_dir[PATH_MAX], full_name[PATH_MAX+2], prefix[PATH_MAX+2];
 				/* 1 for '\\' 1 for trailing '\0' */
+
     char * pattern;
     char c;
     int pos;
     const char *path=NULL;
     int position;
-    const add_plugin_list_t *cur_ep;
     struct stat statbuf;		/* filesystem entry information */
     intptr_t fhandle;			/* file handle for _findnext function */
     struct _finddata_t finddata;	/* data returned by _findnext() */
     size_t prefix_len;
-
+#endif /* STATIC_PLUGIN 2 */
+    
     if (! entrypoints
 	|| ! getpath_cb
 	|| getpath_cb->id != SASL_CB_GETPATH
@@ -190,6 +203,33 @@ int _sasl_load_plugins(const add_plugin_list_t *entrypoints,
 	|| ! verifyfile_cb->proc)
 	return SASL_BADPARAM;
 
+#ifdef STATIC_PLUGIN /* STATIC_PLUGIN 3 */
+    /* do all the static plugins first */
+    for(cur_ep = entrypoints; cur_ep->entryname; cur_ep++) {
+
+	/* What type of plugin are we looking for? */
+	if(!strcmp(cur_ep->entryname, "sasl_server_plug_init")) {
+	    type = SERVER;
+	    add_plugin = (add_plugin_t *)sasl_server_add_plugin;
+	} else if (!strcmp(cur_ep->entryname, "sasl_client_plug_init")) {
+	    type = CLIENT;
+	    add_plugin = (add_plugin_t *)sasl_client_add_plugin;
+	} else if (!strcmp(cur_ep->entryname, "sasl_auxprop_plug_init")) {
+	    type = AUXPROP;
+	    add_plugin = (add_plugin_t *)sasl_auxprop_add_plugin;
+	} else if (!strcmp(cur_ep->entryname, "sasl_canonuser_init")) {
+	    type = CANONUSER;
+	    add_plugin = (add_plugin_t *)sasl_canonuser_add_plugin;
+	} else {
+	    /* What are we looking for then? */
+	    return SASL_FAIL;
+	}
+	for (p=_sasl_static_plugins; p->type; p++) {
+	    if(type == p->type)
+	    	result = add_plugin(p->name, p->plug);
+	}
+    }
+#else /* STATIC_PLUGIN 3 */
     /* get the path to the plugins */
     result = ((sasl_getpath_t *)(getpath_cb->proc))(getpath_cb->context,
 						    &path);
@@ -307,6 +347,7 @@ int _sasl_load_plugins(const add_plugin_list_t *entrypoints,
 	_findclose (fhandle);
 
     } while ((c!='=') && (c!=0));
+#endif /* STATIC_PLUGIN 3 */
 
     return SASL_OK;
 }
diff --git a/plugins/NTMakefile b/plugins/NTMakefile
index a0a57a6..fcc0229 100755
--- a/plugins/NTMakefile
+++ b/plugins/NTMakefile
@@ -1,52 +1,6 @@
 !INCLUDE ..\win32\common.mak
 
-!IF "$(NTLM)" == "1"
-PLUGINS_EXT=saslNTLM.dll
-!ELSE 
-PLUGINS_EXT=
-!ENDIF 
-
-!IF "$(GSSAPI)" == "CyberSafe"
-PLUGINS_EXT=$(PLUGINS_EXT) saslGSSAPI.dll
-!ENDIF 
-
-!IF "$(SRP)" == "1"
-PLUGINS_EXT=$(PLUGINS_EXT) saslSRP.dll
-!IF "$(DO_SRP_SETPASS)" == "1"
-SRP_FLAGS=/DDO_SRP_SETPASS=1
-!ENDIF 
-!ENDIF 
-
-!IF "$(OTP)" == "1"
-PLUGINS_EXT=$(PLUGINS_EXT) saslOTP.dll
-!ENDIF
-
-!IF "$(LDAP)" == "1"
-PLUGINS_EXT=$(PLUGINS_EXT) saslLDAPDB.dll
-
-# NB: linking to libsasl itself!!!
-LDAP_FLAGS = /I $(LDAP_INCLUDE)
-LDAP_LIBS = $(LDAP_LIB_BASE)\olber32.lib $(LDAP_LIB_BASE)\oldap32.lib ..\lib\libsasl.lib
-!ENDIF 
-
-!IF "$(SQL)" == "SQLITE"
-PLUGINS_EXT=$(PLUGINS_EXT) saslSQLITE.dll
-SQL_FLAGS= $(SQLITE_INCLUDES) /DHAVE_SQLITE=1
-SQLITE_LIBS = /libpath:$(SQLITE_LIBPATH) libsqlite.lib
-!ENDIF
-!IF "$(SQL)" == "SQLITE3"
-PLUGINS_EXT=$(PLUGINS_EXT) saslSQLITE.dll
-SQL_FLAGS= $(SQLITE_INCLUDES3) /DHAVE_SQLITE3=1
-SQLITE_LIBS = /libpath:$(SQLITE_LIBPATH3) libsqlite3.lib
-!ENDIF
-
-PLUGINS=saslANONYMOUS.dll \
-	saslPLAIN.dll \
-	saslCRAMMD5.dll \
-	saslDIGESTMD5.dll \
-	saslLOGIN.dll \
-	$(PLUGINS_EXT) \
-	saslSASLDB.dll
+!INCLUDE .\plugin.mak
 
 generated_rc=saslANONYMOUS.rc saslPLAIN.rc saslCRAMMD5.rc saslDIGESTMD5.rc saslLOGIN.rc saslNTLM.rc saslGSSAPI.rc saslSRP.rc saslOTP.rc saslSASLDB.rc saslSQLITE.rc saslLDAPDB.rc
 
@@ -93,7 +47,7 @@ saslSRP_sources = srp.c srp_init.c $(common_sources)
 saslSRP_objs = srp.obj srp_init.obj $(common_objs)
 saslSRP_out = saslSRP.dll saslSRP.exp saslSRP.lib
 
-saslOTP_sources = otp.c otp_init.c $(common_sources)
+saslOTP_sources = otp.c otp_init.c otp.h $(common_sources)
 saslOTP_objs = otp.obj otp_init.obj $(common_objs)
 saslOTP_out = saslOTP.dll saslOTP.exp saslOTP.lib
 
@@ -106,22 +60,6 @@ saslLDAPDB_sources = ldapdb.c $(common_sources)
 saslLDAPDB_objs = ldapdb.obj $(common_objs)
 saslLDAPDB_out = saslLDAPDB.dll saslLDAPDB.exp saslLDAPDB.lib
 
-!IF "$(NTLM)" == "1" || "$(SRP)" == "1" || "$(OTP)" == "1"
-OPENSSL_FLAGS= /I $(OPENSSL_INCLUDE)
-!ELSE 
-OPENSSL_FLAGS=
-!ENDIF 
-
-!IF "$(GSSAPI)" == "CyberSafe"
-GSS_FLAGS= /I $(GSSAPI_INCLUDE) /D "HAVE_GSS_C_NT_HOSTBASED_SERVICE" /D "HAVE_GSS_C_NT_USER_NAME"
-GSS_LIBS=/libpath:$(GSSAPI_LIBPATH) gssapi32.lib
-!ELSE 
-GSS_FLAGS=
-GSS_LIBS=
-!ENDIF 
-
-DIGEST_FLAGS=/D "WITH_RC4"
-
 # Auxprop Plugin
 libsasldb_sources = allockey.c db_berkeley.c
 libsasldb_objs = allockey.obj db_berkeley.obj
@@ -133,24 +71,16 @@ saslSASLDB_out = saslSASLDB.dll saslSASLDB.exp saslSASLDB.lib
 all_objs = $(saslANONYMOUS_objs) $(saslPLAIN_objs) $(saslCRAMMD5_objs) $(saslDIGESTMD5_objs) $(saslLOGIN_objs) $(saslNTLM_objs) $(saslGSSAPI_objs) $(saslSRP_objs) $(saslOTP_objs) $(saslSASLDB_objs) $(saslSQL_objs) $(saslLDAPDB_objs)
 all_out = $(saslANONYMOUS_out) $(saslPLAIN_out) $(saslCRAMMD5_out) $(saslDIGESTMD5_out) $(saslLOGIN_out) $(saslNTLM_out) $(saslGSSAPI_out) $(saslSRP_out) $(saslOTP_out) $(saslSASLDB_out) $(saslSQL_out) $(saslLDAPDB_out)
 
-# LIBSASL_EXPORTS is required to export additional DB routines from sasldb
-DB_FLAGS = /I $(DB_INCLUDE) /I "..\sasldb" /D "LIBSASL_EXPORTS" /D "KEEP_DB_OPEN"
-
-!IF $(TARGET_WIN_SYSTEM) >= 51
-EXTRA_FLAGS = /D TARGET_WIN_SYSTEM=$(TARGET_WIN_SYSTEM) $(EXTRA_FLAGS)
-!ENDIF
-
-EXTRA_FLAGS=$(EXTRA_FLAGS) $(DB_FLAGS) $(OPENSSL_FLAGS) $(GSS_FLAGS) $(SRP_FLAGS) $(SQL_FLAGS) $(DIGEST_FLAGS) $(LDAP_FLAGS)
-CPPFLAGS = /I "..\win32\include" /I "." /I "..\include" $(EXTRA_FLAGS) /D "WIN32" /D "_WIN32" /D "_WINDOWS" /D "_MBCS" /D "_USRDLL"
-
-DB_LIBS=/libpath:$(DB_LIBPATH) $(DB_LIB)
-OPENSSL_LIBS=/libpath:$(OPENSSL_LIBPATH) libeay32.lib ssleay32.lib
 
 # Where to install files from this directory
 libdir = $(prefix)\lib
 bindir = $(prefix)\bin\sasl2
 
+!IF "$(STATIC_PLUGIN)" == "yes"
+all :
+!ELSE
 all : all-recursive
+!ENDIF
 
 #
 # /I flag to xcopy tells to treat the last parameter as directory and create all missing levels
@@ -167,17 +97,6 @@ install: $(PLUGINS)
 
 all-recursive : $(PLUGINS)
 
-getaddrinfo.c: ..\lib\getaddrinfo.c
-	copy ..\lib\getaddrinfo.c .
-
-getnameinfo.c: ..\lib\getnameinfo.c
-	copy ..\lib\getnameinfo.c .
-
-allockey.c: ..\sasldb\allockey.c
-	copy ..\sasldb\allockey.c .
-
-db_berkeley.c: ..\sasldb\db_berkeley.c
-	copy ..\sasldb\db_berkeley.c .
 
 #Add /pdb: option?
 
diff --git a/plugins/plugin.mak b/plugins/plugin.mak
new file mode 100755
index 0000000..5404cfb
--- /dev/null
+++ b/plugins/plugin.mak
@@ -0,0 +1,213 @@
+#!INCLUDE ..\win32\common.mak
+
+!IF "$(NTLM)" == "1"
+PLUGINS_EXT=saslNTLM.dll
+STATIC_PLUGIN_SRC=ntlm.c
+STATIC_PLUGIN_OBJ=ntlm.obj
+STATIC_PLUGIN_CFLAGS=/D "STATIC_NTLM"
+!ELSE 
+PLUGINS_EXT=
+STATIC_PLUGIN_SRC=
+STATIC_PLUGIN_OBJ=
+STATIC_PLUGIN_CFLAGS=
+!ENDIF 
+
+!IF "$(GSSAPI)" == "CyberSafe"
+PLUGINS_EXT=$(PLUGINS_EXT) saslGSSAPI.dll
+STATIC_PLUGIN_SRC=$(STATIC_PLUGIN_SRC) gssapi.c
+STATIC_PLUGIN_OBJ=$(STATIC_PLUGIN_OBJ) gssapi.obj
+STATIC_PLUGIN_CFLAGS=$(STATIC_PLUGIN_CFLAGS) /D "STATIC_GSSAPIV2"
+!ENDIF 
+
+!IF "$(SRP)" == "1"
+PLUGINS_EXT=$(PLUGINS_EXT) saslSRP.dll
+STATIC_PLUGIN_SRC=$(STATIC_PLUGIN_SRC) srp.c
+STATIC_PLUGIN_OBJ=$(STATIC_PLUGIN_OBJ) srp.obj
+STATIC_PLUGIN_CFLAGS=$(STATIC_PLUGIN_CFLAGS) /D "STATIC_SRP"
+!IF "$(DO_SRP_SETPASS)" == "1"
+SRP_FLAGS=/DDO_SRP_SETPASS=1
+!ENDIF 
+!ENDIF 
+
+!IF "$(OTP)" == "1"
+PLUGINS_EXT=$(PLUGINS_EXT) saslOTP.dll
+STATIC_PLUGIN_SRC=$(STATIC_PLUGIN_SRC) otp.c otp.h
+STATIC_PLUGIN_OBJ=$(STATIC_PLUGIN_OBJ) otp.obj
+STATIC_PLUGIN_CFLAGS=$(STATIC_PLUGIN_CFLAGS) /D "STATIC_OTP"
+!ENDIF
+
+!IF "$(LDAP)" == "1"
+PLUGINS_EXT=$(PLUGINS_EXT) saslLDAPDB.dll
+STATIC_PLUGIN_SRC=$(STATIC_PLUGIN_SRC) ldapdb.c
+STATIC_PLUGIN_OBJ=$(STATIC_PLUGIN_OBJ) ldapdb.obj
+STATIC_PLUGIN_CFLAGS=$(STATIC_PLUGIN_CFLAGS) /D "STATIC_LDAPDB"
+!ENDIF 
+
+!IF "$(SQL)" == "SQLITE" || "$(SQL)" == "SQLITE3"
+PLUGINS_EXT=$(PLUGINS_EXT) saslSQLITE.dll
+STATIC_PLUGIN_SRC=$(STATIC_PLUGIN_SRC) sql.c
+STATIC_PLUGIN_OBJ=$(STATIC_PLUGIN_OBJ) sql.obj
+STATIC_PLUGIN_CFLAGS=$(STATIC_PLUGIN_CFLAGS) /D "STATIC_SQL"
+!ENDIF
+
+!IF "$(SASLDB)" == "1"
+PLUGINS_EXT=$(PLUGINS_EXT) saslSASLDB.dll
+STATIC_PLUGIN_SRC=$(STATIC_PLUGIN_SRC) sasldb.c sasldb.h
+STATIC_PLUGIN_OBJ=$(STATIC_PLUGIN_OBJ) sasldb.obj
+STATIC_PLUGIN_CFLAGS=$(STATIC_PLUGIN_CFLAGS) /D "STATIC_SASLDB"
+!IF "$(DBLIB)" == "berkeley"
+SASLDB_SRC=allockey.c db_berkeley.c
+SASLDB_OBJ=allockey.obj db_berkeley.obj
+!ELSE
+DBLIB="no"
+SASLDB_SRC=db_none.c
+SASLDB_OBJ=db_none.obj
+!ENDIF # DBLIB
+STATIC_PLUGIN_SRC=$(STATIC_PLUGIN_SRC) $(SASLDB_SRC)
+STATIC_PLUGIN_OBJ=$(STATIC_PLUGIN_OBJ) $(SASLDB_OBJ)
+!ENDIF # SASLDB
+
+
+STATIC_PLUGIN_SRC=anonymous.c \
+                  plain.c \
+                  cram.c \
+                  digestmd5.c \
+                  login.c \
+                  $(STATIC_PLUGIN_SRC)
+
+STATIC_PLUGIN_OBJ=anonymous.obj \
+                  plain.obj \
+                  cram.obj \
+                  digestmd5.obj \
+                  login.obj \
+                  $(STATIC_PLUGIN_OBJ)
+
+STATIC_PLUGIN_CFLAGS=/D "STATIC_ANONYMOUS" \
+                     /D "STATIC_PLAIN" \
+                     /D "STATIC_CRAMMD5" \
+                     /D "STATIC_DIGESTMD5" \
+                     /D "STATIC_LOGIN" \
+                     $(STATIC_PLUGIN_CFLAGS)
+
+PLUGINS=saslANONYMOUS.dll \
+	saslPLAIN.dll \
+	saslCRAMMD5.dll \
+	saslDIGESTMD5.dll \
+	saslLOGIN.dll \
+	$(PLUGINS_EXT)
+
+
+anonymous.c: ..\plugins\anonymous.c
+	copy ..\plugins\anonymous.c .
+
+plain.c: ..\plugins\plain.c
+	copy ..\plugins\plain.c .
+
+cram.c: ..\plugins\cram.c
+	copy ..\plugins\cram.c .
+
+digestmd5.c: ..\plugins\digestmd5.c
+	copy ..\plugins\digestmd5.c .
+
+login.c: ..\plugins\login.c
+	copy ..\plugins\login.c .
+
+ntlm.c: ..\plugins\ntlm.c
+	copy ..\plugins\ntlm.c .
+
+gssapi.c: ..\plugins\gssapi.c
+	copy ..\plugins\gssapi.c .
+
+srp.c: ..\plugins\srp.c
+	copy ..\plugins\srp.c .
+
+otp.c: ..\plugins\otp.c otp.h
+	copy ..\plugins\otp.c .
+
+otp.h: ..\plugins\otp.h
+	copy ..\plugins\otp.h .
+
+ldapdb.c: ..\plugins\ldapdb.c
+	copy ..\plugins\ldapdb.c .
+
+sql.c: ..\plugins\sql.c
+	copy ..\plugins\sql.c .
+
+sasldb.c: ..\plugins\sasldb.c ..\sasldb\sasldb.h
+	copy ..\plugins\sasldb.c .
+
+#sasldb.h: ..\sasldb\sasldb.h
+#	copy ..\sasldb\sasldb.h .
+
+getaddrinfo.c: ..\lib\getaddrinfo.c
+	copy ..\lib\getaddrinfo.c .
+
+getnameinfo.c: ..\lib\getnameinfo.c
+	copy ..\lib\getnameinfo.c .
+
+allockey.c: ..\sasldb\allockey.c
+	copy ..\sasldb\allockey.c .
+
+db_berkeley.c: ..\sasldb\db_berkeley.c
+	copy ..\sasldb\db_berkeley.c .
+
+db_none.c: ..\sasldb\db_none.c
+	copy ..\sasldb\db_none.c .
+
+
+
+#
+# COMPILER FLAGS
+#
+
+!IF "$(NTLM)" == "1" || "$(SRP)" == "1" || "$(OTP)" == "1"
+OPENSSL_FLAGS= /I $(OPENSSL_INCLUDE)
+OPENSSL_LIBS=/libpath:$(OPENSSL_LIBPATH) libeay32.lib ssleay32.lib
+!ELSE 
+OPENSSL_FLAGS=
+OPENSSL_LIBS=
+!ENDIF 
+
+!IF "$(GSSAPI)" == "CyberSafe"
+GSS_FLAGS= /I $(GSSAPI_INCLUDE) /D "HAVE_GSS_C_NT_HOSTBASED_SERVICE" /D "HAVE_GSS_C_NT_USER_NAME"
+GSS_LIBS=/libpath:$(GSSAPI_LIBPATH) gssapi32.lib
+!ELSE 
+GSS_FLAGS=
+GSS_LIBS=
+!ENDIF 
+
+!IF "$(LDAP)" == "1"
+LDAP_FLAGS = /I $(LDAP_INCLUDE)
+LDAP_LIBS = $(LDAP_LIB_BASE)\olber32.lib $(LDAP_LIB_BASE)\oldap32.lib ..\lib\libsasl.lib
+!ENDIF
+
+!IF "$(SQL)" == "SQLITE"
+SQL_FLAGS= $(SQLITE_INCLUDES) /DHAVE_SQLITE=1
+SQLITE_LIBS = /libpath:$(SQLITE_LIBPATH) libsqlite.lib
+!ELSEIF "$(SQL)" == "SQLITE3"
+SQL_FLAGS= $(SQLITE_INCLUDES3) /DHAVE_SQLITE3=1
+SQLITE_LIBS = /libpath:$(SQLITE_LIBPATH3) libsqlite3.lib
+!ENDIF
+
+DIGEST_FLAGS=/D "WITH_RC4"
+
+# LIBSASL_EXPORTS is required to export additional DB routines from sasldb
+!IF "$(SASLDB)" == "1"
+DB_FLAGS = /I $(DB_INCLUDE) /I "..\sasldb" /D "LIBSASL_EXPORTS" /D "KEEP_DB_OPEN" /D "SASL_DB_PATH=\"$(SASL_DB_PATH)\""
+!IF "$(DBLIB)" == "berkeley"
+DB_FLAGS = $(DB_FLAGS) /D "SASL_BERKELEYDB"
+!ENDIF #DBLIB
+DB_LIBS=/libpath:$(DB_LIBPATH) $(DB_LIB)
+!ELSE
+DB_FLAGS=
+DB_LIBS=
+!ENDIF #SASLDB
+
+!IF $(TARGET_WIN_SYSTEM) >= 51
+EXTRA_FLAGS = /D TARGET_WIN_SYSTEM=$(TARGET_WIN_SYSTEM) $(EXTRA_FLAGS)
+!ENDIF
+
+EXTRA_FLAGS=$(EXTRA_FLAGS) $(DB_FLAGS) $(OPENSSL_FLAGS) $(GSS_FLAGS) $(SRP_FLAGS) $(SQL_FLAGS) $(DIGEST_FLAGS) $(LDAP_FLAGS)
+CPPFLAGS = /I "..\win32\include" /I "." /I "..\include" $(EXTRA_FLAGS) /D "WIN32" /D "_WIN32" /D "_WINDOWS" /D "_MBCS" /D "_USRDLL" /D "_CRT_SECURE_NO_WARNINGS" /D "_SCL_SECURE_NO_WARNINGS"
+
+STATIC_PLUGIN_EXTLIBS=$(OPENSSL_LIBS) $(GSS_LIBS) $(DB_LIBS) $(SQLITE_LIBS) $(LDAP_LIBS)
diff --git a/sample/NTMakefile b/sample/NTMakefile
index 62c38bc..434d2a9 100644
--- a/sample/NTMakefile
+++ b/sample/NTMakefile
@@ -25,7 +25,7 @@ all_objs = $(common_objs) $(server_objs) $(client_objs) $(sample_client_objs) $(
 all_out = $(sample_apps) $(sample_out)
 
 DB_FLAGS = /I $(DB_INCLUDE)
-CPPFLAGS = /I "..\win32\include" /I "." /I "..\include" $(DB_FLAGS) /D "WIN32" /D "_WINDOWS" /D "_MBCS" /D "_USRDLL"
+CPPFLAGS = /I "..\win32\include" /I "." /I "..\include" $(DB_FLAGS) /D "WIN32" /D "_WINDOWS" /D "_MBCS" /D "_USRDLL" /D "_CRT_SECURE_NO_WARNINGS" /D "_SCL_SECURE_NO_WARNINGS"
 CPPFLAGS = $(CPPFLAGS) /DNEED_GETOPT
 
 !IF $(TARGET_WIN_SYSTEM) >= 51
diff --git a/utils/NTMakefile b/utils/NTMakefile
index 1d0a0fa..cfed384 100644
--- a/utils/NTMakefile
+++ b/utils/NTMakefile
@@ -1,7 +1,11 @@
 !INCLUDE ..\win32\common.mak
 
-sasl_apps=saslpasswd2.exe sasldblistusers2.exe testsuite.exe pluginviewer.exe
-sasl_out=saslpasswd2.pdb sasldblistusers2.pdb testsuite.pdb pluginviewer.pdb
+sasl_apps=pluginviewer.exe
+sasl_out=pluginviewer.pdb
+!IF "$(SASLDB)" == "1"
+sasl_apps=$(sasl_apps) saslpasswd2.exe sasldblistusers2.exe testsuite.exe
+sasl_out=$(sasl_out) saslpasswd2.pdb sasldblistusers2.pdb testsuite.pdb
+!ENDIF
 
 saslpwd_objs = saslpasswd.obj
 sasldblistusers_objs = sasldblistusers.obj
@@ -11,14 +15,18 @@ pluginviewer_objs = pluginviewer.obj
 all_objs = $(saslpwd_objs) $(sasldblistusers_objs) $(testsuite_objs) $(pluginviewer_objs)
 all_out = $(sasl_apps) $(sasl_out)
 
-DB_FLAGS = /I $(DB_INCLUDE) /I "..\sasldb"
-CPPFLAGS = /I "..\win32\include" /I "." /I "..\include" $(DB_FLAGS) /D "WIN32" /D "_WINDOWS" /D "_MBCS" /D "_USRDLL"
+DB_FLAGS = /I $(DB_INCLUDE) /I "..\sasldb" /D "SASL_DB_PATH=\"$(SASL_DB_PATH)\""
+CPPFLAGS = /I "..\win32\include" /I "." /I "..\include" $(DB_FLAGS) /D "WIN32" /D "_WINDOWS" /D "_MBCS" /D "_USRDLL" /D "_CRT_SECURE_NO_WARNINGS" /D "_SCL_SECURE_NO_WARNINGS"
 CPPFLAGS = $(CPPFLAGS) /DNEED_GETOPT
 
 
 DB_LIBS=/libpath:$(DB_LIBPATH) $(DB_LIB)
 SASL_LIB=/libpath:"..\lib" libsasl.lib
+!IF "$(STATIC_PLUGIN)" == "yes"
+SASL_DB_LIB=
+!ELSE
 SASL_DB_LIB=/libpath:"..\plugins" saslSASLDB.lib
+!ENDIF
 
 # EXTRA_LIBS is automatically included into LINK32EXE_FLAGS/LINK32DLL_FLAGS
 EXTRA_LIBS=$(SASL_LIB) 
diff --git a/win32/common.mak b/win32/common.mak
index 41d4103..6ed6e4d 100644
--- a/win32/common.mak
+++ b/win32/common.mak
@@ -5,17 +5,70 @@ SASL_VERSION_MINOR=1
 SASL_VERSION_STEP=24
 
 !IF "$(STATIC)" == ""
-STATIC=yes
+STATIC=no
+!ENDIF
+
+!IF "$(STATIC_PLUGIN)" == ""
+STATIC_PLUGIN=no
+!ENDIF
+
+# Detect Visual Studio Version
+# Reference: http://mateusz.loskot.net/2009/03/29/detecting-visual-c-version-in-nmake-makefile/
+!IF "$(_NMAKE_VER)" == ""
+MSVC_VER = 4.0
+VCVER = 4
+!ERROR *** Prehistoric version of Visual C++
+!ELSEIF "$(_NMAKE_VER)" == "162"
+MSVC_VER = 5.0
+VCVER = 5
+!ERROR *** Prehistoric version of Visual C++
+!ELSEIF "$(_NMAKE_VER)" == "6.00.8168.0"
+MSVC_VER = 6.0
+VCVER = 6
+MSC_VER = 1200
+!ERROR *** Prehistoric version of Visual C++
+!ELSEIF "$(_NMAKE_VER)" == "7.00.9466"
+MSVC_VER = 7.0
+VCVER = 7
+MSC_VER = 1300
+!ELSEIF "$(_NMAKE_VER)" == "7.10.3077"
+MSVC_VER = 7.1
+VCVER = 7
+MSC_VER = 1310
+!ELSEIF "$(_NMAKE_VER)" == "8.00.50727.42"
+MSVC_VER = 8.0
+VCVER = 8
+MSC_VER = 1400
+!ELSEIF "$(_NMAKE_VER)" == "8.00.50727.762"
+MSVC_VER = 8.0
+VCVER = 8
+MSC_VER = 1400
+!ELSEIF "$(_NMAKE_VER)" == "9.00.21022.08"
+MSVC_VER = 9.0
+VCVER = 9
+MSC_VER = 1500
+!ELSEIF "$(_NMAKE_VER)" == "9.00.30729.01"
+MSVC_VER = 9.0
+VCVER = 9
+MSC_VER = 1500
+!ELSE
+MSVC_VER = 0.0
+VCVER = 0
+MSC_VER = 0
 !ENDIF
+!MESSAGE *** Using Microsoft NMAKE version $(_NMAKE_VER)
+!MESSAGE *** Using Microsoft Visual C++ version $(MSVC_VER)
 
-# Uncomment the following line, if you want to use Visual Studio 6
-#VCVER=6
 
+!IF "$(VCVER)" == "6" || "$(VCVER)" == "7"
 # Use in Visual Studio 6 & 7:
-#EXCEPTHANDLING=/GX
-
-# Use in Visual Studio 8:
+EXCEPTHANDLING=/GX
+RUNTIMEERRORCHECK=/GZ
+!ELSE
+# Use in Visual Studio 8 & 9:
 EXCEPTHANDLING=/EHsc
+RUNTIMEERRORCHECK=/RTC1
+!ENDIF
 
 # Define compiler/linker/etc.
 
@@ -61,21 +114,21 @@ CFG=Release
 !IF "$(DB_LIB)" == ""
 DB_LIB=libdb41s.lib
 !IF "$(VERBOSE)" != "0"
-!MESSAGE Defaulting SleepyCat library name to $(DB_LIB).
+!MESSAGE Defaulting BerkeleyDB to $(DB_LIB).
 !ENDIF
 !ENDIF
 
 !IF "$(DB_INCLUDE)" == ""
 DB_INCLUDE=c:\work\isode\db\build_win32
 !IF "$(VERBOSE)" != "0"
-!MESSAGE Defaulting SleepyCat include path to $(DB_INCLUDE).
+!MESSAGE Defaulting BerkeleyDB include path to $(DB_INCLUDE).
 !ENDIF
 !ENDIF
 
 !IF "$(DB_LIBPATH)" == ""
 DB_LIBPATH=c:\work\isode\db\build_win32\Release_static
 !IF "$(VERBOSE)" != "0"
-!MESSAGE Defaulting SleepyCat library path to $(DB_LIBPATH).
+!MESSAGE Defaulting BerkeleyDB library path to $(DB_LIBPATH).
 !ENDIF
 !ENDIF
 
@@ -149,6 +202,10 @@ LDAP_INCLUDE = c:\work\open_source\openldap\openldap-head\ldap\include
 !ENDIF
 !ENDIF
 
+!IF "$(SASL_DB_PATH)" == ""
+SASL_DB_PATH="C:\\CMU\\SASLDB"
+!ENDIF
+
 !IF "$(OS)" == "Windows_NT"
 NULL=
 !ELSE 
@@ -169,11 +226,11 @@ CODEGEN=/MD
 !ENDIF 
 !ENDIF 
 
-!IF "$(VCVER)" != "6"
+!IF "$(VCVER)" == "7" || "$(VCVER)" == "8"
 ENABLE_WIN64_WARNINGS=/Wp64
 !ENDIF
 
-CPP_PROJ= $(CODEGEN) /W3 $(EXCEPTHANDLING) /O2 $(ENABLE_WIN64_WARNINGS) /Zi /D "NDEBUG" $(CPPFLAGS) /FD /c 
+CPP_PROJ= $(CODEGEN) /W3 $(EXCEPTHANDLING) /O2 $(ENABLE_WIN64_WARNINGS) /Zi /D "NDEBUG" $(CPPFLAGS) /c 
 
 incremental=no
 
@@ -194,13 +251,14 @@ CODEGEN=/MDd
 !ENDIF 
 !ENDIF 
 
-CPP_PROJ=$(CODEGEN) /W3 /Gm $(EXCEPTHANDLING) /ZI /Od /D "_DEBUG" $(CPPFLAGS) /FD /GZ /c 
+CPP_PROJ=$(CODEGEN) /W3 /Gm $(EXCEPTHANDLING) /Zi /Od /D "_DEBUG" $(CPPFLAGS) $(RUNTIMEERRORCHECK) /c 
 
 incremental=yes
 
 # This use to contain /machine:I386. This breaks cross compiling to Windows 64.
 # It doesn't seem that the /machine option is needed anyway.
-LINK32_FLAGS=/debug /pdbtype:sept
+LINK32_FLAGS=/debug
+
 
 !ENDIF
 
diff --git a/win32/include/config.h b/win32/include/config.h
index 3d74e2a..32b9a46 100644
--- a/win32/include/config.h
+++ b/win32/include/config.h
@@ -84,25 +84,30 @@ typedef int		    intptr_t;
 
 /* : This should probably be replaced with a call to a function
    : that gets the proper value from Registry */
-#define SASL_DB_PATH "c:\\CMU\\sasldb2"
+/* now this is set automatically as compiler flag in plugin.mak */
+/* #define SASL_DB_PATH "c:\\CMU\\sasldb2" */
 
 /* what db package are we using? */
+/* now this is set automatically as compiler flag in plugin.mak. */
 /* #undef SASL_GDBM */
 /* #undef SASL_NDBM */
-#define SASL_BERKELEYDB 1
+/* #undef SASL_BERKELEYDB */
+/* #undef SASL_BERKELEYDB */
 
 /* which mechs can we link staticly? */
-#define STATIC_ANONYMOUS 1
-#define STATIC_CRAMMD5 1
-#define STATIC_DIGESTMD5 1
-#define STATIC_GSSAPIV2 1
+/* now this is set automatically as compiler flag in plugin.mak. */
+/* #undef STATIC_ANONYMOUS */
+/* #undef STATIC_CRAMMD5 */
+/* #undef STATIC_DIGESTMD5 */
+/* #undef STATIC_GSSAPIV2 */
 /* #undef STATIC_KERBEROS4 */
-#define STATIC_LOGIN 1
+/* #undef STATIC_LOGIN */
 /* #undef STATIC_MYSQL */
-#define STATIC_OTP 1
-#define STATIC_PLAIN 1
-#define STATIC_SASLDB 1
-#define STATIC_SRP 1
+/* #undef STATIC_NTLM */
+/* #undef STATIC_OTP */
+/* #undef STATIC_PLAIN */
+/* #undef STATIC_SASLDB */
+/* #undef STATIC_SRP */
 
 /* ------------------------------------------------------------ */
 
@@ -115,9 +120,11 @@ typedef int		    intptr_t;
 
 /* Windows calls these functions something else
  */
-#define strcasecmp   stricmp
-#define snprintf    _snprintf
-#define strncasecmp  strnicmp
+#define strcasecmp   _stricmp
+#define snprintf     _snprintf
+#define strncasecmp  _strnicmp
+#define strdup       _strdup
+#define fdopen       _fdopen
 
 #define MAXHOSTNAMELEN 1024
 