5 Author: Pekka Riikonen <priikone@silcnet.org>
7 Copyright (C) 1997 - 2002 Pekka Riikonen
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
21 * Server packet routines to handle received packets.
25 #include "serverincludes.h"
26 #include "server_internal.h"
28 /* Received notify packet. Server can receive notify packets from router.
29 Server then relays the notify messages to clients if needed. */
31 void silc_server_notify(SilcServer server,
32 SilcSocketConnection sock,
33 SilcPacketContext *packet)
35 SilcNotifyPayload payload;
37 SilcArgumentPayload args;
38 SilcChannelID *channel_id = NULL, *channel_id2;
39 SilcClientID *client_id, *client_id2;
40 SilcServerID *server_id;
42 SilcChannelEntry channel = NULL;
43 SilcClientEntry client = NULL, client2 = NULL;
44 SilcServerEntry server_entry = NULL;
45 SilcChannelClientEntry chl;
46 SilcIDCacheEntry cache;
47 SilcHashTableList htl;
53 SILC_LOG_DEBUG(("Start"));
55 if (sock->type == SILC_SOCKET_TYPE_CLIENT ||
56 packet->src_id_type != SILC_ID_SERVER)
62 /* If the packet is destined directly to a client then relay the packet
63 before processing it. */
64 if (packet->dst_id_type == SILC_ID_CLIENT) {
66 SilcSocketConnection dst_sock;
68 /* Get the route to the client */
69 dst_sock = silc_server_get_client_route(server, packet->dst_id,
70 packet->dst_id_len, NULL,
73 /* Relay the packet */
74 silc_server_relay_packet(server, dst_sock, idata->send_key,
75 idata->hmac_send, idata->psn_send++,
79 /* Parse the Notify Payload */
80 payload = silc_notify_payload_parse(packet->buffer->data,
85 /* If we are router and this packet is not already broadcast packet
86 we will broadcast it. The sending socket really cannot be router or
87 the router is buggy. If this packet is coming from router then it must
88 have the broadcast flag set already and we won't do anything. */
89 if (!server->standalone && server->server_type == SILC_ROUTER &&
90 sock->type == SILC_SOCKET_TYPE_SERVER &&
91 !(packet->flags & SILC_PACKET_FLAG_BROADCAST)) {
92 SILC_LOG_DEBUG(("Broadcasting received Notify packet"));
93 if (packet->dst_id_type == SILC_ID_CHANNEL) {
94 /* Packet is destined to channel */
95 channel_id = silc_id_str2id(packet->dst_id, packet->dst_id_len,
100 silc_server_packet_send_dest(server, server->router->connection,
102 packet->flags | SILC_PACKET_FLAG_BROADCAST,
103 channel_id, SILC_ID_CHANNEL,
104 packet->buffer->data, packet->buffer->len,
106 silc_server_backup_send_dest(server, (SilcServerEntry)sock->user_data,
107 packet->type, packet->flags,
108 channel_id, SILC_ID_CHANNEL,
109 packet->buffer->data, packet->buffer->len,
112 /* Packet is destined to client or server */
113 silc_server_packet_send(server, server->router->connection,
115 packet->flags | SILC_PACKET_FLAG_BROADCAST,
116 packet->buffer->data, packet->buffer->len,
118 silc_server_backup_send(server, (SilcServerEntry)sock->user_data,
119 packet->type, packet->flags,
120 packet->buffer->data, packet->buffer->len,
125 type = silc_notify_get_type(payload);
126 args = silc_notify_get_args(payload);
131 case SILC_NOTIFY_TYPE_JOIN:
133 * Distribute the notify to local clients on the channel
135 SILC_LOG_DEBUG(("JOIN notify"));
138 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
141 channel_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
145 /* Get channel entry */
146 channel = silc_idlist_find_channel_by_id(server->global_list,
149 channel = silc_idlist_find_channel_by_id(server->local_list,
152 silc_free(channel_id);
156 silc_free(channel_id);
159 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
162 client_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
166 /* If the the client is not in local list we check global list (ie. the
167 channel will be global channel) and if it does not exist then create
168 entry for the client. */
169 client = silc_idlist_find_client_by_id(server->global_list,
170 client_id, server->server_type,
173 client = silc_idlist_find_client_by_id(server->local_list,
174 client_id, server->server_type,
177 /* If router did not find the client the it is bogus */
178 if (server->server_type != SILC_SERVER)
182 silc_idlist_add_client(server->global_list, NULL, NULL, NULL,
183 silc_id_dup(client_id, SILC_ID_CLIENT),
184 sock->user_data, NULL, 0);
186 SILC_LOG_ERROR(("Could not add new client to the ID Cache"));
187 silc_free(client_id);
191 client->data.status |= SILC_IDLIST_STATUS_REGISTERED;
195 /* Do not process the notify if the client is not registered */
196 if (!(client->data.status & SILC_IDLIST_STATUS_REGISTERED))
199 /* Do not add client to channel if it is there already */
200 if (silc_server_client_on_channel(client, channel, NULL)) {
201 SILC_LOG_DEBUG(("Client already on channel"));
205 /* Send to channel */
206 silc_server_packet_send_to_channel(server, sock, channel, packet->type,
207 FALSE, packet->buffer->data,
208 packet->buffer->len, FALSE);
210 if (server->server_type != SILC_ROUTER &&
211 sock->type == SILC_SOCKET_TYPE_ROUTER)
212 /* The channel is global now */
213 channel->global_users = TRUE;
215 SILC_LOG_DEBUG(("Joining to channel %s", channel->channel_name));
217 /* JOIN the global client to the channel (local clients (if router
218 created the channel) is joined in the pending JOIN command). */
219 chl = silc_calloc(1, sizeof(*chl));
220 chl->client = client;
221 chl->channel = channel;
223 /* If this is the first one on the channel then it is the founder of
225 if (!silc_hash_table_count(channel->user_list))
226 chl->mode = (SILC_CHANNEL_UMODE_CHANOP | SILC_CHANNEL_UMODE_CHANFO);
228 silc_hash_table_add(channel->user_list, client, chl);
229 silc_hash_table_add(client->channels, channel, chl);
230 silc_free(client_id);
231 channel->user_count++;
232 channel->disabled = FALSE;
236 case SILC_NOTIFY_TYPE_LEAVE:
238 * Distribute the notify to local clients on the channel
240 SILC_LOG_DEBUG(("LEAVE notify"));
243 channel_id = silc_id_str2id(packet->dst_id, packet->dst_id_len,
244 packet->dst_id_type);
249 /* Get channel entry */
250 channel = silc_idlist_find_channel_by_id(server->global_list,
253 channel = silc_idlist_find_channel_by_id(server->local_list,
256 silc_free(channel_id);
262 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
264 silc_free(channel_id);
267 client_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
269 silc_free(channel_id);
273 /* Get client entry */
274 client = silc_idlist_find_client_by_id(server->global_list,
275 client_id, TRUE, NULL);
277 client = silc_idlist_find_client_by_id(server->local_list,
278 client_id, TRUE, NULL);
280 silc_free(client_id);
281 silc_free(channel_id);
285 silc_free(client_id);
287 /* Check if on channel */
288 if (!silc_server_client_on_channel(client, channel, NULL))
291 /* Send the leave notify to channel */
292 silc_server_packet_send_to_channel(server, sock, channel, packet->type,
293 FALSE, packet->buffer->data,
294 packet->buffer->len, FALSE);
296 /* Remove the user from channel */
297 silc_server_remove_from_one_channel(server, sock, channel, client, FALSE);
300 case SILC_NOTIFY_TYPE_SIGNOFF:
302 * Distribute the notify to local clients on the channel
304 SILC_LOG_DEBUG(("SIGNOFF notify"));
307 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
310 client_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
314 /* Get client entry */
315 client = silc_idlist_find_client_by_id(server->global_list,
316 client_id, TRUE, &cache);
318 client = silc_idlist_find_client_by_id(server->local_list,
319 client_id, TRUE, &cache);
321 silc_free(client_id);
325 silc_free(client_id);
327 /* Get signoff message */
328 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
332 /* Update statistics */
333 server->stat.clients--;
334 if (server->stat.cell_clients)
335 server->stat.cell_clients--;
336 SILC_OPER_STATS_UPDATE(client, server, SILC_UMODE_SERVER_OPERATOR);
337 SILC_OPER_STATS_UPDATE(client, router, SILC_UMODE_ROUTER_OPERATOR);
339 /* Remove the client from all channels. */
340 silc_server_remove_from_channels(server, NULL, client, TRUE, tmp, FALSE);
342 /* Check if anyone is watching this nickname */
343 if (server->server_type == SILC_ROUTER)
344 silc_server_check_watcher_list(server, client, NULL,
345 SILC_NOTIFY_TYPE_SIGNOFF);
347 client->data.status &= ~SILC_IDLIST_STATUS_REGISTERED;
348 cache->expire = SILC_ID_CACHE_EXPIRE_DEF;
351 case SILC_NOTIFY_TYPE_TOPIC_SET:
353 * Distribute the notify to local clients on the channel
356 SILC_LOG_DEBUG(("TOPIC SET notify"));
359 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
362 client_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
366 /* Get client entry */
367 client = silc_idlist_find_client_by_id(server->global_list,
368 client_id, TRUE, &cache);
370 client = silc_idlist_find_client_by_id(server->local_list,
371 client_id, TRUE, &cache);
373 silc_free(client_id);
377 silc_free(client_id);
380 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
382 silc_free(channel_id);
387 channel_id = silc_id_str2id(packet->dst_id, packet->dst_id_len,
388 packet->dst_id_type);
393 /* Get channel entry */
394 channel = silc_idlist_find_channel_by_id(server->global_list,
397 channel = silc_idlist_find_channel_by_id(server->local_list,
400 silc_free(channel_id);
405 if (channel->topic && !strcmp(channel->topic, tmp))
408 /* Get user's channel entry and check that topic set is allowed. */
409 if (!silc_server_client_on_channel(client, channel, &chl))
411 if (channel->mode & SILC_CHANNEL_MODE_TOPIC &&
412 !(chl->mode & SILC_CHANNEL_UMODE_CHANOP) &&
413 !(chl->mode & SILC_CHANNEL_UMODE_CHANFO)) {
414 SILC_LOG_DEBUG(("Topic change is not allowed"));
418 /* Change the topic */
419 silc_free(channel->topic);
420 channel->topic = strdup(tmp);
422 /* Send the same notify to the channel */
423 silc_server_packet_send_to_channel(server, sock, channel, packet->type,
424 FALSE, packet->buffer->data,
425 packet->buffer->len, FALSE);
426 silc_free(channel_id);
429 case SILC_NOTIFY_TYPE_NICK_CHANGE:
432 * Distribute the notify to local clients on the channel
434 unsigned char *id, *id2;
436 SilcUInt32 nickname_len;
438 SILC_LOG_DEBUG(("NICK CHANGE notify"));
440 /* Get old client ID */
441 id = silc_argument_get_arg_type(args, 1, &tmp_len);
444 client_id = silc_id_payload_parse_id(id, tmp_len, NULL);
448 /* Get new client ID */
449 id2 = silc_argument_get_arg_type(args, 2, &tmp_len);
452 client_id2 = silc_id_payload_parse_id(id2, tmp_len, NULL);
456 SILC_LOG_DEBUG(("Old Client ID id(%s)",
457 silc_id_render(client_id, SILC_ID_CLIENT)));
458 SILC_LOG_DEBUG(("New Client ID id(%s)",
459 silc_id_render(client_id2, SILC_ID_CLIENT)));
461 /* From protocol version 1.1 we also get the new nickname */
462 nickname = silc_argument_get_arg_type(args, 3, &nickname_len);;
464 /* Replace the Client ID */
465 client = silc_idlist_replace_client_id(server,
466 server->global_list, client_id,
467 client_id2, nickname);
469 client = silc_idlist_replace_client_id(server,
470 server->local_list, client_id,
471 client_id2, nickname);
474 /* Send the NICK_CHANGE notify type to local clients on the channels
475 this client is joined to. */
476 silc_server_send_notify_on_channels(server, client, client,
477 SILC_NOTIFY_TYPE_NICK_CHANGE, 3,
478 id, tmp_len, id2, tmp_len,
483 silc_free(client_id);
485 silc_free(client_id2);
489 case SILC_NOTIFY_TYPE_CMODE_CHANGE:
491 * Distribute the notify to local clients on the channel
494 SILC_LOG_DEBUG(("CMODE CHANGE notify"));
497 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
500 client_id = silc_id_payload_parse_id(tmp, tmp_len, &id_type);
504 /* Get client entry */
505 if (id_type == SILC_ID_CLIENT) {
506 client = silc_idlist_find_client_by_id(server->global_list,
507 client_id, TRUE, &cache);
509 client = silc_idlist_find_client_by_id(server->local_list,
510 client_id, TRUE, &cache);
512 silc_free(client_id);
516 silc_free(client_id);
520 channel_id = silc_id_str2id(packet->dst_id, packet->dst_id_len,
521 packet->dst_id_type);
526 /* Get channel entry */
527 channel = silc_idlist_find_channel_by_id(server->global_list,
530 channel = silc_idlist_find_channel_by_id(server->local_list,
533 silc_free(channel_id);
537 silc_free(channel_id);
540 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
543 SILC_GET32_MSB(mode, tmp);
545 /* Check if mode changed */
546 if (channel->mode == mode)
549 /* Get user's channel entry and check that mode change is allowed */
551 if (!silc_server_client_on_channel(client, channel, &chl))
553 if (!silc_server_check_cmode_rights(server, channel, chl, mode)) {
554 SILC_LOG_DEBUG(("CMODE change is not allowed"));
559 /* Send the same notify to the channel */
560 silc_server_packet_send_to_channel(server, sock, channel, packet->type,
561 FALSE, packet->buffer->data,
562 packet->buffer->len, FALSE);
564 /* If the channel had private keys set and the mode was removed then
565 we must re-generate and re-distribute a new channel key */
566 if (channel->mode & SILC_CHANNEL_MODE_PRIVKEY &&
567 !(mode & SILC_CHANNEL_MODE_PRIVKEY)) {
568 /* Re-generate channel key */
569 if (!silc_server_create_channel_key(server, channel, 0))
572 /* Send the channel key. This sends it to our local clients and if
573 we are normal server to our router as well. */
574 silc_server_send_channel_key(server, NULL, channel,
575 server->server_type == SILC_ROUTER ?
576 FALSE : !server->standalone);
580 tmp = silc_argument_get_arg_type(args, 4, &tmp_len);
582 unsigned char hash[32];
585 silc_hmac_free(channel->hmac);
586 if (!silc_hmac_alloc(tmp, NULL, &channel->hmac))
589 /* Set the HMAC key out of current channel key. The client must do
591 silc_hash_make(silc_hmac_get_hash(channel->hmac), channel->key,
592 channel->key_len / 8, hash);
593 silc_hmac_set_key(channel->hmac, hash,
594 silc_hash_len(silc_hmac_get_hash(channel->hmac)));
595 memset(hash, 0, sizeof(hash));
598 /* Get the passphrase */
599 tmp = silc_argument_get_arg_type(args, 5, &tmp_len);
601 silc_free(channel->passphrase);
602 channel->passphrase = silc_memdup(tmp, tmp_len);
605 /* Get founder public key */
606 tmp = silc_argument_get_arg_type(args, 6, &tmp_len);
607 if (tmp && mode & SILC_CHANNEL_MODE_FOUNDER_AUTH) {
608 if (channel->founder_key)
609 silc_pkcs_public_key_free(channel->founder_key);
610 channel->founder_key = NULL;
611 silc_pkcs_public_key_decode(tmp, tmp_len, &channel->founder_key);
613 if (!channel->founder_key ||
614 (client && client->data.public_key &&
615 server->server_type == SILC_ROUTER &&
616 !silc_pkcs_public_key_compare(channel->founder_key,
617 client->data.public_key))) {
618 /* A really buggy server isn't checking public keys correctly.
619 It's not possible that the mode setter and founder wouldn't
620 have same public key. */
621 SILC_LOG_DEBUG(("Enforcing sender to change channel mode"));
623 mode &= ~SILC_CHANNEL_MODE_FOUNDER_AUTH;
624 silc_server_send_notify_cmode(server, sock, FALSE, channel,
625 mode, server->id, SILC_ID_SERVER,
628 channel->passphrase, NULL);
629 if (channel->founder_key)
630 silc_pkcs_public_key_free(channel->founder_key);
631 channel->founder_key = NULL;
632 } else if (client && !client->data.public_key) {
633 client->data.public_key =
634 silc_pkcs_public_key_copy(channel->founder_key);
638 if (mode & SILC_CHANNEL_MODE_FOUNDER_AUTH && !channel->founder_key &&
639 server->server_type == SILC_ROUTER) {
640 SILC_LOG_DEBUG(("Enforcing sender to change channel mode"));
641 mode &= ~SILC_CHANNEL_MODE_FOUNDER_AUTH;
642 silc_server_send_notify_cmode(server, sock, FALSE, channel,
643 mode, server->id, SILC_ID_SERVER,
646 channel->passphrase, NULL);
650 channel->mode = mode;
652 if (!(channel->mode & SILC_CHANNEL_MODE_FOUNDER_AUTH) &&
653 channel->founder_key) {
654 silc_pkcs_public_key_free(channel->founder_key);
655 channel->founder_key = NULL;
660 case SILC_NOTIFY_TYPE_CUMODE_CHANGE:
663 * Distribute the notify to local clients on the channel
665 SilcChannelClientEntry chl2 = NULL;
666 bool notify_sent = FALSE;
668 SILC_LOG_DEBUG(("CUMODE CHANGE notify"));
671 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
674 client_id = silc_id_payload_parse_id(tmp, tmp_len, &id_type);
678 /* Get client entry */
679 if (id_type == SILC_ID_CLIENT) {
680 client = silc_idlist_find_client_by_id(server->global_list,
681 client_id, TRUE, &cache);
683 client = silc_idlist_find_client_by_id(server->local_list,
684 client_id, TRUE, &cache);
686 silc_free(client_id);
690 silc_free(client_id);
694 channel_id = silc_id_str2id(packet->dst_id, packet->dst_id_len,
695 packet->dst_id_type);
700 /* Get channel entry */
701 channel = silc_idlist_find_channel_by_id(server->global_list,
704 channel = silc_idlist_find_channel_by_id(server->local_list,
707 silc_free(channel_id);
713 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
715 silc_free(channel_id);
719 SILC_GET32_MSB(mode, tmp);
721 /* Get target client */
722 tmp = silc_argument_get_arg_type(args, 3, &tmp_len);
725 client_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
729 /* Get client entry */
730 client2 = silc_idlist_find_client_by_id(server->global_list,
731 client_id, TRUE, NULL);
733 client2 = silc_idlist_find_client_by_id(server->local_list,
734 client_id, TRUE, NULL);
736 silc_free(client_id);
740 silc_free(client_id);
743 /* Check that sender is on channel */
744 if (!silc_server_client_on_channel(client, channel, &chl))
747 if (client != client2) {
748 /* Sender must be operator */
749 if (!(chl->mode & SILC_CHANNEL_UMODE_CHANOP) &&
750 !(chl->mode & SILC_CHANNEL_UMODE_CHANFO)) {
751 SILC_LOG_DEBUG(("CUMODE change is not allowed"));
755 /* Check that target is on channel */
756 if (!silc_server_client_on_channel(client2, channel, &chl))
759 /* If target is founder mode change is not allowed. */
760 if (chl->mode & SILC_CHANNEL_UMODE_CHANFO) {
761 SILC_LOG_DEBUG(("CUMODE change is not allowed"));
767 if (mode & SILC_CHANNEL_UMODE_CHANFO &&
768 !(chl->mode & SILC_CHANNEL_UMODE_CHANFO) &&
769 server->server_type == SILC_ROUTER) {
770 /* Check whether this client is allowed to be channel founder on
772 SilcPublicKey founder_key = NULL;
774 /* If channel doesn't have founder auth mode then it's impossible
775 that someone would be getting founder rights with CUMODE command.
776 In that case there already either is founder or there isn't
777 founder at all on the channel. */
778 if (!(channel->mode & SILC_CHANNEL_MODE_FOUNDER_AUTH)) {
779 /* Force the mode to not have founder mode */
780 mode &= ~SILC_CHANNEL_UMODE_CHANFO;
781 silc_server_force_cumode_change(server, sock, channel, chl, mode);
786 /* Get the founder of the channel and if found then this client
787 cannot be the founder since there already is one. */
788 silc_hash_table_list(channel->user_list, &htl);
789 while (silc_hash_table_get(&htl, NULL, (void *)&chl2))
790 if (chl2->mode & SILC_CHANNEL_UMODE_CHANFO) {
791 mode &= ~SILC_CHANNEL_UMODE_CHANFO;
792 silc_server_force_cumode_change(server, sock, channel, chl, mode);
796 silc_hash_table_list_reset(&htl);
797 if (!(mode & SILC_CHANNEL_UMODE_CHANFO))
800 /* Founder not found of the channel. Since the founder auth mode
801 is set on the channel now check whether this is the client that
802 originally set the mode. */
804 /* Get public key that must be present in notify */
805 tmp = silc_argument_get_arg_type(args, 4, &tmp_len);
806 if (!tmp || !silc_pkcs_public_key_decode(tmp, tmp_len,
808 mode &= ~SILC_CHANNEL_UMODE_CHANFO;
809 silc_server_force_cumode_change(server, sock, channel, chl, mode);
814 /* Now match the public key we have cached and public key sent.
816 if (client && client->data.public_key &&
817 !silc_pkcs_public_key_compare(channel->founder_key,
818 client->data.public_key)) {
819 mode &= ~SILC_CHANNEL_UMODE_CHANFO;
820 silc_server_force_cumode_change(server, sock, channel, chl, mode);
824 if (!silc_pkcs_public_key_compare(channel->founder_key,
826 mode &= ~SILC_CHANNEL_UMODE_CHANFO;
827 silc_server_force_cumode_change(server, sock, channel, chl, mode);
833 silc_pkcs_public_key_free(founder_key);
836 SILC_LOG_DEBUG(("Changing the channel user mode"));
838 /* Change the mode */
841 /* Send the same notify to the channel */
843 silc_server_packet_send_to_channel(server, sock, channel,
845 FALSE, packet->buffer->data,
846 packet->buffer->len, FALSE);
848 silc_free(channel_id);
852 case SILC_NOTIFY_TYPE_INVITE:
854 if (packet->dst_id_type == SILC_ID_CLIENT)
857 SILC_LOG_DEBUG(("INVITE notify"));
860 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
863 channel_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
867 /* Get channel entry */
868 channel = silc_idlist_find_channel_by_id(server->global_list,
871 channel = silc_idlist_find_channel_by_id(server->local_list,
874 silc_free(channel_id);
878 silc_free(channel_id);
881 tmp = silc_argument_get_arg_type(args, 3, &tmp_len);
884 client_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
888 /* Get client entry */
889 client = silc_idlist_find_client_by_id(server->global_list,
890 client_id, TRUE, &cache);
892 client = silc_idlist_find_client_by_id(server->local_list,
893 client_id, TRUE, &cache);
895 silc_free(client_id);
899 silc_free(client_id);
901 /* Get user's channel entry and check that inviting is allowed. */
902 if (!silc_server_client_on_channel(client, channel, &chl))
904 if (channel->mode & SILC_CHANNEL_MODE_INVITE &&
905 !(chl->mode & SILC_CHANNEL_UMODE_CHANOP) &&
906 !(chl->mode & SILC_CHANNEL_UMODE_CHANFO)) {
907 SILC_LOG_DEBUG(("Inviting is not allowed"));
911 /* Get the added invite */
912 tmp = silc_argument_get_arg_type(args, 4, &tmp_len);
914 if (!channel->invite_list)
915 channel->invite_list = silc_calloc(tmp_len + 2,
916 sizeof(*channel->invite_list));
918 channel->invite_list = silc_realloc(channel->invite_list,
919 sizeof(*channel->invite_list) *
921 strlen(channel->invite_list) +
923 if (tmp[tmp_len - 1] == ',')
924 tmp[tmp_len - 1] = '\0';
926 strncat(channel->invite_list, tmp, tmp_len);
927 strncat(channel->invite_list, ",", 1);
930 /* Get the deleted invite */
931 tmp = silc_argument_get_arg_type(args, 5, &tmp_len);
932 if (tmp && channel->invite_list) {
933 char *start, *end, *n;
935 if (!strncmp(channel->invite_list, tmp,
936 strlen(channel->invite_list) - 1)) {
937 silc_free(channel->invite_list);
938 channel->invite_list = NULL;
940 start = strstr(channel->invite_list, tmp);
941 if (start && strlen(start) >= tmp_len) {
942 end = start + tmp_len;
943 n = silc_calloc(strlen(channel->invite_list) - tmp_len, sizeof(*n));
944 strncat(n, channel->invite_list, start - channel->invite_list);
945 strncat(n, end + 1, ((channel->invite_list +
946 strlen(channel->invite_list)) - end) - 1);
947 silc_free(channel->invite_list);
948 channel->invite_list = n;
955 case SILC_NOTIFY_TYPE_CHANNEL_CHANGE:
957 * Distribute to the local clients on the channel and change the
961 SILC_LOG_DEBUG(("CHANNEL CHANGE"));
963 if (sock->type != SILC_SOCKET_TYPE_ROUTER)
966 /* Get the old Channel ID */
967 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
970 channel_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
974 /* Get the channel entry */
975 channel = silc_idlist_find_channel_by_id(server->local_list,
978 channel = silc_idlist_find_channel_by_id(server->global_list,
981 silc_free(channel_id);
986 /* Send the notify to the channel */
987 silc_server_packet_send_to_channel(server, sock, channel, packet->type,
988 FALSE, packet->buffer->data,
989 packet->buffer->len, FALSE);
991 /* Get the new Channel ID */
992 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
995 channel_id2 = silc_id_payload_parse_id(tmp, tmp_len, NULL);
999 SILC_LOG_DEBUG(("Old Channel ID id(%s)",
1000 silc_id_render(channel_id, SILC_ID_CHANNEL)));
1001 SILC_LOG_DEBUG(("New Channel ID id(%s)",
1002 silc_id_render(channel_id2, SILC_ID_CHANNEL)));
1004 /* Replace the Channel ID */
1005 if (!silc_idlist_replace_channel_id(server->local_list, channel_id,
1007 if (!silc_idlist_replace_channel_id(server->global_list, channel_id,
1009 silc_free(channel_id2);
1014 SilcBuffer modes = NULL, users = NULL, users_modes = NULL;
1016 /* Re-announce this channel which ID was changed. */
1017 silc_server_send_new_channel(server, sock, FALSE, channel->channel_name,
1019 silc_id_get_len(channel->id,
1023 /* Re-announce our clients on the channel as the ID has changed now */
1024 silc_server_announce_get_channel_users(server, channel, &modes, &users,
1027 silc_buffer_push(modes, modes->data - modes->head);
1028 silc_server_packet_send_dest(server, sock,
1029 SILC_PACKET_NOTIFY, SILC_PACKET_FLAG_LIST,
1030 channel->id, SILC_ID_CHANNEL,
1031 modes->data, modes->len, FALSE);
1032 silc_buffer_free(modes);
1035 silc_buffer_push(users, users->data - users->head);
1036 silc_server_packet_send(server, sock,
1037 SILC_PACKET_NOTIFY, SILC_PACKET_FLAG_LIST,
1038 users->data, users->len, FALSE);
1039 silc_buffer_free(users);
1042 silc_buffer_push(users_modes, users_modes->data - users_modes->head);
1043 silc_server_packet_send_dest(server, sock,
1044 SILC_PACKET_NOTIFY, SILC_PACKET_FLAG_LIST,
1045 channel->id, SILC_ID_CHANNEL,
1047 users_modes->len, FALSE);
1048 silc_buffer_free(users_modes);
1051 /* Re-announce channel's topic */
1052 if (channel->topic) {
1053 silc_server_send_notify_topic_set(server, sock,
1054 server->server_type == SILC_ROUTER ?
1055 TRUE : FALSE, channel,
1056 channel->id, SILC_ID_CHANNEL,
1061 silc_free(channel_id);
1065 case SILC_NOTIFY_TYPE_SERVER_SIGNOFF:
1067 * Remove the server entry and all clients that this server owns.
1070 SILC_LOG_DEBUG(("SERVER SIGNOFF notify"));
1073 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
1076 server_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
1080 /* Get server entry */
1081 server_entry = silc_idlist_find_server_by_id(server->global_list,
1082 server_id, TRUE, NULL);
1084 if (!server_entry) {
1085 server_entry = silc_idlist_find_server_by_id(server->local_list,
1086 server_id, TRUE, NULL);
1088 if (!server_entry) {
1089 /* If we are normal server then we might not have the server. Check
1090 whether router was kind enough to send the list of all clients
1091 that actually was to be removed. Remove them if the list is
1093 if (server->server_type != SILC_ROUTER &&
1094 silc_argument_get_arg_num(args) > 1) {
1097 for (i = 1; i < silc_argument_get_arg_num(args); i++) {
1099 tmp = silc_argument_get_arg_type(args, i + 1, &tmp_len);
1102 client_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
1106 /* Get client entry */
1107 client = silc_idlist_find_client_by_id(server->global_list,
1108 client_id, TRUE, &cache);
1111 client = silc_idlist_find_client_by_id(server->local_list,
1112 client_id, TRUE, &cache);
1115 silc_free(client_id);
1119 silc_free(client_id);
1121 /* Update statistics */
1122 server->stat.clients--;
1123 if (server->stat.cell_clients)
1124 server->stat.cell_clients--;
1125 SILC_OPER_STATS_UPDATE(client, server, SILC_UMODE_SERVER_OPERATOR);
1126 SILC_OPER_STATS_UPDATE(client, router, SILC_UMODE_ROUTER_OPERATOR);
1128 /* Remove the client from all channels. */
1129 silc_server_remove_from_channels(server, NULL, client,
1132 /* Check if anyone is watching this nickname */
1133 if (server->server_type == SILC_ROUTER)
1134 silc_server_check_watcher_list(server, client, NULL,
1135 SILC_NOTIFY_TYPE_SERVER_SIGNOFF);
1137 /* Remove this client from watcher list if it is */
1139 silc_server_del_from_watcher_list(server, client);
1141 /* Remove the client */
1142 silc_idlist_del_client(local ? server->local_list :
1143 server->global_list, client);
1147 silc_free(server_id);
1151 silc_free(server_id);
1153 /* Free all client entries that this server owns as they will
1154 become invalid now as well. */
1155 silc_server_remove_clients_by_server(server, server_entry, TRUE);
1157 /* Remove the server entry */
1158 silc_idlist_del_server(local ? server->local_list :
1159 server->global_list, server_entry);
1161 /* XXX update statistics */
1165 case SILC_NOTIFY_TYPE_KICKED:
1167 * Distribute the notify to local clients on the channel
1170 SILC_LOG_DEBUG(("KICKED notify"));
1173 channel_id = silc_id_str2id(packet->dst_id, packet->dst_id_len,
1174 packet->dst_id_type);
1179 /* Get channel entry */
1180 channel = silc_idlist_find_channel_by_id(server->global_list,
1183 channel = silc_idlist_find_channel_by_id(server->local_list,
1186 silc_free(channel_id);
1190 silc_free(channel_id);
1193 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
1196 client_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
1200 /* If the the client is not in local list we check global list */
1201 client = silc_idlist_find_client_by_id(server->global_list,
1202 client_id, TRUE, NULL);
1204 client = silc_idlist_find_client_by_id(server->local_list,
1205 client_id, TRUE, NULL);
1207 silc_free(client_id);
1211 silc_free(client_id);
1213 /* If target is founder they cannot be kicked */
1214 if (!silc_server_client_on_channel(client, channel, &chl))
1216 if (chl->mode & SILC_CHANNEL_UMODE_CHANFO)
1219 /* From protocol version 1.1 we get the kicker's ID as well. */
1220 tmp = silc_argument_get_arg_type(args, 3, &tmp_len);
1222 client_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
1226 /* If the the client is not in local list we check global list */
1227 client2 = silc_idlist_find_client_by_id(server->global_list,
1228 client_id, TRUE, NULL);
1230 client2 = silc_idlist_find_client_by_id(server->local_list,
1231 client_id, TRUE, NULL);
1233 silc_free(client_id);
1237 silc_free(client_id);
1239 /* Kicker must be operator on channel */
1240 if (!silc_server_client_on_channel(client2, channel, &chl))
1242 if (!(chl->mode & SILC_CHANNEL_UMODE_CHANOP) &&
1243 !(chl->mode & SILC_CHANNEL_UMODE_CHANFO)) {
1244 SILC_LOG_DEBUG(("Kicking is not allowed"));
1249 /* Send to channel */
1250 silc_server_packet_send_to_channel(server, sock, channel, packet->type,
1251 FALSE, packet->buffer->data,
1252 packet->buffer->len, FALSE);
1254 /* Remove the client from channel */
1255 silc_server_remove_from_one_channel(server, sock, channel, client, FALSE);
1259 case SILC_NOTIFY_TYPE_KILLED:
1262 * Distribute the notify to local clients on channels
1264 unsigned char *id, *comment;
1265 SilcUInt32 id_len, comment_len;
1267 SILC_LOG_DEBUG(("KILLED notify"));
1270 id = silc_argument_get_arg_type(args, 1, &id_len);
1273 client_id = silc_id_payload_parse_id(id, id_len, NULL);
1277 /* If the the client is not in local list we check global list */
1278 client = silc_idlist_find_client_by_id(server->global_list,
1279 client_id, TRUE, NULL);
1281 client = silc_idlist_find_client_by_id(server->local_list,
1282 client_id, TRUE, NULL);
1284 silc_free(client_id);
1288 silc_free(client_id);
1290 /* If the client is one of ours, then close the connection to the
1291 client now. This removes the client from all channels as well. */
1292 if (packet->dst_id_type == SILC_ID_CLIENT && client->connection) {
1293 sock = client->connection;
1294 silc_server_free_client_data(server, NULL, client, FALSE, NULL);
1295 silc_server_close_connection(server, sock);
1300 comment = silc_argument_get_arg_type(args, 2, &comment_len);
1301 if (comment_len > 128)
1304 /* From protocol version 1.1 we get the killer's ID as well. */
1305 tmp = silc_argument_get_arg_type(args, 3, &tmp_len);
1307 client_id = silc_id_payload_parse_id(tmp, tmp_len, &id_type);
1311 if (id_type == SILC_ID_CLIENT) {
1312 /* If the the client is not in local list we check global list */
1313 client2 = silc_idlist_find_client_by_id(server->global_list,
1314 client_id, TRUE, NULL);
1316 client2 = silc_idlist_find_client_by_id(server->local_list,
1317 client_id, TRUE, NULL);
1319 silc_free(client_id);
1323 silc_free(client_id);
1325 /* Killer must be router operator */
1326 if (!(client2->mode & SILC_UMODE_ROUTER_OPERATOR)) {
1327 SILC_LOG_DEBUG(("Killing is not allowed"));
1333 /* Send the notify to local clients on the channels except to the
1334 client who is killed. */
1335 silc_server_send_notify_on_channels(server, client, client,
1336 SILC_NOTIFY_TYPE_KILLED, 3,
1337 id, id_len, comment, comment_len,
1340 /* Remove the client from all channels */
1341 silc_server_remove_from_channels(server, NULL, client, FALSE, NULL,
1344 /* Check if anyone is watching this nickname */
1345 if (server->server_type == SILC_ROUTER)
1346 silc_server_check_watcher_list(server, client, NULL,
1347 SILC_NOTIFY_TYPE_KILLED);
1352 case SILC_NOTIFY_TYPE_UMODE_CHANGE:
1354 * Save the mode of the client.
1357 SILC_LOG_DEBUG(("UMODE_CHANGE notify"));
1360 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
1363 client_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
1367 /* Get client entry */
1368 client = silc_idlist_find_client_by_id(server->global_list,
1369 client_id, TRUE, NULL);
1371 client = silc_idlist_find_client_by_id(server->local_list,
1372 client_id, TRUE, NULL);
1374 silc_free(client_id);
1378 silc_free(client_id);
1381 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
1384 SILC_GET32_MSB(mode, tmp);
1386 /* Check that mode changing is allowed. */
1387 if (!silc_server_check_umode_rights(server, client, mode)) {
1388 SILC_LOG_DEBUG(("UMODE change is not allowed"));
1392 /* Remove internal resumed flag if client is marked detached now */
1393 if (mode & SILC_UMODE_DETACHED)
1394 client->data.status &= ~SILC_IDLIST_STATUS_RESUMED;
1396 /* Change the mode */
1397 client->mode = mode;
1399 /* Check if anyone is watching this nickname */
1400 if (server->server_type == SILC_ROUTER)
1401 silc_server_check_watcher_list(server, client, NULL,
1402 SILC_NOTIFY_TYPE_UMODE_CHANGE);
1406 case SILC_NOTIFY_TYPE_BAN:
1411 SILC_LOG_DEBUG(("BAN notify"));
1413 /* Get Channel ID */
1414 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
1417 channel_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
1421 /* Get channel entry */
1422 channel = silc_idlist_find_channel_by_id(server->global_list,
1425 channel = silc_idlist_find_channel_by_id(server->local_list,
1428 silc_free(channel_id);
1432 silc_free(channel_id);
1434 /* Get the new ban and add it to the ban list */
1435 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
1437 if (!channel->ban_list)
1438 channel->ban_list = silc_calloc(tmp_len + 2,
1439 sizeof(*channel->ban_list));
1441 channel->ban_list = silc_realloc(channel->ban_list,
1442 sizeof(*channel->ban_list) *
1444 strlen(channel->ban_list) + 2));
1445 strncat(channel->ban_list, tmp, tmp_len);
1446 strncat(channel->ban_list, ",", 1);
1449 /* Get the ban to be removed and remove it from the list */
1450 tmp = silc_argument_get_arg_type(args, 3, &tmp_len);
1451 if (tmp && channel->ban_list) {
1452 char *start, *end, *n;
1454 if (!strncmp(channel->ban_list, tmp, strlen(channel->ban_list) - 1)) {
1455 silc_free(channel->ban_list);
1456 channel->ban_list = NULL;
1458 start = strstr(channel->ban_list, tmp);
1459 if (start && strlen(start) >= tmp_len) {
1460 end = start + tmp_len;
1461 n = silc_calloc(strlen(channel->ban_list) - tmp_len, sizeof(*n));
1462 strncat(n, channel->ban_list, start - channel->ban_list);
1463 strncat(n, end + 1, ((channel->ban_list +
1464 strlen(channel->ban_list)) - end) - 1);
1465 silc_free(channel->ban_list);
1466 channel->ban_list = n;
1472 case SILC_NOTIFY_TYPE_ERROR:
1479 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
1480 if (!tmp && tmp_len != 1)
1482 error = (SilcStatus)tmp[0];
1484 SILC_LOG_DEBUG(("ERROR notify (%d)", error));
1486 if (error == SILC_STATUS_ERR_NO_SUCH_CLIENT_ID &&
1487 sock->type == SILC_SOCKET_TYPE_ROUTER) {
1488 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
1490 SILC_LOG_DEBUG(("Received invalid client ID notification, deleting "
1491 "the entry from cache"));
1492 client_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
1495 client = silc_idlist_find_client_by_id(server->global_list,
1496 client_id, FALSE, NULL);
1498 silc_server_remove_from_channels(server, NULL, client, TRUE,
1500 silc_idlist_del_client(server->global_list, client);
1502 silc_free(client_id);
1508 /* Ignore rest of the notify types for now */
1509 case SILC_NOTIFY_TYPE_NONE:
1510 case SILC_NOTIFY_TYPE_MOTD:
1517 silc_notify_payload_free(payload);
1520 void silc_server_notify_list(SilcServer server,
1521 SilcSocketConnection sock,
1522 SilcPacketContext *packet)
1524 SilcPacketContext *new;
1528 SILC_LOG_DEBUG(("Processing Notify List"));
1530 if (sock->type == SILC_SOCKET_TYPE_CLIENT ||
1531 packet->src_id_type != SILC_ID_SERVER)
1534 /* Make copy of the original packet context, except for the actual
1535 data buffer, which we will here now fetch from the original buffer. */
1536 new = silc_packet_context_alloc();
1537 new->type = SILC_PACKET_NOTIFY;
1538 new->flags = packet->flags;
1539 new->src_id = packet->src_id;
1540 new->src_id_len = packet->src_id_len;
1541 new->src_id_type = packet->src_id_type;
1542 new->dst_id = packet->dst_id;
1543 new->dst_id_len = packet->dst_id_len;
1544 new->dst_id_type = packet->dst_id_type;
1546 buffer = silc_buffer_alloc(1024);
1547 new->buffer = buffer;
1549 while (packet->buffer->len) {
1550 SILC_GET16_MSB(len, packet->buffer->data + 2);
1551 if (len > packet->buffer->len)
1554 if (len > buffer->truelen) {
1555 silc_buffer_free(buffer);
1556 buffer = silc_buffer_alloc(1024 + len);
1559 silc_buffer_pull_tail(buffer, len);
1560 silc_buffer_put(buffer, packet->buffer->data, len);
1562 /* Process the Notify */
1563 silc_server_notify(server, sock, new);
1565 silc_buffer_push_tail(buffer, len);
1566 silc_buffer_pull(packet->buffer, len);
1569 silc_buffer_free(buffer);
1573 /* Received private message. This resolves the destination of the message
1574 and sends the packet. This is used by both server and router. If the
1575 destination is our locally connected client this sends the packet to
1576 the client. This may also send the message for further routing if
1577 the destination is not in our server (or router). */
1579 void silc_server_private_message(SilcServer server,
1580 SilcSocketConnection sock,
1581 SilcPacketContext *packet)
1583 SilcSocketConnection dst_sock;
1584 SilcIDListData idata;
1585 SilcClientEntry client;
1587 SILC_LOG_DEBUG(("Start"));
1589 if (packet->src_id_type != SILC_ID_CLIENT ||
1590 packet->dst_id_type != SILC_ID_CLIENT || !packet->dst_id)
1593 /* Get the route to the client */
1594 dst_sock = silc_server_get_client_route(server, packet->dst_id,
1595 packet->dst_id_len, NULL,
1599 unsigned char error;
1601 if (client && client->mode & SILC_UMODE_DETACHED) {
1602 SILC_LOG_DEBUG(("Client is detached, discarding packet"));
1606 /* Send SILC_NOTIFY_TYPE_ERROR to indicate that such destination ID
1607 does not exist or is invalid. */
1608 idp = silc_id_payload_encode_data(packet->dst_id,
1610 packet->dst_id_type);
1614 error = SILC_STATUS_ERR_NO_SUCH_CLIENT_ID;
1615 if (packet->src_id_type == SILC_ID_CLIENT) {
1616 SilcClientID *client_id = silc_id_str2id(packet->src_id,
1618 packet->src_id_type);
1619 silc_server_send_notify_dest(server, sock, FALSE,
1620 client_id, SILC_ID_CLIENT,
1621 SILC_NOTIFY_TYPE_ERROR, 2,
1623 idp->data, idp->len);
1624 silc_free(client_id);
1626 silc_server_send_notify(server, sock, FALSE,
1627 SILC_NOTIFY_TYPE_ERROR, 2,
1629 idp->data, idp->len);
1632 silc_buffer_free(idp);
1636 /* Check whether destination client wishes to receive private messages */
1637 if (client && !(packet->flags & SILC_PACKET_FLAG_PRIVMSG_KEY) &&
1638 client->mode & SILC_UMODE_BLOCK_PRIVMSG) {
1639 SILC_LOG_DEBUG(("Client blocks private messages, discarding packet"));
1643 /* Send the private message */
1644 silc_server_send_private_message(server, dst_sock, idata->send_key,
1645 idata->hmac_send, idata->psn_send++,
1649 /* Received private message key packet.. This packet is never for us. It is to
1650 the client in the packet's destination ID. Sending of this sort of packet
1651 equals sending private message, ie. it is sent point to point from
1652 one client to another. */
1654 void silc_server_private_message_key(SilcServer server,
1655 SilcSocketConnection sock,
1656 SilcPacketContext *packet)
1658 SilcSocketConnection dst_sock;
1659 SilcIDListData idata;
1661 SILC_LOG_DEBUG(("Start"));
1663 if (packet->src_id_type != SILC_ID_CLIENT ||
1664 packet->dst_id_type != SILC_ID_CLIENT)
1667 if (!packet->dst_id)
1670 /* Get the route to the client */
1671 dst_sock = silc_server_get_client_route(server, packet->dst_id,
1672 packet->dst_id_len, NULL,
1677 /* Relay the packet */
1678 silc_server_relay_packet(server, dst_sock, idata->send_key,
1679 idata->hmac_send, idata->psn_send++, packet, FALSE);
1682 /* Processes incoming command reply packet. The command reply packet may
1683 be destined to one of our clients or it may directly for us. We will
1684 call the command reply routine after processing the packet. */
1686 void silc_server_command_reply(SilcServer server,
1687 SilcSocketConnection sock,
1688 SilcPacketContext *packet)
1690 SilcBuffer buffer = packet->buffer;
1691 SilcClientEntry client = NULL;
1692 SilcSocketConnection dst_sock;
1693 SilcIDListData idata;
1694 SilcClientID *id = NULL;
1696 SILC_LOG_DEBUG(("Start"));
1698 /* Source must be server or router */
1699 if (packet->src_id_type != SILC_ID_SERVER &&
1700 sock->type != SILC_SOCKET_TYPE_ROUTER)
1703 if (packet->dst_id_type == SILC_ID_CHANNEL)
1706 if (packet->dst_id_type == SILC_ID_CLIENT) {
1707 /* Destination must be one of ours */
1708 id = silc_id_str2id(packet->dst_id, packet->dst_id_len, SILC_ID_CLIENT);
1711 client = silc_idlist_find_client_by_id(server->local_list, id, TRUE, NULL);
1713 SILC_LOG_ERROR(("Cannot process command reply to unknown client"));
1719 if (packet->dst_id_type == SILC_ID_SERVER) {
1720 /* For now this must be for us */
1721 if (memcmp(packet->dst_id, server->id_string, server->id_string_len)) {
1722 SILC_LOG_ERROR(("Cannot process command reply to unknown server"));
1727 /* Execute command reply locally for the command */
1728 silc_server_command_reply_process(server, sock, buffer);
1730 if (packet->dst_id_type == SILC_ID_CLIENT && client && id) {
1731 /* Relay the packet to the client */
1732 const SilcBufferStruct p;
1734 dst_sock = (SilcSocketConnection)client->connection;
1735 idata = (SilcIDListData)client;
1737 silc_buffer_push(buffer, SILC_PACKET_HEADER_LEN + packet->src_id_len
1738 + packet->dst_id_len + packet->padlen);
1739 if (!silc_packet_send_prepare(dst_sock, 0, 0, buffer->len,
1740 idata->hmac_send, (const SilcBuffer)&p)) {
1741 SILC_LOG_ERROR(("Cannot send packet"));
1744 silc_buffer_put((SilcBuffer)&p, buffer->data, buffer->len);
1746 /* Encrypt packet */
1747 silc_packet_encrypt(idata->send_key, idata->hmac_send, idata->psn_send++,
1748 (SilcBuffer)&p, buffer->len);
1750 /* Send the packet */
1751 silc_server_packet_send_real(server, dst_sock, TRUE);
1757 /* Process received channel message. The message can be originated from
1758 client or server. */
1760 void silc_server_channel_message(SilcServer server,
1761 SilcSocketConnection sock,
1762 SilcPacketContext *packet)
1764 SilcChannelEntry channel = NULL;
1765 SilcChannelID *id = NULL;
1766 void *sender_id = NULL;
1767 SilcClientEntry sender_entry = NULL;
1768 SilcChannelClientEntry chl;
1771 SILC_LOG_DEBUG(("Processing channel message"));
1774 if (packet->dst_id_type != SILC_ID_CHANNEL) {
1775 SILC_LOG_DEBUG(("Received bad message for channel, dropped"));
1779 /* Find channel entry */
1780 id = silc_id_str2id(packet->dst_id, packet->dst_id_len, SILC_ID_CHANNEL);
1783 channel = silc_idlist_find_channel_by_id(server->local_list, id, NULL);
1785 channel = silc_idlist_find_channel_by_id(server->global_list, id, NULL);
1788 unsigned char error;
1790 /* Send SILC_NOTIFY_TYPE_ERROR to indicate that such destination ID
1791 does not exist or is invalid. */
1792 idp = silc_id_payload_encode_data(packet->dst_id,
1794 packet->dst_id_type);
1798 error = SILC_STATUS_ERR_NO_SUCH_CHANNEL_ID;
1799 if (packet->src_id_type == SILC_ID_CLIENT) {
1800 SilcClientID *client_id = silc_id_str2id(packet->src_id,
1802 packet->src_id_type);
1803 silc_server_send_notify_dest(server, sock, FALSE,
1804 client_id, SILC_ID_CLIENT,
1805 SILC_NOTIFY_TYPE_ERROR, 2,
1806 &error, 1, idp->data, idp->len);
1807 silc_free(client_id);
1809 silc_server_send_notify(server, sock, FALSE,
1810 SILC_NOTIFY_TYPE_ERROR, 2,
1811 &error, 1, idp->data, idp->len);
1814 silc_buffer_free(idp);
1819 /* See that this client is on the channel. If the original sender is
1820 not client (as it can be server as well) we don't do the check. */
1821 sender_id = silc_id_str2id(packet->src_id, packet->src_id_len,
1822 packet->src_id_type);
1825 if (packet->src_id_type == SILC_ID_CLIENT) {
1826 sender_entry = silc_idlist_find_client_by_id(server->local_list,
1827 sender_id, TRUE, NULL);
1828 if (!sender_entry) {
1830 sender_entry = silc_idlist_find_client_by_id(server->global_list,
1831 sender_id, TRUE, NULL);
1833 if (!sender_entry || !silc_server_client_on_channel(sender_entry,
1835 SILC_LOG_DEBUG(("Client not on channel"));
1839 /* If channel is moderated check that client is allowed to send
1841 if (channel->mode & SILC_CHANNEL_MODE_SILENCE_USERS &&
1842 !(chl->mode & SILC_CHANNEL_UMODE_CHANOP) &&
1843 !(chl->mode & SILC_CHANNEL_UMODE_CHANFO)) {
1844 SILC_LOG_DEBUG(("Channel is silenced from normal users"));
1847 if (channel->mode & SILC_CHANNEL_MODE_SILENCE_OPERS &&
1848 chl->mode & SILC_CHANNEL_UMODE_CHANOP &&
1849 !(chl->mode & SILC_CHANNEL_UMODE_CHANFO)) {
1850 SILC_LOG_DEBUG(("Channel is silenced from operators"));
1854 /* If the packet is coming from router, but the client entry is local
1855 entry to us then some router is rerouting this to us and it is not
1856 allowed. When the client is local to us it means that we've routed
1857 this packet to network, and now someone is routing it back to us. */
1858 if (server->server_type == SILC_ROUTER &&
1859 sock->type == SILC_SOCKET_TYPE_ROUTER && local) {
1860 SILC_LOG_DEBUG(("Channel message rerouted to the sender, drop it"));
1865 /* Distribute the packet to our local clients. This will send the
1866 packet for further routing as well, if needed. */
1867 silc_server_packet_relay_to_channel(server, sock, channel, sender_id,
1868 packet->src_id_type, sender_entry,
1869 packet->buffer->data,
1870 packet->buffer->len, FALSE);
1873 silc_free(sender_id);
1877 /* Received channel key packet. We distribute the key to all of our locally
1878 connected clients on the channel. */
1880 void silc_server_channel_key(SilcServer server,
1881 SilcSocketConnection sock,
1882 SilcPacketContext *packet)
1884 SilcBuffer buffer = packet->buffer;
1885 SilcChannelEntry channel;
1887 if (packet->src_id_type != SILC_ID_SERVER ||
1888 (server->server_type == SILC_ROUTER &&
1889 sock->type == SILC_SOCKET_TYPE_ROUTER))
1892 /* Save the channel key */
1893 channel = silc_server_save_channel_key(server, buffer, NULL);
1897 /* Distribute the key to everybody who is on the channel. If we are router
1898 we will also send it to locally connected servers. */
1899 silc_server_send_channel_key(server, sock, channel, FALSE);
1901 if (server->server_type != SILC_BACKUP_ROUTER) {
1902 /* Distribute to local cell backup routers. */
1903 silc_server_backup_send(server, (SilcServerEntry)sock->user_data,
1904 SILC_PACKET_CHANNEL_KEY, 0,
1905 buffer->data, buffer->len, FALSE, TRUE);
1909 /* Received New Client packet and processes it. Creates Client ID for the
1910 client. Client becomes registered after calling this functions. */
1912 SilcClientEntry silc_server_new_client(SilcServer server,
1913 SilcSocketConnection sock,
1914 SilcPacketContext *packet)
1916 SilcBuffer buffer = packet->buffer;
1917 SilcClientEntry client;
1918 SilcClientID *client_id;
1919 SilcIDListData idata;
1920 char *username = NULL, *realname = NULL;
1921 SilcUInt16 username_len;
1924 char *hostname, *nickname;
1927 SILC_LOG_DEBUG(("Creating new client"));
1929 if (sock->type != SILC_SOCKET_TYPE_CLIENT)
1932 /* Take client entry */
1933 client = (SilcClientEntry)sock->user_data;
1934 idata = (SilcIDListData)client;
1936 /* Remove the old cache entry. */
1937 if (!silc_idcache_del_by_context(server->local_list->clients, client)) {
1938 SILC_LOG_INFO(("Unauthenticated client attempted to register to network"));
1939 silc_server_disconnect_remote(server, sock,
1940 SILC_STATUS_ERR_NOT_AUTHENTICATED, NULL);
1941 if (sock->user_data)
1942 silc_server_free_sock_user_data(server, sock, NULL);
1946 /* Parse incoming packet */
1947 ret = silc_buffer_unformat(buffer,
1948 SILC_STR_UI16_NSTRING_ALLOC(&username,
1950 SILC_STR_UI16_STRING_ALLOC(&realname),
1953 silc_free(username);
1954 silc_free(realname);
1955 SILC_LOG_ERROR(("Client %s (%s) sent incomplete information, closing "
1956 "connection", sock->hostname, sock->ip));
1957 silc_server_disconnect_remote(server, sock,
1958 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
1960 if (sock->user_data)
1961 silc_server_free_sock_user_data(server, sock, NULL);
1966 silc_free(username);
1967 silc_free(realname);
1968 SILC_LOG_ERROR(("Client %s (%s) did not send its username, closing "
1969 "connection", sock->hostname, sock->ip));
1970 silc_server_disconnect_remote(server, sock,
1971 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
1973 if (sock->user_data)
1974 silc_server_free_sock_user_data(server, sock, NULL);
1978 if (username_len > 128)
1979 username[128] = '\0';
1981 /* Check for bad characters for nickname, and modify the nickname if
1982 it includes those. */
1983 if (silc_server_name_bad_chars(username, username_len)) {
1984 nickname = silc_server_name_modify_bad(username, username_len);
1986 nickname = strdup(username);
1989 /* Make sanity checks for the hostname of the client. If the hostname
1990 is provided in the `username' check that it is the same than the
1991 resolved hostname, or if not resolved the hostname that appears in
1992 the client's public key. If the hostname is not present then put
1993 it from the resolved name or from the public key. */
1994 if (strchr(username, '@')) {
1995 SilcPublicKeyIdentifier pident;
1996 int tlen = strcspn(username, "@");
1997 char *phostname = NULL;
1999 hostname = silc_memdup(username + tlen + 1, strlen(username) - tlen - 1);
2001 if (strcmp(sock->hostname, sock->ip) &&
2002 strcmp(sock->hostname, hostname)) {
2003 silc_free(username);
2004 silc_free(hostname);
2005 silc_free(realname);
2006 SILC_LOG_ERROR(("Client %s (%s) sent incomplete information, closing "
2007 "connection", sock->hostname, sock->ip));
2008 silc_server_disconnect_remote(server, sock,
2009 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
2011 if (sock->user_data)
2012 silc_server_free_sock_user_data(server, sock, NULL);
2016 pident = silc_pkcs_decode_identifier(client->data.public_key->identifier);
2018 phostname = strdup(pident->host);
2019 silc_pkcs_free_identifier(pident);
2022 if (!strcmp(sock->hostname, sock->ip) &&
2023 phostname && strcmp(phostname, hostname)) {
2024 silc_free(username);
2025 silc_free(hostname);
2026 silc_free(phostname);
2027 silc_free(realname);
2028 SILC_LOG_ERROR(("Client %s (%s) sent incomplete information, closing "
2029 "connection", sock->hostname, sock->ip));
2030 silc_server_disconnect_remote(server, sock,
2031 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
2033 if (sock->user_data)
2034 silc_server_free_sock_user_data(server, sock, NULL);
2038 silc_free(phostname);
2040 /* The hostname is not present, add it. */
2042 /* XXX For now we cannot take the host name from the public key since
2043 they are not trusted or we cannot verify them as trusted. Just take
2044 what the resolved name or address is. */
2046 if (strcmp(sock->hostname, sock->ip)) {
2048 newusername = silc_calloc(strlen(username) +
2049 strlen(sock->hostname) + 2,
2050 sizeof(*newusername));
2051 strncat(newusername, username, strlen(username));
2052 strncat(newusername, "@", 1);
2053 strncat(newusername, sock->hostname, strlen(sock->hostname));
2054 silc_free(username);
2055 username = newusername;
2058 SilcPublicKeyIdentifier pident =
2059 silc_pkcs_decode_identifier(client->data.public_key->identifier);
2062 newusername = silc_calloc(strlen(username) +
2063 strlen(pident->host) + 2,
2064 sizeof(*newusername));
2065 strncat(newusername, username, strlen(username));
2066 strncat(newusername, "@", 1);
2067 strncat(newusername, pident->host, strlen(pident->host));
2068 silc_free(username);
2069 username = newusername;
2070 silc_pkcs_free_identifier(pident);
2076 /* Create Client ID */
2077 while (!silc_id_create_client_id(server, server->id, server->rng,
2078 server->md5hash, nickname, &client_id)) {
2081 silc_server_disconnect_remote(server, sock,
2082 SILC_STATUS_ERR_BAD_NICKNAME, NULL);
2083 if (sock->user_data)
2084 silc_server_free_sock_user_data(server, sock, NULL);
2087 snprintf(&nickname[strlen(nickname) - 1], 1, "%d", nickfail);
2090 /* Update client entry */
2091 idata->status |= SILC_IDLIST_STATUS_REGISTERED;
2092 client->nickname = nickname;
2093 client->username = username;
2094 client->userinfo = realname ? realname : strdup(" ");
2095 client->id = client_id;
2096 id_len = silc_id_get_len(client_id, SILC_ID_CLIENT);
2098 /* Add the client again to the ID cache */
2099 silc_idcache_add(server->local_list->clients, client->nickname,
2100 client_id, client, 0, NULL);
2102 /* Notify our router about new client on the SILC network */
2103 if (!server->standalone)
2104 silc_server_send_new_id(server, (SilcSocketConnection)
2105 server->router->connection,
2106 server->server_type == SILC_ROUTER ? TRUE : FALSE,
2107 client->id, SILC_ID_CLIENT, id_len);
2109 /* Send the new client ID to the client. */
2110 silc_server_send_new_id(server, sock, FALSE, client->id, SILC_ID_CLIENT,
2111 silc_id_get_len(client->id, SILC_ID_CLIENT));
2113 /* Send some nice info to the client */
2114 silc_server_send_connect_notifys(server, sock, client);
2116 /* Check if anyone is watching this nickname */
2117 if (server->server_type == SILC_ROUTER)
2118 silc_server_check_watcher_list(server, client, NULL, 0);
2123 /* Create new server. This processes received New Server packet and
2124 saves the received Server ID. The server is our locally connected
2125 server thus we save all the information and save it to local list.
2126 This funtion can be used by both normal server and router server.
2127 If normal server uses this it means that its router has connected
2128 to the server. If router uses this it means that one of the cell's
2129 servers is connected to the router. */
2131 SilcServerEntry silc_server_new_server(SilcServer server,
2132 SilcSocketConnection sock,
2133 SilcPacketContext *packet)
2135 SilcBuffer buffer = packet->buffer;
2136 SilcServerEntry new_server, server_entry;
2137 SilcServerID *server_id;
2138 SilcIDListData idata;
2139 unsigned char *server_name, *id_string;
2140 SilcUInt16 id_len, name_len;
2144 SILC_LOG_DEBUG(("Creating new server"));
2146 if (sock->type != SILC_SOCKET_TYPE_SERVER &&
2147 sock->type != SILC_SOCKET_TYPE_ROUTER)
2150 /* Take server entry */
2151 new_server = (SilcServerEntry)sock->user_data;
2152 idata = (SilcIDListData)new_server;
2154 /* Remove the old cache entry */
2155 if (!silc_idcache_del_by_context(server->local_list->servers, new_server)) {
2156 if (!silc_idcache_del_by_context(server->global_list->servers,
2158 SILC_LOG_INFO(("Unauthenticated %s attempted to register to "
2159 "network", (sock->type == SILC_SOCKET_TYPE_SERVER ?
2160 "server" : "router")));
2161 silc_server_disconnect_remote(server, sock,
2162 SILC_STATUS_ERR_NOT_AUTHENTICATED, NULL);
2163 if (sock->user_data)
2164 silc_server_free_sock_user_data(server, sock, NULL);
2170 /* Parse the incoming packet */
2171 ret = silc_buffer_unformat(buffer,
2172 SILC_STR_UI16_NSTRING_ALLOC(&id_string, &id_len),
2173 SILC_STR_UI16_NSTRING_ALLOC(&server_name,
2177 silc_free(id_string);
2178 silc_free(server_name);
2179 silc_server_disconnect_remote(server, sock,
2180 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
2182 if (sock->user_data)
2183 silc_server_free_sock_user_data(server, sock, NULL);
2187 if (id_len > buffer->len) {
2188 silc_free(id_string);
2189 silc_free(server_name);
2190 silc_server_disconnect_remote(server, sock,
2191 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
2193 if (sock->user_data)
2194 silc_server_free_sock_user_data(server, sock, NULL);
2199 server_name[255] = '\0';
2202 server_id = silc_id_str2id(id_string, id_len, SILC_ID_SERVER);
2204 silc_free(id_string);
2205 silc_free(server_name);
2206 silc_server_disconnect_remote(server, sock,
2207 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
2209 if (sock->user_data)
2210 silc_server_free_sock_user_data(server, sock, NULL);
2213 silc_free(id_string);
2215 /* Check for valid server ID */
2216 if (!silc_id_is_valid_server_id(server, server_id, sock)) {
2217 SILC_LOG_INFO(("Invalid server ID sent by %s (%s)",
2218 sock->ip, sock->hostname));
2219 silc_server_disconnect_remote(server, sock,
2220 SILC_STATUS_ERR_BAD_SERVER_ID, NULL);
2221 if (sock->user_data)
2222 silc_server_free_sock_user_data(server, sock, NULL);
2223 silc_free(server_name);
2227 /* Check that we do not have this ID already */
2228 server_entry = silc_idlist_find_server_by_id(server->local_list,
2229 server_id, TRUE, NULL);
2231 silc_idcache_del_by_context(server->local_list->servers, server_entry);
2233 server_entry = silc_idlist_find_server_by_id(server->global_list,
2234 server_id, TRUE, NULL);
2236 silc_idcache_del_by_context(server->global_list->servers, server_entry);
2239 /* Update server entry */
2240 idata->status |= SILC_IDLIST_STATUS_REGISTERED;
2241 new_server->server_name = server_name;
2242 new_server->id = server_id;
2244 SILC_LOG_DEBUG(("New server id(%s)",
2245 silc_id_render(server_id, SILC_ID_SERVER)));
2247 /* Add again the entry to the ID cache. */
2248 silc_idcache_add(local ? server->local_list->servers :
2249 server->global_list->servers, server_name, server_id,
2250 new_server, 0, NULL);
2252 /* Distribute the information about new server in the SILC network
2253 to our router. If we are normal server we won't send anything
2254 since this connection must be our router connection. */
2255 if (server->server_type == SILC_ROUTER && !server->standalone &&
2256 server->router->connection != sock)
2257 silc_server_send_new_id(server, server->router->connection,
2258 TRUE, new_server->id, SILC_ID_SERVER,
2259 silc_id_get_len(server_id, SILC_ID_SERVER));
2261 if (server->server_type == SILC_ROUTER)
2262 server->stat.cell_servers++;
2264 /* Check whether this router connection has been replaced by an
2265 backup router. If it has been then we'll disable the server and will
2266 ignore everything it will send until the backup router resuming
2267 protocol has been completed. */
2268 if (sock->type == SILC_SOCKET_TYPE_ROUTER &&
2269 silc_server_backup_replaced_get(server, server_id, NULL)) {
2270 /* Send packet to the server indicating that it cannot use this
2271 connection as it has been replaced by backup router. */
2272 SilcBuffer packet = silc_buffer_alloc(2);
2273 silc_buffer_pull_tail(packet, SILC_BUFFER_END(packet));
2274 silc_buffer_format(packet,
2275 SILC_STR_UI_CHAR(SILC_SERVER_BACKUP_REPLACED),
2276 SILC_STR_UI_CHAR(0),
2278 silc_server_packet_send(server, sock,
2279 SILC_PACKET_RESUME_ROUTER, 0,
2280 packet->data, packet->len, TRUE);
2281 silc_buffer_free(packet);
2283 /* Mark the router disabled. The data sent earlier will go but nothing
2284 after this does not go to this connection. */
2285 idata->status |= SILC_IDLIST_STATUS_DISABLED;
2287 /* If it is router announce our stuff to it. */
2288 if (sock->type == SILC_SOCKET_TYPE_ROUTER &&
2289 server->server_type == SILC_ROUTER) {
2290 silc_server_announce_servers(server, FALSE, 0, sock);
2291 silc_server_announce_clients(server, 0, sock);
2292 silc_server_announce_channels(server, 0, sock);
2299 /* Processes incoming New ID packet. New ID Payload is used to distribute
2300 information about newly registered clients and servers. */
2302 static void silc_server_new_id_real(SilcServer server,
2303 SilcSocketConnection sock,
2304 SilcPacketContext *packet,
2307 SilcBuffer buffer = packet->buffer;
2309 SilcServerEntry router, server_entry;
2310 SilcSocketConnection router_sock;
2315 SILC_LOG_DEBUG(("Processing new ID"));
2317 if (sock->type == SILC_SOCKET_TYPE_CLIENT ||
2318 server->server_type == SILC_SERVER ||
2319 packet->src_id_type != SILC_ID_SERVER)
2322 idp = silc_id_payload_parse(buffer->data, buffer->len);
2326 id_type = silc_id_payload_get_type(idp);
2328 /* Normal server cannot have other normal server connections */
2329 server_entry = (SilcServerEntry)sock->user_data;
2330 if (id_type == SILC_ID_SERVER && sock->type == SILC_SOCKET_TYPE_SERVER &&
2331 server_entry->server_type == SILC_SERVER)
2334 id = silc_id_payload_get_id(idp);
2338 /* If the packet is coming from server then use the sender as the
2339 origin of the the packet. If it came from router then check the real
2340 sender of the packet and use that as the origin. */
2341 if (sock->type == SILC_SOCKET_TYPE_SERVER) {
2342 id_list = server->local_list;
2344 router = sock->user_data;
2346 /* If the sender is backup router and ID is server (and we are not
2347 backup router) then switch the entry to global list. */
2348 if (server_entry->server_type == SILC_BACKUP_ROUTER &&
2349 id_type == SILC_ID_SERVER &&
2350 server->id_entry->server_type != SILC_BACKUP_ROUTER) {
2351 id_list = server->global_list;
2352 router_sock = server->router ? server->router->connection : sock;
2355 void *sender_id = silc_id_str2id(packet->src_id, packet->src_id_len,
2356 packet->src_id_type);
2357 router = silc_idlist_find_server_by_id(server->global_list,
2358 sender_id, TRUE, NULL);
2360 router = silc_idlist_find_server_by_id(server->local_list,
2361 sender_id, TRUE, NULL);
2362 silc_free(sender_id);
2364 id_list = server->global_list;
2371 case SILC_ID_CLIENT:
2373 SilcClientEntry entry;
2375 /* Check that we do not have this client already */
2376 entry = silc_idlist_find_client_by_id(server->global_list,
2377 id, server->server_type,
2380 entry = silc_idlist_find_client_by_id(server->local_list,
2381 id, server->server_type,
2384 SILC_LOG_DEBUG(("Ignoring client that we already have"));
2388 SILC_LOG_DEBUG(("New client id(%s) from [%s] %s",
2389 silc_id_render(id, SILC_ID_CLIENT),
2390 sock->type == SILC_SOCKET_TYPE_SERVER ?
2391 "Server" : "Router", sock->hostname));
2393 /* As a router we keep information of all global information in our
2394 global list. Cell wide information however is kept in the local
2396 entry = silc_idlist_add_client(id_list, NULL, NULL, NULL,
2397 id, router, NULL, 0);
2399 SILC_LOG_ERROR(("Could not add new client to the ID Cache"));
2401 /* Inform the sender that the ID is not usable */
2402 silc_server_send_notify_signoff(server, sock, FALSE, id, NULL);
2405 entry->nickname = NULL;
2406 entry->data.status |= SILC_IDLIST_STATUS_REGISTERED;
2408 if (sock->type == SILC_SOCKET_TYPE_SERVER)
2409 server->stat.cell_clients++;
2410 server->stat.clients++;
2412 /* Check if anyone is watching this nickname */
2413 if (server->server_type == SILC_ROUTER && id_list == server->local_list)
2414 silc_server_check_watcher_list(server, entry, NULL, 0);
2418 case SILC_ID_SERVER:
2420 SilcServerEntry entry;
2422 /* If the ID is mine, ignore it. */
2423 if (SILC_ID_SERVER_COMPARE(id, server->id)) {
2424 SILC_LOG_DEBUG(("Ignoring my own ID as new ID"));
2428 /* If the ID is the sender's ID, ignore it (we have it already) */
2429 if (SILC_ID_SERVER_COMPARE(id, router->id)) {
2430 SILC_LOG_DEBUG(("Ignoring sender's own ID"));
2434 /* Check that we do not have this server already */
2435 entry = silc_idlist_find_server_by_id(server->global_list,
2436 id, server->server_type,
2439 entry = silc_idlist_find_server_by_id(server->local_list,
2440 id, server->server_type,
2443 SILC_LOG_DEBUG(("Ignoring server that we already have"));
2447 SILC_LOG_DEBUG(("New server id(%s) from [%s] %s",
2448 silc_id_render(id, SILC_ID_SERVER),
2449 sock->type == SILC_SOCKET_TYPE_SERVER ?
2450 "Server" : "Router", sock->hostname));
2452 /* As a router we keep information of all global information in our
2453 global list. Cell wide information however is kept in the local
2455 entry = silc_idlist_add_server(id_list, NULL, 0, id, router,
2458 SILC_LOG_ERROR(("Could not add new server to the ID Cache"));
2461 entry->data.status |= SILC_IDLIST_STATUS_REGISTERED;
2463 if (sock->type == SILC_SOCKET_TYPE_SERVER)
2464 server->stat.cell_servers++;
2465 server->stat.servers++;
2469 case SILC_ID_CHANNEL:
2470 SILC_LOG_ERROR(("Channel cannot be registered with NEW_ID packet"));
2479 /* If the sender of this packet is server and we are router we need to
2480 broadcast this packet to other routers in the network. */
2481 if (broadcast && !server->standalone && server->server_type == SILC_ROUTER &&
2482 sock->type == SILC_SOCKET_TYPE_SERVER &&
2483 !(packet->flags & SILC_PACKET_FLAG_BROADCAST)) {
2484 SILC_LOG_DEBUG(("Broadcasting received New ID packet"));
2485 silc_server_packet_send(server, server->router->connection,
2487 packet->flags | SILC_PACKET_FLAG_BROADCAST,
2488 buffer->data, buffer->len, FALSE);
2489 silc_server_backup_send(server, (SilcServerEntry)sock->user_data,
2490 packet->type, packet->flags,
2491 packet->buffer->data, packet->buffer->len,
2496 silc_id_payload_free(idp);
2500 /* Processes incoming New ID packet. New ID Payload is used to distribute
2501 information about newly registered clients and servers. */
2503 void silc_server_new_id(SilcServer server, SilcSocketConnection sock,
2504 SilcPacketContext *packet)
2506 silc_server_new_id_real(server, sock, packet, TRUE);
2509 /* Receoved New Id List packet, list of New ID payloads inside one
2510 packet. Process the New ID payloads one by one. */
2512 void silc_server_new_id_list(SilcServer server, SilcSocketConnection sock,
2513 SilcPacketContext *packet)
2515 SilcPacketContext *new_id;
2519 SILC_LOG_DEBUG(("Processing New ID List"));
2521 if (sock->type == SILC_SOCKET_TYPE_CLIENT ||
2522 packet->src_id_type != SILC_ID_SERVER)
2525 /* If the sender of this packet is server and we are router we need to
2526 broadcast this packet to other routers in the network. Broadcast
2527 this list packet instead of multiple New ID packets. */
2528 if (!server->standalone && server->server_type == SILC_ROUTER &&
2529 sock->type == SILC_SOCKET_TYPE_SERVER &&
2530 !(packet->flags & SILC_PACKET_FLAG_BROADCAST)) {
2531 SILC_LOG_DEBUG(("Broadcasting received New ID List packet"));
2532 silc_server_packet_send(server, server->router->connection,
2534 packet->flags | SILC_PACKET_FLAG_BROADCAST,
2535 packet->buffer->data, packet->buffer->len, FALSE);
2536 silc_server_backup_send(server, (SilcServerEntry)sock->user_data,
2537 packet->type, packet->flags,
2538 packet->buffer->data, packet->buffer->len,
2542 /* Make copy of the original packet context, except for the actual
2543 data buffer, which we will here now fetch from the original buffer. */
2544 new_id = silc_packet_context_alloc();
2545 new_id->type = SILC_PACKET_NEW_ID;
2546 new_id->flags = packet->flags;
2547 new_id->src_id = packet->src_id;
2548 new_id->src_id_len = packet->src_id_len;
2549 new_id->src_id_type = packet->src_id_type;
2550 new_id->dst_id = packet->dst_id;
2551 new_id->dst_id_len = packet->dst_id_len;
2552 new_id->dst_id_type = packet->dst_id_type;
2554 idp = silc_buffer_alloc(256);
2555 new_id->buffer = idp;
2557 while (packet->buffer->len) {
2558 SILC_GET16_MSB(id_len, packet->buffer->data + 2);
2559 if ((id_len > packet->buffer->len) ||
2560 (id_len > idp->truelen))
2563 silc_buffer_pull_tail(idp, 4 + id_len);
2564 silc_buffer_put(idp, packet->buffer->data, 4 + id_len);
2566 /* Process the New ID */
2567 silc_server_new_id_real(server, sock, new_id, FALSE);
2569 silc_buffer_push_tail(idp, 4 + id_len);
2570 silc_buffer_pull(packet->buffer, 4 + id_len);
2573 silc_buffer_free(idp);
2577 /* Received New Channel packet. Information about new channels in the
2578 network are distributed using this packet. Save the information about
2579 the new channel. This usually comes from router but also normal server
2580 can send this to notify channels it has when it connects to us. */
2582 void silc_server_new_channel(SilcServer server,
2583 SilcSocketConnection sock,
2584 SilcPacketContext *packet)
2586 SilcChannelPayload payload;
2587 SilcChannelID *channel_id;
2589 SilcUInt32 name_len;
2593 SilcServerEntry server_entry;
2594 SilcChannelEntry channel;
2596 SILC_LOG_DEBUG(("Processing New Channel"));
2598 if (sock->type == SILC_SOCKET_TYPE_CLIENT ||
2599 packet->src_id_type != SILC_ID_SERVER ||
2600 server->server_type == SILC_SERVER)
2603 /* Parse the channel payload */
2604 payload = silc_channel_payload_parse(packet->buffer->data,
2605 packet->buffer->len);
2609 /* Get the channel ID */
2610 channel_id = silc_channel_get_id_parse(payload);
2612 silc_channel_payload_free(payload);
2616 channel_name = silc_channel_get_name(payload, &name_len);
2618 channel_name[255] = '\0';
2620 id = silc_channel_get_id(payload, &id_len);
2622 server_entry = (SilcServerEntry)sock->user_data;
2624 if (sock->type == SILC_SOCKET_TYPE_ROUTER) {
2625 /* Add the channel to global list as it is coming from router. It
2626 cannot be our own channel as it is coming from router. */
2628 /* Check that we don't already have this channel */
2629 channel = silc_idlist_find_channel_by_name(server->local_list,
2630 channel_name, NULL);
2632 channel = silc_idlist_find_channel_by_name(server->global_list,
2633 channel_name, NULL);
2635 SILC_LOG_DEBUG(("New channel id(%s) from [Router] %s",
2636 silc_id_render(channel_id, SILC_ID_CHANNEL),
2640 silc_idlist_add_channel(server->global_list, strdup(channel_name),
2641 0, channel_id, sock->user_data, NULL, NULL, 0);
2644 channel->disabled = TRUE;
2646 server->stat.channels++;
2647 if (server->server_type == SILC_ROUTER)
2648 channel->users_resolved = TRUE;
2651 /* The channel is coming from our server, thus it is in our cell
2652 we will add it to our local list. */
2655 SILC_LOG_DEBUG(("Channel id(%s) from [Server] %s",
2656 silc_id_render(channel_id, SILC_ID_CHANNEL),
2659 /* Check that we don't already have this channel */
2660 channel = silc_idlist_find_channel_by_name(server->local_list,
2661 channel_name, NULL);
2663 channel = silc_idlist_find_channel_by_name(server->global_list,
2664 channel_name, NULL);
2666 /* If the channel does not exist, then create it. This creates a new
2667 key to the channel as well that we will send to the server. */
2669 /* The protocol says that the Channel ID's IP address must be based
2670 on the router's IP address. Check whether the ID is based in our
2671 IP and if it is not then create a new ID and enforce the server
2672 to switch the ID. */
2673 if (server_entry->server_type != SILC_BACKUP_ROUTER &&
2674 !SILC_ID_COMPARE(channel_id, server->id, server->id->ip.data_len)) {
2676 SILC_LOG_DEBUG(("Forcing the server to change Channel ID"));
2678 if (silc_id_create_channel_id(server, server->id, server->rng, &tmp)) {
2679 silc_server_send_notify_channel_change(server, sock, FALSE,
2681 silc_free(channel_id);
2686 /* Create the channel with the provided Channel ID */
2687 channel = silc_server_create_new_channel_with_id(server, NULL, NULL,
2691 silc_channel_payload_free(payload);
2692 silc_free(channel_id);
2695 channel->disabled = TRUE;
2698 /* CMODE change notify is expected */
2699 /* Get the mode and set it to the channel */
2700 channel->mode = silc_channel_get_mode(payload);
2703 /* Send the new channel key to the server */
2704 id = silc_id_id2str(channel->id, SILC_ID_CHANNEL);
2705 id_len = silc_id_get_len(channel->id, SILC_ID_CHANNEL);
2706 chk = silc_channel_key_payload_encode(id_len, id,
2707 strlen(channel->channel_key->
2709 channel->channel_key->cipher->name,
2710 channel->key_len / 8,
2712 silc_server_packet_send(server, sock, SILC_PACKET_CHANNEL_KEY, 0,
2713 chk->data, chk->len, FALSE);
2714 silc_buffer_free(chk);
2717 /* The channel exist by that name, check whether the ID's match.
2718 If they don't then we'll force the server to use the ID we have.
2719 We also create a new key for the channel. */
2720 SilcBuffer modes = NULL, users = NULL, users_modes = NULL;
2722 if (!SILC_ID_CHANNEL_COMPARE(channel_id, channel->id)) {
2723 /* They don't match, send CHANNEL_CHANGE notify to the server to
2724 force the ID change. */
2725 SILC_LOG_DEBUG(("Forcing the server to change Channel ID"));
2726 silc_server_send_notify_channel_change(server, sock, FALSE,
2727 channel_id, channel->id);
2730 /* If the mode is different from what we have then enforce the
2732 mode = silc_channel_get_mode(payload);
2733 if (channel->mode != mode) {
2734 SILC_LOG_DEBUG(("Forcing the server to change channel mode"));
2735 silc_server_send_notify_cmode(server, sock, FALSE, channel,
2736 channel->mode, server->id,
2737 SILC_ID_SERVER, channel->cipher,
2739 channel->passphrase,
2740 channel->founder_key);
2743 /* Create new key for the channel and send it to the server and
2744 everybody else possibly on the channel. */
2745 if (!(channel->mode & SILC_CHANNEL_MODE_PRIVKEY)) {
2746 if (!silc_server_create_channel_key(server, channel, 0))
2749 /* Send to the channel */
2750 silc_server_send_channel_key(server, sock, channel, FALSE);
2751 id = silc_id_id2str(channel->id, SILC_ID_CHANNEL);
2752 id_len = silc_id_get_len(channel->id, SILC_ID_CHANNEL);
2754 /* Send to the server */
2755 chk = silc_channel_key_payload_encode(id_len, id,
2756 strlen(channel->channel_key->
2758 channel->channel_key->
2760 channel->key_len / 8,
2762 silc_server_packet_send(server, sock, SILC_PACKET_CHANNEL_KEY, 0,
2763 chk->data, chk->len, FALSE);
2764 silc_buffer_free(chk);
2768 silc_free(channel_id);
2770 /* Since the channel is coming from server and we also know about it
2771 then send the JOIN notify to the server so that it see's our
2772 users on the channel "joining" the channel. */
2773 silc_server_announce_get_channel_users(server, channel, &modes, &users,
2776 silc_buffer_push(modes, modes->data - modes->head);
2777 silc_server_packet_send_dest(server, sock,
2778 SILC_PACKET_NOTIFY, SILC_PACKET_FLAG_LIST,
2779 channel->id, SILC_ID_CHANNEL,
2780 modes->data, modes->len, FALSE);
2781 silc_buffer_free(modes);
2784 silc_buffer_push(users, users->data - users->head);
2785 silc_server_packet_send(server, sock,
2786 SILC_PACKET_NOTIFY, SILC_PACKET_FLAG_LIST,
2787 users->data, users->len, FALSE);
2788 silc_buffer_free(users);
2791 silc_buffer_push(users_modes, users_modes->data - users_modes->head);
2792 silc_server_packet_send_dest(server, sock,
2793 SILC_PACKET_NOTIFY, SILC_PACKET_FLAG_LIST,
2794 channel->id, SILC_ID_CHANNEL,
2796 users_modes->len, FALSE);
2797 silc_buffer_free(users_modes);
2802 silc_channel_payload_free(payload);
2805 /* Received New Channel List packet, list of New Channel List payloads inside
2806 one packet. Process the New Channel payloads one by one. */
2808 void silc_server_new_channel_list(SilcServer server,
2809 SilcSocketConnection sock,
2810 SilcPacketContext *packet)
2812 SilcPacketContext *new;
2814 SilcUInt16 len1, len2;
2816 SILC_LOG_DEBUG(("Processing New Channel List"));
2818 if (sock->type == SILC_SOCKET_TYPE_CLIENT ||
2819 packet->src_id_type != SILC_ID_SERVER ||
2820 server->server_type == SILC_SERVER)
2823 /* If the sender of this packet is server and we are router we need to
2824 broadcast this packet to other routers in the network. Broadcast
2825 this list packet instead of multiple New Channel packets. */
2826 if (!server->standalone && server->server_type == SILC_ROUTER &&
2827 sock->type == SILC_SOCKET_TYPE_SERVER &&
2828 !(packet->flags & SILC_PACKET_FLAG_BROADCAST)) {
2829 SILC_LOG_DEBUG(("Broadcasting received New Channel List packet"));
2830 silc_server_packet_send(server, server->router->connection,
2832 packet->flags | SILC_PACKET_FLAG_BROADCAST,
2833 packet->buffer->data, packet->buffer->len, FALSE);
2834 silc_server_backup_send(server, (SilcServerEntry)sock->user_data,
2835 packet->type, packet->flags,
2836 packet->buffer->data, packet->buffer->len,
2840 /* Make copy of the original packet context, except for the actual
2841 data buffer, which we will here now fetch from the original buffer. */
2842 new = silc_packet_context_alloc();
2843 new->type = SILC_PACKET_NEW_CHANNEL;
2844 new->flags = packet->flags;
2845 new->src_id = packet->src_id;
2846 new->src_id_len = packet->src_id_len;
2847 new->src_id_type = packet->src_id_type;
2848 new->dst_id = packet->dst_id;
2849 new->dst_id_len = packet->dst_id_len;
2850 new->dst_id_type = packet->dst_id_type;
2852 buffer = silc_buffer_alloc(512);
2853 new->buffer = buffer;
2855 while (packet->buffer->len) {
2856 SILC_GET16_MSB(len1, packet->buffer->data);
2857 if ((len1 > packet->buffer->len) ||
2858 (len1 > buffer->truelen))
2861 SILC_GET16_MSB(len2, packet->buffer->data + 2 + len1);
2862 if ((len2 > packet->buffer->len) ||
2863 (len2 > buffer->truelen))
2866 silc_buffer_pull_tail(buffer, 8 + len1 + len2);
2867 silc_buffer_put(buffer, packet->buffer->data, 8 + len1 + len2);
2869 /* Process the New Channel */
2870 silc_server_new_channel(server, sock, new);
2872 silc_buffer_push_tail(buffer, 8 + len1 + len2);
2873 silc_buffer_pull(packet->buffer, 8 + len1 + len2);
2876 silc_buffer_free(buffer);
2880 /* Received key agreement packet. This packet is never for us. It is to
2881 the client in the packet's destination ID. Sending of this sort of packet
2882 equals sending private message, ie. it is sent point to point from
2883 one client to another. */
2885 void silc_server_key_agreement(SilcServer server,
2886 SilcSocketConnection sock,
2887 SilcPacketContext *packet)
2889 SilcSocketConnection dst_sock;
2890 SilcIDListData idata;
2892 SILC_LOG_DEBUG(("Start"));
2894 if (packet->src_id_type != SILC_ID_CLIENT ||
2895 packet->dst_id_type != SILC_ID_CLIENT)
2898 if (!packet->dst_id)
2901 /* Get the route to the client */
2902 dst_sock = silc_server_get_client_route(server, packet->dst_id,
2903 packet->dst_id_len, NULL,
2908 /* Relay the packet */
2909 silc_server_relay_packet(server, dst_sock, idata->send_key,
2910 idata->hmac_send, idata->psn_send++,
2914 /* Received connection auth request packet that is used during connection
2915 phase to resolve the mandatory authentication method. This packet can
2916 actually be received at anytime but usually it is used only during
2917 the connection authentication phase. Now, protocol says that this packet
2918 can come from client or server, however, we support only this coming
2919 from client and expect that server always knows what authentication
2922 void silc_server_connection_auth_request(SilcServer server,
2923 SilcSocketConnection sock,
2924 SilcPacketContext *packet)
2926 SilcServerConfigClient *client = NULL;
2927 SilcUInt16 conn_type;
2929 SilcAuthMethod auth_meth = SILC_AUTH_NONE;
2931 SILC_LOG_DEBUG(("Start"));
2933 if (packet->src_id_type && packet->src_id_type != SILC_ID_CLIENT)
2936 /* Parse the payload */
2937 ret = silc_buffer_unformat(packet->buffer,
2938 SILC_STR_UI_SHORT(&conn_type),
2939 SILC_STR_UI_SHORT(NULL),
2944 if (conn_type != SILC_SOCKET_TYPE_CLIENT)
2947 /* Get the authentication method for the client */
2948 auth_meth = SILC_AUTH_NONE;
2949 client = silc_server_config_find_client(server, sock->ip);
2951 client = silc_server_config_find_client(server, sock->hostname);
2953 if (client->passphrase) {
2954 if (client->publickeys && !server->config->prefer_passphrase_auth)
2955 auth_meth = SILC_AUTH_PUBLIC_KEY;
2957 auth_meth = SILC_AUTH_PASSWORD;
2958 } else if (client->publickeys)
2959 auth_meth = SILC_AUTH_PUBLIC_KEY;
2962 /* Send it back to the client */
2963 silc_server_send_connection_auth_request(server, sock, conn_type, auth_meth);
2966 /* Received REKEY packet. The sender of the packet wants to regenerate
2967 its session keys. This starts the REKEY protocol. */
2969 void silc_server_rekey(SilcServer server,
2970 SilcSocketConnection sock,
2971 SilcPacketContext *packet)
2973 SilcProtocol protocol;
2974 SilcServerRekeyInternalContext *proto_ctx;
2975 SilcIDListData idata = (SilcIDListData)sock->user_data;
2977 SILC_LOG_DEBUG(("Start"));
2979 /* Allocate internal protocol context. This is sent as context
2981 proto_ctx = silc_calloc(1, sizeof(*proto_ctx));
2982 proto_ctx->server = (void *)server;
2983 proto_ctx->sock = sock;
2984 proto_ctx->responder = TRUE;
2985 proto_ctx->pfs = idata->rekey->pfs;
2987 /* Perform rekey protocol. Will call the final callback after the
2988 protocol is over. */
2989 silc_protocol_alloc(SILC_PROTOCOL_SERVER_REKEY,
2990 &protocol, proto_ctx, silc_server_rekey_final);
2991 sock->protocol = protocol;
2993 if (proto_ctx->pfs == FALSE)
2994 /* Run the protocol */
2995 silc_protocol_execute(protocol, server->schedule, 0, 0);
2998 /* Received file transger packet. This packet is never for us. It is to
2999 the client in the packet's destination ID. Sending of this sort of packet
3000 equals sending private message, ie. it is sent point to point from
3001 one client to another. */
3003 void silc_server_ftp(SilcServer server,
3004 SilcSocketConnection sock,
3005 SilcPacketContext *packet)
3007 SilcSocketConnection dst_sock;
3008 SilcIDListData idata;
3010 SILC_LOG_DEBUG(("Start"));
3012 if (packet->src_id_type != SILC_ID_CLIENT ||
3013 packet->dst_id_type != SILC_ID_CLIENT)
3016 if (!packet->dst_id)
3019 /* Get the route to the client */
3020 dst_sock = silc_server_get_client_route(server, packet->dst_id,
3021 packet->dst_id_len, NULL,
3026 /* Relay the packet */
3027 silc_server_relay_packet(server, dst_sock, idata->send_key,
3028 idata->hmac_send, idata->psn_send++,
3034 SilcSocketConnection sock;
3035 SilcPacketContext *packet;
3037 } *SilcServerResumeResolve;
3039 SILC_SERVER_CMD_FUNC(resume_resolve)
3041 SilcServerResumeResolve r = (SilcServerResumeResolve)context;
3042 SilcServer server = r->server;
3043 SilcSocketConnection sock = r->sock;
3044 SilcServerCommandReplyContext reply = context2;
3045 SilcClientEntry client;
3047 SILC_LOG_DEBUG(("Start"));
3049 if (!reply || !silc_command_get_status(reply->payload, NULL, NULL)) {
3050 SILC_LOG_ERROR(("Client %s (%s) tried to resume unknown client, "
3051 "closing connection", sock->hostname, sock->ip));
3052 silc_server_disconnect_remote(server, sock,
3053 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
3054 "Resuming not possible");
3058 if (reply && silc_command_get(reply->payload) == SILC_COMMAND_WHOIS) {
3059 /* Get entry to the client, and resolve it if we don't have it. */
3060 client = silc_idlist_find_client_by_id(server->local_list,
3061 r->data, TRUE, NULL);
3063 client = silc_idlist_find_client_by_id(server->global_list,
3064 r->data, TRUE, NULL);
3066 SILC_LOG_ERROR(("Client %s (%s) tried to resume unknown client, "
3067 "closing connection", sock->hostname, sock->ip));
3068 silc_server_disconnect_remote(server, sock,
3069 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
3070 "Resuming not possible");
3075 if (!(client->mode & SILC_UMODE_DETACHED)) {
3076 SILC_LOG_ERROR(("Client %s (%s) tried to resume un-detached client, "
3077 "closing connection", sock->hostname, sock->ip));
3078 silc_server_disconnect_remote(server, sock,
3079 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
3080 "Resuming not possible");
3085 /* Reprocess the packet */
3086 silc_server_resume_client(server, sock, r->packet);
3089 silc_socket_free(r->sock);
3090 silc_packet_context_free(r->packet);
3095 /* Received client resuming packet. This is used to resume detached
3096 client session. It can be sent by the client who wishes to resume
3097 but this is also sent by servers and routers to notify other routers
3098 that the client is not detached anymore. */
3100 void silc_server_resume_client(SilcServer server,
3101 SilcSocketConnection sock,
3102 SilcPacketContext *packet)
3104 SilcBuffer buffer = packet->buffer, buf;
3105 SilcIDListData idata;
3106 SilcClientEntry detached_client;
3107 SilcClientID *client_id = NULL;
3108 unsigned char *id_string, *auth = NULL;
3109 SilcUInt16 id_len, auth_len = 0;
3110 int ret, nickfail = 0;
3111 bool resolved, local, nick_change = FALSE, resolve = FALSE;
3112 SilcChannelEntry channel;
3113 SilcHashTableList htl;
3114 SilcChannelClientEntry chl;
3115 SilcServerResumeResolve r;
3117 SILC_LOG_DEBUG(("Start"));
3119 ret = silc_buffer_unformat(buffer,
3120 SILC_STR_UI16_NSTRING(&id_string, &id_len),
3123 client_id = silc_id_str2id(id_string, id_len, SILC_ID_CLIENT);
3125 if (sock->type == SILC_SOCKET_TYPE_CLIENT) {
3126 /* Client send this and is attempting to resume to old client session */
3127 SilcClientEntry client;
3131 silc_buffer_pull(buffer, 2 + id_len);
3132 auth = buffer->data;
3133 auth_len = buffer->len;
3134 silc_buffer_push(buffer, 2 + id_len);
3137 if (!client_id || auth_len < 128) {
3138 SILC_LOG_ERROR(("Client %s (%s) sent incomplete resume information, "
3139 "closing connection", sock->hostname, sock->ip));
3140 silc_server_disconnect_remote(server, sock,
3141 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
3142 "Resuming not possible");
3146 /* Take client entry of this connection */
3147 client = (SilcClientEntry)sock->user_data;
3148 idata = (SilcIDListData)client;
3150 /* Get entry to the client, and resolve it if we don't have it. */
3151 detached_client = silc_server_get_client_resolve(server, client_id, FALSE,
3153 if (!detached_client) {
3155 /* The client info is being resolved. Reprocess this packet after
3156 receiving the reply to the query. */
3157 SILC_LOG_DEBUG(("Resolving client"));
3158 r = silc_calloc(1, sizeof(*r));
3162 r->sock = silc_socket_dup(sock);
3163 r->packet = silc_packet_context_dup(packet);
3164 r->data = silc_id_dup(client_id, SILC_ID_CLIENT);
3165 silc_server_command_pending(server, SILC_COMMAND_WHOIS,
3167 silc_server_command_resume_resolve, r);
3169 SILC_LOG_ERROR(("Client %s (%s) tried to resume unknown client, "
3170 "closing connection", sock->hostname, sock->ip));
3171 silc_server_disconnect_remote(server, sock,
3172 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
3173 "Resuming not possible");
3178 if (!(detached_client->mode & SILC_UMODE_DETACHED))
3180 if (!silc_hash_table_count(detached_client->channels) &&
3181 detached_client->router)
3183 if (!detached_client->nickname)
3187 if (server->server_type == SILC_SERVER && !server->standalone) {
3188 /* The client info is being resolved. Reprocess this packet after
3189 receiving the reply to the query. */
3190 SILC_LOG_DEBUG(("Resolving client info"));
3191 silc_server_get_client_resolve(server, client_id, TRUE, NULL);
3192 r = silc_calloc(1, sizeof(*r));
3196 r->sock = silc_socket_dup(sock);
3197 r->packet = silc_packet_context_dup(packet);
3198 r->data = silc_id_dup(client_id, SILC_ID_CLIENT);
3199 silc_server_command_pending(server, SILC_COMMAND_WHOIS,
3201 silc_server_command_resume_resolve, r);
3204 if (server->server_type == SILC_SERVER) {
3205 SILC_LOG_ERROR(("Client %s (%s) tried to resume un-detached client, "
3206 "closing connection", sock->hostname, sock->ip));
3207 silc_server_disconnect_remote(server, sock,
3208 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
3209 "Resuming not possible");
3214 /* Check that we have the public key of the client, if not then we must
3215 resolve it first. */
3216 if (!detached_client->data.public_key) {
3217 if (server->server_type == SILC_SERVER && server->standalone) {
3218 silc_server_disconnect_remote(server, sock,
3219 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
3220 "Resuming not possible");
3222 /* We must retrieve the detached client's public key by sending
3223 GETKEY command. Reprocess this packet after receiving the key */
3224 SilcBuffer idp = silc_id_payload_encode(client_id, SILC_ID_CLIENT);
3225 SilcSocketConnection dest_sock =
3226 silc_server_get_client_route(server, NULL, 0, client_id, NULL, NULL);
3228 SILC_LOG_DEBUG(("Resolving client public key"));
3230 silc_server_send_command(server, dest_sock ? dest_sock :
3231 server->router->connection,
3232 SILC_COMMAND_GETKEY, ++server->cmd_ident,
3233 1, 1, idp->data, idp->len);
3235 r = silc_calloc(1, sizeof(*r));
3240 r->sock = silc_socket_dup(sock);
3241 r->packet = silc_packet_context_dup(packet);
3242 silc_server_command_pending(server, SILC_COMMAND_GETKEY,
3244 silc_server_command_resume_resolve, r);
3246 silc_buffer_free(idp);
3249 } else if (!silc_pkcs_public_key_compare(detached_client->data.public_key,
3250 idata->public_key)) {
3251 /* We require that the connection and resuming authentication data
3252 must be using same key pair. */
3253 silc_server_disconnect_remote(server, sock,
3254 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
3255 "Resuming not possible");
3259 /* Verify the authentication payload. This has to be successful in
3260 order to allow the resuming */
3262 !silc_auth_verify_data(auth, auth_len, SILC_AUTH_PUBLIC_KEY,
3263 detached_client->data.public_key, 0,
3264 idata->hash, detached_client->id,
3266 SILC_LOG_ERROR(("Client %s (%s) resume authentication failed, "
3267 "closing connection", sock->hostname, sock->ip));
3268 silc_server_disconnect_remote(server, sock,
3269 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
3270 "Resuming not possible");
3274 /* Now resume the client to the network */
3276 silc_schedule_task_del_by_context(server->schedule, detached_client);
3277 sock->user_data = detached_client;
3278 detached_client->connection = sock;
3280 /* Take new keys and stuff into use in the old entry */
3281 silc_idlist_del_data(detached_client);
3282 silc_idlist_add_data(detached_client, idata);
3283 detached_client->data.status |= SILC_IDLIST_STATUS_REGISTERED;
3284 detached_client->data.status |= SILC_IDLIST_STATUS_RESUMED;
3285 detached_client->mode &= ~SILC_UMODE_DETACHED;
3287 /* Send the RESUME_CLIENT packet to our primary router so that others
3288 know this client isn't detached anymore. */
3289 buf = silc_buffer_alloc_size(2 + id_len);
3290 silc_buffer_format(buf,
3291 SILC_STR_UI_SHORT(id_len),
3292 SILC_STR_UI_XNSTRING(id_string, id_len),
3295 /* Send to primary router */
3296 if (!server->standalone)
3297 silc_server_packet_send(server, server->router->connection,
3298 SILC_PACKET_RESUME_CLIENT, 0,
3299 buf->data, buf->len, TRUE);
3301 /* As router we must deliver this packet directly to the original
3302 server whom this client was earlier. */
3303 if (server->server_type == SILC_ROUTER && detached_client->router &&
3304 detached_client->router->server_type != SILC_ROUTER)
3305 silc_server_packet_send(server, detached_client->router->connection,
3306 SILC_PACKET_RESUME_CLIENT, 0,
3307 buf->data, buf->len, TRUE);
3308 silc_buffer_free(buf);
3310 detached_client->router = NULL;
3312 /* Delete this client entry since we're resuming to old one. */
3313 server->stat.my_clients--;
3314 server->stat.clients--;
3315 if (server->stat.cell_clients)
3316 server->stat.cell_clients--;
3317 silc_idlist_del_client(server->local_list, client);
3318 client = detached_client;
3320 /* If the ID is not based in our ID then change it */
3321 if (!SILC_ID_COMPARE(client->id, server->id, server->id->ip.data_len)) {
3322 while (!silc_id_create_client_id(server, server->id, server->rng,
3323 server->md5hash, client->nickname,
3327 silc_server_disconnect_remote(server, sock,
3328 SILC_STATUS_ERR_BAD_NICKNAME, NULL);
3331 snprintf(&client->nickname[strlen(client->nickname) - 1], 1,
3338 /* Notify about Client ID change, nickname doesn't actually change. */
3339 if (!server->standalone)
3340 silc_server_send_notify_nick_change(server, server->router->connection,
3341 FALSE, client->id, client_id,
3345 /* Resolve users on those channels that client has joined but we
3346 haven't resolved user list yet. */
3347 if (server->server_type == SILC_SERVER && !server->standalone) {
3348 silc_hash_table_list(client->channels, &htl);
3349 while (silc_hash_table_get(&htl, NULL, (void **)&chl)) {
3350 channel = chl->channel;
3351 SILC_LOG_DEBUG(("Resolving users for %s channel",
3352 channel->channel_name));
3353 if (channel->disabled || !channel->users_resolved) {
3354 silc_server_send_command(server, server->router->connection,
3355 SILC_COMMAND_USERS, ++server->cmd_ident,
3356 1, 2, channel->channel_name,
3357 strlen(channel->channel_name));
3360 silc_hash_table_list_reset(&htl);
3363 /* Send the new client ID to the client. After this client may start
3364 receiving other packets, and may start sending packets too. */
3365 silc_server_send_new_id(server, sock, FALSE, client_id, SILC_ID_CLIENT,
3366 silc_id_get_len(client_id, SILC_ID_CLIENT));
3369 /* Send NICK change notify to channels as well. */
3370 SilcBuffer oidp, nidp;
3371 oidp = silc_id_payload_encode(client->id, SILC_ID_CLIENT);
3372 nidp = silc_id_payload_encode(client_id, SILC_ID_CLIENT);
3373 silc_server_send_notify_on_channels(server, NULL, client,
3374 SILC_NOTIFY_TYPE_NICK_CHANGE, 3,
3375 oidp->data, oidp->len,
3376 nidp->data, nidp->len,
3378 strlen(client->nickname));
3379 silc_buffer_free(oidp);
3380 silc_buffer_free(nidp);
3383 /* Add the client again to the ID cache to get it to correct list */
3384 if (!silc_idcache_del_by_context(server->local_list->clients, client))
3385 silc_idcache_del_by_context(server->global_list->clients, client);
3386 silc_free(client->id);
3387 client->id = client_id;
3389 silc_idcache_add(server->local_list->clients, client->nickname,
3390 client->id, client, 0, NULL);
3392 /* Send some nice info to the client */
3393 silc_server_send_connect_notifys(server, sock, client);
3395 /* Send all channel keys of channels the client has joined */
3396 silc_hash_table_list(client->channels, &htl);
3397 while (silc_hash_table_get(&htl, NULL, (void **)&chl)) {
3398 bool created = FALSE;
3399 channel = chl->channel;
3401 if (channel->mode & SILC_CHANNEL_MODE_PRIVKEY)
3404 /* If we don't have channel key, then create one */
3405 if (!channel->channel_key) {
3406 if (!silc_server_create_channel_key(server, channel, 0))
3411 id_string = silc_id_id2str(channel->id, SILC_ID_CHANNEL);
3413 silc_channel_key_payload_encode(silc_id_get_len(channel->id,
3416 strlen(channel->channel_key->
3418 channel->channel_key->cipher->name,
3419 channel->key_len / 8, channel->key);
3420 silc_free(id_string);
3422 /* Send the key packet to client */
3423 silc_server_packet_send(server, sock, SILC_PACKET_CHANNEL_KEY, 0,
3424 keyp->data, keyp->len, FALSE);
3426 if (created && server->server_type == SILC_SERVER &&
3427 !server->standalone)
3428 silc_server_packet_send(server, server->router->connection,
3429 SILC_PACKET_CHANNEL_KEY, 0,
3430 keyp->data, keyp->len, FALSE);
3432 silc_buffer_free(keyp);
3434 silc_hash_table_list_reset(&htl);
3436 } else if (sock->type != SILC_SOCKET_TYPE_CLIENT) {
3437 /* Server or router sent this to us to notify that that a client has
3439 SilcServerEntry server_entry;
3440 SilcServerID *server_id;
3445 /* Get entry to the client, and resolve it if we don't have it. */
3446 detached_client = silc_idlist_find_client_by_id(server->local_list,
3447 client_id, TRUE, NULL);
3448 if (!detached_client) {
3449 detached_client = silc_idlist_find_client_by_id(server->global_list,
3450 client_id, TRUE, NULL);
3451 if (!detached_client)
3455 /* Check that the client has not been resumed already because it is
3456 protocol error to attempt to resume more than once. The client
3457 will be killed if this protocol error occurs. */
3458 if (detached_client->data.status & SILC_IDLIST_STATUS_RESUMED &&
3459 !(detached_client->mode & SILC_UMODE_DETACHED)) {
3460 /* The client is clearly attempting to resume more than once and
3461 perhaps playing around by resuming from several different places
3462 at the same time. */
3463 silc_server_kill_client(server, detached_client, NULL,
3464 server->id, SILC_ID_SERVER);
3468 /* Check whether client is detached at all */
3469 if (!(detached_client->mode & SILC_UMODE_DETACHED))
3472 /* Client is detached, and now it is resumed. Remove the detached
3473 mode and mark that it is resumed. */
3474 detached_client->mode &= ~SILC_UMODE_DETACHED;
3475 detached_client->data.status |= SILC_IDLIST_STATUS_RESUMED;
3477 /* Get the new owner of the resumed client */
3478 server_id = silc_id_str2id(packet->src_id, packet->src_id_len,
3479 packet->src_id_type);
3483 /* Get server entry */
3484 server_entry = silc_idlist_find_server_by_id(server->global_list,
3485 server_id, TRUE, NULL);
3487 if (!server_entry) {
3488 server_entry = silc_idlist_find_server_by_id(server->local_list,
3489 server_id, TRUE, NULL);
3491 if (!server_entry) {
3492 silc_free(server_id);
3497 if (server->server_type == SILC_ROUTER &&
3498 sock->type == SILC_SOCKET_TYPE_ROUTER &&
3499 server_entry->server_type == SILC_ROUTER)
3502 SILC_LOG_DEBUG(("Resuming detached client"));
3504 /* Change the client to correct list. */
3505 if (!silc_idcache_del_by_context(server->local_list->clients,
3507 silc_idcache_del_by_context(server->global_list->clients,
3509 silc_idcache_add(local && server->server_type == SILC_ROUTER ?
3510 server->local_list->clients :
3511 server->global_list->clients,
3512 detached_client->nickname,
3513 detached_client->id, detached_client, FALSE, NULL);
3515 /* Change the owner of the client if needed */
3516 if (detached_client->router != server_entry)
3517 detached_client->router = server_entry;
3519 /* Update channel information regarding global clients on channel. */
3520 if (server->server_type == SILC_SERVER) {
3521 silc_hash_table_list(detached_client->channels, &htl);
3522 while (silc_hash_table_get(&htl, NULL, (void **)&chl))
3523 chl->channel->global_users =
3524 silc_server_channel_has_global(chl->channel);
3525 silc_hash_table_list_reset(&htl);
3528 silc_schedule_task_del_by_context(server->schedule, detached_client);
3530 /* If the sender of this packet is server and we are router we need to
3531 broadcast this packet to other routers in the network. */
3532 if (!server->standalone && server->server_type == SILC_ROUTER &&
3533 sock->type == SILC_SOCKET_TYPE_SERVER &&
3534 !(packet->flags & SILC_PACKET_FLAG_BROADCAST)) {
3535 SILC_LOG_DEBUG(("Broadcasting received Resume Client packet"));
3536 silc_server_packet_send(server, server->router->connection,
3538 packet->flags | SILC_PACKET_FLAG_BROADCAST,
3539 buffer->data, buffer->len, FALSE);
3540 silc_server_backup_send(server, (SilcServerEntry)sock->user_data,
3541 packet->type, packet->flags,
3542 packet->buffer->data, packet->buffer->len,
3546 silc_free(server_id);
3549 silc_free(client_id);