rp

simple email tools
git clone https://git.parazyd.org/rp
Log | Files | Refs | README | LICENSE

util.c (2613B)


      1 /* See LICENSE file for copyright and license details */
      2 
      3 #include <stdio.h>
      4 
      5 #include <openssl/ssl.h>
      6 #include <openssl/x509.h>
      7 
      8 #include "arg.h"
      9 #include "net.h"
     10 #include "ind.h"
     11 
     12 char *argv0;
     13 
     14 char be[] =
     15 	"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
     16 
     17 void utilusage(void) {
     18 	die("Usage: %s [-e util [args]]\n"
     19 		"  -e: run util\n\n"
     20 		"Available utils:\n"
     21 		"  * genmsgid:  generates a Message-ID header\n"
     22 		"  * genmimeb:  generates a semi-random MIME boundary\n"
     23 		"  * humansize: calculates byte size into human readable\n"
     24 		"  * getfpr:    get tls fingerprint of $netspec\n", argv0);
     25 }
     26 
     27 struct util {
     28 	char *cmd;
     29 	int (*func)(char *);
     30 };
     31 
     32 int randint(int max) {
     33 	unsigned int ret;
     34 	FILE *f;
     35 
     36 	f = fopen("/dev/urandom", "r");
     37 	fread(&ret, sizeof(ret), 1, f);
     38 	fclose(f);
     39 
     40 	return ret % max;
     41 }
     42 
     43 char *randstring(char *str, int size) {
     44 	if (size) {
     45 		size--;
     46 		for (int i = 0; i < size; i++) {
     47 			int key = randint(strlen(be));
     48 			str[i] = be[key];
     49 		}
     50 		str[size] = '\0';
     51 	}
     52 	return str;
     53 }
     54 
     55 
     56 int genmsgid(char *foo) {
     57 	char buf[15];
     58 	time_t now = time(0);
     59 	strftime(buf, 15, "%Y%m%d%H%M%S", localtime(&now));
     60 	printf("<%s.GA%d@fq>\n", buf, randint(9999));
     61 	return 0;
     62 }
     63 
     64 int genmimeb(char *foo) {
     65 	char buf[6];
     66 	randstring(buf, sizeof(buf));
     67 	printf("%sFuNkADeLiC\n", buf);
     68 	return 0;
     69 }
     70 
     71 int humansize(char *sizestr) {
     72 	int i = 0;
     73 	unsigned long size = atol(sizestr);
     74 	const char *units[] = {"Bytes", "KiB", "MiB", "GiB"};
     75 	while (size > 1024) {
     76 		size /= 1024;
     77 		i++;
     78 	}
     79 	printf("%ld %s", size, units[i]);
     80 
     81 	return 0;
     82 }
     83 
     84 int getfpr(char *desc) {
     85 	net_t *net;
     86 
     87 	unsigned int i, fpr_size;
     88 	X509 *cert;
     89 	const EVP_MD *fpr_type;
     90 	unsigned char fpr[EVP_MAX_MD_SIZE];
     91 
     92 	net = net_new(desc);
     93 	if (net == NULL)
     94 		edie("net_new");
     95 
     96 	if (net_connect(net))
     97 		edie("net_connect");
     98 
     99 	cert = SSL_get_peer_certificate(net->data[0]);
    100 	if (cert == NULL)
    101 		goto getfprend;
    102 	fpr_type = EVP_sha1();
    103 	X509_digest(cert, fpr_type, fpr, &fpr_size);
    104 
    105 	for (i = 0; i < fpr_size; i++)
    106 		printf("%02X%c", fpr[i], (i+1 == fpr_size) ? '\n':':');
    107 
    108 	X509_free(cert);
    109 getfprend:
    110 	net_close(net);
    111 	net_free(net);
    112 
    113 	return 0;
    114 }
    115 
    116 struct util utils[] = {
    117 	{"genmsgid", genmsgid},
    118 	{"genmimeb", genmimeb},
    119 	{"humansize", humansize},
    120 	{"getfpr", getfpr},
    121 };
    122 
    123 int utilmain(int argc, char *argv[]) {
    124 	unsigned long int i;
    125 	char *runcmd = NULL;
    126 
    127 	ARGBEGIN {
    128 	case 'e':
    129 		runcmd = EARGF(utilusage());
    130 		break;
    131 	default:
    132 		utilusage();
    133 	} ARGEND;
    134 
    135 	if (runcmd == NULL)
    136 		utilusage();
    137 
    138 	for (i = 0; i < nelem(utils); i++) {
    139 		if (!strcmp(runcmd, utils[i].cmd))
    140 			return utils[i].func(argv[0]);
    141 	}
    142 
    143 	utilusage();
    144 	return 1;
    145 }