1
0

Admin.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602
  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 <https://www.gnu.org/licenses/>.
  14. */
  15. #include "admin/Admin.h"
  16. #include "benc/String.h"
  17. #include "benc/Int.h"
  18. #include "benc/Dict.h"
  19. #include "benc/serialization/standard/BencMessageWriter.h"
  20. #include "benc/serialization/standard/BencMessageReader.h"
  21. #include "memory/Allocator.h"
  22. #include "util/Assert.h"
  23. #include "util/Bits.h"
  24. #include "util/Hex.h"
  25. #include "util/log/Log.h"
  26. #include "util/events/Time.h"
  27. #include "util/events/Timeout.h"
  28. #include "util/Identity.h"
  29. #include "util/platform/Sockaddr.h"
  30. #include "util/Defined.h"
  31. #include <sodium/crypto_hash_sha256.h>
  32. #include <sodium/crypto_verify_32.h>
  33. #include <stdlib.h>
  34. #include <stdio.h>
  35. static String* TYPE = String_CONST_SO("type");
  36. static String* REQUIRED = String_CONST_SO("required");
  37. static String* STRING = String_CONST_SO("String");
  38. static String* INTEGER = String_CONST_SO("Int");
  39. static String* DICT = String_CONST_SO("Dict");
  40. static String* LIST = String_CONST_SO("List");
  41. static String* TXID = String_CONST_SO("txid");
  42. /** Number of milliseconds before a session times out and outgoing messages are failed. */
  43. #define TIMEOUT_MILLISECONDS 30000
  44. /** map values for tracking time of last message by source address */
  45. struct MapValue
  46. {
  47. /** time when the last incoming message was received. */
  48. uint64_t timeOfLastMessage;
  49. /** used to allocate the memory for the key (Sockaddr) and value (this). */
  50. struct Allocator* allocator;
  51. };
  52. //////// generate time-of-last-message-by-address map
  53. #define Map_USE_HASH
  54. #define Map_USE_COMPARATOR
  55. #define Map_NAME LastMessageTimeByAddr
  56. #define Map_KEY_TYPE struct Sockaddr*
  57. #define Map_VALUE_TYPE struct MapValue*
  58. #include "util/Map.h"
  59. static inline uint32_t Map_LastMessageTimeByAddr_hash(struct Sockaddr** key)
  60. {
  61. return Sockaddr_hash(*key);
  62. }
  63. static inline int Map_LastMessageTimeByAddr_compare(struct Sockaddr** keyA, struct Sockaddr** keyB)
  64. {
  65. return Sockaddr_compare(*keyA, *keyB);
  66. }
  67. /////// end map
  68. struct Function
  69. {
  70. String* name;
  71. Admin_Function call;
  72. void* context;
  73. bool needsAuth;
  74. Dict* args;
  75. };
  76. struct Admin_pvt
  77. {
  78. struct Admin pub;
  79. struct Iface iface;
  80. struct EventBase* eventBase;
  81. struct Function* functions;
  82. int functionCount;
  83. struct Allocator* allocator;
  84. String* password;
  85. struct Log* logger;
  86. struct Map_LastMessageTimeByAddr map;
  87. /** non-null if we are currently in an admin request. */
  88. struct Message* currentRequest;
  89. /** non-zero if this session able to receive asynchronous messages. */
  90. int asyncEnabled;
  91. struct Message* tempSendMsg;
  92. Identity
  93. };
  94. static struct RTypes_Error_t* sendMessage(
  95. struct Message* message, struct Sockaddr* dest, struct Admin_pvt* admin)
  96. {
  97. // stack overflow when used with admin logger.
  98. //Log_keys(admin->logger, "sending message to angel [%s]", message->bytes);
  99. Er_assert(Message_epush(message, dest, dest->addrLen));
  100. return Iface_send(&admin->iface, message);
  101. }
  102. static struct RTypes_Error_t* sendBenc(Dict* message,
  103. struct Sockaddr* dest,
  104. struct Allocator* alloc,
  105. struct Admin_pvt* admin,
  106. int fd)
  107. {
  108. Message_reset(admin->tempSendMsg);
  109. Er_assert(BencMessageWriter_write(message, admin->tempSendMsg));
  110. struct Message* msg = Message_new(0, Message_getLength(admin->tempSendMsg) + 32, alloc);
  111. Er_assert(Message_epush(msg, admin->tempSendMsg->msgbytes, Message_getLength(admin->tempSendMsg)));
  112. Message_setAssociatedFd(msg, fd);
  113. return sendMessage(msg, dest, admin);
  114. }
  115. /**
  116. * If no incoming data has been sent by this address in TIMEOUT_MILLISECONDS
  117. * then Admin_sendMessage() should fail so that it doesn't endlessly send
  118. * udp packets into outer space after a logging client disconnects.
  119. */
  120. static int checkAddress(struct Admin_pvt* admin, int index, uint64_t now)
  121. {
  122. uint64_t diff = now - admin->map.values[index]->timeOfLastMessage;
  123. // check for backwards time
  124. if (diff > TIMEOUT_MILLISECONDS && diff < ((uint64_t)INT64_MAX)) {
  125. Allocator_free(admin->map.values[index]->allocator);
  126. Map_LastMessageTimeByAddr_remove(index, &admin->map);
  127. return -1;
  128. }
  129. return 0;
  130. }
  131. static void clearExpiredAddresses(void* vAdmin)
  132. {
  133. struct Admin_pvt* admin = Identity_check((struct Admin_pvt*) vAdmin);
  134. uint64_t now = Time_currentTimeMilliseconds();
  135. int count = 0;
  136. for (int i = admin->map.count - 1; i >= 0; i--) {
  137. if (checkAddress(admin, i, now)) {
  138. count++;
  139. }
  140. }
  141. Log_debug(admin->logger, "Cleared [%d] expired sessions", count);
  142. }
  143. static int sendMessage0(Dict* message, String* txid, struct Admin* adminPub, int fd)
  144. {
  145. struct Admin_pvt* admin = Identity_check((struct Admin_pvt*) adminPub);
  146. if (!admin) {
  147. return 0;
  148. }
  149. Assert_true(txid && txid->len >= sizeof(struct Sockaddr));
  150. uint16_t addrLen = 0;
  151. Bits_memcpy(&addrLen, txid->bytes, 2);
  152. Assert_true(txid->len >= addrLen);
  153. struct Allocator* alloc = NULL;
  154. if (admin->currentRequest) {
  155. alloc = Message_getAlloc(admin->currentRequest);
  156. } else {
  157. alloc = Allocator_child(admin->allocator);
  158. }
  159. struct Sockaddr* addr = Sockaddr_clone((struct Sockaddr*) txid->bytes, alloc);
  160. // if this is an async call, check if we've got any input from that client.
  161. // if the client is nresponsive then fail the call so logs don't get sent
  162. // out forever after a disconnection.
  163. if (!admin->currentRequest) {
  164. int index = Map_LastMessageTimeByAddr_indexForKey(&addr, &admin->map);
  165. uint64_t now = Time_currentTimeMilliseconds();
  166. if (index < 0 || checkAddress(admin, index, now)) {
  167. return Admin_sendMessage_CHANNEL_CLOSED;
  168. }
  169. }
  170. // Bounce back the user-supplied txid.
  171. if (txid->len > addr->addrLen) {
  172. String* userTxid =
  173. String_newBinary(&txid->bytes[addr->addrLen], txid->len - addr->addrLen, alloc);
  174. Dict_putString(message, TXID, userTxid, alloc);
  175. }
  176. sendBenc(message, addr, alloc, admin, fd);
  177. Dict_remove(message, TXID);
  178. if (!admin->currentRequest) {
  179. Allocator_free(alloc);
  180. }
  181. return 0;
  182. }
  183. int Admin_sendMessage(Dict* message, String* txid, struct Admin* adminPub)
  184. {
  185. return sendMessage0(message, txid, adminPub, -1);
  186. }
  187. static inline bool authValid(Dict* message, struct Message* messageBytes, struct Admin_pvt* admin)
  188. {
  189. String* cookieStr = Dict_getStringC(message, "cookie");
  190. uint32_t cookie = (cookieStr != NULL) ? strtoll(cookieStr->bytes, NULL, 10) : 0;
  191. if (!cookie) {
  192. int64_t* cookieInt = Dict_getIntC(message, "cookie");
  193. cookie = (cookieInt) ? *cookieInt : 0;
  194. }
  195. uint64_t nowSecs = Time_currentTimeSeconds();
  196. String* submittedHash = Dict_getStringC(message, "hash");
  197. if (cookie > nowSecs || cookie < nowSecs - 20 || !submittedHash || submittedHash->len != 64) {
  198. return false;
  199. }
  200. uint8_t* hashPtr = CString_strstr(messageBytes->msgbytes, submittedHash->bytes);
  201. if (!hashPtr || !admin->password) {
  202. return false;
  203. }
  204. uint8_t passAndCookie[64];
  205. snprintf((char*) passAndCookie, 64, "%s%u", admin->password->bytes, cookie);
  206. uint8_t hash[32];
  207. crypto_hash_sha256(hash, passAndCookie, CString_strlen((char*) passAndCookie));
  208. Hex_encode(hashPtr, 64, hash, 32);
  209. crypto_hash_sha256(hash, messageBytes->msgbytes, Message_getLength(messageBytes));
  210. Hex_encode(hashPtr, 64, hash, 32);
  211. int res = crypto_verify_32(hashPtr, submittedHash->bytes);
  212. res |= crypto_verify_32(hashPtr + 32, submittedHash->bytes + 32);
  213. return res == 0;
  214. }
  215. static bool checkArgs(Dict* args,
  216. struct Function* func,
  217. String* txid,
  218. struct Allocator* requestAlloc,
  219. struct Admin_pvt* admin)
  220. {
  221. struct Dict_Entry* entry = *func->args;
  222. String* error = NULL;
  223. while (entry != NULL) {
  224. String* key = (String*) entry->key;
  225. Assert_ifParanoid(entry->val->type == Object_DICT);
  226. Dict* value = entry->val->as.dictionary;
  227. entry = entry->next;
  228. if (*Dict_getIntC(value, "required") == 0) {
  229. continue;
  230. }
  231. String* type = Dict_getStringC(value, "type");
  232. if ((type == STRING && !Dict_getString(args, key))
  233. || (type == DICT && !Dict_getDict(args, key))
  234. || (type == INTEGER && !Dict_getInt(args, key))
  235. || (type == LIST && !Dict_getList(args, key)))
  236. {
  237. error = String_printf(requestAlloc,
  238. "Entry [%s] is required and must be of type [%s]",
  239. key->bytes,
  240. type->bytes);
  241. break;
  242. }
  243. }
  244. if (error) {
  245. Dict d = Dict_CONST(String_CONST("error"), String_OBJ(error), NULL);
  246. Admin_sendMessage(&d, txid, &admin->pub);
  247. }
  248. return !error;
  249. }
  250. static void asyncEnabled(Dict* args, void* vAdmin, String* txid, struct Allocator* requestAlloc)
  251. {
  252. struct Admin_pvt* admin = Identity_check((struct Admin_pvt*) vAdmin);
  253. int64_t enabled = admin->asyncEnabled;
  254. Dict d = Dict_CONST(String_CONST("asyncEnabled"), Int_OBJ(enabled), NULL);
  255. Admin_sendMessage(&d, txid, &admin->pub);
  256. }
  257. #define ENTRIES_PER_PAGE 8
  258. static void availableFunctions(Dict* args, void* vAdmin, String* txid, struct Allocator* tempAlloc)
  259. {
  260. struct Admin_pvt* admin = Identity_check((struct Admin_pvt*) vAdmin);
  261. int64_t* page = Dict_getIntC(args, "page");
  262. uint32_t i = (page) ? *page * ENTRIES_PER_PAGE : 0;
  263. Dict* d = Dict_new(tempAlloc);
  264. Dict* functions = Dict_new(tempAlloc);
  265. int count = 0;
  266. for (; i < (uint32_t)admin->functionCount && count++ < ENTRIES_PER_PAGE; i++) {
  267. Dict_putDict(functions, admin->functions[i].name, admin->functions[i].args, tempAlloc);
  268. }
  269. if (count >= ENTRIES_PER_PAGE) {
  270. Dict_putIntC(d, "more", 1, tempAlloc);
  271. }
  272. Dict_putDictC(d, "availableFunctions", functions, tempAlloc);
  273. Admin_sendMessage(d, txid, &admin->pub);
  274. }
  275. static void handleRequest(Dict* messageDict,
  276. struct Message* message,
  277. struct Sockaddr* src,
  278. struct Allocator* allocator,
  279. struct Admin_pvt* admin)
  280. {
  281. String* query = Dict_getStringC(messageDict, "q");
  282. if (!query) {
  283. Log_info(admin->logger, "Got a non-query from admin interface");
  284. return;
  285. }
  286. // txid becomes the user supplied txid combined with the channel num.
  287. String* userTxid = Dict_getString(messageDict, TXID);
  288. uint32_t txidlen = ((userTxid) ? userTxid->len : 0) + src->addrLen;
  289. String* txid = String_newBinary(NULL, txidlen, allocator);
  290. Bits_memcpy(txid->bytes, src, src->addrLen);
  291. if (userTxid) {
  292. Bits_memcpy(txid->bytes + src->addrLen, userTxid->bytes, userTxid->len);
  293. }
  294. // If they're asking for a cookie then lets give them one.
  295. String* cookie = String_CONST("cookie");
  296. if (String_equals(query, cookie)) {
  297. //Log_debug(admin->logger, "Got a request for a cookie");
  298. Dict* d = Dict_new(allocator);
  299. char bytes[32];
  300. snprintf(bytes, 32, "%u", (uint32_t) Time_currentTimeSeconds());
  301. String* theCookie = &(String) { .len = CString_strlen(bytes), .bytes = bytes };
  302. Dict_putString(d, cookie, theCookie, allocator);
  303. Admin_sendMessage(d, txid, &admin->pub);
  304. return;
  305. }
  306. // If this is a permitted query, make sure the cookie is right.
  307. String* auth = String_CONST("auth");
  308. bool authed = false;
  309. if (String_equals(query, auth)) {
  310. if (!authValid(messageDict, message, admin)) {
  311. Dict* d = Dict_new(allocator);
  312. Dict_putStringCC(d, "error", "Auth failed.", allocator);
  313. Admin_sendMessage(d, txid, &admin->pub);
  314. return;
  315. }
  316. query = Dict_getStringC(messageDict, "aq");
  317. authed = true;
  318. }
  319. if (String_equals(admin->password, String_CONST("NONE"))) {
  320. // If there's no password then we'll consider everything to be authed
  321. authed = true;
  322. }
  323. // Then sent a valid authed query, lets track their address so they can receive
  324. // asynchronous messages.
  325. int index = Map_LastMessageTimeByAddr_indexForKey(&src, &admin->map);
  326. uint64_t now = Time_currentTimeMilliseconds();
  327. admin->asyncEnabled = 1;
  328. if (index >= 0) {
  329. admin->map.values[index]->timeOfLastMessage = now;
  330. } else if (authed) {
  331. struct Allocator* entryAlloc = Allocator_child(admin->allocator);
  332. struct MapValue* mv = Allocator_calloc(entryAlloc, sizeof(struct MapValue), 1);
  333. mv->timeOfLastMessage = now;
  334. mv->allocator = entryAlloc;
  335. struct Sockaddr* storedAddr = Sockaddr_clone(src, entryAlloc);
  336. Map_LastMessageTimeByAddr_put(&storedAddr, &mv, &admin->map);
  337. } else {
  338. admin->asyncEnabled = 0;
  339. }
  340. Dict* args = Dict_getDictC(messageDict, "args");
  341. bool noFunctionsCalled = true;
  342. for (int i = 0; i < admin->functionCount; i++) {
  343. if (String_equals(query, admin->functions[i].name)
  344. && (authed || !admin->functions[i].needsAuth))
  345. {
  346. if (checkArgs(args, &admin->functions[i], txid, Message_getAlloc(message), admin)) {
  347. admin->functions[i].call(args, admin->functions[i].context, txid, Message_getAlloc(message));
  348. }
  349. noFunctionsCalled = false;
  350. }
  351. }
  352. if (noFunctionsCalled) {
  353. Dict d = Dict_CONST(
  354. String_CONST("error"),
  355. String_OBJ(String_CONST("No functions matched your request, "
  356. "try Admin_availableFunctions()")),
  357. NULL
  358. );
  359. Admin_sendMessage(&d, txid, &admin->pub);
  360. }
  361. return;
  362. }
  363. static void handleMessage(struct Message* message,
  364. struct Sockaddr* src,
  365. struct Allocator* alloc,
  366. struct Admin_pvt* admin)
  367. {
  368. if (Defined(Log_KEYS)) {
  369. uint8_t lastChar = message->msgbytes[Message_getLength(message) - 1];
  370. message->msgbytes[Message_getLength(message) - 1] = '\0';
  371. Log_keys(admin->logger, "Got message from [%s] [%s]",
  372. Sockaddr_print(src, alloc), message->msgbytes);
  373. message->msgbytes[Message_getLength(message) - 1] = lastChar;
  374. }
  375. // handle non empty message data
  376. if (Message_getLength(message) > Admin_MAX_REQUEST_SIZE) {
  377. #define TOO_BIG "d5:error16:Request too big.e"
  378. #define TOO_BIG_STRLEN (sizeof(TOO_BIG) - 1)
  379. Bits_memcpy(message->msgbytes, TOO_BIG, TOO_BIG_STRLEN);
  380. Er_assert(Message_truncate(message, TOO_BIG_STRLEN));
  381. sendMessage(message, src, admin);
  382. return;
  383. }
  384. int origMessageLen = Message_getLength(message);
  385. Dict* messageDict = NULL;
  386. const char* err = BencMessageReader_readNoExcept(message, alloc, &messageDict);
  387. if (err) {
  388. Log_warn(admin->logger,
  389. "Unparsable data from [%s] content: [%s] error: [%s]",
  390. Sockaddr_print(src, alloc),
  391. Hex_print(message->msgbytes, Message_getLength(message), alloc),
  392. err);
  393. return;
  394. }
  395. if (Message_getLength(message)) {
  396. Log_warn(admin->logger,
  397. "Message from [%s] contained garbage after byte [%d] content: [%s]",
  398. Sockaddr_print(src, alloc), Message_getLength(message), message->msgbytes);
  399. return;
  400. }
  401. // put the data back in the front of the message because it is used by the auth checker.
  402. Er_assert(Message_eshift(message, origMessageLen));
  403. handleRequest(messageDict, message, src, alloc, admin);
  404. }
  405. static Iface_DEFUN receiveMessage(struct Message* message, struct Iface* iface)
  406. {
  407. struct Admin_pvt* admin = Identity_containerOf(iface, struct Admin_pvt, iface);
  408. struct Allocator* alloc = Allocator_child(admin->allocator);
  409. struct Sockaddr* addrPtr = Er_assert(AddrIface_popAddr(message));
  410. admin->currentRequest = message;
  411. handleMessage(message, Sockaddr_clone(addrPtr, alloc), alloc, admin);
  412. admin->currentRequest = NULL;
  413. Allocator_free(alloc);
  414. // We don't return errors here because the caller can't make use of them
  415. // instead we reply with anything which went wrong.
  416. return NULL;
  417. }
  418. void Admin_registerFunctionWithArgCount(char* name,
  419. Admin_Function callback,
  420. void* callbackContext,
  421. bool needsAuth,
  422. struct Admin_FunctionArg* arguments,
  423. int argCount,
  424. struct Admin* adminPub)
  425. {
  426. struct Admin_pvt* admin = Identity_check((struct Admin_pvt*) adminPub);
  427. String* str = String_new(name, admin->allocator);
  428. admin->functions =
  429. Allocator_realloc(admin->allocator,
  430. admin->functions,
  431. sizeof(struct Function) * (admin->functionCount + 1));
  432. struct Function* fu = &admin->functions[admin->functionCount];
  433. admin->functionCount++;
  434. fu->name = str;
  435. fu->call = callback;
  436. fu->context = callbackContext;
  437. fu->needsAuth = needsAuth;
  438. fu->args = Dict_new(admin->allocator);
  439. for (int i = 0; arguments && i < argCount; i++) {
  440. // "type" must be one of: [ "String", "Int", "Dict", "List" ]
  441. String* type = NULL;
  442. if (!CString_strcmp(arguments[i].type, STRING->bytes)) {
  443. type = STRING;
  444. } else if (!CString_strcmp(arguments[i].type, INTEGER->bytes)) {
  445. type = INTEGER;
  446. } else if (!CString_strcmp(arguments[i].type, DICT->bytes)) {
  447. type = DICT;
  448. } else if (!CString_strcmp(arguments[i].type, LIST->bytes)) {
  449. type = LIST;
  450. } else {
  451. abort();
  452. }
  453. Dict* arg = Dict_new(admin->allocator);
  454. Dict_putString(arg, TYPE, type, admin->allocator);
  455. Dict_putInt(arg, REQUIRED, arguments[i].required, admin->allocator);
  456. String* name = String_new(arguments[i].name, admin->allocator);
  457. Dict_putDict(fu->args, name, arg, admin->allocator);
  458. }
  459. }
  460. static void importFd(Dict* args, void* vAdmin, String* txid, struct Allocator* requestAlloc)
  461. {
  462. struct Admin_pvt* admin = Identity_check((struct Admin_pvt*) vAdmin);
  463. int fd = Message_getAssociatedFd(admin->currentRequest);
  464. Dict* res = Dict_new(requestAlloc);
  465. char* error = "none";
  466. if (fd < 0) {
  467. if (Defined(win32)) {
  468. error = "Admin_importFd() does not support win32";
  469. } else {
  470. error = "file descriptor was not attached to message";
  471. }
  472. } else {
  473. Dict_putIntC(res, "fd", fd, requestAlloc);
  474. }
  475. Dict_putStringCC(res, "error", error, requestAlloc);
  476. Admin_sendMessage(res, txid, &admin->pub);
  477. }
  478. static void exportFd(Dict* args, void* vAdmin, String* txid, struct Allocator* requestAlloc)
  479. {
  480. struct Admin_pvt* admin = Identity_check((struct Admin_pvt*) vAdmin);
  481. int64_t* fd_p = Dict_getIntC(args, "fd");
  482. if (!fd_p || *fd_p < 0) {
  483. Dict* res = Dict_new(requestAlloc);
  484. Dict_putStringCC(res, "error", "invalid fd", requestAlloc);
  485. Admin_sendMessage(res, txid, &admin->pub);
  486. return;
  487. }
  488. int fd = *fd_p;
  489. Dict* res = Dict_new(requestAlloc);
  490. char* error = "none";
  491. if (fd < 0) {
  492. if (Defined(win32)) {
  493. error = "Admin_exportFd() does not support win32";
  494. } else {
  495. error = "file descriptor was not attached to message";
  496. }
  497. }
  498. Dict_putStringCC(res, "error", error, requestAlloc);
  499. sendMessage0(res, txid, &admin->pub, fd);
  500. }
  501. struct Admin* Admin_new(struct AddrIface* ai,
  502. struct Log* logger,
  503. struct EventBase* eventBase,
  504. String* password)
  505. {
  506. struct Allocator* alloc = ai->alloc;
  507. struct Admin_pvt* admin = Allocator_calloc(alloc, sizeof(struct Admin_pvt), 1);
  508. Identity_set(admin);
  509. admin->allocator = alloc;
  510. admin->logger = logger;
  511. admin->eventBase = eventBase;
  512. admin->map.allocator = alloc;
  513. admin->iface.send = receiveMessage;
  514. Iface_plumb(&admin->iface, &ai->iface);
  515. admin->tempSendMsg = Message_new(0, Admin_MAX_RESPONSE_SIZE, alloc);
  516. admin->password = String_clone(password, alloc);
  517. Timeout_setInterval(clearExpiredAddresses, admin, TIMEOUT_MILLISECONDS * 3, eventBase, alloc);
  518. Admin_registerFunction("Admin_asyncEnabled", asyncEnabled, admin, false, NULL, &admin->pub);
  519. Admin_registerFunction("Admin_availableFunctions", availableFunctions, admin, false,
  520. ((struct Admin_FunctionArg[]) {
  521. { .name = "page", .required = 0, .type = "Int" }
  522. }), &admin->pub);
  523. Admin_registerFunction("Admin_importFd", importFd, admin, true, NULL, &admin->pub);
  524. Admin_registerFunction("Admin_exportFd", exportFd, admin, true,
  525. ((struct Admin_FunctionArg[]) {
  526. { .name = "fd", .required = 1, .type = "Int" }
  527. }), &admin->pub);
  528. return &admin->pub;
  529. }