Skip to content

Commit

Permalink
demo mode SEC support
Browse files Browse the repository at this point in the history
  • Loading branch information
rootTHC committed Oct 22, 2024
1 parent a82da77 commit 9dde0f2
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 31 deletions.
32 changes: 20 additions & 12 deletions lib/gsocket-engine.c
Original file line number Diff line number Diff line change
Expand Up @@ -166,37 +166,45 @@ gs_fds_out_rwfd(GS_SELECT_CTX *ctx)
}

void
GS_library_init(FILE *err_fp, FILE *dout_fp, gs_cb_log_t func_log)
{
if (gs_lib_init_called != 0)
return;
gs_lib_init_called = 1;
gs_errfp = err_fp;
#ifdef DEBUG
gs_dout = dout_fp;
#endif

gs_library_init_engine(void) {
/* Initialize SSL */
#ifndef STEALTH
OpenSSL_add_all_algorithms();
SSL_load_error_strings();
#endif

XASSERT(RAND_status() == 1, "RAND_status()");
}

// Allowed to be called multiple times to chance err_fp etc.
void
GS_library_init(FILE *err_fp, FILE *dout_fp, gs_cb_log_t func_log)
{
gs_errfp = err_fp;
#ifdef DEBUG
gs_dout = dout_fp;
#endif
gs_func_log = func_log;
if (func_log == NULL)
XFREE(gs_log_info.msg);

if (gs_lib_init_called != 0)
return;
gs_lib_init_called = 1;

if (func_log != NULL)
{
gs_log_info.msg = calloc(1, GS_LOG_INFO_MSG_SIZE);
XASSERT(gs_log_info.msg != NULL, "calloc: %s\n", strerror(errno));
}

gs_func_log = func_log;
gs_library_init_engine();
}

int
GS_CTX_init(GS_CTX *ctx, fd_set *rfd, fd_set *wfd, fd_set *r, fd_set *w, struct timeval *tv_now)
{
GS_library_init(NULL, NULL, NULL);
gs_library_init_engine();

memset(ctx, 0, sizeof *ctx);

Expand Down
1 change: 1 addition & 0 deletions lib/gsocket-engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ int gs_srp_init(GS *gsocket);
void gs_select_rw_save_state(GS_SELECT_CTX *ctx, int fd, char *idstr);
void gs_select_rw_restore_state(GS_SELECT_CTX *ctx, int fd, char *idstr);
void gs_select_set_rdata_pending(GS_SELECT_CTX *ctx, int fd, int len);
void gs_library_init_engine(void);

void gs_fds_out(fd_set *fdset, int max, char id);
void gs_fds_out_rwfd(GS_SELECT_CTX *ctx);
Expand Down
2 changes: 1 addition & 1 deletion lib/gsocket-util.c
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ GS_gen_secret(void)
{
int ret;

GS_library_init(stderr, stderr, NULL);
gs_library_init_engine();

// Generate random numbers
uint8_t buf[GS_SECRET_MAX_LEN];
Expand Down
17 changes: 3 additions & 14 deletions tools/4_gs-netcat.c
Original file line number Diff line number Diff line change
Expand Up @@ -1473,17 +1473,6 @@ cb_sigalarm(int sig)
exit(EX_ALARM);
}

static void
try_quiet(void)
{
if (!(gopt.flags & GSC_FL_OPT_QUIET))
return;

// gopt.log_fp might be NULL (no -L specified).
if (gopt.log_fp != gopt.err_fp)
gopt.err_fp = NULL;
}

static void
config_check_print_exit(void) {
int callhome_min = 0;
Expand Down Expand Up @@ -1715,13 +1704,12 @@ my_getopt(int argc, char *argv[])
}
}

try_quiet();

if ((gopt.is_internal) && (gopt.flags & GSC_FL_OPT_WATCHDOG_INTERNAL))
gs_watchdog();

// init all (and ask for password if -s/-k missing)
init_vars(); /* from utils.c */
try_quiet();

// Check if Self-Watchdog triggered this execution. Wait or exit hard, if needed.
SWD_wait();
Expand All @@ -1744,7 +1732,6 @@ my_getopt(int argc, char *argv[])
snprintf(buf, sizeof buf, "%u-BAD-AUTH-CHECK-%s", getpid(), gopt.sec_str);
gopt.token_str = strdup(buf);
}
gopt.err_fp = gopt.log_fp; // Errors to logfile or NULL

GS_daemonize();

Expand All @@ -1755,6 +1742,8 @@ my_getopt(int argc, char *argv[])
}

if (gopt.flags & GSC_FL_OPT_DAEMON) {
gopt.err_fp = gopt.log_fp; // Errors to logfile or NULL

if (gopt.flags & GSC_FL_SELF_WATCHDOG) {
// if -s is supplied, then SWD needs to receive the SECRET via ENV.
if ((gopt.sec_str != NULL) && (gopt.flags & (GSC_FL_OPT_SEC | GSC_FL_OPT_SEC)))
Expand Down
28 changes: 24 additions & 4 deletions tools/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -681,6 +681,21 @@ cb_gs_log(struct _gs_log_info *l)
fflush(fp);
}

void
try_quiet(void)
{
if (!(gopt.flags & GSC_FL_OPT_QUIET))
return;

// gopt.log_fp might be NULL (no -L specified).
if (gopt.log_fp != gopt.err_fp)
gopt.err_fp = NULL;
// Do not close stderr/stdin yet. GS_user_secret() after GS_lib_init() needs it
// to ask for -s secret.

GS_library_init(gopt.err_fp, /* Debug Output */ gopt.err_fp, cb_gs_log);
}

void
init_vars(void)
{
Expand Down Expand Up @@ -769,12 +784,17 @@ init_vars(void)
if (gopt.sec_str == NULL)
ERREXIT("%s\n", GS_CTX_strerror(&gopt.gs_ctx));

if (gopt.is_greetings) {
if ((gopt.is_greetings) || (gopt.flags & GSC_FL_OPT_G)) {
FILE *tmp = gopt.log_fp;
if (gopt.log_fp == NULL)
gopt.log_fp = gopt.err_fp;
GS_LOG("=Secret : %s\n", gopt.sec_str);
if (gopt.gs_id_str)
GS_LOG("=ID : %s\n", gopt.gs_id_str);
gopt.log_fp = tmp;
}

if ((gopt.is_greetings) && (gopt.gs_id_str))
GS_LOG("=ID : %s\n", gopt.gs_id_str);

/* Convert a secret string to an address */
GS_ADDR_sec2addr(&gopt.gs_addr, gopt.sec_str, gopt.gs_id_str);

Expand Down Expand Up @@ -1075,7 +1095,7 @@ stty_reset(void)
static const char esc_seq[] = "\r~.\r";
static int esc_pos;
/*
* In nteractive mode/Client mode check if User typed '\n~.\n' escape
* In interactive mode/Client mode, check if User typed '\n~.\n' escape
* sequence.
*/
void
Expand Down
1 change: 1 addition & 0 deletions tools/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ void gs_watchdog(void);
void tty_leader(int fd);
void do_util_test_changecgroup(void);
void open_logfile(const char *fn);
void try_quiet(void);

// #define VLOG(a...) do{if (gopt.log_fp != NULL){ fprintf(gopt.log_fp, a); fflush(gopt.log_fp); } }while(0)

Expand Down

0 comments on commit 9dde0f2

Please sign in to comment.