AngelInit.c 8.0 KB

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