updates.
[silc.git] / apps / silcd / packet_receive.c
index ef57bcd6acf250e4df931a5c280b72c71979815a..83e19b8ef7ef4d8949adcc009ca129b43ea7ce65 100644 (file)
 
 extern char *server_version;
 
+/* Check whereto relay the received notify packet that was destined
+   to a client. */
+
+static void 
+silc_server_packet_process_relay_notify(SilcServer server,
+                                       SilcSocketConnection sock,
+                                       SilcPacketContext *packet)
+{
+  SilcClientID *id;
+  SilcServerEntry router;
+  SilcSocketConnection dst_sock;
+  SilcClientEntry client;
+  SilcIDListData idata;
+
+  SILC_LOG_DEBUG(("Start"));
+
+  /* Decode destination Client ID */
+  id = silc_id_str2id(packet->dst_id, packet->dst_id_len, SILC_ID_CLIENT);
+  if (!id) {
+    SILC_LOG_ERROR(("Could not decode destination Client ID, dropped"));
+    return;
+  }
+
+  /* If the destination belongs to our server we don't have to route
+     the packet anywhere but to send it to the local destination. */
+  client = silc_idlist_find_client_by_id(server->local_list, id, NULL);
+  if (client) {
+    /* It exists, now deliver the packet to the destination */
+    dst_sock = (SilcSocketConnection)client->connection;
+
+    /* If we are router and the client has router then the client is in
+       our cell but not directly connected to us. */
+    if (server->server_type == SILC_ROUTER && client->router) {
+      /* We are of course in this case the client's router thus the real
+        "router" of the client is the server who owns the client. Thus
+        we will send the packet to that server. */
+      router = (SilcServerEntry)client->router;
+      idata = (SilcIDListData)router;
+      silc_server_packet_relay_notify(server, router->connection,
+                                     idata->send_key,
+                                     idata->hmac,
+                                     packet);
+      silc_free(id);
+      return;
+    }
+
+    /* Seems that client really is directly connected to us */
+    idata = (SilcIDListData)client;
+    silc_server_packet_relay_notify(server, dst_sock, 
+                                   idata->send_key,
+                                   idata->hmac, packet);
+    silc_free(id);
+    return;
+  }
+
+  /* Destination belongs to someone not in this server. If we are normal
+     server our action is to send the packet to our router. */
+  if (server->server_type == SILC_SERVER && !server->standalone) {
+    router = server->router;
+
+    /* Send to primary route */
+    if (router) {
+      dst_sock = (SilcSocketConnection)router->connection;
+      idata = (SilcIDListData)router;
+      silc_server_packet_relay_notify(server, dst_sock, 
+                                     idata->send_key,
+                                     idata->hmac, packet);
+    }
+    silc_free(id);
+    return;
+  }
+
+  /* We are router and we will perform route lookup for the destination 
+     and send the packet to fastest route. */
+  if (server->server_type == SILC_ROUTER && !server->standalone) {
+    /* Check first that the ID is valid */
+    client = silc_idlist_find_client_by_id(server->global_list, id, NULL);
+    if (client) {
+      dst_sock = silc_server_route_get(server, id, SILC_ID_CLIENT);
+      router = (SilcServerEntry)dst_sock->user_data;
+      idata = (SilcIDListData)router;
+
+      /* Get fastest route and send packet. */
+      if (router)
+       silc_server_packet_relay_notify(server, dst_sock, 
+                                       idata->send_key,
+                                       idata->hmac, packet);
+      silc_free(id);
+      return;
+    }
+  }
+}
+
 /* Received notify packet. Server can receive notify packets from router. 
    Server then relays the notify messages to clients if needed. */
 
@@ -52,6 +145,13 @@ void silc_server_notify(SilcServer server,
       packet->src_id_type != SILC_ID_SERVER)
     return;
 
+  /* If the packet is destined directly to a client, then we don't
+     process the packet at all but just relay it to the client. */
+  if (packet->dst_id_type == SILC_ID_CLIENT) {
+    silc_server_packet_process_relay_notify(server, sock, packet);
+    return;
+  }
+
   /* If we are router and this packet is not already broadcast packet
      we will broadcast it. The sending socket really cannot be router or
      the router is buggy. If this packet is coming from router then it must
@@ -174,8 +274,12 @@ void silc_server_notify(SilcServer server,
     channel = silc_idlist_find_channel_by_id(server->local_list, 
                                             channel_id, NULL);
     if (!channel) { 
-      silc_free(channel_id);
-      goto out;
+      channel = silc_idlist_find_channel_by_id(server->global_list, 
+                                              channel_id, NULL);
+      if (!channel) {
+       silc_free(channel_id);
+       goto out;
+      }
     }
 
     /* Get client ID */
@@ -246,7 +350,7 @@ void silc_server_notify(SilcServer server,
       tmp = NULL;
 
     /* Remove the client from all channels */
-    silc_server_remove_from_channels(server, NULL, client, tmp);
+    silc_server_remove_from_channels(server, NULL, client, TRUE, tmp, TRUE);
 
     /* Remove the client entry */
     if (!silc_idlist_del_client(server->global_list, client))
@@ -342,7 +446,7 @@ void silc_server_notify(SilcServer server,
 
        /* Send the NICK_CHANGE notify type to local clients on the channels
           this client is joined to. */
-       silc_server_send_notify_on_channels(server, client, 
+       silc_server_send_notify_on_channels(server, NULL, client, 
                                            SILC_NOTIFY_TYPE_NICK_CHANGE, 2,
                                            id, tmp_len, 
                                            id2, tmp_len);
@@ -534,6 +638,15 @@ void silc_server_notify(SilcServer server,
 
     break;
 
+  case SILC_NOTIFY_TYPE_KILLED:
+    /* 
+     * Distribute the notify to local clients on channels
+     */
+    
+    SILC_LOG_DEBUG(("KILLED notify"));
+      
+    break;
+
     /* Ignore rest of the notify types for now */
   case SILC_NOTIFY_TYPE_NONE:
   case SILC_NOTIFY_TYPE_MOTD:
@@ -617,14 +730,18 @@ void silc_server_private_message(SilcServer server,
 
   SILC_LOG_DEBUG(("Start"));
 
+  if (packet->src_id_type != SILC_ID_CLIENT ||
+      packet->dst_id_type != SILC_ID_CLIENT)
+    return;
+
   if (!packet->dst_id)
-    goto err;
+    return;
 
   /* Decode destination Client ID */
   id = silc_id_str2id(packet->dst_id, packet->dst_id_len, SILC_ID_CLIENT);
   if (!id) {
     SILC_LOG_ERROR(("Could not decode destination Client ID, dropped"));
-    goto err;
+    return;
   }
 
   /* If the destination belongs to our server we don't have to route
@@ -647,6 +764,7 @@ void silc_server_private_message(SilcServer server,
                                       idata->send_key,
                                       idata->hmac,
                                       packet);
+      silc_free(id);
       return;
     }
 
@@ -655,6 +773,7 @@ void silc_server_private_message(SilcServer server,
     silc_server_send_private_message(server, dst_sock, 
                                     idata->send_key,
                                     idata->hmac, packet);
+    silc_free(id);
     return;
   }
 
@@ -671,6 +790,7 @@ void silc_server_private_message(SilcServer server,
                                       idata->send_key,
                                       idata->hmac, packet);
     }
+    silc_free(id);
     return;
   }
 
@@ -689,13 +809,111 @@ void silc_server_private_message(SilcServer server,
        silc_server_send_private_message(server, dst_sock, 
                                         idata->send_key,
                                         idata->hmac, packet);
+      silc_free(id);
       return;
     }
   }
+}
+
+/* Received private message key packet.. This packet is never for us. It is to
+   the client in the packet's destination ID. Sending of this sort of packet
+   equals sending private message, ie. it is sent point to point from
+   one client to another. */
+
+void silc_server_private_message_key(SilcServer server,
+                                    SilcSocketConnection sock,
+                                    SilcPacketContext *packet)
+{
+  SilcClientID *id;
+  SilcServerEntry router;
+  SilcSocketConnection dst_sock;
+  SilcClientEntry client;
+  SilcIDListData idata;
+
+  SILC_LOG_DEBUG(("Start"));
+
+  if (packet->src_id_type != SILC_ID_CLIENT ||
+      packet->dst_id_type != SILC_ID_CLIENT)
+    return;
+
+  if (!packet->dst_id)
+    return;
+
+  /* Decode destination Client ID */
+  id = silc_id_str2id(packet->dst_id, packet->dst_id_len, SILC_ID_CLIENT);
+  if (!id) {
+    SILC_LOG_ERROR(("Could not decode destination Client ID, dropped"));
+    return;
+  }
+
+  /* If the destination belongs to our server we don't have to route
+     the message anywhere but to send it to the local destination. */
+  client = silc_idlist_find_client_by_id(server->local_list, id, NULL);
+  if (client) {
+    /* It exists, now deliver the message to the destination */
+    dst_sock = (SilcSocketConnection)client->connection;
 
- err:
-  silc_server_send_error(server, sock, 
-                        "No such nickname: Private message not sent");
+    /* If we are router and the client has router then the client is in
+       our cell but not directly connected to us. */
+    if (server->server_type == SILC_ROUTER && client->router) {
+      /* We are of course in this case the client's router thus the real
+        "router" of the client is the server who owns the client. Thus
+        we will send the packet to that server. */
+      router = (SilcServerEntry)client->router;
+      idata = (SilcIDListData)router;
+      silc_server_send_private_message_key(server, router->connection,
+                                          idata->send_key,
+                                          idata->hmac,
+                                          packet);
+      silc_free(id);
+      return;
+    }
+
+    /* Seems that client really is directly connected to us */
+    idata = (SilcIDListData)client;
+    silc_server_send_private_message_key(server, dst_sock, 
+                                        idata->send_key,
+                                        idata->hmac, packet);
+    silc_free(id);
+    return;
+  }
+
+  /* Destination belongs to someone not in this server. If we are normal
+     server our action is to send the packet to our router. */
+  if (server->server_type == SILC_SERVER && !server->standalone) {
+    router = server->router;
+
+    /* Send to primary route */
+    if (router) {
+      dst_sock = (SilcSocketConnection)router->connection;
+      idata = (SilcIDListData)router;
+      silc_server_send_private_message_key(server, dst_sock, 
+                                          idata->send_key,
+                                          idata->hmac, packet);
+    }
+    silc_free(id);
+    return;
+  }
+
+  /* We are router and we will perform route lookup for the destination 
+     and send the packet to fastest route. */
+  if (server->server_type == SILC_ROUTER && !server->standalone) {
+    /* Check first that the ID is valid */
+    client = silc_idlist_find_client_by_id(server->global_list, id, NULL);
+    if (client) {
+      dst_sock = silc_server_route_get(server, id, SILC_ID_CLIENT);
+      router = (SilcServerEntry)dst_sock->user_data;
+      idata = (SilcIDListData)router;
+
+      /* Get fastest route and send packet. */
+      if (router)
+       silc_server_send_private_message_key(server, dst_sock, 
+                                            idata->send_key,
+                                            idata->hmac, packet);
+      silc_free(id);
+      return;
+    }
+  }
 }
 
 /* Processes incoming command reply packet. The command reply packet may
@@ -823,6 +1041,34 @@ void silc_server_channel_message(SilcServer server,
     }
   }
 
+  /* If we are router and the packet came from router and private key
+     has not been set for the channel then we must encrypt the packet
+     as it was decrypted with the session key shared between us and the
+     router which sent it. This is so, because cells does not share the
+     same channel key */
+  if (server->server_type == SILC_ROUTER &&
+      sock->type == SILC_SOCKET_TYPE_ROUTER &&
+      !(channel->mode & SILC_CHANNEL_MODE_PRIVKEY)) {
+    SilcBuffer chp;
+    unsigned int iv_len, i, data_len;
+
+    iv_len = silc_cipher_get_block_len(channel->channel_key);
+    if (channel->iv[0] == '\0')
+      for (i = 0; i < iv_len; i++) channel->iv[i] = 
+                                    silc_rng_get_byte(server->rng);
+    else
+      silc_hash_make(server->md5hash, channel->iv, iv_len, channel->iv);
+    
+    /* Encode new payload. This encrypts it also. */
+    SILC_GET16_MSB(data_len, packet->buffer->data);
+    chp = silc_channel_payload_encode(data_len, packet->buffer->data + 2,
+                                     iv_len, channel->iv,
+                                     channel->channel_key,
+                                     channel->hmac, server->rng);
+    silc_buffer_put(packet->buffer, chp->data, chp->len);
+    silc_buffer_free(chp);
+  }
+
   /* Distribute the packet to our local clients. This will send the
      packet for further routing as well, if needed. */
   silc_server_packet_relay_to_channel(server, sock, channel, sender,
@@ -918,6 +1164,9 @@ SilcClientEntry silc_server_new_client(SilcServer server,
   silc_id_create_client_id(server->id, server->rng, server->md5hash,
                           username, &client_id);
 
+  if (strlen(username) > 128)
+    username[127] = '\0';
+
   /* Update client entry */
   idata->registered = TRUE;
   client->nickname = strdup(username);
@@ -1357,7 +1606,7 @@ void silc_server_new_channel(SilcServer server,
                    sock->hostname));
     
     silc_idlist_add_channel(server->global_list, channel_name, 0, channel_id, 
-                           server->router->connection, NULL);
+                           server->router->connection, NULL, NULL);
 
     server->stat.channels++;
   } else {
@@ -1381,7 +1630,7 @@ void silc_server_new_channel(SilcServer server,
        with the channel ID provided by the server. This creates a new
        key to the channel as well that we will send to the server. */
     if (!channel) {
-      channel = silc_server_create_new_channel_with_id(server, NULL,
+      channel = silc_server_create_new_channel_with_id(server, NULL, NULL,
                                                       channel_name,
                                                       channel_id, FALSE);
       if (!channel)
@@ -1512,3 +1761,104 @@ void silc_server_new_channel_list(SilcServer server,
   silc_buffer_free(buffer);
   silc_free(new);
 }
+
+/* Received key agreement packet. This packet is never for us. It is to
+   the client in the packet's destination ID. Sending of this sort of packet
+   equals sending private message, ie. it is sent point to point from
+   one client to another. */
+
+void silc_server_key_agreement(SilcServer server,
+                              SilcSocketConnection sock,
+                              SilcPacketContext *packet)
+{
+  SilcClientID *id;
+  SilcServerEntry router;
+  SilcSocketConnection dst_sock;
+  SilcClientEntry client;
+  SilcIDListData idata;
+
+  SILC_LOG_DEBUG(("Start"));
+
+  if (packet->src_id_type != SILC_ID_CLIENT ||
+      packet->dst_id_type != SILC_ID_CLIENT)
+    return;
+
+  if (!packet->dst_id)
+    return;
+
+  /* Decode destination Client ID */
+  id = silc_id_str2id(packet->dst_id, packet->dst_id_len, SILC_ID_CLIENT);
+  if (!id) {
+    SILC_LOG_ERROR(("Could not decode destination Client ID, dropped"));
+    return;
+  }
+
+  /* If the destination belongs to our server we don't have to route
+     the packet anywhere but to send it to the local destination. */
+  client = silc_idlist_find_client_by_id(server->local_list, id, NULL);
+  if (client) {
+    /* It exists, now deliver the packet to the destination */
+    dst_sock = (SilcSocketConnection)client->connection;
+
+    /* If we are router and the client has router then the client is in
+       our cell but not directly connected to us. */
+    if (server->server_type == SILC_ROUTER && client->router) {
+      /* We are of course in this case the client's router thus the real
+        "router" of the client is the server who owns the client. Thus
+        we will send the packet to that server. */
+      router = (SilcServerEntry)client->router;
+      idata = (SilcIDListData)router;
+      silc_server_send_key_agreement(server, router->connection,
+                                    idata->send_key,
+                                    idata->hmac,
+                                    packet);
+      silc_free(id);
+      return;
+    }
+
+    /* Seems that client really is directly connected to us */
+    idata = (SilcIDListData)client;
+    silc_server_send_key_agreement(server, dst_sock, 
+                                  idata->send_key,
+                                  idata->hmac, packet);
+    silc_free(id);
+    return;
+  }
+
+  /* Destination belongs to someone not in this server. If we are normal
+     server our action is to send the packet to our router. */
+  if (server->server_type == SILC_SERVER && !server->standalone) {
+    router = server->router;
+
+    /* Send to primary route */
+    if (router) {
+      dst_sock = (SilcSocketConnection)router->connection;
+      idata = (SilcIDListData)router;
+      silc_server_send_key_agreement(server, dst_sock, 
+                                    idata->send_key,
+                                    idata->hmac, packet);
+    }
+    silc_free(id);
+    return;
+  }
+
+  /* We are router and we will perform route lookup for the destination 
+     and send the packet to fastest route. */
+  if (server->server_type == SILC_ROUTER && !server->standalone) {
+    /* Check first that the ID is valid */
+    client = silc_idlist_find_client_by_id(server->global_list, id, NULL);
+    if (client) {
+      dst_sock = silc_server_route_get(server, id, SILC_ID_CLIENT);
+      router = (SilcServerEntry)dst_sock->user_data;
+      idata = (SilcIDListData)router;
+
+      /* Get fastest route and send packet. */
+      if (router)
+       silc_server_send_key_agreement(server, dst_sock, 
+                                      idata->send_key,
+                                      idata->hmac, packet);
+      silc_free(id);
+      return;
+    }
+  }
+}