gnunet-namestore.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2012, 2013, 2014 GNUnet e.V.
  4. GNUnet is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published
  6. by the Free Software Foundation; either version 3, or (at your
  7. 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. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNUnet; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  15. Boston, MA 02110-1301, USA.
  16. */
  17. /**
  18. * @file gnunet-namestore.c
  19. * @brief command line tool to manipulate the local zone
  20. * @author Christian Grothoff
  21. *
  22. * TODO:
  23. * - test
  24. */
  25. #include "platform.h"
  26. #include <gnunet_util_lib.h>
  27. #include <gnunet_dnsparser_lib.h>
  28. #include <gnunet_identity_service.h>
  29. #include <gnunet_gnsrecord_lib.h>
  30. #include <gnunet_gns_service.h>
  31. #include <gnunet_namestore_service.h>
  32. /**
  33. * Handle to the namestore.
  34. */
  35. static struct GNUNET_NAMESTORE_Handle *ns;
  36. /**
  37. * Private key for the our zone.
  38. */
  39. static struct GNUNET_CRYPTO_EcdsaPrivateKey zone_pkey;
  40. /**
  41. * Handle to identity lookup.
  42. */
  43. static struct GNUNET_IDENTITY_EgoLookup *el;
  44. /**
  45. * Identity service handle
  46. */
  47. static struct GNUNET_IDENTITY_Handle *idh;
  48. /**
  49. * Obtain default ego
  50. */
  51. struct GNUNET_IDENTITY_Operation *get_default;
  52. /**
  53. * Name of the ego controlling the zone.
  54. */
  55. static char *ego_name;
  56. /**
  57. * Desired action is to add a record.
  58. */
  59. static int add;
  60. /**
  61. * Queue entry for the 'add-uri' operation.
  62. */
  63. static struct GNUNET_NAMESTORE_QueueEntry *add_qe_uri;
  64. /**
  65. * Queue entry for the 'add' operation.
  66. */
  67. static struct GNUNET_NAMESTORE_QueueEntry *add_qe;
  68. /**
  69. * Queue entry for the 'reverse lookup' operation (in combination with a name).
  70. */
  71. static struct GNUNET_NAMESTORE_QueueEntry *reverse_qe;
  72. /**
  73. * Desired action is to list records.
  74. */
  75. static int list;
  76. /**
  77. * List iterator for the 'list' operation.
  78. */
  79. static struct GNUNET_NAMESTORE_ZoneIterator *list_it;
  80. /**
  81. * Desired action is to remove a record.
  82. */
  83. static int del;
  84. /**
  85. * Is record public (opposite of #GNUNET_GNSRECORD_RF_PRIVATE)
  86. */
  87. static int is_public;
  88. /**
  89. * Is record a shadow record (#GNUNET_GNSRECORD_RF_SHADOW_RECORD)
  90. */
  91. static int is_shadow;
  92. /**
  93. * Queue entry for the 'del' operation.
  94. */
  95. static struct GNUNET_NAMESTORE_QueueEntry *del_qe;
  96. /**
  97. * Name of the records to add/list/remove.
  98. */
  99. static char *name;
  100. /**
  101. * Value of the record to add/remove.
  102. */
  103. static char *value;
  104. /**
  105. * URI to import.
  106. */
  107. static char *uri;
  108. /**
  109. * Reverse lookup to perform.
  110. */
  111. static char *reverse_pkey;
  112. /**
  113. * Type of the record to add/remove, NULL to remove all.
  114. */
  115. static char *typestring;
  116. /**
  117. * Desired expiration time.
  118. */
  119. static char *expirationstring;
  120. /**
  121. * Desired nick name.
  122. */
  123. static char *nickstring;
  124. /**
  125. * Global return value
  126. */
  127. static int ret;
  128. /**
  129. * Type string converted to DNS type value.
  130. */
  131. static uint32_t type;
  132. /**
  133. * Value in binary format.
  134. */
  135. static void *data;
  136. /**
  137. * Number of bytes in #data.
  138. */
  139. static size_t data_size;
  140. /**
  141. * Expirationstring converted to relative time.
  142. */
  143. static struct GNUNET_TIME_Relative etime_rel;
  144. /**
  145. * Expirationstring converted to absolute time.
  146. */
  147. static struct GNUNET_TIME_Absolute etime_abs;
  148. /**
  149. * Is expiration time relative or absolute time?
  150. */
  151. static int etime_is_rel = GNUNET_SYSERR;
  152. /**
  153. * Monitor handle.
  154. */
  155. static struct GNUNET_NAMESTORE_ZoneMonitor *zm;
  156. /**
  157. * Enables monitor mode.
  158. */
  159. static int monitor;
  160. /**
  161. * Task run on shutdown. Cleans up everything.
  162. *
  163. * @param cls unused
  164. */
  165. static void
  166. do_shutdown (void *cls)
  167. {
  168. if (NULL != get_default)
  169. {
  170. GNUNET_IDENTITY_cancel (get_default);
  171. get_default = NULL;
  172. }
  173. if (NULL != idh)
  174. {
  175. GNUNET_IDENTITY_disconnect (idh);
  176. idh = NULL;
  177. }
  178. if (NULL != el)
  179. {
  180. GNUNET_IDENTITY_ego_lookup_cancel (el);
  181. el = NULL;
  182. }
  183. if (NULL != list_it)
  184. {
  185. GNUNET_NAMESTORE_zone_iteration_stop (list_it);
  186. list_it = NULL;
  187. }
  188. if (NULL != add_qe)
  189. {
  190. GNUNET_NAMESTORE_cancel (add_qe);
  191. add_qe = NULL;
  192. }
  193. if (NULL != add_qe_uri)
  194. {
  195. GNUNET_NAMESTORE_cancel (add_qe_uri);
  196. add_qe_uri = NULL;
  197. }
  198. if (NULL != del_qe)
  199. {
  200. GNUNET_NAMESTORE_cancel (del_qe);
  201. del_qe = NULL;
  202. }
  203. if (NULL != ns)
  204. {
  205. GNUNET_NAMESTORE_disconnect (ns);
  206. ns = NULL;
  207. }
  208. memset (&zone_pkey, 0, sizeof (zone_pkey));
  209. if (NULL != uri)
  210. {
  211. GNUNET_free (uri);
  212. uri = NULL;
  213. }
  214. if (NULL != zm)
  215. {
  216. GNUNET_NAMESTORE_zone_monitor_stop (zm);
  217. zm = NULL;
  218. }
  219. if (NULL != data)
  220. {
  221. GNUNET_free (data);
  222. data = NULL;
  223. }
  224. }
  225. /**
  226. * Check if we are finished, and if so, perform shutdown.
  227. */
  228. static void
  229. test_finished ()
  230. {
  231. if ( (NULL == add_qe) &&
  232. (NULL == add_qe_uri) &&
  233. (NULL == del_qe) &&
  234. (NULL == reverse_qe) &&
  235. (NULL == list_it) )
  236. GNUNET_SCHEDULER_shutdown ();
  237. }
  238. /**
  239. * Continuation called to notify client about result of the
  240. * operation.
  241. *
  242. * @param cls closure, location of the QueueEntry pointer to NULL out
  243. * @param success #GNUNET_SYSERR on failure (including timeout/queue drop/failure to validate)
  244. * #GNUNET_NO if content was already there
  245. * #GNUNET_YES (or other positive value) on success
  246. * @param emsg NULL on success, otherwise an error message
  247. */
  248. static void
  249. add_continuation (void *cls,
  250. int32_t success,
  251. const char *emsg)
  252. {
  253. struct GNUNET_NAMESTORE_QueueEntry **qe = cls;
  254. *qe = NULL;
  255. if (GNUNET_YES != success)
  256. {
  257. fprintf (stderr,
  258. _("Adding record failed: %s\n"),
  259. (GNUNET_NO == success) ? "record exists" : emsg);
  260. if (GNUNET_NO != success)
  261. ret = 1;
  262. }
  263. ret = 0;
  264. test_finished ();
  265. }
  266. /**
  267. * Continuation called to notify client about result of the
  268. * operation.
  269. *
  270. * @param cls closure, unused
  271. * @param success #GNUNET_SYSERR on failure (including timeout/queue drop/failure to validate)
  272. * #GNUNET_NO if content was already there
  273. * #GNUNET_YES (or other positive value) on success
  274. * @param emsg NULL on success, otherwise an error message
  275. */
  276. static void
  277. del_continuation (void *cls,
  278. int32_t success,
  279. const char *emsg)
  280. {
  281. del_qe = NULL;
  282. if (GNUNET_NO == success)
  283. {
  284. fprintf (stderr,
  285. _("Deleting record failed, record does not exist%s%s\n"),
  286. (NULL != emsg) ? ": " : "",
  287. (NULL != emsg) ? emsg : "");
  288. }
  289. if (GNUNET_SYSERR == success)
  290. {
  291. fprintf (stderr,
  292. _("Deleting record failed%s%s\n"),
  293. (NULL != emsg) ? ": " : "",
  294. (NULL != emsg) ? emsg : "");
  295. }
  296. test_finished ();
  297. }
  298. /**
  299. * Function called when we are done with a zone iteration.
  300. */
  301. static void
  302. zone_iteration_finished (void *cls)
  303. {
  304. list_it = NULL;
  305. test_finished ();
  306. }
  307. /**
  308. * Function called when we encountered an error in a zone iteration.
  309. */
  310. static void
  311. zone_iteration_error_cb (void *cls)
  312. {
  313. list_it = NULL;
  314. fprintf (stderr,
  315. "Error iterating over zone\n");
  316. ret = 1;
  317. test_finished ();
  318. }
  319. /**
  320. * Process a record that was stored in the namestore.
  321. *
  322. * @param cls closure
  323. * @param zone_key private key of the zone
  324. * @param rname name that is being mapped (at most 255 characters long)
  325. * @param rd_len number of entries in @a rd array
  326. * @param rd array of records with data to store
  327. */
  328. static void
  329. display_record (void *cls,
  330. const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key,
  331. const char *rname,
  332. unsigned int rd_len,
  333. const struct GNUNET_GNSRECORD_Data *rd)
  334. {
  335. const char *typestring;
  336. char *s;
  337. unsigned int i;
  338. const char *ets;
  339. struct GNUNET_TIME_Absolute at;
  340. struct GNUNET_TIME_Relative rt;
  341. if ( (NULL != name) &&
  342. (0 != strcmp (name, rname)) )
  343. {
  344. GNUNET_NAMESTORE_zone_iterator_next (list_it);
  345. return;
  346. }
  347. FPRINTF (stdout,
  348. "%s:\n",
  349. rname);
  350. for (i=0;i<rd_len;i++)
  351. {
  352. if ( (GNUNET_GNSRECORD_TYPE_NICK == rd[i].record_type) &&
  353. (0 != strcmp (rname,
  354. "+")) )
  355. continue;
  356. typestring = GNUNET_GNSRECORD_number_to_typename (rd[i].record_type);
  357. s = GNUNET_GNSRECORD_value_to_string (rd[i].record_type,
  358. rd[i].data,
  359. rd[i].data_size);
  360. if (NULL == s)
  361. {
  362. FPRINTF (stdout,
  363. _("\tCorrupt or unsupported record of type %u\n"),
  364. (unsigned int) rd[i].record_type);
  365. continue;
  366. }
  367. if (0 != (rd[i].flags & GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION))
  368. {
  369. rt.rel_value_us = rd[i].expiration_time;
  370. ets = GNUNET_STRINGS_relative_time_to_string (rt, GNUNET_YES);
  371. }
  372. else
  373. {
  374. at.abs_value_us = rd[i].expiration_time;
  375. ets = GNUNET_STRINGS_absolute_time_to_string (at);
  376. }
  377. FPRINTF (stdout,
  378. "\t%s: %s (%s)\t%s\t%s\n",
  379. typestring,
  380. s,
  381. ets,
  382. (0 != (rd[i].flags & GNUNET_GNSRECORD_RF_PRIVATE)) ? "PRIVATE" : "PUBLIC",
  383. (0 != (rd[i].flags & GNUNET_GNSRECORD_RF_SHADOW_RECORD)) ? "SHADOW" : "");
  384. GNUNET_free (s);
  385. }
  386. FPRINTF (stdout, "%s", "\n");
  387. GNUNET_NAMESTORE_zone_iterator_next (list_it);
  388. }
  389. /**
  390. * Function called once we are in sync in monitor mode.
  391. *
  392. * @param cls NULL
  393. */
  394. static void
  395. sync_cb (void *cls)
  396. {
  397. FPRINTF (stdout, "%s", "Monitor is now in sync.\n");
  398. }
  399. /**
  400. * Function called on errors while monitoring.
  401. *
  402. * @param cls NULL
  403. */
  404. static void
  405. monitor_error_cb (void *cls)
  406. {
  407. FPRINTF (stderr, "%s", "Monitor disconnected and out of sync.\n");
  408. }
  409. /**
  410. * Function called if lookup fails.
  411. */
  412. static void
  413. lookup_error_cb (void *cls)
  414. {
  415. add_qe = NULL;
  416. GNUNET_break (0);
  417. ret = 1;
  418. test_finished ();
  419. }
  420. /**
  421. * We're storing a record; this function is given the existing record
  422. * so that we can merge the information.
  423. *
  424. * @param cls closure, unused
  425. * @param zone_key private key of the zone
  426. * @param rec_name name that is being mapped (at most 255 characters long)
  427. * @param rd_count number of entries in @a rd array
  428. * @param rd array of records with data to store
  429. */
  430. static void
  431. get_existing_record (void *cls,
  432. const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key,
  433. const char *rec_name,
  434. unsigned int rd_count,
  435. const struct GNUNET_GNSRECORD_Data *rd)
  436. {
  437. struct GNUNET_GNSRECORD_Data rdn[rd_count + 1];
  438. struct GNUNET_GNSRECORD_Data *rde;
  439. unsigned int i;
  440. add_qe = NULL;
  441. if (0 != strcmp (rec_name, name))
  442. {
  443. GNUNET_break (0);
  444. ret = 1;
  445. test_finished ();
  446. return;
  447. }
  448. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  449. "Received %u records for name `%s'\n",
  450. rd_count, rec_name);
  451. for (i=0;i<rd_count;i++)
  452. {
  453. switch (rd[i].record_type)
  454. {
  455. case GNUNET_DNSPARSER_TYPE_CNAME:
  456. fprintf (stderr,
  457. _("A %s record exists already under `%s', no other records can be added.\n"),
  458. "CNAME",
  459. rec_name);
  460. ret = 1;
  461. test_finished ();
  462. return;
  463. case GNUNET_GNSRECORD_TYPE_PKEY:
  464. fprintf (stderr,
  465. _("A %s record exists already under `%s', no other records can be added.\n"),
  466. "PKEY",
  467. rec_name);
  468. ret = 1;
  469. test_finished ();
  470. return;
  471. case GNUNET_GNSRECORD_TYPE_GNS2DNS:
  472. fprintf (stderr,
  473. _("A %s record exists already under `%s', no other records can be added.\n"),
  474. "GNS2DNS",
  475. rec_name);
  476. ret = 1;
  477. test_finished ();
  478. return;
  479. }
  480. }
  481. switch (type)
  482. {
  483. case GNUNET_DNSPARSER_TYPE_CNAME:
  484. if (0 != rd_count)
  485. {
  486. fprintf (stderr,
  487. _("Records already exist under `%s', cannot add `%s' record.\n"),
  488. rec_name,
  489. "CNAME");
  490. ret = 1;
  491. test_finished ();
  492. return;
  493. }
  494. break;
  495. case GNUNET_GNSRECORD_TYPE_PKEY:
  496. if (0 != rd_count)
  497. {
  498. fprintf (stderr,
  499. _("Records already exist under `%s', cannot add `%s' record.\n"),
  500. rec_name,
  501. "PKEY");
  502. ret = 1;
  503. test_finished ();
  504. return;
  505. }
  506. break;
  507. case GNUNET_GNSRECORD_TYPE_GNS2DNS:
  508. if (0 != rd_count)
  509. {
  510. fprintf (stderr,
  511. _("Records already exist under `%s', cannot add `%s' record.\n"),
  512. rec_name,
  513. "GNS2DNS");
  514. ret = 1;
  515. test_finished ();
  516. return;
  517. }
  518. break;
  519. }
  520. memset (rdn, 0, sizeof (struct GNUNET_GNSRECORD_Data));
  521. GNUNET_memcpy (&rdn[1],
  522. rd,
  523. rd_count * sizeof (struct GNUNET_GNSRECORD_Data));
  524. rde = &rdn[0];
  525. rde->data = data;
  526. rde->data_size = data_size;
  527. rde->record_type = type;
  528. if (1 == is_shadow)
  529. rde->flags |= GNUNET_GNSRECORD_RF_SHADOW_RECORD;
  530. if (1 != is_public)
  531. rde->flags |= GNUNET_GNSRECORD_RF_PRIVATE;
  532. if (GNUNET_YES == etime_is_rel)
  533. {
  534. rde->expiration_time = etime_rel.rel_value_us;
  535. rde->flags |= GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION;
  536. }
  537. else if (GNUNET_NO == etime_is_rel)
  538. rde->expiration_time = etime_abs.abs_value_us;
  539. else
  540. rde->expiration_time = GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us;
  541. GNUNET_assert (NULL != name);
  542. add_qe = GNUNET_NAMESTORE_records_store (ns,
  543. &zone_pkey,
  544. name,
  545. rd_count + 1,
  546. rde,
  547. &add_continuation,
  548. &add_qe);
  549. }
  550. /**
  551. * Function called if we encountered an error in zone-to-name.
  552. */
  553. static void
  554. reverse_error_cb (void *cls)
  555. {
  556. reverse_qe = NULL;
  557. FPRINTF (stdout,
  558. "%s.zkey\n",
  559. reverse_pkey);
  560. }
  561. /**
  562. * Function called with the result of our attempt to obtain a name for a given
  563. * public key.
  564. *
  565. * @param cls NULL
  566. * @param zone private key of the zone; NULL on disconnect
  567. * @param label label of the records; NULL on disconnect
  568. * @param rd_count number of entries in @a rd array, 0 if label was deleted
  569. * @param rd array of records with data to store
  570. */
  571. static void
  572. handle_reverse_lookup (void *cls,
  573. const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
  574. const char *label,
  575. unsigned int rd_count,
  576. const struct GNUNET_GNSRECORD_Data *rd)
  577. {
  578. reverse_qe = NULL;
  579. if (NULL == label)
  580. FPRINTF (stdout,
  581. "%s.zkey\n",
  582. reverse_pkey);
  583. else
  584. FPRINTF (stdout,
  585. "%s.gnu\n",
  586. label);
  587. test_finished ();
  588. }
  589. /**
  590. * Function called if lookup for deletion fails.
  591. */
  592. static void
  593. del_lookup_error_cb (void *cls)
  594. {
  595. del_qe = NULL;
  596. GNUNET_break (0);
  597. ret = 1;
  598. test_finished ();
  599. }
  600. /**
  601. * We were asked to delete something; this function is called with
  602. * the existing records. Now we should determine what should be
  603. * deleted and then issue the deletion operation.
  604. *
  605. * @param cls NULL
  606. * @param zone private key of the zone we are deleting from
  607. * @param label name of the records we are editing
  608. * @param rd_count size of the @a rd array
  609. * @param rd existing records
  610. */
  611. static void
  612. del_monitor (void *cls,
  613. const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
  614. const char *label,
  615. unsigned int rd_count,
  616. const struct GNUNET_GNSRECORD_Data *rd)
  617. {
  618. struct GNUNET_GNSRECORD_Data rdx[rd_count];
  619. unsigned int rd_left;
  620. unsigned int i;
  621. uint32_t type;
  622. char *vs;
  623. del_qe = NULL;
  624. if (0 == rd_count)
  625. {
  626. FPRINTF (stderr,
  627. _("There are no records under label `%s' that could be deleted.\n"),
  628. label);
  629. ret = 1;
  630. test_finished ();
  631. return;
  632. }
  633. if ( (NULL == value) &&
  634. (NULL == typestring) )
  635. {
  636. /* delete everything */
  637. del_qe = GNUNET_NAMESTORE_records_store (ns,
  638. &zone_pkey,
  639. name,
  640. 0,
  641. NULL,
  642. &del_continuation,
  643. NULL);
  644. return;
  645. }
  646. rd_left = 0;
  647. if (NULL != typestring)
  648. type = GNUNET_GNSRECORD_typename_to_number (typestring);
  649. else
  650. type = GNUNET_GNSRECORD_TYPE_ANY;
  651. for (i=0;i<rd_count;i++)
  652. {
  653. vs = NULL;
  654. if (! ( ( (GNUNET_GNSRECORD_TYPE_ANY == type) ||
  655. (rd[i].record_type == type) ) &&
  656. ( (NULL == value) ||
  657. (NULL == (vs = (GNUNET_GNSRECORD_value_to_string (rd[i].record_type,
  658. rd[i].data,
  659. rd[i].data_size)))) ||
  660. (0 == strcmp (vs, value)) ) ) )
  661. rdx[rd_left++] = rd[i];
  662. GNUNET_free_non_null (vs);
  663. }
  664. if (rd_count == rd_left)
  665. {
  666. /* nothing got deleted */
  667. FPRINTF (stderr,
  668. _("There are no records under label `%s' that match the request for deletion.\n"),
  669. label);
  670. test_finished ();
  671. return;
  672. }
  673. /* delete everything but what we copied to 'rdx' */
  674. del_qe = GNUNET_NAMESTORE_records_store (ns,
  675. &zone_pkey,
  676. name,
  677. rd_left,
  678. rdx,
  679. &del_continuation,
  680. NULL);
  681. }
  682. /**
  683. * Callback invoked from identity service with ego information.
  684. * An @a ego of NULL means the ego was not found.
  685. *
  686. * @param cls closure with the configuration
  687. * @param ego an ego known to identity service, or NULL
  688. */
  689. static void
  690. identity_cb (void *cls,
  691. const struct GNUNET_IDENTITY_Ego *ego)
  692. {
  693. const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
  694. struct GNUNET_CRYPTO_EcdsaPublicKey pub;
  695. struct GNUNET_GNSRECORD_Data rd;
  696. el = NULL;
  697. if (NULL == ego)
  698. {
  699. if (NULL != ego_name)
  700. {
  701. fprintf (stderr,
  702. _("Ego `%s' not known to identity service\n"),
  703. ego_name);
  704. }
  705. GNUNET_SCHEDULER_shutdown ();
  706. ret = -1;
  707. return;
  708. }
  709. zone_pkey = *GNUNET_IDENTITY_ego_get_private_key (ego);
  710. GNUNET_free_non_null (ego_name);
  711. ego_name = NULL;
  712. if (! (add|del|list|(NULL != nickstring)|(NULL != uri)|(NULL != reverse_pkey)) )
  713. {
  714. /* nothing more to be done */
  715. fprintf (stderr,
  716. _("No options given\n"));
  717. GNUNET_SCHEDULER_shutdown ();
  718. return;
  719. }
  720. GNUNET_CRYPTO_ecdsa_key_get_public (&zone_pkey,
  721. &pub);
  722. ns = GNUNET_NAMESTORE_connect (cfg);
  723. if (NULL == ns)
  724. {
  725. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  726. _("Failed to connect to namestore\n"));
  727. return;
  728. }
  729. if (add)
  730. {
  731. if (NULL == name)
  732. {
  733. fprintf (stderr,
  734. _("Missing option `%s' for operation `%s'\n"),
  735. "-n", _("add"));
  736. GNUNET_SCHEDULER_shutdown ();
  737. ret = 1;
  738. return;
  739. }
  740. if (NULL == typestring)
  741. {
  742. fprintf (stderr,
  743. _("Missing option `%s' for operation `%s'\n"),
  744. "-t", _("add"));
  745. GNUNET_SCHEDULER_shutdown ();
  746. ret = 1;
  747. return;
  748. }
  749. type = GNUNET_GNSRECORD_typename_to_number (typestring);
  750. if (UINT32_MAX == type)
  751. {
  752. fprintf (stderr,
  753. _("Unsupported type `%s'\n"),
  754. typestring);
  755. GNUNET_SCHEDULER_shutdown ();
  756. ret = 1;
  757. return;
  758. }
  759. if (NULL == value)
  760. {
  761. fprintf (stderr,
  762. _("Missing option `%s' for operation `%s'\n"),
  763. "-V", _("add"));
  764. ret = 1;
  765. GNUNET_SCHEDULER_shutdown ();
  766. return;
  767. }
  768. if (GNUNET_OK !=
  769. GNUNET_GNSRECORD_string_to_value (type,
  770. value,
  771. &data,
  772. &data_size))
  773. {
  774. fprintf (stderr,
  775. _("Value `%s' invalid for record type `%s'\n"),
  776. value,
  777. typestring);
  778. GNUNET_SCHEDULER_shutdown ();
  779. ret = 1;
  780. return;
  781. }
  782. if (NULL == expirationstring)
  783. {
  784. fprintf (stderr,
  785. _("Missing option `%s' for operation `%s'\n"),
  786. "-e",
  787. _("add"));
  788. GNUNET_SCHEDULER_shutdown ();
  789. ret = 1;
  790. return;
  791. }
  792. if (0 == strcmp (expirationstring,
  793. "never"))
  794. {
  795. etime_abs = GNUNET_TIME_UNIT_FOREVER_ABS;
  796. etime_is_rel = GNUNET_NO;
  797. }
  798. else if (GNUNET_OK ==
  799. GNUNET_STRINGS_fancy_time_to_relative (expirationstring,
  800. &etime_rel))
  801. {
  802. etime_is_rel = GNUNET_YES;
  803. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  804. "Storing record with relative expiration time of %s\n",
  805. GNUNET_STRINGS_relative_time_to_string (etime_rel,
  806. GNUNET_NO));
  807. }
  808. else if (GNUNET_OK ==
  809. GNUNET_STRINGS_fancy_time_to_absolute (expirationstring,
  810. &etime_abs))
  811. {
  812. etime_is_rel = GNUNET_NO;
  813. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  814. "Storing record with absolute expiration time of %s\n",
  815. GNUNET_STRINGS_absolute_time_to_string (etime_abs));
  816. }
  817. else
  818. {
  819. fprintf (stderr,
  820. _("Invalid time format `%s'\n"),
  821. expirationstring);
  822. GNUNET_SCHEDULER_shutdown ();
  823. ret = 1;
  824. return;
  825. }
  826. add_qe = GNUNET_NAMESTORE_records_lookup (ns,
  827. &zone_pkey,
  828. name,
  829. &lookup_error_cb,
  830. NULL,
  831. &get_existing_record,
  832. NULL);
  833. }
  834. if (del)
  835. {
  836. if (NULL == name)
  837. {
  838. fprintf (stderr,
  839. _("Missing option `%s' for operation `%s'\n"),
  840. "-n", _("del"));
  841. GNUNET_SCHEDULER_shutdown ();
  842. ret = 1;
  843. return;
  844. }
  845. del_qe = GNUNET_NAMESTORE_records_lookup (ns,
  846. &zone_pkey,
  847. name,
  848. &del_lookup_error_cb,
  849. NULL,
  850. &del_monitor,
  851. NULL);
  852. }
  853. if (list)
  854. {
  855. list_it = GNUNET_NAMESTORE_zone_iteration_start (ns,
  856. &zone_pkey,
  857. &zone_iteration_error_cb,
  858. NULL,
  859. &display_record,
  860. NULL,
  861. &zone_iteration_finished,
  862. NULL);
  863. }
  864. if (NULL != reverse_pkey)
  865. {
  866. struct GNUNET_CRYPTO_EcdsaPublicKey pubkey;
  867. if (GNUNET_OK !=
  868. GNUNET_CRYPTO_ecdsa_public_key_from_string (reverse_pkey,
  869. strlen (reverse_pkey),
  870. &pubkey))
  871. {
  872. fprintf (stderr,
  873. _("Invalid public key for reverse lookup `%s'\n"),
  874. reverse_pkey);
  875. GNUNET_SCHEDULER_shutdown ();
  876. }
  877. reverse_qe = GNUNET_NAMESTORE_zone_to_name (ns,
  878. &zone_pkey,
  879. &pubkey,
  880. &reverse_error_cb,
  881. NULL,
  882. &handle_reverse_lookup,
  883. NULL);
  884. }
  885. if (NULL != uri)
  886. {
  887. char sh[105];
  888. char sname[64];
  889. struct GNUNET_CRYPTO_EcdsaPublicKey pkey;
  890. GNUNET_STRINGS_utf8_tolower (uri, uri);
  891. if ( (2 != (sscanf (uri,
  892. "gnunet://gns/%52s/%63s",
  893. sh,
  894. sname)) ) ||
  895. (GNUNET_OK !=
  896. GNUNET_CRYPTO_ecdsa_public_key_from_string (sh,
  897. strlen (sh),
  898. &pkey)) )
  899. {
  900. fprintf (stderr,
  901. _("Invalid URI `%s'\n"),
  902. uri);
  903. GNUNET_SCHEDULER_shutdown ();
  904. ret = 1;
  905. return;
  906. }
  907. memset (&rd, 0, sizeof (rd));
  908. rd.data = &pkey;
  909. rd.data_size = sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey);
  910. rd.record_type = GNUNET_GNSRECORD_TYPE_PKEY;
  911. if (GNUNET_YES == etime_is_rel)
  912. {
  913. rd.expiration_time = etime_rel.rel_value_us;
  914. rd.flags |= GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION;
  915. }
  916. else if (GNUNET_NO == etime_is_rel)
  917. rd.expiration_time = etime_abs.abs_value_us;
  918. else
  919. rd.expiration_time = GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us;
  920. if (1 == is_shadow)
  921. rd.flags |= GNUNET_GNSRECORD_RF_SHADOW_RECORD;
  922. add_qe_uri = GNUNET_NAMESTORE_records_store (ns,
  923. &zone_pkey,
  924. sname,
  925. 1,
  926. &rd,
  927. &add_continuation,
  928. &add_qe_uri);
  929. }
  930. if (NULL != nickstring)
  931. {
  932. if (0 == strlen(nickstring))
  933. {
  934. fprintf (stderr,
  935. _("Invalid nick `%s'\n"),
  936. nickstring);
  937. GNUNET_SCHEDULER_shutdown ();
  938. ret = 1;
  939. return;
  940. }
  941. add_qe_uri = GNUNET_NAMESTORE_set_nick (ns,
  942. &zone_pkey,
  943. nickstring,
  944. &add_continuation,
  945. &add_qe_uri);
  946. }
  947. if (monitor)
  948. {
  949. zm = GNUNET_NAMESTORE_zone_monitor_start (cfg,
  950. &zone_pkey,
  951. GNUNET_YES,
  952. &monitor_error_cb,
  953. NULL,
  954. &display_record,
  955. NULL,
  956. &sync_cb,
  957. NULL);
  958. }
  959. }
  960. static void
  961. default_ego_cb (void *cls,
  962. struct GNUNET_IDENTITY_Ego *ego,
  963. void **ctx,
  964. const char *name)
  965. {
  966. get_default = NULL;
  967. if (NULL == ego)
  968. {
  969. fprintf (stderr,
  970. _("No default ego configured in identity service\n"));
  971. GNUNET_SCHEDULER_shutdown ();
  972. ret = -1;
  973. return;
  974. }
  975. else
  976. {
  977. identity_cb (cls, ego);
  978. }
  979. }
  980. static void
  981. id_connect_cb (void *cls,
  982. struct GNUNET_IDENTITY_Ego *ego,
  983. void **ctx,
  984. const char *name)
  985. {
  986. const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
  987. if (NULL == ego)
  988. {
  989. get_default = GNUNET_IDENTITY_get (idh,
  990. "namestore",
  991. &default_ego_cb,
  992. (void *) cfg);
  993. }
  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. if ( (NULL != args[0]) && (NULL == uri) )
  1010. uri = GNUNET_strdup (args[0]);
  1011. GNUNET_SCHEDULER_add_shutdown (&do_shutdown,
  1012. (void *) cfg);
  1013. if (NULL == ego_name)
  1014. {
  1015. idh = GNUNET_IDENTITY_connect (cfg,
  1016. &id_connect_cb,
  1017. (void *) cfg);
  1018. if (NULL == idh)
  1019. fprintf (stderr,
  1020. _("Cannot connect to identity service\n"));
  1021. ret = -1;
  1022. return;
  1023. }
  1024. el = GNUNET_IDENTITY_ego_lookup (cfg,
  1025. ego_name,
  1026. &identity_cb,
  1027. (void *) cfg);
  1028. }
  1029. /**
  1030. * The main function for gnunet-namestore.
  1031. *
  1032. * @param argc number of arguments from the command line
  1033. * @param argv command line arguments
  1034. * @return 0 ok, 1 on error
  1035. */
  1036. int
  1037. main (int argc,
  1038. char *const *argv)
  1039. {
  1040. is_public = -1;
  1041. is_shadow = -1;
  1042. struct GNUNET_GETOPT_CommandLineOption options[] = {
  1043. GNUNET_GETOPT_option_flag ('a',
  1044. "add",
  1045. gettext_noop ("add record"),
  1046. &add),
  1047. GNUNET_GETOPT_option_flag ('d',
  1048. "delete",
  1049. gettext_noop ("delete record"),
  1050. &del),
  1051. GNUNET_GETOPT_option_flag ('D',
  1052. "display",
  1053. gettext_noop ("display records"),
  1054. &list),
  1055. GNUNET_GETOPT_option_string ('e',
  1056. "expiration",
  1057. "TIME",
  1058. gettext_noop ("expiration time for record to use (for adding only), \"never\" is possible"),
  1059. &expirationstring),
  1060. GNUNET_GETOPT_option_string ('i',
  1061. "nick",
  1062. "NICKNAME",
  1063. gettext_noop ("set the desired nick name for the zone"),
  1064. &nickstring),
  1065. GNUNET_GETOPT_option_flag ('m',
  1066. "monitor",
  1067. gettext_noop ("monitor changes in the namestore"),
  1068. &monitor),
  1069. GNUNET_GETOPT_option_string ('n',
  1070. "name",
  1071. "NAME",
  1072. gettext_noop ("name of the record to add/delete/display"),
  1073. &name),
  1074. GNUNET_GETOPT_option_string ('r',
  1075. "reverse",
  1076. "PKEY",
  1077. gettext_noop ("determine our name for the given PKEY"),
  1078. &reverse_pkey),
  1079. GNUNET_GETOPT_option_string ('t',
  1080. "type",
  1081. "TYPE",
  1082. gettext_noop ("type of the record to add/delete/display"),
  1083. &typestring),
  1084. GNUNET_GETOPT_option_string ('u',
  1085. "uri",
  1086. "URI",
  1087. gettext_noop ("URI to import into our zone"),
  1088. &uri),
  1089. GNUNET_GETOPT_option_string ('V',
  1090. "value",
  1091. "VALUE",
  1092. gettext_noop ("value of the record to add/delete"),
  1093. &value),
  1094. GNUNET_GETOPT_option_flag ('p',
  1095. "public",
  1096. gettext_noop ("create or list public record"),
  1097. &is_public),
  1098. GNUNET_GETOPT_option_flag ('s',
  1099. "shadow",
  1100. gettext_noop ("create shadow record (only valid if all other records of the same type have expired"),
  1101. &is_shadow),
  1102. GNUNET_GETOPT_option_string ('z',
  1103. "zone",
  1104. "EGO",
  1105. gettext_noop ("name of the ego controlling the zone"),
  1106. &ego_name),
  1107. GNUNET_GETOPT_OPTION_END
  1108. };
  1109. if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
  1110. return 2;
  1111. GNUNET_log_setup ("gnunet-namestore",
  1112. "WARNING",
  1113. NULL);
  1114. if (GNUNET_OK !=
  1115. GNUNET_PROGRAM_run (argc,
  1116. argv,
  1117. "gnunet-namestore",
  1118. _("GNUnet zone manipulation tool"),
  1119. options,
  1120. &run, NULL))
  1121. {
  1122. GNUNET_free ((void*) argv);
  1123. GNUNET_CRYPTO_ecdsa_key_clear (&zone_pkey);
  1124. return 1;
  1125. }
  1126. GNUNET_free ((void*) argv);
  1127. GNUNET_CRYPTO_ecdsa_key_clear (&zone_pkey);
  1128. return ret;
  1129. }
  1130. /* end of gnunet-namestore.c */