updates.
[silc.git] / apps / silcd / command_reply.c
index 79eb2bbeabebbc518b25719608d2e336f2e17d5f..2bc68693f2f79de17ddeef74b1a0f94e64a8cd0a 100644 (file)
 #include "server_internal.h"
 #include "command_reply.h"
 
+/* All functions that call the COMMAND_CHECK_STATUS or the
+   COMMAND_CHECK_STATUS_LIST macros must have out: goto label. */
+
 #define COMMAND_CHECK_STATUS                                             \
 do {                                                                     \
   SILC_LOG_DEBUG(("Start"));                                             \
   SILC_GET16_MSB(status, silc_argument_get_arg_type(cmd->args, 1, NULL)); \
-  if (status != SILC_STATUS_OK) {                                        \
-    silc_server_command_reply_free(cmd);                                 \
-    return;                                                              \
-  }                                                                      \
+  if (status != SILC_STATUS_OK)                                                  \
+    goto out;                                                            \
 } while(0)
 
 #define COMMAND_CHECK_STATUS_LIST                                        \
 do {                                                                     \
   SILC_LOG_DEBUG(("Start"));                                             \
   SILC_GET16_MSB(status, silc_argument_get_arg_type(cmd->args, 1, NULL)); \
-  if (status != SILC_STATUS_OK &&                                        \
+  if (status != SILC_STATUS_OK &&                                        \
       status != SILC_STATUS_LIST_START &&                                \
       status != SILC_STATUS_LIST_ITEM &&                                 \
-      status != SILC_STATUS_LIST_END) {                                          \
-    silc_server_command_reply_free(cmd);                                 \
-    return;                                                              \
-  }                                                                      \
+      status != SILC_STATUS_LIST_END)                                    \
+    goto out;                                                            \
 } while(0)
 
 /* Server command reply list. Not all commands have reply function as
@@ -57,6 +56,7 @@ SilcServerCommandReply silc_command_reply_list[] =
   SILC_SERVER_CMD_REPLY(motd, MOTD),
   SILC_SERVER_CMD_REPLY(join, JOIN),
   SILC_SERVER_CMD_REPLY(users, USERS),
+  SILC_SERVER_CMD_REPLY(getkey, GETKEY),
 
   { NULL, 0 },
 };
@@ -71,7 +71,7 @@ void silc_server_command_reply_process(SilcServer server,
   SilcServerCommandReplyContext ctx;
   SilcCommandPayload payload;
   SilcCommand command;
-  unsigned short ident;
+  uint16 ident;
 
   SILC_LOG_DEBUG(("Start"));
 
@@ -128,7 +128,6 @@ static char
 silc_server_command_reply_whois_save(SilcServerCommandReplyContext cmd)
 {
   SilcServer server = cmd->server;
-  int len, id_len;
   unsigned char *tmp, *id_data;
   char *nickname, *username, *realname;
   SilcClientID *client_id;
@@ -136,7 +135,7 @@ silc_server_command_reply_whois_save(SilcServerCommandReplyContext cmd)
   SilcIDCacheEntry cache = NULL;
   char global = FALSE;
   char *nick;
-  unsigned int mode = 0;
+  uint32 mode = 0, len, id_len;
 
   id_data = silc_argument_get_arg_type(cmd->args, 2, &id_len);
   nickname = silc_argument_get_arg_type(cmd->args, 3, &len);
@@ -250,10 +249,15 @@ SILC_SERVER_CMD_REPLY_FUNC(whois)
   if (!silc_server_command_reply_whois_save(cmd))
     goto out;
 
-  /* Execute any pending commands */
-  SILC_SERVER_PENDING_EXEC(cmd, SILC_COMMAND_WHOIS);
+  /* Pending callbacks are not executed if this was an list entry */
+  if (status != SILC_STATUS_OK &&
+      status != SILC_STATUS_LIST_END) {
+    silc_server_command_reply_free(cmd);
+    return;
+  }
 
  out:
+  SILC_SERVER_PENDING_EXEC(cmd, SILC_COMMAND_WHOIS);
   SILC_SERVER_PENDING_DESTRUCTOR(cmd, SILC_COMMAND_WHOIS);
   silc_server_command_reply_free(cmd);
 }
@@ -264,7 +268,7 @@ static char
 silc_server_command_reply_whowas_save(SilcServerCommandReplyContext cmd)
 {
   SilcServer server = cmd->server;
-  int len, id_len;
+  uint32 len, id_len;
   unsigned char *id_data;
   char *nickname, *username, *realname;
   SilcClientID *client_id;
@@ -370,10 +374,15 @@ SILC_SERVER_CMD_REPLY_FUNC(whowas)
   if (!silc_server_command_reply_whowas_save(cmd))
     goto out;
 
-  /* Execute any pending commands */
-  SILC_SERVER_PENDING_EXEC(cmd, SILC_COMMAND_WHOWAS);
+  /* Pending callbacks are not executed if this was an list entry */
+  if (status != SILC_STATUS_OK &&
+      status != SILC_STATUS_LIST_END) {
+    silc_server_command_reply_free(cmd);
+    return;
+  }
 
  out:
+  SILC_SERVER_PENDING_EXEC(cmd, SILC_COMMAND_WHOWAS);
   SILC_SERVER_PENDING_DESTRUCTOR(cmd, SILC_COMMAND_WHOWAS);
   silc_server_command_reply_free(cmd);
 }
@@ -384,7 +393,7 @@ static char
 silc_server_command_reply_identify_save(SilcServerCommandReplyContext cmd)
 {
   SilcServer server = cmd->server;
-  int len, id_len;
+  uint32 len, id_len;
   unsigned char *id_data;
   char *nickname, *username;
   SilcClientID *client_id;
@@ -492,10 +501,15 @@ SILC_SERVER_CMD_REPLY_FUNC(identify)
   if (!silc_server_command_reply_identify_save(cmd))
     goto out;
 
-  /* Execute any pending commands */
-  SILC_SERVER_PENDING_EXEC(cmd, SILC_COMMAND_IDENTIFY);
+  /* Pending callbacks are not executed if this was an list entry */
+  if (status != SILC_STATUS_OK &&
+      status != SILC_STATUS_LIST_END) {
+    silc_server_command_reply_free(cmd);
+    return;
+  }
 
  out:
+  SILC_SERVER_PENDING_EXEC(cmd, SILC_COMMAND_IDENTIFY);
   SILC_SERVER_PENDING_DESTRUCTOR(cmd, SILC_COMMAND_IDENTIFY);
   silc_server_command_reply_free(cmd);
 }
@@ -509,7 +523,7 @@ SILC_SERVER_CMD_REPLY_FUNC(info)
   SilcCommandStatus status;
   SilcServerEntry entry;
   SilcServerID *server_id;
-  unsigned int tmp_len;
+  uint32 tmp_len;
   unsigned char *tmp, *name;
 
   COMMAND_CHECK_STATUS;
@@ -550,10 +564,8 @@ SILC_SERVER_CMD_REPLY_FUNC(info)
 
   entry->server_info = tmp ? strdup(tmp) : NULL;
 
-  /* Execute any pending commands */
-  SILC_SERVER_PENDING_EXEC(cmd, SILC_COMMAND_INFO);
-
  out:
+  SILC_SERVER_PENDING_EXEC(cmd, SILC_COMMAND_INFO);
   SILC_SERVER_PENDING_DESTRUCTOR(cmd, SILC_COMMAND_INFO);
   silc_server_command_reply_free(cmd);
 }
@@ -565,9 +577,9 @@ SILC_SERVER_CMD_REPLY_FUNC(motd)
   SilcServerCommandReplyContext cmd = (SilcServerCommandReplyContext)context;
   SilcServer server = cmd->server;
   SilcCommandStatus status;
-  SilcServerEntry entry;
+  SilcServerEntry entry = NULL;
   SilcServerID *server_id;
-  unsigned int tmp_len;
+  uint32 tmp_len;
   unsigned char *tmp;
 
   COMMAND_CHECK_STATUS;
@@ -595,14 +607,13 @@ SILC_SERVER_CMD_REPLY_FUNC(motd)
 
   entry->motd = tmp;
 
-  /* Execute any pending commands */
-  SILC_SERVER_PENDING_EXEC(cmd, SILC_COMMAND_MOTD);
-
-  entry->motd = NULL;
-
  out:
+  SILC_SERVER_PENDING_EXEC(cmd, SILC_COMMAND_MOTD);
   SILC_SERVER_PENDING_DESTRUCTOR(cmd, SILC_COMMAND_MOTD);
   silc_server_command_reply_free(cmd);
+
+  if (entry)
+    entry->motd = NULL;
 }
 
 /* Received reply for forwarded JOIN command. Router has created or joined
@@ -619,11 +630,11 @@ SILC_SERVER_CMD_REPLY_FUNC(join)
   SilcClientID *client_id = NULL;
   SilcChannelEntry entry;
   SilcHmac hmac = NULL;
-  unsigned int id_len, len, list_count;
+  uint32 id_len, len, list_count;
   unsigned char *id_string;
   char *channel_name, *tmp;
-  unsigned int mode, created;
-  SilcBuffer keyp = NULL, client_id_list, client_mode_list;
+  uint32 mode, created;
+  SilcBuffer keyp = NULL, client_id_list = NULL, client_mode_list = NULL;
 
   COMMAND_CHECK_STATUS;
 
@@ -722,8 +733,8 @@ SILC_SERVER_CMD_REPLY_FUNC(join)
     }
   } else {
     /* The entry exists. */
-    if (entry->id)
-      silc_free(entry->id);
+    if (cache->id)
+      silc_free(cache->id);
     entry->id = id;
     cache->id = entry->id;
 
@@ -784,9 +795,8 @@ SILC_SERVER_CMD_REPLY_FUNC(join)
   entry->mode = mode;
 
   /* Save channel key */
-  if (!(entry->mode & SILC_CHANNEL_MODE_PRIVKEY)) {
+  if (!(entry->mode & SILC_CHANNEL_MODE_PRIVKEY))
     silc_server_save_channel_key(server, keyp, entry);
-  }
   if (keyp)
     silc_buffer_free(keyp);
 
@@ -795,17 +805,17 @@ SILC_SERVER_CMD_REPLY_FUNC(join)
                                    client_id, client_id_list,
                                    client_mode_list, list_count);
 
-  silc_buffer_free(client_id_list);
-  silc_buffer_free(client_mode_list);
-
-  /* Execute any pending commands */
-  SILC_SERVER_PENDING_EXEC(cmd, SILC_COMMAND_JOIN);
-
  out:
+  SILC_SERVER_PENDING_EXEC(cmd, SILC_COMMAND_JOIN);
   SILC_SERVER_PENDING_DESTRUCTOR(cmd, SILC_COMMAND_JOIN);
   if (client_id)
     silc_free(client_id);
   silc_server_command_reply_free(cmd);
+
+  if (client_id_list)
+    silc_buffer_free(client_id_list);
+  if (client_mode_list)
+    silc_buffer_free(client_mode_list);
 }
 
 SILC_SERVER_CMD_REPLY_FUNC(users)
@@ -818,8 +828,8 @@ SILC_SERVER_CMD_REPLY_FUNC(users)
   SilcBuffer client_id_list;
   SilcBuffer client_mode_list;
   unsigned char *tmp;
-  unsigned int tmp_len;
-  unsigned int list_count;
+  uint32 tmp_len;
+  uint32 list_count;
 
   COMMAND_CHECK_STATUS;
 
@@ -873,12 +883,95 @@ SILC_SERVER_CMD_REPLY_FUNC(users)
   silc_buffer_free(client_id_list);
   silc_buffer_free(client_mode_list);
 
-  /* Execute any pending commands */
+ out:
   SILC_SERVER_PENDING_EXEC(cmd, SILC_COMMAND_USERS);
+  SILC_SERVER_PENDING_DESTRUCTOR(cmd, SILC_COMMAND_USERS);
+  silc_free(channel_id);
+  silc_server_command_reply_free(cmd);
+}
+
+SILC_SERVER_CMD_REPLY_FUNC(getkey)
+{
+  SilcServerCommandReplyContext cmd = (SilcServerCommandReplyContext)context;
+  SilcServer server = cmd->server;
+  SilcCommandStatus status;
+  SilcClientEntry client = NULL;
+  SilcServerEntry server_entry = NULL;
+  SilcClientID *client_id = NULL;
+  SilcServerID *server_id = NULL;
+  SilcSKEPKType type;
+  unsigned char *tmp, *pk;
+  uint32 len;
+  uint16 pk_len;
+  SilcIDPayload idp = NULL;
+  SilcIdType id_type;
+  SilcPublicKey public_key = NULL;
+
+  COMMAND_CHECK_STATUS;
+
+  tmp = silc_argument_get_arg_type(cmd->args, 2, &len);
+  if (!tmp)
+    goto out;
+  idp = silc_id_payload_parse_data(tmp, len);
+  if (!idp)
+    goto out;
+
+  /* Get the public key payload */
+  tmp = silc_argument_get_arg_type(cmd->args, 3, &len);
+  if (!tmp)
+    goto out;
+
+  /* Decode the public key */
+
+  SILC_GET16_MSB(pk_len, tmp);
+  SILC_GET16_MSB(type, tmp + 2);
+  pk = tmp + 4;
+
+  if (type != SILC_SKE_PK_TYPE_SILC)
+    goto out;
+
+  if (!silc_pkcs_public_key_decode(pk, pk_len, &public_key))
+    goto out;
+
+  id_type = silc_id_payload_get_type(idp);
+  if (id_type == SILC_ID_CLIENT) {
+    client_id = silc_id_payload_get_id(idp);
+
+    client = silc_idlist_find_client_by_id(server->local_list, client_id,
+                                          NULL);
+    if (!client) {
+      client = silc_idlist_find_client_by_id(server->global_list, 
+                                            client_id, NULL);
+      if (!client)
+       goto out;
+    }
+
+    client->data.public_key = public_key;
+  } else if (id_type == SILC_ID_SERVER) {
+    server_id = silc_id_payload_get_id(idp);
+
+    server_entry = silc_idlist_find_server_by_id(server->local_list, server_id,
+                                                NULL);
+    if (!server_entry) {
+      server_entry = silc_idlist_find_server_by_id(server->global_list, 
+                                                  server_id, NULL);
+      if (!server_entry)
+       goto out;
+    }
+
+    server_entry->data.public_key = public_key;
+  } else {
+    goto out;
+  }
 
  out:
+  SILC_SERVER_PENDING_EXEC(cmd, SILC_COMMAND_USERS);
   SILC_SERVER_PENDING_DESTRUCTOR(cmd, SILC_COMMAND_USERS);
-  if (channel_id)
-    silc_free(channel_id);
+  if (idp)
+    silc_id_payload_free(idp);
+  silc_free(client_id);
+  silc_free(server_id);
+  if (public_key)
+    silc_pkcs_public_key_free(public_key);
   silc_server_command_reply_free(cmd);
 }