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 if (sock->type == SILC_SOCKET_TYPE_CLIENT ||
54 packet->src_id_type != SILC_ID_SERVER || !packet->dst_id) {
55 SILC_LOG_DEBUG(("Bad notify packet received"));
59 /* If the packet is destined directly to a client then relay the packet
60 before processing it. */
61 if (packet->dst_id_type == SILC_ID_CLIENT) {
63 SilcSocketConnection dst_sock;
65 /* Get the route to the client */
66 dst_sock = silc_server_get_client_route(server, packet->dst_id,
67 packet->dst_id_len, NULL,
70 /* Relay the packet */
71 silc_server_relay_packet(server, dst_sock, idata->send_key,
72 idata->hmac_send, idata->psn_send++,
76 /* Parse the Notify Payload */
77 payload = silc_notify_payload_parse(packet->buffer->data,
82 /* If we are router and this packet is not already broadcast packet
83 we will broadcast it. The sending socket really cannot be router or
84 the router is buggy. If this packet is coming from router then it must
85 have the broadcast flag set already and we won't do anything. */
86 if (server->server_type == SILC_ROUTER &&
87 sock->type == SILC_SOCKET_TYPE_SERVER &&
88 !(packet->flags & SILC_PACKET_FLAG_BROADCAST)) {
89 SILC_LOG_DEBUG(("Broadcasting received Notify packet"));
90 if (packet->dst_id_type == SILC_ID_CHANNEL) {
91 /* Packet is destined to channel */
92 channel_id = silc_id_str2id(packet->dst_id, packet->dst_id_len,
97 silc_server_packet_send_dest(server, SILC_PRIMARY_ROUTE(server),
98 packet->type, packet->flags |
99 SILC_PACKET_FLAG_BROADCAST,
100 channel_id, SILC_ID_CHANNEL,
101 packet->buffer->data,
102 packet->buffer->len, FALSE);
103 silc_server_backup_send_dest(server, (SilcServerEntry)sock->user_data,
104 packet->type, packet->flags,
105 channel_id, SILC_ID_CHANNEL,
106 packet->buffer->data, packet->buffer->len,
109 /* Packet is destined to client or server */
110 silc_server_packet_send(server, SILC_PRIMARY_ROUTE(server),
112 packet->flags | SILC_PACKET_FLAG_BROADCAST,
113 packet->buffer->data, packet->buffer->len,
115 silc_server_backup_send(server, (SilcServerEntry)sock->user_data,
116 packet->type, packet->flags,
117 packet->buffer->data, packet->buffer->len,
122 type = silc_notify_get_type(payload);
123 args = silc_notify_get_args(payload);
128 case SILC_NOTIFY_TYPE_JOIN:
130 * Distribute the notify to local clients on the channel
132 SILC_LOG_DEBUG(("JOIN notify"));
135 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
138 channel_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
142 /* Get channel entry */
143 channel = silc_idlist_find_channel_by_id(server->global_list,
146 channel = silc_idlist_find_channel_by_id(server->local_list,
149 SILC_LOG_DEBUG(("Notify for unknown channel"));
150 silc_free(channel_id);
154 silc_free(channel_id);
157 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
160 client_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
164 /* If the the client is not in local list we check global list (ie. the
165 channel will be global channel) and if it does not exist then create
166 entry for the client. */
167 client = silc_idlist_find_client_by_id(server->global_list,
168 client_id, server->server_type,
171 client = silc_idlist_find_client_by_id(server->local_list,
172 client_id, server->server_type,
175 /* If router did not find the client the it is bogus */
176 if (server->server_type != SILC_SERVER)
180 silc_idlist_add_client(server->global_list, NULL, NULL, NULL,
181 silc_id_dup(client_id, SILC_ID_CLIENT),
182 sock->user_data, NULL, 0);
184 SILC_LOG_ERROR(("Could not add new client to the ID Cache"));
185 silc_free(client_id);
189 client->data.status |= SILC_IDLIST_STATUS_REGISTERED;
193 /* Do not process the notify if the client is not registered */
194 if (!(client->data.status & SILC_IDLIST_STATUS_REGISTERED))
197 /* Do not add client to channel if it is there already */
198 if (silc_server_client_on_channel(client, channel, NULL)) {
199 SILC_LOG_DEBUG(("Client already on channel"));
203 /* Send to channel */
204 silc_server_packet_send_to_channel(server, sock, channel, packet->type,
205 FALSE, packet->buffer->data,
206 packet->buffer->len, FALSE);
208 if (server->server_type != SILC_ROUTER &&
209 sock->type == SILC_SOCKET_TYPE_ROUTER)
210 /* The channel is global now */
211 channel->global_users = TRUE;
213 SILC_LOG_DEBUG(("Joining to channel %s", channel->channel_name));
215 /* JOIN the global client to the channel (local clients (if router
216 created the channel) is joined in the pending JOIN command). */
217 chl = silc_calloc(1, sizeof(*chl));
218 chl->client = client;
219 chl->channel = channel;
221 if (server->server_type != SILC_ROUTER ||
222 sock->type == SILC_SOCKET_TYPE_ROUTER) {
223 /* If this is the first one on the channel then it is the founder of
224 the channel. This is done on normal server and on router if this
225 notify is coming from router */
226 if (!silc_hash_table_count(channel->user_list)) {
227 SILC_LOG_DEBUG(("Client %s is founder on channel",
228 silc_id_render(chl->client->id, SILC_ID_CLIENT)));
229 chl->mode = (SILC_CHANNEL_UMODE_CHANOP | SILC_CHANNEL_UMODE_CHANFO);
233 silc_hash_table_add(channel->user_list, client, chl);
234 silc_hash_table_add(client->channels, channel, chl);
235 silc_free(client_id);
236 channel->user_count++;
237 channel->disabled = FALSE;
239 /* Update statistics */
240 if (server->server_type == SILC_ROUTER) {
241 if (sock->type != SILC_SOCKET_TYPE_ROUTER)
242 server->stat.cell_chanclients++;
243 server->stat.chanclients++;
248 case SILC_NOTIFY_TYPE_LEAVE:
250 * Distribute the notify to local clients on the channel
252 SILC_LOG_DEBUG(("LEAVE notify"));
255 channel_id = silc_id_str2id(packet->dst_id, packet->dst_id_len,
256 packet->dst_id_type);
261 /* Get channel entry */
262 channel = silc_idlist_find_channel_by_id(server->global_list,
265 channel = silc_idlist_find_channel_by_id(server->local_list,
268 SILC_LOG_DEBUG(("Notify for unknown channel"));
269 silc_free(channel_id);
275 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
277 silc_free(channel_id);
280 client_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
282 silc_free(channel_id);
286 /* Get client entry */
287 client = silc_idlist_find_client_by_id(server->global_list,
288 client_id, TRUE, NULL);
290 client = silc_idlist_find_client_by_id(server->local_list,
291 client_id, TRUE, NULL);
293 silc_free(client_id);
294 silc_free(channel_id);
298 silc_free(client_id);
300 /* Check if on channel */
301 if (!silc_server_client_on_channel(client, channel, NULL))
304 /* Send the leave notify to channel */
305 silc_server_packet_send_to_channel(server, sock, channel, packet->type,
306 FALSE, packet->buffer->data,
307 packet->buffer->len, FALSE);
309 /* Remove the user from channel */
310 silc_server_remove_from_one_channel(server, sock, channel, client, FALSE);
313 case SILC_NOTIFY_TYPE_SIGNOFF:
315 * Distribute the notify to local clients on the channel
317 SILC_LOG_DEBUG(("SIGNOFF notify"));
320 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
323 client_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
327 /* Get client entry */
328 client = silc_idlist_find_client_by_id(server->global_list,
329 client_id, TRUE, &cache);
331 client = silc_idlist_find_client_by_id(server->local_list,
332 client_id, TRUE, &cache);
334 silc_free(client_id);
338 silc_free(client_id);
340 /* Get signoff message */
341 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
345 /* Update statistics */
346 server->stat.clients--;
347 if (server->stat.cell_clients)
348 server->stat.cell_clients--;
349 SILC_OPER_STATS_UPDATE(client, server, SILC_UMODE_SERVER_OPERATOR);
350 SILC_OPER_STATS_UPDATE(client, router, SILC_UMODE_ROUTER_OPERATOR);
351 silc_schedule_task_del_by_context(server->schedule, client);
353 /* Remove the client from all channels. */
354 silc_server_remove_from_channels(server, NULL, client, TRUE, tmp, FALSE);
356 /* Check if anyone is watching this nickname */
357 if (server->server_type == SILC_ROUTER)
358 silc_server_check_watcher_list(server, client, NULL,
359 SILC_NOTIFY_TYPE_SIGNOFF);
361 /* Remove this client from watcher list if it is */
362 silc_server_del_from_watcher_list(server, client);
364 client->data.status &= ~SILC_IDLIST_STATUS_REGISTERED;
365 cache->expire = SILC_ID_CACHE_EXPIRE_DEF;
368 case SILC_NOTIFY_TYPE_TOPIC_SET:
370 * Distribute the notify to local clients on the channel
373 SILC_LOG_DEBUG(("TOPIC SET notify"));
376 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
379 client_id = silc_id_payload_parse_id(tmp, tmp_len, &id_type);
383 /* Get client entry */
384 if (id_type == SILC_ID_CLIENT) {
385 client = silc_idlist_find_client_by_id(server->global_list,
386 client_id, TRUE, &cache);
388 client = silc_idlist_find_client_by_id(server->local_list,
389 client_id, TRUE, &cache);
391 silc_free(client_id);
395 silc_free(client_id);
399 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
401 silc_free(channel_id);
406 channel_id = silc_id_str2id(packet->dst_id, packet->dst_id_len,
407 packet->dst_id_type);
412 /* Get channel entry */
413 channel = silc_idlist_find_channel_by_id(server->global_list,
416 channel = silc_idlist_find_channel_by_id(server->local_list,
419 SILC_LOG_DEBUG(("Notify for unknown channel"));
420 silc_free(channel_id);
425 if (channel->topic && !strcmp(channel->topic, tmp)) {
426 SILC_LOG_DEBUG(("Topic is already set and same"));
431 /* Get user's channel entry and check that topic set is allowed. */
432 if (!silc_server_client_on_channel(client, channel, &chl))
434 if (channel->mode & SILC_CHANNEL_MODE_TOPIC &&
435 !(chl->mode & SILC_CHANNEL_UMODE_CHANOP) &&
436 !(chl->mode & SILC_CHANNEL_UMODE_CHANFO)) {
437 SILC_LOG_DEBUG(("Topic change is not allowed"));
442 /* Change the topic */
443 silc_free(channel->topic);
444 channel->topic = strdup(tmp);
446 /* Send the same notify to the channel */
447 silc_server_packet_send_to_channel(server, NULL, channel, packet->type,
448 FALSE, packet->buffer->data,
449 packet->buffer->len, FALSE);
450 silc_free(channel_id);
453 case SILC_NOTIFY_TYPE_NICK_CHANGE:
456 * Distribute the notify to local clients on the channel
458 unsigned char *id, *id2;
460 SilcUInt32 nickname_len;
462 SILC_LOG_DEBUG(("NICK CHANGE notify"));
464 /* Get old client ID */
465 id = silc_argument_get_arg_type(args, 1, &tmp_len);
468 client_id = silc_id_payload_parse_id(id, tmp_len, NULL);
472 /* Get new client ID */
473 id2 = silc_argument_get_arg_type(args, 2, &tmp_len);
476 client_id2 = silc_id_payload_parse_id(id2, tmp_len, NULL);
480 SILC_LOG_DEBUG(("Old Client ID id(%s)",
481 silc_id_render(client_id, SILC_ID_CLIENT)));
482 SILC_LOG_DEBUG(("New Client ID id(%s)",
483 silc_id_render(client_id2, SILC_ID_CLIENT)));
485 /* From protocol version 1.1 we also get the new nickname */
486 nickname = silc_argument_get_arg_type(args, 3, &nickname_len);;
488 /* Replace the Client ID */
489 client = silc_idlist_replace_client_id(server,
490 server->global_list, client_id,
491 client_id2, nickname);
493 client = silc_idlist_replace_client_id(server,
494 server->local_list, client_id,
495 client_id2, nickname);
498 /* Send the NICK_CHANGE notify type to local clients on the channels
499 this client is joined to. */
500 silc_server_send_notify_on_channels(server, client, client,
501 SILC_NOTIFY_TYPE_NICK_CHANGE, 3,
502 id, tmp_len, id2, tmp_len,
507 silc_free(client_id);
509 silc_free(client_id2);
513 case SILC_NOTIFY_TYPE_CMODE_CHANGE:
515 * Distribute the notify to local clients on the channel
518 SILC_LOG_DEBUG(("CMODE CHANGE notify"));
521 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
524 client_id = silc_id_payload_parse_id(tmp, tmp_len, &id_type);
528 /* Get client entry */
529 if (id_type == SILC_ID_CLIENT) {
530 client = silc_idlist_find_client_by_id(server->global_list,
531 client_id, TRUE, &cache);
533 client = silc_idlist_find_client_by_id(server->local_list,
534 client_id, TRUE, &cache);
536 silc_free(client_id);
540 silc_free(client_id);
544 channel_id = silc_id_str2id(packet->dst_id, packet->dst_id_len,
545 packet->dst_id_type);
550 /* Get channel entry */
551 channel = silc_idlist_find_channel_by_id(server->global_list,
554 channel = silc_idlist_find_channel_by_id(server->local_list,
557 SILC_LOG_DEBUG(("Notify for unknown channel"));
558 silc_free(channel_id);
562 silc_free(channel_id);
565 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
568 SILC_GET32_MSB(mode, tmp);
570 /* Check if mode changed */
571 if (channel->mode == mode) {
572 SILC_LOG_DEBUG(("Mode is changed already"));
574 /* If this mode change has founder mode then we'll enforce the
575 change so that the server gets the real founder public key */
576 if (server->server_type != SILC_SERVER &&
577 sock != SILC_PRIMARY_ROUTE(server) &&
578 mode & SILC_CHANNEL_MODE_FOUNDER_AUTH && channel->founder_key) {
579 SILC_LOG_DEBUG(("Sending founder public key to server"));
580 silc_server_send_notify_cmode(server, sock, FALSE, channel,
581 channel->mode, server->id,
582 SILC_ID_SERVER, channel->cipher,
585 channel->founder_key);
588 /* If we received same mode from our primary check whether founder
589 mode and key in the notify is set. We update the founder key
590 here since we may have wrong one */
591 if (server->server_type == SILC_SERVER &&
592 sock == SILC_PRIMARY_ROUTE(server) &&
593 mode & SILC_CHANNEL_MODE_FOUNDER_AUTH) {
594 SILC_LOG_DEBUG(("Founder public key received from primary router"));
595 tmp = silc_argument_get_arg_type(args, 6, &tmp_len);
599 if (channel->founder_key)
600 silc_pkcs_public_key_free(channel->founder_key);
601 channel->founder_key = NULL;
602 silc_pkcs_public_key_decode(tmp, tmp_len, &channel->founder_key);
608 /* Get user's channel entry and check that mode change is allowed */
610 if (!silc_server_client_on_channel(client, channel, &chl))
612 if (!silc_server_check_cmode_rights(server, channel, chl, mode)) {
613 SILC_LOG_DEBUG(("CMODE change is not allowed"));
614 silc_server_send_notify_cmode(server, sock, FALSE, channel,
615 channel->mode, server->id,
616 SILC_ID_SERVER, channel->cipher,
619 channel->founder_key);
623 /* Assure that server is not removing founder mode from us */
624 if (server->server_type == SILC_ROUTER &&
625 sock != SILC_PRIMARY_ROUTE(server) &&
626 channel->mode & SILC_CHANNEL_MODE_FOUNDER_AUTH &&
627 !(mode & SILC_CHANNEL_MODE_FOUNDER_AUTH)) {
628 SILC_LOG_DEBUG(("Enforcing sender to change channel mode"));
629 silc_server_send_notify_cmode(server, sock, FALSE, channel,
630 channel->mode, server->id,
631 SILC_ID_SERVER, channel->cipher,
634 channel->founder_key);
638 /* If server is adding founder mode, check whether there is founder
639 on channel already and is not from this server */
640 if (server->server_type == SILC_ROUTER &&
641 sock != SILC_PRIMARY_ROUTE(server) &&
642 mode & SILC_CHANNEL_MODE_FOUNDER_AUTH) {
643 silc_hash_table_list(channel->user_list, &htl);
644 while (silc_hash_table_get(&htl, NULL, (void *)&chl))
645 if (chl->mode & SILC_CHANNEL_UMODE_CHANFO &&
646 chl->client->router != sock->user_data) {
647 SILC_LOG_DEBUG(("Enforcing sender to change channel mode"));
648 silc_server_send_notify_cmode(server, sock, FALSE, channel,
649 channel->mode, server->id,
650 SILC_ID_SERVER, channel->cipher,
653 channel->founder_key);
654 silc_hash_table_list_reset(&htl);
657 silc_hash_table_list_reset(&htl);
661 /* If the channel had private keys set and the mode was removed then
662 we must re-generate and re-distribute a new channel key */
663 if (channel->mode & SILC_CHANNEL_MODE_PRIVKEY &&
664 !(mode & SILC_CHANNEL_MODE_PRIVKEY)) {
665 /* Re-generate channel key */
666 if (!silc_server_create_channel_key(server, channel, 0))
669 /* Send the channel key. This sends it to our local clients and if
670 we are normal server to our router as well. */
671 silc_server_send_channel_key(server, NULL, channel,
672 server->server_type == SILC_ROUTER ?
673 FALSE : !server->standalone);
677 tmp = silc_argument_get_arg_type(args, 4, &tmp_len);
679 unsigned char hash[32];
682 silc_hmac_free(channel->hmac);
683 if (!silc_hmac_alloc(tmp, NULL, &channel->hmac))
686 /* Set the HMAC key out of current channel key. The client must do
688 silc_hash_make(silc_hmac_get_hash(channel->hmac), channel->key,
689 channel->key_len / 8, hash);
690 silc_hmac_set_key(channel->hmac, hash,
691 silc_hash_len(silc_hmac_get_hash(channel->hmac)));
692 memset(hash, 0, sizeof(hash));
695 /* Get the passphrase */
696 tmp = silc_argument_get_arg_type(args, 5, &tmp_len);
698 silc_free(channel->passphrase);
699 channel->passphrase = silc_memdup(tmp, tmp_len);
702 /* Get founder public key */
703 tmp = silc_argument_get_arg_type(args, 6, &tmp_len);
704 if (tmp && mode & SILC_CHANNEL_MODE_FOUNDER_AUTH) {
705 if (channel->founder_key)
706 silc_pkcs_public_key_free(channel->founder_key);
707 channel->founder_key = NULL;
708 silc_pkcs_public_key_decode(tmp, tmp_len, &channel->founder_key);
710 if (!channel->founder_key ||
711 (client && client->data.public_key &&
712 server->server_type == SILC_ROUTER &&
713 !silc_pkcs_public_key_compare(channel->founder_key,
714 client->data.public_key))) {
715 /* A really buggy server isn't checking public keys correctly.
716 It's not possible that the mode setter and founder wouldn't
717 have same public key. */
718 SILC_LOG_DEBUG(("Enforcing sender to change channel mode"));
720 mode &= ~SILC_CHANNEL_MODE_FOUNDER_AUTH;
721 silc_server_send_notify_cmode(server, sock, FALSE, channel,
722 mode, server->id, SILC_ID_SERVER,
725 channel->passphrase, NULL);
726 if (channel->founder_key)
727 silc_pkcs_public_key_free(channel->founder_key);
728 channel->founder_key = NULL;
729 } else if (client && !client->data.public_key) {
730 client->data.public_key =
731 silc_pkcs_public_key_copy(channel->founder_key);
735 if (mode & SILC_CHANNEL_MODE_FOUNDER_AUTH && !channel->founder_key &&
736 server->server_type == SILC_ROUTER) {
737 SILC_LOG_DEBUG(("Enforcing sender to change channel mode"));
738 mode &= ~SILC_CHANNEL_MODE_FOUNDER_AUTH;
739 silc_server_send_notify_cmode(server, sock, FALSE, channel,
740 mode, server->id, SILC_ID_SERVER,
743 channel->passphrase, NULL);
746 /* Send the same notify to the channel */
747 silc_server_packet_send_to_channel(server, NULL, channel, packet->type,
748 FALSE, packet->buffer->data,
749 packet->buffer->len, FALSE);
752 channel->mode = mode;
754 if (!(channel->mode & SILC_CHANNEL_MODE_FOUNDER_AUTH) &&
755 channel->founder_key) {
756 silc_pkcs_public_key_free(channel->founder_key);
757 channel->founder_key = NULL;
762 case SILC_NOTIFY_TYPE_CUMODE_CHANGE:
765 * Distribute the notify to local clients on the channel
767 SilcChannelClientEntry chl2 = NULL;
768 bool notify_sent = FALSE;
770 SILC_LOG_DEBUG(("CUMODE CHANGE notify"));
773 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
776 client_id = silc_id_payload_parse_id(tmp, tmp_len, &id_type);
780 /* Get client entry */
781 if (id_type == SILC_ID_CLIENT) {
782 client = silc_idlist_find_client_by_id(server->global_list,
783 client_id, TRUE, &cache);
785 client = silc_idlist_find_client_by_id(server->local_list,
786 client_id, TRUE, &cache);
788 silc_free(client_id);
792 silc_free(client_id);
796 channel_id = silc_id_str2id(packet->dst_id, packet->dst_id_len,
797 packet->dst_id_type);
802 /* Get channel entry */
803 channel = silc_idlist_find_channel_by_id(server->global_list,
806 channel = silc_idlist_find_channel_by_id(server->local_list,
809 SILC_LOG_DEBUG(("Notify for unknown channel"));
810 silc_free(channel_id);
816 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
818 silc_free(channel_id);
822 SILC_GET32_MSB(mode, tmp);
824 /* Get target client */
825 tmp = silc_argument_get_arg_type(args, 3, &tmp_len);
828 client_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
832 /* Get client entry */
833 client2 = silc_idlist_find_client_by_id(server->global_list,
834 client_id, TRUE, NULL);
836 client2 = silc_idlist_find_client_by_id(server->local_list,
837 client_id, TRUE, NULL);
839 silc_free(client_id);
843 silc_free(client_id);
846 /* Check that sender is on channel */
847 if (!silc_server_client_on_channel(client, channel, &chl))
850 if (client != client2 && server->server_type == SILC_ROUTER) {
851 /* Sender must be operator */
852 if (!(chl->mode & SILC_CHANNEL_UMODE_CHANOP) &&
853 !(chl->mode & SILC_CHANNEL_UMODE_CHANFO)) {
854 SILC_LOG_DEBUG(("CUMODE change is not allowed"));
858 if (!silc_server_client_on_channel(client2, channel, &chl))
861 /* If target is founder mode change is not allowed. */
862 if (chl->mode & SILC_CHANNEL_UMODE_CHANFO) {
863 SILC_LOG_DEBUG(("CUMODE change is not allowed"));
869 /* Get target channel user entry */
870 if (!silc_server_client_on_channel(client2, channel, &chl))
873 if (server->server_type == SILC_SERVER && chl->mode == mode) {
874 SILC_LOG_DEBUG(("Mode is changed already"));
878 if (mode & SILC_CHANNEL_UMODE_CHANFO &&
879 !(chl->mode & SILC_CHANNEL_UMODE_CHANFO) &&
880 server->server_type == SILC_ROUTER &&
881 sock != SILC_PRIMARY_ROUTE(server)) {
882 SilcPublicKey founder_key = NULL;
884 /* If channel doesn't have founder auth mode then it's impossible
885 that someone would be getting founder rights with CUMODE command.
886 In that case there already either is founder or there isn't
887 founder at all on the channel. */
888 if (client && !(channel->mode & SILC_CHANNEL_MODE_FOUNDER_AUTH)) {
889 /* Force the mode to not have founder mode */
890 chl->mode = mode &= ~SILC_CHANNEL_UMODE_CHANFO;
891 silc_server_force_cumode_change(server, sock, channel, chl, mode);
896 /* Get the founder of the channel and if found then this client
897 cannot be the founder since there already is one. */
898 silc_hash_table_list(channel->user_list, &htl);
899 while (silc_hash_table_get(&htl, NULL, (void *)&chl2))
900 if (chl2->mode & SILC_CHANNEL_UMODE_CHANFO) {
901 /* If the founder on the channel is not the one whom has set
902 the founder mode, then it's possible that this CUMODE_CHANGE
903 is correct. Due to netsplits it's possible that this
904 situation happens. */
905 if (!(channel->mode & SILC_CHANNEL_MODE_FOUNDER_AUTH) ||
906 (channel->founder_key && chl2->client->data.public_key &&
907 silc_pkcs_public_key_compare(
908 channel->founder_key,
909 chl2->client->data.public_key))) {
910 chl->mode = mode &= ~SILC_CHANNEL_UMODE_CHANFO;
911 silc_server_force_cumode_change(server, sock, channel,
917 silc_hash_table_list_reset(&htl);
918 if (!(mode & SILC_CHANNEL_UMODE_CHANFO))
921 /* Founder not found of the channel. Since the founder auth mode
922 is set on the channel now check whether this is the client that
923 originally set the mode. */
925 if (channel->founder_key) {
926 /* Get public key that must be present in notify */
927 tmp = silc_argument_get_arg_type(args, 4, &tmp_len);
928 if (!tmp || !silc_pkcs_public_key_decode(tmp, tmp_len,
930 chl->mode = mode &= ~SILC_CHANNEL_UMODE_CHANFO;
931 silc_server_force_cumode_change(server, sock, channel, chl, mode);
936 /* Now match the public key we have cached and public key sent.
938 if (client && client->data.public_key &&
939 !silc_pkcs_public_key_compare(channel->founder_key,
940 client->data.public_key)) {
941 chl->mode = mode &= ~SILC_CHANNEL_UMODE_CHANFO;
942 silc_server_force_cumode_change(server, sock, channel, chl, mode);
946 if (!silc_pkcs_public_key_compare(channel->founder_key,
948 chl->mode = mode &= ~SILC_CHANNEL_UMODE_CHANFO;
949 silc_server_force_cumode_change(server, sock, channel, chl, mode);
955 /* There cannot be anyone else as founder on the channel now. This
956 client is definitely the founder due to this authentication */
957 silc_hash_table_list(channel->user_list, &htl);
958 while (silc_hash_table_get(&htl, NULL, (void *)&chl2))
959 if (chl2->mode & SILC_CHANNEL_UMODE_CHANFO) {
960 chl2->mode &= ~SILC_CHANNEL_UMODE_CHANFO;
961 silc_server_force_cumode_change(server, NULL, channel, chl2,
965 silc_hash_table_list_reset(&htl);
968 silc_pkcs_public_key_free(founder_key);
971 if (server->server_type != SILC_SERVER && chl->mode == mode) {
972 SILC_LOG_DEBUG(("Mode is changed already"));
976 SILC_LOG_DEBUG(("Changing %s channel user mode",
977 chl->client->nickname ? chl->client->nickname :
978 (unsigned char *)""));
980 /* Change the mode */
983 /* Send the same notify to the channel */
985 silc_server_packet_send_to_channel(server, NULL, channel,
987 FALSE, packet->buffer->data,
988 packet->buffer->len, FALSE);
990 silc_free(channel_id);
994 case SILC_NOTIFY_TYPE_INVITE:
996 if (packet->dst_id_type == SILC_ID_CLIENT)
999 SILC_LOG_DEBUG(("INVITE notify"));
1001 /* Get Channel ID */
1002 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
1005 channel_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
1009 /* Get channel entry */
1010 channel = silc_idlist_find_channel_by_id(server->global_list,
1013 channel = silc_idlist_find_channel_by_id(server->local_list,
1016 SILC_LOG_DEBUG(("Notify for unknown channel"));
1017 silc_free(channel_id);
1021 silc_free(channel_id);
1024 tmp = silc_argument_get_arg_type(args, 3, &tmp_len);
1027 client_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
1031 /* Get client entry */
1032 client = silc_idlist_find_client_by_id(server->global_list,
1033 client_id, TRUE, &cache);
1035 client = silc_idlist_find_client_by_id(server->local_list,
1036 client_id, TRUE, &cache);
1038 silc_free(client_id);
1042 silc_free(client_id);
1044 /* Get user's channel entry and check that inviting is allowed. */
1045 if (!silc_server_client_on_channel(client, channel, &chl))
1047 if (channel->mode & SILC_CHANNEL_MODE_INVITE &&
1048 !(chl->mode & SILC_CHANNEL_UMODE_CHANOP) &&
1049 !(chl->mode & SILC_CHANNEL_UMODE_CHANFO)) {
1050 SILC_LOG_DEBUG(("Inviting is not allowed"));
1054 /* Get the added invite */
1055 tmp = silc_argument_get_arg_type(args, 4, &tmp_len);
1057 if (!channel->invite_list)
1058 channel->invite_list = silc_calloc(tmp_len + 2,
1059 sizeof(*channel->invite_list));
1061 channel->invite_list = silc_realloc(channel->invite_list,
1062 sizeof(*channel->invite_list) *
1064 strlen(channel->invite_list) +
1066 if (tmp[tmp_len - 1] == ',')
1067 tmp[tmp_len - 1] = '\0';
1069 strncat(channel->invite_list, tmp, tmp_len);
1070 strncat(channel->invite_list, ",", 1);
1073 /* Get the deleted invite */
1074 tmp = silc_argument_get_arg_type(args, 5, &tmp_len);
1075 if (tmp && channel->invite_list) {
1076 char *start, *end, *n;
1078 if (!strncmp(channel->invite_list, tmp,
1079 strlen(channel->invite_list) - 1)) {
1080 silc_free(channel->invite_list);
1081 channel->invite_list = NULL;
1083 start = strstr(channel->invite_list, tmp);
1084 if (start && strlen(start) >= tmp_len) {
1085 end = start + tmp_len;
1086 n = silc_calloc(strlen(channel->invite_list) - tmp_len, sizeof(*n));
1087 strncat(n, channel->invite_list, start - channel->invite_list);
1088 strncat(n, end + 1, ((channel->invite_list +
1089 strlen(channel->invite_list)) - end) - 1);
1090 silc_free(channel->invite_list);
1091 channel->invite_list = n;
1098 case SILC_NOTIFY_TYPE_CHANNEL_CHANGE:
1100 * Distribute to the local clients on the channel and change the
1104 SILC_LOG_DEBUG(("CHANNEL CHANGE"));
1106 if (sock->type != SILC_SOCKET_TYPE_ROUTER)
1109 /* Get the old Channel ID */
1110 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
1113 channel_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
1117 /* Get the channel entry */
1118 channel = silc_idlist_find_channel_by_id(server->local_list,
1121 channel = silc_idlist_find_channel_by_id(server->global_list,
1124 SILC_LOG_DEBUG(("Notify for unknown channel"));
1125 silc_free(channel_id);
1130 /* Send the notify to the channel */
1131 silc_server_packet_send_to_channel(server, sock, channel, packet->type,
1132 FALSE, packet->buffer->data,
1133 packet->buffer->len, FALSE);
1135 /* Get the new Channel ID */
1136 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
1139 channel_id2 = silc_id_payload_parse_id(tmp, tmp_len, NULL);
1143 SILC_LOG_DEBUG(("Old Channel ID id(%s)",
1144 silc_id_render(channel_id, SILC_ID_CHANNEL)));
1145 SILC_LOG_DEBUG(("New Channel ID id(%s)",
1146 silc_id_render(channel_id2, SILC_ID_CHANNEL)));
1148 /* Replace the Channel ID */
1149 if (!silc_idlist_replace_channel_id(server->local_list, channel_id,
1151 if (!silc_idlist_replace_channel_id(server->global_list, channel_id,
1153 silc_free(channel_id2);
1158 SilcBuffer modes = NULL, users = NULL, users_modes = NULL;
1160 /* Re-announce this channel which ID was changed. */
1161 silc_server_send_new_channel(server, sock, FALSE, channel->channel_name,
1163 silc_id_get_len(channel->id,
1167 /* Re-announce our clients on the channel as the ID has changed now */
1168 silc_server_announce_get_channel_users(server, channel, &modes, &users,
1171 silc_buffer_push(users, users->data - users->head);
1172 silc_server_packet_send(server, sock,
1173 SILC_PACKET_NOTIFY, SILC_PACKET_FLAG_LIST,
1174 users->data, users->len, FALSE);
1175 silc_buffer_free(users);
1178 silc_buffer_push(modes, modes->data - modes->head);
1179 silc_server_packet_send_dest(server, sock,
1180 SILC_PACKET_NOTIFY, SILC_PACKET_FLAG_LIST,
1181 channel->id, SILC_ID_CHANNEL,
1182 modes->data, modes->len, FALSE);
1183 silc_buffer_free(modes);
1186 silc_buffer_push(users_modes, users_modes->data - users_modes->head);
1187 silc_server_packet_send_dest(server, sock,
1188 SILC_PACKET_NOTIFY, SILC_PACKET_FLAG_LIST,
1189 channel->id, SILC_ID_CHANNEL,
1191 users_modes->len, FALSE);
1192 silc_buffer_free(users_modes);
1195 /* Re-announce channel's topic */
1196 if (channel->topic) {
1197 silc_server_send_notify_topic_set(server, sock,
1198 server->server_type == SILC_ROUTER ?
1199 TRUE : FALSE, channel,
1200 server->id, SILC_ID_SERVER,
1205 silc_free(channel_id);
1209 case SILC_NOTIFY_TYPE_SERVER_SIGNOFF:
1211 * Remove the server entry and all clients that this server owns.
1214 SILC_LOG_DEBUG(("SERVER SIGNOFF notify"));
1217 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
1220 server_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
1224 /* Get server entry */
1225 server_entry = silc_idlist_find_server_by_id(server->global_list,
1226 server_id, TRUE, NULL);
1228 if (!server_entry) {
1229 server_entry = silc_idlist_find_server_by_id(server->local_list,
1230 server_id, TRUE, NULL);
1232 if (!server_entry) {
1233 /* If we are normal server then we might not have the server. Check
1234 whether router was kind enough to send the list of all clients
1235 that actually was to be removed. Remove them if the list is
1237 if (server->server_type != SILC_ROUTER &&
1238 silc_argument_get_arg_num(args) > 1) {
1241 for (i = 1; i < silc_argument_get_arg_num(args); i++) {
1243 tmp = silc_argument_get_arg_type(args, i + 1, &tmp_len);
1246 client_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
1250 /* Get client entry */
1251 client = silc_idlist_find_client_by_id(server->global_list,
1252 client_id, TRUE, &cache);
1255 client = silc_idlist_find_client_by_id(server->local_list,
1256 client_id, TRUE, &cache);
1259 silc_free(client_id);
1263 silc_free(client_id);
1265 /* Update statistics */
1266 server->stat.clients--;
1267 if (server->stat.cell_clients)
1268 server->stat.cell_clients--;
1269 SILC_OPER_STATS_UPDATE(client, server, SILC_UMODE_SERVER_OPERATOR);
1270 SILC_OPER_STATS_UPDATE(client, router, SILC_UMODE_ROUTER_OPERATOR);
1272 /* Remove the client from all channels. */
1273 silc_server_remove_from_channels(server, NULL, client,
1276 /* Check if anyone is watching this nickname */
1277 if (server->server_type == SILC_ROUTER)
1278 silc_server_check_watcher_list(server, client, NULL,
1279 SILC_NOTIFY_TYPE_SERVER_SIGNOFF);
1281 /* Remove this client from watcher list if it is */
1283 silc_server_del_from_watcher_list(server, client);
1285 /* Remove the client */
1286 silc_idlist_del_client(local ? server->local_list :
1287 server->global_list, client);
1291 silc_free(server_id);
1295 silc_free(server_id);
1297 /* Sending SERVER_SIGNOFF is not right way to signoff local connection */
1298 if (SILC_IS_LOCAL(server_entry))
1301 /* Remove all servers that are originated from this server, and
1302 remove the clients of those servers too. */
1303 silc_server_remove_servers_by_server(server, server_entry, TRUE);
1305 /* Remove the clients that this server owns as they will become
1307 silc_server_remove_clients_by_server(server, server_entry,
1308 server_entry, TRUE);
1309 silc_server_backup_del(server, server_entry);
1311 /* Remove the server entry */
1312 silc_idlist_del_server(local ? server->local_list :
1313 server->global_list, server_entry);
1315 /* Update statistics */
1316 if (server->server_type == SILC_ROUTER)
1317 server->stat.servers--;
1321 case SILC_NOTIFY_TYPE_KICKED:
1323 * Distribute the notify to local clients on the channel
1326 SILC_LOG_DEBUG(("KICKED notify"));
1329 channel_id = silc_id_str2id(packet->dst_id, packet->dst_id_len,
1330 packet->dst_id_type);
1335 /* Get channel entry */
1336 channel = silc_idlist_find_channel_by_id(server->global_list,
1339 channel = silc_idlist_find_channel_by_id(server->local_list,
1342 SILC_LOG_DEBUG(("Notify for unknown channel"));
1343 silc_free(channel_id);
1347 silc_free(channel_id);
1350 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
1353 client_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
1357 /* If the the client is not in local list we check global list */
1358 client = silc_idlist_find_client_by_id(server->global_list,
1359 client_id, TRUE, NULL);
1361 client = silc_idlist_find_client_by_id(server->local_list,
1362 client_id, TRUE, NULL);
1364 silc_free(client_id);
1368 silc_free(client_id);
1370 /* If target is founder they cannot be kicked */
1371 if (!silc_server_client_on_channel(client, channel, &chl))
1373 if (chl->mode & SILC_CHANNEL_UMODE_CHANFO)
1376 /* From protocol version 1.1 we get the kicker's ID as well. */
1377 tmp = silc_argument_get_arg_type(args, 3, &tmp_len);
1379 client_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
1383 /* If the the client is not in local list we check global list */
1384 client2 = silc_idlist_find_client_by_id(server->global_list,
1385 client_id, TRUE, NULL);
1387 client2 = silc_idlist_find_client_by_id(server->local_list,
1388 client_id, TRUE, NULL);
1390 silc_free(client_id);
1394 silc_free(client_id);
1396 /* Kicker must be operator on channel */
1397 if (!silc_server_client_on_channel(client2, channel, &chl))
1399 if (!(chl->mode & SILC_CHANNEL_UMODE_CHANOP) &&
1400 !(chl->mode & SILC_CHANNEL_UMODE_CHANFO)) {
1401 SILC_LOG_DEBUG(("Kicking is not allowed"));
1406 /* Send to channel */
1407 silc_server_packet_send_to_channel(server, sock, channel, packet->type,
1408 FALSE, packet->buffer->data,
1409 packet->buffer->len, FALSE);
1411 /* Remove the client from channel */
1412 silc_server_remove_from_one_channel(server, sock, channel, client, FALSE);
1416 case SILC_NOTIFY_TYPE_KILLED:
1419 * Distribute the notify to local clients on channels
1421 unsigned char *id, *comment;
1422 SilcUInt32 id_len, comment_len;
1424 SILC_LOG_DEBUG(("KILLED notify"));
1427 id = silc_argument_get_arg_type(args, 1, &id_len);
1430 client_id = silc_id_payload_parse_id(id, id_len, NULL);
1434 /* If the the client is not in local list we check global list */
1435 client = silc_idlist_find_client_by_id(server->global_list,
1436 client_id, TRUE, &cache);
1438 client = silc_idlist_find_client_by_id(server->local_list,
1439 client_id, TRUE, &cache);
1441 silc_free(client_id);
1445 silc_free(client_id);
1447 /* If the client is one of ours, then close the connection to the
1448 client now. This removes the client from all channels as well. */
1449 if (packet->dst_id_type == SILC_ID_CLIENT && client->connection) {
1450 sock = client->connection;
1451 silc_server_free_client_data(server, NULL, client, FALSE, NULL);
1452 silc_server_close_connection(server, sock);
1457 comment = silc_argument_get_arg_type(args, 2, &comment_len);
1458 if (comment_len > 128)
1461 /* From protocol version 1.1 we get the killer's ID as well. */
1462 tmp = silc_argument_get_arg_type(args, 3, &tmp_len);
1464 client_id = silc_id_payload_parse_id(tmp, tmp_len, &id_type);
1468 if (id_type == SILC_ID_CLIENT) {
1469 /* If the the client is not in local list we check global list */
1470 client2 = silc_idlist_find_client_by_id(server->global_list,
1471 client_id, TRUE, NULL);
1473 client2 = silc_idlist_find_client_by_id(server->local_list,
1474 client_id, TRUE, NULL);
1476 silc_free(client_id);
1480 silc_free(client_id);
1482 /* Killer must be router operator */
1483 if (server->server_type != SILC_SERVER &&
1484 !(client2->mode & SILC_UMODE_ROUTER_OPERATOR)) {
1485 SILC_LOG_DEBUG(("Killing is not allowed"));
1491 /* Send the notify to local clients on the channels except to the
1492 client who is killed. */
1493 silc_server_send_notify_on_channels(server, client, client,
1494 SILC_NOTIFY_TYPE_KILLED, 3,
1495 id, id_len, comment, comment_len,
1498 /* Remove the client from all channels */
1499 silc_server_remove_from_channels(server, NULL, client, FALSE, NULL,
1502 /* Check if anyone is watching this nickname */
1503 silc_server_check_watcher_list(server, client, NULL,
1504 SILC_NOTIFY_TYPE_KILLED);
1506 /* Update statistics */
1507 server->stat.clients--;
1508 if (server->stat.cell_clients)
1509 server->stat.cell_clients--;
1510 SILC_OPER_STATS_UPDATE(client, server, SILC_UMODE_SERVER_OPERATOR);
1511 SILC_OPER_STATS_UPDATE(client, router, SILC_UMODE_ROUTER_OPERATOR);
1513 if (SILC_IS_LOCAL(client)) {
1514 server->stat.my_clients--;
1515 silc_schedule_task_del_by_context(server->schedule, client);
1516 silc_idlist_del_data(client);
1520 client->data.status &= ~SILC_IDLIST_STATUS_REGISTERED;
1521 cache->expire = SILC_ID_CACHE_EXPIRE_DEF;
1525 case SILC_NOTIFY_TYPE_UMODE_CHANGE:
1527 * Save the mode of the client.
1530 SILC_LOG_DEBUG(("UMODE_CHANGE notify"));
1533 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
1536 client_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
1540 /* Get client entry */
1541 client = silc_idlist_find_client_by_id(server->global_list,
1542 client_id, TRUE, NULL);
1544 client = silc_idlist_find_client_by_id(server->local_list,
1545 client_id, TRUE, NULL);
1547 silc_free(client_id);
1551 silc_free(client_id);
1554 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
1557 SILC_GET32_MSB(mode, tmp);
1559 /* Remove internal resumed flag if client is marked detached now */
1560 if (mode & SILC_UMODE_DETACHED)
1561 client->data.status &= ~SILC_IDLIST_STATUS_RESUMED;
1563 /* Update statistics */
1564 if (server->server_type == SILC_ROUTER) {
1565 if (mode & SILC_UMODE_GONE) {
1566 if (!(client->mode & SILC_UMODE_GONE))
1567 server->stat.aways++;
1569 if (client->mode & SILC_UMODE_GONE)
1570 server->stat.aways--;
1572 if (mode & SILC_UMODE_DETACHED) {
1573 if (!(client->mode & SILC_UMODE_DETACHED))
1574 server->stat.detached++;
1576 if (client->mode & SILC_UMODE_DETACHED)
1577 server->stat.detached--;
1581 /* Change the mode */
1582 client->mode = mode;
1584 /* Check if anyone is watching this nickname */
1585 if (server->server_type == SILC_ROUTER)
1586 silc_server_check_watcher_list(server, client, NULL,
1587 SILC_NOTIFY_TYPE_UMODE_CHANGE);
1591 case SILC_NOTIFY_TYPE_BAN:
1596 SILC_LOG_DEBUG(("BAN notify"));
1598 /* Get Channel ID */
1599 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
1602 channel_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
1606 /* Get channel entry */
1607 channel = silc_idlist_find_channel_by_id(server->global_list,
1610 channel = silc_idlist_find_channel_by_id(server->local_list,
1613 SILC_LOG_DEBUG(("Notify for unknown channel"));
1614 silc_free(channel_id);
1618 silc_free(channel_id);
1620 /* Get the new ban and add it to the ban list */
1621 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
1623 if (!channel->ban_list)
1624 channel->ban_list = silc_calloc(tmp_len + 2,
1625 sizeof(*channel->ban_list));
1627 channel->ban_list = silc_realloc(channel->ban_list,
1628 sizeof(*channel->ban_list) *
1630 strlen(channel->ban_list) + 2));
1631 strncat(channel->ban_list, tmp, tmp_len);
1632 strncat(channel->ban_list, ",", 1);
1635 /* Get the ban to be removed and remove it from the list */
1636 tmp = silc_argument_get_arg_type(args, 3, &tmp_len);
1637 if (tmp && channel->ban_list) {
1638 char *start, *end, *n;
1640 if (!strncmp(channel->ban_list, tmp, strlen(channel->ban_list) - 1)) {
1641 silc_free(channel->ban_list);
1642 channel->ban_list = NULL;
1644 start = strstr(channel->ban_list, tmp);
1645 if (start && strlen(start) >= tmp_len) {
1646 end = start + tmp_len;
1647 n = silc_calloc(strlen(channel->ban_list) - tmp_len, sizeof(*n));
1648 strncat(n, channel->ban_list, start - channel->ban_list);
1649 strncat(n, end + 1, ((channel->ban_list +
1650 strlen(channel->ban_list)) - end) - 1);
1651 silc_free(channel->ban_list);
1652 channel->ban_list = n;
1658 case SILC_NOTIFY_TYPE_ERROR:
1665 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
1666 if (!tmp && tmp_len != 1)
1668 error = (SilcStatus)tmp[0];
1670 SILC_LOG_DEBUG(("ERROR notify (%d)", error));
1672 if (error == SILC_STATUS_ERR_NO_SUCH_CLIENT_ID &&
1673 sock->type == SILC_SOCKET_TYPE_ROUTER) {
1674 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
1676 SILC_LOG_DEBUG(("Received invalid client ID notification, deleting "
1677 "the entry from cache"));
1678 client_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
1681 client = silc_idlist_find_client_by_id(server->global_list,
1682 client_id, FALSE, NULL);
1684 silc_server_remove_from_channels(server, NULL, client, TRUE,
1686 silc_idlist_del_client(server->global_list, client);
1688 silc_free(client_id);
1694 /* Ignore rest of the notify types for now */
1695 case SILC_NOTIFY_TYPE_NONE:
1696 case SILC_NOTIFY_TYPE_MOTD:
1703 silc_notify_payload_free(payload);
1706 void silc_server_notify_list(SilcServer server,
1707 SilcSocketConnection sock,
1708 SilcPacketContext *packet)
1710 SilcPacketContext *new;
1714 SILC_LOG_DEBUG(("Processing Notify List"));
1716 if (sock->type == SILC_SOCKET_TYPE_CLIENT ||
1717 packet->src_id_type != SILC_ID_SERVER)
1720 /* Make copy of the original packet context, except for the actual
1721 data buffer, which we will here now fetch from the original buffer. */
1722 new = silc_packet_context_alloc();
1723 new->type = SILC_PACKET_NOTIFY;
1724 new->flags = packet->flags;
1725 new->src_id = packet->src_id;
1726 new->src_id_len = packet->src_id_len;
1727 new->src_id_type = packet->src_id_type;
1728 new->dst_id = packet->dst_id;
1729 new->dst_id_len = packet->dst_id_len;
1730 new->dst_id_type = packet->dst_id_type;
1732 buffer = silc_buffer_alloc(1024);
1733 new->buffer = buffer;
1735 while (packet->buffer->len) {
1736 SILC_GET16_MSB(len, packet->buffer->data + 2);
1737 if (len > packet->buffer->len)
1740 if (len > buffer->truelen) {
1741 silc_buffer_free(buffer);
1742 buffer = silc_buffer_alloc(1024 + len);
1745 silc_buffer_pull_tail(buffer, len);
1746 silc_buffer_put(buffer, packet->buffer->data, len);
1748 /* Process the Notify */
1749 silc_server_notify(server, sock, new);
1751 silc_buffer_push_tail(buffer, len);
1752 silc_buffer_pull(packet->buffer, len);
1755 silc_buffer_free(buffer);
1759 /* Received private message. This resolves the destination of the message
1760 and sends the packet. This is used by both server and router. If the
1761 destination is our locally connected client this sends the packet to
1762 the client. This may also send the message for further routing if
1763 the destination is not in our server (or router). */
1765 void silc_server_private_message(SilcServer server,
1766 SilcSocketConnection sock,
1767 SilcPacketContext *packet)
1769 SilcSocketConnection dst_sock;
1770 SilcIDListData idata;
1771 SilcClientEntry client;
1773 SILC_LOG_DEBUG(("Start"));
1775 if (packet->src_id_type != SILC_ID_CLIENT ||
1776 packet->dst_id_type != SILC_ID_CLIENT || !packet->dst_id)
1779 /* Get the route to the client */
1780 dst_sock = silc_server_get_client_route(server, packet->dst_id,
1781 packet->dst_id_len, NULL,
1785 unsigned char error;
1787 if (client && client->mode & SILC_UMODE_DETACHED) {
1788 SILC_LOG_DEBUG(("Client is detached, discarding packet"));
1792 /* Send SILC_NOTIFY_TYPE_ERROR to indicate that such destination ID
1793 does not exist or is invalid. */
1794 idp = silc_id_payload_encode_data(packet->dst_id,
1796 packet->dst_id_type);
1800 error = SILC_STATUS_ERR_NO_SUCH_CLIENT_ID;
1801 if (packet->src_id_type == SILC_ID_CLIENT) {
1802 SilcClientID *client_id = silc_id_str2id(packet->src_id,
1804 packet->src_id_type);
1805 silc_server_send_notify_dest(server, sock, FALSE,
1806 client_id, SILC_ID_CLIENT,
1807 SILC_NOTIFY_TYPE_ERROR, 2,
1809 idp->data, idp->len);
1810 silc_free(client_id);
1812 silc_server_send_notify(server, sock, FALSE,
1813 SILC_NOTIFY_TYPE_ERROR, 2,
1815 idp->data, idp->len);
1818 silc_buffer_free(idp);
1822 /* Check whether destination client wishes to receive private messages */
1823 if (client && !(packet->flags & SILC_PACKET_FLAG_PRIVMSG_KEY) &&
1824 client->mode & SILC_UMODE_BLOCK_PRIVMSG) {
1825 SILC_LOG_DEBUG(("Client blocks private messages, discarding packet"));
1829 /* Send the private message */
1830 silc_server_send_private_message(server, dst_sock, idata->send_key,
1831 idata->hmac_send, idata->psn_send++,
1835 /* Received private message key packet.. This packet is never for us. It is to
1836 the client in the packet's destination ID. Sending of this sort of packet
1837 equals sending private message, ie. it is sent point to point from
1838 one client to another. */
1840 void silc_server_private_message_key(SilcServer server,
1841 SilcSocketConnection sock,
1842 SilcPacketContext *packet)
1844 SilcSocketConnection dst_sock;
1845 SilcIDListData idata;
1847 SILC_LOG_DEBUG(("Start"));
1849 if (packet->src_id_type != SILC_ID_CLIENT ||
1850 packet->dst_id_type != SILC_ID_CLIENT)
1853 if (!packet->dst_id)
1856 /* Get the route to the client */
1857 dst_sock = silc_server_get_client_route(server, packet->dst_id,
1858 packet->dst_id_len, NULL,
1863 /* Relay the packet */
1864 silc_server_relay_packet(server, dst_sock, idata->send_key,
1865 idata->hmac_send, idata->psn_send++, packet, FALSE);
1868 /* Processes incoming command reply packet. The command reply packet may
1869 be destined to one of our clients or it may directly for us. We will
1870 call the command reply routine after processing the packet. */
1872 void silc_server_command_reply(SilcServer server,
1873 SilcSocketConnection sock,
1874 SilcPacketContext *packet)
1876 SilcBuffer buffer = packet->buffer;
1877 SilcClientEntry client = NULL;
1878 SilcSocketConnection dst_sock;
1879 SilcIDListData idata;
1880 SilcClientID *id = NULL;
1882 SILC_LOG_DEBUG(("Start"));
1884 /* Source must be server or router */
1885 if (packet->src_id_type != SILC_ID_SERVER &&
1886 sock->type != SILC_SOCKET_TYPE_ROUTER)
1889 if (packet->dst_id_type == SILC_ID_CHANNEL)
1892 if (packet->dst_id_type == SILC_ID_CLIENT) {
1893 /* Destination must be one of ours */
1894 id = silc_id_str2id(packet->dst_id, packet->dst_id_len, SILC_ID_CLIENT);
1897 client = silc_idlist_find_client_by_id(server->local_list, id, TRUE, NULL);
1899 SILC_LOG_ERROR(("Cannot process command reply to unknown client"));
1905 if (packet->dst_id_type == SILC_ID_SERVER) {
1906 /* For now this must be for us */
1907 if (memcmp(packet->dst_id, server->id_string, server->id_string_len)) {
1908 SILC_LOG_ERROR(("Cannot process command reply to unknown server"));
1913 /* Execute command reply locally for the command */
1914 silc_server_command_reply_process(server, sock, buffer);
1916 if (packet->dst_id_type == SILC_ID_CLIENT && client && id) {
1917 /* Relay the packet to the client */
1918 const SilcBufferStruct p;
1920 dst_sock = (SilcSocketConnection)client->connection;
1921 idata = (SilcIDListData)client;
1923 silc_buffer_push(buffer, SILC_PACKET_HEADER_LEN + packet->src_id_len
1924 + packet->dst_id_len + packet->padlen);
1925 if (!silc_packet_send_prepare(dst_sock, 0, 0, buffer->len,
1926 idata->hmac_send, (const SilcBuffer)&p)) {
1927 SILC_LOG_ERROR(("Cannot send packet"));
1930 silc_buffer_put((SilcBuffer)&p, buffer->data, buffer->len);
1932 /* Encrypt packet */
1933 silc_packet_encrypt(idata->send_key, idata->hmac_send, idata->psn_send++,
1934 (SilcBuffer)&p, buffer->len);
1936 /* Send the packet */
1937 silc_server_packet_send_real(server, dst_sock, TRUE);
1943 /* Process received channel message. The message can be originated from
1944 client or server. */
1946 void silc_server_channel_message(SilcServer server,
1947 SilcSocketConnection sock,
1948 SilcPacketContext *packet)
1950 SilcChannelEntry channel = NULL;
1951 SilcChannelID *id = NULL;
1952 void *sender_id = NULL;
1953 SilcClientEntry sender_entry = NULL;
1954 SilcChannelClientEntry chl;
1957 SILC_LOG_DEBUG(("Processing channel message"));
1960 if (packet->dst_id_type != SILC_ID_CHANNEL) {
1961 SILC_LOG_DEBUG(("Received bad message for channel, dropped"));
1965 /* Find channel entry */
1966 id = silc_id_str2id(packet->dst_id, packet->dst_id_len, SILC_ID_CHANNEL);
1969 channel = silc_idlist_find_channel_by_id(server->local_list, id, NULL);
1971 channel = silc_idlist_find_channel_by_id(server->global_list, id, NULL);
1974 unsigned char error;
1976 /* Send SILC_NOTIFY_TYPE_ERROR to indicate that such destination ID
1977 does not exist or is invalid. */
1978 idp = silc_id_payload_encode_data(packet->dst_id,
1980 packet->dst_id_type);
1984 error = SILC_STATUS_ERR_NO_SUCH_CHANNEL_ID;
1985 if (packet->src_id_type == SILC_ID_CLIENT) {
1986 SilcClientID *client_id = silc_id_str2id(packet->src_id,
1988 packet->src_id_type);
1989 silc_server_send_notify_dest(server, sock, FALSE,
1990 client_id, SILC_ID_CLIENT,
1991 SILC_NOTIFY_TYPE_ERROR, 2,
1992 &error, 1, idp->data, idp->len);
1993 silc_free(client_id);
1995 silc_server_send_notify(server, sock, FALSE,
1996 SILC_NOTIFY_TYPE_ERROR, 2,
1997 &error, 1, idp->data, idp->len);
2000 silc_buffer_free(idp);
2005 /* See that this client is on the channel. If the original sender is
2006 not client (as it can be server as well) we don't do the check. */
2007 sender_id = silc_id_str2id(packet->src_id, packet->src_id_len,
2008 packet->src_id_type);
2011 if (packet->src_id_type == SILC_ID_CLIENT) {
2012 sender_entry = silc_idlist_find_client_by_id(server->local_list,
2013 sender_id, TRUE, NULL);
2014 if (!sender_entry) {
2016 sender_entry = silc_idlist_find_client_by_id(server->global_list,
2017 sender_id, TRUE, NULL);
2019 if (!sender_entry || !silc_server_client_on_channel(sender_entry,
2021 SILC_LOG_DEBUG(("Client not on channel"));
2025 /* If channel is moderated check that client is allowed to send
2027 if (channel->mode & SILC_CHANNEL_MODE_SILENCE_USERS &&
2028 !(chl->mode & SILC_CHANNEL_UMODE_CHANOP) &&
2029 !(chl->mode & SILC_CHANNEL_UMODE_CHANFO)) {
2030 SILC_LOG_DEBUG(("Channel is silenced from normal users"));
2033 if (channel->mode & SILC_CHANNEL_MODE_SILENCE_OPERS &&
2034 chl->mode & SILC_CHANNEL_UMODE_CHANOP &&
2035 !(chl->mode & SILC_CHANNEL_UMODE_CHANFO)) {
2036 SILC_LOG_DEBUG(("Channel is silenced from operators"));
2039 if (chl->mode & SILC_CHANNEL_UMODE_QUIET) {
2040 SILC_LOG_DEBUG(("Sender is quieted on the channel"));
2044 /* If the packet is coming from router, but the client entry is local
2045 entry to us then some router is rerouting this to us and it is not
2046 allowed. When the client is local to us it means that we've routed
2047 this packet to network, and now someone is routing it back to us. */
2048 if (server->server_type == SILC_ROUTER &&
2049 sock->type == SILC_SOCKET_TYPE_ROUTER && local) {
2050 SILC_LOG_DEBUG(("Channel message rerouted to the sender, drop it"));
2055 /* Distribute the packet to our local clients. This will send the
2056 packet for further routing as well, if needed. */
2057 silc_server_packet_relay_to_channel(server, sock, channel, sender_id,
2058 packet->src_id_type, sender_entry,
2059 packet->buffer->data,
2060 packet->buffer->len, FALSE);
2063 silc_free(sender_id);
2067 /* Received channel key packet. We distribute the key to all of our locally
2068 connected clients on the channel. */
2070 void silc_server_channel_key(SilcServer server,
2071 SilcSocketConnection sock,
2072 SilcPacketContext *packet)
2074 SilcBuffer buffer = packet->buffer;
2075 SilcChannelEntry channel;
2077 if (packet->src_id_type != SILC_ID_SERVER ||
2078 (server->server_type == SILC_ROUTER &&
2079 sock->type == SILC_SOCKET_TYPE_ROUTER))
2082 /* Save the channel key */
2083 channel = silc_server_save_channel_key(server, buffer, NULL);
2087 /* Distribute the key to everybody who is on the channel. If we are router
2088 we will also send it to locally connected servers. */
2089 silc_server_send_channel_key(server, sock, channel, FALSE);
2091 if (server->server_type != SILC_BACKUP_ROUTER) {
2092 /* Distribute to local cell backup routers. */
2093 silc_server_backup_send(server, (SilcServerEntry)sock->user_data,
2094 SILC_PACKET_CHANNEL_KEY, 0,
2095 buffer->data, buffer->len, FALSE, TRUE);
2099 /* Received New Client packet and processes it. Creates Client ID for the
2100 client. Client becomes registered after calling this functions. */
2102 SilcClientEntry silc_server_new_client(SilcServer server,
2103 SilcSocketConnection sock,
2104 SilcPacketContext *packet)
2106 SilcBuffer buffer = packet->buffer;
2107 SilcClientEntry client;
2108 SilcClientID *client_id;
2109 SilcIDListData idata;
2110 char *username = NULL, *realname = NULL;
2111 SilcUInt16 username_len;
2114 char *hostname, *nickname;
2117 SILC_LOG_DEBUG(("Creating new client"));
2119 if (sock->type != SILC_SOCKET_TYPE_CLIENT)
2122 /* Take client entry */
2123 client = (SilcClientEntry)sock->user_data;
2124 idata = (SilcIDListData)client;
2126 /* Remove the old cache entry. */
2127 if (!silc_idcache_del_by_context(server->local_list->clients, client)) {
2128 SILC_LOG_INFO(("Unauthenticated client attempted to register to network"));
2129 silc_server_disconnect_remote(server, sock,
2130 SILC_STATUS_ERR_NOT_AUTHENTICATED, NULL);
2131 if (sock->user_data)
2132 silc_server_free_sock_user_data(server, sock, NULL);
2136 /* Parse incoming packet */
2137 ret = silc_buffer_unformat(buffer,
2138 SILC_STR_UI16_NSTRING_ALLOC(&username,
2140 SILC_STR_UI16_STRING_ALLOC(&realname),
2143 silc_free(username);
2144 silc_free(realname);
2145 SILC_LOG_ERROR(("Client %s (%s) sent incomplete information, closing "
2146 "connection", sock->hostname, sock->ip));
2147 silc_server_disconnect_remote(server, sock,
2148 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
2150 if (sock->user_data)
2151 silc_server_free_sock_user_data(server, sock, NULL);
2156 silc_free(username);
2157 silc_free(realname);
2158 SILC_LOG_ERROR(("Client %s (%s) did not send its username, closing "
2159 "connection", sock->hostname, sock->ip));
2160 silc_server_disconnect_remote(server, sock,
2161 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
2163 if (sock->user_data)
2164 silc_server_free_sock_user_data(server, sock, NULL);
2168 if (username_len > 128)
2169 username[128] = '\0';
2171 /* Check for bad characters for nickname, and modify the nickname if
2172 it includes those. */
2173 if (silc_server_name_bad_chars(username, username_len)) {
2174 nickname = silc_server_name_modify_bad(username, username_len);
2176 nickname = strdup(username);
2179 /* Make sanity checks for the hostname of the client. If the hostname
2180 is provided in the `username' check that it is the same than the
2181 resolved hostname, or if not resolved the hostname that appears in
2182 the client's public key. If the hostname is not present then put
2183 it from the resolved name or from the public key. */
2184 if (strchr(username, '@')) {
2185 SilcPublicKeyIdentifier pident;
2186 int tlen = strcspn(username, "@");
2187 char *phostname = NULL;
2189 hostname = silc_memdup(username + tlen + 1, strlen(username) - tlen - 1);
2191 if (strcmp(sock->hostname, sock->ip) &&
2192 strcmp(sock->hostname, hostname)) {
2193 silc_free(username);
2194 silc_free(hostname);
2195 silc_free(realname);
2196 SILC_LOG_ERROR(("Client %s (%s) sent incomplete information, closing "
2197 "connection", sock->hostname, sock->ip));
2198 silc_server_disconnect_remote(server, sock,
2199 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
2201 if (sock->user_data)
2202 silc_server_free_sock_user_data(server, sock, NULL);
2206 pident = silc_pkcs_decode_identifier(client->data.public_key->identifier);
2208 phostname = strdup(pident->host);
2209 silc_pkcs_free_identifier(pident);
2212 if (!strcmp(sock->hostname, sock->ip) &&
2213 phostname && strcmp(phostname, hostname)) {
2214 silc_free(username);
2215 silc_free(hostname);
2216 silc_free(phostname);
2217 silc_free(realname);
2218 SILC_LOG_ERROR(("Client %s (%s) sent incomplete information, closing "
2219 "connection", sock->hostname, sock->ip));
2220 silc_server_disconnect_remote(server, sock,
2221 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
2223 if (sock->user_data)
2224 silc_server_free_sock_user_data(server, sock, NULL);
2228 silc_free(phostname);
2230 /* The hostname is not present, add it. */
2232 /* XXX For now we cannot take the host name from the public key since
2233 they are not trusted or we cannot verify them as trusted. Just take
2234 what the resolved name or address is. */
2236 if (strcmp(sock->hostname, sock->ip)) {
2238 newusername = silc_calloc(strlen(username) +
2239 strlen(sock->hostname) + 2,
2240 sizeof(*newusername));
2241 strncat(newusername, username, strlen(username));
2242 strncat(newusername, "@", 1);
2243 strncat(newusername, sock->hostname, strlen(sock->hostname));
2244 silc_free(username);
2245 username = newusername;
2248 SilcPublicKeyIdentifier pident =
2249 silc_pkcs_decode_identifier(client->data.public_key->identifier);
2252 newusername = silc_calloc(strlen(username) +
2253 strlen(pident->host) + 2,
2254 sizeof(*newusername));
2255 strncat(newusername, username, strlen(username));
2256 strncat(newusername, "@", 1);
2257 strncat(newusername, pident->host, strlen(pident->host));
2258 silc_free(username);
2259 username = newusername;
2260 silc_pkcs_free_identifier(pident);
2266 /* Create Client ID */
2267 while (!silc_id_create_client_id(server, server->id, server->rng,
2268 server->md5hash, nickname, &client_id)) {
2271 silc_server_disconnect_remote(server, sock,
2272 SILC_STATUS_ERR_BAD_NICKNAME, NULL);
2273 if (sock->user_data)
2274 silc_server_free_sock_user_data(server, sock, NULL);
2277 snprintf(&nickname[strlen(nickname) - 1], 1, "%d", nickfail);
2280 /* Update client entry */
2281 idata->status |= SILC_IDLIST_STATUS_REGISTERED;
2282 client->nickname = nickname;
2283 client->username = username;
2284 client->userinfo = realname ? realname : strdup(" ");
2285 client->id = client_id;
2286 id_len = silc_id_get_len(client_id, SILC_ID_CLIENT);
2288 /* Add the client again to the ID cache */
2289 silc_idcache_add(server->local_list->clients, client->nickname,
2290 client_id, client, 0, NULL);
2292 /* Notify our router about new client on the SILC network */
2293 silc_server_send_new_id(server, SILC_PRIMARY_ROUTE(server),
2294 SILC_BROADCAST(server), client->id,
2295 SILC_ID_CLIENT, id_len);
2297 /* Distribute to backup routers */
2298 if (server->server_type == SILC_ROUTER) {
2299 SilcBuffer idp = silc_id_payload_encode(client->id, SILC_ID_CLIENT);
2300 silc_server_backup_send(server, NULL, SILC_PACKET_NEW_ID, 0,
2301 idp->data, idp->len, FALSE, TRUE);
2302 silc_buffer_free(idp);
2305 /* Send the new client ID to the client. */
2306 silc_server_send_new_id(server, sock, FALSE, client->id, SILC_ID_CLIENT,
2307 silc_id_get_len(client->id, SILC_ID_CLIENT));
2309 /* Send some nice info to the client */
2310 silc_server_send_connect_notifys(server, sock, client);
2312 /* Check if anyone is watching this nickname */
2313 if (server->server_type == SILC_ROUTER)
2314 silc_server_check_watcher_list(server, client, NULL, 0);
2319 /* Create new server. This processes received New Server packet and
2320 saves the received Server ID. The server is our locally connected
2321 server thus we save all the information and save it to local list.
2322 This funtion can be used by both normal server and router server.
2323 If normal server uses this it means that its router has connected
2324 to the server. If router uses this it means that one of the cell's
2325 servers is connected to the router. */
2327 SilcServerEntry silc_server_new_server(SilcServer server,
2328 SilcSocketConnection sock,
2329 SilcPacketContext *packet)
2331 SilcBuffer buffer = packet->buffer;
2332 SilcServerEntry new_server, server_entry;
2333 SilcServerID *server_id;
2334 SilcIDListData idata;
2335 unsigned char *server_name, *id_string;
2336 SilcUInt16 id_len, name_len;
2340 SILC_LOG_DEBUG(("Creating new server"));
2342 if (sock->type != SILC_SOCKET_TYPE_SERVER &&
2343 sock->type != SILC_SOCKET_TYPE_ROUTER)
2346 /* Take server entry */
2347 new_server = (SilcServerEntry)sock->user_data;
2348 idata = (SilcIDListData)new_server;
2350 /* Remove the old cache entry */
2351 if (!silc_idcache_del_by_context(server->local_list->servers, new_server)) {
2352 if (!silc_idcache_del_by_context(server->global_list->servers,
2354 SILC_LOG_INFO(("Unauthenticated %s attempted to register to "
2355 "network", (sock->type == SILC_SOCKET_TYPE_SERVER ?
2356 "server" : "router")));
2357 silc_server_disconnect_remote(server, sock,
2358 SILC_STATUS_ERR_NOT_AUTHENTICATED, NULL);
2359 if (sock->user_data)
2360 silc_server_free_sock_user_data(server, sock, NULL);
2366 /* Parse the incoming packet */
2367 ret = silc_buffer_unformat(buffer,
2368 SILC_STR_UI16_NSTRING_ALLOC(&id_string, &id_len),
2369 SILC_STR_UI16_NSTRING_ALLOC(&server_name,
2373 silc_free(id_string);
2374 silc_free(server_name);
2375 silc_server_disconnect_remote(server, sock,
2376 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
2378 if (sock->user_data)
2379 silc_server_free_sock_user_data(server, sock, NULL);
2383 if (id_len > buffer->len) {
2384 silc_free(id_string);
2385 silc_free(server_name);
2386 silc_server_disconnect_remote(server, sock,
2387 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
2389 if (sock->user_data)
2390 silc_server_free_sock_user_data(server, sock, NULL);
2395 server_name[255] = '\0';
2398 server_id = silc_id_str2id(id_string, id_len, SILC_ID_SERVER);
2400 silc_free(id_string);
2401 silc_free(server_name);
2402 silc_server_disconnect_remote(server, sock,
2403 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
2405 if (sock->user_data)
2406 silc_server_free_sock_user_data(server, sock, NULL);
2409 silc_free(id_string);
2411 /* Check for valid server ID */
2412 if (!silc_id_is_valid_server_id(server, server_id, sock)) {
2413 SILC_LOG_INFO(("Invalid server ID sent by %s (%s)",
2414 sock->ip, sock->hostname));
2415 silc_server_disconnect_remote(server, sock,
2416 SILC_STATUS_ERR_BAD_SERVER_ID, NULL);
2417 if (sock->user_data)
2418 silc_server_free_sock_user_data(server, sock, NULL);
2419 silc_free(server_name);
2423 /* Check that we do not have this ID already */
2424 server_entry = silc_idlist_find_server_by_id(server->local_list,
2425 server_id, TRUE, NULL);
2427 silc_idcache_del_by_context(server->local_list->servers, server_entry);
2429 server_entry = silc_idlist_find_server_by_id(server->global_list,
2430 server_id, TRUE, NULL);
2432 silc_idcache_del_by_context(server->global_list->servers, server_entry);
2435 /* Update server entry */
2436 idata->status |= SILC_IDLIST_STATUS_REGISTERED;
2437 new_server->server_name = server_name;
2438 new_server->id = server_id;
2440 SILC_LOG_DEBUG(("New server id(%s)",
2441 silc_id_render(server_id, SILC_ID_SERVER)));
2443 /* Add again the entry to the ID cache. */
2444 silc_idcache_add(local ? server->local_list->servers :
2445 server->global_list->servers, server_name, server_id,
2446 new_server, 0, NULL);
2448 /* Distribute the information about new server in the SILC network
2449 to our router. If we are normal server we won't send anything
2450 since this connection must be our router connection. */
2451 if (server->server_type == SILC_ROUTER && !server->standalone &&
2452 SILC_PRIMARY_ROUTE(server) != sock)
2453 silc_server_send_new_id(server, SILC_PRIMARY_ROUTE(server),
2454 TRUE, new_server->id, SILC_ID_SERVER,
2455 silc_id_get_len(server_id, SILC_ID_SERVER));
2457 if (server->server_type == SILC_ROUTER) {
2458 /* Distribute to backup routers */
2459 SilcBuffer idp = silc_id_payload_encode(new_server->id, SILC_ID_SERVER);
2460 silc_server_backup_send(server, NULL, SILC_PACKET_NEW_ID, 0,
2461 idp->data, idp->len, FALSE, TRUE);
2462 silc_buffer_free(idp);
2465 server->stat.cell_servers++;
2468 /* Check whether this router connection has been replaced by an
2469 backup router. If it has been then we'll disable the server and will
2470 ignore everything it will send until the backup router resuming
2471 protocol has been completed. */
2472 if (sock->type == SILC_SOCKET_TYPE_ROUTER &&
2473 silc_server_backup_replaced_get(server, server_id, NULL)) {
2474 /* Send packet to the server indicating that it cannot use this
2475 connection as it has been replaced by backup router. */
2476 SilcBuffer packet = silc_buffer_alloc(2);
2477 silc_buffer_pull_tail(packet, SILC_BUFFER_END(packet));
2478 silc_buffer_format(packet,
2479 SILC_STR_UI_CHAR(SILC_SERVER_BACKUP_REPLACED),
2480 SILC_STR_UI_CHAR(0),
2482 silc_server_packet_send(server, sock,
2483 SILC_PACKET_RESUME_ROUTER, 0,
2484 packet->data, packet->len, TRUE);
2485 silc_buffer_free(packet);
2487 /* Mark the router disabled. The data sent earlier will go but nothing
2488 after this does not go to this connection. */
2489 idata->status |= SILC_IDLIST_STATUS_DISABLED;
2491 /* If it is router announce our stuff to it. */
2492 if (sock->type == SILC_SOCKET_TYPE_ROUTER &&
2493 server->server_type == SILC_ROUTER) {
2494 silc_server_announce_servers(server, FALSE, 0, sock);
2495 silc_server_announce_clients(server, 0, sock);
2496 silc_server_announce_channels(server, 0, sock);
2499 /* By default the servers connected to backup router are disabled
2500 until backup router has become the primary */
2501 if (server->server_type == SILC_BACKUP_ROUTER &&
2502 sock->type == SILC_SOCKET_TYPE_SERVER)
2503 idata->status |= SILC_IDLIST_STATUS_DISABLED;
2509 /* Processes incoming New ID packet. New ID Payload is used to distribute
2510 information about newly registered clients and servers. */
2512 static void silc_server_new_id_real(SilcServer server,
2513 SilcSocketConnection sock,
2514 SilcPacketContext *packet,
2517 SilcBuffer buffer = packet->buffer;
2519 SilcServerEntry router, server_entry;
2520 SilcSocketConnection router_sock;
2525 SILC_LOG_DEBUG(("Processing new ID"));
2527 if (sock->type == SILC_SOCKET_TYPE_CLIENT ||
2528 server->server_type == SILC_SERVER ||
2529 packet->src_id_type != SILC_ID_SERVER)
2532 idp = silc_id_payload_parse(buffer->data, buffer->len);
2536 id_type = silc_id_payload_get_type(idp);
2538 /* Normal server cannot have other normal server connections */
2539 server_entry = (SilcServerEntry)sock->user_data;
2540 if (id_type == SILC_ID_SERVER && sock->type == SILC_SOCKET_TYPE_SERVER &&
2541 server_entry->server_type == SILC_SERVER)
2544 id = silc_id_payload_get_id(idp);
2548 /* If the packet is coming from server then use the sender as the
2549 origin of the the packet. If it came from router then check the real
2550 sender of the packet and use that as the origin. */
2551 if (sock->type == SILC_SOCKET_TYPE_SERVER) {
2552 id_list = server->local_list;
2554 router = sock->user_data;
2556 /* If the sender is backup router and ID is server (and we are not
2557 backup router) then switch the entry to global list. */
2558 if (server_entry->server_type == SILC_BACKUP_ROUTER &&
2559 id_type == SILC_ID_SERVER &&
2560 server->id_entry->server_type != SILC_BACKUP_ROUTER) {
2561 id_list = server->global_list;
2562 router_sock = server->router ? SILC_PRIMARY_ROUTE(server) : sock;
2565 void *sender_id = silc_id_str2id(packet->src_id, packet->src_id_len,
2566 packet->src_id_type);
2567 router = silc_idlist_find_server_by_id(server->global_list,
2568 sender_id, TRUE, NULL);
2570 router = silc_idlist_find_server_by_id(server->local_list,
2571 sender_id, TRUE, NULL);
2572 silc_free(sender_id);
2574 id_list = server->global_list;
2581 case SILC_ID_CLIENT:
2583 SilcClientEntry entry;
2585 /* Check that we do not have this client already */
2586 entry = silc_idlist_find_client_by_id(server->global_list,
2587 id, server->server_type,
2590 entry = silc_idlist_find_client_by_id(server->local_list,
2591 id, server->server_type,
2594 SILC_LOG_DEBUG(("Ignoring client that we already have"));
2598 SILC_LOG_DEBUG(("New client id(%s) from [%s] %s",
2599 silc_id_render(id, SILC_ID_CLIENT),
2600 sock->type == SILC_SOCKET_TYPE_SERVER ?
2601 "Server" : "Router", sock->hostname));
2603 /* As a router we keep information of all global information in our
2604 global list. Cell wide information however is kept in the local
2606 entry = silc_idlist_add_client(id_list, NULL, NULL, NULL,
2607 id, router, NULL, 0);
2609 SILC_LOG_ERROR(("Could not add new client to the ID Cache"));
2611 /* Inform the sender that the ID is not usable */
2612 silc_server_send_notify_signoff(server, sock, FALSE, id, NULL);
2615 entry->nickname = NULL;
2616 entry->data.status |= SILC_IDLIST_STATUS_REGISTERED;
2618 if (sock->type == SILC_SOCKET_TYPE_SERVER)
2619 server->stat.cell_clients++;
2620 server->stat.clients++;
2622 /* Check if anyone is watching this nickname */
2623 if (server->server_type == SILC_ROUTER && id_list == server->local_list)
2624 silc_server_check_watcher_list(server, entry, NULL, 0);
2628 case SILC_ID_SERVER:
2630 SilcServerEntry entry;
2632 /* If the ID is mine, ignore it. */
2633 if (SILC_ID_SERVER_COMPARE(id, server->id)) {
2634 SILC_LOG_DEBUG(("Ignoring my own ID as new ID"));
2638 /* If the ID is the sender's ID, ignore it (we have it already) */
2639 if (SILC_ID_SERVER_COMPARE(id, router->id)) {
2640 SILC_LOG_DEBUG(("Ignoring sender's own ID"));
2644 /* Check that we do not have this server already */
2645 entry = silc_idlist_find_server_by_id(server->global_list,
2646 id, server->server_type,
2649 entry = silc_idlist_find_server_by_id(server->local_list,
2650 id, server->server_type,
2653 SILC_LOG_DEBUG(("Ignoring server that we already have"));
2657 SILC_LOG_DEBUG(("New server id(%s) from [%s] %s",
2658 silc_id_render(id, SILC_ID_SERVER),
2659 sock->type == SILC_SOCKET_TYPE_SERVER ?
2660 "Server" : "Router", sock->hostname));
2662 /* As a router we keep information of all global information in our
2663 global list. Cell wide information however is kept in the local
2665 entry = silc_idlist_add_server(id_list, NULL, 0, id, router,
2668 SILC_LOG_ERROR(("Could not add new server to the ID Cache"));
2671 entry->data.status |= SILC_IDLIST_STATUS_REGISTERED;
2673 if (sock->type == SILC_SOCKET_TYPE_SERVER)
2674 server->stat.cell_servers++;
2675 server->stat.servers++;
2679 case SILC_ID_CHANNEL:
2680 SILC_LOG_ERROR(("Channel cannot be registered with NEW_ID packet"));
2689 /* If the sender of this packet is server and we are router we need to
2690 broadcast this packet to other routers in the network. */
2691 if (broadcast && server->server_type == SILC_ROUTER &&
2692 sock->type == SILC_SOCKET_TYPE_SERVER &&
2693 !(packet->flags & SILC_PACKET_FLAG_BROADCAST)) {
2694 SILC_LOG_DEBUG(("Broadcasting received New ID packet"));
2695 silc_server_packet_send(server, SILC_PRIMARY_ROUTE(server),
2697 packet->flags | SILC_PACKET_FLAG_BROADCAST,
2698 buffer->data, buffer->len, FALSE);
2699 silc_server_backup_send(server, (SilcServerEntry)sock->user_data,
2700 packet->type, packet->flags,
2701 packet->buffer->data, packet->buffer->len,
2706 silc_id_payload_free(idp);
2710 /* Processes incoming New ID packet. New ID Payload is used to distribute
2711 information about newly registered clients and servers. */
2713 void silc_server_new_id(SilcServer server, SilcSocketConnection sock,
2714 SilcPacketContext *packet)
2716 silc_server_new_id_real(server, sock, packet, TRUE);
2719 /* Receoved New Id List packet, list of New ID payloads inside one
2720 packet. Process the New ID payloads one by one. */
2722 void silc_server_new_id_list(SilcServer server, SilcSocketConnection sock,
2723 SilcPacketContext *packet)
2725 SilcPacketContext *new_id;
2729 SILC_LOG_DEBUG(("Processing New ID List"));
2731 if (sock->type == SILC_SOCKET_TYPE_CLIENT ||
2732 packet->src_id_type != SILC_ID_SERVER)
2735 /* If the sender of this packet is server and we are router we need to
2736 broadcast this packet to other routers in the network. Broadcast
2737 this list packet instead of multiple New ID packets. */
2738 if (server->server_type == SILC_ROUTER &&
2739 sock->type == SILC_SOCKET_TYPE_SERVER &&
2740 !(packet->flags & SILC_PACKET_FLAG_BROADCAST)) {
2741 SILC_LOG_DEBUG(("Broadcasting received New ID List packet"));
2742 silc_server_packet_send(server, SILC_PRIMARY_ROUTE(server),
2744 packet->flags | SILC_PACKET_FLAG_BROADCAST,
2745 packet->buffer->data,
2746 packet->buffer->len, FALSE);
2747 silc_server_backup_send(server, (SilcServerEntry)sock->user_data,
2748 packet->type, packet->flags,
2749 packet->buffer->data, packet->buffer->len,
2753 /* Make copy of the original packet context, except for the actual
2754 data buffer, which we will here now fetch from the original buffer. */
2755 new_id = silc_packet_context_alloc();
2756 new_id->type = SILC_PACKET_NEW_ID;
2757 new_id->flags = packet->flags;
2758 new_id->src_id = packet->src_id;
2759 new_id->src_id_len = packet->src_id_len;
2760 new_id->src_id_type = packet->src_id_type;
2761 new_id->dst_id = packet->dst_id;
2762 new_id->dst_id_len = packet->dst_id_len;
2763 new_id->dst_id_type = packet->dst_id_type;
2765 idp = silc_buffer_alloc(256);
2766 new_id->buffer = idp;
2768 while (packet->buffer->len) {
2769 SILC_GET16_MSB(id_len, packet->buffer->data + 2);
2770 if ((id_len > packet->buffer->len) ||
2771 (id_len > idp->truelen))
2774 silc_buffer_pull_tail(idp, 4 + id_len);
2775 silc_buffer_put(idp, packet->buffer->data, 4 + id_len);
2777 /* Process the New ID */
2778 silc_server_new_id_real(server, sock, new_id, FALSE);
2780 silc_buffer_push_tail(idp, 4 + id_len);
2781 silc_buffer_pull(packet->buffer, 4 + id_len);
2784 silc_buffer_free(idp);
2788 /* Received New Channel packet. Information about new channels in the
2789 network are distributed using this packet. Save the information about
2790 the new channel. This usually comes from router but also normal server
2791 can send this to notify channels it has when it connects to us. */
2793 void silc_server_new_channel(SilcServer server,
2794 SilcSocketConnection sock,
2795 SilcPacketContext *packet)
2797 SilcChannelPayload payload;
2798 SilcChannelID *channel_id;
2800 SilcUInt32 name_len;
2803 SilcServerEntry server_entry;
2804 SilcChannelEntry channel;
2806 if (sock->type == SILC_SOCKET_TYPE_CLIENT ||
2807 packet->src_id_type != SILC_ID_SERVER ||
2808 server->server_type == SILC_SERVER)
2811 /* Parse the channel payload */
2812 payload = silc_channel_payload_parse(packet->buffer->data,
2813 packet->buffer->len);
2817 /* Get the channel ID */
2818 channel_id = silc_channel_get_id_parse(payload);
2820 silc_channel_payload_free(payload);
2824 channel_name = silc_channel_get_name(payload, &name_len);
2826 channel_name[255] = '\0';
2828 id = silc_channel_get_id(payload, &id_len);
2830 server_entry = (SilcServerEntry)sock->user_data;
2832 if (sock->type == SILC_SOCKET_TYPE_ROUTER) {
2833 /* Add the channel to global list as it is coming from router. It
2834 cannot be our own channel as it is coming from router. */
2836 /* Check that we don't already have this channel */
2837 channel = silc_idlist_find_channel_by_name(server->local_list,
2838 channel_name, NULL);
2840 channel = silc_idlist_find_channel_by_name(server->global_list,
2841 channel_name, NULL);
2843 SILC_LOG_DEBUG(("New channel id(%s) from [Router] %s",
2844 silc_id_render(channel_id, SILC_ID_CHANNEL),
2848 silc_idlist_add_channel(server->global_list, strdup(channel_name),
2849 0, channel_id, sock->user_data, NULL, NULL, 0);
2852 channel->disabled = TRUE;
2854 server->stat.channels++;
2855 if (server->server_type == SILC_ROUTER)
2856 channel->users_resolved = TRUE;
2859 /* The channel is coming from our server, thus it is in our cell
2860 we will add it to our local list. */
2863 SILC_LOG_DEBUG(("Channel id(%s) from [Server] %s",
2864 silc_id_render(channel_id, SILC_ID_CHANNEL),
2867 /* Check that we don't already have this channel */
2868 channel = silc_idlist_find_channel_by_name(server->local_list,
2869 channel_name, NULL);
2871 channel = silc_idlist_find_channel_by_name(server->global_list,
2872 channel_name, NULL);
2874 /* If the channel does not exist, then create it. This creates a new
2875 key to the channel as well that we will send to the server. */
2877 SILC_LOG_DEBUG(("Channel is new to us"));
2879 /* The protocol says that the Channel ID's IP address must be based
2880 on the router's IP address. Check whether the ID is based in our
2881 IP and if it is not then create a new ID and enforce the server
2882 to switch the ID. */
2883 if (server_entry->server_type != SILC_BACKUP_ROUTER &&
2884 !SILC_ID_COMPARE(channel_id, server->id, server->id->ip.data_len)) {
2886 SILC_LOG_DEBUG(("Forcing the server to change Channel ID"));
2888 if (silc_id_create_channel_id(server, server->id, server->rng, &tmp)) {
2889 silc_server_send_notify_channel_change(server, sock, FALSE,
2891 silc_free(channel_id);
2896 /* Create the channel with the provided Channel ID */
2897 channel = silc_server_create_new_channel_with_id(server, NULL, NULL,
2901 silc_channel_payload_free(payload);
2902 silc_free(channel_id);
2905 channel->disabled = TRUE;
2906 channel->mode = silc_channel_get_mode(payload);
2908 /* Send the new channel key to the server */
2909 id = silc_id_id2str(channel->id, SILC_ID_CHANNEL);
2910 id_len = silc_id_get_len(channel->id, SILC_ID_CHANNEL);
2911 chk = silc_channel_key_payload_encode(id_len, id,
2912 strlen(channel->channel_key->
2914 channel->channel_key->cipher->name,
2915 channel->key_len / 8,
2917 silc_server_packet_send(server, sock, SILC_PACKET_CHANNEL_KEY, 0,
2918 chk->data, chk->len, FALSE);
2919 silc_buffer_free(chk);
2921 /* The channel exist by that name, check whether the ID's match.
2922 If they don't then we'll force the server to use the ID we have.
2923 We also create a new key for the channel. */
2924 SilcBuffer modes = NULL, users = NULL, users_modes = NULL;
2926 SILC_LOG_DEBUG(("Channel already exists"));
2928 if (!SILC_ID_CHANNEL_COMPARE(channel_id, channel->id)) {
2929 /* They don't match, send CHANNEL_CHANGE notify to the server to
2930 force the ID change. */
2931 SILC_LOG_DEBUG(("Forcing the server to change Channel ID"));
2932 silc_server_send_notify_channel_change(server, sock, FALSE,
2933 channel_id, channel->id);
2935 /* Wait that server re-announces this channel */
2939 #if 0 /* We will announce our CMODE anyway for this channel, so no need
2940 to check it (implicit enforce). */
2942 /* If the mode is different from what we have then enforce the
2944 mode = silc_channel_get_mode(payload);
2945 if (channel->mode != mode) {
2946 SILC_LOG_DEBUG(("Forcing the server to change channel mode"));
2947 silc_server_send_notify_cmode(server, sock, FALSE, channel,
2948 channel->mode, server->id,
2949 SILC_ID_SERVER, channel->cipher,
2951 channel->passphrase,
2952 channel->founder_key);
2956 /* Create new key for the channel and send it to the server and
2957 everybody else possibly on the channel. */
2958 if (!(channel->mode & SILC_CHANNEL_MODE_PRIVKEY)) {
2959 if (!silc_server_create_channel_key(server, channel, 0))
2962 /* Send to the channel */
2963 silc_server_send_channel_key(server, sock, channel, FALSE);
2964 id = silc_id_id2str(channel->id, SILC_ID_CHANNEL);
2965 id_len = silc_id_get_len(channel->id, SILC_ID_CHANNEL);
2967 /* Send to the server */
2968 chk = silc_channel_key_payload_encode(id_len, id,
2969 strlen(channel->channel_key->
2971 channel->channel_key->
2973 channel->key_len / 8,
2975 silc_server_packet_send(server, sock, SILC_PACKET_CHANNEL_KEY, 0,
2976 chk->data, chk->len, FALSE);
2977 silc_buffer_free(chk);
2981 silc_free(channel_id);
2983 /* Update statistics */
2984 server->stat.channels++;
2985 server->stat.cell_channels++;
2987 /* Since the channel is coming from server and we also know about it
2988 then send the JOIN notify to the server so that it see's our
2989 users on the channel "joining" the channel. */
2990 silc_server_announce_get_channel_users(server, channel, &modes, &users,
2993 silc_buffer_push(users, users->data - users->head);
2994 silc_server_packet_send(server, sock,
2995 SILC_PACKET_NOTIFY, SILC_PACKET_FLAG_LIST,
2996 users->data, users->len, FALSE);
2997 silc_buffer_free(users);
3000 silc_buffer_push(modes, modes->data - modes->head);
3001 silc_server_packet_send_dest(server, sock,
3002 SILC_PACKET_NOTIFY, SILC_PACKET_FLAG_LIST,
3003 channel->id, SILC_ID_CHANNEL,
3004 modes->data, modes->len, FALSE);
3005 silc_buffer_free(modes);
3008 silc_buffer_push(users_modes, users_modes->data - users_modes->head);
3009 silc_server_packet_send_dest(server, sock,
3010 SILC_PACKET_NOTIFY, SILC_PACKET_FLAG_LIST,
3011 channel->id, SILC_ID_CHANNEL,
3013 users_modes->len, FALSE);
3014 silc_buffer_free(users_modes);
3016 if (channel->topic) {
3017 silc_server_send_notify_topic_set(server, sock,
3018 server->server_type == SILC_ROUTER ?
3019 TRUE : FALSE, channel,
3020 server->id, SILC_ID_SERVER,
3026 silc_channel_payload_free(payload);
3029 /* Received New Channel List packet, list of New Channel List payloads inside
3030 one packet. Process the New Channel payloads one by one. */
3032 void silc_server_new_channel_list(SilcServer server,
3033 SilcSocketConnection sock,
3034 SilcPacketContext *packet)
3036 SilcPacketContext *new;
3038 SilcUInt16 len1, len2;
3040 SILC_LOG_DEBUG(("Processing New Channel List"));
3042 if (sock->type == SILC_SOCKET_TYPE_CLIENT ||
3043 packet->src_id_type != SILC_ID_SERVER ||
3044 server->server_type == SILC_SERVER)
3047 /* If the sender of this packet is server and we are router we need to
3048 broadcast this packet to other routers in the network. Broadcast
3049 this list packet instead of multiple New Channel packets. */
3050 if (server->server_type == SILC_ROUTER &&
3051 sock->type == SILC_SOCKET_TYPE_SERVER &&
3052 !(packet->flags & SILC_PACKET_FLAG_BROADCAST)) {
3053 SILC_LOG_DEBUG(("Broadcasting received New Channel List packet"));
3054 silc_server_packet_send(server, SILC_PRIMARY_ROUTE(server),
3056 packet->flags | SILC_PACKET_FLAG_BROADCAST,
3057 packet->buffer->data,
3058 packet->buffer->len, FALSE);
3059 silc_server_backup_send(server, (SilcServerEntry)sock->user_data,
3060 packet->type, packet->flags,
3061 packet->buffer->data, packet->buffer->len,
3065 /* Make copy of the original packet context, except for the actual
3066 data buffer, which we will here now fetch from the original buffer. */
3067 new = silc_packet_context_alloc();
3068 new->type = SILC_PACKET_NEW_CHANNEL;
3069 new->flags = packet->flags;
3070 new->src_id = packet->src_id;
3071 new->src_id_len = packet->src_id_len;
3072 new->src_id_type = packet->src_id_type;
3073 new->dst_id = packet->dst_id;
3074 new->dst_id_len = packet->dst_id_len;
3075 new->dst_id_type = packet->dst_id_type;
3077 buffer = silc_buffer_alloc(512);
3078 new->buffer = buffer;
3080 while (packet->buffer->len) {
3081 SILC_GET16_MSB(len1, packet->buffer->data);
3082 if ((len1 > packet->buffer->len) ||
3083 (len1 > buffer->truelen))
3086 SILC_GET16_MSB(len2, packet->buffer->data + 2 + len1);
3087 if ((len2 > packet->buffer->len) ||
3088 (len2 > buffer->truelen))
3091 silc_buffer_pull_tail(buffer, 8 + len1 + len2);
3092 silc_buffer_put(buffer, packet->buffer->data, 8 + len1 + len2);
3094 /* Process the New Channel */
3095 silc_server_new_channel(server, sock, new);
3097 silc_buffer_push_tail(buffer, 8 + len1 + len2);
3098 silc_buffer_pull(packet->buffer, 8 + len1 + len2);
3101 silc_buffer_free(buffer);
3105 /* Received key agreement packet. This packet is never for us. It is to
3106 the client in the packet's destination ID. Sending of this sort of packet
3107 equals sending private message, ie. it is sent point to point from
3108 one client to another. */
3110 void silc_server_key_agreement(SilcServer server,
3111 SilcSocketConnection sock,
3112 SilcPacketContext *packet)
3114 SilcSocketConnection dst_sock;
3115 SilcIDListData idata;
3117 SILC_LOG_DEBUG(("Start"));
3119 if (packet->src_id_type != SILC_ID_CLIENT ||
3120 packet->dst_id_type != SILC_ID_CLIENT)
3123 if (!packet->dst_id)
3126 /* Get the route to the client */
3127 dst_sock = silc_server_get_client_route(server, packet->dst_id,
3128 packet->dst_id_len, NULL,
3133 /* Relay the packet */
3134 silc_server_relay_packet(server, dst_sock, idata->send_key,
3135 idata->hmac_send, idata->psn_send++,
3139 /* Received connection auth request packet that is used during connection
3140 phase to resolve the mandatory authentication method. This packet can
3141 actually be received at anytime but usually it is used only during
3142 the connection authentication phase. Now, protocol says that this packet
3143 can come from client or server, however, we support only this coming
3144 from client and expect that server always knows what authentication
3147 void silc_server_connection_auth_request(SilcServer server,
3148 SilcSocketConnection sock,
3149 SilcPacketContext *packet)
3151 SilcServerConfigClient *client = NULL;
3152 SilcUInt16 conn_type;
3154 SilcAuthMethod auth_meth = SILC_AUTH_NONE;
3156 if (packet->src_id_type && packet->src_id_type != SILC_ID_CLIENT) {
3157 SILC_LOG_DEBUG(("Request not from client"));
3161 /* Parse the payload */
3162 ret = silc_buffer_unformat(packet->buffer,
3163 SILC_STR_UI_SHORT(&conn_type),
3164 SILC_STR_UI_SHORT(NULL),
3169 if (conn_type != SILC_SOCKET_TYPE_CLIENT)
3172 /* Get the authentication method for the client */
3173 auth_meth = SILC_AUTH_NONE;
3174 client = silc_server_config_find_client(server, sock->ip);
3176 client = silc_server_config_find_client(server, sock->hostname);
3178 if (client->passphrase) {
3179 if (client->publickeys && !server->config->prefer_passphrase_auth)
3180 auth_meth = SILC_AUTH_PUBLIC_KEY;
3182 auth_meth = SILC_AUTH_PASSWORD;
3183 } else if (client->publickeys)
3184 auth_meth = SILC_AUTH_PUBLIC_KEY;
3187 SILC_LOG_DEBUG(("Authentication method is [%s]",
3188 (auth_meth == SILC_AUTH_NONE ? "None" :
3189 auth_meth == SILC_AUTH_PASSWORD ? "Passphrase" :
3190 "Digital signatures")));
3192 /* Send it back to the client */
3193 silc_server_send_connection_auth_request(server, sock, conn_type, auth_meth);
3196 /* Received REKEY packet. The sender of the packet wants to regenerate
3197 its session keys. This starts the REKEY protocol. */
3199 void silc_server_rekey(SilcServer server,
3200 SilcSocketConnection sock,
3201 SilcPacketContext *packet)
3203 SilcProtocol protocol;
3204 SilcServerRekeyInternalContext *proto_ctx;
3205 SilcIDListData idata = (SilcIDListData)sock->user_data;
3207 SILC_LOG_DEBUG(("Start"));
3209 /* Allocate internal protocol context. This is sent as context
3211 proto_ctx = silc_calloc(1, sizeof(*proto_ctx));
3212 proto_ctx->server = (void *)server;
3213 proto_ctx->sock = sock;
3214 proto_ctx->responder = TRUE;
3215 proto_ctx->pfs = idata->rekey->pfs;
3217 /* Perform rekey protocol. Will call the final callback after the
3218 protocol is over. */
3219 silc_protocol_alloc(SILC_PROTOCOL_SERVER_REKEY,
3220 &protocol, proto_ctx, silc_server_rekey_final);
3221 sock->protocol = protocol;
3223 if (proto_ctx->pfs == FALSE)
3224 /* Run the protocol */
3225 silc_protocol_execute(protocol, server->schedule, 0, 0);
3228 /* Received file transger packet. This packet is never for us. It is to
3229 the client in the packet's destination ID. Sending of this sort of packet
3230 equals sending private message, ie. it is sent point to point from
3231 one client to another. */
3233 void silc_server_ftp(SilcServer server,
3234 SilcSocketConnection sock,
3235 SilcPacketContext *packet)
3237 SilcSocketConnection dst_sock;
3238 SilcIDListData idata;
3240 SILC_LOG_DEBUG(("Start"));
3242 if (packet->src_id_type != SILC_ID_CLIENT ||
3243 packet->dst_id_type != SILC_ID_CLIENT)
3246 if (!packet->dst_id)
3249 /* Get the route to the client */
3250 dst_sock = silc_server_get_client_route(server, packet->dst_id,
3251 packet->dst_id_len, NULL,
3256 /* Relay the packet */
3257 silc_server_relay_packet(server, dst_sock, idata->send_key,
3258 idata->hmac_send, idata->psn_send++,
3264 SilcSocketConnection sock;
3265 SilcPacketContext *packet;
3267 } *SilcServerResumeResolve;
3269 SILC_SERVER_CMD_FUNC(resume_resolve)
3271 SilcServerResumeResolve r = (SilcServerResumeResolve)context;
3272 SilcServer server = r->server;
3273 SilcSocketConnection sock = r->sock;
3274 SilcServerCommandReplyContext reply = context2;
3275 SilcClientEntry client;
3277 SILC_LOG_DEBUG(("Start"));
3279 if (!reply || !silc_command_get_status(reply->payload, NULL, NULL)) {
3280 SILC_LOG_ERROR(("Client %s (%s) tried to resume unknown client, "
3281 "closing connection", sock->hostname, sock->ip));
3282 silc_server_disconnect_remote(server, sock,
3283 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
3284 "Resuming not possible");
3285 if (sock->user_data)
3286 silc_server_free_sock_user_data(server, sock, NULL);
3290 if (reply && silc_command_get(reply->payload) == SILC_COMMAND_WHOIS) {
3291 /* Get entry to the client, and resolve it if we don't have it. */
3292 client = silc_idlist_find_client_by_id(server->local_list,
3293 r->data, TRUE, NULL);
3295 client = silc_idlist_find_client_by_id(server->global_list,
3296 r->data, TRUE, NULL);
3298 SILC_LOG_ERROR(("Client %s (%s) tried to resume unknown client, "
3299 "closing connection", sock->hostname, sock->ip));
3300 silc_server_disconnect_remote(server, sock,
3301 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
3302 "Resuming not possible");
3303 if (sock->user_data)
3304 silc_server_free_sock_user_data(server, sock, NULL);
3309 if (!(client->mode & SILC_UMODE_DETACHED)) {
3310 SILC_LOG_ERROR(("Client %s (%s) tried to resume un-detached client, "
3311 "closing connection", sock->hostname, sock->ip));
3312 silc_server_disconnect_remote(server, sock,
3313 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
3314 "Resuming not possible");
3315 if (sock->user_data)
3316 silc_server_free_sock_user_data(server, sock, NULL);
3321 /* Reprocess the packet */
3322 silc_server_resume_client(server, sock, r->packet);
3325 silc_socket_free(r->sock);
3326 silc_packet_context_free(r->packet);
3331 /* Received client resuming packet. This is used to resume detached
3332 client session. It can be sent by the client who wishes to resume
3333 but this is also sent by servers and routers to notify other routers
3334 that the client is not detached anymore. */
3336 void silc_server_resume_client(SilcServer server,
3337 SilcSocketConnection sock,
3338 SilcPacketContext *packet)
3340 SilcBuffer buffer = packet->buffer, buf;
3341 SilcIDListData idata;
3342 SilcClientEntry detached_client;
3343 SilcClientID *client_id = NULL;
3344 unsigned char *id_string, *auth = NULL;
3345 SilcUInt16 id_len, auth_len = 0;
3346 int ret, nickfail = 0;
3347 bool resolved, local, nick_change = FALSE, resolve = FALSE;
3348 SilcChannelEntry channel;
3349 SilcHashTableList htl;
3350 SilcChannelClientEntry chl;
3351 SilcServerResumeResolve r;
3353 ret = silc_buffer_unformat(buffer,
3354 SILC_STR_UI16_NSTRING(&id_string, &id_len),
3357 client_id = silc_id_str2id(id_string, id_len, SILC_ID_CLIENT);
3359 if (sock->type == SILC_SOCKET_TYPE_CLIENT) {
3360 /* Client send this and is attempting to resume to old client session */
3361 SilcClientEntry client;
3365 silc_buffer_pull(buffer, 2 + id_len);
3366 auth = buffer->data;
3367 auth_len = buffer->len;
3368 silc_buffer_push(buffer, 2 + id_len);
3371 if (!client_id || auth_len < 128) {
3372 SILC_LOG_ERROR(("Client %s (%s) sent incomplete resume information, "
3373 "closing connection", sock->hostname, sock->ip));
3374 silc_server_disconnect_remote(server, sock,
3375 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
3376 "Resuming not possible");
3377 if (sock->user_data)
3378 silc_server_free_sock_user_data(server, sock, NULL);
3379 silc_free(client_id);
3383 /* Take client entry of this connection */
3384 client = (SilcClientEntry)sock->user_data;
3385 idata = (SilcIDListData)client;
3387 /* Get entry to the client, and resolve it if we don't have it. */
3388 detached_client = silc_server_get_client_resolve(server, client_id, FALSE,
3390 if (!detached_client) {
3392 /* The client info is being resolved. Reprocess this packet after
3393 receiving the reply to the query. */
3394 SILC_LOG_DEBUG(("Resolving client"));
3395 r = silc_calloc(1, sizeof(*r));
3399 r->sock = silc_socket_dup(sock);
3400 r->packet = silc_packet_context_dup(packet);
3401 r->data = client_id;
3402 silc_server_command_pending(server, SILC_COMMAND_WHOIS,
3404 silc_server_command_resume_resolve, r);
3406 SILC_LOG_ERROR(("Client %s (%s) tried to resume unknown client, "
3407 "closing connection", sock->hostname, sock->ip));
3408 silc_server_disconnect_remote(server, sock,
3409 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
3410 "Resuming not possible");
3411 if (sock->user_data)
3412 silc_server_free_sock_user_data(server, sock, NULL);
3413 silc_free(client_id);
3418 if (!(detached_client->mode & SILC_UMODE_DETACHED))
3420 if (!silc_hash_table_count(detached_client->channels) &&
3421 detached_client->router)
3423 if (!detached_client->nickname)
3427 if (server->server_type == SILC_SERVER && !server->standalone) {
3428 /* The client info is being resolved. Reprocess this packet after
3429 receiving the reply to the query. */
3430 SILC_LOG_DEBUG(("Resolving client info"));
3431 silc_server_get_client_resolve(server, client_id, TRUE, NULL);
3432 r = silc_calloc(1, sizeof(*r));
3436 r->sock = silc_socket_dup(sock);
3437 r->packet = silc_packet_context_dup(packet);
3438 r->data = client_id;
3439 silc_server_command_pending(server, SILC_COMMAND_WHOIS,
3441 silc_server_command_resume_resolve, r);
3444 if (server->server_type == SILC_SERVER) {
3445 SILC_LOG_ERROR(("Client %s (%s) tried to resume un-detached client, "
3446 "closing connection", sock->hostname, sock->ip));
3447 silc_server_disconnect_remote(server, sock,
3448 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
3449 "Resuming not possible");
3450 if (sock->user_data)
3451 silc_server_free_sock_user_data(server, sock, NULL);
3452 silc_free(client_id);
3457 /* Check that we have the public key of the client, if not then we must
3458 resolve it first. */
3459 if (!detached_client->data.public_key) {
3460 if (server->server_type == SILC_SERVER && server->standalone) {
3461 SILC_LOG_ERROR(("Detached client's public key not present, "
3462 "closing connection"));
3463 silc_server_disconnect_remote(server, sock,
3464 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
3465 "Resuming not possible");
3466 if (sock->user_data)
3467 silc_server_free_sock_user_data(server, sock, NULL);
3468 silc_free(client_id);
3470 /* We must retrieve the detached client's public key by sending
3471 GETKEY command. Reprocess this packet after receiving the key */
3472 SilcBuffer idp = silc_id_payload_encode(client_id, SILC_ID_CLIENT);
3473 SilcSocketConnection dest_sock =
3474 silc_server_get_client_route(server, NULL, 0, client_id, NULL, NULL);
3476 SILC_LOG_DEBUG(("Resolving client public key"));
3478 silc_server_send_command(server, dest_sock ? dest_sock :
3479 SILC_PRIMARY_ROUTE(server),
3480 SILC_COMMAND_GETKEY, ++server->cmd_ident,
3481 1, 1, idp->data, idp->len);
3483 r = silc_calloc(1, sizeof(*r));
3485 silc_free(client_id);
3490 r->sock = silc_socket_dup(sock);
3491 r->packet = silc_packet_context_dup(packet);
3492 silc_server_command_pending(server, SILC_COMMAND_GETKEY,
3494 silc_server_command_resume_resolve, r);
3496 silc_buffer_free(idp);
3498 silc_free(client_id);
3500 } else if (!silc_pkcs_public_key_compare(detached_client->data.public_key,
3501 idata->public_key)) {
3502 /* We require that the connection and resuming authentication data
3503 must be using same key pair. */
3504 SILC_LOG_ERROR(("Resuming attempted with wrong public key, "
3505 "closing connection"));
3506 silc_server_disconnect_remote(server, sock,
3507 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
3508 "Resuming not possible");
3509 if (sock->user_data)
3510 silc_server_free_sock_user_data(server, sock, NULL);
3511 silc_free(client_id);
3515 /* Verify the authentication payload. This has to be successful in
3516 order to allow the resuming */
3518 !silc_auth_verify_data(auth, auth_len, SILC_AUTH_PUBLIC_KEY,
3519 detached_client->data.public_key, 0,
3520 idata->hash, detached_client->id,
3522 SILC_LOG_ERROR(("Client %s (%s) resume authentication failed, "
3523 "closing connection", sock->hostname, sock->ip));
3524 silc_server_disconnect_remote(server, sock,
3525 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
3526 "Resuming not possible");
3527 if (sock->user_data)
3528 silc_server_free_sock_user_data(server, sock, NULL);
3529 silc_free(client_id);
3533 /* Now resume the client to the network */
3535 silc_schedule_task_del_by_context(server->schedule, detached_client);
3536 sock->user_data = detached_client;
3537 detached_client->connection = sock;
3539 /* Take new keys and stuff into use in the old entry */
3540 silc_idlist_del_data(detached_client);
3541 silc_idlist_add_data(detached_client, idata);
3542 detached_client->data.status |= SILC_IDLIST_STATUS_REGISTERED;
3543 detached_client->data.status |= SILC_IDLIST_STATUS_RESUMED;
3544 detached_client->mode &= ~SILC_UMODE_DETACHED;
3545 server->stat.my_detached--;
3547 /* Send the RESUME_CLIENT packet to our primary router so that others
3548 know this client isn't detached anymore. */
3549 buf = silc_buffer_alloc_size(2 + id_len);
3550 silc_buffer_format(buf,
3551 SILC_STR_UI_SHORT(id_len),
3552 SILC_STR_UI_XNSTRING(id_string, id_len),
3555 /* Send to primary router */
3556 silc_server_packet_send(server, SILC_PRIMARY_ROUTE(server),
3557 SILC_PACKET_RESUME_CLIENT, 0,
3558 buf->data, buf->len, TRUE);
3560 /* As router we must deliver this packet directly to the original
3561 server whom this client was earlier. */
3562 if (server->server_type == SILC_ROUTER && detached_client->router &&
3563 detached_client->router->server_type != SILC_ROUTER)
3564 silc_server_packet_send(server, detached_client->router->connection,
3565 SILC_PACKET_RESUME_CLIENT, 0,
3566 buf->data, buf->len, TRUE);
3567 silc_buffer_free(buf);
3569 detached_client->router = NULL;
3571 /* Delete this client entry since we're resuming to old one. */
3572 server->stat.my_clients--;
3573 server->stat.clients--;
3574 if (server->stat.cell_clients)
3575 server->stat.cell_clients--;
3576 silc_server_del_from_watcher_list(server, client);
3577 silc_idlist_del_client(server->local_list, client);
3578 client = detached_client;
3580 /* If the ID is not based in our ID then change it */
3581 if (!SILC_ID_COMPARE(client->id, server->id, server->id->ip.data_len)) {
3582 silc_free(client_id);
3583 while (!silc_id_create_client_id(server, server->id, server->rng,
3584 server->md5hash, client->nickname,
3588 silc_server_disconnect_remote(server, sock,
3589 SILC_STATUS_ERR_BAD_NICKNAME, NULL);
3590 if (sock->user_data)
3591 silc_server_free_sock_user_data(server, sock, NULL);
3594 snprintf(&client->nickname[strlen(client->nickname) - 1], 1,
3601 /* Notify about Client ID change, nickname doesn't actually change. */
3602 silc_server_send_notify_nick_change(server, SILC_PRIMARY_ROUTE(server),
3603 SILC_BROADCAST(server),
3604 client->id, client_id,
3608 /* Resolve users on those channels that client has joined but we
3609 haven't resolved user list yet. */
3610 if (server->server_type == SILC_SERVER && !server->standalone) {
3611 silc_hash_table_list(client->channels, &htl);
3612 while (silc_hash_table_get(&htl, NULL, (void **)&chl)) {
3613 channel = chl->channel;
3614 SILC_LOG_DEBUG(("Resolving users for %s channel",
3615 channel->channel_name));
3616 if (channel->disabled || !channel->users_resolved) {
3617 silc_server_send_command(server, SILC_PRIMARY_ROUTE(server),
3618 SILC_COMMAND_USERS, ++server->cmd_ident,
3619 1, 2, channel->channel_name,
3620 strlen(channel->channel_name));
3623 silc_hash_table_list_reset(&htl);
3626 /* Send the new client ID to the client. After this client may start
3627 receiving other packets, and may start sending packets too. */
3628 silc_server_send_new_id(server, sock, FALSE, client_id, SILC_ID_CLIENT,
3629 silc_id_get_len(client_id, SILC_ID_CLIENT));
3632 /* Send NICK change notify to channels as well. */
3633 SilcBuffer oidp, nidp;
3634 oidp = silc_id_payload_encode(client->id, SILC_ID_CLIENT);
3635 nidp = silc_id_payload_encode(client_id, SILC_ID_CLIENT);
3636 silc_server_send_notify_on_channels(server, NULL, client,
3637 SILC_NOTIFY_TYPE_NICK_CHANGE, 3,
3638 oidp->data, oidp->len,
3639 nidp->data, nidp->len,
3641 strlen(client->nickname));
3642 silc_buffer_free(oidp);
3643 silc_buffer_free(nidp);
3646 /* Add the client again to the ID cache to get it to correct list */
3647 if (!silc_idcache_del_by_context(server->local_list->clients, client))
3648 silc_idcache_del_by_context(server->global_list->clients, client);
3649 silc_free(client->id);
3650 client->id = client_id;
3652 silc_idcache_add(server->local_list->clients, client->nickname,
3653 client->id, client, 0, NULL);
3655 /* Send some nice info to the client */
3656 silc_server_send_connect_notifys(server, sock, client);
3658 /* Send all channel keys of channels the client has joined */
3659 silc_hash_table_list(client->channels, &htl);
3660 while (silc_hash_table_get(&htl, NULL, (void **)&chl)) {
3661 bool created = FALSE;
3662 channel = chl->channel;
3664 if (channel->mode & SILC_CHANNEL_MODE_PRIVKEY)
3667 /* If we don't have channel key, then create one */
3668 if (!channel->channel_key) {
3669 if (!silc_server_create_channel_key(server, channel, 0))
3674 id_string = silc_id_id2str(channel->id, SILC_ID_CHANNEL);
3676 silc_channel_key_payload_encode(silc_id_get_len(channel->id,
3679 strlen(channel->channel_key->
3681 channel->channel_key->cipher->name,
3682 channel->key_len / 8, channel->key);
3683 silc_free(id_string);
3685 /* Send the key packet to client */
3686 silc_server_packet_send(server, sock, SILC_PACKET_CHANNEL_KEY, 0,
3687 keyp->data, keyp->len, FALSE);
3689 if (created && server->server_type == SILC_SERVER)
3690 silc_server_packet_send(server, SILC_PRIMARY_ROUTE(server),
3691 SILC_PACKET_CHANNEL_KEY, 0,
3692 keyp->data, keyp->len, FALSE);
3694 silc_buffer_free(keyp);
3696 silc_hash_table_list_reset(&htl);
3698 } else if (sock->type != SILC_SOCKET_TYPE_CLIENT) {
3699 /* Server or router sent this to us to notify that that a client has
3701 SilcServerEntry server_entry;
3702 SilcServerID *server_id;
3705 SILC_LOG_DEBUG(("Malformed resuming packet"));
3709 /* Get entry to the client, and resolve it if we don't have it. */
3710 detached_client = silc_idlist_find_client_by_id(server->local_list,
3711 client_id, TRUE, NULL);
3712 if (!detached_client) {
3713 detached_client = silc_idlist_find_client_by_id(server->global_list,
3714 client_id, TRUE, NULL);
3715 if (!detached_client) {
3716 SILC_LOG_DEBUG(("Resuming client is unknown"));
3717 silc_free(client_id);
3722 /* Check that the client has not been resumed already because it is
3723 protocol error to attempt to resume more than once. The client
3724 will be killed if this protocol error occurs. */
3725 if (detached_client->data.status & SILC_IDLIST_STATUS_RESUMED &&
3726 !(detached_client->mode & SILC_UMODE_DETACHED)) {
3727 /* The client is clearly attempting to resume more than once and
3728 perhaps playing around by resuming from several different places
3729 at the same time. */
3730 SILC_LOG_DEBUG(("Attempting to re-resume client, killing both"));
3731 silc_server_kill_client(server, detached_client, NULL,
3732 server->id, SILC_ID_SERVER);
3733 silc_free(client_id);
3737 /* Check whether client is detached at all */
3738 if (!(detached_client->mode & SILC_UMODE_DETACHED)) {
3739 SILC_LOG_DEBUG(("Client is not detached"));
3740 silc_free(client_id);
3744 /* Client is detached, and now it is resumed. Remove the detached
3745 mode and mark that it is resumed. */
3746 detached_client->mode &= ~SILC_UMODE_DETACHED;
3747 detached_client->data.status |= SILC_IDLIST_STATUS_RESUMED;
3749 /* Get the new owner of the resumed client */
3750 server_id = silc_id_str2id(packet->src_id, packet->src_id_len,
3751 packet->src_id_type);
3753 silc_free(client_id);
3757 /* Get server entry */
3758 server_entry = silc_idlist_find_server_by_id(server->global_list,
3759 server_id, TRUE, NULL);
3761 if (!server_entry) {
3762 server_entry = silc_idlist_find_server_by_id(server->local_list,
3763 server_id, TRUE, NULL);
3765 if (!server_entry) {
3766 silc_free(server_id);
3767 silc_free(client_id);
3772 if (server->server_type == SILC_ROUTER &&
3773 sock->type == SILC_SOCKET_TYPE_ROUTER &&
3774 server_entry->server_type == SILC_ROUTER)
3777 SILC_LOG_DEBUG(("Resuming detached client"));
3779 /* Change the client to correct list. */
3780 if (!silc_idcache_del_by_context(server->local_list->clients,
3782 silc_idcache_del_by_context(server->global_list->clients,
3784 silc_idcache_add(local && server->server_type == SILC_ROUTER ?
3785 server->local_list->clients :
3786 server->global_list->clients,
3787 detached_client->nickname,
3788 detached_client->id, detached_client, FALSE, NULL);
3789 detached_client->data.status &= ~SILC_IDLIST_STATUS_LOCAL;
3791 /* Change the owner of the client if needed */
3792 if (detached_client->router != server_entry)
3793 detached_client->router = server_entry;
3795 /* Update channel information regarding global clients on channel. */
3796 if (server->server_type == SILC_SERVER) {
3797 silc_hash_table_list(detached_client->channels, &htl);
3798 while (silc_hash_table_get(&htl, NULL, (void **)&chl))
3799 chl->channel->global_users =
3800 silc_server_channel_has_global(chl->channel);
3801 silc_hash_table_list_reset(&htl);
3804 silc_schedule_task_del_by_context(server->schedule, detached_client);
3806 /* If the sender of this packet is server and we are router we need to
3807 broadcast this packet to other routers in the network. */
3808 if (server->server_type == SILC_ROUTER &&
3809 sock->type == SILC_SOCKET_TYPE_SERVER &&
3810 !(packet->flags & SILC_PACKET_FLAG_BROADCAST)) {
3811 SILC_LOG_DEBUG(("Broadcasting received Resume Client packet"));
3812 silc_server_packet_send(server, SILC_PRIMARY_ROUTE(server),
3814 packet->flags | SILC_PACKET_FLAG_BROADCAST,
3815 buffer->data, buffer->len, FALSE);
3816 silc_server_backup_send(server, (SilcServerEntry)sock->user_data,
3817 packet->type, packet->flags,
3818 packet->buffer->data, packet->buffer->len,
3822 silc_free(server_id);
3825 silc_free(client_id);