5 Author: Pekka Riikonen <priikone@silcnet.org>
7 Copyright (C) 1997 - 2002 Pekka Riikonen
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
21 * Server packet routines to handle received packets.
25 #include "serverincludes.h"
26 #include "server_internal.h"
28 /* Received notify packet. Server can receive notify packets from router.
29 Server then relays the notify messages to clients if needed. */
31 void silc_server_notify(SilcServer server,
32 SilcSocketConnection sock,
33 SilcPacketContext *packet)
35 SilcNotifyPayload payload;
37 SilcArgumentPayload args;
38 SilcChannelID *channel_id = NULL, *channel_id2;
39 SilcClientID *client_id, *client_id2;
40 SilcServerID *server_id;
42 SilcChannelEntry channel = NULL;
43 SilcClientEntry client = NULL, client2 = NULL;
44 SilcServerEntry server_entry = NULL;
45 SilcChannelClientEntry chl;
46 SilcIDCacheEntry cache;
47 SilcHashTableList htl;
53 SILC_LOG_DEBUG(("Start"));
55 if (sock->type == SILC_SOCKET_TYPE_CLIENT ||
56 packet->src_id_type != SILC_ID_SERVER)
62 /* If the packet is destined directly to a client then relay the packet
63 before processing it. */
64 if (packet->dst_id_type == SILC_ID_CLIENT) {
66 SilcSocketConnection dst_sock;
68 /* Get the route to the client */
69 dst_sock = silc_server_get_client_route(server, packet->dst_id,
70 packet->dst_id_len, NULL,
73 /* Relay the packet */
74 silc_server_relay_packet(server, dst_sock, idata->send_key,
75 idata->hmac_send, idata->psn_send++,
79 /* Parse the Notify Payload */
80 payload = silc_notify_payload_parse(packet->buffer->data,
85 /* If we are router and this packet is not already broadcast packet
86 we will broadcast it. The sending socket really cannot be router or
87 the router is buggy. If this packet is coming from router then it must
88 have the broadcast flag set already and we won't do anything. */
89 if (!server->standalone && server->server_type == SILC_ROUTER &&
90 sock->type == SILC_SOCKET_TYPE_SERVER &&
91 !(packet->flags & SILC_PACKET_FLAG_BROADCAST)) {
92 SILC_LOG_DEBUG(("Broadcasting received Notify packet"));
93 if (packet->dst_id_type == SILC_ID_CHANNEL) {
94 /* Packet is destined to channel */
95 channel_id = silc_id_str2id(packet->dst_id, packet->dst_id_len,
100 silc_server_packet_send_dest(server, server->router->connection,
102 packet->flags | SILC_PACKET_FLAG_BROADCAST,
103 channel_id, SILC_ID_CHANNEL,
104 packet->buffer->data, packet->buffer->len,
106 silc_server_backup_send_dest(server, (SilcServerEntry)sock->user_data,
107 packet->type, packet->flags,
108 channel_id, SILC_ID_CHANNEL,
109 packet->buffer->data, packet->buffer->len,
112 /* Packet is destined to client or server */
113 silc_server_packet_send(server, server->router->connection,
115 packet->flags | SILC_PACKET_FLAG_BROADCAST,
116 packet->buffer->data, packet->buffer->len,
118 silc_server_backup_send(server, (SilcServerEntry)sock->user_data,
119 packet->type, packet->flags,
120 packet->buffer->data, packet->buffer->len,
125 type = silc_notify_get_type(payload);
126 args = silc_notify_get_args(payload);
131 case SILC_NOTIFY_TYPE_JOIN:
133 * Distribute the notify to local clients on the channel
135 SILC_LOG_DEBUG(("JOIN notify"));
138 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
141 channel_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
145 /* Get channel entry */
146 channel = silc_idlist_find_channel_by_id(server->global_list,
149 channel = silc_idlist_find_channel_by_id(server->local_list,
152 silc_free(channel_id);
156 silc_free(channel_id);
159 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
162 client_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
166 /* If the the client is not in local list we check global list (ie. the
167 channel will be global channel) and if it does not exist then create
168 entry for the client. */
169 client = silc_idlist_find_client_by_id(server->global_list,
170 client_id, server->server_type,
173 client = silc_idlist_find_client_by_id(server->local_list,
174 client_id, server->server_type,
177 /* If router did not find the client the it is bogus */
178 if (server->server_type != SILC_SERVER)
182 silc_idlist_add_client(server->global_list, NULL, NULL, NULL,
183 silc_id_dup(client_id, SILC_ID_CLIENT),
184 sock->user_data, NULL, 0);
186 SILC_LOG_ERROR(("Could not add new client to the ID Cache"));
187 silc_free(client_id);
191 client->data.status |= SILC_IDLIST_STATUS_REGISTERED;
195 /* Do not process the notify if the client is not registered */
196 if (!(client->data.status & SILC_IDLIST_STATUS_REGISTERED))
199 /* Do not add client to channel if it is there already */
200 if (silc_server_client_on_channel(client, channel, NULL)) {
201 SILC_LOG_DEBUG(("Client already on channel"));
205 /* Send to channel */
206 silc_server_packet_send_to_channel(server, sock, channel, packet->type,
207 FALSE, packet->buffer->data,
208 packet->buffer->len, FALSE);
210 if (server->server_type != SILC_ROUTER &&
211 sock->type == SILC_SOCKET_TYPE_ROUTER)
212 /* The channel is global now */
213 channel->global_users = TRUE;
215 SILC_LOG_DEBUG(("Joining to channel %s", channel->channel_name));
217 /* JOIN the global client to the channel (local clients (if router
218 created the channel) is joined in the pending JOIN command). */
219 chl = silc_calloc(1, sizeof(*chl));
220 chl->client = client;
221 chl->channel = channel;
223 /* If this is the first one on the channel then it is the founder of
225 if (!silc_hash_table_count(channel->user_list))
226 chl->mode = (SILC_CHANNEL_UMODE_CHANOP | SILC_CHANNEL_UMODE_CHANFO);
228 silc_hash_table_add(channel->user_list, client, chl);
229 silc_hash_table_add(client->channels, channel, chl);
230 silc_free(client_id);
231 channel->user_count++;
235 case SILC_NOTIFY_TYPE_LEAVE:
237 * Distribute the notify to local clients on the channel
239 SILC_LOG_DEBUG(("LEAVE notify"));
242 channel_id = silc_id_str2id(packet->dst_id, packet->dst_id_len,
243 packet->dst_id_type);
248 /* Get channel entry */
249 channel = silc_idlist_find_channel_by_id(server->global_list,
252 channel = silc_idlist_find_channel_by_id(server->local_list,
255 silc_free(channel_id);
261 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
263 silc_free(channel_id);
266 client_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
268 silc_free(channel_id);
272 /* Get client entry */
273 client = silc_idlist_find_client_by_id(server->global_list,
274 client_id, TRUE, NULL);
276 client = silc_idlist_find_client_by_id(server->local_list,
277 client_id, TRUE, NULL);
279 silc_free(client_id);
280 silc_free(channel_id);
284 silc_free(client_id);
286 /* Check if on channel */
287 if (!silc_server_client_on_channel(client, channel, NULL))
290 /* Send the leave notify to channel */
291 silc_server_packet_send_to_channel(server, sock, channel, packet->type,
292 FALSE, packet->buffer->data,
293 packet->buffer->len, FALSE);
295 /* Remove the user from channel */
296 silc_server_remove_from_one_channel(server, sock, channel, client, FALSE);
299 case SILC_NOTIFY_TYPE_SIGNOFF:
301 * Distribute the notify to local clients on the channel
303 SILC_LOG_DEBUG(("SIGNOFF notify"));
306 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
309 client_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
313 /* Get client entry */
314 client = silc_idlist_find_client_by_id(server->global_list,
315 client_id, TRUE, &cache);
317 client = silc_idlist_find_client_by_id(server->local_list,
318 client_id, TRUE, &cache);
320 silc_free(client_id);
324 silc_free(client_id);
326 /* Get signoff message */
327 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
331 /* Update statistics */
332 server->stat.clients--;
333 if (server->stat.cell_clients)
334 server->stat.cell_clients--;
335 SILC_OPER_STATS_UPDATE(client, server, SILC_UMODE_SERVER_OPERATOR);
336 SILC_OPER_STATS_UPDATE(client, router, SILC_UMODE_ROUTER_OPERATOR);
338 /* Remove the client from all channels. */
339 silc_server_remove_from_channels(server, NULL, client, TRUE, tmp, FALSE);
341 /* Check if anyone is watching this nickname */
342 if (server->server_type == SILC_ROUTER)
343 silc_server_check_watcher_list(server, client, NULL,
344 SILC_NOTIFY_TYPE_SIGNOFF);
346 client->data.status &= ~SILC_IDLIST_STATUS_REGISTERED;
347 cache->expire = SILC_ID_CACHE_EXPIRE_DEF;
350 case SILC_NOTIFY_TYPE_TOPIC_SET:
352 * Distribute the notify to local clients on the channel
355 SILC_LOG_DEBUG(("TOPIC SET notify"));
358 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
361 client_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
365 /* Get client entry */
366 client = silc_idlist_find_client_by_id(server->global_list,
367 client_id, TRUE, &cache);
369 client = silc_idlist_find_client_by_id(server->local_list,
370 client_id, TRUE, &cache);
372 silc_free(client_id);
376 silc_free(client_id);
379 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
381 silc_free(channel_id);
386 channel_id = silc_id_str2id(packet->dst_id, packet->dst_id_len,
387 packet->dst_id_type);
392 /* Get channel entry */
393 channel = silc_idlist_find_channel_by_id(server->global_list,
396 channel = silc_idlist_find_channel_by_id(server->local_list,
399 silc_free(channel_id);
404 if (channel->topic && !strcmp(channel->topic, tmp))
407 /* Get user's channel entry and check that topic set is allowed. */
408 if (!silc_server_client_on_channel(client, channel, &chl))
410 if (channel->mode & SILC_CHANNEL_MODE_TOPIC &&
411 !(chl->mode & SILC_CHANNEL_UMODE_CHANOP) &&
412 !(chl->mode & SILC_CHANNEL_UMODE_CHANFO)) {
413 SILC_LOG_DEBUG(("Topic change is not allowed"));
417 /* Change the topic */
418 silc_free(channel->topic);
419 channel->topic = strdup(tmp);
421 /* Send the same notify to the channel */
422 silc_server_packet_send_to_channel(server, sock, channel, packet->type,
423 FALSE, packet->buffer->data,
424 packet->buffer->len, FALSE);
425 silc_free(channel_id);
428 case SILC_NOTIFY_TYPE_NICK_CHANGE:
431 * Distribute the notify to local clients on the channel
433 unsigned char *id, *id2;
435 SilcUInt32 nickname_len;
437 SILC_LOG_DEBUG(("NICK CHANGE notify"));
439 /* Get old client ID */
440 id = silc_argument_get_arg_type(args, 1, &tmp_len);
443 client_id = silc_id_payload_parse_id(id, tmp_len, NULL);
447 /* Get new client ID */
448 id2 = silc_argument_get_arg_type(args, 2, &tmp_len);
451 client_id2 = silc_id_payload_parse_id(id2, tmp_len, NULL);
455 SILC_LOG_DEBUG(("Old Client ID id(%s)",
456 silc_id_render(client_id, SILC_ID_CLIENT)));
457 SILC_LOG_DEBUG(("New Client ID id(%s)",
458 silc_id_render(client_id2, SILC_ID_CLIENT)));
460 /* From protocol version 1.1 we also get the new nickname */
461 nickname = silc_argument_get_arg_type(args, 3, &nickname_len);;
463 /* Replace the Client ID */
464 client = silc_idlist_replace_client_id(server,
465 server->global_list, client_id,
466 client_id2, nickname);
468 client = silc_idlist_replace_client_id(server,
469 server->local_list, client_id,
470 client_id2, nickname);
473 /* Send the NICK_CHANGE notify type to local clients on the channels
474 this client is joined to. */
475 silc_server_send_notify_on_channels(server, client, client,
476 SILC_NOTIFY_TYPE_NICK_CHANGE, 3,
477 id, tmp_len, id2, tmp_len,
482 silc_free(client_id);
484 silc_free(client_id2);
488 case SILC_NOTIFY_TYPE_CMODE_CHANGE:
490 * Distribute the notify to local clients on the channel
493 SILC_LOG_DEBUG(("CMODE CHANGE notify"));
496 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
499 client_id = silc_id_payload_parse_id(tmp, tmp_len, &id_type);
503 /* Get client entry */
504 if (id_type == SILC_ID_CLIENT) {
505 client = silc_idlist_find_client_by_id(server->global_list,
506 client_id, TRUE, &cache);
508 client = silc_idlist_find_client_by_id(server->local_list,
509 client_id, TRUE, &cache);
511 silc_free(client_id);
515 silc_free(client_id);
519 channel_id = silc_id_str2id(packet->dst_id, packet->dst_id_len,
520 packet->dst_id_type);
525 /* Get channel entry */
526 channel = silc_idlist_find_channel_by_id(server->global_list,
529 channel = silc_idlist_find_channel_by_id(server->local_list,
532 silc_free(channel_id);
536 silc_free(channel_id);
539 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
542 SILC_GET32_MSB(mode, tmp);
544 /* Check if mode changed */
545 if (channel->mode == mode)
548 /* Get user's channel entry and check that mode change is allowed */
550 if (!silc_server_client_on_channel(client, channel, &chl))
552 if (!silc_server_check_cmode_rights(server, channel, chl, mode)) {
553 SILC_LOG_DEBUG(("CMODE change is not allowed"));
558 /* Send the same notify to the channel */
559 silc_server_packet_send_to_channel(server, sock, channel, packet->type,
560 FALSE, packet->buffer->data,
561 packet->buffer->len, FALSE);
563 /* If the channel had private keys set and the mode was removed then
564 we must re-generate and re-distribute a new channel key */
565 if (channel->mode & SILC_CHANNEL_MODE_PRIVKEY &&
566 !(mode & SILC_CHANNEL_MODE_PRIVKEY)) {
567 /* Re-generate channel key */
568 if (!silc_server_create_channel_key(server, channel, 0))
571 /* Send the channel key. This sends it to our local clients and if
572 we are normal server to our router as well. */
573 silc_server_send_channel_key(server, NULL, channel,
574 server->server_type == SILC_ROUTER ?
575 FALSE : !server->standalone);
579 channel->mode = mode;
582 tmp = silc_argument_get_arg_type(args, 4, &tmp_len);
584 unsigned char hash[32];
587 silc_hmac_free(channel->hmac);
588 if (!silc_hmac_alloc(tmp, NULL, &channel->hmac))
591 /* Set the HMAC key out of current channel key. The client must do
593 silc_hash_make(silc_hmac_get_hash(channel->hmac), channel->key,
594 channel->key_len / 8, hash);
595 silc_hmac_set_key(channel->hmac, hash,
596 silc_hash_len(silc_hmac_get_hash(channel->hmac)));
597 memset(hash, 0, sizeof(hash));
600 /* Get the passphrase */
601 tmp = silc_argument_get_arg_type(args, 5, &tmp_len);
603 silc_free(channel->passphrase);
604 channel->passphrase = silc_memdup(tmp, tmp_len);
609 case SILC_NOTIFY_TYPE_CUMODE_CHANGE:
612 * Distribute the notify to local clients on the channel
614 SilcChannelClientEntry chl2 = NULL;
615 bool notify_sent = FALSE;
617 SILC_LOG_DEBUG(("CUMODE CHANGE notify"));
620 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
623 client_id = silc_id_payload_parse_id(tmp, tmp_len, &id_type);
627 /* Get client entry */
628 if (id_type == SILC_ID_CLIENT) {
629 client = silc_idlist_find_client_by_id(server->global_list,
630 client_id, TRUE, &cache);
632 client = silc_idlist_find_client_by_id(server->local_list,
633 client_id, TRUE, &cache);
635 silc_free(client_id);
639 silc_free(client_id);
643 channel_id = silc_id_str2id(packet->dst_id, packet->dst_id_len,
644 packet->dst_id_type);
649 /* Get channel entry */
650 channel = silc_idlist_find_channel_by_id(server->global_list,
653 channel = silc_idlist_find_channel_by_id(server->local_list,
656 silc_free(channel_id);
662 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
664 silc_free(channel_id);
668 SILC_GET32_MSB(mode, tmp);
670 /* Get target client */
671 tmp = silc_argument_get_arg_type(args, 3, &tmp_len);
674 client_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
678 /* Get client entry */
679 client2 = silc_idlist_find_client_by_id(server->global_list,
680 client_id, TRUE, NULL);
682 client2 = silc_idlist_find_client_by_id(server->local_list,
683 client_id, TRUE, NULL);
685 silc_free(client_id);
689 silc_free(client_id);
692 /* Check that sender is on channel */
693 if (!silc_server_client_on_channel(client, channel, &chl))
696 if (client != client2) {
697 /* Sender must be operator */
698 if (!(chl->mode & SILC_CHANNEL_UMODE_CHANOP) &&
699 !(chl->mode & SILC_CHANNEL_UMODE_CHANFO)) {
700 SILC_LOG_DEBUG(("CUMODE change is not allowed"));
704 /* Check that target is on channel */
705 if (!silc_server_client_on_channel(client2, channel, &chl))
708 /* If target is founder mode change is not allowed. */
709 if (chl->mode & SILC_CHANNEL_UMODE_CHANFO) {
710 SILC_LOG_DEBUG(("CUMODE change is not allowed"));
716 /* Get entry to the channel user list */
717 silc_hash_table_list(channel->user_list, &htl);
718 while (silc_hash_table_get(&htl, NULL, (void *)&chl)) {
719 /* If the mode is channel founder and we already find a client
720 to have that mode on the channel we will enforce the sender
721 to change the channel founder mode away. There can be only one
722 channel founder on the channel. */
723 if (server->server_type == SILC_ROUTER &&
724 mode & SILC_CHANNEL_UMODE_CHANFO &&
725 chl->mode & SILC_CHANNEL_UMODE_CHANFO) {
727 unsigned char cumode[4];
729 if (chl->client == client && chl->mode == mode) {
734 mode &= ~SILC_CHANNEL_UMODE_CHANFO;
735 silc_server_send_notify_cumode(server, sock, FALSE, channel, mode,
736 client2->id, SILC_ID_CLIENT,
739 idp = silc_id_payload_encode(client2->id, SILC_ID_CLIENT);
740 SILC_PUT32_MSB(mode, cumode);
741 silc_server_send_notify_to_channel(server, sock, channel, FALSE,
742 SILC_NOTIFY_TYPE_CUMODE_CHANGE,
743 3, idp->data, idp->len,
745 idp->data, idp->len);
746 silc_buffer_free(idp);
749 /* Force the mode change if we alredy set the mode */
752 silc_free(channel_id);
753 silc_hash_table_list_reset(&htl);
758 if (chl->client == client2) {
759 if (chl->mode == mode) {
764 SILC_LOG_DEBUG(("Changing the channel user mode"));
766 /* Change the mode */
768 if (!(mode & SILC_CHANNEL_UMODE_CHANFO))
774 silc_hash_table_list_reset(&htl);
776 /* Send the same notify to the channel */
778 silc_server_packet_send_to_channel(server, sock, channel,
780 FALSE, packet->buffer->data,
781 packet->buffer->len, FALSE);
783 silc_free(channel_id);
787 case SILC_NOTIFY_TYPE_INVITE:
789 if (packet->dst_id_type == SILC_ID_CLIENT)
792 SILC_LOG_DEBUG(("INVITE notify"));
795 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
798 channel_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
802 /* Get channel entry */
803 channel = silc_idlist_find_channel_by_id(server->global_list,
806 channel = silc_idlist_find_channel_by_id(server->local_list,
809 silc_free(channel_id);
813 silc_free(channel_id);
816 tmp = silc_argument_get_arg_type(args, 3, &tmp_len);
819 client_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
823 /* Get client entry */
824 client = silc_idlist_find_client_by_id(server->global_list,
825 client_id, TRUE, &cache);
827 client = silc_idlist_find_client_by_id(server->local_list,
828 client_id, TRUE, &cache);
830 silc_free(client_id);
834 silc_free(client_id);
836 /* Get user's channel entry and check that inviting is allowed. */
837 if (!silc_server_client_on_channel(client, channel, &chl))
839 if (channel->mode & SILC_CHANNEL_MODE_INVITE &&
840 !(chl->mode & SILC_CHANNEL_UMODE_CHANOP) &&
841 !(chl->mode & SILC_CHANNEL_UMODE_CHANFO)) {
842 SILC_LOG_DEBUG(("Inviting is not allowed"));
846 /* Get the added invite */
847 tmp = silc_argument_get_arg_type(args, 4, &tmp_len);
849 if (!channel->invite_list)
850 channel->invite_list = silc_calloc(tmp_len + 2,
851 sizeof(*channel->invite_list));
853 channel->invite_list = silc_realloc(channel->invite_list,
854 sizeof(*channel->invite_list) *
856 strlen(channel->invite_list) +
858 if (tmp[tmp_len - 1] == ',')
859 tmp[tmp_len - 1] = '\0';
861 strncat(channel->invite_list, tmp, tmp_len);
862 strncat(channel->invite_list, ",", 1);
865 /* Get the deleted invite */
866 tmp = silc_argument_get_arg_type(args, 5, &tmp_len);
867 if (tmp && channel->invite_list) {
868 char *start, *end, *n;
870 if (!strncmp(channel->invite_list, tmp,
871 strlen(channel->invite_list) - 1)) {
872 silc_free(channel->invite_list);
873 channel->invite_list = NULL;
875 start = strstr(channel->invite_list, tmp);
876 if (start && strlen(start) >= tmp_len) {
877 end = start + tmp_len;
878 n = silc_calloc(strlen(channel->invite_list) - tmp_len, sizeof(*n));
879 strncat(n, channel->invite_list, start - channel->invite_list);
880 strncat(n, end + 1, ((channel->invite_list +
881 strlen(channel->invite_list)) - end) - 1);
882 silc_free(channel->invite_list);
883 channel->invite_list = n;
890 case SILC_NOTIFY_TYPE_CHANNEL_CHANGE:
892 * Distribute to the local clients on the channel and change the
896 SILC_LOG_DEBUG(("CHANNEL CHANGE"));
898 if (sock->type != SILC_SOCKET_TYPE_ROUTER)
901 /* Get the old Channel ID */
902 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
905 channel_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
909 /* Get the channel entry */
910 channel = silc_idlist_find_channel_by_id(server->local_list,
913 channel = silc_idlist_find_channel_by_id(server->global_list,
916 silc_free(channel_id);
921 /* Send the notify to the channel */
922 silc_server_packet_send_to_channel(server, sock, channel, packet->type,
923 FALSE, packet->buffer->data,
924 packet->buffer->len, FALSE);
926 /* Get the new Channel ID */
927 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
930 channel_id2 = silc_id_payload_parse_id(tmp, tmp_len, NULL);
934 SILC_LOG_DEBUG(("Old Channel ID id(%s)",
935 silc_id_render(channel_id, SILC_ID_CHANNEL)));
936 SILC_LOG_DEBUG(("New Channel ID id(%s)",
937 silc_id_render(channel_id2, SILC_ID_CHANNEL)));
939 /* Replace the Channel ID */
940 if (!silc_idlist_replace_channel_id(server->local_list, channel_id,
942 if (!silc_idlist_replace_channel_id(server->global_list, channel_id,
944 silc_free(channel_id2);
949 SilcBuffer users = NULL, users_modes = NULL;
951 /* Re-announce this channel which ID was changed. */
952 silc_server_send_new_channel(server, sock, FALSE, channel->channel_name,
954 silc_id_get_len(channel->id,
958 /* Re-announce our clients on the channel as the ID has changed now */
959 silc_server_announce_get_channel_users(server, channel, &users,
962 silc_buffer_push(users, users->data - users->head);
963 silc_server_packet_send(server, sock,
964 SILC_PACKET_NOTIFY, SILC_PACKET_FLAG_LIST,
965 users->data, users->len, FALSE);
966 silc_buffer_free(users);
969 silc_buffer_push(users_modes, users_modes->data - users_modes->head);
970 silc_server_packet_send_dest(server, sock,
971 SILC_PACKET_NOTIFY, SILC_PACKET_FLAG_LIST,
972 channel->id, SILC_ID_CHANNEL,
974 users_modes->len, FALSE);
975 silc_buffer_free(users_modes);
978 /* Re-announce channel's topic */
979 if (channel->topic) {
980 silc_server_send_notify_topic_set(server, sock,
981 server->server_type == SILC_ROUTER ?
982 TRUE : FALSE, channel,
983 channel->id, SILC_ID_CHANNEL,
988 silc_free(channel_id);
992 case SILC_NOTIFY_TYPE_SERVER_SIGNOFF:
994 * Remove the server entry and all clients that this server owns.
997 SILC_LOG_DEBUG(("SERVER SIGNOFF notify"));
1000 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
1003 server_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
1007 /* Get server entry */
1008 server_entry = silc_idlist_find_server_by_id(server->global_list,
1009 server_id, TRUE, NULL);
1011 if (!server_entry) {
1012 server_entry = silc_idlist_find_server_by_id(server->local_list,
1013 server_id, TRUE, NULL);
1015 if (!server_entry) {
1016 /* If we are normal server then we might not have the server. Check
1017 whether router was kind enough to send the list of all clients
1018 that actually was to be removed. Remove them if the list is
1020 if (server->server_type != SILC_ROUTER &&
1021 silc_argument_get_arg_num(args) > 1) {
1024 for (i = 1; i < silc_argument_get_arg_num(args); i++) {
1026 tmp = silc_argument_get_arg_type(args, i + 1, &tmp_len);
1029 client_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
1033 /* Get client entry */
1034 client = silc_idlist_find_client_by_id(server->global_list,
1035 client_id, TRUE, &cache);
1038 client = silc_idlist_find_client_by_id(server->local_list,
1039 client_id, TRUE, &cache);
1042 silc_free(client_id);
1046 silc_free(client_id);
1048 /* Update statistics */
1049 server->stat.clients--;
1050 if (server->stat.cell_clients)
1051 server->stat.cell_clients--;
1052 SILC_OPER_STATS_UPDATE(client, server, SILC_UMODE_SERVER_OPERATOR);
1053 SILC_OPER_STATS_UPDATE(client, router, SILC_UMODE_ROUTER_OPERATOR);
1055 /* Remove the client from all channels. */
1056 silc_server_remove_from_channels(server, NULL, client,
1059 /* Check if anyone is watching this nickname */
1060 if (server->server_type == SILC_ROUTER)
1061 silc_server_check_watcher_list(server, client, NULL,
1062 SILC_NOTIFY_TYPE_SERVER_SIGNOFF);
1064 /* Remove this client from watcher list if it is */
1066 silc_server_del_from_watcher_list(server, client);
1068 /* Remove the client */
1069 silc_idlist_del_client(local ? server->local_list :
1070 server->global_list, client);
1074 silc_free(server_id);
1078 silc_free(server_id);
1080 /* Free all client entries that this server owns as they will
1081 become invalid now as well. */
1082 silc_server_remove_clients_by_server(server, server_entry, TRUE);
1084 /* Remove the server entry */
1085 silc_idlist_del_server(local ? server->local_list :
1086 server->global_list, server_entry);
1088 /* XXX update statistics */
1092 case SILC_NOTIFY_TYPE_KICKED:
1094 * Distribute the notify to local clients on the channel
1097 SILC_LOG_DEBUG(("KICKED notify"));
1100 channel_id = silc_id_str2id(packet->dst_id, packet->dst_id_len,
1101 packet->dst_id_type);
1106 /* Get channel entry */
1107 channel = silc_idlist_find_channel_by_id(server->global_list,
1110 channel = silc_idlist_find_channel_by_id(server->local_list,
1113 silc_free(channel_id);
1117 silc_free(channel_id);
1120 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
1123 client_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
1127 /* If the the client is not in local list we check global list */
1128 client = silc_idlist_find_client_by_id(server->global_list,
1129 client_id, TRUE, NULL);
1131 client = silc_idlist_find_client_by_id(server->local_list,
1132 client_id, TRUE, NULL);
1134 silc_free(client_id);
1138 silc_free(client_id);
1140 /* If target is founder they cannot be kicked */
1141 if (!silc_server_client_on_channel(client, channel, &chl))
1143 if (chl->mode & SILC_CHANNEL_UMODE_CHANFO)
1146 /* From protocol version 1.1 we get the kicker's ID as well. */
1147 tmp = silc_argument_get_arg_type(args, 3, &tmp_len);
1149 client_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
1153 /* If the the client is not in local list we check global list */
1154 client2 = silc_idlist_find_client_by_id(server->global_list,
1155 client_id, TRUE, NULL);
1157 client2 = silc_idlist_find_client_by_id(server->local_list,
1158 client_id, TRUE, NULL);
1160 silc_free(client_id);
1164 silc_free(client_id);
1166 /* Kicker must be operator on channel */
1167 if (!silc_server_client_on_channel(client2, channel, &chl))
1169 if (!(chl->mode & SILC_CHANNEL_UMODE_CHANOP) &&
1170 !(chl->mode & SILC_CHANNEL_UMODE_CHANFO)) {
1171 SILC_LOG_DEBUG(("Kicking is not allowed"));
1176 /* Send to channel */
1177 silc_server_packet_send_to_channel(server, sock, channel, packet->type,
1178 FALSE, packet->buffer->data,
1179 packet->buffer->len, FALSE);
1181 /* Remove the client from channel */
1182 silc_server_remove_from_one_channel(server, sock, channel, client, FALSE);
1186 case SILC_NOTIFY_TYPE_KILLED:
1189 * Distribute the notify to local clients on channels
1191 unsigned char *id, *comment;
1192 SilcUInt32 id_len, comment_len;
1194 SILC_LOG_DEBUG(("KILLED notify"));
1197 id = silc_argument_get_arg_type(args, 1, &id_len);
1200 client_id = silc_id_payload_parse_id(id, id_len, NULL);
1204 /* If the the client is not in local list we check global list */
1205 client = silc_idlist_find_client_by_id(server->global_list,
1206 client_id, TRUE, NULL);
1208 client = silc_idlist_find_client_by_id(server->local_list,
1209 client_id, TRUE, NULL);
1211 silc_free(client_id);
1215 silc_free(client_id);
1217 /* If the client is one of ours, then close the connection to the
1218 client now. This removes the client from all channels as well. */
1219 if (packet->dst_id_type == SILC_ID_CLIENT && client->connection) {
1220 sock = client->connection;
1221 silc_server_free_client_data(server, NULL, client, FALSE, NULL);
1222 silc_server_close_connection(server, sock);
1227 comment = silc_argument_get_arg_type(args, 2, &comment_len);
1228 if (comment_len > 128)
1231 /* From protocol version 1.1 we get the killer's ID as well. */
1232 tmp = silc_argument_get_arg_type(args, 3, &tmp_len);
1234 client_id = silc_id_payload_parse_id(tmp, tmp_len, &id_type);
1238 if (id_type == SILC_ID_CLIENT) {
1239 /* If the the client is not in local list we check global list */
1240 client2 = silc_idlist_find_client_by_id(server->global_list,
1241 client_id, TRUE, NULL);
1243 client2 = silc_idlist_find_client_by_id(server->local_list,
1244 client_id, TRUE, NULL);
1246 silc_free(client_id);
1250 silc_free(client_id);
1252 /* Killer must be router operator */
1253 if (!(client2->mode & SILC_UMODE_ROUTER_OPERATOR)) {
1254 SILC_LOG_DEBUG(("Killing is not allowed"));
1260 /* Send the notify to local clients on the channels except to the
1261 client who is killed. */
1262 silc_server_send_notify_on_channels(server, client, client,
1263 SILC_NOTIFY_TYPE_KILLED, 3,
1264 id, id_len, comment, comment_len,
1267 /* Remove the client from all channels */
1268 silc_server_remove_from_channels(server, NULL, client, FALSE, NULL,
1271 /* Check if anyone is watching this nickname */
1272 if (server->server_type == SILC_ROUTER)
1273 silc_server_check_watcher_list(server, client, NULL,
1274 SILC_NOTIFY_TYPE_KILLED);
1279 case SILC_NOTIFY_TYPE_UMODE_CHANGE:
1281 * Save the mode of the client.
1284 SILC_LOG_DEBUG(("UMODE_CHANGE notify"));
1287 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
1290 client_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
1294 /* Get client entry */
1295 client = silc_idlist_find_client_by_id(server->global_list,
1296 client_id, TRUE, NULL);
1298 client = silc_idlist_find_client_by_id(server->local_list,
1299 client_id, TRUE, NULL);
1301 silc_free(client_id);
1305 silc_free(client_id);
1308 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
1311 SILC_GET32_MSB(mode, tmp);
1313 /* Check that mode changing is allowed. */
1314 if (!silc_server_check_umode_rights(server, client, mode)) {
1315 SILC_LOG_DEBUG(("UMODE change is not allowed"));
1319 /* Remove internal resumed flag if client is marked detached now */
1320 if (mode & SILC_UMODE_DETACHED)
1321 client->data.status &= ~SILC_IDLIST_STATUS_RESUMED;
1323 /* Change the mode */
1324 client->mode = mode;
1326 /* Check if anyone is watching this nickname */
1327 if (server->server_type == SILC_ROUTER)
1328 silc_server_check_watcher_list(server, client, NULL,
1329 SILC_NOTIFY_TYPE_UMODE_CHANGE);
1333 case SILC_NOTIFY_TYPE_BAN:
1338 SILC_LOG_DEBUG(("BAN notify"));
1340 /* Get Channel ID */
1341 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
1344 channel_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
1348 /* Get channel entry */
1349 channel = silc_idlist_find_channel_by_id(server->global_list,
1352 channel = silc_idlist_find_channel_by_id(server->local_list,
1355 silc_free(channel_id);
1359 silc_free(channel_id);
1361 /* Get the new ban and add it to the ban list */
1362 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
1364 if (!channel->ban_list)
1365 channel->ban_list = silc_calloc(tmp_len + 2,
1366 sizeof(*channel->ban_list));
1368 channel->ban_list = silc_realloc(channel->ban_list,
1369 sizeof(*channel->ban_list) *
1371 strlen(channel->ban_list) + 2));
1372 strncat(channel->ban_list, tmp, tmp_len);
1373 strncat(channel->ban_list, ",", 1);
1376 /* Get the ban to be removed and remove it from the list */
1377 tmp = silc_argument_get_arg_type(args, 3, &tmp_len);
1378 if (tmp && channel->ban_list) {
1379 char *start, *end, *n;
1381 if (!strncmp(channel->ban_list, tmp, strlen(channel->ban_list) - 1)) {
1382 silc_free(channel->ban_list);
1383 channel->ban_list = NULL;
1385 start = strstr(channel->ban_list, tmp);
1386 if (start && strlen(start) >= tmp_len) {
1387 end = start + tmp_len;
1388 n = silc_calloc(strlen(channel->ban_list) - tmp_len, sizeof(*n));
1389 strncat(n, channel->ban_list, start - channel->ban_list);
1390 strncat(n, end + 1, ((channel->ban_list +
1391 strlen(channel->ban_list)) - end) - 1);
1392 silc_free(channel->ban_list);
1393 channel->ban_list = n;
1399 case SILC_NOTIFY_TYPE_ERROR:
1406 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
1407 if (!tmp && tmp_len != 1)
1409 error = (SilcStatus)tmp[0];
1411 SILC_LOG_DEBUG(("ERROR notify (%d)", error));
1413 if (error == SILC_STATUS_ERR_NO_SUCH_CLIENT_ID &&
1414 sock->type == SILC_SOCKET_TYPE_ROUTER) {
1415 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
1417 SILC_LOG_DEBUG(("Received invalid client ID notification, deleting "
1418 "the entry from cache"));
1419 client_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
1422 client = silc_idlist_find_client_by_id(server->global_list,
1423 client_id, FALSE, NULL);
1425 silc_server_remove_from_channels(server, NULL, client, TRUE,
1427 silc_idlist_del_client(server->global_list, client);
1429 silc_free(client_id);
1435 /* Ignore rest of the notify types for now */
1436 case SILC_NOTIFY_TYPE_NONE:
1437 case SILC_NOTIFY_TYPE_MOTD:
1444 silc_notify_payload_free(payload);
1447 void silc_server_notify_list(SilcServer server,
1448 SilcSocketConnection sock,
1449 SilcPacketContext *packet)
1451 SilcPacketContext *new;
1455 SILC_LOG_DEBUG(("Processing Notify List"));
1457 if (sock->type == SILC_SOCKET_TYPE_CLIENT ||
1458 packet->src_id_type != SILC_ID_SERVER)
1461 /* Make copy of the original packet context, except for the actual
1462 data buffer, which we will here now fetch from the original buffer. */
1463 new = silc_packet_context_alloc();
1464 new->type = SILC_PACKET_NOTIFY;
1465 new->flags = packet->flags;
1466 new->src_id = packet->src_id;
1467 new->src_id_len = packet->src_id_len;
1468 new->src_id_type = packet->src_id_type;
1469 new->dst_id = packet->dst_id;
1470 new->dst_id_len = packet->dst_id_len;
1471 new->dst_id_type = packet->dst_id_type;
1473 buffer = silc_buffer_alloc(1024);
1474 new->buffer = buffer;
1476 while (packet->buffer->len) {
1477 SILC_GET16_MSB(len, packet->buffer->data + 2);
1478 if (len > packet->buffer->len)
1481 if (len > buffer->truelen) {
1482 silc_buffer_free(buffer);
1483 buffer = silc_buffer_alloc(1024 + len);
1486 silc_buffer_pull_tail(buffer, len);
1487 silc_buffer_put(buffer, packet->buffer->data, len);
1489 /* Process the Notify */
1490 silc_server_notify(server, sock, new);
1492 silc_buffer_push_tail(buffer, len);
1493 silc_buffer_pull(packet->buffer, len);
1496 silc_buffer_free(buffer);
1500 /* Received private message. This resolves the destination of the message
1501 and sends the packet. This is used by both server and router. If the
1502 destination is our locally connected client this sends the packet to
1503 the client. This may also send the message for further routing if
1504 the destination is not in our server (or router). */
1506 void silc_server_private_message(SilcServer server,
1507 SilcSocketConnection sock,
1508 SilcPacketContext *packet)
1510 SilcSocketConnection dst_sock;
1511 SilcIDListData idata;
1512 SilcClientEntry client;
1514 SILC_LOG_DEBUG(("Start"));
1516 if (packet->src_id_type != SILC_ID_CLIENT ||
1517 packet->dst_id_type != SILC_ID_CLIENT || !packet->dst_id)
1520 /* Get the route to the client */
1521 dst_sock = silc_server_get_client_route(server, packet->dst_id,
1522 packet->dst_id_len, NULL,
1526 unsigned char error;
1528 if (client && client->mode & SILC_UMODE_DETACHED) {
1529 SILC_LOG_DEBUG(("Client is detached, discarding packet"));
1533 /* Send SILC_NOTIFY_TYPE_ERROR to indicate that such destination ID
1534 does not exist or is invalid. */
1535 idp = silc_id_payload_encode_data(packet->dst_id,
1537 packet->dst_id_type);
1541 error = SILC_STATUS_ERR_NO_SUCH_CLIENT_ID;
1542 if (packet->src_id_type == SILC_ID_CLIENT) {
1543 SilcClientID *client_id = silc_id_str2id(packet->src_id,
1545 packet->src_id_type);
1546 silc_server_send_notify_dest(server, sock, FALSE,
1547 client_id, SILC_ID_CLIENT,
1548 SILC_NOTIFY_TYPE_ERROR, 2,
1550 idp->data, idp->len);
1551 silc_free(client_id);
1553 silc_server_send_notify(server, sock, FALSE,
1554 SILC_NOTIFY_TYPE_ERROR, 2,
1556 idp->data, idp->len);
1559 silc_buffer_free(idp);
1563 /* Check whether destination client wishes to receive private messages */
1564 if (client && !(packet->flags & SILC_PACKET_FLAG_PRIVMSG_KEY) &&
1565 client->mode & SILC_UMODE_BLOCK_PRIVMSG) {
1566 SILC_LOG_DEBUG(("Client blocks private messages, discarding packet"));
1570 /* Send the private message */
1571 silc_server_send_private_message(server, dst_sock, idata->send_key,
1572 idata->hmac_send, idata->psn_send++,
1576 /* Received private message key packet.. This packet is never for us. It is to
1577 the client in the packet's destination ID. Sending of this sort of packet
1578 equals sending private message, ie. it is sent point to point from
1579 one client to another. */
1581 void silc_server_private_message_key(SilcServer server,
1582 SilcSocketConnection sock,
1583 SilcPacketContext *packet)
1585 SilcSocketConnection dst_sock;
1586 SilcIDListData idata;
1588 SILC_LOG_DEBUG(("Start"));
1590 if (packet->src_id_type != SILC_ID_CLIENT ||
1591 packet->dst_id_type != SILC_ID_CLIENT)
1594 if (!packet->dst_id)
1597 /* Get the route to the client */
1598 dst_sock = silc_server_get_client_route(server, packet->dst_id,
1599 packet->dst_id_len, NULL,
1604 /* Relay the packet */
1605 silc_server_relay_packet(server, dst_sock, idata->send_key,
1606 idata->hmac_send, idata->psn_send++, packet, FALSE);
1609 /* Processes incoming command reply packet. The command reply packet may
1610 be destined to one of our clients or it may directly for us. We will
1611 call the command reply routine after processing the packet. */
1613 void silc_server_command_reply(SilcServer server,
1614 SilcSocketConnection sock,
1615 SilcPacketContext *packet)
1617 SilcBuffer buffer = packet->buffer;
1618 SilcClientEntry client = NULL;
1619 SilcSocketConnection dst_sock;
1620 SilcIDListData idata;
1621 SilcClientID *id = NULL;
1623 SILC_LOG_DEBUG(("Start"));
1625 /* Source must be server or router */
1626 if (packet->src_id_type != SILC_ID_SERVER &&
1627 sock->type != SILC_SOCKET_TYPE_ROUTER)
1630 if (packet->dst_id_type == SILC_ID_CHANNEL)
1633 if (packet->dst_id_type == SILC_ID_CLIENT) {
1634 /* Destination must be one of ours */
1635 id = silc_id_str2id(packet->dst_id, packet->dst_id_len, SILC_ID_CLIENT);
1638 client = silc_idlist_find_client_by_id(server->local_list, id, TRUE, NULL);
1640 SILC_LOG_ERROR(("Cannot process command reply to unknown client"));
1646 if (packet->dst_id_type == SILC_ID_SERVER) {
1647 /* For now this must be for us */
1648 if (memcmp(packet->dst_id, server->id_string, server->id_string_len)) {
1649 SILC_LOG_ERROR(("Cannot process command reply to unknown server"));
1654 /* Execute command reply locally for the command */
1655 silc_server_command_reply_process(server, sock, buffer);
1657 if (packet->dst_id_type == SILC_ID_CLIENT && client && id) {
1658 /* Relay the packet to the client */
1659 const SilcBufferStruct p;
1661 dst_sock = (SilcSocketConnection)client->connection;
1662 idata = (SilcIDListData)client;
1664 silc_buffer_push(buffer, SILC_PACKET_HEADER_LEN + packet->src_id_len
1665 + packet->dst_id_len + packet->padlen);
1666 if (!silc_packet_send_prepare(dst_sock, 0, 0, buffer->len,
1667 idata->hmac_send, (const SilcBuffer)&p)) {
1668 SILC_LOG_ERROR(("Cannot send packet"));
1671 silc_buffer_put((SilcBuffer)&p, buffer->data, buffer->len);
1673 /* Encrypt packet */
1674 silc_packet_encrypt(idata->send_key, idata->hmac_send, idata->psn_send++,
1675 (SilcBuffer)&p, buffer->len);
1677 /* Send the packet */
1678 silc_server_packet_send_real(server, dst_sock, TRUE);
1684 /* Process received channel message. The message can be originated from
1685 client or server. */
1687 void silc_server_channel_message(SilcServer server,
1688 SilcSocketConnection sock,
1689 SilcPacketContext *packet)
1691 SilcChannelEntry channel = NULL;
1692 SilcChannelID *id = NULL;
1693 void *sender_id = NULL;
1694 SilcClientEntry sender_entry = NULL;
1695 SilcChannelClientEntry chl;
1698 SILC_LOG_DEBUG(("Processing channel message"));
1701 if (packet->dst_id_type != SILC_ID_CHANNEL) {
1702 SILC_LOG_DEBUG(("Received bad message for channel, dropped"));
1706 /* Find channel entry */
1707 id = silc_id_str2id(packet->dst_id, packet->dst_id_len, SILC_ID_CHANNEL);
1710 channel = silc_idlist_find_channel_by_id(server->local_list, id, NULL);
1712 channel = silc_idlist_find_channel_by_id(server->global_list, id, NULL);
1715 unsigned char error;
1717 /* Send SILC_NOTIFY_TYPE_ERROR to indicate that such destination ID
1718 does not exist or is invalid. */
1719 idp = silc_id_payload_encode_data(packet->dst_id,
1721 packet->dst_id_type);
1725 error = SILC_STATUS_ERR_NO_SUCH_CHANNEL_ID;
1726 if (packet->src_id_type == SILC_ID_CLIENT) {
1727 SilcClientID *client_id = silc_id_str2id(packet->src_id,
1729 packet->src_id_type);
1730 silc_server_send_notify_dest(server, sock, FALSE,
1731 client_id, SILC_ID_CLIENT,
1732 SILC_NOTIFY_TYPE_ERROR, 2,
1733 &error, 1, idp->data, idp->len);
1734 silc_free(client_id);
1736 silc_server_send_notify(server, sock, FALSE,
1737 SILC_NOTIFY_TYPE_ERROR, 2,
1738 &error, 1, idp->data, idp->len);
1741 silc_buffer_free(idp);
1746 /* See that this client is on the channel. If the original sender is
1747 not client (as it can be server as well) we don't do the check. */
1748 sender_id = silc_id_str2id(packet->src_id, packet->src_id_len,
1749 packet->src_id_type);
1752 if (packet->src_id_type == SILC_ID_CLIENT) {
1753 sender_entry = silc_idlist_find_client_by_id(server->local_list,
1754 sender_id, TRUE, NULL);
1755 if (!sender_entry) {
1757 sender_entry = silc_idlist_find_client_by_id(server->global_list,
1758 sender_id, TRUE, NULL);
1760 if (!sender_entry || !silc_server_client_on_channel(sender_entry,
1762 SILC_LOG_DEBUG(("Client not on channel"));
1766 /* If channel is moderated check that client is allowed to send
1768 if (channel->mode & SILC_CHANNEL_MODE_SILENCE_USERS &&
1769 !(chl->mode & SILC_CHANNEL_UMODE_CHANOP) &&
1770 !(chl->mode & SILC_CHANNEL_UMODE_CHANFO)) {
1771 SILC_LOG_DEBUG(("Channel is silenced from normal users"));
1774 if (channel->mode & SILC_CHANNEL_MODE_SILENCE_OPERS &&
1775 chl->mode & SILC_CHANNEL_UMODE_CHANOP &&
1776 !(chl->mode & SILC_CHANNEL_UMODE_CHANFO)) {
1777 SILC_LOG_DEBUG(("Channel is silenced from operators"));
1781 /* If the packet is coming from router, but the client entry is local
1782 entry to us then some router is rerouting this to us and it is not
1783 allowed. When the client is local to us it means that we've routed
1784 this packet to network, and now someone is routing it back to us. */
1785 if (server->server_type == SILC_ROUTER &&
1786 sock->type == SILC_SOCKET_TYPE_ROUTER && local) {
1787 SILC_LOG_DEBUG(("Channel message rerouted to the sender, drop it"));
1792 /* Distribute the packet to our local clients. This will send the
1793 packet for further routing as well, if needed. */
1794 silc_server_packet_relay_to_channel(server, sock, channel, sender_id,
1795 packet->src_id_type, sender_entry,
1796 packet->buffer->data,
1797 packet->buffer->len, FALSE);
1800 silc_free(sender_id);
1804 /* Received channel key packet. We distribute the key to all of our locally
1805 connected clients on the channel. */
1807 void silc_server_channel_key(SilcServer server,
1808 SilcSocketConnection sock,
1809 SilcPacketContext *packet)
1811 SilcBuffer buffer = packet->buffer;
1812 SilcChannelEntry channel;
1814 if (packet->src_id_type != SILC_ID_SERVER ||
1815 (server->server_type == SILC_ROUTER &&
1816 sock->type == SILC_SOCKET_TYPE_ROUTER))
1819 /* Save the channel key */
1820 channel = silc_server_save_channel_key(server, buffer, NULL);
1824 /* Distribute the key to everybody who is on the channel. If we are router
1825 we will also send it to locally connected servers. */
1826 silc_server_send_channel_key(server, sock, channel, FALSE);
1828 if (server->server_type != SILC_BACKUP_ROUTER) {
1829 /* Distribute to local cell backup routers. */
1830 silc_server_backup_send(server, (SilcServerEntry)sock->user_data,
1831 SILC_PACKET_CHANNEL_KEY, 0,
1832 buffer->data, buffer->len, FALSE, TRUE);
1836 /* Received New Client packet and processes it. Creates Client ID for the
1837 client. Client becomes registered after calling this functions. */
1839 SilcClientEntry silc_server_new_client(SilcServer server,
1840 SilcSocketConnection sock,
1841 SilcPacketContext *packet)
1843 SilcBuffer buffer = packet->buffer;
1844 SilcClientEntry client;
1845 SilcClientID *client_id;
1846 SilcIDListData idata;
1847 char *username = NULL, *realname = NULL;
1848 SilcUInt16 username_len;
1851 char *hostname, *nickname;
1854 SILC_LOG_DEBUG(("Creating new client"));
1856 if (sock->type != SILC_SOCKET_TYPE_CLIENT)
1859 /* Take client entry */
1860 client = (SilcClientEntry)sock->user_data;
1861 idata = (SilcIDListData)client;
1863 /* Remove the old cache entry. */
1864 if (!silc_idcache_del_by_context(server->local_list->clients, client)) {
1865 SILC_LOG_INFO(("Unauthenticated client attempted to register to network"));
1866 silc_server_disconnect_remote(server, sock,
1867 SILC_STATUS_ERR_NOT_AUTHENTICATED, NULL);
1871 /* Parse incoming packet */
1872 ret = silc_buffer_unformat(buffer,
1873 SILC_STR_UI16_NSTRING_ALLOC(&username,
1875 SILC_STR_UI16_STRING_ALLOC(&realname),
1878 silc_free(username);
1879 silc_free(realname);
1880 SILC_LOG_ERROR(("Client %s (%s) sent incomplete information, closing "
1881 "connection", sock->hostname, sock->ip));
1882 silc_server_disconnect_remote(server, sock,
1883 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
1889 silc_free(username);
1890 silc_free(realname);
1891 SILC_LOG_ERROR(("Client %s (%s) did not send its username, closing "
1892 "connection", sock->hostname, sock->ip));
1893 silc_server_disconnect_remote(server, sock,
1894 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
1899 if (username_len > 128)
1900 username[128] = '\0';
1902 /* Check for bad characters for nickname, and modify the nickname if
1903 it includes those. */
1904 if (silc_server_name_bad_chars(username, username_len)) {
1905 nickname = silc_server_name_modify_bad(username, username_len);
1907 nickname = strdup(username);
1910 /* Make sanity checks for the hostname of the client. If the hostname
1911 is provided in the `username' check that it is the same than the
1912 resolved hostname, or if not resolved the hostname that appears in
1913 the client's public key. If the hostname is not present then put
1914 it from the resolved name or from the public key. */
1915 if (strchr(username, '@')) {
1916 SilcPublicKeyIdentifier pident;
1917 int tlen = strcspn(username, "@");
1918 char *phostname = NULL;
1920 hostname = silc_memdup(username + tlen + 1, strlen(username) - tlen - 1);
1922 if (strcmp(sock->hostname, sock->ip) &&
1923 strcmp(sock->hostname, hostname)) {
1924 silc_free(username);
1925 silc_free(hostname);
1926 silc_free(realname);
1927 SILC_LOG_ERROR(("Client %s (%s) sent incomplete information, closing "
1928 "connection", sock->hostname, sock->ip));
1929 silc_server_disconnect_remote(server, sock,
1930 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
1935 pident = silc_pkcs_decode_identifier(client->data.public_key->identifier);
1937 phostname = strdup(pident->host);
1938 silc_pkcs_free_identifier(pident);
1941 if (!strcmp(sock->hostname, sock->ip) &&
1942 phostname && strcmp(phostname, hostname)) {
1943 silc_free(username);
1944 silc_free(hostname);
1945 silc_free(phostname);
1946 silc_free(realname);
1947 SILC_LOG_ERROR(("Client %s (%s) sent incomplete information, closing "
1948 "connection", sock->hostname, sock->ip));
1949 silc_server_disconnect_remote(server, sock,
1950 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
1955 silc_free(phostname);
1957 /* The hostname is not present, add it. */
1959 /* XXX For now we cannot take the host name from the public key since
1960 they are not trusted or we cannot verify them as trusted. Just take
1961 what the resolved name or address is. */
1963 if (strcmp(sock->hostname, sock->ip)) {
1965 newusername = silc_calloc(strlen(username) +
1966 strlen(sock->hostname) + 2,
1967 sizeof(*newusername));
1968 strncat(newusername, username, strlen(username));
1969 strncat(newusername, "@", 1);
1970 strncat(newusername, sock->hostname, strlen(sock->hostname));
1971 silc_free(username);
1972 username = newusername;
1975 SilcPublicKeyIdentifier pident =
1976 silc_pkcs_decode_identifier(client->data.public_key->identifier);
1979 newusername = silc_calloc(strlen(username) +
1980 strlen(pident->host) + 2,
1981 sizeof(*newusername));
1982 strncat(newusername, username, strlen(username));
1983 strncat(newusername, "@", 1);
1984 strncat(newusername, pident->host, strlen(pident->host));
1985 silc_free(username);
1986 username = newusername;
1987 silc_pkcs_free_identifier(pident);
1993 /* Create Client ID */
1994 while (!silc_id_create_client_id(server, server->id, server->rng,
1995 server->md5hash, nickname, &client_id)) {
1998 silc_server_disconnect_remote(server, sock,
1999 SILC_STATUS_ERR_BAD_NICKNAME, NULL);
2002 snprintf(&nickname[strlen(nickname) - 1], 1, "%d", nickfail);
2005 /* Update client entry */
2006 idata->status |= SILC_IDLIST_STATUS_REGISTERED;
2007 client->nickname = nickname;
2008 client->username = username;
2009 client->userinfo = realname ? realname : strdup(" ");
2010 client->id = client_id;
2011 id_len = silc_id_get_len(client_id, SILC_ID_CLIENT);
2013 /* Add the client again to the ID cache */
2014 silc_idcache_add(server->local_list->clients, client->nickname,
2015 client_id, client, 0, NULL);
2017 /* Notify our router about new client on the SILC network */
2018 if (!server->standalone)
2019 silc_server_send_new_id(server, (SilcSocketConnection)
2020 server->router->connection,
2021 server->server_type == SILC_ROUTER ? TRUE : FALSE,
2022 client->id, SILC_ID_CLIENT, id_len);
2024 /* Send the new client ID to the client. */
2025 silc_server_send_new_id(server, sock, FALSE, client->id, SILC_ID_CLIENT,
2026 silc_id_get_len(client->id, SILC_ID_CLIENT));
2028 /* Send some nice info to the client */
2029 silc_server_send_connect_notifys(server, sock, client);
2031 /* Check if anyone is watching this nickname */
2032 if (server->server_type == SILC_ROUTER)
2033 silc_server_check_watcher_list(server, client, NULL, 0);
2038 /* Create new server. This processes received New Server packet and
2039 saves the received Server ID. The server is our locally connected
2040 server thus we save all the information and save it to local list.
2041 This funtion can be used by both normal server and router server.
2042 If normal server uses this it means that its router has connected
2043 to the server. If router uses this it means that one of the cell's
2044 servers is connected to the router. */
2046 SilcServerEntry silc_server_new_server(SilcServer server,
2047 SilcSocketConnection sock,
2048 SilcPacketContext *packet)
2050 SilcBuffer buffer = packet->buffer;
2051 SilcServerEntry new_server, server_entry;
2052 SilcServerID *server_id;
2053 SilcIDListData idata;
2054 unsigned char *server_name, *id_string;
2055 SilcUInt16 id_len, name_len;
2059 SILC_LOG_DEBUG(("Creating new server"));
2061 if (sock->type != SILC_SOCKET_TYPE_SERVER &&
2062 sock->type != SILC_SOCKET_TYPE_ROUTER)
2065 /* Take server entry */
2066 new_server = (SilcServerEntry)sock->user_data;
2067 idata = (SilcIDListData)new_server;
2069 /* Remove the old cache entry */
2070 if (!silc_idcache_del_by_context(server->local_list->servers, new_server)) {
2071 if (!silc_idcache_del_by_context(server->global_list->servers,
2073 SILC_LOG_INFO(("Unauthenticated %s attempted to register to "
2074 "network", (sock->type == SILC_SOCKET_TYPE_SERVER ?
2075 "server" : "router")));
2076 silc_server_disconnect_remote(server, sock,
2077 SILC_STATUS_ERR_NOT_AUTHENTICATED, NULL);
2083 /* Parse the incoming packet */
2084 ret = silc_buffer_unformat(buffer,
2085 SILC_STR_UI16_NSTRING_ALLOC(&id_string, &id_len),
2086 SILC_STR_UI16_NSTRING_ALLOC(&server_name,
2091 silc_free(id_string);
2093 silc_free(server_name);
2097 if (id_len > buffer->len) {
2098 silc_free(id_string);
2099 silc_free(server_name);
2104 server_name[255] = '\0';
2107 server_id = silc_id_str2id(id_string, id_len, SILC_ID_SERVER);
2109 silc_free(id_string);
2110 silc_free(server_name);
2113 silc_free(id_string);
2115 /* Check for valid server ID */
2116 if (!silc_id_is_valid_server_id(server, server_id, sock)) {
2117 SILC_LOG_INFO(("Invalid server ID sent by %s (%s)",
2118 sock->ip, sock->hostname));
2119 silc_server_disconnect_remote(server, sock,
2120 SILC_STATUS_ERR_BAD_SERVER_ID, NULL);
2121 silc_free(server_name);
2125 /* Check that we do not have this ID already */
2126 server_entry = silc_idlist_find_server_by_id(server->local_list,
2127 server_id, TRUE, NULL);
2129 silc_idcache_del_by_context(server->local_list->servers, server_entry);
2131 server_entry = silc_idlist_find_server_by_id(server->global_list,
2132 server_id, TRUE, NULL);
2134 silc_idcache_del_by_context(server->global_list->servers, server_entry);
2137 /* Update server entry */
2138 idata->status |= SILC_IDLIST_STATUS_REGISTERED;
2139 new_server->server_name = server_name;
2140 new_server->id = server_id;
2142 SILC_LOG_DEBUG(("New server id(%s)",
2143 silc_id_render(server_id, SILC_ID_SERVER)));
2145 /* Add again the entry to the ID cache. */
2146 silc_idcache_add(local ? server->local_list->servers :
2147 server->global_list->servers, server_name, server_id,
2148 new_server, 0, NULL);
2150 /* Distribute the information about new server in the SILC network
2151 to our router. If we are normal server we won't send anything
2152 since this connection must be our router connection. */
2153 if (server->server_type == SILC_ROUTER && !server->standalone &&
2154 server->router->connection != sock)
2155 silc_server_send_new_id(server, server->router->connection,
2156 TRUE, new_server->id, SILC_ID_SERVER,
2157 silc_id_get_len(server_id, SILC_ID_SERVER));
2159 if (server->server_type == SILC_ROUTER)
2160 server->stat.cell_servers++;
2162 /* Check whether this router connection has been replaced by an
2163 backup router. If it has been then we'll disable the server and will
2164 ignore everything it will send until the backup router resuming
2165 protocol has been completed. */
2166 if (sock->type == SILC_SOCKET_TYPE_ROUTER &&
2167 silc_server_backup_replaced_get(server, server_id, NULL)) {
2168 /* Send packet to the server indicating that it cannot use this
2169 connection as it has been replaced by backup router. */
2170 SilcBuffer packet = silc_buffer_alloc(2);
2171 silc_buffer_pull_tail(packet, SILC_BUFFER_END(packet));
2172 silc_buffer_format(packet,
2173 SILC_STR_UI_CHAR(SILC_SERVER_BACKUP_REPLACED),
2174 SILC_STR_UI_CHAR(0),
2176 silc_server_packet_send(server, sock,
2177 SILC_PACKET_RESUME_ROUTER, 0,
2178 packet->data, packet->len, TRUE);
2179 silc_buffer_free(packet);
2181 /* Mark the router disabled. The data sent earlier will go but nothing
2182 after this does not go to this connection. */
2183 idata->status |= SILC_IDLIST_STATUS_DISABLED;
2185 /* If it is router announce our stuff to it. */
2186 if (sock->type == SILC_SOCKET_TYPE_ROUTER &&
2187 server->server_type == SILC_ROUTER) {
2188 silc_server_announce_servers(server, FALSE, 0, sock);
2189 silc_server_announce_clients(server, 0, sock);
2190 silc_server_announce_channels(server, 0, sock);
2197 /* Processes incoming New ID packet. New ID Payload is used to distribute
2198 information about newly registered clients and servers. */
2200 static void silc_server_new_id_real(SilcServer server,
2201 SilcSocketConnection sock,
2202 SilcPacketContext *packet,
2205 SilcBuffer buffer = packet->buffer;
2207 SilcServerEntry router, server_entry;
2208 SilcSocketConnection router_sock;
2213 SILC_LOG_DEBUG(("Processing new ID"));
2215 if (sock->type == SILC_SOCKET_TYPE_CLIENT ||
2216 server->server_type == SILC_SERVER ||
2217 packet->src_id_type != SILC_ID_SERVER)
2220 idp = silc_id_payload_parse(buffer->data, buffer->len);
2224 id_type = silc_id_payload_get_type(idp);
2226 /* Normal server cannot have other normal server connections */
2227 server_entry = (SilcServerEntry)sock->user_data;
2228 if (id_type == SILC_ID_SERVER && sock->type == SILC_SOCKET_TYPE_SERVER &&
2229 server_entry->server_type == SILC_SERVER)
2232 id = silc_id_payload_get_id(idp);
2236 /* If the packet is coming from server then use the sender as the
2237 origin of the the packet. If it came from router then check the real
2238 sender of the packet and use that as the origin. */
2239 if (sock->type == SILC_SOCKET_TYPE_SERVER) {
2240 id_list = server->local_list;
2242 router = sock->user_data;
2244 /* If the sender is backup router and ID is server (and we are not
2245 backup router) then switch the entry to global list. */
2246 if (server_entry->server_type == SILC_BACKUP_ROUTER &&
2247 id_type == SILC_ID_SERVER &&
2248 server->id_entry->server_type != SILC_BACKUP_ROUTER) {
2249 id_list = server->global_list;
2250 router_sock = server->router ? server->router->connection : sock;
2253 void *sender_id = silc_id_str2id(packet->src_id, packet->src_id_len,
2254 packet->src_id_type);
2255 router = silc_idlist_find_server_by_id(server->global_list,
2256 sender_id, TRUE, NULL);
2258 router = silc_idlist_find_server_by_id(server->local_list,
2259 sender_id, TRUE, NULL);
2260 silc_free(sender_id);
2262 id_list = server->global_list;
2269 case SILC_ID_CLIENT:
2271 SilcClientEntry entry;
2273 /* Check that we do not have this client already */
2274 entry = silc_idlist_find_client_by_id(server->global_list,
2275 id, server->server_type,
2278 entry = silc_idlist_find_client_by_id(server->local_list,
2279 id, server->server_type,
2282 SILC_LOG_DEBUG(("Ignoring client that we already have"));
2286 SILC_LOG_DEBUG(("New client id(%s) from [%s] %s",
2287 silc_id_render(id, SILC_ID_CLIENT),
2288 sock->type == SILC_SOCKET_TYPE_SERVER ?
2289 "Server" : "Router", sock->hostname));
2291 /* As a router we keep information of all global information in our
2292 global list. Cell wide information however is kept in the local
2294 entry = silc_idlist_add_client(id_list, NULL, NULL, NULL,
2295 id, router, NULL, 0);
2297 SILC_LOG_ERROR(("Could not add new client to the ID Cache"));
2299 /* Inform the sender that the ID is not usable */
2300 silc_server_send_notify_signoff(server, sock, FALSE, id, NULL);
2303 entry->nickname = NULL;
2304 entry->data.status |= SILC_IDLIST_STATUS_REGISTERED;
2306 if (sock->type == SILC_SOCKET_TYPE_SERVER)
2307 server->stat.cell_clients++;
2308 server->stat.clients++;
2310 /* Check if anyone is watching this nickname */
2311 if (server->server_type == SILC_ROUTER && id_list == server->local_list)
2312 silc_server_check_watcher_list(server, entry, NULL, 0);
2316 case SILC_ID_SERVER:
2318 SilcServerEntry entry;
2320 /* If the ID is mine, ignore it. */
2321 if (SILC_ID_SERVER_COMPARE(id, server->id)) {
2322 SILC_LOG_DEBUG(("Ignoring my own ID as new ID"));
2326 /* If the ID is the sender's ID, ignore it (we have it already) */
2327 if (SILC_ID_SERVER_COMPARE(id, router->id)) {
2328 SILC_LOG_DEBUG(("Ignoring sender's own ID"));
2332 /* Check that we do not have this server already */
2333 entry = silc_idlist_find_server_by_id(server->global_list,
2334 id, server->server_type,
2337 entry = silc_idlist_find_server_by_id(server->local_list,
2338 id, server->server_type,
2341 SILC_LOG_DEBUG(("Ignoring server that we already have"));
2345 SILC_LOG_DEBUG(("New server id(%s) from [%s] %s",
2346 silc_id_render(id, SILC_ID_SERVER),
2347 sock->type == SILC_SOCKET_TYPE_SERVER ?
2348 "Server" : "Router", sock->hostname));
2350 /* As a router we keep information of all global information in our
2351 global list. Cell wide information however is kept in the local
2353 entry = silc_idlist_add_server(id_list, NULL, 0, id, router,
2356 SILC_LOG_ERROR(("Could not add new server to the ID Cache"));
2359 entry->data.status |= SILC_IDLIST_STATUS_REGISTERED;
2361 if (sock->type == SILC_SOCKET_TYPE_SERVER)
2362 server->stat.cell_servers++;
2363 server->stat.servers++;
2367 case SILC_ID_CHANNEL:
2368 SILC_LOG_ERROR(("Channel cannot be registered with NEW_ID packet"));
2377 /* If the sender of this packet is server and we are router we need to
2378 broadcast this packet to other routers in the network. */
2379 if (broadcast && !server->standalone && server->server_type == SILC_ROUTER &&
2380 sock->type == SILC_SOCKET_TYPE_SERVER &&
2381 !(packet->flags & SILC_PACKET_FLAG_BROADCAST)) {
2382 SILC_LOG_DEBUG(("Broadcasting received New ID packet"));
2383 silc_server_packet_send(server, server->router->connection,
2385 packet->flags | SILC_PACKET_FLAG_BROADCAST,
2386 buffer->data, buffer->len, FALSE);
2387 silc_server_backup_send(server, (SilcServerEntry)sock->user_data,
2388 packet->type, packet->flags,
2389 packet->buffer->data, packet->buffer->len,
2394 silc_id_payload_free(idp);
2398 /* Processes incoming New ID packet. New ID Payload is used to distribute
2399 information about newly registered clients and servers. */
2401 void silc_server_new_id(SilcServer server, SilcSocketConnection sock,
2402 SilcPacketContext *packet)
2404 silc_server_new_id_real(server, sock, packet, TRUE);
2407 /* Receoved New Id List packet, list of New ID payloads inside one
2408 packet. Process the New ID payloads one by one. */
2410 void silc_server_new_id_list(SilcServer server, SilcSocketConnection sock,
2411 SilcPacketContext *packet)
2413 SilcPacketContext *new_id;
2417 SILC_LOG_DEBUG(("Processing New ID List"));
2419 if (sock->type == SILC_SOCKET_TYPE_CLIENT ||
2420 packet->src_id_type != SILC_ID_SERVER)
2423 /* If the sender of this packet is server and we are router we need to
2424 broadcast this packet to other routers in the network. Broadcast
2425 this list packet instead of multiple New ID packets. */
2426 if (!server->standalone && server->server_type == SILC_ROUTER &&
2427 sock->type == SILC_SOCKET_TYPE_SERVER &&
2428 !(packet->flags & SILC_PACKET_FLAG_BROADCAST)) {
2429 SILC_LOG_DEBUG(("Broadcasting received New ID List packet"));
2430 silc_server_packet_send(server, server->router->connection,
2432 packet->flags | SILC_PACKET_FLAG_BROADCAST,
2433 packet->buffer->data, packet->buffer->len, FALSE);
2434 silc_server_backup_send(server, (SilcServerEntry)sock->user_data,
2435 packet->type, packet->flags,
2436 packet->buffer->data, packet->buffer->len,
2440 /* Make copy of the original packet context, except for the actual
2441 data buffer, which we will here now fetch from the original buffer. */
2442 new_id = silc_packet_context_alloc();
2443 new_id->type = SILC_PACKET_NEW_ID;
2444 new_id->flags = packet->flags;
2445 new_id->src_id = packet->src_id;
2446 new_id->src_id_len = packet->src_id_len;
2447 new_id->src_id_type = packet->src_id_type;
2448 new_id->dst_id = packet->dst_id;
2449 new_id->dst_id_len = packet->dst_id_len;
2450 new_id->dst_id_type = packet->dst_id_type;
2452 idp = silc_buffer_alloc(256);
2453 new_id->buffer = idp;
2455 while (packet->buffer->len) {
2456 SILC_GET16_MSB(id_len, packet->buffer->data + 2);
2457 if ((id_len > packet->buffer->len) ||
2458 (id_len > idp->truelen))
2461 silc_buffer_pull_tail(idp, 4 + id_len);
2462 silc_buffer_put(idp, packet->buffer->data, 4 + id_len);
2464 /* Process the New ID */
2465 silc_server_new_id_real(server, sock, new_id, FALSE);
2467 silc_buffer_push_tail(idp, 4 + id_len);
2468 silc_buffer_pull(packet->buffer, 4 + id_len);
2471 silc_buffer_free(idp);
2475 /* Received New Channel packet. Information about new channels in the
2476 network are distributed using this packet. Save the information about
2477 the new channel. This usually comes from router but also normal server
2478 can send this to notify channels it has when it connects to us. */
2480 void silc_server_new_channel(SilcServer server,
2481 SilcSocketConnection sock,
2482 SilcPacketContext *packet)
2484 SilcChannelPayload payload;
2485 SilcChannelID *channel_id;
2487 SilcUInt32 name_len;
2491 SilcServerEntry server_entry;
2492 SilcChannelEntry channel;
2494 SILC_LOG_DEBUG(("Processing New Channel"));
2496 if (sock->type == SILC_SOCKET_TYPE_CLIENT ||
2497 packet->src_id_type != SILC_ID_SERVER ||
2498 server->server_type == SILC_SERVER)
2501 /* Parse the channel payload */
2502 payload = silc_channel_payload_parse(packet->buffer->data,
2503 packet->buffer->len);
2507 /* Get the channel ID */
2508 channel_id = silc_channel_get_id_parse(payload);
2510 silc_channel_payload_free(payload);
2514 channel_name = silc_channel_get_name(payload, &name_len);
2516 channel_name[255] = '\0';
2518 id = silc_channel_get_id(payload, &id_len);
2520 server_entry = (SilcServerEntry)sock->user_data;
2522 if (sock->type == SILC_SOCKET_TYPE_ROUTER) {
2523 /* Add the channel to global list as it is coming from router. It
2524 cannot be our own channel as it is coming from router. */
2526 /* Check that we don't already have this channel */
2527 channel = silc_idlist_find_channel_by_name(server->local_list,
2528 channel_name, NULL);
2530 channel = silc_idlist_find_channel_by_name(server->global_list,
2531 channel_name, NULL);
2533 SILC_LOG_DEBUG(("New channel id(%s) from [Router] %s",
2534 silc_id_render(channel_id, SILC_ID_CHANNEL),
2538 silc_idlist_add_channel(server->global_list, strdup(channel_name),
2539 0, channel_id, sock->user_data, NULL, NULL, 0);
2543 server->stat.channels++;
2544 if (server->server_type == SILC_ROUTER)
2545 channel->users_resolved = TRUE;
2548 /* The channel is coming from our server, thus it is in our cell
2549 we will add it to our local list. */
2552 SILC_LOG_DEBUG(("Channel id(%s) from [Server] %s",
2553 silc_id_render(channel_id, SILC_ID_CHANNEL),
2556 /* Check that we don't already have this channel */
2557 channel = silc_idlist_find_channel_by_name(server->local_list,
2558 channel_name, NULL);
2560 channel = silc_idlist_find_channel_by_name(server->global_list,
2561 channel_name, NULL);
2563 /* If the channel does not exist, then create it. This creates a new
2564 key to the channel as well that we will send to the server. */
2566 /* The protocol says that the Channel ID's IP address must be based
2567 on the router's IP address. Check whether the ID is based in our
2568 IP and if it is not then create a new ID and enforce the server
2569 to switch the ID. */
2570 if (server_entry->server_type != SILC_BACKUP_ROUTER &&
2571 !SILC_ID_COMPARE(channel_id, server->id, server->id->ip.data_len)) {
2573 SILC_LOG_DEBUG(("Forcing the server to change Channel ID"));
2575 if (silc_id_create_channel_id(server, server->id, server->rng, &tmp)) {
2576 silc_server_send_notify_channel_change(server, sock, FALSE,
2578 silc_free(channel_id);
2583 /* Create the channel with the provided Channel ID */
2584 channel = silc_server_create_new_channel_with_id(server, NULL, NULL,
2588 silc_channel_payload_free(payload);
2589 silc_free(channel_id);
2593 /* Get the mode and set it to the channel */
2594 channel->mode = silc_channel_get_mode(payload);
2596 /* Send the new channel key to the server */
2597 id = silc_id_id2str(channel->id, SILC_ID_CHANNEL);
2598 id_len = silc_id_get_len(channel->id, SILC_ID_CHANNEL);
2599 chk = silc_channel_key_payload_encode(id_len, id,
2600 strlen(channel->channel_key->
2602 channel->channel_key->cipher->name,
2603 channel->key_len / 8,
2605 silc_server_packet_send(server, sock, SILC_PACKET_CHANNEL_KEY, 0,
2606 chk->data, chk->len, FALSE);
2607 silc_buffer_free(chk);
2610 /* The channel exist by that name, check whether the ID's match.
2611 If they don't then we'll force the server to use the ID we have.
2612 We also create a new key for the channel. */
2613 SilcBuffer users = NULL, users_modes = NULL;
2615 if (!SILC_ID_CHANNEL_COMPARE(channel_id, channel->id)) {
2616 /* They don't match, send CHANNEL_CHANGE notify to the server to
2617 force the ID change. */
2618 SILC_LOG_DEBUG(("Forcing the server to change Channel ID"));
2619 silc_server_send_notify_channel_change(server, sock, FALSE,
2620 channel_id, channel->id);
2623 /* If the mode is different from what we have then enforce the
2625 mode = silc_channel_get_mode(payload);
2626 if (channel->mode != mode) {
2627 SILC_LOG_DEBUG(("Forcing the server to change channel mode"));
2628 silc_server_send_notify_cmode(server, sock, FALSE, channel,
2629 channel->mode, server->id,
2631 channel->cipher, channel->hmac_name,
2632 channel->passphrase);
2635 /* Create new key for the channel and send it to the server and
2636 everybody else possibly on the channel. */
2638 if (!(channel->mode & SILC_CHANNEL_MODE_PRIVKEY)) {
2639 if (!silc_server_create_channel_key(server, channel, 0))
2642 /* Send to the channel */
2643 silc_server_send_channel_key(server, sock, channel, FALSE);
2644 id = silc_id_id2str(channel->id, SILC_ID_CHANNEL);
2645 id_len = silc_id_get_len(channel->id, SILC_ID_CHANNEL);
2647 /* Send to the server */
2648 chk = silc_channel_key_payload_encode(id_len, id,
2649 strlen(channel->channel_key->
2651 channel->channel_key->
2653 channel->key_len / 8,
2655 silc_server_packet_send(server, sock, SILC_PACKET_CHANNEL_KEY, 0,
2656 chk->data, chk->len, FALSE);
2657 silc_buffer_free(chk);
2661 silc_free(channel_id);
2663 /* Since the channel is coming from server and we also know about it
2664 then send the JOIN notify to the server so that it see's our
2665 users on the channel "joining" the channel. */
2666 silc_server_announce_get_channel_users(server, channel, &users,
2669 silc_buffer_push(users, users->data - users->head);
2670 silc_server_packet_send(server, sock,
2671 SILC_PACKET_NOTIFY, SILC_PACKET_FLAG_LIST,
2672 users->data, users->len, FALSE);
2673 silc_buffer_free(users);
2676 silc_buffer_push(users_modes, users_modes->data - users_modes->head);
2677 silc_server_packet_send_dest(server, sock,
2678 SILC_PACKET_NOTIFY, SILC_PACKET_FLAG_LIST,
2679 channel->id, SILC_ID_CHANNEL,
2681 users_modes->len, FALSE);
2682 silc_buffer_free(users_modes);
2687 silc_channel_payload_free(payload);
2690 /* Received New Channel List packet, list of New Channel List payloads inside
2691 one packet. Process the New Channel payloads one by one. */
2693 void silc_server_new_channel_list(SilcServer server,
2694 SilcSocketConnection sock,
2695 SilcPacketContext *packet)
2697 SilcPacketContext *new;
2699 SilcUInt16 len1, len2;
2701 SILC_LOG_DEBUG(("Processing New Channel List"));
2703 if (sock->type == SILC_SOCKET_TYPE_CLIENT ||
2704 packet->src_id_type != SILC_ID_SERVER ||
2705 server->server_type == SILC_SERVER)
2708 /* If the sender of this packet is server and we are router we need to
2709 broadcast this packet to other routers in the network. Broadcast
2710 this list packet instead of multiple New Channel packets. */
2711 if (!server->standalone && server->server_type == SILC_ROUTER &&
2712 sock->type == SILC_SOCKET_TYPE_SERVER &&
2713 !(packet->flags & SILC_PACKET_FLAG_BROADCAST)) {
2714 SILC_LOG_DEBUG(("Broadcasting received New Channel List packet"));
2715 silc_server_packet_send(server, server->router->connection,
2717 packet->flags | SILC_PACKET_FLAG_BROADCAST,
2718 packet->buffer->data, packet->buffer->len, FALSE);
2719 silc_server_backup_send(server, (SilcServerEntry)sock->user_data,
2720 packet->type, packet->flags,
2721 packet->buffer->data, packet->buffer->len,
2725 /* Make copy of the original packet context, except for the actual
2726 data buffer, which we will here now fetch from the original buffer. */
2727 new = silc_packet_context_alloc();
2728 new->type = SILC_PACKET_NEW_CHANNEL;
2729 new->flags = packet->flags;
2730 new->src_id = packet->src_id;
2731 new->src_id_len = packet->src_id_len;
2732 new->src_id_type = packet->src_id_type;
2733 new->dst_id = packet->dst_id;
2734 new->dst_id_len = packet->dst_id_len;
2735 new->dst_id_type = packet->dst_id_type;
2737 buffer = silc_buffer_alloc(512);
2738 new->buffer = buffer;
2740 while (packet->buffer->len) {
2741 SILC_GET16_MSB(len1, packet->buffer->data);
2742 if ((len1 > packet->buffer->len) ||
2743 (len1 > buffer->truelen))
2746 SILC_GET16_MSB(len2, packet->buffer->data + 2 + len1);
2747 if ((len2 > packet->buffer->len) ||
2748 (len2 > buffer->truelen))
2751 silc_buffer_pull_tail(buffer, 8 + len1 + len2);
2752 silc_buffer_put(buffer, packet->buffer->data, 8 + len1 + len2);
2754 /* Process the New Channel */
2755 silc_server_new_channel(server, sock, new);
2757 silc_buffer_push_tail(buffer, 8 + len1 + len2);
2758 silc_buffer_pull(packet->buffer, 8 + len1 + len2);
2761 silc_buffer_free(buffer);
2765 /* Received key agreement packet. This packet is never for us. It is to
2766 the client in the packet's destination ID. Sending of this sort of packet
2767 equals sending private message, ie. it is sent point to point from
2768 one client to another. */
2770 void silc_server_key_agreement(SilcServer server,
2771 SilcSocketConnection sock,
2772 SilcPacketContext *packet)
2774 SilcSocketConnection dst_sock;
2775 SilcIDListData idata;
2777 SILC_LOG_DEBUG(("Start"));
2779 if (packet->src_id_type != SILC_ID_CLIENT ||
2780 packet->dst_id_type != SILC_ID_CLIENT)
2783 if (!packet->dst_id)
2786 /* Get the route to the client */
2787 dst_sock = silc_server_get_client_route(server, packet->dst_id,
2788 packet->dst_id_len, NULL,
2793 /* Relay the packet */
2794 silc_server_relay_packet(server, dst_sock, idata->send_key,
2795 idata->hmac_send, idata->psn_send++,
2799 /* Received connection auth request packet that is used during connection
2800 phase to resolve the mandatory authentication method. This packet can
2801 actually be received at anytime but usually it is used only during
2802 the connection authentication phase. Now, protocol says that this packet
2803 can come from client or server, however, we support only this coming
2804 from client and expect that server always knows what authentication
2807 void silc_server_connection_auth_request(SilcServer server,
2808 SilcSocketConnection sock,
2809 SilcPacketContext *packet)
2811 SilcServerConfigClient *client = NULL;
2812 SilcUInt16 conn_type;
2814 SilcAuthMethod auth_meth = SILC_AUTH_NONE;
2816 SILC_LOG_DEBUG(("Start"));
2818 if (packet->src_id_type && packet->src_id_type != SILC_ID_CLIENT)
2821 /* Parse the payload */
2822 ret = silc_buffer_unformat(packet->buffer,
2823 SILC_STR_UI_SHORT(&conn_type),
2824 SILC_STR_UI_SHORT(NULL),
2829 if (conn_type != SILC_SOCKET_TYPE_CLIENT)
2832 /* Get the authentication method for the client */
2833 auth_meth = SILC_AUTH_NONE;
2834 client = silc_server_config_find_client(server, sock->ip);
2836 client = silc_server_config_find_client(server, sock->hostname);
2838 if (client->passphrase) {
2839 if (client->publickeys && !server->config->prefer_passphrase_auth)
2840 auth_meth = SILC_AUTH_PUBLIC_KEY;
2842 auth_meth = SILC_AUTH_PASSWORD;
2843 } else if (client->publickeys)
2844 auth_meth = SILC_AUTH_PUBLIC_KEY;
2847 /* Send it back to the client */
2848 silc_server_send_connection_auth_request(server, sock, conn_type, auth_meth);
2851 /* Received REKEY packet. The sender of the packet wants to regenerate
2852 its session keys. This starts the REKEY protocol. */
2854 void silc_server_rekey(SilcServer server,
2855 SilcSocketConnection sock,
2856 SilcPacketContext *packet)
2858 SilcProtocol protocol;
2859 SilcServerRekeyInternalContext *proto_ctx;
2860 SilcIDListData idata = (SilcIDListData)sock->user_data;
2862 SILC_LOG_DEBUG(("Start"));
2864 /* Allocate internal protocol context. This is sent as context
2866 proto_ctx = silc_calloc(1, sizeof(*proto_ctx));
2867 proto_ctx->server = (void *)server;
2868 proto_ctx->sock = sock;
2869 proto_ctx->responder = TRUE;
2870 proto_ctx->pfs = idata->rekey->pfs;
2872 /* Perform rekey protocol. Will call the final callback after the
2873 protocol is over. */
2874 silc_protocol_alloc(SILC_PROTOCOL_SERVER_REKEY,
2875 &protocol, proto_ctx, silc_server_rekey_final);
2876 sock->protocol = protocol;
2878 if (proto_ctx->pfs == FALSE)
2879 /* Run the protocol */
2880 silc_protocol_execute(protocol, server->schedule, 0, 0);
2883 /* Received file transger packet. This packet is never for us. It is to
2884 the client in the packet's destination ID. Sending of this sort of packet
2885 equals sending private message, ie. it is sent point to point from
2886 one client to another. */
2888 void silc_server_ftp(SilcServer server,
2889 SilcSocketConnection sock,
2890 SilcPacketContext *packet)
2892 SilcSocketConnection dst_sock;
2893 SilcIDListData idata;
2895 SILC_LOG_DEBUG(("Start"));
2897 if (packet->src_id_type != SILC_ID_CLIENT ||
2898 packet->dst_id_type != SILC_ID_CLIENT)
2901 if (!packet->dst_id)
2904 /* Get the route to the client */
2905 dst_sock = silc_server_get_client_route(server, packet->dst_id,
2906 packet->dst_id_len, NULL,
2911 /* Relay the packet */
2912 silc_server_relay_packet(server, dst_sock, idata->send_key,
2913 idata->hmac_send, idata->psn_send++,
2919 SilcSocketConnection sock;
2920 SilcPacketContext *packet;
2922 } *SilcServerResumeResolve;
2924 SILC_SERVER_CMD_FUNC(resume_resolve)
2926 SilcServerResumeResolve r = (SilcServerResumeResolve)context;
2927 SilcServer server = r->server;
2928 SilcSocketConnection sock = r->sock;
2929 SilcServerCommandReplyContext reply = context2;
2930 SilcClientEntry client;
2932 SILC_LOG_DEBUG(("Start"));
2934 if (!reply || !silc_command_get_status(reply->payload, NULL, NULL)) {
2935 SILC_LOG_ERROR(("Client %s (%s) tried to resume unknown client, "
2936 "closing connection", sock->hostname, sock->ip));
2937 silc_server_disconnect_remote(server, sock,
2938 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
2939 "Resuming not possible");
2943 if (reply && silc_command_get(reply->payload) == SILC_COMMAND_WHOIS) {
2944 /* Get entry to the client, and resolve it if we don't have it. */
2945 client = silc_idlist_find_client_by_id(server->local_list,
2946 r->data, TRUE, NULL);
2948 client = silc_idlist_find_client_by_id(server->global_list,
2949 r->data, TRUE, NULL);
2951 SILC_LOG_ERROR(("Client %s (%s) tried to resume unknown client, "
2952 "closing connection", sock->hostname, sock->ip));
2953 silc_server_disconnect_remote(server, sock,
2954 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
2955 "Resuming not possible");
2960 if (!(client->mode & SILC_UMODE_DETACHED)) {
2961 SILC_LOG_ERROR(("Client %s (%s) tried to resume un-detached client, "
2962 "closing connection", sock->hostname, sock->ip));
2963 silc_server_disconnect_remote(server, sock,
2964 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
2965 "Resuming not possible");
2970 /* Reprocess the packet */
2971 silc_server_resume_client(server, sock, r->packet);
2974 silc_socket_free(r->sock);
2975 silc_packet_context_free(r->packet);
2980 /* Received client resuming packet. This is used to resume detached
2981 client session. It can be sent by the client who wishes to resume
2982 but this is also sent by servers and routers to notify other routers
2983 that the client is not detached anymore. */
2985 void silc_server_resume_client(SilcServer server,
2986 SilcSocketConnection sock,
2987 SilcPacketContext *packet)
2989 SilcBuffer buffer = packet->buffer, buf;
2990 SilcIDListData idata;
2991 SilcClientEntry detached_client;
2992 SilcClientID *client_id = NULL;
2993 unsigned char *id_string, *auth = NULL;
2994 SilcUInt16 id_len, auth_len = 0;
2995 int ret, nickfail = 0;
2996 bool resolved, local, nick_change = FALSE;
2997 SilcChannelEntry channel;
2998 SilcHashTableList htl;
2999 SilcChannelClientEntry chl;
3000 SilcServerResumeResolve r;
3002 SILC_LOG_DEBUG(("Start"));
3004 ret = silc_buffer_unformat(buffer,
3005 SILC_STR_UI16_NSTRING(&id_string, &id_len),
3008 client_id = silc_id_str2id(id_string, id_len, SILC_ID_CLIENT);
3010 if (sock->type == SILC_SOCKET_TYPE_CLIENT) {
3011 /* Client send this and is attempting to resume to old client session */
3012 SilcClientEntry client;
3016 silc_buffer_pull(buffer, 2 + id_len);
3017 auth = buffer->data;
3018 auth_len = buffer->len;
3019 silc_buffer_push(buffer, 2 + id_len);
3022 if (!client_id || auth_len < 128) {
3023 SILC_LOG_ERROR(("Client %s (%s) sent incomplete resume information, "
3024 "closing connection", sock->hostname, sock->ip));
3025 silc_server_disconnect_remote(server, sock,
3026 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
3027 "Resuming not possible");
3031 /* Take client entry of this connection */
3032 client = (SilcClientEntry)sock->user_data;
3033 idata = (SilcIDListData)client;
3035 /* Get entry to the client, and resolve it if we don't have it. */
3036 detached_client = silc_server_get_client_resolve(server, client_id, FALSE,
3038 if (!detached_client) {
3040 /* The client info is being resolved. Reprocess this packet after
3041 receiving the reply to the query. */
3042 SILC_LOG_DEBUG(("Resolving client"));
3043 r = silc_calloc(1, sizeof(*r));
3047 r->sock = silc_socket_dup(sock);
3048 r->packet = silc_packet_context_dup(packet);
3049 r->data = silc_id_dup(client_id, SILC_ID_CLIENT);
3050 silc_server_command_pending(server, SILC_COMMAND_WHOIS,
3052 silc_server_command_resume_resolve, r);
3054 SILC_LOG_ERROR(("Client %s (%s) tried to resume unknown client, "
3055 "closing connection", sock->hostname, sock->ip));
3056 silc_server_disconnect_remote(server, sock,
3057 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
3058 "Resuming not possible");
3063 /* Check that the client is detached, and that we have other info too */
3064 if (!(detached_client->mode & SILC_UMODE_DETACHED) ||
3065 !silc_hash_table_count(detached_client->channels) ||
3066 !detached_client->nickname) {
3067 if (server->server_type == SILC_SERVER && !server->standalone) {
3068 /* The client info is being resolved. Reprocess this packet after
3069 receiving the reply to the query. */
3070 SILC_LOG_DEBUG(("Resolving client info"));
3071 silc_server_get_client_resolve(server, client_id, TRUE, NULL);
3072 r = silc_calloc(1, sizeof(*r));
3076 r->sock = silc_socket_dup(sock);
3077 r->packet = silc_packet_context_dup(packet);
3078 r->data = silc_id_dup(client_id, SILC_ID_CLIENT);
3079 silc_server_command_pending(server, SILC_COMMAND_WHOIS,
3081 silc_server_command_resume_resolve, r);
3084 if (server->server_type == SILC_SERVER) {
3085 SILC_LOG_ERROR(("Client %s (%s) tried to resume un-detached client, "
3086 "closing connection", sock->hostname, sock->ip));
3087 silc_server_disconnect_remote(server, sock,
3088 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
3089 "Resuming not possible");
3094 /* Check that we have the public key of the client, if not then we must
3095 resolve it first. */
3096 if (!detached_client->data.public_key) {
3097 if (server->standalone) {
3098 silc_server_disconnect_remote(server, sock,
3099 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
3100 "Resuming not possible");
3102 /* We must retrieve the detached client's public key by sending
3103 GETKEY command. Reprocess this packet after receiving the key */
3104 SilcBuffer idp = silc_id_payload_encode(client_id, SILC_ID_CLIENT);
3105 SilcSocketConnection dest_sock =
3106 silc_server_get_client_route(server, NULL, 0, client_id, NULL, NULL);
3108 SILC_LOG_DEBUG(("Resolving client public key"));
3110 silc_server_send_command(server, dest_sock ? dest_sock :
3111 server->router->connection,
3112 SILC_COMMAND_GETKEY, ++server->cmd_ident,
3113 1, 1, idp->data, idp->len);
3115 r = silc_calloc(1, sizeof(*r));
3120 r->sock = silc_socket_dup(sock);
3121 r->packet = silc_packet_context_dup(packet);
3122 silc_server_command_pending(server, SILC_COMMAND_GETKEY,
3124 silc_server_command_resume_resolve, r);
3126 silc_buffer_free(idp);
3129 } else if (!silc_pkcs_public_key_compare(detached_client->data.public_key,
3130 idata->public_key)) {
3131 /* We require that the connection and resuming authentication data
3132 must be using same key pair. */
3133 silc_server_disconnect_remote(server, sock,
3134 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
3135 "Resuming not possible");
3139 /* Verify the authentication payload. This has to be successful in
3140 order to allow the resuming */
3141 if (!silc_auth_verify_data(auth, auth_len, SILC_AUTH_PUBLIC_KEY,
3142 detached_client->data.public_key, 0,
3143 idata->hash, detached_client->id,
3145 SILC_LOG_ERROR(("Client %s (%s) resume authentication failed, "
3146 "closing connection", sock->hostname, sock->ip));
3147 silc_server_disconnect_remote(server, sock,
3148 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
3149 "Resuming not possible");
3153 /* Now resume the client to the network */
3155 silc_schedule_task_del_by_context(server->schedule, detached_client);
3156 sock->user_data = detached_client;
3157 detached_client->connection = sock;
3159 /* Take new keys and stuff into use in the old entry */
3160 silc_idlist_del_data(detached_client);
3161 silc_idlist_add_data(detached_client, idata);
3162 detached_client->data.status |= SILC_IDLIST_STATUS_REGISTERED;
3163 detached_client->data.status |= SILC_IDLIST_STATUS_RESUMED;
3164 detached_client->mode &= ~SILC_UMODE_DETACHED;
3166 /* Send the RESUME_CLIENT packet to our primary router so that others
3167 know this client isn't detached anymore. */
3168 buf = silc_buffer_alloc_size(2 + id_len);
3169 silc_buffer_format(buf,
3170 SILC_STR_UI_SHORT(id_len),
3171 SILC_STR_UI_XNSTRING(id_string, id_len),
3174 /* Send to primary router */
3175 if (!server->standalone)
3176 silc_server_packet_send(server, server->router->connection,
3177 SILC_PACKET_RESUME_CLIENT, 0,
3178 buf->data, buf->len, TRUE);
3180 /* As router we must deliver this packet directly to the original
3181 server whom this client was earlier. */
3182 if (server->server_type == SILC_ROUTER && detached_client->router &&
3183 detached_client->router->server_type != SILC_ROUTER)
3184 silc_server_packet_send(server, detached_client->router->connection,
3185 SILC_PACKET_RESUME_CLIENT, 0,
3186 buf->data, buf->len, TRUE);
3187 silc_buffer_free(buf);
3189 detached_client->router = NULL;
3191 /* Delete this client entry since we're resuming to old one. */
3192 server->stat.my_clients--;
3193 server->stat.clients--;
3194 if (server->stat.cell_clients)
3195 server->stat.cell_clients--;
3196 silc_idlist_del_client(server->local_list, client);
3197 client = detached_client;
3199 /* If the ID is not based in our ID then change it */
3200 if (!SILC_ID_COMPARE(client->id, server->id, server->id->ip.data_len)) {
3201 while (!silc_id_create_client_id(server, server->id, server->rng,
3202 server->md5hash, client->nickname,
3206 silc_server_disconnect_remote(server, sock,
3207 SILC_STATUS_ERR_BAD_NICKNAME, NULL);
3210 snprintf(&client->nickname[strlen(client->nickname) - 1], 1,
3217 /* Notify about Client ID change, nickname doesn't actually change. */
3218 if (!server->standalone)
3219 silc_server_send_notify_nick_change(server, server->router->connection,
3220 FALSE, client->id, client_id,
3224 /* Resolve users on those channels that client has joined but we
3225 haven't resolved user list yet. */
3226 if (server->server_type == SILC_SERVER && !server->standalone) {
3227 silc_hash_table_list(client->channels, &htl);
3228 while (silc_hash_table_get(&htl, NULL, (void **)&chl)) {
3229 channel = chl->channel;
3230 SILC_LOG_DEBUG(("Resolving users for %s channel",
3231 channel->channel_name));
3232 if (channel->disabled || !channel->users_resolved) {
3233 silc_server_send_command(server, server->router->connection,
3234 SILC_COMMAND_USERS, ++server->cmd_ident,
3235 1, 2, channel->channel_name,
3236 strlen(channel->channel_name));
3239 silc_hash_table_list_reset(&htl);
3242 /* Send the new client ID to the client. After this client may start
3243 receiving other packets, and may start sending packets too. */
3244 silc_server_send_new_id(server, sock, FALSE, client_id, SILC_ID_CLIENT,
3245 silc_id_get_len(client_id, SILC_ID_CLIENT));
3248 /* Send NICK change notify to channels as well. */
3249 SilcBuffer oidp, nidp;
3250 oidp = silc_id_payload_encode(client->id, SILC_ID_CLIENT);
3251 nidp = silc_id_payload_encode(client_id, SILC_ID_CLIENT);
3252 silc_server_send_notify_on_channels(server, NULL, client,
3253 SILC_NOTIFY_TYPE_NICK_CHANGE, 3,
3254 oidp->data, oidp->len,
3255 nidp->data, nidp->len,
3257 strlen(client->nickname));
3258 silc_buffer_free(oidp);
3259 silc_buffer_free(nidp);
3262 /* Add the client again to the ID cache to get it to correct list */
3263 if (!silc_idcache_del_by_context(server->local_list->clients, client))
3264 silc_idcache_del_by_context(server->global_list->clients, client);
3265 silc_free(client->id);
3266 client->id = client_id;
3268 silc_idcache_add(server->local_list->clients, client->nickname,
3269 client->id, client, 0, NULL);
3271 /* Send some nice info to the client */
3272 silc_server_send_connect_notifys(server, sock, client);
3274 /* Send all channel keys of channels the client has joined */
3275 silc_hash_table_list(client->channels, &htl);
3276 while (silc_hash_table_get(&htl, NULL, (void **)&chl)) {
3277 bool created = FALSE;
3278 channel = chl->channel;
3280 if (channel->mode & SILC_CHANNEL_MODE_PRIVKEY)
3283 /* If we don't have channel key, then create one */
3284 if (!channel->channel_key) {
3285 if (!silc_server_create_channel_key(server, channel, 0))
3290 id_string = silc_id_id2str(channel->id, SILC_ID_CHANNEL);
3292 silc_channel_key_payload_encode(silc_id_get_len(channel->id,
3295 strlen(channel->channel_key->
3297 channel->channel_key->cipher->name,
3298 channel->key_len / 8, channel->key);
3299 silc_free(id_string);
3301 /* Send the key packet to client */
3302 silc_server_packet_send(server, sock, SILC_PACKET_CHANNEL_KEY, 0,
3303 keyp->data, keyp->len, FALSE);
3305 if (created && server->server_type == SILC_SERVER &&
3306 !server->standalone)
3307 silc_server_packet_send(server, server->router->connection,
3308 SILC_PACKET_CHANNEL_KEY, 0,
3309 keyp->data, keyp->len, FALSE);
3311 silc_buffer_free(keyp);
3313 silc_hash_table_list_reset(&htl);
3315 } else if (sock->type != SILC_SOCKET_TYPE_CLIENT) {
3316 /* Server or router sent this to us to notify that that a client has
3318 SilcServerEntry server_entry;
3319 SilcServerID *server_id;
3324 /* Get entry to the client, and resolve it if we don't have it. */
3325 detached_client = silc_idlist_find_client_by_id(server->local_list,
3326 client_id, TRUE, NULL);
3327 if (!detached_client) {
3328 detached_client = silc_idlist_find_client_by_id(server->global_list,
3329 client_id, TRUE, NULL);
3330 if (!detached_client)
3334 /* Check that the client has not been resumed already because it is
3335 protocol error to attempt to resume more than once. The client
3336 will be killed if this protocol error occurs. */
3337 if (detached_client->data.status & SILC_IDLIST_STATUS_RESUMED &&
3338 !(detached_client->mode & SILC_UMODE_DETACHED)) {
3339 /* The client is clearly attempting to resume more than once and
3340 perhaps playing around by resuming from several different places
3341 at the same time. */
3342 silc_server_kill_client(server, detached_client, NULL,
3343 server->id, SILC_ID_SERVER);
3347 /* Check whether client is detached at all */
3348 if (!(detached_client->mode & SILC_UMODE_DETACHED))
3351 /* Client is detached, and now it is resumed. Remove the detached
3352 mode and mark that it is resumed. */
3353 detached_client->mode &= ~SILC_UMODE_DETACHED;
3354 detached_client->data.status |= SILC_IDLIST_STATUS_RESUMED;
3356 /* Get the new owner of the resumed client */
3357 server_id = silc_id_str2id(packet->src_id, packet->src_id_len,
3358 packet->src_id_type);
3362 /* Get server entry */
3363 server_entry = silc_idlist_find_server_by_id(server->global_list,
3364 server_id, TRUE, NULL);
3366 if (!server_entry) {
3367 server_entry = silc_idlist_find_server_by_id(server->local_list,
3368 server_id, TRUE, NULL);
3370 if (!server_entry) {
3371 silc_free(server_id);
3376 if (server->server_type == SILC_ROUTER &&
3377 sock->type == SILC_SOCKET_TYPE_ROUTER &&
3378 server_entry->server_type == SILC_ROUTER)
3381 SILC_LOG_DEBUG(("Resuming detached client"));
3383 /* Change the client to correct list. */
3384 if (!silc_idcache_del_by_context(server->local_list->clients,
3386 silc_idcache_del_by_context(server->global_list->clients,
3388 silc_idcache_add(local && server->server_type == SILC_ROUTER ?
3389 server->local_list->clients :
3390 server->global_list->clients,
3391 detached_client->nickname,
3392 detached_client->id, detached_client, FALSE, NULL);
3394 /* Change the owner of the client if needed */
3395 if (detached_client->router != server_entry)
3396 detached_client->router = server_entry;
3398 /* Update channel information regarding global clients on channel. */
3399 if (server->server_type == SILC_SERVER) {
3400 silc_hash_table_list(detached_client->channels, &htl);
3401 while (silc_hash_table_get(&htl, NULL, (void **)&chl))
3402 chl->channel->global_users =
3403 silc_server_channel_has_global(chl->channel);
3404 silc_hash_table_list_reset(&htl);
3407 silc_schedule_task_del_by_context(server->schedule, detached_client);
3409 /* If the sender of this packet is server and we are router we need to
3410 broadcast this packet to other routers in the network. */
3411 if (!server->standalone && server->server_type == SILC_ROUTER &&
3412 sock->type == SILC_SOCKET_TYPE_SERVER &&
3413 !(packet->flags & SILC_PACKET_FLAG_BROADCAST)) {
3414 SILC_LOG_DEBUG(("Broadcasting received Resume Client packet"));
3415 silc_server_packet_send(server, server->router->connection,
3417 packet->flags | SILC_PACKET_FLAG_BROADCAST,
3418 buffer->data, buffer->len, FALSE);
3419 silc_server_backup_send(server, (SilcServerEntry)sock->user_data,
3420 packet->type, packet->flags,
3421 packet->buffer->data, packet->buffer->len,
3425 silc_free(server_id);
3428 silc_free(client_id);