Configurator.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. /* vim: set expandtab ts=4 sw=4: */
  2. /*
  3. * You may redistribute this program and/or modify it under the terms of
  4. * the GNU General Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. #define string_strrchr
  16. #define string_strlen
  17. #include "admin/AdminClient.h"
  18. #include "admin/Configurator.h"
  19. #include "benc/String.h"
  20. #include "util/platform/libc/string.h"
  21. #include "benc/Dict.h"
  22. #include "benc/Int.h"
  23. #include "memory/Allocator.h"
  24. #include "util/events/Event.h"
  25. #include "util/Bits.h"
  26. #include "util/log/Log.h"
  27. #include "util/platform/Sockaddr.h"
  28. #include <stdlib.h>
  29. #include <stdbool.h>
  30. struct Context
  31. {
  32. struct Log* logger;
  33. struct Allocator* alloc;
  34. struct AdminClient* client;
  35. };
  36. static void die(struct AdminClient_Result* res, struct Context* ctx, struct Allocator* alloc)
  37. {
  38. Log_keys(ctx->logger, "message bytes = [%s]", res->messageBytes);
  39. #ifndef Log_KEYS
  40. Log_critical(ctx->logger, "enable Log_LEVEL=KEYS to see message content.");
  41. #endif
  42. Dict d = NULL;
  43. struct AdminClient_Result* exitRes =
  44. AdminClient_rpcCall(String_CONST("Core_exit"), &d, ctx->client, alloc);
  45. if (exitRes->err) {
  46. Log_critical(ctx->logger, "Failed to stop the core.");
  47. }
  48. Log_critical(ctx->logger, "Aborting.");
  49. exit(1);
  50. }
  51. static int rpcCall0(String* function,
  52. Dict* args,
  53. struct Context* ctx,
  54. struct Allocator* alloc,
  55. bool exitIfError)
  56. {
  57. struct AdminClient_Result* res = AdminClient_rpcCall(function, args, ctx->client, alloc);
  58. if (res->err) {
  59. Log_critical(ctx->logger,
  60. "Failed to make function call [%s], error: [%s]",
  61. AdminClient_errorString(res->err),
  62. function->bytes);
  63. die(res, ctx, alloc);
  64. }
  65. String* error = Dict_getString(res->responseDict, String_CONST("error"));
  66. if (error && !String_equals(error, String_CONST("none"))) {
  67. if (exitIfError) {
  68. Log_critical(ctx->logger,
  69. "Got error [%s] calling [%s]",
  70. error->bytes,
  71. function->bytes);
  72. die(res, ctx, alloc);
  73. }
  74. Log_warn(ctx->logger, "Got error [%s] calling [%s], ignoring.",
  75. error->bytes, function->bytes);
  76. return 1;
  77. }
  78. return 0;
  79. }
  80. static void rpcCall(String* function, Dict* args, struct Context* ctx, struct Allocator* alloc)
  81. {
  82. rpcCall0(function, args, ctx, alloc, true);
  83. }
  84. static void authorizedPasswords(List* list, struct Context* ctx)
  85. {
  86. uint32_t count = List_size(list);
  87. for (uint32_t i = 0; i < count; i++) {
  88. Dict* d = List_getDict(list, i);
  89. Log_info(ctx->logger, "Checking authorized password %d.", i);
  90. if (!d) {
  91. Log_critical(ctx->logger, "Not a dictionary type %d.", i);
  92. exit(-1);
  93. }
  94. String* passwd = Dict_getString(d, String_CONST("password"));
  95. if (!passwd) {
  96. Log_critical(ctx->logger, "Must specify a password %d.", i);
  97. exit(-1);
  98. }
  99. }
  100. for (uint32_t i = 0; i < count; i++) {
  101. struct Allocator* child = Allocator_child(ctx->alloc);
  102. Dict* d = List_getDict(list, i);
  103. String* passwd = Dict_getString(d, String_CONST("password"));
  104. String* user = Dict_getString(d, String_CONST("user"));
  105. if (!user) {
  106. user = String_printf(child, "password [%d]", i);
  107. }
  108. Log_info(ctx->logger, "Adding authorized password #[%d] for user [%s].", i, user->bytes);
  109. Dict args = Dict_CONST(
  110. String_CONST("authType"), Int_OBJ(1), Dict_CONST(
  111. String_CONST("password"), String_OBJ(passwd), Dict_CONST(
  112. String_CONST("user"), String_OBJ(user), NULL
  113. )));
  114. rpcCall(String_CONST("AuthorizedPasswords_add"), &args, ctx, child);
  115. Allocator_free(child);
  116. }
  117. }
  118. static void udpInterface(Dict* config, struct Context* ctx)
  119. {
  120. List* ifaces = Dict_getList(config, String_CONST("UDPInterface"));
  121. if (!ifaces) {
  122. ifaces = List_addDict(ifaces,
  123. Dict_getDict(config, String_CONST("UDPInterface")), ctx->alloc);
  124. }
  125. uint32_t count = List_size(ifaces);
  126. for (uint32_t i = 0; i < count; i++) {
  127. Dict *udp = List_getDict(ifaces, i);
  128. if (!udp) {
  129. continue;
  130. }
  131. // Setup the interface.
  132. String* bindStr = Dict_getString(udp, String_CONST("bind"));
  133. Dict* d = Dict_new(ctx->alloc);
  134. if (bindStr) {
  135. Dict_putString(d, String_CONST("bindAddress"), bindStr, ctx->alloc);
  136. }
  137. rpcCall(String_CONST("UDPInterface_new"), d, ctx, ctx->alloc);
  138. // Make the connections.
  139. Dict* connectTo = Dict_getDict(udp, String_CONST("connectTo"));
  140. if (connectTo) {
  141. struct Dict_Entry* entry = *connectTo;
  142. struct Allocator* perCallAlloc = Allocator_child(ctx->alloc);
  143. while (entry != NULL) {
  144. String* key = (String*) entry->key;
  145. if (entry->val->type != Object_DICT) {
  146. Log_critical(ctx->logger, "interfaces.UDPInterface.connectTo: entry [%s] "
  147. "is not a dictionary type.", key->bytes);
  148. exit(-1);
  149. }
  150. Dict* value = entry->val->as.dictionary;
  151. Log_keys(ctx->logger, "Attempting to connect to node [%s].", key->bytes);
  152. key = String_clone(key, perCallAlloc);
  153. char* lastColon = strrchr(key->bytes, ':');
  154. if (lastColon) {
  155. int port = atoi(lastColon+1);
  156. if (!port) {
  157. Log_critical(ctx->logger, "Couldn't get port number from [%s]", key->bytes);
  158. exit(-1);
  159. }
  160. *lastColon = '\0';
  161. struct Sockaddr* adr = Sockaddr_fromName(key->bytes, perCallAlloc);
  162. if (adr != NULL) {
  163. Sockaddr_setPort(adr, port);
  164. key = String_new(Sockaddr_print(adr, perCallAlloc), perCallAlloc);
  165. } else {
  166. Log_warn(ctx->logger, "Failed to lookup hostname [%s]", key->bytes);
  167. entry = entry->next;
  168. continue;
  169. }
  170. }
  171. Dict_putString(value, String_CONST("address"), key, perCallAlloc);
  172. rpcCall(String_CONST("UDPInterface_beginConnection"), value, ctx, perCallAlloc);
  173. entry = entry->next;
  174. }
  175. Allocator_free(perCallAlloc);
  176. }
  177. }
  178. }
  179. static void tunInterface(Dict* ifaceConf, struct Allocator* tempAlloc, struct Context* ctx)
  180. {
  181. String* ifaceType = Dict_getString(ifaceConf, String_CONST("type"));
  182. if (!String_equals(ifaceType, String_CONST("TUNInterface"))) {
  183. return;
  184. }
  185. // Setup the interface.
  186. String* device = Dict_getString(ifaceConf, String_CONST("tunDevice"));
  187. Dict* args = Dict_new(tempAlloc);
  188. if (device) {
  189. Dict_putString(args, String_CONST("desiredTunName"), device, tempAlloc);
  190. }
  191. rpcCall0(String_CONST("Core_initTunnel"), args, ctx, tempAlloc, false);
  192. }
  193. static void ipTunnel(Dict* ifaceConf, struct Allocator* tempAlloc, struct Context* ctx)
  194. {
  195. List* incoming = Dict_getList(ifaceConf, String_CONST("allowedConnections"));
  196. Dict* d;
  197. for (int i = 0; (d = List_getDict(incoming, i)) != NULL; i++) {
  198. String* key = Dict_getString(d, String_CONST("publicKey"));
  199. String* ip4 = Dict_getString(d, String_CONST("ip4Address"));
  200. String* ip6 = Dict_getString(d, String_CONST("ip6Address"));
  201. if (!key) {
  202. Log_critical(ctx->logger, "In router.ipTunnel.allowedConnections[%d]"
  203. "'publicKey' required.", i);
  204. exit(1);
  205. }
  206. if (!ip4 && !ip6) {
  207. Log_critical(ctx->logger, "In router.ipTunnel.allowedConnections[%d]"
  208. "either ip4Address or ip6Address required.", i);
  209. exit(1);
  210. }
  211. Log_debug(ctx->logger, "Allowing IpTunnel connections from [%s]", key->bytes);
  212. Dict_putString(d, String_CONST("publicKeyOfAuthorizedNode"), key, tempAlloc);
  213. rpcCall0(String_CONST("IpTunnel_allowConnection"), d, ctx, tempAlloc, true);
  214. }
  215. List* outgoing = Dict_getList(ifaceConf, String_CONST("outgoingConnections"));
  216. String* s;
  217. for (int i = 0; (s = List_getString(outgoing, i)) != NULL; i++) {
  218. Log_debug(ctx->logger, "Initiating IpTunnel connection to [%s]", s->bytes);
  219. Dict requestDict =
  220. Dict_CONST(String_CONST("publicKeyOfNodeToConnectTo"), String_OBJ(s), NULL);
  221. rpcCall0(String_CONST("IpTunnel_connectTo"), &requestDict, ctx, tempAlloc, true);
  222. }
  223. }
  224. static void routerConfig(Dict* routerConf, struct Allocator* tempAlloc, struct Context* ctx)
  225. {
  226. tunInterface(Dict_getDict(routerConf, String_CONST("interface")), tempAlloc, ctx);
  227. ipTunnel(Dict_getDict(routerConf, String_CONST("ipTunnel")), tempAlloc, ctx);
  228. }
  229. #ifdef HAS_ETH_INTERFACE
  230. static void ethInterface(Dict* config, struct Context* ctx)
  231. {
  232. List* ifaces = Dict_getList(config, String_CONST("ETHInterface"));
  233. if (!ifaces) {
  234. ifaces = List_addDict(ifaces,
  235. Dict_getDict(config, String_CONST("ETHInterface")), ctx->alloc);
  236. }
  237. uint32_t count = List_size(ifaces);
  238. for (uint32_t i = 0; i < count; i++) {
  239. Dict *eth = List_getDict(ifaces, i);
  240. if (!eth) {
  241. continue;
  242. }
  243. // Setup the interface.
  244. String* deviceStr = Dict_getString(eth, String_CONST("bind"));
  245. Log_info(ctx->logger, "Setting up ETHInterface [%d].", i);
  246. Dict* d = Dict_new(ctx->alloc);
  247. if (deviceStr) {
  248. Log_info(ctx->logger, "Binding to device [%s].", deviceStr->bytes);
  249. Dict_putString(d, String_CONST("bindDevice"), deviceStr, ctx->alloc);
  250. }
  251. if (rpcCall0(String_CONST("ETHInterface_new"), d, ctx, ctx->alloc, false)) {
  252. Log_warn(ctx->logger, "Failed to create ETHInterface.");
  253. continue;
  254. }
  255. // Make the connections.
  256. Dict* connectTo = Dict_getDict(eth, String_CONST("connectTo"));
  257. if (connectTo) {
  258. Log_info(ctx->logger, "ETHInterface should connect to a specific node.");
  259. struct Dict_Entry* entry = *connectTo;
  260. while (entry != NULL) {
  261. String* key = (String*) entry->key;
  262. if (entry->val->type != Object_DICT) {
  263. Log_critical(ctx->logger, "interfaces.ETHInterface.connectTo: entry [%s] "
  264. "is not a dictionary type.", key->bytes);
  265. exit(-1);
  266. }
  267. Dict* value = entry->val->as.dictionary;
  268. Log_keys(ctx->logger, "Attempting to connect to node [%s].", key->bytes);
  269. struct Allocator* perCallAlloc = Allocator_child(ctx->alloc);
  270. // Turn the dict from the config into our RPC args dict by filling in all
  271. // the arguments,
  272. Dict_putString(value, String_CONST("macAddress"), key, perCallAlloc);
  273. Dict_putInt(value, String_CONST("interfaceNumber"), i, perCallAlloc);
  274. rpcCall(String_CONST("ETHInterface_beginConnection"), value, ctx, perCallAlloc);
  275. Allocator_free(perCallAlloc);
  276. entry = entry->next;
  277. }
  278. }
  279. int64_t* beaconP = Dict_getInt(eth, String_CONST("beacon"));
  280. if (beaconP) {
  281. int64_t beacon = *beaconP;
  282. if (beacon > 3 || beacon < 0) {
  283. Log_error(ctx->logger, "interfaces.ETHInterface.beacon may only be 0, 1,or 2");
  284. } else {
  285. // We can cast beacon to an int here because we know it's small enough
  286. Log_info(ctx->logger, "Setting beacon mode on ETHInterface to [%d].", (int) beacon);
  287. Dict d = Dict_CONST(String_CONST("interfaceNumber"), Int_OBJ(i),
  288. Dict_CONST(String_CONST("state"), Int_OBJ(beacon), NULL));
  289. rpcCall(String_CONST("ETHInterface_beacon"), &d, ctx, ctx->alloc);
  290. }
  291. }
  292. }
  293. }
  294. #endif
  295. static void security(List* securityConf, struct Allocator* tempAlloc, struct Context* ctx)
  296. {
  297. bool noFiles = false;
  298. for (int i = 0; i < List_size(securityConf); i++) {
  299. if (String_equals(String_CONST("nofiles"), List_getString(securityConf, i))) {
  300. noFiles = true;
  301. } else {
  302. Dict* userDict = List_getDict(securityConf, i);
  303. String* userName = Dict_getString(userDict, String_CONST("setuser"));
  304. if (userName) {
  305. Dict d = Dict_CONST(String_CONST("user"), String_OBJ(userName), NULL);
  306. // If this call returns an error, it is ok.
  307. rpcCall0(String_CONST("Security_setUser"), &d, ctx, tempAlloc, false);
  308. }
  309. }
  310. }
  311. if (noFiles) {
  312. Dict d = NULL;
  313. rpcCall(String_CONST("Security_noFiles"), &d, ctx, tempAlloc);
  314. }
  315. }
  316. void Configurator_config(Dict* config,
  317. struct Sockaddr* sockAddr,
  318. String* adminPassword,
  319. struct EventBase* eventBase,
  320. struct Log* logger,
  321. struct Allocator* alloc)
  322. {
  323. struct Allocator* tempAlloc = Allocator_child(alloc);
  324. struct AdminClient* client =
  325. AdminClient_new(sockAddr, adminPassword, eventBase, logger, tempAlloc);
  326. struct Context ctx = { .logger = logger, .alloc = tempAlloc, .client = client };
  327. List* authedPasswords = Dict_getList(config, String_CONST("authorizedPasswords"));
  328. if (authedPasswords) {
  329. authorizedPasswords(authedPasswords, &ctx);
  330. }
  331. Dict* ifaces = Dict_getDict(config, String_CONST("interfaces"));
  332. udpInterface(ifaces, &ctx);
  333. #ifdef HAS_ETH_INTERFACE
  334. ethInterface(ifaces, &ctx);
  335. #endif
  336. Dict* routerConf = Dict_getDict(config, String_CONST("router"));
  337. routerConfig(routerConf, tempAlloc, &ctx);
  338. List* securityList = Dict_getList(config, String_CONST("security"));
  339. security(securityList, tempAlloc, &ctx);
  340. Allocator_free(tempAlloc);
  341. }