Fixed KILL command related crash in silcd
[runtime.git] / apps / silcd / server_util.c
index 19ea23e67821e2efb77f6fe817ce5bc773890ed3..d9fc0ffc952ad07ca7afa6dff5603f101390b555 100644 (file)
@@ -4,7 +4,7 @@
 
   Author: Pekka Riikonen <priikone@silcnet.org>
 
-  Copyright (C) 1997 - 2005, 2007 Pekka Riikonen
+  Copyright (C) 1997 - 2008 Pekka Riikonen
 
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
@@ -997,7 +997,7 @@ SilcUInt32 silc_server_num_sockets_by_ip(SilcServer server, const char *ip,
 
   silc_dlist_start(server->conns);
   while ((conn = silc_dlist_get(server->conns))) {
-    if (!conn->sock)
+    if (!conn->sock || !silc_packet_stream_is_valid(conn->sock))
       continue;
     silc_socket_stream_get_info(silc_packet_stream_get_stream(conn->sock),
                                NULL, NULL, &ipaddr, NULL);
@@ -1023,7 +1023,7 @@ silc_server_find_socket_by_host(SilcServer server,
 
   silc_dlist_start(server->conns);
   while ((conn = silc_dlist_get(server->conns))) {
-    if (!conn->sock)
+    if (!conn->sock || !silc_packet_stream_is_valid(conn->sock))
       continue;
     idata = silc_packet_get_context(conn->sock);
     silc_socket_stream_get_info(silc_packet_stream_get_stream(conn->sock),
@@ -1112,6 +1112,13 @@ SilcPublicKey silc_server_get_public_key(SilcServer server,
   silc_skr_find(server->repository, server->schedule,
                find, find_callback, &public_key);
 
+#ifdef SILC_DEBUG
+  if (public_key)
+    SILC_LOG_DEBUG(("Found public key"));
+  else
+    SILC_LOG_DEBUG(("Public key not found"));
+#endif /* SILC_DEBUG */
+
   return public_key;
 }
 
@@ -1188,6 +1195,7 @@ SilcBool silc_server_connection_allowed(SilcServer server,
       silc_server_disconnect_remote(server, sock,
                                    SILC_STATUS_ERR_BAD_VERSION,
                                    "You support too old protocol version");
+      silc_server_free_sock_user_data(server, sock, NULL);
       return FALSE;
     }
 
@@ -1199,6 +1207,7 @@ SilcBool silc_server_connection_allowed(SilcServer server,
       silc_server_disconnect_remote(server, sock,
                                    SILC_STATUS_ERR_BAD_VERSION,
                                    "You support too old software version");
+      silc_server_free_sock_user_data(server, sock, NULL);
       return FALSE;
     }
 
@@ -1210,6 +1219,7 @@ SilcBool silc_server_connection_allowed(SilcServer server,
       silc_server_disconnect_remote(server, sock,
                                    SILC_STATUS_ERR_BAD_VERSION,
                                    "Your software is not supported");
+      silc_server_free_sock_user_data(server, sock, NULL);
       return FALSE;
     }
   }
@@ -1229,6 +1239,7 @@ SilcBool silc_server_connection_allowed(SilcServer server,
     silc_server_disconnect_remote(server, sock,
                                  SILC_STATUS_ERR_RESOURCE_LIMIT,
                                  "Server is full, try again later");
+    silc_server_free_sock_user_data(server, sock, NULL);
     return FALSE;
   }
 
@@ -1240,6 +1251,7 @@ SilcBool silc_server_connection_allowed(SilcServer server,
     silc_server_disconnect_remote(server, sock,
                                  SILC_STATUS_ERR_RESOURCE_LIMIT,
                                  "Too many connections from your host");
+    silc_server_free_sock_user_data(server, sock, NULL);
     return FALSE;
   }
 
@@ -1531,7 +1543,7 @@ void silc_server_kill_client(SilcServer server,
   if (remote_client->connection) {
     /* Remove locally conneted client */
     SilcPacketStream sock = remote_client->connection;
-    silc_server_free_client_data(server, sock, remote_client, FALSE, NULL);
+    silc_server_free_sock_user_data(server, sock, NULL);
     silc_server_close_connection(server, sock);
   } else {
     /* Update statistics */
@@ -1746,10 +1758,11 @@ SilcBool silc_server_inviteban_match(SilcServer server, SilcHashTable list,
                                     SilcUInt8 type, void *check)
 {
   unsigned char *tmp = NULL;
-  SilcUInt32 len = 0, t;
+  SilcUInt32 len = 0;
   SilcHashTableList htl;
   SilcBuffer entry, idp = NULL, pkp = NULL;
   SilcBool ret = FALSE;
+  void *t;
 
   SILC_LOG_DEBUG(("Matching invite/ban"));
 
@@ -1779,13 +1792,14 @@ SilcBool silc_server_inviteban_match(SilcServer server, SilcHashTable list,
   /* Compare the list */
   silc_hash_table_list(list, &htl);
   while (silc_hash_table_get(&htl, (void *)&t, (void *)&entry)) {
-    if (type == t) {
+    if (type == SILC_PTR_TO_32(t)) {
       if (type == 1) {
        if (silc_string_match(entry->data, tmp)) {
          ret = TRUE;
          break;
        }
-      } else if (!memcmp(entry->data, tmp, len)) {
+      } else if (silc_buffer_len(entry) == len &&
+                !memcmp(entry->data, tmp, len)) {
        ret = TRUE;
        break;
       }
@@ -1809,6 +1823,7 @@ SilcBool silc_server_inviteban_process(SilcServer server,
 {
   unsigned char *tmp;
   SilcUInt32 type, len;
+  void *ptype;
   SilcBuffer tmp2;
   SilcHashTableList htl;
 
@@ -1835,8 +1850,9 @@ SilcBool silc_server_inviteban_process(SilcServer server,
 
        /* Check if the string is added already */
        silc_hash_table_list(list, &htl);
-       while (silc_hash_table_get(&htl, (void *)&type, (void *)&tmp2)) {
-         if (type == 1 && silc_string_match(tmp2->data, tmp)) {
+       while (silc_hash_table_get(&htl, (void *)&ptype, (void *)&tmp2)) {
+         if (SILC_PTR_TO_32(ptype) == 1 &&
+             silc_string_match(tmp2->data, tmp)) {
            tmp = NULL;
            break;
          }
@@ -1866,8 +1882,8 @@ SilcBool silc_server_inviteban_process(SilcServer server,
 
        /* Check if the public key is in the list already */
        silc_hash_table_list(list, &htl);
-       while (silc_hash_table_get(&htl, (void *)&type, (void *)&tmp2)) {
-         if (type == 2 && !memcmp(tmp2->data, tmp, len)) {
+       while (silc_hash_table_get(&htl, (void *)&ptype, (void *)&tmp2)) {
+         if (SILC_PTR_TO_32(ptype) == 2 && !memcmp(tmp2->data, tmp, len)) {
            tmp = NULL;
            break;
          }
@@ -1886,8 +1902,8 @@ SilcBool silc_server_inviteban_process(SilcServer server,
 
        /* Check if the ID is in the list already */
        silc_hash_table_list(list, &htl);
-       while (silc_hash_table_get(&htl, (void *)&type, (void *)&tmp2)) {
-         if (type == 3 && !memcmp(tmp2->data, tmp, len)) {
+       while (silc_hash_table_get(&htl, (void *)&ptype, (void *)&tmp2)) {
+         if (SILC_PTR_TO_32(ptype) == 3 && !memcmp(tmp2->data, tmp, len)) {
            tmp = NULL;
            break;
          }
@@ -1925,8 +1941,9 @@ SilcBool silc_server_inviteban_process(SilcServer server,
 
        /* Delete from the list */
        silc_hash_table_list(list, &htl);
-       while (silc_hash_table_get(&htl, (void *)&type, (void *)&tmp2)) {
-         if (type == 1 && silc_string_match(tmp2->data, tmp)) {
+       while (silc_hash_table_get(&htl, (void *)&ptype, (void *)&tmp2)) {
+         if (SILC_PTR_TO_32(ptype) == 1 &&
+             silc_string_match(tmp2->data, tmp)) {
            silc_hash_table_del_by_context(list, (void *)1, tmp2);
            break;
          }
@@ -1946,8 +1963,8 @@ SilcBool silc_server_inviteban_process(SilcServer server,
 
        /* Delete from the invite list */
        silc_hash_table_list(list, &htl);
-       while (silc_hash_table_get(&htl, (void *)&type, (void *)&tmp2)) {
-         if (type == 2 && !memcmp(tmp2->data, tmp, len)) {
+       while (silc_hash_table_get(&htl, (void *)&ptype, (void *)&tmp2)) {
+         if (SILC_PTR_TO_32(ptype) == 2 && !memcmp(tmp2->data, tmp, len)) {
            silc_hash_table_del_by_context(list, (void *)2, tmp2);
            break;
          }
@@ -1959,8 +1976,8 @@ SilcBool silc_server_inviteban_process(SilcServer server,
 
        /* Delete from the invite list */
        silc_hash_table_list(list, &htl);
-       while (silc_hash_table_get(&htl, (void *)&type, (void *)&tmp2)) {
-         if (type == 3 && !memcmp(tmp2->data, tmp, len)) {
+       while (silc_hash_table_get(&htl, (void *)&ptype, (void *)&tmp2)) {
+         if (SILC_PTR_TO_32(ptype) == 3 && !memcmp(tmp2->data, tmp, len)) {
            silc_hash_table_del_by_context(list, (void *)3, tmp2);
            break;
          }