passdb_slim.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. /*
  2. * Copyright (c) 2019 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. #ifndef _PASSDB_H
  21. #define _PASSDB_H
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include "../common/common_defs.h"
  25. #include "ruleparam_db.h"
  26. #include "byte_access.h"
  27. typedef unsigned long long seq_t;
  28. typedef unsigned long long logical_card_id_t;
  29. //#define PASSDB_CONSISTENCY_CHECK
  30. #define FN_SZ 1024
  31. //#define RIDER_ONE_CRED_SIZE 27
  32. //#define RIDER_TWO_CRED_SIZE ( sizeof(seq_t) + sizeof(logical_card_id_t) +
  33. // sizeof(unsigned char) + sizeof(unsigned long long) +
  34. // sizeof(unsigned char) + sizeof (unsigned long) + sizeof(unsigned long) +
  35. // sizeof(unsigned short int) )
  36. // This record is only used for transport
  37. // of information. On disk storage needs to be
  38. // done through the helper functions.
  39. //
  40. typedef struct rider_record_slim_two_cred_struct
  41. {
  42. seq_t seq;
  43. logical_card_id_t id;
  44. unsigned long long magstripe;
  45. unsigned long rfid_site;
  46. unsigned long rfid_val;
  47. unsigned short int rule_param_bucket_id;
  48. unsigned char rfid_code;
  49. unsigned char magstripe_code;
  50. } rider_record_slim_two_cred;
  51. //#define RIDER_TWO_CRED_SIZE 36
  52. //#define RIDER_ONE_CRED_SIZE ( sizeof(seq_t) + sizeof(logical_card_id_t) +
  53. // sizeof(unsigned char) + sizeof(unsigned long long) +
  54. // sizeof (unsigned short int) )
  55. // This record is only used for transport
  56. // of information. On disk storage needs to be
  57. // done through the helper functions.
  58. //
  59. typedef struct rider_record_slim_one_cred
  60. {
  61. seq_t seq;
  62. logical_card_id_t id;
  63. unsigned long long credential;
  64. unsigned short int rule_param_bucket_id;
  65. unsigned char code;
  66. } rider_record_slim_one_cred;
  67. //THIS STRUCTURE MUST BE A POWER OF 2 BYTES IN SIZE
  68. //NOTE: this was the old entry format for the old database.
  69. // rider_records are still used, but serve only as transport
  70. // structures to hold information and for the 'spillover'
  71. // records. The main on-disk storage now is the rider one
  72. // cred and rider two cred structures above, which are not
  73. // powers of two in size.
  74. // This rider_record struct is also used to transport
  75. // information about the card and provide a consistent
  76. // interface to the rest of the ecosystem.
  77. //
  78. typedef struct rider_record_struct
  79. {
  80. seq_t seq; //sequence number
  81. logical_card_id_t id; //rider ID
  82. char magstripe_value[CREDENTIAL_LEN];
  83. char rfid_value[CREDENTIAL_LEN];
  84. char rule_name[RULENAME_LEN];
  85. char rule_param[PARAM_LEN];
  86. } rider_record;
  87. typedef struct rule_pass_param_bucket_struct
  88. {
  89. unsigned int rule_param_bucket_id;
  90. char rule_name[RULENAME_LEN];
  91. char rule_param[PARAM_LEN];
  92. } rule_pass_param_bucket;
  93. // 0 <= idx < INDEX_MIDPOINT -> single credential (either rfid or mag only)
  94. // INDEX_MIDPOINT <= idx < 2*INDEX_MIDPOINT -> double credential (both rfid + mag)
  95. //
  96. // idx >= 2*INDEX_MIDPOINT in spillover
  97. //
  98. // Each 'bank' is a block of X one credential, two credential or spillover packed
  99. // structures. X is read from the passdb.conf file (see passdb_slim_config.[ch]).
  100. //
  101. typedef struct rider_node_struct
  102. {
  103. int idx;
  104. struct rider_node_struct *next;
  105. } rider_node;
  106. typedef struct passdb_slim_context_struct
  107. {
  108. void **rider_one_cred_bank;
  109. void **rider_two_cred_bank;
  110. void **rider_spillover_bank;
  111. // Helper vairables to keep count of number of one and two credit
  112. // credentials in all of the banks.
  113. //
  114. // heap of active and free rider indexes
  115. // (indexed into rider_one_cred and rider_two_cred)
  116. // simple linear linked lists
  117. //
  118. rider_node *activelist;
  119. rider_node *freelist_one_cred;
  120. rider_node *freelist_two_cred;
  121. rider_node *freelist_spillover;
  122. char **one_cred_db_bank_fn;
  123. char **two_cred_db_bank_fn;
  124. char **spillover_db_bank_fn;
  125. // current number of banks for each database type
  126. //
  127. int n_one_cred;
  128. int n_two_cred;
  129. int n_spillover;
  130. int n_one_cred_bank;
  131. int n_two_cred_bank;
  132. int n_spillover_bank;
  133. int n_one_cred_bank_size;
  134. int n_two_cred_bank_size;
  135. int n_spillover_bank_size;
  136. int n_one_cred_max_bank;
  137. int n_two_cred_max_bank;
  138. int n_spillover_max_bank;
  139. int hash_modulus;
  140. // hashes of logical_card_id, mag and rf
  141. // for quick lookup
  142. //
  143. rider_node **logical_card_id_hash;
  144. rider_node **rider_mag_hash;
  145. rider_node **rider_rf_hash;
  146. seq_t seq;
  147. // THESE ARE USED IF MMAP IS DETERMINED TO BE BROKEN...
  148. // it's broken...
  149. int mmap_broken;
  150. int num_free;
  151. int num_active;
  152. int *passes_one_cred_bank_fd;
  153. int *passes_two_cred_bank_fd;
  154. int *passes_spillover_bank_fd;
  155. size_t rider_one_file_size;
  156. size_t rider_two_file_size;
  157. size_t rider_spillover_file_size;
  158. size_t rider_one_file_bank_size;
  159. size_t rider_two_file_bank_size;
  160. size_t rider_spillover_file_bank_size;
  161. char diagnostic_str[128];
  162. ruleparam_db_ctx *ruleparam_db;
  163. char *ruleparam_db_fn;
  164. char *one_cred_db_base_fn;
  165. char *two_cred_db_base_fn;
  166. char *spillover_db_base_fn;
  167. char *db_fn_suffix;
  168. float high_watermark_threshold;
  169. float low_watermark_threshold;
  170. } passdb_slim_context;
  171. typedef struct passdb_slim_config_t {
  172. int hash_modulus;
  173. int n_one_cred_bank;
  174. int n_two_cred_bank;
  175. int n_spillover_bank;
  176. int n_one_cred_bank_size;
  177. int n_two_cred_bank_size;
  178. int n_spillover_bank_size;
  179. int n_one_cred_max_bank;
  180. int n_two_cred_max_bank;
  181. int n_spillover_max_bank;
  182. char *one_cred_db_base_fn;
  183. char *two_cred_db_base_fn;
  184. char *spillover_db_base_fn;
  185. char *db_fn_suffix;
  186. char *ruleparam_db_fn;
  187. size_t rider_one_file_bank_size;
  188. size_t rider_two_file_bank_size;
  189. size_t rider_spillover_file_bank_size;
  190. float high_watermark_threshold;
  191. float low_watermark_threshold;
  192. } passdb_slim_config;
  193. //--------------------------
  194. int format_new_passdb( char *, int, long );
  195. int format_new_passdbs();
  196. int attach_to_passdb(passdb_slim_context *ctx);
  197. int detach_from_passdb(passdb_slim_context *ctx);
  198. int delete_rider(passdb_slim_context *ctx, rider_record *rec, int sync);
  199. int update_rider(passdb_slim_context *ctx, rider_record *rec, int sync);
  200. void sync_all_riders(passdb_slim_context *ctx);
  201. void dump_hashes(passdb_slim_context *ctx);
  202. int find_mag_in_hash(passdb_slim_context *ctx, char *mag);
  203. int find_rf_in_hash(passdb_slim_context *ctx, char *rfid);
  204. void populate_one_cred_rider_record( rider_record_slim_one_cred *, int, void *);
  205. void populate_two_cred_rider_record( rider_record_slim_two_cred *, int, void *);
  206. void populate_spillover_rider_record( rider_record *, int, void *);
  207. int make_rider_record_from_rider_one_cred( passdb_slim_context *ctx, rider_record *rr, rider_record_slim_one_cred *rr1 );
  208. int make_rider_record_from_rider_two_cred( passdb_slim_context *ctx, rider_record *rr, rider_record_slim_two_cred *rr2 );
  209. int make_rider_record_from_rider_spillover( passdb_slim_context *, rider_record *, rider_record *);
  210. void make_rider_record( passdb_slim_context *ctx, rider_record *rr, int idx );
  211. int passdb_slim_get_cred_bank_and_pos( passdb_slim_context *ctx, int *bank, int *pos, int idx );
  212. int passdb_slim_consistency_check( passdb_slim_context *ctx );
  213. // config file functions
  214. //
  215. void passdb_slim_copy_config( passdb_slim_config *, passdb_slim_context *);
  216. int save_config( passdb_slim_config *, char *);
  217. int read_config( passdb_slim_config *, char *);
  218. int init_context_from_config( passdb_slim_context *, char *);
  219. int make_default_config( char *);
  220. // DEBUGGING
  221. //
  222. void passdb_slim_dump( passdb_slim_context * );
  223. #endif