Merged silc_1_1_branch to trunk.
[silc.git] / apps / silcd / server_util.c
index f7eb97b8d36298b6cbd600d1441e39903bd9614a..19ea23e67821e2efb77f6fe817ce5bc773890ed3 100644 (file)
@@ -189,17 +189,19 @@ SilcBool silc_server_remove_clients_by_server(SilcServer server,
 
       if (server_signoff) {
        idp = silc_id_payload_encode(client->id, SILC_ID_CLIENT);
-       argv = silc_realloc(argv, sizeof(*argv) * (argc + 1));
-       argv_lens = silc_realloc(argv_lens, sizeof(*argv_lens) *
-                                (argc + 1));
-       argv_types = silc_realloc(argv_types, sizeof(*argv_types) *
-                                 (argc + 1));
-       argv[argc] = silc_calloc(silc_buffer_len(idp), sizeof(*argv[0]));
-       memcpy(argv[argc], idp->data, silc_buffer_len(idp));
-       argv_lens[argc] = silc_buffer_len(idp);
-       argv_types[argc] = argc + 1;
-       argc++;
-       silc_buffer_free(idp);
+       if (idp) {
+         argv = silc_realloc(argv, sizeof(*argv) * (argc + 1));
+         argv_lens = silc_realloc(argv_lens, sizeof(*argv_lens) *
+                                  (argc + 1));
+         argv_types = silc_realloc(argv_types, sizeof(*argv_types) *
+                                   (argc + 1));
+         argv[argc] = silc_calloc(silc_buffer_len(idp), sizeof(*argv[0]));
+         memcpy(argv[argc], idp->data, silc_buffer_len(idp));
+         argv_lens[argc] = silc_buffer_len(idp);
+         argv_types[argc] = argc + 1;
+         argc++;
+         silc_buffer_free(idp);
+       }
       }
 
       /* Update statistics */
@@ -209,10 +211,13 @@ SilcBool silc_server_remove_clients_by_server(SilcServer server,
       SILC_OPER_STATS_UPDATE(client, server, SILC_UMODE_SERVER_OPERATOR);
       SILC_OPER_STATS_UPDATE(client, router, SILC_UMODE_ROUTER_OPERATOR);
 
-      if (client->data.public_key)
-       silc_hash_table_del_by_context(server->pk_hash,
-                                      client->data.public_key,
-                                      client);
+      /* Remove client's public key from repository, this will free it too. */
+      if (client->data.public_key) {
+       silc_skr_del_public_key(server->repository, client->data.public_key,
+                               client);
+       client->data.public_key = NULL;
+      }
+
       silc_server_remove_clients_channels(server, entry, clients,
                                          client, channels);
       silc_server_del_from_watcher_list(server, client);
@@ -223,6 +228,7 @@ SilcBool silc_server_remove_clients_by_server(SilcServer server,
        client->mode = 0;
        client->router = NULL;
        client->connection = NULL;
+       silc_dlist_add(server->expired_clients, client);
       } else {
        silc_idlist_del_data(client);
        silc_idlist_del_client(server->local_list, client);
@@ -266,10 +272,13 @@ SilcBool silc_server_remove_clients_by_server(SilcServer server,
       SILC_OPER_STATS_UPDATE(client, server, SILC_UMODE_SERVER_OPERATOR);
       SILC_OPER_STATS_UPDATE(client, router, SILC_UMODE_ROUTER_OPERATOR);
 
-      if (client->data.public_key)
-       silc_hash_table_del_by_context(server->pk_hash,
-                                      client->data.public_key,
-                                      client);
+      /* Remove client's public key from repository, this will free it too. */
+      if (client->data.public_key) {
+       silc_skr_del_public_key(server->repository, client->data.public_key,
+                               client);
+       client->data.public_key = NULL;
+      }
+
       silc_server_remove_clients_channels(server, entry, clients,
                                          client, channels);
       silc_server_del_from_watcher_list(server, client);
@@ -280,6 +289,7 @@ SilcBool silc_server_remove_clients_by_server(SilcServer server,
        client->mode = 0;
        client->router = NULL;
        client->connection = NULL;
+       silc_dlist_add(server->expired_clients, client);
       } else {
        silc_idlist_del_data(client);
        silc_idlist_del_client(server->global_list, client);
@@ -403,10 +413,8 @@ silc_server_update_clients_by_real_server(SilcServer server,
           since the server is local. */
        if (!local) {
          SILC_LOG_DEBUG(("Moving client to local list"));
-         silc_idcache_add(server->local_list->clients, client_cache->name,
-                          client_cache->id, client_cache->context);
-         silc_idcache_del_by_context(server->global_list->clients, client,
-                                     NULL);
+         silc_idcache_move(server->global_list->clients,
+                           server->local_list->clients, client_cache);
        }
        server_entry = server_entry->router;
       } else {
@@ -415,20 +423,16 @@ silc_server_update_clients_by_real_server(SilcServer server,
           since the server is local. */
        if (server_entry->server_type != SILC_BACKUP_ROUTER && !local) {
          SILC_LOG_DEBUG(("Moving client to local list"));
-         silc_idcache_add(server->local_list->clients, client_cache->name,
-                          client_cache->id, client_cache->context);
-         silc_idcache_del_by_context(server->global_list->clients, client,
-                                     NULL);
+         silc_idcache_move(server->global_list->clients,
+                           server->local_list->clients, client_cache);
 
        } else if (server->server_type == SILC_BACKUP_ROUTER && local) {
          /* If we are backup router and this client is on local list, we
             must move it to global list, as it is not currently local to
             us (we are not primary). */
          SILC_LOG_DEBUG(("Moving client to global list"));
-         silc_idcache_add(server->global_list->clients, client_cache->name,
-                          client_cache->id, client_cache->context);
-         silc_idcache_del_by_context(server->local_list->clients, client,
-                                     NULL);
+         silc_idcache_move(server->local_list->clients,
+                           server->global_list->clients, client_cache);
        }
       }
 
@@ -455,10 +459,8 @@ silc_server_update_clients_by_real_server(SilcServer server,
           since the server is global. */
        if (local) {
          SILC_LOG_DEBUG(("Moving client to global list"));
-         silc_idcache_add(server->global_list->clients, client_cache->name,
-                          client_cache->id, client_cache->context);
-         silc_idcache_del_by_context(server->local_list->clients, client,
-                                     NULL);
+         silc_idcache_move(server->local_list->clients,
+                           server->global_list->clients, client_cache);
        }
        server_entry = server_entry->router;
       } else {
@@ -467,10 +469,8 @@ silc_server_update_clients_by_real_server(SilcServer server,
           since the server is global. */
        if (server_entry->server_type != SILC_BACKUP_ROUTER && local) {
          SILC_LOG_DEBUG(("Moving client to global list"));
-         silc_idcache_add(server->global_list->clients, client_cache->name,
-                          client_cache->id, client_cache->context);
-         silc_idcache_del_by_context(server->local_list->clients, client,
-                                     NULL);
+         silc_idcache_move(server->local_list->clients,
+                           server->global_list->clients, client_cache);
        }
       }
       return server_entry;
@@ -999,7 +999,8 @@ SilcUInt32 silc_server_num_sockets_by_ip(SilcServer server, const char *ip,
   while ((conn = silc_dlist_get(server->conns))) {
     if (!conn->sock)
       continue;
-    silc_socket_stream_get_info(conn->sock, NULL, NULL, &ipaddr, NULL);
+    silc_socket_stream_get_info(silc_packet_stream_get_stream(conn->sock),
+                               NULL, NULL, &ipaddr, NULL);
     idata = silc_packet_get_context(conn->sock);
     if (!strcmp(ipaddr, ip) && idata && idata->conn_type == type)
       count++;
@@ -1025,7 +1026,8 @@ silc_server_find_socket_by_host(SilcServer server,
     if (!conn->sock)
       continue;
     idata = silc_packet_get_context(conn->sock);
-    silc_socket_stream_get_info(conn->sock, NULL, NULL, &ipaddr, NULL);
+    silc_socket_stream_get_info(silc_packet_stream_get_stream(conn->sock),
+                               NULL, NULL, &ipaddr, NULL);
     if (!strcmp(ipaddr, ip) &&
        (!port || conn->remote_port == port) &&
        idata->conn_type == type)
@@ -1042,19 +1044,29 @@ silc_server_find_socket_by_host(SilcServer server,
 SilcUInt32 silc_server_num_sockets_by_remote(SilcServer server,
                                             const char *ip,
                                             const char *hostname,
-                                            SilcUInt16 port)
+                                            SilcUInt16 port,
+                                            SilcConnectionType type)
 {
   SilcServerConnection conn;
+  SilcIDListData idata;
+  SilcConnectionType t = SILC_CONN_UNKNOWN;
   int count = 0;
 
   if (!ip && !hostname)
     return 0;
 
+  SILC_LOG_DEBUG(("Num connections %d", silc_dlist_count(server->conns)));
+
   silc_dlist_start(server->conns);
   while ((conn = silc_dlist_get(server->conns))) {
+    if (conn->sock) {
+      idata = silc_packet_get_context(conn->sock);
+      if (idata)
+       t = idata->conn_type;
+    }
     if (((ip && !strcmp(conn->remote_host, ip)) ||
         (hostname && !strcmp(conn->remote_host, hostname))) &&
-       conn->remote_port == port)
+       conn->remote_port == port && t == type)
       count++;
   }
 
@@ -1080,8 +1092,7 @@ static void find_callback(SilcSKR skr, SilcSKRFind find,
   silc_skr_find_free(find);
 }
 
-/* Get public key. For public key tables that has multiple keys in it the
-   silc_server_find_public_key must be used. */
+/* Get public key by key usage and key context. */
 
 SilcPublicKey silc_server_get_public_key(SilcServer server,
                                         SilcSKRKeyUsage usage,
@@ -1104,6 +1115,27 @@ SilcPublicKey silc_server_get_public_key(SilcServer server,
   return public_key;
 }
 
+/* Find public key by client for identification purposes.  Finds keys
+   with SILC_SKR_USAGE_IDENTIFICATION. */
+
+SilcBool silc_server_get_public_key_by_client(SilcServer server,
+                                             SilcClientEntry client,
+                                             SilcPublicKey *public_key)
+{
+  SilcPublicKey pubkey = NULL;
+  SilcBool ret = FALSE;
+
+  pubkey = silc_server_get_public_key(server, SILC_SKR_USAGE_IDENTIFICATION,
+                                     client);
+  if (pubkey)
+    ret = TRUE;
+
+  if (public_key)
+    *public_key = pubkey;
+
+  return ret;
+}
+
 /* Check whether the connection `sock' is allowed to connect to us.  This
    checks for example whether there is too much connections for this host,
    and required version for the host etc. */
@@ -1126,7 +1158,8 @@ SilcBool silc_server_connection_allowed(SilcServer server,
   char *r_vendor_version = NULL, *l_vendor_version;
   const char *hostname, *ip;
 
-  silc_socket_stream_get_info(sock, NULL, &hostname, &ip, NULL);
+  silc_socket_stream_get_info(silc_packet_stream_get_stream(sock),
+                             NULL, &hostname, &ip, NULL);
 
   SILC_LOG_DEBUG(("Checking whether connection is allowed"));
 
@@ -1190,6 +1223,7 @@ SilcBool silc_server_connection_allowed(SilcServer server,
                  global->connections_max_per_host);
 
   if (max_hosts && conn_number >= max_hosts) {
+    SILC_LOG_DEBUG(("Server is full, %d >= %d", conn_number, max_hosts));
     SILC_LOG_INFO(("Server is full, closing %s (%s) connection",
                   hostname, ip));
     silc_server_disconnect_remote(server, sock,
@@ -1199,6 +1233,8 @@ SilcBool silc_server_connection_allowed(SilcServer server,
   }
 
   if (num_sockets >= max_per_host) {
+    SILC_LOG_DEBUG(("Too many connections, %d >= %d", num_sockets,
+                   max_per_host));
     SILC_LOG_INFO(("Too many connections from %s (%s), closing connection",
                   hostname, ip));
     silc_server_disconnect_remote(server, sock,
@@ -1214,9 +1250,9 @@ SilcBool silc_server_connection_allowed(SilcServer server,
    of the checks fails FALSE is returned. */
 
 SilcBool silc_server_check_cmode_rights(SilcServer server,
-                                   SilcChannelEntry channel,
-                                   SilcChannelClientEntry client,
-                                   SilcUInt32 mode)
+                                       SilcChannelEntry channel,
+                                       SilcChannelClientEntry client,
+                                       SilcUInt32 mode)
 {
   SilcBool is_op = client->mode & SILC_CHANNEL_UMODE_CHANOP;
   SilcBool is_fo = client->mode & SILC_CHANNEL_UMODE_CHANFO;
@@ -1326,8 +1362,8 @@ SilcBool silc_server_check_cmode_rights(SilcServer server,
    FALSE if setting some mode is not allowed. */
 
 SilcBool silc_server_check_umode_rights(SilcServer server,
-                                   SilcClientEntry client,
-                                   SilcUInt32 mode)
+                                       SilcClientEntry client,
+                                       SilcUInt32 mode)
 {
   SilcBool server_op = FALSE, router_op = FALSE;
 
@@ -1505,10 +1541,12 @@ void silc_server_kill_client(SilcServer server,
     SILC_OPER_STATS_UPDATE(remote_client, server, SILC_UMODE_SERVER_OPERATOR);
     SILC_OPER_STATS_UPDATE(remote_client, router, SILC_UMODE_ROUTER_OPERATOR);
 
-    if (remote_client->data.public_key)
-      silc_hash_table_del_by_context(server->pk_hash,
-                                     remote_client->data.public_key,
-                                     remote_client);
+    /* Remove client's public key from repository, this will free it too. */
+    if (remote_client->data.public_key) {
+      silc_skr_del_public_key(server->repository,
+                             remote_client->data.public_key, remote_client);
+      remote_client->data.public_key = NULL;
+    }
 
     if (SILC_IS_LOCAL(remote_client)) {
       server->stat.my_clients--;
@@ -1570,9 +1608,9 @@ silc_server_check_watcher_list_foreach(void *key, void *context,
    notify change of notify type indicated by `notify'. */
 
 SilcBool silc_server_check_watcher_list(SilcServer server,
-                                   SilcClientEntry client,
-                                   const char *new_nick,
-                                   SilcNotifyType notify)
+                                       SilcClientEntry client,
+                                       const char *new_nick,
+                                       SilcNotifyType notify)
 {
   unsigned char hash[16];
   WatcherNotifyContext n;
@@ -1621,7 +1659,7 @@ SilcBool silc_server_check_watcher_list(SilcServer server,
    is not watching any nicknames. */
 
 SilcBool silc_server_del_from_watcher_list(SilcServer server,
-                                      SilcClientEntry client)
+                                          SilcClientEntry client)
 {
   SilcHashTableList htl;
   void *key;
@@ -1764,8 +1802,10 @@ SilcBool silc_server_inviteban_match(SilcServer server, SilcHashTable list,
 
 /* Process invite or ban information */
 
-SilcBool silc_server_inviteban_process(SilcServer server, SilcHashTable list,
-                                  SilcUInt8 action, SilcArgumentPayload args)
+SilcBool silc_server_inviteban_process(SilcServer server,
+                                      SilcHashTable list,
+                                      SilcUInt8 action,
+                                      SilcArgumentPayload args)
 {
   unsigned char *tmp;
   SilcUInt32 type, len;
@@ -1950,7 +1990,7 @@ void silc_server_create_connections(SilcServer server)
   silc_schedule_task_del_by_callback(server->schedule,
                                     silc_server_connect_to_router);
   silc_schedule_task_add_timeout(server->schedule,
-                                silc_server_connect_to_router, server, 0, 1);
+                                silc_server_connect_to_router, server, 1, 0);
 }
 
 static void
@@ -2155,10 +2195,10 @@ SilcStatus silc_server_set_channel_pk_list(SilcServer server,
    on the `channel' public key list. */
 
 SilcBool silc_server_verify_channel_auth(SilcServer server,
-                                    SilcChannelEntry channel,
-                                    SilcClientID *client_id,
-                                    const unsigned char *auth,
-                                    SilcUInt32 auth_len)
+                                        SilcChannelEntry channel,
+                                        SilcClientID *client_id,
+                                        const unsigned char *auth,
+                                        SilcUInt32 auth_len)
 {
   SilcAuthPayload ap;
   SilcPublicKey chpk;