registry_internals.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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. netdata_log_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. netdata_log_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(const 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. netdata_log_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. netdata_log_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. netdata_log_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. netdata_log_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. netdata_log_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. STRING *u = string_strdupz(url);
  99. REGISTRY_PERSON_URL *pu = registry_person_url_index_find(p, u);
  100. string_freez(u);
  101. if(!pu) {
  102. netdata_log_info("Registry Request Verification: URL not found for person, person: '%s', machine '%s', url '%s'", person_guid, machine_guid, url);
  103. return NULL;
  104. }
  105. //else if (pu->machine != m) {
  106. // netdata_log_info("Registry Request Verification: Machine mismatch: person: '%s', machine requested='%s' <> loaded='%s', url '%s'", person_guid, machine_guid, pu->machine->guid, url);
  107. // return NULL;
  108. //}
  109. return pu;
  110. }
  111. // ----------------------------------------------------------------------------
  112. // REGISTRY REQUESTS
  113. REGISTRY_PERSON *registry_request_access(const char *person_guid, char *machine_guid, char *url, char *name, time_t when) {
  114. netdata_log_debug(D_REGISTRY, "registry_request_access('%s', '%s', '%s'): NEW REQUEST", (person_guid)?person_guid:"", machine_guid, url);
  115. bool is_dummy = is_dummy_person(person_guid);
  116. REGISTRY_MACHINE *m = registry_machine_find_or_create(machine_guid, when, is_dummy);
  117. if(!m) return NULL;
  118. REGISTRY_PERSON *p = registry_person_find_or_create(person_guid, when, is_dummy);
  119. // make sure the name is valid
  120. size_t name_len;
  121. name = registry_fix_machine_name(name, &name_len);
  122. size_t url_len;
  123. url = registry_fix_url(url, &url_len);
  124. STRING *u = string_strdupz(url);
  125. if(!is_dummy)
  126. registry_person_link_to_url(p, m, u, name, name_len, when);
  127. registry_machine_link_to_url(m, u, when);
  128. registry_log('A', p, m, u, name);
  129. registry.usages_count++;
  130. return p;
  131. }
  132. REGISTRY_PERSON *registry_request_delete(const char *person_guid, char *machine_guid, char *url, char *delete_url, time_t when) {
  133. (void) when;
  134. REGISTRY_PERSON *p = NULL;
  135. REGISTRY_MACHINE *m = NULL;
  136. REGISTRY_PERSON_URL *pu = registry_verify_request(person_guid, machine_guid, url, &p, &m);
  137. if(!pu || !p || !m) return NULL;
  138. // normalize the url
  139. delete_url = registry_fix_url(delete_url, NULL);
  140. // make sure the user is not deleting the url it uses
  141. /*
  142. if(!strcmp(delete_url, pu->url->url)) {
  143. netdata_log_info("Registry Delete Request: delete URL is the one currently accessed, person: '%s', machine '%s', url '%s', delete url '%s'"
  144. , p->guid, m->guid, pu->url->url, delete_url);
  145. return NULL;
  146. }
  147. */
  148. STRING *d_url = string_strdupz(delete_url);
  149. REGISTRY_PERSON_URL *dpu = registry_person_url_index_find(p, d_url);
  150. string_freez(d_url);
  151. if(!dpu) {
  152. netdata_log_info("Registry Delete Request: URL not found for person: '%s', machine '%s', url '%s', delete url '%s'", p->guid
  153. , m->guid, string2str(pu->url), delete_url);
  154. return NULL;
  155. }
  156. registry_log('D', p, m, pu->url, string2str(dpu->url));
  157. registry_person_unlink_from_url(p, dpu);
  158. return p;
  159. }
  160. REGISTRY_MACHINE *registry_request_machine(const char *person_guid, char *request_machine, STRING **hostname) {
  161. char pbuf[GUID_LEN + 1];
  162. char mbuf[GUID_LEN + 1];
  163. // make sure the person GUID is valid
  164. if(regenerate_guid(person_guid, pbuf) == -1) {
  165. netdata_log_info("REGISTRY: %s(): invalid person GUID '%s'", __FUNCTION__ , person_guid);
  166. return NULL;
  167. }
  168. person_guid = pbuf;
  169. // make sure the person GUID is valid
  170. if(regenerate_guid(request_machine, mbuf) == -1) {
  171. netdata_log_info("REGISTRY: %s(): invalid search machine GUID '%s'", __FUNCTION__ , request_machine);
  172. return NULL;
  173. }
  174. request_machine = mbuf;
  175. REGISTRY_PERSON *p = registry_person_find(person_guid);
  176. if(!p) return NULL;
  177. REGISTRY_MACHINE *m = registry_machine_find(request_machine);
  178. if(!m) return NULL;
  179. // Verify the user has in the past accessed this machine
  180. // We will walk through the PERSON_URLs to find the machine
  181. // linking to our machine
  182. // make sure the user has access
  183. for(REGISTRY_PERSON_URL *pu = p->person_urls; pu ;pu = pu->next)
  184. if(pu->machine == m) {
  185. *hostname = string_dup(pu->machine_name);
  186. return m;
  187. }
  188. return NULL;
  189. }
  190. // ----------------------------------------------------------------------------
  191. // REGISTRY THIS MACHINE UNIQUE ID
  192. static inline int is_machine_guid_blacklisted(const char *guid) {
  193. // these are machine GUIDs that have been included in distribution packages.
  194. // we blacklist them here, so that the next version of netdata will generate
  195. // new ones.
  196. if(!strcmp(guid, "8a795b0c-2311-11e6-8563-000c295076a6")
  197. || !strcmp(guid, "4aed1458-1c3e-11e6-a53f-000c290fc8f5")
  198. ) {
  199. netdata_log_error("Blacklisted machine GUID '%s' found.", guid);
  200. return 1;
  201. }
  202. return 0;
  203. }
  204. char *registry_get_this_machine_hostname(void) {
  205. return registry.hostname;
  206. }
  207. char *registry_get_this_machine_guid(void) {
  208. static char guid[GUID_LEN + 1] = "";
  209. if(likely(guid[0]))
  210. return guid;
  211. // read it from disk
  212. int fd = open(registry.machine_guid_filename, O_RDONLY);
  213. if(fd != -1) {
  214. char buf[GUID_LEN + 1];
  215. if(read(fd, buf, GUID_LEN) != GUID_LEN)
  216. netdata_log_error("Failed to read machine GUID from '%s'", registry.machine_guid_filename);
  217. else {
  218. buf[GUID_LEN] = '\0';
  219. if(regenerate_guid(buf, guid) == -1) {
  220. netdata_log_error("Failed to validate machine GUID '%s' from '%s'. Ignoring it - this might mean this netdata will appear as duplicate in the registry.",
  221. buf, registry.machine_guid_filename);
  222. guid[0] = '\0';
  223. }
  224. else if(is_machine_guid_blacklisted(guid))
  225. guid[0] = '\0';
  226. }
  227. close(fd);
  228. }
  229. // generate a new one?
  230. if(!guid[0]) {
  231. uuid_t uuid;
  232. uuid_generate_time(uuid);
  233. uuid_unparse_lower(uuid, guid);
  234. guid[GUID_LEN] = '\0';
  235. // save it
  236. fd = open(registry.machine_guid_filename, O_WRONLY|O_CREAT|O_TRUNC, 444);
  237. if(fd == -1)
  238. fatal("Cannot create unique machine id file '%s'. Please fix this.", registry.machine_guid_filename);
  239. if(write(fd, guid, GUID_LEN) != GUID_LEN)
  240. fatal("Cannot write the unique machine id file '%s'. Please fix this.", registry.machine_guid_filename);
  241. close(fd);
  242. }
  243. setenv("NETDATA_REGISTRY_UNIQUE_ID", guid, 1);
  244. return guid;
  245. }