registry_internals.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #include "daemon/common.h"
  3. #include "registry_internals.h"
  4. struct registry registry;
  5. // ----------------------------------------------------------------------------
  6. // common functions
  7. // parse a GUID and re-generated to be always lower case
  8. // this is used as a protection against the variations of GUIDs
  9. int regenerate_guid(const char *guid, char *result) {
  10. uuid_t uuid;
  11. if(unlikely(uuid_parse(guid, uuid) == -1)) {
  12. info("Registry: GUID '%s' is not a valid GUID.", guid);
  13. return -1;
  14. }
  15. else {
  16. uuid_unparse_lower(uuid, result);
  17. #ifdef NETDATA_INTERNAL_CHECKS
  18. if(strcmp(guid, result) != 0)
  19. info("GUID '%s' and re-generated GUID '%s' differ!", guid, result);
  20. #endif /* NETDATA_INTERNAL_CHECKS */
  21. }
  22. return 0;
  23. }
  24. // make sure the names of the machines / URLs do not contain any tabs
  25. // (which are used as our separator in the database files)
  26. // and are properly trimmed (before and after)
  27. static inline char *registry_fix_machine_name(char *name, size_t *len) {
  28. char *s = name?name:"";
  29. // skip leading spaces
  30. while(*s && isspace(*s)) s++;
  31. // make sure all spaces are a SPACE
  32. char *t = s;
  33. while(*t) {
  34. if(unlikely(isspace(*t)))
  35. *t = ' ';
  36. t++;
  37. }
  38. // remove trailing spaces
  39. while(--t >= s) {
  40. if(*t == ' ')
  41. *t = '\0';
  42. else
  43. break;
  44. }
  45. t++;
  46. if(likely(len))
  47. *len = (t - s);
  48. return s;
  49. }
  50. static inline char *registry_fix_url(char *url, size_t *len) {
  51. size_t l = 0;
  52. char *s = registry_fix_machine_name(url, &l);
  53. // protection from too big URLs
  54. if(l > registry.max_url_length) {
  55. l = registry.max_url_length;
  56. s[l] = '\0';
  57. }
  58. if(len) *len = l;
  59. return s;
  60. }
  61. // ----------------------------------------------------------------------------
  62. // HELPERS
  63. // verify the person, the machine and the URL exist in our DB
  64. REGISTRY_PERSON_URL *registry_verify_request(char *person_guid, char *machine_guid, char *url, REGISTRY_PERSON **pp, REGISTRY_MACHINE **mm) {
  65. char pbuf[GUID_LEN + 1], mbuf[GUID_LEN + 1];
  66. if(!person_guid || !*person_guid || !machine_guid || !*machine_guid || !url || !*url) {
  67. 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");
  68. return NULL;
  69. }
  70. // normalize the url
  71. url = registry_fix_url(url, NULL);
  72. // make sure the person GUID is valid
  73. if(regenerate_guid(person_guid, pbuf) == -1) {
  74. info("Registry Request Verification: invalid person GUID, person: '%s', machine '%s', url '%s'", person_guid, machine_guid, url);
  75. return NULL;
  76. }
  77. person_guid = pbuf;
  78. // make sure the machine GUID is valid
  79. if(regenerate_guid(machine_guid, mbuf) == -1) {
  80. info("Registry Request Verification: invalid machine GUID, person: '%s', machine '%s', url '%s'", person_guid, machine_guid, url);
  81. return NULL;
  82. }
  83. machine_guid = mbuf;
  84. // make sure the machine exists
  85. REGISTRY_MACHINE *m = registry_machine_find(machine_guid);
  86. if(!m) {
  87. info("Registry Request Verification: machine not found, person: '%s', machine '%s', url '%s'", person_guid, machine_guid, url);
  88. return NULL;
  89. }
  90. if(mm) *mm = m;
  91. // make sure the person exist
  92. REGISTRY_PERSON *p = registry_person_find(person_guid);
  93. if(!p) {
  94. info("Registry Request Verification: person not found, person: '%s', machine '%s', url '%s'", person_guid, machine_guid, url);
  95. return NULL;
  96. }
  97. if(pp) *pp = p;
  98. REGISTRY_PERSON_URL *pu = registry_person_url_index_find(p, url);
  99. if(!pu) {
  100. info("Registry Request Verification: URL not found for person, person: '%s', machine '%s', url '%s'", person_guid, machine_guid, url);
  101. return NULL;
  102. }
  103. //else if (pu->machine != m) {
  104. // info("Registry Request Verification: Machine mismatch: person: '%s', machine requested='%s' <> loaded='%s', url '%s'", person_guid, machine_guid, pu->machine->guid, url);
  105. // return NULL;
  106. //}
  107. return pu;
  108. }
  109. // ----------------------------------------------------------------------------
  110. // REGISTRY REQUESTS
  111. REGISTRY_PERSON *registry_request_access(char *person_guid, char *machine_guid, char *url, char *name, time_t when) {
  112. debug(D_REGISTRY, "registry_request_access('%s', '%s', '%s'): NEW REQUEST", (person_guid)?person_guid:"", machine_guid, url);
  113. REGISTRY_MACHINE *m = registry_machine_get(machine_guid, when);
  114. if(!m) return NULL;
  115. // make sure the name is valid
  116. size_t namelen;
  117. name = registry_fix_machine_name(name, &namelen);
  118. size_t urllen;
  119. url = registry_fix_url(url, &urllen);
  120. REGISTRY_PERSON *p = registry_person_get(person_guid, when);
  121. REGISTRY_URL *u = registry_url_get(url, urllen);
  122. registry_person_link_to_url(p, m, u, name, namelen, when);
  123. registry_machine_link_to_url(m, u, when);
  124. registry_log('A', p, m, u, name);
  125. registry.usages_count++;
  126. return p;
  127. }
  128. REGISTRY_PERSON *registry_request_delete(char *person_guid, char *machine_guid, char *url, char *delete_url, time_t when) {
  129. (void) when;
  130. REGISTRY_PERSON *p = NULL;
  131. REGISTRY_MACHINE *m = NULL;
  132. REGISTRY_PERSON_URL *pu = registry_verify_request(person_guid, machine_guid, url, &p, &m);
  133. if(!pu || !p || !m) return NULL;
  134. // normalize the url
  135. delete_url = registry_fix_url(delete_url, NULL);
  136. // make sure the user is not deleting the url it uses
  137. /*
  138. if(!strcmp(delete_url, pu->url->url)) {
  139. info("Registry Delete Request: delete URL is the one currently accessed, person: '%s', machine '%s', url '%s', delete url '%s'"
  140. , p->guid, m->guid, pu->url->url, delete_url);
  141. return NULL;
  142. }
  143. */
  144. REGISTRY_PERSON_URL *dpu = registry_person_url_index_find(p, delete_url);
  145. if(!dpu) {
  146. info("Registry Delete Request: URL not found for person: '%s', machine '%s', url '%s', delete url '%s'", p->guid
  147. , m->guid, pu->url->url, delete_url);
  148. return NULL;
  149. }
  150. registry_log('D', p, m, pu->url, dpu->url->url);
  151. registry_person_unlink_from_url(p, dpu);
  152. return p;
  153. }
  154. // a structure to pass to the dictionary_walkthrough_read() callback handler
  155. struct machine_request_callback_data {
  156. REGISTRY_MACHINE *find_this_machine;
  157. REGISTRY_PERSON_URL *result;
  158. };
  159. // the callback function
  160. // this will be run for every PERSON_URL of this PERSON
  161. static int machine_request_callback(void *entry, void *data) {
  162. REGISTRY_PERSON_URL *mypu = (REGISTRY_PERSON_URL *)entry;
  163. struct machine_request_callback_data *myrdata = (struct machine_request_callback_data *)data;
  164. if(mypu->machine == myrdata->find_this_machine) {
  165. myrdata->result = mypu;
  166. return -1; // this will also stop the walk through
  167. }
  168. return 0; // continue
  169. }
  170. REGISTRY_MACHINE *registry_request_machine(char *person_guid, char *machine_guid, char *url, char *request_machine, time_t when) {
  171. (void)when;
  172. char mbuf[GUID_LEN + 1];
  173. REGISTRY_PERSON *p = NULL;
  174. REGISTRY_MACHINE *m = NULL;
  175. REGISTRY_PERSON_URL *pu = registry_verify_request(person_guid, machine_guid, url, &p, &m);
  176. if(!pu || !p || !m) return NULL;
  177. // make sure the machine GUID is valid
  178. if(regenerate_guid(request_machine, mbuf) == -1) {
  179. 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);
  180. return NULL;
  181. }
  182. request_machine = mbuf;
  183. // make sure the machine exists
  184. m = registry_machine_find(request_machine);
  185. if(!m) {
  186. 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);
  187. return NULL;
  188. }
  189. // Verify the user has in the past accessed this machine
  190. // We will walk through the PERSON_URLs to find the machine
  191. // linking to our machine
  192. // a structure to pass to the dictionary_walkthrough_read() callback handler
  193. struct machine_request_callback_data rdata = { m, NULL };
  194. // request a walk through on the dictionary
  195. avl_traverse(&p->person_urls, machine_request_callback, &rdata);
  196. if(rdata.result)
  197. return m;
  198. return NULL;
  199. }
  200. // ----------------------------------------------------------------------------
  201. // REGISTRY THIS MACHINE UNIQUE ID
  202. static inline int is_machine_guid_blacklisted(const char *guid) {
  203. // these are machine GUIDs that have been included in distribution packages.
  204. // we blacklist them here, so that the next version of netdata will generate
  205. // new ones.
  206. if(!strcmp(guid, "8a795b0c-2311-11e6-8563-000c295076a6")
  207. || !strcmp(guid, "4aed1458-1c3e-11e6-a53f-000c290fc8f5")
  208. ) {
  209. error("Blacklisted machine GUID '%s' found.", guid);
  210. return 1;
  211. }
  212. return 0;
  213. }
  214. char *registry_get_this_machine_hostname(void) {
  215. return registry.hostname;
  216. }
  217. char *registry_get_this_machine_guid(void) {
  218. static char guid[GUID_LEN + 1] = "";
  219. if(likely(guid[0]))
  220. return guid;
  221. // read it from disk
  222. int fd = open(registry.machine_guid_filename, O_RDONLY);
  223. if(fd != -1) {
  224. char buf[GUID_LEN + 1];
  225. if(read(fd, buf, GUID_LEN) != GUID_LEN)
  226. error("Failed to read machine GUID from '%s'", registry.machine_guid_filename);
  227. else {
  228. buf[GUID_LEN] = '\0';
  229. if(regenerate_guid(buf, guid) == -1) {
  230. error("Failed to validate machine GUID '%s' from '%s'. Ignoring it - this might mean this netdata will appear as duplicate in the registry.",
  231. buf, registry.machine_guid_filename);
  232. guid[0] = '\0';
  233. }
  234. else if(is_machine_guid_blacklisted(guid))
  235. guid[0] = '\0';
  236. }
  237. close(fd);
  238. }
  239. // generate a new one?
  240. if(!guid[0]) {
  241. uuid_t uuid;
  242. uuid_generate_time(uuid);
  243. uuid_unparse_lower(uuid, guid);
  244. guid[GUID_LEN] = '\0';
  245. // save it
  246. fd = open(registry.machine_guid_filename, O_WRONLY|O_CREAT|O_TRUNC, 444);
  247. if(fd == -1)
  248. fatal("Cannot create unique machine id file '%s'. Please fix this.", registry.machine_guid_filename);
  249. if(write(fd, guid, GUID_LEN) != GUID_LEN)
  250. fatal("Cannot write the unique machine id file '%s'. Please fix this.", registry.machine_guid_filename);
  251. close(fd);
  252. }
  253. setenv("NETDATA_REGISTRY_UNIQUE_ID", guid, 1);
  254. return guid;
  255. }