From f91203b30c4816d67cbe8b9853742dcc698e6ac9 Mon Sep 17 00:00:00 2001 From: je Date: Sun, 9 Jan 2011 19:47:43 +0100 Subject: [PATCH] Import wrapper, honor CFLAGS and LDFLAGS env vars. * See wrapper.c for information about what the wrapper does. * CFLAGS and LDFLAGS environment variables are now honored when running make(1). --- Makefile | 14 ++++--- config.mk | 4 +- wrapper.c | 127 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 138 insertions(+), 7 deletions(-) create mode 100644 wrapper.c diff --git a/Makefile b/Makefile index 0bd4ca5..43a5bf3 100644 --- a/Makefile +++ b/Makefile @@ -7,8 +7,8 @@ include config.mk SRC = ii.c OBJ = ${SRC:.c=.o} -all: options ii - @echo built ii +all: options ii wrapper + @echo built ii and wrapper options: @echo ii build options: @@ -24,7 +24,7 @@ options: dist: clean @mkdir -p ii-${VERSION} - @cp -R query.sh Makefile CHANGES README FAQ LICENSE config.mk ii.c ii.1 ii-${VERSION} + @cp -R query.sh Makefile CHANGES README FAQ LICENSE config.mk ii.c ii.1 wrapper.c ii-${VERSION} @tar -cf ii-${VERSION}.tar ii-${VERSION} @gzip ii-${VERSION}.tar @rm -rf ii-${VERSION} @@ -41,7 +41,7 @@ install: all @install -d ${DESTDIR}${BINDIR} ${DESTDIR}${MAN1DIR} @install -m 644 CHANGES README query.sh FAQ LICENSE ${DESTDIR}${DOCDIR} - @install -m 775 ii ${DESTDIR}${BINDIR} + @install -m 775 ii wrapper ${DESTDIR}${BINDIR} @install -m 444 ii.1 ${DESTDIR}${MAN1DIR} @echo "installed ii" @@ -52,4 +52,8 @@ uninstall: all @echo "uninstalled ii" clean: - rm -f ii *~ *.o *core *.tar.gz + rm -f ii wrapper *~ *.o *core *.tar.gz + +wrapper: wrapper.o + @echo LD $@ + @${CC} -o $@ $@.o ${LDFLAGS} diff --git a/config.mk b/config.mk index ac9a047..86b76c4 100644 --- a/config.mk +++ b/config.mk @@ -23,5 +23,5 @@ LIBS = -L${LIBDIR} -L/usr/lib -lc # compiler CC = cc -CFLAGS = -g -O0 -W -Wall ${INCLUDES} -DVERSION=\"${VERSION}\" -LDFLAGS = ${LIBS} +CFLAGS := ${CFLAGS} -g -O0 -W -Wall ${INCLUDES} -DVERSION=\"${VERSION}\" +LDFLAGS := ${LDFLAGS} ${LIBS} diff --git a/wrapper.c b/wrapper.c new file mode 100644 index 0000000..5d4c242 --- /dev/null +++ b/wrapper.c @@ -0,0 +1,127 @@ +#include +#include + +#include +#include +#include +#include +#include +#include + +#define EXECUTABLE "ii" + +/* + * Takes user specified hostnames and port numbers and randomly selects one pair[1] + * before it passes them as arguments to EXECUTABLE via system(3). Whenever system(3) + * dies, a new pair is selected[2]. + * [2] is done in the background while [1] is not. + */ +int +main(int argc, char **argv) +{ + const unsigned int uargc = (unsigned int)argc; + char *init = NULL; + size_t i, j, r, rv = 0, s; + pid_t pid, sid; + + /* Validate the user input. */ + if (uargc % 2 != 0 || uargc < 4) + err(1, "\"\" ...\n"); + + /* + * sh(1) meta-characters: `<', `>', `|', `;', `(', `)', and `&'. + * system(3) caveat: + * http://www.openbsd.org/cgi-bin/man.cgi?query=system&apropos=0&sektion=0&manpath=OpenBSD+Current&arch=i386&format=html#CAVEATS + */ + for (i = 0; i < strlen(argv[1]); i++) + if (argv[1][i] == '<' || argv[1][i] == '>' || argv[1][i] == '|' + || argv[1][i] == ';' || argv[1][i] == '(' || argv[1][i] == ')' + || argv[1][i] == '&' || argv[1][i] == '"' || argv[1][i] == '\'') + err(1, "sh(1) meta-characters and quotation marks are" + " not allowed.\n"); + + for (i = 2; i < uargc; i++) + if (i % 2 == 1) { + if (atoi(argv[i]) < 1 || atoi(argv[i]) > 65535) + err(1, "Port number `%lu' is not in range." + " (Counting from zero.)\n", (unsigned long)i / 2 - 1); + for (j = 0; j < strlen(argv[i]); j++) + if (argv[i][j] < 0x30 || argv[i][j] > 0x39) /* !0-9 */ + err(1, "Character `%lu' in port `%lu'" + " is not allowed." + " (Counting from zero.)\n", + (unsigned long)j, (unsigned long)i / 2 - 1); + } else { + for (j = 0; j < strlen(argv[i]); j++) + if (((argv[i][j] < 0x61 || argv[i][j] > 0x7a) /* !a-z */ + && (argv[i][j] < 0x30 || argv[i][j] > 0x39) /* !0-9 */ + && (argv[i][j] < 0x41 || argv[i][j] > 0x5a)) /* !A-Z */ + && argv[i][j] != 0x2d && argv[i][j] != 0x2e) /* !. !- */ + err(1, "Character `%lu' in hostname `%lu'" + " is not allowed." + " (Counting from zero.)\n", + (unsigned long)j, (unsigned long)i / 2 - 1); + } + + /* Tell the user what we have got. */ + for (i = 2; i < uargc; i += 2) + printf("[%lu]: %s:%d\n", (unsigned long)(i - 1) / 2, argv[i], + atoi(argv[i + 1])); + + /* Lets continue in the background. */ + pid = fork(); + if (pid < 0) { + rv = 1; + goto clean; + } else if (pid > 0) + goto clean; + + umask(0); + + sid = setsid(); + if (sid < 0) { + rv = 1; + goto clean; + } + + if ((chdir("/")) < 0) { + rv = 1; + goto clean; + } + + close(STDIN_FILENO); + close(STDOUT_FILENO); + close(STDERR_FILENO); + + srandom(time(NULL)); + + while (1) { + /* + * Calculate a random number in the range 0-(i/2 - 1). + * http://eternallyconfuzzled.com/arts/jsw_art_rand.aspx + */ + r = random() * 1.0 / ( RAND_MAX + 1.0 ) * (i / 2 - 1); + + s = strlen(argv[1]) + strlen(argv[r * 2 + 2]) + + strlen(argv[r * 2 + 3]) + 10 + sizeof(EXECUTABLE); + + if ((init = calloc(s, 1)) == NULL) { + rv = 1; + goto clean; + } + strlcpy(init, EXECUTABLE" ", s); + strlcat(init, argv[1], s); + strlcat(init, " -s ", s); + strlcat(init, argv[r * 2 + 2], s); + strlcat(init, " -p ", s); + strlcat(init, argv[r * 2 + 3], s); + + system(init); + free(init); + } + +clean: + if (init != NULL) + free(init); + return rv; +} -- 2.11.4.GIT