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