auth.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <string.h>
  4. #include "auth.h"
  5. #include "fingerdb.h"
  6. #include "launcherd.h"
  7. /************************************
  8. if username == NULL, then search all matches
  9. if username != NULL, then search match for specific user
  10. return
  11. positive as finger id which match
  12. 0 if no match found
  13. negative on error
  14. ************************************/
  15. int authSearchMatch( unsigned char* fingerImage, char* username ){
  16. int res;
  17. int foundId;
  18. int currentId;
  19. int currentThreshold;
  20. char currentUsername[40];
  21. void* db;
  22. struct xyt_packed probe[NBIS_XYT_MAX_COUNT];
  23. int probeCount;
  24. struct xyt_packed gallery[NBIS_XYT_MAX_COUNT];
  25. int galleryCount;
  26. foundId = 0;
  27. //find minutiae in probe
  28. probeCount = nbisPgmToMinutiae( fingerImage, probe );
  29. if( probeCount < 0 ){
  30. printf( "Probe to minutiae error\n" );
  31. return -1;
  32. }
  33. //open finger db
  34. db = fingerdbOpen();
  35. if( !db ){
  36. printf( "Fingerdb open error\n" );
  37. return -2;
  38. }
  39. l_nextFinger:
  40. currentId = fingerdbSearchNextId( db );
  41. if( !currentId ) goto l_close;//end of db
  42. //is username is set, then check it match
  43. if( username ){
  44. res = fingerdbGetField( currentId, "user", currentUsername, 32 );
  45. if( res < 1 ) goto l_nextFinger;
  46. currentUsername[res] = 0x00;//string must be null-terminated
  47. if( strcmp(username,currentUsername) ) goto l_nextFinger;
  48. }
  49. //read threshold
  50. res = fingerdbGetField( currentId, "threshold", &currentThreshold, sizeof(currentThreshold) );
  51. if( res != sizeof(currentThreshold) ) goto l_nextFinger;
  52. if( currentThreshold < 1 ) goto l_nextFinger;
  53. //read packed minutiae
  54. res = fingerdbGetField( currentId, "xyt_packed", gallery, NBIS_XYT_MAX_COUNT * sizeof(*gallery) );
  55. if( res < 1 ){
  56. printf( "WARNING: Finger %i has not minutiae!\n", currentId );
  57. goto l_nextFinger;
  58. }
  59. //calculate num of gallery minutia
  60. galleryCount = res / sizeof( *gallery );
  61. //check match score
  62. if( nbisBozorth3Packed(probe, probeCount, gallery, galleryCount) < currentThreshold ) goto l_nextFinger;
  63. foundId = currentId;
  64. l_close:
  65. fingerdbClose( db );
  66. return foundId;
  67. }
  68. void authFingerTrigger( struct driverInstance* this ){
  69. int res;
  70. int fingerId;
  71. struct launcherdRequest rq;
  72. //search any match in finger DB
  73. fingerId = authSearchMatch( this->data, NULL );
  74. if( fingerId < 1 ){
  75. printf( "[trigger] No match found\n" );
  76. goto l_authTrigger;
  77. }
  78. printf( "[trigger] detected finger %i\n", fingerId );
  79. //get finger owner
  80. res = fingerdbGetField( fingerId, "user", &rq.username, sizeof(rq.username)-1 );
  81. if( res < 1 ){
  82. printf( "[trigger] Finger %i has not user.\n", fingerId );
  83. goto l_authTrigger;
  84. }
  85. //if passive auth enabled check match username
  86. if( this->passiveAuthUsername ){
  87. if( !strcmp(this->passiveAuthUsername, rq.username) ){
  88. this->passiveAuthModeIndicator = PA_SUCCESS;
  89. goto l_PA_send_answer;
  90. }
  91. }
  92. //get finger action
  93. res = fingerdbGetField( fingerId, "action", &rq.programm, sizeof(rq.programm)-1 );
  94. if( res < 1 ){
  95. printf( "[trigger] Finger %i has not action.\n", fingerId );
  96. goto l_authTrigger;
  97. }
  98. //strings must be null-terminated
  99. rq.username[sizeof(rq.username)-1] = 0x00;
  100. rq.programm[sizeof(rq.programm)-1] = 0x00;
  101. //send command to launcherd
  102. if( write(this->launcherdSocket, &rq, sizeof(rq)) != sizeof(rq) ){
  103. perror( "[trigger] Send request to launcherd failed" );
  104. }
  105. return;
  106. l_authTrigger:
  107. //if passive auth enabled, then send auth failed
  108. if( this->passiveAuthUsername ){
  109. this->passiveAuthModeIndicator = PA_FAILED;
  110. l_PA_send_answer:
  111. this->driverState = STATE_WAIT_IPC;
  112. sem_post( &this->semaphoreIpc );
  113. sem_wait( &this->semaphoreScan );
  114. this->driverState = STATE_RUNNING;
  115. }
  116. }
  117. /********************************
  118. func authEnroll
  119. register new fingerprint
  120. input
  121. xyts - pointer to minutiae
  122. xyt_count - number of minutiae
  123. return
  124. negative on error
  125. positive as new finger id
  126. ********************************/
  127. int authEnroll( struct xyt_packed* xyts, int xyt_count ){
  128. int res;
  129. int new_id;
  130. //create new record in the DB
  131. new_id = fingerdbCreateFinger();
  132. if( new_id < 0 ){
  133. printf( "%s failed to create new record in db\n", __FUNCTION__ );
  134. return -2;
  135. }
  136. //save minutiae in the DB
  137. res = fingerdbSetField( new_id, "xyt_packed", xyts, sizeof(*xyts) * xyt_count );
  138. if( res ){
  139. printf( "%s save minutiae error\n", __FUNCTION__ );
  140. return -3;
  141. }
  142. return new_id;
  143. }
  144. /**************************************************************
  145. func authTest
  146. calculate match score between exist and provided fingerprint
  147. input
  148. fingerId - existing finger id (gallery)
  149. fingerImage - provided finger image (probe)
  150. return
  151. negative on error
  152. match score
  153. ***************************************************************/
  154. int authTest( int fingerId, unsigned char* fingerImage ){
  155. struct xyt_packed gallery[NBIS_XYT_MAX_COUNT];
  156. struct xyt_packed probe[NBIS_XYT_MAX_COUNT];
  157. int galleryCount;
  158. int probeCount;
  159. //check id exist
  160. if( !fingerdbCheckIdExist(fingerId) ){
  161. printf( "%s) Fingerdb has not record with id %i.\n", __FUNCTION__, fingerId );
  162. return -1;
  163. }
  164. //open gallery packed minutia
  165. galleryCount = fingerdbGetField( fingerId, "xyt_packed", gallery, sizeof(*gallery) * NBIS_XYT_MAX_COUNT );
  166. if( galleryCount <= 0 ){
  167. printf( "%s) Load gallery error.\n", __FUNCTION__ );
  168. return -2;
  169. }
  170. //convert size to count
  171. galleryCount /= sizeof(struct xyt_packed);
  172. //convert finger image to minutiae
  173. probeCount = nbisPgmToMinutiae( fingerImage, probe );
  174. if( probeCount < 0 ) return -3;
  175. return nbisBozorth3Packed( probe, probeCount, gallery, galleryCount );
  176. }