passdb_inspect.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /*
  2. * Copyright (c) 2021 Clementine Computing LLC.
  3. *
  4. * This file is part of PopuFare.
  5. *
  6. * PopuFare is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Affero General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * PopuFare is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Affero General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Affero General Public License
  17. * along with PopuFare. If not, see <https://www.gnu.org/licenses/>.
  18. *
  19. */
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <sys/types.h>
  23. #include <sys/stat.h>
  24. #include <fcntl.h>
  25. #include <sys/time.h>
  26. #include <unistd.h>
  27. #include <string.h>
  28. #include <ctype.h>
  29. #include <getopt.h>
  30. #include <errno.h>
  31. #include "../common/common_config.h"
  32. #include "../passdb/passdb.h"
  33. #define VERSION "0.1.0"
  34. void _version(FILE *fp) {
  35. fprintf(fp, "version: %s\n", VERSION);
  36. }
  37. void _usage(FILE *fp) {
  38. _version(fp);
  39. fprintf(fp, "usage:\n\n");
  40. fprintf(fp, " passdb_inspect [-h] [-v] [passdb]\n\n");
  41. fprintf(fp, " [passdb] passdb file (defualt '%s')\n", PASSES_FILE);
  42. fprintf(fp, " [-h] help (this screen)\n");
  43. fprintf(fp, " [-v] show version\n");
  44. fprintf(fp, "\n");
  45. }
  46. struct option g_long_option[] = {
  47. {"version", no_argument, 0, 'v'},
  48. {"help", no_argument, 0, 'h'},
  49. {0,0,0,0}
  50. };
  51. char *g_pass_file = NULL;
  52. double del_tv( struct timeval *tv_e, struct timeval *tv_s) {
  53. double d=0.0;
  54. d += (double)(tv_e->tv_sec - tv_s->tv_sec) * (1000000.0);
  55. d += (double)(tv_e->tv_usec - tv_s->tv_usec);
  56. return d;
  57. }
  58. int main(int argc, char **argv) {
  59. int i, j, k;
  60. extern char *optarg;
  61. extern int optind;
  62. int option_index;
  63. int ch;
  64. int retcode = 0;
  65. int fd;
  66. ssize_t del_s=0;
  67. size_t offset=0, pass_size=0;
  68. rider_record *passmem = NULL;
  69. struct timeval tv_s, tv_e;
  70. while ((ch = getopt_long(argc, argv, "hv", g_long_option, &option_index)) > 0) switch(ch) {
  71. case 'h':
  72. _usage(stdout);
  73. exit(0);
  74. break;
  75. case 'v':
  76. _version(stdout);
  77. exit(0);
  78. break;
  79. case 0:
  80. default:
  81. fprintf(stderr, "bad option (%i, %i)\n", option_index, (int)ch);
  82. _usage(stderr);
  83. exit(-2);
  84. break;
  85. }
  86. if (optind < argc) {
  87. g_pass_file = strdup(argv[optind]);
  88. } else {
  89. g_pass_file = strdup(PASSES_FILE);
  90. }
  91. //---
  92. gettimeofday(&tv_s, NULL);
  93. pass_size = sizeof(rider_record)*NUM_STORED_PASSES;
  94. passmem = (rider_record *)malloc(pass_size);
  95. fd = open(g_pass_file, O_RDONLY);
  96. if (fd<0) {
  97. perror(g_pass_file);
  98. retcode = -1;
  99. goto free_and_exit;
  100. }
  101. while (offset < pass_size) {
  102. del_s = read(fd, ((void *)passmem) + offset, pass_size - offset);
  103. if (del_s<0) {
  104. perror(g_pass_file);
  105. goto free_and_exit;
  106. }
  107. offset += del_s;
  108. }
  109. close(fd);
  110. gettimeofday(&tv_e, NULL);
  111. printf("## loaded in %f\n", del_tv(&tv_e, &tv_s));
  112. //---
  113. for (i=0; i<NUM_STORED_PASSES; i++) {
  114. if (passmem[i].seq == 0) { continue; }
  115. printf("[%4i] ", i);
  116. printf(" %8lli %8lli ", passmem[i].seq, passmem[i].id);
  117. for (j=0; j<CREDENTIAL_LEN; j++) {
  118. //printf("%2x", passmem[i].magstripe_value[j]);
  119. //continue;
  120. if (isprint(passmem[i].magstripe_value[j])) {
  121. printf("%c", passmem[i].magstripe_value[j]);
  122. }
  123. else {
  124. printf(".");
  125. }
  126. }
  127. printf(" ");
  128. for (j=0; j<CREDENTIAL_LEN; j++) {
  129. if (isprint(passmem[i].rfid_value[j])) {
  130. printf("%c", passmem[i].rfid_value[j]);
  131. }
  132. else {
  133. printf(".");
  134. }
  135. }
  136. printf(" ");
  137. for (j=0; j<RULENAME_LEN; j++) {
  138. if (isprint(passmem[i].rule_name[j])) {
  139. printf("%c", passmem[i].rule_name[j]);
  140. }
  141. else {
  142. printf(".");
  143. }
  144. }
  145. printf(" ");
  146. for (j=0; j<PARAM_LEN; j++) {
  147. if (isprint(passmem[i].rule_param[j])) {
  148. printf("%c", passmem[i].rule_param[j]);
  149. }
  150. else {
  151. printf(".");
  152. }
  153. }
  154. printf("\n");
  155. }
  156. //---
  157. free_and_exit:
  158. if (g_pass_file) {
  159. free(g_pass_file);
  160. }
  161. if (passmem) {
  162. free(passmem);
  163. }
  164. exit(retcode);
  165. }