passdb_slim.c 89 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667
  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. #include <sys/user.h>
  21. #include <sys/types.h>
  22. #include <sys/stat.h>
  23. #include <sys/mman.h>
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <unistd.h>
  27. #include <fcntl.h>
  28. #include <string.h>
  29. #include <errno.h>
  30. #include <time.h>
  31. #include <sys/time.h>
  32. #include "../common/common_defs.h"
  33. #include "passdb_slim.h"
  34. int read_config( passdb_slim_config *cfg, char *config_fn );
  35. int passdb_slim_manage_rider_banks(passdb_slim_context *ctx);
  36. static void free_rider_node_list(rider_node *head)
  37. {
  38. rider_node *p = head;
  39. rider_node *q;
  40. while(p)
  41. {
  42. q = p;
  43. p = p->next;
  44. free(q);
  45. }
  46. }
  47. #define FIND_IDX_IN_BUCKET(b, idx, p, q)\
  48. { \
  49. p = b; \
  50. q = NULL; \
  51. \
  52. while(p) \
  53. { \
  54. if(p->idx == idx) \
  55. { \
  56. break; \
  57. } \
  58. \
  59. q = p; \
  60. p = p->next; \
  61. } \
  62. } \
  63. //---------------------------
  64. #define ADD_TO_BUCKET(b, idx, p, q) \
  65. { \
  66. p = (rider_node *) malloc( sizeof(rider_node) );\
  67. \
  68. if(p == NULL) return FAIL_MEM; \
  69. \
  70. p->next = NULL; \
  71. p->idx = idx; \
  72. \
  73. if(q) \
  74. { \
  75. q->next = p; \
  76. } \
  77. else \
  78. { \
  79. b = p; \
  80. } \
  81. } \
  82. //---------------------------
  83. #define DEL_FROM_BUCKET(b, p, q) \
  84. { \
  85. if(q) \
  86. { \
  87. q->next = p->next; \
  88. } \
  89. else \
  90. { \
  91. b = p->next; \
  92. } \
  93. \
  94. free(p); \
  95. } \
  96. //---------------------------
  97. //#define INDEX_MIDPOINT 500000
  98. int make_rider_record_from_rider_one_cred( passdb_slim_context *ctx, rider_record *rr, rider_record_slim_one_cred *rr1 )
  99. {
  100. long long unsigned llu;
  101. long unsigned a, b;
  102. int retval;
  103. memset(rr, 0, sizeof(rider_record));
  104. if (rr1->id == ID_INVALID)
  105. return WARN_NOTFOUND;
  106. rr->seq = rr1->seq;
  107. rr->id = rr1->id;
  108. if (rr1->code < 4)
  109. {
  110. rr->rfid_value[0] = '\0';
  111. snprintf(rr->magstripe_value, CREDENTIAL_LEN, "%i:%llu", rr1->code, rr1->credential );
  112. }
  113. else
  114. {
  115. llu = 0xffffffffL;
  116. b = (long unsigned)(rr1->credential & llu);
  117. llu = llu << 32;
  118. a = (long unsigned)( (rr1->credential & llu) >> 32 );
  119. rr->magstripe_value[0] = '\0';
  120. snprintf(rr->rfid_value, CREDENTIAL_LEN, "%i:%lu:%lu", rr1->code, a, b );
  121. }
  122. retval = ruleparam_db_get( rr->rule_name, rr->rule_param, ctx->ruleparam_db, rr1->rule_param_bucket_id );
  123. if (retval < 0)
  124. {
  125. fprintf(stderr, "ERROR: make_rider_record_from_rider_one_cred: no mapping for rr1->rule_param_bucket_id (%i), got %i\n",
  126. rr1->rule_param_bucket_id, retval);
  127. //DEBUG
  128. fprintf(stderr, "ERROR: mrrfroc: ruleid %i, got %i rr1( seq:%llu, id:%llu, %u:%llu, %i)\n",
  129. rr1->rule_param_bucket_id, retval,
  130. rr1->seq, rr1->id,
  131. (unsigned int)rr1->code, rr1->credential,
  132. (unsigned int)rr1->rule_param_bucket_id
  133. );
  134. return retval;
  135. }
  136. return 0;
  137. }
  138. //--
  139. int make_rider_record_from_rider_two_cred( passdb_slim_context *ctx, rider_record *rr, rider_record_slim_two_cred *rr2 )
  140. {
  141. int retval;
  142. memset(rr, 0, sizeof(rider_record));
  143. if (rr2->id == ID_INVALID)
  144. return WARN_NOTFOUND;
  145. rr->seq = rr2->seq;
  146. rr->id = rr2->id;
  147. snprintf(rr->magstripe_value,
  148. CREDENTIAL_LEN,
  149. "%i:%llu",
  150. rr2->magstripe_code, rr2->magstripe );
  151. snprintf(rr->rfid_value,
  152. CREDENTIAL_LEN,
  153. "%i:%lu:%lu",
  154. rr2->rfid_code, rr2->rfid_site, rr2->rfid_val );
  155. retval = ruleparam_db_get( rr->rule_name, rr->rule_param, ctx->ruleparam_db, rr2->rule_param_bucket_id );
  156. if (retval < 0)
  157. {
  158. fprintf(stderr, "ERROR: make_rider_record_from_rider_two_cred: no mapping for rr2->rule_param_bucket_id (%i), got %i\n",
  159. rr2->rule_param_bucket_id, retval);
  160. //DEBUG
  161. fprintf(stderr, "ERROR: mrrfrtc: ruleid %i, got %i rr2( seq:%llu, id:%llu, %u:%lu:%lu %u:%llu, %i)\n",
  162. rr2->rule_param_bucket_id, retval,
  163. rr2->seq, rr2->id,
  164. (unsigned int)rr2->rfid_code, rr2->rfid_site, rr2->rfid_val,
  165. (unsigned int)rr2->magstripe_code, rr2->magstripe,
  166. (unsigned int)rr2->rule_param_bucket_id
  167. );
  168. return retval;
  169. }
  170. return 0;
  171. }
  172. //--
  173. // UNDER DEVELOPMENT
  174. //
  175. //int make_rider_record_from_rider_spillover( passdb_slim_context *ctx, rider_record *rr, rider_record_slim_spillover *rr2 )
  176. int make_rider_record_from_rider_spillover( passdb_slim_context *ctx, rider_record *rr, rider_record *rr_spillover )
  177. {
  178. int bucket_id;
  179. memcpy( rr, rr_spillover, sizeof(rider_record) );
  180. bucket_id = ruleparam_db_find( ctx->ruleparam_db, rr->rule_name, rr->rule_param );
  181. if (bucket_id == RULEPARAM_DB_NOT_FOUND )
  182. {
  183. fprintf(stderr, "ERROR: make_rider_record_from_rider_spillover: no mapping for rule/param (%s,%s), got %i\n",
  184. rr->rule_name,
  185. rr->rule_param,
  186. bucket_id);
  187. return -1;
  188. }
  189. return 0;
  190. }
  191. //--
  192. void populate_one_cred_rider_record(
  193. rider_record_slim_one_cred *rr_one,
  194. int pos,
  195. void *rider_p )
  196. {
  197. void *p;
  198. memset(rr_one, 0, sizeof(rider_record_slim_one_cred));
  199. p = rider_p + (pos*RIDER_ONE_CRED_SIZE);
  200. //rr_one->seq = (seq_t)( *((seq_t *)p) );
  201. rr_one->seq = _ulli(p);
  202. p += sizeof(seq_t);
  203. //rr_one->id = (logical_card_id_t)( *((logical_card_id_t *)p) );
  204. rr_one->id = _ulli(p);
  205. p += sizeof(logical_card_id_t);
  206. rr_one->code = (unsigned char)( *((unsigned char *)p) );
  207. p += sizeof(unsigned char);
  208. //rr_one->credential = (unsigned long long)( *((unsigned long long *)p) );
  209. rr_one->credential = _ulli(p);
  210. p += sizeof(unsigned long long);
  211. //rr_one->rule_param_bucket_id = (unsigned short int)( *((unsigned short int *)p) );
  212. rr_one->rule_param_bucket_id = _usi(p);
  213. //p += sizeof(unsigned short int);
  214. }
  215. void populate_two_cred_rider_record( rider_record_slim_two_cred *rr_two, int pos, void *rider_p)
  216. {
  217. void *p;
  218. memset(rr_two, 0, sizeof(rider_record_slim_two_cred));
  219. p = rider_p + (pos*RIDER_TWO_CRED_SIZE);
  220. //rr_two->seq = (seq_t)( *((seq_t *)p) );
  221. rr_two->seq = _ulli(p);
  222. p += sizeof(seq_t);
  223. //rr_two->id = (logical_card_id_t)( *((logical_card_id_t *)p) );
  224. rr_two->id = _ulli(p);
  225. p += sizeof(logical_card_id_t);
  226. rr_two->magstripe_code = (unsigned char)( *((unsigned char *)p) );
  227. p += sizeof(unsigned char);
  228. //rr_two->magstripe = (unsigned long long)( *((unsigned long long *)p) );
  229. rr_two->magstripe = _ulli(p);
  230. p += sizeof(unsigned long long);
  231. rr_two->rfid_code = (unsigned char)( *((unsigned char *)p) );
  232. p += sizeof(unsigned char);
  233. //rr_two->rfid_site = (unsigned long)( *((unsigned long *)p) );
  234. rr_two->rfid_site = _uli(p);
  235. p += sizeof(unsigned long);
  236. //rr_two->rfid_val = (unsigned long)( *((unsigned long *)p) );
  237. rr_two->rfid_val = _uli(p);
  238. p += sizeof(unsigned long);
  239. //rr_two->rule_param_bucket_id = (unsigned short int)( *((unsigned short int *)p) );
  240. rr_two->rule_param_bucket_id = _usi(p);
  241. //p += sizeof(unsigned short int);
  242. }
  243. // ---
  244. void populate_spillover_rider_record( rider_record *rr, int pos, void *rider_p)
  245. {
  246. memcpy(rr, rider_p + (pos*RIDER_SPILLOVER_SIZE), sizeof(rider_record) );
  247. }
  248. // ---
  249. void *passdb_slim_get_record_address( passdb_slim_context *ctx, int idx )
  250. {
  251. int q,r;
  252. if (idx < INDEX_MIDPOINT)
  253. {
  254. q = idx / ctx->n_one_cred_bank_size;
  255. r = idx - (q * ctx->n_one_cred_bank_size);
  256. if ( (q<0) || (q >= ctx->n_one_cred_bank) )
  257. {
  258. fprintf(stderr, "ERROR: passdb_slim_get_record_address, bank out of range (%i<0 or %i>%i) (%i,%i) from idx %i\n", q, q, ctx->n_one_cred_bank, q, r, idx);
  259. return NULL;
  260. }
  261. return ctx->rider_one_cred_bank[q] + (r * RIDER_ONE_CRED_SIZE);
  262. }
  263. else if ( idx < (2*INDEX_MIDPOINT) )
  264. {
  265. q = (idx - INDEX_MIDPOINT) / ctx->n_two_cred_bank_size;
  266. r = (idx - INDEX_MIDPOINT) - (q * ctx->n_two_cred_bank_size);
  267. if ( (q<0) || (q >= ctx->n_two_cred_bank) )
  268. {
  269. fprintf(stderr, "ERROR: passdb_slim_get_record_address, bank out of range (%i<0 or %i>%i) (%i,%i) from idx %i\n", q,q, ctx->n_two_cred_bank, q, r, idx);
  270. return NULL;
  271. }
  272. return ctx->rider_two_cred_bank[q] + (r * RIDER_TWO_CRED_SIZE);
  273. }
  274. else
  275. {
  276. q = (idx - (2*INDEX_MIDPOINT)) / ctx->n_spillover_bank_size;
  277. r = (idx - (2*INDEX_MIDPOINT)) - (q * ctx->n_spillover_bank_size);
  278. if ( (q<0) || (q >= ctx->n_spillover_bank) )
  279. {
  280. fprintf(stderr, "ERROR: passdb_slim_get_record_address, bank out of range (%i<0 or %i>%i) (%i,%i) from idx %i\n", q,q, ctx->n_spillover_bank, q, r, idx);
  281. return NULL;
  282. }
  283. return ctx->rider_spillover_bank[q] + (r * RIDER_SPILLOVER_SIZE);
  284. }
  285. return NULL;
  286. }
  287. // Helper function that stores the bank and position of the index.
  288. // That is, find the quotent and remainder of idx (suitably repositioned)
  289. // with the number of entries in the appropriate bank.
  290. // For example, if we have 251 entries per bank, with 3 banks and an index
  291. // of 255, this will store 1 in bank and 4 in pos.
  292. // You must multiply by the value of the record size and offset by the bank
  293. // start poition to get the mmap'd memory location.
  294. //
  295. // If all you want is the memory record location, use the function
  296. // passdb_slim_get_record_address.
  297. //
  298. // Return -1 if the bank number is invalid (less than 0 or greater than
  299. // the maximum bank number)
  300. //
  301. int passdb_slim_get_cred_bank_and_pos( passdb_slim_context *ctx, int *bank, int *pos, int idx )
  302. {
  303. int q, r;
  304. if (idx < INDEX_MIDPOINT)
  305. {
  306. q = idx / ctx->n_one_cred_bank_size ;
  307. r = idx - (q * ctx->n_one_cred_bank_size);
  308. if ( (q < 0) || (q >= ctx->n_one_cred_max_bank) )
  309. {
  310. fprintf(stderr, "ERROR: passdb_slim_get_cred_bank_and_pos, bank out of range (%i,%i) from idx %i\n", q, r, idx);
  311. return -1;
  312. }
  313. }
  314. else if (idx < (2*INDEX_MIDPOINT))
  315. {
  316. q = (idx - INDEX_MIDPOINT) / ctx->n_two_cred_bank_size ;
  317. r = (idx - INDEX_MIDPOINT) - (q * ctx->n_two_cred_bank_size);
  318. if ( (q < 0) || (q >= ctx->n_two_cred_max_bank) )
  319. {
  320. fprintf(stderr, "ERROR: passdb_slim_get_cred_bank_and_pos, bank out of range (%i,%i) from idx %i\n", q, r, idx);
  321. return -1;
  322. }
  323. }
  324. else
  325. {
  326. q = (idx - (2*INDEX_MIDPOINT)) / ctx->n_spillover_bank_size ;
  327. r = (idx - (2*INDEX_MIDPOINT)) - (q * ctx->n_spillover_bank_size);
  328. if ( (q < 0) || (q >= ctx->n_spillover_max_bank) )
  329. {
  330. fprintf(stderr, "ERROR: passdb_slim_get_cred_bank_and_pos, bank out of range (%i,%i) from idx %i\n", q, r, idx);
  331. return -1;
  332. }
  333. }
  334. *bank = q;
  335. *pos = r;
  336. return 0;
  337. }
  338. //--------------------------------
  339. void make_rider_record( passdb_slim_context *ctx, rider_record *rr, int idx )
  340. {
  341. int r;
  342. void *p;
  343. rider_record_slim_one_cred rr1 = {0};
  344. rider_record_slim_two_cred rr2 = {0};
  345. p = passdb_slim_get_record_address( ctx, idx );
  346. if (p == NULL)
  347. {
  348. fprintf(stderr, "make_rider_record: passdb_slim_get_record_address returned NULL! (idx %i)\n", idx);
  349. return;
  350. }
  351. if (idx < INDEX_MIDPOINT )
  352. {
  353. populate_one_cred_rider_record( &rr1, 0, p );
  354. r = make_rider_record_from_rider_one_cred( ctx, rr, &rr1);
  355. }
  356. else if (idx < (2*INDEX_MIDPOINT))
  357. {
  358. populate_two_cred_rider_record( &rr2, 0, p );
  359. r = make_rider_record_from_rider_two_cred( ctx, rr, &rr2);
  360. }
  361. else
  362. {
  363. memcpy( rr, p , sizeof(rider_record) );
  364. }
  365. }
  366. //----
  367. int find_id_in_hash(passdb_slim_context *ctx, logical_card_id_t id)
  368. {
  369. rider_record rr = {0};
  370. rider_node *p;
  371. if(id <= 0)
  372. return FAIL_PARAM;
  373. p = ctx->logical_card_id_hash[id % ctx->hash_modulus];
  374. while(p)
  375. {
  376. make_rider_record( ctx, &rr, p->idx );
  377. if (rr.id == id)
  378. return p->idx;
  379. p = p->next;
  380. }
  381. return WARN_NOTFOUND;
  382. }
  383. int add_to_id_hash(passdb_slim_context *ctx, int idx)
  384. {
  385. rider_record rr = {0};
  386. rider_node *p, *q;
  387. unsigned int bucket;
  388. if(idx < 0)
  389. {
  390. return FAIL_PARAM;
  391. }
  392. make_rider_record( ctx, &rr, idx );
  393. if (rr.id == ID_INVALID)
  394. {
  395. return FAIL_PARAM;
  396. }
  397. bucket = rr.id % ctx->hash_modulus;
  398. FIND_IDX_IN_BUCKET( ctx->logical_card_id_hash[bucket], idx, p, q )
  399. if(p)
  400. {
  401. return FAIL_DUPKEY; //already exists!
  402. }
  403. ADD_TO_BUCKET( ctx->logical_card_id_hash[bucket], idx, p, q );
  404. return 0;
  405. }
  406. int delete_from_id_hash(passdb_slim_context *ctx, int idx)
  407. {
  408. rider_record rr = {0};
  409. rider_node *p, *q;
  410. unsigned int bucket;
  411. if(idx < 0) return FAIL_PARAM;
  412. make_rider_record( ctx, &rr, idx );
  413. bucket = rr.id % ctx->hash_modulus;
  414. FIND_IDX_IN_BUCKET( ctx->logical_card_id_hash[bucket], idx, p, q)
  415. if(p)
  416. {
  417. DEL_FROM_BUCKET( ctx->logical_card_id_hash[bucket], p, q )
  418. return 0;
  419. }
  420. else
  421. {
  422. return WARN_NOTFOUND;
  423. }
  424. }
  425. //##
  426. int find_mag_in_hash(passdb_slim_context *ctx, char *mag)
  427. {
  428. rider_record rr = {0};
  429. rider_node *p;
  430. if(mag[0] == '\0') return FAIL_PARAM;
  431. p = ctx->rider_mag_hash[stringhash(mag) % ctx->hash_modulus];
  432. while(p)
  433. {
  434. make_rider_record( ctx, &rr, p->idx );
  435. if(!strncmp(rr.magstripe_value, mag, CREDENTIAL_LEN))
  436. return p->idx;
  437. p = p->next;
  438. }
  439. return WARN_NOTFOUND;
  440. }
  441. int add_to_mag_hash(passdb_slim_context *ctx, int idx)
  442. {
  443. rider_record rr = {0};
  444. rider_node *p, *q;
  445. unsigned int bucket;
  446. if(idx < 0) return 0; //if we have a non index or a non-value, return silently
  447. make_rider_record( ctx, &rr, idx );
  448. if (rr.magstripe_value[0] == '\0') return 0;
  449. bucket = stringhash( rr.magstripe_value) % ctx->hash_modulus;
  450. FIND_IDX_IN_BUCKET( ctx->rider_mag_hash[bucket], idx, p, q )
  451. #ifndef ALLOW_CREDENTIAL_COLLISIONS
  452. //On allowing hash collisions among credentials see comment tagged **STUPID** later in this file.
  453. if(p)
  454. {
  455. return FAIL_DUPKEY; //already exists!
  456. }
  457. else
  458. #endif
  459. {
  460. ADD_TO_BUCKET( ctx->rider_mag_hash[bucket], idx, p, q );
  461. return 0;
  462. }
  463. }
  464. int delete_from_mag_hash(passdb_slim_context *ctx, int idx)
  465. {
  466. rider_record rr = {0};
  467. rider_node *p, *q;
  468. unsigned int bucket;
  469. if(idx < 0) return FAIL_PARAM;
  470. make_rider_record( ctx, &rr, idx );
  471. bucket = stringhash( rr.magstripe_value ) % ctx->hash_modulus;
  472. FIND_IDX_IN_BUCKET( ctx->rider_mag_hash[bucket], idx, p, q)
  473. if(p)
  474. {
  475. DEL_FROM_BUCKET( ctx->rider_mag_hash[bucket], p, q )
  476. return 0;
  477. }
  478. else
  479. {
  480. return WARN_NOTFOUND;
  481. }
  482. }
  483. //##
  484. int find_rf_in_hash(passdb_slim_context *ctx, char *rfid)
  485. {
  486. rider_record rr = {0} ;
  487. rider_node *p;
  488. if(rfid[0] == '\0') return WARN_NOTFOUND;
  489. p = ctx->rider_rf_hash[stringhash(rfid)% ctx->hash_modulus];
  490. while(p)
  491. {
  492. make_rider_record( ctx, &rr, p->idx );
  493. if(!strncmp( rr.rfid_value, rfid, CREDENTIAL_LEN))
  494. return p->idx;
  495. p = p->next;
  496. }
  497. return WARN_NOTFOUND;
  498. }
  499. int add_to_rf_hash(passdb_slim_context *ctx, int idx)
  500. {
  501. rider_record rr = {0};
  502. rider_node *p, *q;
  503. unsigned int bucket;
  504. if(idx < 0) return 0; //if we have a non index or a non-value, return silently
  505. make_rider_record( ctx, &rr, idx );
  506. if(rr.rfid_value[0] == '\0') return 0;
  507. bucket = stringhash(rr.rfid_value) % ctx->hash_modulus;
  508. FIND_IDX_IN_BUCKET( ctx->rider_rf_hash[bucket], idx, p, q )
  509. #ifndef ALLOW_CREDENTIAL_COLLISIONS
  510. //On allowing hash collisions among credentials see comment tagged **STUPID** later in this file.
  511. if(p)
  512. {
  513. return FAIL_DUPKEY; //already exists!
  514. }
  515. else
  516. #endif
  517. {
  518. ADD_TO_BUCKET( ctx->rider_rf_hash[bucket], idx, p, q );
  519. return 0;
  520. }
  521. }
  522. int delete_from_rf_hash(passdb_slim_context *ctx, int idx)
  523. {
  524. rider_record rr = {0} ;
  525. rider_node *p, *q;
  526. unsigned int bucket;
  527. if(idx < 0) return FAIL_PARAM;
  528. make_rider_record( ctx, &rr, idx );
  529. bucket = stringhash(rr.rfid_value) % ctx->hash_modulus;
  530. FIND_IDX_IN_BUCKET( ctx->rider_rf_hash[bucket], idx, p, q)
  531. if(p)
  532. {
  533. DEL_FROM_BUCKET( ctx->rider_rf_hash[bucket], p, q )
  534. return 0;
  535. }
  536. else
  537. {
  538. return WARN_NOTFOUND;
  539. }
  540. }
  541. //##
  542. int build_hashes(passdb_slim_context *ctx)
  543. {
  544. rider_record rr = {0};
  545. rider_node *p = ctx->activelist;
  546. int retval;
  547. while(p)
  548. {
  549. make_rider_record( ctx, &rr, p->idx );
  550. ctx->ruleparam_db->seq = ctx->seq;
  551. ruleparam_db_update( ctx->ruleparam_db, rr.rule_name, rr.rule_param , 1 );
  552. retval = add_to_id_hash(ctx, p->idx);
  553. if( !DB_OKAY(retval) )
  554. {
  555. fprintf(stderr, "Error (%d) indexing rider ID %llu at index %d!\n", retval, rr.id, p->idx);
  556. return retval;
  557. }
  558. retval = add_to_mag_hash(ctx, p->idx);
  559. if( !DB_OKAY(retval) )
  560. {
  561. fprintf(stderr, "Error (%d) indexing magstripe %s at index %d!\n", retval, rr.magstripe_value, p->idx);
  562. return -1;
  563. }
  564. retval = add_to_rf_hash(ctx, p->idx);
  565. if( !DB_OKAY(retval) )
  566. {
  567. fprintf(stderr, "Error (%d) indexing RFID %s at index %d!\n", retval, rr.rfid_value, p->idx);
  568. return -1;
  569. }
  570. p = p->next;
  571. }
  572. // Write reference counts for debugging.
  573. // Rules and params should have been recorded by
  574. // ruleparam_db_update, but we'd like, at least for debugging
  575. // purposes, to write out the reference counts. Do this
  576. // here.
  577. //
  578. ruleparam_db_clean( ctx->ruleparam_db );
  579. #ifdef PASSDB_CONSISTENCY_CHECK
  580. retval = ruleparam_db_consistency_check( ctx->ruleparam_db );
  581. if (retval < 0)
  582. {
  583. fprintf(stderr, "CONSISTENCY CHECK failed in build_hashes: got %i\n", retval);
  584. }
  585. #endif
  586. ctx->ruleparam_db->seq = ctx->seq;
  587. //ruleparam_db_save( ctx->ruleparam_db, ctx->ruleparam_db_fn );
  588. ruleparam_db_save( ctx->ruleparam_db );
  589. return 0;
  590. }
  591. // memory page size 4096
  592. int format_new_passdb( char *fn, int sz, long pagesize )
  593. {
  594. int i,n;
  595. int fd;
  596. char *blank;
  597. blank = calloc( pagesize, sizeof(char) );
  598. if (!blank)
  599. return FAIL_MEM;
  600. fd = creat(fn, S_IRUSR | S_IWUSR);
  601. if( fd < 0 )
  602. {
  603. fprintf(stderr, "Cannot create pass file %s!\n", fn);
  604. free(blank);
  605. return FAIL_DATABASE;
  606. }
  607. n = sz / pagesize;
  608. if ( (sz % pagesize ) > 0 )
  609. {
  610. n++;
  611. }
  612. for(i = 0; i < n; i++)
  613. {
  614. if( write(fd, blank, pagesize) != pagesize )
  615. {
  616. fprintf(stderr, "Cannot write blank data to passes file %s!\n", fn);
  617. free(blank);
  618. close(fd);
  619. return FAIL_DATABASE;
  620. }
  621. }
  622. free(blank);
  623. close(fd);
  624. return 0;
  625. }
  626. int format_new_passdbs()
  627. {
  628. char fn[64];
  629. int bank;
  630. int r;
  631. long pagesize;
  632. passdb_slim_config cfg;
  633. int sz;
  634. int quotient, remainder;
  635. ruleparam_db_ctx *ruleparam_ctx;
  636. pagesize = sysconf(_SC_PAGE_SIZE);
  637. r = read_config(&cfg, PASSDB_SLIM_CONFIG_FILE);
  638. if (r != 0)
  639. {
  640. printf("WARNING: %s was not read, formatting default config file\n", PASSDB_SLIM_CONFIG_FILE);
  641. make_default_config( PASSDB_SLIM_CONFIG_FILE );
  642. r = read_config(&cfg, PASSDB_SLIM_CONFIG_FILE);
  643. if (r!=0)
  644. return r;
  645. }
  646. // Try to load the ruleparam. If it doesn't exist, try to create it.
  647. // Else, just deallocate the dummy load.
  648. //
  649. r = ruleparam_db_load( &ruleparam_ctx , RULEPARAM_DB_FILE );
  650. if (r < 0)
  651. {
  652. printf("WARNING: ruleparam database %s was not read, creating a blank new one\n", RULEPARAM_DB_FILE );
  653. r = format_new_ruleparamdb( RULEPARAM_DB_FILE );
  654. if (r < 0)
  655. {
  656. perror(RULEPARAM_DB_FILE);
  657. return r;
  658. }
  659. }
  660. else
  661. {
  662. ruleparam_db_free( ruleparam_ctx );
  663. }
  664. // For creation of banks
  665. //
  666. for (bank=0; bank<cfg.n_one_cred_bank; bank++)
  667. {
  668. snprintf(fn, 64, "%s%i%s", cfg.one_cred_db_base_fn, bank, cfg.db_fn_suffix );
  669. quotient = cfg.n_one_cred_bank_size * RIDER_ONE_CRED_SIZE / pagesize;
  670. remainder = (cfg.n_one_cred_bank_size * RIDER_ONE_CRED_SIZE ) - (pagesize * quotient) ;
  671. sz = quotient * pagesize;
  672. if (remainder > 0)
  673. sz += pagesize;
  674. cfg.rider_one_file_bank_size = sz;
  675. r = format_new_passdb( fn, sz, pagesize );
  676. if (r != 0) return r;
  677. }
  678. //printf("cfg.n_two_cred_bank %i\n", cfg.n_two_cred_bank );
  679. for (bank=0; bank<cfg.n_two_cred_bank; bank++)
  680. {
  681. snprintf(fn, 64, "%s%i%s", cfg.two_cred_db_base_fn, bank, cfg.db_fn_suffix );
  682. quotient = cfg.n_two_cred_bank_size * RIDER_TWO_CRED_SIZE / pagesize;
  683. remainder = (cfg.n_two_cred_bank_size * RIDER_TWO_CRED_SIZE ) - (pagesize * quotient) ;
  684. sz = quotient * pagesize;
  685. if (remainder > 0)
  686. sz += pagesize;
  687. cfg.rider_two_file_bank_size = sz;
  688. r = format_new_passdb( fn, sz, pagesize );
  689. if (r!=0) return r;
  690. }
  691. for (bank=0; bank<cfg.n_spillover_bank; bank++)
  692. {
  693. snprintf(fn, 64, "%s%i%s", cfg.spillover_db_base_fn, bank, cfg.db_fn_suffix );
  694. quotient = cfg.n_spillover_bank_size * RIDER_SPILLOVER_SIZE / pagesize;
  695. remainder = (cfg.n_spillover_bank_size * RIDER_SPILLOVER_SIZE ) - (pagesize * quotient) ;
  696. sz = quotient * pagesize;
  697. if (remainder > 0)
  698. sz += pagesize;
  699. cfg.rider_spillover_file_bank_size = sz;
  700. r = format_new_passdb( fn, sz, pagesize );
  701. if (r!=0) return r;
  702. }
  703. free( cfg.one_cred_db_base_fn );
  704. free( cfg.two_cred_db_base_fn );
  705. free( cfg.spillover_db_base_fn );
  706. free( cfg.db_fn_suffix );
  707. free( cfg.ruleparam_db_fn );
  708. return 0;
  709. }
  710. int detach_from_passdb(passdb_slim_context *ctx)
  711. {
  712. int i;
  713. int bank;
  714. if(!ctx)
  715. return FAIL_PARAM;
  716. if (ctx->ruleparam_db)
  717. {
  718. ctx->ruleparam_db->seq = ctx->seq;
  719. //ruleparam_db_save( ctx->ruleparam_db, ctx->ruleparam_db_fn );
  720. ruleparam_db_save( ctx->ruleparam_db );
  721. }
  722. free_rider_node_list(ctx->freelist_one_cred);
  723. free_rider_node_list(ctx->freelist_two_cred);
  724. free_rider_node_list(ctx->freelist_spillover);
  725. free_rider_node_list(ctx->activelist);
  726. for(i=0; i < ctx->hash_modulus; i++)
  727. {
  728. free_rider_node_list(ctx->logical_card_id_hash[i]);
  729. free_rider_node_list(ctx->rider_mag_hash[i]);
  730. free_rider_node_list(ctx->rider_rf_hash[i]);
  731. }
  732. for (bank=0; bank<ctx->n_one_cred_bank; bank++)
  733. munmap( ctx->rider_one_cred_bank[bank], ctx->rider_one_file_bank_size );
  734. for (bank=0; bank<ctx->n_two_cred_bank; bank++)
  735. munmap( ctx->rider_two_cred_bank[bank], ctx->rider_two_file_bank_size );
  736. for (bank=0; bank<ctx->n_spillover_bank; bank++)
  737. munmap( ctx->rider_spillover_bank[bank], ctx->rider_two_file_bank_size );
  738. free( ctx->logical_card_id_hash );
  739. free( ctx->rider_mag_hash );
  740. free( ctx->rider_rf_hash );
  741. if (ctx->one_cred_db_bank_fn)
  742. {
  743. for (i=0; i<ctx->n_one_cred_max_bank; i++)
  744. {
  745. if (ctx->one_cred_db_bank_fn[i])
  746. {
  747. free(ctx->one_cred_db_bank_fn[i]);
  748. }
  749. }
  750. free(ctx->one_cred_db_bank_fn);
  751. }
  752. if (ctx->two_cred_db_bank_fn)
  753. {
  754. for (i=0; i<ctx->n_two_cred_max_bank; i++)
  755. {
  756. if (ctx->two_cred_db_bank_fn[i])
  757. {
  758. free(ctx->two_cred_db_bank_fn[i]);
  759. }
  760. }
  761. free(ctx->two_cred_db_bank_fn);
  762. }
  763. if (ctx->spillover_db_bank_fn)
  764. {
  765. for (i=0; i<ctx->n_spillover_max_bank; i++)
  766. {
  767. if (ctx->spillover_db_bank_fn[i])
  768. {
  769. free(ctx->spillover_db_bank_fn[i]);
  770. }
  771. }
  772. free(ctx->spillover_db_bank_fn);
  773. }
  774. if (ctx->one_cred_db_base_fn)
  775. free( ctx->one_cred_db_base_fn);
  776. if (ctx->two_cred_db_base_fn)
  777. free( ctx->two_cred_db_base_fn);
  778. if (ctx->spillover_db_base_fn)
  779. free( ctx->spillover_db_base_fn);
  780. if (ctx->db_fn_suffix)
  781. free( ctx->db_fn_suffix );
  782. if (ctx->ruleparam_db_fn)
  783. free( ctx->ruleparam_db_fn );
  784. if (ctx->rider_one_cred_bank)
  785. free( ctx->rider_one_cred_bank );
  786. if (ctx->rider_two_cred_bank)
  787. free( ctx->rider_two_cred_bank );
  788. if (ctx->rider_spillover_bank)
  789. free( ctx->rider_spillover_bank );
  790. for (bank=0; bank<ctx->n_one_cred_bank; bank++)
  791. close(ctx->passes_one_cred_bank_fd[bank]);
  792. for (bank=0; bank<ctx->n_two_cred_bank; bank++)
  793. close(ctx->passes_two_cred_bank_fd[bank]);
  794. for (bank=0; bank<ctx->n_spillover_bank; bank++)
  795. close(ctx->passes_spillover_bank_fd[bank]);
  796. if (ctx->passes_one_cred_bank_fd)
  797. free(ctx->passes_one_cred_bank_fd);
  798. if (ctx->passes_two_cred_bank_fd)
  799. free(ctx->passes_two_cred_bank_fd);
  800. if (ctx->passes_spillover_bank_fd)
  801. free(ctx->passes_spillover_bank_fd);
  802. if (ctx->ruleparam_db)
  803. ruleparam_db_free( ctx->ruleparam_db );
  804. memset(ctx, 0, sizeof(passdb_slim_context));
  805. return 0;
  806. }
  807. int init_mmap_passfile( int *fd, void **rider_p, size_t *file_size, size_t rider_size, char *fn )
  808. {
  809. struct stat st;
  810. void *t_rider_p;
  811. int retval;
  812. int t_fd;
  813. //int i, k;
  814. retval = stat(fn, &st);
  815. if(retval)
  816. {
  817. fprintf(stderr, "Cannot find one credential passes file %s!\n", fn);
  818. return FAIL_DATABASE;
  819. }
  820. t_fd = open( fn, O_RDWR | O_SYNC);
  821. if(t_fd < 0)
  822. {
  823. fprintf(stderr, "Cannot open one credential passes file %s!\n", fn);
  824. return FAIL_DATABASE;
  825. }
  826. t_rider_p = (void *) mmap( NULL, st.st_size , PROT_READ | PROT_WRITE, MAP_PRIVATE, t_fd, 0 );
  827. /*
  828. for (i=0; i<st.st_size; i++)
  829. {
  830. char ch = (unsigned char)(*((unsigned char *)t_rider_p));
  831. if (ch > 3)
  832. {
  833. k = 1;
  834. }
  835. else
  836. k=0;
  837. }
  838. */
  839. if ( (t_rider_p == NULL) || (t_rider_p == MAP_FAILED) )
  840. {
  841. close(t_fd);
  842. fprintf( stderr,
  843. "Cannot mmap passes file! Try checking sysctl settings kernel.shmall and kernel.shmmax (return == %p errno == %d)\n",
  844. t_rider_p, errno);
  845. return FAIL_MEM;
  846. }
  847. else
  848. {
  849. //printf("operating in braindead file IO mode...\n");
  850. }
  851. *fd = t_fd;
  852. *rider_p = t_rider_p;
  853. *file_size = st.st_size ;
  854. return 0;
  855. }
  856. int load_one_cred(passdb_slim_context *ctx)
  857. {
  858. int bank ;
  859. int i, n;
  860. rider_record_slim_one_cred rr_one;
  861. seq_t maxseq = 0;
  862. rider_node *freehead=NULL, *acthead=NULL, *q;
  863. int numfree = 0, numact = 0;
  864. n = ctx->n_one_cred_bank * ctx->n_one_cred_bank_size;
  865. if (ctx->activelist) acthead = ctx->activelist;
  866. if (ctx->freelist_one_cred) freehead = ctx->freelist_one_cred;
  867. maxseq = ctx->seq;
  868. //For all records in our flat file
  869. //
  870. for(i=0; i < n; i++)
  871. {
  872. bank = i / ctx->n_one_cred_bank_size;
  873. populate_one_cred_rider_record( &rr_one, i - (bank * ctx->n_one_cred_bank_size), ctx->rider_one_cred_bank[bank] );
  874. // Check the sequence number and update our latest tally if it is newer.
  875. //
  876. if(rr_one.seq > maxseq)
  877. {
  878. maxseq = rr_one.seq;
  879. }
  880. // If the record is not in use
  881. //
  882. if(rr_one.id == ID_INVALID)
  883. {
  884. // Add it to the freelist
  885. //
  886. q = (rider_node *) malloc( sizeof(rider_node) );
  887. if(!q)
  888. {
  889. free_rider_node_list(freehead);
  890. free_rider_node_list(acthead);
  891. fprintf(stderr, "Malloc returned NULL loading riders!\n");
  892. for (bank=0; bank<ctx->n_one_cred_bank; bank++)
  893. {
  894. munmap( ctx->rider_one_cred_bank[bank], ctx->n_one_cred_bank_size * RIDER_ONE_CRED_SIZE );
  895. }
  896. fprintf(stderr, "FAIL_MEM: load_one_cred (1)\n");
  897. return FAIL_MEM;
  898. }
  899. else
  900. {
  901. numfree++;
  902. q->next = freehead;
  903. q->idx = i;
  904. freehead = q;
  905. }
  906. }
  907. // Else it's an active record
  908. //
  909. else
  910. {
  911. // Add it to the active list
  912. //
  913. q = (rider_node *) malloc( sizeof(rider_node) );
  914. if(!q)
  915. {
  916. free_rider_node_list(freehead);
  917. free_rider_node_list(acthead);
  918. fprintf(stderr, "Malloc returned NULL loading riders!\n");
  919. for (bank=0; bank<ctx->n_one_cred_bank; bank++)
  920. {
  921. munmap( ctx->rider_one_cred_bank[bank], ctx->n_one_cred_bank_size * RIDER_ONE_CRED_SIZE );
  922. }
  923. fprintf(stderr, "FAIL_MEM: load_one_cred (2)\n");
  924. return FAIL_MEM;
  925. }
  926. else
  927. {
  928. numact++;
  929. q->next = acthead;
  930. q->idx = i;
  931. acthead = q;
  932. }
  933. }
  934. } // for bank
  935. ctx->freelist_one_cred = freehead;
  936. ctx->activelist = acthead;
  937. ctx->seq = maxseq;
  938. ctx->num_active += numact;
  939. ctx->num_free += numfree;
  940. ctx->n_one_cred = numact;
  941. return 0;
  942. }
  943. int load_two_cred(passdb_slim_context *ctx)
  944. {
  945. int bank ;
  946. int i, n;
  947. rider_record_slim_two_cred rr_two;
  948. seq_t maxseq = 0;
  949. rider_node *freehead=NULL, *acthead=NULL, *q;
  950. int numfree = 0, numact = 0;
  951. n = ctx->n_two_cred_bank * ctx->n_two_cred_bank_size ;
  952. if (ctx->freelist_two_cred) freehead = ctx->freelist_two_cred;
  953. if (ctx->activelist) acthead = ctx->activelist;
  954. maxseq = ctx->seq;
  955. for(i=0; i < n; i++)
  956. {
  957. bank = i / ctx->n_two_cred_bank_size;
  958. populate_two_cred_rider_record( &rr_two, i - (bank*ctx->n_two_cred_bank_size), ctx->rider_two_cred_bank[bank] );
  959. //check the sequence number and update our "latest" tally if it is newer.
  960. if(rr_two.seq > maxseq)
  961. {
  962. maxseq = rr_two.seq;
  963. }
  964. //if the record is not in use
  965. if(rr_two.id == ID_INVALID)
  966. {
  967. //add it to the freelist
  968. q = (rider_node *) malloc( sizeof(rider_node) );
  969. if(!q)
  970. {
  971. free_rider_node_list(freehead);
  972. free_rider_node_list(acthead);
  973. fprintf(stderr, "Malloc returned NULL loading riders!\n");
  974. for (bank=0; bank<ctx->n_two_cred_bank; bank++)
  975. {
  976. munmap( ctx->rider_two_cred_bank[bank], ctx->n_two_cred_bank_size * RIDER_ONE_CRED_SIZE );
  977. }
  978. fprintf(stderr, "FAIL_MEM: load_two_cred (1)\n");
  979. return FAIL_MEM;
  980. }
  981. else
  982. {
  983. numfree++;
  984. q->next = freehead;
  985. q->idx = i + INDEX_MIDPOINT;
  986. freehead = q;
  987. }
  988. }
  989. else
  990. {
  991. //add it to the active list
  992. q = (rider_node *) malloc( sizeof(rider_node) );
  993. if(!q)
  994. {
  995. free_rider_node_list(freehead);
  996. free_rider_node_list(acthead);
  997. fprintf(stderr, "Malloc returned NULL loading riders!\n");
  998. for (bank=0; bank<ctx->n_two_cred_bank; bank++)
  999. {
  1000. munmap( ctx->rider_two_cred_bank[bank], ctx->n_two_cred_bank_size * RIDER_ONE_CRED_SIZE );
  1001. }
  1002. fprintf(stderr, "FAIL_MEM: load_two_cred (2)\n");
  1003. return FAIL_MEM;
  1004. }
  1005. else
  1006. {
  1007. numact++;
  1008. q->next = acthead;
  1009. q->idx = i + INDEX_MIDPOINT;
  1010. acthead = q;
  1011. }
  1012. }
  1013. }
  1014. ctx->activelist = acthead;
  1015. ctx->freelist_two_cred = freehead;
  1016. ctx->seq = maxseq;
  1017. ctx->num_active += numact;
  1018. ctx->num_free += numfree;
  1019. ctx->n_two_cred = numact;
  1020. return 0;
  1021. }
  1022. // UNDER DELEVEOPMENT
  1023. //
  1024. int load_spillover(passdb_slim_context *ctx)
  1025. {
  1026. int bank ;
  1027. int i, n;
  1028. rider_record rr_spillover;
  1029. seq_t maxseq = 0;
  1030. rider_node *freehead=NULL, *acthead=NULL, *q;
  1031. int numfree = 0, numact = 0;
  1032. n = ctx->n_spillover_bank * ctx->n_spillover_bank_size ;
  1033. if (ctx->freelist_spillover) freehead = ctx->freelist_spillover;
  1034. if (ctx->activelist) acthead = ctx->activelist;
  1035. maxseq = ctx->seq;
  1036. for(i=0; i < n; i++)
  1037. {
  1038. bank = i / ctx->n_spillover_bank_size;
  1039. populate_spillover_rider_record( &rr_spillover, i - (bank*ctx->n_spillover_bank_size), ctx->rider_spillover_bank[bank] );
  1040. //check the sequence number and update our "latest" tally if it is newer.
  1041. if(rr_spillover.seq > maxseq)
  1042. {
  1043. maxseq = rr_spillover.seq;
  1044. }
  1045. //if the record is not in use
  1046. if(rr_spillover.id == ID_INVALID)
  1047. {
  1048. //add it to the freelist
  1049. q = (rider_node *) malloc( sizeof(rider_node) );
  1050. if(!q)
  1051. {
  1052. free_rider_node_list(freehead);
  1053. free_rider_node_list(acthead);
  1054. fprintf(stderr, "Malloc returned NULL loading riders!\n");
  1055. for (bank=0; bank<ctx->n_spillover_bank; bank++)
  1056. {
  1057. munmap( ctx->rider_spillover_bank[bank], ctx->n_spillover_bank_size * RIDER_ONE_CRED_SIZE );
  1058. }
  1059. fprintf(stderr, "FAIL_MEM: load_spillover (1)\n");
  1060. return FAIL_MEM;
  1061. }
  1062. else
  1063. {
  1064. numfree++;
  1065. q->next = freehead;
  1066. q->idx = i + (2*INDEX_MIDPOINT);
  1067. freehead = q;
  1068. }
  1069. }
  1070. else
  1071. {
  1072. //add it to the active list
  1073. q = (rider_node *) malloc( sizeof(rider_node) );
  1074. if(!q)
  1075. {
  1076. free_rider_node_list(freehead);
  1077. free_rider_node_list(acthead);
  1078. fprintf(stderr, "Malloc returned NULL loading riders!\n");
  1079. for (bank=0; bank<ctx->n_spillover_bank; bank++)
  1080. {
  1081. munmap( ctx->rider_spillover_bank[bank], ctx->n_spillover_bank_size * RIDER_ONE_CRED_SIZE );
  1082. }
  1083. fprintf(stderr, "FAIL_MEM: load_spillover (2)\n");
  1084. return FAIL_MEM;
  1085. }
  1086. else
  1087. {
  1088. numact++;
  1089. q->next = acthead;
  1090. q->idx = i + (2*INDEX_MIDPOINT);
  1091. acthead = q;
  1092. }
  1093. }
  1094. }
  1095. ctx->activelist = acthead;
  1096. ctx->freelist_spillover = freehead;
  1097. ctx->seq = maxseq;
  1098. ctx->num_active += numact;
  1099. ctx->num_free += numfree;
  1100. ctx->n_spillover = numact;
  1101. return 0;
  1102. }
  1103. // Slow but sure way of deleting entries. Restarting from the beginning will
  1104. // be slower than if we exploited knowledge of the data structures, but makes
  1105. // this routine much more robust and simple to code. By restarting after
  1106. // every delete, we don't need any in depth knowledge about how to search
  1107. // or delete entries other than walking through the 'activelist' and using
  1108. // 'delete_rider' to delete the entry.
  1109. //
  1110. void passdb_slim_delete_seq_above( passdb_slim_context *ctx, seq_t cur_seq )
  1111. {
  1112. int dirty = 0;
  1113. rider_node *nod;
  1114. rider_record rr;
  1115. do {
  1116. dirty = 0;
  1117. nod = ctx->activelist;
  1118. while (nod)
  1119. {
  1120. make_rider_record( ctx, &rr, nod->idx );
  1121. if (rr.seq > cur_seq)
  1122. {
  1123. delete_rider( ctx, &rr, 1 );
  1124. dirty = 1;
  1125. break;
  1126. }
  1127. nod = nod->next;
  1128. }
  1129. } while (dirty);
  1130. }
  1131. int attach_to_passdb(passdb_slim_context *ctx)
  1132. {
  1133. int retval;
  1134. int numfree = 0, numact = 0;
  1135. int read_n_one_cred;
  1136. int read_n_two_cred;
  1137. int read_n_spillover;
  1138. int i;
  1139. int n;
  1140. seq_t ruleparam_seq = 0;
  1141. //--------
  1142. if(!ctx) //fail if we get passed a null pointer
  1143. return FAIL_PARAM;
  1144. //We also want to fail if we get passed a pointer to an active/in-use context...
  1145. //
  1146. if( ctx->rider_one_cred_bank ||
  1147. ctx->rider_two_cred_bank ||
  1148. ctx->rider_spillover_bank ||
  1149. ctx->activelist ||
  1150. ctx->freelist_one_cred || ctx->freelist_two_cred )
  1151. {
  1152. return FAIL_PARAM;
  1153. }
  1154. ctx->rider_one_cred_bank = NULL;
  1155. ctx->rider_two_cred_bank = NULL;
  1156. ctx->rider_spillover_bank = NULL;
  1157. ctx->activelist = NULL;
  1158. ctx->freelist_one_cred = NULL;
  1159. ctx->freelist_two_cred = NULL;
  1160. ctx->freelist_spillover = NULL;
  1161. ctx->num_active = 0;
  1162. ctx->num_free = 0;
  1163. ctx->seq = 0;
  1164. ctx->n_one_cred = 0;
  1165. ctx->n_two_cred = 0;
  1166. ctx->n_spillover = 0;
  1167. ctx->one_cred_db_bank_fn = NULL;
  1168. ctx->two_cred_db_bank_fn = NULL;
  1169. ctx->spillover_db_bank_fn = NULL;
  1170. ctx->ruleparam_db_fn = NULL;
  1171. ctx->one_cred_db_base_fn = NULL;
  1172. ctx->two_cred_db_base_fn = NULL;
  1173. ctx->spillover_db_base_fn = NULL;
  1174. ctx->db_fn_suffix = NULL;
  1175. retval =
  1176. init_context_from_config( ctx, PASSDB_SLIM_CONFIG_FILE );
  1177. if (retval != 0)
  1178. {
  1179. fprintf(stderr, "ERROR: init_context_from_config (%s) failed, can't proceed\n", PASSDB_SLIM_CONFIG_FILE );
  1180. return retval;
  1181. }
  1182. retval = ruleparam_db_load( &(ctx->ruleparam_db), ctx->ruleparam_db_fn );
  1183. if ( !ctx->ruleparam_db )
  1184. {
  1185. fprintf(stderr, "ruleparam_db_load failed (retval %i).\n", retval);
  1186. detach_from_passdb(ctx);
  1187. return retval;
  1188. }
  1189. ruleparam_seq = ctx->ruleparam_db->seq;
  1190. for (i=0; i<ctx->n_one_cred_bank; i++)
  1191. {
  1192. retval =
  1193. init_mmap_passfile( &(ctx->passes_one_cred_bank_fd[i]),
  1194. &(ctx->rider_one_cred_bank[i]),
  1195. &(ctx->rider_one_file_size),
  1196. RIDER_ONE_CRED_SIZE,
  1197. ctx->one_cred_db_bank_fn[i] );
  1198. if (retval != 0)
  1199. {
  1200. fprintf(stderr, "ERROR: attach_to_passdb one credential: %s\n", ctx->one_cred_db_bank_fn[i]);
  1201. return retval;
  1202. }
  1203. read_n_one_cred = ctx->rider_one_file_size / RIDER_ONE_CRED_SIZE ;
  1204. if (read_n_one_cred != ctx->n_one_cred_bank_size )
  1205. {
  1206. fprintf(stderr, "WARNING: n_one_cred in config file (%i) does not match n_one_cred (%i) in db file (%s). Using db file as truth\n",
  1207. ctx->n_one_cred_bank_size, read_n_one_cred, ctx->one_cred_db_bank_fn[i] );
  1208. }
  1209. }
  1210. for (i=0; i<ctx->n_two_cred_bank; i++)
  1211. {
  1212. retval =
  1213. init_mmap_passfile( &(ctx->passes_two_cred_bank_fd[i]),
  1214. &(ctx->rider_two_cred_bank[i]),
  1215. &(ctx->rider_two_file_size),
  1216. RIDER_TWO_CRED_SIZE,
  1217. ctx->two_cred_db_bank_fn[i] );
  1218. if (retval != 0)
  1219. {
  1220. fprintf(stderr, "ERROR: attach_to_passdb two credential: %s\n", ctx->two_cred_db_bank_fn[i]);
  1221. return retval;
  1222. }
  1223. read_n_two_cred = ctx->rider_two_file_size / RIDER_TWO_CRED_SIZE ;
  1224. if (read_n_two_cred != ctx->n_two_cred_bank_size )
  1225. {
  1226. fprintf(stderr, "WARNING: n_two_cred in config file (%i) does not match n_two_cred (%i) in db file (%s). Using db file as truth\n",
  1227. ctx->n_two_cred_bank_size, read_n_two_cred, ctx->two_cred_db_bank_fn[i] );
  1228. }
  1229. }
  1230. for (i=0; i<ctx->n_spillover_bank; i++)
  1231. {
  1232. retval =
  1233. init_mmap_passfile( &(ctx->passes_spillover_bank_fd[i]),
  1234. &(ctx->rider_spillover_bank[i]),
  1235. &(ctx->rider_spillover_file_size),
  1236. RIDER_SPILLOVER_SIZE,
  1237. ctx->spillover_db_bank_fn[i] );
  1238. if (retval != 0)
  1239. {
  1240. fprintf(stderr, "ERROR: attach_to_passdb spillover: %s\n", ctx->spillover_db_bank_fn[i]);
  1241. return retval;
  1242. }
  1243. read_n_spillover = ctx->rider_spillover_file_size / RIDER_SPILLOVER_SIZE ;
  1244. if (read_n_spillover != ctx->n_spillover_bank_size )
  1245. {
  1246. fprintf(stderr, "WARNING: n_spillover in config file (%i) does not match n_spillover (%i) in db file (%s). Using db file as truth\n",
  1247. ctx->n_spillover_bank_size, read_n_spillover, ctx->spillover_db_bank_fn[i] );
  1248. }
  1249. }
  1250. //-------------
  1251. retval = load_one_cred(ctx);
  1252. if (retval != 0)
  1253. {
  1254. fprintf(stderr, "load_one_cred failed.\n");
  1255. detach_from_passdb(ctx);
  1256. return retval;
  1257. }
  1258. retval = load_two_cred(ctx);
  1259. if (retval != 0)
  1260. {
  1261. fprintf(stderr, "load_two_cred failed.\n");
  1262. detach_from_passdb(ctx);
  1263. return retval;
  1264. }
  1265. retval = load_spillover(ctx);
  1266. if (retval != 0)
  1267. {
  1268. fprintf(stderr, "load_spillover failed.\n");
  1269. detach_from_passdb(ctx);
  1270. return retval;
  1271. }
  1272. retval = build_hashes(ctx);
  1273. if( DB_FAIL(retval) )
  1274. {
  1275. fprintf(stderr, "Building hashes failed.\n");
  1276. detach_from_passdb(ctx);
  1277. return retval;
  1278. }
  1279. if (ruleparam_seq > 0)
  1280. {
  1281. // ruleparam.db didn't get a chance to flush
  1282. // after a pass db update. We set the current sequence
  1283. // number to be the one we recorded from the old
  1284. // ruleparma.db so that we pick up updates from the
  1285. // lost caedential and we go through and delete
  1286. // the old entries with sequence numbers older than
  1287. // ruleparam_db->seq.
  1288. //
  1289. // To be safe, we ake the minimum of the two sequence numbers
  1290. // as truth.
  1291. //
  1292. if (ruleparam_seq < ctx->seq)
  1293. {
  1294. //DEBUG
  1295. fprintf(stderr, "WARNING: passdb.attach_to_passdb discr. in ruleparam_seq (%llu) and mem seq (%llu): removing newer seq numbers\n", ruleparam_seq, ctx->seq);
  1296. ctx->seq = ruleparam_seq;
  1297. passdb_slim_delete_seq_above( ctx, ctx->seq );
  1298. }
  1299. }
  1300. ctx->mmap_broken = 1;
  1301. numfree = ctx->num_free;
  1302. numact = ctx->num_active;
  1303. n = numfree + numact;
  1304. // DEBUG
  1305. //
  1306. printf("Loaded and indexed %d records (%d used, %d free); Newest seq = %llu\n", n, numact, numfree, ctx->seq);
  1307. return n;
  1308. }
  1309. static int convert_magstripe( unsigned char *magstripe_code, unsigned long long *magstripe, char *magstripe_value )
  1310. {
  1311. int a;
  1312. unsigned long long b;
  1313. int cpos=0;
  1314. for ( cpos=0;
  1315. (cpos < CREDENTIAL_LEN) && ( magstripe_value[cpos] != ':' );
  1316. cpos++);
  1317. if (cpos == CREDENTIAL_LEN)
  1318. {
  1319. return -1;
  1320. }
  1321. magstripe_value[cpos] = '\0';
  1322. a = atoi(magstripe_value);
  1323. b = (unsigned long long)atoll( magstripe_value + cpos + 1 );
  1324. magstripe_value[cpos] = ':';
  1325. if ((a < 0) || (a > 255))
  1326. {
  1327. return -1;
  1328. }
  1329. *magstripe_code = (unsigned char)a;
  1330. *magstripe = b;
  1331. return 0;
  1332. }
  1333. static int convert_rfid( unsigned char *rfid_code,
  1334. unsigned long *rfid_site,
  1335. unsigned long *rfid_val,
  1336. char *rfid_value )
  1337. {
  1338. int a;
  1339. unsigned long b, c;
  1340. int cpos0, cpos1;
  1341. for ( cpos0=0;
  1342. (cpos0 < CREDENTIAL_LEN) && (rfid_value[cpos0] != ':');
  1343. cpos0++);
  1344. if (cpos0 == CREDENTIAL_LEN)
  1345. return -1;
  1346. for ( cpos1=cpos0+1;
  1347. (cpos1 < CREDENTIAL_LEN) && (rfid_value[cpos1] != ':');
  1348. cpos1++);
  1349. if (cpos1 == CREDENTIAL_LEN)
  1350. return -1;
  1351. rfid_value[cpos0] = '\0';
  1352. rfid_value[cpos1] = '\0';
  1353. a = atoi(rfid_value);
  1354. b = (unsigned long )atol(rfid_value + cpos0 + 1);
  1355. c = (unsigned long )atol(rfid_value + cpos1 + 1);
  1356. rfid_value[cpos0] = ':';
  1357. rfid_value[cpos1] = ':';
  1358. if ((a < 0) || (a > 255))
  1359. return -1;
  1360. *rfid_code = (unsigned char)a;
  1361. *rfid_site = (unsigned long)b;
  1362. *rfid_val = (unsigned long)c;
  1363. return 0;
  1364. }
  1365. static int convert_single_credential( rider_record_slim_one_cred *rr1, rider_record *rec )
  1366. {
  1367. unsigned char magstripe_code, rfid_code;
  1368. unsigned long long magstripe;
  1369. unsigned long rfid_site, rfid_val;
  1370. unsigned long long tsite, tval;
  1371. int r;
  1372. r = convert_magstripe( &magstripe_code, &magstripe, rec->magstripe_value );
  1373. if ( (r == 0) &&
  1374. ((magstripe_code != 0) || (magstripe != 0)) )
  1375. {
  1376. rr1->code = magstripe_code;
  1377. rr1->credential = magstripe;
  1378. return 0;
  1379. }
  1380. r = convert_rfid( &rfid_code, &rfid_site, &rfid_val, rec->rfid_value );
  1381. if ( (r == 0) &&
  1382. ((rfid_code != 0) || (rfid_site != 0) || (rfid_val != 0)) )
  1383. {
  1384. rr1->code = rfid_code;
  1385. tsite = rfid_site;
  1386. tval = rfid_val;
  1387. rr1->credential = (tsite << 32) | tval;
  1388. return 0;
  1389. }
  1390. return -1;
  1391. }
  1392. static int copy_rider_one_cred( passdb_slim_context *ctx, int idx, rider_record_slim_one_cred *rr1 )
  1393. {
  1394. void *p;
  1395. p = passdb_slim_get_record_address( ctx, idx );
  1396. if (!p)
  1397. {
  1398. fprintf(stderr, "ERROR: copy_rider_one_cred, got NULL address, bad index? (idx %i)\n", idx);
  1399. return -1;
  1400. }
  1401. //*((seq_t *)p) = rr1->seq;
  1402. _ulliw(p, rr1->seq);
  1403. p += sizeof(seq_t);
  1404. //*((logical_card_id_t *)p) = rr1->id;
  1405. _ulliw(p, rr1->id);
  1406. p += sizeof(logical_card_id_t);
  1407. *((unsigned char *)p) = rr1->code;
  1408. p += sizeof(unsigned char);
  1409. //*((unsigned long long *)p) = rr1->credential;
  1410. _ulliw(p, rr1->credential);
  1411. p += sizeof(unsigned long long);
  1412. //*((unsigned short int *)p) = rr1->rule_param_bucket_id;;
  1413. _usiw(p, rr1->rule_param_bucket_id);
  1414. //p += sizeof(unsigned short int );
  1415. return 0;
  1416. }
  1417. static int copy_rider_two_cred( passdb_slim_context *ctx, int idx, rider_record_slim_two_cred *rr2 )
  1418. {
  1419. void *p;
  1420. p = passdb_slim_get_record_address( ctx, idx );
  1421. if (!p)
  1422. {
  1423. fprintf(stderr, "ERROR: copy_rider_two_cred, got NULL address, bad index? (idx %i)\n", idx);
  1424. return -1;
  1425. }
  1426. //*((seq_t *)p) = rr2->seq;
  1427. _ulliw(p, rr2->seq);
  1428. p += sizeof(seq_t);
  1429. //*((logical_card_id_t *)p) = rr2->id;
  1430. _ulliw(p, rr2->id);
  1431. p += sizeof(logical_card_id_t);
  1432. *((unsigned char *)p) = rr2->magstripe_code;
  1433. p += sizeof(unsigned char);
  1434. //*((unsigned long long *)p) = rr2->magstripe;
  1435. _ulliw(p, rr2->magstripe);
  1436. p += sizeof(unsigned long long);
  1437. *((unsigned char *)p) = rr2->rfid_code;
  1438. p += sizeof(unsigned char );
  1439. //*((unsigned long *)p) = rr2->rfid_site;
  1440. _uliw(p, rr2->rfid_site);
  1441. p += sizeof(unsigned long );
  1442. //*((unsigned long *)p) = rr2->rfid_val;
  1443. _uliw(p, rr2->rfid_val);
  1444. p += sizeof(unsigned long );
  1445. //*((unsigned short int *)p) = rr2->rule_param_bucket_id;;
  1446. _usiw(p, rr2->rule_param_bucket_id);
  1447. //p += sizeof(unsigned short int );
  1448. return 0;
  1449. }
  1450. static int copy_rider_spillover( passdb_slim_context *ctx, int idx, rider_record *rec )
  1451. {
  1452. void *p;
  1453. p = passdb_slim_get_record_address( ctx, idx );
  1454. if (!p)
  1455. {
  1456. fprintf(stderr, "ERROR: copy_rider_spillover, got NULL address, bad index? (idx %i)\n", idx);
  1457. return -1;
  1458. }
  1459. memcpy(p, rec, sizeof(rider_record) );
  1460. return 0;
  1461. }
  1462. // Take rider as stored in rider record, store in either one_cred or two_cred
  1463. // DB, depending on content.
  1464. //
  1465. static int copy_rider( passdb_slim_context *ctx, int idx, rider_record *src)
  1466. {
  1467. int rule_param_bucket_id;
  1468. rider_record_slim_one_cred rr1 = {0};
  1469. rider_record_slim_two_cred rr2 = {0};
  1470. if(! (src) )
  1471. return FAIL_PARAM;
  1472. // find rule param bucket
  1473. //
  1474. rule_param_bucket_id =
  1475. ruleparam_db_find( ctx->ruleparam_db, src->rule_name, src->rule_param );
  1476. if ( rule_param_bucket_id == RULEPARAM_DB_NOT_FOUND )
  1477. {
  1478. fprintf(stderr, "ERROR: copy_rider: rule_param_bucket_id for %s %s not found\n", src->rule_name, src->rule_param);
  1479. return rule_param_bucket_id;
  1480. }
  1481. else if (rule_param_bucket_id == RULEPARAM_DB_FULL )
  1482. {
  1483. fprintf(stderr, "ERROR: copy_rider: RULEPARAM DB FULL!\n");
  1484. return rule_param_bucket_id;
  1485. }
  1486. if (idx < INDEX_MIDPOINT)
  1487. {
  1488. rr1.rule_param_bucket_id = (unsigned short int)rule_param_bucket_id;
  1489. rr1.seq = src->seq;
  1490. rr1.id = src->id;
  1491. convert_single_credential( &rr1, src );
  1492. copy_rider_one_cred( ctx, idx, &rr1 );
  1493. }
  1494. else if (idx < (2*INDEX_MIDPOINT))
  1495. {
  1496. rr2.rule_param_bucket_id = (unsigned short int)rule_param_bucket_id;
  1497. rr2.seq = src->seq;
  1498. rr2.id = src->id;
  1499. convert_magstripe( &(rr2.magstripe_code), &(rr2.magstripe), src->magstripe_value );
  1500. convert_rfid( &(rr2.rfid_code), &(rr2.rfid_site), &(rr2.rfid_val), src->rfid_value );
  1501. copy_rider_two_cred( ctx, idx, &rr2 );
  1502. }
  1503. else
  1504. {
  1505. copy_rider_spillover( ctx, idx, src );
  1506. }
  1507. return 0;
  1508. }
  1509. static int alloc_rider_one_cred(passdb_slim_context *ctx)
  1510. {
  1511. rider_node *p;
  1512. p = ctx->freelist_one_cred;
  1513. if(p)
  1514. {
  1515. ctx->freelist_one_cred = ctx->freelist_one_cred->next;
  1516. p->next = ctx->activelist;
  1517. ctx->activelist = p;
  1518. ctx->n_one_cred++;
  1519. return p->idx;
  1520. }
  1521. else
  1522. {
  1523. return FAIL_FULL;
  1524. }
  1525. }
  1526. static int alloc_rider_two_cred(passdb_slim_context *ctx)
  1527. {
  1528. rider_node *p;
  1529. p = ctx->freelist_two_cred;
  1530. if(p)
  1531. {
  1532. ctx->freelist_two_cred = ctx->freelist_two_cred->next;
  1533. p->next = ctx->activelist;
  1534. ctx->activelist = p;
  1535. ctx->n_two_cred++;
  1536. return p->idx;
  1537. }
  1538. else
  1539. {
  1540. return FAIL_FULL;
  1541. }
  1542. }
  1543. static int alloc_rider_spillover(passdb_slim_context *ctx)
  1544. {
  1545. rider_node *p;
  1546. p = ctx->freelist_spillover;
  1547. if(p)
  1548. {
  1549. ctx->freelist_spillover = ctx->freelist_spillover->next;
  1550. p->next = ctx->activelist;
  1551. ctx->activelist = p;
  1552. ctx->n_spillover++;
  1553. return p->idx;
  1554. }
  1555. else
  1556. {
  1557. return FAIL_FULL;
  1558. }
  1559. }
  1560. static int free_rider(passdb_slim_context *ctx, int idx)
  1561. {
  1562. rider_node *p, *q;
  1563. void *record_p;
  1564. q = NULL;
  1565. p = ctx->activelist;
  1566. while(p)
  1567. {
  1568. if( p->idx == idx )
  1569. break;
  1570. q = p;
  1571. p = p->next;
  1572. }
  1573. if(p)
  1574. {
  1575. if(q)
  1576. {
  1577. q->next = p->next;
  1578. }
  1579. else
  1580. {
  1581. ctx->activelist = p->next;
  1582. }
  1583. if ( idx < INDEX_MIDPOINT )
  1584. {
  1585. p->next = ctx->freelist_one_cred;
  1586. ctx->freelist_one_cred = p;
  1587. }
  1588. else if (idx < (2*INDEX_MIDPOINT))
  1589. {
  1590. p->next = ctx->freelist_two_cred;
  1591. ctx->freelist_two_cred = p;
  1592. }
  1593. else
  1594. {
  1595. p->next = ctx->freelist_spillover;
  1596. ctx->freelist_spillover = p;
  1597. }
  1598. // ID_INVALID _should_ be 0
  1599. //
  1600. record_p = passdb_slim_get_record_address( ctx, idx );
  1601. if (!record_p)
  1602. return -1;
  1603. if ( idx < INDEX_MIDPOINT )
  1604. {
  1605. memset( record_p, 0, sizeof(RIDER_ONE_CRED_SIZE) );
  1606. }
  1607. else if (idx < (2*INDEX_MIDPOINT))
  1608. {
  1609. memset( record_p, 0, sizeof(RIDER_ONE_CRED_SIZE) );
  1610. }
  1611. else
  1612. {
  1613. memset( record_p, 0, sizeof(RIDER_SPILLOVER_SIZE) );
  1614. }
  1615. return 0;
  1616. }
  1617. else
  1618. {
  1619. return WARN_NOTFOUND;
  1620. }
  1621. }
  1622. static void sync_rider_change(passdb_slim_context *ctx, int idx)
  1623. {
  1624. int bank=-1, pos=-1;
  1625. int offset;
  1626. int offset_next;
  1627. int retval;
  1628. int n_pages = 1;
  1629. int tmpa, tmpb;
  1630. retval=-1;
  1631. if(idx < 0)
  1632. return;
  1633. if(!ctx)
  1634. return;
  1635. passdb_slim_get_cred_bank_and_pos( ctx, &bank, &pos, idx );
  1636. if ( idx < INDEX_MIDPOINT )
  1637. {
  1638. offset = (pos * RIDER_ONE_CRED_SIZE) / MEMORY_PAGE_SIZE; // calculate the beginning page number
  1639. offset *= MEMORY_PAGE_SIZE; // multiply by page size
  1640. retval = lseek(ctx->passes_one_cred_bank_fd[bank], offset, SEEK_SET);
  1641. offset_next = (((pos+1)*RIDER_ONE_CRED_SIZE) - 1) / MEMORY_PAGE_SIZE;
  1642. offset_next *= MEMORY_PAGE_SIZE;
  1643. if (offset_next != offset )
  1644. {
  1645. n_pages = 2;
  1646. }
  1647. if(retval != offset)
  1648. {
  1649. fprintf(stderr, "lseek() failed in sync_rider_change(). errno = %d\n", errno);
  1650. perror("one");
  1651. return;
  1652. }
  1653. retval = write(ctx->passes_one_cred_bank_fd[bank], ctx->rider_one_cred_bank[bank] + offset, n_pages * MEMORY_PAGE_SIZE);
  1654. tmpa = offset;
  1655. tmpb = offset + RIDER_ONE_CRED_SIZE;
  1656. if( retval != (n_pages*MEMORY_PAGE_SIZE) )
  1657. {
  1658. fprintf(stderr, "write() failed (one_cred) in sync_rider_change(). errno = %d\n", errno);
  1659. return;
  1660. }
  1661. fsync(ctx->passes_one_cred_bank_fd[bank]);
  1662. }
  1663. else if ( idx < (2*INDEX_MIDPOINT) )
  1664. {
  1665. offset = (pos * RIDER_TWO_CRED_SIZE) / MEMORY_PAGE_SIZE; // calculate the beginning page number
  1666. offset *= MEMORY_PAGE_SIZE; // multiply by page size
  1667. retval = lseek(ctx->passes_two_cred_bank_fd[bank], offset, SEEK_SET);
  1668. offset_next = (((pos+1)*RIDER_TWO_CRED_SIZE) - 1) / MEMORY_PAGE_SIZE;
  1669. offset_next *= MEMORY_PAGE_SIZE;
  1670. if (offset_next != offset )
  1671. {
  1672. n_pages = 2;
  1673. }
  1674. if(retval != offset)
  1675. {
  1676. fprintf(stderr, "lseek() failed in sync_rider_change(). errno = %d\n", errno);
  1677. perror("two");
  1678. return;
  1679. }
  1680. retval = write(ctx->passes_two_cred_bank_fd[bank], ctx->rider_two_cred_bank[bank] + offset, n_pages * MEMORY_PAGE_SIZE);
  1681. if ( retval != (n_pages*MEMORY_PAGE_SIZE) )
  1682. {
  1683. fprintf(stderr, "write() failed (two_cred) in sync_rider_change(). errno = %d\n", errno);
  1684. return;
  1685. }
  1686. fsync(ctx->passes_two_cred_bank_fd[bank]);
  1687. }
  1688. else
  1689. {
  1690. // SPILLOVER should be page aligned, but in case it's not, just do the same calculation as we do above
  1691. //
  1692. offset = (pos * RIDER_SPILLOVER_SIZE) / MEMORY_PAGE_SIZE; // calculate the beginning page number
  1693. offset *= MEMORY_PAGE_SIZE; // multiply by page size
  1694. retval = lseek(ctx->passes_spillover_bank_fd[bank], offset, SEEK_SET);
  1695. offset_next = (((pos+1)*RIDER_SPILLOVER_SIZE) - 1) / MEMORY_PAGE_SIZE;
  1696. offset_next *= MEMORY_PAGE_SIZE;
  1697. if (offset_next != offset )
  1698. {
  1699. n_pages = 2;
  1700. }
  1701. if(retval != offset)
  1702. {
  1703. fprintf(stderr, "lseek() failed in sync_rider_change(). errno = %d (\n", errno);
  1704. perror("three");
  1705. return;
  1706. }
  1707. retval = write(ctx->passes_spillover_bank_fd[bank], ctx->rider_spillover_bank[bank] + offset, n_pages * MEMORY_PAGE_SIZE);
  1708. if ( retval != (n_pages*MEMORY_PAGE_SIZE) )
  1709. {
  1710. fprintf(stderr, "write() failed (spillover) in sync_rider_change(). errno = %d\n", errno);
  1711. return;
  1712. }
  1713. fsync(ctx->passes_spillover_bank_fd[bank]);
  1714. }
  1715. }
  1716. void sync_all_riders(passdb_slim_context *ctx)
  1717. {
  1718. int retval;
  1719. int bank;
  1720. if(!ctx)
  1721. return;
  1722. // flush one cred first
  1723. //
  1724. for (bank=0; bank<ctx->n_one_cred_bank; bank++)
  1725. {
  1726. retval = lseek(ctx->passes_one_cred_bank_fd[bank], 0, SEEK_SET);
  1727. if(retval != 0)
  1728. {
  1729. fprintf(stderr, "lseek() failed in sync_all_riders() for one credential DB (bank %i). errno = %d\n", bank, errno);
  1730. return;
  1731. }
  1732. retval = write(ctx->passes_one_cred_bank_fd[bank],
  1733. ctx->rider_one_cred_bank[bank],
  1734. ctx->n_one_cred_bank_size * RIDER_ONE_CRED_SIZE );
  1735. if(retval != (ctx->n_one_cred_bank_size * RIDER_ONE_CRED_SIZE) )
  1736. {
  1737. fprintf(stderr, "write() failed in sync_all_riders() for one credential DB (bank %i). errno = %d\n", bank, errno);
  1738. return;
  1739. }
  1740. }
  1741. // flush two cred next
  1742. //
  1743. for (bank=0; bank<ctx->n_two_cred_bank; bank++)
  1744. {
  1745. retval = lseek(ctx->passes_two_cred_bank_fd[bank], 0, SEEK_SET);
  1746. if(retval != 0)
  1747. {
  1748. fprintf(stderr, "lseek() failed in sync_all_riders() for two credential DB (bank %i). errno = %d\n", bank, errno);
  1749. return;
  1750. }
  1751. retval = write(ctx->passes_two_cred_bank_fd[bank],
  1752. ctx->rider_two_cred_bank[bank],
  1753. ctx->n_two_cred_bank_size * RIDER_TWO_CRED_SIZE );
  1754. if(retval != (ctx->n_two_cred_bank_size * RIDER_TWO_CRED_SIZE) )
  1755. {
  1756. fprintf(stderr, "write() failed in sync_all_riders() for two credential DB (bank %i). errno = %d\n", bank, errno);
  1757. return;
  1758. }
  1759. }
  1760. // finall flush our spillover banks
  1761. //
  1762. for (bank=0; bank<ctx->n_spillover_bank; bank++)
  1763. {
  1764. retval = lseek(ctx->passes_spillover_bank_fd[bank], 0, SEEK_SET);
  1765. if(retval != 0)
  1766. {
  1767. fprintf(stderr, "lseek() failed in sync_all_riders() for two credential DB (bank %i). errno = %d\n", bank, errno);
  1768. return;
  1769. }
  1770. retval = write(ctx->passes_spillover_bank_fd[bank],
  1771. ctx->rider_spillover_bank[bank],
  1772. ctx->n_spillover_bank_size * RIDER_SPILLOVER_SIZE );
  1773. if(retval != (ctx->n_spillover_bank_size * RIDER_SPILLOVER_SIZE) )
  1774. {
  1775. fprintf(stderr, "write() failed in sync_all_riders() for two credential DB (bank %i). errno = %d\n", bank, errno);
  1776. return;
  1777. }
  1778. }
  1779. }
  1780. int delete_rider(passdb_slim_context *ctx, rider_record *rec, int sync)
  1781. {
  1782. int bank;
  1783. int id_idx;
  1784. void *rider_p;
  1785. rider_record rr;
  1786. if(!ctx)
  1787. {
  1788. return FAIL_PARAM;
  1789. }
  1790. if (!ctx->rider_one_cred_bank) return FAIL_PARAM;
  1791. if (!ctx->rider_two_cred_bank) return FAIL_PARAM;
  1792. for (bank=0; bank<ctx->n_one_cred_bank; bank++)
  1793. if (!ctx->rider_one_cred_bank[bank]) return FAIL_PARAM;
  1794. for (bank=0; bank<ctx->n_two_cred_bank; bank++)
  1795. if (!ctx->rider_two_cred_bank[bank]) return FAIL_PARAM;
  1796. for (bank=0; bank<ctx->n_spillover_bank; bank++)
  1797. if (!ctx->rider_spillover_bank[bank]) return FAIL_PARAM;
  1798. //If this record is older than out current database, ignore it as a duplicate.
  1799. //
  1800. if( ctx->seq >= rec->seq )
  1801. {
  1802. return 0;
  1803. }
  1804. //find the record to be deleted in our ID hash
  1805. //
  1806. id_idx = find_id_in_hash(ctx, rec->id);
  1807. //If we didn't find it, it must have already been deleted...
  1808. //
  1809. if(id_idx < 0)
  1810. {
  1811. return 0;
  1812. }
  1813. make_rider_record( ctx, &rr, id_idx );
  1814. //delete it from all hashes
  1815. //
  1816. delete_from_id_hash(ctx, id_idx);
  1817. delete_from_mag_hash(ctx, id_idx);
  1818. delete_from_rf_hash(ctx, id_idx);
  1819. //free the record (this zeros out the entire block)
  1820. //
  1821. free_rider(ctx, id_idx);
  1822. passdb_slim_manage_rider_banks(ctx);
  1823. rider_p = passdb_slim_get_record_address( ctx, id_idx );
  1824. if (!rider_p)
  1825. return -1;
  1826. if (id_idx < INDEX_MIDPOINT )
  1827. {
  1828. ctx->n_one_cred--;
  1829. memset( rider_p, 0, RIDER_ONE_CRED_SIZE );
  1830. }
  1831. else if (id_idx < (2*INDEX_MIDPOINT) )
  1832. {
  1833. ctx->n_two_cred--;
  1834. memset( rider_p, 0, RIDER_TWO_CRED_SIZE );
  1835. }
  1836. else
  1837. {
  1838. ctx->n_spillover--;
  1839. memset( rider_p, 0, RIDER_SPILLOVER_SIZE );
  1840. }
  1841. ctx->num_active--;
  1842. ctx->num_free++;
  1843. ctx->ruleparam_db->seq = ctx->seq;
  1844. ruleparam_db_update( ctx->ruleparam_db, rr.rule_name, rr.rule_param, -1 );
  1845. ruleparam_db_rate_limited_sync( ctx->ruleparam_db );
  1846. //and sync our SHM
  1847. //
  1848. if(sync)
  1849. {
  1850. sync_rider_change(ctx, id_idx);
  1851. }
  1852. return 1;
  1853. }
  1854. // Return the number of non blank credentials
  1855. //
  1856. int rider_count_credential( rider_record *rec )
  1857. {
  1858. int mag_flag = 0;
  1859. int rfid_flag = 0;
  1860. int count = 0;
  1861. int s, e;
  1862. int r;
  1863. unsigned char dummy_code;
  1864. unsigned long dummy_site, dummy_rfid;
  1865. unsigned long long dummy_mag;
  1866. for (s=0; (s<CREDENTIAL_LEN) && (rec->magstripe_value[s] == ' ') ; s++);
  1867. for (e=s+1; (e<CREDENTIAL_LEN) && (rec->magstripe_value[e]) ; e++ );
  1868. if ((e - s) > 1) mag_flag = 1;
  1869. for (s=0; (s<CREDENTIAL_LEN) && (rec->rfid_value[s] == ' ') ; s++);
  1870. for (e=s+1; (e<CREDENTIAL_LEN) && (rec->rfid_value[e]) ; e++ );
  1871. if ((e - s) > 1) rfid_flag = 1;
  1872. if (mag_flag)
  1873. {
  1874. r = convert_magstripe( &dummy_code, &dummy_mag, rec->magstripe_value );
  1875. if (r < 0)
  1876. return r;
  1877. count++;
  1878. }
  1879. if (rfid_flag)
  1880. {
  1881. r = convert_rfid( &dummy_code, &dummy_site, &dummy_rfid, rec->rfid_value );
  1882. if (r < 0)
  1883. return r;
  1884. count++;
  1885. }
  1886. return count;
  1887. }
  1888. // Called from pass_communication on an UPDATE message
  1889. //
  1890. int update_rider(passdb_slim_context *ctx, rider_record *rec, int sync)
  1891. {
  1892. int id_idx;
  1893. int mag_idx;
  1894. int rf_idx;
  1895. int update_credentials = 0;
  1896. int update_id_hash = 0;
  1897. int retval;
  1898. int credential_count = 0;
  1899. int old_credential_count = 0;
  1900. int is_new_record = 0;
  1901. int bank;
  1902. int decrement_old_rule_ref = 0;
  1903. rider_record rr = {0};
  1904. if(!ctx)
  1905. {
  1906. return FAIL_MEM;
  1907. }
  1908. if (!ctx->rider_one_cred_bank) return FAIL_PARAM;
  1909. if (!ctx->rider_two_cred_bank) return FAIL_PARAM;
  1910. for (bank=0; bank<ctx->n_one_cred_bank; bank++)
  1911. if (!ctx->rider_one_cred_bank[bank]) return FAIL_PARAM;
  1912. for (bank=0; bank<ctx->n_two_cred_bank; bank++)
  1913. if (!ctx->rider_two_cred_bank[bank]) return FAIL_PARAM;
  1914. if( ctx->seq >= rec->seq )
  1915. {
  1916. return 0;
  1917. }
  1918. credential_count = rider_count_credential( rec );
  1919. old_credential_count = credential_count;
  1920. if (credential_count == 0)
  1921. {
  1922. return 0;
  1923. }
  1924. id_idx = find_id_in_hash(ctx, rec->id);
  1925. mag_idx = find_mag_in_hash(ctx, rec->magstripe_value);
  1926. rf_idx = find_rf_in_hash(ctx, rec->rfid_value);
  1927. // We want to allow a short period of magstrip or RFID collision as the lesser of two evils vs. **STUPID**
  1928. // possibly losing a record due to a degenerately stupid administrator doing the following:
  1929. //
  1930. // 1) Create user 1 with magstripe '1:foo'
  1931. // 2) Delete user 1
  1932. // 3) Create user 2 with magstripe '1:foo'
  1933. // 4) Create user 1 with magstripe '1:bar' <---- THIS IS NOT ALLOWED (Creating user 1 is dissalowed after a delete of user 1. This card should be created as user 3).
  1934. //
  1935. // The issue here is that if the bus asks what's happensed since sequence number 1, it will get rows
  1936. // 3 and 4.
  1937. //
  1938. // In reality, we'd hope that each bus would complete a sync at least once on a shorter interval
  1939. // than the frequency at which credentials are recycled, but you never know... And if somebody manually
  1940. // fucks things up such that a user id (card id) is deleted, and then created again (this is a big no-no), we
  1941. // can recover by allowing a hash collision to exist in the meantime.
  1942. #ifndef ALLOW_CREDENTIAL_COLLISIONS
  1943. if( (mag_idx >= 0) && (mag_idx != id_idx) )
  1944. {
  1945. //fprintf(stderr, "Refusing to accept change that would introduce duplicate magstripe \"%s\" for records %llu and %llu.\n", rec->magstripe_value, ctx->riders[mag_idx].id, rec->id);
  1946. make_rider_record( ctx, &rr, id_idx );
  1947. fprintf(stderr, "Refusing to accept change that would introduce duplicate magstripe \"%s\" for records %llu and %llu.\n", rec->magstripe_value, rr.id, rec->id);
  1948. return FAIL_DUPKEY;
  1949. }
  1950. if( (rf_idx >= 0) && (rf_idx != id_idx) )
  1951. {
  1952. make_rider_record( ctx, &rr, id_idx );
  1953. fprintf(stderr, "Refusing to accept change that would introduce duplicate RFID \"%s\" for records %llu and %llu.\n", rec->rfid_value, rr.id, rec->id);
  1954. return FAIL_DUPKEY;
  1955. }
  1956. #endif
  1957. if(id_idx >= 0) //if rec->id already exists, we're updating an existing record...
  1958. {
  1959. // If EITHER the RFID or MAGSTRIPE values have changed...
  1960. //
  1961. make_rider_record( ctx, &rr, id_idx );
  1962. if( strncmp(rr.magstripe_value, rec->magstripe_value, CREDENTIAL_LEN) ||
  1963. strncmp(rr.rfid_value, rec->rfid_value, CREDENTIAL_LEN) )
  1964. {
  1965. update_credentials = 1;
  1966. }
  1967. // EXPERIMENTAL
  1968. //
  1969. if ( id_idx < INDEX_MIDPOINT ) { old_credential_count = 1; }
  1970. else if ( id_idx < 2*INDEX_MIDPOINT ) { old_credential_count = 2; }
  1971. else { old_credential_count = -1; }
  1972. /*
  1973. if ( strncmp( rr.magstripe_value , rec->magstripe_value, CREDENTIAL_LEN ) != 0 )
  1974. update_magstrip_credential = 1;
  1975. if ( strncmp( rr.rfid_value, rec->rfid_value, CREDENTIAL_LEN ) != 0 )
  1976. update_rfid_credential = 1;
  1977. */
  1978. // For simplicity, decrement the reference count of the rule for the pre-existing
  1979. // rule record. The 'new' rule record will be incremented below. If the 'new'
  1980. // rule record is the same, this has the effect of not changing the reference count.
  1981. // If there is only one reference, this will delete and re-create it, but we
  1982. // expect rule records to most often have more than just a single reference, so
  1983. // this should be a case that is not hit very often.
  1984. // If the rule record has changed, this will correctly decrement the reference
  1985. // to the old rule record here, and increment the reference count of the new
  1986. // rule record below.
  1987. //
  1988. decrement_old_rule_ref = 1;
  1989. }
  1990. // EXPERIMENTAL
  1991. //
  1992. // If we've changed credential count (most likely 1 to 2 or 2 to 1),
  1993. // then we need to switch the database type (e.g. from the one credential
  1994. // database banks to the two credential database banks).
  1995. // Delete the old entry of the rider, both on disk and in our local
  1996. // hashes in memory, allocate a new index, then proceed as normal.
  1997. //
  1998. // If this is a new record, old_credential_count is default
  1999. // equal to credential count, so this block gets ignored.
  2000. //
  2001. if ( credential_count != old_credential_count )
  2002. {
  2003. // Delete old entry completely. delete_rider will
  2004. // update counts and delete the index from the hashes.
  2005. //
  2006. // We need to 'trick' delete_rider into accepting the change
  2007. // by setting the sequence number to be the current one from
  2008. // the fetched record. delete_rider also updates (decmeents etc.)
  2009. // the rule, so we don't need to do it later.
  2010. //
  2011. rr.seq = rec->seq;
  2012. delete_rider( ctx, &rr, 1 );
  2013. decrement_old_rule_ref = 0;
  2014. // Create new index and update counts
  2015. //
  2016. if ( credential_count == 1 ) { id_idx = alloc_rider_one_cred(ctx); }
  2017. else if ( credential_count == 2 ) { id_idx = alloc_rider_two_cred(ctx); }
  2018. else { id_idx = alloc_rider_spillover(ctx); }
  2019. update_credentials = update_id_hash = 1;
  2020. }
  2021. if (id_idx < 0) // we've got a new record
  2022. //else // otherwise, we're creating a new record...
  2023. {
  2024. if ( credential_count == 1 ) { id_idx = alloc_rider_one_cred(ctx); }
  2025. else if ( credential_count == 2 ) { id_idx = alloc_rider_two_cred(ctx); }
  2026. else { id_idx = alloc_rider_spillover(ctx); }
  2027. if(DB_FAIL(id_idx))
  2028. {
  2029. fprintf(stderr, "Error (%d) trying to allocate rider\n", id_idx);
  2030. return id_idx;
  2031. }
  2032. ctx->num_active++;
  2033. ctx->num_free--;
  2034. update_credentials = update_id_hash = 1;
  2035. is_new_record = 1;
  2036. }
  2037. if(update_credentials)
  2038. {
  2039. delete_from_mag_hash(ctx, id_idx);
  2040. delete_from_rf_hash(ctx, id_idx);
  2041. }
  2042. if(update_id_hash)
  2043. {
  2044. delete_from_id_hash(ctx, id_idx);
  2045. }
  2046. // Update our reference count to the rule/rule param for this
  2047. // rider record. ruleparam_db_update will automatically create
  2048. // a new rule if necessary and, if a new rule is created, will
  2049. // synchronize to it's database.
  2050. //
  2051. // We need to update ruleparam_db so that the record will be found
  2052. // by copy_record. After it's been updated, we can decrement
  2053. // the reference count if need be.
  2054. //
  2055. // At the very end, update ruleparam_db context with most recent
  2056. // sequence number and save the sequence to disk.
  2057. //
  2058. // - If we die after update but before copy_record completes,
  2059. // we have old sequence number, at worst a new rule/param will be inserted
  2060. // - If we die after copy_record, the record will have newer sequence number
  2061. // and get deleted, waiting for a replay
  2062. // - If we die after rule/param decrement, and the rule gets removed from ruleparam.db,
  2063. // credential will also get removed as it has newer sequnce number.
  2064. // - once we write sequence number, we're in a consistent state
  2065. //
  2066. ruleparam_db_update( ctx->ruleparam_db, rec->rule_name, rec->rule_param, 1 );
  2067. #ifdef PASSDB_CONSISTENCY_CHECK
  2068. retval = ruleparam_db_consistency_check( ctx->ruleparam_db );
  2069. if (retval < 0)
  2070. {
  2071. fprintf(stderr, "CONSISTENCY CHECK failed in update_rider: got %i\n", retval );
  2072. }
  2073. #endif
  2074. copy_rider( ctx, id_idx, rec );
  2075. if (decrement_old_rule_ref)
  2076. {
  2077. ruleparam_db_update( ctx->ruleparam_db, rr.rule_name, rr.rule_param, -1 );
  2078. #ifdef PASSDB_CONSISTENCY_CHECK
  2079. retval = ruleparam_db_consistency_check( ctx->ruleparam_db );
  2080. if (retval < 0)
  2081. {
  2082. fprintf(stderr, "CONSISTENCY CHECK failed in update_rider: got %i\n", retval );
  2083. }
  2084. #endif
  2085. }
  2086. passdb_slim_manage_rider_banks(ctx);
  2087. if(sync)
  2088. {
  2089. sync_rider_change(ctx, id_idx);
  2090. }
  2091. if(update_id_hash)
  2092. {
  2093. retval = add_to_id_hash(ctx, id_idx);
  2094. if( !DB_OKAY(retval) )
  2095. {
  2096. fprintf(stderr, "Error (%d) updating id hash for record seq = %llu\n", retval, (unsigned long long)rec->seq);
  2097. }
  2098. }
  2099. if(update_credentials)
  2100. {
  2101. retval = add_to_mag_hash(ctx, id_idx);
  2102. if( !DB_OKAY(retval) )
  2103. {
  2104. fprintf(stderr, "Error (%d) updating magstripe hash for record seq = %llu\n", retval, (unsigned long long)rec->seq);
  2105. }
  2106. retval = add_to_rf_hash(ctx, id_idx);
  2107. if( !DB_OKAY(retval) )
  2108. {
  2109. fprintf(stderr, "Error (%d) updating rf hash for record seq = %llu\n", retval, (unsigned long long)rec->seq);
  2110. }
  2111. }
  2112. ctx->seq = rec->seq;
  2113. // Rate limited sync always updates sequence number file,
  2114. // occassionally updates ruleparam.db. ruleparam.db updated
  2115. // above with relevant information if we added or removed
  2116. // rules.
  2117. //
  2118. ctx->ruleparam_db->seq = ctx->seq;
  2119. ruleparam_db_rate_limited_sync( ctx->ruleparam_db );
  2120. return 1;
  2121. }
  2122. void dump_hashes(passdb_slim_context *ctx)
  2123. {
  2124. int i;
  2125. rider_node *p;
  2126. rider_record rr = {0};
  2127. if(!ctx)
  2128. {
  2129. printf("NULL Context!\n");
  2130. return;
  2131. }
  2132. printf("ID HASH[%i]:\n", ctx->hash_modulus);
  2133. for(i=0; i < ctx->hash_modulus; i++)
  2134. {
  2135. if(!ctx->logical_card_id_hash[i]) continue;
  2136. printf("\t%d:", i);
  2137. p = ctx->logical_card_id_hash[i];
  2138. while(p)
  2139. {
  2140. make_rider_record(ctx, &rr, p->idx);
  2141. printf(" [%d] %llu", p->idx, rr.id );
  2142. p = p -> next;
  2143. }
  2144. printf("\n");
  2145. }
  2146. printf("RFID HASH[%i]:\n", ctx->hash_modulus);
  2147. for(i=0; i < ctx->hash_modulus; i++)
  2148. {
  2149. if(!ctx->rider_rf_hash[i]) continue;
  2150. printf("\t%d:", i);
  2151. p = ctx->rider_rf_hash[i];
  2152. while(p)
  2153. {
  2154. make_rider_record(ctx, &rr, p->idx);
  2155. printf(" [%d] %llu", p->idx, rr.id );
  2156. p = p -> next;
  2157. }
  2158. printf("\n");
  2159. }
  2160. printf("MAGSTRIPE HASH[%i]:\n", ctx->hash_modulus);
  2161. for(i=0; i < ctx->hash_modulus; i++)
  2162. {
  2163. if(!ctx->rider_mag_hash[i]) continue;
  2164. printf("\t%d:", i);
  2165. p = ctx->rider_mag_hash[i];
  2166. while(p)
  2167. {
  2168. make_rider_record(ctx, &rr, p->idx);
  2169. printf(" [%d] %llu", p->idx, rr.id );
  2170. p = p -> next;
  2171. }
  2172. printf("\n");
  2173. }
  2174. };
  2175. //-- database expansion functions
  2176. int expand_one_cred(passdb_slim_context *ctx)
  2177. {
  2178. int i;
  2179. char fn[FN_SZ];
  2180. int cur_bank;
  2181. int fd;
  2182. void *p;
  2183. size_t file_size;
  2184. int r;
  2185. long pagesize;
  2186. rider_node *q = NULL;
  2187. pagesize = sysconf(_SC_PAGE_SIZE);
  2188. cur_bank = ctx->n_one_cred_bank;
  2189. sprintf(fn, "%s%i%s", ctx->one_cred_db_base_fn, cur_bank, ctx->db_fn_suffix );
  2190. r = format_new_passdb( fn, ctx->n_one_cred_bank_size * RIDER_ONE_CRED_SIZE, pagesize );
  2191. if (r<0)
  2192. return r;
  2193. r = init_mmap_passfile( &fd, &p, &file_size, RIDER_ONE_CRED_SIZE, fn );
  2194. if (r<0)
  2195. return r;
  2196. for (i=0; i < ctx->n_one_cred_bank_size; i++)
  2197. {
  2198. q = (rider_node *) malloc( sizeof(rider_node) );
  2199. if (!q)
  2200. {
  2201. fprintf(stderr, "FAIL_MEM: expand_one_cred\n");
  2202. return FAIL_MEM;
  2203. }
  2204. q->next = ctx->freelist_one_cred;
  2205. q->idx = i + (cur_bank * ctx->n_one_cred_bank_size);
  2206. ctx->freelist_one_cred = q;
  2207. ctx->num_free++;
  2208. }
  2209. ctx->passes_one_cred_bank_fd[cur_bank] = fd;
  2210. ctx->rider_one_cred_bank[cur_bank] = p;
  2211. snprintf( ctx->one_cred_db_bank_fn[cur_bank] , FN_SZ, "%s", fn );
  2212. ctx->n_one_cred_bank++;
  2213. return 0;
  2214. }
  2215. // unused and untested
  2216. // EXPERIMENTAL:
  2217. // We only expand. In the future we might want to contract, so this
  2218. // function is left here for a starting point. Contraction still
  2219. // needs work, so only use this as a starting point.
  2220. //
  2221. /*
  2222. int contract_one_cred(passdb_slim_context *ctx)
  2223. {
  2224. int cur_bank;
  2225. int min_idx, max_idx;
  2226. rider_node *freenod = NULL, *prevnod = NULL, *nexnod = NULL;
  2227. void *p;
  2228. rider_record rr = {0};
  2229. int old_idx, new_idx;
  2230. cur_bank = ctx->n_one_cred_bank-1;
  2231. if (cur_bank < 0) return -1;
  2232. min_idx = cur_bank * ctx->n_one_cred_bank_size ;
  2233. max_idx = (cur_bank + 1) * ctx->n_one_cred_bank_size ;
  2234. // delete all nodes in the free list that fall into the
  2235. // bank we're about to delete.
  2236. // We'll be moving current riders into lower banks
  2237. // and choosing free indexes based on this list,
  2238. // so it needs to happen first.
  2239. //
  2240. freenod = ctx->freelist_one_cred;
  2241. while (freenod)
  2242. {
  2243. nexnod = freenod->next;
  2244. if ( (freenod->idx >= min_idx) &&
  2245. (freenod->idx < max_idx) )
  2246. {
  2247. if (prevnod)
  2248. prevnod->next = nexnod;
  2249. free(freenod);
  2250. }
  2251. else
  2252. {
  2253. prevnod = freenod;
  2254. }
  2255. freenod = nexnod;
  2256. }
  2257. // Now find all records that fall within the index
  2258. // range, delete all references to it in the
  2259. // id, mag and rf hash. To be "safe", 0
  2260. // out record in the old bank.
  2261. // Allocate a new index (from the lower banks)
  2262. // and put it back in.
  2263. //
  2264. freenod = ctx->activelist;
  2265. while (freenod)
  2266. {
  2267. nexnod = freenod->next;
  2268. if ( (freenod->idx < min_idx) &&
  2269. (freenod->idx >= max_idx) )
  2270. {
  2271. prevnod = freenod;
  2272. freenod = nexnod;
  2273. continue;
  2274. }
  2275. old_idx = freenod->idx;
  2276. make_rider_record( ctx, &rr, old_idx );
  2277. delete_from_id_hash(ctx, old_idx);
  2278. delete_from_mag_hash(ctx, old_idx);
  2279. delete_from_rf_hash(ctx, old_idx);
  2280. //just in case?
  2281. p = passdb_slim_get_record_address( ctx, old_idx );
  2282. memset(p, 0, sizeof(char)*RIDER_ONE_CRED_SIZE );
  2283. new_idx = alloc_rider_one_cred( ctx );
  2284. copy_rider( ctx, new_idx, &rr );
  2285. free(freenod);
  2286. if (prevnod)
  2287. prevnod->next = nexnod;
  2288. freenod = nexnod;
  2289. }
  2290. // finally, munmap the file, free the relevant memory
  2291. // and decrement the number of one credential banks.
  2292. //
  2293. munmap( ctx->rider_one_cred_bank[cur_bank], ctx->n_one_cred_bank_size * RIDER_ONE_CRED_SIZE );
  2294. ctx->rider_one_cred_bank[cur_bank] = NULL;
  2295. close(ctx->passes_two_cred_bank_fd[cur_bank]);
  2296. ctx->passes_one_cred_bank_fd[cur_bank] = -1;
  2297. free( ctx->one_cred_db_bank_fn[cur_bank] );
  2298. ctx->one_cred_db_bank_fn[cur_bank] = NULL;
  2299. ctx->n_one_cred_bank--;
  2300. return 0;
  2301. }
  2302. */
  2303. //-----
  2304. int expand_two_cred(passdb_slim_context *ctx)
  2305. {
  2306. int i;
  2307. char fn[FN_SZ];
  2308. int cur_bank;
  2309. int fd;
  2310. void *p;
  2311. size_t file_size;
  2312. int r;
  2313. long pagesize;
  2314. rider_node *q = NULL;
  2315. pagesize = sysconf(_SC_PAGE_SIZE);
  2316. cur_bank = ctx->n_two_cred_bank;
  2317. sprintf(fn, "%s%i%s", ctx->two_cred_db_base_fn, cur_bank, ctx->db_fn_suffix );
  2318. r = format_new_passdb( fn, ctx->n_two_cred_bank_size * RIDER_TWO_CRED_SIZE, pagesize );
  2319. if (r<0)
  2320. return r;
  2321. r = init_mmap_passfile( &fd, &p, &file_size, RIDER_TWO_CRED_SIZE, fn );
  2322. if (r<0)
  2323. return r;
  2324. for (i=0; i < ctx->n_two_cred_bank_size; i++)
  2325. {
  2326. q = (rider_node *) malloc( sizeof(rider_node) );
  2327. if (!q)
  2328. {
  2329. fprintf(stderr, "FAIL_MEM: expand_two_cred\n");
  2330. return FAIL_MEM;
  2331. }
  2332. q->next = ctx->freelist_two_cred;
  2333. q->idx = INDEX_MIDPOINT + i + (cur_bank * ctx->n_two_cred_bank_size);
  2334. ctx->freelist_two_cred = q;
  2335. ctx->num_free++;
  2336. }
  2337. ctx->passes_two_cred_bank_fd[cur_bank] = fd;
  2338. ctx->rider_two_cred_bank[cur_bank] = p;
  2339. snprintf( ctx->two_cred_db_bank_fn[cur_bank] , FN_SZ, "%s", fn );
  2340. ctx->n_two_cred_bank++;
  2341. return 0;
  2342. }
  2343. // unused and untested
  2344. //
  2345. // EXPERIMENTAL:
  2346. // We only expand. In the future we might want to contract, so this
  2347. // function is left here for a starting point. Contraction still
  2348. // needs work, so only use this as a starting point.
  2349. //
  2350. /*
  2351. int contract_two_cred(passdb_slim_context *ctx)
  2352. {
  2353. int cur_bank;
  2354. int min_idx, max_idx;
  2355. rider_node *freenod = NULL, *prevnod = NULL, *nexnod = NULL;
  2356. void *p;
  2357. rider_record rr = {0};
  2358. int old_idx, new_idx;
  2359. cur_bank = ctx->n_two_cred_bank-1;
  2360. if (cur_bank < 0) return -1;
  2361. min_idx = cur_bank * ctx->n_two_cred_bank_size ;
  2362. max_idx = (cur_bank + 1) * ctx->n_two_cred_bank_size ;
  2363. min_idx += INDEX_MIDPOINT;
  2364. max_idx += INDEX_MIDPOINT;
  2365. // delete all nodes in the free list that fall into the
  2366. // bank we're about to delete.
  2367. // We'll be moving current riders into lower banks
  2368. // and choosing free indexes based on this list,
  2369. // so it needs to happen first.
  2370. //
  2371. freenod = ctx->freelist_two_cred;
  2372. while (freenod)
  2373. {
  2374. nexnod = freenod->next;
  2375. if ( (freenod->idx >= min_idx) &&
  2376. (freenod->idx < max_idx) )
  2377. {
  2378. if (prevnod)
  2379. prevnod->next = nexnod;
  2380. free(freenod);
  2381. }
  2382. else
  2383. {
  2384. prevnod = freenod;
  2385. }
  2386. freenod = nexnod;
  2387. }
  2388. // Now find all records that fall within the index
  2389. // range, delete all references to it in the
  2390. // id, mag and rf hash. To be "safe", 0
  2391. // out record in the old bank.
  2392. // Allocate a new index (from the lower banks)
  2393. // and put it back in.
  2394. //
  2395. freenod = ctx->activelist;
  2396. while (freenod)
  2397. {
  2398. nexnod = freenod->next;
  2399. if ( (freenod->idx < min_idx) &&
  2400. (freenod->idx >= max_idx) )
  2401. {
  2402. prevnod = freenod;
  2403. freenod = nexnod;
  2404. continue;
  2405. }
  2406. old_idx = freenod->idx;
  2407. make_rider_record( ctx, &rr, old_idx );
  2408. delete_from_id_hash(ctx, old_idx);
  2409. delete_from_mag_hash(ctx, old_idx);
  2410. delete_from_rf_hash(ctx, old_idx);
  2411. //just in case?
  2412. p = passdb_slim_get_record_address( ctx, old_idx );
  2413. memset(p, 0, sizeof(char)*RIDER_ONE_CRED_SIZE );
  2414. new_idx = alloc_rider_two_cred( ctx );
  2415. copy_rider( ctx, new_idx, &rr );
  2416. free(freenod);
  2417. if (prevnod)
  2418. prevnod->next = nexnod;
  2419. freenod = nexnod;
  2420. }
  2421. // finally, munmap the file, free the relevant memory
  2422. // and decrement the number of two credential banks.
  2423. //
  2424. munmap( ctx->rider_two_cred_bank[cur_bank], ctx->n_two_cred_bank_size * RIDER_TWO_CRED_SIZE );
  2425. ctx->rider_two_cred_bank[cur_bank] = NULL;
  2426. close(ctx->passes_two_cred_bank_fd[cur_bank]);
  2427. ctx->passes_two_cred_bank_fd[cur_bank] = -1;
  2428. free( ctx->two_cred_db_bank_fn[cur_bank] );
  2429. ctx->two_cred_db_bank_fn[cur_bank] = NULL;
  2430. ctx->n_two_cred_bank--;
  2431. return 0;
  2432. }
  2433. */
  2434. // ---
  2435. // SPILLOVER
  2436. //
  2437. int expand_spillover(passdb_slim_context *ctx)
  2438. {
  2439. int i;
  2440. char fn[FN_SZ];
  2441. int cur_bank;
  2442. int fd;
  2443. void *p;
  2444. size_t file_size;
  2445. int r;
  2446. long pagesize;
  2447. rider_node *q = NULL;
  2448. pagesize = sysconf(_SC_PAGE_SIZE);
  2449. cur_bank = ctx->n_spillover_bank;
  2450. sprintf(fn, "%s%i%s", ctx->spillover_db_base_fn, cur_bank, ctx->db_fn_suffix );
  2451. r = format_new_passdb( fn, ctx->n_spillover_bank_size * RIDER_SPILLOVER_SIZE, pagesize );
  2452. if (r<0)
  2453. return r;
  2454. r = init_mmap_passfile( &fd, &p, &file_size, RIDER_SPILLOVER_SIZE, fn );
  2455. if (r<0)
  2456. return r;
  2457. for (i=0; i < ctx->n_spillover_bank_size; i++)
  2458. {
  2459. q = (rider_node *) malloc( sizeof(rider_node) );
  2460. if (!q)
  2461. {
  2462. fprintf(stderr, "FAIL_MEM: expand_spillover\n");
  2463. return FAIL_MEM;
  2464. }
  2465. q->next = ctx->freelist_spillover;
  2466. q->idx = (2*INDEX_MIDPOINT) + i + (cur_bank * ctx->n_spillover_bank_size);
  2467. ctx->freelist_spillover = q;
  2468. ctx->num_free++;
  2469. }
  2470. ctx->passes_spillover_bank_fd[cur_bank] = fd;
  2471. ctx->rider_spillover_bank[cur_bank] = p;
  2472. snprintf( ctx->spillover_db_bank_fn[cur_bank] , FN_SZ, "%s", fn );
  2473. ctx->n_spillover_bank++;
  2474. return 0;
  2475. }
  2476. int passdb_slim_manage_rider_banks(passdb_slim_context *ctx)
  2477. {
  2478. int r;
  2479. int upper_threshold;
  2480. int lower_threshold;
  2481. float hi_water;
  2482. float lo_water;
  2483. int bank_sz;
  2484. int max_bank;
  2485. int n;
  2486. int n_bank;
  2487. int save_config_flag = 0;
  2488. passdb_slim_config cfg = {0};
  2489. hi_water = ctx->high_watermark_threshold;
  2490. lo_water = ctx->low_watermark_threshold;
  2491. n = ctx->n_one_cred;
  2492. n_bank = ctx->n_one_cred_bank;
  2493. bank_sz = ctx->n_one_cred_bank_size;
  2494. max_bank = ctx->n_one_cred_max_bank;
  2495. //check one credenteial watermarks
  2496. //
  2497. upper_threshold = (int)( hi_water * (float)(bank_sz * n_bank) );
  2498. lower_threshold = (int)( lo_water * (float)(bank_sz * n_bank) );
  2499. // make sure we haven't maxed out our number of banks
  2500. //
  2501. if ( n_bank < max_bank )
  2502. {
  2503. if ( ctx->n_one_cred > upper_threshold )
  2504. {
  2505. r = expand_one_cred(ctx);
  2506. if (r<0) return r;
  2507. save_config_flag = 1;
  2508. }
  2509. }
  2510. n = ctx->n_two_cred;
  2511. n_bank = ctx->n_two_cred_bank;
  2512. bank_sz = ctx->n_two_cred_bank_size;
  2513. max_bank = ctx->n_two_cred_max_bank;
  2514. //check two credenteial watermarks
  2515. //
  2516. upper_threshold = (int)( hi_water * (float)(bank_sz * n_bank) );
  2517. lower_threshold = (int)( lo_water * (float)(bank_sz * n_bank) );
  2518. // make sure we haven't maxed out our number of banks
  2519. //
  2520. if ( n_bank < max_bank )
  2521. {
  2522. if ( n > upper_threshold )
  2523. {
  2524. r = expand_two_cred(ctx);
  2525. if (r<0) return r;
  2526. save_config_flag = 1;
  2527. }
  2528. }
  2529. n = ctx->n_spillover;
  2530. n_bank = ctx->n_spillover_bank;
  2531. bank_sz = ctx->n_spillover_bank_size;
  2532. max_bank = ctx->n_spillover_max_bank;
  2533. //check two credenteial watermarks
  2534. //
  2535. upper_threshold = (int)( hi_water * (float)(bank_sz * n_bank) );
  2536. lower_threshold = (int)( lo_water * (float)(bank_sz * n_bank) );
  2537. // make sure we haven't maxed out our number of banks
  2538. //
  2539. if ( n_bank < max_bank )
  2540. {
  2541. if ( n > upper_threshold )
  2542. {
  2543. r = expand_spillover(ctx);
  2544. if (r<0) return r;
  2545. save_config_flag = 1;
  2546. }
  2547. }
  2548. // --
  2549. if (save_config_flag)
  2550. {
  2551. passdb_slim_copy_config( &cfg, ctx );
  2552. save_config( &cfg, PASSDB_SLIM_CONFIG_FILE );
  2553. }
  2554. return 0;
  2555. }
  2556. // ------------------------------------------------
  2557. // CONSISTENCY CHECK
  2558. int _consist_update_ruleparam_entry( passdb_slim_ruleparam **rule_head, rider_record *rr )
  2559. {
  2560. static int id=1;
  2561. passdb_slim_ruleparam *nod = NULL, *prv = NULL;
  2562. nod = *rule_head;
  2563. if ( !nod )
  2564. {
  2565. nod = _alloc_ruleparam_db_nod( id++, rr->rule_name, rr->rule_param );
  2566. nod->reference_count++;
  2567. *rule_head = nod;
  2568. return id;
  2569. }
  2570. while (nod)
  2571. {
  2572. if ( (strncmp( nod->name , rr->rule_name , RULENAME_LEN) == 0) &&
  2573. (strncmp( nod->param, rr->rule_param , PARAM_LEN ) == 0) )
  2574. {
  2575. nod->reference_count++;
  2576. return nod->id;
  2577. }
  2578. prv = nod;
  2579. nod = nod->next;
  2580. }
  2581. prv->next = _alloc_ruleparam_db_nod( id++, rr->rule_name, rr->rule_param );
  2582. nod = prv->next;
  2583. nod->reference_count++;
  2584. return id;
  2585. }
  2586. int passdb_slim_consistency_check( passdb_slim_context *ctx )
  2587. {
  2588. //int idx;
  2589. int acount1=0, acount2=0, acounts=0;
  2590. int fcount1=0, fcount2=0, fcounts=0;
  2591. int tot1=0, tot2=0, tots=0;
  2592. int bank_acount1=0, bank_acount2=0, bank_acounts=0;
  2593. int bank, pos;
  2594. void *rider_p;
  2595. int k;
  2596. int rule_id;
  2597. char name[1024], param[1024];
  2598. rider_node *nod;
  2599. rider_record_slim_one_cred rr_one_cred;
  2600. rider_record_slim_two_cred rr_two_cred;
  2601. rider_record rr_spillover;
  2602. rider_record rr;
  2603. passdb_slim_ruleparam *rule_head = NULL, *rule_nod = NULL;
  2604. nod = ctx->activelist;
  2605. while (nod)
  2606. {
  2607. if ( nod->idx < INDEX_MIDPOINT )
  2608. {
  2609. acount1++;
  2610. tot1++;
  2611. }
  2612. else if (nod->idx < (2*INDEX_MIDPOINT))
  2613. {
  2614. acount2++;
  2615. tot2++;
  2616. }
  2617. else
  2618. {
  2619. acounts++;
  2620. tots++;
  2621. }
  2622. nod = nod->next;
  2623. }
  2624. nod = ctx->freelist_one_cred;
  2625. while (nod)
  2626. {
  2627. tot1++;
  2628. fcount1++;
  2629. nod = nod->next;
  2630. }
  2631. nod = ctx->freelist_two_cred;
  2632. while (nod)
  2633. {
  2634. tot2++;
  2635. fcount2++;
  2636. nod = nod->next;
  2637. }
  2638. nod = ctx->freelist_spillover;
  2639. while (nod)
  2640. {
  2641. tots++;
  2642. fcounts++;
  2643. nod = nod->next;
  2644. }
  2645. // Check that the active record count for each
  2646. // type appears in the banks and the counts match up
  2647. if ( tot1 != (ctx->n_one_cred_bank * ctx->n_one_cred_bank_size) )
  2648. return -1;
  2649. if ( tot2 != (ctx->n_two_cred_bank * ctx->n_two_cred_bank_size) )
  2650. return -2;
  2651. if ( tots != (ctx->n_spillover_bank * ctx->n_spillover_bank_size) )
  2652. return -3;
  2653. if ( acount1 != ctx->n_one_cred )
  2654. return -4;
  2655. if ( acount2 != ctx->n_two_cred )
  2656. return -5;
  2657. if ( acounts != ctx->n_spillover )
  2658. return -6;
  2659. // go through each of the elements in the banks and
  2660. // make sure they appear properly in the hash
  2661. //
  2662. // OK, this is a bit hard as we don't keep the information
  2663. // about what index is associated with which entry in the actual
  2664. // bank. That is, from the active list we can infer the position
  2665. // in the bank, but from the bank we would need to search the whole
  2666. // active list.
  2667. // Instead, count all entries. If the counts match up and all
  2668. // active list entries map properly, we can then infer consistency
  2669. // between the active list and the banks.
  2670. //
  2671. for (bank=0; bank < ctx->n_one_cred_bank; bank++)
  2672. {
  2673. for (pos=0; pos < ctx->n_one_cred_bank_size; pos++)
  2674. {
  2675. rider_p = ctx->rider_one_cred_bank[bank] + (pos*RIDER_ONE_CRED_SIZE);
  2676. populate_one_cred_rider_record( &rr_one_cred, 0, rider_p );
  2677. if (rr_one_cred.id == ID_INVALID)
  2678. continue;
  2679. bank_acount1++;
  2680. }
  2681. }
  2682. for (bank=0; bank < ctx->n_two_cred_bank; bank++)
  2683. {
  2684. for (pos=0; pos < ctx->n_two_cred_bank_size; pos++)
  2685. {
  2686. rider_p = ctx->rider_two_cred_bank[bank] + (pos*RIDER_TWO_CRED_SIZE);
  2687. populate_two_cred_rider_record( &rr_two_cred, 0, rider_p );
  2688. if (rr_two_cred.id == ID_INVALID)
  2689. continue;
  2690. bank_acount2++;
  2691. }
  2692. }
  2693. for (bank=0; bank < ctx->n_spillover_bank; bank++)
  2694. {
  2695. for (pos=0; pos < ctx->n_spillover_bank_size; pos++)
  2696. {
  2697. rider_p = ctx->rider_spillover_bank[bank] + (pos*RIDER_SPILLOVER_SIZE);
  2698. populate_spillover_rider_record( &rr_spillover, 0, rider_p );
  2699. if (rr_spillover.id == ID_INVALID)
  2700. continue;
  2701. bank_acounts++;
  2702. }
  2703. }
  2704. if ( (bank_acount1 != acount1) ||
  2705. (bank_acount2 != acount2) ||
  2706. (bank_acounts != acounts) )
  2707. {
  2708. fprintf(stderr, "CONSISTENCY ERROR: bank_acount1 (%i) !=? acount1 (%i), bank_acount2 (%i) !=? acount2 (%i) bank_acounts (%i) !=? acounts (%i)\n",
  2709. bank_acount1, acount1,
  2710. bank_acount2, acount2,
  2711. bank_acounts, acounts );
  2712. return -8;
  2713. }
  2714. // Make sure all records in activelist have a valid
  2715. // entry in the appropriate bank.
  2716. //
  2717. nod = ctx->activelist;
  2718. while (nod)
  2719. {
  2720. make_rider_record( ctx, &rr, nod->idx );
  2721. if (rr.id == ID_INVALID)
  2722. return -9;
  2723. nod = nod->next;
  2724. }
  2725. // we get to this point, hash and banks match up:
  2726. // go through all active records, add to a temporary
  2727. // rule param list and count the number of references,
  2728. // make sure it matches up to the ruleparam stored
  2729. //
  2730. // construct our own version of the rule names and parameters
  2731. //
  2732. nod = ctx->activelist;
  2733. while (nod)
  2734. {
  2735. make_rider_record( ctx, &rr, nod->idx );
  2736. _consist_update_ruleparam_entry( &rule_head, &rr );
  2737. nod = nod->next;
  2738. }
  2739. // compare each reference count to what we have in memory
  2740. //
  2741. rule_nod = rule_head;
  2742. while (rule_nod)
  2743. {
  2744. if ( rule_nod->reference_count != (k=ruleparam_db_reference_count( ctx->ruleparam_db, rule_nod->name, rule_nod->param )) )
  2745. {
  2746. rule_id = ruleparam_db_find( ctx->ruleparam_db, rule_nod->name, rule_nod->param );
  2747. ruleparam_db_get( name, param, ctx->ruleparam_db, rule_id );
  2748. fprintf(stderr, "CONSISTENCY ERROR: ruleparam_db reference count error: (%s,%s %i) != (%s,%s %i)\n",
  2749. rule_nod->name, rule_nod->param, rule_nod->reference_count,
  2750. name, param, k );
  2751. return -10;
  2752. }
  2753. rule_nod = rule_nod->next;
  2754. }
  2755. // free our local copy of the rule param
  2756. //
  2757. rule_nod = rule_head ;
  2758. while (rule_nod)
  2759. {
  2760. rule_nod = rule_nod->next;
  2761. free(rule_head);
  2762. rule_head = rule_nod;
  2763. }
  2764. return 1;
  2765. }
  2766. /*
  2767. int main(int argc, char **argv)
  2768. {
  2769. passdb_slim_context ctx = {0};
  2770. int retval;
  2771. rider_record foo = {12362, 6, "SILLYRF", "FOOBAR", "STUPIDRULE", "STUPIDPARAM"};
  2772. rider_record bar = {12354, 7, "WAWAWA", "SILLYMAG", "STUPIDRULE", "STUPIDPARAM"};
  2773. rider_record baz = {12361, 8, "", "BARFOLA_MAG", "STUPIDRULE2", "STUPIDPARAM2"};
  2774. rider_record bat = {12363, 7, "", "", "", ""};
  2775. retval = attach_to_passdb(&ctx);
  2776. dump_hashes(&ctx);
  2777. retval = update_rider(&ctx, &foo);
  2778. retval = update_rider(&ctx, &bar);
  2779. retval = update_rider(&ctx, &baz);
  2780. retval = delete_rider(&ctx, &bat);
  2781. dump_hashes(&ctx);
  2782. return 0;
  2783. }
  2784. */