]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/libsystemd/sd-id128/sd-id128.c
core: correct spacing near eol in code comments
[thirdparty/systemd.git] / src / libsystemd / sd-id128 / sd-id128.c
CommitLineData
87d2c1ff
LP
1/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3/***
4 This file is part of systemd.
5
6 Copyright 2011 Lennart Poettering
7
8 systemd is free software; you can redistribute it and/or modify it
5430f7f2
LP
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
87d2c1ff
LP
11 (at your option) any later version.
12
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
5430f7f2 16 Lesser General Public License for more details.
87d2c1ff 17
5430f7f2 18 You should have received a copy of the GNU Lesser General Public License
87d2c1ff
LP
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20***/
21
22#include <errno.h>
23#include <fcntl.h>
24#include <unistd.h>
25
87d2c1ff
LP
26#include "util.h"
27#include "macro.h"
80514f9c 28#include "sd-id128.h"
87d2c1ff 29
000a2c98 30_public_ char *sd_id128_to_string(sd_id128_t id, char s[33]) {
87d2c1ff
LP
31 unsigned n;
32
1ae464e0 33 assert_return(s, NULL);
87d2c1ff
LP
34
35 for (n = 0; n < 16; n++) {
36 s[n*2] = hexchar(id.bytes[n] >> 4);
37 s[n*2+1] = hexchar(id.bytes[n] & 0xF);
38 }
39
40 s[32] = 0;
41
42 return s;
43}
44
aa96c6cb
LP
45_public_ int sd_id128_from_string(const char s[], sd_id128_t *ret) {
46 unsigned n, i;
87d2c1ff 47 sd_id128_t t;
aa96c6cb 48 bool is_guid = false;
87d2c1ff 49
1ae464e0
TA
50 assert_return(s, -EINVAL);
51 assert_return(ret, -EINVAL);
87d2c1ff 52
aa96c6cb 53 for (n = 0, i = 0; n < 16;) {
87d2c1ff
LP
54 int a, b;
55
aa96c6cb
LP
56 if (s[i] == '-') {
57 /* Is this a GUID? Then be nice, and skip over
58 * the dashes */
59
60 if (i == 8)
61 is_guid = true;
62 else if (i == 13 || i == 18 || i == 23) {
63 if (!is_guid)
64 return -EINVAL;
65 } else
66 return -EINVAL;
67
68 i++;
69 continue;
70 }
71
72 a = unhexchar(s[i++]);
87d2c1ff
LP
73 if (a < 0)
74 return -EINVAL;
75
aa96c6cb 76 b = unhexchar(s[i++]);
87d2c1ff
LP
77 if (b < 0)
78 return -EINVAL;
79
aa96c6cb 80 t.bytes[n++] = (a << 4) | b;
87d2c1ff
LP
81 }
82
aa96c6cb
LP
83 if (i != (is_guid ? 36 : 32))
84 return -EINVAL;
85
86 if (s[i] != 0)
87d2c1ff
LP
87 return -EINVAL;
88
89 *ret = t;
90 return 0;
91}
92
e4bac488 93static sd_id128_t make_v4_uuid(sd_id128_t id) {
87d2c1ff
LP
94 /* Stolen from generate_random_uuid() of drivers/char/random.c
95 * in the kernel sources */
96
97 /* Set UUID version to 4 --- truly random generation */
98 id.bytes[6] = (id.bytes[6] & 0x0F) | 0x40;
99
100 /* Set the UUID variant to DCE */
101 id.bytes[8] = (id.bytes[8] & 0x3F) | 0x80;
102
103 return id;
104}
105
000a2c98 106_public_ int sd_id128_get_machine(sd_id128_t *ret) {
ec202eae
SL
107 static thread_local sd_id128_t saved_machine_id;
108 static thread_local bool saved_machine_id_valid = false;
aa96c6cb
LP
109 _cleanup_close_ int fd = -1;
110 char buf[33];
87d2c1ff
LP
111 ssize_t k;
112 unsigned j;
113 sd_id128_t t;
114
1ae464e0 115 assert_return(ret, -EINVAL);
000a2c98 116
87d2c1ff
LP
117 if (saved_machine_id_valid) {
118 *ret = saved_machine_id;
119 return 0;
120 }
121
122 fd = open("/etc/machine-id", O_RDONLY|O_CLOEXEC|O_NOCTTY);
123 if (fd < 0)
124 return -errno;
125
aa96c6cb 126 k = loop_read(fd, buf, 33, false);
87d2c1ff
LP
127 if (k < 0)
128 return (int) k;
129
aa96c6cb
LP
130 if (k != 33)
131 return -EIO;
132
133 if (buf[32] !='\n')
87d2c1ff
LP
134 return -EIO;
135
136 for (j = 0; j < 16; j++) {
137 int a, b;
138
139 a = unhexchar(buf[j*2]);
140 b = unhexchar(buf[j*2+1]);
141
142 if (a < 0 || b < 0)
143 return -EIO;
144
145 t.bytes[j] = a << 4 | b;
146 }
147
148 saved_machine_id = t;
149 saved_machine_id_valid = true;
150
151 *ret = t;
152 return 0;
153}
154
000a2c98 155_public_ int sd_id128_get_boot(sd_id128_t *ret) {
ec202eae
SL
156 static thread_local sd_id128_t saved_boot_id;
157 static thread_local bool saved_boot_id_valid = false;
aa96c6cb 158 _cleanup_close_ int fd = -1;
87d2c1ff
LP
159 char buf[36];
160 ssize_t k;
161 unsigned j;
162 sd_id128_t t;
163 char *p;
164
1ae464e0 165 assert_return(ret, -EINVAL);
000a2c98 166
87d2c1ff
LP
167 if (saved_boot_id_valid) {
168 *ret = saved_boot_id;
169 return 0;
170 }
171
172 fd = open("/proc/sys/kernel/random/boot_id", O_RDONLY|O_CLOEXEC|O_NOCTTY);
173 if (fd < 0)
174 return -errno;
175
176 k = loop_read(fd, buf, 36, false);
87d2c1ff
LP
177 if (k < 0)
178 return (int) k;
179
aa96c6cb 180 if (k != 36)
87d2c1ff
LP
181 return -EIO;
182
183 for (j = 0, p = buf; j < 16; j++) {
184 int a, b;
185
cef35669 186 if (p >= buf + k - 1)
54c7d1f4
ZJS
187 return -EIO;
188
cef35669 189 if (*p == '-') {
87d2c1ff 190 p++;
cef35669
ZJS
191 if (p >= buf + k - 1)
192 return -EIO;
193 }
87d2c1ff
LP
194
195 a = unhexchar(p[0]);
196 b = unhexchar(p[1]);
197
198 if (a < 0 || b < 0)
199 return -EIO;
200
201 t.bytes[j] = a << 4 | b;
202
203 p += 2;
204 }
205
206 saved_boot_id = t;
207 saved_boot_id_valid = true;
208
209 *ret = t;
210 return 0;
211}
212
000a2c98 213_public_ int sd_id128_randomize(sd_id128_t *ret) {
87d2c1ff 214 sd_id128_t t;
0f0e240c 215 int r;
87d2c1ff 216
1ae464e0 217 assert_return(ret, -EINVAL);
87d2c1ff 218
0f0e240c
LP
219 r = dev_urandom(&t, sizeof(t));
220 if (r < 0)
221 return r;
87d2c1ff
LP
222
223 /* Turn this into a valid v4 UUID, to be nice. Note that we
224 * only guarantee this for newly generated UUIDs, not for
225 * pre-existing ones.*/
226
e4bac488 227 *ret = make_v4_uuid(t);
87d2c1ff
LP
228 return 0;
229}