From 78f15a7f657e3c3163d7f697e79cf6dff06da9d4 Mon Sep 17 00:00:00 2001 From: fyodor Date: Sat, 11 Aug 2007 06:05:48 +0000 Subject: [PATCH] make pcre an NSE C module --- Makefile.in | 6 +- mswin32/nmap.vcproj | 8 --- nse_init.cc | 2 - nse_pcrelib.h | 9 --- nselib/Makefile.in | 10 ++- nselib/configure | 119 +++++++++++++++++++++++++------- nselib/configure.ac | 48 +++++++++++++ nse_pcrelib.cc => nselib/pcre.c | 24 +++++-- nselib/pcre.h | 9 +++ scripts/ircServerInfo.nse | 1 + 10 files changed, 181 insertions(+), 55 deletions(-) delete mode 100644 nse_pcrelib.h rename nse_pcrelib.cc => nselib/pcre.c (95%) create mode 100644 nselib/pcre.h diff --git a/Makefile.in b/Makefile.in index 59ede246b..ca709dd75 100644 --- a/Makefile.in +++ b/Makefile.in @@ -56,9 +56,9 @@ INSTALLNSE=@INSTALLNSE@ INSTALLUMIT=@INSTALLUMIT@ ifneq (@LIBLUA_LIBS@,) -NSE_SRC=nse_main.cc nse_auxiliar.cc nse_nsock.cc nse_init.cc nse_nmaplib.cc nse_debug.cc nse_pcrelib.cc nse_string.cc -NSE_HDRS=nse_main.h nse_auxiliar.h nse_nsock.h nse_init.h nse_nmaplib.h nse_debug.h nse_macros.h nse_pcrelib.h nse_string.h -NSE_OBJS=nse_main.o nse_auxiliar.o nse_nsock.o nse_init.o nse_nmaplib.o nse_debug.o nse_pcrelib.o nse_string.o +NSE_SRC=nse_main.cc nse_auxiliar.cc nse_nsock.cc nse_init.cc nse_nmaplib.cc nse_debug.cc nse_string.cc +NSE_HDRS=nse_main.h nse_auxiliar.h nse_nsock.h nse_init.h nse_nmaplib.h nse_debug.h nse_macros.h nse_string.h +NSE_OBJS=nse_main.o nse_auxiliar.o nse_nsock.o nse_init.o nse_nmaplib.o nse_debug.o nse_string.o NSESTDLIB=nsestdlib endif diff --git a/mswin32/nmap.vcproj b/mswin32/nmap.vcproj index 9738e95b2..f271db204 100644 --- a/mswin32/nmap.vcproj +++ b/mswin32/nmap.vcproj @@ -285,10 +285,6 @@ RelativePath="..\nse_nsock.cc" > - - @@ -474,10 +470,6 @@ RelativePath="..\nse_nsock.h" > - - diff --git a/nse_init.cc b/nse_init.cc index f24415408..b31115c79 100644 --- a/nse_init.cc +++ b/nse_init.cc @@ -4,7 +4,6 @@ #include "nse_debug.h" // 3rd Party libs -#include "nse_pcrelib.h" #include "nbase.h" @@ -44,7 +43,6 @@ int init_lua(lua_State* l) { {LUA_STRLIBNAME, luaopen_string}, {LUA_MATHLIBNAME, luaopen_math}, {LUA_DBLIBNAME, luaopen_debug}, - {NSE_PCRELIBNAME, luaopen_pcrelib}, {NULL, NULL} }; diff --git a/nse_pcrelib.h b/nse_pcrelib.h deleted file mode 100644 index 512d2a85a..000000000 --- a/nse_pcrelib.h +++ /dev/null @@ -1,9 +0,0 @@ -#ifndef NSE_PCRELIB -#define NSE_PCRELIB - -#define NSE_PCRELIBNAME "pcre" - -LUALIB_API int luaopen_pcrelib (lua_State *L); - -#endif - diff --git a/nselib/Makefile.in b/nselib/Makefile.in index 30ce5b83b..e632703c0 100644 --- a/nselib/Makefile.in +++ b/nselib/Makefile.in @@ -12,15 +12,21 @@ INSTALL = $(SHTOOL) install LIBTOOL= ./libtool LTFLAGS = --tag=CC --silent -all: bit.so +all: bit.so pcre.so bit.so: bit.c @LIBTOOL_DEPS@ $(LIBTOOL) $(LTFLAGS) --mode=compile $(CC) @LUAINCLUDE@ $(CFLAGS) -c bit.c $(LIBTOOL) $(LTFLAGS) --mode=link $(CC) -avoid-version -module -rpath /usr/local/lib -o bit.la bit.lo mv .libs/bit.so bit.so +pcre.so: pcre.c @LIBTOOL_DEPS@ + $(LIBTOOL) $(LTFLAGS) --mode=compile $(CC) @NBASE_INCLUDE@ @PCRE_INCLUDE@ @LUAINCLUDE@ $(CFLAGS) -c pcre.c + $(LIBTOOL) $(LTFLAGS) --mode=link $(CC) @NBASE_LD@ @PCRE_LD@ @NBASE_LIBS@ @PCRE_LIBS@ -avoid-version -module -rpath /usr/local/lib -o pcre.la pcre.lo + mv .libs/pcre.so pcre.so + + clean: - rm -f bit.so *.la *.lo + rm -f bit.so pcre.so *.la *.lo rm -rf .libs distclean: clean diff --git a/nselib/configure b/nselib/configure index ab5d01b34..ac5997f09 100755 --- a/nselib/configure +++ b/nselib/configure @@ -837,6 +837,12 @@ ac_ct_F77 LIBTOOL LIBTOOL_DEPS LUAINCLUDE +NBASE_LIBS +NBASE_LD +NBASE_INCLUDE +PCRE_INCLUDE +PCRE_LD +PCRE_LIBS LIBOBJS LTLIBOBJS' ac_subst_files='' @@ -1440,6 +1446,10 @@ Optional Packages: --with-pic try to use only PIC/non-PIC objects [default=use both] --with-tags[=TAGS] include additional configurations [automatic] + --with-libnbase=DIR Look for nbase include/libs in DIR + --with-libpcre=DIR Use an existing (compiled) pcre lib from DIR/include + and DIR/lib. + --with-libpcre=included Always use the version included with Nmap Some influential environment variables: CC C compiler command @@ -3631,7 +3641,7 @@ ia64-*-hpux*) ;; *-*-irix6*) # Find out which ABI we are using. - echo '#line 3634 "configure"' > conftest.$ac_ext + echo '#line 3644 "configure"' > conftest.$ac_ext if { (eval echo "$as_me:$LINENO: \"$ac_compile\"") >&5 (eval $ac_compile) 2>&5 ac_status=$? @@ -6253,11 +6263,11 @@ else -e 's:.*FLAGS}\{0,1\} :&$lt_compiler_flag :; t' \ -e 's: [^ ]*conftest\.: $lt_compiler_flag&:; t' \ -e 's:$: $lt_compiler_flag:'` - (eval echo "\"\$as_me:6256: $lt_compile\"" >&5) + (eval echo "\"\$as_me:6266: $lt_compile\"" >&5) (eval "$lt_compile" 2>conftest.err) ac_status=$? cat conftest.err >&5 - echo "$as_me:6260: \$? = $ac_status" >&5 + echo "$as_me:6270: \$? = $ac_status" >&5 if (exit $ac_status) && test -s "$ac_outfile"; then # The compiler can only warn and ignore the option if not recognized # So say no if there are warnings other than the usual output. @@ -6521,11 +6531,11 @@ else -e 's:.*FLAGS}\{0,1\} :&$lt_compiler_flag :; t' \ -e 's: [^ ]*conftest\.: $lt_compiler_flag&:; t' \ -e 's:$: $lt_compiler_flag:'` - (eval echo "\"\$as_me:6524: $lt_compile\"" >&5) + (eval echo "\"\$as_me:6534: $lt_compile\"" >&5) (eval "$lt_compile" 2>conftest.err) ac_status=$? cat conftest.err >&5 - echo "$as_me:6528: \$? = $ac_status" >&5 + echo "$as_me:6538: \$? = $ac_status" >&5 if (exit $ac_status) && test -s "$ac_outfile"; then # The compiler can only warn and ignore the option if not recognized # So say no if there are warnings other than the usual output. @@ -6625,11 +6635,11 @@ else -e 's:.*FLAGS}\{0,1\} :&$lt_compiler_flag :; t' \ -e 's: [^ ]*conftest\.: $lt_compiler_flag&:; t' \ -e 's:$: $lt_compiler_flag:'` - (eval echo "\"\$as_me:6628: $lt_compile\"" >&5) + (eval echo "\"\$as_me:6638: $lt_compile\"" >&5) (eval "$lt_compile" 2>out/conftest.err) ac_status=$? cat out/conftest.err >&5 - echo "$as_me:6632: \$? = $ac_status" >&5 + echo "$as_me:6642: \$? = $ac_status" >&5 if (exit $ac_status) && test -s out/conftest2.$ac_objext then # The compiler can only warn and ignore the option if not recognized @@ -8922,7 +8932,7 @@ else lt_dlunknown=0; lt_dlno_uscore=1; lt_dlneed_uscore=2 lt_status=$lt_dlunknown cat > conftest.$ac_ext < conftest.$ac_ext <&5) + (eval echo "\"\$as_me:11375: $lt_compile\"" >&5) (eval "$lt_compile" 2>conftest.err) ac_status=$? cat conftest.err >&5 - echo "$as_me:11369: \$? = $ac_status" >&5 + echo "$as_me:11379: \$? = $ac_status" >&5 if (exit $ac_status) && test -s "$ac_outfile"; then # The compiler can only warn and ignore the option if not recognized # So say no if there are warnings other than the usual output. @@ -11466,11 +11476,11 @@ else -e 's:.*FLAGS}\{0,1\} :&$lt_compiler_flag :; t' \ -e 's: [^ ]*conftest\.: $lt_compiler_flag&:; t' \ -e 's:$: $lt_compiler_flag:'` - (eval echo "\"\$as_me:11469: $lt_compile\"" >&5) + (eval echo "\"\$as_me:11479: $lt_compile\"" >&5) (eval "$lt_compile" 2>out/conftest.err) ac_status=$? cat out/conftest.err >&5 - echo "$as_me:11473: \$? = $ac_status" >&5 + echo "$as_me:11483: \$? = $ac_status" >&5 if (exit $ac_status) && test -s out/conftest2.$ac_objext then # The compiler can only warn and ignore the option if not recognized @@ -13027,11 +13037,11 @@ else -e 's:.*FLAGS}\{0,1\} :&$lt_compiler_flag :; t' \ -e 's: [^ ]*conftest\.: $lt_compiler_flag&:; t' \ -e 's:$: $lt_compiler_flag:'` - (eval echo "\"\$as_me:13030: $lt_compile\"" >&5) + (eval echo "\"\$as_me:13040: $lt_compile\"" >&5) (eval "$lt_compile" 2>conftest.err) ac_status=$? cat conftest.err >&5 - echo "$as_me:13034: \$? = $ac_status" >&5 + echo "$as_me:13044: \$? = $ac_status" >&5 if (exit $ac_status) && test -s "$ac_outfile"; then # The compiler can only warn and ignore the option if not recognized # So say no if there are warnings other than the usual output. @@ -13131,11 +13141,11 @@ else -e 's:.*FLAGS}\{0,1\} :&$lt_compiler_flag :; t' \ -e 's: [^ ]*conftest\.: $lt_compiler_flag&:; t' \ -e 's:$: $lt_compiler_flag:'` - (eval echo "\"\$as_me:13134: $lt_compile\"" >&5) + (eval echo "\"\$as_me:13144: $lt_compile\"" >&5) (eval "$lt_compile" 2>out/conftest.err) ac_status=$? cat out/conftest.err >&5 - echo "$as_me:13138: \$? = $ac_status" >&5 + echo "$as_me:13148: \$? = $ac_status" >&5 if (exit $ac_status) && test -s out/conftest2.$ac_objext then # The compiler can only warn and ignore the option if not recognized @@ -15322,11 +15332,11 @@ else -e 's:.*FLAGS}\{0,1\} :&$lt_compiler_flag :; t' \ -e 's: [^ ]*conftest\.: $lt_compiler_flag&:; t' \ -e 's:$: $lt_compiler_flag:'` - (eval echo "\"\$as_me:15325: $lt_compile\"" >&5) + (eval echo "\"\$as_me:15335: $lt_compile\"" >&5) (eval "$lt_compile" 2>conftest.err) ac_status=$? cat conftest.err >&5 - echo "$as_me:15329: \$? = $ac_status" >&5 + echo "$as_me:15339: \$? = $ac_status" >&5 if (exit $ac_status) && test -s "$ac_outfile"; then # The compiler can only warn and ignore the option if not recognized # So say no if there are warnings other than the usual output. @@ -15590,11 +15600,11 @@ else -e 's:.*FLAGS}\{0,1\} :&$lt_compiler_flag :; t' \ -e 's: [^ ]*conftest\.: $lt_compiler_flag&:; t' \ -e 's:$: $lt_compiler_flag:'` - (eval echo "\"\$as_me:15593: $lt_compile\"" >&5) + (eval echo "\"\$as_me:15603: $lt_compile\"" >&5) (eval "$lt_compile" 2>conftest.err) ac_status=$? cat conftest.err >&5 - echo "$as_me:15597: \$? = $ac_status" >&5 + echo "$as_me:15607: \$? = $ac_status" >&5 if (exit $ac_status) && test -s "$ac_outfile"; then # The compiler can only warn and ignore the option if not recognized # So say no if there are warnings other than the usual output. @@ -15694,11 +15704,11 @@ else -e 's:.*FLAGS}\{0,1\} :&$lt_compiler_flag :; t' \ -e 's: [^ ]*conftest\.: $lt_compiler_flag&:; t' \ -e 's:$: $lt_compiler_flag:'` - (eval echo "\"\$as_me:15697: $lt_compile\"" >&5) + (eval echo "\"\$as_me:15707: $lt_compile\"" >&5) (eval "$lt_compile" 2>out/conftest.err) ac_status=$? cat out/conftest.err >&5 - echo "$as_me:15701: \$? = $ac_status" >&5 + echo "$as_me:15711: \$? = $ac_status" >&5 if (exit $ac_status) && test -s out/conftest2.$ac_objext then # The compiler can only warn and ignore the option if not recognized @@ -18523,6 +18533,61 @@ fi +#needed for pcre - libpcre and nbase + +# Check whether --with-libnbase was given. +if test "${with_libnbase+set}" = set; then + withval=$with_libnbase; case "$with_libnbase" in + yes) + ;; + *) + NBASEDIR="$with_libnbase" + ;; + esac +else + NBASEDIR="../nbase" + +fi + + + +NBASE_INCLUDE="-I$NBASEDIR" +NBASE_LD="-L$NBASEDIR" +NBASE_LIBS="-lnbase" + + + + + +# First we test whether they specified libpcre explicitly + +# Check whether --with-libpcre was given. +if test "${with_libpcre+set}" = set; then + withval=$with_libpcre; case "$with_libpcre" in + yes) + ;; + included) + PCREDIR="../libpcre" + ;; + *) + PCREDIR="$with_libpcre" + ;; + esac +else + PCREDIR="../libpcre" + +fi + +PCRE_INCLUDE="-I$PCREDIR" +PCRE_LD="-L$PCREDIR" +PCRE_LIBS="-lpcre" + +# AC_SUBST(PCREDIR) + + + + + ac_config_files="$ac_config_files Makefile" cat >confcache <<\_ACEOF @@ -19223,11 +19288,17 @@ ac_ct_F77!$ac_ct_F77$ac_delim LIBTOOL!$LIBTOOL$ac_delim LIBTOOL_DEPS!$LIBTOOL_DEPS$ac_delim LUAINCLUDE!$LUAINCLUDE$ac_delim +NBASE_LIBS!$NBASE_LIBS$ac_delim +NBASE_LD!$NBASE_LD$ac_delim +NBASE_INCLUDE!$NBASE_INCLUDE$ac_delim +PCRE_INCLUDE!$PCRE_INCLUDE$ac_delim +PCRE_LD!$PCRE_LD$ac_delim +PCRE_LIBS!$PCRE_LIBS$ac_delim LIBOBJS!$LIBOBJS$ac_delim LTLIBOBJS!$LTLIBOBJS$ac_delim _ACEOF - if test `sed -n "s/.*$ac_delim\$/X/p" conf$$subs.sed | grep -c X` = 72; then + if test `sed -n "s/.*$ac_delim\$/X/p" conf$$subs.sed | grep -c X` = 78; then break elif $ac_last_try; then { { echo "$as_me:$LINENO: error: could not make $CONFIG_STATUS" >&5 diff --git a/nselib/configure.ac b/nselib/configure.ac index e97d1ac15..b20975982 100644 --- a/nselib/configure.ac +++ b/nselib/configure.ac @@ -19,6 +19,54 @@ AC_CANONICAL_HOST AC_CHECK_HEADER([lua.h],,[AC_MSG_NOTICE(using lua-includefiles provided with nmap);[LUAINCLUDE=-I../liblua/]],) AC_SUBST(LUAINCLUDE) +#needed for pcre - libpcre and nbase +AC_ARG_WITH(libnbase, +AC_HELP_STRING([--with-libnbase=DIR], [Look for nbase include/libs in DIR]), +[ case "$with_libnbase" in + yes) + ;; + *) + NBASEDIR="$with_libnbase" + ;; + esac], +NBASEDIR="../nbase" +) + + +NBASE_INCLUDE="-I$NBASEDIR" +NBASE_LD="-L$NBASEDIR" +NBASE_LIBS="-lnbase" + +AC_SUBST(NBASE_LIBS) +AC_SUBST(NBASE_LD) +AC_SUBST(NBASE_INCLUDE) + +# First we test whether they specified libpcre explicitly +AC_ARG_WITH(libpcre, +AC_HELP_STRING([--with-libpcre=DIR], [Use an existing (compiled) pcre lib from DIR/include and DIR/lib.]) +AC_HELP_STRING([--with-libpcre=included], [Always use the version included with Nmap]), +[ case "$with_libpcre" in + yes) + ;; + included) + PCREDIR="../libpcre" + ;; + *) + PCREDIR="$with_libpcre" + ;; + esac], +PCREDIR="../libpcre" +) +PCRE_INCLUDE="-I$PCREDIR" +PCRE_LD="-L$PCREDIR" +PCRE_LIBS="-lpcre" + +# AC_SUBST(PCREDIR) +AC_SUBST(PCRE_INCLUDE) +AC_SUBST(PCRE_LD) +AC_SUBST(PCRE_LIBS) + + AC_CONFIG_FILES([Makefile]) AC_OUTPUT diff --git a/nse_pcrelib.cc b/nselib/pcre.c similarity index 95% rename from nse_pcrelib.cc rename to nselib/pcre.c index f6b68d5e0..9a2ea2bbe 100644 --- a/nse_pcrelib.cc +++ b/nselib/pcre.c @@ -7,18 +7,20 @@ #include #include +#ifdef __cplusplus extern "C" { +#endif #include "lua.h" #include "lauxlib.h" + +#ifdef __cplusplus } +#endif #include #include -#include "nbase.h" -#include "nmap_error.h" - -#include "nse_pcrelib.h" +#include "pcre.h" static void L_lua_error(lua_State *L, const char *message) { @@ -147,9 +149,17 @@ static int Lpcre_comp(lua_State *L) if(error) L_lua_error(L, error); pcre_fullinfo(ud->pr, ud->extra, PCRE_INFO_CAPTURECOUNT, &ud->ncapt); + /* since some platforms have problems with nbase and exporting symbols we + * emulate it + */ + if(((ud->ncapt + 1) * 3 * sizeof(int))<0){ + L_lua_error(L, "PCRE: negative argument to malloc"); + } /* need (2 ints per capture, plus one for substring match) * 3/2 */ - ud->match = (int *) safe_malloc((ud->ncapt + 1) * 3 * sizeof(int)); - + ud->match = (int *) malloc((ud->ncapt + 1) * 3 * sizeof(int)); + if(ud->match==NULL){ + L_lua_error(L, "PCRE: malloc failed!"); + } return 1; } @@ -362,7 +372,7 @@ static const luaL_reg pcrelib[] = { {NULL, NULL} }; -LUALIB_API int luaopen_pcrelib(lua_State *L) +LUALIB_API int luaopen_pcre(lua_State *L) { createmeta(L, pcre_handle); luaL_openlib(L, NULL, pcremeta, 0); diff --git a/nselib/pcre.h b/nselib/pcre.h new file mode 100644 index 000000000..54b63ca69 --- /dev/null +++ b/nselib/pcre.h @@ -0,0 +1,9 @@ +#ifndef PCRE_H +#define PCRE_H + +#define NSE_PCRELIBNAME "pcre" + +LUALIB_API int luaopen_pcre(lua_State *L); + +#endif + diff --git a/scripts/ircServerInfo.nse b/scripts/ircServerInfo.nse index b77cd8b5d..9fb6ef08d 100644 --- a/scripts/ircServerInfo.nse +++ b/scripts/ircServerInfo.nse @@ -10,6 +10,7 @@ categories = {"discovery"} require("stdnse") require "shortport" +require "pcre" portrule = shortport.port_or_service(6667, "irc")