AngelInit.c 7.3 KB

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