From 0022ffa6178d7cbf46615a9b39510cb3aa40d243 Mon Sep 17 00:00:00 2001 From: Damien Stuart Date: Sun, 30 Nov 2008 22:21:15 +0000 Subject: [PATCH] Added more source files. Split out libfwknop functions to a static lib. Misc updates. git-svn-id: file:///home/mbr/svn/fwknop/trunk@5 510a4753-2344-4c79-9c09-4d669213fbeb --- Makefile | 56 ++++++++++++++++------- README | 45 +++++++++++-------- fko_test.c | 17 +++++++ fwknop.c | 108 +++++++++++++++++++++++++++++++++++++++++++++ fwknop.h | 47 +++++++++++--------- spa_message_type.c | 41 +++++++++++++++++ spa_user.c | 2 +- spa_version.c | 34 ++++++++++++++ 8 files changed, 291 insertions(+), 59 deletions(-) create mode 100644 fwknop.c create mode 100644 spa_message_type.c create mode 100644 spa_version.c diff --git a/Makefile b/Makefile index 4d3ffd6d..2638a191 100644 --- a/Makefile +++ b/Makefile @@ -24,10 +24,7 @@ ############################################################################ # CC = gcc - -# Specify pcap library (typically pcap or pcap_ring). -# -PCAP_LIB = -lpcap +AR = ar # Base CFLAGS # For Full debugging (for extreme verbose output at runtime), add @@ -42,31 +39,53 @@ BASE_CFLAGS = -Wall -fno-strict-aliasing #CFLAGS = -O2 $(BASE_CFLAGS) # ## For debugging symbols if you plan to use a debugger +# CFLAGS = -g -O0 $(BASE_CFLAGS) LDFLAGS = LIBS = #$(PCAP_LIB) -lm -lz -PROG = fko_test +PROG = fwknop +SRC = fwknop.c -SRCS = fko_test.c \ - spa_random_number.c \ +OBJ = $(SRC:.c=.o) + +TPROG = fko_test +TSRC = fko_test.c + +TOBJ = $(TSRC:.c=.o) + +LIBFWK = libfwknop.a +LIBSRCS = spa_random_number.c \ spa_user.c \ spa_timestamp.c \ + spa_version.c \ + spa_message_type.c \ strlcat.c \ strlcpy.c -OBJS = $(SRCS:.c=.o) +LIBOBJS = $(LIBSRCS:.c=.o) +# Group all the source files for make depend. +# +ALLSRCS = $(LIBSRCS) $(SRC) $(TSRC) ########################################################################### # Targets # -all: $(PROG) +all: $(PROG) $(TPROG) -$(PROG): $(OBJS) - $(CC) $(LDFLAGS) -o $@ $(OBJS) $(LIBS) +$(PROG): $(OBJ) $(LIBFWK) + $(CC) $(LDFLAGS) -o $@ $(OBJ) $(LIBFWK) $(LIBS) + +$(TPROG): $(TOBJ) $(LIBFWK) + $(CC) $(LDFLAGS) -o $@ $(TOBJ) $(LIBFWK) $(LIBS) + +lib: $(LIBFWK) + +$(LIBFWK): $(LIBOBJS) + $(AR) rcs $@ $(LIBOBJS) # Force a normal rebuild. # @@ -76,18 +95,18 @@ strip: $(PROG) strip $(PROG) clean: - rm -f $(PROG) $(OBJS) + rm -f $(PROG) $(TPROG) *.o *.a *.so -realclean: - rm -f $(PROG) $(OBJS) core *.bak *.tmp *[-~] +realclean: clean + rm -f core *.bak *.tmp *[-~] # Generate the dependencies for the sources in this current directory # while ignoring warnings. Note: If you don't have makedepend in your PATH, # you will simple get a warning and noting will happen. # depend: - @`which makedepend 2>/dev/null` -Y -- $(CFLAGS) -- $(SRCS) 2> /dev/null \ - && echo "makedepend -Y -- $(CFLAGS) -- $(SRCS) 2> /dev/null" \ + @`which makedepend 2>/dev/null` -Y -- $(CFLAGS) -- $(ALLSRCS) 2> /dev/null \ + && echo "makedepend -Y -- $(CFLAGS) -- $(ALLSRCS) 2> /dev/null" \ || echo " - makedepend not found. Aborting..." @@ -96,7 +115,10 @@ depend: # # DO NOT DELETE -fko_test.o: fwknop.h spa_random_number.o: fwknop.h spa_user.o: fwknop.h spa_timestamp.o: fwknop.h +spa_version.o: fwknop.h +spa_message_type.o: fwknop.h +fwknop.o: fwknop.h +fko_test.o: fwknop.h diff --git a/README b/README index bde2a51a..50b69c1a 100644 --- a/README +++ b/README @@ -1,30 +1,37 @@ This is the top-level directory for the C version of fwknop. +Additionl information and details can be found on the fwknop-c site at +http://devmetrix.org/trac/fwknop-c. + libfwnop source files: ====================== +These will be compiled into libfwknop.a (static for now). -spa_random_number.c - Usage: "char* spa_random_number(char* rand_val);" +spa_random_number.c - Generate a string of 16 random hex digits. +spa_user.c - Get or spoof the current user. +spa_timestamp.c - Get the current timestamp with optional offset. +spa_version.c - Get the fwknop-client version number. +spa_message_type.c - +spa_message.c - +spa_nat_access.c - +spa_server_auth.c - +spa_client_timeout.c - +spa_digest.c - + -more to come- - Where 'rand_val' must be a char array of at least 17 bytes. - Returns a pointer to the provided char array. +Executables: +============ -spa_user.c +fwknop - Soon to be the fwknop client. At present, it just + dumps some internal data for functional verification + during development. -spa_timestamp.c +fko_test - A temp program for testing libfwknop functions during + during development. This will go away once we have + a fair amount of progress in developing the client + (and maybe the server). -spa_version.c + -more to come- -spa_message_type.c -spa_message.c - -spa_nat_access.c - -spa_server_auth.c - -spa_client_timeout.c - -spa_digest.c - -Someday, I'll put something meaningful here... --DSS +###EOF### diff --git a/fko_test.c b/fko_test.c index 7486f560..1a68ad64 100644 --- a/fko_test.c +++ b/fko_test.c @@ -59,6 +59,23 @@ int main(int argc, char **argv) spa_timestamp(&sm, -600); printf("SPA_TIMESTAMP (-600): %u\n", sm.timestamp); + /********************************************************************* + * Get the version of fwknop. + */ + printf(" SPA_Version: %s\n", spa_version(&sm)); + + /********************************************************************* + * Set and get the message type. set ACCESS, COMMAND, NAT, Then + * invalid + */ + spa_message_type(&sm, SPA_ACCESS_MSG); + printf(" SPA Message Type: %u\n", sm.message_type); + spa_message_type(&sm, SPA_COMMAND_MSG); + printf("SPA CMD Message Type: %u\n", sm.message_type); + spa_message_type(&sm, SPA_LOCAL_NAT_ACCESS_MSG); + printf("SPA NAT Message Type: %u\n", sm.message_type); + spa_message_type(&sm, 100); + printf("SPA bad Message Type: %u\n", sm.message_type); return(0); diff --git a/fwknop.c b/fwknop.c new file mode 100644 index 00000000..68ffab5b --- /dev/null +++ b/fwknop.c @@ -0,0 +1,108 @@ +/* $Id$ + ***************************************************************************** + * + * File: fwknop.c + * + * Author: Damien S. Stuart + * + * Purpose: fwknop client program. + * + * Copyright (C) 2008 Damien Stuart (dstuart@dstuart.org) + * + * License (GNU Public License): + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 + * USA + * + ***************************************************************************** +*/ +#include "fwknop.h" + +/* Local prototypes +*/ +void init_spa_message(spa_message_t *sm); +void dump_spa_message_data(spa_message_t *sm); + +int main(int argc, char **argv) +{ + spa_message_t sm; + + init_spa_message(&sm); + + /* Timestamp */ + spa_timestamp(&sm, 0); + + dump_spa_message_data(&sm); + + return(0); +} + +/* Initialize the spa_message data struct, and set some default/preliminary + * values. +*/ +void init_spa_message(spa_message_t *sm) +{ + /* Zero our SPA message struct. + */ + memset(sm, 0x0, sizeof(spa_message_t)); + + /* Initialize default values. + */ + sm->digest_type = DEFAULT_DIGEST; + sm->enc_pcap_port = DEFAULT_PORT; + sm->message_type = DEFAULT_MSG_TYPE; + sm->client_timeout = DEFAULT_CLIENT_TIMEOUT; + + /* Go ahead and and setup the random and user fields. + */ + spa_random_number(sm); + spa_user(sm, NULL); + + /* Version is static, so we add it here as well. + */ + spa_version(sm); +} + +/* Pretty print the data in the spa_message data struct. +*/ +void dump_spa_message_data(spa_message_t *sm) +{ + printf( + "\nCurrent SPA Message Data:\n\n" + " Random Val: %s\n" + " User: %s\n" + " Timestamp: %u\n" + " Version: %s\n" + " Message Type: %u\n" + " Message String: %s\n" + " Nat Access: %s\n" + " Server Auth: %s\n" + " Client Timeout: %u\n" + " Digest: %s\n" + "\n" + " Digest Type: %u\n" + " Port: %u\n" + "\n", + sm->rand_val, + sm->user, + sm->timestamp, + sm->version, + sm->message_type, + sm->message, + sm->nat_access, + sm->server_auth, + sm->client_timeout, + sm->digest, + sm->digest_type, + sm->enc_pcap_port + ); +} + +/***EOF***/ diff --git a/fwknop.h b/fwknop.h index 36e7a691..81af0b0b 100644 --- a/fwknop.h +++ b/fwknop.h @@ -37,21 +37,21 @@ /* General params */ -#define FWKNOP_VERSION "1.9.10-pre1" /* The fwknop client version # */ -#define VERSION_LENGTH 11 /* Length of the version string */ +#define FWKNOP_VERSION "1.9.9" /* The fwknop client version # */ +#define VERSION_LENGTH 11 /* Length of the version string */ -#define MIN_PORT 10000 -#define MAX_PORT 65535 +#define MIN_PORT 10000 +#define MAX_PORT 65535 -#define ENC_KEYSIZE 16 /* RIJNDAEL Key Size */ +#define ENC_KEYSIZE 16 /* RIJNDAEL Key Size */ /* For random string generation. */ -#define RAND_VAL_SIZE 16 -#define RAND_FILE "/dev/urandom" -#define RAND_MASK 0xFFFF +#define RAND_VAL_SIZE 16 +#define RAND_FILE "/dev/urandom" +#define RAND_MASK 0xFFFF -#define TIMESTAMP_SIZE 10 +#define TIMESTAMP_SIZE 10 #define MAX_USER_SIZE 32 #define MAX_MESSAGE_SIZE 128 @@ -62,29 +62,32 @@ /* SPA Message types... */ enum { - SPA_COMMAND_MODE, - SPA_ACCESS_MODE, - SPA_NAT_ACCESS_MODE, - SPA_CLIENT_TIMEOUT_ACCESS_MODE, - SPA_CLIENT_TIMEOUT_NAT_ACCESS_MODE, - SPA_LOCAL_NAT_ACCESS_MODE, - SPA_CLIENT_TIMEOUT_LOCAL_NAT_ACCESS_MODE + SPA_COMMAND_MSG = 0, + SPA_ACCESS_MSG, + SPA_NAT_ACCESS_MSG, + SPA_CLIENT_TIMEOUT_ACCESS_MSG, + SPA_CLIENT_TIMEOUT_NAT_ACCESS_MSG, + SPA_LOCAL_NAT_ACCESS_MSG, + SPA_CLIENT_TIMEOUT_LOCAL_NAT_ACCESS_MSG, + LAST_MSG_TYPE /* Always leave this as the last one */ }; /* Digest types... */ enum { - MD5_DIGEST, + MD5_DIGEST = 0, SHA1_DIGEST, SHA256_DIGEST }; /* General Defaults */ -#define DEFAULT_USER "root" -#define DEFAULT_PORT 62201 -#define DEFAULT_DIGEST SHA256_DIGEST -#define KNOCK_INTERVAL 60 +#define DEFAULT_USER "root" +#define DEFAULT_PORT 62201 +#define DEFAULT_DIGEST SHA256_DIGEST +#define DEFAULT_MSG_TYPE SPA_ACCESS_MSG +#define DEFAULT_CLIENT_TIMEOUT 0 +#define KNOCK_INTERVAL 60 /* The pieces we need to make a SPA packet. */ @@ -109,7 +112,7 @@ char* spa_random_number(spa_message_t *sm); char* spa_user(spa_message_t *sm, char *spoof_user); unsigned int spa_timestamp(spa_message_t *sm, int offset); char* spa_version(spa_message_t *sm); -unsigned short spa_message_type(spa_message_t *sm); +int spa_message_type(spa_message_t *sm, unsigned short msg_type); char* spa_message(spa_message_t *sm); char* spa_nat_access(spa_message_t *sm); char* spa_server_auth(spa_message_t *sm); diff --git a/spa_message_type.c b/spa_message_type.c new file mode 100644 index 00000000..db8c1745 --- /dev/null +++ b/spa_message_type.c @@ -0,0 +1,41 @@ +/* $Id$ + ***************************************************************************** + * + * File: spa_message_type.c + * + * Author: Damien S. Stuart + * + * Purpose: Set the current fwknop message type. + * + * Copyright (C) 2008 Damien Stuart (dstuart@dstuart.org) + * + * License (GNU Public License): + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 + * USA + * + ***************************************************************************** +*/ +#include "fwknop.h" + +int spa_message_type(spa_message_t *sm, unsigned short msg_type) +{ + if(msg_type >= LAST_MSG_TYPE) + { + fprintf(stderr, "*Invlaid fwknop message type: %u.\n", msg_type); + return(-1); + } + + sm->message_type = msg_type; + + return(0); +} + +/***EOF***/ diff --git a/spa_user.c b/spa_user.c index 1f7df17e..a8a45d6e 100644 --- a/spa_user.c +++ b/spa_user.c @@ -55,7 +55,7 @@ char* spa_user(spa_message_t *sm, char *spoof_user) */ if(res < 1) if((strlcpy(sm->user, getenv("LOGNAME"), MAX_USER_SIZE)) < 1) - if((strlcpy(sm->user, getenv("USER"), MAX_USER_SIZE)) < 1); + if((strlcpy(sm->user, getenv("USER"), MAX_USER_SIZE)) < 1) strlcpy(sm->user, DEFAULT_USER, strlen(DEFAULT_USER) + 1); } diff --git a/spa_version.c b/spa_version.c new file mode 100644 index 00000000..2da37c0a --- /dev/null +++ b/spa_version.c @@ -0,0 +1,34 @@ +/* $Id$ + ***************************************************************************** + * + * File: spa_version.c + * + * Author: Damien S. Stuart + * + * Purpose: Get the current fwknop version. + * + * Copyright (C) 2008 Damien Stuart (dstuart@dstuart.org) + * + * License (GNU Public License): + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 + * USA + * + ***************************************************************************** +*/ +#include "fwknop.h" + +char* spa_version(spa_message_t *sm) +{ + strlcpy(sm->version, FWKNOP_VERSION, strlen(FWKNOP_VERSION) + 1); + return(sm->version); +} + +/***EOF***/