master
c 198 lines 8.33 KB
Raw
1 // SPDX-License-Identifier: GPL-3.0-or-later
2
3 #include "api_v1_calls.h"
4
5 // Pings a netdata server:
6 // /api/v1/registry?action=hello
7 //
8 // Access to a netdata registry:
9 // /api/v1/registry?action=access&machine=${machine_guid}&name=${hostname}&url=${url}
10 //
11 // Delete from a netdata registry:
12 // /api/v1/registry?action=delete&machine=${machine_guid}&name=${hostname}&url=${url}&delete_url=${delete_url}
13 //
14 // Search for the URLs of a machine:
15 // /api/v1/registry?action=search&for=${machine_guid}
16 //
17 // Impersonate:
18 // /api/v1/registry?action=switch&machine=${machine_guid}&name=${hostname}&url=${url}&to=${new_person_guid}
19 int api_v1_registry(RRDHOST *host, struct web_client *w, char *url) {
20 static uint32_t hash_action = 0, hash_access = 0, hash_hello = 0, hash_delete = 0, hash_search = 0,
21 hash_switch = 0, hash_machine = 0, hash_url = 0, hash_name = 0, hash_delete_url = 0, hash_for = 0,
22 hash_to = 0 /*, hash_redirects = 0 */;
23
24 if(unlikely(!hash_action)) {
25 hash_action = simple_hash("action");
26 hash_access = simple_hash("access");
27 hash_hello = simple_hash("hello");
28 hash_delete = simple_hash("delete");
29 hash_search = simple_hash("search");
30 hash_switch = simple_hash("switch");
31 hash_machine = simple_hash("machine");
32 hash_url = simple_hash("url");
33 hash_name = simple_hash("name");
34 hash_delete_url = simple_hash("delete_url");
35 hash_for = simple_hash("for");
36 hash_to = simple_hash("to");
37 /*
38 hash_redirects = simple_hash("redirects");
39 */
40 }
41
42 netdata_log_debug(D_WEB_CLIENT, "%llu: API v1 registry with URL '%s'", w->id, url);
43
44 // TODO
45 // The browser may send multiple cookies with our id
46
47 char person_guid[UUID_STR_LEN] = "";
48 char *cookie = strstr(w->response.data->buffer, NETDATA_REGISTRY_COOKIE_NAME "=");
49 if(cookie)
50 strncpyz(person_guid, &cookie[sizeof(NETDATA_REGISTRY_COOKIE_NAME)], UUID_STR_LEN - 1);
51 else if(!extract_bearer_token_from_request(w, person_guid, sizeof(person_guid)))
52 person_guid[0] = '\0';
53
54 char action = '\0';
55 char *machine_guid = NULL,
56 *machine_url = NULL,
57 *url_name = NULL,
58 *search_machine_guid = NULL,
59 *delete_url = NULL,
60 *to_person_guid = NULL;
61 /*
62 int redirects = 0;
63 */
64
65 // Don't cache registry responses
66 buffer_no_cacheable(w->response.data);
67
68 while(url) {
69 char *value = strsep_skip_consecutive_separators(&url, "&");
70 if (!value || !*value) continue;
71
72 char *name = strsep_skip_consecutive_separators(&value, "=");
73 if (!name || !*name) continue;
74 if (!value || !*value) continue;
75
76 netdata_log_debug(D_WEB_CLIENT, "%llu: API v1 registry query param '%s' with value '%s'", w->id, name, value);
77
78 uint32_t hash = simple_hash(name);
79
80 if(hash == hash_action && !strcmp(name, "action")) {
81 uint32_t vhash = simple_hash(value);
82
83 if(vhash == hash_access && !strcmp(value, "access")) action = 'A';
84 else if(vhash == hash_hello && !strcmp(value, "hello")) action = 'H';
85 else if(vhash == hash_delete && !strcmp(value, "delete")) action = 'D';
86 else if(vhash == hash_search && !strcmp(value, "search")) action = 'S';
87 else if(vhash == hash_switch && !strcmp(value, "switch")) action = 'W';
88 #ifdef NETDATA_INTERNAL_CHECKS
89 else netdata_log_error("unknown registry action '%s'", value);
90 #endif /* NETDATA_INTERNAL_CHECKS */
91 }
92 /*
93 else if(hash == hash_redirects && !strcmp(name, "redirects"))
94 redirects = atoi(value);
95 */
96 else if(hash == hash_machine && !strcmp(name, "machine"))
97 machine_guid = value;
98
99 else if(hash == hash_url && !strcmp(name, "url"))
100 machine_url = value;
101
102 else if(action == 'A') {
103 if(hash == hash_name && !strcmp(name, "name"))
104 url_name = value;
105 }
106 else if(action == 'D') {
107 if(hash == hash_delete_url && !strcmp(name, "delete_url"))
108 delete_url = value;
109 }
110 else if(action == 'S') {
111 if(hash == hash_for && !strcmp(name, "for"))
112 search_machine_guid = value;
113 }
114 else if(action == 'W') {
115 if(hash == hash_to && !strcmp(name, "to"))
116 to_person_guid = value;
117 }
118 #ifdef NETDATA_INTERNAL_CHECKS
119 else netdata_log_error("unused registry URL parameter '%s' with value '%s'", name, value);
120 #endif /* NETDATA_INTERNAL_CHECKS */
121 }
122
123 bool do_not_track = respect_web_browser_do_not_track_policy && web_client_has_donottrack(w);
124
125 if(unlikely(action == 'H')) {
126 // HELLO request, dashboard ACL
127 analytics_log_dashboard();
128 if(unlikely(!http_can_access_dashboard(w)))
129 return web_client_permission_denied_acl(w);
130 }
131 else {
132 // everything else, registry ACL
133 if(unlikely(!http_can_access_registry(w)))
134 return web_client_permission_denied_acl(w);
135
136 if(unlikely(do_not_track)) {
137 buffer_flush(w->response.data);
138 buffer_sprintf(w->response.data, "Your web browser is sending 'DNT: 1' (Do Not Track). The registry requires persistent cookies on your browser to work.");
139 return HTTP_RESP_BAD_REQUEST;
140 }
141 }
142
143 buffer_no_cacheable(w->response.data);
144
145 switch(action) {
146 case 'A':
147 if(unlikely(!machine_guid || !machine_url || !url_name)) {
148 netdata_log_error("Invalid registry request - access requires these parameters: machine ('%s'), url ('%s'), name ('%s')", machine_guid ? machine_guid : "UNSET", machine_url ? machine_url : "UNSET", url_name ? url_name : "UNSET");
149 buffer_flush(w->response.data);
150 buffer_strcat(w->response.data, "Invalid registry Access request.");
151 return HTTP_RESP_BAD_REQUEST;
152 }
153
154 web_client_enable_tracking_required(w);
155 return registry_request_access_json(host, w, person_guid, machine_guid, machine_url, url_name, now_realtime_sec());
156
157 case 'D':
158 if(unlikely(!machine_guid || !machine_url || !delete_url)) {
159 netdata_log_error("Invalid registry request - delete requires these parameters: machine ('%s'), url ('%s'), delete_url ('%s')", machine_guid?machine_guid:"UNSET", machine_url?machine_url:"UNSET", delete_url?delete_url:"UNSET");
160 buffer_flush(w->response.data);
161 buffer_strcat(w->response.data, "Invalid registry Delete request.");
162 return HTTP_RESP_BAD_REQUEST;
163 }
164
165 web_client_enable_tracking_required(w);
166 return registry_request_delete_json(host, w, person_guid, machine_guid, machine_url, delete_url, now_realtime_sec());
167
168 case 'S':
169 if(unlikely(!search_machine_guid)) {
170 netdata_log_error("Invalid registry request - search requires these parameters: for ('%s')", search_machine_guid?search_machine_guid:"UNSET");
171 buffer_flush(w->response.data);
172 buffer_strcat(w->response.data, "Invalid registry Search request.");
173 return HTTP_RESP_BAD_REQUEST;
174 }
175
176 web_client_enable_tracking_required(w);
177 return registry_request_search_json(host, w, person_guid, search_machine_guid);
178
179 case 'W':
180 if(unlikely(!machine_guid || !machine_url || !to_person_guid)) {
181 netdata_log_error("Invalid registry request - switching identity requires these parameters: machine ('%s'), url ('%s'), to ('%s')", machine_guid?machine_guid:"UNSET", machine_url?machine_url:"UNSET", to_person_guid?to_person_guid:"UNSET");
182 buffer_flush(w->response.data);
183 buffer_strcat(w->response.data, "Invalid registry Switch request.");
184 return HTTP_RESP_BAD_REQUEST;
185 }
186
187 web_client_enable_tracking_required(w);
188 return registry_request_switch_json(host, w, person_guid, machine_guid, machine_url, to_person_guid, now_realtime_sec());
189
190 case 'H':
191 return registry_request_hello_json(host, w, do_not_track);
192
193 default:
194 buffer_flush(w->response.data);
195 buffer_strcat(w->response.data, "Invalid registry request - you need to set an action: hello, access, delete, search");
196 return HTTP_RESP_BAD_REQUEST;
197 }
198 }