AngelInit.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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/angel/Angel.h"
  16. #include "admin/angel/InterfaceWaiter.h"
  17. #include "benc/Dict.h"
  18. #include "benc/String.h"
  19. #include "benc/serialization/standard/StandardBencSerializer.h"
  20. #include "benc/serialization/BencSerializer.h"
  21. #include "crypto/random/Random.h"
  22. #include "interface/Interface.h"
  23. #include "interface/FramingInterface.h"
  24. #include "io/ArrayReader.h"
  25. #include "io/ArrayWriter.h"
  26. #include "io/FileReader.h"
  27. #include "io/FileWriter.h"
  28. #include "memory/Allocator.h"
  29. #include "memory/MallocAllocator.h"
  30. #include "exception/Jmp.h"
  31. #include "exception/Except.h"
  32. #include "util/events/EventBase.h"
  33. #include "util/events/Pipe.h"
  34. #include "util/events/Process.h"
  35. #include "util/platform/libc/strlen.h"
  36. #include "util/Bits.h"
  37. #include "util/Assert.h"
  38. #include "util/Hex.h"
  39. #include "util/log/WriterLog.h"
  40. #include "util/Security.h"
  41. #include "wire/Message.h"
  42. #include <unistd.h>
  43. #include <stdint.h>
  44. #include <stdlib.h>
  45. static void initCore(char* coreBinaryPath,
  46. String* corePipeName,
  47. struct EventBase* base,
  48. struct Allocator* alloc,
  49. struct Except* eh)
  50. {
  51. char* args[] = { "core", corePipeName->bytes, NULL };
  52. FILE* file;
  53. if ((file = fopen(coreBinaryPath, "r")) != NULL) {
  54. fclose(file);
  55. } else {
  56. Except_raise(eh, -1, "Can't open core executable [%s] for reading.", coreBinaryPath);
  57. }
  58. if (Process_spawn(coreBinaryPath, args, base, alloc)) {
  59. Except_raise(eh, -1, "Failed to spawn core process.");
  60. }
  61. }
  62. static void sendConfToCore(struct Interface* toCoreInterface,
  63. struct Allocator* tempAlloc,
  64. Dict* config,
  65. struct Except* eh,
  66. struct Log* logger)
  67. {
  68. #define CONFIG_BUFF_SIZE 1024
  69. uint8_t buff[CONFIG_BUFF_SIZE + 32] = {0};
  70. uint8_t* start = buff + 32;
  71. struct Writer* writer = ArrayWriter_new(start, CONFIG_BUFF_SIZE - 33, tempAlloc);
  72. if (StandardBencSerializer_get()->serializeDictionary(writer, config)) {
  73. Except_raise(eh, -1, "Failed to serialize pre-configuration for core.");
  74. }
  75. struct Message* m = &(struct Message) {
  76. .bytes = start,
  77. .length = writer->bytesWritten,
  78. .padding = 32
  79. };
  80. m = Message_clone(m, tempAlloc);
  81. Log_keys(logger, "Sent [%d] bytes to core [%s].", m->length, m->bytes);
  82. toCoreInterface->sendMessage(m, toCoreInterface);
  83. }
  84. static void setUser(char* user, struct Log* logger, struct Except* eh)
  85. {
  86. struct Jmp jmp;
  87. Jmp_try(jmp) {
  88. Security_setUser(user, logger, &jmp.handler);
  89. } Jmp_catch {
  90. if (jmp.code == Security_setUser_PERMISSION) {
  91. return;
  92. }
  93. Except_raise(eh, jmp.code, "%s", jmp.message);
  94. }
  95. }
  96. static struct Pipe* getClientPipe(int argc,
  97. char** argv,
  98. struct EventBase* base,
  99. struct Except* eh,
  100. struct Allocator* alloc)
  101. {
  102. int inFromClientNo;
  103. int outToClientNo;
  104. if (argc < 4 || (inFromClientNo = atoi(argv[2])) == 0) {
  105. inFromClientNo = STDIN_FILENO;
  106. }
  107. if (argc < 4 || (outToClientNo = atoi(argv[3])) == 0) {
  108. outToClientNo = STDOUT_FILENO;
  109. }
  110. // named pipe.
  111. if (argc > 2 && inFromClientNo == STDIN_FILENO) {
  112. return Pipe_named(argv[2], base, eh, alloc);
  113. }
  114. return Pipe_forFiles(inFromClientNo, outToClientNo, base, eh, alloc);
  115. }
  116. static void coreDied(struct Pipe* p, int status)
  117. {
  118. exit(1);
  119. }
  120. static void clientDisconnected(struct Pipe* p, int status)
  121. {
  122. fprintf(stdout, "Cjdns has started up in the background\n");
  123. }
  124. /**
  125. * Input:
  126. * {
  127. * "admin": {
  128. * "core": "/path/to/core/binary",
  129. * "bind": "127.0.0.1:12345",
  130. * "pass": "12345adminsocketpassword",
  131. * "user": "setUidToThisUser"
  132. * }
  133. * }
  134. * for example:
  135. * d5:admind4:core30:./build/admin/angel/cjdns-core4:bind15:127.0.0.1:123454:pass4:abcdee
  136. *
  137. * Pre-existing core mode:
  138. * {
  139. * "admin": {
  140. * "core": {
  141. * "fromCore": 12,
  142. * "toCore": 14
  143. * },
  144. * "bind": "127.0.0.1:12345",
  145. * "pass": "12345adminsocketpassword",
  146. * "user": "setUidToThisUser"
  147. * }
  148. * }
  149. *
  150. * If "core" is a dictionary, the angel will behave as though the core is already spawned and
  151. * it will read from the core on the file descriptor given by "fromCore" and write to the file
  152. * given by "toCore".
  153. *
  154. * "user" is optional, if set the angel will setuid() that user's uid.
  155. */
  156. int AngelInit_main(int argc, char** argv)
  157. {
  158. struct Except* eh = NULL;
  159. struct Allocator* alloc = MallocAllocator_new(1<<21);
  160. struct Writer* logWriter = FileWriter_new(stdout, alloc);
  161. struct Log* logger = WriterLog_new(logWriter, alloc);
  162. struct Random* rand = Random_new(alloc, logger, eh);
  163. MallocAllocator_setCanary(alloc, (long)Random_int64(rand));
  164. struct Allocator* tempAlloc = Allocator_child(alloc);
  165. struct EventBase* eventBase = EventBase_new(alloc);
  166. struct Pipe* clientPipe = getClientPipe(argc, argv, eventBase, eh, alloc);
  167. clientPipe->logger = logger;
  168. clientPipe->onClose = clientDisconnected;
  169. Log_debug(logger, "Getting pre-configuration from client");
  170. struct Message* preConf = InterfaceWaiter_waitForData(&clientPipe->iface, eventBase, alloc, eh);
  171. Log_debug(logger, "Finished getting pre-configuration from client");
  172. struct Reader* reader = ArrayReader_new(preConf->bytes, preConf->length, tempAlloc);
  173. Dict config;
  174. if (StandardBencSerializer_get()->parseDictionary(reader, tempAlloc, &config)) {
  175. Except_raise(eh, -1, "Failed to parse configuration.");
  176. }
  177. Dict* admin = Dict_getDict(&config, String_CONST("admin"));
  178. String* core = Dict_getString(admin, String_CONST("core"));
  179. String* bind = Dict_getString(admin, String_CONST("bind"));
  180. String* pass = Dict_getString(admin, String_CONST("pass"));
  181. String* user = Dict_getString(admin, String_CONST("user"));
  182. String* corePipeName = Dict_getString(admin, String_CONST("corePipeName"));
  183. if (!bind || !pass || (!core && !corePipeName)) {
  184. Except_raise(eh, -1, "missing configuration params in preconfig. [%s]", preConf->bytes);
  185. }
  186. if (!corePipeName) {
  187. char name[32] = {0};
  188. Random_base32(rand, (uint8_t*)name, 31);
  189. corePipeName = String_new(name, tempAlloc);
  190. }
  191. struct Pipe* corePipe = Pipe_named(corePipeName->bytes, eventBase, eh, alloc);
  192. corePipe->logger = logger;
  193. corePipe->onClose = coreDied;
  194. struct Interface* coreIface = FramingInterface_new(65535, &corePipe->iface, alloc);
  195. if (core) {
  196. Log_info(logger, "Initializing core [%s]", core->bytes);
  197. initCore(core->bytes, corePipeName, eventBase, alloc, eh);
  198. }
  199. Log_debug(logger, "Sending pre-configuration to core.");
  200. sendConfToCore(coreIface, tempAlloc, &config, eh, logger);
  201. struct Message* coreResponse = InterfaceWaiter_waitForData(coreIface, eventBase, tempAlloc, eh);
  202. Interface_sendMessage(&clientPipe->iface, coreResponse);
  203. #ifdef Log_KEYS
  204. uint8_t lastChar = coreResponse->bytes[coreResponse->length-1];
  205. coreResponse->bytes[coreResponse->length-1] = 0;
  206. Log_keys(logger, "Sent [%s%c] to client.", coreResponse->bytes, lastChar);
  207. coreResponse->bytes[coreResponse->length-1] = lastChar;
  208. #endif
  209. if (user) {
  210. setUser(user->bytes, logger, eh);
  211. }
  212. Allocator_free(tempAlloc);
  213. Angel_start(coreIface, eventBase, logger, alloc);
  214. return 0;
  215. }