Add FoundationDB stuff
[slow/toolbox.git] / sngen.c
1 #define _XOPEN_SOURCE 600
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
5 #include <errno.h>
6 #include <string.h>
7 #include <unistd.h>
8 #include <ctype.h>
9 #include <sys/socket.h>
10 #include <netinet/in.h>
11 #include <arpa/inet.h>
12 #include <inttypes.h>
13
14 #include <time.h>
15 #include <locale.h>
16
17 static void usage(void)
18 {
19         printf("usage: sngen [-k] serial date [users]\n");
20 }
21
22 int main(int argc, char **argv)
23 {
24         char timebuf[80];
25         long long serial = 0;
26         long users = 0;
27         uint64_t key = 0;
28         struct tm date = {0};
29         time_t time = 0;
30         char *p;
31         int opt;
32         int keyonly = 0;
33         char *tz_save;
34
35         while ((opt = getopt(argc, argv, "k")) != -1) {
36                 switch (opt) {
37                 case 'k':
38                         keyonly = 1;
39                         break;
40                 default:
41                         usage();
42                         exit(0);
43                 }
44         }
45
46         if (argc - optind < 2) {
47                 usage();
48                 exit(0);
49         }
50
51         tz_save = getenv("TZ");
52
53         setenv("TZ", "UTC", 1);
54         tzset();
55
56         serial = atoll(argv[optind]);
57
58         if (strptime(argv[optind + 1], "%d/%m/%Y", &date) == NULL) {
59                 perror("Couldn't parse date");
60                 exit(1);
61         }
62         date.tm_isdst = -1;
63         time = mktime(&date);
64         strftime(timebuf, 80, "%d/%m/%Y", &date);
65
66         if (argc - optind >= 3) {
67                 users = atoll(argv[optind + 2]);
68         }
69
70         key = ((time + 0xad)  * (serial + users)) << 32;
71         key += ((time % serial) * (serial % 0xaf)) + ((time % (0xdead + users)) * (serial % (users + 1)));
72
73         if (keyonly) {
74                 printf("%04" PRIx16 "-", (key & 0xffff000000000000) >> 48);
75                 printf("%04" PRIx16 "-", (key & 0xffff00000000) >> 32);
76                 printf("%04" PRIx16 "-", (key & 0xffff0000) >> 16);
77                 printf("%04" PRIx16 "\n", key & 0xffff);
78
79         } else {
80                 printf("serial:  %lld\n", serial);
81                 printf("expires: %s UTC\n", timebuf);
82                 if (users) {
83                         printf("users:   %ld\n", users);
84                 }
85                 printf("key:     ");
86                 printf("%04" PRIx16 "-", (key & 0xffff000000000000) >> 48);
87                 printf("%04" PRIx16 "-", (key & 0xffff00000000) >> 32);
88                 printf("%04" PRIx16 "-", (key & 0xffff0000) >> 16);
89                 printf("%04" PRIx16 "\n", key & 0xffff);
90         }
91 }