Admin.c 19 KB

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