passdb_slim.c 89 KB

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