IpTunnel_admin.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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. #include "admin/Admin.h"
  16. #include "benc/String.h"
  17. #include "benc/Int.h"
  18. #include "benc/Dict.h"
  19. #include "benc/List.h"
  20. #include "crypto/Key.h"
  21. #include "memory/Allocator.h"
  22. #include "tunnel/IpTunnel.h"
  23. #include "tunnel/IpTunnel_admin.h"
  24. #include "util/platform/Sockaddr.h"
  25. #include <stddef.h>
  26. struct Context
  27. {
  28. struct IpTunnel* ipTun;
  29. struct Admin* admin;
  30. };
  31. static void sendResponse(int conn, String* txid, struct Admin* admin)
  32. {
  33. Dict resp = Dict_CONST(
  34. String_CONST("connection"), Int_OBJ(conn), Dict_CONST(
  35. String_CONST("error"), String_OBJ(String_CONST("none")), NULL
  36. ));
  37. Admin_sendMessage(&resp, txid, admin);
  38. }
  39. static void sendError(char* error, String* txid, struct Admin* admin)
  40. {
  41. Dict resp = Dict_CONST(
  42. String_CONST("error"), String_OBJ(String_CONST(error)), NULL
  43. );
  44. Admin_sendMessage(&resp, txid, admin);
  45. }
  46. static void allowConnection(Dict* args,
  47. void* vcontext,
  48. String* txid,
  49. struct Allocator* requestAlloc)
  50. {
  51. struct Context* context = (struct Context*) vcontext;
  52. String* publicKeyOfAuthorizedNode =
  53. Dict_getString(args, String_CONST("publicKeyOfAuthorizedNode"));
  54. String* ip6Address = Dict_getString(args, String_CONST("ip6Address"));
  55. int64_t* ip6Prefix = Dict_getInt(args, String_CONST("ip6Prefix"));
  56. int64_t* ip6Alloc = Dict_getInt(args, String_CONST("ip6Alloc"));
  57. String* ip4Address = Dict_getString(args, String_CONST("ip4Address"));
  58. int64_t* ip4Prefix = Dict_getInt(args, String_CONST("ip4Prefix"));
  59. int64_t* ip4Alloc = Dict_getInt(args, String_CONST("ip4Alloc"));
  60. uint8_t pubKey[32];
  61. uint8_t ip6Addr[16];
  62. struct Sockaddr_storage ip6ToGive;
  63. struct Sockaddr_storage ip4ToGive;
  64. char* error;
  65. int ret;
  66. if (!ip6Address && !ip4Address) {
  67. error = "Must specify ip6Address or ip4Address";
  68. } else if ((ret = Key_parse(publicKeyOfAuthorizedNode, pubKey, ip6Addr)) != 0) {
  69. error = Key_parse_strerror(ret);
  70. } else if (ip6Prefix && !ip6Address) {
  71. error = "Must specify ip6Address with ip6Prefix";
  72. } else if (ip6Alloc && !ip6Address) {
  73. error = "Must specify ip6Address with ip6Alloc";
  74. } else if (ip6Prefix && (*ip6Prefix > 128 || *ip6Prefix < 0)) {
  75. error = "ip6Prefix out of range: must be 0 to 128";
  76. } else if (ip6Alloc && (*ip6Alloc > 128 || *ip6Alloc < 1)) {
  77. error = "ip6Alloc out of range: must be 1 to 128";
  78. } else if (ip4Prefix && !ip4Address) {
  79. error = "Must specify ip4Address with ip4Prefix";
  80. } else if (ip4Alloc && !ip4Address) {
  81. error = "Must specify ip4Address with ip4Alloc";
  82. } else if (ip4Prefix && (*ip4Prefix > 32 || *ip4Prefix < 0)) {
  83. error = "ip4Prefix out of range: must be 0 to 32";
  84. } else if (ip4Alloc && (*ip4Alloc > 32 || *ip4Alloc < 1)) {
  85. error = "ip6Alloc out of range: must be 1 to 32";
  86. } else if (ip6Address
  87. && (Sockaddr_parse(ip6Address->bytes, &ip6ToGive)
  88. || Sockaddr_getFamily(&ip6ToGive.addr) != Sockaddr_AF_INET6))
  89. {
  90. error = "malformed ip6Address";
  91. } else if (ip4Address
  92. && (Sockaddr_parse(ip4Address->bytes, &ip4ToGive)
  93. || Sockaddr_getFamily(&ip4ToGive.addr) != Sockaddr_AF_INET))
  94. {
  95. error = "malformed ip4Address";
  96. } else {
  97. int conn = IpTunnel_allowConnection(pubKey,
  98. (ip6Address) ? &ip6ToGive.addr : NULL,
  99. (ip6Prefix) ? (uint8_t) (*ip6Prefix) : 32,
  100. (ip6Alloc) ? (uint8_t) (*ip6Alloc) : 32,
  101. (ip4Address) ? &ip4ToGive.addr : NULL,
  102. (ip4Prefix) ? (uint8_t) (*ip4Prefix) : 32,
  103. (ip4Alloc) ? (uint8_t) (*ip4Alloc) : 32,
  104. context->ipTun);
  105. sendResponse(conn, txid, context->admin);
  106. return;
  107. }
  108. sendError(error, txid, context->admin);
  109. }
  110. static void connectTo(Dict* args, void* vcontext, String* txid, struct Allocator* requestAlloc)
  111. {
  112. struct Context* context = vcontext;
  113. String* publicKeyOfNodeToConnectTo =
  114. Dict_getString(args, String_CONST("publicKeyOfNodeToConnectTo"));
  115. uint8_t pubKey[32];
  116. uint8_t ip6[16];
  117. int ret;
  118. if ((ret = Key_parse(publicKeyOfNodeToConnectTo, pubKey, ip6)) != 0) {
  119. sendError(Key_parse_strerror(ret), txid, context->admin);
  120. return;
  121. }
  122. int conn = IpTunnel_connectTo(pubKey, context->ipTun);
  123. sendResponse(conn, txid, context->admin);
  124. }
  125. static void removeConnection(Dict* args,
  126. void* vcontext,
  127. String* txid,
  128. struct Allocator* requestAlloc)
  129. {
  130. struct Context* context = vcontext;
  131. int conn = (int) *(Dict_getInt(args, String_CONST("connection")));
  132. char* error = "none";
  133. int ret = IpTunnel_removeConnection(conn, context->ipTun);
  134. if (ret == IpTunnel_removeConnection_NOT_FOUND) {
  135. error = "not_found";
  136. } else if (ret) {
  137. error = "unknown";
  138. }
  139. sendError(error, txid, context->admin);
  140. }
  141. static void listConnections(Dict* args,
  142. void* vcontext,
  143. String* txid,
  144. struct Allocator* alloc)
  145. {
  146. struct Context* context = vcontext;
  147. List* l = List_new(alloc);
  148. for (int i = 0; i < (int)context->ipTun->connectionList.count; i++) {
  149. List_addInt(l, context->ipTun->connectionList.connections[i].number, alloc);
  150. }
  151. Dict* resp = Dict_new(alloc);
  152. Dict_putList(resp, String_CONST("connections"), l, alloc);
  153. Dict_putString(resp, String_CONST("error"), String_CONST("none"), alloc);
  154. Admin_sendMessage(resp, txid, context->admin);
  155. }
  156. static void showConn(struct IpTunnel_Connection* conn,
  157. String* txid,
  158. struct Admin* admin,
  159. struct Allocator* alloc)
  160. {
  161. Dict* d = Dict_new(alloc);
  162. if (!Bits_isZero(conn->connectionIp6, 16)) {
  163. struct Sockaddr* addr = Sockaddr_clone(Sockaddr_LOOPBACK6, alloc);
  164. uint8_t* address;
  165. Assert_true(16 == Sockaddr_getAddress(addr, &address));
  166. Bits_memcpy(address, conn->connectionIp6, 16);
  167. char* printedAddr = Sockaddr_print(addr, alloc);
  168. Dict_putString(d, String_CONST("ip6Address"), String_CONST(printedAddr), alloc);
  169. Dict_putInt(d, String_CONST("ip6Prefix"), conn->connectionIp6Prefix, alloc);
  170. Dict_putInt(d, String_CONST("ip6Alloc"), conn->connectionIp6Alloc, alloc);
  171. }
  172. if (!Bits_isZero(conn->connectionIp4, 4)) {
  173. struct Sockaddr* addr = Sockaddr_clone(Sockaddr_LOOPBACK, alloc);
  174. uint8_t* address;
  175. Assert_true(4 == Sockaddr_getAddress(addr, &address));
  176. Bits_memcpy(address, conn->connectionIp4, 4);
  177. char* printedAddr = Sockaddr_print(addr, alloc);
  178. Dict_putString(d, String_CONST("ip4Address"), String_CONST(printedAddr), alloc);
  179. Dict_putInt(d, String_CONST("ip4Prefix"), conn->connectionIp4Prefix, alloc);
  180. Dict_putInt(d, String_CONST("ip4Alloc"), conn->connectionIp4Alloc, alloc);
  181. }
  182. Dict_putString(d, String_CONST("key"),
  183. Key_stringify(conn->routeHeader.publicKey, alloc), alloc);
  184. Dict_putInt(d, String_CONST("outgoing"), (conn->isOutgoing) ? 1 : 0, alloc);
  185. Dict_putString(d, String_CONST("error"), String_CONST("none"), alloc);
  186. Admin_sendMessage(d, txid, admin);
  187. }
  188. static void showConnection(Dict* args, void* vcontext, String* txid, struct Allocator* alloc)
  189. {
  190. struct Context* context = vcontext;
  191. int connNum = (int) *(Dict_getInt(args, String_CONST("connection")));
  192. for (int i = 0; i < (int)context->ipTun->connectionList.count; i++) {
  193. if (connNum == context->ipTun->connectionList.connections[i].number) {
  194. showConn(&context->ipTun->connectionList.connections[i], txid, context->admin, alloc);
  195. return;
  196. }
  197. }
  198. sendError("connection not found", txid, context->admin);
  199. }
  200. void IpTunnel_admin_register(struct IpTunnel* ipTun, struct Admin* admin, struct Allocator* alloc)
  201. {
  202. struct Context* context = Allocator_clone(alloc, (&(struct Context) {
  203. .admin = admin,
  204. .ipTun = ipTun
  205. }));
  206. Admin_registerFunction("IpTunnel_allowConnection", allowConnection, context, true,
  207. ((struct Admin_FunctionArg[]) {
  208. { .name = "publicKeyOfAuthorizedNode", .required = 1, .type = "String" },
  209. { .name = "ip6Address", .required = 0, .type = "String" },
  210. { .name = "ip6Prefix", .required = 0, .type = "Int" },
  211. { .name = "ip6Alloc", .required = 0, .type = "Int" },
  212. { .name = "ip4Address", .required = 0, .type = "String" },
  213. { .name = "ip4Prefix", .required = 0, .type = "Int" },
  214. { .name = "ip4Alloc", .required = 0, .type = "Int" },
  215. }), admin);
  216. Admin_registerFunction("IpTunnel_connectTo", connectTo, context, true,
  217. ((struct Admin_FunctionArg[]) {
  218. { .name = "publicKeyOfNodeToConnectTo", .required = 1, .type = "String" }
  219. }), admin);
  220. Admin_registerFunction("IpTunnel_removeConnection", removeConnection, context, true,
  221. ((struct Admin_FunctionArg[]) {
  222. { .name = "connection", .required = 1, .type = "Int" }
  223. }), admin);
  224. Admin_registerFunction("IpTunnel_showConnection", showConnection, context, true,
  225. ((struct Admin_FunctionArg[]) {
  226. { .name = "connection", .required = 1, .type = "Int" }
  227. }), admin);
  228. Admin_registerFunction("IpTunnel_listConnections", listConnections, context, true, NULL, admin);
  229. }