libgluonutil.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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 "libgluonutil.h"
  23. #include <json-c/json.h>
  24. #include <uci.h>
  25. #include <arpa/inet.h>
  26. #include <stdio.h>
  27. #include <string.h>
  28. #include <stdlib.h>
  29. #include <unistd.h>
  30. /**
  31. * Merges two JSON objects
  32. *
  33. * Both objects are consumed. On conflicts, object b will be preferred.
  34. */
  35. static struct json_object * merge_json(struct json_object *a, struct json_object *b) {
  36. if (!json_object_is_type(a, json_type_object) || !json_object_is_type(b, json_type_object)) {
  37. json_object_put(a);
  38. return b;
  39. }
  40. json_object *m = json_object_new_object();
  41. json_object_object_foreach(a, key_a, val_a)
  42. json_object_object_add(m, key_a, json_object_get(val_a));
  43. json_object_put(a);
  44. json_object_object_foreach(b, key_b, val_b) {
  45. struct json_object *val_m;
  46. if (json_object_object_get_ex(m, key_b, &val_m))
  47. val_m = merge_json(json_object_get(val_m), json_object_get(val_b));
  48. else
  49. val_m = json_object_get(val_b);
  50. json_object_object_add(m, key_b, val_m);
  51. }
  52. json_object_put(b);
  53. return m;
  54. }
  55. char * gluonutil_read_line(const char *filename) {
  56. FILE *f = fopen(filename, "r");
  57. if (!f)
  58. return NULL;
  59. char *line = NULL;
  60. size_t len = 0;
  61. ssize_t r = getline(&line, &len, f);
  62. fclose(f);
  63. if (r >= 0) {
  64. len = strlen(line);
  65. if (len && line[len-1] == '\n')
  66. line[len-1] = 0;
  67. }
  68. else {
  69. free(line);
  70. line = NULL;
  71. }
  72. return line;
  73. }
  74. char * gluonutil_get_sysconfig(const char *key) {
  75. if (strchr(key, '/'))
  76. return NULL;
  77. const char prefix[] = "/lib/gluon/core/sysconfig/";
  78. char path[strlen(prefix) + strlen(key) + 1];
  79. snprintf(path, sizeof(path), "%s%s", prefix, key);
  80. return gluonutil_read_line(path);
  81. }
  82. char * gluonutil_get_node_id(void) {
  83. char *node_id = gluonutil_get_sysconfig("primary_mac");
  84. if (!node_id)
  85. return NULL;
  86. char *in = node_id, *out = node_id;
  87. do {
  88. if (*in != ':')
  89. *out++ = *in;
  90. } while (*in++);
  91. return node_id;
  92. }
  93. char * gluonutil_get_interface_address(const char *ifname) {
  94. const char *format = "/sys/class/net/%s/address";
  95. char path[strlen(format) + strlen(ifname) - 1];
  96. snprintf(path, sizeof(path), format, ifname);
  97. return gluonutil_read_line(path);
  98. }
  99. struct json_object * gluonutil_wrap_string(const char *str) {
  100. if (!str)
  101. return NULL;
  102. return json_object_new_string(str);
  103. }
  104. struct json_object * gluonutil_wrap_and_free_string(char *str) {
  105. struct json_object *ret = gluonutil_wrap_string(str);
  106. free(str);
  107. return ret;
  108. }
  109. bool gluonutil_get_node_prefix6(struct in6_addr *prefix) {
  110. struct json_object *site = gluonutil_load_site_config();
  111. if (!site)
  112. return false;
  113. struct json_object *node_prefix = NULL;
  114. if (!json_object_object_get_ex(site, "node_prefix6", &node_prefix)) {
  115. json_object_put(site);
  116. return false;
  117. }
  118. const char *str_prefix = json_object_get_string(node_prefix);
  119. if (!str_prefix) {
  120. json_object_put(site);
  121. return false;
  122. }
  123. char *prefix_addr = strndup(str_prefix, strchrnul(str_prefix, '/')-str_prefix);
  124. int ret = inet_pton(AF_INET6, prefix_addr, prefix);
  125. free(prefix_addr);
  126. json_object_put(site);
  127. if (ret != 1)
  128. return false;
  129. return true;
  130. }
  131. bool gluonutil_has_domains(void) {
  132. return (access("/lib/gluon/domains/", F_OK) == 0);
  133. }
  134. char * gluonutil_get_domain(void) {
  135. if (!gluonutil_has_domains())
  136. return NULL;
  137. char *ret = NULL;
  138. struct uci_context *ctx = uci_alloc_context();
  139. if (!ctx)
  140. goto uci_fail;
  141. ctx->flags &= ~UCI_FLAG_STRICT;
  142. struct uci_package *p;
  143. if (uci_load(ctx, "gluon", &p))
  144. goto uci_fail;
  145. struct uci_section *s = uci_lookup_section(ctx, p, "core");
  146. if (!s)
  147. goto uci_fail;
  148. const char *domain_code = uci_lookup_option_string(ctx, s, "domain");
  149. if (!domain_code)
  150. goto uci_fail;
  151. ret = strdup(domain_code);
  152. uci_fail:
  153. if (ctx)
  154. uci_free_context(ctx);
  155. return ret;
  156. }
  157. struct json_object * gluonutil_load_site_config(void) {
  158. char *domain_code = NULL;
  159. struct json_object *site = NULL, *domain = NULL;
  160. site = json_object_from_file("/lib/gluon/site.json");
  161. if (!site)
  162. return NULL;
  163. if (!gluonutil_has_domains())
  164. return site;
  165. domain_code = gluonutil_get_domain();
  166. if (!domain_code)
  167. goto err;
  168. {
  169. const char *domain_path_fmt = "/lib/gluon/domains/%s.json";
  170. char domain_path[strlen(domain_path_fmt) + strlen(domain_code)];
  171. snprintf(domain_path, sizeof(domain_path), domain_path_fmt, domain_code);
  172. free(domain_code);
  173. domain = json_object_from_file(domain_path);
  174. }
  175. if (!domain)
  176. goto err;
  177. return merge_json(site, domain);
  178. err:
  179. json_object_put(site);
  180. free(domain_code);
  181. return NULL;
  182. }