// verify the person, the machine and the URL exist in our DB REGISTRY_PERSON_URL *registry_verify_request(char *person_guid, char *machine_guid, char *url, REGISTRY_PERSON **pp, REGISTRY_MACHINE **mm) { char pbuf[GUID_LEN + 1], mbuf[GUID_LEN + 1]; if(!person_guid || !*person_guid || !machine_guid || !*machine_guid || !url || !*url) { 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"); return NULL; } // normalize the url url = registry_fix_url(url, NULL); // make sure the person GUID is valid if(regenerate_guid(person_guid, pbuf) == -1) { info("Registry Request Verification: invalid person GUID, person: '%s', machine '%s', url '%s'", person_guid, machine_guid, url); return NULL; } person_guid = pbuf; // make sure the machine GUID is valid if(regenerate_guid(machine_guid, mbuf) == -1) { info("Registry Request Verification: invalid machine GUID, person: '%s', machine '%s', url '%s'", person_guid, machine_guid, url); return NULL; } machine_guid = mbuf; // make sure the machine exists REGISTRY_MACHINE *m = registry_machine_find(machine_guid); if(!m) { info("Registry Request Verification: machine not found, person: '%s', machine '%s', url '%s'", person_guid, machine_guid, url); return NULL; } if(mm) *mm = m; // make sure the person exist REGISTRY_PERSON *p = registry_person_find(person_guid); if(!p) { info("Registry Request Verification: person not found, person: '%s', machine '%s', url '%s'", person_guid, machine_guid, url); return NULL; } if(pp) *pp = p; REGISTRY_PERSON_URL *pu = registry_person_url_index_find(p, url); if(!pu) { info("Registry Request Verification: URL not found for person, person: '%s', machine '%s', url '%s'", person_guid, machine_guid, url); return NULL; } return pu; }
char *registry_get_this_machine_guid(void) { static char guid[GUID_LEN + 1] = ""; if(likely(guid[0])) return guid; // read it from disk int fd = open(registry.machine_guid_filename, O_RDONLY); if(fd != -1) { char buf[GUID_LEN + 1]; if(read(fd, buf, GUID_LEN) != GUID_LEN) error("Failed to read machine GUID from '%s'", registry.machine_guid_filename); else { buf[GUID_LEN] = '\0'; if(regenerate_guid(buf, guid) == -1) { error("Failed to validate machine GUID '%s' from '%s'. Ignoring it - this might mean this netdata will appear as duplicate in the registry.", buf, registry.machine_guid_filename); guid[0] = '\0'; } else if(is_machine_guid_blacklisted(guid)) guid[0] = '\0'; } close(fd); } // generate a new one? if(!guid[0]) { uuid_t uuid; uuid_generate_time(uuid); uuid_unparse_lower(uuid, guid); guid[GUID_LEN] = '\0'; // save it fd = open(registry.machine_guid_filename, O_WRONLY|O_CREAT|O_TRUNC, 444); if(fd == -1) fatal("Cannot create unique machine id file '%s'. Please fix this.", registry.machine_guid_filename); if(write(fd, guid, GUID_LEN) != GUID_LEN) fatal("Cannot write the unique machine id file '%s'. Please fix this.", registry.machine_guid_filename); close(fd); } setenv("NETDATA_REGISTRY_UNIQUE_ID", guid, 1); return guid; }
// 1. validate machine GUID // 2. if it is valid, find it or create it and return it // 3. if it is not valid, return NULL REGISTRY_MACHINE *registry_machine_get(const char *machine_guid, time_t when) { REGISTRY_MACHINE *m = NULL; if(likely(machine_guid && *machine_guid)) { // validate it is a GUID char buf[GUID_LEN + 1]; if(unlikely(regenerate_guid(machine_guid, buf) == -1)) info("Registry: machine guid '%s' is not a valid guid. Ignoring it.", machine_guid); else { machine_guid = buf; m = registry_machine_find(machine_guid); if(!m) m = registry_machine_allocate(machine_guid, when); } } return m; }
// 1. validate person GUID // 2. if it is valid, find it // 3. if it is not valid, create a new one // 4. return it REGISTRY_PERSON *registry_person_get(const char *person_guid, time_t when) { debug(D_REGISTRY, "Registry: registry_person_get('%s'): creating dictionary of urls", person_guid); REGISTRY_PERSON *p = NULL; if(person_guid && *person_guid) { char buf[GUID_LEN + 1]; // validate it is a GUID if(unlikely(regenerate_guid(person_guid, buf) == -1)) info("Registry: person guid '%s' is not a valid guid. Ignoring it.", person_guid); else { person_guid = buf; p = registry_person_find(person_guid); } } if(!p) p = registry_person_allocate(NULL, when); return p; }
REGISTRY_MACHINE *registry_request_machine(char *person_guid, char *machine_guid, char *url, char *request_machine, time_t when) { (void)when; char mbuf[GUID_LEN + 1]; REGISTRY_PERSON *p = NULL; REGISTRY_MACHINE *m = NULL; REGISTRY_PERSON_URL *pu = registry_verify_request(person_guid, machine_guid, url, &p, &m); if(!pu || !p || !m) return NULL; // make sure the machine GUID is valid if(regenerate_guid(request_machine, mbuf) == -1) { 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); return NULL; } request_machine = mbuf; // make sure the machine exists m = registry_machine_find(request_machine); if(!m) { 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); return NULL; } // Verify the user has in the past accessed this machine // We will walk through the PERSON_URLs to find the machine // linking to our machine // a structure to pass to the dictionary_get_all() callback handler struct machine_request_callback_data rdata = { m, NULL }; // request a walk through on the dictionary avl_traverse(&p->person_urls, machine_request_callback, &rdata); if(rdata.result) return m; return NULL; }