AngelInit.c 7.0 KB

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