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 /* Free all client entries that this server owns as they will
1302 become invalid now as well. */
1303 silc_server_remove_clients_by_server(server, server_entry, TRUE);
1304 silc_server_backup_del(server, server_entry);
1306 /* Remove the server entry */
1307 silc_idlist_del_server(local ? server->local_list :
1308 server->global_list, server_entry);
1310 /* Update statistics */
1311 if (server->server_type == SILC_ROUTER)
1312 server->stat.servers--;
1316 case SILC_NOTIFY_TYPE_KICKED:
1318 * Distribute the notify to local clients on the channel
1321 SILC_LOG_DEBUG(("KICKED notify"));
1324 channel_id = silc_id_str2id(packet->dst_id, packet->dst_id_len,
1325 packet->dst_id_type);
1330 /* Get channel entry */
1331 channel = silc_idlist_find_channel_by_id(server->global_list,
1334 channel = silc_idlist_find_channel_by_id(server->local_list,
1337 SILC_LOG_DEBUG(("Notify for unknown channel"));
1338 silc_free(channel_id);
1342 silc_free(channel_id);
1345 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
1348 client_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
1352 /* If the the client is not in local list we check global list */
1353 client = silc_idlist_find_client_by_id(server->global_list,
1354 client_id, TRUE, NULL);
1356 client = silc_idlist_find_client_by_id(server->local_list,
1357 client_id, TRUE, NULL);
1359 silc_free(client_id);
1363 silc_free(client_id);
1365 /* If target is founder they cannot be kicked */
1366 if (!silc_server_client_on_channel(client, channel, &chl))
1368 if (chl->mode & SILC_CHANNEL_UMODE_CHANFO)
1371 /* From protocol version 1.1 we get the kicker's ID as well. */
1372 tmp = silc_argument_get_arg_type(args, 3, &tmp_len);
1374 client_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
1378 /* If the the client is not in local list we check global list */
1379 client2 = silc_idlist_find_client_by_id(server->global_list,
1380 client_id, TRUE, NULL);
1382 client2 = silc_idlist_find_client_by_id(server->local_list,
1383 client_id, TRUE, NULL);
1385 silc_free(client_id);
1389 silc_free(client_id);
1391 /* Kicker must be operator on channel */
1392 if (!silc_server_client_on_channel(client2, channel, &chl))
1394 if (!(chl->mode & SILC_CHANNEL_UMODE_CHANOP) &&
1395 !(chl->mode & SILC_CHANNEL_UMODE_CHANFO)) {
1396 SILC_LOG_DEBUG(("Kicking is not allowed"));
1401 /* Send to channel */
1402 silc_server_packet_send_to_channel(server, sock, channel, packet->type,
1403 FALSE, packet->buffer->data,
1404 packet->buffer->len, FALSE);
1406 /* Remove the client from channel */
1407 silc_server_remove_from_one_channel(server, sock, channel, client, FALSE);
1411 case SILC_NOTIFY_TYPE_KILLED:
1414 * Distribute the notify to local clients on channels
1416 unsigned char *id, *comment;
1417 SilcUInt32 id_len, comment_len;
1419 SILC_LOG_DEBUG(("KILLED notify"));
1422 id = silc_argument_get_arg_type(args, 1, &id_len);
1425 client_id = silc_id_payload_parse_id(id, id_len, NULL);
1429 /* If the the client is not in local list we check global list */
1430 client = silc_idlist_find_client_by_id(server->global_list,
1431 client_id, TRUE, &cache);
1433 client = silc_idlist_find_client_by_id(server->local_list,
1434 client_id, TRUE, &cache);
1436 silc_free(client_id);
1440 silc_free(client_id);
1442 /* If the client is one of ours, then close the connection to the
1443 client now. This removes the client from all channels as well. */
1444 if (packet->dst_id_type == SILC_ID_CLIENT && client->connection) {
1445 sock = client->connection;
1446 silc_server_free_client_data(server, NULL, client, FALSE, NULL);
1447 silc_server_close_connection(server, sock);
1452 comment = silc_argument_get_arg_type(args, 2, &comment_len);
1453 if (comment_len > 128)
1456 /* From protocol version 1.1 we get the killer's ID as well. */
1457 tmp = silc_argument_get_arg_type(args, 3, &tmp_len);
1459 client_id = silc_id_payload_parse_id(tmp, tmp_len, &id_type);
1463 if (id_type == SILC_ID_CLIENT) {
1464 /* If the the client is not in local list we check global list */
1465 client2 = silc_idlist_find_client_by_id(server->global_list,
1466 client_id, TRUE, NULL);
1468 client2 = silc_idlist_find_client_by_id(server->local_list,
1469 client_id, TRUE, NULL);
1471 silc_free(client_id);
1475 silc_free(client_id);
1477 /* Killer must be router operator */
1478 if (server->server_type != SILC_SERVER &&
1479 !(client2->mode & SILC_UMODE_ROUTER_OPERATOR)) {
1480 SILC_LOG_DEBUG(("Killing is not allowed"));
1486 /* Send the notify to local clients on the channels except to the
1487 client who is killed. */
1488 silc_server_send_notify_on_channels(server, client, client,
1489 SILC_NOTIFY_TYPE_KILLED, 3,
1490 id, id_len, comment, comment_len,
1493 /* Remove the client from all channels */
1494 silc_server_remove_from_channels(server, NULL, client, FALSE, NULL,
1497 /* Check if anyone is watching this nickname */
1498 silc_server_check_watcher_list(server, client, NULL,
1499 SILC_NOTIFY_TYPE_KILLED);
1501 /* Update statistics */
1502 server->stat.clients--;
1503 if (server->stat.cell_clients)
1504 server->stat.cell_clients--;
1505 SILC_OPER_STATS_UPDATE(client, server, SILC_UMODE_SERVER_OPERATOR);
1506 SILC_OPER_STATS_UPDATE(client, router, SILC_UMODE_ROUTER_OPERATOR);
1508 if (SILC_IS_LOCAL(client)) {
1509 server->stat.my_clients--;
1510 silc_schedule_task_del_by_context(server->schedule, client);
1511 silc_idlist_del_data(client);
1515 client->data.status &= ~SILC_IDLIST_STATUS_REGISTERED;
1516 cache->expire = SILC_ID_CACHE_EXPIRE_DEF;
1520 case SILC_NOTIFY_TYPE_UMODE_CHANGE:
1522 * Save the mode of the client.
1525 SILC_LOG_DEBUG(("UMODE_CHANGE notify"));
1528 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
1531 client_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
1535 /* Get client entry */
1536 client = silc_idlist_find_client_by_id(server->global_list,
1537 client_id, TRUE, NULL);
1539 client = silc_idlist_find_client_by_id(server->local_list,
1540 client_id, TRUE, NULL);
1542 silc_free(client_id);
1546 silc_free(client_id);
1549 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
1552 SILC_GET32_MSB(mode, tmp);
1554 /* Remove internal resumed flag if client is marked detached now */
1555 if (mode & SILC_UMODE_DETACHED)
1556 client->data.status &= ~SILC_IDLIST_STATUS_RESUMED;
1558 /* Update statistics */
1559 if (server->server_type == SILC_ROUTER) {
1560 if (mode & SILC_UMODE_GONE) {
1561 if (!(client->mode & SILC_UMODE_GONE))
1562 server->stat.aways++;
1564 if (client->mode & SILC_UMODE_GONE)
1565 server->stat.aways--;
1567 if (mode & SILC_UMODE_DETACHED) {
1568 if (!(client->mode & SILC_UMODE_DETACHED))
1569 server->stat.detached++;
1571 if (client->mode & SILC_UMODE_DETACHED)
1572 server->stat.detached--;
1576 /* Change the mode */
1577 client->mode = mode;
1579 /* Check if anyone is watching this nickname */
1580 if (server->server_type == SILC_ROUTER)
1581 silc_server_check_watcher_list(server, client, NULL,
1582 SILC_NOTIFY_TYPE_UMODE_CHANGE);
1586 case SILC_NOTIFY_TYPE_BAN:
1591 SILC_LOG_DEBUG(("BAN notify"));
1593 /* Get Channel ID */
1594 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
1597 channel_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
1601 /* Get channel entry */
1602 channel = silc_idlist_find_channel_by_id(server->global_list,
1605 channel = silc_idlist_find_channel_by_id(server->local_list,
1608 SILC_LOG_DEBUG(("Notify for unknown channel"));
1609 silc_free(channel_id);
1613 silc_free(channel_id);
1615 /* Get the new ban and add it to the ban list */
1616 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
1618 if (!channel->ban_list)
1619 channel->ban_list = silc_calloc(tmp_len + 2,
1620 sizeof(*channel->ban_list));
1622 channel->ban_list = silc_realloc(channel->ban_list,
1623 sizeof(*channel->ban_list) *
1625 strlen(channel->ban_list) + 2));
1626 strncat(channel->ban_list, tmp, tmp_len);
1627 strncat(channel->ban_list, ",", 1);
1630 /* Get the ban to be removed and remove it from the list */
1631 tmp = silc_argument_get_arg_type(args, 3, &tmp_len);
1632 if (tmp && channel->ban_list) {
1633 char *start, *end, *n;
1635 if (!strncmp(channel->ban_list, tmp, strlen(channel->ban_list) - 1)) {
1636 silc_free(channel->ban_list);
1637 channel->ban_list = NULL;
1639 start = strstr(channel->ban_list, tmp);
1640 if (start && strlen(start) >= tmp_len) {
1641 end = start + tmp_len;
1642 n = silc_calloc(strlen(channel->ban_list) - tmp_len, sizeof(*n));
1643 strncat(n, channel->ban_list, start - channel->ban_list);
1644 strncat(n, end + 1, ((channel->ban_list +
1645 strlen(channel->ban_list)) - end) - 1);
1646 silc_free(channel->ban_list);
1647 channel->ban_list = n;
1653 case SILC_NOTIFY_TYPE_ERROR:
1660 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
1661 if (!tmp && tmp_len != 1)
1663 error = (SilcStatus)tmp[0];
1665 SILC_LOG_DEBUG(("ERROR notify (%d)", error));
1667 if (error == SILC_STATUS_ERR_NO_SUCH_CLIENT_ID &&
1668 sock->type == SILC_SOCKET_TYPE_ROUTER) {
1669 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
1671 SILC_LOG_DEBUG(("Received invalid client ID notification, deleting "
1672 "the entry from cache"));
1673 client_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
1676 client = silc_idlist_find_client_by_id(server->global_list,
1677 client_id, FALSE, NULL);
1679 silc_server_remove_from_channels(server, NULL, client, TRUE,
1681 silc_idlist_del_client(server->global_list, client);
1683 silc_free(client_id);
1689 /* Ignore rest of the notify types for now */
1690 case SILC_NOTIFY_TYPE_NONE:
1691 case SILC_NOTIFY_TYPE_MOTD:
1698 silc_notify_payload_free(payload);
1701 void silc_server_notify_list(SilcServer server,
1702 SilcSocketConnection sock,
1703 SilcPacketContext *packet)
1705 SilcPacketContext *new;
1709 SILC_LOG_DEBUG(("Processing Notify List"));
1711 if (sock->type == SILC_SOCKET_TYPE_CLIENT ||
1712 packet->src_id_type != SILC_ID_SERVER)
1715 /* Make copy of the original packet context, except for the actual
1716 data buffer, which we will here now fetch from the original buffer. */
1717 new = silc_packet_context_alloc();
1718 new->type = SILC_PACKET_NOTIFY;
1719 new->flags = packet->flags;
1720 new->src_id = packet->src_id;
1721 new->src_id_len = packet->src_id_len;
1722 new->src_id_type = packet->src_id_type;
1723 new->dst_id = packet->dst_id;
1724 new->dst_id_len = packet->dst_id_len;
1725 new->dst_id_type = packet->dst_id_type;
1727 buffer = silc_buffer_alloc(1024);
1728 new->buffer = buffer;
1730 while (packet->buffer->len) {
1731 SILC_GET16_MSB(len, packet->buffer->data + 2);
1732 if (len > packet->buffer->len)
1735 if (len > buffer->truelen) {
1736 silc_buffer_free(buffer);
1737 buffer = silc_buffer_alloc(1024 + len);
1740 silc_buffer_pull_tail(buffer, len);
1741 silc_buffer_put(buffer, packet->buffer->data, len);
1743 /* Process the Notify */
1744 silc_server_notify(server, sock, new);
1746 silc_buffer_push_tail(buffer, len);
1747 silc_buffer_pull(packet->buffer, len);
1750 silc_buffer_free(buffer);
1754 /* Received private message. This resolves the destination of the message
1755 and sends the packet. This is used by both server and router. If the
1756 destination is our locally connected client this sends the packet to
1757 the client. This may also send the message for further routing if
1758 the destination is not in our server (or router). */
1760 void silc_server_private_message(SilcServer server,
1761 SilcSocketConnection sock,
1762 SilcPacketContext *packet)
1764 SilcSocketConnection dst_sock;
1765 SilcIDListData idata;
1766 SilcClientEntry client;
1768 SILC_LOG_DEBUG(("Start"));
1770 if (packet->src_id_type != SILC_ID_CLIENT ||
1771 packet->dst_id_type != SILC_ID_CLIENT || !packet->dst_id)
1774 /* Get the route to the client */
1775 dst_sock = silc_server_get_client_route(server, packet->dst_id,
1776 packet->dst_id_len, NULL,
1780 unsigned char error;
1782 if (client && client->mode & SILC_UMODE_DETACHED) {
1783 SILC_LOG_DEBUG(("Client is detached, discarding packet"));
1787 /* Send SILC_NOTIFY_TYPE_ERROR to indicate that such destination ID
1788 does not exist or is invalid. */
1789 idp = silc_id_payload_encode_data(packet->dst_id,
1791 packet->dst_id_type);
1795 error = SILC_STATUS_ERR_NO_SUCH_CLIENT_ID;
1796 if (packet->src_id_type == SILC_ID_CLIENT) {
1797 SilcClientID *client_id = silc_id_str2id(packet->src_id,
1799 packet->src_id_type);
1800 silc_server_send_notify_dest(server, sock, FALSE,
1801 client_id, SILC_ID_CLIENT,
1802 SILC_NOTIFY_TYPE_ERROR, 2,
1804 idp->data, idp->len);
1805 silc_free(client_id);
1807 silc_server_send_notify(server, sock, FALSE,
1808 SILC_NOTIFY_TYPE_ERROR, 2,
1810 idp->data, idp->len);
1813 silc_buffer_free(idp);
1817 /* Check whether destination client wishes to receive private messages */
1818 if (client && !(packet->flags & SILC_PACKET_FLAG_PRIVMSG_KEY) &&
1819 client->mode & SILC_UMODE_BLOCK_PRIVMSG) {
1820 SILC_LOG_DEBUG(("Client blocks private messages, discarding packet"));
1824 /* Send the private message */
1825 silc_server_send_private_message(server, dst_sock, idata->send_key,
1826 idata->hmac_send, idata->psn_send++,
1830 /* Received private message key packet.. This packet is never for us. It is to
1831 the client in the packet's destination ID. Sending of this sort of packet
1832 equals sending private message, ie. it is sent point to point from
1833 one client to another. */
1835 void silc_server_private_message_key(SilcServer server,
1836 SilcSocketConnection sock,
1837 SilcPacketContext *packet)
1839 SilcSocketConnection dst_sock;
1840 SilcIDListData idata;
1842 SILC_LOG_DEBUG(("Start"));
1844 if (packet->src_id_type != SILC_ID_CLIENT ||
1845 packet->dst_id_type != SILC_ID_CLIENT)
1848 if (!packet->dst_id)
1851 /* Get the route to the client */
1852 dst_sock = silc_server_get_client_route(server, packet->dst_id,
1853 packet->dst_id_len, NULL,
1858 /* Relay the packet */
1859 silc_server_relay_packet(server, dst_sock, idata->send_key,
1860 idata->hmac_send, idata->psn_send++, packet, FALSE);
1863 /* Processes incoming command reply packet. The command reply packet may
1864 be destined to one of our clients or it may directly for us. We will
1865 call the command reply routine after processing the packet. */
1867 void silc_server_command_reply(SilcServer server,
1868 SilcSocketConnection sock,
1869 SilcPacketContext *packet)
1871 SilcBuffer buffer = packet->buffer;
1872 SilcClientEntry client = NULL;
1873 SilcSocketConnection dst_sock;
1874 SilcIDListData idata;
1875 SilcClientID *id = NULL;
1877 SILC_LOG_DEBUG(("Start"));
1879 /* Source must be server or router */
1880 if (packet->src_id_type != SILC_ID_SERVER &&
1881 sock->type != SILC_SOCKET_TYPE_ROUTER)
1884 if (packet->dst_id_type == SILC_ID_CHANNEL)
1887 if (packet->dst_id_type == SILC_ID_CLIENT) {
1888 /* Destination must be one of ours */
1889 id = silc_id_str2id(packet->dst_id, packet->dst_id_len, SILC_ID_CLIENT);
1892 client = silc_idlist_find_client_by_id(server->local_list, id, TRUE, NULL);
1894 SILC_LOG_ERROR(("Cannot process command reply to unknown client"));
1900 if (packet->dst_id_type == SILC_ID_SERVER) {
1901 /* For now this must be for us */
1902 if (memcmp(packet->dst_id, server->id_string, server->id_string_len)) {
1903 SILC_LOG_ERROR(("Cannot process command reply to unknown server"));
1908 /* Execute command reply locally for the command */
1909 silc_server_command_reply_process(server, sock, buffer);
1911 if (packet->dst_id_type == SILC_ID_CLIENT && client && id) {
1912 /* Relay the packet to the client */
1913 const SilcBufferStruct p;
1915 dst_sock = (SilcSocketConnection)client->connection;
1916 idata = (SilcIDListData)client;
1918 silc_buffer_push(buffer, SILC_PACKET_HEADER_LEN + packet->src_id_len
1919 + packet->dst_id_len + packet->padlen);
1920 if (!silc_packet_send_prepare(dst_sock, 0, 0, buffer->len,
1921 idata->hmac_send, (const SilcBuffer)&p)) {
1922 SILC_LOG_ERROR(("Cannot send packet"));
1925 silc_buffer_put((SilcBuffer)&p, buffer->data, buffer->len);
1927 /* Encrypt packet */
1928 silc_packet_encrypt(idata->send_key, idata->hmac_send, idata->psn_send++,
1929 (SilcBuffer)&p, buffer->len);
1931 /* Send the packet */
1932 silc_server_packet_send_real(server, dst_sock, TRUE);
1938 /* Process received channel message. The message can be originated from
1939 client or server. */
1941 void silc_server_channel_message(SilcServer server,
1942 SilcSocketConnection sock,
1943 SilcPacketContext *packet)
1945 SilcChannelEntry channel = NULL;
1946 SilcChannelID *id = NULL;
1947 void *sender_id = NULL;
1948 SilcClientEntry sender_entry = NULL;
1949 SilcChannelClientEntry chl;
1952 SILC_LOG_DEBUG(("Processing channel message"));
1955 if (packet->dst_id_type != SILC_ID_CHANNEL) {
1956 SILC_LOG_DEBUG(("Received bad message for channel, dropped"));
1960 /* Find channel entry */
1961 id = silc_id_str2id(packet->dst_id, packet->dst_id_len, SILC_ID_CHANNEL);
1964 channel = silc_idlist_find_channel_by_id(server->local_list, id, NULL);
1966 channel = silc_idlist_find_channel_by_id(server->global_list, id, NULL);
1969 unsigned char error;
1971 /* Send SILC_NOTIFY_TYPE_ERROR to indicate that such destination ID
1972 does not exist or is invalid. */
1973 idp = silc_id_payload_encode_data(packet->dst_id,
1975 packet->dst_id_type);
1979 error = SILC_STATUS_ERR_NO_SUCH_CHANNEL_ID;
1980 if (packet->src_id_type == SILC_ID_CLIENT) {
1981 SilcClientID *client_id = silc_id_str2id(packet->src_id,
1983 packet->src_id_type);
1984 silc_server_send_notify_dest(server, sock, FALSE,
1985 client_id, SILC_ID_CLIENT,
1986 SILC_NOTIFY_TYPE_ERROR, 2,
1987 &error, 1, idp->data, idp->len);
1988 silc_free(client_id);
1990 silc_server_send_notify(server, sock, FALSE,
1991 SILC_NOTIFY_TYPE_ERROR, 2,
1992 &error, 1, idp->data, idp->len);
1995 silc_buffer_free(idp);
2000 /* See that this client is on the channel. If the original sender is
2001 not client (as it can be server as well) we don't do the check. */
2002 sender_id = silc_id_str2id(packet->src_id, packet->src_id_len,
2003 packet->src_id_type);
2006 if (packet->src_id_type == SILC_ID_CLIENT) {
2007 sender_entry = silc_idlist_find_client_by_id(server->local_list,
2008 sender_id, TRUE, NULL);
2009 if (!sender_entry) {
2011 sender_entry = silc_idlist_find_client_by_id(server->global_list,
2012 sender_id, TRUE, NULL);
2014 if (!sender_entry || !silc_server_client_on_channel(sender_entry,
2016 SILC_LOG_DEBUG(("Client not on channel"));
2020 /* If channel is moderated check that client is allowed to send
2022 if (channel->mode & SILC_CHANNEL_MODE_SILENCE_USERS &&
2023 !(chl->mode & SILC_CHANNEL_UMODE_CHANOP) &&
2024 !(chl->mode & SILC_CHANNEL_UMODE_CHANFO)) {
2025 SILC_LOG_DEBUG(("Channel is silenced from normal users"));
2028 if (channel->mode & SILC_CHANNEL_MODE_SILENCE_OPERS &&
2029 chl->mode & SILC_CHANNEL_UMODE_CHANOP &&
2030 !(chl->mode & SILC_CHANNEL_UMODE_CHANFO)) {
2031 SILC_LOG_DEBUG(("Channel is silenced from operators"));
2034 if (chl->mode & SILC_CHANNEL_UMODE_QUIET) {
2035 SILC_LOG_DEBUG(("Sender is quieted on the channel"));
2039 /* If the packet is coming from router, but the client entry is local
2040 entry to us then some router is rerouting this to us and it is not
2041 allowed. When the client is local to us it means that we've routed
2042 this packet to network, and now someone is routing it back to us. */
2043 if (server->server_type == SILC_ROUTER &&
2044 sock->type == SILC_SOCKET_TYPE_ROUTER && local) {
2045 SILC_LOG_DEBUG(("Channel message rerouted to the sender, drop it"));
2050 /* Distribute the packet to our local clients. This will send the
2051 packet for further routing as well, if needed. */
2052 silc_server_packet_relay_to_channel(server, sock, channel, sender_id,
2053 packet->src_id_type, sender_entry,
2054 packet->buffer->data,
2055 packet->buffer->len, FALSE);
2058 silc_free(sender_id);
2062 /* Received channel key packet. We distribute the key to all of our locally
2063 connected clients on the channel. */
2065 void silc_server_channel_key(SilcServer server,
2066 SilcSocketConnection sock,
2067 SilcPacketContext *packet)
2069 SilcBuffer buffer = packet->buffer;
2070 SilcChannelEntry channel;
2072 if (packet->src_id_type != SILC_ID_SERVER ||
2073 (server->server_type == SILC_ROUTER &&
2074 sock->type == SILC_SOCKET_TYPE_ROUTER))
2077 /* Save the channel key */
2078 channel = silc_server_save_channel_key(server, buffer, NULL);
2082 /* Distribute the key to everybody who is on the channel. If we are router
2083 we will also send it to locally connected servers. */
2084 silc_server_send_channel_key(server, sock, channel, FALSE);
2086 if (server->server_type != SILC_BACKUP_ROUTER) {
2087 /* Distribute to local cell backup routers. */
2088 silc_server_backup_send(server, (SilcServerEntry)sock->user_data,
2089 SILC_PACKET_CHANNEL_KEY, 0,
2090 buffer->data, buffer->len, FALSE, TRUE);
2094 /* Received New Client packet and processes it. Creates Client ID for the
2095 client. Client becomes registered after calling this functions. */
2097 SilcClientEntry silc_server_new_client(SilcServer server,
2098 SilcSocketConnection sock,
2099 SilcPacketContext *packet)
2101 SilcBuffer buffer = packet->buffer;
2102 SilcClientEntry client;
2103 SilcClientID *client_id;
2104 SilcIDListData idata;
2105 char *username = NULL, *realname = NULL;
2106 SilcUInt16 username_len;
2109 char *hostname, *nickname;
2112 SILC_LOG_DEBUG(("Creating new client"));
2114 if (sock->type != SILC_SOCKET_TYPE_CLIENT)
2117 /* Take client entry */
2118 client = (SilcClientEntry)sock->user_data;
2119 idata = (SilcIDListData)client;
2121 /* Remove the old cache entry. */
2122 if (!silc_idcache_del_by_context(server->local_list->clients, client)) {
2123 SILC_LOG_INFO(("Unauthenticated client attempted to register to network"));
2124 silc_server_disconnect_remote(server, sock,
2125 SILC_STATUS_ERR_NOT_AUTHENTICATED, NULL);
2126 if (sock->user_data)
2127 silc_server_free_sock_user_data(server, sock, NULL);
2131 /* Parse incoming packet */
2132 ret = silc_buffer_unformat(buffer,
2133 SILC_STR_UI16_NSTRING_ALLOC(&username,
2135 SILC_STR_UI16_STRING_ALLOC(&realname),
2138 silc_free(username);
2139 silc_free(realname);
2140 SILC_LOG_ERROR(("Client %s (%s) sent incomplete information, closing "
2141 "connection", sock->hostname, sock->ip));
2142 silc_server_disconnect_remote(server, sock,
2143 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
2145 if (sock->user_data)
2146 silc_server_free_sock_user_data(server, sock, NULL);
2151 silc_free(username);
2152 silc_free(realname);
2153 SILC_LOG_ERROR(("Client %s (%s) did not send its username, closing "
2154 "connection", sock->hostname, sock->ip));
2155 silc_server_disconnect_remote(server, sock,
2156 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
2158 if (sock->user_data)
2159 silc_server_free_sock_user_data(server, sock, NULL);
2163 if (username_len > 128)
2164 username[128] = '\0';
2166 /* Check for bad characters for nickname, and modify the nickname if
2167 it includes those. */
2168 if (silc_server_name_bad_chars(username, username_len)) {
2169 nickname = silc_server_name_modify_bad(username, username_len);
2171 nickname = strdup(username);
2174 /* Make sanity checks for the hostname of the client. If the hostname
2175 is provided in the `username' check that it is the same than the
2176 resolved hostname, or if not resolved the hostname that appears in
2177 the client's public key. If the hostname is not present then put
2178 it from the resolved name or from the public key. */
2179 if (strchr(username, '@')) {
2180 SilcPublicKeyIdentifier pident;
2181 int tlen = strcspn(username, "@");
2182 char *phostname = NULL;
2184 hostname = silc_memdup(username + tlen + 1, strlen(username) - tlen - 1);
2186 if (strcmp(sock->hostname, sock->ip) &&
2187 strcmp(sock->hostname, hostname)) {
2188 silc_free(username);
2189 silc_free(hostname);
2190 silc_free(realname);
2191 SILC_LOG_ERROR(("Client %s (%s) sent incomplete information, closing "
2192 "connection", sock->hostname, sock->ip));
2193 silc_server_disconnect_remote(server, sock,
2194 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
2196 if (sock->user_data)
2197 silc_server_free_sock_user_data(server, sock, NULL);
2201 pident = silc_pkcs_decode_identifier(client->data.public_key->identifier);
2203 phostname = strdup(pident->host);
2204 silc_pkcs_free_identifier(pident);
2207 if (!strcmp(sock->hostname, sock->ip) &&
2208 phostname && strcmp(phostname, hostname)) {
2209 silc_free(username);
2210 silc_free(hostname);
2211 silc_free(phostname);
2212 silc_free(realname);
2213 SILC_LOG_ERROR(("Client %s (%s) sent incomplete information, closing "
2214 "connection", sock->hostname, sock->ip));
2215 silc_server_disconnect_remote(server, sock,
2216 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
2218 if (sock->user_data)
2219 silc_server_free_sock_user_data(server, sock, NULL);
2223 silc_free(phostname);
2225 /* The hostname is not present, add it. */
2227 /* XXX For now we cannot take the host name from the public key since
2228 they are not trusted or we cannot verify them as trusted. Just take
2229 what the resolved name or address is. */
2231 if (strcmp(sock->hostname, sock->ip)) {
2233 newusername = silc_calloc(strlen(username) +
2234 strlen(sock->hostname) + 2,
2235 sizeof(*newusername));
2236 strncat(newusername, username, strlen(username));
2237 strncat(newusername, "@", 1);
2238 strncat(newusername, sock->hostname, strlen(sock->hostname));
2239 silc_free(username);
2240 username = newusername;
2243 SilcPublicKeyIdentifier pident =
2244 silc_pkcs_decode_identifier(client->data.public_key->identifier);
2247 newusername = silc_calloc(strlen(username) +
2248 strlen(pident->host) + 2,
2249 sizeof(*newusername));
2250 strncat(newusername, username, strlen(username));
2251 strncat(newusername, "@", 1);
2252 strncat(newusername, pident->host, strlen(pident->host));
2253 silc_free(username);
2254 username = newusername;
2255 silc_pkcs_free_identifier(pident);
2261 /* Create Client ID */
2262 while (!silc_id_create_client_id(server, server->id, server->rng,
2263 server->md5hash, nickname, &client_id)) {
2266 silc_server_disconnect_remote(server, sock,
2267 SILC_STATUS_ERR_BAD_NICKNAME, NULL);
2268 if (sock->user_data)
2269 silc_server_free_sock_user_data(server, sock, NULL);
2272 snprintf(&nickname[strlen(nickname) - 1], 1, "%d", nickfail);
2275 /* Update client entry */
2276 idata->status |= SILC_IDLIST_STATUS_REGISTERED;
2277 client->nickname = nickname;
2278 client->username = username;
2279 client->userinfo = realname ? realname : strdup(" ");
2280 client->id = client_id;
2281 id_len = silc_id_get_len(client_id, SILC_ID_CLIENT);
2283 /* Add the client again to the ID cache */
2284 silc_idcache_add(server->local_list->clients, client->nickname,
2285 client_id, client, 0, NULL);
2287 /* Notify our router about new client on the SILC network */
2288 silc_server_send_new_id(server, SILC_PRIMARY_ROUTE(server),
2289 SILC_BROADCAST(server), client->id,
2290 SILC_ID_CLIENT, id_len);
2292 /* Distribute to backup routers */
2293 if (server->server_type == SILC_ROUTER) {
2294 SilcBuffer idp = silc_id_payload_encode(client->id, SILC_ID_CLIENT);
2295 silc_server_backup_send(server, NULL, SILC_PACKET_NEW_ID, 0,
2296 idp->data, idp->len, FALSE, TRUE);
2297 silc_buffer_free(idp);
2300 /* Send the new client ID to the client. */
2301 silc_server_send_new_id(server, sock, FALSE, client->id, SILC_ID_CLIENT,
2302 silc_id_get_len(client->id, SILC_ID_CLIENT));
2304 /* Send some nice info to the client */
2305 silc_server_send_connect_notifys(server, sock, client);
2307 /* Check if anyone is watching this nickname */
2308 if (server->server_type == SILC_ROUTER)
2309 silc_server_check_watcher_list(server, client, NULL, 0);
2314 /* Create new server. This processes received New Server packet and
2315 saves the received Server ID. The server is our locally connected
2316 server thus we save all the information and save it to local list.
2317 This funtion can be used by both normal server and router server.
2318 If normal server uses this it means that its router has connected
2319 to the server. If router uses this it means that one of the cell's
2320 servers is connected to the router. */
2322 SilcServerEntry silc_server_new_server(SilcServer server,
2323 SilcSocketConnection sock,
2324 SilcPacketContext *packet)
2326 SilcBuffer buffer = packet->buffer;
2327 SilcServerEntry new_server, server_entry;
2328 SilcServerID *server_id;
2329 SilcIDListData idata;
2330 unsigned char *server_name, *id_string;
2331 SilcUInt16 id_len, name_len;
2335 SILC_LOG_DEBUG(("Creating new server"));
2337 if (sock->type != SILC_SOCKET_TYPE_SERVER &&
2338 sock->type != SILC_SOCKET_TYPE_ROUTER)
2341 /* Take server entry */
2342 new_server = (SilcServerEntry)sock->user_data;
2343 idata = (SilcIDListData)new_server;
2345 /* Remove the old cache entry */
2346 if (!silc_idcache_del_by_context(server->local_list->servers, new_server)) {
2347 if (!silc_idcache_del_by_context(server->global_list->servers,
2349 SILC_LOG_INFO(("Unauthenticated %s attempted to register to "
2350 "network", (sock->type == SILC_SOCKET_TYPE_SERVER ?
2351 "server" : "router")));
2352 silc_server_disconnect_remote(server, sock,
2353 SILC_STATUS_ERR_NOT_AUTHENTICATED, NULL);
2354 if (sock->user_data)
2355 silc_server_free_sock_user_data(server, sock, NULL);
2361 /* Parse the incoming packet */
2362 ret = silc_buffer_unformat(buffer,
2363 SILC_STR_UI16_NSTRING_ALLOC(&id_string, &id_len),
2364 SILC_STR_UI16_NSTRING_ALLOC(&server_name,
2368 silc_free(id_string);
2369 silc_free(server_name);
2370 silc_server_disconnect_remote(server, sock,
2371 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
2373 if (sock->user_data)
2374 silc_server_free_sock_user_data(server, sock, NULL);
2378 if (id_len > buffer->len) {
2379 silc_free(id_string);
2380 silc_free(server_name);
2381 silc_server_disconnect_remote(server, sock,
2382 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
2384 if (sock->user_data)
2385 silc_server_free_sock_user_data(server, sock, NULL);
2390 server_name[255] = '\0';
2393 server_id = silc_id_str2id(id_string, id_len, SILC_ID_SERVER);
2395 silc_free(id_string);
2396 silc_free(server_name);
2397 silc_server_disconnect_remote(server, sock,
2398 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
2400 if (sock->user_data)
2401 silc_server_free_sock_user_data(server, sock, NULL);
2404 silc_free(id_string);
2406 /* Check for valid server ID */
2407 if (!silc_id_is_valid_server_id(server, server_id, sock)) {
2408 SILC_LOG_INFO(("Invalid server ID sent by %s (%s)",
2409 sock->ip, sock->hostname));
2410 silc_server_disconnect_remote(server, sock,
2411 SILC_STATUS_ERR_BAD_SERVER_ID, NULL);
2412 if (sock->user_data)
2413 silc_server_free_sock_user_data(server, sock, NULL);
2414 silc_free(server_name);
2418 /* Check that we do not have this ID already */
2419 server_entry = silc_idlist_find_server_by_id(server->local_list,
2420 server_id, TRUE, NULL);
2422 silc_idcache_del_by_context(server->local_list->servers, server_entry);
2424 server_entry = silc_idlist_find_server_by_id(server->global_list,
2425 server_id, TRUE, NULL);
2427 silc_idcache_del_by_context(server->global_list->servers, server_entry);
2430 /* Update server entry */
2431 idata->status |= SILC_IDLIST_STATUS_REGISTERED;
2432 new_server->server_name = server_name;
2433 new_server->id = server_id;
2435 SILC_LOG_DEBUG(("New server id(%s)",
2436 silc_id_render(server_id, SILC_ID_SERVER)));
2438 /* Add again the entry to the ID cache. */
2439 silc_idcache_add(local ? server->local_list->servers :
2440 server->global_list->servers, server_name, server_id,
2441 new_server, 0, NULL);
2443 /* Distribute the information about new server in the SILC network
2444 to our router. If we are normal server we won't send anything
2445 since this connection must be our router connection. */
2446 if (server->server_type == SILC_ROUTER && !server->standalone &&
2447 SILC_PRIMARY_ROUTE(server) != sock)
2448 silc_server_send_new_id(server, SILC_PRIMARY_ROUTE(server),
2449 TRUE, new_server->id, SILC_ID_SERVER,
2450 silc_id_get_len(server_id, SILC_ID_SERVER));
2452 if (server->server_type == SILC_ROUTER) {
2453 /* Distribute to backup routers */
2454 SilcBuffer idp = silc_id_payload_encode(new_server->id, SILC_ID_SERVER);
2455 silc_server_backup_send(server, NULL, SILC_PACKET_NEW_ID, 0,
2456 idp->data, idp->len, FALSE, TRUE);
2457 silc_buffer_free(idp);
2460 server->stat.cell_servers++;
2463 /* Check whether this router connection has been replaced by an
2464 backup router. If it has been then we'll disable the server and will
2465 ignore everything it will send until the backup router resuming
2466 protocol has been completed. */
2467 if (sock->type == SILC_SOCKET_TYPE_ROUTER &&
2468 silc_server_backup_replaced_get(server, server_id, NULL)) {
2469 /* Send packet to the server indicating that it cannot use this
2470 connection as it has been replaced by backup router. */
2471 SilcBuffer packet = silc_buffer_alloc(2);
2472 silc_buffer_pull_tail(packet, SILC_BUFFER_END(packet));
2473 silc_buffer_format(packet,
2474 SILC_STR_UI_CHAR(SILC_SERVER_BACKUP_REPLACED),
2475 SILC_STR_UI_CHAR(0),
2477 silc_server_packet_send(server, sock,
2478 SILC_PACKET_RESUME_ROUTER, 0,
2479 packet->data, packet->len, TRUE);
2480 silc_buffer_free(packet);
2482 /* Mark the router disabled. The data sent earlier will go but nothing
2483 after this does not go to this connection. */
2484 idata->status |= SILC_IDLIST_STATUS_DISABLED;
2486 /* If it is router announce our stuff to it. */
2487 if (sock->type == SILC_SOCKET_TYPE_ROUTER &&
2488 server->server_type == SILC_ROUTER) {
2489 silc_server_announce_servers(server, FALSE, 0, sock);
2490 silc_server_announce_clients(server, 0, sock);
2491 silc_server_announce_channels(server, 0, sock);
2494 /* By default the servers connected to backup router are disabled
2495 until backup router has become the primary */
2496 if (server->server_type == SILC_BACKUP_ROUTER &&
2497 sock->type == SILC_SOCKET_TYPE_SERVER)
2498 idata->status |= SILC_IDLIST_STATUS_DISABLED;
2504 /* Processes incoming New ID packet. New ID Payload is used to distribute
2505 information about newly registered clients and servers. */
2507 static void silc_server_new_id_real(SilcServer server,
2508 SilcSocketConnection sock,
2509 SilcPacketContext *packet,
2512 SilcBuffer buffer = packet->buffer;
2514 SilcServerEntry router, server_entry;
2515 SilcSocketConnection router_sock;
2520 SILC_LOG_DEBUG(("Processing new ID"));
2522 if (sock->type == SILC_SOCKET_TYPE_CLIENT ||
2523 server->server_type == SILC_SERVER ||
2524 packet->src_id_type != SILC_ID_SERVER)
2527 idp = silc_id_payload_parse(buffer->data, buffer->len);
2531 id_type = silc_id_payload_get_type(idp);
2533 /* Normal server cannot have other normal server connections */
2534 server_entry = (SilcServerEntry)sock->user_data;
2535 if (id_type == SILC_ID_SERVER && sock->type == SILC_SOCKET_TYPE_SERVER &&
2536 server_entry->server_type == SILC_SERVER)
2539 id = silc_id_payload_get_id(idp);
2543 /* If the packet is coming from server then use the sender as the
2544 origin of the the packet. If it came from router then check the real
2545 sender of the packet and use that as the origin. */
2546 if (sock->type == SILC_SOCKET_TYPE_SERVER) {
2547 id_list = server->local_list;
2549 router = sock->user_data;
2551 /* If the sender is backup router and ID is server (and we are not
2552 backup router) then switch the entry to global list. */
2553 if (server_entry->server_type == SILC_BACKUP_ROUTER &&
2554 id_type == SILC_ID_SERVER &&
2555 server->id_entry->server_type != SILC_BACKUP_ROUTER) {
2556 id_list = server->global_list;
2557 router_sock = server->router ? SILC_PRIMARY_ROUTE(server) : sock;
2560 void *sender_id = silc_id_str2id(packet->src_id, packet->src_id_len,
2561 packet->src_id_type);
2562 router = silc_idlist_find_server_by_id(server->global_list,
2563 sender_id, TRUE, NULL);
2565 router = silc_idlist_find_server_by_id(server->local_list,
2566 sender_id, TRUE, NULL);
2567 silc_free(sender_id);
2569 id_list = server->global_list;
2576 case SILC_ID_CLIENT:
2578 SilcClientEntry entry;
2580 /* Check that we do not have this client already */
2581 entry = silc_idlist_find_client_by_id(server->global_list,
2582 id, server->server_type,
2585 entry = silc_idlist_find_client_by_id(server->local_list,
2586 id, server->server_type,
2589 SILC_LOG_DEBUG(("Ignoring client that we already have"));
2593 SILC_LOG_DEBUG(("New client id(%s) from [%s] %s",
2594 silc_id_render(id, SILC_ID_CLIENT),
2595 sock->type == SILC_SOCKET_TYPE_SERVER ?
2596 "Server" : "Router", sock->hostname));
2598 /* As a router we keep information of all global information in our
2599 global list. Cell wide information however is kept in the local
2601 entry = silc_idlist_add_client(id_list, NULL, NULL, NULL,
2602 id, router, NULL, 0);
2604 SILC_LOG_ERROR(("Could not add new client to the ID Cache"));
2606 /* Inform the sender that the ID is not usable */
2607 silc_server_send_notify_signoff(server, sock, FALSE, id, NULL);
2610 entry->nickname = NULL;
2611 entry->data.status |= SILC_IDLIST_STATUS_REGISTERED;
2613 if (sock->type == SILC_SOCKET_TYPE_SERVER)
2614 server->stat.cell_clients++;
2615 server->stat.clients++;
2617 /* Check if anyone is watching this nickname */
2618 if (server->server_type == SILC_ROUTER && id_list == server->local_list)
2619 silc_server_check_watcher_list(server, entry, NULL, 0);
2623 case SILC_ID_SERVER:
2625 SilcServerEntry entry;
2627 /* If the ID is mine, ignore it. */
2628 if (SILC_ID_SERVER_COMPARE(id, server->id)) {
2629 SILC_LOG_DEBUG(("Ignoring my own ID as new ID"));
2633 /* If the ID is the sender's ID, ignore it (we have it already) */
2634 if (SILC_ID_SERVER_COMPARE(id, router->id)) {
2635 SILC_LOG_DEBUG(("Ignoring sender's own ID"));
2639 /* Check that we do not have this server already */
2640 entry = silc_idlist_find_server_by_id(server->global_list,
2641 id, server->server_type,
2644 entry = silc_idlist_find_server_by_id(server->local_list,
2645 id, server->server_type,
2648 SILC_LOG_DEBUG(("Ignoring server that we already have"));
2652 SILC_LOG_DEBUG(("New server id(%s) from [%s] %s",
2653 silc_id_render(id, SILC_ID_SERVER),
2654 sock->type == SILC_SOCKET_TYPE_SERVER ?
2655 "Server" : "Router", sock->hostname));
2657 /* As a router we keep information of all global information in our
2658 global list. Cell wide information however is kept in the local
2660 entry = silc_idlist_add_server(id_list, NULL, 0, id, router,
2663 SILC_LOG_ERROR(("Could not add new server to the ID Cache"));
2666 entry->data.status |= SILC_IDLIST_STATUS_REGISTERED;
2668 if (sock->type == SILC_SOCKET_TYPE_SERVER)
2669 server->stat.cell_servers++;
2670 server->stat.servers++;
2674 case SILC_ID_CHANNEL:
2675 SILC_LOG_ERROR(("Channel cannot be registered with NEW_ID packet"));
2684 /* If the sender of this packet is server and we are router we need to
2685 broadcast this packet to other routers in the network. */
2686 if (broadcast && server->server_type == SILC_ROUTER &&
2687 sock->type == SILC_SOCKET_TYPE_SERVER &&
2688 !(packet->flags & SILC_PACKET_FLAG_BROADCAST)) {
2689 SILC_LOG_DEBUG(("Broadcasting received New ID packet"));
2690 silc_server_packet_send(server, SILC_PRIMARY_ROUTE(server),
2692 packet->flags | SILC_PACKET_FLAG_BROADCAST,
2693 buffer->data, buffer->len, FALSE);
2694 silc_server_backup_send(server, (SilcServerEntry)sock->user_data,
2695 packet->type, packet->flags,
2696 packet->buffer->data, packet->buffer->len,
2701 silc_id_payload_free(idp);
2705 /* Processes incoming New ID packet. New ID Payload is used to distribute
2706 information about newly registered clients and servers. */
2708 void silc_server_new_id(SilcServer server, SilcSocketConnection sock,
2709 SilcPacketContext *packet)
2711 silc_server_new_id_real(server, sock, packet, TRUE);
2714 /* Receoved New Id List packet, list of New ID payloads inside one
2715 packet. Process the New ID payloads one by one. */
2717 void silc_server_new_id_list(SilcServer server, SilcSocketConnection sock,
2718 SilcPacketContext *packet)
2720 SilcPacketContext *new_id;
2724 SILC_LOG_DEBUG(("Processing New ID List"));
2726 if (sock->type == SILC_SOCKET_TYPE_CLIENT ||
2727 packet->src_id_type != SILC_ID_SERVER)
2730 /* If the sender of this packet is server and we are router we need to
2731 broadcast this packet to other routers in the network. Broadcast
2732 this list packet instead of multiple New ID packets. */
2733 if (server->server_type == SILC_ROUTER &&
2734 sock->type == SILC_SOCKET_TYPE_SERVER &&
2735 !(packet->flags & SILC_PACKET_FLAG_BROADCAST)) {
2736 SILC_LOG_DEBUG(("Broadcasting received New ID List packet"));
2737 silc_server_packet_send(server, SILC_PRIMARY_ROUTE(server),
2739 packet->flags | SILC_PACKET_FLAG_BROADCAST,
2740 packet->buffer->data,
2741 packet->buffer->len, FALSE);
2742 silc_server_backup_send(server, (SilcServerEntry)sock->user_data,
2743 packet->type, packet->flags,
2744 packet->buffer->data, packet->buffer->len,
2748 /* Make copy of the original packet context, except for the actual
2749 data buffer, which we will here now fetch from the original buffer. */
2750 new_id = silc_packet_context_alloc();
2751 new_id->type = SILC_PACKET_NEW_ID;
2752 new_id->flags = packet->flags;
2753 new_id->src_id = packet->src_id;
2754 new_id->src_id_len = packet->src_id_len;
2755 new_id->src_id_type = packet->src_id_type;
2756 new_id->dst_id = packet->dst_id;
2757 new_id->dst_id_len = packet->dst_id_len;
2758 new_id->dst_id_type = packet->dst_id_type;
2760 idp = silc_buffer_alloc(256);
2761 new_id->buffer = idp;
2763 while (packet->buffer->len) {
2764 SILC_GET16_MSB(id_len, packet->buffer->data + 2);
2765 if ((id_len > packet->buffer->len) ||
2766 (id_len > idp->truelen))
2769 silc_buffer_pull_tail(idp, 4 + id_len);
2770 silc_buffer_put(idp, packet->buffer->data, 4 + id_len);
2772 /* Process the New ID */
2773 silc_server_new_id_real(server, sock, new_id, FALSE);
2775 silc_buffer_push_tail(idp, 4 + id_len);
2776 silc_buffer_pull(packet->buffer, 4 + id_len);
2779 silc_buffer_free(idp);
2783 /* Received New Channel packet. Information about new channels in the
2784 network are distributed using this packet. Save the information about
2785 the new channel. This usually comes from router but also normal server
2786 can send this to notify channels it has when it connects to us. */
2788 void silc_server_new_channel(SilcServer server,
2789 SilcSocketConnection sock,
2790 SilcPacketContext *packet)
2792 SilcChannelPayload payload;
2793 SilcChannelID *channel_id;
2795 SilcUInt32 name_len;
2798 SilcServerEntry server_entry;
2799 SilcChannelEntry channel;
2801 if (sock->type == SILC_SOCKET_TYPE_CLIENT ||
2802 packet->src_id_type != SILC_ID_SERVER ||
2803 server->server_type == SILC_SERVER)
2806 /* Parse the channel payload */
2807 payload = silc_channel_payload_parse(packet->buffer->data,
2808 packet->buffer->len);
2812 /* Get the channel ID */
2813 channel_id = silc_channel_get_id_parse(payload);
2815 silc_channel_payload_free(payload);
2819 channel_name = silc_channel_get_name(payload, &name_len);
2821 channel_name[255] = '\0';
2823 id = silc_channel_get_id(payload, &id_len);
2825 server_entry = (SilcServerEntry)sock->user_data;
2827 if (sock->type == SILC_SOCKET_TYPE_ROUTER) {
2828 /* Add the channel to global list as it is coming from router. It
2829 cannot be our own channel as it is coming from router. */
2831 /* Check that we don't already have this channel */
2832 channel = silc_idlist_find_channel_by_name(server->local_list,
2833 channel_name, NULL);
2835 channel = silc_idlist_find_channel_by_name(server->global_list,
2836 channel_name, NULL);
2838 SILC_LOG_DEBUG(("New channel id(%s) from [Router] %s",
2839 silc_id_render(channel_id, SILC_ID_CHANNEL),
2843 silc_idlist_add_channel(server->global_list, strdup(channel_name),
2844 0, channel_id, sock->user_data, NULL, NULL, 0);
2847 channel->disabled = TRUE;
2849 server->stat.channels++;
2850 if (server->server_type == SILC_ROUTER)
2851 channel->users_resolved = TRUE;
2854 /* The channel is coming from our server, thus it is in our cell
2855 we will add it to our local list. */
2858 SILC_LOG_DEBUG(("Channel id(%s) from [Server] %s",
2859 silc_id_render(channel_id, SILC_ID_CHANNEL),
2862 /* Check that we don't already have this channel */
2863 channel = silc_idlist_find_channel_by_name(server->local_list,
2864 channel_name, NULL);
2866 channel = silc_idlist_find_channel_by_name(server->global_list,
2867 channel_name, NULL);
2869 /* If the channel does not exist, then create it. This creates a new
2870 key to the channel as well that we will send to the server. */
2872 SILC_LOG_DEBUG(("Channel is new to us"));
2874 /* The protocol says that the Channel ID's IP address must be based
2875 on the router's IP address. Check whether the ID is based in our
2876 IP and if it is not then create a new ID and enforce the server
2877 to switch the ID. */
2878 if (server_entry->server_type != SILC_BACKUP_ROUTER &&
2879 !SILC_ID_COMPARE(channel_id, server->id, server->id->ip.data_len)) {
2881 SILC_LOG_DEBUG(("Forcing the server to change Channel ID"));
2883 if (silc_id_create_channel_id(server, server->id, server->rng, &tmp)) {
2884 silc_server_send_notify_channel_change(server, sock, FALSE,
2886 silc_free(channel_id);
2891 /* Create the channel with the provided Channel ID */
2892 channel = silc_server_create_new_channel_with_id(server, NULL, NULL,
2896 silc_channel_payload_free(payload);
2897 silc_free(channel_id);
2900 channel->disabled = TRUE;
2901 channel->mode = silc_channel_get_mode(payload);
2903 /* Send the new channel key to the server */
2904 id = silc_id_id2str(channel->id, SILC_ID_CHANNEL);
2905 id_len = silc_id_get_len(channel->id, SILC_ID_CHANNEL);
2906 chk = silc_channel_key_payload_encode(id_len, id,
2907 strlen(channel->channel_key->
2909 channel->channel_key->cipher->name,
2910 channel->key_len / 8,
2912 silc_server_packet_send(server, sock, SILC_PACKET_CHANNEL_KEY, 0,
2913 chk->data, chk->len, FALSE);
2914 silc_buffer_free(chk);
2916 /* The channel exist by that name, check whether the ID's match.
2917 If they don't then we'll force the server to use the ID we have.
2918 We also create a new key for the channel. */
2919 SilcBuffer modes = NULL, users = NULL, users_modes = NULL;
2921 SILC_LOG_DEBUG(("Channel already exists"));
2923 if (!SILC_ID_CHANNEL_COMPARE(channel_id, channel->id)) {
2924 /* They don't match, send CHANNEL_CHANGE notify to the server to
2925 force the ID change. */
2926 SILC_LOG_DEBUG(("Forcing the server to change Channel ID"));
2927 silc_server_send_notify_channel_change(server, sock, FALSE,
2928 channel_id, channel->id);
2930 /* Wait that server re-announces this channel */
2934 #if 0 /* We will announce our CMODE anyway for this channel, so no need
2935 to check it (implicit enforce). */
2937 /* If the mode is different from what we have then enforce the
2939 mode = silc_channel_get_mode(payload);
2940 if (channel->mode != mode) {
2941 SILC_LOG_DEBUG(("Forcing the server to change channel mode"));
2942 silc_server_send_notify_cmode(server, sock, FALSE, channel,
2943 channel->mode, server->id,
2944 SILC_ID_SERVER, channel->cipher,
2946 channel->passphrase,
2947 channel->founder_key);
2951 /* Create new key for the channel and send it to the server and
2952 everybody else possibly on the channel. */
2953 if (!(channel->mode & SILC_CHANNEL_MODE_PRIVKEY)) {
2954 if (!silc_server_create_channel_key(server, channel, 0))
2957 /* Send to the channel */
2958 silc_server_send_channel_key(server, sock, channel, FALSE);
2959 id = silc_id_id2str(channel->id, SILC_ID_CHANNEL);
2960 id_len = silc_id_get_len(channel->id, SILC_ID_CHANNEL);
2962 /* Send to the server */
2963 chk = silc_channel_key_payload_encode(id_len, id,
2964 strlen(channel->channel_key->
2966 channel->channel_key->
2968 channel->key_len / 8,
2970 silc_server_packet_send(server, sock, SILC_PACKET_CHANNEL_KEY, 0,
2971 chk->data, chk->len, FALSE);
2972 silc_buffer_free(chk);
2976 silc_free(channel_id);
2978 /* Update statistics */
2979 server->stat.channels++;
2980 server->stat.cell_channels++;
2982 /* Since the channel is coming from server and we also know about it
2983 then send the JOIN notify to the server so that it see's our
2984 users on the channel "joining" the channel. */
2985 silc_server_announce_get_channel_users(server, channel, &modes, &users,
2988 silc_buffer_push(users, users->data - users->head);
2989 silc_server_packet_send(server, sock,
2990 SILC_PACKET_NOTIFY, SILC_PACKET_FLAG_LIST,
2991 users->data, users->len, FALSE);
2992 silc_buffer_free(users);
2995 silc_buffer_push(modes, modes->data - modes->head);
2996 silc_server_packet_send_dest(server, sock,
2997 SILC_PACKET_NOTIFY, SILC_PACKET_FLAG_LIST,
2998 channel->id, SILC_ID_CHANNEL,
2999 modes->data, modes->len, FALSE);
3000 silc_buffer_free(modes);
3003 silc_buffer_push(users_modes, users_modes->data - users_modes->head);
3004 silc_server_packet_send_dest(server, sock,
3005 SILC_PACKET_NOTIFY, SILC_PACKET_FLAG_LIST,
3006 channel->id, SILC_ID_CHANNEL,
3008 users_modes->len, FALSE);
3009 silc_buffer_free(users_modes);
3011 if (channel->topic) {
3012 silc_server_send_notify_topic_set(server, sock,
3013 server->server_type == SILC_ROUTER ?
3014 TRUE : FALSE, channel,
3015 server->id, SILC_ID_SERVER,
3021 silc_channel_payload_free(payload);
3024 /* Received New Channel List packet, list of New Channel List payloads inside
3025 one packet. Process the New Channel payloads one by one. */
3027 void silc_server_new_channel_list(SilcServer server,
3028 SilcSocketConnection sock,
3029 SilcPacketContext *packet)
3031 SilcPacketContext *new;
3033 SilcUInt16 len1, len2;
3035 SILC_LOG_DEBUG(("Processing New Channel List"));
3037 if (sock->type == SILC_SOCKET_TYPE_CLIENT ||
3038 packet->src_id_type != SILC_ID_SERVER ||
3039 server->server_type == SILC_SERVER)
3042 /* If the sender of this packet is server and we are router we need to
3043 broadcast this packet to other routers in the network. Broadcast
3044 this list packet instead of multiple New Channel packets. */
3045 if (server->server_type == SILC_ROUTER &&
3046 sock->type == SILC_SOCKET_TYPE_SERVER &&
3047 !(packet->flags & SILC_PACKET_FLAG_BROADCAST)) {
3048 SILC_LOG_DEBUG(("Broadcasting received New Channel List packet"));
3049 silc_server_packet_send(server, SILC_PRIMARY_ROUTE(server),
3051 packet->flags | SILC_PACKET_FLAG_BROADCAST,
3052 packet->buffer->data,
3053 packet->buffer->len, FALSE);
3054 silc_server_backup_send(server, (SilcServerEntry)sock->user_data,
3055 packet->type, packet->flags,
3056 packet->buffer->data, packet->buffer->len,
3060 /* Make copy of the original packet context, except for the actual
3061 data buffer, which we will here now fetch from the original buffer. */
3062 new = silc_packet_context_alloc();
3063 new->type = SILC_PACKET_NEW_CHANNEL;
3064 new->flags = packet->flags;
3065 new->src_id = packet->src_id;
3066 new->src_id_len = packet->src_id_len;
3067 new->src_id_type = packet->src_id_type;
3068 new->dst_id = packet->dst_id;
3069 new->dst_id_len = packet->dst_id_len;
3070 new->dst_id_type = packet->dst_id_type;
3072 buffer = silc_buffer_alloc(512);
3073 new->buffer = buffer;
3075 while (packet->buffer->len) {
3076 SILC_GET16_MSB(len1, packet->buffer->data);
3077 if ((len1 > packet->buffer->len) ||
3078 (len1 > buffer->truelen))
3081 SILC_GET16_MSB(len2, packet->buffer->data + 2 + len1);
3082 if ((len2 > packet->buffer->len) ||
3083 (len2 > buffer->truelen))
3086 silc_buffer_pull_tail(buffer, 8 + len1 + len2);
3087 silc_buffer_put(buffer, packet->buffer->data, 8 + len1 + len2);
3089 /* Process the New Channel */
3090 silc_server_new_channel(server, sock, new);
3092 silc_buffer_push_tail(buffer, 8 + len1 + len2);
3093 silc_buffer_pull(packet->buffer, 8 + len1 + len2);
3096 silc_buffer_free(buffer);
3100 /* Received key agreement packet. This packet is never for us. It is to
3101 the client in the packet's destination ID. Sending of this sort of packet
3102 equals sending private message, ie. it is sent point to point from
3103 one client to another. */
3105 void silc_server_key_agreement(SilcServer server,
3106 SilcSocketConnection sock,
3107 SilcPacketContext *packet)
3109 SilcSocketConnection dst_sock;
3110 SilcIDListData idata;
3112 SILC_LOG_DEBUG(("Start"));
3114 if (packet->src_id_type != SILC_ID_CLIENT ||
3115 packet->dst_id_type != SILC_ID_CLIENT)
3118 if (!packet->dst_id)
3121 /* Get the route to the client */
3122 dst_sock = silc_server_get_client_route(server, packet->dst_id,
3123 packet->dst_id_len, NULL,
3128 /* Relay the packet */
3129 silc_server_relay_packet(server, dst_sock, idata->send_key,
3130 idata->hmac_send, idata->psn_send++,
3134 /* Received connection auth request packet that is used during connection
3135 phase to resolve the mandatory authentication method. This packet can
3136 actually be received at anytime but usually it is used only during
3137 the connection authentication phase. Now, protocol says that this packet
3138 can come from client or server, however, we support only this coming
3139 from client and expect that server always knows what authentication
3142 void silc_server_connection_auth_request(SilcServer server,
3143 SilcSocketConnection sock,
3144 SilcPacketContext *packet)
3146 SilcServerConfigClient *client = NULL;
3147 SilcUInt16 conn_type;
3149 SilcAuthMethod auth_meth = SILC_AUTH_NONE;
3151 if (packet->src_id_type && packet->src_id_type != SILC_ID_CLIENT) {
3152 SILC_LOG_DEBUG(("Request not from client"));
3156 /* Parse the payload */
3157 ret = silc_buffer_unformat(packet->buffer,
3158 SILC_STR_UI_SHORT(&conn_type),
3159 SILC_STR_UI_SHORT(NULL),
3164 if (conn_type != SILC_SOCKET_TYPE_CLIENT)
3167 /* Get the authentication method for the client */
3168 auth_meth = SILC_AUTH_NONE;
3169 client = silc_server_config_find_client(server, sock->ip);
3171 client = silc_server_config_find_client(server, sock->hostname);
3173 if (client->passphrase) {
3174 if (client->publickeys && !server->config->prefer_passphrase_auth)
3175 auth_meth = SILC_AUTH_PUBLIC_KEY;
3177 auth_meth = SILC_AUTH_PASSWORD;
3178 } else if (client->publickeys)
3179 auth_meth = SILC_AUTH_PUBLIC_KEY;
3182 SILC_LOG_DEBUG(("Authentication method is [%s]",
3183 (auth_meth == SILC_AUTH_NONE ? "None" :
3184 auth_meth == SILC_AUTH_PASSWORD ? "Passphrase" :
3185 "Digital signatures")));
3187 /* Send it back to the client */
3188 silc_server_send_connection_auth_request(server, sock, conn_type, auth_meth);
3191 /* Received REKEY packet. The sender of the packet wants to regenerate
3192 its session keys. This starts the REKEY protocol. */
3194 void silc_server_rekey(SilcServer server,
3195 SilcSocketConnection sock,
3196 SilcPacketContext *packet)
3198 SilcProtocol protocol;
3199 SilcServerRekeyInternalContext *proto_ctx;
3200 SilcIDListData idata = (SilcIDListData)sock->user_data;
3202 SILC_LOG_DEBUG(("Start"));
3204 /* Allocate internal protocol context. This is sent as context
3206 proto_ctx = silc_calloc(1, sizeof(*proto_ctx));
3207 proto_ctx->server = (void *)server;
3208 proto_ctx->sock = sock;
3209 proto_ctx->responder = TRUE;
3210 proto_ctx->pfs = idata->rekey->pfs;
3212 /* Perform rekey protocol. Will call the final callback after the
3213 protocol is over. */
3214 silc_protocol_alloc(SILC_PROTOCOL_SERVER_REKEY,
3215 &protocol, proto_ctx, silc_server_rekey_final);
3216 sock->protocol = protocol;
3218 if (proto_ctx->pfs == FALSE)
3219 /* Run the protocol */
3220 silc_protocol_execute(protocol, server->schedule, 0, 0);
3223 /* Received file transger packet. This packet is never for us. It is to
3224 the client in the packet's destination ID. Sending of this sort of packet
3225 equals sending private message, ie. it is sent point to point from
3226 one client to another. */
3228 void silc_server_ftp(SilcServer server,
3229 SilcSocketConnection sock,
3230 SilcPacketContext *packet)
3232 SilcSocketConnection dst_sock;
3233 SilcIDListData idata;
3235 SILC_LOG_DEBUG(("Start"));
3237 if (packet->src_id_type != SILC_ID_CLIENT ||
3238 packet->dst_id_type != SILC_ID_CLIENT)
3241 if (!packet->dst_id)
3244 /* Get the route to the client */
3245 dst_sock = silc_server_get_client_route(server, packet->dst_id,
3246 packet->dst_id_len, NULL,
3251 /* Relay the packet */
3252 silc_server_relay_packet(server, dst_sock, idata->send_key,
3253 idata->hmac_send, idata->psn_send++,
3259 SilcSocketConnection sock;
3260 SilcPacketContext *packet;
3262 } *SilcServerResumeResolve;
3264 SILC_SERVER_CMD_FUNC(resume_resolve)
3266 SilcServerResumeResolve r = (SilcServerResumeResolve)context;
3267 SilcServer server = r->server;
3268 SilcSocketConnection sock = r->sock;
3269 SilcServerCommandReplyContext reply = context2;
3270 SilcClientEntry client;
3272 SILC_LOG_DEBUG(("Start"));
3274 if (!reply || !silc_command_get_status(reply->payload, NULL, NULL)) {
3275 SILC_LOG_ERROR(("Client %s (%s) tried to resume unknown client, "
3276 "closing connection", sock->hostname, sock->ip));
3277 silc_server_disconnect_remote(server, sock,
3278 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
3279 "Resuming not possible");
3280 if (sock->user_data)
3281 silc_server_free_sock_user_data(server, sock, NULL);
3285 if (reply && silc_command_get(reply->payload) == SILC_COMMAND_WHOIS) {
3286 /* Get entry to the client, and resolve it if we don't have it. */
3287 client = silc_idlist_find_client_by_id(server->local_list,
3288 r->data, TRUE, NULL);
3290 client = silc_idlist_find_client_by_id(server->global_list,
3291 r->data, TRUE, NULL);
3293 SILC_LOG_ERROR(("Client %s (%s) tried to resume unknown client, "
3294 "closing connection", sock->hostname, sock->ip));
3295 silc_server_disconnect_remote(server, sock,
3296 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
3297 "Resuming not possible");
3298 if (sock->user_data)
3299 silc_server_free_sock_user_data(server, sock, NULL);
3304 if (!(client->mode & SILC_UMODE_DETACHED)) {
3305 SILC_LOG_ERROR(("Client %s (%s) tried to resume un-detached client, "
3306 "closing connection", sock->hostname, sock->ip));
3307 silc_server_disconnect_remote(server, sock,
3308 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
3309 "Resuming not possible");
3310 if (sock->user_data)
3311 silc_server_free_sock_user_data(server, sock, NULL);
3316 /* Reprocess the packet */
3317 silc_server_resume_client(server, sock, r->packet);
3320 silc_socket_free(r->sock);
3321 silc_packet_context_free(r->packet);
3326 /* Received client resuming packet. This is used to resume detached
3327 client session. It can be sent by the client who wishes to resume
3328 but this is also sent by servers and routers to notify other routers
3329 that the client is not detached anymore. */
3331 void silc_server_resume_client(SilcServer server,
3332 SilcSocketConnection sock,
3333 SilcPacketContext *packet)
3335 SilcBuffer buffer = packet->buffer, buf;
3336 SilcIDListData idata;
3337 SilcClientEntry detached_client;
3338 SilcClientID *client_id = NULL;
3339 unsigned char *id_string, *auth = NULL;
3340 SilcUInt16 id_len, auth_len = 0;
3341 int ret, nickfail = 0;
3342 bool resolved, local, nick_change = FALSE, resolve = FALSE;
3343 SilcChannelEntry channel;
3344 SilcHashTableList htl;
3345 SilcChannelClientEntry chl;
3346 SilcServerResumeResolve r;
3348 ret = silc_buffer_unformat(buffer,
3349 SILC_STR_UI16_NSTRING(&id_string, &id_len),
3352 client_id = silc_id_str2id(id_string, id_len, SILC_ID_CLIENT);
3354 if (sock->type == SILC_SOCKET_TYPE_CLIENT) {
3355 /* Client send this and is attempting to resume to old client session */
3356 SilcClientEntry client;
3360 silc_buffer_pull(buffer, 2 + id_len);
3361 auth = buffer->data;
3362 auth_len = buffer->len;
3363 silc_buffer_push(buffer, 2 + id_len);
3366 if (!client_id || auth_len < 128) {
3367 SILC_LOG_ERROR(("Client %s (%s) sent incomplete resume information, "
3368 "closing connection", sock->hostname, sock->ip));
3369 silc_server_disconnect_remote(server, sock,
3370 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
3371 "Resuming not possible");
3372 if (sock->user_data)
3373 silc_server_free_sock_user_data(server, sock, NULL);
3374 silc_free(client_id);
3378 /* Take client entry of this connection */
3379 client = (SilcClientEntry)sock->user_data;
3380 idata = (SilcIDListData)client;
3382 /* Get entry to the client, and resolve it if we don't have it. */
3383 detached_client = silc_server_get_client_resolve(server, client_id, FALSE,
3385 if (!detached_client) {
3387 /* The client info is being resolved. Reprocess this packet after
3388 receiving the reply to the query. */
3389 SILC_LOG_DEBUG(("Resolving client"));
3390 r = silc_calloc(1, sizeof(*r));
3394 r->sock = silc_socket_dup(sock);
3395 r->packet = silc_packet_context_dup(packet);
3396 r->data = client_id;
3397 silc_server_command_pending(server, SILC_COMMAND_WHOIS,
3399 silc_server_command_resume_resolve, r);
3401 SILC_LOG_ERROR(("Client %s (%s) tried to resume unknown client, "
3402 "closing connection", sock->hostname, sock->ip));
3403 silc_server_disconnect_remote(server, sock,
3404 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
3405 "Resuming not possible");
3406 if (sock->user_data)
3407 silc_server_free_sock_user_data(server, sock, NULL);
3408 silc_free(client_id);
3413 if (!(detached_client->mode & SILC_UMODE_DETACHED))
3415 if (!silc_hash_table_count(detached_client->channels) &&
3416 detached_client->router)
3418 if (!detached_client->nickname)
3422 if (server->server_type == SILC_SERVER && !server->standalone) {
3423 /* The client info is being resolved. Reprocess this packet after
3424 receiving the reply to the query. */
3425 SILC_LOG_DEBUG(("Resolving client info"));
3426 silc_server_get_client_resolve(server, client_id, TRUE, NULL);
3427 r = silc_calloc(1, sizeof(*r));
3431 r->sock = silc_socket_dup(sock);
3432 r->packet = silc_packet_context_dup(packet);
3433 r->data = client_id;
3434 silc_server_command_pending(server, SILC_COMMAND_WHOIS,
3436 silc_server_command_resume_resolve, r);
3439 if (server->server_type == SILC_SERVER) {
3440 SILC_LOG_ERROR(("Client %s (%s) tried to resume un-detached client, "
3441 "closing connection", sock->hostname, sock->ip));
3442 silc_server_disconnect_remote(server, sock,
3443 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
3444 "Resuming not possible");
3445 if (sock->user_data)
3446 silc_server_free_sock_user_data(server, sock, NULL);
3447 silc_free(client_id);
3452 /* Check that we have the public key of the client, if not then we must
3453 resolve it first. */
3454 if (!detached_client->data.public_key) {
3455 if (server->server_type == SILC_SERVER && server->standalone) {
3456 SILC_LOG_ERROR(("Detached client's public key not present, "
3457 "closing connection"));
3458 silc_server_disconnect_remote(server, sock,
3459 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
3460 "Resuming not possible");
3461 if (sock->user_data)
3462 silc_server_free_sock_user_data(server, sock, NULL);
3463 silc_free(client_id);
3465 /* We must retrieve the detached client's public key by sending
3466 GETKEY command. Reprocess this packet after receiving the key */
3467 SilcBuffer idp = silc_id_payload_encode(client_id, SILC_ID_CLIENT);
3468 SilcSocketConnection dest_sock =
3469 silc_server_get_client_route(server, NULL, 0, client_id, NULL, NULL);
3471 SILC_LOG_DEBUG(("Resolving client public key"));
3473 silc_server_send_command(server, dest_sock ? dest_sock :
3474 SILC_PRIMARY_ROUTE(server),
3475 SILC_COMMAND_GETKEY, ++server->cmd_ident,
3476 1, 1, idp->data, idp->len);
3478 r = silc_calloc(1, sizeof(*r));
3480 silc_free(client_id);
3485 r->sock = silc_socket_dup(sock);
3486 r->packet = silc_packet_context_dup(packet);
3487 silc_server_command_pending(server, SILC_COMMAND_GETKEY,
3489 silc_server_command_resume_resolve, r);
3491 silc_buffer_free(idp);
3493 silc_free(client_id);
3495 } else if (!silc_pkcs_public_key_compare(detached_client->data.public_key,
3496 idata->public_key)) {
3497 /* We require that the connection and resuming authentication data
3498 must be using same key pair. */
3499 SILC_LOG_ERROR(("Resuming attempted with wrong public key, "
3500 "closing connection"));
3501 silc_server_disconnect_remote(server, sock,
3502 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
3503 "Resuming not possible");
3504 if (sock->user_data)
3505 silc_server_free_sock_user_data(server, sock, NULL);
3506 silc_free(client_id);
3510 /* Verify the authentication payload. This has to be successful in
3511 order to allow the resuming */
3513 !silc_auth_verify_data(auth, auth_len, SILC_AUTH_PUBLIC_KEY,
3514 detached_client->data.public_key, 0,
3515 idata->hash, detached_client->id,
3517 SILC_LOG_ERROR(("Client %s (%s) resume authentication failed, "
3518 "closing connection", sock->hostname, sock->ip));
3519 silc_server_disconnect_remote(server, sock,
3520 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
3521 "Resuming not possible");
3522 if (sock->user_data)
3523 silc_server_free_sock_user_data(server, sock, NULL);
3524 silc_free(client_id);
3528 /* Now resume the client to the network */
3530 silc_schedule_task_del_by_context(server->schedule, detached_client);
3531 sock->user_data = detached_client;
3532 detached_client->connection = sock;
3534 /* Take new keys and stuff into use in the old entry */
3535 silc_idlist_del_data(detached_client);
3536 silc_idlist_add_data(detached_client, idata);
3537 detached_client->data.status |= SILC_IDLIST_STATUS_REGISTERED;
3538 detached_client->data.status |= SILC_IDLIST_STATUS_RESUMED;
3539 detached_client->mode &= ~SILC_UMODE_DETACHED;
3540 server->stat.my_detached--;
3542 /* Send the RESUME_CLIENT packet to our primary router so that others
3543 know this client isn't detached anymore. */
3544 buf = silc_buffer_alloc_size(2 + id_len);
3545 silc_buffer_format(buf,
3546 SILC_STR_UI_SHORT(id_len),
3547 SILC_STR_UI_XNSTRING(id_string, id_len),
3550 /* Send to primary router */
3551 silc_server_packet_send(server, SILC_PRIMARY_ROUTE(server),
3552 SILC_PACKET_RESUME_CLIENT, 0,
3553 buf->data, buf->len, TRUE);
3555 /* As router we must deliver this packet directly to the original
3556 server whom this client was earlier. */
3557 if (server->server_type == SILC_ROUTER && detached_client->router &&
3558 detached_client->router->server_type != SILC_ROUTER)
3559 silc_server_packet_send(server, detached_client->router->connection,
3560 SILC_PACKET_RESUME_CLIENT, 0,
3561 buf->data, buf->len, TRUE);
3562 silc_buffer_free(buf);
3564 detached_client->router = NULL;
3566 /* Delete this client entry since we're resuming to old one. */
3567 server->stat.my_clients--;
3568 server->stat.clients--;
3569 if (server->stat.cell_clients)
3570 server->stat.cell_clients--;
3571 silc_server_del_from_watcher_list(server, client);
3572 silc_idlist_del_client(server->local_list, client);
3573 client = detached_client;
3575 /* If the ID is not based in our ID then change it */
3576 if (!SILC_ID_COMPARE(client->id, server->id, server->id->ip.data_len)) {
3577 silc_free(client_id);
3578 while (!silc_id_create_client_id(server, server->id, server->rng,
3579 server->md5hash, client->nickname,
3583 silc_server_disconnect_remote(server, sock,
3584 SILC_STATUS_ERR_BAD_NICKNAME, NULL);
3585 if (sock->user_data)
3586 silc_server_free_sock_user_data(server, sock, NULL);
3589 snprintf(&client->nickname[strlen(client->nickname) - 1], 1,
3596 /* Notify about Client ID change, nickname doesn't actually change. */
3597 silc_server_send_notify_nick_change(server, SILC_PRIMARY_ROUTE(server),
3598 SILC_BROADCAST(server),
3599 client->id, client_id,
3603 /* Resolve users on those channels that client has joined but we
3604 haven't resolved user list yet. */
3605 if (server->server_type == SILC_SERVER && !server->standalone) {
3606 silc_hash_table_list(client->channels, &htl);
3607 while (silc_hash_table_get(&htl, NULL, (void **)&chl)) {
3608 channel = chl->channel;
3609 SILC_LOG_DEBUG(("Resolving users for %s channel",
3610 channel->channel_name));
3611 if (channel->disabled || !channel->users_resolved) {
3612 silc_server_send_command(server, SILC_PRIMARY_ROUTE(server),
3613 SILC_COMMAND_USERS, ++server->cmd_ident,
3614 1, 2, channel->channel_name,
3615 strlen(channel->channel_name));
3618 silc_hash_table_list_reset(&htl);
3621 /* Send the new client ID to the client. After this client may start
3622 receiving other packets, and may start sending packets too. */
3623 silc_server_send_new_id(server, sock, FALSE, client_id, SILC_ID_CLIENT,
3624 silc_id_get_len(client_id, SILC_ID_CLIENT));
3627 /* Send NICK change notify to channels as well. */
3628 SilcBuffer oidp, nidp;
3629 oidp = silc_id_payload_encode(client->id, SILC_ID_CLIENT);
3630 nidp = silc_id_payload_encode(client_id, SILC_ID_CLIENT);
3631 silc_server_send_notify_on_channels(server, NULL, client,
3632 SILC_NOTIFY_TYPE_NICK_CHANGE, 3,
3633 oidp->data, oidp->len,
3634 nidp->data, nidp->len,
3636 strlen(client->nickname));
3637 silc_buffer_free(oidp);
3638 silc_buffer_free(nidp);
3641 /* Add the client again to the ID cache to get it to correct list */
3642 if (!silc_idcache_del_by_context(server->local_list->clients, client))
3643 silc_idcache_del_by_context(server->global_list->clients, client);
3644 silc_free(client->id);
3645 client->id = client_id;
3647 silc_idcache_add(server->local_list->clients, client->nickname,
3648 client->id, client, 0, NULL);
3650 /* Send some nice info to the client */
3651 silc_server_send_connect_notifys(server, sock, client);
3653 /* Send all channel keys of channels the client has joined */
3654 silc_hash_table_list(client->channels, &htl);
3655 while (silc_hash_table_get(&htl, NULL, (void **)&chl)) {
3656 bool created = FALSE;
3657 channel = chl->channel;
3659 if (channel->mode & SILC_CHANNEL_MODE_PRIVKEY)
3662 /* If we don't have channel key, then create one */
3663 if (!channel->channel_key) {
3664 if (!silc_server_create_channel_key(server, channel, 0))
3669 id_string = silc_id_id2str(channel->id, SILC_ID_CHANNEL);
3671 silc_channel_key_payload_encode(silc_id_get_len(channel->id,
3674 strlen(channel->channel_key->
3676 channel->channel_key->cipher->name,
3677 channel->key_len / 8, channel->key);
3678 silc_free(id_string);
3680 /* Send the key packet to client */
3681 silc_server_packet_send(server, sock, SILC_PACKET_CHANNEL_KEY, 0,
3682 keyp->data, keyp->len, FALSE);
3684 if (created && server->server_type == SILC_SERVER)
3685 silc_server_packet_send(server, SILC_PRIMARY_ROUTE(server),
3686 SILC_PACKET_CHANNEL_KEY, 0,
3687 keyp->data, keyp->len, FALSE);
3689 silc_buffer_free(keyp);
3691 silc_hash_table_list_reset(&htl);
3693 } else if (sock->type != SILC_SOCKET_TYPE_CLIENT) {
3694 /* Server or router sent this to us to notify that that a client has
3696 SilcServerEntry server_entry;
3697 SilcServerID *server_id;
3700 SILC_LOG_DEBUG(("Malformed resuming packet"));
3704 /* Get entry to the client, and resolve it if we don't have it. */
3705 detached_client = silc_idlist_find_client_by_id(server->local_list,
3706 client_id, TRUE, NULL);
3707 if (!detached_client) {
3708 detached_client = silc_idlist_find_client_by_id(server->global_list,
3709 client_id, TRUE, NULL);
3710 if (!detached_client) {
3711 SILC_LOG_DEBUG(("Resuming client is unknown"));
3712 silc_free(client_id);
3717 /* Check that the client has not been resumed already because it is
3718 protocol error to attempt to resume more than once. The client
3719 will be killed if this protocol error occurs. */
3720 if (detached_client->data.status & SILC_IDLIST_STATUS_RESUMED &&
3721 !(detached_client->mode & SILC_UMODE_DETACHED)) {
3722 /* The client is clearly attempting to resume more than once and
3723 perhaps playing around by resuming from several different places
3724 at the same time. */
3725 SILC_LOG_DEBUG(("Attempting to re-resume client, killing both"));
3726 silc_server_kill_client(server, detached_client, NULL,
3727 server->id, SILC_ID_SERVER);
3728 silc_free(client_id);
3732 /* Check whether client is detached at all */
3733 if (!(detached_client->mode & SILC_UMODE_DETACHED)) {
3734 SILC_LOG_DEBUG(("Client is not detached"));
3735 silc_free(client_id);
3739 /* Client is detached, and now it is resumed. Remove the detached
3740 mode and mark that it is resumed. */
3741 detached_client->mode &= ~SILC_UMODE_DETACHED;
3742 detached_client->data.status |= SILC_IDLIST_STATUS_RESUMED;
3744 /* Get the new owner of the resumed client */
3745 server_id = silc_id_str2id(packet->src_id, packet->src_id_len,
3746 packet->src_id_type);
3748 silc_free(client_id);
3752 /* Get server entry */
3753 server_entry = silc_idlist_find_server_by_id(server->global_list,
3754 server_id, TRUE, NULL);
3756 if (!server_entry) {
3757 server_entry = silc_idlist_find_server_by_id(server->local_list,
3758 server_id, TRUE, NULL);
3760 if (!server_entry) {
3761 silc_free(server_id);
3762 silc_free(client_id);
3767 if (server->server_type == SILC_ROUTER &&
3768 sock->type == SILC_SOCKET_TYPE_ROUTER &&
3769 server_entry->server_type == SILC_ROUTER)
3772 SILC_LOG_DEBUG(("Resuming detached client"));
3774 /* Change the client to correct list. */
3775 if (!silc_idcache_del_by_context(server->local_list->clients,
3777 silc_idcache_del_by_context(server->global_list->clients,
3779 silc_idcache_add(local && server->server_type == SILC_ROUTER ?
3780 server->local_list->clients :
3781 server->global_list->clients,
3782 detached_client->nickname,
3783 detached_client->id, detached_client, FALSE, NULL);
3784 detached_client->data.status &= ~SILC_IDLIST_STATUS_LOCAL;
3786 /* Change the owner of the client if needed */
3787 if (detached_client->router != server_entry)
3788 detached_client->router = server_entry;
3790 /* Update channel information regarding global clients on channel. */
3791 if (server->server_type == SILC_SERVER) {
3792 silc_hash_table_list(detached_client->channels, &htl);
3793 while (silc_hash_table_get(&htl, NULL, (void **)&chl))
3794 chl->channel->global_users =
3795 silc_server_channel_has_global(chl->channel);
3796 silc_hash_table_list_reset(&htl);
3799 silc_schedule_task_del_by_context(server->schedule, detached_client);
3801 /* If the sender of this packet is server and we are router we need to
3802 broadcast this packet to other routers in the network. */
3803 if (server->server_type == SILC_ROUTER &&
3804 sock->type == SILC_SOCKET_TYPE_SERVER &&
3805 !(packet->flags & SILC_PACKET_FLAG_BROADCAST)) {
3806 SILC_LOG_DEBUG(("Broadcasting received Resume Client packet"));
3807 silc_server_packet_send(server, SILC_PRIMARY_ROUTE(server),
3809 packet->flags | SILC_PACKET_FLAG_BROADCAST,
3810 buffer->data, buffer->len, FALSE);
3811 silc_server_backup_send(server, (SilcServerEntry)sock->user_data,
3812 packet->type, packet->flags,
3813 packet->buffer->data, packet->buffer->len,
3817 silc_free(server_id);
3820 silc_free(client_id);