5 Author: Pekka Riikonen <priikone@silcnet.org>
7 Copyright (C) 1997 - 2002 Pekka Riikonen
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
21 * Server packet routines to handle received packets.
25 #include "serverincludes.h"
26 #include "server_internal.h"
28 /* Received notify packet. Server can receive notify packets from router.
29 Server then relays the notify messages to clients if needed. */
31 void silc_server_notify(SilcServer server,
32 SilcSocketConnection sock,
33 SilcPacketContext *packet)
35 SilcNotifyPayload payload;
37 SilcArgumentPayload args;
38 SilcChannelID *channel_id = NULL, *channel_id2;
39 SilcClientID *client_id, *client_id2;
40 SilcServerID *server_id;
42 SilcChannelEntry channel = NULL;
43 SilcClientEntry client = NULL, client2 = NULL;
44 SilcServerEntry server_entry = NULL;
45 SilcChannelClientEntry chl;
46 SilcIDCacheEntry cache;
47 SilcHashTableList htl;
53 SILC_LOG_DEBUG(("Start"));
55 if (sock->type == SILC_SOCKET_TYPE_CLIENT ||
56 packet->src_id_type != SILC_ID_SERVER)
62 /* If the packet is destined directly to a client then relay the packet
63 before processing it. */
64 if (packet->dst_id_type == SILC_ID_CLIENT) {
66 SilcSocketConnection dst_sock;
68 /* Get the route to the client */
69 dst_sock = silc_server_get_client_route(server, packet->dst_id,
70 packet->dst_id_len, NULL,
73 /* Relay the packet */
74 silc_server_relay_packet(server, dst_sock, idata->send_key,
75 idata->hmac_send, idata->psn_send++,
79 /* Parse the Notify Payload */
80 payload = silc_notify_payload_parse(packet->buffer->data,
85 /* If we are router and this packet is not already broadcast packet
86 we will broadcast it. The sending socket really cannot be router or
87 the router is buggy. If this packet is coming from router then it must
88 have the broadcast flag set already and we won't do anything. */
89 if (!server->standalone && server->server_type == SILC_ROUTER &&
90 sock->type == SILC_SOCKET_TYPE_SERVER &&
91 !(packet->flags & SILC_PACKET_FLAG_BROADCAST)) {
92 SILC_LOG_DEBUG(("Broadcasting received Notify packet"));
93 if (packet->dst_id_type == SILC_ID_CHANNEL) {
94 /* Packet is destined to channel */
95 channel_id = silc_id_str2id(packet->dst_id, packet->dst_id_len,
100 silc_server_packet_send_dest(server, server->router->connection,
102 packet->flags | SILC_PACKET_FLAG_BROADCAST,
103 channel_id, SILC_ID_CHANNEL,
104 packet->buffer->data, packet->buffer->len,
106 silc_server_backup_send_dest(server, (SilcServerEntry)sock->user_data,
107 packet->type, packet->flags,
108 channel_id, SILC_ID_CHANNEL,
109 packet->buffer->data, packet->buffer->len,
112 /* Packet is destined to client or server */
113 silc_server_packet_send(server, server->router->connection,
115 packet->flags | SILC_PACKET_FLAG_BROADCAST,
116 packet->buffer->data, packet->buffer->len,
118 silc_server_backup_send(server, (SilcServerEntry)sock->user_data,
119 packet->type, packet->flags,
120 packet->buffer->data, packet->buffer->len,
125 type = silc_notify_get_type(payload);
126 args = silc_notify_get_args(payload);
131 case SILC_NOTIFY_TYPE_JOIN:
133 * Distribute the notify to local clients on the channel
135 SILC_LOG_DEBUG(("JOIN notify"));
138 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
141 channel_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
145 /* Get channel entry */
146 channel = silc_idlist_find_channel_by_id(server->global_list,
149 channel = silc_idlist_find_channel_by_id(server->local_list,
152 silc_free(channel_id);
156 silc_free(channel_id);
159 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
162 client_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
166 /* If the the client is not in local list we check global list (ie. the
167 channel will be global channel) and if it does not exist then create
168 entry for the client. */
169 client = silc_idlist_find_client_by_id(server->global_list,
170 client_id, server->server_type,
173 client = silc_idlist_find_client_by_id(server->local_list,
174 client_id, server->server_type,
177 /* If router did not find the client the it is bogus */
178 if (server->server_type != SILC_SERVER)
182 silc_idlist_add_client(server->global_list, NULL, NULL, NULL,
183 silc_id_dup(client_id, SILC_ID_CLIENT),
184 sock->user_data, NULL, 0);
186 SILC_LOG_ERROR(("Could not add new client to the ID Cache"));
187 silc_free(client_id);
191 client->data.status |= SILC_IDLIST_STATUS_REGISTERED;
195 /* Do not process the notify if the client is not registered */
196 if (!(client->data.status & SILC_IDLIST_STATUS_REGISTERED))
199 /* Do not add client to channel if it is there already */
200 if (silc_server_client_on_channel(client, channel, NULL)) {
201 SILC_LOG_DEBUG(("Client already on channel"));
205 /* Send to channel */
206 silc_server_packet_send_to_channel(server, sock, channel, packet->type,
207 FALSE, packet->buffer->data,
208 packet->buffer->len, FALSE);
210 if (server->server_type != SILC_ROUTER &&
211 sock->type == SILC_SOCKET_TYPE_ROUTER)
212 /* The channel is global now */
213 channel->global_users = TRUE;
215 SILC_LOG_DEBUG(("Joining to channel %s", channel->channel_name));
217 /* JOIN the global client to the channel (local clients (if router
218 created the channel) is joined in the pending JOIN command). */
219 chl = silc_calloc(1, sizeof(*chl));
220 chl->client = client;
221 chl->channel = channel;
223 /* If this is the first one on the channel then it is the founder of
225 if (!silc_hash_table_count(channel->user_list))
226 chl->mode = (SILC_CHANNEL_UMODE_CHANOP | SILC_CHANNEL_UMODE_CHANFO);
228 silc_hash_table_add(channel->user_list, client, chl);
229 silc_hash_table_add(client->channels, channel, chl);
230 silc_free(client_id);
231 channel->user_count++;
235 case SILC_NOTIFY_TYPE_LEAVE:
237 * Distribute the notify to local clients on the channel
239 SILC_LOG_DEBUG(("LEAVE notify"));
242 channel_id = silc_id_str2id(packet->dst_id, packet->dst_id_len,
243 packet->dst_id_type);
248 /* Get channel entry */
249 channel = silc_idlist_find_channel_by_id(server->global_list,
252 channel = silc_idlist_find_channel_by_id(server->local_list,
255 silc_free(channel_id);
261 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
263 silc_free(channel_id);
266 client_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
268 silc_free(channel_id);
272 /* Get client entry */
273 client = silc_idlist_find_client_by_id(server->global_list,
274 client_id, TRUE, NULL);
276 client = silc_idlist_find_client_by_id(server->local_list,
277 client_id, TRUE, NULL);
279 silc_free(client_id);
280 silc_free(channel_id);
284 silc_free(client_id);
286 /* Check if on channel */
287 if (!silc_server_client_on_channel(client, channel, NULL))
290 /* Send the leave notify to channel */
291 silc_server_packet_send_to_channel(server, sock, channel, packet->type,
292 FALSE, packet->buffer->data,
293 packet->buffer->len, FALSE);
295 /* Remove the user from channel */
296 silc_server_remove_from_one_channel(server, sock, channel, client, FALSE);
299 case SILC_NOTIFY_TYPE_SIGNOFF:
301 * Distribute the notify to local clients on the channel
303 SILC_LOG_DEBUG(("SIGNOFF notify"));
306 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
309 client_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
313 /* Get client entry */
314 client = silc_idlist_find_client_by_id(server->global_list,
315 client_id, TRUE, &cache);
317 client = silc_idlist_find_client_by_id(server->local_list,
318 client_id, TRUE, &cache);
320 silc_free(client_id);
324 silc_free(client_id);
326 /* Get signoff message */
327 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
331 /* Update statistics */
332 server->stat.clients--;
333 if (server->stat.cell_clients)
334 server->stat.cell_clients--;
335 SILC_OPER_STATS_UPDATE(client, server, SILC_UMODE_SERVER_OPERATOR);
336 SILC_OPER_STATS_UPDATE(client, router, SILC_UMODE_ROUTER_OPERATOR);
338 /* Remove the client from all channels. */
339 silc_server_remove_from_channels(server, NULL, client, TRUE, tmp, FALSE);
341 /* Check if anyone is watching this nickname */
342 if (server->server_type == SILC_ROUTER)
343 silc_server_check_watcher_list(server, client, NULL,
344 SILC_NOTIFY_TYPE_SIGNOFF);
346 client->data.status &= ~SILC_IDLIST_STATUS_REGISTERED;
347 cache->expire = SILC_ID_CACHE_EXPIRE_DEF;
350 case SILC_NOTIFY_TYPE_TOPIC_SET:
352 * Distribute the notify to local clients on the channel
355 SILC_LOG_DEBUG(("TOPIC SET notify"));
358 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
361 client_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
365 /* Get client entry */
366 client = silc_idlist_find_client_by_id(server->global_list,
367 client_id, TRUE, &cache);
369 client = silc_idlist_find_client_by_id(server->local_list,
370 client_id, TRUE, &cache);
372 silc_free(client_id);
376 silc_free(client_id);
379 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
381 silc_free(channel_id);
386 channel_id = silc_id_str2id(packet->dst_id, packet->dst_id_len,
387 packet->dst_id_type);
392 /* Get channel entry */
393 channel = silc_idlist_find_channel_by_id(server->global_list,
396 channel = silc_idlist_find_channel_by_id(server->local_list,
399 silc_free(channel_id);
404 if (channel->topic && !strcmp(channel->topic, tmp))
407 /* Get user's channel entry and check that topic set is allowed. */
408 if (!silc_server_client_on_channel(client, channel, &chl))
410 if (channel->mode & SILC_CHANNEL_MODE_TOPIC &&
411 !(chl->mode & SILC_CHANNEL_UMODE_CHANOP) &&
412 !(chl->mode & SILC_CHANNEL_UMODE_CHANFO)) {
413 SILC_LOG_DEBUG(("Topic change is not allowed"));
417 /* Change the topic */
418 silc_free(channel->topic);
419 channel->topic = strdup(tmp);
421 /* Send the same notify to the channel */
422 silc_server_packet_send_to_channel(server, sock, channel, packet->type,
423 FALSE, packet->buffer->data,
424 packet->buffer->len, FALSE);
425 silc_free(channel_id);
428 case SILC_NOTIFY_TYPE_NICK_CHANGE:
431 * Distribute the notify to local clients on the channel
433 unsigned char *id, *id2;
435 SilcUInt32 nickname_len;
437 SILC_LOG_DEBUG(("NICK CHANGE notify"));
439 /* Get old client ID */
440 id = silc_argument_get_arg_type(args, 1, &tmp_len);
443 client_id = silc_id_payload_parse_id(id, tmp_len, NULL);
447 /* Get new client ID */
448 id2 = silc_argument_get_arg_type(args, 2, &tmp_len);
451 client_id2 = silc_id_payload_parse_id(id2, tmp_len, NULL);
455 SILC_LOG_DEBUG(("Old Client ID id(%s)",
456 silc_id_render(client_id, SILC_ID_CLIENT)));
457 SILC_LOG_DEBUG(("New Client ID id(%s)",
458 silc_id_render(client_id2, SILC_ID_CLIENT)));
460 /* From protocol version 1.1 we also get the new nickname */
461 nickname = silc_argument_get_arg_type(args, 3, &nickname_len);;
463 /* Replace the Client ID */
464 client = silc_idlist_replace_client_id(server,
465 server->global_list, client_id,
466 client_id2, nickname);
468 client = silc_idlist_replace_client_id(server,
469 server->local_list, client_id,
470 client_id2, nickname);
473 /* Send the NICK_CHANGE notify type to local clients on the channels
474 this client is joined to. */
475 silc_server_send_notify_on_channels(server, client, client,
476 SILC_NOTIFY_TYPE_NICK_CHANGE, 3,
477 id, tmp_len, id2, tmp_len,
482 silc_free(client_id);
484 silc_free(client_id2);
488 case SILC_NOTIFY_TYPE_CMODE_CHANGE:
490 * Distribute the notify to local clients on the channel
493 SILC_LOG_DEBUG(("CMODE CHANGE notify"));
496 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
499 client_id = silc_id_payload_parse_id(tmp, tmp_len, &id_type);
503 /* Get client entry */
504 if (id_type == SILC_ID_CLIENT) {
505 client = silc_idlist_find_client_by_id(server->global_list,
506 client_id, TRUE, &cache);
508 client = silc_idlist_find_client_by_id(server->local_list,
509 client_id, TRUE, &cache);
511 silc_free(client_id);
515 silc_free(client_id);
519 channel_id = silc_id_str2id(packet->dst_id, packet->dst_id_len,
520 packet->dst_id_type);
525 /* Get channel entry */
526 channel = silc_idlist_find_channel_by_id(server->global_list,
529 channel = silc_idlist_find_channel_by_id(server->local_list,
532 silc_free(channel_id);
536 silc_free(channel_id);
539 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
542 SILC_GET32_MSB(mode, tmp);
544 /* Check if mode changed */
545 if (channel->mode == mode)
548 /* Get user's channel entry and check that mode change is allowed */
550 if (!silc_server_client_on_channel(client, channel, &chl))
552 if (!silc_server_check_cmode_rights(server, channel, chl, mode)) {
553 SILC_LOG_DEBUG(("CMODE change is not allowed"));
558 /* Send the same notify to the channel */
559 silc_server_packet_send_to_channel(server, sock, channel, packet->type,
560 FALSE, packet->buffer->data,
561 packet->buffer->len, FALSE);
563 /* If the channel had private keys set and the mode was removed then
564 we must re-generate and re-distribute a new channel key */
565 if (channel->mode & SILC_CHANNEL_MODE_PRIVKEY &&
566 !(mode & SILC_CHANNEL_MODE_PRIVKEY)) {
567 /* Re-generate channel key */
568 if (!silc_server_create_channel_key(server, channel, 0))
571 /* Send the channel key. This sends it to our local clients and if
572 we are normal server to our router as well. */
573 silc_server_send_channel_key(server, NULL, channel,
574 server->server_type == SILC_ROUTER ?
575 FALSE : !server->standalone);
579 tmp = silc_argument_get_arg_type(args, 4, &tmp_len);
581 unsigned char hash[32];
584 silc_hmac_free(channel->hmac);
585 if (!silc_hmac_alloc(tmp, NULL, &channel->hmac))
588 /* Set the HMAC key out of current channel key. The client must do
590 silc_hash_make(silc_hmac_get_hash(channel->hmac), channel->key,
591 channel->key_len / 8, hash);
592 silc_hmac_set_key(channel->hmac, hash,
593 silc_hash_len(silc_hmac_get_hash(channel->hmac)));
594 memset(hash, 0, sizeof(hash));
597 /* Get the passphrase */
598 tmp = silc_argument_get_arg_type(args, 5, &tmp_len);
600 silc_free(channel->passphrase);
601 channel->passphrase = silc_memdup(tmp, tmp_len);
604 /* Get founder public key */
605 tmp = silc_argument_get_arg_type(args, 6, &tmp_len);
606 if (tmp && mode & SILC_CHANNEL_MODE_FOUNDER_AUTH) {
607 if (channel->founder_key)
608 silc_pkcs_public_key_free(channel->founder_key);
609 channel->founder_key = NULL;
610 silc_pkcs_public_key_decode(tmp, tmp_len, &channel->founder_key);
612 if (!channel->founder_key ||
613 (client && client->data.public_key &&
614 server->server_type == SILC_ROUTER &&
615 !silc_pkcs_public_key_compare(channel->founder_key,
616 client->data.public_key))) {
617 /* A really buggy server isn't checking public keys correctly.
618 It's not possible that the mode setter and founder wouldn't
619 have same public key. */
620 SILC_LOG_DEBUG(("Enforcing sender to change channel mode"));
622 mode &= ~SILC_CHANNEL_MODE_FOUNDER_AUTH;
623 silc_server_send_notify_cmode(server, sock, FALSE, channel,
624 mode, server->id, SILC_ID_SERVER,
627 channel->passphrase, NULL);
628 if (channel->founder_key)
629 silc_pkcs_public_key_free(channel->founder_key);
630 channel->founder_key = NULL;
631 } else if (!client->data.public_key) {
632 client->data.public_key =
633 silc_pkcs_public_key_copy(channel->founder_key);
638 channel->mode = mode;
640 if (!(channel->mode & SILC_CHANNEL_MODE_FOUNDER_AUTH) &&
641 channel->founder_key) {
642 silc_pkcs_public_key_free(channel->founder_key);
643 channel->founder_key = NULL;
648 case SILC_NOTIFY_TYPE_CUMODE_CHANGE:
651 * Distribute the notify to local clients on the channel
653 SilcChannelClientEntry chl2 = NULL;
654 bool notify_sent = FALSE;
656 SILC_LOG_DEBUG(("CUMODE CHANGE notify"));
659 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
662 client_id = silc_id_payload_parse_id(tmp, tmp_len, &id_type);
666 /* Get client entry */
667 if (id_type == SILC_ID_CLIENT) {
668 client = silc_idlist_find_client_by_id(server->global_list,
669 client_id, TRUE, &cache);
671 client = silc_idlist_find_client_by_id(server->local_list,
672 client_id, TRUE, &cache);
674 silc_free(client_id);
678 silc_free(client_id);
682 channel_id = silc_id_str2id(packet->dst_id, packet->dst_id_len,
683 packet->dst_id_type);
688 /* Get channel entry */
689 channel = silc_idlist_find_channel_by_id(server->global_list,
692 channel = silc_idlist_find_channel_by_id(server->local_list,
695 silc_free(channel_id);
701 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
703 silc_free(channel_id);
707 SILC_GET32_MSB(mode, tmp);
709 /* Get target client */
710 tmp = silc_argument_get_arg_type(args, 3, &tmp_len);
713 client_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
717 /* Get client entry */
718 client2 = silc_idlist_find_client_by_id(server->global_list,
719 client_id, TRUE, NULL);
721 client2 = silc_idlist_find_client_by_id(server->local_list,
722 client_id, TRUE, NULL);
724 silc_free(client_id);
728 silc_free(client_id);
731 /* Check that sender is on channel */
732 if (!silc_server_client_on_channel(client, channel, &chl))
735 if (client != client2) {
736 /* Sender must be operator */
737 if (!(chl->mode & SILC_CHANNEL_UMODE_CHANOP) &&
738 !(chl->mode & SILC_CHANNEL_UMODE_CHANFO)) {
739 SILC_LOG_DEBUG(("CUMODE change is not allowed"));
743 /* Check that target is on channel */
744 if (!silc_server_client_on_channel(client2, channel, &chl))
747 /* If target is founder mode change is not allowed. */
748 if (chl->mode & SILC_CHANNEL_UMODE_CHANFO) {
749 SILC_LOG_DEBUG(("CUMODE change is not allowed"));
755 if (mode & SILC_CHANNEL_UMODE_CHANFO &&
756 !(chl->mode & SILC_CHANNEL_UMODE_CHANFO) &&
757 server->server_type == SILC_ROUTER) {
758 /* Check whether this client is allowed to be channel founder on
760 SilcPublicKey founder_key;
762 /* If channel doesn't have founder auth mode then it's impossible
763 that someone would be getting founder rights with CUMODE command.
764 In that case there already either is founder or there isn't
765 founder at all on the channel. */
766 if (!(channel->mode & SILC_CHANNEL_MODE_FOUNDER_AUTH)) {
767 /* Force the mode to not have founder mode */
768 mode &= ~SILC_CHANNEL_UMODE_CHANFO;
769 silc_server_force_cumode_change(server, sock, channel, chl, mode);
774 /* Get the founder of the channel and if found then this client
775 cannot be the founder since there already is one. */
776 silc_hash_table_list(channel->user_list, &htl);
777 while (silc_hash_table_get(&htl, NULL, (void *)&chl2))
778 if (chl2->mode & SILC_CHANNEL_UMODE_CHANFO) {
779 mode &= ~SILC_CHANNEL_UMODE_CHANFO;
780 silc_server_force_cumode_change(server, sock, channel, chl, mode);
784 silc_hash_table_list_reset(&htl);
785 if (!(mode & SILC_CHANNEL_UMODE_CHANFO))
788 /* Founder not found of the channel. Since the founder auth mode
789 is set on the channel now check whether this is the client that
790 originally set the mode. */
792 /* Get public key that must be present in notify */
793 tmp = silc_argument_get_arg_type(args, 4, &tmp_len);
794 if (!tmp || !silc_pkcs_public_key_decode(tmp, tmp_len,
796 mode &= ~SILC_CHANNEL_UMODE_CHANFO;
797 silc_server_force_cumode_change(server, sock, channel, chl, mode);
802 /* Now match the public key we have cached and publick key sent.
804 if (!silc_pkcs_public_key_compare(channel->founder_key,
805 client->data.public_key)) {
806 mode &= ~SILC_CHANNEL_UMODE_CHANFO;
807 silc_server_force_cumode_change(server, sock, channel, chl, mode);
813 SILC_LOG_DEBUG(("Changing the channel user mode"));
815 /* Change the mode */
818 /* Send the same notify to the channel */
820 silc_server_packet_send_to_channel(server, sock, channel,
822 FALSE, packet->buffer->data,
823 packet->buffer->len, FALSE);
825 silc_free(channel_id);
829 case SILC_NOTIFY_TYPE_INVITE:
831 if (packet->dst_id_type == SILC_ID_CLIENT)
834 SILC_LOG_DEBUG(("INVITE notify"));
837 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
840 channel_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
844 /* Get channel entry */
845 channel = silc_idlist_find_channel_by_id(server->global_list,
848 channel = silc_idlist_find_channel_by_id(server->local_list,
851 silc_free(channel_id);
855 silc_free(channel_id);
858 tmp = silc_argument_get_arg_type(args, 3, &tmp_len);
861 client_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
865 /* Get client entry */
866 client = silc_idlist_find_client_by_id(server->global_list,
867 client_id, TRUE, &cache);
869 client = silc_idlist_find_client_by_id(server->local_list,
870 client_id, TRUE, &cache);
872 silc_free(client_id);
876 silc_free(client_id);
878 /* Get user's channel entry and check that inviting is allowed. */
879 if (!silc_server_client_on_channel(client, channel, &chl))
881 if (channel->mode & SILC_CHANNEL_MODE_INVITE &&
882 !(chl->mode & SILC_CHANNEL_UMODE_CHANOP) &&
883 !(chl->mode & SILC_CHANNEL_UMODE_CHANFO)) {
884 SILC_LOG_DEBUG(("Inviting is not allowed"));
888 /* Get the added invite */
889 tmp = silc_argument_get_arg_type(args, 4, &tmp_len);
891 if (!channel->invite_list)
892 channel->invite_list = silc_calloc(tmp_len + 2,
893 sizeof(*channel->invite_list));
895 channel->invite_list = silc_realloc(channel->invite_list,
896 sizeof(*channel->invite_list) *
898 strlen(channel->invite_list) +
900 if (tmp[tmp_len - 1] == ',')
901 tmp[tmp_len - 1] = '\0';
903 strncat(channel->invite_list, tmp, tmp_len);
904 strncat(channel->invite_list, ",", 1);
907 /* Get the deleted invite */
908 tmp = silc_argument_get_arg_type(args, 5, &tmp_len);
909 if (tmp && channel->invite_list) {
910 char *start, *end, *n;
912 if (!strncmp(channel->invite_list, tmp,
913 strlen(channel->invite_list) - 1)) {
914 silc_free(channel->invite_list);
915 channel->invite_list = NULL;
917 start = strstr(channel->invite_list, tmp);
918 if (start && strlen(start) >= tmp_len) {
919 end = start + tmp_len;
920 n = silc_calloc(strlen(channel->invite_list) - tmp_len, sizeof(*n));
921 strncat(n, channel->invite_list, start - channel->invite_list);
922 strncat(n, end + 1, ((channel->invite_list +
923 strlen(channel->invite_list)) - end) - 1);
924 silc_free(channel->invite_list);
925 channel->invite_list = n;
932 case SILC_NOTIFY_TYPE_CHANNEL_CHANGE:
934 * Distribute to the local clients on the channel and change the
938 SILC_LOG_DEBUG(("CHANNEL CHANGE"));
940 if (sock->type != SILC_SOCKET_TYPE_ROUTER)
943 /* Get the old Channel ID */
944 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
947 channel_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
951 /* Get the channel entry */
952 channel = silc_idlist_find_channel_by_id(server->local_list,
955 channel = silc_idlist_find_channel_by_id(server->global_list,
958 silc_free(channel_id);
963 /* Send the notify to the channel */
964 silc_server_packet_send_to_channel(server, sock, channel, packet->type,
965 FALSE, packet->buffer->data,
966 packet->buffer->len, FALSE);
968 /* Get the new Channel ID */
969 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
972 channel_id2 = silc_id_payload_parse_id(tmp, tmp_len, NULL);
976 SILC_LOG_DEBUG(("Old Channel ID id(%s)",
977 silc_id_render(channel_id, SILC_ID_CHANNEL)));
978 SILC_LOG_DEBUG(("New Channel ID id(%s)",
979 silc_id_render(channel_id2, SILC_ID_CHANNEL)));
981 /* Replace the Channel ID */
982 if (!silc_idlist_replace_channel_id(server->local_list, channel_id,
984 if (!silc_idlist_replace_channel_id(server->global_list, channel_id,
986 silc_free(channel_id2);
991 SilcBuffer users = NULL, users_modes = NULL;
993 /* Re-announce this channel which ID was changed. */
994 silc_server_send_new_channel(server, sock, FALSE, channel->channel_name,
996 silc_id_get_len(channel->id,
1000 /* Re-announce our clients on the channel as the ID has changed now */
1001 silc_server_announce_get_channel_users(server, channel, &users,
1004 silc_buffer_push(users, users->data - users->head);
1005 silc_server_packet_send(server, sock,
1006 SILC_PACKET_NOTIFY, SILC_PACKET_FLAG_LIST,
1007 users->data, users->len, FALSE);
1008 silc_buffer_free(users);
1011 silc_buffer_push(users_modes, users_modes->data - users_modes->head);
1012 silc_server_packet_send_dest(server, sock,
1013 SILC_PACKET_NOTIFY, SILC_PACKET_FLAG_LIST,
1014 channel->id, SILC_ID_CHANNEL,
1016 users_modes->len, FALSE);
1017 silc_buffer_free(users_modes);
1020 /* Re-announce channel's topic */
1021 if (channel->topic) {
1022 silc_server_send_notify_topic_set(server, sock,
1023 server->server_type == SILC_ROUTER ?
1024 TRUE : FALSE, channel,
1025 channel->id, SILC_ID_CHANNEL,
1030 silc_free(channel_id);
1034 case SILC_NOTIFY_TYPE_SERVER_SIGNOFF:
1036 * Remove the server entry and all clients that this server owns.
1039 SILC_LOG_DEBUG(("SERVER SIGNOFF notify"));
1042 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
1045 server_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
1049 /* Get server entry */
1050 server_entry = silc_idlist_find_server_by_id(server->global_list,
1051 server_id, TRUE, NULL);
1053 if (!server_entry) {
1054 server_entry = silc_idlist_find_server_by_id(server->local_list,
1055 server_id, TRUE, NULL);
1057 if (!server_entry) {
1058 /* If we are normal server then we might not have the server. Check
1059 whether router was kind enough to send the list of all clients
1060 that actually was to be removed. Remove them if the list is
1062 if (server->server_type != SILC_ROUTER &&
1063 silc_argument_get_arg_num(args) > 1) {
1066 for (i = 1; i < silc_argument_get_arg_num(args); i++) {
1068 tmp = silc_argument_get_arg_type(args, i + 1, &tmp_len);
1071 client_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
1075 /* Get client entry */
1076 client = silc_idlist_find_client_by_id(server->global_list,
1077 client_id, TRUE, &cache);
1080 client = silc_idlist_find_client_by_id(server->local_list,
1081 client_id, TRUE, &cache);
1084 silc_free(client_id);
1088 silc_free(client_id);
1090 /* Update statistics */
1091 server->stat.clients--;
1092 if (server->stat.cell_clients)
1093 server->stat.cell_clients--;
1094 SILC_OPER_STATS_UPDATE(client, server, SILC_UMODE_SERVER_OPERATOR);
1095 SILC_OPER_STATS_UPDATE(client, router, SILC_UMODE_ROUTER_OPERATOR);
1097 /* Remove the client from all channels. */
1098 silc_server_remove_from_channels(server, NULL, client,
1101 /* Check if anyone is watching this nickname */
1102 if (server->server_type == SILC_ROUTER)
1103 silc_server_check_watcher_list(server, client, NULL,
1104 SILC_NOTIFY_TYPE_SERVER_SIGNOFF);
1106 /* Remove this client from watcher list if it is */
1108 silc_server_del_from_watcher_list(server, client);
1110 /* Remove the client */
1111 silc_idlist_del_client(local ? server->local_list :
1112 server->global_list, client);
1116 silc_free(server_id);
1120 silc_free(server_id);
1122 /* Free all client entries that this server owns as they will
1123 become invalid now as well. */
1124 silc_server_remove_clients_by_server(server, server_entry, TRUE);
1126 /* Remove the server entry */
1127 silc_idlist_del_server(local ? server->local_list :
1128 server->global_list, server_entry);
1130 /* XXX update statistics */
1134 case SILC_NOTIFY_TYPE_KICKED:
1136 * Distribute the notify to local clients on the channel
1139 SILC_LOG_DEBUG(("KICKED notify"));
1142 channel_id = silc_id_str2id(packet->dst_id, packet->dst_id_len,
1143 packet->dst_id_type);
1148 /* Get channel entry */
1149 channel = silc_idlist_find_channel_by_id(server->global_list,
1152 channel = silc_idlist_find_channel_by_id(server->local_list,
1155 silc_free(channel_id);
1159 silc_free(channel_id);
1162 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
1165 client_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
1169 /* If the the client is not in local list we check global list */
1170 client = silc_idlist_find_client_by_id(server->global_list,
1171 client_id, TRUE, NULL);
1173 client = silc_idlist_find_client_by_id(server->local_list,
1174 client_id, TRUE, NULL);
1176 silc_free(client_id);
1180 silc_free(client_id);
1182 /* If target is founder they cannot be kicked */
1183 if (!silc_server_client_on_channel(client, channel, &chl))
1185 if (chl->mode & SILC_CHANNEL_UMODE_CHANFO)
1188 /* From protocol version 1.1 we get the kicker's ID as well. */
1189 tmp = silc_argument_get_arg_type(args, 3, &tmp_len);
1191 client_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
1195 /* If the the client is not in local list we check global list */
1196 client2 = silc_idlist_find_client_by_id(server->global_list,
1197 client_id, TRUE, NULL);
1199 client2 = silc_idlist_find_client_by_id(server->local_list,
1200 client_id, TRUE, NULL);
1202 silc_free(client_id);
1206 silc_free(client_id);
1208 /* Kicker must be operator on channel */
1209 if (!silc_server_client_on_channel(client2, channel, &chl))
1211 if (!(chl->mode & SILC_CHANNEL_UMODE_CHANOP) &&
1212 !(chl->mode & SILC_CHANNEL_UMODE_CHANFO)) {
1213 SILC_LOG_DEBUG(("Kicking is not allowed"));
1218 /* Send to channel */
1219 silc_server_packet_send_to_channel(server, sock, channel, packet->type,
1220 FALSE, packet->buffer->data,
1221 packet->buffer->len, FALSE);
1223 /* Remove the client from channel */
1224 silc_server_remove_from_one_channel(server, sock, channel, client, FALSE);
1228 case SILC_NOTIFY_TYPE_KILLED:
1231 * Distribute the notify to local clients on channels
1233 unsigned char *id, *comment;
1234 SilcUInt32 id_len, comment_len;
1236 SILC_LOG_DEBUG(("KILLED notify"));
1239 id = silc_argument_get_arg_type(args, 1, &id_len);
1242 client_id = silc_id_payload_parse_id(id, id_len, NULL);
1246 /* If the the client is not in local list we check global list */
1247 client = silc_idlist_find_client_by_id(server->global_list,
1248 client_id, TRUE, NULL);
1250 client = silc_idlist_find_client_by_id(server->local_list,
1251 client_id, TRUE, NULL);
1253 silc_free(client_id);
1257 silc_free(client_id);
1259 /* If the client is one of ours, then close the connection to the
1260 client now. This removes the client from all channels as well. */
1261 if (packet->dst_id_type == SILC_ID_CLIENT && client->connection) {
1262 sock = client->connection;
1263 silc_server_free_client_data(server, NULL, client, FALSE, NULL);
1264 silc_server_close_connection(server, sock);
1269 comment = silc_argument_get_arg_type(args, 2, &comment_len);
1270 if (comment_len > 128)
1273 /* From protocol version 1.1 we get the killer's ID as well. */
1274 tmp = silc_argument_get_arg_type(args, 3, &tmp_len);
1276 client_id = silc_id_payload_parse_id(tmp, tmp_len, &id_type);
1280 if (id_type == SILC_ID_CLIENT) {
1281 /* If the the client is not in local list we check global list */
1282 client2 = silc_idlist_find_client_by_id(server->global_list,
1283 client_id, TRUE, NULL);
1285 client2 = silc_idlist_find_client_by_id(server->local_list,
1286 client_id, TRUE, NULL);
1288 silc_free(client_id);
1292 silc_free(client_id);
1294 /* Killer must be router operator */
1295 if (!(client2->mode & SILC_UMODE_ROUTER_OPERATOR)) {
1296 SILC_LOG_DEBUG(("Killing is not allowed"));
1302 /* Send the notify to local clients on the channels except to the
1303 client who is killed. */
1304 silc_server_send_notify_on_channels(server, client, client,
1305 SILC_NOTIFY_TYPE_KILLED, 3,
1306 id, id_len, comment, comment_len,
1309 /* Remove the client from all channels */
1310 silc_server_remove_from_channels(server, NULL, client, FALSE, NULL,
1313 /* Check if anyone is watching this nickname */
1314 if (server->server_type == SILC_ROUTER)
1315 silc_server_check_watcher_list(server, client, NULL,
1316 SILC_NOTIFY_TYPE_KILLED);
1321 case SILC_NOTIFY_TYPE_UMODE_CHANGE:
1323 * Save the mode of the client.
1326 SILC_LOG_DEBUG(("UMODE_CHANGE notify"));
1329 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
1332 client_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
1336 /* Get client entry */
1337 client = silc_idlist_find_client_by_id(server->global_list,
1338 client_id, TRUE, NULL);
1340 client = silc_idlist_find_client_by_id(server->local_list,
1341 client_id, TRUE, NULL);
1343 silc_free(client_id);
1347 silc_free(client_id);
1350 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
1353 SILC_GET32_MSB(mode, tmp);
1355 /* Check that mode changing is allowed. */
1356 if (!silc_server_check_umode_rights(server, client, mode)) {
1357 SILC_LOG_DEBUG(("UMODE change is not allowed"));
1361 /* Remove internal resumed flag if client is marked detached now */
1362 if (mode & SILC_UMODE_DETACHED)
1363 client->data.status &= ~SILC_IDLIST_STATUS_RESUMED;
1365 /* Change the mode */
1366 client->mode = mode;
1368 /* Check if anyone is watching this nickname */
1369 if (server->server_type == SILC_ROUTER)
1370 silc_server_check_watcher_list(server, client, NULL,
1371 SILC_NOTIFY_TYPE_UMODE_CHANGE);
1375 case SILC_NOTIFY_TYPE_BAN:
1380 SILC_LOG_DEBUG(("BAN notify"));
1382 /* Get Channel ID */
1383 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
1386 channel_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
1390 /* Get channel entry */
1391 channel = silc_idlist_find_channel_by_id(server->global_list,
1394 channel = silc_idlist_find_channel_by_id(server->local_list,
1397 silc_free(channel_id);
1401 silc_free(channel_id);
1403 /* Get the new ban and add it to the ban list */
1404 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
1406 if (!channel->ban_list)
1407 channel->ban_list = silc_calloc(tmp_len + 2,
1408 sizeof(*channel->ban_list));
1410 channel->ban_list = silc_realloc(channel->ban_list,
1411 sizeof(*channel->ban_list) *
1413 strlen(channel->ban_list) + 2));
1414 strncat(channel->ban_list, tmp, tmp_len);
1415 strncat(channel->ban_list, ",", 1);
1418 /* Get the ban to be removed and remove it from the list */
1419 tmp = silc_argument_get_arg_type(args, 3, &tmp_len);
1420 if (tmp && channel->ban_list) {
1421 char *start, *end, *n;
1423 if (!strncmp(channel->ban_list, tmp, strlen(channel->ban_list) - 1)) {
1424 silc_free(channel->ban_list);
1425 channel->ban_list = NULL;
1427 start = strstr(channel->ban_list, tmp);
1428 if (start && strlen(start) >= tmp_len) {
1429 end = start + tmp_len;
1430 n = silc_calloc(strlen(channel->ban_list) - tmp_len, sizeof(*n));
1431 strncat(n, channel->ban_list, start - channel->ban_list);
1432 strncat(n, end + 1, ((channel->ban_list +
1433 strlen(channel->ban_list)) - end) - 1);
1434 silc_free(channel->ban_list);
1435 channel->ban_list = n;
1441 case SILC_NOTIFY_TYPE_ERROR:
1448 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
1449 if (!tmp && tmp_len != 1)
1451 error = (SilcStatus)tmp[0];
1453 SILC_LOG_DEBUG(("ERROR notify (%d)", error));
1455 if (error == SILC_STATUS_ERR_NO_SUCH_CLIENT_ID &&
1456 sock->type == SILC_SOCKET_TYPE_ROUTER) {
1457 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
1459 SILC_LOG_DEBUG(("Received invalid client ID notification, deleting "
1460 "the entry from cache"));
1461 client_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
1464 client = silc_idlist_find_client_by_id(server->global_list,
1465 client_id, FALSE, NULL);
1467 silc_server_remove_from_channels(server, NULL, client, TRUE,
1469 silc_idlist_del_client(server->global_list, client);
1471 silc_free(client_id);
1477 /* Ignore rest of the notify types for now */
1478 case SILC_NOTIFY_TYPE_NONE:
1479 case SILC_NOTIFY_TYPE_MOTD:
1486 silc_notify_payload_free(payload);
1489 void silc_server_notify_list(SilcServer server,
1490 SilcSocketConnection sock,
1491 SilcPacketContext *packet)
1493 SilcPacketContext *new;
1497 SILC_LOG_DEBUG(("Processing Notify List"));
1499 if (sock->type == SILC_SOCKET_TYPE_CLIENT ||
1500 packet->src_id_type != SILC_ID_SERVER)
1503 /* Make copy of the original packet context, except for the actual
1504 data buffer, which we will here now fetch from the original buffer. */
1505 new = silc_packet_context_alloc();
1506 new->type = SILC_PACKET_NOTIFY;
1507 new->flags = packet->flags;
1508 new->src_id = packet->src_id;
1509 new->src_id_len = packet->src_id_len;
1510 new->src_id_type = packet->src_id_type;
1511 new->dst_id = packet->dst_id;
1512 new->dst_id_len = packet->dst_id_len;
1513 new->dst_id_type = packet->dst_id_type;
1515 buffer = silc_buffer_alloc(1024);
1516 new->buffer = buffer;
1518 while (packet->buffer->len) {
1519 SILC_GET16_MSB(len, packet->buffer->data + 2);
1520 if (len > packet->buffer->len)
1523 if (len > buffer->truelen) {
1524 silc_buffer_free(buffer);
1525 buffer = silc_buffer_alloc(1024 + len);
1528 silc_buffer_pull_tail(buffer, len);
1529 silc_buffer_put(buffer, packet->buffer->data, len);
1531 /* Process the Notify */
1532 silc_server_notify(server, sock, new);
1534 silc_buffer_push_tail(buffer, len);
1535 silc_buffer_pull(packet->buffer, len);
1538 silc_buffer_free(buffer);
1542 /* Received private message. This resolves the destination of the message
1543 and sends the packet. This is used by both server and router. If the
1544 destination is our locally connected client this sends the packet to
1545 the client. This may also send the message for further routing if
1546 the destination is not in our server (or router). */
1548 void silc_server_private_message(SilcServer server,
1549 SilcSocketConnection sock,
1550 SilcPacketContext *packet)
1552 SilcSocketConnection dst_sock;
1553 SilcIDListData idata;
1554 SilcClientEntry client;
1556 SILC_LOG_DEBUG(("Start"));
1558 if (packet->src_id_type != SILC_ID_CLIENT ||
1559 packet->dst_id_type != SILC_ID_CLIENT || !packet->dst_id)
1562 /* Get the route to the client */
1563 dst_sock = silc_server_get_client_route(server, packet->dst_id,
1564 packet->dst_id_len, NULL,
1568 unsigned char error;
1570 if (client && client->mode & SILC_UMODE_DETACHED) {
1571 SILC_LOG_DEBUG(("Client is detached, discarding packet"));
1575 /* Send SILC_NOTIFY_TYPE_ERROR to indicate that such destination ID
1576 does not exist or is invalid. */
1577 idp = silc_id_payload_encode_data(packet->dst_id,
1579 packet->dst_id_type);
1583 error = SILC_STATUS_ERR_NO_SUCH_CLIENT_ID;
1584 if (packet->src_id_type == SILC_ID_CLIENT) {
1585 SilcClientID *client_id = silc_id_str2id(packet->src_id,
1587 packet->src_id_type);
1588 silc_server_send_notify_dest(server, sock, FALSE,
1589 client_id, SILC_ID_CLIENT,
1590 SILC_NOTIFY_TYPE_ERROR, 2,
1592 idp->data, idp->len);
1593 silc_free(client_id);
1595 silc_server_send_notify(server, sock, FALSE,
1596 SILC_NOTIFY_TYPE_ERROR, 2,
1598 idp->data, idp->len);
1601 silc_buffer_free(idp);
1605 /* Check whether destination client wishes to receive private messages */
1606 if (client && !(packet->flags & SILC_PACKET_FLAG_PRIVMSG_KEY) &&
1607 client->mode & SILC_UMODE_BLOCK_PRIVMSG) {
1608 SILC_LOG_DEBUG(("Client blocks private messages, discarding packet"));
1612 /* Send the private message */
1613 silc_server_send_private_message(server, dst_sock, idata->send_key,
1614 idata->hmac_send, idata->psn_send++,
1618 /* Received private message key packet.. This packet is never for us. It is to
1619 the client in the packet's destination ID. Sending of this sort of packet
1620 equals sending private message, ie. it is sent point to point from
1621 one client to another. */
1623 void silc_server_private_message_key(SilcServer server,
1624 SilcSocketConnection sock,
1625 SilcPacketContext *packet)
1627 SilcSocketConnection dst_sock;
1628 SilcIDListData idata;
1630 SILC_LOG_DEBUG(("Start"));
1632 if (packet->src_id_type != SILC_ID_CLIENT ||
1633 packet->dst_id_type != SILC_ID_CLIENT)
1636 if (!packet->dst_id)
1639 /* Get the route to the client */
1640 dst_sock = silc_server_get_client_route(server, packet->dst_id,
1641 packet->dst_id_len, NULL,
1646 /* Relay the packet */
1647 silc_server_relay_packet(server, dst_sock, idata->send_key,
1648 idata->hmac_send, idata->psn_send++, packet, FALSE);
1651 /* Processes incoming command reply packet. The command reply packet may
1652 be destined to one of our clients or it may directly for us. We will
1653 call the command reply routine after processing the packet. */
1655 void silc_server_command_reply(SilcServer server,
1656 SilcSocketConnection sock,
1657 SilcPacketContext *packet)
1659 SilcBuffer buffer = packet->buffer;
1660 SilcClientEntry client = NULL;
1661 SilcSocketConnection dst_sock;
1662 SilcIDListData idata;
1663 SilcClientID *id = NULL;
1665 SILC_LOG_DEBUG(("Start"));
1667 /* Source must be server or router */
1668 if (packet->src_id_type != SILC_ID_SERVER &&
1669 sock->type != SILC_SOCKET_TYPE_ROUTER)
1672 if (packet->dst_id_type == SILC_ID_CHANNEL)
1675 if (packet->dst_id_type == SILC_ID_CLIENT) {
1676 /* Destination must be one of ours */
1677 id = silc_id_str2id(packet->dst_id, packet->dst_id_len, SILC_ID_CLIENT);
1680 client = silc_idlist_find_client_by_id(server->local_list, id, TRUE, NULL);
1682 SILC_LOG_ERROR(("Cannot process command reply to unknown client"));
1688 if (packet->dst_id_type == SILC_ID_SERVER) {
1689 /* For now this must be for us */
1690 if (memcmp(packet->dst_id, server->id_string, server->id_string_len)) {
1691 SILC_LOG_ERROR(("Cannot process command reply to unknown server"));
1696 /* Execute command reply locally for the command */
1697 silc_server_command_reply_process(server, sock, buffer);
1699 if (packet->dst_id_type == SILC_ID_CLIENT && client && id) {
1700 /* Relay the packet to the client */
1701 const SilcBufferStruct p;
1703 dst_sock = (SilcSocketConnection)client->connection;
1704 idata = (SilcIDListData)client;
1706 silc_buffer_push(buffer, SILC_PACKET_HEADER_LEN + packet->src_id_len
1707 + packet->dst_id_len + packet->padlen);
1708 if (!silc_packet_send_prepare(dst_sock, 0, 0, buffer->len,
1709 idata->hmac_send, (const SilcBuffer)&p)) {
1710 SILC_LOG_ERROR(("Cannot send packet"));
1713 silc_buffer_put((SilcBuffer)&p, buffer->data, buffer->len);
1715 /* Encrypt packet */
1716 silc_packet_encrypt(idata->send_key, idata->hmac_send, idata->psn_send++,
1717 (SilcBuffer)&p, buffer->len);
1719 /* Send the packet */
1720 silc_server_packet_send_real(server, dst_sock, TRUE);
1726 /* Process received channel message. The message can be originated from
1727 client or server. */
1729 void silc_server_channel_message(SilcServer server,
1730 SilcSocketConnection sock,
1731 SilcPacketContext *packet)
1733 SilcChannelEntry channel = NULL;
1734 SilcChannelID *id = NULL;
1735 void *sender_id = NULL;
1736 SilcClientEntry sender_entry = NULL;
1737 SilcChannelClientEntry chl;
1740 SILC_LOG_DEBUG(("Processing channel message"));
1743 if (packet->dst_id_type != SILC_ID_CHANNEL) {
1744 SILC_LOG_DEBUG(("Received bad message for channel, dropped"));
1748 /* Find channel entry */
1749 id = silc_id_str2id(packet->dst_id, packet->dst_id_len, SILC_ID_CHANNEL);
1752 channel = silc_idlist_find_channel_by_id(server->local_list, id, NULL);
1754 channel = silc_idlist_find_channel_by_id(server->global_list, id, NULL);
1757 unsigned char error;
1759 /* Send SILC_NOTIFY_TYPE_ERROR to indicate that such destination ID
1760 does not exist or is invalid. */
1761 idp = silc_id_payload_encode_data(packet->dst_id,
1763 packet->dst_id_type);
1767 error = SILC_STATUS_ERR_NO_SUCH_CHANNEL_ID;
1768 if (packet->src_id_type == SILC_ID_CLIENT) {
1769 SilcClientID *client_id = silc_id_str2id(packet->src_id,
1771 packet->src_id_type);
1772 silc_server_send_notify_dest(server, sock, FALSE,
1773 client_id, SILC_ID_CLIENT,
1774 SILC_NOTIFY_TYPE_ERROR, 2,
1775 &error, 1, idp->data, idp->len);
1776 silc_free(client_id);
1778 silc_server_send_notify(server, sock, FALSE,
1779 SILC_NOTIFY_TYPE_ERROR, 2,
1780 &error, 1, idp->data, idp->len);
1783 silc_buffer_free(idp);
1788 /* See that this client is on the channel. If the original sender is
1789 not client (as it can be server as well) we don't do the check. */
1790 sender_id = silc_id_str2id(packet->src_id, packet->src_id_len,
1791 packet->src_id_type);
1794 if (packet->src_id_type == SILC_ID_CLIENT) {
1795 sender_entry = silc_idlist_find_client_by_id(server->local_list,
1796 sender_id, TRUE, NULL);
1797 if (!sender_entry) {
1799 sender_entry = silc_idlist_find_client_by_id(server->global_list,
1800 sender_id, TRUE, NULL);
1802 if (!sender_entry || !silc_server_client_on_channel(sender_entry,
1804 SILC_LOG_DEBUG(("Client not on channel"));
1808 /* If channel is moderated check that client is allowed to send
1810 if (channel->mode & SILC_CHANNEL_MODE_SILENCE_USERS &&
1811 !(chl->mode & SILC_CHANNEL_UMODE_CHANOP) &&
1812 !(chl->mode & SILC_CHANNEL_UMODE_CHANFO)) {
1813 SILC_LOG_DEBUG(("Channel is silenced from normal users"));
1816 if (channel->mode & SILC_CHANNEL_MODE_SILENCE_OPERS &&
1817 chl->mode & SILC_CHANNEL_UMODE_CHANOP &&
1818 !(chl->mode & SILC_CHANNEL_UMODE_CHANFO)) {
1819 SILC_LOG_DEBUG(("Channel is silenced from operators"));
1823 /* If the packet is coming from router, but the client entry is local
1824 entry to us then some router is rerouting this to us and it is not
1825 allowed. When the client is local to us it means that we've routed
1826 this packet to network, and now someone is routing it back to us. */
1827 if (server->server_type == SILC_ROUTER &&
1828 sock->type == SILC_SOCKET_TYPE_ROUTER && local) {
1829 SILC_LOG_DEBUG(("Channel message rerouted to the sender, drop it"));
1834 /* Distribute the packet to our local clients. This will send the
1835 packet for further routing as well, if needed. */
1836 silc_server_packet_relay_to_channel(server, sock, channel, sender_id,
1837 packet->src_id_type, sender_entry,
1838 packet->buffer->data,
1839 packet->buffer->len, FALSE);
1842 silc_free(sender_id);
1846 /* Received channel key packet. We distribute the key to all of our locally
1847 connected clients on the channel. */
1849 void silc_server_channel_key(SilcServer server,
1850 SilcSocketConnection sock,
1851 SilcPacketContext *packet)
1853 SilcBuffer buffer = packet->buffer;
1854 SilcChannelEntry channel;
1856 if (packet->src_id_type != SILC_ID_SERVER ||
1857 (server->server_type == SILC_ROUTER &&
1858 sock->type == SILC_SOCKET_TYPE_ROUTER))
1861 /* Save the channel key */
1862 channel = silc_server_save_channel_key(server, buffer, NULL);
1866 /* Distribute the key to everybody who is on the channel. If we are router
1867 we will also send it to locally connected servers. */
1868 silc_server_send_channel_key(server, sock, channel, FALSE);
1870 if (server->server_type != SILC_BACKUP_ROUTER) {
1871 /* Distribute to local cell backup routers. */
1872 silc_server_backup_send(server, (SilcServerEntry)sock->user_data,
1873 SILC_PACKET_CHANNEL_KEY, 0,
1874 buffer->data, buffer->len, FALSE, TRUE);
1878 /* Received New Client packet and processes it. Creates Client ID for the
1879 client. Client becomes registered after calling this functions. */
1881 SilcClientEntry silc_server_new_client(SilcServer server,
1882 SilcSocketConnection sock,
1883 SilcPacketContext *packet)
1885 SilcBuffer buffer = packet->buffer;
1886 SilcClientEntry client;
1887 SilcClientID *client_id;
1888 SilcIDListData idata;
1889 char *username = NULL, *realname = NULL;
1890 SilcUInt16 username_len;
1893 char *hostname, *nickname;
1896 SILC_LOG_DEBUG(("Creating new client"));
1898 if (sock->type != SILC_SOCKET_TYPE_CLIENT)
1901 /* Take client entry */
1902 client = (SilcClientEntry)sock->user_data;
1903 idata = (SilcIDListData)client;
1905 /* Remove the old cache entry. */
1906 if (!silc_idcache_del_by_context(server->local_list->clients, client)) {
1907 SILC_LOG_INFO(("Unauthenticated client attempted to register to network"));
1908 silc_server_disconnect_remote(server, sock,
1909 SILC_STATUS_ERR_NOT_AUTHENTICATED, NULL);
1910 if (sock->user_data)
1911 silc_server_free_sock_user_data(server, sock, NULL);
1915 /* Parse incoming packet */
1916 ret = silc_buffer_unformat(buffer,
1917 SILC_STR_UI16_NSTRING_ALLOC(&username,
1919 SILC_STR_UI16_STRING_ALLOC(&realname),
1922 silc_free(username);
1923 silc_free(realname);
1924 SILC_LOG_ERROR(("Client %s (%s) sent incomplete information, closing "
1925 "connection", sock->hostname, sock->ip));
1926 silc_server_disconnect_remote(server, sock,
1927 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
1929 if (sock->user_data)
1930 silc_server_free_sock_user_data(server, sock, NULL);
1935 silc_free(username);
1936 silc_free(realname);
1937 SILC_LOG_ERROR(("Client %s (%s) did not send its username, closing "
1938 "connection", sock->hostname, sock->ip));
1939 silc_server_disconnect_remote(server, sock,
1940 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
1942 if (sock->user_data)
1943 silc_server_free_sock_user_data(server, sock, NULL);
1947 if (username_len > 128)
1948 username[128] = '\0';
1950 /* Check for bad characters for nickname, and modify the nickname if
1951 it includes those. */
1952 if (silc_server_name_bad_chars(username, username_len)) {
1953 nickname = silc_server_name_modify_bad(username, username_len);
1955 nickname = strdup(username);
1958 /* Make sanity checks for the hostname of the client. If the hostname
1959 is provided in the `username' check that it is the same than the
1960 resolved hostname, or if not resolved the hostname that appears in
1961 the client's public key. If the hostname is not present then put
1962 it from the resolved name or from the public key. */
1963 if (strchr(username, '@')) {
1964 SilcPublicKeyIdentifier pident;
1965 int tlen = strcspn(username, "@");
1966 char *phostname = NULL;
1968 hostname = silc_memdup(username + tlen + 1, strlen(username) - tlen - 1);
1970 if (strcmp(sock->hostname, sock->ip) &&
1971 strcmp(sock->hostname, hostname)) {
1972 silc_free(username);
1973 silc_free(hostname);
1974 silc_free(realname);
1975 SILC_LOG_ERROR(("Client %s (%s) sent incomplete information, closing "
1976 "connection", sock->hostname, sock->ip));
1977 silc_server_disconnect_remote(server, sock,
1978 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
1980 if (sock->user_data)
1981 silc_server_free_sock_user_data(server, sock, NULL);
1985 pident = silc_pkcs_decode_identifier(client->data.public_key->identifier);
1987 phostname = strdup(pident->host);
1988 silc_pkcs_free_identifier(pident);
1991 if (!strcmp(sock->hostname, sock->ip) &&
1992 phostname && strcmp(phostname, hostname)) {
1993 silc_free(username);
1994 silc_free(hostname);
1995 silc_free(phostname);
1996 silc_free(realname);
1997 SILC_LOG_ERROR(("Client %s (%s) sent incomplete information, closing "
1998 "connection", sock->hostname, sock->ip));
1999 silc_server_disconnect_remote(server, sock,
2000 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
2002 if (sock->user_data)
2003 silc_server_free_sock_user_data(server, sock, NULL);
2007 silc_free(phostname);
2009 /* The hostname is not present, add it. */
2011 /* XXX For now we cannot take the host name from the public key since
2012 they are not trusted or we cannot verify them as trusted. Just take
2013 what the resolved name or address is. */
2015 if (strcmp(sock->hostname, sock->ip)) {
2017 newusername = silc_calloc(strlen(username) +
2018 strlen(sock->hostname) + 2,
2019 sizeof(*newusername));
2020 strncat(newusername, username, strlen(username));
2021 strncat(newusername, "@", 1);
2022 strncat(newusername, sock->hostname, strlen(sock->hostname));
2023 silc_free(username);
2024 username = newusername;
2027 SilcPublicKeyIdentifier pident =
2028 silc_pkcs_decode_identifier(client->data.public_key->identifier);
2031 newusername = silc_calloc(strlen(username) +
2032 strlen(pident->host) + 2,
2033 sizeof(*newusername));
2034 strncat(newusername, username, strlen(username));
2035 strncat(newusername, "@", 1);
2036 strncat(newusername, pident->host, strlen(pident->host));
2037 silc_free(username);
2038 username = newusername;
2039 silc_pkcs_free_identifier(pident);
2045 /* Create Client ID */
2046 while (!silc_id_create_client_id(server, server->id, server->rng,
2047 server->md5hash, nickname, &client_id)) {
2050 silc_server_disconnect_remote(server, sock,
2051 SILC_STATUS_ERR_BAD_NICKNAME, NULL);
2052 if (sock->user_data)
2053 silc_server_free_sock_user_data(server, sock, NULL);
2056 snprintf(&nickname[strlen(nickname) - 1], 1, "%d", nickfail);
2059 /* Update client entry */
2060 idata->status |= SILC_IDLIST_STATUS_REGISTERED;
2061 client->nickname = nickname;
2062 client->username = username;
2063 client->userinfo = realname ? realname : strdup(" ");
2064 client->id = client_id;
2065 id_len = silc_id_get_len(client_id, SILC_ID_CLIENT);
2067 /* Add the client again to the ID cache */
2068 silc_idcache_add(server->local_list->clients, client->nickname,
2069 client_id, client, 0, NULL);
2071 /* Notify our router about new client on the SILC network */
2072 if (!server->standalone)
2073 silc_server_send_new_id(server, (SilcSocketConnection)
2074 server->router->connection,
2075 server->server_type == SILC_ROUTER ? TRUE : FALSE,
2076 client->id, SILC_ID_CLIENT, id_len);
2078 /* Send the new client ID to the client. */
2079 silc_server_send_new_id(server, sock, FALSE, client->id, SILC_ID_CLIENT,
2080 silc_id_get_len(client->id, SILC_ID_CLIENT));
2082 /* Send some nice info to the client */
2083 silc_server_send_connect_notifys(server, sock, client);
2085 /* Check if anyone is watching this nickname */
2086 if (server->server_type == SILC_ROUTER)
2087 silc_server_check_watcher_list(server, client, NULL, 0);
2092 /* Create new server. This processes received New Server packet and
2093 saves the received Server ID. The server is our locally connected
2094 server thus we save all the information and save it to local list.
2095 This funtion can be used by both normal server and router server.
2096 If normal server uses this it means that its router has connected
2097 to the server. If router uses this it means that one of the cell's
2098 servers is connected to the router. */
2100 SilcServerEntry silc_server_new_server(SilcServer server,
2101 SilcSocketConnection sock,
2102 SilcPacketContext *packet)
2104 SilcBuffer buffer = packet->buffer;
2105 SilcServerEntry new_server, server_entry;
2106 SilcServerID *server_id;
2107 SilcIDListData idata;
2108 unsigned char *server_name, *id_string;
2109 SilcUInt16 id_len, name_len;
2113 SILC_LOG_DEBUG(("Creating new server"));
2115 if (sock->type != SILC_SOCKET_TYPE_SERVER &&
2116 sock->type != SILC_SOCKET_TYPE_ROUTER)
2119 /* Take server entry */
2120 new_server = (SilcServerEntry)sock->user_data;
2121 idata = (SilcIDListData)new_server;
2123 /* Remove the old cache entry */
2124 if (!silc_idcache_del_by_context(server->local_list->servers, new_server)) {
2125 if (!silc_idcache_del_by_context(server->global_list->servers,
2127 SILC_LOG_INFO(("Unauthenticated %s attempted to register to "
2128 "network", (sock->type == SILC_SOCKET_TYPE_SERVER ?
2129 "server" : "router")));
2130 silc_server_disconnect_remote(server, sock,
2131 SILC_STATUS_ERR_NOT_AUTHENTICATED, NULL);
2132 if (sock->user_data)
2133 silc_server_free_sock_user_data(server, sock, NULL);
2139 /* Parse the incoming packet */
2140 ret = silc_buffer_unformat(buffer,
2141 SILC_STR_UI16_NSTRING_ALLOC(&id_string, &id_len),
2142 SILC_STR_UI16_NSTRING_ALLOC(&server_name,
2146 silc_free(id_string);
2147 silc_free(server_name);
2148 silc_server_disconnect_remote(server, sock,
2149 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
2151 if (sock->user_data)
2152 silc_server_free_sock_user_data(server, sock, NULL);
2156 if (id_len > buffer->len) {
2157 silc_free(id_string);
2158 silc_free(server_name);
2159 silc_server_disconnect_remote(server, sock,
2160 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
2162 if (sock->user_data)
2163 silc_server_free_sock_user_data(server, sock, NULL);
2168 server_name[255] = '\0';
2171 server_id = silc_id_str2id(id_string, id_len, SILC_ID_SERVER);
2173 silc_free(id_string);
2174 silc_free(server_name);
2175 silc_server_disconnect_remote(server, sock,
2176 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
2178 if (sock->user_data)
2179 silc_server_free_sock_user_data(server, sock, NULL);
2182 silc_free(id_string);
2184 /* Check for valid server ID */
2185 if (!silc_id_is_valid_server_id(server, server_id, sock)) {
2186 SILC_LOG_INFO(("Invalid server ID sent by %s (%s)",
2187 sock->ip, sock->hostname));
2188 silc_server_disconnect_remote(server, sock,
2189 SILC_STATUS_ERR_BAD_SERVER_ID, NULL);
2190 if (sock->user_data)
2191 silc_server_free_sock_user_data(server, sock, NULL);
2192 silc_free(server_name);
2196 /* Check that we do not have this ID already */
2197 server_entry = silc_idlist_find_server_by_id(server->local_list,
2198 server_id, TRUE, NULL);
2200 silc_idcache_del_by_context(server->local_list->servers, server_entry);
2202 server_entry = silc_idlist_find_server_by_id(server->global_list,
2203 server_id, TRUE, NULL);
2205 silc_idcache_del_by_context(server->global_list->servers, server_entry);
2208 /* Update server entry */
2209 idata->status |= SILC_IDLIST_STATUS_REGISTERED;
2210 new_server->server_name = server_name;
2211 new_server->id = server_id;
2213 SILC_LOG_DEBUG(("New server id(%s)",
2214 silc_id_render(server_id, SILC_ID_SERVER)));
2216 /* Add again the entry to the ID cache. */
2217 silc_idcache_add(local ? server->local_list->servers :
2218 server->global_list->servers, server_name, server_id,
2219 new_server, 0, NULL);
2221 /* Distribute the information about new server in the SILC network
2222 to our router. If we are normal server we won't send anything
2223 since this connection must be our router connection. */
2224 if (server->server_type == SILC_ROUTER && !server->standalone &&
2225 server->router->connection != sock)
2226 silc_server_send_new_id(server, server->router->connection,
2227 TRUE, new_server->id, SILC_ID_SERVER,
2228 silc_id_get_len(server_id, SILC_ID_SERVER));
2230 if (server->server_type == SILC_ROUTER)
2231 server->stat.cell_servers++;
2233 /* Check whether this router connection has been replaced by an
2234 backup router. If it has been then we'll disable the server and will
2235 ignore everything it will send until the backup router resuming
2236 protocol has been completed. */
2237 if (sock->type == SILC_SOCKET_TYPE_ROUTER &&
2238 silc_server_backup_replaced_get(server, server_id, NULL)) {
2239 /* Send packet to the server indicating that it cannot use this
2240 connection as it has been replaced by backup router. */
2241 SilcBuffer packet = silc_buffer_alloc(2);
2242 silc_buffer_pull_tail(packet, SILC_BUFFER_END(packet));
2243 silc_buffer_format(packet,
2244 SILC_STR_UI_CHAR(SILC_SERVER_BACKUP_REPLACED),
2245 SILC_STR_UI_CHAR(0),
2247 silc_server_packet_send(server, sock,
2248 SILC_PACKET_RESUME_ROUTER, 0,
2249 packet->data, packet->len, TRUE);
2250 silc_buffer_free(packet);
2252 /* Mark the router disabled. The data sent earlier will go but nothing
2253 after this does not go to this connection. */
2254 idata->status |= SILC_IDLIST_STATUS_DISABLED;
2256 /* If it is router announce our stuff to it. */
2257 if (sock->type == SILC_SOCKET_TYPE_ROUTER &&
2258 server->server_type == SILC_ROUTER) {
2259 silc_server_announce_servers(server, FALSE, 0, sock);
2260 silc_server_announce_clients(server, 0, sock);
2261 silc_server_announce_channels(server, 0, sock);
2268 /* Processes incoming New ID packet. New ID Payload is used to distribute
2269 information about newly registered clients and servers. */
2271 static void silc_server_new_id_real(SilcServer server,
2272 SilcSocketConnection sock,
2273 SilcPacketContext *packet,
2276 SilcBuffer buffer = packet->buffer;
2278 SilcServerEntry router, server_entry;
2279 SilcSocketConnection router_sock;
2284 SILC_LOG_DEBUG(("Processing new ID"));
2286 if (sock->type == SILC_SOCKET_TYPE_CLIENT ||
2287 server->server_type == SILC_SERVER ||
2288 packet->src_id_type != SILC_ID_SERVER)
2291 idp = silc_id_payload_parse(buffer->data, buffer->len);
2295 id_type = silc_id_payload_get_type(idp);
2297 /* Normal server cannot have other normal server connections */
2298 server_entry = (SilcServerEntry)sock->user_data;
2299 if (id_type == SILC_ID_SERVER && sock->type == SILC_SOCKET_TYPE_SERVER &&
2300 server_entry->server_type == SILC_SERVER)
2303 id = silc_id_payload_get_id(idp);
2307 /* If the packet is coming from server then use the sender as the
2308 origin of the the packet. If it came from router then check the real
2309 sender of the packet and use that as the origin. */
2310 if (sock->type == SILC_SOCKET_TYPE_SERVER) {
2311 id_list = server->local_list;
2313 router = sock->user_data;
2315 /* If the sender is backup router and ID is server (and we are not
2316 backup router) then switch the entry to global list. */
2317 if (server_entry->server_type == SILC_BACKUP_ROUTER &&
2318 id_type == SILC_ID_SERVER &&
2319 server->id_entry->server_type != SILC_BACKUP_ROUTER) {
2320 id_list = server->global_list;
2321 router_sock = server->router ? server->router->connection : sock;
2324 void *sender_id = silc_id_str2id(packet->src_id, packet->src_id_len,
2325 packet->src_id_type);
2326 router = silc_idlist_find_server_by_id(server->global_list,
2327 sender_id, TRUE, NULL);
2329 router = silc_idlist_find_server_by_id(server->local_list,
2330 sender_id, TRUE, NULL);
2331 silc_free(sender_id);
2333 id_list = server->global_list;
2340 case SILC_ID_CLIENT:
2342 SilcClientEntry entry;
2344 /* Check that we do not have this client already */
2345 entry = silc_idlist_find_client_by_id(server->global_list,
2346 id, server->server_type,
2349 entry = silc_idlist_find_client_by_id(server->local_list,
2350 id, server->server_type,
2353 SILC_LOG_DEBUG(("Ignoring client that we already have"));
2357 SILC_LOG_DEBUG(("New client id(%s) from [%s] %s",
2358 silc_id_render(id, SILC_ID_CLIENT),
2359 sock->type == SILC_SOCKET_TYPE_SERVER ?
2360 "Server" : "Router", sock->hostname));
2362 /* As a router we keep information of all global information in our
2363 global list. Cell wide information however is kept in the local
2365 entry = silc_idlist_add_client(id_list, NULL, NULL, NULL,
2366 id, router, NULL, 0);
2368 SILC_LOG_ERROR(("Could not add new client to the ID Cache"));
2370 /* Inform the sender that the ID is not usable */
2371 silc_server_send_notify_signoff(server, sock, FALSE, id, NULL);
2374 entry->nickname = NULL;
2375 entry->data.status |= SILC_IDLIST_STATUS_REGISTERED;
2377 if (sock->type == SILC_SOCKET_TYPE_SERVER)
2378 server->stat.cell_clients++;
2379 server->stat.clients++;
2381 /* Check if anyone is watching this nickname */
2382 if (server->server_type == SILC_ROUTER && id_list == server->local_list)
2383 silc_server_check_watcher_list(server, entry, NULL, 0);
2387 case SILC_ID_SERVER:
2389 SilcServerEntry entry;
2391 /* If the ID is mine, ignore it. */
2392 if (SILC_ID_SERVER_COMPARE(id, server->id)) {
2393 SILC_LOG_DEBUG(("Ignoring my own ID as new ID"));
2397 /* If the ID is the sender's ID, ignore it (we have it already) */
2398 if (SILC_ID_SERVER_COMPARE(id, router->id)) {
2399 SILC_LOG_DEBUG(("Ignoring sender's own ID"));
2403 /* Check that we do not have this server already */
2404 entry = silc_idlist_find_server_by_id(server->global_list,
2405 id, server->server_type,
2408 entry = silc_idlist_find_server_by_id(server->local_list,
2409 id, server->server_type,
2412 SILC_LOG_DEBUG(("Ignoring server that we already have"));
2416 SILC_LOG_DEBUG(("New server id(%s) from [%s] %s",
2417 silc_id_render(id, SILC_ID_SERVER),
2418 sock->type == SILC_SOCKET_TYPE_SERVER ?
2419 "Server" : "Router", sock->hostname));
2421 /* As a router we keep information of all global information in our
2422 global list. Cell wide information however is kept in the local
2424 entry = silc_idlist_add_server(id_list, NULL, 0, id, router,
2427 SILC_LOG_ERROR(("Could not add new server to the ID Cache"));
2430 entry->data.status |= SILC_IDLIST_STATUS_REGISTERED;
2432 if (sock->type == SILC_SOCKET_TYPE_SERVER)
2433 server->stat.cell_servers++;
2434 server->stat.servers++;
2438 case SILC_ID_CHANNEL:
2439 SILC_LOG_ERROR(("Channel cannot be registered with NEW_ID packet"));
2448 /* If the sender of this packet is server and we are router we need to
2449 broadcast this packet to other routers in the network. */
2450 if (broadcast && !server->standalone && server->server_type == SILC_ROUTER &&
2451 sock->type == SILC_SOCKET_TYPE_SERVER &&
2452 !(packet->flags & SILC_PACKET_FLAG_BROADCAST)) {
2453 SILC_LOG_DEBUG(("Broadcasting received New ID packet"));
2454 silc_server_packet_send(server, server->router->connection,
2456 packet->flags | SILC_PACKET_FLAG_BROADCAST,
2457 buffer->data, buffer->len, FALSE);
2458 silc_server_backup_send(server, (SilcServerEntry)sock->user_data,
2459 packet->type, packet->flags,
2460 packet->buffer->data, packet->buffer->len,
2465 silc_id_payload_free(idp);
2469 /* Processes incoming New ID packet. New ID Payload is used to distribute
2470 information about newly registered clients and servers. */
2472 void silc_server_new_id(SilcServer server, SilcSocketConnection sock,
2473 SilcPacketContext *packet)
2475 silc_server_new_id_real(server, sock, packet, TRUE);
2478 /* Receoved New Id List packet, list of New ID payloads inside one
2479 packet. Process the New ID payloads one by one. */
2481 void silc_server_new_id_list(SilcServer server, SilcSocketConnection sock,
2482 SilcPacketContext *packet)
2484 SilcPacketContext *new_id;
2488 SILC_LOG_DEBUG(("Processing New ID List"));
2490 if (sock->type == SILC_SOCKET_TYPE_CLIENT ||
2491 packet->src_id_type != SILC_ID_SERVER)
2494 /* If the sender of this packet is server and we are router we need to
2495 broadcast this packet to other routers in the network. Broadcast
2496 this list packet instead of multiple New ID packets. */
2497 if (!server->standalone && server->server_type == SILC_ROUTER &&
2498 sock->type == SILC_SOCKET_TYPE_SERVER &&
2499 !(packet->flags & SILC_PACKET_FLAG_BROADCAST)) {
2500 SILC_LOG_DEBUG(("Broadcasting received New ID List packet"));
2501 silc_server_packet_send(server, server->router->connection,
2503 packet->flags | SILC_PACKET_FLAG_BROADCAST,
2504 packet->buffer->data, packet->buffer->len, FALSE);
2505 silc_server_backup_send(server, (SilcServerEntry)sock->user_data,
2506 packet->type, packet->flags,
2507 packet->buffer->data, packet->buffer->len,
2511 /* Make copy of the original packet context, except for the actual
2512 data buffer, which we will here now fetch from the original buffer. */
2513 new_id = silc_packet_context_alloc();
2514 new_id->type = SILC_PACKET_NEW_ID;
2515 new_id->flags = packet->flags;
2516 new_id->src_id = packet->src_id;
2517 new_id->src_id_len = packet->src_id_len;
2518 new_id->src_id_type = packet->src_id_type;
2519 new_id->dst_id = packet->dst_id;
2520 new_id->dst_id_len = packet->dst_id_len;
2521 new_id->dst_id_type = packet->dst_id_type;
2523 idp = silc_buffer_alloc(256);
2524 new_id->buffer = idp;
2526 while (packet->buffer->len) {
2527 SILC_GET16_MSB(id_len, packet->buffer->data + 2);
2528 if ((id_len > packet->buffer->len) ||
2529 (id_len > idp->truelen))
2532 silc_buffer_pull_tail(idp, 4 + id_len);
2533 silc_buffer_put(idp, packet->buffer->data, 4 + id_len);
2535 /* Process the New ID */
2536 silc_server_new_id_real(server, sock, new_id, FALSE);
2538 silc_buffer_push_tail(idp, 4 + id_len);
2539 silc_buffer_pull(packet->buffer, 4 + id_len);
2542 silc_buffer_free(idp);
2546 /* Received New Channel packet. Information about new channels in the
2547 network are distributed using this packet. Save the information about
2548 the new channel. This usually comes from router but also normal server
2549 can send this to notify channels it has when it connects to us. */
2551 void silc_server_new_channel(SilcServer server,
2552 SilcSocketConnection sock,
2553 SilcPacketContext *packet)
2555 SilcChannelPayload payload;
2556 SilcChannelID *channel_id;
2558 SilcUInt32 name_len;
2562 SilcServerEntry server_entry;
2563 SilcChannelEntry channel;
2565 SILC_LOG_DEBUG(("Processing New Channel"));
2567 if (sock->type == SILC_SOCKET_TYPE_CLIENT ||
2568 packet->src_id_type != SILC_ID_SERVER ||
2569 server->server_type == SILC_SERVER)
2572 /* Parse the channel payload */
2573 payload = silc_channel_payload_parse(packet->buffer->data,
2574 packet->buffer->len);
2578 /* Get the channel ID */
2579 channel_id = silc_channel_get_id_parse(payload);
2581 silc_channel_payload_free(payload);
2585 channel_name = silc_channel_get_name(payload, &name_len);
2587 channel_name[255] = '\0';
2589 id = silc_channel_get_id(payload, &id_len);
2591 server_entry = (SilcServerEntry)sock->user_data;
2593 if (sock->type == SILC_SOCKET_TYPE_ROUTER) {
2594 /* Add the channel to global list as it is coming from router. It
2595 cannot be our own channel as it is coming from router. */
2597 /* Check that we don't already have this channel */
2598 channel = silc_idlist_find_channel_by_name(server->local_list,
2599 channel_name, NULL);
2601 channel = silc_idlist_find_channel_by_name(server->global_list,
2602 channel_name, NULL);
2604 SILC_LOG_DEBUG(("New channel id(%s) from [Router] %s",
2605 silc_id_render(channel_id, SILC_ID_CHANNEL),
2609 silc_idlist_add_channel(server->global_list, strdup(channel_name),
2610 0, channel_id, sock->user_data, NULL, NULL, 0);
2614 server->stat.channels++;
2615 if (server->server_type == SILC_ROUTER)
2616 channel->users_resolved = TRUE;
2619 /* The channel is coming from our server, thus it is in our cell
2620 we will add it to our local list. */
2623 SILC_LOG_DEBUG(("Channel id(%s) from [Server] %s",
2624 silc_id_render(channel_id, SILC_ID_CHANNEL),
2627 /* Check that we don't already have this channel */
2628 channel = silc_idlist_find_channel_by_name(server->local_list,
2629 channel_name, NULL);
2631 channel = silc_idlist_find_channel_by_name(server->global_list,
2632 channel_name, NULL);
2634 /* If the channel does not exist, then create it. This creates a new
2635 key to the channel as well that we will send to the server. */
2637 /* The protocol says that the Channel ID's IP address must be based
2638 on the router's IP address. Check whether the ID is based in our
2639 IP and if it is not then create a new ID and enforce the server
2640 to switch the ID. */
2641 if (server_entry->server_type != SILC_BACKUP_ROUTER &&
2642 !SILC_ID_COMPARE(channel_id, server->id, server->id->ip.data_len)) {
2644 SILC_LOG_DEBUG(("Forcing the server to change Channel ID"));
2646 if (silc_id_create_channel_id(server, server->id, server->rng, &tmp)) {
2647 silc_server_send_notify_channel_change(server, sock, FALSE,
2649 silc_free(channel_id);
2654 /* Create the channel with the provided Channel ID */
2655 channel = silc_server_create_new_channel_with_id(server, NULL, NULL,
2659 silc_channel_payload_free(payload);
2660 silc_free(channel_id);
2664 /* Get the mode and set it to the channel */
2665 channel->mode = silc_channel_get_mode(payload);
2667 /* Send the new channel key to the server */
2668 id = silc_id_id2str(channel->id, SILC_ID_CHANNEL);
2669 id_len = silc_id_get_len(channel->id, SILC_ID_CHANNEL);
2670 chk = silc_channel_key_payload_encode(id_len, id,
2671 strlen(channel->channel_key->
2673 channel->channel_key->cipher->name,
2674 channel->key_len / 8,
2676 silc_server_packet_send(server, sock, SILC_PACKET_CHANNEL_KEY, 0,
2677 chk->data, chk->len, FALSE);
2678 silc_buffer_free(chk);
2681 /* The channel exist by that name, check whether the ID's match.
2682 If they don't then we'll force the server to use the ID we have.
2683 We also create a new key for the channel. */
2684 SilcBuffer users = NULL, users_modes = NULL;
2686 if (!SILC_ID_CHANNEL_COMPARE(channel_id, channel->id)) {
2687 /* They don't match, send CHANNEL_CHANGE notify to the server to
2688 force the ID change. */
2689 SILC_LOG_DEBUG(("Forcing the server to change Channel ID"));
2690 silc_server_send_notify_channel_change(server, sock, FALSE,
2691 channel_id, channel->id);
2694 /* If the mode is different from what we have then enforce the
2696 mode = silc_channel_get_mode(payload);
2697 if (channel->mode != mode) {
2698 SILC_LOG_DEBUG(("Forcing the server to change channel mode"));
2699 silc_server_send_notify_cmode(server, sock, FALSE, channel,
2700 channel->mode, server->id,
2701 SILC_ID_SERVER, channel->cipher,
2703 channel->passphrase,
2704 channel->founder_key);
2707 /* Create new key for the channel and send it to the server and
2708 everybody else possibly on the channel. */
2709 if (!(channel->mode & SILC_CHANNEL_MODE_PRIVKEY)) {
2710 if (!silc_server_create_channel_key(server, channel, 0))
2713 /* Send to the channel */
2714 silc_server_send_channel_key(server, sock, channel, FALSE);
2715 id = silc_id_id2str(channel->id, SILC_ID_CHANNEL);
2716 id_len = silc_id_get_len(channel->id, SILC_ID_CHANNEL);
2718 /* Send to the server */
2719 chk = silc_channel_key_payload_encode(id_len, id,
2720 strlen(channel->channel_key->
2722 channel->channel_key->
2724 channel->key_len / 8,
2726 silc_server_packet_send(server, sock, SILC_PACKET_CHANNEL_KEY, 0,
2727 chk->data, chk->len, FALSE);
2728 silc_buffer_free(chk);
2732 silc_free(channel_id);
2734 /* Since the channel is coming from server and we also know about it
2735 then send the JOIN notify to the server so that it see's our
2736 users on the channel "joining" the channel. */
2737 silc_server_announce_get_channel_users(server, channel, &users,
2740 silc_buffer_push(users, users->data - users->head);
2741 silc_server_packet_send(server, sock,
2742 SILC_PACKET_NOTIFY, SILC_PACKET_FLAG_LIST,
2743 users->data, users->len, FALSE);
2744 silc_buffer_free(users);
2747 silc_buffer_push(users_modes, users_modes->data - users_modes->head);
2748 silc_server_packet_send_dest(server, sock,
2749 SILC_PACKET_NOTIFY, SILC_PACKET_FLAG_LIST,
2750 channel->id, SILC_ID_CHANNEL,
2752 users_modes->len, FALSE);
2753 silc_buffer_free(users_modes);
2758 silc_channel_payload_free(payload);
2761 /* Received New Channel List packet, list of New Channel List payloads inside
2762 one packet. Process the New Channel payloads one by one. */
2764 void silc_server_new_channel_list(SilcServer server,
2765 SilcSocketConnection sock,
2766 SilcPacketContext *packet)
2768 SilcPacketContext *new;
2770 SilcUInt16 len1, len2;
2772 SILC_LOG_DEBUG(("Processing New Channel List"));
2774 if (sock->type == SILC_SOCKET_TYPE_CLIENT ||
2775 packet->src_id_type != SILC_ID_SERVER ||
2776 server->server_type == SILC_SERVER)
2779 /* If the sender of this packet is server and we are router we need to
2780 broadcast this packet to other routers in the network. Broadcast
2781 this list packet instead of multiple New Channel packets. */
2782 if (!server->standalone && server->server_type == SILC_ROUTER &&
2783 sock->type == SILC_SOCKET_TYPE_SERVER &&
2784 !(packet->flags & SILC_PACKET_FLAG_BROADCAST)) {
2785 SILC_LOG_DEBUG(("Broadcasting received New Channel List packet"));
2786 silc_server_packet_send(server, server->router->connection,
2788 packet->flags | SILC_PACKET_FLAG_BROADCAST,
2789 packet->buffer->data, packet->buffer->len, FALSE);
2790 silc_server_backup_send(server, (SilcServerEntry)sock->user_data,
2791 packet->type, packet->flags,
2792 packet->buffer->data, packet->buffer->len,
2796 /* Make copy of the original packet context, except for the actual
2797 data buffer, which we will here now fetch from the original buffer. */
2798 new = silc_packet_context_alloc();
2799 new->type = SILC_PACKET_NEW_CHANNEL;
2800 new->flags = packet->flags;
2801 new->src_id = packet->src_id;
2802 new->src_id_len = packet->src_id_len;
2803 new->src_id_type = packet->src_id_type;
2804 new->dst_id = packet->dst_id;
2805 new->dst_id_len = packet->dst_id_len;
2806 new->dst_id_type = packet->dst_id_type;
2808 buffer = silc_buffer_alloc(512);
2809 new->buffer = buffer;
2811 while (packet->buffer->len) {
2812 SILC_GET16_MSB(len1, packet->buffer->data);
2813 if ((len1 > packet->buffer->len) ||
2814 (len1 > buffer->truelen))
2817 SILC_GET16_MSB(len2, packet->buffer->data + 2 + len1);
2818 if ((len2 > packet->buffer->len) ||
2819 (len2 > buffer->truelen))
2822 silc_buffer_pull_tail(buffer, 8 + len1 + len2);
2823 silc_buffer_put(buffer, packet->buffer->data, 8 + len1 + len2);
2825 /* Process the New Channel */
2826 silc_server_new_channel(server, sock, new);
2828 silc_buffer_push_tail(buffer, 8 + len1 + len2);
2829 silc_buffer_pull(packet->buffer, 8 + len1 + len2);
2832 silc_buffer_free(buffer);
2836 /* Received key agreement packet. This packet is never for us. It is to
2837 the client in the packet's destination ID. Sending of this sort of packet
2838 equals sending private message, ie. it is sent point to point from
2839 one client to another. */
2841 void silc_server_key_agreement(SilcServer server,
2842 SilcSocketConnection sock,
2843 SilcPacketContext *packet)
2845 SilcSocketConnection dst_sock;
2846 SilcIDListData idata;
2848 SILC_LOG_DEBUG(("Start"));
2850 if (packet->src_id_type != SILC_ID_CLIENT ||
2851 packet->dst_id_type != SILC_ID_CLIENT)
2854 if (!packet->dst_id)
2857 /* Get the route to the client */
2858 dst_sock = silc_server_get_client_route(server, packet->dst_id,
2859 packet->dst_id_len, NULL,
2864 /* Relay the packet */
2865 silc_server_relay_packet(server, dst_sock, idata->send_key,
2866 idata->hmac_send, idata->psn_send++,
2870 /* Received connection auth request packet that is used during connection
2871 phase to resolve the mandatory authentication method. This packet can
2872 actually be received at anytime but usually it is used only during
2873 the connection authentication phase. Now, protocol says that this packet
2874 can come from client or server, however, we support only this coming
2875 from client and expect that server always knows what authentication
2878 void silc_server_connection_auth_request(SilcServer server,
2879 SilcSocketConnection sock,
2880 SilcPacketContext *packet)
2882 SilcServerConfigClient *client = NULL;
2883 SilcUInt16 conn_type;
2885 SilcAuthMethod auth_meth = SILC_AUTH_NONE;
2887 SILC_LOG_DEBUG(("Start"));
2889 if (packet->src_id_type && packet->src_id_type != SILC_ID_CLIENT)
2892 /* Parse the payload */
2893 ret = silc_buffer_unformat(packet->buffer,
2894 SILC_STR_UI_SHORT(&conn_type),
2895 SILC_STR_UI_SHORT(NULL),
2900 if (conn_type != SILC_SOCKET_TYPE_CLIENT)
2903 /* Get the authentication method for the client */
2904 auth_meth = SILC_AUTH_NONE;
2905 client = silc_server_config_find_client(server, sock->ip);
2907 client = silc_server_config_find_client(server, sock->hostname);
2909 if (client->passphrase) {
2910 if (client->publickeys && !server->config->prefer_passphrase_auth)
2911 auth_meth = SILC_AUTH_PUBLIC_KEY;
2913 auth_meth = SILC_AUTH_PASSWORD;
2914 } else if (client->publickeys)
2915 auth_meth = SILC_AUTH_PUBLIC_KEY;
2918 /* Send it back to the client */
2919 silc_server_send_connection_auth_request(server, sock, conn_type, auth_meth);
2922 /* Received REKEY packet. The sender of the packet wants to regenerate
2923 its session keys. This starts the REKEY protocol. */
2925 void silc_server_rekey(SilcServer server,
2926 SilcSocketConnection sock,
2927 SilcPacketContext *packet)
2929 SilcProtocol protocol;
2930 SilcServerRekeyInternalContext *proto_ctx;
2931 SilcIDListData idata = (SilcIDListData)sock->user_data;
2933 SILC_LOG_DEBUG(("Start"));
2935 /* Allocate internal protocol context. This is sent as context
2937 proto_ctx = silc_calloc(1, sizeof(*proto_ctx));
2938 proto_ctx->server = (void *)server;
2939 proto_ctx->sock = sock;
2940 proto_ctx->responder = TRUE;
2941 proto_ctx->pfs = idata->rekey->pfs;
2943 /* Perform rekey protocol. Will call the final callback after the
2944 protocol is over. */
2945 silc_protocol_alloc(SILC_PROTOCOL_SERVER_REKEY,
2946 &protocol, proto_ctx, silc_server_rekey_final);
2947 sock->protocol = protocol;
2949 if (proto_ctx->pfs == FALSE)
2950 /* Run the protocol */
2951 silc_protocol_execute(protocol, server->schedule, 0, 0);
2954 /* Received file transger packet. This packet is never for us. It is to
2955 the client in the packet's destination ID. Sending of this sort of packet
2956 equals sending private message, ie. it is sent point to point from
2957 one client to another. */
2959 void silc_server_ftp(SilcServer server,
2960 SilcSocketConnection sock,
2961 SilcPacketContext *packet)
2963 SilcSocketConnection dst_sock;
2964 SilcIDListData idata;
2966 SILC_LOG_DEBUG(("Start"));
2968 if (packet->src_id_type != SILC_ID_CLIENT ||
2969 packet->dst_id_type != SILC_ID_CLIENT)
2972 if (!packet->dst_id)
2975 /* Get the route to the client */
2976 dst_sock = silc_server_get_client_route(server, packet->dst_id,
2977 packet->dst_id_len, NULL,
2982 /* Relay the packet */
2983 silc_server_relay_packet(server, dst_sock, idata->send_key,
2984 idata->hmac_send, idata->psn_send++,
2990 SilcSocketConnection sock;
2991 SilcPacketContext *packet;
2993 } *SilcServerResumeResolve;
2995 SILC_SERVER_CMD_FUNC(resume_resolve)
2997 SilcServerResumeResolve r = (SilcServerResumeResolve)context;
2998 SilcServer server = r->server;
2999 SilcSocketConnection sock = r->sock;
3000 SilcServerCommandReplyContext reply = context2;
3001 SilcClientEntry client;
3003 SILC_LOG_DEBUG(("Start"));
3005 if (!reply || !silc_command_get_status(reply->payload, NULL, NULL)) {
3006 SILC_LOG_ERROR(("Client %s (%s) tried to resume unknown client, "
3007 "closing connection", sock->hostname, sock->ip));
3008 silc_server_disconnect_remote(server, sock,
3009 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
3010 "Resuming not possible");
3014 if (reply && silc_command_get(reply->payload) == SILC_COMMAND_WHOIS) {
3015 /* Get entry to the client, and resolve it if we don't have it. */
3016 client = silc_idlist_find_client_by_id(server->local_list,
3017 r->data, TRUE, NULL);
3019 client = silc_idlist_find_client_by_id(server->global_list,
3020 r->data, TRUE, NULL);
3022 SILC_LOG_ERROR(("Client %s (%s) tried to resume unknown client, "
3023 "closing connection", sock->hostname, sock->ip));
3024 silc_server_disconnect_remote(server, sock,
3025 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
3026 "Resuming not possible");
3031 if (!(client->mode & SILC_UMODE_DETACHED)) {
3032 SILC_LOG_ERROR(("Client %s (%s) tried to resume un-detached client, "
3033 "closing connection", sock->hostname, sock->ip));
3034 silc_server_disconnect_remote(server, sock,
3035 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
3036 "Resuming not possible");
3041 /* Reprocess the packet */
3042 silc_server_resume_client(server, sock, r->packet);
3045 silc_socket_free(r->sock);
3046 silc_packet_context_free(r->packet);
3051 /* Received client resuming packet. This is used to resume detached
3052 client session. It can be sent by the client who wishes to resume
3053 but this is also sent by servers and routers to notify other routers
3054 that the client is not detached anymore. */
3056 void silc_server_resume_client(SilcServer server,
3057 SilcSocketConnection sock,
3058 SilcPacketContext *packet)
3060 SilcBuffer buffer = packet->buffer, buf;
3061 SilcIDListData idata;
3062 SilcClientEntry detached_client;
3063 SilcClientID *client_id = NULL;
3064 unsigned char *id_string, *auth = NULL;
3065 SilcUInt16 id_len, auth_len = 0;
3066 int ret, nickfail = 0;
3067 bool resolved, local, nick_change = FALSE, resolve = FALSE;
3068 SilcChannelEntry channel;
3069 SilcHashTableList htl;
3070 SilcChannelClientEntry chl;
3071 SilcServerResumeResolve r;
3073 SILC_LOG_DEBUG(("Start"));
3075 ret = silc_buffer_unformat(buffer,
3076 SILC_STR_UI16_NSTRING(&id_string, &id_len),
3079 client_id = silc_id_str2id(id_string, id_len, SILC_ID_CLIENT);
3081 if (sock->type == SILC_SOCKET_TYPE_CLIENT) {
3082 /* Client send this and is attempting to resume to old client session */
3083 SilcClientEntry client;
3087 silc_buffer_pull(buffer, 2 + id_len);
3088 auth = buffer->data;
3089 auth_len = buffer->len;
3090 silc_buffer_push(buffer, 2 + id_len);
3093 if (!client_id || auth_len < 128) {
3094 SILC_LOG_ERROR(("Client %s (%s) sent incomplete resume information, "
3095 "closing connection", sock->hostname, sock->ip));
3096 silc_server_disconnect_remote(server, sock,
3097 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
3098 "Resuming not possible");
3102 /* Take client entry of this connection */
3103 client = (SilcClientEntry)sock->user_data;
3104 idata = (SilcIDListData)client;
3106 /* Get entry to the client, and resolve it if we don't have it. */
3107 detached_client = silc_server_get_client_resolve(server, client_id, FALSE,
3109 if (!detached_client) {
3111 /* The client info is being resolved. Reprocess this packet after
3112 receiving the reply to the query. */
3113 SILC_LOG_DEBUG(("Resolving client"));
3114 r = silc_calloc(1, sizeof(*r));
3118 r->sock = silc_socket_dup(sock);
3119 r->packet = silc_packet_context_dup(packet);
3120 r->data = silc_id_dup(client_id, SILC_ID_CLIENT);
3121 silc_server_command_pending(server, SILC_COMMAND_WHOIS,
3123 silc_server_command_resume_resolve, r);
3125 SILC_LOG_ERROR(("Client %s (%s) tried to resume unknown client, "
3126 "closing connection", sock->hostname, sock->ip));
3127 silc_server_disconnect_remote(server, sock,
3128 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
3129 "Resuming not possible");
3134 if (!(detached_client->mode & SILC_UMODE_DETACHED))
3136 if (!silc_hash_table_count(detached_client->channels) &&
3137 detached_client->router)
3139 if (!detached_client->nickname)
3143 if (server->server_type == SILC_SERVER && !server->standalone) {
3144 /* The client info is being resolved. Reprocess this packet after
3145 receiving the reply to the query. */
3146 SILC_LOG_DEBUG(("Resolving client info"));
3147 silc_server_get_client_resolve(server, client_id, TRUE, NULL);
3148 r = silc_calloc(1, sizeof(*r));
3152 r->sock = silc_socket_dup(sock);
3153 r->packet = silc_packet_context_dup(packet);
3154 r->data = silc_id_dup(client_id, SILC_ID_CLIENT);
3155 silc_server_command_pending(server, SILC_COMMAND_WHOIS,
3157 silc_server_command_resume_resolve, r);
3160 if (server->server_type == SILC_SERVER) {
3161 SILC_LOG_ERROR(("Client %s (%s) tried to resume un-detached client, "
3162 "closing connection", sock->hostname, sock->ip));
3163 silc_server_disconnect_remote(server, sock,
3164 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
3165 "Resuming not possible");
3170 /* Check that we have the public key of the client, if not then we must
3171 resolve it first. */
3172 if (!detached_client->data.public_key) {
3173 if (server->server_type == SILC_SERVER && server->standalone) {
3174 silc_server_disconnect_remote(server, sock,
3175 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
3176 "Resuming not possible");
3178 /* We must retrieve the detached client's public key by sending
3179 GETKEY command. Reprocess this packet after receiving the key */
3180 SilcBuffer idp = silc_id_payload_encode(client_id, SILC_ID_CLIENT);
3181 SilcSocketConnection dest_sock =
3182 silc_server_get_client_route(server, NULL, 0, client_id, NULL, NULL);
3184 SILC_LOG_DEBUG(("Resolving client public key"));
3186 silc_server_send_command(server, dest_sock ? dest_sock :
3187 server->router->connection,
3188 SILC_COMMAND_GETKEY, ++server->cmd_ident,
3189 1, 1, idp->data, idp->len);
3191 r = silc_calloc(1, sizeof(*r));
3196 r->sock = silc_socket_dup(sock);
3197 r->packet = silc_packet_context_dup(packet);
3198 silc_server_command_pending(server, SILC_COMMAND_GETKEY,
3200 silc_server_command_resume_resolve, r);
3202 silc_buffer_free(idp);
3205 } else if (!silc_pkcs_public_key_compare(detached_client->data.public_key,
3206 idata->public_key)) {
3207 /* We require that the connection and resuming authentication data
3208 must be using same key pair. */
3209 silc_server_disconnect_remote(server, sock,
3210 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
3211 "Resuming not possible");
3215 /* Verify the authentication payload. This has to be successful in
3216 order to allow the resuming */
3218 !silc_auth_verify_data(auth, auth_len, SILC_AUTH_PUBLIC_KEY,
3219 detached_client->data.public_key, 0,
3220 idata->hash, detached_client->id,
3222 SILC_LOG_ERROR(("Client %s (%s) resume authentication failed, "
3223 "closing connection", sock->hostname, sock->ip));
3224 silc_server_disconnect_remote(server, sock,
3225 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
3226 "Resuming not possible");
3230 /* Now resume the client to the network */
3232 silc_schedule_task_del_by_context(server->schedule, detached_client);
3233 sock->user_data = detached_client;
3234 detached_client->connection = sock;
3236 /* Take new keys and stuff into use in the old entry */
3237 silc_idlist_del_data(detached_client);
3238 silc_idlist_add_data(detached_client, idata);
3239 detached_client->data.status |= SILC_IDLIST_STATUS_REGISTERED;
3240 detached_client->data.status |= SILC_IDLIST_STATUS_RESUMED;
3241 detached_client->mode &= ~SILC_UMODE_DETACHED;
3243 /* Send the RESUME_CLIENT packet to our primary router so that others
3244 know this client isn't detached anymore. */
3245 buf = silc_buffer_alloc_size(2 + id_len);
3246 silc_buffer_format(buf,
3247 SILC_STR_UI_SHORT(id_len),
3248 SILC_STR_UI_XNSTRING(id_string, id_len),
3251 /* Send to primary router */
3252 if (!server->standalone)
3253 silc_server_packet_send(server, server->router->connection,
3254 SILC_PACKET_RESUME_CLIENT, 0,
3255 buf->data, buf->len, TRUE);
3257 /* As router we must deliver this packet directly to the original
3258 server whom this client was earlier. */
3259 if (server->server_type == SILC_ROUTER && detached_client->router &&
3260 detached_client->router->server_type != SILC_ROUTER)
3261 silc_server_packet_send(server, detached_client->router->connection,
3262 SILC_PACKET_RESUME_CLIENT, 0,
3263 buf->data, buf->len, TRUE);
3264 silc_buffer_free(buf);
3266 detached_client->router = NULL;
3268 /* Delete this client entry since we're resuming to old one. */
3269 server->stat.my_clients--;
3270 server->stat.clients--;
3271 if (server->stat.cell_clients)
3272 server->stat.cell_clients--;
3273 silc_idlist_del_client(server->local_list, client);
3274 client = detached_client;
3276 /* If the ID is not based in our ID then change it */
3277 if (!SILC_ID_COMPARE(client->id, server->id, server->id->ip.data_len)) {
3278 while (!silc_id_create_client_id(server, server->id, server->rng,
3279 server->md5hash, client->nickname,
3283 silc_server_disconnect_remote(server, sock,
3284 SILC_STATUS_ERR_BAD_NICKNAME, NULL);
3287 snprintf(&client->nickname[strlen(client->nickname) - 1], 1,
3294 /* Notify about Client ID change, nickname doesn't actually change. */
3295 if (!server->standalone)
3296 silc_server_send_notify_nick_change(server, server->router->connection,
3297 FALSE, client->id, client_id,
3301 /* Resolve users on those channels that client has joined but we
3302 haven't resolved user list yet. */
3303 if (server->server_type == SILC_SERVER && !server->standalone) {
3304 silc_hash_table_list(client->channels, &htl);
3305 while (silc_hash_table_get(&htl, NULL, (void **)&chl)) {
3306 channel = chl->channel;
3307 SILC_LOG_DEBUG(("Resolving users for %s channel",
3308 channel->channel_name));
3309 if (channel->disabled || !channel->users_resolved) {
3310 silc_server_send_command(server, server->router->connection,
3311 SILC_COMMAND_USERS, ++server->cmd_ident,
3312 1, 2, channel->channel_name,
3313 strlen(channel->channel_name));
3316 silc_hash_table_list_reset(&htl);
3319 /* Send the new client ID to the client. After this client may start
3320 receiving other packets, and may start sending packets too. */
3321 silc_server_send_new_id(server, sock, FALSE, client_id, SILC_ID_CLIENT,
3322 silc_id_get_len(client_id, SILC_ID_CLIENT));
3325 /* Send NICK change notify to channels as well. */
3326 SilcBuffer oidp, nidp;
3327 oidp = silc_id_payload_encode(client->id, SILC_ID_CLIENT);
3328 nidp = silc_id_payload_encode(client_id, SILC_ID_CLIENT);
3329 silc_server_send_notify_on_channels(server, NULL, client,
3330 SILC_NOTIFY_TYPE_NICK_CHANGE, 3,
3331 oidp->data, oidp->len,
3332 nidp->data, nidp->len,
3334 strlen(client->nickname));
3335 silc_buffer_free(oidp);
3336 silc_buffer_free(nidp);
3339 /* Add the client again to the ID cache to get it to correct list */
3340 if (!silc_idcache_del_by_context(server->local_list->clients, client))
3341 silc_idcache_del_by_context(server->global_list->clients, client);
3342 silc_free(client->id);
3343 client->id = client_id;
3345 silc_idcache_add(server->local_list->clients, client->nickname,
3346 client->id, client, 0, NULL);
3348 /* Send some nice info to the client */
3349 silc_server_send_connect_notifys(server, sock, client);
3351 /* Send all channel keys of channels the client has joined */
3352 silc_hash_table_list(client->channels, &htl);
3353 while (silc_hash_table_get(&htl, NULL, (void **)&chl)) {
3354 bool created = FALSE;
3355 channel = chl->channel;
3357 if (channel->mode & SILC_CHANNEL_MODE_PRIVKEY)
3360 /* If we don't have channel key, then create one */
3361 if (!channel->channel_key) {
3362 if (!silc_server_create_channel_key(server, channel, 0))
3367 id_string = silc_id_id2str(channel->id, SILC_ID_CHANNEL);
3369 silc_channel_key_payload_encode(silc_id_get_len(channel->id,
3372 strlen(channel->channel_key->
3374 channel->channel_key->cipher->name,
3375 channel->key_len / 8, channel->key);
3376 silc_free(id_string);
3378 /* Send the key packet to client */
3379 silc_server_packet_send(server, sock, SILC_PACKET_CHANNEL_KEY, 0,
3380 keyp->data, keyp->len, FALSE);
3382 if (created && server->server_type == SILC_SERVER &&
3383 !server->standalone)
3384 silc_server_packet_send(server, server->router->connection,
3385 SILC_PACKET_CHANNEL_KEY, 0,
3386 keyp->data, keyp->len, FALSE);
3388 silc_buffer_free(keyp);
3390 silc_hash_table_list_reset(&htl);
3392 } else if (sock->type != SILC_SOCKET_TYPE_CLIENT) {
3393 /* Server or router sent this to us to notify that that a client has
3395 SilcServerEntry server_entry;
3396 SilcServerID *server_id;
3401 /* Get entry to the client, and resolve it if we don't have it. */
3402 detached_client = silc_idlist_find_client_by_id(server->local_list,
3403 client_id, TRUE, NULL);
3404 if (!detached_client) {
3405 detached_client = silc_idlist_find_client_by_id(server->global_list,
3406 client_id, TRUE, NULL);
3407 if (!detached_client)
3411 /* Check that the client has not been resumed already because it is
3412 protocol error to attempt to resume more than once. The client
3413 will be killed if this protocol error occurs. */
3414 if (detached_client->data.status & SILC_IDLIST_STATUS_RESUMED &&
3415 !(detached_client->mode & SILC_UMODE_DETACHED)) {
3416 /* The client is clearly attempting to resume more than once and
3417 perhaps playing around by resuming from several different places
3418 at the same time. */
3419 silc_server_kill_client(server, detached_client, NULL,
3420 server->id, SILC_ID_SERVER);
3424 /* Check whether client is detached at all */
3425 if (!(detached_client->mode & SILC_UMODE_DETACHED))
3428 /* Client is detached, and now it is resumed. Remove the detached
3429 mode and mark that it is resumed. */
3430 detached_client->mode &= ~SILC_UMODE_DETACHED;
3431 detached_client->data.status |= SILC_IDLIST_STATUS_RESUMED;
3433 /* Get the new owner of the resumed client */
3434 server_id = silc_id_str2id(packet->src_id, packet->src_id_len,
3435 packet->src_id_type);
3439 /* Get server entry */
3440 server_entry = silc_idlist_find_server_by_id(server->global_list,
3441 server_id, TRUE, NULL);
3443 if (!server_entry) {
3444 server_entry = silc_idlist_find_server_by_id(server->local_list,
3445 server_id, TRUE, NULL);
3447 if (!server_entry) {
3448 silc_free(server_id);
3453 if (server->server_type == SILC_ROUTER &&
3454 sock->type == SILC_SOCKET_TYPE_ROUTER &&
3455 server_entry->server_type == SILC_ROUTER)
3458 SILC_LOG_DEBUG(("Resuming detached client"));
3460 /* Change the client to correct list. */
3461 if (!silc_idcache_del_by_context(server->local_list->clients,
3463 silc_idcache_del_by_context(server->global_list->clients,
3465 silc_idcache_add(local && server->server_type == SILC_ROUTER ?
3466 server->local_list->clients :
3467 server->global_list->clients,
3468 detached_client->nickname,
3469 detached_client->id, detached_client, FALSE, NULL);
3471 /* Change the owner of the client if needed */
3472 if (detached_client->router != server_entry)
3473 detached_client->router = server_entry;
3475 /* Update channel information regarding global clients on channel. */
3476 if (server->server_type == SILC_SERVER) {
3477 silc_hash_table_list(detached_client->channels, &htl);
3478 while (silc_hash_table_get(&htl, NULL, (void **)&chl))
3479 chl->channel->global_users =
3480 silc_server_channel_has_global(chl->channel);
3481 silc_hash_table_list_reset(&htl);
3484 silc_schedule_task_del_by_context(server->schedule, detached_client);
3486 /* If the sender of this packet is server and we are router we need to
3487 broadcast this packet to other routers in the network. */
3488 if (!server->standalone && server->server_type == SILC_ROUTER &&
3489 sock->type == SILC_SOCKET_TYPE_SERVER &&
3490 !(packet->flags & SILC_PACKET_FLAG_BROADCAST)) {
3491 SILC_LOG_DEBUG(("Broadcasting received Resume Client packet"));
3492 silc_server_packet_send(server, server->router->connection,
3494 packet->flags | SILC_PACKET_FLAG_BROADCAST,
3495 buffer->data, buffer->len, FALSE);
3496 silc_server_backup_send(server, (SilcServerEntry)sock->user_data,
3497 packet->type, packet->flags,
3498 packet->buffer->data, packet->buffer->len,
3502 silc_free(server_id);
3505 silc_free(client_id);