web_api_v1.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #include "web_api_v1.h"
  3. static struct {
  4. const char *name;
  5. uint32_t hash;
  6. RRDR_OPTIONS value;
  7. } api_v1_data_options[] = {
  8. { "nonzero" , 0 , RRDR_OPTION_NONZERO}
  9. , {"flip" , 0 , RRDR_OPTION_REVERSED}
  10. , {"reversed" , 0 , RRDR_OPTION_REVERSED}
  11. , {"reverse" , 0 , RRDR_OPTION_REVERSED}
  12. , {"jsonwrap" , 0 , RRDR_OPTION_JSON_WRAP}
  13. , {"min2max" , 0 , RRDR_OPTION_MIN2MAX}
  14. , {"ms" , 0 , RRDR_OPTION_MILLISECONDS}
  15. , {"milliseconds" , 0 , RRDR_OPTION_MILLISECONDS}
  16. , {"abs" , 0 , RRDR_OPTION_ABSOLUTE}
  17. , {"absolute" , 0 , RRDR_OPTION_ABSOLUTE}
  18. , {"absolute_sum" , 0 , RRDR_OPTION_ABSOLUTE}
  19. , {"absolute-sum" , 0 , RRDR_OPTION_ABSOLUTE}
  20. , {"display_absolute", 0 , RRDR_OPTION_DISPLAY_ABS}
  21. , {"display-absolute", 0 , RRDR_OPTION_DISPLAY_ABS}
  22. , {"seconds" , 0 , RRDR_OPTION_SECONDS}
  23. , {"null2zero" , 0 , RRDR_OPTION_NULL2ZERO}
  24. , {"objectrows" , 0 , RRDR_OPTION_OBJECTSROWS}
  25. , {"google_json" , 0 , RRDR_OPTION_GOOGLE_JSON}
  26. , {"google-json" , 0 , RRDR_OPTION_GOOGLE_JSON}
  27. , {"percentage" , 0 , RRDR_OPTION_PERCENTAGE}
  28. , {"unaligned" , 0 , RRDR_OPTION_NOT_ALIGNED}
  29. , {"match_ids" , 0 , RRDR_OPTION_MATCH_IDS}
  30. , {"match-ids" , 0 , RRDR_OPTION_MATCH_IDS}
  31. , {"match_names" , 0 , RRDR_OPTION_MATCH_NAMES}
  32. , {"match-names" , 0 , RRDR_OPTION_MATCH_NAMES}
  33. , { NULL, 0, 0}
  34. };
  35. static struct {
  36. const char *name;
  37. uint32_t hash;
  38. uint32_t value;
  39. } api_v1_data_formats[] = {
  40. { DATASOURCE_FORMAT_DATATABLE_JSON , 0 , DATASOURCE_DATATABLE_JSON}
  41. , {DATASOURCE_FORMAT_DATATABLE_JSONP, 0 , DATASOURCE_DATATABLE_JSONP}
  42. , {DATASOURCE_FORMAT_JSON , 0 , DATASOURCE_JSON}
  43. , {DATASOURCE_FORMAT_JSONP , 0 , DATASOURCE_JSONP}
  44. , {DATASOURCE_FORMAT_SSV , 0 , DATASOURCE_SSV}
  45. , {DATASOURCE_FORMAT_CSV , 0 , DATASOURCE_CSV}
  46. , {DATASOURCE_FORMAT_TSV , 0 , DATASOURCE_TSV}
  47. , {"tsv-excel" , 0 , DATASOURCE_TSV}
  48. , {DATASOURCE_FORMAT_HTML , 0 , DATASOURCE_HTML}
  49. , {DATASOURCE_FORMAT_JS_ARRAY , 0 , DATASOURCE_JS_ARRAY}
  50. , {DATASOURCE_FORMAT_SSV_COMMA , 0 , DATASOURCE_SSV_COMMA}
  51. , {DATASOURCE_FORMAT_CSV_JSON_ARRAY , 0 , DATASOURCE_CSV_JSON_ARRAY}
  52. , {DATASOURCE_FORMAT_CSV_MARKDOWN , 0 , DATASOURCE_CSV_MARKDOWN}
  53. , { NULL, 0, 0}
  54. };
  55. static struct {
  56. const char *name;
  57. uint32_t hash;
  58. uint32_t value;
  59. } api_v1_data_google_formats[] = {
  60. // this is not error - when google requests json, it expects javascript
  61. // https://developers.google.com/chart/interactive/docs/dev/implementing_data_source#responseformat
  62. { "json" , 0 , DATASOURCE_DATATABLE_JSONP}
  63. , {"html" , 0 , DATASOURCE_HTML}
  64. , {"csv" , 0 , DATASOURCE_CSV}
  65. , {"tsv-excel", 0 , DATASOURCE_TSV}
  66. , { NULL, 0, 0}
  67. };
  68. void web_client_api_v1_init(void) {
  69. int i;
  70. for(i = 0; api_v1_data_options[i].name ; i++)
  71. api_v1_data_options[i].hash = simple_hash(api_v1_data_options[i].name);
  72. for(i = 0; api_v1_data_formats[i].name ; i++)
  73. api_v1_data_formats[i].hash = simple_hash(api_v1_data_formats[i].name);
  74. for(i = 0; api_v1_data_google_formats[i].name ; i++)
  75. api_v1_data_google_formats[i].hash = simple_hash(api_v1_data_google_formats[i].name);
  76. web_client_api_v1_init_grouping();
  77. uuid_t uuid;
  78. // generate
  79. uuid_generate(uuid);
  80. // unparse (to string)
  81. char uuid_str[37];
  82. uuid_unparse_lower(uuid, uuid_str);
  83. }
  84. char *get_mgmt_api_key(void) {
  85. char filename[FILENAME_MAX + 1];
  86. snprintfz(filename, FILENAME_MAX, "%s/netdata.api.key", netdata_configured_varlib_dir);
  87. char *api_key_filename=config_get(CONFIG_SECTION_REGISTRY, "netdata management api key file", filename);
  88. static char guid[GUID_LEN + 1] = "";
  89. if(likely(guid[0]))
  90. return guid;
  91. // read it from disk
  92. int fd = open(api_key_filename, O_RDONLY);
  93. if(fd != -1) {
  94. char buf[GUID_LEN + 1];
  95. if(read(fd, buf, GUID_LEN) != GUID_LEN)
  96. error("Failed to read management API key from '%s'", api_key_filename);
  97. else {
  98. buf[GUID_LEN] = '\0';
  99. if(regenerate_guid(buf, guid) == -1) {
  100. error("Failed to validate management API key '%s' from '%s'.",
  101. buf, api_key_filename);
  102. guid[0] = '\0';
  103. }
  104. }
  105. close(fd);
  106. }
  107. // generate a new one?
  108. if(!guid[0]) {
  109. uuid_t uuid;
  110. uuid_generate_time(uuid);
  111. uuid_unparse_lower(uuid, guid);
  112. guid[GUID_LEN] = '\0';
  113. // save it
  114. fd = open(api_key_filename, O_WRONLY|O_CREAT|O_TRUNC, 444);
  115. if(fd == -1)
  116. fatal("Cannot create unique management API key file '%s'. Please fix this.", api_key_filename);
  117. if(write(fd, guid, GUID_LEN) != GUID_LEN)
  118. fatal("Cannot write the unique management API key file '%s'. Please fix this.", api_key_filename);
  119. close(fd);
  120. }
  121. return guid;
  122. }
  123. void web_client_api_v1_management_init(void) {
  124. api_secret = get_mgmt_api_key();
  125. }
  126. inline uint32_t web_client_api_request_v1_data_options(char *o) {
  127. uint32_t ret = 0x00000000;
  128. char *tok;
  129. while(o && *o && (tok = mystrsep(&o, ", |"))) {
  130. if(!*tok) continue;
  131. uint32_t hash = simple_hash(tok);
  132. int i;
  133. for(i = 0; api_v1_data_options[i].name ; i++) {
  134. if (unlikely(hash == api_v1_data_options[i].hash && !strcmp(tok, api_v1_data_options[i].name))) {
  135. ret |= api_v1_data_options[i].value;
  136. break;
  137. }
  138. }
  139. }
  140. return ret;
  141. }
  142. inline uint32_t web_client_api_request_v1_data_format(char *name) {
  143. uint32_t hash = simple_hash(name);
  144. int i;
  145. for(i = 0; api_v1_data_formats[i].name ; i++) {
  146. if (unlikely(hash == api_v1_data_formats[i].hash && !strcmp(name, api_v1_data_formats[i].name))) {
  147. return api_v1_data_formats[i].value;
  148. }
  149. }
  150. return DATASOURCE_JSON;
  151. }
  152. inline uint32_t web_client_api_request_v1_data_google_format(char *name) {
  153. uint32_t hash = simple_hash(name);
  154. int i;
  155. for(i = 0; api_v1_data_google_formats[i].name ; i++) {
  156. if (unlikely(hash == api_v1_data_google_formats[i].hash && !strcmp(name, api_v1_data_google_formats[i].name))) {
  157. return api_v1_data_google_formats[i].value;
  158. }
  159. }
  160. return DATASOURCE_JSON;
  161. }
  162. inline int web_client_api_request_v1_alarms(RRDHOST *host, struct web_client *w, char *url) {
  163. int all = 0;
  164. while(url) {
  165. char *value = mystrsep(&url, "&");
  166. if (!value || !*value) continue;
  167. if(!strcmp(value, "all")) all = 1;
  168. else if(!strcmp(value, "active")) all = 0;
  169. }
  170. buffer_flush(w->response.data);
  171. w->response.data->contenttype = CT_APPLICATION_JSON;
  172. health_alarms2json(host, w->response.data, all);
  173. buffer_no_cacheable(w->response.data);
  174. return 200;
  175. }
  176. inline int web_client_api_request_v1_alarm_log(RRDHOST *host, struct web_client *w, char *url) {
  177. uint32_t after = 0;
  178. while(url) {
  179. char *value = mystrsep(&url, "&");
  180. if (!value || !*value) continue;
  181. char *name = mystrsep(&value, "=");
  182. if(!name || !*name) continue;
  183. if(!value || !*value) continue;
  184. if(!strcmp(name, "after")) after = (uint32_t)strtoul(value, NULL, 0);
  185. }
  186. buffer_flush(w->response.data);
  187. w->response.data->contenttype = CT_APPLICATION_JSON;
  188. health_alarm_log2json(host, w->response.data, after);
  189. return 200;
  190. }
  191. inline int web_client_api_request_single_chart(RRDHOST *host, struct web_client *w, char *url, void callback(RRDSET *st, BUFFER *buf)) {
  192. int ret = 400;
  193. char *chart = NULL;
  194. buffer_flush(w->response.data);
  195. while(url) {
  196. char *value = mystrsep(&url, "&");
  197. if(!value || !*value) continue;
  198. char *name = mystrsep(&value, "=");
  199. if(!name || !*name) continue;
  200. if(!value || !*value) continue;
  201. // name and value are now the parameters
  202. // they are not null and not empty
  203. if(!strcmp(name, "chart")) chart = value;
  204. //else {
  205. /// buffer_sprintf(w->response.data, "Unknown parameter '%s' in request.", name);
  206. // goto cleanup;
  207. //}
  208. }
  209. if(!chart || !*chart) {
  210. buffer_sprintf(w->response.data, "No chart id is given at the request.");
  211. goto cleanup;
  212. }
  213. RRDSET *st = rrdset_find(host, chart);
  214. if(!st) st = rrdset_find_byname(host, chart);
  215. if(!st) {
  216. buffer_strcat(w->response.data, "Chart is not found: ");
  217. buffer_strcat_htmlescape(w->response.data, chart);
  218. ret = 404;
  219. goto cleanup;
  220. }
  221. w->response.data->contenttype = CT_APPLICATION_JSON;
  222. st->last_accessed_time = now_realtime_sec();
  223. callback(st, w->response.data);
  224. return 200;
  225. cleanup:
  226. return ret;
  227. }
  228. inline int web_client_api_request_v1_alarm_variables(RRDHOST *host, struct web_client *w, char *url) {
  229. return web_client_api_request_single_chart(host, w, url, health_api_v1_chart_variables2json);
  230. }
  231. inline int web_client_api_request_v1_charts(RRDHOST *host, struct web_client *w, char *url) {
  232. (void)url;
  233. buffer_flush(w->response.data);
  234. w->response.data->contenttype = CT_APPLICATION_JSON;
  235. charts2json(host, w->response.data);
  236. return 200;
  237. }
  238. inline int web_client_api_request_v1_chart(RRDHOST *host, struct web_client *w, char *url) {
  239. return web_client_api_request_single_chart(host, w, url, rrd_stats_api_v1_chart);
  240. }
  241. void fix_google_param(char *s) {
  242. if(unlikely(!s)) return;
  243. for( ; *s ;s++) {
  244. if(!isalnum(*s) && *s != '.' && *s != '_' && *s != '-')
  245. *s = '_';
  246. }
  247. }
  248. // returns the HTTP code
  249. inline int web_client_api_request_v1_data(RRDHOST *host, struct web_client *w, char *url) {
  250. debug(D_WEB_CLIENT, "%llu: API v1 data with URL '%s'", w->id, url);
  251. int ret = 400;
  252. BUFFER *dimensions = NULL;
  253. buffer_flush(w->response.data);
  254. char *google_version = "0.6",
  255. *google_reqId = "0",
  256. *google_sig = "0",
  257. *google_out = "json",
  258. *responseHandler = NULL,
  259. *outFileName = NULL;
  260. time_t last_timestamp_in_data = 0, google_timestamp = 0;
  261. char *chart = NULL
  262. , *before_str = NULL
  263. , *after_str = NULL
  264. , *group_time_str = NULL
  265. , *points_str = NULL;
  266. int group = RRDR_GROUPING_AVERAGE;
  267. uint32_t format = DATASOURCE_JSON;
  268. uint32_t options = 0x00000000;
  269. while(url) {
  270. char *value = mystrsep(&url, "&");
  271. if(!value || !*value) continue;
  272. char *name = mystrsep(&value, "=");
  273. if(!name || !*name) continue;
  274. if(!value || !*value) continue;
  275. debug(D_WEB_CLIENT, "%llu: API v1 data query param '%s' with value '%s'", w->id, name, value);
  276. // name and value are now the parameters
  277. // they are not null and not empty
  278. if(!strcmp(name, "chart")) chart = value;
  279. else if(!strcmp(name, "dimension") || !strcmp(name, "dim") || !strcmp(name, "dimensions") || !strcmp(name, "dims")) {
  280. if(!dimensions) dimensions = buffer_create(100);
  281. buffer_strcat(dimensions, "|");
  282. buffer_strcat(dimensions, value);
  283. }
  284. else if(!strcmp(name, "after")) after_str = value;
  285. else if(!strcmp(name, "before")) before_str = value;
  286. else if(!strcmp(name, "points")) points_str = value;
  287. else if(!strcmp(name, "gtime")) group_time_str = value;
  288. else if(!strcmp(name, "group")) {
  289. group = web_client_api_request_v1_data_group(value, RRDR_GROUPING_AVERAGE);
  290. }
  291. else if(!strcmp(name, "format")) {
  292. format = web_client_api_request_v1_data_format(value);
  293. }
  294. else if(!strcmp(name, "options")) {
  295. options |= web_client_api_request_v1_data_options(value);
  296. }
  297. else if(!strcmp(name, "callback")) {
  298. responseHandler = value;
  299. }
  300. else if(!strcmp(name, "filename")) {
  301. outFileName = value;
  302. }
  303. else if(!strcmp(name, "tqx")) {
  304. // parse Google Visualization API options
  305. // https://developers.google.com/chart/interactive/docs/dev/implementing_data_source
  306. char *tqx_name, *tqx_value;
  307. while(value) {
  308. tqx_value = mystrsep(&value, ";");
  309. if(!tqx_value || !*tqx_value) continue;
  310. tqx_name = mystrsep(&tqx_value, ":");
  311. if(!tqx_name || !*tqx_name) continue;
  312. if(!tqx_value || !*tqx_value) continue;
  313. if(!strcmp(tqx_name, "version"))
  314. google_version = tqx_value;
  315. else if(!strcmp(tqx_name, "reqId"))
  316. google_reqId = tqx_value;
  317. else if(!strcmp(tqx_name, "sig")) {
  318. google_sig = tqx_value;
  319. google_timestamp = strtoul(google_sig, NULL, 0);
  320. }
  321. else if(!strcmp(tqx_name, "out")) {
  322. google_out = tqx_value;
  323. format = web_client_api_request_v1_data_google_format(google_out);
  324. }
  325. else if(!strcmp(tqx_name, "responseHandler"))
  326. responseHandler = tqx_value;
  327. else if(!strcmp(tqx_name, "outFileName"))
  328. outFileName = tqx_value;
  329. }
  330. }
  331. }
  332. // validate the google parameters given
  333. fix_google_param(google_out);
  334. fix_google_param(google_sig);
  335. fix_google_param(google_reqId);
  336. fix_google_param(google_version);
  337. fix_google_param(responseHandler);
  338. fix_google_param(outFileName);
  339. if(!chart || !*chart) {
  340. buffer_sprintf(w->response.data, "No chart id is given at the request.");
  341. goto cleanup;
  342. }
  343. RRDSET *st = rrdset_find(host, chart);
  344. if(!st) st = rrdset_find_byname(host, chart);
  345. if(!st) {
  346. buffer_strcat(w->response.data, "Chart is not found: ");
  347. buffer_strcat_htmlescape(w->response.data, chart);
  348. ret = 404;
  349. goto cleanup;
  350. }
  351. st->last_accessed_time = now_realtime_sec();
  352. long long before = (before_str && *before_str)?str2l(before_str):0;
  353. long long after = (after_str && *after_str) ?str2l(after_str):0;
  354. int points = (points_str && *points_str)?str2i(points_str):0;
  355. long group_time = (group_time_str && *group_time_str)?str2l(group_time_str):0;
  356. debug(D_WEB_CLIENT, "%llu: API command 'data' for chart '%s', dimensions '%s', after '%lld', before '%lld', points '%d', group '%d', format '%u', options '0x%08x'"
  357. , w->id
  358. , chart
  359. , (dimensions)?buffer_tostring(dimensions):""
  360. , after
  361. , before
  362. , points
  363. , group
  364. , format
  365. , options
  366. );
  367. if(outFileName && *outFileName) {
  368. buffer_sprintf(w->response.header, "Content-Disposition: attachment; filename=\"%s\"\r\n", outFileName);
  369. debug(D_WEB_CLIENT, "%llu: generating outfilename header: '%s'", w->id, outFileName);
  370. }
  371. if(format == DATASOURCE_DATATABLE_JSONP) {
  372. if(responseHandler == NULL)
  373. responseHandler = "google.visualization.Query.setResponse";
  374. debug(D_WEB_CLIENT_ACCESS, "%llu: GOOGLE JSON/JSONP: version = '%s', reqId = '%s', sig = '%s', out = '%s', responseHandler = '%s', outFileName = '%s'",
  375. w->id, google_version, google_reqId, google_sig, google_out, responseHandler, outFileName
  376. );
  377. buffer_sprintf(w->response.data,
  378. "%s({version:'%s',reqId:'%s',status:'ok',sig:'%ld',table:",
  379. responseHandler, google_version, google_reqId, st->last_updated.tv_sec);
  380. }
  381. else if(format == DATASOURCE_JSONP) {
  382. if(responseHandler == NULL)
  383. responseHandler = "callback";
  384. buffer_strcat(w->response.data, responseHandler);
  385. buffer_strcat(w->response.data, "(");
  386. }
  387. ret = rrdset2anything_api_v1(st, w->response.data, dimensions, format, points, after, before, group, group_time
  388. , options, &last_timestamp_in_data);
  389. if(format == DATASOURCE_DATATABLE_JSONP) {
  390. if(google_timestamp < last_timestamp_in_data)
  391. buffer_strcat(w->response.data, "});");
  392. else {
  393. // the client already has the latest data
  394. buffer_flush(w->response.data);
  395. buffer_sprintf(w->response.data,
  396. "%s({version:'%s',reqId:'%s',status:'error',errors:[{reason:'not_modified',message:'Data not modified'}]});",
  397. responseHandler, google_version, google_reqId);
  398. }
  399. }
  400. else if(format == DATASOURCE_JSONP)
  401. buffer_strcat(w->response.data, ");");
  402. cleanup:
  403. buffer_free(dimensions);
  404. return ret;
  405. }
  406. // Pings a netdata server:
  407. // /api/v1/registry?action=hello
  408. //
  409. // Access to a netdata registry:
  410. // /api/v1/registry?action=access&machine=${machine_guid}&name=${hostname}&url=${url}
  411. //
  412. // Delete from a netdata registry:
  413. // /api/v1/registry?action=delete&machine=${machine_guid}&name=${hostname}&url=${url}&delete_url=${delete_url}
  414. //
  415. // Search for the URLs of a machine:
  416. // /api/v1/registry?action=search&machine=${machine_guid}&name=${hostname}&url=${url}&for=${machine_guid}
  417. //
  418. // Impersonate:
  419. // /api/v1/registry?action=switch&machine=${machine_guid}&name=${hostname}&url=${url}&to=${new_person_guid}
  420. inline int web_client_api_request_v1_registry(RRDHOST *host, struct web_client *w, char *url) {
  421. static uint32_t hash_action = 0, hash_access = 0, hash_hello = 0, hash_delete = 0, hash_search = 0,
  422. hash_switch = 0, hash_machine = 0, hash_url = 0, hash_name = 0, hash_delete_url = 0, hash_for = 0,
  423. hash_to = 0 /*, hash_redirects = 0 */;
  424. if(unlikely(!hash_action)) {
  425. hash_action = simple_hash("action");
  426. hash_access = simple_hash("access");
  427. hash_hello = simple_hash("hello");
  428. hash_delete = simple_hash("delete");
  429. hash_search = simple_hash("search");
  430. hash_switch = simple_hash("switch");
  431. hash_machine = simple_hash("machine");
  432. hash_url = simple_hash("url");
  433. hash_name = simple_hash("name");
  434. hash_delete_url = simple_hash("delete_url");
  435. hash_for = simple_hash("for");
  436. hash_to = simple_hash("to");
  437. /*
  438. hash_redirects = simple_hash("redirects");
  439. */
  440. }
  441. char person_guid[GUID_LEN + 1] = "";
  442. debug(D_WEB_CLIENT, "%llu: API v1 registry with URL '%s'", w->id, url);
  443. // TODO
  444. // The browser may send multiple cookies with our id
  445. char *cookie = strstr(w->response.data->buffer, NETDATA_REGISTRY_COOKIE_NAME "=");
  446. if(cookie)
  447. strncpyz(person_guid, &cookie[sizeof(NETDATA_REGISTRY_COOKIE_NAME)], 36);
  448. char action = '\0';
  449. char *machine_guid = NULL,
  450. *machine_url = NULL,
  451. *url_name = NULL,
  452. *search_machine_guid = NULL,
  453. *delete_url = NULL,
  454. *to_person_guid = NULL;
  455. /*
  456. int redirects = 0;
  457. */
  458. while(url) {
  459. char *value = mystrsep(&url, "&");
  460. if (!value || !*value) continue;
  461. char *name = mystrsep(&value, "=");
  462. if (!name || !*name) continue;
  463. if (!value || !*value) continue;
  464. debug(D_WEB_CLIENT, "%llu: API v1 registry query param '%s' with value '%s'", w->id, name, value);
  465. uint32_t hash = simple_hash(name);
  466. if(hash == hash_action && !strcmp(name, "action")) {
  467. uint32_t vhash = simple_hash(value);
  468. if(vhash == hash_access && !strcmp(value, "access")) action = 'A';
  469. else if(vhash == hash_hello && !strcmp(value, "hello")) action = 'H';
  470. else if(vhash == hash_delete && !strcmp(value, "delete")) action = 'D';
  471. else if(vhash == hash_search && !strcmp(value, "search")) action = 'S';
  472. else if(vhash == hash_switch && !strcmp(value, "switch")) action = 'W';
  473. #ifdef NETDATA_INTERNAL_CHECKS
  474. else error("unknown registry action '%s'", value);
  475. #endif /* NETDATA_INTERNAL_CHECKS */
  476. }
  477. /*
  478. else if(hash == hash_redirects && !strcmp(name, "redirects"))
  479. redirects = atoi(value);
  480. */
  481. else if(hash == hash_machine && !strcmp(name, "machine"))
  482. machine_guid = value;
  483. else if(hash == hash_url && !strcmp(name, "url"))
  484. machine_url = value;
  485. else if(action == 'A') {
  486. if(hash == hash_name && !strcmp(name, "name"))
  487. url_name = value;
  488. }
  489. else if(action == 'D') {
  490. if(hash == hash_delete_url && !strcmp(name, "delete_url"))
  491. delete_url = value;
  492. }
  493. else if(action == 'S') {
  494. if(hash == hash_for && !strcmp(name, "for"))
  495. search_machine_guid = value;
  496. }
  497. else if(action == 'W') {
  498. if(hash == hash_to && !strcmp(name, "to"))
  499. to_person_guid = value;
  500. }
  501. #ifdef NETDATA_INTERNAL_CHECKS
  502. else error("unused registry URL parameter '%s' with value '%s'", name, value);
  503. #endif /* NETDATA_INTERNAL_CHECKS */
  504. }
  505. if(unlikely(respect_web_browser_do_not_track_policy && web_client_has_donottrack(w))) {
  506. buffer_flush(w->response.data);
  507. 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.");
  508. return 400;
  509. }
  510. if(unlikely(action == 'H')) {
  511. // HELLO request, dashboard ACL
  512. if(unlikely(!web_client_can_access_dashboard(w)))
  513. return web_client_permission_denied(w);
  514. }
  515. else {
  516. // everything else, registry ACL
  517. if(unlikely(!web_client_can_access_registry(w)))
  518. return web_client_permission_denied(w);
  519. }
  520. switch(action) {
  521. case 'A':
  522. if(unlikely(!machine_guid || !machine_url || !url_name)) {
  523. 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");
  524. buffer_flush(w->response.data);
  525. buffer_strcat(w->response.data, "Invalid registry Access request.");
  526. return 400;
  527. }
  528. web_client_enable_tracking_required(w);
  529. return registry_request_access_json(host, w, person_guid, machine_guid, machine_url, url_name, now_realtime_sec());
  530. case 'D':
  531. if(unlikely(!machine_guid || !machine_url || !delete_url)) {
  532. 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");
  533. buffer_flush(w->response.data);
  534. buffer_strcat(w->response.data, "Invalid registry Delete request.");
  535. return 400;
  536. }
  537. web_client_enable_tracking_required(w);
  538. return registry_request_delete_json(host, w, person_guid, machine_guid, machine_url, delete_url, now_realtime_sec());
  539. case 'S':
  540. if(unlikely(!machine_guid || !machine_url || !search_machine_guid)) {
  541. error("Invalid registry request - search requires these parameters: machine ('%s'), url ('%s'), for ('%s')", machine_guid?machine_guid:"UNSET", machine_url?machine_url:"UNSET", search_machine_guid?search_machine_guid:"UNSET");
  542. buffer_flush(w->response.data);
  543. buffer_strcat(w->response.data, "Invalid registry Search request.");
  544. return 400;
  545. }
  546. web_client_enable_tracking_required(w);
  547. return registry_request_search_json(host, w, person_guid, machine_guid, machine_url, search_machine_guid, now_realtime_sec());
  548. case 'W':
  549. if(unlikely(!machine_guid || !machine_url || !to_person_guid)) {
  550. 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");
  551. buffer_flush(w->response.data);
  552. buffer_strcat(w->response.data, "Invalid registry Switch request.");
  553. return 400;
  554. }
  555. web_client_enable_tracking_required(w);
  556. return registry_request_switch_json(host, w, person_guid, machine_guid, machine_url, to_person_guid, now_realtime_sec());
  557. case 'H':
  558. return registry_request_hello_json(host, w);
  559. default:
  560. buffer_flush(w->response.data);
  561. buffer_strcat(w->response.data, "Invalid registry request - you need to set an action: hello, access, delete, search");
  562. return 400;
  563. }
  564. }
  565. static inline void web_client_api_request_v1_info_summary_alarm_statuses(RRDHOST *host, BUFFER *wb) {
  566. int alarm_normal = 0, alarm_warn = 0, alarm_crit = 0;
  567. RRDCALC *rc;
  568. rrdhost_rdlock(host);
  569. for(rc = host->alarms; rc ; rc = rc->next) {
  570. if(unlikely(!rc->rrdset || !rc->rrdset->last_collected_time.tv_sec))
  571. continue;
  572. switch(rc->status) {
  573. case RRDCALC_STATUS_WARNING:
  574. alarm_warn++;
  575. break;
  576. case RRDCALC_STATUS_CRITICAL:
  577. alarm_crit++;
  578. break;
  579. default:
  580. alarm_normal++;
  581. }
  582. }
  583. rrdhost_unlock(host);
  584. buffer_sprintf(wb, "\t\t\"normal\": %d,\n", alarm_normal);
  585. buffer_sprintf(wb, "\t\t\"warning\": %d,\n", alarm_warn);
  586. buffer_sprintf(wb, "\t\t\"critical\": %d\n", alarm_crit);
  587. }
  588. static inline void web_client_api_request_v1_info_mirrored_hosts(BUFFER *wb) {
  589. RRDHOST *rc;
  590. int count = 0;
  591. rrd_rdlock();
  592. rrdhost_foreach_read(rc) {
  593. if(count > 0) buffer_strcat(wb, ",\n");
  594. buffer_sprintf(wb, "\t\t\"%s\"", rc->hostname);
  595. count++;
  596. }
  597. buffer_strcat(wb, "\n");
  598. rrd_unlock();
  599. }
  600. inline int web_client_api_request_v1_info(RRDHOST *host, struct web_client *w, char *url) {
  601. (void)url;
  602. if (!netdata_ready) return 503;
  603. BUFFER *wb = w->response.data;
  604. buffer_flush(wb);
  605. wb->contenttype = CT_APPLICATION_JSON;
  606. buffer_strcat(wb, "{\n");
  607. buffer_sprintf(wb, "\t\"version\": \"%s\",\n", host->program_version);
  608. buffer_sprintf(wb, "\t\"uid\": \"%s\",\n", host->machine_guid);
  609. buffer_strcat(wb, "\t\"mirrored_hosts\": [\n");
  610. web_client_api_request_v1_info_mirrored_hosts(wb);
  611. buffer_strcat(wb, "\t],\n");
  612. buffer_strcat(wb, "\t\"alarms\": {\n");
  613. web_client_api_request_v1_info_summary_alarm_statuses(host, wb);
  614. buffer_strcat(wb, "\t},\n");
  615. buffer_sprintf(wb, "\t\"os_name\": %s,\n", (host->system_info->os_name) ? host->system_info->os_name : "\"\"");
  616. buffer_sprintf(wb, "\t\"os_id\": \"%s\",\n", (host->system_info->os_id) ? host->system_info->os_id : "");
  617. buffer_sprintf(wb, "\t\"os_id_like\": \"%s\",\n", (host->system_info->os_id_like) ? host->system_info->os_id_like : "");
  618. buffer_sprintf(wb, "\t\"os_version\": \"%s\",\n", (host->system_info->os_version) ? host->system_info->os_version : "");
  619. buffer_sprintf(wb, "\t\"os_version_id\": \"%s\",\n", (host->system_info->os_version_id) ? host->system_info->os_version_id : "");
  620. buffer_sprintf(wb, "\t\"os_detection\": \"%s\",\n", (host->system_info->os_detection) ? host->system_info->os_detection : "");
  621. buffer_sprintf(wb, "\t\"kernel_name\": \"%s\",\n", (host->system_info->kernel_name) ? host->system_info->kernel_name : "");
  622. buffer_sprintf(wb, "\t\"kernel_version\": \"%s\",\n", (host->system_info->kernel_version) ? host->system_info->kernel_version : "");
  623. buffer_sprintf(wb, "\t\"architecture\": \"%s\",\n", (host->system_info->architecture) ? host->system_info->architecture : "");
  624. buffer_sprintf(wb, "\t\"virtualization\": \"%s\",\n", (host->system_info->virtualization) ? host->system_info->virtualization : "");
  625. buffer_sprintf(wb, "\t\"virt_detection\": \"%s\",\n", (host->system_info->virt_detection) ? host->system_info->virt_detection : "");
  626. buffer_sprintf(wb, "\t\"container\": \"%s\",\n", (host->system_info->container) ? host->system_info->container : "");
  627. buffer_sprintf(wb, "\t\"container_detection\": \"%s\",\n", (host->system_info->container_detection) ? host->system_info->container_detection : "");
  628. buffer_strcat(wb, "\t\"collectors\": [");
  629. chartcollectors2json(host, wb);
  630. buffer_strcat(wb, "\n\t]\n");
  631. buffer_strcat(wb, "}");
  632. buffer_no_cacheable(wb);
  633. return 200;
  634. }
  635. static struct api_command {
  636. const char *command;
  637. uint32_t hash;
  638. WEB_CLIENT_ACL acl;
  639. int (*callback)(RRDHOST *host, struct web_client *w, char *url);
  640. } api_commands[] = {
  641. { "info", 0, WEB_CLIENT_ACL_DASHBOARD, web_client_api_request_v1_info },
  642. { "data", 0, WEB_CLIENT_ACL_DASHBOARD, web_client_api_request_v1_data },
  643. { "chart", 0, WEB_CLIENT_ACL_DASHBOARD, web_client_api_request_v1_chart },
  644. { "charts", 0, WEB_CLIENT_ACL_DASHBOARD, web_client_api_request_v1_charts },
  645. // registry checks the ACL by itself, so we allow everything
  646. { "registry", 0, WEB_CLIENT_ACL_NOCHECK, web_client_api_request_v1_registry },
  647. // badges can be fetched with both dashboard and badge permissions
  648. { "badge.svg", 0, WEB_CLIENT_ACL_DASHBOARD|WEB_CLIENT_ACL_BADGE, web_client_api_request_v1_badge },
  649. { "alarms", 0, WEB_CLIENT_ACL_DASHBOARD, web_client_api_request_v1_alarms },
  650. { "alarm_log", 0, WEB_CLIENT_ACL_DASHBOARD, web_client_api_request_v1_alarm_log },
  651. { "alarm_variables", 0, WEB_CLIENT_ACL_DASHBOARD, web_client_api_request_v1_alarm_variables },
  652. { "allmetrics", 0, WEB_CLIENT_ACL_DASHBOARD, web_client_api_request_v1_allmetrics },
  653. { "manage/health", 0, WEB_CLIENT_ACL_MGMT, web_client_api_request_v1_mgmt_health },
  654. // terminator
  655. { NULL, 0, WEB_CLIENT_ACL_NONE, NULL },
  656. };
  657. inline int web_client_api_request_v1(RRDHOST *host, struct web_client *w, char *url) {
  658. static int initialized = 0;
  659. int i;
  660. if(unlikely(initialized == 0)) {
  661. initialized = 1;
  662. for(i = 0; api_commands[i].command ; i++)
  663. api_commands[i].hash = simple_hash(api_commands[i].command);
  664. }
  665. // get the command
  666. char *tok = mystrsep(&url, "?");
  667. if(tok && *tok) {
  668. debug(D_WEB_CLIENT, "%llu: Searching for API v1 command '%s'.", w->id, tok);
  669. uint32_t hash = simple_hash(tok);
  670. for(i = 0; api_commands[i].command ;i++) {
  671. if(unlikely(hash == api_commands[i].hash && !strcmp(tok, api_commands[i].command))) {
  672. if(unlikely(api_commands[i].acl != WEB_CLIENT_ACL_NOCHECK) && !(w->acl & api_commands[i].acl))
  673. return web_client_permission_denied(w);
  674. return api_commands[i].callback(host, w, url);
  675. }
  676. }
  677. buffer_flush(w->response.data);
  678. buffer_strcat(w->response.data, "Unsupported v1 API command: ");
  679. buffer_strcat_htmlescape(w->response.data, tok);
  680. return 404;
  681. }
  682. else {
  683. buffer_flush(w->response.data);
  684. buffer_sprintf(w->response.data, "Which API v1 command?");
  685. return 400;
  686. }
  687. }