chiark / gitweb /
91c26a0c8e290b937428761c70756049734e8fa0
[elogind.git] / src / libelogind / sd-id128 / id128-util.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3   This file is part of systemd.
4
5   Copyright 2016 Lennart Poettering
6 ***/
7
8 #include <errno.h>
9 #include <fcntl.h>
10 #include <unistd.h>
11
12 #include "fd-util.h"
13 #include "fs-util.h"
14 #include "hexdecoct.h"
15 #include "id128-util.h"
16 #include "io-util.h"
17 #include "stdio-util.h"
18
19 char *id128_to_uuid_string(sd_id128_t id, char s[37]) {
20         unsigned n, k = 0;
21
22         assert(s);
23
24         /* Similar to sd_id128_to_string() but formats the result as UUID instead of plain hex chars */
25
26         for (n = 0; n < 16; n++) {
27
28                 if (IN_SET(n, 4, 6, 8, 10))
29                         s[k++] = '-';
30
31                 s[k++] = hexchar(id.bytes[n] >> 4);
32                 s[k++] = hexchar(id.bytes[n] & 0xF);
33         }
34
35         assert(k == 36);
36
37         s[k] = 0;
38
39         return s;
40 }
41
42 bool id128_is_valid(const char *s) {
43         size_t i, l;
44
45         assert(s);
46
47         l = strlen(s);
48         if (l == 32) {
49
50                 /* Plain formatted 128bit hex string */
51
52                 for (i = 0; i < l; i++) {
53                         char c = s[i];
54
55                         if (!(c >= '0' && c <= '9') &&
56                             !(c >= 'a' && c <= 'z') &&
57                             !(c >= 'A' && c <= 'Z'))
58                                 return false;
59                 }
60
61         } else if (l == 36) {
62
63                 /* Formatted UUID */
64
65                 for (i = 0; i < l; i++) {
66                         char c = s[i];
67
68                         if (IN_SET(i, 8, 13, 18, 23)) {
69                                 if (c != '-')
70                                         return false;
71                         } else {
72                                 if (!(c >= '0' && c <= '9') &&
73                                     !(c >= 'a' && c <= 'z') &&
74                                     !(c >= 'A' && c <= 'Z'))
75                                         return false;
76                         }
77                 }
78
79         } else
80                 return false;
81
82         return true;
83 }
84
85 int id128_read_fd(int fd, Id128Format f, sd_id128_t *ret) {
86         char buffer[36 + 2];
87         ssize_t l;
88
89         assert(fd >= 0);
90         assert(f < _ID128_FORMAT_MAX);
91
92         /* Reads an 128bit ID from a file, which may either be in plain format (32 hex digits), or in UUID format, both
93          * optionally followed by a newline and nothing else. ID files should really be newline terminated, but if they
94          * aren't that's OK too, following the rule of "Be conservative in what you send, be liberal in what you
95          * accept". */
96
97         l = loop_read(fd, buffer, sizeof(buffer), false); /* we expect a short read of either 32/33 or 36/37 chars */
98         if (l < 0)
99                 return (int) l;
100         if (l == 0) /* empty? */
101                 return -ENOMEDIUM;
102
103         switch (l) {
104
105         case 33: /* plain UUID with trailing newline */
106                 if (buffer[32] != '\n')
107                         return -EINVAL;
108
109                 _fallthrough_;
110         case 32: /* plain UUID without trailing newline */
111                 if (f == ID128_UUID)
112                         return -EINVAL;
113
114                 buffer[32] = 0;
115                 break;
116
117         case 37: /* RFC UUID with trailing newline */
118                 if (buffer[36] != '\n')
119                         return -EINVAL;
120
121                 _fallthrough_;
122         case 36: /* RFC UUID without trailing newline */
123                 if (f == ID128_PLAIN)
124                         return -EINVAL;
125
126                 buffer[36] = 0;
127                 break;
128
129         default:
130                 return -EINVAL;
131         }
132
133         return sd_id128_from_string(buffer, ret);
134 }
135
136 int id128_read(const char *p, Id128Format f, sd_id128_t *ret) {
137         _cleanup_close_ int fd = -1;
138
139         fd = open(p, O_RDONLY|O_CLOEXEC|O_NOCTTY);
140         if (fd < 0)
141                 return -errno;
142
143         return id128_read_fd(fd, f, ret);
144 }
145
146 int id128_write_fd(int fd, Id128Format f, sd_id128_t id, bool do_sync) {
147         char buffer[36 + 2];
148         size_t sz;
149         int r;
150
151         assert(fd >= 0);
152         assert(f < _ID128_FORMAT_MAX);
153
154         if (f != ID128_UUID) {
155                 sd_id128_to_string(id, buffer);
156                 buffer[32] = '\n';
157                 sz = 33;
158         } else {
159                 id128_to_uuid_string(id, buffer);
160                 buffer[36] = '\n';
161                 sz = 37;
162         }
163
164         r = loop_write(fd, buffer, sz, false);
165         if (r < 0)
166                 return r;
167
168         if (do_sync) {
169                 if (fsync(fd) < 0)
170                         return -errno;
171
172                 r = fsync_directory_of_file(fd);
173                 if (r < 0)
174                         return r;
175         }
176
177         return 0;
178 }
179
180 #if 0 /// UNNEEDED by elogind
181 int id128_write(const char *p, Id128Format f, sd_id128_t id, bool do_sync) {
182         _cleanup_close_ int fd = -1;
183
184         fd = open(p, O_WRONLY|O_CREAT|O_CLOEXEC|O_NOCTTY|O_TRUNC, 0444);
185         if (fd < 0)
186                 return -errno;
187
188         return id128_write_fd(fd, f, id, do_sync);
189 }
190
191 void id128_hash_func(const void *p, struct siphash *state) {
192         siphash24_compress(p, 16, state);
193 }
194
195 int id128_compare_func(const void *a, const void *b) {
196         return memcmp(a, b, 16);
197 }
198
199 const struct hash_ops id128_hash_ops = {
200         .hash = id128_hash_func,
201         .compare = id128_compare_func,
202 };
203 #endif // 0