Skip to content

Commit

Permalink
Merge pull request #21 from hackerschoice/filexfer
Browse files Browse the repository at this point in the history
Filetransfer
  • Loading branch information
rootTHC authored Feb 18, 2021
2 parents 44f3981 + aa4ecf1 commit d67bf4d
Show file tree
Hide file tree
Showing 52 changed files with 5,415 additions and 464 deletions.
2 changes: 1 addition & 1 deletion Makefile.am
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
SUBDIRS = lib tools man
EXTRA_DIST = README.md config bootstrap include/gsocket LICENSE
EXTRA_DIST = README.md config bootstrap tests/Makefile tests/run_all_tests.sh tests/run_gs_tests.sh tests/run_ft_tests.sh include/gsocket LICENSE

2 changes: 1 addition & 1 deletion bootstrap
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ if test $? -ne 0; then
exit 1
fi
echo "automake --foreign --add-missing -Wno-syntax"
automake --foreign --add-missing -Wno-syntax
automake --foreign --copy --add-missing -Wno-syntax
if test $? -ne 0; then
exit 1
fi
Expand Down
92 changes: 60 additions & 32 deletions configure.ac
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
dnl Process this File with autoconf to produce a configure script.
AC_PREREQ(2.61)
AC_INIT([gsocket], 1.4.23)
AC_INIT([gsocket], 1.4.23-ft3)
dnl AC_CONFIG_AUX_DIR(config-x86_64-apple-darwin19.6.0)
AC_CONFIG_AUX_DIR(config)
AC_CANONICAL_SYSTEM
Expand Down Expand Up @@ -49,7 +49,7 @@ for xincdir in $trydir_i ; do
INCLUDES="$INCLUDES -I${xincdir}";
fi
done
CPPFLAGS="${INCLUDES} $CPPFLAGS"
CPPFLAGS="-I../include ${INCLUDES} $CPPFLAGS"

dnl Try library paths...
trydir_l="${trydir_l} /usr/local/opt/openssl/lib"
Expand Down Expand Up @@ -80,21 +80,10 @@ mips-sony-bsd|mips-sony-newsos4)
;;
esac

AC_CHECK_LIB(util, forkpty)
dnl AC_CHECK_LIB(bsd, main)
AC_CHECK_LIB(socket, socket)
AC_CHECK_LIB(nsl, gethostbyname)
dnl AC_CHECK_LIB([m], [sqrt])
dnl AC_CHECK_LIB([net], [libnet_name_resolve], [AC_MSG_ERROR([libnet 1.0.x found. Requires libnet 1.1 or newer])])
dnl AC_CHECK_LIB([net], [libnet_init], ,[AC_MSG_ERROR([libnet 1.1.x not found])])
AC_CHECK_LIB(dl, dlopen)
AC_CHECK_LIB([crypto], [ENGINE_init], [], [AC_MSG_ERROR([libcrypto not found])])
AC_CHECK_LIB([ssl], [SRP_VBASE_get1_by_user], [], [AC_MSG_ERROR([SRP not supported. Please upgrade OpenSSL lib])])

dnl Checks for header files.
AC_HEADER_STDC
AC_HEADER_SYS_WAIT
AC_CHECK_HEADERS(sys/time.h unistd.h string.h utmp.h utmpx.h pty.h openssl/srp.h util.h libutil.h netinet/in_systm.h sys/loadavg.h)
AC_CHECK_HEADERS(sys/time.h sys/endian.h unistd.h fnmatch.h string.h utmp.h utmpx.h pty.h openssl/srp.h util.h libutil.h netinet/in_systm.h sys/loadavg.h libproc.h)

AC_CHECK_HEADER(openssl/srp.h, [], [AC_MSG_ERROR([openssl/srp.h not found. Update OpenSSL?])])

Expand All @@ -112,41 +101,65 @@ AC_TYPE_SIZE_T
dnl If uid_t is not defined, define uid_t to be int and gid_t to be int.
AC_TYPE_UID_T

AC_ARG_ENABLE(static,
[ --enable-static Compile static binary],
[STATIC="yes"], [STATIC="no"]
)

if test x"$STATIC" = x"yes"; then
dnl static OpenSSL lib requires pthread
dnl AC_CHECK_LIB(pthread, pthread_once)
dnl LDADD_STATIC="" dnl -static"
CFLAGS_STATIC="-static "
fi

AC_CHECK_LIB(util, forkpty)
AC_CHECK_LIB(socket, socket)
AC_CHECK_LIB(nsl, gethostbyname)
dnl AC_CHECK_LIB([net], [libnet_name_resolve], [AC_MSG_ERROR([libnet 1.0.x found. Requires libnet 1.1 or newer])])
dnl AC_CHECK_LIB([net], [libnet_init], ,[AC_MSG_ERROR([libnet 1.1.x not found])])
if test x"$STATIC" = xno; then
AC_CHECK_LIB(dl, dlopen)
fi
AC_CHECK_LIB(procstat, procstat_close)
AC_CHECK_LIB([crypto], [ENGINE_init], [], [AC_MSG_ERROR([libcrypto not found])])
AC_CHECK_LIB([ssl], [SRP_VBASE_get1_by_user], [], [AC_MSG_ERROR([SRP not supported. Please upgrade OpenSSL lib])])

AC_CHECK_FUNCS(gettimeofday memcpy strchr strlcat forkpty openpty getline stat64 open64 statvfs64)

AC_ARG_ENABLE([31337],
AS_HELP_STRING([--enable-31337],
[Enable experimental features.]),
AS_HELP_STRING([--enable-31337], [Enable experimental features.]),
AC_DEFINE(D31337, 1, [Expermental feature])
)

AC_ARG_ENABLE(debug,
[ --enable-debug Enable debug information],
AC_DEFINE(DEBUG, 1, [Debug infos])
AC_ARG_ENABLE([debug],
AS_HELP_STRING([--enable-debug], [Enable debug information.]),
[debug=true AC_DEFINE(DEBUG, 1, [Debug infos])]
)

AC_ARG_ENABLE([tests],
AS_HELP_STRING([--enable-tests], [Enable self-tests.]),
[selftests=true]
)

AS_IF([test x$enable_debug = xyes], AC_DEFINE(D31337, 1, [Expermental feature]))
AS_IF([test x$enable_debug = xyes], [debug=true])
AS_IF([test x$enable_debug = xyes], [selftests=true])

AS_IF([test x$selftests = xtrue], AC_DEFINE(SELFTESTS, 1, [Self Tests]))

AC_ARG_ENABLE(dist,
[ --enable-dist Enable distribution mode, Use own libraries.],
[DIST="yes"], [DIST="no"]
)

AC_ARG_ENABLE(static,
[ --enable-static Compile static binary],
[STATIC="yes"], [STATIC="no"]
)

if test x"$STATIC" = x"yes"; then
CFLAGS="-static ${CFLAGS}"
fi

AS_IF([test x$debug = xtrue], AC_SUBST(PROGRAMS_TEST_LIB, "list-test event-test"))
AS_IF([test x$debug = xtrue], AC_SUBST(PROGRAMS_TEST_TOOLS, "packet-test readline-test console_display-test"))
AS_IF([test x$selftests = xtrue], AC_SUBST(PROGRAMS_TEST_LIB, "list-test${EXEEXT} event-test${EXEEXT}"))
AS_IF([test x$selftests = xtrue], AC_SUBST(PROGRAMS_TEST_TOOLS, "packet-test${EXEEXT} readline-test${EXEEXT} console_display-test${EXEEXT} filetransfer-test${EXEEXT}"))

AC_SUBST(LDADD_STATIC, "${LDADD_STATIC}")
AC_SUBST(CFLAGS_STATIC, "${CFLAGS_STATIC}")
AC_OUTPUT([Makefile lib/Makefile tools/Makefile man/Makefile])


echo "

\"If netcat is a swiss army knife then
Expand All @@ -155,12 +168,27 @@ echo "
--acpizer/United Cracking Force
"

if test x"${STATIC}" = xyes; then
echo "
********************************** WARNING ***********************************
* Your MUST compile OpenSSL like this: *
* openssl-src> *
* ./Configure --prefix=\$HOME/usr no-dso no-threads no-shared linux-generic64 *
* mkdir -p \$HOME/usr && make all install *
* Only then compile gsocket \(using the same --prefix=\): *
* gsocket-src> ./configure --prefix=\$HOME/usr --enable-static *
* gsocket-src> make all install *
* gsocket-src> export PATH=\$HOME/usr/bin:\$PATH *
******************************************************************************
"
fi

echo "
${PACKAGE_NAME}-${PACKAGE_VERSION} has been configured:

Host..............: ${host}
Compiler..........: ${CC}
Compiler flags....: ${CFLAGS}
Compiler flags....: ${CFLAGS_STATIC}${CFLAGS}
Preprocessor flags: ${CPPFLAGS}
Linker flags......: ${LDFLAGS}
Libraries.........: ${LIBS}
Expand Down
26 changes: 26 additions & 0 deletions include/gsocket/buf.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#ifndef __GS_BUF_H__
#define __GS_BUF_H__ 1


typedef struct
{
void *data;
size_t sz_total;
size_t sz_used;

size_t sz_max_add;
} GS_BUF;

void GS_BUF_init(GS_BUF *gsb, size_t sz_min_free);
void GS_BUF_free(GS_BUF *gsb);
int GS_BUF_resize(GS_BUF *gsb, size_t sz_new);
int GS_BUF_add(GS_BUF *gsb, size_t len);
int GS_BUF_add_data(GS_BUF *gsb, void *data, size_t len);
int GS_BUF_del(GS_BUF *gsb, size_t len);

#define GS_BUF_UNUSED(gsb) ((gsb)->sz_total - (gsb)->sz_used)
#define GS_BUF_RSRC(gsb) (gsb)->data
#define GS_BUF_WDST(gsb) ((uint8_t *)(gsb)->data + (gsb)->sz_used)
#define GS_BUF_USED(gsb) (gsb)->sz_used

#endif /* !__GS_BUF_H__ */
4 changes: 4 additions & 0 deletions include/gsocket/gsocket.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
#include <gsocket/gs-select.h>
#include <gsocket/packet.h>
#include <gsocket/gs-readline.h>
#include <gsocket/buf.h>

/* ###########################
* ### PROTOCOL DEFINITION ###
Expand Down Expand Up @@ -371,6 +372,9 @@ uint32_t GS_hton(const char *hostname);
void GS_SELECT_FD_SET_W(GS *gs);

void GS_daemonize(FILE *logfp);
uint64_t GS_usec(void);
void GS_format_bps(char *dst, size_t size, int64_t bytes, const char *suffix);
char *GS_getpidwd(pid_t pid);

const char *GS_gen_secret(void);
const char *GS_user_secret(GS_CTX *ctx, const char *file, const char *sec_str);
Expand Down
2 changes: 2 additions & 0 deletions include/gsocket/list.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,12 @@ typedef struct

int GS_LIST_init(GS_LIST *gsl, int opt);
GS_LIST_ITEM *GS_LIST_add(GS_LIST *gsl, GS_LIST_ITEM *src_li, void *data, uint64_t id);
void GS_LIST_move(GS_LIST *gsl, GS_LIST_ITEM *li);
int GS_LIST_del(GS_LIST_ITEM *li);
int GS_LIST_del_all(GS_LIST *gsl, int deep);
GS_LIST_ITEM *GS_LIST_next(GS_LIST *gsl, GS_LIST_ITEM *li);
GS_LIST_ITEM *GS_LIST_by_pos(GS_LIST *gsl, int pos);
GS_LIST_ITEM *GS_LIST_by_id(GS_LIST *gsl, uint64_t id);
void GS_LIST_relink(GS_LIST_ITEM *li, uint64_t id);


Expand Down
22 changes: 21 additions & 1 deletion include/gsocket/packet.h
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
#ifndef __GS_PACKET_H__
#define __GS_PACKET_H__ 1

#define GS_PKT_MAX_SIZE (2048)
#define GS_PKT_MAX_SIZE (2048) // content length without pkt-header (2 or 4 bytes)
#define GS_PKT_HDR_MAX_SIZE (4)
#define GS_PKT_MAX_MSG 128 // type = 0..127
#define GS_PKT_MAX_CHN 128 // type = 128..255
// #define GS_PKT_ESC 'e' // TESTING ONLY
#ifndef GS_PKT_ESC
# define GS_PKT_ESC 0xFB // escape character
#endif

#define GS_PKT_MSG_HDR_LEN (2)
#define GS_PKT_CHN_HDR_LEN (4)

typedef void (*gspkt_cb_t)(uint8_t type, const uint8_t *data, size_t len, void *arg);

/*
Expand All @@ -25,7 +30,22 @@ typedef struct
void *args[256];
} GS_PKT;

struct gs_pkt_msg_hdr
{
uint8_t esc;
uint8_t type;
} __attribute__((__packed__));

struct gs_pkt_chn_hdr
{
uint8_t esc;
uint8_t type;
uint16_t len;
} __attribute__((__packed__));


#define GS_PKT_IS_CHANNEL(a) (((a) >> 7) & 0x01)
#define GS_PKT_CHN2TYPE(a) (GS_PKT_MAX_MSG + a)

int GS_PKT_init(GS_PKT *pkt);
int GS_PKT_close(GS_PKT *pkt);
Expand Down
3 changes: 1 addition & 2 deletions lib/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ noinst_LIBRARIES = libgsocket.a
noinst_PROGRAMS = @PROGRAMS_TEST_LIB@
EXTRA_PROGRAMS = list-test event-test

libgsocket_a_SOURCES = gsocket-util.c gsocket-select.c gsocket-sha256.c gsocket-ssl.c gsocket-engine.c packet.c gs-readline.c list.c event.c
libgsocket_a_SOURCES = gsocket-util.c gsocket-select.c gsocket-sha256.c gsocket-ssl.c gsocket-engine.c packet.c gs-readline.c list.c event.c buf.c

list_test_SOURCES = list-test.c
list_test_LDADD = libgsocket.a
Expand All @@ -11,4 +11,3 @@ event_test_SOURCES = event-test.c
event_test_LDADD = libgsocket.a

noinst_HEADERS = gsocket-sha256.h gs-common.h gs-externs.h gsocket-engine.h
AM_CFLAGS = -I../include
80 changes: 80 additions & 0 deletions lib/buf.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* A FIFO like buffer to. Used by file transfer as a write-buffer to queue
* control messages (such as chn_accept, chn_error, ...).
*/
#include "gs-common.h"
#include <gsocket/gsocket.h>
#include "gs-externs.h"

void
GS_BUF_init(GS_BUF *gsb, size_t sz_max_add)
{
memset(gsb, 0, sizeof *gsb);
gsb->sz_max_add = sz_max_add;

// gsb->sz_total = 16*1024*1024; // FIXME
// gsb->data = malloc(gsb->sz_total); // FIXME

GS_BUF_resize(gsb, 0);
}

void
GS_BUF_free(GS_BUF *gsb)
{
XFREE(gsb->data);
memset(gsb, 0, sizeof *gsb);
}

// Adjust size to have at least sz_min_free available.
int
GS_BUF_resize(GS_BUF *gsb, size_t sz_new)
{
if (GS_BUF_UNUSED(gsb) >= sz_new + gsb->sz_max_add)
return 0;

gsb->sz_total = gsb->sz_used + sz_new + gsb->sz_max_add;
DEBUGF_R("realloc to %zu, used %zu\n", gsb->sz_total, gsb->sz_used);
gsb->data = realloc(gsb->data, gsb->sz_total);

return 0;
}

int
GS_BUF_add(GS_BUF *gsb, size_t len)
{
// Bail. There is sz_max_add space available but looks like caller wrote
// more ata...
XASSERT(len <= GS_BUF_UNUSED(gsb), "Not enough space in buffer\n");

gsb->sz_used += len;

// Resize
GS_BUF_resize(gsb, 0);

return 0;
}

int
GS_BUF_add_data(GS_BUF *gsb, void *data, size_t len)
{
GS_BUF_resize(gsb, len);
memcpy((uint8_t *)gsb->data + gsb->sz_used, data, len);

gsb->sz_used += len;

return 0;
}

/*
* Consume data from beginning.
*/
int
GS_BUF_del(GS_BUF *gsb, size_t len)
{
XASSERT(gsb->sz_used >= len, "Cant. used=%zu, len=%zu\n", gsb->sz_used, len);
gsb->sz_used -= len;
memmove(gsb->data, (uint8_t *)gsb->data + len, gsb->sz_used);

return 0;
}

18 changes: 17 additions & 1 deletion lib/gs-common.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,13 @@
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/wait.h>
#if defined(__FREEBSD__)
# include <sys/sysctl.h>
# include <sys/caprights.h>
# include <sys/param.h>
# include <sys/queue.h>
#endif // __FREEBSD__
#include <netinet/in.h>
#ifdef HAVE_NETINET_IN_SYSTM_H
# include <netinet/in_systm.h>
Expand All @@ -36,6 +41,17 @@
#include <inttypes.h>
#include <signal.h>
#include <libgen.h> /* basename() */
#if defined(__APPLE__) && defined(HAVE_LIBPROC_H)
# include <libproc.h> // getpidwd(pid_t)
#endif
#if defined(__FREEBSD__)
// FIXME: Please tell me where this is defined? fbsd 12-1 complains:
// /usr/include/libprocstat.h:122:15: error: field 'fs_cap_rights' has incomplete type
# ifndef cap_rights_t
typedef struct cap_rights cap_rights_t;
# endif
# include <libprocstat.h>
#endif
#include <openssl/srp.h>
#include <openssl/ssl.h>
#include <openssl/rand.h>
Expand Down
Loading

0 comments on commit d67bf4d

Please sign in to comment.