-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathnfcauth.c
93 lines (77 loc) · 2.34 KB
/
nfcauth.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <nfc/nfc.h>
#include "nfcauth.h"
#if !defined(SYSCONFDIR)
# define SYSCONFDIR "/etc"
#endif /* !SYSCONFDIR */
#if !defined(PAM_NFC_FILE)
# define PAM_NFC_FILE SYSCONFDIR "/pam_nfc.conf"
#endif /* !PAM_NFC_FILE */
#define CRED_FORMAT "%s %s\n"
#define MAX_DEVICES 8
#define MAX_TARGET 32
extern int nfcauth_is_authorized (const char *user, char *target);
extern int nfcauth_get_targets (char **targets[]);
int
nfcauth_authorize (const char *user)
{
int grant_access = 0;
char **targets;
int i, target_count;
target_count = nfcauth_get_targets(&targets);
for (i = 0; i < target_count; i++) {
if (nfcauth_is_authorized (user, targets[i])) {
grant_access = 1;
break;
}
}
for (i = 0; i < target_count; i++) {
free (targets[i]);
}
free (targets);
return grant_access;
}
int
nfcauth_get_targets (char **targets[])
{
int ret = 0;
*targets = malloc (MAX_TARGET * sizeof (char *));
nfc_connstring devices[MAX_DEVICES];
size_t device_count;
size_t target_count;
size_t i;
nfc_context *context;
nfc_init(&context);
device_count = nfc_list_devices(context, devices, MAX_DEVICES);
nfc_modulation nm = {
.nmt = NMT_ISO14443A,
.nbr = NBR_UNDEFINED
};
for (i = 0; i < device_count; i++) {
nfc_device* initiator = nfc_open (context, devices[i]);
if (initiator) {
nfc_initiator_init (initiator);
nfc_target ant[MAX_TARGET];
int res;
if ((res = nfc_initiator_list_passive_targets (initiator, nm, ant, MAX_TARGET)) >= 0) {
size_t iTarget;
target_count = res;
for (iTarget = 0; iTarget < target_count; iTarget++) {
if (((*targets)[ret] = malloc (2 * ant[iTarget].nti.nai.szUidLen + 1))) {
size_t n;
(*targets)[ret][0] = '\0';
for (n = 0; n < ant[iTarget].nti.nai.szUidLen; n++) {
sprintf ((*targets)[ret] + 2 * n, "%02x", ant[iTarget].nti.nai.abtUid[n]);
}
ret++;
}
}
}
nfc_close (initiator);
}
}
nfc_exit(context);
return ret;
}