ttoken.c - plan9port - [fork] Plan 9 from user space
git clone git://src.adamsgaard.dk/plan9port
Log
Files
Refs
README
LICENSE
---
ttoken.c (1313B)
---
     1 #include 
     2 #include 
     3 #include 
     4 #include 
     5 #include "dat.h"
     6 
     7 void
     8 usage(void)
     9 {
    10         fprint(2, "usage: %s key [token]\n", argv0);
    11         exits("usage");
    12 }
    13 
    14 static String*
    15 mktoken(char *key, long thetime)
    16 {
    17         char *now;
    18         uchar digest[SHA1dlen];
    19         char token[64];
    20         String *s;
    21 
    22         now = ctime(thetime);
    23         memset(now+11, ':', 8);
    24         hmac_sha1((uchar*)now, strlen(now), (uchar*)key, strlen(key), digest, nil);
    25         enc64(token, sizeof token, digest, sizeof digest);
    26         s = s_new();
    27         s_nappend(s, token, 5);
    28         return s;
    29 }
    30 
    31 static char*
    32 check_token(char *key, char *file)
    33 {
    34         String *s;
    35         long now;
    36         int i;
    37         char buf[1024];
    38         int fd;
    39 
    40         fd = open(file, OREAD);
    41         if(fd < 0)
    42                 return "no match";
    43         i = read(fd, buf, sizeof(buf)-1);
    44         close(fd);
    45         if(i < 0)
    46                 return "no match";
    47         buf[i] = 0;
    48 
    49         now = time(0);
    50 
    51         for(i = 0; i < 14; i++){
    52                 s = mktoken(key, now-24*60*60*i);
    53                 if(strstr(buf, s_to_c(s)) != nil){
    54                         s_free(s);
    55                         return nil;
    56                 }
    57                 s_free(s);
    58         }
    59         return "no match";
    60 }
    61 
    62 static char*
    63 create_token(char *key)
    64 {
    65         String *s;
    66 
    67         s = mktoken(key, time(0));
    68         print("%s", s_to_c(s));
    69         return nil;
    70 }
    71 
    72 void
    73 main(int argc, char **argv)
    74 {
    75         ARGBEGIN {
    76         } ARGEND;
    77 
    78         switch(argc){
    79         case 2:
    80                 exits(check_token(argv[0], argv[1]));
    81                 break;
    82         case 1:
    83                 exits(create_token(argv[0]));
    84                 break;
    85         default:
    86                 usage();
    87         }
    88         exits(0);
    89 }