Merge branch 'master' of hackerspace.pl:q3k/hackfridge

Conflicts:
	terminal/CMakeLists.txt
master
Tomek Dubrownik 2012-05-01 20:08:41 +02:00
commit 03c2a7aa38
11 changed files with 276 additions and 32 deletions

3
.gitignore vendored
View File

@ -8,3 +8,6 @@ install_manifest.txt
*.o
hf-terminal
test-barcode
config.h

View File

@ -1,8 +1,9 @@
project(hf-terminal)
add_executable(hf-terminal main.c tts.c base64.c ldap.c nfc.c network.c)
add_executable(hf-terminal main.c tts.c base64.c ldap.c nfc.c network.c barcode.c)
add_executable(hash-one hash-one.c ldap.c)
set(CMAKE_C_FLAGS "-std=c99 -g -I/usr/local/include -L/usr/local/lib")
add_executable(test-barcode test-barcode.c barcode.c)
target_link_libraries(hf-terminal nfc m ldap crypto ssl)
target_link_libraries(hash-one m crypto ldap)

109
terminal/barcode.c Normal file
View File

@ -0,0 +1,109 @@
#include <termios.h>
#include <unistd.h>
#include <stdio.h>
#include <fcntl.h>
#include <string.h>
#include <sys/select.h>
#include <sys/time.h>
#include "config.h"
int g_BarcodeFD;
int barcode_initialize(char *Device)
{
g_BarcodeFD = open(Device, O_RDWR | O_NONBLOCK);
if (g_BarcodeFD == -1)
{
printf("Failed to open barcode reader TTY.\n");
return 1;
}
struct termios Config;
if (!isatty(g_BarcodeFD))
{
printf("Barcode TTY is not a TTY!\n");
return 1;
}
if (tcgetattr(g_BarcodeFD, &Config))
{
printf("Could not get barcode TTY config.\n");
return 1;
}
memset(&Config, 0, sizeof(Config));
Config.c_iflag = 0;
Config.c_oflag = 0;
Config.c_lflag = 0;
Config.c_cflag = CS8|CREAD|CLOCAL;
Config.c_cc[VMIN] = 1;
Config.c_cc[VTIME] = 0;
if (cfsetispeed(&Config, B9600) < 0 || cfsetospeed(&Config, B9600) < 0)
{
printf("Could not set barcode TTY baudrate!\n");
return 1;
}
if (tcsetattr(g_BarcodeFD, TCSAFLUSH, &Config) < 0)
{
printf("Could not set barcode TTY attributes!\n");
return 1;
}
tcflush(g_BarcodeFD, TCIOFLUSH);
return 0;
}
int _async_read_timeout(int FD, char *DataOut, int NumBytes, int Timeout)
{
fd_set Read, Write, Special;
FD_ZERO(&Read);
FD_ZERO(&Write);
FD_ZERO(&Special);
FD_SET(FD, &Read);
struct timeval TTimeout;
memset(&TTimeout, 0, sizeof(TTimeout));
TTimeout.tv_sec = Timeout;
int ResultFD = select(FD + 1, &Read, &Write, &Special, &TTimeout);
if (ResultFD == -1)
{
return -1;
}
return read(FD, DataOut, NumBytes);
}
int barcode_read(char *BarcodeOut)
{
int BytesRead = 0;
// read the first byte with a long timeout
BytesRead += _async_read_timeout(g_BarcodeFD, BarcodeOut + BytesRead, 14, BARCODE_TIMEOUT);
if (BytesRead == -1)
{
printf("Timeout on reading first barcode bytes.\n");
return 1;
}
while (BytesRead < 14)
{
int Result = _async_read_timeout(g_BarcodeFD, BarcodeOut + BytesRead, 14 - BytesRead, 1);
if (Result == -1)
{
printf("Timeout on reading following bytecode bytes.\n");
return 1;
}
BytesRead += Result;
}
BarcodeOut[13] = 0;
return 0;
}

7
terminal/barcode.h Normal file
View File

@ -0,0 +1,7 @@
#ifndef __BARCODE_H__
#define __BARCODE_H__
int barcode_initialize(char *Device);
int barcode_read(char *BarcodeOut);
#endif

View File

@ -7,4 +7,7 @@
#define LDAP_BASE "ou=Peole,dc=somecorp,dc=com"
#define LDAP_FILTER_HASHES "objectClass=hsMember"
#define BARCODE_PORT "/dev/ttyUSB0"
#define BARCODE_TIMEOUT 20
#endif

View File

@ -12,6 +12,9 @@
const int NO_SUCH_CARD = LDAP_NO_SUCH_OBJECT;
// fucking gcc
size_t strnlen(const char *s, size_t maxlen);
static char* ldap_attrs_hashes[] = {
"uid",
"mifareIDHash",
@ -65,7 +68,7 @@ int ldap_dn_by_mifare(char *MifareID, char *DNOut, int *DNLength)
nentries = ldap_count_entries(ld, msg);
result = LDAP_NO_SUCH_OBJECT;
for(entry = ldap_first_entry(ld, msg); entry != NULL; entry = ldap_next_entry(ld, entry)) {
values = ldap_get_values(ld, entry, "mifareIDHash");
values = (unsigned char**)ldap_get_values(ld, entry, "mifareIDHash");
if(values) {
for(i = 0; values[i] != NULL; ++i) {
bzero(entry_salt, 20);
@ -81,7 +84,7 @@ int ldap_dn_by_mifare(char *MifareID, char *DNOut, int *DNLength)
result = 0;
}
}
ldap_value_free(values);
ldap_value_free((char **)values);
}
}
search_finalize:
@ -90,3 +93,36 @@ finalize:
ldap_unbind_s(ld);
return result;
}
// because fuck you that's why
int ldap_uid_from_dn(char *DN, char *UIDOut)
{
char *KeyPair = DN;
int KeyPairLength = 0;
for (char *i = DN; i < DN + strlen(DN); i++)
{
if (*i == ',')
{
char *KeyPairSplit;
for (KeyPairSplit = KeyPair; *KeyPairSplit != '='; KeyPairSplit++) {}
int KeyLength = KeyPairSplit - KeyPair;
char *Key = KeyPair;
int ValueLength = KeyPairLength - KeyLength - 1;
char *Value = KeyPairSplit + 1;
if (KeyLength == 3 && strncmp(Key, "uid", KeyLength) == 0)
{
memcpy(UIDOut, Value, ValueLength);
UIDOut[ValueLength] = 0;
}
KeyPair = i + 1;
KeyPairLength = 0;
}
else
KeyPairLength++;
}
}

View File

@ -2,6 +2,8 @@
#define __LDAP_H__
int ldap_dn_by_mifare(char *MifareID, char *DNOut, int *DNLength);
int hash_mifare(char *MifareID, char *salt, char* target);
int ldap_uid_from_dn(char *DN, char *UIDOut);
const int NO_SUCH_CARD;
#endif

View File

@ -2,17 +2,82 @@
#include <stdlib.h>
#include "nfc.h"
#include "barcode.h"
#include "ldap.h"
#include "tts.h"
#include "config.h"
int main(int argc, char **argv)
{
nfc_initialize();
if (nfc_initialize())
{
printf("NFC initialization failed\n");
tts_speak("Nie można było zainicjalizować czytnika mifare.\n");
return 1;
}
printf("NFC initialization successful\n");
if (barcode_initialize(BARCODE_PORT))
{
printf("Barcode initialization failed\n");
tts_speak("Nie można było zainicjalizować czytnika kodów kreskowych.\n");
return 1;
}
printf("Barcode initialization successful\n");
tts_speak("Refryżyrator gotowy do pracy.");
char MifareID[128];
char Barcode[14];
while (1)
{
nfc_poll();
int NFCResult = nfc_read(MifareID);
if (NFCResult == 0)
{
char DN[256];
int DNLength = 256;
int LDAPResult = ldap_dn_by_mifare(MifareID, DN, &DNLength);
if (LDAPResult > 0)
{
if (LDAPResult == NO_SUCH_CARD)
{
tts_speak("Nieznana karta.");
printf("NFC: Unknown card.\n");
continue;
}
else
{
tts_speak("Błąd eldapa.");
printf("LDAP: Unknown error %i\n", LDAPResult);
continue;
}
}
printf("LDAP DN: %s\n", DN);
char UID[128];
ldap_uid_from_dn(DN, UID);
printf("LDAP uid: %s\n", UID);
tts_speak("Zeskanuj produkt.\n");
int BarcodeResult = barcode_read(Barcode);
if (BarcodeResult == 0)
{
printf("Scanned barcode %s\n", Barcode);
}
else
{
printf("Barcode error %i\n", BarcodeResult);
// Do error handling here
}
}
else if (NFCResult != NFC_TIMEOUT)
{
printf("NFC error %i\n", NFCResult);
// Do error handling here
}
}
}

View File

@ -10,6 +10,11 @@
#include "ldap.h"
#include "base64.h"
#define E_BAD_CARD 1
#define E_NO_SUCH_USER 2
#define E_FUCKUP 3
#define E_TIMEOUT 4
static nfc_device_t *g_NFCDevice = NULL;
const nfc_modulation_t g_NFCModulations[5] = {
@ -21,7 +26,7 @@ const nfc_modulation_t g_NFCModulations[5] = {
};
const size_t g_NFCModulationCount = 5;
void nfc_poll(void)
int nfc_read(char *MifareOut)
{
nfc_target_t Target;
bool Result = nfc_initiator_poll_target(g_NFCDevice, g_NFCModulations, g_NFCModulationCount, 20, 2, &Target);
@ -29,37 +34,17 @@ void nfc_poll(void)
if (Result)
{
if (Target.nm.nmt != NMT_ISO14443A)
return;
return E_BAD_CARD;
if (Target.nti.nai.szUidLen != 4)
return;
return E_BAD_CARD;
printf("Scanned Mifare %x.\n", *(int *)Target.nti.nai.abtUid);
char MifareID[5];
sprintf(MifareID, "%04x", *(int *)Target.nti.nai.abtUid);
char DN[128];
int DNLength = 128;
int LDAPResult = ldap_dn_by_mifare(MifareID, DN, &DNLength);
if (LDAPResult > 0)
{
if (LDAPResult == NO_SUCH_CARD)
{
tts_speak("Nieznana karta.");
sleep(5);
return;
}
else
{
tts_speak("Nieznany błąd przy połączeniu z eldapem.");
sleep(10);
return;
}
}
printf("This appears to be %s.\n", DN);
sprintf(MifareOut, "%02x%02x%02x%02x", Target.nti.nai.abtUid[0], Target.nti.nai.abtUid[1], Target.nti.nai.abtUid[2], Target.nti.nai.abtUid[3]);
return 0;
}
return E_TIMEOUT;
}
int nfc_initialize(void)

View File

@ -1,7 +1,12 @@
#ifndef __NFC_H__
#define __NFC_H__
#define NFC_BAD_CARD 1
#define NFC_NO_SUCH_USER 2
#define NFC_FUCKUP 3
#define NFC_TIMEOUT 4
int nfc_initialize(void);
void nfc_poll(void);
int nfc_read(char *UIDOut);
#endif

28
terminal/test-barcode.c Normal file
View File

@ -0,0 +1,28 @@
#include <stdio.h>
#include "barcode.h"
int main()
{
if (barcode_initialize("/dev/ttyUSB0"))
{
printf("Could not initialize barcode.\n");
return 1;
}
char Barcode[14];
if (barcode_read(Barcode))
{
printf("Could not read barcode.\n");
return 1;
}
printf("barcode: %s\n", Barcode);
if (barcode_read(Barcode))
{
printf("Could not read barcode.\n");
return 1;
}
printf("barcode: %s\n", Barcode);
return 0;
}