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