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