AngelInit.c 8.0 KB

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