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 && server->server_type == SILC_ROUTER) {
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 if (!silc_server_client_on_channel(client2, channel, &chl))
758 /* If target is founder mode change is not allowed. */
759 if (chl->mode & SILC_CHANNEL_UMODE_CHANFO) {
760 SILC_LOG_DEBUG(("CUMODE change is not allowed"));
766 /* Get target channel entry */
767 if (!silc_server_client_on_channel(client2, channel, &chl))
770 if (mode & SILC_CHANNEL_UMODE_CHANFO &&
771 !(channel->mode & SILC_CHANNEL_MODE_FOUNDER_AUTH) && !client &&
772 server->server_type == SILC_ROUTER) {
773 /* Get the founder of the channel and if found then this client
774 cannot be the founder since there already is one. */
775 silc_hash_table_list(channel->user_list, &htl);
776 while (silc_hash_table_get(&htl, NULL, (void *)&chl2))
777 if (chl2->mode & SILC_CHANNEL_UMODE_CHANFO) {
778 mode &= ~SILC_CHANNEL_UMODE_CHANFO;
779 silc_server_force_cumode_change(server, sock, channel, chl, mode);
783 silc_hash_table_list_reset(&htl);
784 if (!(mode & SILC_CHANNEL_UMODE_CHANFO))
788 if (client && mode & SILC_CHANNEL_UMODE_CHANFO &&
789 !(chl->mode & SILC_CHANNEL_UMODE_CHANFO) &&
790 server->server_type == SILC_ROUTER) {
791 /* Check whether this client is allowed to be channel founder on
793 SilcPublicKey founder_key = NULL;
795 /* If channel doesn't have founder auth mode then it's impossible
796 that someone would be getting founder rights with CUMODE command.
797 In that case there already either is founder or there isn't
798 founder at all on the channel. */
799 if (!(channel->mode & SILC_CHANNEL_MODE_FOUNDER_AUTH)) {
800 /* Force the mode to not have founder mode */
801 mode &= ~SILC_CHANNEL_UMODE_CHANFO;
802 silc_server_force_cumode_change(server, sock, channel, chl, mode);
807 /* Get the founder of the channel and if found then this client
808 cannot be the founder since there already is one. */
809 silc_hash_table_list(channel->user_list, &htl);
810 while (silc_hash_table_get(&htl, NULL, (void *)&chl2))
811 if (chl2->mode & SILC_CHANNEL_UMODE_CHANFO) {
812 mode &= ~SILC_CHANNEL_UMODE_CHANFO;
813 silc_server_force_cumode_change(server, sock, channel, chl, mode);
817 silc_hash_table_list_reset(&htl);
818 if (!(mode & SILC_CHANNEL_UMODE_CHANFO))
821 /* Founder not found of the channel. Since the founder auth mode
822 is set on the channel now check whether this is the client that
823 originally set the mode. */
825 /* Get public key that must be present in notify */
826 tmp = silc_argument_get_arg_type(args, 4, &tmp_len);
827 if (!tmp || !silc_pkcs_public_key_decode(tmp, tmp_len,
829 mode &= ~SILC_CHANNEL_UMODE_CHANFO;
830 silc_server_force_cumode_change(server, sock, channel, chl, mode);
835 /* Now match the public key we have cached and public key sent.
837 if (client->data.public_key &&
838 !silc_pkcs_public_key_compare(channel->founder_key,
839 client->data.public_key)) {
840 mode &= ~SILC_CHANNEL_UMODE_CHANFO;
841 silc_server_force_cumode_change(server, sock, channel, chl, mode);
845 if (!silc_pkcs_public_key_compare(channel->founder_key,
847 mode &= ~SILC_CHANNEL_UMODE_CHANFO;
848 silc_server_force_cumode_change(server, sock, channel, chl, mode);
854 silc_pkcs_public_key_free(founder_key);
857 SILC_LOG_DEBUG(("Changing the channel user mode"));
859 /* Change the mode */
862 /* Send the same notify to the channel */
864 silc_server_packet_send_to_channel(server, sock, channel,
866 FALSE, packet->buffer->data,
867 packet->buffer->len, FALSE);
869 silc_free(channel_id);
873 case SILC_NOTIFY_TYPE_INVITE:
875 if (packet->dst_id_type == SILC_ID_CLIENT)
878 SILC_LOG_DEBUG(("INVITE notify"));
881 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
884 channel_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
888 /* Get channel entry */
889 channel = silc_idlist_find_channel_by_id(server->global_list,
892 channel = silc_idlist_find_channel_by_id(server->local_list,
895 silc_free(channel_id);
899 silc_free(channel_id);
902 tmp = silc_argument_get_arg_type(args, 3, &tmp_len);
905 client_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
909 /* Get client entry */
910 client = silc_idlist_find_client_by_id(server->global_list,
911 client_id, TRUE, &cache);
913 client = silc_idlist_find_client_by_id(server->local_list,
914 client_id, TRUE, &cache);
916 silc_free(client_id);
920 silc_free(client_id);
922 /* Get user's channel entry and check that inviting is allowed. */
923 if (!silc_server_client_on_channel(client, channel, &chl))
925 if (channel->mode & SILC_CHANNEL_MODE_INVITE &&
926 !(chl->mode & SILC_CHANNEL_UMODE_CHANOP) &&
927 !(chl->mode & SILC_CHANNEL_UMODE_CHANFO)) {
928 SILC_LOG_DEBUG(("Inviting is not allowed"));
932 /* Get the added invite */
933 tmp = silc_argument_get_arg_type(args, 4, &tmp_len);
935 if (!channel->invite_list)
936 channel->invite_list = silc_calloc(tmp_len + 2,
937 sizeof(*channel->invite_list));
939 channel->invite_list = silc_realloc(channel->invite_list,
940 sizeof(*channel->invite_list) *
942 strlen(channel->invite_list) +
944 if (tmp[tmp_len - 1] == ',')
945 tmp[tmp_len - 1] = '\0';
947 strncat(channel->invite_list, tmp, tmp_len);
948 strncat(channel->invite_list, ",", 1);
951 /* Get the deleted invite */
952 tmp = silc_argument_get_arg_type(args, 5, &tmp_len);
953 if (tmp && channel->invite_list) {
954 char *start, *end, *n;
956 if (!strncmp(channel->invite_list, tmp,
957 strlen(channel->invite_list) - 1)) {
958 silc_free(channel->invite_list);
959 channel->invite_list = NULL;
961 start = strstr(channel->invite_list, tmp);
962 if (start && strlen(start) >= tmp_len) {
963 end = start + tmp_len;
964 n = silc_calloc(strlen(channel->invite_list) - tmp_len, sizeof(*n));
965 strncat(n, channel->invite_list, start - channel->invite_list);
966 strncat(n, end + 1, ((channel->invite_list +
967 strlen(channel->invite_list)) - end) - 1);
968 silc_free(channel->invite_list);
969 channel->invite_list = n;
976 case SILC_NOTIFY_TYPE_CHANNEL_CHANGE:
978 * Distribute to the local clients on the channel and change the
982 SILC_LOG_DEBUG(("CHANNEL CHANGE"));
984 if (sock->type != SILC_SOCKET_TYPE_ROUTER)
987 /* Get the old Channel ID */
988 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
991 channel_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
995 /* Get the channel entry */
996 channel = silc_idlist_find_channel_by_id(server->local_list,
999 channel = silc_idlist_find_channel_by_id(server->global_list,
1002 silc_free(channel_id);
1007 /* Send the notify to the channel */
1008 silc_server_packet_send_to_channel(server, sock, channel, packet->type,
1009 FALSE, packet->buffer->data,
1010 packet->buffer->len, FALSE);
1012 /* Get the new Channel ID */
1013 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
1016 channel_id2 = silc_id_payload_parse_id(tmp, tmp_len, NULL);
1020 SILC_LOG_DEBUG(("Old Channel ID id(%s)",
1021 silc_id_render(channel_id, SILC_ID_CHANNEL)));
1022 SILC_LOG_DEBUG(("New Channel ID id(%s)",
1023 silc_id_render(channel_id2, SILC_ID_CHANNEL)));
1025 /* Replace the Channel ID */
1026 if (!silc_idlist_replace_channel_id(server->local_list, channel_id,
1028 if (!silc_idlist_replace_channel_id(server->global_list, channel_id,
1030 silc_free(channel_id2);
1035 SilcBuffer modes = NULL, users = NULL, users_modes = NULL;
1037 /* Re-announce this channel which ID was changed. */
1038 silc_server_send_new_channel(server, sock, FALSE, channel->channel_name,
1040 silc_id_get_len(channel->id,
1044 /* Re-announce our clients on the channel as the ID has changed now */
1045 silc_server_announce_get_channel_users(server, channel, &modes, &users,
1048 silc_buffer_push(modes, modes->data - modes->head);
1049 silc_server_packet_send_dest(server, sock,
1050 SILC_PACKET_NOTIFY, SILC_PACKET_FLAG_LIST,
1051 channel->id, SILC_ID_CHANNEL,
1052 modes->data, modes->len, FALSE);
1053 silc_buffer_free(modes);
1056 silc_buffer_push(users, users->data - users->head);
1057 silc_server_packet_send(server, sock,
1058 SILC_PACKET_NOTIFY, SILC_PACKET_FLAG_LIST,
1059 users->data, users->len, FALSE);
1060 silc_buffer_free(users);
1063 silc_buffer_push(users_modes, users_modes->data - users_modes->head);
1064 silc_server_packet_send_dest(server, sock,
1065 SILC_PACKET_NOTIFY, SILC_PACKET_FLAG_LIST,
1066 channel->id, SILC_ID_CHANNEL,
1068 users_modes->len, FALSE);
1069 silc_buffer_free(users_modes);
1072 /* Re-announce channel's topic */
1073 if (channel->topic) {
1074 silc_server_send_notify_topic_set(server, sock,
1075 server->server_type == SILC_ROUTER ?
1076 TRUE : FALSE, channel,
1077 channel->id, SILC_ID_CHANNEL,
1082 silc_free(channel_id);
1086 case SILC_NOTIFY_TYPE_SERVER_SIGNOFF:
1088 * Remove the server entry and all clients that this server owns.
1091 SILC_LOG_DEBUG(("SERVER SIGNOFF notify"));
1094 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
1097 server_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
1101 /* Get server entry */
1102 server_entry = silc_idlist_find_server_by_id(server->global_list,
1103 server_id, TRUE, NULL);
1105 if (!server_entry) {
1106 server_entry = silc_idlist_find_server_by_id(server->local_list,
1107 server_id, TRUE, NULL);
1109 if (!server_entry) {
1110 /* If we are normal server then we might not have the server. Check
1111 whether router was kind enough to send the list of all clients
1112 that actually was to be removed. Remove them if the list is
1114 if (server->server_type != SILC_ROUTER &&
1115 silc_argument_get_arg_num(args) > 1) {
1118 for (i = 1; i < silc_argument_get_arg_num(args); i++) {
1120 tmp = silc_argument_get_arg_type(args, i + 1, &tmp_len);
1123 client_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
1127 /* Get client entry */
1128 client = silc_idlist_find_client_by_id(server->global_list,
1129 client_id, TRUE, &cache);
1132 client = silc_idlist_find_client_by_id(server->local_list,
1133 client_id, TRUE, &cache);
1136 silc_free(client_id);
1140 silc_free(client_id);
1142 /* Update statistics */
1143 server->stat.clients--;
1144 if (server->stat.cell_clients)
1145 server->stat.cell_clients--;
1146 SILC_OPER_STATS_UPDATE(client, server, SILC_UMODE_SERVER_OPERATOR);
1147 SILC_OPER_STATS_UPDATE(client, router, SILC_UMODE_ROUTER_OPERATOR);
1149 /* Remove the client from all channels. */
1150 silc_server_remove_from_channels(server, NULL, client,
1153 /* Check if anyone is watching this nickname */
1154 if (server->server_type == SILC_ROUTER)
1155 silc_server_check_watcher_list(server, client, NULL,
1156 SILC_NOTIFY_TYPE_SERVER_SIGNOFF);
1158 /* Remove this client from watcher list if it is */
1160 silc_server_del_from_watcher_list(server, client);
1162 /* Remove the client */
1163 silc_idlist_del_client(local ? server->local_list :
1164 server->global_list, client);
1168 silc_free(server_id);
1172 silc_free(server_id);
1174 /* Free all client entries that this server owns as they will
1175 become invalid now as well. */
1176 silc_server_remove_clients_by_server(server, server_entry, TRUE);
1178 /* Remove the server entry */
1179 silc_idlist_del_server(local ? server->local_list :
1180 server->global_list, server_entry);
1182 /* XXX update statistics */
1186 case SILC_NOTIFY_TYPE_KICKED:
1188 * Distribute the notify to local clients on the channel
1191 SILC_LOG_DEBUG(("KICKED notify"));
1194 channel_id = silc_id_str2id(packet->dst_id, packet->dst_id_len,
1195 packet->dst_id_type);
1200 /* Get channel entry */
1201 channel = silc_idlist_find_channel_by_id(server->global_list,
1204 channel = silc_idlist_find_channel_by_id(server->local_list,
1207 silc_free(channel_id);
1211 silc_free(channel_id);
1214 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
1217 client_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
1221 /* If the the client is not in local list we check global list */
1222 client = silc_idlist_find_client_by_id(server->global_list,
1223 client_id, TRUE, NULL);
1225 client = silc_idlist_find_client_by_id(server->local_list,
1226 client_id, TRUE, NULL);
1228 silc_free(client_id);
1232 silc_free(client_id);
1234 /* If target is founder they cannot be kicked */
1235 if (!silc_server_client_on_channel(client, channel, &chl))
1237 if (chl->mode & SILC_CHANNEL_UMODE_CHANFO)
1240 /* From protocol version 1.1 we get the kicker's ID as well. */
1241 tmp = silc_argument_get_arg_type(args, 3, &tmp_len);
1243 client_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
1247 /* If the the client is not in local list we check global list */
1248 client2 = silc_idlist_find_client_by_id(server->global_list,
1249 client_id, TRUE, NULL);
1251 client2 = silc_idlist_find_client_by_id(server->local_list,
1252 client_id, TRUE, NULL);
1254 silc_free(client_id);
1258 silc_free(client_id);
1260 /* Kicker must be operator on channel */
1261 if (!silc_server_client_on_channel(client2, channel, &chl))
1263 if (!(chl->mode & SILC_CHANNEL_UMODE_CHANOP) &&
1264 !(chl->mode & SILC_CHANNEL_UMODE_CHANFO)) {
1265 SILC_LOG_DEBUG(("Kicking is not allowed"));
1270 /* Send to channel */
1271 silc_server_packet_send_to_channel(server, sock, channel, packet->type,
1272 FALSE, packet->buffer->data,
1273 packet->buffer->len, FALSE);
1275 /* Remove the client from channel */
1276 silc_server_remove_from_one_channel(server, sock, channel, client, FALSE);
1280 case SILC_NOTIFY_TYPE_KILLED:
1283 * Distribute the notify to local clients on channels
1285 unsigned char *id, *comment;
1286 SilcUInt32 id_len, comment_len;
1288 SILC_LOG_DEBUG(("KILLED notify"));
1291 id = silc_argument_get_arg_type(args, 1, &id_len);
1294 client_id = silc_id_payload_parse_id(id, id_len, NULL);
1298 /* If the the client is not in local list we check global list */
1299 client = silc_idlist_find_client_by_id(server->global_list,
1300 client_id, TRUE, NULL);
1302 client = silc_idlist_find_client_by_id(server->local_list,
1303 client_id, TRUE, NULL);
1305 silc_free(client_id);
1309 silc_free(client_id);
1311 /* If the client is one of ours, then close the connection to the
1312 client now. This removes the client from all channels as well. */
1313 if (packet->dst_id_type == SILC_ID_CLIENT && client->connection) {
1314 sock = client->connection;
1315 silc_server_free_client_data(server, NULL, client, FALSE, NULL);
1316 silc_server_close_connection(server, sock);
1321 comment = silc_argument_get_arg_type(args, 2, &comment_len);
1322 if (comment_len > 128)
1325 /* From protocol version 1.1 we get the killer's ID as well. */
1326 tmp = silc_argument_get_arg_type(args, 3, &tmp_len);
1328 client_id = silc_id_payload_parse_id(tmp, tmp_len, &id_type);
1332 if (id_type == SILC_ID_CLIENT) {
1333 /* If the the client is not in local list we check global list */
1334 client2 = silc_idlist_find_client_by_id(server->global_list,
1335 client_id, TRUE, NULL);
1337 client2 = silc_idlist_find_client_by_id(server->local_list,
1338 client_id, TRUE, NULL);
1340 silc_free(client_id);
1344 silc_free(client_id);
1346 /* Killer must be router operator */
1347 if (!(client2->mode & SILC_UMODE_ROUTER_OPERATOR)) {
1348 SILC_LOG_DEBUG(("Killing is not allowed"));
1354 /* Send the notify to local clients on the channels except to the
1355 client who is killed. */
1356 silc_server_send_notify_on_channels(server, client, client,
1357 SILC_NOTIFY_TYPE_KILLED, 3,
1358 id, id_len, comment, comment_len,
1361 /* Remove the client from all channels */
1362 silc_server_remove_from_channels(server, NULL, client, FALSE, NULL,
1365 /* Check if anyone is watching this nickname */
1366 if (server->server_type == SILC_ROUTER)
1367 silc_server_check_watcher_list(server, client, NULL,
1368 SILC_NOTIFY_TYPE_KILLED);
1373 case SILC_NOTIFY_TYPE_UMODE_CHANGE:
1375 * Save the mode of the client.
1378 SILC_LOG_DEBUG(("UMODE_CHANGE notify"));
1381 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
1384 client_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
1388 /* Get client entry */
1389 client = silc_idlist_find_client_by_id(server->global_list,
1390 client_id, TRUE, NULL);
1392 client = silc_idlist_find_client_by_id(server->local_list,
1393 client_id, TRUE, NULL);
1395 silc_free(client_id);
1399 silc_free(client_id);
1402 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
1405 SILC_GET32_MSB(mode, tmp);
1407 /* Check that mode changing is allowed. */
1408 if (!silc_server_check_umode_rights(server, client, mode)) {
1409 SILC_LOG_DEBUG(("UMODE change is not allowed"));
1413 /* Remove internal resumed flag if client is marked detached now */
1414 if (mode & SILC_UMODE_DETACHED)
1415 client->data.status &= ~SILC_IDLIST_STATUS_RESUMED;
1417 /* Change the mode */
1418 client->mode = mode;
1420 /* Check if anyone is watching this nickname */
1421 if (server->server_type == SILC_ROUTER)
1422 silc_server_check_watcher_list(server, client, NULL,
1423 SILC_NOTIFY_TYPE_UMODE_CHANGE);
1427 case SILC_NOTIFY_TYPE_BAN:
1432 SILC_LOG_DEBUG(("BAN notify"));
1434 /* Get Channel ID */
1435 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
1438 channel_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
1442 /* Get channel entry */
1443 channel = silc_idlist_find_channel_by_id(server->global_list,
1446 channel = silc_idlist_find_channel_by_id(server->local_list,
1449 silc_free(channel_id);
1453 silc_free(channel_id);
1455 /* Get the new ban and add it to the ban list */
1456 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
1458 if (!channel->ban_list)
1459 channel->ban_list = silc_calloc(tmp_len + 2,
1460 sizeof(*channel->ban_list));
1462 channel->ban_list = silc_realloc(channel->ban_list,
1463 sizeof(*channel->ban_list) *
1465 strlen(channel->ban_list) + 2));
1466 strncat(channel->ban_list, tmp, tmp_len);
1467 strncat(channel->ban_list, ",", 1);
1470 /* Get the ban to be removed and remove it from the list */
1471 tmp = silc_argument_get_arg_type(args, 3, &tmp_len);
1472 if (tmp && channel->ban_list) {
1473 char *start, *end, *n;
1475 if (!strncmp(channel->ban_list, tmp, strlen(channel->ban_list) - 1)) {
1476 silc_free(channel->ban_list);
1477 channel->ban_list = NULL;
1479 start = strstr(channel->ban_list, tmp);
1480 if (start && strlen(start) >= tmp_len) {
1481 end = start + tmp_len;
1482 n = silc_calloc(strlen(channel->ban_list) - tmp_len, sizeof(*n));
1483 strncat(n, channel->ban_list, start - channel->ban_list);
1484 strncat(n, end + 1, ((channel->ban_list +
1485 strlen(channel->ban_list)) - end) - 1);
1486 silc_free(channel->ban_list);
1487 channel->ban_list = n;
1493 case SILC_NOTIFY_TYPE_ERROR:
1500 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
1501 if (!tmp && tmp_len != 1)
1503 error = (SilcStatus)tmp[0];
1505 SILC_LOG_DEBUG(("ERROR notify (%d)", error));
1507 if (error == SILC_STATUS_ERR_NO_SUCH_CLIENT_ID &&
1508 sock->type == SILC_SOCKET_TYPE_ROUTER) {
1509 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
1511 SILC_LOG_DEBUG(("Received invalid client ID notification, deleting "
1512 "the entry from cache"));
1513 client_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
1516 client = silc_idlist_find_client_by_id(server->global_list,
1517 client_id, FALSE, NULL);
1519 silc_server_remove_from_channels(server, NULL, client, TRUE,
1521 silc_idlist_del_client(server->global_list, client);
1523 silc_free(client_id);
1529 /* Ignore rest of the notify types for now */
1530 case SILC_NOTIFY_TYPE_NONE:
1531 case SILC_NOTIFY_TYPE_MOTD:
1538 silc_notify_payload_free(payload);
1541 void silc_server_notify_list(SilcServer server,
1542 SilcSocketConnection sock,
1543 SilcPacketContext *packet)
1545 SilcPacketContext *new;
1549 SILC_LOG_DEBUG(("Processing Notify List"));
1551 if (sock->type == SILC_SOCKET_TYPE_CLIENT ||
1552 packet->src_id_type != SILC_ID_SERVER)
1555 /* Make copy of the original packet context, except for the actual
1556 data buffer, which we will here now fetch from the original buffer. */
1557 new = silc_packet_context_alloc();
1558 new->type = SILC_PACKET_NOTIFY;
1559 new->flags = packet->flags;
1560 new->src_id = packet->src_id;
1561 new->src_id_len = packet->src_id_len;
1562 new->src_id_type = packet->src_id_type;
1563 new->dst_id = packet->dst_id;
1564 new->dst_id_len = packet->dst_id_len;
1565 new->dst_id_type = packet->dst_id_type;
1567 buffer = silc_buffer_alloc(1024);
1568 new->buffer = buffer;
1570 while (packet->buffer->len) {
1571 SILC_GET16_MSB(len, packet->buffer->data + 2);
1572 if (len > packet->buffer->len)
1575 if (len > buffer->truelen) {
1576 silc_buffer_free(buffer);
1577 buffer = silc_buffer_alloc(1024 + len);
1580 silc_buffer_pull_tail(buffer, len);
1581 silc_buffer_put(buffer, packet->buffer->data, len);
1583 /* Process the Notify */
1584 silc_server_notify(server, sock, new);
1586 silc_buffer_push_tail(buffer, len);
1587 silc_buffer_pull(packet->buffer, len);
1590 silc_buffer_free(buffer);
1594 /* Received private message. This resolves the destination of the message
1595 and sends the packet. This is used by both server and router. If the
1596 destination is our locally connected client this sends the packet to
1597 the client. This may also send the message for further routing if
1598 the destination is not in our server (or router). */
1600 void silc_server_private_message(SilcServer server,
1601 SilcSocketConnection sock,
1602 SilcPacketContext *packet)
1604 SilcSocketConnection dst_sock;
1605 SilcIDListData idata;
1606 SilcClientEntry client;
1608 SILC_LOG_DEBUG(("Start"));
1610 if (packet->src_id_type != SILC_ID_CLIENT ||
1611 packet->dst_id_type != SILC_ID_CLIENT || !packet->dst_id)
1614 /* Get the route to the client */
1615 dst_sock = silc_server_get_client_route(server, packet->dst_id,
1616 packet->dst_id_len, NULL,
1620 unsigned char error;
1622 if (client && client->mode & SILC_UMODE_DETACHED) {
1623 SILC_LOG_DEBUG(("Client is detached, discarding packet"));
1627 /* Send SILC_NOTIFY_TYPE_ERROR to indicate that such destination ID
1628 does not exist or is invalid. */
1629 idp = silc_id_payload_encode_data(packet->dst_id,
1631 packet->dst_id_type);
1635 error = SILC_STATUS_ERR_NO_SUCH_CLIENT_ID;
1636 if (packet->src_id_type == SILC_ID_CLIENT) {
1637 SilcClientID *client_id = silc_id_str2id(packet->src_id,
1639 packet->src_id_type);
1640 silc_server_send_notify_dest(server, sock, FALSE,
1641 client_id, SILC_ID_CLIENT,
1642 SILC_NOTIFY_TYPE_ERROR, 2,
1644 idp->data, idp->len);
1645 silc_free(client_id);
1647 silc_server_send_notify(server, sock, FALSE,
1648 SILC_NOTIFY_TYPE_ERROR, 2,
1650 idp->data, idp->len);
1653 silc_buffer_free(idp);
1657 /* Check whether destination client wishes to receive private messages */
1658 if (client && !(packet->flags & SILC_PACKET_FLAG_PRIVMSG_KEY) &&
1659 client->mode & SILC_UMODE_BLOCK_PRIVMSG) {
1660 SILC_LOG_DEBUG(("Client blocks private messages, discarding packet"));
1664 /* Send the private message */
1665 silc_server_send_private_message(server, dst_sock, idata->send_key,
1666 idata->hmac_send, idata->psn_send++,
1670 /* Received private message key packet.. This packet is never for us. It is to
1671 the client in the packet's destination ID. Sending of this sort of packet
1672 equals sending private message, ie. it is sent point to point from
1673 one client to another. */
1675 void silc_server_private_message_key(SilcServer server,
1676 SilcSocketConnection sock,
1677 SilcPacketContext *packet)
1679 SilcSocketConnection dst_sock;
1680 SilcIDListData idata;
1682 SILC_LOG_DEBUG(("Start"));
1684 if (packet->src_id_type != SILC_ID_CLIENT ||
1685 packet->dst_id_type != SILC_ID_CLIENT)
1688 if (!packet->dst_id)
1691 /* Get the route to the client */
1692 dst_sock = silc_server_get_client_route(server, packet->dst_id,
1693 packet->dst_id_len, NULL,
1698 /* Relay the packet */
1699 silc_server_relay_packet(server, dst_sock, idata->send_key,
1700 idata->hmac_send, idata->psn_send++, packet, FALSE);
1703 /* Processes incoming command reply packet. The command reply packet may
1704 be destined to one of our clients or it may directly for us. We will
1705 call the command reply routine after processing the packet. */
1707 void silc_server_command_reply(SilcServer server,
1708 SilcSocketConnection sock,
1709 SilcPacketContext *packet)
1711 SilcBuffer buffer = packet->buffer;
1712 SilcClientEntry client = NULL;
1713 SilcSocketConnection dst_sock;
1714 SilcIDListData idata;
1715 SilcClientID *id = NULL;
1717 SILC_LOG_DEBUG(("Start"));
1719 /* Source must be server or router */
1720 if (packet->src_id_type != SILC_ID_SERVER &&
1721 sock->type != SILC_SOCKET_TYPE_ROUTER)
1724 if (packet->dst_id_type == SILC_ID_CHANNEL)
1727 if (packet->dst_id_type == SILC_ID_CLIENT) {
1728 /* Destination must be one of ours */
1729 id = silc_id_str2id(packet->dst_id, packet->dst_id_len, SILC_ID_CLIENT);
1732 client = silc_idlist_find_client_by_id(server->local_list, id, TRUE, NULL);
1734 SILC_LOG_ERROR(("Cannot process command reply to unknown client"));
1740 if (packet->dst_id_type == SILC_ID_SERVER) {
1741 /* For now this must be for us */
1742 if (memcmp(packet->dst_id, server->id_string, server->id_string_len)) {
1743 SILC_LOG_ERROR(("Cannot process command reply to unknown server"));
1748 /* Execute command reply locally for the command */
1749 silc_server_command_reply_process(server, sock, buffer);
1751 if (packet->dst_id_type == SILC_ID_CLIENT && client && id) {
1752 /* Relay the packet to the client */
1753 const SilcBufferStruct p;
1755 dst_sock = (SilcSocketConnection)client->connection;
1756 idata = (SilcIDListData)client;
1758 silc_buffer_push(buffer, SILC_PACKET_HEADER_LEN + packet->src_id_len
1759 + packet->dst_id_len + packet->padlen);
1760 if (!silc_packet_send_prepare(dst_sock, 0, 0, buffer->len,
1761 idata->hmac_send, (const SilcBuffer)&p)) {
1762 SILC_LOG_ERROR(("Cannot send packet"));
1765 silc_buffer_put((SilcBuffer)&p, buffer->data, buffer->len);
1767 /* Encrypt packet */
1768 silc_packet_encrypt(idata->send_key, idata->hmac_send, idata->psn_send++,
1769 (SilcBuffer)&p, buffer->len);
1771 /* Send the packet */
1772 silc_server_packet_send_real(server, dst_sock, TRUE);
1778 /* Process received channel message. The message can be originated from
1779 client or server. */
1781 void silc_server_channel_message(SilcServer server,
1782 SilcSocketConnection sock,
1783 SilcPacketContext *packet)
1785 SilcChannelEntry channel = NULL;
1786 SilcChannelID *id = NULL;
1787 void *sender_id = NULL;
1788 SilcClientEntry sender_entry = NULL;
1789 SilcChannelClientEntry chl;
1792 SILC_LOG_DEBUG(("Processing channel message"));
1795 if (packet->dst_id_type != SILC_ID_CHANNEL) {
1796 SILC_LOG_DEBUG(("Received bad message for channel, dropped"));
1800 /* Find channel entry */
1801 id = silc_id_str2id(packet->dst_id, packet->dst_id_len, SILC_ID_CHANNEL);
1804 channel = silc_idlist_find_channel_by_id(server->local_list, id, NULL);
1806 channel = silc_idlist_find_channel_by_id(server->global_list, id, NULL);
1809 unsigned char error;
1811 /* Send SILC_NOTIFY_TYPE_ERROR to indicate that such destination ID
1812 does not exist or is invalid. */
1813 idp = silc_id_payload_encode_data(packet->dst_id,
1815 packet->dst_id_type);
1819 error = SILC_STATUS_ERR_NO_SUCH_CHANNEL_ID;
1820 if (packet->src_id_type == SILC_ID_CLIENT) {
1821 SilcClientID *client_id = silc_id_str2id(packet->src_id,
1823 packet->src_id_type);
1824 silc_server_send_notify_dest(server, sock, FALSE,
1825 client_id, SILC_ID_CLIENT,
1826 SILC_NOTIFY_TYPE_ERROR, 2,
1827 &error, 1, idp->data, idp->len);
1828 silc_free(client_id);
1830 silc_server_send_notify(server, sock, FALSE,
1831 SILC_NOTIFY_TYPE_ERROR, 2,
1832 &error, 1, idp->data, idp->len);
1835 silc_buffer_free(idp);
1840 /* See that this client is on the channel. If the original sender is
1841 not client (as it can be server as well) we don't do the check. */
1842 sender_id = silc_id_str2id(packet->src_id, packet->src_id_len,
1843 packet->src_id_type);
1846 if (packet->src_id_type == SILC_ID_CLIENT) {
1847 sender_entry = silc_idlist_find_client_by_id(server->local_list,
1848 sender_id, TRUE, NULL);
1849 if (!sender_entry) {
1851 sender_entry = silc_idlist_find_client_by_id(server->global_list,
1852 sender_id, TRUE, NULL);
1854 if (!sender_entry || !silc_server_client_on_channel(sender_entry,
1856 SILC_LOG_DEBUG(("Client not on channel"));
1860 /* If channel is moderated check that client is allowed to send
1862 if (channel->mode & SILC_CHANNEL_MODE_SILENCE_USERS &&
1863 !(chl->mode & SILC_CHANNEL_UMODE_CHANOP) &&
1864 !(chl->mode & SILC_CHANNEL_UMODE_CHANFO)) {
1865 SILC_LOG_DEBUG(("Channel is silenced from normal users"));
1868 if (channel->mode & SILC_CHANNEL_MODE_SILENCE_OPERS &&
1869 chl->mode & SILC_CHANNEL_UMODE_CHANOP &&
1870 !(chl->mode & SILC_CHANNEL_UMODE_CHANFO)) {
1871 SILC_LOG_DEBUG(("Channel is silenced from operators"));
1875 /* If the packet is coming from router, but the client entry is local
1876 entry to us then some router is rerouting this to us and it is not
1877 allowed. When the client is local to us it means that we've routed
1878 this packet to network, and now someone is routing it back to us. */
1879 if (server->server_type == SILC_ROUTER &&
1880 sock->type == SILC_SOCKET_TYPE_ROUTER && local) {
1881 SILC_LOG_DEBUG(("Channel message rerouted to the sender, drop it"));
1886 /* Distribute the packet to our local clients. This will send the
1887 packet for further routing as well, if needed. */
1888 silc_server_packet_relay_to_channel(server, sock, channel, sender_id,
1889 packet->src_id_type, sender_entry,
1890 packet->buffer->data,
1891 packet->buffer->len, FALSE);
1894 silc_free(sender_id);
1898 /* Received channel key packet. We distribute the key to all of our locally
1899 connected clients on the channel. */
1901 void silc_server_channel_key(SilcServer server,
1902 SilcSocketConnection sock,
1903 SilcPacketContext *packet)
1905 SilcBuffer buffer = packet->buffer;
1906 SilcChannelEntry channel;
1908 if (packet->src_id_type != SILC_ID_SERVER ||
1909 (server->server_type == SILC_ROUTER &&
1910 sock->type == SILC_SOCKET_TYPE_ROUTER))
1913 /* Save the channel key */
1914 channel = silc_server_save_channel_key(server, buffer, NULL);
1918 /* Distribute the key to everybody who is on the channel. If we are router
1919 we will also send it to locally connected servers. */
1920 silc_server_send_channel_key(server, sock, channel, FALSE);
1922 if (server->server_type != SILC_BACKUP_ROUTER) {
1923 /* Distribute to local cell backup routers. */
1924 silc_server_backup_send(server, (SilcServerEntry)sock->user_data,
1925 SILC_PACKET_CHANNEL_KEY, 0,
1926 buffer->data, buffer->len, FALSE, TRUE);
1930 /* Received New Client packet and processes it. Creates Client ID for the
1931 client. Client becomes registered after calling this functions. */
1933 SilcClientEntry silc_server_new_client(SilcServer server,
1934 SilcSocketConnection sock,
1935 SilcPacketContext *packet)
1937 SilcBuffer buffer = packet->buffer;
1938 SilcClientEntry client;
1939 SilcClientID *client_id;
1940 SilcIDListData idata;
1941 char *username = NULL, *realname = NULL;
1942 SilcUInt16 username_len;
1945 char *hostname, *nickname;
1948 SILC_LOG_DEBUG(("Creating new client"));
1950 if (sock->type != SILC_SOCKET_TYPE_CLIENT)
1953 /* Take client entry */
1954 client = (SilcClientEntry)sock->user_data;
1955 idata = (SilcIDListData)client;
1957 /* Remove the old cache entry. */
1958 if (!silc_idcache_del_by_context(server->local_list->clients, client)) {
1959 SILC_LOG_INFO(("Unauthenticated client attempted to register to network"));
1960 silc_server_disconnect_remote(server, sock,
1961 SILC_STATUS_ERR_NOT_AUTHENTICATED, NULL);
1962 if (sock->user_data)
1963 silc_server_free_sock_user_data(server, sock, NULL);
1967 /* Parse incoming packet */
1968 ret = silc_buffer_unformat(buffer,
1969 SILC_STR_UI16_NSTRING_ALLOC(&username,
1971 SILC_STR_UI16_STRING_ALLOC(&realname),
1974 silc_free(username);
1975 silc_free(realname);
1976 SILC_LOG_ERROR(("Client %s (%s) sent incomplete information, closing "
1977 "connection", sock->hostname, sock->ip));
1978 silc_server_disconnect_remote(server, sock,
1979 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
1981 if (sock->user_data)
1982 silc_server_free_sock_user_data(server, sock, NULL);
1987 silc_free(username);
1988 silc_free(realname);
1989 SILC_LOG_ERROR(("Client %s (%s) did not send its username, closing "
1990 "connection", sock->hostname, sock->ip));
1991 silc_server_disconnect_remote(server, sock,
1992 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
1994 if (sock->user_data)
1995 silc_server_free_sock_user_data(server, sock, NULL);
1999 if (username_len > 128)
2000 username[128] = '\0';
2002 /* Check for bad characters for nickname, and modify the nickname if
2003 it includes those. */
2004 if (silc_server_name_bad_chars(username, username_len)) {
2005 nickname = silc_server_name_modify_bad(username, username_len);
2007 nickname = strdup(username);
2010 /* Make sanity checks for the hostname of the client. If the hostname
2011 is provided in the `username' check that it is the same than the
2012 resolved hostname, or if not resolved the hostname that appears in
2013 the client's public key. If the hostname is not present then put
2014 it from the resolved name or from the public key. */
2015 if (strchr(username, '@')) {
2016 SilcPublicKeyIdentifier pident;
2017 int tlen = strcspn(username, "@");
2018 char *phostname = NULL;
2020 hostname = silc_memdup(username + tlen + 1, strlen(username) - tlen - 1);
2022 if (strcmp(sock->hostname, sock->ip) &&
2023 strcmp(sock->hostname, hostname)) {
2024 silc_free(username);
2025 silc_free(hostname);
2026 silc_free(realname);
2027 SILC_LOG_ERROR(("Client %s (%s) sent incomplete information, closing "
2028 "connection", sock->hostname, sock->ip));
2029 silc_server_disconnect_remote(server, sock,
2030 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
2032 if (sock->user_data)
2033 silc_server_free_sock_user_data(server, sock, NULL);
2037 pident = silc_pkcs_decode_identifier(client->data.public_key->identifier);
2039 phostname = strdup(pident->host);
2040 silc_pkcs_free_identifier(pident);
2043 if (!strcmp(sock->hostname, sock->ip) &&
2044 phostname && strcmp(phostname, hostname)) {
2045 silc_free(username);
2046 silc_free(hostname);
2047 silc_free(phostname);
2048 silc_free(realname);
2049 SILC_LOG_ERROR(("Client %s (%s) sent incomplete information, closing "
2050 "connection", sock->hostname, sock->ip));
2051 silc_server_disconnect_remote(server, sock,
2052 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
2054 if (sock->user_data)
2055 silc_server_free_sock_user_data(server, sock, NULL);
2059 silc_free(phostname);
2061 /* The hostname is not present, add it. */
2063 /* XXX For now we cannot take the host name from the public key since
2064 they are not trusted or we cannot verify them as trusted. Just take
2065 what the resolved name or address is. */
2067 if (strcmp(sock->hostname, sock->ip)) {
2069 newusername = silc_calloc(strlen(username) +
2070 strlen(sock->hostname) + 2,
2071 sizeof(*newusername));
2072 strncat(newusername, username, strlen(username));
2073 strncat(newusername, "@", 1);
2074 strncat(newusername, sock->hostname, strlen(sock->hostname));
2075 silc_free(username);
2076 username = newusername;
2079 SilcPublicKeyIdentifier pident =
2080 silc_pkcs_decode_identifier(client->data.public_key->identifier);
2083 newusername = silc_calloc(strlen(username) +
2084 strlen(pident->host) + 2,
2085 sizeof(*newusername));
2086 strncat(newusername, username, strlen(username));
2087 strncat(newusername, "@", 1);
2088 strncat(newusername, pident->host, strlen(pident->host));
2089 silc_free(username);
2090 username = newusername;
2091 silc_pkcs_free_identifier(pident);
2097 /* Create Client ID */
2098 while (!silc_id_create_client_id(server, server->id, server->rng,
2099 server->md5hash, nickname, &client_id)) {
2102 silc_server_disconnect_remote(server, sock,
2103 SILC_STATUS_ERR_BAD_NICKNAME, NULL);
2104 if (sock->user_data)
2105 silc_server_free_sock_user_data(server, sock, NULL);
2108 snprintf(&nickname[strlen(nickname) - 1], 1, "%d", nickfail);
2111 /* Update client entry */
2112 idata->status |= SILC_IDLIST_STATUS_REGISTERED;
2113 client->nickname = nickname;
2114 client->username = username;
2115 client->userinfo = realname ? realname : strdup(" ");
2116 client->id = client_id;
2117 id_len = silc_id_get_len(client_id, SILC_ID_CLIENT);
2119 /* Add the client again to the ID cache */
2120 silc_idcache_add(server->local_list->clients, client->nickname,
2121 client_id, client, 0, NULL);
2123 /* Notify our router about new client on the SILC network */
2124 if (!server->standalone)
2125 silc_server_send_new_id(server, (SilcSocketConnection)
2126 server->router->connection,
2127 server->server_type == SILC_ROUTER ? TRUE : FALSE,
2128 client->id, SILC_ID_CLIENT, id_len);
2130 /* Send the new client ID to the client. */
2131 silc_server_send_new_id(server, sock, FALSE, client->id, SILC_ID_CLIENT,
2132 silc_id_get_len(client->id, SILC_ID_CLIENT));
2134 /* Send some nice info to the client */
2135 silc_server_send_connect_notifys(server, sock, client);
2137 /* Check if anyone is watching this nickname */
2138 if (server->server_type == SILC_ROUTER)
2139 silc_server_check_watcher_list(server, client, NULL, 0);
2144 /* Create new server. This processes received New Server packet and
2145 saves the received Server ID. The server is our locally connected
2146 server thus we save all the information and save it to local list.
2147 This funtion can be used by both normal server and router server.
2148 If normal server uses this it means that its router has connected
2149 to the server. If router uses this it means that one of the cell's
2150 servers is connected to the router. */
2152 SilcServerEntry silc_server_new_server(SilcServer server,
2153 SilcSocketConnection sock,
2154 SilcPacketContext *packet)
2156 SilcBuffer buffer = packet->buffer;
2157 SilcServerEntry new_server, server_entry;
2158 SilcServerID *server_id;
2159 SilcIDListData idata;
2160 unsigned char *server_name, *id_string;
2161 SilcUInt16 id_len, name_len;
2165 SILC_LOG_DEBUG(("Creating new server"));
2167 if (sock->type != SILC_SOCKET_TYPE_SERVER &&
2168 sock->type != SILC_SOCKET_TYPE_ROUTER)
2171 /* Take server entry */
2172 new_server = (SilcServerEntry)sock->user_data;
2173 idata = (SilcIDListData)new_server;
2175 /* Remove the old cache entry */
2176 if (!silc_idcache_del_by_context(server->local_list->servers, new_server)) {
2177 if (!silc_idcache_del_by_context(server->global_list->servers,
2179 SILC_LOG_INFO(("Unauthenticated %s attempted to register to "
2180 "network", (sock->type == SILC_SOCKET_TYPE_SERVER ?
2181 "server" : "router")));
2182 silc_server_disconnect_remote(server, sock,
2183 SILC_STATUS_ERR_NOT_AUTHENTICATED, NULL);
2184 if (sock->user_data)
2185 silc_server_free_sock_user_data(server, sock, NULL);
2191 /* Parse the incoming packet */
2192 ret = silc_buffer_unformat(buffer,
2193 SILC_STR_UI16_NSTRING_ALLOC(&id_string, &id_len),
2194 SILC_STR_UI16_NSTRING_ALLOC(&server_name,
2198 silc_free(id_string);
2199 silc_free(server_name);
2200 silc_server_disconnect_remote(server, sock,
2201 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
2203 if (sock->user_data)
2204 silc_server_free_sock_user_data(server, sock, NULL);
2208 if (id_len > buffer->len) {
2209 silc_free(id_string);
2210 silc_free(server_name);
2211 silc_server_disconnect_remote(server, sock,
2212 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
2214 if (sock->user_data)
2215 silc_server_free_sock_user_data(server, sock, NULL);
2220 server_name[255] = '\0';
2223 server_id = silc_id_str2id(id_string, id_len, SILC_ID_SERVER);
2225 silc_free(id_string);
2226 silc_free(server_name);
2227 silc_server_disconnect_remote(server, sock,
2228 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
2230 if (sock->user_data)
2231 silc_server_free_sock_user_data(server, sock, NULL);
2234 silc_free(id_string);
2236 /* Check for valid server ID */
2237 if (!silc_id_is_valid_server_id(server, server_id, sock)) {
2238 SILC_LOG_INFO(("Invalid server ID sent by %s (%s)",
2239 sock->ip, sock->hostname));
2240 silc_server_disconnect_remote(server, sock,
2241 SILC_STATUS_ERR_BAD_SERVER_ID, NULL);
2242 if (sock->user_data)
2243 silc_server_free_sock_user_data(server, sock, NULL);
2244 silc_free(server_name);
2248 /* Check that we do not have this ID already */
2249 server_entry = silc_idlist_find_server_by_id(server->local_list,
2250 server_id, TRUE, NULL);
2252 silc_idcache_del_by_context(server->local_list->servers, server_entry);
2254 server_entry = silc_idlist_find_server_by_id(server->global_list,
2255 server_id, TRUE, NULL);
2257 silc_idcache_del_by_context(server->global_list->servers, server_entry);
2260 /* Update server entry */
2261 idata->status |= SILC_IDLIST_STATUS_REGISTERED;
2262 new_server->server_name = server_name;
2263 new_server->id = server_id;
2265 SILC_LOG_DEBUG(("New server id(%s)",
2266 silc_id_render(server_id, SILC_ID_SERVER)));
2268 /* Add again the entry to the ID cache. */
2269 silc_idcache_add(local ? server->local_list->servers :
2270 server->global_list->servers, server_name, server_id,
2271 new_server, 0, NULL);
2273 /* Distribute the information about new server in the SILC network
2274 to our router. If we are normal server we won't send anything
2275 since this connection must be our router connection. */
2276 if (server->server_type == SILC_ROUTER && !server->standalone &&
2277 server->router->connection != sock)
2278 silc_server_send_new_id(server, server->router->connection,
2279 TRUE, new_server->id, SILC_ID_SERVER,
2280 silc_id_get_len(server_id, SILC_ID_SERVER));
2282 if (server->server_type == SILC_ROUTER)
2283 server->stat.cell_servers++;
2285 /* Check whether this router connection has been replaced by an
2286 backup router. If it has been then we'll disable the server and will
2287 ignore everything it will send until the backup router resuming
2288 protocol has been completed. */
2289 if (sock->type == SILC_SOCKET_TYPE_ROUTER &&
2290 silc_server_backup_replaced_get(server, server_id, NULL)) {
2291 /* Send packet to the server indicating that it cannot use this
2292 connection as it has been replaced by backup router. */
2293 SilcBuffer packet = silc_buffer_alloc(2);
2294 silc_buffer_pull_tail(packet, SILC_BUFFER_END(packet));
2295 silc_buffer_format(packet,
2296 SILC_STR_UI_CHAR(SILC_SERVER_BACKUP_REPLACED),
2297 SILC_STR_UI_CHAR(0),
2299 silc_server_packet_send(server, sock,
2300 SILC_PACKET_RESUME_ROUTER, 0,
2301 packet->data, packet->len, TRUE);
2302 silc_buffer_free(packet);
2304 /* Mark the router disabled. The data sent earlier will go but nothing
2305 after this does not go to this connection. */
2306 idata->status |= SILC_IDLIST_STATUS_DISABLED;
2308 /* If it is router announce our stuff to it. */
2309 if (sock->type == SILC_SOCKET_TYPE_ROUTER &&
2310 server->server_type == SILC_ROUTER) {
2311 silc_server_announce_servers(server, FALSE, 0, sock);
2312 silc_server_announce_clients(server, 0, sock);
2313 silc_server_announce_channels(server, 0, sock);
2320 /* Processes incoming New ID packet. New ID Payload is used to distribute
2321 information about newly registered clients and servers. */
2323 static void silc_server_new_id_real(SilcServer server,
2324 SilcSocketConnection sock,
2325 SilcPacketContext *packet,
2328 SilcBuffer buffer = packet->buffer;
2330 SilcServerEntry router, server_entry;
2331 SilcSocketConnection router_sock;
2336 SILC_LOG_DEBUG(("Processing new ID"));
2338 if (sock->type == SILC_SOCKET_TYPE_CLIENT ||
2339 server->server_type == SILC_SERVER ||
2340 packet->src_id_type != SILC_ID_SERVER)
2343 idp = silc_id_payload_parse(buffer->data, buffer->len);
2347 id_type = silc_id_payload_get_type(idp);
2349 /* Normal server cannot have other normal server connections */
2350 server_entry = (SilcServerEntry)sock->user_data;
2351 if (id_type == SILC_ID_SERVER && sock->type == SILC_SOCKET_TYPE_SERVER &&
2352 server_entry->server_type == SILC_SERVER)
2355 id = silc_id_payload_get_id(idp);
2359 /* If the packet is coming from server then use the sender as the
2360 origin of the the packet. If it came from router then check the real
2361 sender of the packet and use that as the origin. */
2362 if (sock->type == SILC_SOCKET_TYPE_SERVER) {
2363 id_list = server->local_list;
2365 router = sock->user_data;
2367 /* If the sender is backup router and ID is server (and we are not
2368 backup router) then switch the entry to global list. */
2369 if (server_entry->server_type == SILC_BACKUP_ROUTER &&
2370 id_type == SILC_ID_SERVER &&
2371 server->id_entry->server_type != SILC_BACKUP_ROUTER) {
2372 id_list = server->global_list;
2373 router_sock = server->router ? server->router->connection : sock;
2376 void *sender_id = silc_id_str2id(packet->src_id, packet->src_id_len,
2377 packet->src_id_type);
2378 router = silc_idlist_find_server_by_id(server->global_list,
2379 sender_id, TRUE, NULL);
2381 router = silc_idlist_find_server_by_id(server->local_list,
2382 sender_id, TRUE, NULL);
2383 silc_free(sender_id);
2385 id_list = server->global_list;
2392 case SILC_ID_CLIENT:
2394 SilcClientEntry entry;
2396 /* Check that we do not have this client already */
2397 entry = silc_idlist_find_client_by_id(server->global_list,
2398 id, server->server_type,
2401 entry = silc_idlist_find_client_by_id(server->local_list,
2402 id, server->server_type,
2405 SILC_LOG_DEBUG(("Ignoring client that we already have"));
2409 SILC_LOG_DEBUG(("New client id(%s) from [%s] %s",
2410 silc_id_render(id, SILC_ID_CLIENT),
2411 sock->type == SILC_SOCKET_TYPE_SERVER ?
2412 "Server" : "Router", sock->hostname));
2414 /* As a router we keep information of all global information in our
2415 global list. Cell wide information however is kept in the local
2417 entry = silc_idlist_add_client(id_list, NULL, NULL, NULL,
2418 id, router, NULL, 0);
2420 SILC_LOG_ERROR(("Could not add new client to the ID Cache"));
2422 /* Inform the sender that the ID is not usable */
2423 silc_server_send_notify_signoff(server, sock, FALSE, id, NULL);
2426 entry->nickname = NULL;
2427 entry->data.status |= SILC_IDLIST_STATUS_REGISTERED;
2429 if (sock->type == SILC_SOCKET_TYPE_SERVER)
2430 server->stat.cell_clients++;
2431 server->stat.clients++;
2433 /* Check if anyone is watching this nickname */
2434 if (server->server_type == SILC_ROUTER && id_list == server->local_list)
2435 silc_server_check_watcher_list(server, entry, NULL, 0);
2439 case SILC_ID_SERVER:
2441 SilcServerEntry entry;
2443 /* If the ID is mine, ignore it. */
2444 if (SILC_ID_SERVER_COMPARE(id, server->id)) {
2445 SILC_LOG_DEBUG(("Ignoring my own ID as new ID"));
2449 /* If the ID is the sender's ID, ignore it (we have it already) */
2450 if (SILC_ID_SERVER_COMPARE(id, router->id)) {
2451 SILC_LOG_DEBUG(("Ignoring sender's own ID"));
2455 /* Check that we do not have this server already */
2456 entry = silc_idlist_find_server_by_id(server->global_list,
2457 id, server->server_type,
2460 entry = silc_idlist_find_server_by_id(server->local_list,
2461 id, server->server_type,
2464 SILC_LOG_DEBUG(("Ignoring server that we already have"));
2468 SILC_LOG_DEBUG(("New server id(%s) from [%s] %s",
2469 silc_id_render(id, SILC_ID_SERVER),
2470 sock->type == SILC_SOCKET_TYPE_SERVER ?
2471 "Server" : "Router", sock->hostname));
2473 /* As a router we keep information of all global information in our
2474 global list. Cell wide information however is kept in the local
2476 entry = silc_idlist_add_server(id_list, NULL, 0, id, router,
2479 SILC_LOG_ERROR(("Could not add new server to the ID Cache"));
2482 entry->data.status |= SILC_IDLIST_STATUS_REGISTERED;
2484 if (sock->type == SILC_SOCKET_TYPE_SERVER)
2485 server->stat.cell_servers++;
2486 server->stat.servers++;
2490 case SILC_ID_CHANNEL:
2491 SILC_LOG_ERROR(("Channel cannot be registered with NEW_ID packet"));
2500 /* If the sender of this packet is server and we are router we need to
2501 broadcast this packet to other routers in the network. */
2502 if (broadcast && !server->standalone && server->server_type == SILC_ROUTER &&
2503 sock->type == SILC_SOCKET_TYPE_SERVER &&
2504 !(packet->flags & SILC_PACKET_FLAG_BROADCAST)) {
2505 SILC_LOG_DEBUG(("Broadcasting received New ID packet"));
2506 silc_server_packet_send(server, server->router->connection,
2508 packet->flags | SILC_PACKET_FLAG_BROADCAST,
2509 buffer->data, buffer->len, FALSE);
2510 silc_server_backup_send(server, (SilcServerEntry)sock->user_data,
2511 packet->type, packet->flags,
2512 packet->buffer->data, packet->buffer->len,
2517 silc_id_payload_free(idp);
2521 /* Processes incoming New ID packet. New ID Payload is used to distribute
2522 information about newly registered clients and servers. */
2524 void silc_server_new_id(SilcServer server, SilcSocketConnection sock,
2525 SilcPacketContext *packet)
2527 silc_server_new_id_real(server, sock, packet, TRUE);
2530 /* Receoved New Id List packet, list of New ID payloads inside one
2531 packet. Process the New ID payloads one by one. */
2533 void silc_server_new_id_list(SilcServer server, SilcSocketConnection sock,
2534 SilcPacketContext *packet)
2536 SilcPacketContext *new_id;
2540 SILC_LOG_DEBUG(("Processing New ID List"));
2542 if (sock->type == SILC_SOCKET_TYPE_CLIENT ||
2543 packet->src_id_type != SILC_ID_SERVER)
2546 /* If the sender of this packet is server and we are router we need to
2547 broadcast this packet to other routers in the network. Broadcast
2548 this list packet instead of multiple New ID packets. */
2549 if (!server->standalone && server->server_type == SILC_ROUTER &&
2550 sock->type == SILC_SOCKET_TYPE_SERVER &&
2551 !(packet->flags & SILC_PACKET_FLAG_BROADCAST)) {
2552 SILC_LOG_DEBUG(("Broadcasting received New ID List packet"));
2553 silc_server_packet_send(server, server->router->connection,
2555 packet->flags | SILC_PACKET_FLAG_BROADCAST,
2556 packet->buffer->data, packet->buffer->len, FALSE);
2557 silc_server_backup_send(server, (SilcServerEntry)sock->user_data,
2558 packet->type, packet->flags,
2559 packet->buffer->data, packet->buffer->len,
2563 /* Make copy of the original packet context, except for the actual
2564 data buffer, which we will here now fetch from the original buffer. */
2565 new_id = silc_packet_context_alloc();
2566 new_id->type = SILC_PACKET_NEW_ID;
2567 new_id->flags = packet->flags;
2568 new_id->src_id = packet->src_id;
2569 new_id->src_id_len = packet->src_id_len;
2570 new_id->src_id_type = packet->src_id_type;
2571 new_id->dst_id = packet->dst_id;
2572 new_id->dst_id_len = packet->dst_id_len;
2573 new_id->dst_id_type = packet->dst_id_type;
2575 idp = silc_buffer_alloc(256);
2576 new_id->buffer = idp;
2578 while (packet->buffer->len) {
2579 SILC_GET16_MSB(id_len, packet->buffer->data + 2);
2580 if ((id_len > packet->buffer->len) ||
2581 (id_len > idp->truelen))
2584 silc_buffer_pull_tail(idp, 4 + id_len);
2585 silc_buffer_put(idp, packet->buffer->data, 4 + id_len);
2587 /* Process the New ID */
2588 silc_server_new_id_real(server, sock, new_id, FALSE);
2590 silc_buffer_push_tail(idp, 4 + id_len);
2591 silc_buffer_pull(packet->buffer, 4 + id_len);
2594 silc_buffer_free(idp);
2598 /* Received New Channel packet. Information about new channels in the
2599 network are distributed using this packet. Save the information about
2600 the new channel. This usually comes from router but also normal server
2601 can send this to notify channels it has when it connects to us. */
2603 void silc_server_new_channel(SilcServer server,
2604 SilcSocketConnection sock,
2605 SilcPacketContext *packet)
2607 SilcChannelPayload payload;
2608 SilcChannelID *channel_id;
2610 SilcUInt32 name_len;
2614 SilcServerEntry server_entry;
2615 SilcChannelEntry channel;
2617 SILC_LOG_DEBUG(("Processing New Channel"));
2619 if (sock->type == SILC_SOCKET_TYPE_CLIENT ||
2620 packet->src_id_type != SILC_ID_SERVER ||
2621 server->server_type == SILC_SERVER)
2624 /* Parse the channel payload */
2625 payload = silc_channel_payload_parse(packet->buffer->data,
2626 packet->buffer->len);
2630 /* Get the channel ID */
2631 channel_id = silc_channel_get_id_parse(payload);
2633 silc_channel_payload_free(payload);
2637 channel_name = silc_channel_get_name(payload, &name_len);
2639 channel_name[255] = '\0';
2641 id = silc_channel_get_id(payload, &id_len);
2643 server_entry = (SilcServerEntry)sock->user_data;
2645 if (sock->type == SILC_SOCKET_TYPE_ROUTER) {
2646 /* Add the channel to global list as it is coming from router. It
2647 cannot be our own channel as it is coming from router. */
2649 /* Check that we don't already have this channel */
2650 channel = silc_idlist_find_channel_by_name(server->local_list,
2651 channel_name, NULL);
2653 channel = silc_idlist_find_channel_by_name(server->global_list,
2654 channel_name, NULL);
2656 SILC_LOG_DEBUG(("New channel id(%s) from [Router] %s",
2657 silc_id_render(channel_id, SILC_ID_CHANNEL),
2661 silc_idlist_add_channel(server->global_list, strdup(channel_name),
2662 0, channel_id, sock->user_data, NULL, NULL, 0);
2665 channel->disabled = TRUE;
2667 server->stat.channels++;
2668 if (server->server_type == SILC_ROUTER)
2669 channel->users_resolved = TRUE;
2672 /* The channel is coming from our server, thus it is in our cell
2673 we will add it to our local list. */
2676 SILC_LOG_DEBUG(("Channel id(%s) from [Server] %s",
2677 silc_id_render(channel_id, SILC_ID_CHANNEL),
2680 /* Check that we don't already have this channel */
2681 channel = silc_idlist_find_channel_by_name(server->local_list,
2682 channel_name, NULL);
2684 channel = silc_idlist_find_channel_by_name(server->global_list,
2685 channel_name, NULL);
2687 /* If the channel does not exist, then create it. This creates a new
2688 key to the channel as well that we will send to the server. */
2690 /* The protocol says that the Channel ID's IP address must be based
2691 on the router's IP address. Check whether the ID is based in our
2692 IP and if it is not then create a new ID and enforce the server
2693 to switch the ID. */
2694 if (server_entry->server_type != SILC_BACKUP_ROUTER &&
2695 !SILC_ID_COMPARE(channel_id, server->id, server->id->ip.data_len)) {
2697 SILC_LOG_DEBUG(("Forcing the server to change Channel ID"));
2699 if (silc_id_create_channel_id(server, server->id, server->rng, &tmp)) {
2700 silc_server_send_notify_channel_change(server, sock, FALSE,
2702 silc_free(channel_id);
2707 /* Create the channel with the provided Channel ID */
2708 channel = silc_server_create_new_channel_with_id(server, NULL, NULL,
2712 silc_channel_payload_free(payload);
2713 silc_free(channel_id);
2716 channel->disabled = TRUE;
2719 /* CMODE change notify is expected */
2720 /* Get the mode and set it to the channel */
2721 channel->mode = silc_channel_get_mode(payload);
2724 /* Send the new channel key to the server */
2725 id = silc_id_id2str(channel->id, SILC_ID_CHANNEL);
2726 id_len = silc_id_get_len(channel->id, SILC_ID_CHANNEL);
2727 chk = silc_channel_key_payload_encode(id_len, id,
2728 strlen(channel->channel_key->
2730 channel->channel_key->cipher->name,
2731 channel->key_len / 8,
2733 silc_server_packet_send(server, sock, SILC_PACKET_CHANNEL_KEY, 0,
2734 chk->data, chk->len, FALSE);
2735 silc_buffer_free(chk);
2738 /* The channel exist by that name, check whether the ID's match.
2739 If they don't then we'll force the server to use the ID we have.
2740 We also create a new key for the channel. */
2741 SilcBuffer modes = NULL, users = NULL, users_modes = NULL;
2743 if (!SILC_ID_CHANNEL_COMPARE(channel_id, channel->id)) {
2744 /* They don't match, send CHANNEL_CHANGE notify to the server to
2745 force the ID change. */
2746 SILC_LOG_DEBUG(("Forcing the server to change Channel ID"));
2747 silc_server_send_notify_channel_change(server, sock, FALSE,
2748 channel_id, channel->id);
2751 /* If the mode is different from what we have then enforce the
2753 mode = silc_channel_get_mode(payload);
2754 if (channel->mode != mode) {
2755 SILC_LOG_DEBUG(("Forcing the server to change channel mode"));
2756 silc_server_send_notify_cmode(server, sock, FALSE, channel,
2757 channel->mode, server->id,
2758 SILC_ID_SERVER, channel->cipher,
2760 channel->passphrase,
2761 channel->founder_key);
2764 /* Create new key for the channel and send it to the server and
2765 everybody else possibly on the channel. */
2766 if (!(channel->mode & SILC_CHANNEL_MODE_PRIVKEY)) {
2767 if (!silc_server_create_channel_key(server, channel, 0))
2770 /* Send to the channel */
2771 silc_server_send_channel_key(server, sock, channel, FALSE);
2772 id = silc_id_id2str(channel->id, SILC_ID_CHANNEL);
2773 id_len = silc_id_get_len(channel->id, SILC_ID_CHANNEL);
2775 /* Send to the server */
2776 chk = silc_channel_key_payload_encode(id_len, id,
2777 strlen(channel->channel_key->
2779 channel->channel_key->
2781 channel->key_len / 8,
2783 silc_server_packet_send(server, sock, SILC_PACKET_CHANNEL_KEY, 0,
2784 chk->data, chk->len, FALSE);
2785 silc_buffer_free(chk);
2789 silc_free(channel_id);
2791 /* Since the channel is coming from server and we also know about it
2792 then send the JOIN notify to the server so that it see's our
2793 users on the channel "joining" the channel. */
2794 silc_server_announce_get_channel_users(server, channel, &modes, &users,
2797 silc_buffer_push(modes, modes->data - modes->head);
2798 silc_server_packet_send_dest(server, sock,
2799 SILC_PACKET_NOTIFY, SILC_PACKET_FLAG_LIST,
2800 channel->id, SILC_ID_CHANNEL,
2801 modes->data, modes->len, FALSE);
2802 silc_buffer_free(modes);
2805 silc_buffer_push(users, users->data - users->head);
2806 silc_server_packet_send(server, sock,
2807 SILC_PACKET_NOTIFY, SILC_PACKET_FLAG_LIST,
2808 users->data, users->len, FALSE);
2809 silc_buffer_free(users);
2812 silc_buffer_push(users_modes, users_modes->data - users_modes->head);
2813 silc_server_packet_send_dest(server, sock,
2814 SILC_PACKET_NOTIFY, SILC_PACKET_FLAG_LIST,
2815 channel->id, SILC_ID_CHANNEL,
2817 users_modes->len, FALSE);
2818 silc_buffer_free(users_modes);
2823 silc_channel_payload_free(payload);
2826 /* Received New Channel List packet, list of New Channel List payloads inside
2827 one packet. Process the New Channel payloads one by one. */
2829 void silc_server_new_channel_list(SilcServer server,
2830 SilcSocketConnection sock,
2831 SilcPacketContext *packet)
2833 SilcPacketContext *new;
2835 SilcUInt16 len1, len2;
2837 SILC_LOG_DEBUG(("Processing New Channel List"));
2839 if (sock->type == SILC_SOCKET_TYPE_CLIENT ||
2840 packet->src_id_type != SILC_ID_SERVER ||
2841 server->server_type == SILC_SERVER)
2844 /* If the sender of this packet is server and we are router we need to
2845 broadcast this packet to other routers in the network. Broadcast
2846 this list packet instead of multiple New Channel packets. */
2847 if (!server->standalone && server->server_type == SILC_ROUTER &&
2848 sock->type == SILC_SOCKET_TYPE_SERVER &&
2849 !(packet->flags & SILC_PACKET_FLAG_BROADCAST)) {
2850 SILC_LOG_DEBUG(("Broadcasting received New Channel List packet"));
2851 silc_server_packet_send(server, server->router->connection,
2853 packet->flags | SILC_PACKET_FLAG_BROADCAST,
2854 packet->buffer->data, packet->buffer->len, FALSE);
2855 silc_server_backup_send(server, (SilcServerEntry)sock->user_data,
2856 packet->type, packet->flags,
2857 packet->buffer->data, packet->buffer->len,
2861 /* Make copy of the original packet context, except for the actual
2862 data buffer, which we will here now fetch from the original buffer. */
2863 new = silc_packet_context_alloc();
2864 new->type = SILC_PACKET_NEW_CHANNEL;
2865 new->flags = packet->flags;
2866 new->src_id = packet->src_id;
2867 new->src_id_len = packet->src_id_len;
2868 new->src_id_type = packet->src_id_type;
2869 new->dst_id = packet->dst_id;
2870 new->dst_id_len = packet->dst_id_len;
2871 new->dst_id_type = packet->dst_id_type;
2873 buffer = silc_buffer_alloc(512);
2874 new->buffer = buffer;
2876 while (packet->buffer->len) {
2877 SILC_GET16_MSB(len1, packet->buffer->data);
2878 if ((len1 > packet->buffer->len) ||
2879 (len1 > buffer->truelen))
2882 SILC_GET16_MSB(len2, packet->buffer->data + 2 + len1);
2883 if ((len2 > packet->buffer->len) ||
2884 (len2 > buffer->truelen))
2887 silc_buffer_pull_tail(buffer, 8 + len1 + len2);
2888 silc_buffer_put(buffer, packet->buffer->data, 8 + len1 + len2);
2890 /* Process the New Channel */
2891 silc_server_new_channel(server, sock, new);
2893 silc_buffer_push_tail(buffer, 8 + len1 + len2);
2894 silc_buffer_pull(packet->buffer, 8 + len1 + len2);
2897 silc_buffer_free(buffer);
2901 /* Received key agreement packet. This packet is never for us. It is to
2902 the client in the packet's destination ID. Sending of this sort of packet
2903 equals sending private message, ie. it is sent point to point from
2904 one client to another. */
2906 void silc_server_key_agreement(SilcServer server,
2907 SilcSocketConnection sock,
2908 SilcPacketContext *packet)
2910 SilcSocketConnection dst_sock;
2911 SilcIDListData idata;
2913 SILC_LOG_DEBUG(("Start"));
2915 if (packet->src_id_type != SILC_ID_CLIENT ||
2916 packet->dst_id_type != SILC_ID_CLIENT)
2919 if (!packet->dst_id)
2922 /* Get the route to the client */
2923 dst_sock = silc_server_get_client_route(server, packet->dst_id,
2924 packet->dst_id_len, NULL,
2929 /* Relay the packet */
2930 silc_server_relay_packet(server, dst_sock, idata->send_key,
2931 idata->hmac_send, idata->psn_send++,
2935 /* Received connection auth request packet that is used during connection
2936 phase to resolve the mandatory authentication method. This packet can
2937 actually be received at anytime but usually it is used only during
2938 the connection authentication phase. Now, protocol says that this packet
2939 can come from client or server, however, we support only this coming
2940 from client and expect that server always knows what authentication
2943 void silc_server_connection_auth_request(SilcServer server,
2944 SilcSocketConnection sock,
2945 SilcPacketContext *packet)
2947 SilcServerConfigClient *client = NULL;
2948 SilcUInt16 conn_type;
2950 SilcAuthMethod auth_meth = SILC_AUTH_NONE;
2952 SILC_LOG_DEBUG(("Start"));
2954 if (packet->src_id_type && packet->src_id_type != SILC_ID_CLIENT)
2957 /* Parse the payload */
2958 ret = silc_buffer_unformat(packet->buffer,
2959 SILC_STR_UI_SHORT(&conn_type),
2960 SILC_STR_UI_SHORT(NULL),
2965 if (conn_type != SILC_SOCKET_TYPE_CLIENT)
2968 /* Get the authentication method for the client */
2969 auth_meth = SILC_AUTH_NONE;
2970 client = silc_server_config_find_client(server, sock->ip);
2972 client = silc_server_config_find_client(server, sock->hostname);
2974 if (client->passphrase) {
2975 if (client->publickeys && !server->config->prefer_passphrase_auth)
2976 auth_meth = SILC_AUTH_PUBLIC_KEY;
2978 auth_meth = SILC_AUTH_PASSWORD;
2979 } else if (client->publickeys)
2980 auth_meth = SILC_AUTH_PUBLIC_KEY;
2983 /* Send it back to the client */
2984 silc_server_send_connection_auth_request(server, sock, conn_type, auth_meth);
2987 /* Received REKEY packet. The sender of the packet wants to regenerate
2988 its session keys. This starts the REKEY protocol. */
2990 void silc_server_rekey(SilcServer server,
2991 SilcSocketConnection sock,
2992 SilcPacketContext *packet)
2994 SilcProtocol protocol;
2995 SilcServerRekeyInternalContext *proto_ctx;
2996 SilcIDListData idata = (SilcIDListData)sock->user_data;
2998 SILC_LOG_DEBUG(("Start"));
3000 /* Allocate internal protocol context. This is sent as context
3002 proto_ctx = silc_calloc(1, sizeof(*proto_ctx));
3003 proto_ctx->server = (void *)server;
3004 proto_ctx->sock = sock;
3005 proto_ctx->responder = TRUE;
3006 proto_ctx->pfs = idata->rekey->pfs;
3008 /* Perform rekey protocol. Will call the final callback after the
3009 protocol is over. */
3010 silc_protocol_alloc(SILC_PROTOCOL_SERVER_REKEY,
3011 &protocol, proto_ctx, silc_server_rekey_final);
3012 sock->protocol = protocol;
3014 if (proto_ctx->pfs == FALSE)
3015 /* Run the protocol */
3016 silc_protocol_execute(protocol, server->schedule, 0, 0);
3019 /* Received file transger packet. This packet is never for us. It is to
3020 the client in the packet's destination ID. Sending of this sort of packet
3021 equals sending private message, ie. it is sent point to point from
3022 one client to another. */
3024 void silc_server_ftp(SilcServer server,
3025 SilcSocketConnection sock,
3026 SilcPacketContext *packet)
3028 SilcSocketConnection dst_sock;
3029 SilcIDListData idata;
3031 SILC_LOG_DEBUG(("Start"));
3033 if (packet->src_id_type != SILC_ID_CLIENT ||
3034 packet->dst_id_type != SILC_ID_CLIENT)
3037 if (!packet->dst_id)
3040 /* Get the route to the client */
3041 dst_sock = silc_server_get_client_route(server, packet->dst_id,
3042 packet->dst_id_len, NULL,
3047 /* Relay the packet */
3048 silc_server_relay_packet(server, dst_sock, idata->send_key,
3049 idata->hmac_send, idata->psn_send++,
3055 SilcSocketConnection sock;
3056 SilcPacketContext *packet;
3058 } *SilcServerResumeResolve;
3060 SILC_SERVER_CMD_FUNC(resume_resolve)
3062 SilcServerResumeResolve r = (SilcServerResumeResolve)context;
3063 SilcServer server = r->server;
3064 SilcSocketConnection sock = r->sock;
3065 SilcServerCommandReplyContext reply = context2;
3066 SilcClientEntry client;
3068 SILC_LOG_DEBUG(("Start"));
3070 if (!reply || !silc_command_get_status(reply->payload, NULL, NULL)) {
3071 SILC_LOG_ERROR(("Client %s (%s) tried to resume unknown client, "
3072 "closing connection", sock->hostname, sock->ip));
3073 silc_server_disconnect_remote(server, sock,
3074 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
3075 "Resuming not possible");
3079 if (reply && silc_command_get(reply->payload) == SILC_COMMAND_WHOIS) {
3080 /* Get entry to the client, and resolve it if we don't have it. */
3081 client = silc_idlist_find_client_by_id(server->local_list,
3082 r->data, TRUE, NULL);
3084 client = silc_idlist_find_client_by_id(server->global_list,
3085 r->data, TRUE, NULL);
3087 SILC_LOG_ERROR(("Client %s (%s) tried to resume unknown client, "
3088 "closing connection", sock->hostname, sock->ip));
3089 silc_server_disconnect_remote(server, sock,
3090 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
3091 "Resuming not possible");
3096 if (!(client->mode & SILC_UMODE_DETACHED)) {
3097 SILC_LOG_ERROR(("Client %s (%s) tried to resume un-detached client, "
3098 "closing connection", sock->hostname, sock->ip));
3099 silc_server_disconnect_remote(server, sock,
3100 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
3101 "Resuming not possible");
3106 /* Reprocess the packet */
3107 silc_server_resume_client(server, sock, r->packet);
3110 silc_socket_free(r->sock);
3111 silc_packet_context_free(r->packet);
3116 /* Received client resuming packet. This is used to resume detached
3117 client session. It can be sent by the client who wishes to resume
3118 but this is also sent by servers and routers to notify other routers
3119 that the client is not detached anymore. */
3121 void silc_server_resume_client(SilcServer server,
3122 SilcSocketConnection sock,
3123 SilcPacketContext *packet)
3125 SilcBuffer buffer = packet->buffer, buf;
3126 SilcIDListData idata;
3127 SilcClientEntry detached_client;
3128 SilcClientID *client_id = NULL;
3129 unsigned char *id_string, *auth = NULL;
3130 SilcUInt16 id_len, auth_len = 0;
3131 int ret, nickfail = 0;
3132 bool resolved, local, nick_change = FALSE, resolve = FALSE;
3133 SilcChannelEntry channel;
3134 SilcHashTableList htl;
3135 SilcChannelClientEntry chl;
3136 SilcServerResumeResolve r;
3138 SILC_LOG_DEBUG(("Start"));
3140 ret = silc_buffer_unformat(buffer,
3141 SILC_STR_UI16_NSTRING(&id_string, &id_len),
3144 client_id = silc_id_str2id(id_string, id_len, SILC_ID_CLIENT);
3146 if (sock->type == SILC_SOCKET_TYPE_CLIENT) {
3147 /* Client send this and is attempting to resume to old client session */
3148 SilcClientEntry client;
3152 silc_buffer_pull(buffer, 2 + id_len);
3153 auth = buffer->data;
3154 auth_len = buffer->len;
3155 silc_buffer_push(buffer, 2 + id_len);
3158 if (!client_id || auth_len < 128) {
3159 SILC_LOG_ERROR(("Client %s (%s) sent incomplete resume information, "
3160 "closing connection", sock->hostname, sock->ip));
3161 silc_server_disconnect_remote(server, sock,
3162 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
3163 "Resuming not possible");
3167 /* Take client entry of this connection */
3168 client = (SilcClientEntry)sock->user_data;
3169 idata = (SilcIDListData)client;
3171 /* Get entry to the client, and resolve it if we don't have it. */
3172 detached_client = silc_server_get_client_resolve(server, client_id, FALSE,
3174 if (!detached_client) {
3176 /* The client info is being resolved. Reprocess this packet after
3177 receiving the reply to the query. */
3178 SILC_LOG_DEBUG(("Resolving client"));
3179 r = silc_calloc(1, sizeof(*r));
3183 r->sock = silc_socket_dup(sock);
3184 r->packet = silc_packet_context_dup(packet);
3185 r->data = silc_id_dup(client_id, SILC_ID_CLIENT);
3186 silc_server_command_pending(server, SILC_COMMAND_WHOIS,
3188 silc_server_command_resume_resolve, r);
3190 SILC_LOG_ERROR(("Client %s (%s) tried to resume unknown client, "
3191 "closing connection", sock->hostname, sock->ip));
3192 silc_server_disconnect_remote(server, sock,
3193 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
3194 "Resuming not possible");
3199 if (!(detached_client->mode & SILC_UMODE_DETACHED))
3201 if (!silc_hash_table_count(detached_client->channels) &&
3202 detached_client->router)
3204 if (!detached_client->nickname)
3208 if (server->server_type == SILC_SERVER && !server->standalone) {
3209 /* The client info is being resolved. Reprocess this packet after
3210 receiving the reply to the query. */
3211 SILC_LOG_DEBUG(("Resolving client info"));
3212 silc_server_get_client_resolve(server, client_id, TRUE, NULL);
3213 r = silc_calloc(1, sizeof(*r));
3217 r->sock = silc_socket_dup(sock);
3218 r->packet = silc_packet_context_dup(packet);
3219 r->data = silc_id_dup(client_id, SILC_ID_CLIENT);
3220 silc_server_command_pending(server, SILC_COMMAND_WHOIS,
3222 silc_server_command_resume_resolve, r);
3225 if (server->server_type == SILC_SERVER) {
3226 SILC_LOG_ERROR(("Client %s (%s) tried to resume un-detached client, "
3227 "closing connection", sock->hostname, sock->ip));
3228 silc_server_disconnect_remote(server, sock,
3229 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
3230 "Resuming not possible");
3235 /* Check that we have the public key of the client, if not then we must
3236 resolve it first. */
3237 if (!detached_client->data.public_key) {
3238 if (server->server_type == SILC_SERVER && server->standalone) {
3239 silc_server_disconnect_remote(server, sock,
3240 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
3241 "Resuming not possible");
3243 /* We must retrieve the detached client's public key by sending
3244 GETKEY command. Reprocess this packet after receiving the key */
3245 SilcBuffer idp = silc_id_payload_encode(client_id, SILC_ID_CLIENT);
3246 SilcSocketConnection dest_sock =
3247 silc_server_get_client_route(server, NULL, 0, client_id, NULL, NULL);
3249 SILC_LOG_DEBUG(("Resolving client public key"));
3251 silc_server_send_command(server, dest_sock ? dest_sock :
3252 server->router->connection,
3253 SILC_COMMAND_GETKEY, ++server->cmd_ident,
3254 1, 1, idp->data, idp->len);
3256 r = silc_calloc(1, sizeof(*r));
3261 r->sock = silc_socket_dup(sock);
3262 r->packet = silc_packet_context_dup(packet);
3263 silc_server_command_pending(server, SILC_COMMAND_GETKEY,
3265 silc_server_command_resume_resolve, r);
3267 silc_buffer_free(idp);
3270 } else if (!silc_pkcs_public_key_compare(detached_client->data.public_key,
3271 idata->public_key)) {
3272 /* We require that the connection and resuming authentication data
3273 must be using same key pair. */
3274 silc_server_disconnect_remote(server, sock,
3275 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
3276 "Resuming not possible");
3280 /* Verify the authentication payload. This has to be successful in
3281 order to allow the resuming */
3283 !silc_auth_verify_data(auth, auth_len, SILC_AUTH_PUBLIC_KEY,
3284 detached_client->data.public_key, 0,
3285 idata->hash, detached_client->id,
3287 SILC_LOG_ERROR(("Client %s (%s) resume authentication failed, "
3288 "closing connection", sock->hostname, sock->ip));
3289 silc_server_disconnect_remote(server, sock,
3290 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
3291 "Resuming not possible");
3295 /* Now resume the client to the network */
3297 silc_schedule_task_del_by_context(server->schedule, detached_client);
3298 sock->user_data = detached_client;
3299 detached_client->connection = sock;
3301 /* Take new keys and stuff into use in the old entry */
3302 silc_idlist_del_data(detached_client);
3303 silc_idlist_add_data(detached_client, idata);
3304 detached_client->data.status |= SILC_IDLIST_STATUS_REGISTERED;
3305 detached_client->data.status |= SILC_IDLIST_STATUS_RESUMED;
3306 detached_client->mode &= ~SILC_UMODE_DETACHED;
3308 /* Send the RESUME_CLIENT packet to our primary router so that others
3309 know this client isn't detached anymore. */
3310 buf = silc_buffer_alloc_size(2 + id_len);
3311 silc_buffer_format(buf,
3312 SILC_STR_UI_SHORT(id_len),
3313 SILC_STR_UI_XNSTRING(id_string, id_len),
3316 /* Send to primary router */
3317 if (!server->standalone)
3318 silc_server_packet_send(server, server->router->connection,
3319 SILC_PACKET_RESUME_CLIENT, 0,
3320 buf->data, buf->len, TRUE);
3322 /* As router we must deliver this packet directly to the original
3323 server whom this client was earlier. */
3324 if (server->server_type == SILC_ROUTER && detached_client->router &&
3325 detached_client->router->server_type != SILC_ROUTER)
3326 silc_server_packet_send(server, detached_client->router->connection,
3327 SILC_PACKET_RESUME_CLIENT, 0,
3328 buf->data, buf->len, TRUE);
3329 silc_buffer_free(buf);
3331 detached_client->router = NULL;
3333 /* Delete this client entry since we're resuming to old one. */
3334 server->stat.my_clients--;
3335 server->stat.clients--;
3336 if (server->stat.cell_clients)
3337 server->stat.cell_clients--;
3338 silc_server_del_from_watcher_list(server, client);
3339 silc_idlist_del_client(server->local_list, client);
3340 client = detached_client;
3342 /* If the ID is not based in our ID then change it */
3343 if (!SILC_ID_COMPARE(client->id, server->id, server->id->ip.data_len)) {
3344 while (!silc_id_create_client_id(server, server->id, server->rng,
3345 server->md5hash, client->nickname,
3349 silc_server_disconnect_remote(server, sock,
3350 SILC_STATUS_ERR_BAD_NICKNAME, NULL);
3353 snprintf(&client->nickname[strlen(client->nickname) - 1], 1,
3360 /* Notify about Client ID change, nickname doesn't actually change. */
3361 if (!server->standalone)
3362 silc_server_send_notify_nick_change(server, server->router->connection,
3363 FALSE, client->id, client_id,
3367 /* Resolve users on those channels that client has joined but we
3368 haven't resolved user list yet. */
3369 if (server->server_type == SILC_SERVER && !server->standalone) {
3370 silc_hash_table_list(client->channels, &htl);
3371 while (silc_hash_table_get(&htl, NULL, (void **)&chl)) {
3372 channel = chl->channel;
3373 SILC_LOG_DEBUG(("Resolving users for %s channel",
3374 channel->channel_name));
3375 if (channel->disabled || !channel->users_resolved) {
3376 silc_server_send_command(server, server->router->connection,
3377 SILC_COMMAND_USERS, ++server->cmd_ident,
3378 1, 2, channel->channel_name,
3379 strlen(channel->channel_name));
3382 silc_hash_table_list_reset(&htl);
3385 /* Send the new client ID to the client. After this client may start
3386 receiving other packets, and may start sending packets too. */
3387 silc_server_send_new_id(server, sock, FALSE, client_id, SILC_ID_CLIENT,
3388 silc_id_get_len(client_id, SILC_ID_CLIENT));
3391 /* Send NICK change notify to channels as well. */
3392 SilcBuffer oidp, nidp;
3393 oidp = silc_id_payload_encode(client->id, SILC_ID_CLIENT);
3394 nidp = silc_id_payload_encode(client_id, SILC_ID_CLIENT);
3395 silc_server_send_notify_on_channels(server, NULL, client,
3396 SILC_NOTIFY_TYPE_NICK_CHANGE, 3,
3397 oidp->data, oidp->len,
3398 nidp->data, nidp->len,
3400 strlen(client->nickname));
3401 silc_buffer_free(oidp);
3402 silc_buffer_free(nidp);
3405 /* Add the client again to the ID cache to get it to correct list */
3406 if (!silc_idcache_del_by_context(server->local_list->clients, client))
3407 silc_idcache_del_by_context(server->global_list->clients, client);
3408 silc_free(client->id);
3409 client->id = client_id;
3411 silc_idcache_add(server->local_list->clients, client->nickname,
3412 client->id, client, 0, NULL);
3414 /* Send some nice info to the client */
3415 silc_server_send_connect_notifys(server, sock, client);
3417 /* Send all channel keys of channels the client has joined */
3418 silc_hash_table_list(client->channels, &htl);
3419 while (silc_hash_table_get(&htl, NULL, (void **)&chl)) {
3420 bool created = FALSE;
3421 channel = chl->channel;
3423 if (channel->mode & SILC_CHANNEL_MODE_PRIVKEY)
3426 /* If we don't have channel key, then create one */
3427 if (!channel->channel_key) {
3428 if (!silc_server_create_channel_key(server, channel, 0))
3433 id_string = silc_id_id2str(channel->id, SILC_ID_CHANNEL);
3435 silc_channel_key_payload_encode(silc_id_get_len(channel->id,
3438 strlen(channel->channel_key->
3440 channel->channel_key->cipher->name,
3441 channel->key_len / 8, channel->key);
3442 silc_free(id_string);
3444 /* Send the key packet to client */
3445 silc_server_packet_send(server, sock, SILC_PACKET_CHANNEL_KEY, 0,
3446 keyp->data, keyp->len, FALSE);
3448 if (created && server->server_type == SILC_SERVER &&
3449 !server->standalone)
3450 silc_server_packet_send(server, server->router->connection,
3451 SILC_PACKET_CHANNEL_KEY, 0,
3452 keyp->data, keyp->len, FALSE);
3454 silc_buffer_free(keyp);
3456 silc_hash_table_list_reset(&htl);
3458 } else if (sock->type != SILC_SOCKET_TYPE_CLIENT) {
3459 /* Server or router sent this to us to notify that that a client has
3461 SilcServerEntry server_entry;
3462 SilcServerID *server_id;
3467 /* Get entry to the client, and resolve it if we don't have it. */
3468 detached_client = silc_idlist_find_client_by_id(server->local_list,
3469 client_id, TRUE, NULL);
3470 if (!detached_client) {
3471 detached_client = silc_idlist_find_client_by_id(server->global_list,
3472 client_id, TRUE, NULL);
3473 if (!detached_client)
3477 /* Check that the client has not been resumed already because it is
3478 protocol error to attempt to resume more than once. The client
3479 will be killed if this protocol error occurs. */
3480 if (detached_client->data.status & SILC_IDLIST_STATUS_RESUMED &&
3481 !(detached_client->mode & SILC_UMODE_DETACHED)) {
3482 /* The client is clearly attempting to resume more than once and
3483 perhaps playing around by resuming from several different places
3484 at the same time. */
3485 silc_server_kill_client(server, detached_client, NULL,
3486 server->id, SILC_ID_SERVER);
3490 /* Check whether client is detached at all */
3491 if (!(detached_client->mode & SILC_UMODE_DETACHED))
3494 /* Client is detached, and now it is resumed. Remove the detached
3495 mode and mark that it is resumed. */
3496 detached_client->mode &= ~SILC_UMODE_DETACHED;
3497 detached_client->data.status |= SILC_IDLIST_STATUS_RESUMED;
3499 /* Get the new owner of the resumed client */
3500 server_id = silc_id_str2id(packet->src_id, packet->src_id_len,
3501 packet->src_id_type);
3505 /* Get server entry */
3506 server_entry = silc_idlist_find_server_by_id(server->global_list,
3507 server_id, TRUE, NULL);
3509 if (!server_entry) {
3510 server_entry = silc_idlist_find_server_by_id(server->local_list,
3511 server_id, TRUE, NULL);
3513 if (!server_entry) {
3514 silc_free(server_id);
3519 if (server->server_type == SILC_ROUTER &&
3520 sock->type == SILC_SOCKET_TYPE_ROUTER &&
3521 server_entry->server_type == SILC_ROUTER)
3524 SILC_LOG_DEBUG(("Resuming detached client"));
3526 /* Change the client to correct list. */
3527 if (!silc_idcache_del_by_context(server->local_list->clients,
3529 silc_idcache_del_by_context(server->global_list->clients,
3531 silc_idcache_add(local && server->server_type == SILC_ROUTER ?
3532 server->local_list->clients :
3533 server->global_list->clients,
3534 detached_client->nickname,
3535 detached_client->id, detached_client, FALSE, NULL);
3537 /* Change the owner of the client if needed */
3538 if (detached_client->router != server_entry)
3539 detached_client->router = server_entry;
3541 /* Update channel information regarding global clients on channel. */
3542 if (server->server_type == SILC_SERVER) {
3543 silc_hash_table_list(detached_client->channels, &htl);
3544 while (silc_hash_table_get(&htl, NULL, (void **)&chl))
3545 chl->channel->global_users =
3546 silc_server_channel_has_global(chl->channel);
3547 silc_hash_table_list_reset(&htl);
3550 silc_schedule_task_del_by_context(server->schedule, detached_client);
3552 /* If the sender of this packet is server and we are router we need to
3553 broadcast this packet to other routers in the network. */
3554 if (!server->standalone && server->server_type == SILC_ROUTER &&
3555 sock->type == SILC_SOCKET_TYPE_SERVER &&
3556 !(packet->flags & SILC_PACKET_FLAG_BROADCAST)) {
3557 SILC_LOG_DEBUG(("Broadcasting received Resume Client packet"));
3558 silc_server_packet_send(server, server->router->connection,
3560 packet->flags | SILC_PACKET_FLAG_BROADCAST,
3561 buffer->data, buffer->len, FALSE);
3562 silc_server_backup_send(server, (SilcServerEntry)sock->user_data,
3563 packet->type, packet->flags,
3564 packet->buffer->data, packet->buffer->len,
3568 silc_free(server_id);
3571 silc_free(client_id);