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 registry_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("Registry: source 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) {
62 return registry_fix_machine_name(url, len);
66 // ----------------------------------------------------------------------------
67 // forward definition of functions
69 extern REGISTRY_PERSON *registry_request_access(char *person_guid, char *machine_guid, char *url, char *name, time_t when);
70 extern REGISTRY_PERSON *registry_request_delete(char *person_guid, char *machine_guid, char *url, char *delete_url, time_t when);
73 // ----------------------------------------------------------------------------
76 // verify the person, the machine and the URL exist in our DB
77 REGISTRY_PERSON_URL *registry_verify_request(char *person_guid, char *machine_guid, char *url, REGISTRY_PERSON **pp, REGISTRY_MACHINE **mm) {
78 char pbuf[GUID_LEN + 1], mbuf[GUID_LEN + 1];
80 if(!person_guid || !*person_guid || !machine_guid || !*machine_guid || !url || !*url) {
81 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");
86 url = registry_fix_url(url, NULL);
88 // make sure the person GUID is valid
89 if(registry_regenerate_guid(person_guid, pbuf) == -1) {
90 info("Registry Request Verification: invalid person GUID, person: '%s', machine '%s', url '%s'", person_guid, machine_guid, url);
95 // make sure the machine GUID is valid
96 if(registry_regenerate_guid(machine_guid, mbuf) == -1) {
97 info("Registry Request Verification: invalid machine GUID, person: '%s', machine '%s', url '%s'", person_guid, machine_guid, url);
102 // make sure the machine exists
103 REGISTRY_MACHINE *m = registry_machine_find(machine_guid);
105 info("Registry Request Verification: machine not found, person: '%s', machine '%s', url '%s'", person_guid, machine_guid, url);
110 // make sure the person exist
111 REGISTRY_PERSON *p = registry_person_find(person_guid);
113 info("Registry Request Verification: person not found, person: '%s', machine '%s', url '%s'", person_guid, machine_guid, url);
118 REGISTRY_PERSON_URL *pu = dictionary_get(p->person_urls, url);
120 info("Registry Request Verification: URL not found for person, person: '%s', machine '%s', url '%s'", person_guid, machine_guid, url);
127 // ----------------------------------------------------------------------------
130 REGISTRY_PERSON *registry_request_access(char *person_guid, char *machine_guid, char *url, char *name, time_t when) {
131 debug(D_REGISTRY, "registry_request_access('%s', '%s', '%s'): NEW REQUEST", (person_guid)?person_guid:"", machine_guid, url);
133 REGISTRY_MACHINE *m = registry_machine_get(machine_guid, when);
136 // make sure the name is valid
138 name = registry_fix_machine_name(name, &namelen);
141 url = registry_fix_url(url, &urllen);
143 REGISTRY_PERSON *p = registry_person_get(person_guid, when);
145 REGISTRY_URL *u = registry_url_get(url, urllen);
146 registry_person_link_to_url(p, m, u, name, namelen, when);
147 registry_machine_link_to_url(m, u, when);
149 registry_log('A', p, m, u, name);
151 registry.usages_count++;
156 REGISTRY_PERSON *registry_request_delete(char *person_guid, char *machine_guid, char *url, char *delete_url, time_t when) {
159 REGISTRY_PERSON *p = NULL;
160 REGISTRY_MACHINE *m = NULL;
161 REGISTRY_PERSON_URL *pu = registry_verify_request(person_guid, machine_guid, url, &p, &m);
162 if(!pu || !p || !m) return NULL;
165 delete_url = registry_fix_url(delete_url, NULL);
167 // make sure the user is not deleting the url it uses
168 if(!strcmp(delete_url, pu->url->url)) {
169 info("Registry Delete Request: delete URL is the one currently accessed, person: '%s', machine '%s', url '%s', delete url '%s'", p->guid, m->guid, pu->url->url, delete_url);
173 REGISTRY_PERSON_URL *dpu = dictionary_get(p->person_urls, delete_url);
175 info("Registry Delete Request: URL not found for person: '%s', machine '%s', url '%s', delete url '%s'", p->guid, m->guid, pu->url->url, delete_url);
179 registry_log('D', p, m, pu->url, dpu->url->url);
181 dictionary_del(p->person_urls, dpu->url->url);
183 registry_url_unlink(dpu->url);
191 // a structure to pass to the dictionary_get_all() callback handler
192 struct machine_request_callback_data {
193 REGISTRY_MACHINE *find_this_machine;
194 REGISTRY_PERSON_URL *result;
197 // the callback function
198 // this will be run for every PERSON_URL of this PERSON
199 static int machine_request_callback(void *entry, void *data) {
200 REGISTRY_PERSON_URL *mypu = (REGISTRY_PERSON_URL *)entry;
201 struct machine_request_callback_data *myrdata = (struct machine_request_callback_data *)data;
203 if(mypu->machine == myrdata->find_this_machine) {
204 myrdata->result = mypu;
205 return -1; // this will also stop the walk through
208 return 0; // continue
211 REGISTRY_MACHINE *registry_request_machine(char *person_guid, char *machine_guid, char *url, char *request_machine, time_t when) {
214 char mbuf[GUID_LEN + 1];
216 REGISTRY_PERSON *p = NULL;
217 REGISTRY_MACHINE *m = NULL;
218 REGISTRY_PERSON_URL *pu = registry_verify_request(person_guid, machine_guid, url, &p, &m);
219 if(!pu || !p || !m) return NULL;
221 // make sure the machine GUID is valid
222 if(registry_regenerate_guid(request_machine, mbuf) == -1) {
223 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);
226 request_machine = mbuf;
228 // make sure the machine exists
229 m = registry_machine_find(request_machine);
231 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);
235 // Verify the user has in the past accessed this machine
236 // We will walk through the PERSON_URLs to find the machine
237 // linking to our machine
239 // a structure to pass to the dictionary_get_all() callback handler
240 struct machine_request_callback_data rdata = { m, NULL };
242 // request a walk through on the dictionary
243 // no need for locking here, the underlying dictionary has its own
244 dictionary_get_all(p->person_urls, machine_request_callback, &rdata);
253 // ----------------------------------------------------------------------------
254 // REGISTRY THIS MACHINE UNIQUE ID
256 static inline int is_machine_guid_blacklisted(const char *guid) {
257 // these are machine GUIDs that have been included in distribution packages.
258 // we blacklist them here, so that the next version of netdata will generate
261 if(!strcmp(guid, "8a795b0c-2311-11e6-8563-000c295076a6")
262 || !strcmp(guid, "4aed1458-1c3e-11e6-a53f-000c290fc8f5")
264 error("Blacklisted machine GUID '%s' found.", guid);
271 char *registry_get_this_machine_guid(void) {
272 if(likely(registry.machine_guid[0]))
273 return registry.machine_guid;
276 int fd = open(registry.machine_guid_filename, O_RDONLY);
278 char buf[GUID_LEN + 1];
279 if(read(fd, buf, GUID_LEN) != GUID_LEN)
280 error("Failed to read machine GUID from '%s'", registry.machine_guid_filename);
282 buf[GUID_LEN] = '\0';
283 if(registry_regenerate_guid(buf, registry.machine_guid) == -1) {
284 error("Failed to validate machine GUID '%s' from '%s'. Ignoring it - this might mean this netdata will appear as duplicate in the registry.",
285 buf, registry.machine_guid_filename);
287 registry.machine_guid[0] = '\0';
289 else if(is_machine_guid_blacklisted(registry.machine_guid))
290 registry.machine_guid[0] = '\0';
295 // generate a new one?
296 if(!registry.machine_guid[0]) {
299 uuid_generate_time(uuid);
300 uuid_unparse_lower(uuid, registry.machine_guid);
301 registry.machine_guid[GUID_LEN] = '\0';
304 fd = open(registry.machine_guid_filename, O_WRONLY|O_CREAT|O_TRUNC, 444);
306 fatal("Cannot create unique machine id file '%s'. Please fix this.", registry.machine_guid_filename);
308 if(write(fd, registry.machine_guid, GUID_LEN) != GUID_LEN)
309 fatal("Cannot write the unique machine id file '%s'. Please fix this.", registry.machine_guid_filename);
314 setenv("NETDATA_REGISTRY_UNIQUE_ID", registry.machine_guid, 1);
316 return registry.machine_guid;