From e4a410d34b21c1cb6d0a4510e96414c335961497 Mon Sep 17 00:00:00 2001 From: "Jeffrey C. Ollie" Date: Fri, 14 Oct 2011 19:08:37 -0500 Subject: [PATCH 1/3] Allow linking/building against an external editline library. The Asterisk tarball includes a very old copy of the editline library that manages command line history and editing. Fedora (and several other distributions) highly discourage this practice because if there are security problems (or even just severe bugs) in the library it can be very difficult to track down all instances of the library. This patch adds the option of building/linking against an external copy of the editline library. --- build_tools/menuselect-deps.in | 1 + configure.ac | 22 ++++++++++++++++++++++ main/Makefile | 9 +++++---- main/asterisk.c | 30 +++++++----------------------- main/cli.c | 3 ++- makeopts.in | 4 ++++ 6 files changed, 41 insertions(+), 28 deletions(-) diff --git a/build_tools/menuselect-deps.in b/build_tools/menuselect-deps.in index 35573c3..4425204 100644 --- a/build_tools/menuselect-deps.in +++ b/build_tools/menuselect-deps.in @@ -26,6 +26,7 @@ JACK=@PBX_JACK@ KQUEUE=@PBX_KQUEUE@ LDAP=@PBX_LDAP@ LIBXML2=@PBX_LIBXML2@ +LIBEDIT=@PBX_LIBEDIT@ LTDL=@PBX_LTDL@ LUA=@PBX_LUA@ MISDN=@PBX_MISDN@ diff --git a/configure.ac b/configure.ac index f84b92d..440a52e 100644 --- a/configure.ac +++ b/configure.ac @@ -394,6 +394,7 @@ AST_EXT_LIB_SETUP([JACK], [Jack Audio Connection Kit], [jack]) AST_EXT_LIB_SETUP([KQUEUE], [kqueue support], [kqueue]) AST_EXT_LIB_SETUP([LDAP], [OpenLDAP], [ldap]) AST_LIBCURL_CHECK_CONFIG([], [7.10.1]) +AST_EXT_LIB_SETUP([LIBEDIT], [NetBSD Editline library], [libedit]) AST_EXT_LIB_SETUP([LIBXML2], [LibXML2], [libxml2]) AST_EXT_LIB_SETUP([LTDL], [libtool], [ltdl]) AST_EXT_LIB_SETUP([LUA], [Lua], [lua]) @@ -2194,6 +2195,27 @@ AC_SUBST(PBX_GTK2) AC_SUBST(GTK2_INCLUDE) AC_SUBST(GTK2_LIB) +PBX_LIBEDIT=0 +LIBEDIT_INCLUDE=-Ieditline/readline +LIBEDIT_LIB= +LIBEDIT_OBJ=editline/libedit.a +if test "${USE_LIBEDIT}" != "no"; then + AC_CHECK_TOOL(PKGCONFIG, pkg-config, no) + if test "${PKGCONFIG}" != "no"; then + if ${PKGCONFIG} --exists libedit; then + LIBEDIT_INCLUDE=$(${PKGCONFIG} libedit --cflags) + LIBEDIT_LIB=$(${PKGCONFIG} libedit --libs) + LIBEDIT_OBJ= + PBX_LIBEDIT=1 + AC_DEFINE([HAVE_LIBEDIT], 1, [Define if your system has the NetBSD Editline libraries.]) + fi + fi +fi +AC_SUBST(PBX_LIBEDIT) +AC_SUBST(LIBEDIT_INCLUDE) +AC_SUBST(LIBEDIT_LIB) +AC_SUBST(LIBEDIT_OBJ) + # build a GENERIC_ODBC result based on the presence of either UnixODBC (preferred) # or iODBC diff --git a/main/Makefile b/main/Makefile index 728b805..a4fa7b5 100644 --- a/main/Makefile +++ b/main/Makefile @@ -143,6 +143,7 @@ testexpr2: ast_expr2f.c ast_expr2.c ast_expr2.h rm ast_expr2.o ast_expr2f.o db.o: _ASTCFLAGS+=$(SQLITE3_INCLUDE) +cli.o: ASTCFLAGS+=$(LIBEDIT_INCLUDE) ifneq ($(findstring ENABLE_UPLOADS,$(MENUSELECT_CFLAGS)),) http.o: _ASTCFLAGS+=$(GMIME_INCLUDE) @@ -178,13 +179,13 @@ endif $(OBJS): _ASTCFLAGS+=-DAST_MODULE=\"core\" -$(MAIN_TGT): $(OBJS) editline/libedit.a $(AST_EMBED_LDSCRIPTS) +$(MAIN_TGT): $(OBJS) $(LIBEDIT_OBJ) $(AST_EMBED_LDSCRIPTS) @$(CC) -c -o buildinfo.o $(_ASTCFLAGS) buildinfo.c $(ASTCFLAGS) - $(ECHO_PREFIX) echo " [LD] $(OBJS) editline/libedit.a $(AST_EMBED_LDSCRIPTS) -> $@" + $(ECHO_PREFIX) echo " [LD] $(OBJS) $(LIBEDIT_OBJ) $(AST_EMBED_LDSCRIPTS) -> $@" ifneq ($(findstring chan_h323,$(MENUSELECT_CHANNELS)),) - $(CMD_PREFIX) $(CC) $(STATIC_BUILD) -o $@ $(ASTLINK) $(AST_EMBED_LDFLAGS) $(_ASTLDFLAGS) $(ASTLDFLAGS) $(OBJS) editline/libedit.a $(AST_EMBED_LDSCRIPTS) buildinfo.o $(AST_LIBS) $(AST_EMBED_LIBS) $(GMIMELDFLAGS) + $(CMD_PREFIX) $(CC) $(STATIC_BUILD) -o $@ $(ASTLINK) $(AST_EMBED_LDFLAGS) $(_ASTLDFLAGS) $(ASTLDFLAGS) $(OBJS) $(LIBEDIT_OBJ) $(AST_EMBED_LDSCRIPTS) buildinfo.o $(AST_LIBS) $(AST_EMBED_LIBS) $(GMIMELDFLAGS) $(LIBEDIT_LIB) else - $(CMD_PREFIX) $(CXX) $(STATIC_BUILD) -o $@ $(ASTLINK) $(AST_EMBED_LDFLAGS) $(_ASTLDFLAGS) $(ASTLDFLAGS) $(H323LDFLAGS) $(OBJS) editline/libedit.a $(AST_EMBED_LDSCRIPTS) buildinfo.o $(AST_LIBS) $(AST_EMBED_LIBS) $(H323LDLIBS) $(GMIMELDFLAGS) + $(CMD_PREFIX) $(CXX) $(STATIC_BUILD) -o $@ $(ASTLINK) $(AST_EMBED_LDFLAGS) $(_ASTLDFLAGS) $(ASTLDFLAGS) $(H323LDFLAGS) $(OBJS) $(LIBEDIT_OBJ) $(AST_EMBED_LDSCRIPTS) buildinfo.o $(AST_LIBS) $(AST_EMBED_LIBS) $(H323LDLIBS) $(GMIMELDFLAGS) $(LIBEDIT_LIB) endif ifeq ($(GNU_LD),1) diff --git a/main/asterisk.c b/main/asterisk.c index 16f3efa..57c84e7 100644 --- a/main/asterisk.c +++ b/main/asterisk.c @@ -107,6 +107,8 @@ int daemon(int, int); /* defined in libresolv of all places */ #endif /* HAVE_CAP */ #endif /* linux */ +#include + #include "asterisk/paths.h" /* we define here the variables so better agree on the prototype */ #include "asterisk/network.h" #include "asterisk/cli.h" @@ -131,7 +133,6 @@ int daemon(int, int); /* defined in libresolv of all places */ #include "asterisk/utils.h" #include "asterisk/file.h" #include "asterisk/io.h" -#include "editline/histedit.h" #include "asterisk/config.h" #include "asterisk/ast_version.h" #include "asterisk/linkedlists.h" @@ -2500,7 +2501,7 @@ static char *cli_complete(EditLine *editline, int ch) int mlen = 0, maxmbuf = 2048; /* Start with a 2048 byte buffer */ if (!(mbuf = ast_malloc(maxmbuf))) { - lf->cursor[0] = savechr; + *(char *)lf->cursor = savechr; return (char *)(CC_ERROR); } snprintf(buf, sizeof(buf), "_COMMAND MATCHESARRAY \"%s\" \"%s\"", lf->buffer, ptr); @@ -2512,7 +2513,7 @@ static char *cli_complete(EditLine *editline, int ch) /* Every step increment buffer 1024 bytes */ maxmbuf += 1024; if (!(mbuf = ast_realloc(mbuf, maxmbuf))) { - lf->cursor[0] = savechr; + *(char *)lf->cursor = savechr; return (char *)(CC_ERROR); } } @@ -2574,7 +2575,7 @@ static char *cli_complete(EditLine *editline, int ch) ast_free(matches); } - lf->cursor[0] = savechr; + *(char *)lf->cursor = savechr; return (char *)(long)retval; } @@ -2657,29 +2658,12 @@ static int ast_el_write_history(char *filename) static int ast_el_read_history(char *filename) { - char buf[MAX_HISTORY_COMMAND_LENGTH]; - FILE *f; - int ret = -1; + HistEvent ev; if (el_hist == NULL || el == NULL) ast_el_initialize(); - if ((f = fopen(filename, "r")) == NULL) - return ret; - - while (!feof(f)) { - if (!fgets(buf, sizeof(buf), f)) - break; - if (!strcmp(buf, "_HiStOrY_V2_\n")) - continue; - if (ast_all_zeros(buf)) - continue; - if ((ret = ast_el_add_history(buf)) == -1) - break; - } - fclose(f); - - return ret; + return (history(el_hist, &ev, H_LOAD, filename)); } static void ast_remotecontrol(char *data) diff --git a/main/cli.c b/main/cli.c index 85f4473..7b6ae83 100644 --- a/main/cli.c +++ b/main/cli.c @@ -36,6 +36,8 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$") #include #include +#include + #include "asterisk/cli.h" #include "asterisk/linkedlists.h" #include "asterisk/module.h" @@ -44,7 +46,6 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$") #include "asterisk/utils.h" #include "asterisk/app.h" #include "asterisk/lock.h" -#include "editline/readline/readline.h" #include "asterisk/threadstorage.h" #include "asterisk/translate.h" diff --git a/makeopts.in b/makeopts.in index c7a5eaf..4db0e96 100644 --- a/makeopts.in +++ b/makeopts.in @@ -309,3 +309,7 @@ TINFO_DIR=@TINFO_DIR@ # if poll is not present, let the makefile know. POLL_AVAILABLE=@HAS_POLL@ TIMERFD_INCLUDE=@TIMERFD_INCLUDE@ + +LIBEDIT_INCLUDE=@LIBEDIT_INCLUDE@ +LIBEDIT_LIB=@LIBEDIT_LIB@ +LIBEDIT_OBJ=@LIBEDIT_OBJ@ -- 1.7.6.4