passdb_slim.h 7.3 KB

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