respondd.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. /*
  2. Copyright (c) 2016, Matthias Schiffer <mschiffer@universe-factory.net>
  3. All rights reserved.
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions are met:
  6. 1. Redistributions of source code must retain the above copyright notice,
  7. this list of conditions and the following disclaimer.
  8. 2. Redistributions in binary form must reproduce the above copyright notice,
  9. this list of conditions and the following disclaimer in the documentation
  10. and/or other materials provided with the distribution.
  11. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  12. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  13. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  14. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  15. FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  16. DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  17. SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  18. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  19. OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  20. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  21. */
  22. #include <respondd.h>
  23. #include <json-c/json.h>
  24. #include <libgluonutil.h>
  25. #include <uci.h>
  26. #include <stdbool.h>
  27. #include <stdio.h>
  28. #include <string.h>
  29. #include <unistd.h>
  30. #include <sys/socket.h>
  31. #include <sys/un.h>
  32. static struct json_object * get_peer_groups(struct json_object *groups, struct json_object *peers);
  33. static struct json_object * get_fastd_version(void) {
  34. FILE *f = popen("exec fastd -v", "r");
  35. if (!f)
  36. return NULL;
  37. char *line = NULL;
  38. size_t len = 0;
  39. ssize_t r = getline(&line, &len, f);
  40. pclose(f);
  41. if (r >= 0) {
  42. len = strlen(line); /* The len given by getline is the buffer size, not the string length */
  43. if (len && line[len-1] == '\n')
  44. line[len-1] = 0;
  45. }
  46. else {
  47. free(line);
  48. line = NULL;
  49. }
  50. const char *version = line;
  51. if (strncmp(version, "fastd ", 6) == 0)
  52. version += 6;
  53. struct json_object *ret = gluonutil_wrap_string(version);
  54. free(line);
  55. return ret;
  56. }
  57. static struct json_object * get_fastd_public_key(void) {
  58. FILE *f = popen("/etc/init.d/fastd show_key mesh_vpn", "r");
  59. if (!f)
  60. return NULL;
  61. char *line = NULL;
  62. size_t len = 0;
  63. ssize_t r = getline(&line, &len, f);
  64. pclose(f);
  65. if (r >= 0) {
  66. len = strlen(line); /* The len given by getline is the buffer size, not the string length */
  67. if (len && line[len-1] == '\n')
  68. line[len-1] = 0;
  69. }
  70. else {
  71. free(line);
  72. line = NULL;
  73. }
  74. return gluonutil_wrap_and_free_string(line);
  75. }
  76. static bool get_pubkey_privacy(void) {
  77. bool ret = true;
  78. struct json_object *site = NULL;
  79. site = gluonutil_load_site_config();
  80. if (!site)
  81. goto end;
  82. struct json_object *mesh_vpn;
  83. if (!json_object_object_get_ex(site, "mesh_vpn", &mesh_vpn))
  84. goto end;
  85. struct json_object *pubkey_privacy;
  86. if (!json_object_object_get_ex(mesh_vpn, "pubkey_privacy", &pubkey_privacy))
  87. goto end;
  88. ret = json_object_get_boolean(pubkey_privacy);
  89. end:
  90. json_object_put(site);
  91. return ret;
  92. }
  93. static struct json_object * get_fastd(void) {
  94. bool enabled = false;
  95. struct json_object *ret = json_object_new_object();
  96. struct uci_context *ctx = uci_alloc_context();
  97. if (!ctx)
  98. goto disabled_nofree;
  99. ctx->flags &= ~UCI_FLAG_STRICT;
  100. struct uci_package *p;
  101. if (uci_load(ctx, "fastd", &p))
  102. goto disabled;
  103. struct uci_section *s = uci_lookup_section(ctx, p, "mesh_vpn");
  104. if (!s)
  105. goto disabled;
  106. const char *enabled_str = uci_lookup_option_string(ctx, s, "enabled");
  107. if (!enabled_str || !strcmp(enabled_str, "1"))
  108. enabled = true;
  109. disabled:
  110. uci_free_context(ctx);
  111. disabled_nofree:
  112. json_object_object_add(ret, "version", get_fastd_version());
  113. json_object_object_add(ret, "enabled", json_object_new_boolean(enabled));
  114. if (enabled && !get_pubkey_privacy())
  115. json_object_object_add(ret, "public_key", get_fastd_public_key());
  116. return ret;
  117. }
  118. static struct json_object * respondd_provider_nodeinfo(void) {
  119. struct json_object *ret = json_object_new_object();
  120. struct json_object *software = json_object_new_object();
  121. json_object_object_add(software, "fastd", get_fastd());
  122. json_object_object_add(ret, "software", software);
  123. return ret;
  124. }
  125. static const char * get_status_socket(struct uci_context *ctx, struct uci_section *s) {
  126. return uci_lookup_option_string(ctx, s, "status_socket");
  127. }
  128. static struct json_object * read_status(struct uci_context *ctx, struct uci_section *s) {
  129. const char *path = get_status_socket(ctx, s);
  130. size_t addrlen = strlen(path);
  131. /* Allocate enough space for arbitrary-length paths */
  132. char addrbuf[offsetof(struct sockaddr_un, sun_path) + addrlen + 1];
  133. memset(addrbuf, 0, sizeof(addrbuf));
  134. struct sockaddr_un *addr = (struct sockaddr_un *)addrbuf;
  135. addr->sun_family = AF_UNIX;
  136. memcpy(addr->sun_path, path, addrlen+1);
  137. int fd = socket(AF_UNIX, SOCK_STREAM, 0);
  138. if (fd < 0)
  139. return NULL;
  140. if (connect(fd, (struct sockaddr*)addr, sizeof(addrbuf)) < 0) {
  141. close(fd);
  142. return NULL;
  143. }
  144. struct json_object *ret = NULL;
  145. struct json_tokener *tok = json_tokener_new();
  146. do {
  147. char buf[1024];
  148. size_t len = read(fd, buf, sizeof(buf));
  149. if (len <= 0)
  150. break;
  151. ret = json_tokener_parse_ex(tok, buf, len);
  152. } while (!ret && json_tokener_get_error(tok) == json_tokener_continue);
  153. json_tokener_free(tok);
  154. close(fd);
  155. return ret;
  156. }
  157. static struct json_object * get_status(void) {
  158. struct json_object *ret = NULL;
  159. struct uci_context *ctx = uci_alloc_context();
  160. if (!ctx)
  161. return NULL;
  162. ctx->flags &= ~UCI_FLAG_STRICT;
  163. struct uci_package *p;
  164. if (!uci_load(ctx, "fastd", &p)) {
  165. struct uci_section *s = uci_lookup_section(ctx, p, "mesh_vpn");
  166. if (s)
  167. ret = read_status(ctx, s);
  168. }
  169. uci_free_context(ctx);
  170. return ret;
  171. }
  172. static bool get_peer_connection(struct json_object **ret, struct json_object *config, struct json_object *peers) {
  173. struct json_object *key_object;
  174. if (!json_object_object_get_ex(config, "key", &key_object))
  175. return false;
  176. const char *key = json_object_get_string(key_object);
  177. if (!key)
  178. return false;
  179. struct json_object *peer, *connection, *established;
  180. if (!json_object_object_get_ex(peers, key, &peer) ||
  181. !json_object_object_get_ex(peer, "connection", &connection))
  182. return false;
  183. if (json_object_object_get_ex(connection, "established", &established)) {
  184. int64_t established_time = json_object_get_int64(established);
  185. *ret = json_object_new_object();
  186. struct json_object *jso = json_object_new_double(established_time/1000.0);
  187. json_object_set_serializer(jso, json_object_double_to_json_string, "%.3f", NULL);
  188. json_object_object_add(*ret, "established", jso);
  189. }
  190. else {
  191. *ret = NULL;
  192. }
  193. return true;
  194. }
  195. static struct json_object * get_peer_group(struct json_object *config, struct json_object *peers) {
  196. struct json_object *ret = json_object_new_object();
  197. struct json_object *config_peers;
  198. if (json_object_object_get_ex(config, "peers", &config_peers) &&
  199. json_object_is_type(config_peers, json_type_object)) {
  200. struct json_object *ret_peers = json_object_new_object();
  201. json_object_object_foreach(config_peers, peername, peerconfig) {
  202. struct json_object *obj;
  203. if (get_peer_connection(&obj, peerconfig, peers))
  204. json_object_object_add(ret_peers, peername, obj);
  205. }
  206. if (json_object_object_length(ret_peers))
  207. json_object_object_add(ret, "peers", ret_peers);
  208. else
  209. json_object_put(ret_peers);
  210. }
  211. struct json_object *config_groups;
  212. if (json_object_object_get_ex(config, "groups", &config_groups)) {
  213. struct json_object *obj = get_peer_groups(config_groups, peers);
  214. if (obj)
  215. json_object_object_add(ret, "groups", obj);
  216. }
  217. if (!json_object_object_length(ret)) {
  218. json_object_put(ret);
  219. return NULL;
  220. }
  221. return ret;
  222. }
  223. static struct json_object * get_peer_groups(struct json_object *groups, struct json_object *peers) {
  224. if (!json_object_is_type(groups, json_type_object))
  225. return NULL;
  226. struct json_object *ret = json_object_new_object();
  227. json_object_object_foreach(groups, name, group) {
  228. struct json_object *g = get_peer_group(group, peers);
  229. if (g)
  230. json_object_object_add(ret, name, g);
  231. }
  232. if (!json_object_object_length(ret)) {
  233. json_object_put(ret);
  234. return NULL;
  235. }
  236. return ret;
  237. }
  238. static struct json_object * get_mesh_vpn(void) {
  239. struct json_object *ret = NULL;
  240. struct json_object *status = NULL;
  241. struct json_object *site = NULL;
  242. status = get_status();
  243. if (!status)
  244. goto end;
  245. struct json_object *peers;
  246. if (!json_object_object_get_ex(status, "peers", &peers))
  247. goto end;
  248. site = gluonutil_load_site_config();
  249. if (!site)
  250. goto end;
  251. struct json_object *mesh_vpn;
  252. if (!json_object_object_get_ex(site, "mesh_vpn", &mesh_vpn))
  253. goto end;
  254. struct json_object *mesh_vpn_fastd;
  255. if (!json_object_object_get_ex(mesh_vpn, "fastd", &mesh_vpn_fastd))
  256. goto end;
  257. ret = get_peer_group(mesh_vpn_fastd, peers);
  258. end:
  259. json_object_put(site);
  260. json_object_put(status);
  261. return ret;
  262. }
  263. static struct json_object * respondd_provider_statistics(void) {
  264. struct json_object *ret = json_object_new_object();
  265. struct json_object *mesh_vpn = get_mesh_vpn();
  266. if (mesh_vpn)
  267. json_object_object_add(ret, "mesh_vpn", mesh_vpn);
  268. return ret;
  269. }
  270. const struct respondd_provider_info respondd_providers[] = {
  271. {"nodeinfo", respondd_provider_nodeinfo},
  272. {"statistics", respondd_provider_statistics},
  273. {}
  274. };