]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/libsystemd/sd-id128/id128-util.c
fs-util: Add XOpenFlags with XO_LABEL flag to have xopenat() MAC label files/dirs
[thirdparty/systemd.git] / src / libsystemd / sd-id128 / id128-util.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <errno.h>
4 #include <fcntl.h>
5 #include <unistd.h>
6
7 #include "fd-util.h"
8 #include "fs-util.h"
9 #include "hexdecoct.h"
10 #include "id128-util.h"
11 #include "io-util.h"
12 #include "stdio-util.h"
13 #include "string-util.h"
14 #include "sync-util.h"
15
16 bool id128_is_valid(const char *s) {
17 size_t l;
18
19 assert(s);
20
21 l = strlen(s);
22
23 if (l == SD_ID128_STRING_MAX - 1)
24 /* Plain formatted 128bit hex string */
25 return in_charset(s, HEXDIGITS);
26
27 if (l == SD_ID128_UUID_STRING_MAX - 1) {
28 /* Formatted UUID */
29 for (size_t i = 0; i < l; i++) {
30 char c = s[i];
31
32 if (IN_SET(i, 8, 13, 18, 23)) {
33 if (c != '-')
34 return false;
35 } else if (!ascii_ishex(c))
36 return false;
37 }
38 return true;
39 }
40
41 return false;
42 }
43
44 int id128_read_fd(int fd, Id128Flag f, sd_id128_t *ret) {
45 char buffer[SD_ID128_UUID_STRING_MAX + 1]; /* +1 is for trailing newline */
46 sd_id128_t id;
47 ssize_t l;
48 int r;
49
50 assert(fd >= 0);
51
52 /* Reads an 128bit ID from a file, which may either be in plain format (32 hex digits), or in UUID format, both
53 * optionally followed by a newline and nothing else. ID files should really be newline terminated, but if they
54 * aren't that's OK too, following the rule of "Be conservative in what you send, be liberal in what you
55 * accept".
56 *
57 * This returns the following:
58 * -ENOMEDIUM: an empty string,
59 * -ENOPKG: "uninitialized" or "uninitialized\n",
60 * -EUCLEAN: other invalid strings. */
61
62 l = loop_read(fd, buffer, sizeof(buffer), false); /* we expect a short read of either 32/33 or 36/37 chars */
63 if (l < 0)
64 return (int) l;
65 if (l == 0) /* empty? */
66 return -ENOMEDIUM;
67
68 switch (l) {
69
70 case STRLEN("uninitialized"):
71 case STRLEN("uninitialized\n"):
72 return strneq(buffer, "uninitialized\n", l) ? -ENOPKG : -EINVAL;
73
74 case SD_ID128_STRING_MAX: /* plain UUID with trailing newline */
75 if (buffer[SD_ID128_STRING_MAX-1] != '\n')
76 return -EUCLEAN;
77
78 _fallthrough_;
79 case SD_ID128_STRING_MAX-1: /* plain UUID without trailing newline */
80 if (!FLAGS_SET(f, ID128_FORMAT_PLAIN))
81 return -EUCLEAN;
82
83 buffer[SD_ID128_STRING_MAX-1] = 0;
84 break;
85
86 case SD_ID128_UUID_STRING_MAX: /* RFC UUID with trailing newline */
87 if (buffer[SD_ID128_UUID_STRING_MAX-1] != '\n')
88 return -EUCLEAN;
89
90 _fallthrough_;
91 case SD_ID128_UUID_STRING_MAX-1: /* RFC UUID without trailing newline */
92 if (!FLAGS_SET(f, ID128_FORMAT_UUID))
93 return -EUCLEAN;
94
95 buffer[SD_ID128_UUID_STRING_MAX-1] = 0;
96 break;
97
98 default:
99 return -EUCLEAN;
100 }
101
102 r = sd_id128_from_string(buffer, &id);
103 if (r == -EINVAL)
104 return -EUCLEAN;
105 if (r < 0)
106 return r;
107
108 if (FLAGS_SET(f, ID128_REFUSE_NULL) && sd_id128_is_null(id))
109 return -ENOMEDIUM;
110
111 if (ret)
112 *ret = id;
113 return 0;
114 }
115
116 int id128_read_at(int dir_fd, const char *path, Id128Flag f, sd_id128_t *ret) {
117 _cleanup_close_ int fd = -EBADF;
118
119 assert(dir_fd >= 0 || dir_fd == AT_FDCWD);
120 assert(path);
121
122 fd = xopenat(dir_fd, path, O_RDONLY|O_CLOEXEC|O_NOCTTY, /* xopen_flags = */ 0, /* mode = */ 0);
123 if (fd < 0)
124 return fd;
125
126 return id128_read_fd(fd, f, ret);
127 }
128
129 int id128_write_fd(int fd, Id128Flag f, sd_id128_t id) {
130 char buffer[SD_ID128_UUID_STRING_MAX + 1]; /* +1 is for trailing newline */
131 size_t sz;
132 int r;
133
134 assert(fd >= 0);
135 assert(IN_SET((f & ID128_FORMAT_ANY), ID128_FORMAT_PLAIN, ID128_FORMAT_UUID));
136
137 if (FLAGS_SET(f, ID128_REFUSE_NULL) && sd_id128_is_null(id))
138 return -ENOMEDIUM;
139
140 if (FLAGS_SET(f, ID128_FORMAT_PLAIN)) {
141 assert_se(sd_id128_to_string(id, buffer));
142 sz = SD_ID128_STRING_MAX;
143 } else {
144 assert_se(sd_id128_to_uuid_string(id, buffer));
145 sz = SD_ID128_UUID_STRING_MAX;
146 }
147
148 buffer[sz - 1] = '\n';
149 r = loop_write(fd, buffer, sz, false);
150 if (r < 0)
151 return r;
152
153 if (FLAGS_SET(f, ID128_SYNC_ON_WRITE)) {
154 r = fsync_full(fd);
155 if (r < 0)
156 return r;
157 }
158
159 return 0;
160 }
161
162 int id128_write_at(int dir_fd, const char *path, Id128Flag f, sd_id128_t id) {
163 _cleanup_close_ int fd = -EBADF;
164
165 assert(dir_fd >= 0 || dir_fd == AT_FDCWD);
166 assert(path);
167
168 fd = xopenat(dir_fd, path, O_WRONLY|O_CREAT|O_CLOEXEC|O_NOCTTY|O_TRUNC, /* xopen_flags = */ 0, 0444);
169 if (fd < 0)
170 return fd;
171
172 return id128_write_fd(fd, f, id);
173 }
174
175 void id128_hash_func(const sd_id128_t *p, struct siphash *state) {
176 siphash24_compress(p, sizeof(sd_id128_t), state);
177 }
178
179 int id128_compare_func(const sd_id128_t *a, const sd_id128_t *b) {
180 return memcmp(a, b, 16);
181 }
182
183 sd_id128_t id128_make_v4_uuid(sd_id128_t id) {
184 /* Stolen from generate_random_uuid() of drivers/char/random.c
185 * in the kernel sources */
186
187 /* Set UUID version to 4 --- truly random generation */
188 id.bytes[6] = (id.bytes[6] & 0x0F) | 0x40;
189
190 /* Set the UUID variant to DCE */
191 id.bytes[8] = (id.bytes[8] & 0x3F) | 0x80;
192
193 return id;
194 }
195
196 DEFINE_HASH_OPS(id128_hash_ops, sd_id128_t, id128_hash_func, id128_compare_func);
197 DEFINE_HASH_OPS_WITH_KEY_DESTRUCTOR(id128_hash_ops_free, sd_id128_t, id128_hash_func, id128_compare_func, free);
198
199 int id128_get_product(sd_id128_t *ret) {
200 sd_id128_t uuid;
201 int r;
202
203 assert(ret);
204
205 /* Reads the systems product UUID from DMI or devicetree (where it is located on POWER). This is
206 * particularly relevant in VM environments, where VM managers typically place a VM uuid there. */
207
208 r = id128_read("/sys/class/dmi/id/product_uuid", ID128_FORMAT_UUID, &uuid);
209 if (r == -ENOENT)
210 r = id128_read("/proc/device-tree/vm,uuid", ID128_FORMAT_UUID, &uuid);
211 if (r < 0)
212 return r;
213
214 if (sd_id128_is_null(uuid) || sd_id128_is_allf(uuid))
215 return -EADDRNOTAVAIL; /* Recognizable error */
216
217 *ret = uuid;
218 return 0;
219 }