5 Author: Pekka Riikonen <priikone@silcnet.org>
7 Copyright (C) 1997 - 2002 Pekka Riikonen
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
21 * Server packet routines to handle received packets.
25 #include "serverincludes.h"
26 #include "server_internal.h"
28 /* Received notify packet. Server can receive notify packets from router.
29 Server then relays the notify messages to clients if needed. */
31 void silc_server_notify(SilcServer server,
32 SilcSocketConnection sock,
33 SilcPacketContext *packet)
35 SilcNotifyPayload payload;
37 SilcArgumentPayload args;
38 SilcChannelID *channel_id = NULL, *channel_id2;
39 SilcClientID *client_id, *client_id2;
40 SilcServerID *server_id;
42 SilcChannelEntry channel = NULL;
43 SilcClientEntry client = NULL, client2 = NULL;
44 SilcServerEntry server_entry = NULL;
45 SilcChannelClientEntry chl;
46 SilcIDCacheEntry cache;
47 SilcHashTableList htl;
53 if (sock->type == SILC_SOCKET_TYPE_CLIENT ||
54 packet->src_id_type != SILC_ID_SERVER || !packet->dst_id) {
55 SILC_LOG_DEBUG(("Bad notify packet received"));
59 /* If the packet is destined directly to a client then relay the packet
60 before processing it. */
61 if (packet->dst_id_type == SILC_ID_CLIENT) {
63 SilcSocketConnection dst_sock;
65 /* Get the route to the client */
66 dst_sock = silc_server_get_client_route(server, packet->dst_id,
67 packet->dst_id_len, NULL,
70 /* Relay the packet */
71 silc_server_relay_packet(server, dst_sock, idata->send_key,
72 idata->hmac_send, idata->psn_send++,
76 /* Parse the Notify Payload */
77 payload = silc_notify_payload_parse(packet->buffer->data,
82 /* If we are router and this packet is not already broadcast packet
83 we will broadcast it. The sending socket really cannot be router or
84 the router is buggy. If this packet is coming from router then it must
85 have the broadcast flag set already and we won't do anything. */
86 if (server->server_type == SILC_ROUTER &&
87 sock->type == SILC_SOCKET_TYPE_SERVER &&
88 !(packet->flags & SILC_PACKET_FLAG_BROADCAST)) {
89 SILC_LOG_DEBUG(("Broadcasting received Notify packet"));
90 if (packet->dst_id_type == SILC_ID_CHANNEL) {
91 /* Packet is destined to channel */
92 channel_id = silc_id_str2id(packet->dst_id, packet->dst_id_len,
97 silc_server_packet_send_dest(server, SILC_PRIMARY_ROUTE(server),
98 packet->type, packet->flags |
99 SILC_PACKET_FLAG_BROADCAST,
100 channel_id, SILC_ID_CHANNEL,
101 packet->buffer->data,
102 packet->buffer->len, FALSE);
103 silc_server_backup_send_dest(server, (SilcServerEntry)sock->user_data,
104 packet->type, packet->flags,
105 channel_id, SILC_ID_CHANNEL,
106 packet->buffer->data, packet->buffer->len,
109 /* Packet is destined to client or server */
110 silc_server_packet_send(server, SILC_PRIMARY_ROUTE(server),
112 packet->flags | SILC_PACKET_FLAG_BROADCAST,
113 packet->buffer->data, packet->buffer->len,
115 silc_server_backup_send(server, (SilcServerEntry)sock->user_data,
116 packet->type, packet->flags,
117 packet->buffer->data, packet->buffer->len,
122 type = silc_notify_get_type(payload);
123 args = silc_notify_get_args(payload);
128 case SILC_NOTIFY_TYPE_JOIN:
130 * Distribute the notify to local clients on the channel
132 SILC_LOG_DEBUG(("JOIN notify"));
135 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
138 channel_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
142 /* Get channel entry */
143 channel = silc_idlist_find_channel_by_id(server->global_list,
146 channel = silc_idlist_find_channel_by_id(server->local_list,
149 SILC_LOG_DEBUG(("Notify for unknown channel"));
150 silc_free(channel_id);
154 silc_free(channel_id);
157 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
160 client_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
164 /* If the the client is not in local list we check global list (ie. the
165 channel will be global channel) and if it does not exist then create
166 entry for the client. */
167 client = silc_idlist_find_client_by_id(server->global_list,
168 client_id, server->server_type,
171 client = silc_idlist_find_client_by_id(server->local_list,
172 client_id, server->server_type,
175 /* If router did not find the client the it is bogus */
176 if (server->server_type != SILC_SERVER)
180 silc_idlist_add_client(server->global_list, NULL, NULL, NULL,
181 silc_id_dup(client_id, SILC_ID_CLIENT),
182 sock->user_data, NULL, 0);
184 SILC_LOG_ERROR(("Could not add new client to the ID Cache"));
185 silc_free(client_id);
189 client->data.status |= SILC_IDLIST_STATUS_REGISTERED;
193 /* Do not process the notify if the client is not registered */
194 if (!(client->data.status & SILC_IDLIST_STATUS_REGISTERED))
197 /* Do not add client to channel if it is there already */
198 if (silc_server_client_on_channel(client, channel, NULL)) {
199 SILC_LOG_DEBUG(("Client already on channel"));
203 /* Send to channel */
204 silc_server_packet_send_to_channel(server, sock, channel, packet->type,
205 FALSE, packet->buffer->data,
206 packet->buffer->len, FALSE);
208 if (server->server_type != SILC_ROUTER &&
209 sock->type == SILC_SOCKET_TYPE_ROUTER)
210 /* The channel is global now */
211 channel->global_users = TRUE;
213 SILC_LOG_DEBUG(("Joining to channel %s", channel->channel_name));
215 /* JOIN the global client to the channel (local clients (if router
216 created the channel) is joined in the pending JOIN command). */
217 chl = silc_calloc(1, sizeof(*chl));
218 chl->client = client;
219 chl->channel = channel;
221 /* If this is the first one on the channel then it is the founder of
223 if (!silc_hash_table_count(channel->user_list))
224 chl->mode = (SILC_CHANNEL_UMODE_CHANOP | SILC_CHANNEL_UMODE_CHANFO);
226 silc_hash_table_add(channel->user_list, client, chl);
227 silc_hash_table_add(client->channels, channel, chl);
228 silc_free(client_id);
229 channel->user_count++;
230 channel->disabled = FALSE;
232 /* Update statistics */
233 if (server->server_type == SILC_ROUTER) {
234 if (sock->type != SILC_SOCKET_TYPE_ROUTER)
235 server->stat.cell_chanclients++;
236 server->stat.chanclients++;
241 case SILC_NOTIFY_TYPE_LEAVE:
243 * Distribute the notify to local clients on the channel
245 SILC_LOG_DEBUG(("LEAVE notify"));
248 channel_id = silc_id_str2id(packet->dst_id, packet->dst_id_len,
249 packet->dst_id_type);
254 /* Get channel entry */
255 channel = silc_idlist_find_channel_by_id(server->global_list,
258 channel = silc_idlist_find_channel_by_id(server->local_list,
261 SILC_LOG_DEBUG(("Notify for unknown channel"));
262 silc_free(channel_id);
268 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
270 silc_free(channel_id);
273 client_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
275 silc_free(channel_id);
279 /* Get client entry */
280 client = silc_idlist_find_client_by_id(server->global_list,
281 client_id, TRUE, NULL);
283 client = silc_idlist_find_client_by_id(server->local_list,
284 client_id, TRUE, NULL);
286 silc_free(client_id);
287 silc_free(channel_id);
291 silc_free(client_id);
293 /* Check if on channel */
294 if (!silc_server_client_on_channel(client, channel, NULL))
297 /* Send the leave notify to channel */
298 silc_server_packet_send_to_channel(server, sock, channel, packet->type,
299 FALSE, packet->buffer->data,
300 packet->buffer->len, FALSE);
302 /* Remove the user from channel */
303 silc_server_remove_from_one_channel(server, sock, channel, client, FALSE);
306 case SILC_NOTIFY_TYPE_SIGNOFF:
308 * Distribute the notify to local clients on the channel
310 SILC_LOG_DEBUG(("SIGNOFF notify"));
313 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
316 client_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
320 /* Get client entry */
321 client = silc_idlist_find_client_by_id(server->global_list,
322 client_id, TRUE, &cache);
324 client = silc_idlist_find_client_by_id(server->local_list,
325 client_id, TRUE, &cache);
327 silc_free(client_id);
331 silc_free(client_id);
333 /* Get signoff message */
334 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
338 /* Update statistics */
339 server->stat.clients--;
340 if (server->stat.cell_clients)
341 server->stat.cell_clients--;
342 SILC_OPER_STATS_UPDATE(client, server, SILC_UMODE_SERVER_OPERATOR);
343 SILC_OPER_STATS_UPDATE(client, router, SILC_UMODE_ROUTER_OPERATOR);
344 silc_schedule_task_del_by_context(server->schedule, client);
346 /* Remove the client from all channels. */
347 silc_server_remove_from_channels(server, NULL, client, TRUE, tmp, FALSE);
349 /* Check if anyone is watching this nickname */
350 if (server->server_type == SILC_ROUTER)
351 silc_server_check_watcher_list(server, client, NULL,
352 SILC_NOTIFY_TYPE_SIGNOFF);
354 /* Remove this client from watcher list if it is */
355 silc_server_del_from_watcher_list(server, client);
357 client->data.status &= ~SILC_IDLIST_STATUS_REGISTERED;
358 cache->expire = SILC_ID_CACHE_EXPIRE_DEF;
361 case SILC_NOTIFY_TYPE_TOPIC_SET:
363 * Distribute the notify to local clients on the channel
366 SILC_LOG_DEBUG(("TOPIC SET notify"));
369 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
372 client_id = silc_id_payload_parse_id(tmp, tmp_len, &id_type);
376 /* Get client entry */
377 if (id_type == SILC_ID_CLIENT) {
378 client = silc_idlist_find_client_by_id(server->global_list,
379 client_id, TRUE, &cache);
381 client = silc_idlist_find_client_by_id(server->local_list,
382 client_id, TRUE, &cache);
384 silc_free(client_id);
388 silc_free(client_id);
392 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
394 silc_free(channel_id);
399 channel_id = silc_id_str2id(packet->dst_id, packet->dst_id_len,
400 packet->dst_id_type);
405 /* Get channel entry */
406 channel = silc_idlist_find_channel_by_id(server->global_list,
409 channel = silc_idlist_find_channel_by_id(server->local_list,
412 SILC_LOG_DEBUG(("Notify for unknown channel"));
413 silc_free(channel_id);
418 if (channel->topic && !strcmp(channel->topic, tmp)) {
419 SILC_LOG_DEBUG(("Topic is already set and same"));
424 /* Get user's channel entry and check that topic set is allowed. */
425 if (!silc_server_client_on_channel(client, channel, &chl))
427 if (channel->mode & SILC_CHANNEL_MODE_TOPIC &&
428 !(chl->mode & SILC_CHANNEL_UMODE_CHANOP) &&
429 !(chl->mode & SILC_CHANNEL_UMODE_CHANFO)) {
430 SILC_LOG_DEBUG(("Topic change is not allowed"));
435 /* Change the topic */
436 silc_free(channel->topic);
437 channel->topic = strdup(tmp);
439 /* Send the same notify to the channel */
440 silc_server_packet_send_to_channel(server, NULL, channel, packet->type,
441 FALSE, packet->buffer->data,
442 packet->buffer->len, FALSE);
443 silc_free(channel_id);
446 case SILC_NOTIFY_TYPE_NICK_CHANGE:
449 * Distribute the notify to local clients on the channel
451 unsigned char *id, *id2;
453 SilcUInt32 nickname_len;
455 SILC_LOG_DEBUG(("NICK CHANGE notify"));
457 /* Get old client ID */
458 id = silc_argument_get_arg_type(args, 1, &tmp_len);
461 client_id = silc_id_payload_parse_id(id, tmp_len, NULL);
465 /* Get new client ID */
466 id2 = silc_argument_get_arg_type(args, 2, &tmp_len);
469 client_id2 = silc_id_payload_parse_id(id2, tmp_len, NULL);
473 SILC_LOG_DEBUG(("Old Client ID id(%s)",
474 silc_id_render(client_id, SILC_ID_CLIENT)));
475 SILC_LOG_DEBUG(("New Client ID id(%s)",
476 silc_id_render(client_id2, SILC_ID_CLIENT)));
478 /* From protocol version 1.1 we also get the new nickname */
479 nickname = silc_argument_get_arg_type(args, 3, &nickname_len);;
481 /* Replace the Client ID */
482 client = silc_idlist_replace_client_id(server,
483 server->global_list, client_id,
484 client_id2, nickname);
486 client = silc_idlist_replace_client_id(server,
487 server->local_list, client_id,
488 client_id2, nickname);
491 /* Send the NICK_CHANGE notify type to local clients on the channels
492 this client is joined to. */
493 silc_server_send_notify_on_channels(server, client, client,
494 SILC_NOTIFY_TYPE_NICK_CHANGE, 3,
495 id, tmp_len, id2, tmp_len,
500 silc_free(client_id);
502 silc_free(client_id2);
506 case SILC_NOTIFY_TYPE_CMODE_CHANGE:
508 * Distribute the notify to local clients on the channel
511 SILC_LOG_DEBUG(("CMODE CHANGE notify"));
514 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
517 client_id = silc_id_payload_parse_id(tmp, tmp_len, &id_type);
521 /* Get client entry */
522 if (id_type == SILC_ID_CLIENT) {
523 client = silc_idlist_find_client_by_id(server->global_list,
524 client_id, TRUE, &cache);
526 client = silc_idlist_find_client_by_id(server->local_list,
527 client_id, TRUE, &cache);
529 silc_free(client_id);
533 silc_free(client_id);
537 channel_id = silc_id_str2id(packet->dst_id, packet->dst_id_len,
538 packet->dst_id_type);
543 /* Get channel entry */
544 channel = silc_idlist_find_channel_by_id(server->global_list,
547 channel = silc_idlist_find_channel_by_id(server->local_list,
550 SILC_LOG_DEBUG(("Notify for unknown channel"));
551 silc_free(channel_id);
555 silc_free(channel_id);
558 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
561 SILC_GET32_MSB(mode, tmp);
563 /* Check if mode changed */
564 if (channel->mode == mode) {
565 SILC_LOG_DEBUG(("Mode is changed already"));
567 /* If this mode change has founder mode then we'll enforce the
568 change so that the server gets the real founder public key */
569 if (server->server_type != SILC_SERVER &&
570 sock != SILC_PRIMARY_ROUTE(server) &&
571 mode & SILC_CHANNEL_MODE_FOUNDER_AUTH && channel->founder_key) {
572 SILC_LOG_DEBUG(("Sending founder public key to server"));
573 silc_server_send_notify_cmode(server, sock, FALSE, channel,
574 channel->mode, server->id,
575 SILC_ID_SERVER, channel->cipher,
578 channel->founder_key);
581 /* If we received same mode from our primary check whether founder
582 mode and key in the notify is set. We update the founder key
583 here since we may have wrong one */
584 if (server->server_type == SILC_SERVER &&
585 sock == SILC_PRIMARY_ROUTE(server) &&
586 mode & SILC_CHANNEL_MODE_FOUNDER_AUTH) {
587 SILC_LOG_DEBUG(("Founder public key received from primary router"));
588 tmp = silc_argument_get_arg_type(args, 6, &tmp_len);
592 if (channel->founder_key)
593 silc_pkcs_public_key_free(channel->founder_key);
594 channel->founder_key = NULL;
595 silc_pkcs_public_key_decode(tmp, tmp_len, &channel->founder_key);
601 /* Get user's channel entry and check that mode change is allowed */
603 if (!silc_server_client_on_channel(client, channel, &chl))
605 if (!silc_server_check_cmode_rights(server, channel, chl, mode)) {
606 SILC_LOG_DEBUG(("CMODE change is not allowed"));
607 silc_server_send_notify_cmode(server, sock, FALSE, channel,
608 channel->mode, server->id,
609 SILC_ID_SERVER, channel->cipher,
612 channel->founder_key);
616 /* Assure that server is not removing founder mode from us */
617 if (server->server_type == SILC_ROUTER &&
618 sock != SILC_PRIMARY_ROUTE(server) &&
619 channel->mode & SILC_CHANNEL_MODE_FOUNDER_AUTH &&
620 !(mode & SILC_CHANNEL_MODE_FOUNDER_AUTH)) {
621 SILC_LOG_DEBUG(("Enforcing sender to change channel mode"));
622 silc_server_send_notify_cmode(server, sock, FALSE, channel,
623 channel->mode, server->id,
624 SILC_ID_SERVER, channel->cipher,
627 channel->founder_key);
631 /* If server is adding founder mode, check whether there is founder
632 on channel already and is not from this server */
633 if (server->server_type == SILC_ROUTER &&
634 sock != SILC_PRIMARY_ROUTE(server) &&
635 mode & SILC_CHANNEL_MODE_FOUNDER_AUTH) {
636 silc_hash_table_list(channel->user_list, &htl);
637 while (silc_hash_table_get(&htl, NULL, (void *)&chl))
638 if (chl->mode & SILC_CHANNEL_UMODE_CHANFO &&
639 chl->client->router != sock->user_data) {
640 SILC_LOG_DEBUG(("Enforcing sender to change channel mode"));
641 silc_server_send_notify_cmode(server, sock, FALSE, channel,
642 channel->mode, server->id,
643 SILC_ID_SERVER, channel->cipher,
646 channel->founder_key);
647 silc_hash_table_list_reset(&htl);
650 silc_hash_table_list_reset(&htl);
654 /* If the channel had private keys set and the mode was removed then
655 we must re-generate and re-distribute a new channel key */
656 if (channel->mode & SILC_CHANNEL_MODE_PRIVKEY &&
657 !(mode & SILC_CHANNEL_MODE_PRIVKEY)) {
658 /* Re-generate channel key */
659 if (!silc_server_create_channel_key(server, channel, 0))
662 /* Send the channel key. This sends it to our local clients and if
663 we are normal server to our router as well. */
664 silc_server_send_channel_key(server, NULL, channel,
665 server->server_type == SILC_ROUTER ?
666 FALSE : !server->standalone);
670 tmp = silc_argument_get_arg_type(args, 4, &tmp_len);
672 unsigned char hash[32];
675 silc_hmac_free(channel->hmac);
676 if (!silc_hmac_alloc(tmp, NULL, &channel->hmac))
679 /* Set the HMAC key out of current channel key. The client must do
681 silc_hash_make(silc_hmac_get_hash(channel->hmac), channel->key,
682 channel->key_len / 8, hash);
683 silc_hmac_set_key(channel->hmac, hash,
684 silc_hash_len(silc_hmac_get_hash(channel->hmac)));
685 memset(hash, 0, sizeof(hash));
688 /* Get the passphrase */
689 tmp = silc_argument_get_arg_type(args, 5, &tmp_len);
691 silc_free(channel->passphrase);
692 channel->passphrase = silc_memdup(tmp, tmp_len);
695 /* Get founder public key */
696 tmp = silc_argument_get_arg_type(args, 6, &tmp_len);
697 if (tmp && mode & SILC_CHANNEL_MODE_FOUNDER_AUTH) {
698 if (channel->founder_key)
699 silc_pkcs_public_key_free(channel->founder_key);
700 channel->founder_key = NULL;
701 silc_pkcs_public_key_decode(tmp, tmp_len, &channel->founder_key);
703 if (!channel->founder_key ||
704 (client && client->data.public_key &&
705 server->server_type == SILC_ROUTER &&
706 !silc_pkcs_public_key_compare(channel->founder_key,
707 client->data.public_key))) {
708 /* A really buggy server isn't checking public keys correctly.
709 It's not possible that the mode setter and founder wouldn't
710 have same public key. */
711 SILC_LOG_DEBUG(("Enforcing sender to change channel mode"));
713 mode &= ~SILC_CHANNEL_MODE_FOUNDER_AUTH;
714 silc_server_send_notify_cmode(server, sock, FALSE, channel,
715 mode, server->id, SILC_ID_SERVER,
718 channel->passphrase, NULL);
719 if (channel->founder_key)
720 silc_pkcs_public_key_free(channel->founder_key);
721 channel->founder_key = NULL;
722 } else if (client && !client->data.public_key) {
723 client->data.public_key =
724 silc_pkcs_public_key_copy(channel->founder_key);
728 if (mode & SILC_CHANNEL_MODE_FOUNDER_AUTH && !channel->founder_key &&
729 server->server_type == SILC_ROUTER) {
730 SILC_LOG_DEBUG(("Enforcing sender to change channel mode"));
731 mode &= ~SILC_CHANNEL_MODE_FOUNDER_AUTH;
732 silc_server_send_notify_cmode(server, sock, FALSE, channel,
733 mode, server->id, SILC_ID_SERVER,
736 channel->passphrase, NULL);
739 /* Send the same notify to the channel */
740 silc_server_packet_send_to_channel(server, NULL, channel, packet->type,
741 FALSE, packet->buffer->data,
742 packet->buffer->len, FALSE);
745 channel->mode = mode;
747 if (!(channel->mode & SILC_CHANNEL_MODE_FOUNDER_AUTH) &&
748 channel->founder_key) {
749 silc_pkcs_public_key_free(channel->founder_key);
750 channel->founder_key = NULL;
755 case SILC_NOTIFY_TYPE_CUMODE_CHANGE:
758 * Distribute the notify to local clients on the channel
760 SilcChannelClientEntry chl2 = NULL;
761 bool notify_sent = FALSE;
763 SILC_LOG_DEBUG(("CUMODE CHANGE notify"));
766 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
769 client_id = silc_id_payload_parse_id(tmp, tmp_len, &id_type);
773 /* Get client entry */
774 if (id_type == SILC_ID_CLIENT) {
775 client = silc_idlist_find_client_by_id(server->global_list,
776 client_id, TRUE, &cache);
778 client = silc_idlist_find_client_by_id(server->local_list,
779 client_id, TRUE, &cache);
781 silc_free(client_id);
785 silc_free(client_id);
789 channel_id = silc_id_str2id(packet->dst_id, packet->dst_id_len,
790 packet->dst_id_type);
795 /* Get channel entry */
796 channel = silc_idlist_find_channel_by_id(server->global_list,
799 channel = silc_idlist_find_channel_by_id(server->local_list,
802 SILC_LOG_DEBUG(("Notify for unknown channel"));
803 silc_free(channel_id);
809 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
811 silc_free(channel_id);
815 SILC_GET32_MSB(mode, tmp);
817 /* Get target client */
818 tmp = silc_argument_get_arg_type(args, 3, &tmp_len);
821 client_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
825 /* Get client entry */
826 client2 = silc_idlist_find_client_by_id(server->global_list,
827 client_id, TRUE, NULL);
829 client2 = silc_idlist_find_client_by_id(server->local_list,
830 client_id, TRUE, NULL);
832 silc_free(client_id);
836 silc_free(client_id);
839 /* Check that sender is on channel */
840 if (!silc_server_client_on_channel(client, channel, &chl))
843 if (client != client2 && server->server_type == SILC_ROUTER) {
844 /* Sender must be operator */
845 if (!(chl->mode & SILC_CHANNEL_UMODE_CHANOP) &&
846 !(chl->mode & SILC_CHANNEL_UMODE_CHANFO)) {
847 SILC_LOG_DEBUG(("CUMODE change is not allowed"));
851 if (!silc_server_client_on_channel(client2, channel, &chl))
854 /* If target is founder mode change is not allowed. */
855 if (chl->mode & SILC_CHANNEL_UMODE_CHANFO) {
856 SILC_LOG_DEBUG(("CUMODE change is not allowed"));
862 /* Get target channel user entry */
863 if (!silc_server_client_on_channel(client2, channel, &chl))
866 if (server->server_type == SILC_SERVER && chl->mode == mode) {
867 SILC_LOG_DEBUG(("Mode is changed already"));
871 if (mode & SILC_CHANNEL_UMODE_CHANFO &&
872 !(chl->mode & SILC_CHANNEL_UMODE_CHANFO) &&
873 server->server_type == SILC_ROUTER &&
874 sock != SILC_PRIMARY_ROUTE(server)) {
875 SilcPublicKey founder_key = NULL;
877 /* If channel doesn't have founder auth mode then it's impossible
878 that someone would be getting founder rights with CUMODE command.
879 In that case there already either is founder or there isn't
880 founder at all on the channel. */
881 if (client && !(channel->mode & SILC_CHANNEL_MODE_FOUNDER_AUTH)) {
882 /* Force the mode to not have founder mode */
883 chl->mode = mode &= ~SILC_CHANNEL_UMODE_CHANFO;
884 silc_server_force_cumode_change(server, sock, channel, chl, mode);
889 /* Get the founder of the channel and if found then this client
890 cannot be the founder since there already is one. */
891 silc_hash_table_list(channel->user_list, &htl);
892 while (silc_hash_table_get(&htl, NULL, (void *)&chl2))
893 if (chl2->mode & SILC_CHANNEL_UMODE_CHANFO) {
894 /* If the founder on the channel is not the one whom has set
895 the founder mode, then it's possible that this CUMODE_CHANGE
896 is correct. Due to netsplits it's possible that this
897 situation happens. */
898 if (!(channel->mode & SILC_CHANNEL_MODE_FOUNDER_AUTH) ||
899 (channel->founder_key && chl2->client->data.public_key &&
900 silc_pkcs_public_key_compare(
901 channel->founder_key,
902 chl2->client->data.public_key))) {
903 chl->mode = mode &= ~SILC_CHANNEL_UMODE_CHANFO;
904 silc_server_force_cumode_change(server, sock, channel,
910 silc_hash_table_list_reset(&htl);
911 if (!(mode & SILC_CHANNEL_UMODE_CHANFO))
914 /* Founder not found of the channel. Since the founder auth mode
915 is set on the channel now check whether this is the client that
916 originally set the mode. */
918 /* Get public key that must be present in notify */
919 tmp = silc_argument_get_arg_type(args, 4, &tmp_len);
920 if (!tmp || !silc_pkcs_public_key_decode(tmp, tmp_len,
922 chl->mode = mode &= ~SILC_CHANNEL_UMODE_CHANFO;
923 silc_server_force_cumode_change(server, sock, channel, chl, mode);
928 if (channel->founder_key) {
929 /* Now match the public key we have cached and public key sent.
931 if (client && client->data.public_key &&
932 !silc_pkcs_public_key_compare(channel->founder_key,
933 client->data.public_key)) {
934 chl->mode = mode &= ~SILC_CHANNEL_UMODE_CHANFO;
935 silc_server_force_cumode_change(server, sock, channel, chl, mode);
939 if (!silc_pkcs_public_key_compare(channel->founder_key,
941 chl->mode = mode &= ~SILC_CHANNEL_UMODE_CHANFO;
942 silc_server_force_cumode_change(server, sock, channel, chl, mode);
948 /* There cannot be anyone else as founder on the channel now. This
949 client is definitely the founder due to this authentication */
950 silc_hash_table_list(channel->user_list, &htl);
951 while (silc_hash_table_get(&htl, NULL, (void *)&chl2))
952 if (chl2->mode & SILC_CHANNEL_UMODE_CHANFO) {
953 chl2->mode &= ~SILC_CHANNEL_UMODE_CHANFO;
954 silc_server_force_cumode_change(server, NULL, channel, chl2,
958 silc_hash_table_list_reset(&htl);
961 silc_pkcs_public_key_free(founder_key);
964 if (server->server_type != SILC_SERVER && chl->mode == mode) {
965 SILC_LOG_DEBUG(("Mode is changed already"));
969 SILC_LOG_DEBUG(("Changing %s channel user mode",
970 chl->client->nickname ? chl->client->nickname :
971 (unsigned char *)""));
973 /* Change the mode */
976 /* Send the same notify to the channel */
978 silc_server_packet_send_to_channel(server, NULL, channel,
980 FALSE, packet->buffer->data,
981 packet->buffer->len, FALSE);
983 silc_free(channel_id);
987 case SILC_NOTIFY_TYPE_INVITE:
989 if (packet->dst_id_type == SILC_ID_CLIENT)
992 SILC_LOG_DEBUG(("INVITE notify"));
995 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
998 channel_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
1002 /* Get channel entry */
1003 channel = silc_idlist_find_channel_by_id(server->global_list,
1006 channel = silc_idlist_find_channel_by_id(server->local_list,
1009 SILC_LOG_DEBUG(("Notify for unknown channel"));
1010 silc_free(channel_id);
1014 silc_free(channel_id);
1017 tmp = silc_argument_get_arg_type(args, 3, &tmp_len);
1020 client_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
1024 /* Get client entry */
1025 client = silc_idlist_find_client_by_id(server->global_list,
1026 client_id, TRUE, &cache);
1028 client = silc_idlist_find_client_by_id(server->local_list,
1029 client_id, TRUE, &cache);
1031 silc_free(client_id);
1035 silc_free(client_id);
1037 /* Get user's channel entry and check that inviting is allowed. */
1038 if (!silc_server_client_on_channel(client, channel, &chl))
1040 if (channel->mode & SILC_CHANNEL_MODE_INVITE &&
1041 !(chl->mode & SILC_CHANNEL_UMODE_CHANOP) &&
1042 !(chl->mode & SILC_CHANNEL_UMODE_CHANFO)) {
1043 SILC_LOG_DEBUG(("Inviting is not allowed"));
1047 /* Get the added invite */
1048 tmp = silc_argument_get_arg_type(args, 4, &tmp_len);
1050 if (!channel->invite_list)
1051 channel->invite_list = silc_calloc(tmp_len + 2,
1052 sizeof(*channel->invite_list));
1054 channel->invite_list = silc_realloc(channel->invite_list,
1055 sizeof(*channel->invite_list) *
1057 strlen(channel->invite_list) +
1059 if (tmp[tmp_len - 1] == ',')
1060 tmp[tmp_len - 1] = '\0';
1062 strncat(channel->invite_list, tmp, tmp_len);
1063 strncat(channel->invite_list, ",", 1);
1066 /* Get the deleted invite */
1067 tmp = silc_argument_get_arg_type(args, 5, &tmp_len);
1068 if (tmp && channel->invite_list) {
1069 char *start, *end, *n;
1071 if (!strncmp(channel->invite_list, tmp,
1072 strlen(channel->invite_list) - 1)) {
1073 silc_free(channel->invite_list);
1074 channel->invite_list = NULL;
1076 start = strstr(channel->invite_list, tmp);
1077 if (start && strlen(start) >= tmp_len) {
1078 end = start + tmp_len;
1079 n = silc_calloc(strlen(channel->invite_list) - tmp_len, sizeof(*n));
1080 strncat(n, channel->invite_list, start - channel->invite_list);
1081 strncat(n, end + 1, ((channel->invite_list +
1082 strlen(channel->invite_list)) - end) - 1);
1083 silc_free(channel->invite_list);
1084 channel->invite_list = n;
1091 case SILC_NOTIFY_TYPE_CHANNEL_CHANGE:
1093 * Distribute to the local clients on the channel and change the
1097 SILC_LOG_DEBUG(("CHANNEL CHANGE"));
1099 if (sock->type != SILC_SOCKET_TYPE_ROUTER)
1102 /* Get the old Channel ID */
1103 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
1106 channel_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
1110 /* Get the channel entry */
1111 channel = silc_idlist_find_channel_by_id(server->local_list,
1114 channel = silc_idlist_find_channel_by_id(server->global_list,
1117 SILC_LOG_DEBUG(("Notify for unknown channel"));
1118 silc_free(channel_id);
1123 /* Send the notify to the channel */
1124 silc_server_packet_send_to_channel(server, sock, channel, packet->type,
1125 FALSE, packet->buffer->data,
1126 packet->buffer->len, FALSE);
1128 /* Get the new Channel ID */
1129 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
1132 channel_id2 = silc_id_payload_parse_id(tmp, tmp_len, NULL);
1136 SILC_LOG_DEBUG(("Old Channel ID id(%s)",
1137 silc_id_render(channel_id, SILC_ID_CHANNEL)));
1138 SILC_LOG_DEBUG(("New Channel ID id(%s)",
1139 silc_id_render(channel_id2, SILC_ID_CHANNEL)));
1141 /* Replace the Channel ID */
1142 if (!silc_idlist_replace_channel_id(server->local_list, channel_id,
1144 if (!silc_idlist_replace_channel_id(server->global_list, channel_id,
1146 silc_free(channel_id2);
1151 SilcBuffer modes = NULL, users = NULL, users_modes = NULL;
1153 /* Re-announce this channel which ID was changed. */
1154 silc_server_send_new_channel(server, sock, FALSE, channel->channel_name,
1156 silc_id_get_len(channel->id,
1160 /* Re-announce our clients on the channel as the ID has changed now */
1161 silc_server_announce_get_channel_users(server, channel, &modes, &users,
1164 silc_buffer_push(modes, modes->data - modes->head);
1165 silc_server_packet_send_dest(server, sock,
1166 SILC_PACKET_NOTIFY, SILC_PACKET_FLAG_LIST,
1167 channel->id, SILC_ID_CHANNEL,
1168 modes->data, modes->len, FALSE);
1169 silc_buffer_free(modes);
1172 silc_buffer_push(users, users->data - users->head);
1173 silc_server_packet_send(server, sock,
1174 SILC_PACKET_NOTIFY, SILC_PACKET_FLAG_LIST,
1175 users->data, users->len, FALSE);
1176 silc_buffer_free(users);
1179 silc_buffer_push(users_modes, users_modes->data - users_modes->head);
1180 silc_server_packet_send_dest(server, sock,
1181 SILC_PACKET_NOTIFY, SILC_PACKET_FLAG_LIST,
1182 channel->id, SILC_ID_CHANNEL,
1184 users_modes->len, FALSE);
1185 silc_buffer_free(users_modes);
1188 /* Re-announce channel's topic */
1189 if (channel->topic) {
1190 silc_server_send_notify_topic_set(server, sock,
1191 server->server_type == SILC_ROUTER ?
1192 TRUE : FALSE, channel,
1193 server->id, SILC_ID_SERVER,
1198 silc_free(channel_id);
1202 case SILC_NOTIFY_TYPE_SERVER_SIGNOFF:
1204 * Remove the server entry and all clients that this server owns.
1207 SILC_LOG_DEBUG(("SERVER SIGNOFF notify"));
1210 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
1213 server_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
1217 /* Get server entry */
1218 server_entry = silc_idlist_find_server_by_id(server->global_list,
1219 server_id, TRUE, NULL);
1221 if (!server_entry) {
1222 server_entry = silc_idlist_find_server_by_id(server->local_list,
1223 server_id, TRUE, NULL);
1225 if (!server_entry) {
1226 /* If we are normal server then we might not have the server. Check
1227 whether router was kind enough to send the list of all clients
1228 that actually was to be removed. Remove them if the list is
1230 if (server->server_type != SILC_ROUTER &&
1231 silc_argument_get_arg_num(args) > 1) {
1234 for (i = 1; i < silc_argument_get_arg_num(args); i++) {
1236 tmp = silc_argument_get_arg_type(args, i + 1, &tmp_len);
1239 client_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
1243 /* Get client entry */
1244 client = silc_idlist_find_client_by_id(server->global_list,
1245 client_id, TRUE, &cache);
1248 client = silc_idlist_find_client_by_id(server->local_list,
1249 client_id, TRUE, &cache);
1252 silc_free(client_id);
1256 silc_free(client_id);
1258 /* Update statistics */
1259 server->stat.clients--;
1260 if (server->stat.cell_clients)
1261 server->stat.cell_clients--;
1262 SILC_OPER_STATS_UPDATE(client, server, SILC_UMODE_SERVER_OPERATOR);
1263 SILC_OPER_STATS_UPDATE(client, router, SILC_UMODE_ROUTER_OPERATOR);
1265 /* Remove the client from all channels. */
1266 silc_server_remove_from_channels(server, NULL, client,
1269 /* Check if anyone is watching this nickname */
1270 if (server->server_type == SILC_ROUTER)
1271 silc_server_check_watcher_list(server, client, NULL,
1272 SILC_NOTIFY_TYPE_SERVER_SIGNOFF);
1274 /* Remove this client from watcher list if it is */
1276 silc_server_del_from_watcher_list(server, client);
1278 /* Remove the client */
1279 silc_idlist_del_client(local ? server->local_list :
1280 server->global_list, client);
1284 silc_free(server_id);
1288 silc_free(server_id);
1290 /* Free all client entries that this server owns as they will
1291 become invalid now as well. */
1292 silc_server_remove_clients_by_server(server, server_entry, TRUE);
1294 /* Remove the server entry */
1295 silc_idlist_del_server(local ? server->local_list :
1296 server->global_list, server_entry);
1298 /* Update statistics */
1299 if (server->server_type == SILC_ROUTER)
1300 server->stat.servers--;
1304 case SILC_NOTIFY_TYPE_KICKED:
1306 * Distribute the notify to local clients on the channel
1309 SILC_LOG_DEBUG(("KICKED notify"));
1312 channel_id = silc_id_str2id(packet->dst_id, packet->dst_id_len,
1313 packet->dst_id_type);
1318 /* Get channel entry */
1319 channel = silc_idlist_find_channel_by_id(server->global_list,
1322 channel = silc_idlist_find_channel_by_id(server->local_list,
1325 SILC_LOG_DEBUG(("Notify for unknown channel"));
1326 silc_free(channel_id);
1330 silc_free(channel_id);
1333 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
1336 client_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
1340 /* If the the client is not in local list we check global list */
1341 client = silc_idlist_find_client_by_id(server->global_list,
1342 client_id, TRUE, NULL);
1344 client = silc_idlist_find_client_by_id(server->local_list,
1345 client_id, TRUE, NULL);
1347 silc_free(client_id);
1351 silc_free(client_id);
1353 /* If target is founder they cannot be kicked */
1354 if (!silc_server_client_on_channel(client, channel, &chl))
1356 if (chl->mode & SILC_CHANNEL_UMODE_CHANFO)
1359 /* From protocol version 1.1 we get the kicker's ID as well. */
1360 tmp = silc_argument_get_arg_type(args, 3, &tmp_len);
1362 client_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
1366 /* If the the client is not in local list we check global list */
1367 client2 = silc_idlist_find_client_by_id(server->global_list,
1368 client_id, TRUE, NULL);
1370 client2 = silc_idlist_find_client_by_id(server->local_list,
1371 client_id, TRUE, NULL);
1373 silc_free(client_id);
1377 silc_free(client_id);
1379 /* Kicker must be operator on channel */
1380 if (!silc_server_client_on_channel(client2, channel, &chl))
1382 if (!(chl->mode & SILC_CHANNEL_UMODE_CHANOP) &&
1383 !(chl->mode & SILC_CHANNEL_UMODE_CHANFO)) {
1384 SILC_LOG_DEBUG(("Kicking is not allowed"));
1389 /* Send to channel */
1390 silc_server_packet_send_to_channel(server, sock, channel, packet->type,
1391 FALSE, packet->buffer->data,
1392 packet->buffer->len, FALSE);
1394 /* Remove the client from channel */
1395 silc_server_remove_from_one_channel(server, sock, channel, client, FALSE);
1399 case SILC_NOTIFY_TYPE_KILLED:
1402 * Distribute the notify to local clients on channels
1404 unsigned char *id, *comment;
1405 SilcUInt32 id_len, comment_len;
1407 SILC_LOG_DEBUG(("KILLED notify"));
1410 id = silc_argument_get_arg_type(args, 1, &id_len);
1413 client_id = silc_id_payload_parse_id(id, id_len, NULL);
1417 /* If the the client is not in local list we check global list */
1418 client = silc_idlist_find_client_by_id(server->global_list,
1419 client_id, TRUE, NULL);
1421 client = silc_idlist_find_client_by_id(server->local_list,
1422 client_id, TRUE, NULL);
1424 silc_free(client_id);
1428 silc_free(client_id);
1430 /* If the client is one of ours, then close the connection to the
1431 client now. This removes the client from all channels as well. */
1432 if (packet->dst_id_type == SILC_ID_CLIENT && client->connection) {
1433 sock = client->connection;
1434 silc_server_free_client_data(server, NULL, client, FALSE, NULL);
1435 silc_server_close_connection(server, sock);
1440 comment = silc_argument_get_arg_type(args, 2, &comment_len);
1441 if (comment_len > 128)
1444 /* From protocol version 1.1 we get the killer's ID as well. */
1445 tmp = silc_argument_get_arg_type(args, 3, &tmp_len);
1447 client_id = silc_id_payload_parse_id(tmp, tmp_len, &id_type);
1451 if (id_type == SILC_ID_CLIENT) {
1452 /* If the the client is not in local list we check global list */
1453 client2 = silc_idlist_find_client_by_id(server->global_list,
1454 client_id, TRUE, NULL);
1456 client2 = silc_idlist_find_client_by_id(server->local_list,
1457 client_id, TRUE, NULL);
1459 silc_free(client_id);
1463 silc_free(client_id);
1465 /* Killer must be router operator */
1466 if (!(client2->mode & SILC_UMODE_ROUTER_OPERATOR)) {
1467 SILC_LOG_DEBUG(("Killing is not allowed"));
1473 /* Send the notify to local clients on the channels except to the
1474 client who is killed. */
1475 silc_server_send_notify_on_channels(server, client, client,
1476 SILC_NOTIFY_TYPE_KILLED, 3,
1477 id, id_len, comment, comment_len,
1480 /* Remove the client from all channels */
1481 silc_server_remove_from_channels(server, NULL, client, FALSE, NULL,
1484 /* Check if anyone is watching this nickname */
1485 if (server->server_type == SILC_ROUTER)
1486 silc_server_check_watcher_list(server, client, NULL,
1487 SILC_NOTIFY_TYPE_KILLED);
1492 case SILC_NOTIFY_TYPE_UMODE_CHANGE:
1494 * Save the mode of the client.
1497 SILC_LOG_DEBUG(("UMODE_CHANGE notify"));
1500 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
1503 client_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
1507 /* Get client entry */
1508 client = silc_idlist_find_client_by_id(server->global_list,
1509 client_id, TRUE, NULL);
1511 client = silc_idlist_find_client_by_id(server->local_list,
1512 client_id, TRUE, NULL);
1514 silc_free(client_id);
1518 silc_free(client_id);
1521 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
1524 SILC_GET32_MSB(mode, tmp);
1526 /* Check that mode changing is allowed. */
1527 if (!silc_server_check_umode_rights(server, client, mode)) {
1528 SILC_LOG_DEBUG(("UMODE change is not allowed"));
1532 /* Remove internal resumed flag if client is marked detached now */
1533 if (mode & SILC_UMODE_DETACHED)
1534 client->data.status &= ~SILC_IDLIST_STATUS_RESUMED;
1536 /* Update statistics */
1537 if (server->server_type == SILC_ROUTER) {
1538 if (mode & SILC_UMODE_GONE) {
1539 if (!(client->mode & SILC_UMODE_GONE))
1540 server->stat.aways++;
1542 if (client->mode & SILC_UMODE_GONE)
1543 server->stat.aways--;
1545 if (mode & SILC_UMODE_DETACHED) {
1546 if (!(client->mode & SILC_UMODE_DETACHED))
1547 server->stat.detached++;
1549 if (client->mode & SILC_UMODE_DETACHED)
1550 server->stat.detached--;
1554 /* Change the mode */
1555 client->mode = mode;
1557 /* Check if anyone is watching this nickname */
1558 if (server->server_type == SILC_ROUTER)
1559 silc_server_check_watcher_list(server, client, NULL,
1560 SILC_NOTIFY_TYPE_UMODE_CHANGE);
1564 case SILC_NOTIFY_TYPE_BAN:
1569 SILC_LOG_DEBUG(("BAN notify"));
1571 /* Get Channel ID */
1572 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
1575 channel_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
1579 /* Get channel entry */
1580 channel = silc_idlist_find_channel_by_id(server->global_list,
1583 channel = silc_idlist_find_channel_by_id(server->local_list,
1586 SILC_LOG_DEBUG(("Notify for unknown channel"));
1587 silc_free(channel_id);
1591 silc_free(channel_id);
1593 /* Get the new ban and add it to the ban list */
1594 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
1596 if (!channel->ban_list)
1597 channel->ban_list = silc_calloc(tmp_len + 2,
1598 sizeof(*channel->ban_list));
1600 channel->ban_list = silc_realloc(channel->ban_list,
1601 sizeof(*channel->ban_list) *
1603 strlen(channel->ban_list) + 2));
1604 strncat(channel->ban_list, tmp, tmp_len);
1605 strncat(channel->ban_list, ",", 1);
1608 /* Get the ban to be removed and remove it from the list */
1609 tmp = silc_argument_get_arg_type(args, 3, &tmp_len);
1610 if (tmp && channel->ban_list) {
1611 char *start, *end, *n;
1613 if (!strncmp(channel->ban_list, tmp, strlen(channel->ban_list) - 1)) {
1614 silc_free(channel->ban_list);
1615 channel->ban_list = NULL;
1617 start = strstr(channel->ban_list, tmp);
1618 if (start && strlen(start) >= tmp_len) {
1619 end = start + tmp_len;
1620 n = silc_calloc(strlen(channel->ban_list) - tmp_len, sizeof(*n));
1621 strncat(n, channel->ban_list, start - channel->ban_list);
1622 strncat(n, end + 1, ((channel->ban_list +
1623 strlen(channel->ban_list)) - end) - 1);
1624 silc_free(channel->ban_list);
1625 channel->ban_list = n;
1631 case SILC_NOTIFY_TYPE_ERROR:
1638 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
1639 if (!tmp && tmp_len != 1)
1641 error = (SilcStatus)tmp[0];
1643 SILC_LOG_DEBUG(("ERROR notify (%d)", error));
1645 if (error == SILC_STATUS_ERR_NO_SUCH_CLIENT_ID &&
1646 sock->type == SILC_SOCKET_TYPE_ROUTER) {
1647 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
1649 SILC_LOG_DEBUG(("Received invalid client ID notification, deleting "
1650 "the entry from cache"));
1651 client_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
1654 client = silc_idlist_find_client_by_id(server->global_list,
1655 client_id, FALSE, NULL);
1657 silc_server_remove_from_channels(server, NULL, client, TRUE,
1659 silc_idlist_del_client(server->global_list, client);
1661 silc_free(client_id);
1667 /* Ignore rest of the notify types for now */
1668 case SILC_NOTIFY_TYPE_NONE:
1669 case SILC_NOTIFY_TYPE_MOTD:
1676 silc_notify_payload_free(payload);
1679 void silc_server_notify_list(SilcServer server,
1680 SilcSocketConnection sock,
1681 SilcPacketContext *packet)
1683 SilcPacketContext *new;
1687 SILC_LOG_DEBUG(("Processing Notify List"));
1689 if (sock->type == SILC_SOCKET_TYPE_CLIENT ||
1690 packet->src_id_type != SILC_ID_SERVER)
1693 /* Make copy of the original packet context, except for the actual
1694 data buffer, which we will here now fetch from the original buffer. */
1695 new = silc_packet_context_alloc();
1696 new->type = SILC_PACKET_NOTIFY;
1697 new->flags = packet->flags;
1698 new->src_id = packet->src_id;
1699 new->src_id_len = packet->src_id_len;
1700 new->src_id_type = packet->src_id_type;
1701 new->dst_id = packet->dst_id;
1702 new->dst_id_len = packet->dst_id_len;
1703 new->dst_id_type = packet->dst_id_type;
1705 buffer = silc_buffer_alloc(1024);
1706 new->buffer = buffer;
1708 while (packet->buffer->len) {
1709 SILC_GET16_MSB(len, packet->buffer->data + 2);
1710 if (len > packet->buffer->len)
1713 if (len > buffer->truelen) {
1714 silc_buffer_free(buffer);
1715 buffer = silc_buffer_alloc(1024 + len);
1718 silc_buffer_pull_tail(buffer, len);
1719 silc_buffer_put(buffer, packet->buffer->data, len);
1721 /* Process the Notify */
1722 silc_server_notify(server, sock, new);
1724 silc_buffer_push_tail(buffer, len);
1725 silc_buffer_pull(packet->buffer, len);
1728 silc_buffer_free(buffer);
1732 /* Received private message. This resolves the destination of the message
1733 and sends the packet. This is used by both server and router. If the
1734 destination is our locally connected client this sends the packet to
1735 the client. This may also send the message for further routing if
1736 the destination is not in our server (or router). */
1738 void silc_server_private_message(SilcServer server,
1739 SilcSocketConnection sock,
1740 SilcPacketContext *packet)
1742 SilcSocketConnection dst_sock;
1743 SilcIDListData idata;
1744 SilcClientEntry client;
1746 SILC_LOG_DEBUG(("Start"));
1748 if (packet->src_id_type != SILC_ID_CLIENT ||
1749 packet->dst_id_type != SILC_ID_CLIENT || !packet->dst_id)
1752 /* Get the route to the client */
1753 dst_sock = silc_server_get_client_route(server, packet->dst_id,
1754 packet->dst_id_len, NULL,
1758 unsigned char error;
1760 if (client && client->mode & SILC_UMODE_DETACHED) {
1761 SILC_LOG_DEBUG(("Client is detached, discarding packet"));
1765 /* Send SILC_NOTIFY_TYPE_ERROR to indicate that such destination ID
1766 does not exist or is invalid. */
1767 idp = silc_id_payload_encode_data(packet->dst_id,
1769 packet->dst_id_type);
1773 error = SILC_STATUS_ERR_NO_SUCH_CLIENT_ID;
1774 if (packet->src_id_type == SILC_ID_CLIENT) {
1775 SilcClientID *client_id = silc_id_str2id(packet->src_id,
1777 packet->src_id_type);
1778 silc_server_send_notify_dest(server, sock, FALSE,
1779 client_id, SILC_ID_CLIENT,
1780 SILC_NOTIFY_TYPE_ERROR, 2,
1782 idp->data, idp->len);
1783 silc_free(client_id);
1785 silc_server_send_notify(server, sock, FALSE,
1786 SILC_NOTIFY_TYPE_ERROR, 2,
1788 idp->data, idp->len);
1791 silc_buffer_free(idp);
1795 /* Check whether destination client wishes to receive private messages */
1796 if (client && !(packet->flags & SILC_PACKET_FLAG_PRIVMSG_KEY) &&
1797 client->mode & SILC_UMODE_BLOCK_PRIVMSG) {
1798 SILC_LOG_DEBUG(("Client blocks private messages, discarding packet"));
1802 /* Send the private message */
1803 silc_server_send_private_message(server, dst_sock, idata->send_key,
1804 idata->hmac_send, idata->psn_send++,
1808 /* Received private message key packet.. This packet is never for us. It is to
1809 the client in the packet's destination ID. Sending of this sort of packet
1810 equals sending private message, ie. it is sent point to point from
1811 one client to another. */
1813 void silc_server_private_message_key(SilcServer server,
1814 SilcSocketConnection sock,
1815 SilcPacketContext *packet)
1817 SilcSocketConnection dst_sock;
1818 SilcIDListData idata;
1820 SILC_LOG_DEBUG(("Start"));
1822 if (packet->src_id_type != SILC_ID_CLIENT ||
1823 packet->dst_id_type != SILC_ID_CLIENT)
1826 if (!packet->dst_id)
1829 /* Get the route to the client */
1830 dst_sock = silc_server_get_client_route(server, packet->dst_id,
1831 packet->dst_id_len, NULL,
1836 /* Relay the packet */
1837 silc_server_relay_packet(server, dst_sock, idata->send_key,
1838 idata->hmac_send, idata->psn_send++, packet, FALSE);
1841 /* Processes incoming command reply packet. The command reply packet may
1842 be destined to one of our clients or it may directly for us. We will
1843 call the command reply routine after processing the packet. */
1845 void silc_server_command_reply(SilcServer server,
1846 SilcSocketConnection sock,
1847 SilcPacketContext *packet)
1849 SilcBuffer buffer = packet->buffer;
1850 SilcClientEntry client = NULL;
1851 SilcSocketConnection dst_sock;
1852 SilcIDListData idata;
1853 SilcClientID *id = NULL;
1855 SILC_LOG_DEBUG(("Start"));
1857 /* Source must be server or router */
1858 if (packet->src_id_type != SILC_ID_SERVER &&
1859 sock->type != SILC_SOCKET_TYPE_ROUTER)
1862 if (packet->dst_id_type == SILC_ID_CHANNEL)
1865 if (packet->dst_id_type == SILC_ID_CLIENT) {
1866 /* Destination must be one of ours */
1867 id = silc_id_str2id(packet->dst_id, packet->dst_id_len, SILC_ID_CLIENT);
1870 client = silc_idlist_find_client_by_id(server->local_list, id, TRUE, NULL);
1872 SILC_LOG_ERROR(("Cannot process command reply to unknown client"));
1878 if (packet->dst_id_type == SILC_ID_SERVER) {
1879 /* For now this must be for us */
1880 if (memcmp(packet->dst_id, server->id_string, server->id_string_len)) {
1881 SILC_LOG_ERROR(("Cannot process command reply to unknown server"));
1886 /* Execute command reply locally for the command */
1887 silc_server_command_reply_process(server, sock, buffer);
1889 if (packet->dst_id_type == SILC_ID_CLIENT && client && id) {
1890 /* Relay the packet to the client */
1891 const SilcBufferStruct p;
1893 dst_sock = (SilcSocketConnection)client->connection;
1894 idata = (SilcIDListData)client;
1896 silc_buffer_push(buffer, SILC_PACKET_HEADER_LEN + packet->src_id_len
1897 + packet->dst_id_len + packet->padlen);
1898 if (!silc_packet_send_prepare(dst_sock, 0, 0, buffer->len,
1899 idata->hmac_send, (const SilcBuffer)&p)) {
1900 SILC_LOG_ERROR(("Cannot send packet"));
1903 silc_buffer_put((SilcBuffer)&p, buffer->data, buffer->len);
1905 /* Encrypt packet */
1906 silc_packet_encrypt(idata->send_key, idata->hmac_send, idata->psn_send++,
1907 (SilcBuffer)&p, buffer->len);
1909 /* Send the packet */
1910 silc_server_packet_send_real(server, dst_sock, TRUE);
1916 /* Process received channel message. The message can be originated from
1917 client or server. */
1919 void silc_server_channel_message(SilcServer server,
1920 SilcSocketConnection sock,
1921 SilcPacketContext *packet)
1923 SilcChannelEntry channel = NULL;
1924 SilcChannelID *id = NULL;
1925 void *sender_id = NULL;
1926 SilcClientEntry sender_entry = NULL;
1927 SilcChannelClientEntry chl;
1930 SILC_LOG_DEBUG(("Processing channel message"));
1933 if (packet->dst_id_type != SILC_ID_CHANNEL) {
1934 SILC_LOG_DEBUG(("Received bad message for channel, dropped"));
1938 /* Find channel entry */
1939 id = silc_id_str2id(packet->dst_id, packet->dst_id_len, SILC_ID_CHANNEL);
1942 channel = silc_idlist_find_channel_by_id(server->local_list, id, NULL);
1944 channel = silc_idlist_find_channel_by_id(server->global_list, id, NULL);
1947 unsigned char error;
1949 /* Send SILC_NOTIFY_TYPE_ERROR to indicate that such destination ID
1950 does not exist or is invalid. */
1951 idp = silc_id_payload_encode_data(packet->dst_id,
1953 packet->dst_id_type);
1957 error = SILC_STATUS_ERR_NO_SUCH_CHANNEL_ID;
1958 if (packet->src_id_type == SILC_ID_CLIENT) {
1959 SilcClientID *client_id = silc_id_str2id(packet->src_id,
1961 packet->src_id_type);
1962 silc_server_send_notify_dest(server, sock, FALSE,
1963 client_id, SILC_ID_CLIENT,
1964 SILC_NOTIFY_TYPE_ERROR, 2,
1965 &error, 1, idp->data, idp->len);
1966 silc_free(client_id);
1968 silc_server_send_notify(server, sock, FALSE,
1969 SILC_NOTIFY_TYPE_ERROR, 2,
1970 &error, 1, idp->data, idp->len);
1973 silc_buffer_free(idp);
1978 /* See that this client is on the channel. If the original sender is
1979 not client (as it can be server as well) we don't do the check. */
1980 sender_id = silc_id_str2id(packet->src_id, packet->src_id_len,
1981 packet->src_id_type);
1984 if (packet->src_id_type == SILC_ID_CLIENT) {
1985 sender_entry = silc_idlist_find_client_by_id(server->local_list,
1986 sender_id, TRUE, NULL);
1987 if (!sender_entry) {
1989 sender_entry = silc_idlist_find_client_by_id(server->global_list,
1990 sender_id, TRUE, NULL);
1992 if (!sender_entry || !silc_server_client_on_channel(sender_entry,
1994 SILC_LOG_DEBUG(("Client not on channel"));
1998 /* If channel is moderated check that client is allowed to send
2000 if (channel->mode & SILC_CHANNEL_MODE_SILENCE_USERS &&
2001 !(chl->mode & SILC_CHANNEL_UMODE_CHANOP) &&
2002 !(chl->mode & SILC_CHANNEL_UMODE_CHANFO)) {
2003 SILC_LOG_DEBUG(("Channel is silenced from normal users"));
2006 if (channel->mode & SILC_CHANNEL_MODE_SILENCE_OPERS &&
2007 chl->mode & SILC_CHANNEL_UMODE_CHANOP &&
2008 !(chl->mode & SILC_CHANNEL_UMODE_CHANFO)) {
2009 SILC_LOG_DEBUG(("Channel is silenced from operators"));
2012 if (chl->mode & SILC_CHANNEL_UMODE_QUIET) {
2013 SILC_LOG_DEBUG(("Sender is quieted on the channel"));
2017 /* If the packet is coming from router, but the client entry is local
2018 entry to us then some router is rerouting this to us and it is not
2019 allowed. When the client is local to us it means that we've routed
2020 this packet to network, and now someone is routing it back to us. */
2021 if (server->server_type == SILC_ROUTER &&
2022 sock->type == SILC_SOCKET_TYPE_ROUTER && local) {
2023 SILC_LOG_DEBUG(("Channel message rerouted to the sender, drop it"));
2028 /* Distribute the packet to our local clients. This will send the
2029 packet for further routing as well, if needed. */
2030 silc_server_packet_relay_to_channel(server, sock, channel, sender_id,
2031 packet->src_id_type, sender_entry,
2032 packet->buffer->data,
2033 packet->buffer->len, FALSE);
2036 silc_free(sender_id);
2040 /* Received channel key packet. We distribute the key to all of our locally
2041 connected clients on the channel. */
2043 void silc_server_channel_key(SilcServer server,
2044 SilcSocketConnection sock,
2045 SilcPacketContext *packet)
2047 SilcBuffer buffer = packet->buffer;
2048 SilcChannelEntry channel;
2050 if (packet->src_id_type != SILC_ID_SERVER ||
2051 (server->server_type == SILC_ROUTER &&
2052 sock->type == SILC_SOCKET_TYPE_ROUTER))
2055 /* Save the channel key */
2056 channel = silc_server_save_channel_key(server, buffer, NULL);
2060 /* Distribute the key to everybody who is on the channel. If we are router
2061 we will also send it to locally connected servers. */
2062 silc_server_send_channel_key(server, sock, channel, FALSE);
2064 if (server->server_type != SILC_BACKUP_ROUTER) {
2065 /* Distribute to local cell backup routers. */
2066 silc_server_backup_send(server, (SilcServerEntry)sock->user_data,
2067 SILC_PACKET_CHANNEL_KEY, 0,
2068 buffer->data, buffer->len, FALSE, TRUE);
2072 /* Received New Client packet and processes it. Creates Client ID for the
2073 client. Client becomes registered after calling this functions. */
2075 SilcClientEntry silc_server_new_client(SilcServer server,
2076 SilcSocketConnection sock,
2077 SilcPacketContext *packet)
2079 SilcBuffer buffer = packet->buffer;
2080 SilcClientEntry client;
2081 SilcClientID *client_id;
2082 SilcIDListData idata;
2083 char *username = NULL, *realname = NULL;
2084 SilcUInt16 username_len;
2087 char *hostname, *nickname;
2090 SILC_LOG_DEBUG(("Creating new client"));
2092 if (sock->type != SILC_SOCKET_TYPE_CLIENT)
2095 /* Take client entry */
2096 client = (SilcClientEntry)sock->user_data;
2097 idata = (SilcIDListData)client;
2099 /* Remove the old cache entry. */
2100 if (!silc_idcache_del_by_context(server->local_list->clients, client)) {
2101 SILC_LOG_INFO(("Unauthenticated client attempted to register to network"));
2102 silc_server_disconnect_remote(server, sock,
2103 SILC_STATUS_ERR_NOT_AUTHENTICATED, NULL);
2104 if (sock->user_data)
2105 silc_server_free_sock_user_data(server, sock, NULL);
2109 /* Parse incoming packet */
2110 ret = silc_buffer_unformat(buffer,
2111 SILC_STR_UI16_NSTRING_ALLOC(&username,
2113 SILC_STR_UI16_STRING_ALLOC(&realname),
2116 silc_free(username);
2117 silc_free(realname);
2118 SILC_LOG_ERROR(("Client %s (%s) sent incomplete information, closing "
2119 "connection", sock->hostname, sock->ip));
2120 silc_server_disconnect_remote(server, sock,
2121 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
2123 if (sock->user_data)
2124 silc_server_free_sock_user_data(server, sock, NULL);
2129 silc_free(username);
2130 silc_free(realname);
2131 SILC_LOG_ERROR(("Client %s (%s) did not send its username, closing "
2132 "connection", sock->hostname, sock->ip));
2133 silc_server_disconnect_remote(server, sock,
2134 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
2136 if (sock->user_data)
2137 silc_server_free_sock_user_data(server, sock, NULL);
2141 if (username_len > 128)
2142 username[128] = '\0';
2144 /* Check for bad characters for nickname, and modify the nickname if
2145 it includes those. */
2146 if (silc_server_name_bad_chars(username, username_len)) {
2147 nickname = silc_server_name_modify_bad(username, username_len);
2149 nickname = strdup(username);
2152 /* Make sanity checks for the hostname of the client. If the hostname
2153 is provided in the `username' check that it is the same than the
2154 resolved hostname, or if not resolved the hostname that appears in
2155 the client's public key. If the hostname is not present then put
2156 it from the resolved name or from the public key. */
2157 if (strchr(username, '@')) {
2158 SilcPublicKeyIdentifier pident;
2159 int tlen = strcspn(username, "@");
2160 char *phostname = NULL;
2162 hostname = silc_memdup(username + tlen + 1, strlen(username) - tlen - 1);
2164 if (strcmp(sock->hostname, sock->ip) &&
2165 strcmp(sock->hostname, hostname)) {
2166 silc_free(username);
2167 silc_free(hostname);
2168 silc_free(realname);
2169 SILC_LOG_ERROR(("Client %s (%s) sent incomplete information, closing "
2170 "connection", sock->hostname, sock->ip));
2171 silc_server_disconnect_remote(server, sock,
2172 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
2174 if (sock->user_data)
2175 silc_server_free_sock_user_data(server, sock, NULL);
2179 pident = silc_pkcs_decode_identifier(client->data.public_key->identifier);
2181 phostname = strdup(pident->host);
2182 silc_pkcs_free_identifier(pident);
2185 if (!strcmp(sock->hostname, sock->ip) &&
2186 phostname && strcmp(phostname, hostname)) {
2187 silc_free(username);
2188 silc_free(hostname);
2189 silc_free(phostname);
2190 silc_free(realname);
2191 SILC_LOG_ERROR(("Client %s (%s) sent incomplete information, closing "
2192 "connection", sock->hostname, sock->ip));
2193 silc_server_disconnect_remote(server, sock,
2194 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
2196 if (sock->user_data)
2197 silc_server_free_sock_user_data(server, sock, NULL);
2201 silc_free(phostname);
2203 /* The hostname is not present, add it. */
2205 /* XXX For now we cannot take the host name from the public key since
2206 they are not trusted or we cannot verify them as trusted. Just take
2207 what the resolved name or address is. */
2209 if (strcmp(sock->hostname, sock->ip)) {
2211 newusername = silc_calloc(strlen(username) +
2212 strlen(sock->hostname) + 2,
2213 sizeof(*newusername));
2214 strncat(newusername, username, strlen(username));
2215 strncat(newusername, "@", 1);
2216 strncat(newusername, sock->hostname, strlen(sock->hostname));
2217 silc_free(username);
2218 username = newusername;
2221 SilcPublicKeyIdentifier pident =
2222 silc_pkcs_decode_identifier(client->data.public_key->identifier);
2225 newusername = silc_calloc(strlen(username) +
2226 strlen(pident->host) + 2,
2227 sizeof(*newusername));
2228 strncat(newusername, username, strlen(username));
2229 strncat(newusername, "@", 1);
2230 strncat(newusername, pident->host, strlen(pident->host));
2231 silc_free(username);
2232 username = newusername;
2233 silc_pkcs_free_identifier(pident);
2239 /* Create Client ID */
2240 while (!silc_id_create_client_id(server, server->id, server->rng,
2241 server->md5hash, nickname, &client_id)) {
2244 silc_server_disconnect_remote(server, sock,
2245 SILC_STATUS_ERR_BAD_NICKNAME, NULL);
2246 if (sock->user_data)
2247 silc_server_free_sock_user_data(server, sock, NULL);
2250 snprintf(&nickname[strlen(nickname) - 1], 1, "%d", nickfail);
2253 /* Update client entry */
2254 idata->status |= SILC_IDLIST_STATUS_REGISTERED;
2255 client->nickname = nickname;
2256 client->username = username;
2257 client->userinfo = realname ? realname : strdup(" ");
2258 client->id = client_id;
2259 id_len = silc_id_get_len(client_id, SILC_ID_CLIENT);
2261 /* Add the client again to the ID cache */
2262 silc_idcache_add(server->local_list->clients, client->nickname,
2263 client_id, client, 0, NULL);
2265 /* Notify our router about new client on the SILC network */
2266 silc_server_send_new_id(server, SILC_PRIMARY_ROUTE(server),
2267 SILC_BROADCAST(server), client->id,
2268 SILC_ID_CLIENT, id_len);
2270 /* Distribute to backup routers */
2271 if (server->server_type == SILC_ROUTER) {
2272 SilcBuffer idp = silc_id_payload_encode(client->id, SILC_ID_CLIENT);
2273 silc_server_backup_send(server, NULL, SILC_PACKET_NEW_ID, 0,
2274 idp->data, idp->len, FALSE, TRUE);
2275 silc_buffer_free(idp);
2278 /* Send the new client ID to the client. */
2279 silc_server_send_new_id(server, sock, FALSE, client->id, SILC_ID_CLIENT,
2280 silc_id_get_len(client->id, SILC_ID_CLIENT));
2282 /* Send some nice info to the client */
2283 silc_server_send_connect_notifys(server, sock, client);
2285 /* Check if anyone is watching this nickname */
2286 if (server->server_type == SILC_ROUTER)
2287 silc_server_check_watcher_list(server, client, NULL, 0);
2292 /* Create new server. This processes received New Server packet and
2293 saves the received Server ID. The server is our locally connected
2294 server thus we save all the information and save it to local list.
2295 This funtion can be used by both normal server and router server.
2296 If normal server uses this it means that its router has connected
2297 to the server. If router uses this it means that one of the cell's
2298 servers is connected to the router. */
2300 SilcServerEntry silc_server_new_server(SilcServer server,
2301 SilcSocketConnection sock,
2302 SilcPacketContext *packet)
2304 SilcBuffer buffer = packet->buffer;
2305 SilcServerEntry new_server, server_entry;
2306 SilcServerID *server_id;
2307 SilcIDListData idata;
2308 unsigned char *server_name, *id_string;
2309 SilcUInt16 id_len, name_len;
2313 SILC_LOG_DEBUG(("Creating new server"));
2315 if (sock->type != SILC_SOCKET_TYPE_SERVER &&
2316 sock->type != SILC_SOCKET_TYPE_ROUTER)
2319 /* Take server entry */
2320 new_server = (SilcServerEntry)sock->user_data;
2321 idata = (SilcIDListData)new_server;
2323 /* Remove the old cache entry */
2324 if (!silc_idcache_del_by_context(server->local_list->servers, new_server)) {
2325 if (!silc_idcache_del_by_context(server->global_list->servers,
2327 SILC_LOG_INFO(("Unauthenticated %s attempted to register to "
2328 "network", (sock->type == SILC_SOCKET_TYPE_SERVER ?
2329 "server" : "router")));
2330 silc_server_disconnect_remote(server, sock,
2331 SILC_STATUS_ERR_NOT_AUTHENTICATED, NULL);
2332 if (sock->user_data)
2333 silc_server_free_sock_user_data(server, sock, NULL);
2339 /* Parse the incoming packet */
2340 ret = silc_buffer_unformat(buffer,
2341 SILC_STR_UI16_NSTRING_ALLOC(&id_string, &id_len),
2342 SILC_STR_UI16_NSTRING_ALLOC(&server_name,
2346 silc_free(id_string);
2347 silc_free(server_name);
2348 silc_server_disconnect_remote(server, sock,
2349 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
2351 if (sock->user_data)
2352 silc_server_free_sock_user_data(server, sock, NULL);
2356 if (id_len > buffer->len) {
2357 silc_free(id_string);
2358 silc_free(server_name);
2359 silc_server_disconnect_remote(server, sock,
2360 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
2362 if (sock->user_data)
2363 silc_server_free_sock_user_data(server, sock, NULL);
2368 server_name[255] = '\0';
2371 server_id = silc_id_str2id(id_string, id_len, SILC_ID_SERVER);
2373 silc_free(id_string);
2374 silc_free(server_name);
2375 silc_server_disconnect_remote(server, sock,
2376 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
2378 if (sock->user_data)
2379 silc_server_free_sock_user_data(server, sock, NULL);
2382 silc_free(id_string);
2384 /* Check for valid server ID */
2385 if (!silc_id_is_valid_server_id(server, server_id, sock)) {
2386 SILC_LOG_INFO(("Invalid server ID sent by %s (%s)",
2387 sock->ip, sock->hostname));
2388 silc_server_disconnect_remote(server, sock,
2389 SILC_STATUS_ERR_BAD_SERVER_ID, NULL);
2390 if (sock->user_data)
2391 silc_server_free_sock_user_data(server, sock, NULL);
2392 silc_free(server_name);
2396 /* Check that we do not have this ID already */
2397 server_entry = silc_idlist_find_server_by_id(server->local_list,
2398 server_id, TRUE, NULL);
2400 silc_idcache_del_by_context(server->local_list->servers, server_entry);
2402 server_entry = silc_idlist_find_server_by_id(server->global_list,
2403 server_id, TRUE, NULL);
2405 silc_idcache_del_by_context(server->global_list->servers, server_entry);
2408 /* Update server entry */
2409 idata->status |= SILC_IDLIST_STATUS_REGISTERED;
2410 new_server->server_name = server_name;
2411 new_server->id = server_id;
2413 SILC_LOG_DEBUG(("New server id(%s)",
2414 silc_id_render(server_id, SILC_ID_SERVER)));
2416 /* Add again the entry to the ID cache. */
2417 silc_idcache_add(local ? server->local_list->servers :
2418 server->global_list->servers, server_name, server_id,
2419 new_server, 0, NULL);
2421 /* Distribute the information about new server in the SILC network
2422 to our router. If we are normal server we won't send anything
2423 since this connection must be our router connection. */
2424 if (server->server_type == SILC_ROUTER && !server->standalone &&
2425 SILC_PRIMARY_ROUTE(server) != sock)
2426 silc_server_send_new_id(server, SILC_PRIMARY_ROUTE(server),
2427 TRUE, new_server->id, SILC_ID_SERVER,
2428 silc_id_get_len(server_id, SILC_ID_SERVER));
2430 if (server->server_type == SILC_ROUTER) {
2431 /* Distribute to backup routers */
2432 SilcBuffer idp = silc_id_payload_encode(new_server->id, SILC_ID_SERVER);
2433 silc_server_backup_send(server, NULL, SILC_PACKET_NEW_ID, 0,
2434 idp->data, idp->len, FALSE, TRUE);
2435 silc_buffer_free(idp);
2438 server->stat.cell_servers++;
2441 /* Check whether this router connection has been replaced by an
2442 backup router. If it has been then we'll disable the server and will
2443 ignore everything it will send until the backup router resuming
2444 protocol has been completed. */
2445 if (sock->type == SILC_SOCKET_TYPE_ROUTER &&
2446 silc_server_backup_replaced_get(server, server_id, NULL)) {
2447 /* Send packet to the server indicating that it cannot use this
2448 connection as it has been replaced by backup router. */
2449 SilcBuffer packet = silc_buffer_alloc(2);
2450 silc_buffer_pull_tail(packet, SILC_BUFFER_END(packet));
2451 silc_buffer_format(packet,
2452 SILC_STR_UI_CHAR(SILC_SERVER_BACKUP_REPLACED),
2453 SILC_STR_UI_CHAR(0),
2455 silc_server_packet_send(server, sock,
2456 SILC_PACKET_RESUME_ROUTER, 0,
2457 packet->data, packet->len, TRUE);
2458 silc_buffer_free(packet);
2460 /* Mark the router disabled. The data sent earlier will go but nothing
2461 after this does not go to this connection. */
2462 idata->status |= SILC_IDLIST_STATUS_DISABLED;
2464 /* If it is router announce our stuff to it. */
2465 if (sock->type == SILC_SOCKET_TYPE_ROUTER &&
2466 server->server_type == SILC_ROUTER) {
2467 silc_server_announce_servers(server, FALSE, 0, sock);
2468 silc_server_announce_clients(server, 0, sock);
2469 silc_server_announce_channels(server, 0, sock);
2476 /* Processes incoming New ID packet. New ID Payload is used to distribute
2477 information about newly registered clients and servers. */
2479 static void silc_server_new_id_real(SilcServer server,
2480 SilcSocketConnection sock,
2481 SilcPacketContext *packet,
2484 SilcBuffer buffer = packet->buffer;
2486 SilcServerEntry router, server_entry;
2487 SilcSocketConnection router_sock;
2492 SILC_LOG_DEBUG(("Processing new ID"));
2494 if (sock->type == SILC_SOCKET_TYPE_CLIENT ||
2495 server->server_type == SILC_SERVER ||
2496 packet->src_id_type != SILC_ID_SERVER)
2499 idp = silc_id_payload_parse(buffer->data, buffer->len);
2503 id_type = silc_id_payload_get_type(idp);
2505 /* Normal server cannot have other normal server connections */
2506 server_entry = (SilcServerEntry)sock->user_data;
2507 if (id_type == SILC_ID_SERVER && sock->type == SILC_SOCKET_TYPE_SERVER &&
2508 server_entry->server_type == SILC_SERVER)
2511 id = silc_id_payload_get_id(idp);
2515 /* If the packet is coming from server then use the sender as the
2516 origin of the the packet. If it came from router then check the real
2517 sender of the packet and use that as the origin. */
2518 if (sock->type == SILC_SOCKET_TYPE_SERVER) {
2519 id_list = server->local_list;
2521 router = sock->user_data;
2523 /* If the sender is backup router and ID is server (and we are not
2524 backup router) then switch the entry to global list. */
2525 if (server_entry->server_type == SILC_BACKUP_ROUTER &&
2526 id_type == SILC_ID_SERVER &&
2527 server->id_entry->server_type != SILC_BACKUP_ROUTER) {
2528 id_list = server->global_list;
2529 router_sock = server->router ? SILC_PRIMARY_ROUTE(server) : sock;
2532 void *sender_id = silc_id_str2id(packet->src_id, packet->src_id_len,
2533 packet->src_id_type);
2534 router = silc_idlist_find_server_by_id(server->global_list,
2535 sender_id, TRUE, NULL);
2537 router = silc_idlist_find_server_by_id(server->local_list,
2538 sender_id, TRUE, NULL);
2539 silc_free(sender_id);
2541 id_list = server->global_list;
2548 case SILC_ID_CLIENT:
2550 SilcClientEntry entry;
2552 /* Check that we do not have this client already */
2553 entry = silc_idlist_find_client_by_id(server->global_list,
2554 id, server->server_type,
2557 entry = silc_idlist_find_client_by_id(server->local_list,
2558 id, server->server_type,
2561 SILC_LOG_DEBUG(("Ignoring client that we already have"));
2565 SILC_LOG_DEBUG(("New client id(%s) from [%s] %s",
2566 silc_id_render(id, SILC_ID_CLIENT),
2567 sock->type == SILC_SOCKET_TYPE_SERVER ?
2568 "Server" : "Router", sock->hostname));
2570 /* As a router we keep information of all global information in our
2571 global list. Cell wide information however is kept in the local
2573 entry = silc_idlist_add_client(id_list, NULL, NULL, NULL,
2574 id, router, NULL, 0);
2576 SILC_LOG_ERROR(("Could not add new client to the ID Cache"));
2578 /* Inform the sender that the ID is not usable */
2579 silc_server_send_notify_signoff(server, sock, FALSE, id, NULL);
2582 entry->nickname = NULL;
2583 entry->data.status |= SILC_IDLIST_STATUS_REGISTERED;
2585 if (sock->type == SILC_SOCKET_TYPE_SERVER)
2586 server->stat.cell_clients++;
2587 server->stat.clients++;
2589 /* Check if anyone is watching this nickname */
2590 if (server->server_type == SILC_ROUTER && id_list == server->local_list)
2591 silc_server_check_watcher_list(server, entry, NULL, 0);
2595 case SILC_ID_SERVER:
2597 SilcServerEntry entry;
2599 /* If the ID is mine, ignore it. */
2600 if (SILC_ID_SERVER_COMPARE(id, server->id)) {
2601 SILC_LOG_DEBUG(("Ignoring my own ID as new ID"));
2605 /* If the ID is the sender's ID, ignore it (we have it already) */
2606 if (SILC_ID_SERVER_COMPARE(id, router->id)) {
2607 SILC_LOG_DEBUG(("Ignoring sender's own ID"));
2611 /* Check that we do not have this server already */
2612 entry = silc_idlist_find_server_by_id(server->global_list,
2613 id, server->server_type,
2616 entry = silc_idlist_find_server_by_id(server->local_list,
2617 id, server->server_type,
2620 SILC_LOG_DEBUG(("Ignoring server that we already have"));
2624 SILC_LOG_DEBUG(("New server id(%s) from [%s] %s",
2625 silc_id_render(id, SILC_ID_SERVER),
2626 sock->type == SILC_SOCKET_TYPE_SERVER ?
2627 "Server" : "Router", sock->hostname));
2629 /* As a router we keep information of all global information in our
2630 global list. Cell wide information however is kept in the local
2632 entry = silc_idlist_add_server(id_list, NULL, 0, id, router,
2635 SILC_LOG_ERROR(("Could not add new server to the ID Cache"));
2638 entry->data.status |= SILC_IDLIST_STATUS_REGISTERED;
2640 if (sock->type == SILC_SOCKET_TYPE_SERVER)
2641 server->stat.cell_servers++;
2642 server->stat.servers++;
2646 case SILC_ID_CHANNEL:
2647 SILC_LOG_ERROR(("Channel cannot be registered with NEW_ID packet"));
2656 /* If the sender of this packet is server and we are router we need to
2657 broadcast this packet to other routers in the network. */
2658 if (broadcast && server->server_type == SILC_ROUTER &&
2659 sock->type == SILC_SOCKET_TYPE_SERVER &&
2660 !(packet->flags & SILC_PACKET_FLAG_BROADCAST)) {
2661 SILC_LOG_DEBUG(("Broadcasting received New ID packet"));
2662 silc_server_packet_send(server, SILC_PRIMARY_ROUTE(server),
2664 packet->flags | SILC_PACKET_FLAG_BROADCAST,
2665 buffer->data, buffer->len, FALSE);
2666 silc_server_backup_send(server, (SilcServerEntry)sock->user_data,
2667 packet->type, packet->flags,
2668 packet->buffer->data, packet->buffer->len,
2673 silc_id_payload_free(idp);
2677 /* Processes incoming New ID packet. New ID Payload is used to distribute
2678 information about newly registered clients and servers. */
2680 void silc_server_new_id(SilcServer server, SilcSocketConnection sock,
2681 SilcPacketContext *packet)
2683 silc_server_new_id_real(server, sock, packet, TRUE);
2686 /* Receoved New Id List packet, list of New ID payloads inside one
2687 packet. Process the New ID payloads one by one. */
2689 void silc_server_new_id_list(SilcServer server, SilcSocketConnection sock,
2690 SilcPacketContext *packet)
2692 SilcPacketContext *new_id;
2696 SILC_LOG_DEBUG(("Processing New ID List"));
2698 if (sock->type == SILC_SOCKET_TYPE_CLIENT ||
2699 packet->src_id_type != SILC_ID_SERVER)
2702 /* If the sender of this packet is server and we are router we need to
2703 broadcast this packet to other routers in the network. Broadcast
2704 this list packet instead of multiple New ID packets. */
2705 if (server->server_type == SILC_ROUTER &&
2706 sock->type == SILC_SOCKET_TYPE_SERVER &&
2707 !(packet->flags & SILC_PACKET_FLAG_BROADCAST)) {
2708 SILC_LOG_DEBUG(("Broadcasting received New ID List packet"));
2709 silc_server_packet_send(server, SILC_PRIMARY_ROUTE(server),
2711 packet->flags | SILC_PACKET_FLAG_BROADCAST,
2712 packet->buffer->data,
2713 packet->buffer->len, FALSE);
2714 silc_server_backup_send(server, (SilcServerEntry)sock->user_data,
2715 packet->type, packet->flags,
2716 packet->buffer->data, packet->buffer->len,
2720 /* Make copy of the original packet context, except for the actual
2721 data buffer, which we will here now fetch from the original buffer. */
2722 new_id = silc_packet_context_alloc();
2723 new_id->type = SILC_PACKET_NEW_ID;
2724 new_id->flags = packet->flags;
2725 new_id->src_id = packet->src_id;
2726 new_id->src_id_len = packet->src_id_len;
2727 new_id->src_id_type = packet->src_id_type;
2728 new_id->dst_id = packet->dst_id;
2729 new_id->dst_id_len = packet->dst_id_len;
2730 new_id->dst_id_type = packet->dst_id_type;
2732 idp = silc_buffer_alloc(256);
2733 new_id->buffer = idp;
2735 while (packet->buffer->len) {
2736 SILC_GET16_MSB(id_len, packet->buffer->data + 2);
2737 if ((id_len > packet->buffer->len) ||
2738 (id_len > idp->truelen))
2741 silc_buffer_pull_tail(idp, 4 + id_len);
2742 silc_buffer_put(idp, packet->buffer->data, 4 + id_len);
2744 /* Process the New ID */
2745 silc_server_new_id_real(server, sock, new_id, FALSE);
2747 silc_buffer_push_tail(idp, 4 + id_len);
2748 silc_buffer_pull(packet->buffer, 4 + id_len);
2751 silc_buffer_free(idp);
2755 /* Received New Channel packet. Information about new channels in the
2756 network are distributed using this packet. Save the information about
2757 the new channel. This usually comes from router but also normal server
2758 can send this to notify channels it has when it connects to us. */
2760 void silc_server_new_channel(SilcServer server,
2761 SilcSocketConnection sock,
2762 SilcPacketContext *packet)
2764 SilcChannelPayload payload;
2765 SilcChannelID *channel_id;
2767 SilcUInt32 name_len;
2770 SilcServerEntry server_entry;
2771 SilcChannelEntry channel;
2773 if (sock->type == SILC_SOCKET_TYPE_CLIENT ||
2774 packet->src_id_type != SILC_ID_SERVER ||
2775 server->server_type == SILC_SERVER)
2778 /* Parse the channel payload */
2779 payload = silc_channel_payload_parse(packet->buffer->data,
2780 packet->buffer->len);
2784 /* Get the channel ID */
2785 channel_id = silc_channel_get_id_parse(payload);
2787 silc_channel_payload_free(payload);
2791 channel_name = silc_channel_get_name(payload, &name_len);
2793 channel_name[255] = '\0';
2795 id = silc_channel_get_id(payload, &id_len);
2797 server_entry = (SilcServerEntry)sock->user_data;
2799 if (sock->type == SILC_SOCKET_TYPE_ROUTER) {
2800 /* Add the channel to global list as it is coming from router. It
2801 cannot be our own channel as it is coming from router. */
2803 /* Check that we don't already have this channel */
2804 channel = silc_idlist_find_channel_by_name(server->local_list,
2805 channel_name, NULL);
2807 channel = silc_idlist_find_channel_by_name(server->global_list,
2808 channel_name, NULL);
2810 SILC_LOG_DEBUG(("New channel id(%s) from [Router] %s",
2811 silc_id_render(channel_id, SILC_ID_CHANNEL),
2815 silc_idlist_add_channel(server->global_list, strdup(channel_name),
2816 0, channel_id, sock->user_data, NULL, NULL, 0);
2819 channel->disabled = TRUE;
2821 server->stat.channels++;
2822 if (server->server_type == SILC_ROUTER)
2823 channel->users_resolved = TRUE;
2826 /* The channel is coming from our server, thus it is in our cell
2827 we will add it to our local list. */
2830 SILC_LOG_DEBUG(("Channel id(%s) from [Server] %s",
2831 silc_id_render(channel_id, SILC_ID_CHANNEL),
2834 /* Check that we don't already have this channel */
2835 channel = silc_idlist_find_channel_by_name(server->local_list,
2836 channel_name, NULL);
2838 channel = silc_idlist_find_channel_by_name(server->global_list,
2839 channel_name, NULL);
2841 /* If the channel does not exist, then create it. This creates a new
2842 key to the channel as well that we will send to the server. */
2844 SILC_LOG_DEBUG(("Channel is new to us"));
2846 /* The protocol says that the Channel ID's IP address must be based
2847 on the router's IP address. Check whether the ID is based in our
2848 IP and if it is not then create a new ID and enforce the server
2849 to switch the ID. */
2850 if (server_entry->server_type != SILC_BACKUP_ROUTER &&
2851 !SILC_ID_COMPARE(channel_id, server->id, server->id->ip.data_len)) {
2853 SILC_LOG_DEBUG(("Forcing the server to change Channel ID"));
2855 if (silc_id_create_channel_id(server, server->id, server->rng, &tmp)) {
2856 silc_server_send_notify_channel_change(server, sock, FALSE,
2858 silc_free(channel_id);
2863 /* Create the channel with the provided Channel ID */
2864 channel = silc_server_create_new_channel_with_id(server, NULL, NULL,
2868 silc_channel_payload_free(payload);
2869 silc_free(channel_id);
2872 channel->disabled = TRUE;
2873 channel->mode = silc_channel_get_mode(payload);
2875 /* Send the new channel key to the server */
2876 id = silc_id_id2str(channel->id, SILC_ID_CHANNEL);
2877 id_len = silc_id_get_len(channel->id, SILC_ID_CHANNEL);
2878 chk = silc_channel_key_payload_encode(id_len, id,
2879 strlen(channel->channel_key->
2881 channel->channel_key->cipher->name,
2882 channel->key_len / 8,
2884 silc_server_packet_send(server, sock, SILC_PACKET_CHANNEL_KEY, 0,
2885 chk->data, chk->len, FALSE);
2886 silc_buffer_free(chk);
2888 /* The channel exist by that name, check whether the ID's match.
2889 If they don't then we'll force the server to use the ID we have.
2890 We also create a new key for the channel. */
2891 SilcBuffer modes = NULL, users = NULL, users_modes = NULL;
2893 SILC_LOG_DEBUG(("Channel already exists"));
2895 if (!SILC_ID_CHANNEL_COMPARE(channel_id, channel->id)) {
2896 /* They don't match, send CHANNEL_CHANGE notify to the server to
2897 force the ID change. */
2898 SILC_LOG_DEBUG(("Forcing the server to change Channel ID"));
2899 silc_server_send_notify_channel_change(server, sock, FALSE,
2900 channel_id, channel->id);
2902 /* Wait that server re-announces this channel */
2906 #if 0 /* We will announce our CMODE anyway for this channel, so no need
2907 to check it (implicit enforce). */
2909 /* If the mode is different from what we have then enforce the
2911 mode = silc_channel_get_mode(payload);
2912 if (channel->mode != mode) {
2913 SILC_LOG_DEBUG(("Forcing the server to change channel mode"));
2914 silc_server_send_notify_cmode(server, sock, FALSE, channel,
2915 channel->mode, server->id,
2916 SILC_ID_SERVER, channel->cipher,
2918 channel->passphrase,
2919 channel->founder_key);
2923 /* Create new key for the channel and send it to the server and
2924 everybody else possibly on the channel. */
2925 if (!(channel->mode & SILC_CHANNEL_MODE_PRIVKEY)) {
2926 if (!silc_server_create_channel_key(server, channel, 0))
2929 /* Send to the channel */
2930 silc_server_send_channel_key(server, sock, channel, FALSE);
2931 id = silc_id_id2str(channel->id, SILC_ID_CHANNEL);
2932 id_len = silc_id_get_len(channel->id, SILC_ID_CHANNEL);
2934 /* Send to the server */
2935 chk = silc_channel_key_payload_encode(id_len, id,
2936 strlen(channel->channel_key->
2938 channel->channel_key->
2940 channel->key_len / 8,
2942 silc_server_packet_send(server, sock, SILC_PACKET_CHANNEL_KEY, 0,
2943 chk->data, chk->len, FALSE);
2944 silc_buffer_free(chk);
2948 silc_free(channel_id);
2950 /* Update statistics */
2951 server->stat.channels++;
2952 server->stat.cell_channels++;
2954 /* Since the channel is coming from server and we also know about it
2955 then send the JOIN notify to the server so that it see's our
2956 users on the channel "joining" the channel. */
2957 silc_server_announce_get_channel_users(server, channel, &modes, &users,
2960 silc_buffer_push(modes, modes->data - modes->head);
2961 silc_server_packet_send_dest(server, sock,
2962 SILC_PACKET_NOTIFY, SILC_PACKET_FLAG_LIST,
2963 channel->id, SILC_ID_CHANNEL,
2964 modes->data, modes->len, FALSE);
2965 silc_buffer_free(modes);
2968 silc_buffer_push(users, users->data - users->head);
2969 silc_server_packet_send(server, sock,
2970 SILC_PACKET_NOTIFY, SILC_PACKET_FLAG_LIST,
2971 users->data, users->len, FALSE);
2972 silc_buffer_free(users);
2975 silc_buffer_push(users_modes, users_modes->data - users_modes->head);
2976 silc_server_packet_send_dest(server, sock,
2977 SILC_PACKET_NOTIFY, SILC_PACKET_FLAG_LIST,
2978 channel->id, SILC_ID_CHANNEL,
2980 users_modes->len, FALSE);
2981 silc_buffer_free(users_modes);
2983 if (channel->topic) {
2984 silc_server_send_notify_topic_set(server, sock,
2985 server->server_type == SILC_ROUTER ?
2986 TRUE : FALSE, channel,
2987 server->id, SILC_ID_SERVER,
2993 silc_channel_payload_free(payload);
2996 /* Received New Channel List packet, list of New Channel List payloads inside
2997 one packet. Process the New Channel payloads one by one. */
2999 void silc_server_new_channel_list(SilcServer server,
3000 SilcSocketConnection sock,
3001 SilcPacketContext *packet)
3003 SilcPacketContext *new;
3005 SilcUInt16 len1, len2;
3007 SILC_LOG_DEBUG(("Processing New Channel List"));
3009 if (sock->type == SILC_SOCKET_TYPE_CLIENT ||
3010 packet->src_id_type != SILC_ID_SERVER ||
3011 server->server_type == SILC_SERVER)
3014 /* If the sender of this packet is server and we are router we need to
3015 broadcast this packet to other routers in the network. Broadcast
3016 this list packet instead of multiple New Channel packets. */
3017 if (server->server_type == SILC_ROUTER &&
3018 sock->type == SILC_SOCKET_TYPE_SERVER &&
3019 !(packet->flags & SILC_PACKET_FLAG_BROADCAST)) {
3020 SILC_LOG_DEBUG(("Broadcasting received New Channel List packet"));
3021 silc_server_packet_send(server, SILC_PRIMARY_ROUTE(server),
3023 packet->flags | SILC_PACKET_FLAG_BROADCAST,
3024 packet->buffer->data,
3025 packet->buffer->len, FALSE);
3026 silc_server_backup_send(server, (SilcServerEntry)sock->user_data,
3027 packet->type, packet->flags,
3028 packet->buffer->data, packet->buffer->len,
3032 /* Make copy of the original packet context, except for the actual
3033 data buffer, which we will here now fetch from the original buffer. */
3034 new = silc_packet_context_alloc();
3035 new->type = SILC_PACKET_NEW_CHANNEL;
3036 new->flags = packet->flags;
3037 new->src_id = packet->src_id;
3038 new->src_id_len = packet->src_id_len;
3039 new->src_id_type = packet->src_id_type;
3040 new->dst_id = packet->dst_id;
3041 new->dst_id_len = packet->dst_id_len;
3042 new->dst_id_type = packet->dst_id_type;
3044 buffer = silc_buffer_alloc(512);
3045 new->buffer = buffer;
3047 while (packet->buffer->len) {
3048 SILC_GET16_MSB(len1, packet->buffer->data);
3049 if ((len1 > packet->buffer->len) ||
3050 (len1 > buffer->truelen))
3053 SILC_GET16_MSB(len2, packet->buffer->data + 2 + len1);
3054 if ((len2 > packet->buffer->len) ||
3055 (len2 > buffer->truelen))
3058 silc_buffer_pull_tail(buffer, 8 + len1 + len2);
3059 silc_buffer_put(buffer, packet->buffer->data, 8 + len1 + len2);
3061 /* Process the New Channel */
3062 silc_server_new_channel(server, sock, new);
3064 silc_buffer_push_tail(buffer, 8 + len1 + len2);
3065 silc_buffer_pull(packet->buffer, 8 + len1 + len2);
3068 silc_buffer_free(buffer);
3072 /* Received key agreement packet. This packet is never for us. It is to
3073 the client in the packet's destination ID. Sending of this sort of packet
3074 equals sending private message, ie. it is sent point to point from
3075 one client to another. */
3077 void silc_server_key_agreement(SilcServer server,
3078 SilcSocketConnection sock,
3079 SilcPacketContext *packet)
3081 SilcSocketConnection dst_sock;
3082 SilcIDListData idata;
3084 SILC_LOG_DEBUG(("Start"));
3086 if (packet->src_id_type != SILC_ID_CLIENT ||
3087 packet->dst_id_type != SILC_ID_CLIENT)
3090 if (!packet->dst_id)
3093 /* Get the route to the client */
3094 dst_sock = silc_server_get_client_route(server, packet->dst_id,
3095 packet->dst_id_len, NULL,
3100 /* Relay the packet */
3101 silc_server_relay_packet(server, dst_sock, idata->send_key,
3102 idata->hmac_send, idata->psn_send++,
3106 /* Received connection auth request packet that is used during connection
3107 phase to resolve the mandatory authentication method. This packet can
3108 actually be received at anytime but usually it is used only during
3109 the connection authentication phase. Now, protocol says that this packet
3110 can come from client or server, however, we support only this coming
3111 from client and expect that server always knows what authentication
3114 void silc_server_connection_auth_request(SilcServer server,
3115 SilcSocketConnection sock,
3116 SilcPacketContext *packet)
3118 SilcServerConfigClient *client = NULL;
3119 SilcUInt16 conn_type;
3121 SilcAuthMethod auth_meth = SILC_AUTH_NONE;
3123 if (packet->src_id_type && packet->src_id_type != SILC_ID_CLIENT) {
3124 SILC_LOG_DEBUG(("Request not from client"));
3128 /* Parse the payload */
3129 ret = silc_buffer_unformat(packet->buffer,
3130 SILC_STR_UI_SHORT(&conn_type),
3131 SILC_STR_UI_SHORT(NULL),
3136 if (conn_type != SILC_SOCKET_TYPE_CLIENT)
3139 /* Get the authentication method for the client */
3140 auth_meth = SILC_AUTH_NONE;
3141 client = silc_server_config_find_client(server, sock->ip);
3143 client = silc_server_config_find_client(server, sock->hostname);
3145 if (client->passphrase) {
3146 if (client->publickeys && !server->config->prefer_passphrase_auth)
3147 auth_meth = SILC_AUTH_PUBLIC_KEY;
3149 auth_meth = SILC_AUTH_PASSWORD;
3150 } else if (client->publickeys)
3151 auth_meth = SILC_AUTH_PUBLIC_KEY;
3154 SILC_LOG_DEBUG(("Authentication method is [%s]",
3155 (auth_meth == SILC_AUTH_NONE ? "None" :
3156 auth_meth == SILC_AUTH_PASSWORD ? "Passphrase" :
3157 "Digital signatures")));
3159 /* Send it back to the client */
3160 silc_server_send_connection_auth_request(server, sock, conn_type, auth_meth);
3163 /* Received REKEY packet. The sender of the packet wants to regenerate
3164 its session keys. This starts the REKEY protocol. */
3166 void silc_server_rekey(SilcServer server,
3167 SilcSocketConnection sock,
3168 SilcPacketContext *packet)
3170 SilcProtocol protocol;
3171 SilcServerRekeyInternalContext *proto_ctx;
3172 SilcIDListData idata = (SilcIDListData)sock->user_data;
3174 SILC_LOG_DEBUG(("Start"));
3176 /* Allocate internal protocol context. This is sent as context
3178 proto_ctx = silc_calloc(1, sizeof(*proto_ctx));
3179 proto_ctx->server = (void *)server;
3180 proto_ctx->sock = sock;
3181 proto_ctx->responder = TRUE;
3182 proto_ctx->pfs = idata->rekey->pfs;
3184 /* Perform rekey protocol. Will call the final callback after the
3185 protocol is over. */
3186 silc_protocol_alloc(SILC_PROTOCOL_SERVER_REKEY,
3187 &protocol, proto_ctx, silc_server_rekey_final);
3188 sock->protocol = protocol;
3190 if (proto_ctx->pfs == FALSE)
3191 /* Run the protocol */
3192 silc_protocol_execute(protocol, server->schedule, 0, 0);
3195 /* Received file transger packet. This packet is never for us. It is to
3196 the client in the packet's destination ID. Sending of this sort of packet
3197 equals sending private message, ie. it is sent point to point from
3198 one client to another. */
3200 void silc_server_ftp(SilcServer server,
3201 SilcSocketConnection sock,
3202 SilcPacketContext *packet)
3204 SilcSocketConnection dst_sock;
3205 SilcIDListData idata;
3207 SILC_LOG_DEBUG(("Start"));
3209 if (packet->src_id_type != SILC_ID_CLIENT ||
3210 packet->dst_id_type != SILC_ID_CLIENT)
3213 if (!packet->dst_id)
3216 /* Get the route to the client */
3217 dst_sock = silc_server_get_client_route(server, packet->dst_id,
3218 packet->dst_id_len, NULL,
3223 /* Relay the packet */
3224 silc_server_relay_packet(server, dst_sock, idata->send_key,
3225 idata->hmac_send, idata->psn_send++,
3231 SilcSocketConnection sock;
3232 SilcPacketContext *packet;
3234 } *SilcServerResumeResolve;
3236 SILC_SERVER_CMD_FUNC(resume_resolve)
3238 SilcServerResumeResolve r = (SilcServerResumeResolve)context;
3239 SilcServer server = r->server;
3240 SilcSocketConnection sock = r->sock;
3241 SilcServerCommandReplyContext reply = context2;
3242 SilcClientEntry client;
3244 SILC_LOG_DEBUG(("Start"));
3246 if (!reply || !silc_command_get_status(reply->payload, NULL, NULL)) {
3247 SILC_LOG_ERROR(("Client %s (%s) tried to resume unknown client, "
3248 "closing connection", sock->hostname, sock->ip));
3249 silc_server_disconnect_remote(server, sock,
3250 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
3251 "Resuming not possible");
3252 if (sock->user_data)
3253 silc_server_free_sock_user_data(server, sock, NULL);
3257 if (reply && silc_command_get(reply->payload) == SILC_COMMAND_WHOIS) {
3258 /* Get entry to the client, and resolve it if we don't have it. */
3259 client = silc_idlist_find_client_by_id(server->local_list,
3260 r->data, TRUE, NULL);
3262 client = silc_idlist_find_client_by_id(server->global_list,
3263 r->data, TRUE, NULL);
3265 SILC_LOG_ERROR(("Client %s (%s) tried to resume unknown client, "
3266 "closing connection", sock->hostname, sock->ip));
3267 silc_server_disconnect_remote(server, sock,
3268 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
3269 "Resuming not possible");
3270 if (sock->user_data)
3271 silc_server_free_sock_user_data(server, sock, NULL);
3276 if (!(client->mode & SILC_UMODE_DETACHED)) {
3277 SILC_LOG_ERROR(("Client %s (%s) tried to resume un-detached client, "
3278 "closing connection", sock->hostname, sock->ip));
3279 silc_server_disconnect_remote(server, sock,
3280 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
3281 "Resuming not possible");
3282 if (sock->user_data)
3283 silc_server_free_sock_user_data(server, sock, NULL);
3288 /* Reprocess the packet */
3289 silc_server_resume_client(server, sock, r->packet);
3292 silc_socket_free(r->sock);
3293 silc_packet_context_free(r->packet);
3298 /* Received client resuming packet. This is used to resume detached
3299 client session. It can be sent by the client who wishes to resume
3300 but this is also sent by servers and routers to notify other routers
3301 that the client is not detached anymore. */
3303 void silc_server_resume_client(SilcServer server,
3304 SilcSocketConnection sock,
3305 SilcPacketContext *packet)
3307 SilcBuffer buffer = packet->buffer, buf;
3308 SilcIDListData idata;
3309 SilcClientEntry detached_client;
3310 SilcClientID *client_id = NULL;
3311 unsigned char *id_string, *auth = NULL;
3312 SilcUInt16 id_len, auth_len = 0;
3313 int ret, nickfail = 0;
3314 bool resolved, local, nick_change = FALSE, resolve = FALSE;
3315 SilcChannelEntry channel;
3316 SilcHashTableList htl;
3317 SilcChannelClientEntry chl;
3318 SilcServerResumeResolve r;
3320 ret = silc_buffer_unformat(buffer,
3321 SILC_STR_UI16_NSTRING(&id_string, &id_len),
3324 client_id = silc_id_str2id(id_string, id_len, SILC_ID_CLIENT);
3326 if (sock->type == SILC_SOCKET_TYPE_CLIENT) {
3327 /* Client send this and is attempting to resume to old client session */
3328 SilcClientEntry client;
3332 silc_buffer_pull(buffer, 2 + id_len);
3333 auth = buffer->data;
3334 auth_len = buffer->len;
3335 silc_buffer_push(buffer, 2 + id_len);
3338 if (!client_id || auth_len < 128) {
3339 SILC_LOG_ERROR(("Client %s (%s) sent incomplete resume information, "
3340 "closing connection", sock->hostname, sock->ip));
3341 silc_server_disconnect_remote(server, sock,
3342 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
3343 "Resuming not possible");
3344 if (sock->user_data)
3345 silc_server_free_sock_user_data(server, sock, NULL);
3346 silc_free(client_id);
3350 /* Take client entry of this connection */
3351 client = (SilcClientEntry)sock->user_data;
3352 idata = (SilcIDListData)client;
3354 /* Get entry to the client, and resolve it if we don't have it. */
3355 detached_client = silc_server_get_client_resolve(server, client_id, FALSE,
3357 if (!detached_client) {
3359 /* The client info is being resolved. Reprocess this packet after
3360 receiving the reply to the query. */
3361 SILC_LOG_DEBUG(("Resolving client"));
3362 r = silc_calloc(1, sizeof(*r));
3366 r->sock = silc_socket_dup(sock);
3367 r->packet = silc_packet_context_dup(packet);
3368 r->data = client_id;
3369 silc_server_command_pending(server, SILC_COMMAND_WHOIS,
3371 silc_server_command_resume_resolve, r);
3373 SILC_LOG_ERROR(("Client %s (%s) tried to resume unknown client, "
3374 "closing connection", sock->hostname, sock->ip));
3375 silc_server_disconnect_remote(server, sock,
3376 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
3377 "Resuming not possible");
3378 if (sock->user_data)
3379 silc_server_free_sock_user_data(server, sock, NULL);
3380 silc_free(client_id);
3385 if (!(detached_client->mode & SILC_UMODE_DETACHED))
3387 if (!silc_hash_table_count(detached_client->channels) &&
3388 detached_client->router)
3390 if (!detached_client->nickname)
3394 if (server->server_type == SILC_SERVER && !server->standalone) {
3395 /* The client info is being resolved. Reprocess this packet after
3396 receiving the reply to the query. */
3397 SILC_LOG_DEBUG(("Resolving client info"));
3398 silc_server_get_client_resolve(server, client_id, TRUE, NULL);
3399 r = silc_calloc(1, sizeof(*r));
3403 r->sock = silc_socket_dup(sock);
3404 r->packet = silc_packet_context_dup(packet);
3405 r->data = client_id;
3406 silc_server_command_pending(server, SILC_COMMAND_WHOIS,
3408 silc_server_command_resume_resolve, r);
3411 if (server->server_type == SILC_SERVER) {
3412 SILC_LOG_ERROR(("Client %s (%s) tried to resume un-detached client, "
3413 "closing connection", sock->hostname, sock->ip));
3414 silc_server_disconnect_remote(server, sock,
3415 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
3416 "Resuming not possible");
3417 if (sock->user_data)
3418 silc_server_free_sock_user_data(server, sock, NULL);
3419 silc_free(client_id);
3424 /* Check that we have the public key of the client, if not then we must
3425 resolve it first. */
3426 if (!detached_client->data.public_key) {
3427 if (server->server_type == SILC_SERVER && server->standalone) {
3428 SILC_LOG_ERROR(("Detached client's public key not present, "
3429 "closing connection"));
3430 silc_server_disconnect_remote(server, sock,
3431 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
3432 "Resuming not possible");
3433 if (sock->user_data)
3434 silc_server_free_sock_user_data(server, sock, NULL);
3435 silc_free(client_id);
3437 /* We must retrieve the detached client's public key by sending
3438 GETKEY command. Reprocess this packet after receiving the key */
3439 SilcBuffer idp = silc_id_payload_encode(client_id, SILC_ID_CLIENT);
3440 SilcSocketConnection dest_sock =
3441 silc_server_get_client_route(server, NULL, 0, client_id, NULL, NULL);
3443 SILC_LOG_DEBUG(("Resolving client public key"));
3445 silc_server_send_command(server, dest_sock ? dest_sock :
3446 SILC_PRIMARY_ROUTE(server),
3447 SILC_COMMAND_GETKEY, ++server->cmd_ident,
3448 1, 1, idp->data, idp->len);
3450 r = silc_calloc(1, sizeof(*r));
3452 silc_free(client_id);
3457 r->sock = silc_socket_dup(sock);
3458 r->packet = silc_packet_context_dup(packet);
3459 silc_server_command_pending(server, SILC_COMMAND_GETKEY,
3461 silc_server_command_resume_resolve, r);
3463 silc_buffer_free(idp);
3465 silc_free(client_id);
3467 } else if (!silc_pkcs_public_key_compare(detached_client->data.public_key,
3468 idata->public_key)) {
3469 /* We require that the connection and resuming authentication data
3470 must be using same key pair. */
3471 SILC_LOG_ERROR(("Resuming attempted with wrong public key, "
3472 "closing connection"));
3473 silc_server_disconnect_remote(server, sock,
3474 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
3475 "Resuming not possible");
3476 if (sock->user_data)
3477 silc_server_free_sock_user_data(server, sock, NULL);
3478 silc_free(client_id);
3482 /* Verify the authentication payload. This has to be successful in
3483 order to allow the resuming */
3485 !silc_auth_verify_data(auth, auth_len, SILC_AUTH_PUBLIC_KEY,
3486 detached_client->data.public_key, 0,
3487 idata->hash, detached_client->id,
3489 SILC_LOG_ERROR(("Client %s (%s) resume authentication failed, "
3490 "closing connection", sock->hostname, sock->ip));
3491 silc_server_disconnect_remote(server, sock,
3492 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
3493 "Resuming not possible");
3494 if (sock->user_data)
3495 silc_server_free_sock_user_data(server, sock, NULL);
3496 silc_free(client_id);
3500 /* Now resume the client to the network */
3502 silc_schedule_task_del_by_context(server->schedule, detached_client);
3503 sock->user_data = detached_client;
3504 detached_client->connection = sock;
3506 /* Take new keys and stuff into use in the old entry */
3507 silc_idlist_del_data(detached_client);
3508 silc_idlist_add_data(detached_client, idata);
3509 detached_client->data.status |= SILC_IDLIST_STATUS_REGISTERED;
3510 detached_client->data.status |= SILC_IDLIST_STATUS_RESUMED;
3511 detached_client->mode &= ~SILC_UMODE_DETACHED;
3512 server->stat.my_detached--;
3514 /* Send the RESUME_CLIENT packet to our primary router so that others
3515 know this client isn't detached anymore. */
3516 buf = silc_buffer_alloc_size(2 + id_len);
3517 silc_buffer_format(buf,
3518 SILC_STR_UI_SHORT(id_len),
3519 SILC_STR_UI_XNSTRING(id_string, id_len),
3522 /* Send to primary router */
3523 silc_server_packet_send(server, SILC_PRIMARY_ROUTE(server),
3524 SILC_PACKET_RESUME_CLIENT, 0,
3525 buf->data, buf->len, TRUE);
3527 /* As router we must deliver this packet directly to the original
3528 server whom this client was earlier. */
3529 if (server->server_type == SILC_ROUTER && detached_client->router &&
3530 detached_client->router->server_type != SILC_ROUTER)
3531 silc_server_packet_send(server, detached_client->router->connection,
3532 SILC_PACKET_RESUME_CLIENT, 0,
3533 buf->data, buf->len, TRUE);
3534 silc_buffer_free(buf);
3536 detached_client->router = NULL;
3538 /* Delete this client entry since we're resuming to old one. */
3539 server->stat.my_clients--;
3540 server->stat.clients--;
3541 if (server->stat.cell_clients)
3542 server->stat.cell_clients--;
3543 silc_server_del_from_watcher_list(server, client);
3544 silc_idlist_del_client(server->local_list, client);
3545 client = detached_client;
3547 /* If the ID is not based in our ID then change it */
3548 if (!SILC_ID_COMPARE(client->id, server->id, server->id->ip.data_len)) {
3549 silc_free(client_id);
3550 while (!silc_id_create_client_id(server, server->id, server->rng,
3551 server->md5hash, client->nickname,
3555 silc_server_disconnect_remote(server, sock,
3556 SILC_STATUS_ERR_BAD_NICKNAME, NULL);
3557 if (sock->user_data)
3558 silc_server_free_sock_user_data(server, sock, NULL);
3561 snprintf(&client->nickname[strlen(client->nickname) - 1], 1,
3568 /* Notify about Client ID change, nickname doesn't actually change. */
3569 silc_server_send_notify_nick_change(server, SILC_PRIMARY_ROUTE(server),
3570 SILC_BROADCAST(server),
3571 client->id, client_id,
3575 /* Resolve users on those channels that client has joined but we
3576 haven't resolved user list yet. */
3577 if (server->server_type == SILC_SERVER && !server->standalone) {
3578 silc_hash_table_list(client->channels, &htl);
3579 while (silc_hash_table_get(&htl, NULL, (void **)&chl)) {
3580 channel = chl->channel;
3581 SILC_LOG_DEBUG(("Resolving users for %s channel",
3582 channel->channel_name));
3583 if (channel->disabled || !channel->users_resolved) {
3584 silc_server_send_command(server, SILC_PRIMARY_ROUTE(server),
3585 SILC_COMMAND_USERS, ++server->cmd_ident,
3586 1, 2, channel->channel_name,
3587 strlen(channel->channel_name));
3590 silc_hash_table_list_reset(&htl);
3593 /* Send the new client ID to the client. After this client may start
3594 receiving other packets, and may start sending packets too. */
3595 silc_server_send_new_id(server, sock, FALSE, client_id, SILC_ID_CLIENT,
3596 silc_id_get_len(client_id, SILC_ID_CLIENT));
3599 /* Send NICK change notify to channels as well. */
3600 SilcBuffer oidp, nidp;
3601 oidp = silc_id_payload_encode(client->id, SILC_ID_CLIENT);
3602 nidp = silc_id_payload_encode(client_id, SILC_ID_CLIENT);
3603 silc_server_send_notify_on_channels(server, NULL, client,
3604 SILC_NOTIFY_TYPE_NICK_CHANGE, 3,
3605 oidp->data, oidp->len,
3606 nidp->data, nidp->len,
3608 strlen(client->nickname));
3609 silc_buffer_free(oidp);
3610 silc_buffer_free(nidp);
3613 /* Add the client again to the ID cache to get it to correct list */
3614 if (!silc_idcache_del_by_context(server->local_list->clients, client))
3615 silc_idcache_del_by_context(server->global_list->clients, client);
3616 silc_free(client->id);
3617 client->id = client_id;
3619 silc_idcache_add(server->local_list->clients, client->nickname,
3620 client->id, client, 0, NULL);
3622 /* Send some nice info to the client */
3623 silc_server_send_connect_notifys(server, sock, client);
3625 /* Send all channel keys of channels the client has joined */
3626 silc_hash_table_list(client->channels, &htl);
3627 while (silc_hash_table_get(&htl, NULL, (void **)&chl)) {
3628 bool created = FALSE;
3629 channel = chl->channel;
3631 if (channel->mode & SILC_CHANNEL_MODE_PRIVKEY)
3634 /* If we don't have channel key, then create one */
3635 if (!channel->channel_key) {
3636 if (!silc_server_create_channel_key(server, channel, 0))
3641 id_string = silc_id_id2str(channel->id, SILC_ID_CHANNEL);
3643 silc_channel_key_payload_encode(silc_id_get_len(channel->id,
3646 strlen(channel->channel_key->
3648 channel->channel_key->cipher->name,
3649 channel->key_len / 8, channel->key);
3650 silc_free(id_string);
3652 /* Send the key packet to client */
3653 silc_server_packet_send(server, sock, SILC_PACKET_CHANNEL_KEY, 0,
3654 keyp->data, keyp->len, FALSE);
3656 if (created && server->server_type == SILC_SERVER)
3657 silc_server_packet_send(server, SILC_PRIMARY_ROUTE(server),
3658 SILC_PACKET_CHANNEL_KEY, 0,
3659 keyp->data, keyp->len, FALSE);
3661 silc_buffer_free(keyp);
3663 silc_hash_table_list_reset(&htl);
3665 } else if (sock->type != SILC_SOCKET_TYPE_CLIENT) {
3666 /* Server or router sent this to us to notify that that a client has
3668 SilcServerEntry server_entry;
3669 SilcServerID *server_id;
3672 SILC_LOG_DEBUG(("Malformed resuming packet"));
3676 /* Get entry to the client, and resolve it if we don't have it. */
3677 detached_client = silc_idlist_find_client_by_id(server->local_list,
3678 client_id, TRUE, NULL);
3679 if (!detached_client) {
3680 detached_client = silc_idlist_find_client_by_id(server->global_list,
3681 client_id, TRUE, NULL);
3682 if (!detached_client) {
3683 SILC_LOG_DEBUG(("Resuming client is unknown"));
3684 silc_free(client_id);
3689 /* Check that the client has not been resumed already because it is
3690 protocol error to attempt to resume more than once. The client
3691 will be killed if this protocol error occurs. */
3692 if (detached_client->data.status & SILC_IDLIST_STATUS_RESUMED &&
3693 !(detached_client->mode & SILC_UMODE_DETACHED)) {
3694 /* The client is clearly attempting to resume more than once and
3695 perhaps playing around by resuming from several different places
3696 at the same time. */
3697 SILC_LOG_DEBUG(("Attempting to re-resume client, killing both"));
3698 silc_server_kill_client(server, detached_client, NULL,
3699 server->id, SILC_ID_SERVER);
3700 silc_free(client_id);
3704 /* Check whether client is detached at all */
3705 if (!(detached_client->mode & SILC_UMODE_DETACHED)) {
3706 SILC_LOG_DEBUG(("Client is not detached"));
3707 silc_free(client_id);
3711 /* Client is detached, and now it is resumed. Remove the detached
3712 mode and mark that it is resumed. */
3713 detached_client->mode &= ~SILC_UMODE_DETACHED;
3714 detached_client->data.status |= SILC_IDLIST_STATUS_RESUMED;
3716 /* Get the new owner of the resumed client */
3717 server_id = silc_id_str2id(packet->src_id, packet->src_id_len,
3718 packet->src_id_type);
3720 silc_free(client_id);
3724 /* Get server entry */
3725 server_entry = silc_idlist_find_server_by_id(server->global_list,
3726 server_id, TRUE, NULL);
3728 if (!server_entry) {
3729 server_entry = silc_idlist_find_server_by_id(server->local_list,
3730 server_id, TRUE, NULL);
3732 if (!server_entry) {
3733 silc_free(server_id);
3734 silc_free(client_id);
3739 if (server->server_type == SILC_ROUTER &&
3740 sock->type == SILC_SOCKET_TYPE_ROUTER &&
3741 server_entry->server_type == SILC_ROUTER)
3744 SILC_LOG_DEBUG(("Resuming detached client"));
3746 /* Change the client to correct list. */
3747 if (!silc_idcache_del_by_context(server->local_list->clients,
3749 silc_idcache_del_by_context(server->global_list->clients,
3751 silc_idcache_add(local && server->server_type == SILC_ROUTER ?
3752 server->local_list->clients :
3753 server->global_list->clients,
3754 detached_client->nickname,
3755 detached_client->id, detached_client, FALSE, NULL);
3757 /* Change the owner of the client if needed */
3758 if (detached_client->router != server_entry)
3759 detached_client->router = server_entry;
3761 /* Update channel information regarding global clients on channel. */
3762 if (server->server_type == SILC_SERVER) {
3763 silc_hash_table_list(detached_client->channels, &htl);
3764 while (silc_hash_table_get(&htl, NULL, (void **)&chl))
3765 chl->channel->global_users =
3766 silc_server_channel_has_global(chl->channel);
3767 silc_hash_table_list_reset(&htl);
3770 silc_schedule_task_del_by_context(server->schedule, detached_client);
3772 /* If the sender of this packet is server and we are router we need to
3773 broadcast this packet to other routers in the network. */
3774 if (server->server_type == SILC_ROUTER &&
3775 sock->type == SILC_SOCKET_TYPE_SERVER &&
3776 !(packet->flags & SILC_PACKET_FLAG_BROADCAST)) {
3777 SILC_LOG_DEBUG(("Broadcasting received Resume Client packet"));
3778 silc_server_packet_send(server, SILC_PRIMARY_ROUTE(server),
3780 packet->flags | SILC_PACKET_FLAG_BROADCAST,
3781 buffer->data, buffer->len, FALSE);
3782 silc_server_backup_send(server, (SilcServerEntry)sock->user_data,
3783 packet->type, packet->flags,
3784 packet->buffer->data, packet->buffer->len,
3788 silc_free(server_id);
3791 silc_free(client_id);