]> arthur.barton.de Git - netdata.git/blob - src/registry_internals.c
Merge pull request #1998 from ktsaou/master
[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 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("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     size_t l = 0;
63     char *s = registry_fix_machine_name(url, &l);
64
65     // protection from too big URLs
66     if(l > registry.max_url_length) {
67         l = registry.max_url_length;
68         s[l] = '\0';
69     }
70
71     if(len) *len = l;
72     return s;
73 }
74
75
76 // ----------------------------------------------------------------------------
77 // forward definition of functions
78
79 extern REGISTRY_PERSON *registry_request_access(char *person_guid, char *machine_guid, char *url, char *name, time_t when);
80 extern REGISTRY_PERSON *registry_request_delete(char *person_guid, char *machine_guid, char *url, char *delete_url, time_t when);
81
82
83 // ----------------------------------------------------------------------------
84 // HELPERS
85
86 // verify the person, the machine and the URL exist in our DB
87 REGISTRY_PERSON_URL *registry_verify_request(char *person_guid, char *machine_guid, char *url, REGISTRY_PERSON **pp, REGISTRY_MACHINE **mm) {
88     char pbuf[GUID_LEN + 1], mbuf[GUID_LEN + 1];
89
90     if(!person_guid || !*person_guid || !machine_guid || !*machine_guid || !url || !*url) {
91         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");
92         return NULL;
93     }
94
95     // normalize the url
96     url = registry_fix_url(url, NULL);
97
98     // make sure the person GUID is valid
99     if(regenerate_guid(person_guid, pbuf) == -1) {
100         info("Registry Request Verification: invalid person GUID, person: '%s', machine '%s', url '%s'", person_guid, machine_guid, url);
101         return NULL;
102     }
103     person_guid = pbuf;
104
105     // make sure the machine GUID is valid
106     if(regenerate_guid(machine_guid, mbuf) == -1) {
107         info("Registry Request Verification: invalid machine GUID, person: '%s', machine '%s', url '%s'", person_guid, machine_guid, url);
108         return NULL;
109     }
110     machine_guid = mbuf;
111
112     // make sure the machine exists
113     REGISTRY_MACHINE *m = registry_machine_find(machine_guid);
114     if(!m) {
115         info("Registry Request Verification: machine not found, person: '%s', machine '%s', url '%s'", person_guid, machine_guid, url);
116         return NULL;
117     }
118     if(mm) *mm = m;
119
120     // make sure the person exist
121     REGISTRY_PERSON *p = registry_person_find(person_guid);
122     if(!p) {
123         info("Registry Request Verification: person not found, person: '%s', machine '%s', url '%s'", person_guid, machine_guid, url);
124         return NULL;
125     }
126     if(pp) *pp = p;
127
128     REGISTRY_PERSON_URL *pu = registry_person_url_index_find(p, url);
129     if(!pu) {
130         info("Registry Request Verification: URL not found for person, person: '%s', machine '%s', url '%s'", person_guid, machine_guid, url);
131         return NULL;
132     }
133     return pu;
134 }
135
136
137 // ----------------------------------------------------------------------------
138 // REGISTRY REQUESTS
139
140 REGISTRY_PERSON *registry_request_access(char *person_guid, char *machine_guid, char *url, char *name, time_t when) {
141     debug(D_REGISTRY, "registry_request_access('%s', '%s', '%s'): NEW REQUEST", (person_guid)?person_guid:"", machine_guid, url);
142
143     REGISTRY_MACHINE *m = registry_machine_get(machine_guid, when);
144     if(!m) return NULL;
145
146     // make sure the name is valid
147     size_t namelen;
148     name = registry_fix_machine_name(name, &namelen);
149
150     size_t urllen;
151     url = registry_fix_url(url, &urllen);
152
153     REGISTRY_PERSON *p = registry_person_get(person_guid, when);
154
155     REGISTRY_URL *u = registry_url_get(url, urllen);
156     registry_person_link_to_url(p, m, u, name, namelen, when);
157     registry_machine_link_to_url(m, u, when);
158
159     registry_log('A', p, m, u, name);
160
161     registry.usages_count++;
162
163     return p;
164 }
165
166 REGISTRY_PERSON *registry_request_delete(char *person_guid, char *machine_guid, char *url, char *delete_url, time_t when) {
167     (void) when;
168
169     REGISTRY_PERSON *p = NULL;
170     REGISTRY_MACHINE *m = NULL;
171     REGISTRY_PERSON_URL *pu = registry_verify_request(person_guid, machine_guid, url, &p, &m);
172     if(!pu || !p || !m) return NULL;
173
174     // normalize the url
175     delete_url = registry_fix_url(delete_url, NULL);
176
177     // make sure the user is not deleting the url it uses
178     if(!strcmp(delete_url, pu->url->url)) {
179         info("Registry Delete Request: delete URL is the one currently accessed, person: '%s', machine '%s', url '%s', delete url '%s'"
180              , p->guid, m->guid, pu->url->url, delete_url);
181         return NULL;
182     }
183
184     REGISTRY_PERSON_URL *dpu = registry_person_url_index_find(p, delete_url);
185     if(!dpu) {
186         info("Registry Delete Request: URL not found for person: '%s', machine '%s', url '%s', delete url '%s'", p->guid
187              , m->guid, pu->url->url, delete_url);
188         return NULL;
189     }
190
191     registry_log('D', p, m, pu->url, dpu->url->url);
192     registry_person_unlink_from_url(p, dpu);
193
194     return p;
195 }
196
197
198 // a structure to pass to the dictionary_get_all() callback handler
199 struct machine_request_callback_data {
200     REGISTRY_MACHINE *find_this_machine;
201     REGISTRY_PERSON_URL *result;
202 };
203
204 // the callback function
205 // this will be run for every PERSON_URL of this PERSON
206 static int machine_request_callback(void *entry, void *data) {
207     REGISTRY_PERSON_URL *mypu = (REGISTRY_PERSON_URL *)entry;
208     struct machine_request_callback_data *myrdata = (struct machine_request_callback_data *)data;
209
210     if(mypu->machine == myrdata->find_this_machine) {
211         myrdata->result = mypu;
212         return -1; // this will also stop the walk through
213     }
214
215     return 0; // continue
216 }
217
218 REGISTRY_MACHINE *registry_request_machine(char *person_guid, char *machine_guid, char *url, char *request_machine, time_t when) {
219     (void)when;
220
221     char mbuf[GUID_LEN + 1];
222
223     REGISTRY_PERSON *p = NULL;
224     REGISTRY_MACHINE *m = NULL;
225     REGISTRY_PERSON_URL *pu = registry_verify_request(person_guid, machine_guid, url, &p, &m);
226     if(!pu || !p || !m) return NULL;
227
228     // make sure the machine GUID is valid
229     if(regenerate_guid(request_machine, mbuf) == -1) {
230         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);
231         return NULL;
232     }
233     request_machine = mbuf;
234
235     // make sure the machine exists
236     m = registry_machine_find(request_machine);
237     if(!m) {
238         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);
239         return NULL;
240     }
241
242     // Verify the user has in the past accessed this machine
243     // We will walk through the PERSON_URLs to find the machine
244     // linking to our machine
245
246     // a structure to pass to the dictionary_get_all() callback handler
247     struct machine_request_callback_data rdata = { m, NULL };
248
249     // request a walk through on the dictionary
250     avl_traverse(&p->person_urls, machine_request_callback, &rdata);
251
252     if(rdata.result)
253         return m;
254
255     return NULL;
256 }
257
258
259 // ----------------------------------------------------------------------------
260 // REGISTRY THIS MACHINE UNIQUE ID
261
262 static inline int is_machine_guid_blacklisted(const char *guid) {
263     // these are machine GUIDs that have been included in distribution packages.
264     // we blacklist them here, so that the next version of netdata will generate
265     // new ones.
266
267     if(!strcmp(guid, "8a795b0c-2311-11e6-8563-000c295076a6")
268        || !strcmp(guid, "4aed1458-1c3e-11e6-a53f-000c290fc8f5")
269             ) {
270         error("Blacklisted machine GUID '%s' found.", guid);
271         return 1;
272     }
273
274     return 0;
275 }
276
277 char *registry_get_this_machine_guid(void) {
278     static char guid[GUID_LEN + 1] = "";
279
280     if(likely(guid[0]))
281         return guid;
282
283     // read it from disk
284     int fd = open(registry.machine_guid_filename, O_RDONLY);
285     if(fd != -1) {
286         char buf[GUID_LEN + 1];
287         if(read(fd, buf, GUID_LEN) != GUID_LEN)
288             error("Failed to read machine GUID from '%s'", registry.machine_guid_filename);
289         else {
290             buf[GUID_LEN] = '\0';
291             if(regenerate_guid(buf, guid) == -1) {
292                 error("Failed to validate machine GUID '%s' from '%s'. Ignoring it - this might mean this netdata will appear as duplicate in the registry.",
293                         buf, registry.machine_guid_filename);
294
295                 guid[0] = '\0';
296             }
297             else if(is_machine_guid_blacklisted(guid))
298                 guid[0] = '\0';
299         }
300         close(fd);
301     }
302
303     // generate a new one?
304     if(!guid[0]) {
305         uuid_t uuid;
306
307         uuid_generate_time(uuid);
308         uuid_unparse_lower(uuid, guid);
309         guid[GUID_LEN] = '\0';
310
311         // save it
312         fd = open(registry.machine_guid_filename, O_WRONLY|O_CREAT|O_TRUNC, 444);
313         if(fd == -1)
314             fatal("Cannot create unique machine id file '%s'. Please fix this.", registry.machine_guid_filename);
315
316         if(write(fd, guid, GUID_LEN) != GUID_LEN)
317             fatal("Cannot write the unique machine id file '%s'. Please fix this.", registry.machine_guid_filename);
318
319         close(fd);
320     }
321
322     setenv("NETDATA_REGISTRY_UNIQUE_ID", guid, 1);
323
324     return guid;
325 }