master
c 249 lines 8.54 KB
Raw
1 // SPDX-License-Identifier: GPL-3.0-or-later
2
3 #include "common.h"
4 #include "machine-guid.h"
5
6 static bool machine_guid_check_blacklisted(const char *guid) {
7 // these are machine GUIDs that have been included in distribution packages.
8 // we blacklist them here, so that the next version of netdata will generate
9 // new ones.
10
11 static char *blacklisted[] = {
12 // Third party packaging problems
13 "8a795b0c-2311-11e6-8563-000c295076a6",
14 "4aed1458-1c3e-11e6-a53f-000c290fc8f5",
15
16 // GitHub runner problems
17 "a177c1dc-09d9-11f0-a920-0242ac110002",
18 "983624e2-09d9-11f0-b90c-0242ac110002",
19 "477f97ae-09d9-11f0-903d-0242ac110002",
20 "ded81380-09e1-11f0-ae4c-0242ac110002",
21 "9abc69ec-09d9-11f0-a8a4-0242ac110002",
22 "68a2d17a-0aa2-11f0-97f3-0242ac110002",
23 "6499dbbe-0aa2-11f0-9ccd-0242ac110002",
24 "a9708cba-0aa2-11f0-98b6-0242ac110002",
25 "26903986-0aab-11f0-818e-0242ac110002",
26 "ab576242-0aa2-11f0-89c3-0242ac110002",
27 "eab387c6-0b6b-11f0-b715-0242ac110002",
28 "eaee7dfe-0b6b-11f0-870f-0242ac110002",
29 "c7d4e6b4-0b6b-11f0-878c-0242ac110002",
30 "40ac6d48-0b74-11f0-9cf4-0242ac110002",
31 "e366fc5a-0b6b-11f0-bd77-0242ac110002",
32 "c5955806-0c34-11f0-a302-0242ac110002",
33 "1d4d05d0-0c35-11f0-a01d-0242ac110002",
34 "edfc72b0-0c35-11f0-8e50-0242ac110002",
35 "536a030e-0c3d-11f0-837b-0242ac110002",
36 "10846e2e-0c35-11f0-8422-0242ac110002",
37 "4339f742-0dc7-11f0-838c-0242ac110002",
38 "3f28d7e0-0dc7-11f0-b75f-0242ac110002",
39 "41815788-0dc7-11f0-88e0-0242ac110002",
40 "104b408a-0dd0-11f0-8ca5-0242ac110002",
41 "8e45bc30-0dc7-11f0-8e50-0242ac110002",
42 };
43
44 for(size_t i = 0; i < _countof(blacklisted); i++) {
45 if (!strcmp(guid, blacklisted[i])) {
46 nd_log(NDLS_DAEMON, NDLP_INFO, "MACHINE_GUID: blacklisted machine GUID '%s' found, generating new one.", guid);
47 return true;
48 }
49 }
50
51 return false;
52 }
53
54 static bool machine_guid_read_from_file(const char *filename, ND_MACHINE_GUID *host_id) {
55 if (!filename || !*filename)
56 return false;
57
58 ND_MACHINE_GUID h;
59 memset(&h, 0, sizeof(h));
60
61 int fd = open(filename, O_RDONLY | O_CLOEXEC);
62 if (fd == -1) {
63 nd_log(NDLS_DAEMON, NDLP_ERR, "MACHINE_GUID: cannot open GUID file '%s' for reading", filename);
64 return false;
65 }
66
67 if (read(fd, h.txt, sizeof(h.txt) - 1) != sizeof(h.txt) - 1) {
68 nd_log(NDLS_DAEMON, NDLP_ERR, "MACHINE_GUID: cannot read GUID file '%s'", filename);
69 close(fd);
70 return false;
71 }
72 h.txt[sizeof(h.txt) - 1] = '\0';
73
74 if (uuid_parse(h.txt, h.uuid.uuid) != 0) {
75 nd_log(NDLS_DAEMON, NDLP_ERR, "MACHINE_GUID: cannot parse GUID from file '%s'", filename);
76 close(fd);
77 return false;
78 }
79
80 if (UUIDiszero(h.uuid)) {
81 nd_log(NDLS_DAEMON, NDLP_ERR, "MACHINE_GUID: GUID read from file '%s' is zero", filename);
82 close(fd);
83 return false;
84 }
85
86 struct stat st;
87 if (fstat(fd, &st) != 0) {
88 nd_log(NDLS_DAEMON, NDLP_ERR, "MACHINE_GUID: cannot stat the GUID file '%s'", filename);
89 close(fd);
90 return false;
91 }
92 close(fd);
93
94 // Recreate the text version of it, ensuring lowercase format.
95 uuid_unparse_lower(h.uuid.uuid, h.txt);
96
97 if (machine_guid_check_blacklisted(h.txt))
98 return false;
99
100 // Update last modified timestamp.
101 h.last_modified_ut = STAT_GET_MTIME_SEC(st) * USEC_PER_SEC + STAT_GET_MTIME_NSEC(st) / 1000;
102 rfc3339_datetime_ut(h.last_modified_ut_rfc3339, sizeof(h.last_modified_ut_rfc3339), h.last_modified_ut, 2, true);
103 *host_id = h;
104
105 nd_log(NDLS_DAEMON, NDLP_INFO, "MACHINE_GUID: GUID read from file '%s'", filename);
106
107 return true;
108 }
109
110 static struct timespec usec_to_timespec(usec_t usec) {
111 struct timespec ts;
112 ts.tv_sec = usec / USEC_PER_SEC;
113 ts.tv_nsec = (usec % USEC_PER_SEC) * 1000;
114 return ts;
115 }
116
117 static bool machine_guid_write_to_file(const char *filename, ND_MACHINE_GUID *host_id) {
118 static size_t save_id = 0;
119
120 if (!filename || !*filename)
121 return false;
122
123 ND_MACHINE_GUID h;
124 memset(&h, 0, sizeof(h));
125
126 if (host_id) {
127 h.uuid = host_id->uuid;
128 h.last_modified_ut = host_id->last_modified_ut;
129 safecpy(h.last_modified_ut_rfc3339, host_id->last_modified_ut_rfc3339);
130 }
131
132 // Create the text representation before writing.
133 uuid_unparse_lower(h.uuid.uuid, h.txt);
134
135 // Use a temporary filename for atomic writes.
136 char tmp_filename[FILENAME_MAX];
137 snprintf(tmp_filename, sizeof(tmp_filename), "%s.%zu", filename, __atomic_add_fetch(&save_id, 1, __ATOMIC_RELAXED));
138
139 int fd = open(tmp_filename, O_WRONLY | O_CREAT | O_TRUNC, 0444);
140 if (fd == -1) {
141 nd_log(NDLS_DAEMON, NDLP_ERR, "MACHINE_GUID: cannot create the temporary GUID file '%s'", tmp_filename);
142 return false;
143 }
144
145 if (write(fd, h.txt, sizeof(h.txt) - 1) != sizeof(h.txt) - 1) {
146 nd_log(NDLS_DAEMON, NDLP_ERR, "MACHINE_GUID: cannot write GUID to the temporary GUID file '%s'", tmp_filename);
147 close(fd);
148 return false;
149 }
150
151 close(fd);
152
153 struct timespec times[2] = {
154 usec_to_timespec(h.last_modified_ut), // access time
155 usec_to_timespec(h.last_modified_ut), // modification time
156 };
157
158 // Update file timestamps.
159 if (utimensat(AT_FDCWD, tmp_filename, times, 0) < 0)
160 nd_log(NDLS_DAEMON, NDLP_ERR, "MACHINE_GUID: cannot update the timestamps of the temporary GUID file '%s'", tmp_filename);
161
162 // Rename the temporary file to the target filename atomically.
163 if (rename(tmp_filename, filename) != 0) {
164 nd_log(NDLS_DAEMON, NDLP_ERR, "MACHINE_GUID: cannot rename temporary GUID file '%s' to '%s'", tmp_filename, filename);
165 unlink(tmp_filename);
166 return false;
167 }
168
169 nd_log(NDLS_DAEMON, NDLP_INFO, "MACHINE_GUID: GUID saved to file '%s'", filename);
170
171 return true;
172 }
173
174 static ND_MACHINE_GUID nd_machine_guid = { 0 };
175
176 static ND_MACHINE_GUID machine_guid_get_or_create(void) {
177 char pathname[FILENAME_MAX];
178 char filename[FILENAME_MAX];
179
180 netdata_conf_section_directories();
181
182 ND_MACHINE_GUID h;
183 memset(&h, 0, sizeof(h));
184
185 // Build the file path.
186 snprintfz(pathname, sizeof(pathname), "%s/registry", netdata_configured_varlib_dir);
187 snprintfz(filename, sizeof(filename), "%s/%s", pathname, "netdata.public.unique.id");
188
189 // Attempt to read the GUID from the file.
190 if (machine_guid_read_from_file(filename, &h))
191 return h;
192
193 // Log failure to read the file.
194 nd_log(NDLS_DAEMON, NDLP_ERR, "MACHINE_GUID: failed to read GUID from file '%s'", filename);
195
196 // Attempt to retrieve GUID from daemon status file.
197 h = daemon_status_file_get_host_id();
198 uuid_unparse_lower(h.uuid.uuid, h.txt);
199 if (UUIDiszero(h.uuid) || machine_guid_check_blacklisted(h.txt)) {
200 // If the status file does not contain a GUID, generate a new one.
201 nd_log(NDLS_DAEMON, NDLP_INFO, "MACHINE_GUID: generating a new GUID");
202 uuid_generate(h.uuid.uuid);
203 uuid_unparse_lower(h.uuid.uuid, h.txt);
204 }
205 else
206 nd_log(NDLS_DAEMON, NDLP_INFO, "MACHINE_GUID: got previous GUID from daemon status file");
207
208 h.last_modified_ut = now_realtime_usec();
209 rfc3339_datetime_ut(h.last_modified_ut_rfc3339, sizeof(h.last_modified_ut_rfc3339), h.last_modified_ut, 2, true);
210 nd_machine_guid = h;
211
212 // Avoid a check-then-create race; mkdir() + EEXIST is sufficient.
213 errno_clear();
214 if (mkdir(pathname, 0775) != 0) {
215 if (errno != EEXIST) {
216 nd_log(NDLS_DAEMON, NDLP_ERR, "MACHINE_GUID: cannot create directory '%s'", pathname);
217 // Even if directory creation fails, continue with in-memory GUID.
218 return h;
219 }
220
221 // EEXIST — verify it is actually a directory.
222 struct stat st;
223 if (stat(pathname, &st) != 0 || !S_ISDIR(st.st_mode)) {
224 nd_log(NDLS_DAEMON, NDLP_ERR,
225 "MACHINE_GUID: path '%s' exists but is not a directory", pathname);
226 return h;
227 }
228 }
229
230 errno_clear();
231 if (!machine_guid_write_to_file(filename, &h))
232 nd_log(NDLS_DAEMON, NDLP_ERR, "MACHINE_GUID: cannot save GUID to file '%s'", filename);
233
234 return h;
235 }
236
237 ND_MACHINE_GUID *machine_guid_get(void) {
238 if(UUIDiszero(nd_machine_guid.uuid)) {
239 nd_machine_guid = machine_guid_get_or_create();
240 nd_setenv("NETDATA_REGISTRY_UNIQUE_ID", nd_machine_guid.txt, 1);
241 }
242
243 return &nd_machine_guid;
244 }
245
246 const char *machine_guid_get_txt(void) {
247 ND_MACHINE_GUID *h = machine_guid_get();
248 return h->txt;
249 }