Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
radsecproxy-hash: allow mac to be passed on command line
  • Loading branch information
Fabian Mauchle committed May 1, 2018
1 parent fe262d0 commit a036c52
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 35 deletions.
1 change: 1 addition & 0 deletions ChangeLog
Expand Up @@ -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.
Expand Down
9 changes: 4 additions & 5 deletions radsecproxy-hash.1
Expand Up @@ -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
Expand All @@ -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"
Expand Down
50 changes: 20 additions & 30 deletions radsecproxy-hash.c
Expand Up @@ -13,31 +13,40 @@ 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[])
{
int opt;
#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':
Expand All @@ -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;
Expand Down

0 comments on commit a036c52

Please sign in to comment.