diff options
author | rsc <devnull@localhost> | 2005-10-29 16:26:44 +0000 |
---|---|---|
committer | rsc <devnull@localhost> | 2005-10-29 16:26:44 +0000 |
commit | 5cdb17983ae6e6367ad7a940cb219eab247a9304 (patch) | |
tree | 8ca1ef49af2a96e7daebe624d91fdf679814a057 /src/cmd/upas/filterkit/token.c | |
parent | cd3745196389579fb78b9b01ef1daefb5a57aa71 (diff) | |
download | plan9port-5cdb17983ae6e6367ad7a940cb219eab247a9304.tar.gz plan9port-5cdb17983ae6e6367ad7a940cb219eab247a9304.tar.bz2 plan9port-5cdb17983ae6e6367ad7a940cb219eab247a9304.zip |
Thanks to John Cummings.
Diffstat (limited to 'src/cmd/upas/filterkit/token.c')
-rw-r--r-- | src/cmd/upas/filterkit/token.c | 89 |
1 files changed, 89 insertions, 0 deletions
diff --git a/src/cmd/upas/filterkit/token.c b/src/cmd/upas/filterkit/token.c new file mode 100644 index 00000000..8fbcda66 --- /dev/null +++ b/src/cmd/upas/filterkit/token.c @@ -0,0 +1,89 @@ +#include <u.h> +#include <libc.h> +#include <libsec.h> +#include <String.h> +#include "dat.h" + +void +usage(void) +{ + fprint(2, "usage: %s key [token]\n", argv0); + exits("usage"); +} + +static String* +mktoken(char *key, long thetime) +{ + char *now; + uchar digest[SHA1dlen]; + char token[64]; + String *s; + + now = ctime(thetime); + memset(now+11, ':', 8); + hmac_sha1((uchar*)now, strlen(now), (uchar*)key, strlen(key), digest, nil); + enc64(token, sizeof token, digest, sizeof digest); + s = s_new(); + s_nappend(s, token, 5); + return s; +} + +static char* +check_token(char *key, char *file) +{ + String *s; + long now; + int i; + char buf[1024]; + int fd; + + fd = open(file, OREAD); + if(fd < 0) + return "no match"; + i = read(fd, buf, sizeof(buf)-1); + close(fd); + if(i < 0) + return "no match"; + buf[i] = 0; + + now = time(0); + + for(i = 0; i < 14; i++){ + s = mktoken(key, now-24*60*60*i); + if(strstr(buf, s_to_c(s)) != nil){ + s_free(s); + return nil; + } + s_free(s); + } + return "no match"; +} + +static char* +create_token(char *key) +{ + String *s; + + s = mktoken(key, time(0)); + print("%s", s_to_c(s)); + return nil; +} + +void +main(int argc, char **argv) +{ + ARGBEGIN { + } ARGEND; + + switch(argc){ + case 2: + exits(check_token(argv[0], argv[1])); + break; + case 1: + exits(create_token(argv[0])); + break; + default: + usage(); + } + exits(0); +} |