gnunet-service-identity.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2013 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 identity/gnunet-service-identity.c
  18. * @brief identity management service
  19. * @author Christian Grothoff
  20. *
  21. * The purpose of this service is to manage private keys that
  22. * represent the various egos/pseudonyms/identities of a GNUnet user.
  23. *
  24. * Todo:
  25. * - auto-initialze default egos; maybe trigger default
  26. * initializations (such as gnunet-gns-import.sh?)
  27. */
  28. #include "platform.h"
  29. #include "gnunet_util_lib.h"
  30. #include "gnunet_constants.h"
  31. #include "gnunet_protocols.h"
  32. #include "gnunet_statistics_service.h"
  33. #include "gnunet_identity_service.h"
  34. #include "identity.h"
  35. /**
  36. * Information we keep about each ego.
  37. */
  38. struct Ego
  39. {
  40. /**
  41. * We keep egos in a DLL.
  42. */
  43. struct Ego *next;
  44. /**
  45. * We keep egos in a DLL.
  46. */
  47. struct Ego *prev;
  48. /**
  49. * Private key of the ego.
  50. */
  51. struct GNUNET_CRYPTO_EcdsaPrivateKey *pk;
  52. /**
  53. * String identifier for the ego.
  54. */
  55. char *identifier;
  56. };
  57. /**
  58. * Handle to our current configuration.
  59. */
  60. static const struct GNUNET_CONFIGURATION_Handle *cfg;
  61. /**
  62. * Handle to subsystem configuration which for each subsystem contains
  63. * the name of the default ego.
  64. */
  65. static struct GNUNET_CONFIGURATION_Handle *subsystem_cfg;
  66. /**
  67. * Handle to the statistics service.
  68. */
  69. static struct GNUNET_STATISTICS_Handle *stats;
  70. /**
  71. * Notification context, simplifies client broadcasts.
  72. */
  73. static struct GNUNET_NotificationContext *nc;
  74. /**
  75. * Directory where we store the identities.
  76. */
  77. static char *ego_directory;
  78. /**
  79. * Configuration file name where subsystem information is kept.
  80. */
  81. static char *subsystem_cfg_file;
  82. /**
  83. * Head of DLL of all egos.
  84. */
  85. static struct Ego *ego_head;
  86. /**
  87. * Tail of DLL of all egos.
  88. */
  89. static struct Ego *ego_tail;
  90. /**
  91. * Get the name of the file we use to store a given ego.
  92. *
  93. * @param ego ego for which we need the filename
  94. * @return full filename for the given ego
  95. */
  96. static char *
  97. get_ego_filename (struct Ego *ego)
  98. {
  99. char *filename;
  100. GNUNET_asprintf (&filename,
  101. "%s%s%s",
  102. ego_directory,
  103. DIR_SEPARATOR_STR,
  104. ego->identifier);
  105. return filename;
  106. }
  107. /**
  108. * Called whenever a client is disconnected.
  109. *
  110. * @param cls closure
  111. * @param client identification of the client
  112. * @param app_ctx @a client
  113. */
  114. static void
  115. client_disconnect_cb (void *cls,
  116. struct GNUNET_SERVICE_Client *client,
  117. void *app_ctx)
  118. {
  119. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  120. "Client %p disconnected\n",
  121. client);
  122. }
  123. /**
  124. * Add a client to our list of active clients.
  125. *
  126. * @param cls NULL
  127. * @param client client to add
  128. * @param mq message queue for @a client
  129. * @return internal namestore client structure for this client
  130. */
  131. static void *
  132. client_connect_cb (void *cls,
  133. struct GNUNET_SERVICE_Client *client,
  134. struct GNUNET_MQ_Handle *mq)
  135. {
  136. return client;
  137. }
  138. /**
  139. * Task run during shutdown.
  140. *
  141. * @param cls unused
  142. */
  143. static void
  144. shutdown_task (void *cls)
  145. {
  146. struct Ego *e;
  147. if (NULL != nc)
  148. {
  149. GNUNET_notification_context_destroy (nc);
  150. nc = NULL;
  151. }
  152. if (NULL != stats)
  153. {
  154. GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
  155. stats = NULL;
  156. }
  157. GNUNET_CONFIGURATION_destroy (subsystem_cfg);
  158. subsystem_cfg = NULL;
  159. GNUNET_free (subsystem_cfg_file);
  160. subsystem_cfg_file = NULL;
  161. GNUNET_free (ego_directory);
  162. ego_directory = NULL;
  163. while (NULL != (e = ego_head))
  164. {
  165. GNUNET_CONTAINER_DLL_remove (ego_head, ego_tail, e);
  166. GNUNET_free (e->pk);
  167. GNUNET_free (e->identifier);
  168. GNUNET_free (e);
  169. }
  170. }
  171. /**
  172. * Send a result code back to the client.
  173. *
  174. * @param client client that should receive the result code
  175. * @param result_code code to transmit
  176. * @param emsg error message to include (or NULL for none)
  177. */
  178. static void
  179. send_result_code (struct GNUNET_SERVICE_Client *client,
  180. uint32_t result_code,
  181. const char *emsg)
  182. {
  183. struct ResultCodeMessage *rcm;
  184. struct GNUNET_MQ_Envelope *env;
  185. size_t elen;
  186. if (NULL == emsg)
  187. elen = 0;
  188. else
  189. elen = strlen (emsg) + 1;
  190. env = GNUNET_MQ_msg_extra (rcm,
  191. elen,
  192. GNUNET_MESSAGE_TYPE_IDENTITY_RESULT_CODE);
  193. rcm->result_code = htonl (result_code);
  194. if (0 < elen)
  195. GNUNET_memcpy (&rcm[1], emsg, elen);
  196. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  197. "Sending result %d (%s) to client\n",
  198. (int) result_code,
  199. emsg);
  200. GNUNET_MQ_send (GNUNET_SERVICE_client_get_mq (client), env);
  201. }
  202. /**
  203. * Create an update message with information about the current state of an ego.
  204. *
  205. * @param ego ego to create message for
  206. * @return corresponding update message
  207. */
  208. static struct GNUNET_MQ_Envelope *
  209. create_update_message (struct Ego *ego)
  210. {
  211. struct UpdateMessage *um;
  212. struct GNUNET_MQ_Envelope *env;
  213. size_t name_len;
  214. name_len = (NULL == ego->identifier) ? 0 : (strlen (ego->identifier) + 1);
  215. env = GNUNET_MQ_msg_extra (um,
  216. name_len,
  217. GNUNET_MESSAGE_TYPE_IDENTITY_UPDATE);
  218. um->name_len = htons (name_len);
  219. um->end_of_list = htons (GNUNET_NO);
  220. um->private_key = *ego->pk;
  221. GNUNET_memcpy (&um[1], ego->identifier, name_len);
  222. return env;
  223. }
  224. /**
  225. * Create a set default message with information about the current state of an ego.
  226. *
  227. * @param ego ego to create message for
  228. * @param servicename name of the service to provide in the message
  229. * @return corresponding set default message
  230. */
  231. static struct GNUNET_MQ_Envelope *
  232. create_set_default_message (struct Ego *ego,
  233. const char *servicename)
  234. {
  235. struct SetDefaultMessage *sdm;
  236. struct GNUNET_MQ_Envelope *env;
  237. size_t name_len;
  238. name_len = (NULL == servicename) ? 0 : (strlen (servicename) + 1);
  239. env = GNUNET_MQ_msg_extra (sdm,
  240. name_len,
  241. GNUNET_MESSAGE_TYPE_IDENTITY_SET_DEFAULT);
  242. sdm->name_len = htons (name_len);
  243. sdm->reserved = htons (0);
  244. sdm->private_key = *ego->pk;
  245. GNUNET_memcpy (&sdm[1], servicename, name_len);
  246. return env;
  247. }
  248. /**
  249. * Handler for START message from client, sends information
  250. * about all identities to the client immediately and
  251. * adds the client to the notification context for future
  252. * updates.
  253. *
  254. * @param cls a `struct GNUNET_SERVICE_Client *`
  255. * @param message the message received
  256. */
  257. static void
  258. handle_start_message (void *cls,
  259. const struct GNUNET_MessageHeader *message)
  260. {
  261. struct GNUNET_SERVICE_Client *client = cls;
  262. struct UpdateMessage *ume;
  263. struct GNUNET_MQ_Envelope *env;
  264. struct Ego *ego;
  265. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  266. "Received START message from client\n");
  267. GNUNET_SERVICE_client_mark_monitor (client);
  268. GNUNET_SERVICE_client_disable_continue_warning (client);
  269. GNUNET_notification_context_add (nc,
  270. GNUNET_SERVICE_client_get_mq(client));
  271. for (ego = ego_head; NULL != ego; ego = ego->next)
  272. {
  273. env = create_update_message (ego);
  274. GNUNET_MQ_send (GNUNET_SERVICE_client_get_mq(client), env);
  275. }
  276. env = GNUNET_MQ_msg_extra (ume,
  277. 0,
  278. GNUNET_MESSAGE_TYPE_IDENTITY_UPDATE);
  279. ume->end_of_list = htons (GNUNET_YES);
  280. ume->name_len = htons (0);
  281. GNUNET_MQ_send (GNUNET_SERVICE_client_get_mq(client), env);
  282. GNUNET_SERVICE_client_continue (client);
  283. }
  284. /**
  285. * Handler for LOOKUP message from client, sends information
  286. * about ONE identity to the client immediately.
  287. *
  288. * @param cls unused
  289. * @param message the message received
  290. * @return #GNUNET_SYSERR if message was ill-formed
  291. */
  292. static int
  293. check_lookup_message (void *cls,
  294. const struct LookupMessage *message)
  295. {
  296. GNUNET_MQ_check_zero_termination (message);
  297. return GNUNET_OK;
  298. }
  299. /**
  300. * Handler for LOOKUP message from client, sends information
  301. * about ONE identity to the client immediately.
  302. *
  303. * @param cls a `struct GNUNET_SERVICE_Client *`
  304. * @param message the message received
  305. */
  306. static void
  307. handle_lookup_message (void *cls,
  308. const struct LookupMessage *message)
  309. {
  310. struct GNUNET_SERVICE_Client *client = cls;
  311. const char *name;
  312. struct GNUNET_MQ_Envelope *env;
  313. struct Ego *ego;
  314. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  315. "Received LOOKUP message from client\n");
  316. name = (const char *) &message[1];
  317. for (ego = ego_head; NULL != ego; ego = ego->next)
  318. {
  319. if (0 != strcasecmp (name,
  320. ego->identifier))
  321. continue;
  322. env = create_update_message (ego);
  323. GNUNET_MQ_send (GNUNET_SERVICE_client_get_mq (client), env);
  324. GNUNET_SERVICE_client_continue (client);
  325. return;
  326. }
  327. send_result_code (client,
  328. 0,
  329. "ego not found");
  330. GNUNET_SERVICE_client_continue (client);
  331. }
  332. /**
  333. * Handler for LOOKUP message from client, sends information
  334. * about ONE identity to the client immediately.
  335. *
  336. * @param cls unused
  337. * @param message the message received
  338. * @return #GNUNET_SYSERR if message was ill-formed
  339. */
  340. static int
  341. check_lookup_by_suffix_message (void *cls,
  342. const struct LookupMessage *message)
  343. {
  344. GNUNET_MQ_check_zero_termination (message);
  345. return GNUNET_OK;
  346. }
  347. /**
  348. * Handler for LOOKUP_BY_SUFFIX message from client, sends information
  349. * about ONE identity to the client immediately.
  350. *
  351. * @param cls a `struct GNUNET_SERVICE_Client *`
  352. * @param message the message received
  353. */
  354. static void
  355. handle_lookup_by_suffix_message (void *cls,
  356. const struct LookupMessage *message)
  357. {
  358. struct GNUNET_SERVICE_Client *client = cls;
  359. const char *name;
  360. struct GNUNET_MQ_Envelope *env;
  361. struct Ego *lprefix;
  362. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  363. "Received LOOKUP_BY_SUFFIX message from client\n");
  364. name = (const char *) &message[1];
  365. lprefix = NULL;
  366. for (struct Ego *ego = ego_head; NULL != ego; ego = ego->next)
  367. {
  368. if ((strlen (ego->identifier) <= strlen (name)) &&
  369. (0 == strcmp (ego->identifier,
  370. &name[strlen (name) - strlen (ego->identifier)])) &&
  371. ((strlen (name) == strlen (ego->identifier)) ||
  372. ('.' == name[strlen (name) -
  373. strlen (ego->identifier) - 1])) &&
  374. ((NULL == lprefix) ||
  375. (strlen (ego->identifier) > strlen (lprefix->identifier))))
  376. {
  377. /* found better match, update! */
  378. lprefix = ego;
  379. }
  380. }
  381. if (NULL != lprefix)
  382. {
  383. env = create_update_message (lprefix);
  384. GNUNET_MQ_send (GNUNET_SERVICE_client_get_mq (client), env);
  385. GNUNET_SERVICE_client_continue (client);
  386. return;
  387. }
  388. send_result_code (client,
  389. 0,
  390. "ego not found");
  391. GNUNET_SERVICE_client_continue (client);
  392. }
  393. /**
  394. * Checks a #GNUNET_MESSAGE_TYPE_IDENTITY_GET_DEFAULT message
  395. *
  396. * @param cls client sending the message
  397. * @param msg message of type `struct GetDefaultMessage`
  398. * @return #GNUNET_OK if @a msg is well-formed
  399. */
  400. static int
  401. check_get_default_message (void *cls,
  402. const struct GetDefaultMessage *msg)
  403. {
  404. uint16_t size;
  405. uint16_t name_len;
  406. const char *name;
  407. size = ntohs (msg->header.size);
  408. if (size <= sizeof (struct GetDefaultMessage))
  409. {
  410. GNUNET_break (0);
  411. return GNUNET_SYSERR;
  412. }
  413. name = (const char *) &msg[1];
  414. name_len = ntohs (msg->name_len);
  415. if ( (name_len + sizeof (struct GetDefaultMessage) != size) ||
  416. (0 != ntohs (msg->reserved)) ||
  417. ('\0' != name[name_len - 1]) )
  418. {
  419. GNUNET_break (0);
  420. return GNUNET_SYSERR;
  421. }
  422. return GNUNET_OK;
  423. }
  424. /**
  425. * Handler for GET_DEFAULT message from client, returns
  426. * default identity for some service.
  427. *
  428. * @param cls unused
  429. * @param client who sent the message
  430. * @param message the message received
  431. */
  432. static void
  433. handle_get_default_message (void *cls,
  434. const struct GetDefaultMessage *gdm)
  435. {
  436. struct GNUNET_MQ_Envelope *env;
  437. struct GNUNET_SERVICE_Client *client = cls;
  438. struct Ego *ego;
  439. char *name;
  440. char *identifier;
  441. name = GNUNET_strdup ((const char *) &gdm[1]);
  442. GNUNET_STRINGS_utf8_tolower ((const char *) &gdm[1], name);
  443. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  444. "Received GET_DEFAULT for service `%s' from client\n",
  445. name);
  446. if (GNUNET_OK !=
  447. GNUNET_CONFIGURATION_get_value_string (subsystem_cfg,
  448. name,
  449. "DEFAULT_IDENTIFIER",
  450. &identifier))
  451. {
  452. send_result_code (client, 1, gettext_noop ("no default known"));
  453. GNUNET_SERVICE_client_continue (client);
  454. GNUNET_free (name);
  455. return;
  456. }
  457. for (ego = ego_head; NULL != ego; ego = ego->next)
  458. {
  459. if (0 == strcmp (ego->identifier,
  460. identifier))
  461. {
  462. env = create_set_default_message (ego,
  463. name);
  464. GNUNET_MQ_send (GNUNET_SERVICE_client_get_mq (client), env);
  465. GNUNET_SERVICE_client_continue (client);
  466. GNUNET_free (identifier);
  467. GNUNET_free (name);
  468. return;
  469. }
  470. }
  471. GNUNET_free (identifier);
  472. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  473. "Failed to find ego `%s'\n",
  474. name);
  475. GNUNET_free (name);
  476. send_result_code (client, 1,
  477. gettext_noop ("default configured, but ego unknown (internal error)"));
  478. GNUNET_SERVICE_client_continue (client);
  479. }
  480. /**
  481. * Compare the given two private keys for equality.
  482. *
  483. * @param pk1 one private key
  484. * @param pk2 another private key
  485. * @return 0 if the keys are equal
  486. */
  487. static int
  488. key_cmp (const struct GNUNET_CRYPTO_EcdsaPrivateKey *pk1,
  489. const struct GNUNET_CRYPTO_EcdsaPrivateKey *pk2)
  490. {
  491. return GNUNET_memcmp (pk1, pk2);
  492. }
  493. /**
  494. * Checks a #GNUNET_MESSAGE_TYPE_IDENTITY_SET_DEFAULT message
  495. *
  496. * @param cls client sending the message
  497. * @param msg message of type `struct SetDefaultMessage`
  498. * @return #GNUNET_OK if @a msg is well-formed
  499. */
  500. static int
  501. check_set_default_message (void *cls,
  502. const struct SetDefaultMessage *msg)
  503. {
  504. uint16_t size;
  505. uint16_t name_len;
  506. const char *str;
  507. size = ntohs (msg->header.size);
  508. if (size <= sizeof (struct SetDefaultMessage))
  509. {
  510. GNUNET_break (0);
  511. return GNUNET_SYSERR;
  512. }
  513. name_len = ntohs (msg->name_len);
  514. GNUNET_break (0 == ntohs (msg->reserved));
  515. if (name_len + sizeof (struct SetDefaultMessage) != size)
  516. {
  517. GNUNET_break (0);
  518. return GNUNET_SYSERR;
  519. }
  520. str = (const char *) &msg[1];
  521. if ('\0' != str[name_len - 1])
  522. {
  523. GNUNET_break (0);
  524. return GNUNET_SYSERR;
  525. }
  526. return GNUNET_OK;
  527. }
  528. /**
  529. * Handler for SET_DEFAULT message from client, updates
  530. * default identity for some service.
  531. *
  532. * @param cls unused
  533. * @param client who sent the message
  534. * @param message the message received
  535. */
  536. static void
  537. handle_set_default_message (void *cls,
  538. const struct SetDefaultMessage *sdm)
  539. {
  540. struct Ego *ego;
  541. struct GNUNET_SERVICE_Client *client = cls;
  542. char *str;
  543. str = GNUNET_strdup ((const char *) &sdm[1]);
  544. GNUNET_STRINGS_utf8_tolower ((const char *) &sdm[1], str);
  545. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  546. "Received SET_DEFAULT for service `%s' from client\n",
  547. str);
  548. for (ego = ego_head; NULL != ego; ego = ego->next)
  549. {
  550. if (0 == key_cmp (ego->pk,
  551. &sdm->private_key))
  552. {
  553. GNUNET_CONFIGURATION_set_value_string (subsystem_cfg,
  554. str,
  555. "DEFAULT_IDENTIFIER",
  556. ego->identifier);
  557. if (GNUNET_OK !=
  558. GNUNET_CONFIGURATION_write (subsystem_cfg,
  559. subsystem_cfg_file))
  560. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  561. _("Failed to write subsystem default identifier map to `%s'.\n"),
  562. subsystem_cfg_file);
  563. send_result_code (client, 0, NULL);
  564. GNUNET_SERVICE_client_continue (client);
  565. GNUNET_free (str);
  566. return;
  567. }
  568. }
  569. send_result_code (client, 1, _("Unknown ego specified for service (internal error)"));
  570. GNUNET_free (str);
  571. GNUNET_SERVICE_client_continue (client);
  572. }
  573. /**
  574. * Send an updated message for the given ego to all listeners.
  575. *
  576. * @param ego ego to send the update for
  577. */
  578. static void
  579. notify_listeners (struct Ego *ego)
  580. {
  581. struct UpdateMessage *um;
  582. size_t name_len;
  583. name_len = (NULL == ego->identifier) ? 0 : (strlen (ego->identifier) + 1);
  584. um = GNUNET_malloc (sizeof (struct UpdateMessage) + name_len);
  585. um->header.type = htons (GNUNET_MESSAGE_TYPE_IDENTITY_UPDATE);
  586. um->header.size = htons (sizeof (struct UpdateMessage) + name_len);
  587. um->name_len = htons (name_len);
  588. um->end_of_list = htons (GNUNET_NO);
  589. um->private_key = *ego->pk;
  590. GNUNET_memcpy (&um[1], ego->identifier, name_len);
  591. GNUNET_notification_context_broadcast (nc,
  592. &um->header,
  593. GNUNET_NO);
  594. GNUNET_free (um);
  595. }
  596. /**
  597. * Checks a #GNUNET_MESSAGE_TYPE_IDENTITY_CREATE message
  598. *
  599. * @param cls client sending the message
  600. * @param msg message of type `struct CreateRequestMessage`
  601. * @return #GNUNET_OK if @a msg is well-formed
  602. */
  603. static int
  604. check_create_message (void *cls,
  605. const struct CreateRequestMessage *msg)
  606. {
  607. uint16_t size;
  608. uint16_t name_len;
  609. const char *str;
  610. size = ntohs (msg->header.size);
  611. if (size <= sizeof (struct CreateRequestMessage))
  612. {
  613. GNUNET_break (0);
  614. return GNUNET_SYSERR;
  615. }
  616. name_len = ntohs (msg->name_len);
  617. GNUNET_break (0 == ntohs (msg->reserved));
  618. if (name_len + sizeof (struct CreateRequestMessage) != size)
  619. {
  620. GNUNET_break (0);
  621. return GNUNET_SYSERR;
  622. }
  623. str = (const char *) &msg[1];
  624. if ('\0' != str[name_len - 1])
  625. {
  626. GNUNET_break (0);
  627. return GNUNET_SYSERR;
  628. }
  629. return GNUNET_OK;
  630. }
  631. /**
  632. * Handler for CREATE message from client, creates
  633. * new identity.
  634. *
  635. * @param cls unused
  636. * @param client who sent the message
  637. * @param message the message received
  638. */
  639. static void
  640. handle_create_message (void *cls,
  641. const struct CreateRequestMessage *crm)
  642. {
  643. struct GNUNET_SERVICE_Client *client = cls;
  644. struct Ego *ego;
  645. char *str;
  646. char *fn;
  647. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  648. "Received CREATE message from client\n");
  649. str = GNUNET_strdup ((const char *) &crm[1]);
  650. GNUNET_STRINGS_utf8_tolower ((const char *) &crm[1], str);
  651. for (ego = ego_head; NULL != ego; ego = ego->next)
  652. {
  653. if (0 == strcmp (ego->identifier,
  654. str))
  655. {
  656. send_result_code (client, 1, gettext_noop ("identifier already in use for another ego"));
  657. GNUNET_SERVICE_client_continue (client);
  658. GNUNET_free (str);
  659. return;
  660. }
  661. }
  662. ego = GNUNET_new (struct Ego);
  663. ego->pk = GNUNET_new (struct GNUNET_CRYPTO_EcdsaPrivateKey);
  664. *ego->pk = crm->private_key;
  665. ego->identifier = GNUNET_strdup (str);
  666. GNUNET_CONTAINER_DLL_insert (ego_head,
  667. ego_tail,
  668. ego);
  669. send_result_code (client, 0, NULL);
  670. fn = get_ego_filename (ego);
  671. (void) GNUNET_DISK_directory_create_for_file (fn);
  672. if (sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey) !=
  673. GNUNET_DISK_fn_write (fn,
  674. &crm->private_key,
  675. sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey),
  676. GNUNET_DISK_PERM_USER_READ |
  677. GNUNET_DISK_PERM_USER_WRITE))
  678. GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
  679. "write", fn);
  680. GNUNET_free (fn);
  681. GNUNET_free (str);
  682. notify_listeners (ego);
  683. GNUNET_SERVICE_client_continue (client);
  684. }
  685. /**
  686. * Closure for 'handle_ego_rename'.
  687. */
  688. struct RenameContext
  689. {
  690. /**
  691. * Old name.
  692. */
  693. const char *old_name;
  694. /**
  695. * New name.
  696. */
  697. const char *new_name;
  698. };
  699. /**
  700. * An ego was renamed; rename it in all subsystems where it is
  701. * currently set as the default.
  702. *
  703. * @param cls the 'struct RenameContext'
  704. * @param section a section in the configuration to process
  705. */
  706. static void
  707. handle_ego_rename (void *cls,
  708. const char *section)
  709. {
  710. struct RenameContext *rc = cls;
  711. char *id;
  712. if (GNUNET_OK !=
  713. GNUNET_CONFIGURATION_get_value_string (subsystem_cfg,
  714. section,
  715. "DEFAULT_IDENTIFIER",
  716. &id))
  717. return;
  718. if (0 != strcmp (id, rc->old_name))
  719. {
  720. GNUNET_free (id);
  721. return;
  722. }
  723. GNUNET_CONFIGURATION_set_value_string (subsystem_cfg,
  724. section,
  725. "DEFAULT_IDENTIFIER",
  726. rc->new_name);
  727. GNUNET_free (id);
  728. }
  729. /**
  730. * Checks a #GNUNET_MESSAGE_TYPE_IDENTITY_RENAME message
  731. *
  732. * @param cls client sending the message
  733. * @param msg message of type `struct RenameMessage`
  734. * @return #GNUNET_OK if @a msg is well-formed
  735. */
  736. static int
  737. check_rename_message (void *cls,
  738. const struct RenameMessage *msg)
  739. {
  740. uint16_t size;
  741. uint16_t old_name_len;
  742. uint16_t new_name_len;
  743. const char *old_name;
  744. const char *new_name;
  745. size = ntohs (msg->header.size);
  746. if (size <= sizeof (struct RenameMessage))
  747. {
  748. GNUNET_break (0);
  749. return GNUNET_SYSERR;
  750. }
  751. old_name_len = ntohs (msg->old_name_len);
  752. new_name_len = ntohs (msg->new_name_len);
  753. old_name = (const char *) &msg[1];
  754. new_name = &old_name[old_name_len];
  755. if ( (old_name_len + new_name_len + sizeof (struct RenameMessage) != size) ||
  756. ('\0' != old_name[old_name_len - 1]) ||
  757. ('\0' != new_name[new_name_len - 1]) )
  758. {
  759. GNUNET_break (0);
  760. return GNUNET_SYSERR;
  761. }
  762. return GNUNET_OK;
  763. }
  764. /**
  765. * Handler for RENAME message from client, creates
  766. * new identity.
  767. *
  768. * @param cls unused
  769. * @param client who sent the message
  770. * @param message the message received
  771. */
  772. static void
  773. handle_rename_message (void *cls,
  774. const struct RenameMessage *rm)
  775. {
  776. uint16_t old_name_len;
  777. struct Ego *ego;
  778. char *old_name;
  779. char *new_name;
  780. struct RenameContext rename_ctx;
  781. struct GNUNET_SERVICE_Client *client = cls;
  782. char *fn_old;
  783. char *fn_new;
  784. const char *old_name_tmp;
  785. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  786. "Received RENAME message from client\n");
  787. old_name_len = ntohs (rm->old_name_len);
  788. old_name_tmp = (const char *) &rm[1];
  789. old_name = GNUNET_strdup (old_name_tmp);
  790. GNUNET_STRINGS_utf8_tolower (old_name_tmp, old_name);
  791. new_name = GNUNET_strdup (&old_name_tmp[old_name_len]);
  792. GNUNET_STRINGS_utf8_tolower (&old_name_tmp[old_name_len], new_name);
  793. /* check if new name is already in use */
  794. for (ego = ego_head; NULL != ego; ego = ego->next)
  795. {
  796. if (0 == strcmp (ego->identifier,
  797. new_name))
  798. {
  799. send_result_code (client, 1, gettext_noop ("target name already exists"));
  800. GNUNET_SERVICE_client_continue (client);
  801. GNUNET_free (old_name);
  802. GNUNET_free (new_name);
  803. return;
  804. }
  805. }
  806. /* locate old name and, if found, perform rename */
  807. for (ego = ego_head; NULL != ego; ego = ego->next)
  808. {
  809. if (0 == strcmp (ego->identifier,
  810. old_name))
  811. {
  812. fn_old = get_ego_filename (ego);
  813. GNUNET_free (ego->identifier);
  814. rename_ctx.old_name = old_name;
  815. rename_ctx.new_name = new_name;
  816. GNUNET_CONFIGURATION_iterate_sections (subsystem_cfg,
  817. &handle_ego_rename,
  818. &rename_ctx);
  819. if (GNUNET_OK !=
  820. GNUNET_CONFIGURATION_write (subsystem_cfg,
  821. subsystem_cfg_file))
  822. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  823. _("Failed to write subsystem default identifier map to `%s'.\n"),
  824. subsystem_cfg_file);
  825. ego->identifier = GNUNET_strdup (new_name);
  826. fn_new = get_ego_filename (ego);
  827. if (0 != RENAME (fn_old, fn_new))
  828. GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "rename", fn_old);
  829. GNUNET_free (fn_old);
  830. GNUNET_free (fn_new);
  831. GNUNET_free (old_name);
  832. GNUNET_free (new_name);
  833. notify_listeners (ego);
  834. send_result_code (client, 0, NULL);
  835. GNUNET_SERVICE_client_continue (client);
  836. return;
  837. }
  838. }
  839. /* failed to locate old name */
  840. send_result_code (client, 1, gettext_noop ("no matching ego found"));
  841. GNUNET_free (old_name);
  842. GNUNET_free (new_name);
  843. GNUNET_SERVICE_client_continue (client);
  844. }
  845. /**
  846. * An ego was removed, remove it from all subsystems where it is
  847. * currently set as the default.
  848. *
  849. * @param cls name of the removed ego (const char *)
  850. * @param section a section in the configuration to process
  851. */
  852. static void
  853. handle_ego_delete (void *cls,
  854. const char *section)
  855. {
  856. const char *identifier = cls;
  857. char *id;
  858. if (GNUNET_OK !=
  859. GNUNET_CONFIGURATION_get_value_string (subsystem_cfg,
  860. section,
  861. "DEFAULT_IDENTIFIER",
  862. &id))
  863. return;
  864. if (0 != strcmp (id, identifier))
  865. {
  866. GNUNET_free (id);
  867. return;
  868. }
  869. GNUNET_CONFIGURATION_set_value_string (subsystem_cfg,
  870. section,
  871. "DEFAULT_IDENTIFIER",
  872. NULL);
  873. GNUNET_free (id);
  874. }
  875. /**
  876. * Checks a #GNUNET_MESSAGE_TYPE_IDENTITY_DELETE message
  877. *
  878. * @param cls client sending the message
  879. * @param msg message of type `struct DeleteMessage`
  880. * @return #GNUNET_OK if @a msg is well-formed
  881. */
  882. static int
  883. check_delete_message (void *cls,
  884. const struct DeleteMessage *msg)
  885. {
  886. uint16_t size;
  887. uint16_t name_len;
  888. const char *name;
  889. size = ntohs (msg->header.size);
  890. if (size <= sizeof (struct DeleteMessage))
  891. {
  892. GNUNET_break (0);
  893. return GNUNET_SYSERR;
  894. }
  895. name = (const char *) &msg[1];
  896. name_len = ntohs (msg->name_len);
  897. if ( (name_len + sizeof (struct DeleteMessage) != size) ||
  898. (0 != ntohs (msg->reserved)) ||
  899. ('\0' != name[name_len - 1]) )
  900. {
  901. GNUNET_break (0);
  902. return GNUNET_SYSERR;
  903. }
  904. return GNUNET_OK;
  905. }
  906. /**
  907. * Handler for DELETE message from client, creates
  908. * new identity.
  909. *
  910. * @param cls unused
  911. * @param client who sent the message
  912. * @param message the message received
  913. */
  914. static void
  915. handle_delete_message (void *cls,
  916. const struct DeleteMessage *dm)
  917. {
  918. struct Ego *ego;
  919. char *name;
  920. char *fn;
  921. struct GNUNET_SERVICE_Client *client = cls;
  922. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  923. "Received DELETE message from client\n");
  924. name = GNUNET_strdup ((const char *) &dm[1]);
  925. GNUNET_STRINGS_utf8_tolower ((const char *) &dm[1], name);
  926. for (ego = ego_head; NULL != ego; ego = ego->next)
  927. {
  928. if (0 == strcmp (ego->identifier,
  929. name))
  930. {
  931. GNUNET_CONTAINER_DLL_remove (ego_head,
  932. ego_tail,
  933. ego);
  934. GNUNET_CONFIGURATION_iterate_sections (subsystem_cfg,
  935. &handle_ego_delete,
  936. ego->identifier);
  937. if (GNUNET_OK !=
  938. GNUNET_CONFIGURATION_write (subsystem_cfg,
  939. subsystem_cfg_file))
  940. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  941. _("Failed to write subsystem default identifier map to `%s'.\n"),
  942. subsystem_cfg_file);
  943. fn = get_ego_filename (ego);
  944. if (0 != UNLINK (fn))
  945. GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", fn);
  946. GNUNET_free (fn);
  947. GNUNET_free (ego->identifier);
  948. ego->identifier = NULL;
  949. notify_listeners (ego);
  950. GNUNET_free (ego->pk);
  951. GNUNET_free (ego);
  952. GNUNET_free (name);
  953. send_result_code (client, 0, NULL);
  954. GNUNET_SERVICE_client_continue (client);
  955. return;
  956. }
  957. }
  958. send_result_code (client, 1, gettext_noop ("no matching ego found"));
  959. GNUNET_free (name);
  960. GNUNET_SERVICE_client_continue (client);
  961. }
  962. /**
  963. * Process the given file from the "EGODIR". Parses the file
  964. * and creates the respective 'struct Ego' in memory.
  965. *
  966. * @param cls NULL
  967. * @param filename name of the file to parse
  968. * @return #GNUNET_OK to continue to iterate,
  969. * #GNUNET_NO to stop iteration with no error,
  970. * #GNUNET_SYSERR to abort iteration with error!
  971. */
  972. static int
  973. process_ego_file (void *cls,
  974. const char *filename)
  975. {
  976. struct Ego *ego;
  977. const char *fn;
  978. fn = strrchr (filename, (int) DIR_SEPARATOR);
  979. if (NULL == fn)
  980. {
  981. GNUNET_break (0);
  982. return GNUNET_OK;
  983. }
  984. ego = GNUNET_new (struct Ego);
  985. ego->pk = GNUNET_CRYPTO_ecdsa_key_create_from_file (filename);
  986. if (NULL == ego->pk)
  987. {
  988. GNUNET_free (ego);
  989. GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
  990. _("Failed to parse ego information in `%s'\n"),
  991. filename);
  992. return GNUNET_OK;
  993. }
  994. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  995. "Loaded ego `%s'\n",
  996. fn + 1);
  997. ego->identifier = GNUNET_strdup (fn + 1);
  998. GNUNET_CONTAINER_DLL_insert (ego_head,
  999. ego_tail,
  1000. ego);
  1001. return GNUNET_OK;
  1002. }
  1003. /**
  1004. * Handle network size estimate clients.
  1005. *
  1006. * @param cls closure
  1007. * @param server the initialized server
  1008. * @param c configuration to use
  1009. */
  1010. static void
  1011. run (void *cls,
  1012. const struct GNUNET_CONFIGURATION_Handle *c,
  1013. struct GNUNET_SERVICE_Handle *service)
  1014. {
  1015. cfg = c;
  1016. nc = GNUNET_notification_context_create (1);
  1017. if (GNUNET_OK !=
  1018. GNUNET_CONFIGURATION_get_value_filename (cfg, "identity",
  1019. "EGODIR",
  1020. &ego_directory))
  1021. {
  1022. GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, "identity", "EGODIR");
  1023. GNUNET_SCHEDULER_shutdown ();
  1024. return;
  1025. }
  1026. if (GNUNET_OK !=
  1027. GNUNET_CONFIGURATION_get_value_filename (cfg, "identity",
  1028. "SUBSYSTEM_CFG",
  1029. &subsystem_cfg_file))
  1030. {
  1031. GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, "identity", "SUBSYSTEM_CFG");
  1032. GNUNET_SCHEDULER_shutdown ();
  1033. return;
  1034. }
  1035. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1036. "Loading subsystem configuration `%s'\n",
  1037. subsystem_cfg_file);
  1038. subsystem_cfg = GNUNET_CONFIGURATION_create ();
  1039. if ( (GNUNET_YES ==
  1040. GNUNET_DISK_file_test (subsystem_cfg_file)) &&
  1041. (GNUNET_OK !=
  1042. GNUNET_CONFIGURATION_parse (subsystem_cfg,
  1043. subsystem_cfg_file)) )
  1044. {
  1045. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  1046. _("Failed to parse subsystem identity configuration file `%s'\n"),
  1047. subsystem_cfg_file);
  1048. GNUNET_SCHEDULER_shutdown ();
  1049. return;
  1050. }
  1051. stats = GNUNET_STATISTICS_create ("identity", cfg);
  1052. if (GNUNET_OK !=
  1053. GNUNET_DISK_directory_create (ego_directory))
  1054. {
  1055. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  1056. _("Failed to create directory `%s' for storing egos\n"),
  1057. ego_directory);
  1058. }
  1059. GNUNET_DISK_directory_scan (ego_directory,
  1060. &process_ego_file,
  1061. NULL);
  1062. GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
  1063. NULL);
  1064. }
  1065. /**
  1066. * Define "main" method using service macro.
  1067. */
  1068. GNUNET_SERVICE_MAIN
  1069. ("identity",
  1070. GNUNET_SERVICE_OPTION_NONE,
  1071. &run,
  1072. &client_connect_cb,
  1073. &client_disconnect_cb,
  1074. NULL,
  1075. GNUNET_MQ_hd_fixed_size (start_message,
  1076. GNUNET_MESSAGE_TYPE_IDENTITY_START,
  1077. struct GNUNET_MessageHeader,
  1078. NULL),
  1079. GNUNET_MQ_hd_var_size (lookup_message,
  1080. GNUNET_MESSAGE_TYPE_IDENTITY_LOOKUP,
  1081. struct LookupMessage,
  1082. NULL),
  1083. GNUNET_MQ_hd_var_size (lookup_by_suffix_message,
  1084. GNUNET_MESSAGE_TYPE_IDENTITY_LOOKUP_BY_SUFFIX,
  1085. struct LookupMessage,
  1086. NULL),
  1087. GNUNET_MQ_hd_var_size (get_default_message,
  1088. GNUNET_MESSAGE_TYPE_IDENTITY_GET_DEFAULT,
  1089. struct GetDefaultMessage,
  1090. NULL),
  1091. GNUNET_MQ_hd_var_size (set_default_message,
  1092. GNUNET_MESSAGE_TYPE_IDENTITY_SET_DEFAULT,
  1093. struct SetDefaultMessage,
  1094. NULL),
  1095. GNUNET_MQ_hd_var_size (create_message,
  1096. GNUNET_MESSAGE_TYPE_IDENTITY_CREATE,
  1097. struct CreateRequestMessage,
  1098. NULL),
  1099. GNUNET_MQ_hd_var_size (rename_message,
  1100. GNUNET_MESSAGE_TYPE_IDENTITY_RENAME,
  1101. struct RenameMessage,
  1102. NULL),
  1103. GNUNET_MQ_hd_var_size (delete_message,
  1104. GNUNET_MESSAGE_TYPE_IDENTITY_DELETE,
  1105. struct DeleteMessage,
  1106. NULL),
  1107. GNUNET_MQ_handler_end());
  1108. /* end of gnunet-service-identity.c */