1 #include "registry_internals.h"
3 struct registry registry;
5 // ----------------------------------------------------------------------------
8 // parse a GUID and re-generated to be always lower case
9 // this is used as a protection against the variations of GUIDs
10 int regenerate_guid(const char *guid, char *result) {
12 if(unlikely(uuid_parse(guid, uuid) == -1)) {
13 info("Registry: GUID '%s' is not a valid GUID.", guid);
17 uuid_unparse_lower(uuid, result);
19 #ifdef NETDATA_INTERNAL_CHECKS
20 if(strcmp(guid, result))
21 info("GUID '%s' and re-generated GUID '%s' differ!", guid, result);
22 #endif /* NETDATA_INTERNAL_CHECKS */
28 // make sure the names of the machines / URLs do not contain any tabs
29 // (which are used as our separator in the database files)
30 // and are properly trimmed (before and after)
31 static inline char *registry_fix_machine_name(char *name, size_t *len) {
32 char *s = name?name:"";
34 // skip leading spaces
35 while(*s && isspace(*s)) s++;
37 // make sure all spaces are a SPACE
40 if(unlikely(isspace(*t)))
46 // remove trailing spaces
61 static inline char *registry_fix_url(char *url, size_t *len) {
63 char *s = registry_fix_machine_name(url, &l);
65 // protection from too big URLs
66 if(l > registry.max_url_length) {
67 l = registry.max_url_length;
76 // ----------------------------------------------------------------------------
77 // forward definition of functions
79 extern REGISTRY_PERSON *registry_request_access(char *person_guid, char *machine_guid, char *url, char *name, time_t when);
80 extern REGISTRY_PERSON *registry_request_delete(char *person_guid, char *machine_guid, char *url, char *delete_url, time_t when);
83 // ----------------------------------------------------------------------------
86 // verify the person, the machine and the URL exist in our DB
87 REGISTRY_PERSON_URL *registry_verify_request(char *person_guid, char *machine_guid, char *url, REGISTRY_PERSON **pp, REGISTRY_MACHINE **mm) {
88 char pbuf[GUID_LEN + 1], mbuf[GUID_LEN + 1];
90 if(!person_guid || !*person_guid || !machine_guid || !*machine_guid || !url || !*url) {
91 info("Registry Request Verification: invalid request! person: '%s', machine '%s', url '%s'", person_guid?person_guid:"UNSET", machine_guid?machine_guid:"UNSET", url?url:"UNSET");
96 url = registry_fix_url(url, NULL);
98 // make sure the person GUID is valid
99 if(regenerate_guid(person_guid, pbuf) == -1) {
100 info("Registry Request Verification: invalid person GUID, person: '%s', machine '%s', url '%s'", person_guid, machine_guid, url);
105 // make sure the machine GUID is valid
106 if(regenerate_guid(machine_guid, mbuf) == -1) {
107 info("Registry Request Verification: invalid machine GUID, person: '%s', machine '%s', url '%s'", person_guid, machine_guid, url);
112 // make sure the machine exists
113 REGISTRY_MACHINE *m = registry_machine_find(machine_guid);
115 info("Registry Request Verification: machine not found, person: '%s', machine '%s', url '%s'", person_guid, machine_guid, url);
120 // make sure the person exist
121 REGISTRY_PERSON *p = registry_person_find(person_guid);
123 info("Registry Request Verification: person not found, person: '%s', machine '%s', url '%s'", person_guid, machine_guid, url);
128 REGISTRY_PERSON_URL *pu = registry_person_url_index_find(p, url);
130 info("Registry Request Verification: URL not found for person, person: '%s', machine '%s', url '%s'", person_guid, machine_guid, url);
137 // ----------------------------------------------------------------------------
140 REGISTRY_PERSON *registry_request_access(char *person_guid, char *machine_guid, char *url, char *name, time_t when) {
141 debug(D_REGISTRY, "registry_request_access('%s', '%s', '%s'): NEW REQUEST", (person_guid)?person_guid:"", machine_guid, url);
143 REGISTRY_MACHINE *m = registry_machine_get(machine_guid, when);
146 // make sure the name is valid
148 name = registry_fix_machine_name(name, &namelen);
151 url = registry_fix_url(url, &urllen);
153 REGISTRY_PERSON *p = registry_person_get(person_guid, when);
155 REGISTRY_URL *u = registry_url_get(url, urllen);
156 registry_person_link_to_url(p, m, u, name, namelen, when);
157 registry_machine_link_to_url(m, u, when);
159 registry_log('A', p, m, u, name);
161 registry.usages_count++;
166 REGISTRY_PERSON *registry_request_delete(char *person_guid, char *machine_guid, char *url, char *delete_url, time_t when) {
169 REGISTRY_PERSON *p = NULL;
170 REGISTRY_MACHINE *m = NULL;
171 REGISTRY_PERSON_URL *pu = registry_verify_request(person_guid, machine_guid, url, &p, &m);
172 if(!pu || !p || !m) return NULL;
175 delete_url = registry_fix_url(delete_url, NULL);
177 // make sure the user is not deleting the url it uses
178 if(!strcmp(delete_url, pu->url->url)) {
179 info("Registry Delete Request: delete URL is the one currently accessed, person: '%s', machine '%s', url '%s', delete url '%s'"
180 , p->guid, m->guid, pu->url->url, delete_url);
184 REGISTRY_PERSON_URL *dpu = registry_person_url_index_find(p, delete_url);
186 info("Registry Delete Request: URL not found for person: '%s', machine '%s', url '%s', delete url '%s'", p->guid
187 , m->guid, pu->url->url, delete_url);
191 registry_log('D', p, m, pu->url, dpu->url->url);
192 registry_person_unlink_from_url(p, dpu);
198 // a structure to pass to the dictionary_get_all() callback handler
199 struct machine_request_callback_data {
200 REGISTRY_MACHINE *find_this_machine;
201 REGISTRY_PERSON_URL *result;
204 // the callback function
205 // this will be run for every PERSON_URL of this PERSON
206 static int machine_request_callback(void *entry, void *data) {
207 REGISTRY_PERSON_URL *mypu = (REGISTRY_PERSON_URL *)entry;
208 struct machine_request_callback_data *myrdata = (struct machine_request_callback_data *)data;
210 if(mypu->machine == myrdata->find_this_machine) {
211 myrdata->result = mypu;
212 return -1; // this will also stop the walk through
215 return 0; // continue
218 REGISTRY_MACHINE *registry_request_machine(char *person_guid, char *machine_guid, char *url, char *request_machine, time_t when) {
221 char mbuf[GUID_LEN + 1];
223 REGISTRY_PERSON *p = NULL;
224 REGISTRY_MACHINE *m = NULL;
225 REGISTRY_PERSON_URL *pu = registry_verify_request(person_guid, machine_guid, url, &p, &m);
226 if(!pu || !p || !m) return NULL;
228 // make sure the machine GUID is valid
229 if(regenerate_guid(request_machine, mbuf) == -1) {
230 info("Registry Machine URLs request: invalid machine GUID, person: '%s', machine '%s', url '%s', request machine '%s'", p->guid, m->guid, pu->url->url, request_machine);
233 request_machine = mbuf;
235 // make sure the machine exists
236 m = registry_machine_find(request_machine);
238 info("Registry Machine URLs request: machine not found, person: '%s', machine '%s', url '%s', request machine '%s'", p->guid, machine_guid, pu->url->url, request_machine);
242 // Verify the user has in the past accessed this machine
243 // We will walk through the PERSON_URLs to find the machine
244 // linking to our machine
246 // a structure to pass to the dictionary_get_all() callback handler
247 struct machine_request_callback_data rdata = { m, NULL };
249 // request a walk through on the dictionary
250 avl_traverse(&p->person_urls, machine_request_callback, &rdata);
259 // ----------------------------------------------------------------------------
260 // REGISTRY THIS MACHINE UNIQUE ID
262 static inline int is_machine_guid_blacklisted(const char *guid) {
263 // these are machine GUIDs that have been included in distribution packages.
264 // we blacklist them here, so that the next version of netdata will generate
267 if(!strcmp(guid, "8a795b0c-2311-11e6-8563-000c295076a6")
268 || !strcmp(guid, "4aed1458-1c3e-11e6-a53f-000c290fc8f5")
270 error("Blacklisted machine GUID '%s' found.", guid);
277 char *registry_get_this_machine_guid(void) {
278 static char guid[GUID_LEN + 1] = "";
284 int fd = open(registry.machine_guid_filename, O_RDONLY);
286 char buf[GUID_LEN + 1];
287 if(read(fd, buf, GUID_LEN) != GUID_LEN)
288 error("Failed to read machine GUID from '%s'", registry.machine_guid_filename);
290 buf[GUID_LEN] = '\0';
291 if(regenerate_guid(buf, guid) == -1) {
292 error("Failed to validate machine GUID '%s' from '%s'. Ignoring it - this might mean this netdata will appear as duplicate in the registry.",
293 buf, registry.machine_guid_filename);
297 else if(is_machine_guid_blacklisted(guid))
303 // generate a new one?
307 uuid_generate_time(uuid);
308 uuid_unparse_lower(uuid, guid);
309 guid[GUID_LEN] = '\0';
312 fd = open(registry.machine_guid_filename, O_WRONLY|O_CREAT|O_TRUNC, 444);
314 fatal("Cannot create unique machine id file '%s'. Please fix this.", registry.machine_guid_filename);
316 if(write(fd, guid, GUID_LEN) != GUID_LEN)
317 fatal("Cannot write the unique machine id file '%s'. Please fix this.", registry.machine_guid_filename);
322 setenv("NETDATA_REGISTRY_UNIQUE_ID", guid, 1);