Advertisement
_darkjoker_

RFID login system

Jun 2nd, 2013
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.99 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <windows.h>
  5. #include <mysql/mysql.h>
  6. #include "rs232.h"
  7.  
  8. #define BDRATE  9600
  9. #define USERNAME    "toor"
  10. #define PASSWORD    "password"
  11. #define DATABASE    "test"
  12. #define HOSTNAME    "192.168.1.6"
  13.  
  14.  
  15. char *isValid (char *login) {
  16.     MYSQL *c;
  17.     MYSQL_RES *res;
  18.     MYSQL_ROW row;
  19.     int f,i;
  20.     char *user;
  21.     char query[128];
  22.     c = mysql_init (NULL);
  23.     if (mysql_real_connect (c,HOSTNAME,USERNAME,PASSWORD,DATABASE,0,NULL,0)==NULL) {
  24.         return NULL;
  25.     }
  26.     else {
  27.         sprintf (query, "SELECT username FROM users WHERE login = '%s'",login);
  28.         mysql_query (c,query);
  29.         res = mysql_store_result (c);
  30.         f = mysql_num_fields (res);
  31.         while (row = mysql_fetch_row (res)) {
  32.             if (row[0]) {
  33.                 user = malloc (32);
  34.                 strcpy (user, row[0]);
  35.                 mysql_free_result (res);
  36.                 mysql_close (c);
  37.                 return user;
  38.                
  39.             }
  40.         }
  41.         mysql_free_result (res);
  42.     }
  43.     mysql_close (c);
  44.     return NULL;
  45. }
  46.  
  47. int findReader () {
  48.     int i,c,len;
  49.     char b[512];
  50.     int outcpy = dup (1);
  51.     fclose (stdout);
  52.     for (c=0;c<16;c++) {
  53.         if (!OpenComport (c,BDRATE)) {
  54.             SendBuf (c,"i\r",2);
  55.             for (i=0;i<10;i++) {
  56.                 len = PollComport (c, b, sizeof(b));
  57.                 if (len && !strncmp (b,"i\r\nMOD-RFID1356\r\n",17)) {
  58.                     dup2 (outcpy, 1);
  59.                     return c;
  60.                 }
  61.                 sleep (100);
  62.             }
  63.         }
  64.         CloseComport (c);
  65.     }
  66.     dup2 (outcpy, 1);
  67.     return -1;
  68. }
  69.  
  70. char *readTag (int cport) {
  71.     int i,len,d;
  72.     char buf[512],tmp[64],req[64],ch,*ret,*ptr;
  73.     SendBuf (cport, "r\r", 2);
  74.     memset (buf,0,sizeof(buf));
  75.     for (i=0;i<100;i++) {
  76.         len = PollComport(cport, tmp, sizeof(tmp));
  77.         if (len) {
  78.             strncat (buf, tmp, len);
  79.         }
  80.         if (!strncmp (buf,"r\r\n\r\n-",6)) {
  81.             strcpy (req, "c50,50,2023");
  82.             strncat (req, buf+6, 16);
  83.             strcat (req, "0004\r");
  84.             memset (buf,0,sizeof(buf));
  85.             for (d=0;d<3 && !strlen(buf);d++) {
  86.                 SendBuf (cport, req, strlen(req));
  87.                 for (d=0;d<100;d++) {
  88.                     memset (tmp, 0, sizeof (tmp));
  89.                     len = PollComport (cport, tmp, sizeof(tmp));
  90.                     if (len) {
  91.                         if (!strstr (tmp, "!e")) {
  92.                             strncat (buf, tmp, len-1);
  93.                             while (PollComport (cport, &ch, 1)) {
  94.                                 strncat (buf, &ch, 1);
  95.                             }
  96.                         }
  97.                         break;
  98.                     }
  99.                     sleep (100);
  100.                 }
  101.             }
  102.             ptr = strstr (buf,"<00");
  103.             if (ptr!=NULL) {
  104.                 ptr+=3;
  105.                 ret = malloc (strlen(ptr)-2);
  106.                 memset (ret,0,strlen(ptr)-2);
  107.                 strncpy (ret, ptr, strlen(ptr)-3);
  108.                 return ret;
  109.             }
  110.             return NULL;
  111.         }
  112.         sleep (100);
  113.     }
  114.     return NULL;
  115. }
  116.  
  117. int main () {
  118.     int cport;
  119.     char ch;
  120.     char buf[512];
  121.     char *new, hash[33],*user;
  122.     cport = findReader ();
  123.     if (cport<0) {
  124.         printf ("Reader not found!\n");
  125.         CloseComport (cport);
  126.         return 0;
  127.     }
  128.    
  129.     new=NULL;
  130.     while (new==NULL) {
  131.         new = readTag (cport);
  132.     }
  133.     strncpy (hash, new, 32);
  134.     hash[32]=0;
  135.     user = isValid (hash);
  136.     if (user!=NULL) {
  137.         printf ("Welcome, %s!\n",user);
  138.         free (user);
  139.     }
  140.     else {
  141.         printf ("Login failed!\n");
  142.     }
  143.     free (new);
  144.    
  145.     CloseComport (cport);
  146.    
  147.     scanf ("%c",&ch);
  148.     return 0;
  149. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement