Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <windows.h>
- #include <mysql/mysql.h>
- #include "rs232.h"
- #define BDRATE 9600
- #define USERNAME "toor"
- #define PASSWORD "password"
- #define DATABASE "test"
- #define HOSTNAME "192.168.1.6"
- char *isValid (char *login) {
- MYSQL *c;
- MYSQL_RES *res;
- MYSQL_ROW row;
- int f,i;
- char *user;
- char query[128];
- c = mysql_init (NULL);
- if (mysql_real_connect (c,HOSTNAME,USERNAME,PASSWORD,DATABASE,0,NULL,0)==NULL) {
- return NULL;
- }
- else {
- sprintf (query, "SELECT username FROM users WHERE login = '%s'",login);
- mysql_query (c,query);
- res = mysql_store_result (c);
- f = mysql_num_fields (res);
- while (row = mysql_fetch_row (res)) {
- if (row[0]) {
- user = malloc (32);
- strcpy (user, row[0]);
- mysql_free_result (res);
- mysql_close (c);
- return user;
- }
- }
- mysql_free_result (res);
- }
- mysql_close (c);
- return NULL;
- }
- int findReader () {
- int i,c,len;
- char b[512];
- int outcpy = dup (1);
- fclose (stdout);
- for (c=0;c<16;c++) {
- if (!OpenComport (c,BDRATE)) {
- SendBuf (c,"i\r",2);
- for (i=0;i<10;i++) {
- len = PollComport (c, b, sizeof(b));
- if (len && !strncmp (b,"i\r\nMOD-RFID1356\r\n",17)) {
- dup2 (outcpy, 1);
- return c;
- }
- sleep (100);
- }
- }
- CloseComport (c);
- }
- dup2 (outcpy, 1);
- return -1;
- }
- char *readTag (int cport) {
- int i,len,d;
- char buf[512],tmp[64],req[64],ch,*ret,*ptr;
- SendBuf (cport, "r\r", 2);
- memset (buf,0,sizeof(buf));
- for (i=0;i<100;i++) {
- len = PollComport(cport, tmp, sizeof(tmp));
- if (len) {
- strncat (buf, tmp, len);
- }
- if (!strncmp (buf,"r\r\n\r\n-",6)) {
- strcpy (req, "c50,50,2023");
- strncat (req, buf+6, 16);
- strcat (req, "0004\r");
- memset (buf,0,sizeof(buf));
- for (d=0;d<3 && !strlen(buf);d++) {
- SendBuf (cport, req, strlen(req));
- for (d=0;d<100;d++) {
- memset (tmp, 0, sizeof (tmp));
- len = PollComport (cport, tmp, sizeof(tmp));
- if (len) {
- if (!strstr (tmp, "!e")) {
- strncat (buf, tmp, len-1);
- while (PollComport (cport, &ch, 1)) {
- strncat (buf, &ch, 1);
- }
- }
- break;
- }
- sleep (100);
- }
- }
- ptr = strstr (buf,"<00");
- if (ptr!=NULL) {
- ptr+=3;
- ret = malloc (strlen(ptr)-2);
- memset (ret,0,strlen(ptr)-2);
- strncpy (ret, ptr, strlen(ptr)-3);
- return ret;
- }
- return NULL;
- }
- sleep (100);
- }
- return NULL;
- }
- int main () {
- int cport;
- char ch;
- char buf[512];
- char *new, hash[33],*user;
- cport = findReader ();
- if (cport<0) {
- printf ("Reader not found!\n");
- CloseComport (cport);
- return 0;
- }
- new=NULL;
- while (new==NULL) {
- new = readTag (cport);
- }
- strncpy (hash, new, 32);
- hash[32]=0;
- user = isValid (hash);
- if (user!=NULL) {
- printf ("Welcome, %s!\n",user);
- free (user);
- }
- else {
- printf ("Login failed!\n");
- }
- free (new);
- CloseComport (cport);
- scanf ("%c",&ch);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement