]> arthur.barton.de Git - netdata.git/blob - src/registry.c
Merge pull request #890 from ktsaou/master
[netdata.git] / src / registry.c
1 #include "common.h"
2
3 // ----------------------------------------------------------------------------
4 // TODO
5 //
6 // 1. the default tracking cookie expires in 1 year, but the persons are not
7 //    removed from the db - this means the database only grows - ideally the
8 //    database should be cleaned in registry_save() for both on-disk and
9 //    on-memory entries.
10 //
11 //    Cleanup:
12 //    i. Find all the PERSONs that have expired cookie
13 //    ii. For each of their PERSON_URLs:
14 //     - decrement the linked MACHINE links
15 //     - if the linked MACHINE has no other links, remove the linked MACHINE too
16 //     - remove the PERSON_URL
17 //
18 // 2. add protection to prevent abusing the registry by flooding it with
19 //    requests to fill the memory and crash it.
20 //
21 //    Possible protections:
22 //    - limit the number of URLs per person
23 //    - limit the number of URLs per machine
24 //    - limit the number of persons
25 //    - limit the number of machines
26 //    - [DONE] limit the size of URLs
27 //    - [DONE] limit the size of PERSON_URL names
28 //    - limit the number of requests that add data to the registry,
29 //      per client IP per hour
30
31
32
33 #define REGISTRY_URL_FLAGS_DEFAULT 0x00
34 #define REGISTRY_URL_FLAGS_EXPIRED 0x01
35
36 #define DICTIONARY_FLAGS DICTIONARY_FLAG_VALUE_LINK_DONT_CLONE | DICTIONARY_FLAG_NAME_LINK_DONT_CLONE
37
38 // ----------------------------------------------------------------------------
39 // COMMON structures
40
41 struct registry {
42     int enabled;
43
44     char machine_guid[36 + 1];
45
46     // entries counters / statistics
47     unsigned long long persons_count;
48     unsigned long long machines_count;
49     unsigned long long usages_count;
50     unsigned long long urls_count;
51     unsigned long long persons_urls_count;
52     unsigned long long machines_urls_count;
53     unsigned long long log_count;
54
55     // memory counters / statistics
56     unsigned long long persons_memory;
57     unsigned long long machines_memory;
58     unsigned long long urls_memory;
59     unsigned long long persons_urls_memory;
60     unsigned long long machines_urls_memory;
61
62     // configuration
63     unsigned long long save_registry_every_entries;
64     char *registry_domain;
65     char *hostname;
66     char *registry_to_announce;
67     time_t persons_expiration; // seconds to expire idle persons
68     int verify_cookies_redirects;
69
70     size_t max_url_length;
71     size_t max_name_length;
72
73     // file/path names
74     char *pathname;
75     char *db_filename;
76     char *log_filename;
77     char *machine_guid_filename;
78
79     // open files
80     FILE *log_fp;
81
82     // the database
83     DICTIONARY *persons;    // dictionary of PERSON *, with key the PERSON.guid
84     DICTIONARY *machines;   // dictionary of MACHINE *, with key the MACHINE.guid
85     DICTIONARY *urls;       // dictionary of URL *, with key the URL.url
86
87     // concurrency locking
88     // we keep different locks for different things
89     // so that many tasks can be completed in parallel
90     pthread_mutex_t persons_lock;
91     pthread_mutex_t machines_lock;
92     pthread_mutex_t urls_lock;
93     pthread_mutex_t person_urls_lock;
94     pthread_mutex_t machine_urls_lock;
95     pthread_mutex_t log_lock;
96 } registry;
97
98
99 // ----------------------------------------------------------------------------
100 // URL structures
101 // Save memory by de-duplicating URLs
102 // so instead of storing URLs all over the place
103 // we store them here and we keep pointers elsewhere
104
105 struct url {
106     uint32_t links; // the number of links to this URL - when none is left, we free it
107     uint16_t len;   // the length of the URL in bytes
108     char url[1];    // the URL - dynamically allocated to more size
109 };
110 typedef struct url URL;
111
112
113 // ----------------------------------------------------------------------------
114 // MACHINE structures
115
116 // For each MACHINE-URL pair we keep this
117 struct machine_url {
118     URL *url;                   // de-duplicated URL
119 //  DICTIONARY *persons;        // dictionary of PERSON *
120
121     uint8_t flags;
122     uint32_t first_t;           // the first time we saw this
123     uint32_t last_t;            // the last time we saw this
124     uint32_t usages;            // how many times this has been accessed
125 };
126 typedef struct machine_url MACHINE_URL;
127
128 // A machine
129 struct machine {
130     char guid[36 + 1];          // the GUID
131
132     uint32_t links;             // the number of PERSON_URLs linked to this machine
133
134     DICTIONARY *urls;           // MACHINE_URL *
135
136     uint32_t first_t;           // the first time we saw this
137     uint32_t last_t;            // the last time we saw this
138     uint32_t usages;            // how many times this has been accessed
139 };
140 typedef struct machine MACHINE;
141
142
143 // ----------------------------------------------------------------------------
144 // PERSON structures
145
146 // for each PERSON-URL pair we keep this
147 struct person_url {
148     URL *url;                   // de-duplicated URL
149     MACHINE *machine;           // link the MACHINE of this URL
150
151     uint8_t flags;
152     uint32_t first_t;           // the first time we saw this
153     uint32_t last_t;            // the last time we saw this
154     uint32_t usages;            // how many times this has been accessed
155
156     char name[1];               // the name of the URL, as known by the user
157                                 // dynamically allocated to fit properly
158 };
159 typedef struct person_url PERSON_URL;
160
161 // A person
162 struct person {
163     char guid[36 + 1];          // the person GUID
164
165     DICTIONARY *urls;           // dictionary of PERSON_URL *
166
167     uint32_t first_t;           // the first time we saw this
168     uint32_t last_t;            // the last time we saw this
169     uint32_t usages;            // how many times this has been accessed
170 };
171 typedef struct person PERSON;
172
173
174 // ----------------------------------------------------------------------------
175 // REGISTRY concurrency locking
176
177 static inline void registry_persons_lock(void) {
178     pthread_mutex_lock(&registry.persons_lock);
179 }
180
181 static inline void registry_persons_unlock(void) {
182     pthread_mutex_unlock(&registry.persons_lock);
183 }
184
185 static inline void registry_machines_lock(void) {
186     pthread_mutex_lock(&registry.machines_lock);
187 }
188
189 static inline void registry_machines_unlock(void) {
190     pthread_mutex_unlock(&registry.machines_lock);
191 }
192
193 static inline void registry_urls_lock(void) {
194     pthread_mutex_lock(&registry.urls_lock);
195 }
196
197 static inline void registry_urls_unlock(void) {
198     pthread_mutex_unlock(&registry.urls_lock);
199 }
200
201 // ideally, we should not lock the whole registry for
202 // updating a person's urls.
203 // however, to save the memory required for keeping a
204 // mutex (40 bytes) per person, we do...
205 static inline void registry_person_urls_lock(PERSON *p) {
206     (void)p;
207     pthread_mutex_lock(&registry.person_urls_lock);
208 }
209
210 static inline void registry_person_urls_unlock(PERSON *p) {
211     (void)p;
212     pthread_mutex_unlock(&registry.person_urls_lock);
213 }
214
215 // ideally, we should not lock the whole registry for
216 // updating a machine's urls.
217 // however, to save the memory required for keeping a
218 // mutex (40 bytes) per machine, we do...
219 static inline void registry_machine_urls_lock(MACHINE *m) {
220     (void)m;
221     pthread_mutex_lock(&registry.machine_urls_lock);
222 }
223
224 static inline void registry_machine_urls_unlock(MACHINE *m) {
225     (void)m;
226     pthread_mutex_unlock(&registry.machine_urls_lock);
227 }
228
229 static inline void registry_log_lock(void) {
230     pthread_mutex_lock(&registry.log_lock);
231 }
232
233 static inline void registry_log_unlock(void) {
234     pthread_mutex_unlock(&registry.log_lock);
235 }
236
237
238 // ----------------------------------------------------------------------------
239 // common functions
240
241 // parse a GUID and re-generated to be always lower case
242 // this is used as a protection against the variations of GUIDs
243 static inline int registry_regenerate_guid(const char *guid, char *result) {
244     uuid_t uuid;
245     if(unlikely(uuid_parse(guid, uuid) == -1)) {
246         info("Registry: GUID '%s' is not a valid GUID.", guid);
247         return -1;
248     }
249     else {
250         uuid_unparse_lower(uuid, result);
251
252 #ifdef NETDATA_INTERNAL_CHECKS
253         if(strcmp(guid, result))
254             info("Registry: source GUID '%s' and re-generated GUID '%s' differ!", guid, result);
255 #endif /* NETDATA_INTERNAL_CHECKS */
256     }
257
258     return 0;
259 }
260
261 // make sure the names of the machines / URLs do not contain any tabs
262 // (which are used as our separator in the database files)
263 // and are properly trimmed (before and after)
264 static inline char *registry_fix_machine_name(char *name, size_t *len) {
265     char *s = name?name:"";
266
267     // skip leading spaces
268     while(*s && isspace(*s)) s++;
269
270     // make sure all spaces are a SPACE
271     char *t = s;
272     while(*t) {
273         if(unlikely(isspace(*t)))
274             *t = ' ';
275
276         t++;
277     }
278
279     // remove trailing spaces
280     while(--t >= s) {
281         if(*t == ' ')
282             *t = '\0';
283         else
284             break;
285     }
286     t++;
287
288     if(likely(len))
289         *len = (t - s);
290
291     return s;
292 }
293
294 static inline char *registry_fix_url(char *url, size_t *len) {
295     return registry_fix_machine_name(url, len);
296 }
297
298
299 // ----------------------------------------------------------------------------
300 // forward definition of functions
301
302 extern PERSON *registry_request_access(char *person_guid, char *machine_guid, char *url, char *name, time_t when);
303 extern PERSON *registry_request_delete(char *person_guid, char *machine_guid, char *url, char *delete_url, time_t when);
304
305
306 // ----------------------------------------------------------------------------
307 // URL
308
309 static inline URL *registry_url_allocate_nolock(const char *url, size_t urllen) {
310     // protection from too big URLs
311     if(urllen > registry.max_url_length)
312         urllen = registry.max_url_length;
313
314     debug(D_REGISTRY, "Registry: registry_url_allocate_nolock('%s'): allocating %zu bytes", url, sizeof(URL) + urllen);
315     URL *u = mallocz(sizeof(URL) + urllen);
316
317     // a simple strcpy() should do the job
318     // but I prefer to be safe, since the caller specified urllen
319     u->len = (uint16_t)urllen;
320     strncpyz(u->url, url, u->len);
321     u->links = 0;
322
323     registry.urls_memory += sizeof(URL) + urllen;
324
325     debug(D_REGISTRY, "Registry: registry_url_allocate_nolock('%s'): indexing it", url);
326     dictionary_set(registry.urls, u->url, u, sizeof(URL));
327
328     return u;
329 }
330
331 static inline URL *registry_url_get_nolock(const char *url, size_t urllen) {
332     debug(D_REGISTRY, "Registry: registry_url_get_nolock('%s')", url);
333
334     URL *u = dictionary_get(registry.urls, url);
335     if(!u) {
336         u = registry_url_allocate_nolock(url, urllen);
337         registry.urls_count++;
338     }
339
340     return u;
341 }
342
343 static inline URL *registry_url_get(const char *url, size_t urllen) {
344     debug(D_REGISTRY, "Registry: registry_url_get('%s')", url);
345
346     registry_urls_lock();
347
348     URL *u = registry_url_get_nolock(url, urllen);
349
350     registry_urls_unlock();
351
352     return u;
353 }
354
355 static inline void registry_url_link_nolock(URL *u) {
356     u->links++;
357     debug(D_REGISTRY, "Registry: registry_url_link_nolock('%s'): URL has now %u links", u->url, u->links);
358 }
359
360 static inline void registry_url_unlink_nolock(URL *u) {
361     u->links--;
362     if(!u->links) {
363         debug(D_REGISTRY, "Registry: registry_url_unlink_nolock('%s'): No more links for this URL", u->url);
364         dictionary_del(registry.urls, u->url);
365         freez(u);
366     }
367     else
368         debug(D_REGISTRY, "Registry: registry_url_unlink_nolock('%s'): URL has %u links left", u->url, u->links);
369 }
370
371
372 // ----------------------------------------------------------------------------
373 // MACHINE
374
375 static inline MACHINE *registry_machine_find(const char *machine_guid) {
376     debug(D_REGISTRY, "Registry: registry_machine_find('%s')", machine_guid);
377     return dictionary_get(registry.machines, machine_guid);
378 }
379
380 static inline MACHINE_URL *registry_machine_url_allocate(MACHINE *m, URL *u, time_t when) {
381     debug(D_REGISTRY, "registry_machine_link_to_url('%s', '%s'): allocating %zu bytes", m->guid, u->url, sizeof(MACHINE_URL));
382
383     MACHINE_URL *mu = mallocz(sizeof(MACHINE_URL));
384
385     // mu->persons = dictionary_create(DICTIONARY_FLAGS);
386     // dictionary_set(mu->persons, p->guid, p, sizeof(PERSON));
387
388     mu->first_t = mu->last_t = (uint32_t)when;
389     mu->usages = 1;
390     mu->url = u;
391     mu->flags = REGISTRY_URL_FLAGS_DEFAULT;
392
393     registry.machines_urls_memory += sizeof(MACHINE_URL);
394
395     debug(D_REGISTRY, "registry_machine_link_to_url('%s', '%s'): indexing URL in machine", m->guid, u->url);
396     dictionary_set(m->urls, u->url, mu, sizeof(MACHINE_URL));
397     registry_url_link_nolock(u);
398
399     return mu;
400 }
401
402 static inline MACHINE *registry_machine_allocate(const char *machine_guid, time_t when) {
403     debug(D_REGISTRY, "Registry: registry_machine_allocate('%s'): creating new machine, sizeof(MACHINE)=%zu", machine_guid, sizeof(MACHINE));
404
405     MACHINE *m = mallocz(sizeof(MACHINE));
406
407     strncpyz(m->guid, machine_guid, 36);
408
409     debug(D_REGISTRY, "Registry: registry_machine_allocate('%s'): creating dictionary of urls", machine_guid);
410     m->urls = dictionary_create(DICTIONARY_FLAGS);
411
412     m->first_t = m->last_t = (uint32_t)when;
413     m->usages = 0;
414
415     registry.machines_memory += sizeof(MACHINE);
416
417     registry.machines_count++;
418     dictionary_set(registry.machines, m->guid, m, sizeof(MACHINE));
419
420     return m;
421 }
422
423 // 1. validate machine GUID
424 // 2. if it is valid, find it or create it and return it
425 // 3. if it is not valid, return NULL
426 static inline MACHINE *registry_machine_get(const char *machine_guid, time_t when) {
427     MACHINE *m = NULL;
428
429     registry_machines_lock();
430
431     if(likely(machine_guid && *machine_guid)) {
432         // validate it is a GUID
433         char buf[36 + 1];
434         if(unlikely(registry_regenerate_guid(machine_guid, buf) == -1))
435             info("Registry: machine guid '%s' is not a valid guid. Ignoring it.", machine_guid);
436         else {
437             machine_guid = buf;
438             m = registry_machine_find(machine_guid);
439             if(!m) m = registry_machine_allocate(machine_guid, when);
440         }
441     }
442
443     registry_machines_unlock();
444
445     return m;
446 }
447
448
449 // ----------------------------------------------------------------------------
450 // PERSON
451
452 static inline PERSON *registry_person_find(const char *person_guid) {
453     debug(D_REGISTRY, "Registry: registry_person_find('%s')", person_guid);
454     return dictionary_get(registry.persons, person_guid);
455 }
456
457 static inline PERSON_URL *registry_person_url_allocate(PERSON *p, MACHINE *m, URL *u, char *name, size_t namelen, time_t when) {
458     // protection from too big names
459     if(namelen > registry.max_name_length)
460         namelen = registry.max_name_length;
461
462     debug(D_REGISTRY, "registry_person_url_allocate('%s', '%s', '%s'): allocating %zu bytes", p->guid, m->guid, u->url,
463           sizeof(PERSON_URL) + namelen);
464
465     PERSON_URL *pu = mallocz(sizeof(PERSON_URL) + namelen);
466
467     // a simple strcpy() should do the job
468     // but I prefer to be safe, since the caller specified urllen
469     strncpyz(pu->name, name, namelen);
470
471     pu->machine = m;
472     pu->first_t = pu->last_t = when;
473     pu->usages = 1;
474     pu->url = u;
475     pu->flags = REGISTRY_URL_FLAGS_DEFAULT;
476     m->links++;
477
478     registry.persons_urls_memory += sizeof(PERSON_URL) + namelen;
479
480     debug(D_REGISTRY, "registry_person_url_allocate('%s', '%s', '%s'): indexing URL in person", p->guid, m->guid, u->url);
481     dictionary_set(p->urls, u->url, pu, sizeof(PERSON_URL));
482     registry_url_link_nolock(u);
483
484     return pu;
485 }
486
487 static inline PERSON_URL *registry_person_url_reallocate(PERSON *p, MACHINE *m, URL *u, char *name, size_t namelen, time_t when, PERSON_URL *pu) {
488     // this function is needed to change the name of a PERSON_URL
489
490     debug(D_REGISTRY, "registry_person_url_reallocate('%s', '%s', '%s'): allocating %zu bytes", p->guid, m->guid, u->url,
491           sizeof(PERSON_URL) + namelen);
492
493     PERSON_URL *tpu = registry_person_url_allocate(p, m, u, name, namelen, when);
494     tpu->first_t = pu->first_t;
495     tpu->last_t = pu->last_t;
496     tpu->usages = pu->usages;
497
498     // ok, these are a hack - since the registry_person_url_allocate() is
499     // adding these, we have to subtract them
500     tpu->machine->links--;
501     registry.persons_urls_memory -= sizeof(PERSON_URL) + strlen(pu->name);
502     registry_url_unlink_nolock(u);
503
504     freez(pu);
505
506     return tpu;
507 }
508
509 static inline PERSON *registry_person_allocate(const char *person_guid, time_t when) {
510     PERSON *p = NULL;
511
512     debug(D_REGISTRY, "Registry: registry_person_allocate('%s'): allocating new person, sizeof(PERSON)=%zu", (person_guid)?person_guid:"", sizeof(PERSON));
513
514     p = mallocz(sizeof(PERSON));
515
516     if(!person_guid) {
517         for (; ;) {
518             uuid_t uuid;
519             uuid_generate(uuid);
520             uuid_unparse_lower(uuid, p->guid);
521
522             debug(D_REGISTRY, "Registry: Checking if the generated person guid '%s' is unique", p->guid);
523             if (!dictionary_get(registry.persons, p->guid)) {
524                 debug(D_REGISTRY, "Registry: generated person guid '%s' is unique", p->guid);
525                 break;
526             }
527             else
528                 info("Registry: generated person guid '%s' found in the registry. Retrying...", p->guid);
529         }
530     }
531     else
532         strncpyz(p->guid, person_guid, 36);
533
534     debug(D_REGISTRY, "Registry: registry_person_allocate('%s'): creating dictionary of urls", p->guid);
535     p->urls = dictionary_create(DICTIONARY_FLAGS);
536
537     p->first_t = p->last_t = when;
538     p->usages = 0;
539
540     registry.persons_memory += sizeof(PERSON);
541
542     registry.persons_count++;
543     dictionary_set(registry.persons, p->guid, p, sizeof(PERSON));
544
545     return p;
546 }
547
548
549 // 1. validate person GUID
550 // 2. if it is valid, find it
551 // 3. if it is not valid, create a new one
552 // 4. return it
553 static inline PERSON *registry_person_get(const char *person_guid, time_t when) {
554     PERSON *p = NULL;
555
556     registry_persons_lock();
557
558     if(person_guid && *person_guid) {
559         char buf[36 + 1];
560         // validate it is a GUID
561         if(unlikely(registry_regenerate_guid(person_guid, buf) == -1))
562             info("Registry: person guid '%s' is not a valid guid. Ignoring it.", person_guid);
563         else {
564             person_guid = buf;
565             p = registry_person_find(person_guid);
566             if(!p) person_guid = NULL;
567         }
568     }
569
570     if(!p) p = registry_person_allocate(NULL, when);
571
572     registry_persons_unlock();
573
574     return p;
575 }
576
577 // ----------------------------------------------------------------------------
578 // LINKING OF OBJECTS
579
580 static inline PERSON_URL *registry_person_link_to_url(PERSON *p, MACHINE *m, URL *u, char *name, size_t namelen, time_t when) {
581     debug(D_REGISTRY, "registry_person_link_to_url('%s', '%s', '%s'): searching for URL in person", p->guid, m->guid, u->url);
582
583     registry_person_urls_lock(p);
584
585     PERSON_URL *pu = dictionary_get(p->urls, u->url);
586     if(!pu) {
587         debug(D_REGISTRY, "registry_person_link_to_url('%s', '%s', '%s'): not found", p->guid, m->guid, u->url);
588         pu = registry_person_url_allocate(p, m, u, name, namelen, when);
589         registry.persons_urls_count++;
590     }
591     else {
592         debug(D_REGISTRY, "registry_person_link_to_url('%s', '%s', '%s'): found", p->guid, m->guid, u->url);
593         pu->usages++;
594         if(likely(pu->last_t < (uint32_t)when)) pu->last_t = when;
595
596         if(pu->machine != m) {
597             MACHINE_URL *mu = dictionary_get(pu->machine->urls, u->url);
598             if(mu) {
599                 info("registry_person_link_to_url('%s', '%s', '%s'): URL switched machines (old was '%s') - expiring it from previous machine.",
600                      p->guid, m->guid, u->url, pu->machine->guid);
601                 mu->flags |= REGISTRY_URL_FLAGS_EXPIRED;
602             }
603             else {
604                 info("registry_person_link_to_url('%s', '%s', '%s'): URL switched machines (old was '%s') - but the URL is not linked to the old machine.",
605                      p->guid, m->guid, u->url, pu->machine->guid);
606             }
607
608             pu->machine->links--;
609             pu->machine = m;
610         }
611
612         if(strcmp(pu->name, name)) {
613             // the name of the PERSON_URL has changed !
614             pu = registry_person_url_reallocate(p, m, u, name, namelen, when, pu);
615         }
616     }
617
618     p->usages++;
619     if(likely(p->last_t < (uint32_t)when)) p->last_t = when;
620
621     if(pu->flags & REGISTRY_URL_FLAGS_EXPIRED) {
622         info("registry_person_link_to_url('%s', '%s', '%s'): accessing an expired URL. Re-enabling URL.", p->guid, m->guid, u->url);
623         pu->flags &= ~REGISTRY_URL_FLAGS_EXPIRED;
624     }
625
626     registry_person_urls_unlock(p);
627
628     return pu;
629 }
630
631 static inline MACHINE_URL *registry_machine_link_to_url(PERSON *p, MACHINE *m, URL *u, time_t when) {
632     debug(D_REGISTRY, "registry_machine_link_to_url('%s', '%s', '%s'): searching for URL in machine", p->guid, m->guid, u->url);
633
634     registry_machine_urls_lock(m);
635
636     MACHINE_URL *mu = dictionary_get(m->urls, u->url);
637     if(!mu) {
638         debug(D_REGISTRY, "registry_machine_link_to_url('%s', '%s', '%s'): not found", p->guid, m->guid, u->url);
639         mu = registry_machine_url_allocate(m, u, when);
640         registry.machines_urls_count++;
641     }
642     else {
643         debug(D_REGISTRY, "registry_machine_link_to_url('%s', '%s', '%s'): found", p->guid, m->guid, u->url);
644         mu->usages++;
645         if(likely(mu->last_t < (uint32_t)when)) mu->last_t = when;
646     }
647
648     //debug(D_REGISTRY, "registry_machine_link_to_url('%s', '%s', '%s'): indexing person in machine", p->guid, m->guid, u->url);
649     //dictionary_set(mu->persons, p->guid, p, sizeof(PERSON));
650
651     m->usages++;
652     if(likely(m->last_t < (uint32_t)when)) m->last_t = when;
653
654     if(mu->flags & REGISTRY_URL_FLAGS_EXPIRED) {
655         info("registry_machine_link_to_url('%s', '%s', '%s'): accessing an expired URL.", p->guid, m->guid, u->url);
656         mu->flags &= ~REGISTRY_URL_FLAGS_EXPIRED;
657     }
658
659     registry_machine_urls_unlock(m);
660
661     return mu;
662 }
663
664 // ----------------------------------------------------------------------------
665 // REGISTRY LOG LOAD/SAVE
666
667 static inline int registry_should_save_db(void) {
668     debug(D_REGISTRY, "log entries %llu, max %llu", registry.log_count, registry.save_registry_every_entries);
669     return registry.log_count > registry.save_registry_every_entries;
670 }
671
672 static inline void registry_log(const char action, PERSON *p, MACHINE *m, URL *u, char *name) {
673     if(likely(registry.log_fp)) {
674         // we lock only if the file is open
675         // to allow replaying the log at registry_log_load()
676         registry_log_lock();
677
678         if(unlikely(fprintf(registry.log_fp, "%c\t%08x\t%s\t%s\t%s\t%s\n",
679                 action,
680                 p->last_t,
681                 p->guid,
682                 m->guid,
683                 name,
684                 u->url) < 0))
685             error("Registry: failed to save log. Registry data may be lost in case of abnormal restart.");
686
687         // we increase the counter even on failures
688         // so that the registry will be saved periodically
689         registry.log_count++;
690
691         registry_log_unlock();
692
693         // this must be outside the log_lock(), or a deadlock will happen.
694         // registry_save() checks the same inside the log_lock, so only
695         // one thread will save the db
696         if(unlikely(registry_should_save_db()))
697             registry_save();
698     }
699 }
700
701 static inline int registry_log_open_nolock(void) {
702     if(registry.log_fp)
703         fclose(registry.log_fp);
704
705     registry.log_fp = fopen(registry.log_filename, "a");
706
707     if(registry.log_fp) {
708         if (setvbuf(registry.log_fp, NULL, _IOLBF, 0) != 0)
709             error("Cannot set line buffering on registry log file.");
710         return 0;
711     }
712
713     error("Cannot open registry log file '%s'. Registry data will be lost in case of netdata or server crash.", registry.log_filename);
714     return -1;
715 }
716
717 static inline void registry_log_close_nolock(void) {
718     if(registry.log_fp) {
719         fclose(registry.log_fp);
720         registry.log_fp = NULL;
721     }
722 }
723
724 static inline void registry_log_recreate_nolock(void) {
725     if(registry.log_fp != NULL) {
726         registry_log_close_nolock();
727
728         // open it with truncate
729         registry.log_fp = fopen(registry.log_filename, "w");
730         if(registry.log_fp) fclose(registry.log_fp);
731         else error("Cannot truncate registry log '%s'", registry.log_filename);
732
733         registry.log_fp = NULL;
734
735         registry_log_open_nolock();
736     }
737 }
738
739 int registry_log_load(void) {
740     char *s, buf[4096 + 1];
741     size_t line = -1;
742
743     // closing the log is required here
744     // otherwise we will append to it the values we read
745     registry_log_close_nolock();
746
747     debug(D_REGISTRY, "Registry: loading active db from: %s", registry.log_filename);
748     FILE *fp = fopen(registry.log_filename, "r");
749     if(!fp)
750         error("Registry: cannot open registry file: %s", registry.log_filename);
751     else {
752         line = 0;
753         size_t len = 0;
754         while ((s = fgets_trim_len(buf, 4096, fp, &len))) {
755             line++;
756
757             switch (s[0]) {
758                 case 'A': // accesses
759                 case 'D': // deletes
760
761                     // verify it is valid
762                     if (unlikely(len < 85 || s[1] != '\t' || s[10] != '\t' || s[47] != '\t' || s[84] != '\t')) {
763                         error("Registry: log line %zu is wrong (len = %zu).", line, len);
764                         continue;
765                     }
766                     s[1] = s[10] = s[47] = s[84] = '\0';
767
768                     // get the variables
769                     time_t when = strtoul(&s[2], NULL, 16);
770                     char *person_guid = &s[11];
771                     char *machine_guid = &s[48];
772                     char *name = &s[85];
773
774                     // skip the name to find the url
775                     char *url = name;
776                     while(*url && *url != '\t') url++;
777                     if(!*url) {
778                         error("Registry: log line %zu does not have a url.", line);
779                         continue;
780                     }
781                     *url++ = '\0';
782
783                     // make sure the person exists
784                     // without this, a new person guid will be created
785                     PERSON *p = registry_person_find(person_guid);
786                     if(!p) p = registry_person_allocate(person_guid, when);
787
788                     if(s[0] == 'A')
789                         registry_request_access(p->guid, machine_guid, url, name, when);
790                     else
791                         registry_request_delete(p->guid, machine_guid, url, name, when);
792
793                     registry.log_count++;
794                     break;
795
796                 default:
797                     error("Registry: ignoring line %zu of filename '%s': %s.", line, registry.log_filename, s);
798                     break;
799             }
800         }
801         
802         fclose(fp);
803     }
804
805     // open the log again
806     registry_log_open_nolock();
807
808     return line;
809 }
810
811
812 // ----------------------------------------------------------------------------
813 // REGISTRY REQUESTS
814
815 PERSON *registry_request_access(char *person_guid, char *machine_guid, char *url, char *name, time_t when) {
816     debug(D_REGISTRY, "registry_request_access('%s', '%s', '%s'): NEW REQUEST", (person_guid)?person_guid:"", machine_guid, url);
817
818     MACHINE *m = registry_machine_get(machine_guid, when);
819     if(!m) return NULL;
820
821     // make sure the name is valid
822     size_t namelen;
823     name = registry_fix_machine_name(name, &namelen);
824
825     size_t urllen;
826     url = registry_fix_url(url, &urllen);
827
828     URL *u = registry_url_get(url, urllen);
829     PERSON *p = registry_person_get(person_guid, when);
830
831     registry_person_link_to_url(p, m, u, name, namelen, when);
832     registry_machine_link_to_url(p, m, u, when);
833
834     registry_log('A', p, m, u, name);
835
836     registry.usages_count++;
837     return p;
838 }
839
840 // verify the person, the machine and the URL exist in our DB
841 PERSON_URL *registry_verify_request(char *person_guid, char *machine_guid, char *url, PERSON **pp, MACHINE **mm) {
842     char pbuf[36 + 1], mbuf[36 + 1];
843
844     if(!person_guid || !*person_guid || !machine_guid || !*machine_guid || !url || !*url) {
845         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");
846         return NULL;
847     }
848
849     // normalize the url
850     url = registry_fix_url(url, NULL);
851
852     // make sure the person GUID is valid
853     if(registry_regenerate_guid(person_guid, pbuf) == -1) {
854         info("Registry Request Verification: invalid person GUID, person: '%s', machine '%s', url '%s'", person_guid, machine_guid, url);
855         return NULL;
856     }
857     person_guid = pbuf;
858
859     // make sure the machine GUID is valid
860     if(registry_regenerate_guid(machine_guid, mbuf) == -1) {
861         info("Registry Request Verification: invalid machine GUID, person: '%s', machine '%s', url '%s'", person_guid, machine_guid, url);
862         return NULL;
863     }
864     machine_guid = mbuf;
865
866     // make sure the machine exists
867     MACHINE *m = registry_machine_find(machine_guid);
868     if(!m) {
869         info("Registry Request Verification: machine not found, person: '%s', machine '%s', url '%s'", person_guid, machine_guid, url);
870         return NULL;
871     }
872     if(mm) *mm = m;
873
874     // make sure the person exist
875     PERSON *p = registry_person_find(person_guid);
876     if(!p) {
877         info("Registry Request Verification: person not found, person: '%s', machine '%s', url '%s'", person_guid, machine_guid, url);
878         return NULL;
879     }
880     if(pp) *pp = p;
881
882     PERSON_URL *pu = dictionary_get(p->urls, url);
883     if(!pu) {
884         info("Registry Request Verification: URL not found for person, person: '%s', machine '%s', url '%s'", person_guid, machine_guid, url);
885         return NULL;
886     }
887     return pu;
888 }
889
890 PERSON *registry_request_delete(char *person_guid, char *machine_guid, char *url, char *delete_url, time_t when) {
891     (void)when;
892
893     PERSON *p = NULL;
894     MACHINE *m = NULL;
895     PERSON_URL *pu = registry_verify_request(person_guid, machine_guid, url, &p, &m);
896     if(!pu || !p || !m) return NULL;
897
898     // normalize the url
899     delete_url = registry_fix_url(delete_url, NULL);
900
901     // make sure the user is not deleting the url it uses
902     if(!strcmp(delete_url, pu->url->url)) {
903         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);
904         return NULL;
905     }
906
907     registry_person_urls_lock(p);
908
909     PERSON_URL *dpu = dictionary_get(p->urls, delete_url);
910     if(!dpu) {
911         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);
912         registry_person_urls_unlock(p);
913         return NULL;
914     }
915
916     registry_log('D', p, m, pu->url, dpu->url->url);
917
918     dictionary_del(p->urls, dpu->url->url);
919     registry_url_unlink_nolock(dpu->url);
920     freez(dpu);
921
922     registry_person_urls_unlock(p);
923     return p;
924 }
925
926
927 // a structure to pass to the dictionary_get_all() callback handler
928 struct machine_request_callback_data {
929     MACHINE *find_this_machine;
930     PERSON_URL *result;
931 };
932
933 // the callback function
934 // this will be run for every PERSON_URL of this PERSON
935 int machine_request_callback(void *entry, void *data) {
936     PERSON_URL *mypu = (PERSON_URL *)entry;
937     struct machine_request_callback_data *myrdata = (struct machine_request_callback_data *)data;
938
939     if(mypu->machine == myrdata->find_this_machine) {
940         myrdata->result = mypu;
941         return -1; // this will also stop the walk through
942     }
943
944     return 0; // continue
945 }
946
947 MACHINE *registry_request_machine(char *person_guid, char *machine_guid, char *url, char *request_machine, time_t when) {
948     (void)when;
949
950     char mbuf[36 + 1];
951
952     PERSON *p = NULL;
953     MACHINE *m = NULL;
954     PERSON_URL *pu = registry_verify_request(person_guid, machine_guid, url, &p, &m);
955     if(!pu || !p || !m) return NULL;
956
957     // make sure the machine GUID is valid
958     if(registry_regenerate_guid(request_machine, mbuf) == -1) {
959         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);
960         return NULL;
961     }
962     request_machine = mbuf;
963
964     // make sure the machine exists
965     m = registry_machine_find(request_machine);
966     if(!m) {
967         info("Registry Machine URLs request: machine not found, person: '%s', machine '%s', url '%s', request machine '%s'", p->guid, m->guid, pu->url->url, request_machine);
968         return NULL;
969     }
970
971     // Verify the user has in the past accessed this machine
972     // We will walk through the PERSON_URLs to find the machine
973     // linking to our machine
974
975     // a structure to pass to the dictionary_get_all() callback handler
976     struct machine_request_callback_data rdata = { m, NULL };
977
978     // request a walk through on the dictionary
979     // no need for locking here, the underlying dictionary has its own
980     dictionary_get_all(p->urls, machine_request_callback, &rdata);
981
982     if(rdata.result)
983         return m;
984
985     return NULL;
986 }
987
988
989 // ----------------------------------------------------------------------------
990 // REGISTRY JSON generation
991
992 #define REGISTRY_STATUS_OK "ok"
993 #define REGISTRY_STATUS_FAILED "failed"
994 #define REGISTRY_STATUS_DISABLED "disabled"
995
996 int registry_verify_cookies_redirects(void) {
997     return registry.verify_cookies_redirects;
998 }
999
1000 const char *registry_to_announce(void) {
1001     return registry.registry_to_announce;
1002 }
1003
1004 void registry_set_cookie(struct web_client *w, const char *guid) {
1005     char edate[100];
1006     time_t et = time(NULL) + registry.persons_expiration;
1007     struct tm etmbuf, *etm = gmtime_r(&et, &etmbuf);
1008     strftime(edate, sizeof(edate), "%a, %d %b %Y %H:%M:%S %Z", etm);
1009
1010     snprintfz(w->cookie1, COOKIE_MAX, NETDATA_REGISTRY_COOKIE_NAME "=%s; Expires=%s", guid, edate);
1011
1012     if(registry.registry_domain && registry.registry_domain[0])
1013         snprintfz(w->cookie2, COOKIE_MAX, NETDATA_REGISTRY_COOKIE_NAME "=%s; Domain=%s; Expires=%s", guid, registry.registry_domain, edate);
1014 }
1015
1016 static inline void registry_set_person_cookie(struct web_client *w, PERSON *p) {
1017     registry_set_cookie(w, p->guid);
1018 }
1019
1020 static inline void registry_json_header(struct web_client *w, const char *action, const char *status) {
1021     buffer_flush(w->response.data);
1022     w->response.data->contenttype = CT_APPLICATION_JSON;
1023     buffer_sprintf(w->response.data, "{\n\t\"action\": \"%s\",\n\t\"status\": \"%s\",\n\t\"hostname\": \"%s\",\n\t\"machine_guid\": \"%s\"",
1024                    action, status, registry.hostname, registry.machine_guid);
1025 }
1026
1027 static inline void registry_json_footer(struct web_client *w) {
1028     buffer_strcat(w->response.data, "\n}\n");
1029 }
1030
1031 int registry_request_hello_json(struct web_client *w) {
1032     registry_json_header(w, "hello", REGISTRY_STATUS_OK);
1033
1034     buffer_sprintf(w->response.data, ",\n\t\"registry\": \"%s\"",
1035                    registry.registry_to_announce);
1036
1037     registry_json_footer(w);
1038     return 200;
1039 }
1040
1041 static inline int registry_json_disabled(struct web_client *w, const char *action) {
1042     registry_json_header(w, action, REGISTRY_STATUS_DISABLED);
1043
1044     buffer_sprintf(w->response.data, ",\n\t\"registry\": \"%s\"",
1045                    registry.registry_to_announce);
1046
1047     registry_json_footer(w);
1048     return 200;
1049 }
1050
1051 // structure used be the callbacks below
1052 struct registry_json_walk_person_urls_callback {
1053     PERSON *p;
1054     MACHINE *m;
1055     struct web_client *w;
1056     int count;
1057 };
1058
1059 // callback for rendering PERSON_URLs
1060 static inline int registry_json_person_url_callback(void *entry, void *data) {
1061     PERSON_URL *pu = (PERSON_URL *)entry;
1062     struct registry_json_walk_person_urls_callback *c = (struct registry_json_walk_person_urls_callback *)data;
1063     struct web_client *w = c->w;
1064
1065     if(unlikely(c->count++))
1066         buffer_strcat(w->response.data, ",");
1067
1068     buffer_sprintf(w->response.data, "\n\t\t[ \"%s\", \"%s\", %u000, %u, \"%s\" ]",
1069                    pu->machine->guid, pu->url->url, pu->last_t, pu->usages, pu->name);
1070
1071     return 1;
1072 }
1073
1074 // callback for rendering MACHINE_URLs
1075 static inline int registry_json_machine_url_callback(void *entry, void *data) {
1076     MACHINE_URL *mu = (MACHINE_URL *)entry;
1077     struct registry_json_walk_person_urls_callback *c = (struct registry_json_walk_person_urls_callback *)data;
1078     struct web_client *w = c->w;
1079     MACHINE *m = c->m;
1080
1081     if(unlikely(c->count++))
1082         buffer_strcat(w->response.data, ",");
1083
1084     buffer_sprintf(w->response.data, "\n\t\t[ \"%s\", \"%s\", %u000, %u ]",
1085                    m->guid, mu->url->url, mu->last_t, mu->usages);
1086
1087     return 1;
1088 }
1089
1090
1091 // the main method for registering an access
1092 int registry_request_access_json(struct web_client *w, char *person_guid, char *machine_guid, char *url, char *name, time_t when) {
1093     if(!registry.enabled)
1094         return registry_json_disabled(w, "access");
1095
1096     PERSON *p = registry_request_access(person_guid, machine_guid, url, name, when);
1097     if(!p) {
1098         registry_json_header(w, "access", REGISTRY_STATUS_FAILED);
1099         registry_json_footer(w);
1100         return 412;
1101     }
1102
1103     // set the cookie
1104     registry_set_person_cookie(w, p);
1105
1106     // generate the response
1107     registry_json_header(w, "access", REGISTRY_STATUS_OK);
1108
1109     buffer_sprintf(w->response.data, ",\n\t\"person_guid\": \"%s\",\n\t\"urls\": [", p->guid);
1110     struct registry_json_walk_person_urls_callback c = { p, NULL, w, 0 };
1111     dictionary_get_all(p->urls, registry_json_person_url_callback, &c);
1112     buffer_strcat(w->response.data, "\n\t]\n");
1113
1114     registry_json_footer(w);
1115     return 200;
1116 }
1117
1118 // the main method for deleting a URL from a person
1119 int registry_request_delete_json(struct web_client *w, char *person_guid, char *machine_guid, char *url, char *delete_url, time_t when) {
1120     if(!registry.enabled)
1121         return registry_json_disabled(w, "delete");
1122
1123     PERSON *p = registry_request_delete(person_guid, machine_guid, url, delete_url, when);
1124     if(!p) {
1125         registry_json_header(w, "delete", REGISTRY_STATUS_FAILED);
1126         registry_json_footer(w);
1127         return 412;
1128     }
1129
1130     // generate the response
1131     registry_json_header(w, "delete", REGISTRY_STATUS_OK);
1132     registry_json_footer(w);
1133     return 200;
1134 }
1135
1136 // the main method for searching the URLs of a netdata
1137 int registry_request_search_json(struct web_client *w, char *person_guid, char *machine_guid, char *url, char *request_machine, time_t when) {
1138     if(!registry.enabled)
1139         return registry_json_disabled(w, "search");
1140
1141     MACHINE *m = registry_request_machine(person_guid, machine_guid, url, request_machine, when);
1142     if(!m) {
1143         registry_json_header(w, "search", REGISTRY_STATUS_FAILED);
1144         registry_json_footer(w);
1145         return 404;
1146     }
1147
1148     registry_json_header(w, "search", REGISTRY_STATUS_OK);
1149
1150     buffer_strcat(w->response.data, ",\n\t\"urls\": [");
1151     struct registry_json_walk_person_urls_callback c = { NULL, m, w, 0 };
1152     dictionary_get_all(m->urls, registry_json_machine_url_callback, &c);
1153     buffer_strcat(w->response.data, "\n\t]\n");
1154
1155     registry_json_footer(w);
1156     return 200;
1157 }
1158
1159 // structure used be the callbacks below
1160 struct registry_person_url_callback_verify_machine_exists_data {
1161     MACHINE *m;
1162     int count;
1163 };
1164
1165 int registry_person_url_callback_verify_machine_exists(void *entry, void *data) {
1166     struct registry_person_url_callback_verify_machine_exists_data *d = (struct registry_person_url_callback_verify_machine_exists_data *)data;
1167     PERSON_URL *pu = (PERSON_URL *)entry;
1168     MACHINE *m = d->m;
1169
1170     if(pu->machine == m)
1171         d->count++;
1172
1173     return 0;
1174 }
1175
1176 // the main method for switching user identity
1177 int registry_request_switch_json(struct web_client *w, char *person_guid, char *machine_guid, char *url, char *new_person_guid, time_t when) {
1178     (void)url;
1179     (void)when;
1180
1181     if(!registry.enabled)
1182         return registry_json_disabled(w, "switch");
1183
1184     PERSON *op = registry_person_find(person_guid);
1185     if(!op) {
1186         registry_json_header(w, "switch", REGISTRY_STATUS_FAILED);
1187         registry_json_footer(w);
1188         return 430;
1189     }
1190
1191     PERSON *np = registry_person_find(new_person_guid);
1192     if(!np) {
1193         registry_json_header(w, "switch", REGISTRY_STATUS_FAILED);
1194         registry_json_footer(w);
1195         return 431;
1196     }
1197
1198     MACHINE *m = registry_machine_find(machine_guid);
1199     if(!m) {
1200         registry_json_header(w, "switch", REGISTRY_STATUS_FAILED);
1201         registry_json_footer(w);
1202         return 432;
1203     }
1204
1205     struct registry_person_url_callback_verify_machine_exists_data data = { m, 0 };
1206
1207     // verify the old person has access to this machine
1208     dictionary_get_all(op->urls, registry_person_url_callback_verify_machine_exists, &data);
1209     if(!data.count) {
1210         registry_json_header(w, "switch", REGISTRY_STATUS_FAILED);
1211         registry_json_footer(w);
1212         return 433;
1213     }
1214
1215     // verify the new person has access to this machine
1216     data.count = 0;
1217     dictionary_get_all(np->urls, registry_person_url_callback_verify_machine_exists, &data);
1218     if(!data.count) {
1219         registry_json_header(w, "switch", REGISTRY_STATUS_FAILED);
1220         registry_json_footer(w);
1221         return 434;
1222     }
1223
1224     // set the cookie of the new person
1225     // the user just switched identity
1226     registry_set_person_cookie(w, np);
1227
1228     // generate the response
1229     registry_json_header(w, "switch", REGISTRY_STATUS_OK);
1230     buffer_sprintf(w->response.data, ",\n\t\"person_guid\": \"%s\"", np->guid);
1231     registry_json_footer(w);
1232     return 200;
1233 }
1234
1235
1236 // ----------------------------------------------------------------------------
1237 // REGISTRY THIS MACHINE UNIQUE ID
1238
1239 static inline int is_machine_guid_blacklisted(const char *guid) {
1240     // these are machine GUIDs that have been included in distribution packages.
1241     // we blacklist them here, so that the next version of netdata will generate
1242     // new ones.
1243
1244     if(!strcmp(guid, "8a795b0c-2311-11e6-8563-000c295076a6")
1245     || !strcmp(guid, "4aed1458-1c3e-11e6-a53f-000c290fc8f5")
1246     ) {
1247         error("Blacklisted machine GUID '%s' found.", guid);
1248         return 1;
1249     }
1250
1251     return 0;
1252 }
1253
1254 char *registry_get_this_machine_guid(void) {
1255     if(likely(registry.machine_guid[0]))
1256         return registry.machine_guid;
1257
1258     // read it from disk
1259     int fd = open(registry.machine_guid_filename, O_RDONLY);
1260     if(fd != -1) {
1261         char buf[36 + 1];
1262         if(read(fd, buf, 36) != 36)
1263             error("Failed to read machine GUID from '%s'", registry.machine_guid_filename);
1264         else {
1265             buf[36] = '\0';
1266             if(registry_regenerate_guid(buf, registry.machine_guid) == -1) {
1267                 error("Failed to validate machine GUID '%s' from '%s'. Ignoring it - this might mean this netdata will appear as duplicate in the registry.",
1268                       buf, registry.machine_guid_filename);
1269
1270                 registry.machine_guid[0] = '\0';
1271             }
1272             else if(is_machine_guid_blacklisted(registry.machine_guid))
1273                 registry.machine_guid[0] = '\0';
1274         }
1275         close(fd);
1276     }
1277
1278     // generate a new one?
1279     if(!registry.machine_guid[0]) {
1280         uuid_t uuid;
1281
1282         uuid_generate_time(uuid);
1283         uuid_unparse_lower(uuid, registry.machine_guid);
1284         registry.machine_guid[36] = '\0';
1285
1286         // save it
1287         fd = open(registry.machine_guid_filename, O_WRONLY|O_CREAT|O_TRUNC, 444);
1288         if(fd == -1)
1289             fatal("Cannot create unique machine id file '%s'. Please fix this.", registry.machine_guid_filename);
1290
1291         if(write(fd, registry.machine_guid, 36) != 36)
1292             fatal("Cannot write the unique machine id file '%s'. Please fix this.", registry.machine_guid_filename);
1293
1294         close(fd);
1295     }
1296
1297     setenv("NETDATA_REGISTRY_UNIQUE_ID", registry.machine_guid, 1);
1298
1299     return registry.machine_guid;
1300 }
1301
1302
1303 // ----------------------------------------------------------------------------
1304 // REGISTRY LOAD/SAVE
1305
1306 int registry_machine_save_url(void *entry, void *file) {
1307     MACHINE_URL *mu = entry;
1308     FILE *fp = file;
1309
1310     debug(D_REGISTRY, "Registry: registry_machine_save_url('%s')", mu->url->url);
1311
1312     int ret = fprintf(fp, "V\t%08x\t%08x\t%08x\t%02x\t%s\n",
1313             mu->first_t,
1314             mu->last_t,
1315             mu->usages,
1316             mu->flags,
1317             mu->url->url
1318     );
1319
1320     // error handling is done at registry_save()
1321
1322     return ret;
1323 }
1324
1325 int registry_machine_save(void *entry, void *file) {
1326     MACHINE *m = entry;
1327     FILE *fp = file;
1328
1329     debug(D_REGISTRY, "Registry: registry_machine_save('%s')", m->guid);
1330
1331     int ret = fprintf(fp, "M\t%08x\t%08x\t%08x\t%s\n",
1332             m->first_t,
1333             m->last_t,
1334             m->usages,
1335             m->guid
1336     );
1337
1338     if(ret >= 0) {
1339         int ret2 = dictionary_get_all(m->urls, registry_machine_save_url, fp);
1340         if(ret2 < 0) return ret2;
1341         ret += ret2;
1342     }
1343
1344     // error handling is done at registry_save()
1345
1346     return ret;
1347 }
1348
1349 static inline int registry_person_save_url(void *entry, void *file) {
1350     PERSON_URL *pu = entry;
1351     FILE *fp = file;
1352
1353     debug(D_REGISTRY, "Registry: registry_person_save_url('%s')", pu->url->url);
1354
1355     int ret = fprintf(fp, "U\t%08x\t%08x\t%08x\t%02x\t%s\t%s\t%s\n",
1356             pu->first_t,
1357             pu->last_t,
1358             pu->usages,
1359             pu->flags,
1360             pu->machine->guid,
1361             pu->name,
1362             pu->url->url
1363     );
1364
1365     // error handling is done at registry_save()
1366
1367     return ret;
1368 }
1369
1370 static inline int registry_person_save(void *entry, void *file) {
1371     PERSON *p = entry;
1372     FILE *fp = file;
1373
1374     debug(D_REGISTRY, "Registry: registry_person_save('%s')", p->guid);
1375
1376     int ret = fprintf(fp, "P\t%08x\t%08x\t%08x\t%s\n",
1377             p->first_t,
1378             p->last_t,
1379             p->usages,
1380             p->guid
1381     );
1382
1383     if(ret >= 0) {
1384         int ret2 = dictionary_get_all(p->urls, registry_person_save_url, fp);
1385         if (ret2 < 0) return ret2;
1386         ret += ret2;
1387     }
1388
1389     // error handling is done at registry_save()
1390
1391     return ret;
1392 }
1393
1394 int registry_save(void) {
1395     if(!registry.enabled) return -1;
1396
1397     // make sure the log is not updated
1398     registry_log_lock();
1399
1400     if(unlikely(!registry_should_save_db())) {
1401         registry_log_unlock();
1402         return -2;
1403     }
1404
1405     error_log_limit_unlimited();
1406
1407     char tmp_filename[FILENAME_MAX + 1];
1408     char old_filename[FILENAME_MAX + 1];
1409
1410     snprintfz(old_filename, FILENAME_MAX, "%s.old", registry.db_filename);
1411     snprintfz(tmp_filename, FILENAME_MAX, "%s.tmp", registry.db_filename);
1412
1413     debug(D_REGISTRY, "Registry: Creating file '%s'", tmp_filename);
1414     FILE *fp = fopen(tmp_filename, "w");
1415     if(!fp) {
1416         error("Registry: Cannot create file: %s", tmp_filename);
1417         registry_log_unlock();
1418         error_log_limit_reset();
1419         return -1;
1420     }
1421
1422     // dictionary_get_all() has its own locking, so this is safe to do
1423
1424     debug(D_REGISTRY, "Saving all machines");
1425     int bytes1 = dictionary_get_all(registry.machines, registry_machine_save, fp);
1426     if(bytes1 < 0) {
1427         error("Registry: Cannot save registry machines - return value %d", bytes1);
1428         fclose(fp);
1429         registry_log_unlock();
1430         error_log_limit_reset();
1431         return bytes1;
1432     }
1433     debug(D_REGISTRY, "Registry: saving machines took %d bytes", bytes1);
1434
1435     debug(D_REGISTRY, "Saving all persons");
1436     int bytes2 = dictionary_get_all(registry.persons, registry_person_save, fp);
1437     if(bytes2 < 0) {
1438         error("Registry: Cannot save registry persons - return value %d", bytes2);
1439         fclose(fp);
1440         registry_log_unlock();
1441         error_log_limit_reset();
1442         return bytes2;
1443     }
1444     debug(D_REGISTRY, "Registry: saving persons took %d bytes", bytes2);
1445
1446     // save the totals
1447     fprintf(fp, "T\t%016llx\t%016llx\t%016llx\t%016llx\t%016llx\t%016llx\n",
1448             registry.persons_count,
1449             registry.machines_count,
1450             registry.usages_count + 1, // this is required - it is lost on db rotation
1451             registry.urls_count,
1452             registry.persons_urls_count,
1453             registry.machines_urls_count
1454     );
1455
1456     fclose(fp);
1457
1458     errno = 0;
1459
1460     // remove the .old db
1461     debug(D_REGISTRY, "Registry: Removing old db '%s'", old_filename);
1462     if(unlink(old_filename) == -1 && errno != ENOENT)
1463         error("Registry: cannot remove old registry file '%s'", old_filename);
1464
1465     // rename the db to .old
1466     debug(D_REGISTRY, "Registry: Link current db '%s' to .old: '%s'", registry.db_filename, old_filename);
1467     if(link(registry.db_filename, old_filename) == -1 && errno != ENOENT)
1468         error("Registry: cannot move file '%s' to '%s'. Saving registry DB failed!", tmp_filename, registry.db_filename);
1469
1470     else {
1471         // remove the database (it is saved in .old)
1472         debug(D_REGISTRY, "Registry: removing db '%s'", registry.db_filename);
1473         if (unlink(registry.db_filename) == -1 && errno != ENOENT)
1474             error("Registry: cannot remove old registry file '%s'", registry.db_filename);
1475
1476         // move the .tmp to make it active
1477         debug(D_REGISTRY, "Registry: linking tmp db '%s' to active db '%s'", tmp_filename, registry.db_filename);
1478         if (link(tmp_filename, registry.db_filename) == -1) {
1479             error("Registry: cannot move file '%s' to '%s'. Saving registry DB failed!", tmp_filename,
1480                   registry.db_filename);
1481
1482             // move the .old back
1483             debug(D_REGISTRY, "Registry: linking old db '%s' to active db '%s'", old_filename, registry.db_filename);
1484             if(link(old_filename, registry.db_filename) == -1)
1485                 error("Registry: cannot move file '%s' to '%s'. Recovering the old registry DB failed!", old_filename, registry.db_filename);
1486         }
1487         else {
1488             debug(D_REGISTRY, "Registry: removing tmp db '%s'", tmp_filename);
1489             if(unlink(tmp_filename) == -1)
1490                 error("Registry: cannot remove tmp registry file '%s'", tmp_filename);
1491
1492             // it has been moved successfully
1493             // discard the current registry log
1494             registry_log_recreate_nolock();
1495             registry.log_count = 0;
1496         }
1497     }
1498
1499     // continue operations
1500     registry_log_unlock();
1501     error_log_limit_reset();
1502
1503     return -1;
1504 }
1505
1506 static inline size_t registry_load(void) {
1507     char *s, buf[4096 + 1];
1508     PERSON *p = NULL;
1509     MACHINE *m = NULL;
1510     URL *u = NULL;
1511     size_t line = 0;
1512
1513     debug(D_REGISTRY, "Registry: loading active db from: '%s'", registry.db_filename);
1514     FILE *fp = fopen(registry.db_filename, "r");
1515     if(!fp) {
1516         error("Registry: cannot open registry file: '%s'", registry.db_filename);
1517         return 0;
1518     }
1519
1520     size_t len = 0;
1521     buf[4096] = '\0';
1522     while((s = fgets_trim_len(buf, 4096, fp, &len))) {
1523         line++;
1524
1525         debug(D_REGISTRY, "Registry: read line %zu to length %zu: %s", line, len, s);
1526         switch(*s) {
1527             case 'T': // totals
1528                 if(unlikely(len != 103 || s[1] != '\t' || s[18] != '\t' || s[35] != '\t' || s[52] != '\t' || s[69] != '\t' || s[86] != '\t' || s[103] != '\0')) {
1529                     error("Registry totals line %zu is wrong (len = %zu).", line, len);
1530                     continue;
1531                 }
1532                 registry.persons_count = strtoull(&s[2], NULL, 16);
1533                 registry.machines_count = strtoull(&s[19], NULL, 16);
1534                 registry.usages_count = strtoull(&s[36], NULL, 16);
1535                 registry.urls_count = strtoull(&s[53], NULL, 16);
1536                 registry.persons_urls_count = strtoull(&s[70], NULL, 16);
1537                 registry.machines_urls_count = strtoull(&s[87], NULL, 16);
1538                 break;
1539
1540             case 'P': // person
1541                 m = NULL;
1542                 // verify it is valid
1543                 if(unlikely(len != 65 || s[1] != '\t' || s[10] != '\t' || s[19] != '\t' || s[28] != '\t' || s[65] != '\0')) {
1544                     error("Registry person line %zu is wrong (len = %zu).", line, len);
1545                     continue;
1546                 }
1547
1548                 s[1] = s[10] = s[19] = s[28] = '\0';
1549                 p = registry_person_allocate(&s[29], strtoul(&s[2], NULL, 16));
1550                 p->last_t = strtoul(&s[11], NULL, 16);
1551                 p->usages = strtoul(&s[20], NULL, 16);
1552                 debug(D_REGISTRY, "Registry loaded person '%s', first: %u, last: %u, usages: %u", p->guid, p->first_t, p->last_t, p->usages);
1553                 break;
1554
1555             case 'M': // machine
1556                 p = NULL;
1557                 // verify it is valid
1558                 if(unlikely(len != 65 || s[1] != '\t' || s[10] != '\t' || s[19] != '\t' || s[28] != '\t' || s[65] != '\0')) {
1559                     error("Registry person line %zu is wrong (len = %zu).", line, len);
1560                     continue;
1561                 }
1562
1563                 s[1] = s[10] = s[19] = s[28] = '\0';
1564                 m = registry_machine_allocate(&s[29], strtoul(&s[2], NULL, 16));
1565                 m->last_t = strtoul(&s[11], NULL, 16);
1566                 m->usages = strtoul(&s[20], NULL, 16);
1567                 debug(D_REGISTRY, "Registry loaded machine '%s', first: %u, last: %u, usages: %u", m->guid, m->first_t, m->last_t, m->usages);
1568                 break;
1569
1570             case 'U': // person URL
1571                 if(unlikely(!p)) {
1572                     error("Registry: ignoring line %zu, no person loaded: %s", line, s);
1573                     continue;
1574                 }
1575
1576                 // verify it is valid
1577                 if(len < 69 || s[1] != '\t' || s[10] != '\t' || s[19] != '\t' || s[28] != '\t' || s[31] != '\t' || s[68] != '\t') {
1578                     error("Registry person URL line %zu is wrong (len = %zu).", line, len);
1579                     continue;
1580                 }
1581
1582                 s[1] = s[10] = s[19] = s[28] = s[31] = s[68] = '\0';
1583
1584                 // skip the name to find the url
1585                 char *url = &s[69];
1586                 while(*url && *url != '\t') url++;
1587                 if(!*url) {
1588                     error("Registry person URL line %zu does not have a url.", line);
1589                     continue;
1590                 }
1591                 *url++ = '\0';
1592
1593                 // u = registry_url_allocate_nolock(url, strlen(url));
1594                 u = registry_url_get_nolock(url, strlen(url));
1595
1596                 time_t first_t = strtoul(&s[2], NULL, 16);
1597
1598                 m = registry_machine_find(&s[32]);
1599                 if(!m) m = registry_machine_allocate(&s[32], first_t);
1600
1601                 PERSON_URL *pu = registry_person_url_allocate(p, m, u, &s[69], strlen(&s[69]), first_t);
1602                 pu->last_t = strtoul(&s[11], NULL, 16);
1603                 pu->usages = strtoul(&s[20], NULL, 16);
1604                 pu->flags = strtoul(&s[29], NULL, 16);
1605                 debug(D_REGISTRY, "Registry loaded person URL '%s' with name '%s' of machine '%s', first: %u, last: %u, usages: %u, flags: %02x", u->url, pu->name, m->guid, pu->first_t, pu->last_t, pu->usages, pu->flags);
1606                 break;
1607
1608             case 'V': // machine URL
1609                 if(unlikely(!m)) {
1610                     error("Registry: ignoring line %zu, no machine loaded: %s", line, s);
1611                     continue;
1612                 }
1613
1614                 // verify it is valid
1615                 if(len < 32 || s[1] != '\t' || s[10] != '\t' || s[19] != '\t' || s[28] != '\t' || s[31] != '\t') {
1616                     error("Registry person URL line %zu is wrong (len = %zu).", line, len);
1617                     continue;
1618                 }
1619
1620                 s[1] = s[10] = s[19] = s[28] = s[31] = '\0';
1621                 // u = registry_url_allocate_nolock(&s[32], strlen(&s[32]));
1622                 u = registry_url_get_nolock(&s[32], strlen(&s[32]));
1623
1624                 MACHINE_URL *mu = registry_machine_url_allocate(m, u, strtoul(&s[2], NULL, 16));
1625                 mu->last_t = strtoul(&s[11], NULL, 16);
1626                 mu->usages = strtoul(&s[20], NULL, 16);
1627                 mu->flags = strtoul(&s[29], NULL, 16);
1628                 debug(D_REGISTRY, "Registry loaded machine URL '%s', machine '%s', first: %u, last: %u, usages: %u, flags: %02x", u->url, m->guid, mu->first_t, mu->last_t, mu->usages, mu->flags);
1629                 break;
1630
1631             default:
1632                 error("Registry: ignoring line %zu of filename '%s': %s.", line, registry.db_filename, s);
1633                 break;
1634         }
1635     }
1636     fclose(fp);
1637
1638     return line;
1639 }
1640
1641 // ----------------------------------------------------------------------------
1642 // REGISTRY
1643
1644 int registry_init(void) {
1645     char filename[FILENAME_MAX + 1];
1646
1647     // registry enabled?
1648     registry.enabled = config_get_boolean("registry", "enabled", 0);
1649
1650     // pathnames
1651     registry.pathname = config_get("registry", "registry db directory", VARLIB_DIR "/registry");
1652     if(mkdir(registry.pathname, 0770) == -1 && errno != EEXIST)
1653         fatal("Cannot create directory '%s'.", registry.pathname);
1654
1655     // filenames
1656     snprintfz(filename, FILENAME_MAX, "%s/netdata.public.unique.id", registry.pathname);
1657     registry.machine_guid_filename = config_get("registry", "netdata unique id file", filename);
1658     registry_get_this_machine_guid();
1659
1660     snprintfz(filename, FILENAME_MAX, "%s/registry.db", registry.pathname);
1661     registry.db_filename = config_get("registry", "registry db file", filename);
1662
1663     snprintfz(filename, FILENAME_MAX, "%s/registry-log.db", registry.pathname);
1664     registry.log_filename = config_get("registry", "registry log file", filename);
1665
1666     // configuration options
1667     registry.save_registry_every_entries = config_get_number("registry", "registry save db every new entries", 1000000);
1668     registry.persons_expiration = config_get_number("registry", "registry expire idle persons days", 365) * 86400;
1669     registry.registry_domain = config_get("registry", "registry domain", "");
1670     registry.registry_to_announce = config_get("registry", "registry to announce", "https://registry.my-netdata.io");
1671     registry.hostname = config_get("registry", "registry hostname", config_get("global", "hostname", localhost.hostname));
1672     registry.verify_cookies_redirects = config_get_boolean("registry", "verify browser cookies support", 1);
1673
1674     setenv("NETDATA_REGISTRY_HOSTNAME", registry.hostname, 1);
1675     setenv("NETDATA_REGISTRY_URL", registry.registry_to_announce, 1);
1676
1677     registry.max_url_length = config_get_number("registry", "max URL length", 1024);
1678     if(registry.max_url_length < 10) {
1679         registry.max_url_length = 10;
1680         config_set_number("registry", "max URL length", registry.max_url_length);
1681     }
1682
1683     registry.max_name_length = config_get_number("registry", "max URL name length", 50);
1684     if(registry.max_name_length < 10) {
1685         registry.max_name_length = 10;
1686         config_set_number("registry", "max URL name length", registry.max_name_length);
1687     }
1688
1689     // initialize entries counters
1690     registry.persons_count = 0;
1691     registry.machines_count = 0;
1692     registry.usages_count = 0;
1693     registry.urls_count = 0;
1694     registry.persons_urls_count = 0;
1695     registry.machines_urls_count = 0;
1696
1697     // initialize memory counters
1698     registry.persons_memory = 0;
1699     registry.machines_memory = 0;
1700     registry.urls_memory = 0;
1701     registry.persons_urls_memory = 0;
1702     registry.machines_urls_memory = 0;
1703
1704     // initialize locks
1705     pthread_mutex_init(&registry.persons_lock, NULL);
1706     pthread_mutex_init(&registry.machines_lock, NULL);
1707     pthread_mutex_init(&registry.urls_lock, NULL);
1708     pthread_mutex_init(&registry.person_urls_lock, NULL);
1709     pthread_mutex_init(&registry.machine_urls_lock, NULL);
1710
1711     // create dictionaries
1712     registry.persons = dictionary_create(DICTIONARY_FLAGS);
1713     registry.machines = dictionary_create(DICTIONARY_FLAGS);
1714     registry.urls = dictionary_create(DICTIONARY_FLAGS);
1715
1716     // load the registry database
1717     if(registry.enabled) {
1718         registry_log_open_nolock();
1719         registry_load();
1720         registry_log_load();
1721
1722         if(unlikely(registry_should_save_db()))
1723             registry_save();
1724     }
1725
1726     return 0;
1727 }
1728
1729 void registry_free(void) {
1730     if(!registry.enabled) return;
1731
1732     // we need to destroy the dictionaries ourselves
1733     // since the dictionaries use memory we allocated
1734
1735     while(registry.persons->values_index.root) {
1736         PERSON *p = ((NAME_VALUE *)registry.persons->values_index.root)->value;
1737
1738         // fprintf(stderr, "\nPERSON: '%s', first: %u, last: %u, usages: %u\n", p->guid, p->first_t, p->last_t, p->usages);
1739
1740         while(p->urls->values_index.root) {
1741             PERSON_URL *pu = ((NAME_VALUE *)p->urls->values_index.root)->value;
1742
1743             // fprintf(stderr, "\tURL: '%s', first: %u, last: %u, usages: %u, flags: 0x%02x\n", pu->url->url, pu->first_t, pu->last_t, pu->usages, pu->flags);
1744
1745             debug(D_REGISTRY, "Registry: deleting url '%s' from person '%s'", pu->url->url, p->guid);
1746             dictionary_del(p->urls, pu->url->url);
1747
1748             debug(D_REGISTRY, "Registry: unlinking url '%s' from person", pu->url->url);
1749             registry_url_unlink_nolock(pu->url);
1750
1751             debug(D_REGISTRY, "Registry: freeing person url");
1752             freez(pu);
1753         }
1754
1755         debug(D_REGISTRY, "Registry: deleting person '%s' from persons registry", p->guid);
1756         dictionary_del(registry.persons, p->guid);
1757
1758         debug(D_REGISTRY, "Registry: destroying URL dictionary of person '%s'", p->guid);
1759         dictionary_destroy(p->urls);
1760
1761         debug(D_REGISTRY, "Registry: freeing person '%s'", p->guid);
1762         freez(p);
1763     }
1764
1765     while(registry.machines->values_index.root) {
1766         MACHINE *m = ((NAME_VALUE *)registry.machines->values_index.root)->value;
1767
1768         // fprintf(stderr, "\nMACHINE: '%s', first: %u, last: %u, usages: %u\n", m->guid, m->first_t, m->last_t, m->usages);
1769
1770         while(m->urls->values_index.root) {
1771             MACHINE_URL *mu = ((NAME_VALUE *)m->urls->values_index.root)->value;
1772
1773             // fprintf(stderr, "\tURL: '%s', first: %u, last: %u, usages: %u, flags: 0x%02x\n", mu->url->url, mu->first_t, mu->last_t, mu->usages, mu->flags);
1774
1775             //debug(D_REGISTRY, "Registry: destroying persons dictionary from url '%s'", mu->url->url);
1776             //dictionary_destroy(mu->persons);
1777
1778             debug(D_REGISTRY, "Registry: deleting url '%s' from person '%s'", mu->url->url, m->guid);
1779             dictionary_del(m->urls, mu->url->url);
1780
1781             debug(D_REGISTRY, "Registry: unlinking url '%s' from machine", mu->url->url);
1782             registry_url_unlink_nolock(mu->url);
1783
1784             debug(D_REGISTRY, "Registry: freeing machine url");
1785             freez(mu);
1786         }
1787
1788         debug(D_REGISTRY, "Registry: deleting machine '%s' from machines registry", m->guid);
1789         dictionary_del(registry.machines, m->guid);
1790
1791         debug(D_REGISTRY, "Registry: destroying URL dictionary of machine '%s'", m->guid);
1792         dictionary_destroy(m->urls);
1793
1794         debug(D_REGISTRY, "Registry: freeing machine '%s'", m->guid);
1795         freez(m);
1796     }
1797
1798     // and free the memory of remaining dictionary structures
1799
1800     debug(D_REGISTRY, "Registry: destroying persons dictionary");
1801     dictionary_destroy(registry.persons);
1802
1803     debug(D_REGISTRY, "Registry: destroying machines dictionary");
1804     dictionary_destroy(registry.machines);
1805
1806     debug(D_REGISTRY, "Registry: destroying urls dictionary");
1807     dictionary_destroy(registry.urls);
1808 }
1809
1810 // ----------------------------------------------------------------------------
1811 // STATISTICS
1812
1813 void registry_statistics(void) {
1814     if(!registry.enabled) return;
1815
1816     static RRDSET *sts = NULL, *stc = NULL, *stm = NULL;
1817
1818     if(!sts) sts = rrdset_find("netdata.registry_sessions");
1819     if(!sts) {
1820         sts = rrdset_create("netdata", "registry_sessions", NULL, "registry", NULL, "NetData Registry Sessions", "session", 131000, rrd_update_every, RRDSET_TYPE_LINE);
1821
1822         rrddim_add(sts, "sessions",  NULL,  1, 1, RRDDIM_ABSOLUTE);
1823     }
1824     else rrdset_next(sts);
1825
1826     rrddim_set(sts, "sessions", registry.usages_count);
1827     rrdset_done(sts);
1828
1829     // ------------------------------------------------------------------------
1830
1831     if(!stc) stc = rrdset_find("netdata.registry_entries");
1832     if(!stc) {
1833         stc = rrdset_create("netdata", "registry_entries", NULL, "registry", NULL, "NetData Registry Entries", "entries", 131100, rrd_update_every, RRDSET_TYPE_LINE);
1834
1835         rrddim_add(stc, "persons",        NULL,  1, 1, RRDDIM_ABSOLUTE);
1836         rrddim_add(stc, "machines",       NULL,  1, 1, RRDDIM_ABSOLUTE);
1837         rrddim_add(stc, "urls",           NULL,  1, 1, RRDDIM_ABSOLUTE);
1838         rrddim_add(stc, "persons_urls",   NULL,  1, 1, RRDDIM_ABSOLUTE);
1839         rrddim_add(stc, "machines_urls",  NULL,  1, 1, RRDDIM_ABSOLUTE);
1840     }
1841     else rrdset_next(stc);
1842
1843     rrddim_set(stc, "persons",       registry.persons_count);
1844     rrddim_set(stc, "machines",      registry.machines_count);
1845     rrddim_set(stc, "urls",          registry.urls_count);
1846     rrddim_set(stc, "persons_urls",  registry.persons_urls_count);
1847     rrddim_set(stc, "machines_urls", registry.machines_urls_count);
1848     rrdset_done(stc);
1849
1850     // ------------------------------------------------------------------------
1851
1852     if(!stm) stm = rrdset_find("netdata.registry_mem");
1853     if(!stm) {
1854         stm = rrdset_create("netdata", "registry_mem", NULL, "registry", NULL, "NetData Registry Memory", "KB", 131300, rrd_update_every, RRDSET_TYPE_STACKED);
1855
1856         rrddim_add(stm, "persons",        NULL,  1, 1024, RRDDIM_ABSOLUTE);
1857         rrddim_add(stm, "machines",       NULL,  1, 1024, RRDDIM_ABSOLUTE);
1858         rrddim_add(stm, "urls",           NULL,  1, 1024, RRDDIM_ABSOLUTE);
1859         rrddim_add(stm, "persons_urls",   NULL,  1, 1024, RRDDIM_ABSOLUTE);
1860         rrddim_add(stm, "machines_urls",  NULL,  1, 1024, RRDDIM_ABSOLUTE);
1861     }
1862     else rrdset_next(stm);
1863
1864     rrddim_set(stm, "persons",       registry.persons_memory + registry.persons_count * sizeof(NAME_VALUE) + sizeof(DICTIONARY));
1865     rrddim_set(stm, "machines",      registry.machines_memory + registry.machines_count * sizeof(NAME_VALUE) + sizeof(DICTIONARY));
1866     rrddim_set(stm, "urls",          registry.urls_memory + registry.urls_count * sizeof(NAME_VALUE) + sizeof(DICTIONARY));
1867     rrddim_set(stm, "persons_urls",  registry.persons_urls_memory + registry.persons_count * sizeof(DICTIONARY) + registry.persons_urls_count * sizeof(NAME_VALUE));
1868     rrddim_set(stm, "machines_urls", registry.machines_urls_memory + registry.machines_count * sizeof(DICTIONARY) + registry.machines_urls_count * sizeof(NAME_VALUE));
1869     rrdset_done(stm);
1870 }