-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuuid.c
256 lines (228 loc) · 5.32 KB
/
uuid.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <stdio.h>
#include "wphoto.h"
/*
* We need to obtain an UUID that is persistent across reboots, does not
* depend on the presence of removable hardware and ideally is the same for
* all user accounts (to avoid surprises when running as root, plus there
* can only be one instance of the imink server at a time). The various uuid
* libraries have the opposite goal, hence this implementation. We do not care
* about the format of the UUID, as the camera happily accept any hex string
* in the 8-4-4-4-12 notation.
*/
#define UUID_LEN 16
#define UUID_STR_LEN 36
static char uuid_str[UUID_STR_LEN];
static int have_uuid_str;
/*
* We use NULL as "not available" in the functions below, whereas OOM is a
* temporary condition. Exit early to avoid possibly returning different UUIDs
* under different memory conditions.
*/
static void * xmalloc(size_t len)
{
void *res = malloc(len);
if (!res) {
perror("memory allocation failure");
exit(1);
}
return res;
}
/*
* Check if at least two bytes are not 0x00 or 0xff
*/
static int is_plausible_uuid(unsigned char *uuid)
{
int i, good;
for (i = 0, good = 0; i < UUID_LEN; i++) {
if (uuid[i] == 0 || uuid[i] == 0xff)
continue;
if (++good >= 2)
return 1;
}
return 0;
}
/*
* Reads a hexadecimal uuid from <path> and returns its binary form. The caller
* must free the returned uuid.
*/
static unsigned char * uuid_from_file(const char *path)
{
unsigned char *res, *r;
char *str, *s;
FILE *f;
int cur, prev;
f = fopen(path, "r");
if (!f)
return NULL;
res = xmalloc(UUID_LEN);
memset(res, '\0', UUID_LEN);
str = xmalloc(UUID_STR_LEN + 1);
if (!fgets(str, UUID_STR_LEN, f)) {
free(res);
free(str);
fclose(f);
return NULL;
}
fclose(f);
r = res + UUID_LEN - 1;
prev = -1;
/* Convert the string from the end, skipping non-hex characters */
for (s = strchr(str, '\0'); s >= str && r >= res; s--) {
if (*s >= '0' && *s <= '9')
cur = *s - '0';
else if (*s >= 'A' && *s <= 'F')
cur = *s - 'A' + 10;
else if (*s >= 'a' && *s <= 'f')
cur = *s - 'a' + 10;
else
continue;
if (prev == -1) {
prev = cur;
continue;
}
*r = (cur << 4) + prev;
r--;
prev = -1;
}
free(str);
if (!is_plausible_uuid(res)) {
free(res);
return NULL;
}
return res;
}
/*
* If /sys/class/net/<name> points to ../devices/virtual or ../devices/.../usb,
* we consider the device transient and try a better one if available. This
* should skip usb modems / network cards and things like virtual machine
* interfaces or VPN endpoints.
*/
static int is_transient_interface(const char *name)
{
char path[256], target[256];
ssize_t size;
snprintf(path, sizeof(path), "/sys/class/net/%s", name);
size = readlink(path, target, sizeof(target) - 1);
if (size < 0) {
fprintf(stderr, "warning: cannot read link target of %s: %s\n",
path, strerror(errno));
return 1;
}
target[size] = '\0';
if (!strstr(target, "/devices/")) {
fprintf(stderr, "warning: %s points to an unexpected target: %s\n",
path, target);
return 1;
}
if (strstr(target, "/usb"))
return 1;
if (strstr(target, "/devices/virtual/"))
return 1;
return 0;
}
static unsigned char * linux_mac_uuid(void)
{
DIR *dir;
struct dirent *de;
unsigned char *candidate1 = NULL, *candidate2 = NULL;
int transient;
if (!(dir = opendir("/sys/class/net")))
return NULL;
while ((de = readdir(dir))) {
unsigned char **candidate;
unsigned char *new_uuid;
char path[256];
if (de->d_name[0] == '.')
continue;
transient = is_transient_interface(de->d_name);
if (transient && candidate1)
continue;
candidate = transient ? &candidate2 : &candidate1;
snprintf(path, sizeof(path), "/sys/class/net/%s/address",
de->d_name);
new_uuid = uuid_from_file(path);
if (!new_uuid)
continue;
if (*candidate && memcmp(*candidate, new_uuid, UUID_LEN) < 0) {
free(new_uuid);
continue;
}
free(*candidate);
*candidate = new_uuid;
}
closedir(dir);
if (candidate1) {
free(candidate2);
return candidate1;
}
return candidate2;
}
static unsigned char * fallback_uuid(void)
{
unsigned char *res = xmalloc(UUID_LEN);
int i;
for (i = 0; i < UUID_LEN; i++)
res[i] = i + 1;
return res;
}
static void uuid_to_str(char *uuid_str, const unsigned char *uuid)
{
char *p;
int i;
p = uuid_str;
for (i = 0; i < UUID_LEN; i++) {
sprintf(p, "%02X", uuid[i]);
p += 2;
if (i == 4 || i == 6 || i == 8 || i == 10) {
*p++ = '-';
}
}
uuid_str[UUID_STR_LEN] = '\0';
}
static unsigned char * get_uuid_bin(void)
{
unsigned char *res;
if ((res = linux_mac_uuid()))
return res;
return fallback_uuid();
}
const char * get_uuid(void)
{
unsigned char *uuid;
if (have_uuid_str)
return uuid_str;
uuid = get_uuid_bin();
uuid_to_str(uuid_str, uuid);
free(uuid);
return uuid_str;
}
#ifdef UUID_TEST
int main(int argc, char **argv)
{
unsigned char *uuid;
char uuid_str[UUID_STR_LEN];
printf("get_uuid() : %s\n", get_uuid());
uuid = linux_mac_uuid();
if (uuid) {
uuid_to_str(uuid_str, uuid);
free(uuid);
printf("linux_mac_uuid() : %s\n", uuid_str);
}
while (*++argv) {
uuid = uuid_from_file(*argv);
if (!uuid)
continue;
uuid_to_str(uuid_str, uuid);
free(uuid);
printf("%-20s: %s\n", *argv, uuid_str);
}
return 0;
}
#endif