gnunet-service-identity.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253
  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_IDENTITY_PrivateKey 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,
  166. ego_tail,
  167. e);
  168. GNUNET_free (e->identifier);
  169. GNUNET_free (e);
  170. }
  171. }
  172. /**
  173. * Send a result code back to the client.
  174. *
  175. * @param client client that should receive the result code
  176. * @param result_code code to transmit
  177. * @param emsg error message to include (or NULL for none)
  178. */
  179. static void
  180. send_result_code (struct GNUNET_SERVICE_Client *client,
  181. uint32_t result_code,
  182. const char *emsg)
  183. {
  184. struct ResultCodeMessage *rcm;
  185. struct GNUNET_MQ_Envelope *env;
  186. size_t elen;
  187. if (NULL == emsg)
  188. elen = 0;
  189. else
  190. elen = strlen (emsg) + 1;
  191. env =
  192. GNUNET_MQ_msg_extra (rcm, elen, 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, name_len, GNUNET_MESSAGE_TYPE_IDENTITY_UPDATE);
  216. um->name_len = htons (name_len);
  217. um->end_of_list = htons (GNUNET_NO);
  218. um->private_key = ego->pk;
  219. GNUNET_memcpy (&um[1], ego->identifier, name_len);
  220. return env;
  221. }
  222. /**
  223. * Create a set default message with information about the current state of an ego.
  224. *
  225. * @param ego ego to create message for
  226. * @param servicename name of the service to provide in the message
  227. * @return corresponding set default message
  228. */
  229. static struct GNUNET_MQ_Envelope *
  230. create_set_default_message (struct Ego *ego,
  231. const char *servicename)
  232. {
  233. struct SetDefaultMessage *sdm;
  234. struct GNUNET_MQ_Envelope *env;
  235. size_t name_len;
  236. name_len = (NULL == servicename) ? 0 : (strlen (servicename) + 1);
  237. env = GNUNET_MQ_msg_extra (sdm,
  238. name_len,
  239. GNUNET_MESSAGE_TYPE_IDENTITY_SET_DEFAULT);
  240. sdm->name_len = htons (name_len);
  241. sdm->reserved = htons (0);
  242. sdm->private_key = ego->pk;
  243. GNUNET_memcpy (&sdm[1], servicename, name_len);
  244. return env;
  245. }
  246. /**
  247. * Handler for START message from client, sends information
  248. * about all identities to the client immediately and
  249. * adds the client to the notification context for future
  250. * updates.
  251. *
  252. * @param cls a `struct GNUNET_SERVICE_Client *`
  253. * @param message the message received
  254. */
  255. static void
  256. handle_start_message (void *cls,
  257. const struct GNUNET_MessageHeader *message)
  258. {
  259. struct GNUNET_SERVICE_Client *client = cls;
  260. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  261. "Received START message from client\n");
  262. GNUNET_SERVICE_client_mark_monitor (client);
  263. GNUNET_SERVICE_client_disable_continue_warning (client);
  264. GNUNET_notification_context_add (nc,
  265. GNUNET_SERVICE_client_get_mq (client));
  266. for (struct Ego *ego = ego_head; NULL != ego; ego = ego->next)
  267. {
  268. GNUNET_MQ_send (GNUNET_SERVICE_client_get_mq (client),
  269. create_update_message (ego));
  270. }
  271. {
  272. struct UpdateMessage *ume;
  273. struct GNUNET_MQ_Envelope *env;
  274. env = GNUNET_MQ_msg_extra (ume,
  275. 0,
  276. GNUNET_MESSAGE_TYPE_IDENTITY_UPDATE);
  277. ume->end_of_list = htons (GNUNET_YES);
  278. ume->name_len = htons (0);
  279. GNUNET_MQ_send (GNUNET_SERVICE_client_get_mq (client),
  280. env);
  281. }
  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, ego->identifier))
  320. continue;
  321. env = create_update_message (ego);
  322. GNUNET_MQ_send (GNUNET_SERVICE_client_get_mq (client), env);
  323. GNUNET_SERVICE_client_continue (client);
  324. return;
  325. }
  326. send_result_code (client, 0, "ego not found");
  327. GNUNET_SERVICE_client_continue (client);
  328. }
  329. /**
  330. * Handler for LOOKUP message from client, sends information
  331. * about ONE identity to the client immediately.
  332. *
  333. * @param cls unused
  334. * @param message the message received
  335. * @return #GNUNET_SYSERR if message was ill-formed
  336. */
  337. static int
  338. check_lookup_by_suffix_message (void *cls,
  339. const struct LookupMessage *message)
  340. {
  341. GNUNET_MQ_check_zero_termination (message);
  342. return GNUNET_OK;
  343. }
  344. /**
  345. * Handler for LOOKUP_BY_SUFFIX message from client, sends information
  346. * about ONE identity to the client immediately.
  347. *
  348. * @param cls a `struct GNUNET_SERVICE_Client *`
  349. * @param message the message received
  350. */
  351. static void
  352. handle_lookup_by_suffix_message (void *cls,
  353. const struct LookupMessage *message)
  354. {
  355. struct GNUNET_SERVICE_Client *client = cls;
  356. const char *name;
  357. struct GNUNET_MQ_Envelope *env;
  358. struct Ego *lprefix;
  359. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  360. "Received LOOKUP_BY_SUFFIX message from client\n");
  361. name = (const char *) &message[1];
  362. lprefix = NULL;
  363. for (struct Ego *ego = ego_head; NULL != ego; ego = ego->next)
  364. {
  365. if ((strlen (ego->identifier) <= strlen (name)) &&
  366. (0 == strcmp (ego->identifier,
  367. &name[strlen (name) - strlen (ego->identifier)])) &&
  368. ((strlen (name) == strlen (ego->identifier)) ||
  369. ('.' == name[strlen (name) - strlen (ego->identifier) - 1])) &&
  370. ((NULL == lprefix) ||
  371. (strlen (ego->identifier) > strlen (lprefix->identifier))))
  372. {
  373. /* found better match, update! */
  374. lprefix = ego;
  375. }
  376. }
  377. if (NULL != lprefix)
  378. {
  379. env = create_update_message (lprefix);
  380. GNUNET_MQ_send (GNUNET_SERVICE_client_get_mq (client), env);
  381. GNUNET_SERVICE_client_continue (client);
  382. return;
  383. }
  384. send_result_code (client, 0, "ego not found");
  385. GNUNET_SERVICE_client_continue (client);
  386. }
  387. /**
  388. * Checks a #GNUNET_MESSAGE_TYPE_IDENTITY_GET_DEFAULT message
  389. *
  390. * @param cls client sending the message
  391. * @param msg message of type `struct GetDefaultMessage`
  392. * @return #GNUNET_OK if @a msg is well-formed
  393. */
  394. static int
  395. check_get_default_message (void *cls,
  396. const struct GetDefaultMessage *msg)
  397. {
  398. uint16_t size;
  399. uint16_t name_len;
  400. const char *name;
  401. size = ntohs (msg->header.size);
  402. if (size <= sizeof(struct GetDefaultMessage))
  403. {
  404. GNUNET_break (0);
  405. return GNUNET_SYSERR;
  406. }
  407. name = (const char *) &msg[1];
  408. name_len = ntohs (msg->name_len);
  409. if ((name_len + sizeof(struct GetDefaultMessage) != size) ||
  410. (0 != ntohs (msg->reserved)) || ('\0' != name[name_len - 1]))
  411. {
  412. GNUNET_break (0);
  413. return GNUNET_SYSERR;
  414. }
  415. return GNUNET_OK;
  416. }
  417. /**
  418. * Handler for GET_DEFAULT message from client, returns
  419. * default identity for some service.
  420. *
  421. * @param cls unused
  422. * @param client who sent the message
  423. * @param message the message received
  424. */
  425. static void
  426. handle_get_default_message (void *cls,
  427. const struct GetDefaultMessage *gdm)
  428. {
  429. struct GNUNET_MQ_Envelope *env;
  430. struct GNUNET_SERVICE_Client *client = cls;
  431. char *name;
  432. char *identifier;
  433. name = GNUNET_strdup ((const char *) &gdm[1]);
  434. GNUNET_STRINGS_utf8_tolower ((const char *) &gdm[1],
  435. name);
  436. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  437. "Received GET_DEFAULT for service `%s' from client\n",
  438. name);
  439. if (GNUNET_OK !=
  440. GNUNET_CONFIGURATION_get_value_string (subsystem_cfg,
  441. name,
  442. "DEFAULT_IDENTIFIER",
  443. &identifier))
  444. {
  445. send_result_code (client, 1, gettext_noop ("no default known"));
  446. GNUNET_SERVICE_client_continue (client);
  447. GNUNET_free (name);
  448. return;
  449. }
  450. for (struct Ego *ego = ego_head; NULL != ego; ego = ego->next)
  451. {
  452. if (0 == strcmp (ego->identifier, identifier))
  453. {
  454. env = create_set_default_message (ego, name);
  455. GNUNET_MQ_send (GNUNET_SERVICE_client_get_mq (client), env);
  456. GNUNET_SERVICE_client_continue (client);
  457. GNUNET_free (identifier);
  458. GNUNET_free (name);
  459. return;
  460. }
  461. }
  462. GNUNET_free (identifier);
  463. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  464. "Failed to find ego `%s'\n",
  465. name);
  466. GNUNET_free (name);
  467. send_result_code (client,
  468. 1,
  469. gettext_noop (
  470. "default configured, but ego unknown (internal error)"));
  471. GNUNET_SERVICE_client_continue (client);
  472. }
  473. /**
  474. * Compare the given two private keys for equality.
  475. *
  476. * @param pk1 one private key
  477. * @param pk2 another private key
  478. * @return 0 if the keys are equal
  479. */
  480. static int
  481. key_cmp (const struct GNUNET_IDENTITY_PrivateKey *pk1,
  482. const struct GNUNET_IDENTITY_PrivateKey *pk2)
  483. {
  484. return GNUNET_memcmp (pk1, pk2);
  485. }
  486. /**
  487. * Checks a #GNUNET_MESSAGE_TYPE_IDENTITY_SET_DEFAULT message
  488. *
  489. * @param cls client sending the message
  490. * @param msg message of type `struct SetDefaultMessage`
  491. * @return #GNUNET_OK if @a msg is well-formed
  492. */
  493. static int
  494. check_set_default_message (void *cls,
  495. const struct SetDefaultMessage *msg)
  496. {
  497. uint16_t size;
  498. uint16_t name_len;
  499. const char *str;
  500. size = ntohs (msg->header.size);
  501. if (size <= sizeof(struct SetDefaultMessage))
  502. {
  503. GNUNET_break (0);
  504. return GNUNET_SYSERR;
  505. }
  506. name_len = ntohs (msg->name_len);
  507. GNUNET_break (0 == ntohs (msg->reserved));
  508. if (name_len + sizeof(struct SetDefaultMessage) != size)
  509. {
  510. GNUNET_break (0);
  511. return GNUNET_SYSERR;
  512. }
  513. str = (const char *) &msg[1];
  514. if ('\0' != str[name_len - 1])
  515. {
  516. GNUNET_break (0);
  517. return GNUNET_SYSERR;
  518. }
  519. return GNUNET_OK;
  520. }
  521. /**
  522. * Handler for SET_DEFAULT message from client, updates
  523. * default identity for some service.
  524. *
  525. * @param cls unused
  526. * @param client who sent the message
  527. * @param message the message received
  528. */
  529. static void
  530. handle_set_default_message (void *cls,
  531. const struct SetDefaultMessage *sdm)
  532. {
  533. struct Ego *ego;
  534. struct GNUNET_SERVICE_Client *client = cls;
  535. char *str;
  536. str = GNUNET_strdup ((const char *) &sdm[1]);
  537. GNUNET_STRINGS_utf8_tolower ((const char *) &sdm[1], str);
  538. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  539. "Received SET_DEFAULT for service `%s' from client\n",
  540. str);
  541. for (ego = ego_head; NULL != ego; ego = ego->next)
  542. {
  543. if (0 == key_cmp (&ego->pk,
  544. &sdm->private_key))
  545. {
  546. GNUNET_CONFIGURATION_set_value_string (subsystem_cfg,
  547. str,
  548. "DEFAULT_IDENTIFIER",
  549. ego->identifier);
  550. if (GNUNET_OK !=
  551. GNUNET_CONFIGURATION_write (subsystem_cfg, subsystem_cfg_file))
  552. GNUNET_log (
  553. GNUNET_ERROR_TYPE_ERROR,
  554. _ ("Failed to write subsystem default identifier map to `%s'.\n"),
  555. subsystem_cfg_file);
  556. send_result_code (client, 0, NULL);
  557. GNUNET_SERVICE_client_continue (client);
  558. GNUNET_free (str);
  559. return;
  560. }
  561. }
  562. send_result_code (client,
  563. 1,
  564. _ ("Unknown ego specified for service (internal error)"));
  565. GNUNET_free (str);
  566. GNUNET_SERVICE_client_continue (client);
  567. }
  568. /**
  569. * Send an updated message for the given ego to all listeners.
  570. *
  571. * @param ego ego to send the update for
  572. */
  573. static void
  574. notify_listeners (struct Ego *ego)
  575. {
  576. struct UpdateMessage *um;
  577. size_t name_len;
  578. name_len = (NULL == ego->identifier) ? 0 : (strlen (ego->identifier) + 1);
  579. um = GNUNET_malloc (sizeof(struct UpdateMessage) + name_len);
  580. um->header.type = htons (GNUNET_MESSAGE_TYPE_IDENTITY_UPDATE);
  581. um->header.size = htons (sizeof(struct UpdateMessage) + name_len);
  582. um->name_len = htons (name_len);
  583. um->end_of_list = htons (GNUNET_NO);
  584. um->private_key = ego->pk;
  585. GNUNET_memcpy (&um[1], ego->identifier, name_len);
  586. GNUNET_notification_context_broadcast (nc, &um->header, GNUNET_NO);
  587. GNUNET_free (um);
  588. }
  589. /**
  590. * Checks a #GNUNET_MESSAGE_TYPE_IDENTITY_CREATE message
  591. *
  592. * @param cls client sending the message
  593. * @param msg message of type `struct CreateRequestMessage`
  594. * @return #GNUNET_OK if @a msg is well-formed
  595. */
  596. static int
  597. check_create_message (void *cls,
  598. const struct CreateRequestMessage *msg)
  599. {
  600. uint16_t size;
  601. uint16_t name_len;
  602. const char *str;
  603. size = ntohs (msg->header.size);
  604. if (size <= sizeof(struct CreateRequestMessage))
  605. {
  606. GNUNET_break (0);
  607. return GNUNET_SYSERR;
  608. }
  609. name_len = ntohs (msg->name_len);
  610. GNUNET_break (0 == ntohs (msg->reserved));
  611. if (name_len + sizeof(struct CreateRequestMessage) != size)
  612. {
  613. GNUNET_break (0);
  614. return GNUNET_SYSERR;
  615. }
  616. str = (const char *) &msg[1];
  617. if ('\0' != str[name_len - 1])
  618. {
  619. GNUNET_break (0);
  620. return GNUNET_SYSERR;
  621. }
  622. return GNUNET_OK;
  623. }
  624. /**
  625. * Handler for CREATE message from client, creates new identity.
  626. *
  627. * @param cls unused
  628. * @param client who sent the message
  629. * @param message the message received
  630. */
  631. static void
  632. handle_create_message (void *cls,
  633. const struct CreateRequestMessage *crm)
  634. {
  635. struct GNUNET_SERVICE_Client *client = cls;
  636. struct Ego *ego;
  637. char *str;
  638. char *fn;
  639. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received CREATE message from client\n");
  640. str = GNUNET_strdup ((const char *) &crm[1]);
  641. GNUNET_STRINGS_utf8_tolower ((const char *) &crm[1], str);
  642. for (ego = ego_head; NULL != ego; ego = ego->next)
  643. {
  644. if (0 == strcmp (ego->identifier, str))
  645. {
  646. send_result_code (client,
  647. 1,
  648. gettext_noop (
  649. "identifier already in use for another ego"));
  650. GNUNET_SERVICE_client_continue (client);
  651. GNUNET_free (str);
  652. return;
  653. }
  654. }
  655. ego = GNUNET_new (struct Ego);
  656. ego->pk = crm->private_key;
  657. ego->identifier = GNUNET_strdup (str);
  658. GNUNET_CONTAINER_DLL_insert (ego_head,
  659. ego_tail,
  660. ego);
  661. send_result_code (client, 0, NULL);
  662. fn = get_ego_filename (ego);
  663. if (GNUNET_OK !=
  664. GNUNET_DISK_fn_write (fn,
  665. &crm->private_key,
  666. sizeof(struct GNUNET_IDENTITY_PrivateKey),
  667. GNUNET_DISK_PERM_USER_READ
  668. | GNUNET_DISK_PERM_USER_WRITE))
  669. GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "write", fn);
  670. GNUNET_free (fn);
  671. GNUNET_free (str);
  672. notify_listeners (ego);
  673. GNUNET_SERVICE_client_continue (client);
  674. }
  675. /**
  676. * Closure for 'handle_ego_rename'.
  677. */
  678. struct RenameContext
  679. {
  680. /**
  681. * Old name.
  682. */
  683. const char *old_name;
  684. /**
  685. * New name.
  686. */
  687. const char *new_name;
  688. };
  689. /**
  690. * An ego was renamed; rename it in all subsystems where it is
  691. * currently set as the default.
  692. *
  693. * @param cls the 'struct RenameContext'
  694. * @param section a section in the configuration to process
  695. */
  696. static void
  697. handle_ego_rename (void *cls, const char *section)
  698. {
  699. struct RenameContext *rc = cls;
  700. char *id;
  701. if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (subsystem_cfg,
  702. section,
  703. "DEFAULT_IDENTIFIER",
  704. &id))
  705. return;
  706. if (0 != strcmp (id, rc->old_name))
  707. {
  708. GNUNET_free (id);
  709. return;
  710. }
  711. GNUNET_CONFIGURATION_set_value_string (subsystem_cfg,
  712. section,
  713. "DEFAULT_IDENTIFIER",
  714. rc->new_name);
  715. GNUNET_free (id);
  716. }
  717. /**
  718. * Checks a #GNUNET_MESSAGE_TYPE_IDENTITY_RENAME message
  719. *
  720. * @param cls client sending the message
  721. * @param msg message of type `struct RenameMessage`
  722. * @return #GNUNET_OK if @a msg is well-formed
  723. */
  724. static int
  725. check_rename_message (void *cls, const struct RenameMessage *msg)
  726. {
  727. uint16_t size;
  728. uint16_t old_name_len;
  729. uint16_t new_name_len;
  730. const char *old_name;
  731. const char *new_name;
  732. size = ntohs (msg->header.size);
  733. if (size <= sizeof(struct RenameMessage))
  734. {
  735. GNUNET_break (0);
  736. return GNUNET_SYSERR;
  737. }
  738. old_name_len = ntohs (msg->old_name_len);
  739. new_name_len = ntohs (msg->new_name_len);
  740. old_name = (const char *) &msg[1];
  741. new_name = &old_name[old_name_len];
  742. if ((old_name_len + new_name_len + sizeof(struct RenameMessage) != size) ||
  743. ('\0' != old_name[old_name_len - 1]) ||
  744. ('\0' != new_name[new_name_len - 1]))
  745. {
  746. GNUNET_break (0);
  747. return GNUNET_SYSERR;
  748. }
  749. return GNUNET_OK;
  750. }
  751. /**
  752. * Handler for RENAME message from client, creates
  753. * new identity.
  754. *
  755. * @param cls unused
  756. * @param client who sent the message
  757. * @param message the message received
  758. */
  759. static void
  760. handle_rename_message (void *cls, const struct RenameMessage *rm)
  761. {
  762. uint16_t old_name_len;
  763. struct Ego *ego;
  764. char *old_name;
  765. char *new_name;
  766. struct RenameContext rename_ctx;
  767. struct GNUNET_SERVICE_Client *client = cls;
  768. char *fn_old;
  769. char *fn_new;
  770. const char *old_name_tmp;
  771. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received RENAME message from client\n");
  772. old_name_len = ntohs (rm->old_name_len);
  773. old_name_tmp = (const char *) &rm[1];
  774. old_name = GNUNET_strdup (old_name_tmp);
  775. GNUNET_STRINGS_utf8_tolower (old_name_tmp, old_name);
  776. new_name = GNUNET_strdup (&old_name_tmp[old_name_len]);
  777. GNUNET_STRINGS_utf8_tolower (&old_name_tmp[old_name_len], new_name);
  778. /* check if new name is already in use */
  779. for (ego = ego_head; NULL != ego; ego = ego->next)
  780. {
  781. if (0 == strcmp (ego->identifier, new_name))
  782. {
  783. send_result_code (client, 1, gettext_noop ("target name already exists"));
  784. GNUNET_SERVICE_client_continue (client);
  785. GNUNET_free (old_name);
  786. GNUNET_free (new_name);
  787. return;
  788. }
  789. }
  790. /* locate old name and, if found, perform rename */
  791. for (ego = ego_head; NULL != ego; ego = ego->next)
  792. {
  793. if (0 == strcmp (ego->identifier, old_name))
  794. {
  795. fn_old = get_ego_filename (ego);
  796. GNUNET_free (ego->identifier);
  797. rename_ctx.old_name = old_name;
  798. rename_ctx.new_name = new_name;
  799. GNUNET_CONFIGURATION_iterate_sections (subsystem_cfg,
  800. &handle_ego_rename,
  801. &rename_ctx);
  802. if (GNUNET_OK !=
  803. GNUNET_CONFIGURATION_write (subsystem_cfg, subsystem_cfg_file))
  804. GNUNET_log (
  805. GNUNET_ERROR_TYPE_ERROR,
  806. _ ("Failed to write subsystem default identifier map to `%s'.\n"),
  807. subsystem_cfg_file);
  808. ego->identifier = GNUNET_strdup (new_name);
  809. fn_new = get_ego_filename (ego);
  810. if (0 != rename (fn_old, fn_new))
  811. GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "rename", fn_old);
  812. GNUNET_free (fn_old);
  813. GNUNET_free (fn_new);
  814. GNUNET_free (old_name);
  815. GNUNET_free (new_name);
  816. notify_listeners (ego);
  817. send_result_code (client, 0, NULL);
  818. GNUNET_SERVICE_client_continue (client);
  819. return;
  820. }
  821. }
  822. /* failed to locate old name */
  823. send_result_code (client, 1, gettext_noop ("no matching ego found"));
  824. GNUNET_free (old_name);
  825. GNUNET_free (new_name);
  826. GNUNET_SERVICE_client_continue (client);
  827. }
  828. /**
  829. * An ego was removed, remove it from all subsystems where it is
  830. * currently set as the default.
  831. *
  832. * @param cls name of the removed ego (const char *)
  833. * @param section a section in the configuration to process
  834. */
  835. static void
  836. handle_ego_delete (void *cls, const char *section)
  837. {
  838. const char *identifier = cls;
  839. char *id;
  840. if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (subsystem_cfg,
  841. section,
  842. "DEFAULT_IDENTIFIER",
  843. &id))
  844. return;
  845. if (0 != strcmp (id, identifier))
  846. {
  847. GNUNET_free (id);
  848. return;
  849. }
  850. GNUNET_CONFIGURATION_set_value_string (subsystem_cfg,
  851. section,
  852. "DEFAULT_IDENTIFIER",
  853. NULL);
  854. GNUNET_free (id);
  855. }
  856. /**
  857. * Checks a #GNUNET_MESSAGE_TYPE_IDENTITY_DELETE message
  858. *
  859. * @param cls client sending the message
  860. * @param msg message of type `struct DeleteMessage`
  861. * @return #GNUNET_OK if @a msg is well-formed
  862. */
  863. static int
  864. check_delete_message (void *cls, const struct DeleteMessage *msg)
  865. {
  866. uint16_t size;
  867. uint16_t name_len;
  868. const char *name;
  869. size = ntohs (msg->header.size);
  870. if (size <= sizeof(struct DeleteMessage))
  871. {
  872. GNUNET_break (0);
  873. return GNUNET_SYSERR;
  874. }
  875. name = (const char *) &msg[1];
  876. name_len = ntohs (msg->name_len);
  877. if ((name_len + sizeof(struct DeleteMessage) != size) ||
  878. (0 != ntohs (msg->reserved)) || ('\0' != name[name_len - 1]))
  879. {
  880. GNUNET_break (0);
  881. return GNUNET_SYSERR;
  882. }
  883. return GNUNET_OK;
  884. }
  885. /**
  886. * Handler for DELETE message from client, creates
  887. * new identity.
  888. *
  889. * @param cls unused
  890. * @param client who sent the message
  891. * @param message the message received
  892. */
  893. static void
  894. handle_delete_message (void *cls, const struct DeleteMessage *dm)
  895. {
  896. struct Ego *ego;
  897. char *name;
  898. char *fn;
  899. struct GNUNET_SERVICE_Client *client = cls;
  900. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received DELETE message from client\n");
  901. name = GNUNET_strdup ((const char *) &dm[1]);
  902. GNUNET_STRINGS_utf8_tolower ((const char *) &dm[1], name);
  903. for (ego = ego_head; NULL != ego; ego = ego->next)
  904. {
  905. if (0 == strcmp (ego->identifier, name))
  906. {
  907. GNUNET_CONTAINER_DLL_remove (ego_head, ego_tail, ego);
  908. GNUNET_CONFIGURATION_iterate_sections (subsystem_cfg,
  909. &handle_ego_delete,
  910. ego->identifier);
  911. if (GNUNET_OK !=
  912. GNUNET_CONFIGURATION_write (subsystem_cfg, subsystem_cfg_file))
  913. GNUNET_log (
  914. GNUNET_ERROR_TYPE_ERROR,
  915. _ ("Failed to write subsystem default identifier map to `%s'.\n"),
  916. subsystem_cfg_file);
  917. fn = get_ego_filename (ego);
  918. if (0 != unlink (fn))
  919. GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", fn);
  920. GNUNET_free (fn);
  921. GNUNET_free (ego->identifier);
  922. ego->identifier = NULL;
  923. notify_listeners (ego);
  924. GNUNET_free (ego);
  925. GNUNET_free (name);
  926. send_result_code (client, 0, NULL);
  927. GNUNET_SERVICE_client_continue (client);
  928. return;
  929. }
  930. }
  931. send_result_code (client, 1, gettext_noop ("no matching ego found"));
  932. GNUNET_free (name);
  933. GNUNET_SERVICE_client_continue (client);
  934. }
  935. static int
  936. read_from_file (const char *filename,
  937. void *buf,
  938. size_t buf_size)
  939. {
  940. int fd;
  941. struct stat sb;
  942. fd = open (filename,
  943. O_RDONLY);
  944. if (-1 == fd)
  945. {
  946. memset (buf,
  947. 0,
  948. buf_size);
  949. return GNUNET_SYSERR;
  950. }
  951. if (0 != fstat (fd,
  952. &sb))
  953. {
  954. GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
  955. "stat",
  956. filename);
  957. GNUNET_assert (0 == close (fd));
  958. memset (buf,
  959. 0,
  960. buf_size);
  961. return GNUNET_SYSERR;
  962. }
  963. if (sb.st_size != buf_size)
  964. {
  965. GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
  966. "File `%s' has wrong size (%llu), expected %llu bytes\n",
  967. filename,
  968. (unsigned long long) sb.st_size,
  969. (unsigned long long) buf_size);
  970. GNUNET_assert (0 == close (fd));
  971. memset (buf,
  972. 0,
  973. buf_size);
  974. return GNUNET_SYSERR;
  975. }
  976. if (buf_size !=
  977. read (fd,
  978. buf,
  979. buf_size))
  980. {
  981. GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
  982. "read",
  983. filename);
  984. GNUNET_assert (0 == close (fd));
  985. memset (buf,
  986. 0,
  987. buf_size);
  988. return GNUNET_SYSERR;
  989. }
  990. GNUNET_assert (0 == close (fd));
  991. return GNUNET_OK;
  992. }
  993. /**
  994. * Process the given file from the "EGODIR". Parses the file
  995. * and creates the respective 'struct Ego' in memory.
  996. *
  997. * @param cls NULL
  998. * @param filename name of the file to parse
  999. * @return #GNUNET_OK to continue to iterate,
  1000. * #GNUNET_NO to stop iteration with no error,
  1001. * #GNUNET_SYSERR to abort iteration with error!
  1002. */
  1003. static int
  1004. process_ego_file (void *cls,
  1005. const char *filename)
  1006. {
  1007. struct Ego *ego;
  1008. const char *fn;
  1009. fn = strrchr (filename, (int) DIR_SEPARATOR);
  1010. if (NULL == fn)
  1011. {
  1012. GNUNET_break (0);
  1013. return GNUNET_OK;
  1014. }
  1015. ego = GNUNET_new (struct Ego);
  1016. if (GNUNET_OK !=
  1017. read_from_file (filename,
  1018. &ego->pk,
  1019. sizeof (ego->pk)))
  1020. {
  1021. GNUNET_free (ego);
  1022. GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
  1023. _ ("Failed to parse ego information in `%s'\n"),
  1024. filename);
  1025. return GNUNET_OK;
  1026. }
  1027. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1028. "Loaded ego `%s'\n",
  1029. fn + 1);
  1030. ego->identifier = GNUNET_strdup (fn + 1);
  1031. GNUNET_CONTAINER_DLL_insert (ego_head, ego_tail, ego);
  1032. return GNUNET_OK;
  1033. }
  1034. /**
  1035. * Handle network size estimate clients.
  1036. *
  1037. * @param cls closure
  1038. * @param server the initialized server
  1039. * @param c configuration to use
  1040. */
  1041. static void
  1042. run (void *cls,
  1043. const struct GNUNET_CONFIGURATION_Handle *c,
  1044. struct GNUNET_SERVICE_Handle *service)
  1045. {
  1046. cfg = c;
  1047. nc = GNUNET_notification_context_create (1);
  1048. if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg,
  1049. "identity",
  1050. "EGODIR",
  1051. &ego_directory))
  1052. {
  1053. GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, "identity", "EGODIR");
  1054. GNUNET_SCHEDULER_shutdown ();
  1055. return;
  1056. }
  1057. if (GNUNET_OK !=
  1058. GNUNET_CONFIGURATION_get_value_filename (cfg,
  1059. "identity",
  1060. "SUBSYSTEM_CFG",
  1061. &subsystem_cfg_file))
  1062. {
  1063. GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
  1064. "identity",
  1065. "SUBSYSTEM_CFG");
  1066. GNUNET_SCHEDULER_shutdown ();
  1067. return;
  1068. }
  1069. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1070. "Loading subsystem configuration `%s'\n",
  1071. subsystem_cfg_file);
  1072. subsystem_cfg = GNUNET_CONFIGURATION_create ();
  1073. if ((GNUNET_YES == GNUNET_DISK_file_test (subsystem_cfg_file)) &&
  1074. (GNUNET_OK !=
  1075. GNUNET_CONFIGURATION_parse (subsystem_cfg, subsystem_cfg_file)))
  1076. {
  1077. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  1078. _ (
  1079. "Failed to parse subsystem identity configuration file `%s'\n"),
  1080. subsystem_cfg_file);
  1081. GNUNET_SCHEDULER_shutdown ();
  1082. return;
  1083. }
  1084. stats = GNUNET_STATISTICS_create ("identity", cfg);
  1085. if (GNUNET_OK != GNUNET_DISK_directory_create (ego_directory))
  1086. {
  1087. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  1088. _ ("Failed to create directory `%s' for storing egos\n"),
  1089. ego_directory);
  1090. }
  1091. GNUNET_DISK_directory_scan (ego_directory,
  1092. &process_ego_file,
  1093. NULL);
  1094. GNUNET_SCHEDULER_add_shutdown (&shutdown_task, NULL);
  1095. }
  1096. /**
  1097. * Define "main" method using service macro.
  1098. */
  1099. GNUNET_SERVICE_MAIN (
  1100. "identity",
  1101. GNUNET_SERVICE_OPTION_NONE,
  1102. &run,
  1103. &client_connect_cb,
  1104. &client_disconnect_cb,
  1105. NULL,
  1106. GNUNET_MQ_hd_fixed_size (start_message,
  1107. GNUNET_MESSAGE_TYPE_IDENTITY_START,
  1108. struct GNUNET_MessageHeader,
  1109. NULL),
  1110. GNUNET_MQ_hd_var_size (lookup_message,
  1111. GNUNET_MESSAGE_TYPE_IDENTITY_LOOKUP,
  1112. struct LookupMessage,
  1113. NULL),
  1114. GNUNET_MQ_hd_var_size (lookup_by_suffix_message,
  1115. GNUNET_MESSAGE_TYPE_IDENTITY_LOOKUP_BY_SUFFIX,
  1116. struct LookupMessage,
  1117. NULL),
  1118. GNUNET_MQ_hd_var_size (get_default_message,
  1119. GNUNET_MESSAGE_TYPE_IDENTITY_GET_DEFAULT,
  1120. struct GetDefaultMessage,
  1121. NULL),
  1122. GNUNET_MQ_hd_var_size (set_default_message,
  1123. GNUNET_MESSAGE_TYPE_IDENTITY_SET_DEFAULT,
  1124. struct SetDefaultMessage,
  1125. NULL),
  1126. GNUNET_MQ_hd_var_size (create_message,
  1127. GNUNET_MESSAGE_TYPE_IDENTITY_CREATE,
  1128. struct CreateRequestMessage,
  1129. NULL),
  1130. GNUNET_MQ_hd_var_size (rename_message,
  1131. GNUNET_MESSAGE_TYPE_IDENTITY_RENAME,
  1132. struct RenameMessage,
  1133. NULL),
  1134. GNUNET_MQ_hd_var_size (delete_message,
  1135. GNUNET_MESSAGE_TYPE_IDENTITY_DELETE,
  1136. struct DeleteMessage,
  1137. NULL),
  1138. GNUNET_MQ_handler_end ());
  1139. /* end of gnunet-service-identity.c */