aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortoni <matzeton@googlemail.com>2017-01-11 01:29:06 +0100
committertoni <matzeton@googlemail.com>2017-01-11 01:29:06 +0100
commitca95d761f5d97e677931145e29dd09ab09d01b43 (patch)
treecb492d16fc7435e6b1f0451e059e07af642de976
parentb7e4f6cb468a3d9f1bca1953cd44a52fcf392c1d (diff)
moved sources to https://github.com/lnslbrty/foo-scripts
-rw-r--r--aes.c411
-rw-r--r--asciihexer.c46
-rw-r--r--dummyshell.c618
-rw-r--r--suidcmd.c95
-rw-r--r--xidle.c45
5 files changed, 0 insertions, 1215 deletions
diff --git a/aes.c b/aes.c
deleted file mode 100644
index ba31b41..0000000
--- a/aes.c
+++ /dev/null
@@ -1,411 +0,0 @@
-// AES Implementation by X-N2O
-// Started: 15:41:35 - 18 Nov 2009
-// Finished: 20:03:59 - 21 Nov 2009
-// Logarithm, S-Box, and RCON tables are not hardcoded
-// Instead they are generated when the program starts
-// All of the code below is based from the AES specification
-// You can find it at http://csrc.nist.gov/publications/fips/fips197/fips-197.pdf
-// You may use this code as you wish, but do not remove this comment
-// This is only a proof of concept, and should not be considered as the most efficient implementation
-
-#include <stdlib.h>
-#include <string.h>
-#include <stdio.h>
-
-#define AES_RPOL 0x011b // reduction polynomial (x^8 + x^4 + x^3 + x + 1)
-#define AES_GEN 0x03 // gf(2^8) generator (x + 1)
-#define AES_SBOX_CC 0x63 // S-Box C constant
-
-#define KEY_128 (128/8)
-#define KEY_192 (192/8)
-#define KEY_256 (256/8)
-
-#define aes_mul(a, b) ((a)&&(b)?g_aes_ilogt[(g_aes_logt[(a)]+g_aes_logt[(b)])%0xff]:0)
-#define aes_inv(a) ((a)?g_aes_ilogt[0xff-g_aes_logt[(a)]]:0)
-
-unsigned char g_aes_logt[256], g_aes_ilogt[256];
-unsigned char g_aes_sbox[256], g_aes_isbox[256];
-
-typedef struct {
- unsigned char state[4][4];
- int kcol;
- size_t rounds;
- unsigned long keysched[0];
-} aes_ctx_t;
-
-void aes_init();
-aes_ctx_t *aes_alloc_ctx(unsigned char *key, size_t keyLen);
-inline unsigned long aes_subword(unsigned long w);
-inline unsigned long aes_rotword(unsigned long w);
-void aes_keyexpansion(aes_ctx_t *ctx);
-
-inline unsigned char aes_mul_manual(unsigned char a, unsigned char b); // use aes_mul instead
-
-void aes_subbytes(aes_ctx_t *ctx);
-void aes_shiftrows(aes_ctx_t *ctx);
-void aes_mixcolumns(aes_ctx_t *ctx);
-void aes_addroundkey(aes_ctx_t *ctx, int round);
-void aes_encrypt(aes_ctx_t *ctx, unsigned char input[16], unsigned char output[16]);
-
-void aes_invsubbytes(aes_ctx_t *ctx);
-void aes_invshiftrows(aes_ctx_t *ctx);
-void aes_invmixcolumns(aes_ctx_t *ctx);
-void aes_decrypt(aes_ctx_t *ctx, unsigned char input[16], unsigned char output[16]);
-
-void aes_free_ctx(aes_ctx_t *ctx);
-
-void init_aes()
-{
- int i;
- unsigned char gen;
-
- // build logarithm table and it's inverse
- gen = 1;
- for(i = 0; i < 0xff; i++) {
- g_aes_logt[gen] = i;
- g_aes_ilogt[i] = gen;
- gen = aes_mul_manual(gen, AES_GEN);
- }
-
- // build S-Box and it's inverse
- for(i = 0; i <= 0xff; i++) {
- char bi;
- unsigned char inv = aes_inv(i);
-
- g_aes_sbox[i] = 0;
- for(bi = 0; bi < 8; bi++) {
- // based on transformation 5.1
- // could also be done with a loop based on the matrix
- g_aes_sbox[i] |= ((inv & (1<<bi)?1:0)
- ^ (inv & (1 << ((bi+4) & 7))?1:0)
- ^ (inv & (1 << ((bi+5) & 7))?1:0)
- ^ (inv & (1 << ((bi+6) & 7))?1:0)
- ^ (inv & (1 << ((bi+7) & 7))?1:0)
- ^ (AES_SBOX_CC & (1 << bi)?1:0)
- ) << bi;
- }
- g_aes_isbox[g_aes_sbox[i]] = i;
- }
- // warning: quickhack
- g_aes_sbox[1] = 0x7c;
- g_aes_isbox[0x7c] = 1;
- g_aes_isbox[0x63] = 0;
-}
-
-aes_ctx_t *aes_alloc_ctx(unsigned char *key, size_t keyLen)
-{
- aes_ctx_t *ctx;
- size_t rounds;
- size_t ks_size;
-
- switch(keyLen) {
- case 16: // 128-bit key
- rounds = 10;
- break;
-
- case 24: // 192-bit key
- rounds = 12;
- break;
-
- case 32: // 256-bit key
- rounds = 14;
- break;
-
- default:
- return NULL;
- }
-
- ks_size = 4*(rounds+1)*sizeof(unsigned long);
- ctx = malloc(sizeof(aes_ctx_t)+ks_size);
- if(ctx) {
- ctx->rounds = rounds;
- ctx->kcol = keyLen/4;
- memcpy(ctx->keysched, key, keyLen);
- ctx->keysched[43] = 0;
- aes_keyexpansion(ctx);
- }
-
- return ctx;
-}
-
-inline unsigned long aes_subword(unsigned long w)
-{
- return g_aes_sbox[w & 0x000000ff] |
- (g_aes_sbox[(w & 0x0000ff00) >> 8] << 8) |
- (g_aes_sbox[(w & 0x00ff0000) >> 16] << 16) |
- (g_aes_sbox[(w & 0xff000000) >> 24] << 24);
-}
-
-inline unsigned long aes_rotword(unsigned long w)
-{
- // May seem a bit different from the spec
- // It was changed because unsigned long is represented with little-endian convention on x86
- // Should not depend on architecture, but this is only a POC
- return ((w & 0x000000ff) << 24) |
- ((w & 0x0000ff00) >> 8) |
- ((w & 0x00ff0000) >> 8) |
- ((w & 0xff000000) >> 8);
-}
-
-void aes_keyexpansion(aes_ctx_t *ctx)
-{
- unsigned long temp;
- unsigned long rcon;
- register int i;
-
- rcon = 0x00000001;
- for(i = ctx->kcol; i < (4*(ctx->rounds+1)); i++) {
- temp = ctx->keysched[i-1];
- if(!(i%ctx->kcol)) {
- temp = aes_subword(aes_rotword(temp)) ^ rcon;
- rcon = aes_mul(rcon, 2);
- } else if(ctx->kcol > 6 && i%ctx->kcol == 4)
- temp = aes_subword(temp);
- ctx->keysched[i] = ctx->keysched[i-ctx->kcol] ^ temp;
- }
-}
-
-inline unsigned char aes_mul_manual(unsigned char a, unsigned char b)
-{
- register unsigned short ac;
- register unsigned char ret;
-
- ac = a;
- ret = 0;
- while(b) {
- if(b & 0x01)
- ret ^= ac;
- ac <<= 1;
- b >>= 1;
- if(ac & 0x0100)
- ac ^= AES_RPOL;
- }
-
- return ret;
-}
-
-void aes_subbytes(aes_ctx_t *ctx)
-{
- int i;
-
- for(i = 0; i < 16; i++) {
- int x, y;
-
- x = i & 0x03;
- y = i >> 2;
- ctx->state[x][y] = g_aes_sbox[ctx->state[x][y]];
- }
-}
-
-void aes_shiftrows(aes_ctx_t *ctx)
-{
- unsigned char nstate[4][4];
- int i;
-
- for(i = 0; i < 16; i++) {
- int x, y;
-
- x = i & 0x03;
- y = i >> 2;
- nstate[x][y] = ctx->state[x][(y+x) & 0x03];
- }
-
- memcpy(ctx->state, nstate, sizeof(ctx->state));
-}
-
-void aes_mixcolumns(aes_ctx_t *ctx)
-{
- unsigned char nstate[4][4];
- int i;
-
- for(i = 0; i < 4; i++) {
- nstate[0][i] = aes_mul(0x02, ctx->state[0][i]) ^
- aes_mul(0x03, ctx->state[1][i]) ^
- ctx->state[2][i] ^
- ctx->state[3][i];
- nstate[1][i] = ctx->state[0][i] ^
- aes_mul(0x02, ctx->state[1][i]) ^
- aes_mul(0x03, ctx->state[2][i]) ^
- ctx->state[3][i];
- nstate[2][i] = ctx->state[0][i] ^
- ctx->state[1][i] ^
- aes_mul(0x02, ctx->state[2][i]) ^
- aes_mul(0x03, ctx->state[3][i]);
- nstate[3][i] = aes_mul(0x03, ctx->state[0][i]) ^
- ctx->state[1][i] ^
- ctx->state[2][i] ^
- aes_mul(0x02, ctx->state[3][i]);
- }
-
- memcpy(ctx->state, nstate, sizeof(ctx->state));
-}
-
-void aes_addroundkey(aes_ctx_t *ctx, int round)
-{
- int i;
-
- for(i = 0; i < 16; i++) {
- int x, y;
-
- x = i & 0x03;
- y = i >> 2;
- ctx->state[x][y] = ctx->state[x][y] ^
- ((ctx->keysched[round*4+y] & (0xff << (x*8))) >> (x*8));
- }
-}
-
-void aes_encrypt(aes_ctx_t *ctx, unsigned char input[16], unsigned char output[16])
-{
- int i;
-
- // copy input to state
- for(i = 0; i < 16; i++)
- ctx->state[i & 0x03][i >> 2] = input[i];
-
- aes_addroundkey(ctx, 0);
-
- for(i = 1; i < ctx->rounds; i++) {
- aes_subbytes(ctx);
- aes_shiftrows(ctx);
- aes_mixcolumns(ctx);
- aes_addroundkey(ctx, i);
- }
-
- aes_subbytes(ctx);
- aes_shiftrows(ctx);
- aes_addroundkey(ctx, ctx->rounds);
-
- // copy state to output
- for(i = 0; i < 16; i++)
- output[i] = ctx->state[i & 0x03][i >> 2];
-}
-
-void aes_invshiftrows(aes_ctx_t *ctx)
-{
- unsigned char nstate[4][4];
- int i;
-
- for(i = 0; i < 16; i++) {
- int x, y;
-
- x = i & 0x03;
- y = i >> 2;
- nstate[x][(y+x) & 0x03] = ctx->state[x][y];
- }
-
- memcpy(ctx->state, nstate, sizeof(ctx->state));
-}
-
-void aes_invsubbytes(aes_ctx_t *ctx)
-{
- int i;
-
- for(i = 0; i < 16; i++) {
- int x, y;
-
- x = i & 0x03;
- y = i >> 2;
- ctx->state[x][y] = g_aes_isbox[ctx->state[x][y]];
- }
-}
-
-void aes_invmixcolumns(aes_ctx_t *ctx)
-{
- unsigned char nstate[4][4];
- int i;
-
- for(i = 0; i < 4; i++) {
- nstate[0][i] = aes_mul(0x0e, ctx->state[0][i]) ^
- aes_mul(0x0b, ctx->state[1][i]) ^
- aes_mul(0x0d, ctx->state[2][i]) ^
- aes_mul(0x09, ctx->state[3][i]);
- nstate[1][i] = aes_mul(0x09, ctx->state[0][i]) ^
- aes_mul(0x0e, ctx->state[1][i]) ^
- aes_mul(0x0b, ctx->state[2][i]) ^
- aes_mul(0x0d, ctx->state[3][i]);
- nstate[2][i] = aes_mul(0x0d, ctx->state[0][i]) ^
- aes_mul(0x09, ctx->state[1][i]) ^
- aes_mul(0x0e, ctx->state[2][i]) ^
- aes_mul(0x0b, ctx->state[3][i]);
- nstate[3][i] = aes_mul(0x0b, ctx->state[0][i]) ^
- aes_mul(0x0d, ctx->state[1][i]) ^
- aes_mul(0x09, ctx->state[2][i]) ^
- aes_mul(0x0e, ctx->state[3][i]);
- }
-
- memcpy(ctx->state, nstate, sizeof(ctx->state));
-}
-
-void aes_decrypt(aes_ctx_t *ctx, unsigned char input[16], unsigned char output[16])
-{
- int i;
-
- // copy input to state
- for(i = 0; i < 16; i++)
- ctx->state[i & 0x03][i >> 2] = input[i];
-
- aes_addroundkey(ctx, ctx->rounds);
- for(i = ctx->rounds-1; i >= 1; i--) {
- aes_invshiftrows(ctx);
- aes_invsubbytes(ctx);
- aes_addroundkey(ctx, i);
- aes_invmixcolumns(ctx);
- }
-
- aes_invshiftrows(ctx);
- aes_invsubbytes(ctx);
- aes_addroundkey(ctx, 0);
-
- // copy state to output
- for(i = 0; i < 16; i++)
- output[i] = ctx->state[i & 0x03][i >> 2];
-}
-
-void aes_free_ctx(aes_ctx_t *ctx)
-{
- free(ctx);
-}
-
-#define PRINT_BYTES(bPtr, siz, offset) { int _bPtr_idx; for (_bPtr_idx = offset; _bPtr_idx < offset+siz; _bPtr_idx++) { printf("%02X ", bPtr[_bPtr_idx]); } printf("\n"); }
-int main(int argc, char *argv[])
-{
- if (argc != 3) {
- fprintf(stderr, "usage: %s [KEY] [MSG]\n", argv[0]);
- return 1;
- }
-
- size_t klen = strlen(argv[1]);
- unsigned char key[KEY_128];
- memcpy(key, argv[1], klen);
-
- size_t plen = strlen(argv[2]);
- unsigned char ptext[plen+1];
- memcpy(ptext, argv[2], plen);
- ptext[plen] = '\0';
-
- unsigned char ctext[plen+1];
- memset(ctext, '\0', plen+1);
- unsigned char decptext[plen+1];
- memset(decptext, '\0', plen+1);
-
- aes_ctx_t *ctx;
-
- init_aes();
- ctx = aes_alloc_ctx(key, sizeof(key));
- if(!ctx) {
- perror("aes_alloc_ctx");
- return EXIT_FAILURE;
- }
- printf("Encrypted[HEX]..: ");
- aes_encrypt(ctx, ptext, ctext);
- PRINT_BYTES(ctext, sizeof(ctext), 0);
- aes_decrypt(ctx, ctext, decptext);
- printf("Decrypted[HEX]..: ");
- PRINT_BYTES(decptext, sizeof(decptext), 0);
- printf("Decrypted[ASCII]: ");
- puts((char*)decptext);
-
- aes_free_ctx(ctx);
- return EXIT_SUCCESS;
-}
-
diff --git a/asciihexer.c b/asciihexer.c
deleted file mode 100644
index afa1cd0..0000000
--- a/asciihexer.c
+++ /dev/null
@@ -1,46 +0,0 @@
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-
-#define ASCII_HEXLEN 3
-
-
-static void defaultHexOut(char *text) {
- int i;
- size_t len;
-
- for (i = 0; i < (len = strlen(text)); i++) {
- printf("0x%X%c", text[i], (i == len-1 ? '\n' : ' '));
- }
-}
-
-static void dwordHexOut(char *text) {
- int i;
- size_t len;
-
- for (i = 0; i < (len = strlen(text)); i++) {
- printf("%s%X%s", (i % 4 == 0 ? (i == 0 ? "0x" : " 0x") : ""), text[i], (i == len-1 ? "\n" : ""));
- }
-}
-
-static void strHexOut(char *text) {
- int i;
- size_t len;
-
- for (i = 0; i < (len = strlen(text)); i++) {
- printf("%s%X%s", (i == 0 ? "0x" : ""), text[i], (i == len-1 ? "\n" : ""));
- }
-}
-
-int main(int argc, char **argv)
-{
- if (argc != 2) {
- fprintf(stderr, "usage: %s [TEXT]\n", argv[0]);
- return 1;
- }
-
- defaultHexOut(argv[1]);
- dwordHexOut(argv[1]);
- strHexOut(argv[1]);
- return 0;
-}
diff --git a/dummyshell.c b/dummyshell.c
deleted file mode 100644
index 76bc12b..0000000
--- a/dummyshell.c
+++ /dev/null
@@ -1,618 +0,0 @@
-/*
- * build with: gcc -Wall -O2 -D_GNU_SOURCE=1 -D_HAS_CMD=1 -D_HAS_MSG=1 -D_HAS_HOSTENT=1 -D_HAS_SIGNAL=1 -D_HAS_UTMP=1 -D_HAS_SYSINFO -ffunction-sections -fdata-sections -ffast-math -fomit-frame-pointer dummyshell.c -o dummyshell
- * strip -s dummyshell
- */
-
-#include <stdio.h>
-#include <stdlib.h>
-#include <unistd.h>
-#include <termios.h>
-#include <fcntl.h>
-#include <time.h>
-#include <ctype.h> /* isprint(...) */
-#include <sys/ioctl.h> /* ioctl(...) */
-#include <stdint.h> /* UINT8_MAX */
-#include <string.h> /* memset(...) */
-#include <sys/types.h>
-#include <pwd.h> /* getpwuid(...) */
-#ifdef _HAS_CMD
-#include <sys/wait.h>
-#else
-#warning "COMMANDS(_HAS_CMD) disabled!"
-#endif
-#ifdef _HAS_MSG
-#include <errno.h>
-#include <string.h>
-#else
-#warning "MESSAGE(_HAS_MSG) disabled!"
-#endif
-#ifdef _HAS_HOSTENT
-#include <netdb.h>
-#include <sys/socket.h>
-#include <netinet/in.h>
-#include <arpa/inet.h>
-#else
-#warning "HOSTENT(_HAS_HOSTENT) disabled!"
-#endif
-#ifdef _HAS_SIGNAL
-#include <signal.h> /* signal(...) */
-#else
-#warning "SIGNAL(_HAS_SIGNAL) disabled!"
-#endif
-#ifdef _HAS_UTMP
-#include <utmp.h> /* utmp structure */
-#else
-#warning "UTMP(_HAS_UTMP) disabled!"
-#endif
-#ifdef _HAS_SYSINFO
-#include "sys/types.h" /* sysinfo structture */
-#include "sys/sysinfo.h" /* sysinfo(...) */
-#else
-#warning "SYSINFO(_HAS_SYSINFO) disabled!"
-#endif
-
-/* for print_memusage() and print cpuusage() see: http://stackoverflow.com/questions/63166/how-to-determine-cpu-and-memory-consumption-from-inside-a-process */
-
-
-static const char keymsg[] = " ['q'-EXIT | 'm'-MESSAGE | 'c'-CMDS] ";
-static const char txtheader[] =
- "**************\n"
- "* dummyshell *\n"
- "**************\n"
- "!(C) by Toni Uhlig\n"
- "@see: https://raw.githubusercontent.com/lnslbrty/foo-scripts/master/dummyshell.c\n";
-
-static volatile unsigned char doLoop = 1;
-
-
-static void printQuitLoop(void) {
- printf("quit in 3 .. ");
- fflush(stdout);
- sleep(1);
- printf("2 .. ");
- fflush(stdout);
- sleep(1);
- printf("1 .. ");
- fflush(stdout);
- sleep(1);
- printf("\n");
- doLoop = 0;
-}
-
-#define I_CLEARBUF 0x1
-static char readInput(char* buf, size_t* siz, size_t szMax, char key, int flags) {
- if (flags & I_CLEARBUF) {
- memset(&buf[0], '\0', szMax);
- *siz = 0;
- } else switch (key) {
- case '\n':
- break;
- case 127:
- if (*siz > 0)
- buf[--(*siz)] = '\0';
- break;
- case EOF: break;
- default:
- if (isprint(key) && *siz < szMax)
- buf[(*siz)++] = key;
- break;
- }
- return key;
-}
-
-#ifdef _HAS_CMD
-struct __attribute__((__packed__)) cmd {
- char* name;
- char* path;
- char* defargs;
-};
-
-static struct cmd cmds[] = {
- { "ether-wake", "/var/media/ftp/bin/suid-ether-wake", "-b -i lan" },
- { "echo", "/bin/echo", NULL },
- { NULL, NULL, NULL }
-};
-
-static void print_cmds(void)
-{
- size_t idx = 0;
- printf("\33[2K\r[COMMANDS]\n");
- while ( cmds[idx++].path != NULL ) {
- printf(" [%lu] %s\n", (unsigned long int)idx-1, ( cmds[idx-1].name != NULL ? cmds[idx-1].name : "unknown" ));
- }
-}
-
-int safe_exec(const char* cmdWithArgs)
-{
- pid_t child;
- if ( (child = fork()) == 0 ) {
- size_t szCur = 0, szMax = 10;
- char** args = calloc(szMax, sizeof(char**));
- const char* cmd = NULL;
-
- const char* prv = cmdWithArgs;
- const char* cur = NULL;
- while ( (cur = strchr(prv, ' ')) ) {
- if (cmd == NULL)
- cmd = strndup(prv, cur-prv);
-
- args[szCur++] = strndup(prv, cur-prv);
- if (szCur >= szMax) {
- szMax *= 2;
- args = realloc(args, sizeof(char**)*szMax);
- }
-
- cur++;
- prv = cur;
- }
- if (cmd == NULL) {
- cmd = cmdWithArgs;
- } else {
- args[szCur++] = strndup(prv, cur-prv);
- }
- args[szCur] = NULL;
- execv(cmd, args);
- exit(-5);
- } else if (child != -1) {
- int retval = 0;
- waitpid(child, &retval, 0);
- return retval;
- }
- return -6;
-}
-
-static int exec_cmd(size_t i, char* args, size_t szArgs)
-{
- size_t idx = (size_t)-1;
- while ( cmds[++idx].path != NULL ) {
- if (idx == i) {
- size_t siz = strlen(cmds[idx].path)+szArgs+1;
- char execbuf[siz+1];
- memset(&execbuf[0], '\0', siz+1);
- snprintf(&execbuf[0], siz+1, "%s %s", cmds[idx].path, args);
- return safe_exec(&execbuf[0]);
- }
- }
- return -7;
-}
-#endif
-
-#ifdef _HAS_MSG
-#define MSGFILE "/tmp/dummyshell.msg"
-#define STRLEN(str) (sizeof(str)/sizeof(str[0]))
-static int msgfd = -1;
-static int init_msg(void)
-{
- msgfd = open(MSGFILE, O_RDWR | O_CREAT | O_APPEND | O_DSYNC | O_RSYNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
- if (msgfd < 0) {
- fprintf(stderr, "fopen(\"%s\") with write access: %s\n", MSGFILE, strerror(errno));
- msgfd = open(MSGFILE, O_RDONLY | O_CREAT | O_APPEND | O_DSYNC | O_RSYNC);
- if (msgfd < 0) {
- fprintf(stderr, "fopen(\"%s\") readonly: %s\n", MSGFILE, strerror(errno));
- return -1;
- }
- }
- return 0;
-}
-
-struct __attribute__((__packed__)) msgHdr {
- uint8_t szFrom;
- uint8_t szMsg;
- time_t timestamp;
-};
-
-struct __attribute__((__packed__)) msg {
- char* from;
- char* msg;
-};
-
-static int read_msg(struct msgHdr* hdr, struct msg* msg)
-{
- if (msgfd < 0) return -1;
- int ok = 1;
- size_t rb = 0;
- if ( (rb = read(msgfd, hdr, sizeof(struct msgHdr)*1)) == sizeof(struct msgHdr)*1 ) {
- msg->from = calloc(hdr->szFrom+1, sizeof(char));
- msg->msg = calloc(hdr->szMsg+1, sizeof(char));
- if ( (rb = read(msgfd, &(msg->from[0]), sizeof(char)*hdr->szFrom)) != sizeof(char)*hdr->szFrom )
- ok = 0;
- if ( (rb = read(msgfd, &(msg->msg[0]), sizeof(char)*hdr->szMsg)) != sizeof(char)*hdr->szMsg )
- ok = 0;
- char newline = 0;
- if ( (rb = read(msgfd, &newline, sizeof(char)*1)) != sizeof(char)*1 )
- ok = 0;
- if (!ok || newline != '\n') {
- free(msg->from);
- free(msg->msg);
- msg->from = NULL;
- msg->msg = NULL;
- return -1;
- }
- return 0;
- }
- return -1;
-}
-
-static int print_msg(void) {
- struct msgHdr hdr;
- struct msg msg;
- memset(&hdr, '\0', sizeof(struct msgHdr));
- memset(&msg, '\0', sizeof(struct msg));
- if (read_msg(&hdr, &msg) == 0) {
- struct tm localtime;
- struct passwd* pwd = NULL;
- unsigned long int uid = strtoul(msg.from, NULL, 10);
- if ( (pwd = getpwuid((uid_t)uid)) ) {
- free(msg.from);
- msg.from = strdup(pwd->pw_name);
- }
- if (localtime_r(&hdr.timestamp, &localtime) != NULL) {
- printf("\33[2K\r[%02d-%02d-%04d %02d:%02d:%02d] Message from %s: %s\n", localtime.tm_mday, localtime.tm_mon+1, 1900+localtime.tm_year, localtime.tm_hour, localtime.tm_min, localtime.tm_sec, msg.from, msg.msg);
- } else {
- printf("\33[2K\r\aMessage from %s: %s\n", msg.from, msg.msg);
- }
- free(msg.from);
- free(msg.msg);
- return 0;
- }
- return -1;
-}
-
-static int write_msg(char* msg) {
- char from[6];
- memset(&from[0], '\0', STRLEN(from));
- if (snprintf(&from[0], STRLEN(from), "%u", getuid()) > 0) {
- struct msgHdr hdr;
- hdr.szFrom = strnlen(from, STRLEN(from));
- hdr.szMsg = strnlen(msg, UINT8_MAX);
- hdr.timestamp = time(NULL);
- char* buf = calloc(sizeof(hdr) + hdr.szFrom + hdr.szMsg + 2, sizeof(char));
- if (buf) {
- memcpy(buf, &hdr, sizeof(hdr));
- memcpy(buf+sizeof(hdr), from, hdr.szFrom);
- memcpy(buf+sizeof(hdr)+hdr.szFrom, msg, hdr.szMsg);
- *(buf + sizeof(hdr) + hdr.szFrom + hdr.szMsg) = '\n';
- int failed = 1;
- if ( write(msgfd, buf, sizeof(char)*(sizeof(hdr)+hdr.szFrom+hdr.szMsg+1)) == sizeof(char)*(sizeof(hdr)+hdr.szFrom+hdr.szMsg+1) )
- failed = 0;
- free(buf);
- return failed;
- }
- }
- return -1;
-}
-#endif
-
-#ifdef _HAS_HOSTENT
-#define ARP_STRING_LEN 1024
-#define ARP_IP_LEN 32
-#define XSTR(s) STR(s)
-#define STR(s) #s
-static void print_nethost(void)
-{
- FILE *arpCache = fopen("/proc/net/arp", "r");
- if (arpCache != NULL) {
- char arpline[ARP_STRING_LEN+1];
- memset(&arpline[0], '\0', ARP_STRING_LEN+1);
- if (fgets(arpline, ARP_STRING_LEN, arpCache)) {
- char arpip[ARP_IP_LEN+1];
- memset(&arpip[0], '\0', ARP_IP_LEN);
- const char nonline[] = "\33[2K\rhost online...: ";
- size_t i = 0;
- while (1 == fscanf(arpCache, "%" XSTR(ARP_IP_LEN) "s %*s %*s %*s %*s %*s", &arpip[0])) {
- struct in_addr ip;
- struct hostent *hp = NULL;
- if (inet_aton(&arpip[0], &ip)) {
- hp = gethostbyaddr((const void *)&ip, sizeof ip, AF_INET);
- }
- char *herrmsg = NULL;
- if (hp == NULL) {
- switch (h_errno) {
- case HOST_NOT_FOUND: herrmsg = "HOST UNKNOWN"; break;
- case NO_ADDRESS: herrmsg = "IP UNKNOWN"; break;
- case NO_RECOVERY: herrmsg = "SERVER ERROR"; break;
- case TRY_AGAIN: herrmsg = "TEMPORARY ERROR"; break;
- }
- }
- printf("%s[%lu] %.*s aka %s\n", nonline, (long unsigned int)++i, ARP_IP_LEN, arpip, (hp != NULL ? hp->h_name : herrmsg));
- memset(&arpip[0], '\0', ARP_IP_LEN);
- }
- }
- fclose(arpCache);
- }
-}
-#endif
-
-#ifdef _HAS_UTMP
-#ifndef _GNU_SOURCE
-size_t
-strnlen(const char *str, size_t maxlen)
-{
- const char *cp;
- for (cp = str; maxlen != 0 && *cp != '\0'; cp++, maxlen--);
- return (size_t)(cp - str);
-}
-#endif
-
-static void print_utmp(void)
-{
- int utmpfd = open("/var/run/utmp", O_RDONLY);
- if (utmpfd >= 0) {
- struct utmp ut;
- memset(&ut, '\0', sizeof(struct utmp));
- const char uonline[] = "\33[2K\ruser online...: ";
- size_t i = 0;
- while ( read(utmpfd, &ut, sizeof(struct utmp)) == sizeof(struct utmp) && strnlen(ut.ut_user, UT_NAMESIZE) > 0 ) {
- if (strnlen(ut.ut_host, UT_HOSTSIZE) > 0) {
- printf("%s[%lu] %.*s from %.*s\n", uonline, (long unsigned int)++i, UT_NAMESIZE, ut.ut_user, UT_HOSTSIZE, ut.ut_host);
- } else {
- printf("%s[%lu] %.*s\n", uonline, (long unsigned int)++i, UT_NAMESIZE, ut.ut_user);
- }
- }
- }
-}
-#endif
-
-#ifdef _HAS_SYSINFO
-static unsigned long long lastTotalUser, lastTotalUserLow, lastTotalSys, lastTotalIdle;
-
-static void init_cpuusage(){
- FILE* file = fopen("/proc/stat", "r");
- if (file) {
- fscanf(file, "cpu %llu %llu %llu %llu", &lastTotalUser, &lastTotalUserLow,
- &lastTotalSys, &lastTotalIdle);
- fclose(file);
- }
-}
-
-static void print_cpuusage(){
- double percent;
- FILE* file;
- unsigned long long totalUser, totalUserLow, totalSys, totalIdle, total;
-
- file = fopen("/proc/stat", "r");
- fscanf(file, "cpu %llu %llu %llu %llu", &totalUser, &totalUserLow,
- &totalSys, &totalIdle);
- fclose(file);
-
- if (totalUser < lastTotalUser || totalUserLow < lastTotalUserLow ||
- totalSys < lastTotalSys || totalIdle < lastTotalIdle){
- //Overflow detection. Just skip this value.
- percent = -1.0;
- } else{
- total = (totalUser - lastTotalUser) + (totalUserLow - lastTotalUserLow) +
- (totalSys - lastTotalSys);
- percent = total;
- total += (totalIdle - lastTotalIdle);
- percent /= total;
- percent *= 100;
- }
-
- lastTotalUser = totalUser;
- lastTotalUserLow = totalUserLow;
- lastTotalSys = totalSys;
- lastTotalIdle = totalIdle;
-
- printf("CPU...........: %.02f%%\n", percent);
-}
-
-static void print_memusage(void)
-{
- struct sysinfo meminfo;
- memset(&meminfo, '\0', sizeof(struct sysinfo));
- if (sysinfo(&meminfo) == 0) {
- unsigned long long totalvmem = meminfo.totalram;
- totalvmem += meminfo.totalswap;
- totalvmem *= meminfo.mem_unit;
- unsigned long long usedvmem = meminfo.totalram - meminfo.freeram;
- usedvmem += meminfo.totalswap - meminfo.freeswap;
- usedvmem *= meminfo.mem_unit;
- printf("VMEM(used/max): %llu/%lld (Mb)\n", (usedvmem/(1024*1024)), (totalvmem/(1024*1024)));
- }
-}
-#endif
-
-#ifdef _HAS_SIGNAL
-void SigIntHandler(int signum)
-{
- if (signum == SIGINT) {
- doLoop = 0;
- }
-}
-#endif
-
-enum mainState { MS_DEFAULT, MS_MESSAGE, MS_COMMAND };
-
-int main(int argc, char** argv)
-{
- enum mainState state = MS_DEFAULT;
- struct timeval tv;
- tv.tv_sec = 1;
- tv.tv_usec = 0;
-
-#ifdef _HAS_SIGNAL
- signal(SIGINT, SigIntHandler);
-#endif
- size_t inputsiz = 0, absiz = UINT8_MAX;
- struct winsize wsiz;
- ioctl(0, TIOCGWINSZ, &wsiz);
- if (wsiz.ws_col < absiz)
- absiz = wsiz.ws_col - 3;
- char inputbuf[absiz+1];
- memset(&inputbuf[0], '\0', absiz+1);
-#ifdef _HAS_MSG
- if (init_msg() != 0)
- return 1;
- if (argc > 1) {
- const char optRmsg[] = "readmsg";
- const char optWmsg[] = "writemsg";
- const char optNofollow[] = "-n";
- if (strncmp(argv[1], optRmsg, STRLEN(optRmsg)) == 0) {
- if (argc == 2) while (doLoop) {
- if (print_msg() != 0)
- sleep(1);
- } else if (argc == 3) {
- if (strncmp(argv[2], optNofollow, STRLEN(optNofollow)) == 0) {
- while (print_msg() == 0) {}
- } else {
- fprintf(stderr, "%s readmsg [-n]\n", argv[0]);
- return 1;
- }
- }
- return 0;
- } else if (strncmp(argv[1], optWmsg, STRLEN(optWmsg)) == 0) {
- if (argc == 3) {
- return write_msg(argv[2]);
- } else {
- fprintf(stderr, "%s writemsg msg\n", argv[0]);
- }
- return 0;
- } else {
- fprintf(stderr, "usage: %s [readmsg [-n] | writemsg msg]\n", argv[0]);
- return 1;
- }
- }
-#endif
-#ifdef _HAS_SYSINFO
- init_cpuusage();
-#endif
- int flags = fcntl(STDIN_FILENO, F_GETFL, 0);
- fcntl(STDIN_FILENO, F_SETFL, flags | O_NONBLOCK);
-
- static struct termios oldt, newt;
- tcgetattr(STDIN_FILENO, &oldt);
- newt = oldt;
- newt.c_lflag &= ~(ICANON | ECHO);
- tcsetattr(STDIN_FILENO, TCSANOW, &newt);
-
- printf("%s\n", txtheader);
- fd_set fds;
- time_t start = time(NULL);
- time_t cur;
- unsigned char mins = 0, hrs = 0;
- while (doLoop > 0) {
- cur = time(NULL);
- double diff = difftime(cur, start);
-#if defined(_HAS_UTMP) || defined(_HAS_SYSINFO)
- if ((unsigned int)diff % 60 == 0) {
- if (diff != 0 && ++mins == 60) {
- mins = 0;
- hrs++;
- }
- struct tm localtime;
- if (localtime_r(&cur, &localtime) != NULL) {
- printf("\33[2K\r--- %02d:%02d:%02d ---\n", localtime.tm_hour, localtime.tm_min, localtime.tm_sec);
- }
-#ifdef _HAS_UTMP
- print_utmp();
-#endif
-#ifdef _HAS_HOSTENT
- print_nethost();
-#endif
-#ifdef _HAS_SYSINFO
- print_memusage();
- print_cpuusage();
-#endif
- }
-#endif
-#ifdef _HAS_MSG
- while (print_msg() == 0) {}
-#endif
- switch (state) {
- case MS_DEFAULT:
- printf("\r--- %02d:%02d:%02d ---%s", hrs, mins, ((unsigned int)diff % 60), keymsg);
- break;
- case MS_MESSAGE:
- case MS_COMMAND:
-#if defined(_HAS_MSG) || defined(_HAS_CMD)
- printf("\33[2K\r> %s", inputbuf);
-#endif
- break;
- }
- fflush(stdout);
-
- FD_ZERO(&fds);
- FD_SET(STDIN_FILENO, &fds);
- int ret = select(FD_SETSIZE, &fds, NULL, NULL, &tv);
- if (doLoop == 1 && ret == 0) {
- tv.tv_sec = 1;
- tv.tv_usec = 0;
- } else if (FD_ISSET(STDIN_FILENO, &fds)) {
- char key = getchar();
- switch (state) {
- case MS_DEFAULT:
- switch ( key ) {
- case 'q':
-#ifdef _HAS_SIGNAL
- signal(SIGINT, SIG_IGN);
-#endif
- printQuitLoop();
- break;
- case 'm':
-#ifdef _HAS_MSG
- state = MS_MESSAGE;
-#else
- printf("<feature disabled>\n");
-#endif
- break;
- case 'c':
-#ifdef _HAS_CMD
- print_cmds();
- state = MS_COMMAND;
-#else
- printf("<feature disabled>\n");
-#endif
- break;
- default: printf("unknown key: %c\n", key); break;
- }
- break;
- case MS_COMMAND:
- case MS_MESSAGE:
- switch (readInput(&inputbuf[0], &inputsiz, absiz, key, 0)) {
- case '\n':
- if (state == MS_MESSAGE) {
-#ifdef _HAS_MSG
- printf("\33[2K\rSending message(%lu): %s\n", (long unsigned int)inputsiz, inputbuf);
- if (write_msg(inputbuf) != 0)
- printf("Sending failed.\n");
-#endif
- } else if (state == MS_COMMAND) {
-#ifdef _HAS_CMD
- int inputFail = 0;
- if (inputsiz < 3) {
- inputFail++;
- } else {
- char* endptr = NULL;
- unsigned long int tmpi = strtoul(inputbuf, &endptr, 10);
- if (*endptr == ' ') {
- endptr++;
- printf("\33[2K\rExec CMD #%lu with args: %s\n", tmpi, endptr);
- int retval;
- switch ( (retval = exec_cmd(tmpi, endptr, strlen(endptr))) ) {
- case -7: printf("unknown cmd #%lu\n", tmpi); break;
- case -6: printf("fork error cmd #%lu\n", tmpi); break;
- case -5: printf("execute cmd #%lu\n", tmpi); break;
-
- case 0: break;
- default: printf("Something went wrong, child returned: %d\n", retval); break;
- }
- } else inputFail++;
- }
- if (inputFail > 0)
- printf("\33[2K\rFORMAT: [cmd#] [params]\n");
-#endif
- }
- state = MS_DEFAULT;
- readInput(&inputbuf[0], &inputsiz, absiz, 0, I_CLEARBUF);
- break;
- }
- fflush(stdout);
- break;
- }
- }
- }
- while (getchar() != EOF) {}
-
- tcsetattr( STDIN_FILENO, TCSANOW, &oldt);
- return 0;
-}
diff --git a/suidcmd.c b/suidcmd.c
deleted file mode 100644
index 7244106..0000000
--- a/suidcmd.c
+++ /dev/null
@@ -1,95 +0,0 @@
-/*
- * build with: gcc -std=c99 -D_GNU_SOURCE=1 -Wall -O2 -ffunction-sections -fdata-sections -fomit-frame-pointer ./suidcmd.c -o ./suidcmd
- * strip -s ./suidcmd
- */
-
-#include <stdio.h>
-#include <stdlib.h>
-#include <unistd.h>
-#include <string.h>
-#include <sys/wait.h>
-
-
-#ifndef CMD
-#define CMD "/usr/sbin/ether-wake"
-#endif
-
-
-int safe_exec(const char* cmdWithArgs)
-{
- pid_t child;
- if ( (child = fork()) == 0 ) {
- size_t szCur = 0, szMax = 10;
- char** args = calloc(szMax, sizeof(char**));
- const char* cmd = NULL;
-
- const char* prv = cmdWithArgs;
- const char* cur = NULL;
- while ( (cur = strchr(prv, ' ')) ) {
- if (cmd == NULL)
- cmd = strndup(prv, cur-prv);
-
- args[szCur++] = strndup(prv, cur-prv);
- if (szCur >= szMax) {
- szMax *= 2;
- args = realloc(args, sizeof(char**)*szMax);
- }
-
- cur++;
- prv = cur;
- }
- if (cmd == NULL) {
- cmd = cmdWithArgs;
- } else {
- args[szCur++] = strndup(prv, cur-prv);
- }
- args[szCur] = NULL;
- execv(cmd, args);
- } else {
- int retval = 0;
- waitpid(child, &retval, 0);
- return retval;
- }
- return -1;
-}
-
-int main(int argc, char** argv)
-{
- uid_t ruid, euid, suid;
-
- if (getresuid(&ruid, &euid, &suid) != 0) {
- perror("getresuid()");
- } else {
- printf("%s: RUID:%u , EUID:%u , SUID:%u\n", argv[0], ruid, euid, suid);
- }
-
- if (setuid(0) != 0) {
- perror("setuid(0)");
- } else printf("%s: setuid(0)\n", argv[0]);
-
- char* cmd = NULL;
- if (asprintf(&cmd, "%s", CMD) <= 0) {
- fprintf(stderr, "%s: asprintf(\"%s\") error\n", argv[0], CMD);
- return 1;
- }
-
- char* prev_cmd = NULL;
- for (int i = 1; i < argc; ++i) {
- prev_cmd = cmd;
- if (asprintf(&cmd, "%s %s", prev_cmd, argv[i]) < 0) {
- fprintf(stderr, "%s: asprintf(\"%s\") error\n", argv[0], argv[i]);
- return 1;
- }
- free(prev_cmd);
- }
-
- int retval = -1;
- switch ( (retval = safe_exec(cmd)) ) {
- case -1: fprintf(stderr, "%s: could not create child process..\n", argv[0]); return 1;
- case 127: fprintf(stderr, "%s: could not execute shell (child process)..\n", argv[0]); return 1;
- default:
- printf("%s: child process returned with: %d\n", argv[0], retval);
- }
- free(cmd);
- return 0;
-}
diff --git a/xidle.c b/xidle.c
deleted file mode 100644
index c2794fc..0000000
--- a/xidle.c
+++ /dev/null
@@ -1,45 +0,0 @@
-#include <stdio.h>
-#include <stdbool.h>
-#include <X11/Xlib.h>
-#include <X11/extensions/scrnsaver.h>
-
-
-/* Report amount of X server idle time. */
-/* gcc xidle.c -o xidle -lX11 -lXext -lXss */
-
-int main(int argc, char *argv[])
-{
- Display *display;
- int event_base, error_base;
- XScreenSaverInfo info;
- float seconds;
- unsigned int d_seconds = 0;
-
- if (argc == 2) {
- d_seconds = atoi(argv[1]);
- }
-
- display = XOpenDisplay("");
- if (!display)
- return -1;
-
- if (XScreenSaverQueryExtension(display, &event_base, &error_base) == true) {
- if (XScreenSaverQueryInfo(display, DefaultRootWindow(display), &info) != true) {
- fprintf(stderr, "Error: XScreenSaver QueryInfo failed\n");
- return -1;
- }
- seconds = (float)info.idle/1000.0f;
- if ( d_seconds > 0 ) {
- if (d_seconds <= (unsigned int) seconds) {
- return 1;
- }
- } else {
- printf("%f\n",seconds);
- }
- return 0;
- } else {
- fprintf(stderr,"Error: XScreenSaver Extension not present\n");
- return -1;
- }
-}
-