3 #include "registry_internals.h"
5 #define REGISTRY_STATUS_OK "ok"
6 #define REGISTRY_STATUS_FAILED "failed"
7 #define REGISTRY_STATUS_DISABLED "disabled"
9 // ----------------------------------------------------------------------------
10 // REGISTRY concurrency locking
12 static inline void registry_lock(void) {
13 pthread_mutex_lock(®istry.lock);
16 static inline void registry_unlock(void) {
17 pthread_mutex_unlock(®istry.lock);
21 // ----------------------------------------------------------------------------
24 static void registry_set_cookie(struct web_client *w, const char *guid) {
26 time_t et = now_realtime_sec() + registry.persons_expiration;
27 struct tm etmbuf, *etm = gmtime_r(&et, &etmbuf);
28 strftime(edate, sizeof(edate), "%a, %d %b %Y %H:%M:%S %Z", etm);
30 snprintfz(w->cookie1, COOKIE_MAX, NETDATA_REGISTRY_COOKIE_NAME "=%s; Expires=%s", guid, edate);
32 if(registry.registry_domain && registry.registry_domain[0])
33 snprintfz(w->cookie2, COOKIE_MAX, NETDATA_REGISTRY_COOKIE_NAME "=%s; Domain=%s; Expires=%s", guid, registry.registry_domain, edate);
36 static inline void registry_set_person_cookie(struct web_client *w, REGISTRY_PERSON *p) {
37 registry_set_cookie(w, p->guid);
41 // ----------------------------------------------------------------------------
44 static inline void registry_json_header(struct web_client *w, const char *action, const char *status) {
45 buffer_flush(w->response.data);
46 w->response.data->contenttype = CT_APPLICATION_JSON;
47 buffer_sprintf(w->response.data, "{\n\t\"action\": \"%s\",\n\t\"status\": \"%s\",\n\t\"hostname\": \"%s\",\n\t\"machine_guid\": \"%s\"",
48 action, status, registry.hostname, registry.machine_guid);
51 static inline void registry_json_footer(struct web_client *w) {
52 buffer_strcat(w->response.data, "\n}\n");
55 static inline int registry_json_disabled(struct web_client *w, const char *action) {
56 registry_json_header(w, action, REGISTRY_STATUS_DISABLED);
58 buffer_sprintf(w->response.data, ",\n\t\"registry\": \"%s\"",
59 registry.registry_to_announce);
61 registry_json_footer(w);
66 // ----------------------------------------------------------------------------
67 // CALLBACKS FOR WALKING THROUGH REGISTRY OBJECTS
69 // structure used be the callbacks below
70 struct registry_json_walk_person_urls_callback {
77 // callback for rendering PERSON_URLs
78 static int registry_json_person_url_callback(void *entry, void *data) {
79 REGISTRY_PERSON_URL *pu = (REGISTRY_PERSON_URL *)entry;
80 struct registry_json_walk_person_urls_callback *c = (struct registry_json_walk_person_urls_callback *)data;
81 struct web_client *w = c->w;
83 if(unlikely(c->count++))
84 buffer_strcat(w->response.data, ",");
86 buffer_sprintf(w->response.data, "\n\t\t[ \"%s\", \"%s\", %u000, %u, \"%s\" ]",
87 pu->machine->guid, pu->url->url, pu->last_t, pu->usages, pu->machine_name);
92 // callback for rendering MACHINE_URLs
93 static int registry_json_machine_url_callback(void *entry, void *data) {
94 REGISTRY_MACHINE_URL *mu = (REGISTRY_MACHINE_URL *)entry;
95 struct registry_json_walk_person_urls_callback *c = (struct registry_json_walk_person_urls_callback *)data;
96 struct web_client *w = c->w;
97 REGISTRY_MACHINE *m = c->m;
99 if(unlikely(c->count++))
100 buffer_strcat(w->response.data, ",");
102 buffer_sprintf(w->response.data, "\n\t\t[ \"%s\", \"%s\", %u000, %u ]",
103 m->guid, mu->url->url, mu->last_t, mu->usages);
108 // ----------------------------------------------------------------------------
110 // structure used be the callbacks below
111 struct registry_person_url_callback_verify_machine_exists_data {
116 static inline int registry_person_url_callback_verify_machine_exists(void *entry, void *data) {
117 struct registry_person_url_callback_verify_machine_exists_data *d = (struct registry_person_url_callback_verify_machine_exists_data *)data;
118 REGISTRY_PERSON_URL *pu = (REGISTRY_PERSON_URL *)entry;
119 REGISTRY_MACHINE *m = d->m;
127 // ----------------------------------------------------------------------------
128 // public HELLO request
130 int registry_request_hello_json(struct web_client *w) {
131 registry_json_header(w, "hello", REGISTRY_STATUS_OK);
133 buffer_sprintf(w->response.data, ",\n\t\"registry\": \"%s\"",
134 registry.registry_to_announce);
136 registry_json_footer(w);
140 // ----------------------------------------------------------------------------
141 //public ACCESS request
143 #define REGISTRY_VERIFY_COOKIES_GUID "give-me-back-this-cookie-now--please"
145 // the main method for registering an access
146 int registry_request_access_json(struct web_client *w, char *person_guid, char *machine_guid, char *url, char *name, time_t when) {
147 if(unlikely(!registry.enabled))
148 return registry_json_disabled(w, "access");
150 // ------------------------------------------------------------------------
151 // verify the browser supports cookies
153 if(registry.verify_cookies_redirects > 0 && !person_guid[0]) {
154 buffer_flush(w->response.data);
155 registry_set_cookie(w, REGISTRY_VERIFY_COOKIES_GUID);
156 w->response.data->contenttype = CT_APPLICATION_JSON;
157 buffer_sprintf(w->response.data, "{ \"status\": \"redirect\", \"registry\": \"%s\" }", registry.registry_to_announce);
161 if(unlikely(person_guid[0] && !strsame(person_guid, REGISTRY_VERIFY_COOKIES_GUID)))
162 person_guid[0] = '\0';
164 // ------------------------------------------------------------------------
168 REGISTRY_PERSON *p = registry_request_access(person_guid, machine_guid, url, name, when);
170 registry_json_header(w, "access", REGISTRY_STATUS_FAILED);
171 registry_json_footer(w);
177 registry_set_person_cookie(w, p);
179 // generate the response
180 registry_json_header(w, "access", REGISTRY_STATUS_OK);
182 buffer_sprintf(w->response.data, ",\n\t\"person_guid\": \"%s\",\n\t\"urls\": [", p->guid);
183 struct registry_json_walk_person_urls_callback c = { p, NULL, w, 0 };
184 avl_traverse(&p->person_urls, registry_json_person_url_callback, &c);
185 buffer_strcat(w->response.data, "\n\t]\n");
187 registry_json_footer(w);
192 // ----------------------------------------------------------------------------
193 // public DELETE request
195 // the main method for deleting a URL from a person
196 int registry_request_delete_json(struct web_client *w, char *person_guid, char *machine_guid, char *url, char *delete_url, time_t when) {
197 if(!registry.enabled)
198 return registry_json_disabled(w, "delete");
202 REGISTRY_PERSON *p = registry_request_delete(person_guid, machine_guid, url, delete_url, when);
204 registry_json_header(w, "delete", REGISTRY_STATUS_FAILED);
205 registry_json_footer(w);
210 // generate the response
211 registry_json_header(w, "delete", REGISTRY_STATUS_OK);
212 registry_json_footer(w);
217 // ----------------------------------------------------------------------------
218 // public SEARCH request
220 // the main method for searching the URLs of a netdata
221 int registry_request_search_json(struct web_client *w, char *person_guid, char *machine_guid, char *url, char *request_machine, time_t when) {
222 if(!registry.enabled)
223 return registry_json_disabled(w, "search");
227 REGISTRY_MACHINE *m = registry_request_machine(person_guid, machine_guid, url, request_machine, when);
229 registry_json_header(w, "search", REGISTRY_STATUS_FAILED);
230 registry_json_footer(w);
235 registry_json_header(w, "search", REGISTRY_STATUS_OK);
237 buffer_strcat(w->response.data, ",\n\t\"urls\": [");
238 struct registry_json_walk_person_urls_callback c = { NULL, m, w, 0 };
239 dictionary_get_all(m->machine_urls, registry_json_machine_url_callback, &c);
240 buffer_strcat(w->response.data, "\n\t]\n");
242 registry_json_footer(w);
247 // ----------------------------------------------------------------------------
250 // the main method for switching user identity
251 int registry_request_switch_json(struct web_client *w, char *person_guid, char *machine_guid, char *url, char *new_person_guid, time_t when) {
252 if(!registry.enabled)
253 return registry_json_disabled(w, "switch");
260 REGISTRY_PERSON *op = registry_person_find(person_guid);
262 registry_json_header(w, "switch", REGISTRY_STATUS_FAILED);
263 registry_json_footer(w);
268 REGISTRY_PERSON *np = registry_person_find(new_person_guid);
270 registry_json_header(w, "switch", REGISTRY_STATUS_FAILED);
271 registry_json_footer(w);
276 REGISTRY_MACHINE *m = registry_machine_find(machine_guid);
278 registry_json_header(w, "switch", REGISTRY_STATUS_FAILED);
279 registry_json_footer(w);
284 struct registry_person_url_callback_verify_machine_exists_data data = { m, 0 };
286 // verify the old person has access to this machine
287 avl_traverse(&op->person_urls, registry_person_url_callback_verify_machine_exists, &data);
289 registry_json_header(w, "switch", REGISTRY_STATUS_FAILED);
290 registry_json_footer(w);
295 // verify the new person has access to this machine
297 avl_traverse(&np->person_urls, registry_person_url_callback_verify_machine_exists, &data);
299 registry_json_header(w, "switch", REGISTRY_STATUS_FAILED);
300 registry_json_footer(w);
305 // set the cookie of the new person
306 // the user just switched identity
307 registry_set_person_cookie(w, np);
309 // generate the response
310 registry_json_header(w, "switch", REGISTRY_STATUS_OK);
311 buffer_sprintf(w->response.data, ",\n\t\"person_guid\": \"%s\"", np->guid);
312 registry_json_footer(w);
318 // ----------------------------------------------------------------------------
321 void registry_statistics(void) {
322 if(!registry.enabled) return;
324 static RRDSET *sts = NULL, *stc = NULL, *stm = NULL;
326 if(!sts) sts = rrdset_find("netdata.registry_sessions");
328 sts = rrdset_create("netdata", "registry_sessions", NULL, "registry", NULL, "NetData Registry Sessions", "session", 131000, rrd_update_every, RRDSET_TYPE_LINE);
330 rrddim_add(sts, "sessions", NULL, 1, 1, RRDDIM_ABSOLUTE);
332 else rrdset_next(sts);
334 rrddim_set(sts, "sessions", registry.usages_count);
337 // ------------------------------------------------------------------------
339 if(!stc) stc = rrdset_find("netdata.registry_entries");
341 stc = rrdset_create("netdata", "registry_entries", NULL, "registry", NULL, "NetData Registry Entries", "entries", 131100, rrd_update_every, RRDSET_TYPE_LINE);
343 rrddim_add(stc, "persons", NULL, 1, 1, RRDDIM_ABSOLUTE);
344 rrddim_add(stc, "machines", NULL, 1, 1, RRDDIM_ABSOLUTE);
345 rrddim_add(stc, "urls", NULL, 1, 1, RRDDIM_ABSOLUTE);
346 rrddim_add(stc, "persons_urls", NULL, 1, 1, RRDDIM_ABSOLUTE);
347 rrddim_add(stc, "machines_urls", NULL, 1, 1, RRDDIM_ABSOLUTE);
349 else rrdset_next(stc);
351 rrddim_set(stc, "persons", registry.persons_count);
352 rrddim_set(stc, "machines", registry.machines_count);
353 rrddim_set(stc, "urls", registry.urls_count);
354 rrddim_set(stc, "persons_urls", registry.persons_urls_count);
355 rrddim_set(stc, "machines_urls", registry.machines_urls_count);
358 // ------------------------------------------------------------------------
360 if(!stm) stm = rrdset_find("netdata.registry_mem");
362 stm = rrdset_create("netdata", "registry_mem", NULL, "registry", NULL, "NetData Registry Memory", "KB", 131300, rrd_update_every, RRDSET_TYPE_STACKED);
364 rrddim_add(stm, "persons", NULL, 1, 1024, RRDDIM_ABSOLUTE);
365 rrddim_add(stm, "machines", NULL, 1, 1024, RRDDIM_ABSOLUTE);
366 rrddim_add(stm, "urls", NULL, 1, 1024, RRDDIM_ABSOLUTE);
367 rrddim_add(stm, "persons_urls", NULL, 1, 1024, RRDDIM_ABSOLUTE);
368 rrddim_add(stm, "machines_urls", NULL, 1, 1024, RRDDIM_ABSOLUTE);
370 else rrdset_next(stm);
372 rrddim_set(stm, "persons", registry.persons_memory + registry.persons_count * sizeof(NAME_VALUE) + sizeof(DICTIONARY));
373 rrddim_set(stm, "machines", registry.machines_memory + registry.machines_count * sizeof(NAME_VALUE) + sizeof(DICTIONARY));
374 rrddim_set(stm, "urls", registry.urls_memory);
375 rrddim_set(stm, "persons_urls", registry.persons_urls_memory);
376 rrddim_set(stm, "machines_urls", registry.machines_urls_memory + registry.machines_count * sizeof(DICTIONARY) + registry.machines_urls_count * sizeof(NAME_VALUE));