From a036c524b520f4c31077c1dec40b8fdb6c691864 Mon Sep 17 00:00:00 2001 From: Fabian Mauchle Date: Tue, 1 May 2018 16:44:52 +0200 Subject: [PATCH] radsecproxy-hash: allow mac to be passed on command line --- ChangeLog | 1 + radsecproxy-hash.1 | 9 ++++----- radsecproxy-hash.c | 50 +++++++++++++++++++--------------------------- 3 files changed, 25 insertions(+), 35 deletions(-) diff --git a/ChangeLog b/ChangeLog index c8565bf..a42cb59 100644 --- a/ChangeLog +++ b/ChangeLog @@ -10,6 +10,7 @@ Changes between 1.6.9 and the master branch - Reload TLS certificate CRLs on SIGHUP (RADSECPROXY-78). - Make use of SO_KEEPALIVE for tcp sockets (RADSECPROXY-12). - Optionally include the thread-id in log messages + - radsecproxy-hash: allow MAC addresses to be passed on command line Misc: - libnettle is now an unconditional dependency. diff --git a/radsecproxy-hash.1 b/radsecproxy-hash.1 index 6055433..5ba55ea 100644 --- a/radsecproxy-hash.1 +++ b/radsecproxy-hash.1 @@ -5,12 +5,11 @@ radsecproxy-hash - print digests of Ethernet MAC addresses .SH "SYNOPSIS" .HP 12 -radsecproxy-hash [\-h] [\-k key] [\-t type] +radsecproxy-hash [\-h] [\-k key] [mac]... .sp .SH "DESCRIPTION" -Print the hash or hmac of Ethernet MAC addresses read from standard -input. +Print the hash or hmac of Ethernet MAC addresses .SH "OPTIONS" .TP @@ -22,8 +21,8 @@ input. \fIuse KEY for HMAC calculation\fR .TP -.B \-t type -\fIprint digest of type TYPE [hash|hmac]\fR +.B mac +\fIMAC address to hash. Read from stdin if omitted.\fR .SH "SEE ALSO" diff --git a/radsecproxy-hash.c b/radsecproxy-hash.c index 2e21f38..df629f7 100644 --- a/radsecproxy-hash.c +++ b/radsecproxy-hash.c @@ -13,18 +13,29 @@ void usage() { fprintf(stderr, - "usage: radsecproxy-hash [-h] [-k key] [-t type]\n" + "usage: radsecproxy-hash [-h] [-k key] [mac]...\n" #if defined(READ_CONFIG) " -c configfile\tuse configuration from CONFIGFILE\n" #endif " -h\t\t\tdisplay this help and exit\n" " -k key\t\tuse KEY for HMAC\n" - " -t type\t\tprint digest of type TYPE [hash|hmac]\n"); + " mac\t\tMAC address to hash. Read from stdin if omittedn.\n"); exit(1); } #define MYNAME "radsecproxy-hash" +void +print_hash(uint8_t *mac, uint8_t *key) { + uint8_t buf[64+1]; + + if (fticks_hashmac(mac, key, sizeof(buf), buf) != 0) { + fprintf(stderr, "%s: out of memory\n", MYNAME); + exit(3); + } + puts((const char *) buf); +} + int main(int argc, char *argv[]) { @@ -32,12 +43,10 @@ main(int argc, char *argv[]) #if defined(READ_CONFIG) char *config = NULL; #endif - uint8_t buf[64+1]; char mac[80+1]; uint8_t *key = NULL; - enum { TYPE_HASH, TYPE_HMAC } type = TYPE_HASH; - while ((opt = getopt(argc, argv, "hk:t:")) != -1) { + while ((opt = getopt(argc, argv, "hk:")) != -1) { switch (opt) { #if defined(READ_CONFIG) case 'c': @@ -49,38 +58,19 @@ main(int argc, char *argv[]) case 'k': key = (uint8_t *) optarg; break; - case 't': - if (strcmp(optarg, "hash") == 0) - type = TYPE_HASH; - else if (strcmp(optarg, "hmac") == 0) - type = TYPE_HMAC; - else - usage(); - break; default: usage(); } } - while (fgets(mac, sizeof(mac), stdin) != NULL) { - if (type == TYPE_HASH) { - if (fticks_hashmac((uint8_t *) mac, NULL, sizeof(buf), buf) != 0) { - fprintf(stderr, "%s: out of memory\n", MYNAME); - return 3; - } - } - else if (type == TYPE_HMAC) { - if (key == NULL) { - fprintf(stderr, "%s: generating HMAC requires a key, use `-k'\n", - MYNAME); - return 2; + if (optind < argc) { + while (optind < argc) { + print_hash((uint8_t *)argv[optind++], key); } - if (fticks_hashmac((uint8_t *) mac, key, sizeof(buf), buf) != 0) { - fprintf(stderr, "%s: out of memory\n", MYNAME); - return 3; + } else { + while (fgets(mac, sizeof(mac), stdin) != NULL) { + print_hash((uint8_t *)mac, key); } - } - puts((const char *) buf); } return 0;