Admin.c 19 KB

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