Admin.c 21 KB

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