silcd: check entity validity after command reply
[silc.git] / apps / silcd / command.c
index 2bb776e87ebe4ad45dde71baa8d76cc889877bf0..9439f488ceaa2878cb86f9f517ab91c406d7edf3 100644 (file)
@@ -85,6 +85,27 @@ SilcServerCommand silc_command_list[] =
   { NULL, 0 },
 };
 
+/* Returns TRUE if the connection is registered. Unregistered connections
+   usually cannot send commands hence the check. */
+
+static int silc_server_is_registered(SilcServer server,
+                                    SilcPacketStream sock,
+                                    SilcServerCommandContext cmd,
+                                    SilcCommand command)
+{
+  SilcIDListData idata = silc_packet_get_context(sock);
+
+  if (!idata)
+    return FALSE;
+
+  if (idata->status & SILC_IDLIST_STATUS_REGISTERED)
+    return TRUE;
+
+  silc_server_command_send_status_reply(cmd, command,
+                                       SILC_STATUS_ERR_NOT_REGISTERED, 0);
+  return FALSE;
+}
+
 /* Performs several checks to the command. It first checks whether this
    command was called as pending command callback. If it was then it checks
    whether error occurred in the command reply where the pending command
@@ -102,6 +123,13 @@ do {                                                                            \
     return;                                                                 \
   }                                                                         \
                                                                             \
+  if (context2 &&                                                           \
+      !silc_server_is_registered(cmd->server, cmd->sock, cmd, command)) {    \
+    SILC_LOG_DEBUG(("Not registered, command not called"));                 \
+    silc_server_command_free(cmd);                                          \
+    return;                                                                 \
+  }                                                                         \
+                                                                            \
   _argc = silc_argument_get_arg_num(cmd->args);                                     \
   if (_argc < min) {                                                        \
     SILC_LOG_DEBUG(("Not enough parameters in command"));                   \
@@ -121,27 +149,6 @@ do {                                                                            \
   }                                                                         \
 } while(0)
 
-/* Returns TRUE if the connection is registered. Unregistered connections
-   usually cannot send commands hence the check. */
-
-static int silc_server_is_registered(SilcServer server,
-                                    SilcPacketStream sock,
-                                    SilcServerCommandContext cmd,
-                                    SilcCommand command)
-{
-  SilcIDListData idata = silc_packet_get_context(sock);
-
-  if (!idata)
-    return FALSE;
-
-  if (idata->status & SILC_IDLIST_STATUS_REGISTERED)
-    return TRUE;
-
-  silc_server_command_send_status_reply(cmd, command,
-                                       SILC_STATUS_ERR_NOT_REGISTERED, 0);
-  return FALSE;
-}
-
 /* Internal context to hold data when executed command with timeout. */
 typedef struct {
   SilcServerCommandContext ctx;