gnunet-namestore-fcfsd.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2012-2014 GNUnet e.V.
  4. GNUnet is free software: you can redistribute it and/or modify it
  5. under the terms of the GNU Affero General Public License as published
  6. by the Free Software Foundation, either version 3 of the License,
  7. or (at your option) any later version.
  8. GNUnet is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Affero General Public License for more details.
  12. You should have received a copy of the GNU Affero General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. SPDX-License-Identifier: AGPL3.0-or-later
  15. */
  16. /**
  17. * @file gnunet-namestore-fcfsd.c
  18. * @brief HTTP daemon that offers first-come-first-serve GNS domain registration
  19. * @author Christian Grothoff
  20. *
  21. * TODO:
  22. * - need to track active zone info requests so we can cancel them
  23. * during shutdown, right?
  24. * - the code currently contains a 'race' between checking that the
  25. * domain name is available and allocating it to the new public key
  26. * (should this race be solved by namestore or by fcfsd?)
  27. * - nicer error reporting to browser
  28. */
  29. #include "platform.h"
  30. #include <microhttpd.h>
  31. #include "gnunet_util_lib.h"
  32. #include "gnunet_identity_service.h"
  33. #include "gnunet_gnsrecord_lib.h"
  34. #include "gnunet_namestore_service.h"
  35. /**
  36. * Invalid method page.
  37. */
  38. #define METHOD_ERROR "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\"><html lang=\"en\" xmlns=\"http://www.w3.org/1999/xhtml\"><html><head><title>Illegal request</title></head><body>Go away.</body></html>"
  39. /**
  40. * Front page. (/)
  41. */
  42. #define MAIN_PAGE "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\"><html lang=\"en\" xmlns=\"http://www.w3.org/1999/xhtml\"><html><head><title>GNUnet FCFS Authority Name Registration Service</title></head><body><form action=\"S\" method=\"post\">What is your desired domain name? (at most 63 lowercase characters, no dots allowed.) <input type=\"text\" name=\"domain\" /> <p> What is your public key? (Copy from gnunet-setup.) <input type=\"text\" name=\"pkey\" /> <input type=\"submit\" value=\"Next\" /><br/><a href=./Zoneinfo> List of all registered names </a></body></html>"
  43. /**
  44. * Second page (/S)
  45. */
  46. #define SUBMIT_PAGE "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\"><html lang=\"en\" xmlns=\"http://www.w3.org/1999/xhtml\"><html><head><title>%s</title></head><body>%s</body></html>"
  47. /**
  48. * Fcfs zoneinfo page (/Zoneinfo)
  49. */
  50. #define ZONEINFO_PAGE "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\"><html lang=\"en\" xmlns=\"http://www.w3.org/1999/xhtml\"><html><head><title>FCFS Zoneinfo</title></head><body><h1> FCFS Zoneinfo </h1><table border=\"1\"><th>name</th><th>PKEY</th>%s</table></body></html>"
  51. #define FCFS_ZONEINFO_URL "/Zoneinfo"
  52. /**
  53. * Mime type for HTML pages.
  54. */
  55. #define MIME_HTML "text/html"
  56. /**
  57. * Name of our cookie.
  58. */
  59. #define COOKIE_NAME "namestore-fcfsd"
  60. #define DEFAULT_ZONEINFO_BUFSIZE 2048
  61. /**
  62. * Phases a request goes through.
  63. */
  64. enum Phase
  65. {
  66. /**
  67. * Start phase (parsing POST, checking).
  68. */
  69. RP_START = 0,
  70. /**
  71. * Lookup to see if the domain name is taken.
  72. */
  73. RP_LOOKUP,
  74. /**
  75. * Storing of the record.
  76. */
  77. RP_PUT,
  78. /**
  79. * We're done with success.
  80. */
  81. RP_SUCCESS,
  82. /**
  83. * Send failure message.
  84. */
  85. RP_FAIL
  86. };
  87. /**
  88. * Data kept per request.
  89. */
  90. struct Request
  91. {
  92. /**
  93. * Associated session.
  94. */
  95. // FIXME: struct Session *session;
  96. /**
  97. * Post processor handling form data (IF this is
  98. * a POST request).
  99. */
  100. struct MHD_PostProcessor *pp;
  101. /**
  102. * MHD Connection
  103. */
  104. struct MHD_Connection *con;
  105. /**
  106. * URL to serve in response to this POST (if this request
  107. * was a 'POST')
  108. */
  109. const char *post_url;
  110. /**
  111. * Active request with the namestore.
  112. */
  113. struct GNUNET_NAMESTORE_QueueEntry *qe;
  114. /**
  115. * Active lookup iterator
  116. * TODO: deprecate or fix lookup by label and use above member
  117. */
  118. struct GNUNET_NAMESTORE_ZoneIterator *lookup_it;
  119. /**
  120. * Active iteration with the namestore.
  121. */
  122. struct GNUNET_NAMESTORE_ZoneIterator *zi;
  123. /**
  124. * Current processing phase.
  125. */
  126. enum Phase phase;
  127. /**
  128. * Domain name submitted via form.
  129. */
  130. char domain_name[64];
  131. /**
  132. * Public key submitted via form.
  133. */
  134. char public_key[128];
  135. struct GNUNET_CRYPTO_EcdsaPublicKey pub;
  136. };
  137. /**
  138. * Zoneinfo request
  139. */
  140. struct ZoneinfoRequest
  141. {
  142. /**
  143. * List iterator
  144. */
  145. struct GNUNET_NAMESTORE_ZoneIterator *list_it;
  146. /**
  147. * Buffer
  148. */
  149. char* zoneinfo;
  150. /**
  151. * Buffer length
  152. */
  153. size_t buf_len;
  154. /**
  155. * Buffer write offset
  156. */
  157. size_t write_offset;
  158. };
  159. /**
  160. * MHD deamon reference.
  161. */
  162. static struct MHD_Daemon *httpd;
  163. /**
  164. * Main HTTP task.
  165. */
  166. static struct GNUNET_SCHEDULER_Task * httpd_task;
  167. /**
  168. * Handle to the namestore.
  169. */
  170. static struct GNUNET_NAMESTORE_Handle *ns;
  171. /**
  172. * Private key for the fcfsd zone.
  173. */
  174. static struct GNUNET_CRYPTO_EcdsaPrivateKey fcfs_zone_pkey;
  175. /**
  176. * Connection to identity service.
  177. */
  178. static struct GNUNET_IDENTITY_Handle *identity;
  179. /**
  180. * Zoneinfo page we currently use.
  181. */
  182. static struct MHD_Response *info_page;
  183. /**
  184. * Task that runs #update_zoneinfo_page peridicially.
  185. */
  186. static struct GNUNET_SCHEDULER_Task *uzp_task;
  187. /**
  188. * Request for our ego.
  189. */
  190. static struct GNUNET_IDENTITY_Operation *id_op;
  191. /**
  192. * Port we use for the HTTP server.
  193. */
  194. static unsigned long long port;
  195. /**
  196. * Name of the zone we manage.
  197. */
  198. static char *zone;
  199. /**
  200. * Task run whenever HTTP server operations are pending.
  201. *
  202. * @param cls unused
  203. */
  204. static void
  205. do_httpd (void *cls);
  206. /**
  207. * Schedule task to run MHD server now.
  208. */
  209. static void
  210. run_httpd_now ()
  211. {
  212. if (NULL != httpd_task)
  213. {
  214. GNUNET_SCHEDULER_cancel (httpd_task);
  215. httpd_task = NULL;
  216. }
  217. httpd_task = GNUNET_SCHEDULER_add_now (&do_httpd, NULL);
  218. }
  219. /**
  220. * Create fresh version of zone information.
  221. */
  222. static void
  223. update_zoneinfo_page (void *cls);
  224. /**
  225. * Function called on error in zone iteration.
  226. */
  227. static void
  228. zone_iteration_error (void *cls)
  229. {
  230. struct ZoneinfoRequest *zr = cls;
  231. zr->list_it = NULL;
  232. GNUNET_free (zr->zoneinfo);
  233. GNUNET_SCHEDULER_cancel (uzp_task);
  234. uzp_task = GNUNET_SCHEDULER_add_now (&update_zoneinfo_page,
  235. NULL);
  236. }
  237. /**
  238. * Function called once the zone iteration is done.
  239. */
  240. static void
  241. zone_iteration_end (void *cls)
  242. {
  243. struct ZoneinfoRequest *zr = cls;
  244. struct MHD_Response *response;
  245. char* full_page;
  246. zr->list_it = NULL;
  247. /* return static form */
  248. GNUNET_asprintf (&full_page,
  249. ZONEINFO_PAGE,
  250. zr->zoneinfo,
  251. zr->zoneinfo);
  252. response = MHD_create_response_from_buffer (strlen (full_page),
  253. (void *) full_page,
  254. MHD_RESPMEM_MUST_FREE);
  255. MHD_add_response_header (response,
  256. MHD_HTTP_HEADER_CONTENT_TYPE,
  257. MIME_HTML);
  258. MHD_destroy_response (info_page);
  259. info_page = response;
  260. GNUNET_free (zr->zoneinfo);
  261. }
  262. /**
  263. * Process a record that was stored in the namestore, adding
  264. * the information to the HTML.
  265. *
  266. * @param cls closure with the `struct ZoneinfoRequest *`
  267. * @param zone_key private key of the zone; NULL on disconnect
  268. * @param name label of the records; NULL on disconnect
  269. * @param rd_len number of entries in @a rd array, 0 if label was deleted
  270. * @param rd array of records with data to store
  271. */
  272. static void
  273. iterate_cb (void *cls,
  274. const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key,
  275. const char *name,
  276. unsigned int rd_len,
  277. const struct GNUNET_GNSRECORD_Data *rd)
  278. {
  279. struct ZoneinfoRequest *zr = cls;
  280. size_t bytes_free;
  281. char* pkey;
  282. char* new_buf;
  283. (void) zone_key;
  284. if (1 != rd_len)
  285. {
  286. GNUNET_NAMESTORE_zone_iterator_next (zr->list_it,
  287. 1);
  288. return;
  289. }
  290. if (GNUNET_GNSRECORD_TYPE_PKEY != rd->record_type)
  291. {
  292. GNUNET_NAMESTORE_zone_iterator_next (zr->list_it,
  293. 1);
  294. return;
  295. }
  296. bytes_free = zr->buf_len - zr->write_offset;
  297. pkey = GNUNET_GNSRECORD_value_to_string (rd->record_type,
  298. rd->data,
  299. rd->data_size);
  300. if (NULL == pkey)
  301. {
  302. GNUNET_break (0);
  303. GNUNET_NAMESTORE_zone_iterator_next (zr->list_it,
  304. 1);
  305. return;
  306. }
  307. if (bytes_free < (strlen (name) + strlen (pkey) + 40))
  308. {
  309. new_buf = GNUNET_malloc (zr->buf_len * 2);
  310. GNUNET_memcpy (new_buf, zr->zoneinfo, zr->write_offset);
  311. GNUNET_free (zr->zoneinfo);
  312. zr->zoneinfo = new_buf;
  313. zr->buf_len *= 2;
  314. }
  315. sprintf (zr->zoneinfo + zr->write_offset,
  316. "<tr><td>%s</td><td>%s</td></tr>",
  317. name,
  318. pkey);
  319. zr->write_offset = strlen (zr->zoneinfo);
  320. GNUNET_NAMESTORE_zone_iterator_next (zr->list_it,
  321. 1);
  322. GNUNET_free (pkey);
  323. }
  324. /**
  325. * Handler that returns FCFS zoneinfo page.
  326. *
  327. * @param connection connection to use
  328. */
  329. static int
  330. serve_zoneinfo_page (struct MHD_Connection *connection)
  331. {
  332. return MHD_queue_response (connection,
  333. MHD_HTTP_OK,
  334. info_page);
  335. }
  336. /**
  337. * Create fresh version of zone information.
  338. */
  339. static void
  340. update_zoneinfo_page (void *cls)
  341. {
  342. static struct ZoneinfoRequest zr;
  343. (void) cls;
  344. uzp_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
  345. &update_zoneinfo_page,
  346. NULL);
  347. if (NULL != zr.list_it)
  348. return;
  349. zr.zoneinfo = GNUNET_malloc (DEFAULT_ZONEINFO_BUFSIZE);
  350. zr.buf_len = DEFAULT_ZONEINFO_BUFSIZE;
  351. zr.write_offset = 0;
  352. zr.list_it = GNUNET_NAMESTORE_zone_iteration_start (ns,
  353. &fcfs_zone_pkey,
  354. &zone_iteration_error,
  355. &zr,
  356. &iterate_cb,
  357. &zr,
  358. &zone_iteration_end,
  359. &zr);
  360. }
  361. /**
  362. * Handler that returns a simple static HTTP page.
  363. *
  364. * @param connection connection to use
  365. * @return #MHD_YES on success
  366. */
  367. static int
  368. serve_main_page (struct MHD_Connection *connection)
  369. {
  370. int ret;
  371. struct MHD_Response *response;
  372. /* return static form */
  373. response = MHD_create_response_from_buffer (strlen (MAIN_PAGE),
  374. (void *) MAIN_PAGE,
  375. MHD_RESPMEM_PERSISTENT);
  376. MHD_add_response_header (response,
  377. MHD_HTTP_HEADER_CONTENT_TYPE,
  378. MIME_HTML);
  379. ret = MHD_queue_response (connection,
  380. MHD_HTTP_OK,
  381. response);
  382. MHD_destroy_response (response);
  383. return ret;
  384. }
  385. /**
  386. * Send the 'SUBMIT_PAGE'.
  387. *
  388. * @param info information string to send to the user
  389. * @param request request information
  390. * @param connection connection to use
  391. */
  392. static int
  393. fill_s_reply (const char *info,
  394. struct Request *request,
  395. struct MHD_Connection *connection)
  396. {
  397. int ret;
  398. char *reply;
  399. struct MHD_Response *response;
  400. (void) request;
  401. GNUNET_asprintf (&reply,
  402. SUBMIT_PAGE,
  403. info,
  404. info);
  405. /* return static form */
  406. response = MHD_create_response_from_buffer (strlen (reply),
  407. (void *) reply,
  408. MHD_RESPMEM_MUST_FREE);
  409. MHD_add_response_header (response,
  410. MHD_HTTP_HEADER_CONTENT_TYPE,
  411. MIME_HTML);
  412. ret = MHD_queue_response (connection,
  413. MHD_HTTP_OK,
  414. response);
  415. MHD_destroy_response (response);
  416. return ret;
  417. }
  418. /**
  419. * Iterator over key-value pairs where the value
  420. * maybe made available in increments and/or may
  421. * not be zero-terminated. Used for processing
  422. * POST data.
  423. *
  424. * @param cls user-specified closure
  425. * @param kind type of the value
  426. * @param key 0-terminated key for the value
  427. * @param filename name of the uploaded file, NULL if not known
  428. * @param content_type mime-type of the data, NULL if not known
  429. * @param transfer_encoding encoding of the data, NULL if not known
  430. * @param data pointer to size bytes of data at the
  431. * specified offset
  432. * @param off offset of data in the overall value
  433. * @param size number of bytes in data available
  434. * @return MHD_YES to continue iterating,
  435. * MHD_NO to abort the iteration
  436. */
  437. static int
  438. post_iterator (void *cls,
  439. enum MHD_ValueKind kind,
  440. const char *key,
  441. const char *filename,
  442. const char *content_type,
  443. const char *transfer_encoding,
  444. const char *data,
  445. uint64_t off,
  446. size_t size)
  447. {
  448. struct Request *request = cls;
  449. (void) kind;
  450. (void) filename;
  451. (void) content_type;
  452. (void) transfer_encoding;
  453. if (0 == strcmp ("domain", key))
  454. {
  455. if (size + off >= sizeof(request->domain_name))
  456. size = sizeof (request->domain_name) - off - 1;
  457. GNUNET_memcpy (&request->domain_name[off],
  458. data,
  459. size);
  460. request->domain_name[size+off] = '\0';
  461. return MHD_YES;
  462. }
  463. if (0 == strcmp ("pkey", key))
  464. {
  465. if (size + off >= sizeof(request->public_key))
  466. size = sizeof (request->public_key) - off - 1;
  467. GNUNET_memcpy (&request->public_key[off],
  468. data,
  469. size);
  470. request->public_key[size+off] = '\0';
  471. return MHD_YES;
  472. }
  473. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  474. _("Unsupported form value `%s'\n"),
  475. key);
  476. return MHD_YES;
  477. }
  478. /**
  479. * Continuation called to notify client about result of the
  480. * operation.
  481. *
  482. * @param cls closure
  483. * @param success #GNUNET_SYSERR on failure (including timeout/queue drop/failure to validate)
  484. * #GNUNET_NO if content was already there
  485. * #GNUNET_YES (or other positive value) on success
  486. * @param emsg NULL on success, otherwise an error message
  487. */
  488. static void
  489. put_continuation (void *cls,
  490. int32_t success,
  491. const char *emsg)
  492. {
  493. struct Request *request = cls;
  494. request->qe = NULL;
  495. if (0 >= success)
  496. {
  497. GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
  498. _("Failed to create record for domain `%s': %s\n"),
  499. request->domain_name,
  500. emsg);
  501. request->phase = RP_FAIL;
  502. }
  503. else
  504. request->phase = RP_SUCCESS;
  505. MHD_resume_connection (request->con);
  506. run_httpd_now ();
  507. }
  508. /**
  509. * Function called if we had an error in zone-to-name mapping.
  510. */
  511. static void
  512. zone_to_name_error (void *cls)
  513. {
  514. struct Request *request = cls;
  515. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  516. _("Error when mapping zone to name\n"));
  517. request->phase = RP_FAIL;
  518. MHD_resume_connection (request->con);
  519. run_httpd_now ();
  520. }
  521. /**
  522. * Test if a name mapping was found, if so, refuse. If not, initiate storing of the record.
  523. *
  524. * @param cls closure
  525. * @param zone_key public key of the zone
  526. * @param name name that is being mapped (at most 255 characters long)
  527. * @param rd_count number of entries in @a rd array
  528. * @param rd array of records with data to store
  529. */
  530. static void
  531. zone_to_name_cb (void *cls,
  532. const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key,
  533. const char *name,
  534. unsigned int rd_count,
  535. const struct GNUNET_GNSRECORD_Data *rd)
  536. {
  537. struct Request *request = cls;
  538. struct GNUNET_GNSRECORD_Data r;
  539. (void) rd;
  540. (void) zone_key;
  541. request->qe = NULL;
  542. if (0 != rd_count)
  543. {
  544. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  545. _("Found existing name `%s' for the given key\n"),
  546. name);
  547. request->phase = RP_FAIL;
  548. MHD_resume_connection (request->con);
  549. run_httpd_now ();
  550. return;
  551. }
  552. r.data = &request->pub;
  553. r.data_size = sizeof (request->pub);
  554. r.expiration_time = UINT64_MAX;
  555. r.record_type = GNUNET_GNSRECORD_TYPE_PKEY;
  556. r.flags = GNUNET_GNSRECORD_RF_NONE;
  557. request->qe = GNUNET_NAMESTORE_records_store (ns,
  558. &fcfs_zone_pkey,
  559. request->domain_name,
  560. 1, &r,
  561. &put_continuation,
  562. request);
  563. }
  564. /**
  565. * We encountered an error in the name lookup.
  566. */
  567. static void
  568. lookup_it_error (void *cls)
  569. {
  570. struct Request *request = cls;
  571. MHD_resume_connection (request->con);
  572. request->qe = NULL;
  573. request->phase = RP_FAIL;
  574. run_httpd_now ();
  575. }
  576. /**
  577. * We got a block back from the namestore. Decrypt it
  578. * and continue to process the result.
  579. *
  580. * @param cls the 'struct Request' we are processing
  581. * @param zonekey private key of the zone; NULL on disconnect
  582. * @param label label of the records; NULL on disconnect
  583. * @param rd_count number of entries in @a rd array, 0 if label was deleted
  584. * @param rd array of records with data to store
  585. */
  586. static void
  587. lookup_it_processor (void *cls,
  588. const struct GNUNET_CRYPTO_EcdsaPrivateKey *zonekey,
  589. const char *label,
  590. unsigned int rd_count,
  591. const struct GNUNET_GNSRECORD_Data *rd)
  592. {
  593. struct Request *request = cls;
  594. (void) label;
  595. (void) rd;
  596. (void) zonekey;
  597. if (0 == strcmp (label, request->domain_name)) {
  598. GNUNET_break (0 != rd_count);
  599. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  600. _("Found %u existing records for domain `%s'\n"),
  601. rd_count,
  602. request->domain_name);
  603. request->phase = RP_FAIL;
  604. }
  605. GNUNET_NAMESTORE_zone_iterator_next (request->lookup_it, 1);
  606. }
  607. static void
  608. lookup_it_finished (void *cls)
  609. {
  610. struct Request *request = cls;
  611. if (RP_FAIL == request->phase)
  612. {
  613. MHD_resume_connection (request->con);
  614. run_httpd_now ();
  615. return;
  616. }
  617. if (GNUNET_OK !=
  618. GNUNET_CRYPTO_ecdsa_public_key_from_string (request->public_key,
  619. strlen (request->public_key),
  620. &request->pub))
  621. {
  622. GNUNET_break (0);
  623. request->phase = RP_FAIL;
  624. MHD_resume_connection (request->con);
  625. run_httpd_now ();
  626. return;
  627. }
  628. request->qe = GNUNET_NAMESTORE_zone_to_name (ns,
  629. &fcfs_zone_pkey,
  630. &request->pub,
  631. &zone_to_name_error,
  632. request,
  633. &zone_to_name_cb,
  634. request);
  635. }
  636. /**
  637. * Main MHD callback for handling requests.
  638. *
  639. * @param cls unused
  640. * @param connection MHD connection handle
  641. * @param url the requested url
  642. * @param method the HTTP method used ("GET", "PUT", etc.)
  643. * @param version the HTTP version string (i.e. "HTTP/1.1")
  644. * @param upload_data the data being uploaded (excluding HEADERS,
  645. * for a POST that fits into memory and that is encoded
  646. * with a supported encoding, the POST data will NOT be
  647. * given in upload_data and is instead available as
  648. * part of MHD_get_connection_values; very large POST
  649. * data *will* be made available incrementally in
  650. * upload_data)
  651. * @param upload_data_size set initially to the size of the
  652. * @a upload_data provided; the method must update this
  653. * value to the number of bytes NOT processed;
  654. * @param ptr pointer to location where we store the 'struct Request'
  655. * @return #MHD_YES if the connection was handled successfully,
  656. * #MHD_NO if the socket must be closed due to a serious
  657. * error while handling the request
  658. */
  659. static int
  660. create_response (void *cls,
  661. struct MHD_Connection *connection,
  662. const char *url,
  663. const char *method,
  664. const char *version,
  665. const char *upload_data,
  666. size_t *upload_data_size,
  667. void **ptr)
  668. {
  669. struct MHD_Response *response;
  670. struct Request *request;
  671. struct GNUNET_CRYPTO_EcdsaPublicKey pub;
  672. int ret;
  673. (void) cls;
  674. (void) version;
  675. if ( (0 == strcmp (method, MHD_HTTP_METHOD_GET)) ||
  676. (0 == strcmp (method, MHD_HTTP_METHOD_HEAD)) )
  677. {
  678. if (0 == strcmp (url, FCFS_ZONEINFO_URL))
  679. ret = serve_zoneinfo_page (connection);
  680. else
  681. ret = serve_main_page (connection);
  682. if (ret != MHD_YES)
  683. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  684. _("Failed to create page for `%s'\n"),
  685. url);
  686. return ret;
  687. }
  688. if (0 == strcmp (method, MHD_HTTP_METHOD_POST))
  689. {
  690. request = *ptr;
  691. if (NULL == request)
  692. {
  693. request = GNUNET_new (struct Request);
  694. request->con = connection;
  695. *ptr = request;
  696. request->pp = MHD_create_post_processor (connection,
  697. 1024,
  698. &post_iterator,
  699. request);
  700. if (NULL == request->pp)
  701. {
  702. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  703. _("Failed to setup post processor for `%s'\n"),
  704. url);
  705. return MHD_NO; /* internal error */
  706. }
  707. return MHD_YES;
  708. }
  709. if (NULL != request->pp)
  710. {
  711. /* evaluate POST data */
  712. MHD_post_process (request->pp,
  713. upload_data,
  714. *upload_data_size);
  715. if (0 != *upload_data_size)
  716. {
  717. *upload_data_size = 0;
  718. return MHD_YES;
  719. }
  720. /* done with POST data, serve response */
  721. MHD_destroy_post_processor (request->pp);
  722. request->pp = NULL;
  723. }
  724. if (GNUNET_OK !=
  725. GNUNET_CRYPTO_ecdsa_public_key_from_string (request->public_key,
  726. strlen (request->public_key),
  727. &pub))
  728. {
  729. /* parse error */
  730. return fill_s_reply ("Failed to parse given public key",
  731. request, connection);
  732. }
  733. switch (request->phase)
  734. {
  735. case RP_START:
  736. if (NULL != strchr (request->domain_name, (int) '.'))
  737. {
  738. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  739. _("Domain name must not contain `.'\n"));
  740. request->phase = RP_FAIL;
  741. return fill_s_reply ("Domain name must not contain `.', sorry.",
  742. request,
  743. connection);
  744. }
  745. if (NULL != strchr (request->domain_name, (int) '+'))
  746. {
  747. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  748. _("Domain name must not contain `+'\n"));
  749. request->phase = RP_FAIL;
  750. return fill_s_reply ("Domain name must not contain `+', sorry.",
  751. request, connection);
  752. }
  753. request->phase = RP_LOOKUP;
  754. MHD_suspend_connection (request->con);
  755. request->lookup_it
  756. = GNUNET_NAMESTORE_zone_iteration_start (ns,
  757. &fcfs_zone_pkey,
  758. &lookup_it_error,
  759. request,
  760. &lookup_it_processor,
  761. request,
  762. &lookup_it_finished,
  763. request);
  764. break;
  765. case RP_LOOKUP:
  766. break;
  767. case RP_PUT:
  768. break;
  769. case RP_FAIL:
  770. return fill_s_reply ("Request failed, sorry.",
  771. request, connection);
  772. case RP_SUCCESS:
  773. return fill_s_reply ("Success.",
  774. request, connection);
  775. default:
  776. GNUNET_break (0);
  777. return MHD_NO;
  778. }
  779. return MHD_YES; /* will have a reply later... */
  780. }
  781. /* unsupported HTTP method */
  782. response = MHD_create_response_from_buffer (strlen (METHOD_ERROR),
  783. (void *) METHOD_ERROR,
  784. MHD_RESPMEM_PERSISTENT);
  785. ret = MHD_queue_response (connection,
  786. MHD_HTTP_NOT_ACCEPTABLE,
  787. response);
  788. MHD_destroy_response (response);
  789. return ret;
  790. }
  791. /**
  792. * Callback called upon completion of a request.
  793. * Decrements session reference counter.
  794. *
  795. * @param cls not used
  796. * @param connection connection that completed
  797. * @param con_cls session handle
  798. * @param toe status code
  799. */
  800. static void
  801. request_completed_callback (void *cls,
  802. struct MHD_Connection *connection,
  803. void **con_cls,
  804. enum MHD_RequestTerminationCode toe)
  805. {
  806. struct Request *request = *con_cls;
  807. (void) cls;
  808. (void) connection;
  809. (void) toe;
  810. if (NULL == request)
  811. return;
  812. if (NULL != request->pp)
  813. MHD_destroy_post_processor (request->pp);
  814. if (NULL != request->qe)
  815. GNUNET_NAMESTORE_cancel (request->qe);
  816. GNUNET_free (request);
  817. }
  818. #define UNSIGNED_MHD_LONG_LONG unsigned MHD_LONG_LONG
  819. /**
  820. * Schedule tasks to run MHD server.
  821. */
  822. static void
  823. run_httpd ()
  824. {
  825. fd_set rs;
  826. fd_set ws;
  827. fd_set es;
  828. struct GNUNET_NETWORK_FDSet *wrs;
  829. struct GNUNET_NETWORK_FDSet *wws;
  830. struct GNUNET_NETWORK_FDSet *wes;
  831. int max;
  832. int haveto;
  833. UNSIGNED_MHD_LONG_LONG timeout;
  834. struct GNUNET_TIME_Relative tv;
  835. FD_ZERO (&rs);
  836. FD_ZERO (&ws);
  837. FD_ZERO (&es);
  838. wrs = GNUNET_NETWORK_fdset_create ();
  839. wes = GNUNET_NETWORK_fdset_create ();
  840. wws = GNUNET_NETWORK_fdset_create ();
  841. max = -1;
  842. GNUNET_assert (MHD_YES ==
  843. MHD_get_fdset (httpd,
  844. &rs,
  845. &ws,
  846. &es,
  847. &max));
  848. haveto = MHD_get_timeout (httpd,
  849. &timeout);
  850. if (haveto == MHD_YES)
  851. tv.rel_value_us = (uint64_t) timeout * 1000LL;
  852. else
  853. tv = GNUNET_TIME_UNIT_FOREVER_REL;
  854. GNUNET_NETWORK_fdset_copy_native (wrs,
  855. &rs,
  856. max + 1);
  857. GNUNET_NETWORK_fdset_copy_native (wws,
  858. &ws,
  859. max + 1);
  860. GNUNET_NETWORK_fdset_copy_native (wes,
  861. &es,
  862. max + 1);
  863. httpd_task =
  864. GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_HIGH,
  865. tv,
  866. wrs,
  867. wws,
  868. &do_httpd,
  869. NULL);
  870. GNUNET_NETWORK_fdset_destroy (wrs);
  871. GNUNET_NETWORK_fdset_destroy (wws);
  872. GNUNET_NETWORK_fdset_destroy (wes);
  873. }
  874. /**
  875. * Task run whenever HTTP server operations are pending.
  876. *
  877. * @param cls unused
  878. */
  879. static void
  880. do_httpd (void *cls)
  881. {
  882. (void) cls;
  883. httpd_task = NULL;
  884. MHD_run (httpd);
  885. run_httpd ();
  886. }
  887. /**
  888. * Task run on shutdown. Cleans up everything.
  889. *
  890. * @param cls unused
  891. */
  892. static void
  893. do_shutdown (void *cls)
  894. {
  895. (void) cls;
  896. if (NULL != httpd_task)
  897. {
  898. GNUNET_SCHEDULER_cancel (httpd_task);
  899. httpd_task = NULL;
  900. }
  901. if (NULL != uzp_task)
  902. {
  903. GNUNET_SCHEDULER_cancel (uzp_task);
  904. uzp_task = NULL;
  905. }
  906. if (NULL != ns)
  907. {
  908. GNUNET_NAMESTORE_disconnect (ns);
  909. ns = NULL;
  910. }
  911. if (NULL != httpd)
  912. {
  913. MHD_stop_daemon (httpd);
  914. httpd = NULL;
  915. }
  916. if (NULL != id_op)
  917. {
  918. GNUNET_IDENTITY_cancel (id_op);
  919. id_op = NULL;
  920. }
  921. if (NULL != identity)
  922. {
  923. GNUNET_IDENTITY_disconnect (identity);
  924. identity = NULL;
  925. }
  926. }
  927. /**
  928. * Method called to inform about the egos of this peer.
  929. *
  930. * When used with #GNUNET_IDENTITY_create or #GNUNET_IDENTITY_get,
  931. * this function is only called ONCE, and 'NULL' being passed in
  932. * @a ego does indicate an error (i.e. name is taken or no default
  933. * value is known). If @a ego is non-NULL and if '*ctx'
  934. * is set in those callbacks, the value WILL be passed to a subsequent
  935. * call to the identity callback of #GNUNET_IDENTITY_connect (if
  936. * that one was not NULL).
  937. *
  938. * @param cls closure, NULL
  939. * @param ego ego handle
  940. * @param ctx context for application to store data for this ego
  941. * (during the lifetime of this process, initially NULL)
  942. * @param name name assigned by the user for this ego,
  943. * NULL if the user just deleted the ego and it
  944. * must thus no longer be used
  945. */
  946. static void
  947. identity_cb (void *cls,
  948. struct GNUNET_IDENTITY_Ego *ego,
  949. void **ctx,
  950. const char *name)
  951. {
  952. int options;
  953. (void) cls;
  954. (void) ctx;
  955. if (NULL == name)
  956. return;
  957. if (0 != strcmp (name,
  958. zone))
  959. return;
  960. if (NULL == ego)
  961. {
  962. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  963. _("No ego configured for `fcfsd` subsystem\n"));
  964. GNUNET_SCHEDULER_shutdown ();
  965. return;
  966. }
  967. fcfs_zone_pkey = *GNUNET_IDENTITY_ego_get_private_key (ego);
  968. options = MHD_USE_DUAL_STACK | MHD_USE_DEBUG | MHD_ALLOW_SUSPEND_RESUME;
  969. do
  970. {
  971. httpd = MHD_start_daemon (options,
  972. (uint16_t) port,
  973. NULL, NULL,
  974. &create_response, NULL,
  975. MHD_OPTION_CONNECTION_LIMIT, (unsigned int) 128,
  976. MHD_OPTION_PER_IP_CONNECTION_LIMIT, (unsigned int) 1,
  977. MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 16,
  978. MHD_OPTION_CONNECTION_MEMORY_LIMIT, (size_t) (4 * 1024),
  979. MHD_OPTION_NOTIFY_COMPLETED, &request_completed_callback, NULL,
  980. MHD_OPTION_END);
  981. if (MHD_USE_DEBUG == options)
  982. break;
  983. options = MHD_USE_DEBUG;
  984. }
  985. while (NULL == httpd);
  986. if (NULL == httpd)
  987. {
  988. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  989. _("Failed to start HTTP server\n"));
  990. GNUNET_SCHEDULER_shutdown ();
  991. return;
  992. }
  993. run_httpd ();
  994. }
  995. /**
  996. * Main function that will be run.
  997. *
  998. * @param cls closure
  999. * @param args remaining command-line arguments
  1000. * @param cfgfile name of the configuration file used (for saving, can be NULL!)
  1001. * @param cfg configuration
  1002. */
  1003. static void
  1004. run (void *cls,
  1005. char *const *args,
  1006. const char *cfgfile,
  1007. const struct GNUNET_CONFIGURATION_Handle *cfg)
  1008. {
  1009. (void) cls;
  1010. (void) args;
  1011. (void) cfgfile;
  1012. if (GNUNET_OK !=
  1013. GNUNET_CONFIGURATION_get_value_number (cfg,
  1014. "fcfsd",
  1015. "HTTPPORT",
  1016. &port))
  1017. {
  1018. GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
  1019. "fcfsd", "HTTPPORT");
  1020. return;
  1021. }
  1022. ns = GNUNET_NAMESTORE_connect (cfg);
  1023. if (NULL == ns)
  1024. {
  1025. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  1026. _("Failed to connect to namestore\n"));
  1027. return;
  1028. }
  1029. identity = GNUNET_IDENTITY_connect (cfg,
  1030. &identity_cb,
  1031. NULL);
  1032. if (NULL == identity)
  1033. {
  1034. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  1035. _("Failed to connect to identity\n"));
  1036. return;
  1037. }
  1038. uzp_task = GNUNET_SCHEDULER_add_now (&update_zoneinfo_page,
  1039. NULL);
  1040. GNUNET_SCHEDULER_add_shutdown (&do_shutdown,
  1041. NULL);
  1042. }
  1043. /**
  1044. * The main function for the fcfs daemon.
  1045. *
  1046. * @param argc number of arguments from the command line
  1047. * @param argv command line arguments
  1048. * @return 0 ok, 1 on error
  1049. */
  1050. int
  1051. main (int argc,
  1052. char *const *argv)
  1053. {
  1054. struct GNUNET_GETOPT_CommandLineOption options[] = {
  1055. GNUNET_GETOPT_option_mandatory
  1056. (GNUNET_GETOPT_option_string ('z',
  1057. "zone",
  1058. "EGO",
  1059. gettext_noop ("name of the zone that is to be managed by FCFSD"),
  1060. &zone)),
  1061. GNUNET_GETOPT_OPTION_END
  1062. };
  1063. int ret;
  1064. if (GNUNET_OK !=
  1065. GNUNET_STRINGS_get_utf8_args (argc, argv,
  1066. &argc, &argv))
  1067. return 2;
  1068. GNUNET_log_setup ("fcfsd",
  1069. "WARNING",
  1070. NULL);
  1071. ret =
  1072. (GNUNET_OK ==
  1073. GNUNET_PROGRAM_run (argc,
  1074. argv,
  1075. "gnunet-namestore-fcfsd",
  1076. _("GNU Name System First Come First Serve name registration service"),
  1077. options,
  1078. &run, NULL)) ? 0 : 1;
  1079. GNUNET_free ((void*) argv);
  1080. GNUNET_CRYPTO_ecdsa_key_clear (&fcfs_zone_pkey);
  1081. return ret;
  1082. }
  1083. /* end of gnunet-namestore-fcfsd.c */