aboutsummaryrefslogtreecommitdiff
path: root/src/identity/gnunet-service-identity.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/identity/gnunet-service-identity.c')
-rw-r--r--src/identity/gnunet-service-identity.c263
1 files changed, 12 insertions, 251 deletions
diff --git a/src/identity/gnunet-service-identity.c b/src/identity/gnunet-service-identity.c
index 51fca7816..5e3f7bb35 100644
--- a/src/identity/gnunet-service-identity.c
+++ b/src/identity/gnunet-service-identity.c
@@ -205,30 +205,21 @@ shutdown_task (void *cls)
205 * 205 *
206 * @param client client that should receive the result code 206 * @param client client that should receive the result code
207 * @param result_code code to transmit 207 * @param result_code code to transmit
208 * @param emsg error message to include (or NULL for none)
209 */ 208 */
210static void 209static void
211send_result_code (struct GNUNET_SERVICE_Client *client, 210send_result_code (struct GNUNET_SERVICE_Client *client,
212 uint32_t result_code, 211 uint32_t result_code)
213 const char *emsg)
214{ 212{
215 struct ResultCodeMessage *rcm; 213 struct ResultCodeMessage *rcm;
216 struct GNUNET_MQ_Envelope *env; 214 struct GNUNET_MQ_Envelope *env;
217 size_t elen;
218 215
219 if (NULL == emsg)
220 elen = 0;
221 else
222 elen = strlen (emsg) + 1;
223 env = 216 env =
224 GNUNET_MQ_msg_extra (rcm, elen, GNUNET_MESSAGE_TYPE_IDENTITY_RESULT_CODE); 217 GNUNET_MQ_msg (rcm, GNUNET_MESSAGE_TYPE_IDENTITY_RESULT_CODE);
225 rcm->result_code = htonl (result_code); 218 rcm->result_code = htonl (result_code);
226 if (0 < elen)
227 GNUNET_memcpy (&rcm[1], emsg, elen);
228 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 219 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
229 "Sending result %d (%s) to client\n", 220 "Sending result %d (%s) to client\n",
230 (int) result_code, 221 (int) result_code,
231 emsg); 222 GNUNET_ErrorCode_get_hint (result_code));
232 GNUNET_MQ_send (GNUNET_SERVICE_client_get_mq (client), env); 223 GNUNET_MQ_send (GNUNET_SERVICE_client_get_mq (client), env);
233} 224}
234 225
@@ -257,33 +248,6 @@ create_update_message (struct Ego *ego)
257 248
258 249
259/** 250/**
260 * Create a set default message with information about the current state of an ego.
261 *
262 * @param ego ego to create message for
263 * @param servicename name of the service to provide in the message
264 * @return corresponding set default message
265 */
266static struct GNUNET_MQ_Envelope *
267create_set_default_message (struct Ego *ego,
268 const char *servicename)
269{
270 struct SetDefaultMessage *sdm;
271 struct GNUNET_MQ_Envelope *env;
272 size_t name_len;
273
274 name_len = (NULL == servicename) ? 0 : (strlen (servicename) + 1);
275 env = GNUNET_MQ_msg_extra (sdm,
276 name_len,
277 GNUNET_MESSAGE_TYPE_IDENTITY_SET_DEFAULT);
278 sdm->name_len = htons (name_len);
279 sdm->reserved = htons (0);
280 sdm->private_key = ego->pk;
281 GNUNET_memcpy (&sdm[1], servicename, name_len);
282 return env;
283}
284
285
286/**
287 * Handler for START message from client, sends information 251 * Handler for START message from client, sends information
288 * about all identities to the client immediately and 252 * about all identities to the client immediately and
289 * adds the client to the notification context for future 253 * adds the client to the notification context for future
@@ -370,7 +334,7 @@ handle_lookup_message (void *cls,
370 GNUNET_SERVICE_client_continue (client); 334 GNUNET_SERVICE_client_continue (client);
371 return; 335 return;
372 } 336 }
373 send_result_code (client, 0, "ego not found"); 337 send_result_code (client, GNUNET_EC_IDENTITY_NOT_FOUND);
374 GNUNET_SERVICE_client_continue (client); 338 GNUNET_SERVICE_client_continue (client);
375} 339}
376 340
@@ -433,200 +397,7 @@ handle_lookup_by_suffix_message (void *cls,
433 GNUNET_SERVICE_client_continue (client); 397 GNUNET_SERVICE_client_continue (client);
434 return; 398 return;
435 } 399 }
436 send_result_code (client, 0, "ego not found"); 400 send_result_code (client, GNUNET_EC_IDENTITY_NOT_FOUND);
437 GNUNET_SERVICE_client_continue (client);
438}
439
440
441/**
442 * Checks a #GNUNET_MESSAGE_TYPE_IDENTITY_GET_DEFAULT message
443 *
444 * @param cls client sending the message
445 * @param msg message of type `struct GetDefaultMessage`
446 * @return #GNUNET_OK if @a msg is well-formed
447 */
448static int
449check_get_default_message (void *cls,
450 const struct GetDefaultMessage *msg)
451{
452 uint16_t size;
453 uint16_t name_len;
454 const char *name;
455
456 size = ntohs (msg->header.size);
457 if (size <= sizeof(struct GetDefaultMessage))
458 {
459 GNUNET_break (0);
460 return GNUNET_SYSERR;
461 }
462 name = (const char *) &msg[1];
463 name_len = ntohs (msg->name_len);
464 if ((name_len + sizeof(struct GetDefaultMessage) != size) ||
465 (0 != ntohs (msg->reserved)) || ('\0' != name[name_len - 1]))
466 {
467 GNUNET_break (0);
468 return GNUNET_SYSERR;
469 }
470 return GNUNET_OK;
471}
472
473
474/**
475 * Handler for GET_DEFAULT message from client, returns
476 * default identity for some service.
477 *
478 * @param cls unused
479 * @param gdm the message received
480 */
481static void
482handle_get_default_message (void *cls,
483 const struct GetDefaultMessage *gdm)
484{
485 struct GNUNET_MQ_Envelope *env;
486 struct GNUNET_SERVICE_Client *client = cls;
487 char *name;
488 char *identifier;
489
490 name = GNUNET_strdup ((const char *) &gdm[1]);
491 GNUNET_STRINGS_utf8_tolower ((const char *) &gdm[1], name);
492 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
493 "Received GET_DEFAULT for service `%s' from client\n",
494 name);
495 if (GNUNET_OK !=
496 GNUNET_CONFIGURATION_get_value_string (subsystem_cfg,
497 name,
498 "DEFAULT_IDENTIFIER",
499 &identifier))
500 {
501 send_result_code (client, 1, gettext_noop ("no default known"));
502 GNUNET_SERVICE_client_continue (client);
503 GNUNET_free (name);
504 return;
505 }
506 for (struct Ego *ego = ego_head; NULL != ego; ego = ego->next)
507 {
508 if (0 == strcmp (ego->identifier, identifier))
509 {
510 env = create_set_default_message (ego, name);
511 GNUNET_MQ_send (GNUNET_SERVICE_client_get_mq (client), env);
512 GNUNET_SERVICE_client_continue (client);
513 GNUNET_free (identifier);
514 GNUNET_free (name);
515 return;
516 }
517 }
518 GNUNET_free (identifier);
519 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
520 "Failed to find ego `%s'\n",
521 name);
522 GNUNET_free (name);
523 send_result_code (client,
524 1,
525 gettext_noop (
526 "default configured, but ego unknown (internal error)"));
527 GNUNET_SERVICE_client_continue (client);
528}
529
530
531/**
532 * Compare the given two private keys for equality.
533 *
534 * @param pk1 one private key
535 * @param pk2 another private key
536 * @return 0 if the keys are equal
537 */
538static int
539key_cmp (const struct GNUNET_IDENTITY_PrivateKey *pk1,
540 const struct GNUNET_IDENTITY_PrivateKey *pk2)
541{
542 return GNUNET_memcmp (pk1, pk2);
543}
544
545
546/**
547 * Checks a #GNUNET_MESSAGE_TYPE_IDENTITY_SET_DEFAULT message
548 *
549 * @param cls client sending the message
550 * @param msg message of type `struct SetDefaultMessage`
551 * @return #GNUNET_OK if @a msg is well-formed
552 */
553static int
554check_set_default_message (void *cls,
555 const struct SetDefaultMessage *msg)
556{
557 uint16_t size;
558 uint16_t name_len;
559 const char *str;
560
561 size = ntohs (msg->header.size);
562 if (size <= sizeof(struct SetDefaultMessage))
563 {
564 GNUNET_break (0);
565 return GNUNET_SYSERR;
566 }
567 name_len = ntohs (msg->name_len);
568 GNUNET_break (0 == ntohs (msg->reserved));
569 if (name_len + sizeof(struct SetDefaultMessage) != size)
570 {
571 GNUNET_break (0);
572 return GNUNET_SYSERR;
573 }
574 str = (const char *) &msg[1];
575 if ('\0' != str[name_len - 1])
576 {
577 GNUNET_break (0);
578 return GNUNET_SYSERR;
579 }
580 return GNUNET_OK;
581}
582
583
584/**
585 * Handler for SET_DEFAULT message from client, updates
586 * default identity for some service.
587 *
588 * @param cls unused
589 * @param sdm the message received
590 */
591static void
592handle_set_default_message (void *cls,
593 const struct SetDefaultMessage *sdm)
594{
595 struct Ego *ego;
596 struct GNUNET_SERVICE_Client *client = cls;
597 char *str;
598
599 str = GNUNET_strdup ((const char *) &sdm[1]);
600 GNUNET_STRINGS_utf8_tolower ((const char *) &sdm[1], str);
601
602 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
603 "Received SET_DEFAULT for service `%s' from client\n",
604 str);
605 for (ego = ego_head; NULL != ego; ego = ego->next)
606 {
607 if (0 == key_cmp (&ego->pk,
608 &sdm->private_key))
609 {
610 GNUNET_CONFIGURATION_set_value_string (subsystem_cfg,
611 str,
612 "DEFAULT_IDENTIFIER",
613 ego->identifier);
614 if (GNUNET_OK !=
615 GNUNET_CONFIGURATION_write (subsystem_cfg, subsystem_cfg_file))
616 GNUNET_log (
617 GNUNET_ERROR_TYPE_ERROR,
618 _ ("Failed to write subsystem default identifier map to `%s'.\n"),
619 subsystem_cfg_file);
620 send_result_code (client, 0, NULL);
621 GNUNET_SERVICE_client_continue (client);
622 GNUNET_free (str);
623 return;
624 }
625 }
626 send_result_code (client,
627 1,
628 _ ("Unknown ego specified for service (internal error)"));
629 GNUNET_free (str);
630 GNUNET_SERVICE_client_continue (client); 401 GNUNET_SERVICE_client_continue (client);
631} 402}
632 403
@@ -716,9 +487,7 @@ handle_create_message (void *cls,
716 if (0 == strcmp (ego->identifier, str)) 487 if (0 == strcmp (ego->identifier, str))
717 { 488 {
718 send_result_code (client, 489 send_result_code (client,
719 1, 490 GNUNET_EC_IDENTITY_NAME_CONFLICT);
720 gettext_noop (
721 "identifier already in use for another ego"));
722 GNUNET_SERVICE_client_continue (client); 491 GNUNET_SERVICE_client_continue (client);
723 GNUNET_free (str); 492 GNUNET_free (str);
724 return; 493 return;
@@ -730,7 +499,7 @@ handle_create_message (void *cls,
730 GNUNET_CONTAINER_DLL_insert (ego_head, 499 GNUNET_CONTAINER_DLL_insert (ego_head,
731 ego_tail, 500 ego_tail,
732 ego); 501 ego);
733 send_result_code (client, 0, NULL); 502 send_result_code (client, GNUNET_EC_NONE);
734 fn = get_ego_filename (ego); 503 fn = get_ego_filename (ego);
735 if (GNUNET_OK != 504 if (GNUNET_OK !=
736 GNUNET_DISK_fn_write (fn, 505 GNUNET_DISK_fn_write (fn,
@@ -864,7 +633,7 @@ handle_rename_message (void *cls, const struct RenameMessage *rm)
864 { 633 {
865 if (0 == strcmp (ego->identifier, new_name)) 634 if (0 == strcmp (ego->identifier, new_name))
866 { 635 {
867 send_result_code (client, 1, gettext_noop ("target name already exists")); 636 send_result_code (client, GNUNET_EC_IDENTITY_NAME_CONFLICT);
868 GNUNET_SERVICE_client_continue (client); 637 GNUNET_SERVICE_client_continue (client);
869 GNUNET_free (old_name); 638 GNUNET_free (old_name);
870 GNUNET_free (new_name); 639 GNUNET_free (new_name);
@@ -899,14 +668,14 @@ handle_rename_message (void *cls, const struct RenameMessage *rm)
899 GNUNET_free (old_name); 668 GNUNET_free (old_name);
900 GNUNET_free (new_name); 669 GNUNET_free (new_name);
901 notify_listeners (ego); 670 notify_listeners (ego);
902 send_result_code (client, 0, NULL); 671 send_result_code (client, GNUNET_EC_NONE);
903 GNUNET_SERVICE_client_continue (client); 672 GNUNET_SERVICE_client_continue (client);
904 return; 673 return;
905 } 674 }
906 } 675 }
907 676
908 /* failed to locate old name */ 677 /* failed to locate old name */
909 send_result_code (client, 1, gettext_noop ("no matching ego found")); 678 send_result_code (client, GNUNET_EC_IDENTITY_NOT_FOUND);
910 GNUNET_free (old_name); 679 GNUNET_free (old_name);
911 GNUNET_free (new_name); 680 GNUNET_free (new_name);
912 GNUNET_SERVICE_client_continue (client); 681 GNUNET_SERVICE_client_continue (client);
@@ -1018,13 +787,13 @@ handle_delete_message (void *cls, const struct DeleteMessage *dm)
1018 notify_listeners (ego); 787 notify_listeners (ego);
1019 GNUNET_free (ego); 788 GNUNET_free (ego);
1020 GNUNET_free (name); 789 GNUNET_free (name);
1021 send_result_code (client, 0, NULL); 790 send_result_code (client, GNUNET_EC_NONE);
1022 GNUNET_SERVICE_client_continue (client); 791 GNUNET_SERVICE_client_continue (client);
1023 return; 792 return;
1024 } 793 }
1025 } 794 }
1026 795
1027 send_result_code (client, 1, gettext_noop ("no matching ego found")); 796 send_result_code (client, GNUNET_EC_IDENTITY_NOT_FOUND);
1028 GNUNET_free (name); 797 GNUNET_free (name);
1029 GNUNET_SERVICE_client_continue (client); 798 GNUNET_SERVICE_client_continue (client);
1030} 799}
@@ -1221,14 +990,6 @@ GNUNET_SERVICE_MAIN (
1221 GNUNET_MESSAGE_TYPE_IDENTITY_LOOKUP_BY_SUFFIX, 990 GNUNET_MESSAGE_TYPE_IDENTITY_LOOKUP_BY_SUFFIX,
1222 struct LookupMessage, 991 struct LookupMessage,
1223 NULL), 992 NULL),
1224 GNUNET_MQ_hd_var_size (get_default_message,
1225 GNUNET_MESSAGE_TYPE_IDENTITY_GET_DEFAULT,
1226 struct GetDefaultMessage,
1227 NULL),
1228 GNUNET_MQ_hd_var_size (set_default_message,
1229 GNUNET_MESSAGE_TYPE_IDENTITY_SET_DEFAULT,
1230 struct SetDefaultMessage,
1231 NULL),
1232 GNUNET_MQ_hd_var_size (create_message, 993 GNUNET_MQ_hd_var_size (create_message,
1233 GNUNET_MESSAGE_TYPE_IDENTITY_CREATE, 994 GNUNET_MESSAGE_TYPE_IDENTITY_CREATE,
1234 struct CreateRequestMessage, 995 struct CreateRequestMessage,