Послушный YubiKey01.07.2024 15:15
void usb_read_token(char* token)
{
char* usb_dev = getenv("USB_DEV");
struct pollfd fds[1];
fds[0].fd = open(usb_dev, O_RDONLY | O_NONBLOCK);
fds[0].events = POLLIN;
if (fds[0].fd < 0) {
printf("Error unable open for reading '%s'\n", usb_dev);
exit(1);
}
char keys[100] = { 0, 1,
'1', '2', '3', '4', '5', '6', '7', '8', '9', '0', 12, 13, 14, 15,
'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', 26, 27, 28, 29,
'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 39, 40, 41, 42, 43,
'z', 'x', 'c', 'v', 'b', 'n', 'm' };
struct input_event ev;
int char_count = 0;
char* pass = getenv("PASS");
sprintf(token, "%s", pass);
while (true) {
int timeout_ms = 1000;
int ret = poll(fds, 1, timeout_ms);
if (ret > 0) {
if (fds[0].revents) {
ssize_t r = read(fds[0].fd, &ev, sizeof(ev));
if (r < 0) {
printf("Error %d\n", (int)r);
break;
} else {
if (ev.type == 1 && ev.value == 1) {
sprintf(token + strlen(token), "%c", keys[ev.code]);
char_count++;
if (char_count >= 44) {
sprintf(token + strlen(token), "\n");
break;
}
}
}
}
}
}
close(fds[0].fd);
}
© Habrahabr.ru