-
Notifications
You must be signed in to change notification settings - Fork 0
/
pam_numworks.c
68 lines (59 loc) · 1.7 KB
/
pam_numworks.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <libusb-1.0/libusb.h>
#include <security/pam_modules.h>
int pam_sm_authenticate(pam_handle_t *pamh, int flags, int argc, const char **argv)
{
if (argc == 0) {
fprintf(stdout, "ERROR: Missing serial number in pam.d rules for current application !");
return PAM_AUTH_ERR;
}
libusb_device_handle *device = NULL;
int status = libusb_init(NULL);
if (status != 0)
{
fprintf(stderr, "Failed to init libusb\n");
return PAM_AUTH_ERR;
}
fprintf(stdout, "Waiting (for eternity) for a device ...\n");
fflush(stdout);
while (device == NULL)
{
device = libusb_open_device_with_vid_pid(NULL, 0x0483, 0xa291);
sleep(1);
}
unsigned char serial[20];
struct libusb_device_descriptor desc;
libusb_device *dev = libusb_get_device(device);
int result = libusb_get_device_descriptor(dev, &desc);
if (result < 0)
{
fprintf(stderr, "Couldn't get device descriptor!\n");
libusb_close(device);
libusb_exit(NULL);
return PAM_AUTH_ERR;
}
int r = libusb_get_string_descriptor_ascii(device, desc.iSerialNumber, serial, 20);
if (r < 0)
{
fprintf(stderr, "Couldn't get serial number!\n");
libusb_close(device);
libusb_exit(NULL);
return PAM_AUTH_ERR;
}
if (strcmp(serial, argv[0]) == 0)
{
libusb_close(device);
libusb_exit(NULL);
fprintf(stdout,"Success!\n");
return PAM_SUCCESS;
}
else
{
fprintf(stderr, "This is not my numworks !\n");
libusb_close(device);
libusb_exit(NULL);
return PAM_PERM_DENIED;
}
}