5 Author: Pekka Riikonen <priikone@silcnet.org>
7 Copyright (C) 1997 - 2007 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 #include "serverincludes.h"
22 #include "server_internal.h"
24 /* Received notify packet. Server can receive notify packets from router.
25 Server then relays the notify messages to clients if needed. */
27 static void silc_server_notify_process(SilcServer server,
28 SilcPacketStream sock,
32 SilcIDListData idata = silc_packet_get_context(sock);
33 SilcNotifyPayload payload;
35 SilcArgumentPayload args;
36 SilcChannelID channel_id;
38 SilcChannelEntry channel = NULL;
39 SilcClientEntry client = NULL, client2 = NULL;
40 SilcServerEntry server_entry = NULL;
41 SilcChannelClientEntry chl;
42 SilcIDCacheEntry cache = NULL;
43 SilcHashTableList htl;
45 unsigned char *tmp, *tmp2;
46 SilcUInt32 tmp_len, tmp2_len;
49 if (idata->conn_type == SILC_CONN_CLIENT ||
50 packet->src_id_type != SILC_ID_SERVER || !packet->dst_id) {
51 SILC_LOG_DEBUG(("Bad notify packet received"));
55 /* If the packet is destined directly to a client then relay the packet
56 before processing it. */
57 if (packet->dst_id_type == SILC_ID_CLIENT) {
59 SilcPacketStream dst_sock;
61 /* Get the route to the client */
62 dst_sock = silc_server_get_client_route(server, packet->dst_id,
63 packet->dst_id_len, NULL,
66 /* Relay the packet */
67 silc_server_packet_route(server, dst_sock, packet);
70 /* Parse the Notify Payload */
71 payload = silc_notify_payload_parse(buffer->data, silc_buffer_len(buffer));
75 /* If we are router and this packet is not already broadcast packet
76 we will broadcast it. The sending socket really cannot be router or
77 the router is buggy. If this packet is coming from router then it must
78 have the broadcast flag set already and we won't do anything. */
79 if (server->server_type == SILC_ROUTER &&
80 idata->conn_type == SILC_CONN_SERVER &&
81 !(packet->flags & SILC_PACKET_FLAG_BROADCAST)) {
82 SILC_LOG_DEBUG(("Broadcasting received Notify packet"));
83 if (packet->dst_id_type == SILC_ID_CHANNEL) {
84 /* Packet is destined to channel */
85 if (!silc_id_str2id(packet->dst_id, packet->dst_id_len,
86 packet->dst_id_type, &channel_id,
90 silc_server_packet_send_dest(server, SILC_PRIMARY_ROUTE(server),
91 packet->type, packet->flags |
92 SILC_PACKET_FLAG_BROADCAST,
93 &channel_id, SILC_ID_CHANNEL,
94 buffer->data, silc_buffer_len(buffer));
95 silc_server_backup_send_dest(server, (SilcServerEntry)idata,
96 packet->type, packet->flags,
97 &channel_id, SILC_ID_CHANNEL,
98 buffer->data, silc_buffer_len(buffer),
101 /* Packet is destined to client or server */
102 silc_server_packet_send(server, SILC_PRIMARY_ROUTE(server),
104 packet->flags | SILC_PACKET_FLAG_BROADCAST,
105 buffer->data, silc_buffer_len(buffer));
106 silc_server_backup_send(server, (SilcServerEntry)idata,
107 packet->type, packet->flags,
108 buffer->data, silc_buffer_len(buffer),
113 type = silc_notify_get_type(payload);
114 args = silc_notify_get_args(payload);
119 case SILC_NOTIFY_TYPE_JOIN:
121 * Distribute the notify to local clients on the channel
123 SILC_LOG_DEBUG(("JOIN notify"));
126 if (!silc_argument_get_decoded(args, 2, SILC_ARGUMENT_ID, &id, NULL))
129 /* Get channel entry */
130 channel = silc_idlist_find_channel_by_id(server->global_list,
131 SILC_ID_GET_ID(id), NULL);
133 channel = silc_idlist_find_channel_by_id(server->local_list,
134 SILC_ID_GET_ID(id), NULL);
136 SILC_LOG_DEBUG(("Notify for unknown channel"));
142 if (!silc_argument_get_decoded(args, 1, SILC_ARGUMENT_ID, &id, NULL))
145 /* If the the client is not in local list we check global list (ie. the
146 channel will be global channel) and if it does not exist then create
147 entry for the client. */
148 client = silc_idlist_find_client_by_id(server->global_list,
153 client = silc_idlist_find_client_by_id(server->local_list,
158 /* If router did not find the client the it is bogus */
159 if (server->server_type != SILC_SERVER)
163 silc_idlist_add_client(server->global_list, NULL, NULL, NULL,
164 silc_id_dup(SILC_ID_GET_ID(id),
166 (SilcServerEntry)idata, NULL);
168 SILC_LOG_ERROR(("Could not add new client to the ID Cache"));
172 client->data.status |= SILC_IDLIST_STATUS_REGISTERED;
176 /* Do not process the notify if the client is not registered */
177 if (!(client->data.status & SILC_IDLIST_STATUS_REGISTERED))
180 /* Do not add client to channel if it is there already */
181 if (silc_server_client_on_channel(client, channel, NULL)) {
182 SILC_LOG_DEBUG(("Client already on channel %s",
183 channel->channel_name));
187 /* Send to channel */
188 silc_server_packet_send_to_channel(server, sock, channel, packet->type,
189 FALSE, TRUE, buffer->data,
190 silc_buffer_len(buffer));
192 if (server->server_type != SILC_ROUTER &&
193 idata->conn_type == SILC_CONN_ROUTER)
194 /* The channel is global now */
195 channel->global_users = TRUE;
197 SILC_LOG_DEBUG(("Joining to channel %s", channel->channel_name));
199 /* JOIN the global client to the channel (local clients (if router
200 created the channel) is joined in the pending JOIN command). */
201 chl = silc_calloc(1, sizeof(*chl));
204 chl->client = client;
205 chl->channel = channel;
207 if (server->server_type != SILC_ROUTER ||
208 idata->conn_type == SILC_CONN_ROUTER) {
209 /* If founder auth is set, first client is not automatically founder. */
210 if (!(channel->mode & SILC_CHANNEL_MODE_FOUNDER_AUTH)) {
211 /* If this is the first one on the channel then it is the founder of
212 the channel. This is done on normal server and on router if this
213 notify is coming from router */
214 if (!silc_hash_table_count(channel->user_list)) {
215 SILC_LOG_DEBUG(("Client %s is founder on channel",
216 silc_id_render(chl->client->id, SILC_ID_CLIENT)));
217 chl->mode = (SILC_CHANNEL_UMODE_CHANOP | SILC_CHANNEL_UMODE_CHANFO);
222 silc_hash_table_add(channel->user_list, client, chl);
223 silc_hash_table_add(client->channels, channel, chl);
224 channel->user_count++;
225 channel->disabled = FALSE;
227 /* Update statistics */
228 if (server->server_type == SILC_ROUTER) {
229 if (idata->conn_type != SILC_CONN_ROUTER)
230 server->stat.cell_chanclients++;
231 server->stat.chanclients++;
236 case SILC_NOTIFY_TYPE_LEAVE:
238 * Distribute the notify to local clients on the channel
240 SILC_LOG_DEBUG(("LEAVE notify"));
242 if (!silc_id_str2id(packet->dst_id, packet->dst_id_len,
243 packet->dst_id_type, &channel_id,
247 /* Get channel entry */
248 channel = silc_idlist_find_channel_by_id(server->global_list,
251 channel = silc_idlist_find_channel_by_id(server->local_list,
254 SILC_LOG_DEBUG(("Notify for unknown channel"));
260 if (!silc_argument_get_decoded(args, 1, SILC_ARGUMENT_ID, &id, NULL))
263 /* Get client entry */
264 client = silc_idlist_find_client_by_id(server->global_list,
265 SILC_ID_GET_ID(id), TRUE, NULL);
267 client = silc_idlist_find_client_by_id(server->local_list,
268 SILC_ID_GET_ID(id), TRUE, NULL);
273 /* Check if on channel */
274 if (!silc_server_client_on_channel(client, channel, NULL))
277 /* Send the leave notify to channel */
278 silc_server_packet_send_to_channel(server, sock, channel, packet->type,
279 FALSE, TRUE, buffer->data,
280 silc_buffer_len(buffer));
282 /* Remove the user from channel */
283 silc_server_remove_from_one_channel(server, sock, channel, client, FALSE);
286 case SILC_NOTIFY_TYPE_SIGNOFF:
288 * Distribute the notify to local clients on the channel
290 SILC_LOG_DEBUG(("SIGNOFF notify"));
293 if (!silc_argument_get_decoded(args, 1, SILC_ARGUMENT_ID, &id, NULL))
296 /* Get client entry */
297 client = silc_idlist_find_client_by_id(server->global_list,
298 SILC_ID_GET_ID(id), TRUE, &cache);
300 client = silc_idlist_find_client_by_id(server->local_list,
301 SILC_ID_GET_ID(id), TRUE, &cache);
306 /* Get signoff message */
307 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
311 /* Update statistics */
312 server->stat.clients--;
313 if (server->stat.cell_clients)
314 server->stat.cell_clients--;
315 SILC_OPER_STATS_UPDATE(client, server, SILC_UMODE_SERVER_OPERATOR);
316 SILC_OPER_STATS_UPDATE(client, router, SILC_UMODE_ROUTER_OPERATOR);
317 silc_schedule_task_del_by_context(server->schedule, client);
319 /* Remove client's public key from repository, this will free it too. */
320 if (client->data.public_key) {
321 silc_skr_del_public_key(server->repository, client->data.public_key,
323 client->data.public_key = NULL;
326 /* Remove the client from all channels. */
327 silc_server_remove_from_channels(server, NULL, client, TRUE,
330 /* Check if anyone is watching this nickname */
331 if (server->server_type == SILC_ROUTER)
332 silc_server_check_watcher_list(server, client, NULL,
333 SILC_NOTIFY_TYPE_SIGNOFF);
335 /* Remove this client from watcher list if it is */
336 silc_server_del_from_watcher_list(server, client);
338 client->data.status &= ~SILC_IDLIST_STATUS_REGISTERED;
340 client->router = NULL;
341 client->connection = NULL;
342 silc_dlist_add(server->expired_clients, client);
345 case SILC_NOTIFY_TYPE_TOPIC_SET:
347 * Distribute the notify to local clients on the channel
350 SILC_LOG_DEBUG(("TOPIC SET notify"));
353 if (!silc_argument_get_decoded(args, 1, SILC_ARGUMENT_ID, &id, NULL))
356 /* Get client entry */
357 if (id.type == SILC_ID_CLIENT) {
358 client = silc_idlist_find_client_by_id(server->global_list,
359 SILC_ID_GET_ID(id), TRUE, &cache);
361 client = silc_idlist_find_client_by_id(server->local_list,
370 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
374 if (!silc_id_str2id(packet->dst_id, packet->dst_id_len,
375 packet->dst_id_type, &channel_id,
379 /* Get channel entry */
380 channel = silc_idlist_find_channel_by_id(server->global_list,
383 channel = silc_idlist_find_channel_by_id(server->local_list,
386 SILC_LOG_DEBUG(("Notify for unknown channel"));
391 if (channel->topic && !strcmp(channel->topic, tmp)) {
392 SILC_LOG_DEBUG(("Topic is already set and same"));
397 /* Get user's channel entry and check that topic set is allowed. */
398 if (!silc_server_client_on_channel(client, channel, &chl))
400 if (channel->mode & SILC_CHANNEL_MODE_TOPIC &&
401 !(chl->mode & SILC_CHANNEL_UMODE_CHANOP) &&
402 !(chl->mode & SILC_CHANNEL_UMODE_CHANFO)) {
403 SILC_LOG_DEBUG(("Topic change is not allowed"));
408 /* Change the topic */
409 silc_free(channel->topic);
410 channel->topic = strdup(tmp);
412 /* Send the same notify to the channel */
413 silc_server_packet_send_to_channel(server, NULL, channel, packet->type,
414 FALSE, TRUE, buffer->data,
415 silc_buffer_len(buffer));
418 case SILC_NOTIFY_TYPE_NICK_CHANGE:
421 * Distribute the notify to local clients on the channel
424 SilcUInt32 nickname_len;
426 SILC_LOG_DEBUG(("NICK CHANGE notify"));
428 /* Get old client ID */
429 if (!silc_argument_get_decoded(args, 1, SILC_ARGUMENT_ID, &id, NULL))
432 /* Get new client ID */
433 if (!silc_argument_get_decoded(args, 2, SILC_ARGUMENT_ID, &id2, NULL))
436 SILC_LOG_DEBUG(("Old Client ID id(%s)",
437 silc_id_render(SILC_ID_GET_ID(id), SILC_ID_CLIENT)));
438 SILC_LOG_DEBUG(("New Client ID id(%s)",
439 silc_id_render(SILC_ID_GET_ID(id2), SILC_ID_CLIENT)));
441 /* From protocol version 1.1 we also get the new nickname */
442 nickname = silc_argument_get_arg_type(args, 3, &nickname_len);;
444 /* Replace the Client ID */
445 client = silc_idlist_replace_client_id(server,
448 SILC_ID_GET_ID(id2), nickname);
450 client = silc_idlist_replace_client_id(server,
453 SILC_ID_GET_ID(id2), nickname);
456 /* Send the NICK_CHANGE notify type to local clients on the channels
457 this client is joined to. */
458 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
459 tmp2 = silc_argument_get_arg_type(args, 2, &tmp2_len);
460 silc_server_send_notify_on_channels(server, client, client,
461 SILC_NOTIFY_TYPE_NICK_CHANGE, 3,
462 tmp, tmp_len, tmp2, tmp2_len,
470 case SILC_NOTIFY_TYPE_CMODE_CHANGE:
472 * Distribute the notify to local clients on the channel
475 SILC_LOG_DEBUG(("CMODE CHANGE notify"));
478 if (!silc_argument_get_decoded(args, 1, SILC_ARGUMENT_ID, &id, NULL))
481 /* Get client entry */
482 if (id.type == SILC_ID_CLIENT) {
483 client = silc_idlist_find_client_by_id(server->global_list,
484 SILC_ID_GET_ID(id), TRUE, &cache);
486 client = silc_idlist_find_client_by_id(server->local_list,
494 if (!silc_id_str2id(packet->dst_id, packet->dst_id_len,
495 packet->dst_id_type, &channel_id,
499 /* Get channel entry */
500 channel = silc_idlist_find_channel_by_id(server->global_list,
503 channel = silc_idlist_find_channel_by_id(server->local_list,
506 SILC_LOG_DEBUG(("Notify for unknown channel"));
512 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
515 SILC_GET32_MSB(mode, tmp);
517 /* Check if mode changed */
518 if (channel->mode == mode) {
519 SILC_LOG_DEBUG(("Mode is changed already"));
521 /* If this mode change has founder mode then we'll enforce the
522 change so that the server gets the real founder public key */
523 if (server->server_type != SILC_SERVER &&
524 sock != SILC_PRIMARY_ROUTE(server) &&
525 mode & SILC_CHANNEL_MODE_FOUNDER_AUTH && channel->founder_key) {
526 SILC_LOG_DEBUG(("Sending founder public key to server"));
527 silc_server_send_notify_cmode(server, sock, FALSE, channel,
528 channel->mode, server->id,
529 SILC_ID_SERVER, channel->cipher,
532 channel->founder_key, NULL);
535 /* If we received same mode from our primary check whether founder
536 mode and key in the notify is set. We update the founder key
537 here since we may have wrong one */
538 if (server->server_type != SILC_ROUTER &&
539 sock == SILC_PRIMARY_ROUTE(server) &&
540 mode & SILC_CHANNEL_MODE_FOUNDER_AUTH) {
541 SILC_LOG_DEBUG(("Founder public key received from router"));
542 tmp = silc_argument_get_arg_type(args, 6, &tmp_len);
546 if (channel->founder_key)
547 silc_pkcs_public_key_free(channel->founder_key);
548 channel->founder_key = NULL;
549 silc_public_key_payload_decode(tmp, tmp_len,
550 &channel->founder_key);
553 /* Check also for channel public key list */
554 if (server->server_type == SILC_SERVER &&
555 sock == SILC_PRIMARY_ROUTE(server) &&
556 mode & SILC_CHANNEL_MODE_CHANNEL_AUTH) {
559 unsigned char mask[4], ulimit[4];
561 SILC_LOG_DEBUG(("Channel public key list received from router"));
562 tmp = silc_argument_get_arg_type(args, 7, &tmp_len);
566 /* Set the router's list, and send the notify to channel too so that
567 channel gets the list */
568 silc_server_set_channel_pk_list(server, sock, channel, tmp, tmp_len);
569 chpklist = silc_server_get_channel_pk_list(server, channel,
573 sidp = silc_id_payload_encode(server->router->id, SILC_ID_SERVER);
574 SILC_PUT32_MSB(channel->mode, mask);
575 if (channel->mode & SILC_CHANNEL_MODE_ULIMIT)
576 SILC_PUT32_MSB(channel->user_limit, ulimit);
577 silc_server_send_notify_to_channel(server, NULL, channel, FALSE, TRUE,
578 SILC_NOTIFY_TYPE_CMODE_CHANGE, 8,
579 sidp->data, silc_buffer_len(sidp),
583 strlen(channel->cipher) : 0,
586 strlen(channel->hmac_name) : 0,
588 channel->passphrase ?
589 strlen(channel->passphrase) : 0,
592 silc_buffer_len(chpklist),
594 SILC_CHANNEL_MODE_ULIMIT ?
597 SILC_CHANNEL_MODE_ULIMIT ?
598 sizeof(ulimit) : 0));
599 silc_buffer_free(sidp);
600 silc_buffer_free(chpklist);
607 /* Get user's channel entry and check that mode change is allowed */
609 if (!silc_server_client_on_channel(client, channel, &chl))
611 if (!silc_server_check_cmode_rights(server, channel, chl, mode)) {
612 SILC_LOG_DEBUG(("CMODE change is not allowed"));
613 silc_server_send_notify_cmode(server, sock, FALSE, channel,
614 channel->mode, server->id,
615 SILC_ID_SERVER, channel->cipher,
618 channel->founder_key, NULL);
622 /* Assure that server is not removing founder mode from us */
623 if (server->server_type == SILC_ROUTER &&
624 sock != SILC_PRIMARY_ROUTE(server) &&
625 channel->mode & SILC_CHANNEL_MODE_FOUNDER_AUTH &&
626 !(mode & SILC_CHANNEL_MODE_FOUNDER_AUTH)) {
627 SILC_LOG_DEBUG(("Enforcing sender to change channel mode"));
628 silc_server_send_notify_cmode(server, sock, FALSE, channel,
629 channel->mode, server->id,
630 SILC_ID_SERVER, channel->cipher,
633 channel->founder_key, NULL);
637 /* If server is adding founder mode, check whether there is founder
638 on channel already and is not from this server */
639 if (server->server_type == SILC_ROUTER &&
640 sock != SILC_PRIMARY_ROUTE(server) &&
641 mode & SILC_CHANNEL_MODE_FOUNDER_AUTH) {
642 silc_hash_table_list(channel->user_list, &htl);
643 while (silc_hash_table_get(&htl, NULL, (void *)&chl))
644 if (chl->mode & SILC_CHANNEL_UMODE_CHANFO &&
645 chl->client->router != (SilcServerEntry)idata) {
646 SILC_LOG_DEBUG(("Enforcing sender to change channel mode"));
647 silc_server_send_notify_cmode(server, sock, FALSE, channel,
648 channel->mode, server->id,
649 SILC_ID_SERVER, channel->cipher,
652 channel->founder_key, NULL);
653 silc_hash_table_list_reset(&htl);
656 silc_hash_table_list_reset(&htl);
660 /* If the channel had private keys set and the mode was removed then
661 we must re-generate and re-distribute a new channel key */
662 if (channel->mode & SILC_CHANNEL_MODE_PRIVKEY &&
663 !(mode & SILC_CHANNEL_MODE_PRIVKEY)) {
664 /* Re-generate channel key */
665 if (!silc_server_create_channel_key(server, channel, 0))
668 /* Send the channel key. This sends it to our local clients and if
669 we are normal server to our router as well. */
670 silc_server_send_channel_key(server, NULL, channel,
671 server->server_type == SILC_ROUTER ?
672 FALSE : !server->standalone);
676 tmp = silc_argument_get_arg_type(args, 4, &tmp_len);
678 unsigned char hash[SILC_HASH_MAXLEN];
681 silc_hmac_free(channel->hmac);
682 if (!silc_hmac_alloc(tmp, NULL, &channel->hmac))
685 /* Set the HMAC key out of current channel key. The client must do
687 silc_hash_make(silc_hmac_get_hash(channel->hmac), channel->key,
688 channel->key_len / 8, hash);
689 silc_hmac_set_key(channel->hmac, hash,
690 silc_hash_len(silc_hmac_get_hash(channel->hmac)));
691 memset(hash, 0, sizeof(hash));
694 /* Get the passphrase */
695 tmp = silc_argument_get_arg_type(args, 5, &tmp_len);
697 silc_free(channel->passphrase);
698 channel->passphrase = silc_memdup(tmp, tmp_len);
701 /* Get founder public key */
702 tmp = silc_argument_get_arg_type(args, 6, &tmp_len);
703 if (tmp && mode & SILC_CHANNEL_MODE_FOUNDER_AUTH) {
704 if (channel->founder_key)
705 silc_pkcs_public_key_free(channel->founder_key);
706 channel->founder_key = NULL;
707 SILC_LOG_DEBUG(("Founder public key received"));
708 if (!silc_public_key_payload_decode(tmp, tmp_len,
709 &channel->founder_key)) {
710 SILC_LOG_DEBUG(("Enforcing sender to change channel mode"));
711 mode &= ~SILC_CHANNEL_MODE_FOUNDER_AUTH;
712 silc_server_send_notify_cmode(server, sock, FALSE, channel,
713 mode, server->id, SILC_ID_SERVER,
716 channel->passphrase, NULL, NULL);
717 if (channel->founder_key)
718 silc_pkcs_public_key_free(channel->founder_key);
719 channel->founder_key = NULL;
723 if (mode & SILC_CHANNEL_MODE_FOUNDER_AUTH && !channel->founder_key &&
724 server->server_type == SILC_ROUTER) {
725 SILC_LOG_DEBUG(("Enforcing sender to change channel mode"));
726 mode &= ~SILC_CHANNEL_MODE_FOUNDER_AUTH;
727 silc_server_send_notify_cmode(server, sock, FALSE, channel,
728 mode, server->id, SILC_ID_SERVER,
731 channel->passphrase, NULL, NULL);
734 /* Process channel public key(s). */
735 tmp = silc_argument_get_arg_type(args, 7, &tmp_len);
736 if (tmp && mode & SILC_CHANNEL_MODE_CHANNEL_AUTH) {
738 SILC_LOG_DEBUG(("Channel public key list received from router"));
741 silc_server_set_channel_pk_list(server, sock, channel, tmp, tmp_len);
743 /* If list was set already we will enforce the same list to server. */
744 if (ret == SILC_STATUS_ERR_OPERATION_ALLOWED) {
745 SilcBuffer chpklist = silc_server_get_channel_pk_list(server, channel,
747 silc_server_send_notify_cmode(server, sock, FALSE, channel,
748 mode, server->id, SILC_ID_SERVER,
751 channel->passphrase, NULL,
753 silc_buffer_free(chpklist);
757 /* Get the user limit */
758 tmp = silc_argument_get_arg_type(args, 8, &tmp_len);
759 if (tmp && tmp_len == 4 && mode & SILC_CHANNEL_MODE_ULIMIT)
760 SILC_GET32_MSB(channel->user_limit, tmp);
762 /* Send the same notify to the channel */
763 silc_server_packet_send_to_channel(server, NULL, channel, packet->type,
764 FALSE, TRUE, buffer->data,
765 silc_buffer_len(buffer));
768 channel->mode = mode;
770 /* Cleanup if some modes are removed */
772 if (!(channel->mode & SILC_CHANNEL_MODE_FOUNDER_AUTH) &&
773 channel->founder_key) {
774 silc_pkcs_public_key_free(channel->founder_key);
775 channel->founder_key = NULL;
778 if (!(channel->mode & SILC_CHANNEL_MODE_CHANNEL_AUTH) &&
779 channel->channel_pubkeys) {
780 silc_hash_table_free(channel->channel_pubkeys);
781 channel->channel_pubkeys = NULL;
786 case SILC_NOTIFY_TYPE_CUMODE_CHANGE:
789 * Distribute the notify to local clients on the channel
791 SilcChannelClientEntry chl2 = NULL;
792 SilcBool notify_sent = FALSE;
794 SILC_LOG_DEBUG(("CUMODE CHANGE notify"));
797 if (!silc_argument_get_decoded(args, 1, SILC_ARGUMENT_ID, &id, NULL))
800 /* Get client entry */
801 if (id.type == SILC_ID_CLIENT) {
802 client = silc_idlist_find_client_by_id(server->global_list,
806 client = silc_idlist_find_client_by_id(server->local_list,
814 if (!silc_id_str2id(packet->dst_id, packet->dst_id_len,
815 packet->dst_id_type, &channel_id,
819 /* Get channel entry */
820 channel = silc_idlist_find_channel_by_id(server->global_list,
823 channel = silc_idlist_find_channel_by_id(server->local_list,
826 SILC_LOG_DEBUG(("Notify for unknown channel"));
832 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
836 SILC_GET32_MSB(mode, tmp);
838 /* Get target client */
839 if (!silc_argument_get_decoded(args, 3, SILC_ARGUMENT_ID, &id, NULL))
842 /* Get client entry */
843 client2 = silc_idlist_find_client_by_id(server->global_list,
844 SILC_ID_GET_ID(id), TRUE, NULL);
846 client2 = silc_idlist_find_client_by_id(server->local_list,
854 /* Check that sender is on channel */
855 if (!silc_server_client_on_channel(client, channel, &chl))
858 if (client != client2 && server->server_type == SILC_ROUTER) {
859 /* Sender must be operator */
860 if (!(chl->mode & SILC_CHANNEL_UMODE_CHANOP) &&
861 !(chl->mode & SILC_CHANNEL_UMODE_CHANFO)) {
862 SILC_LOG_DEBUG(("CUMODE change is not allowed"));
866 if (!silc_server_client_on_channel(client2, channel, &chl))
869 /* If target is founder mode change is not allowed. */
870 if (chl->mode & SILC_CHANNEL_UMODE_CHANFO) {
871 SILC_LOG_DEBUG(("CUMODE change is not allowed"));
877 /* Get target channel user entry */
878 if (!silc_server_client_on_channel(client2, channel, &chl))
881 if (server->server_type == SILC_SERVER && chl->mode == mode) {
882 SILC_LOG_DEBUG(("Mode is changed already"));
886 /* Check whether to give founder rights to this user or not. The
887 problem here is that we get only the public key of the client,
888 but no authentication data. We must assume that server has
889 already authenticated the user (and thus we must trust the
891 if (mode & SILC_CHANNEL_UMODE_CHANFO &&
892 !(chl->mode & SILC_CHANNEL_UMODE_CHANFO) &&
893 server->server_type == SILC_ROUTER &&
894 sock != SILC_PRIMARY_ROUTE(server)) {
895 SilcPublicKey founder_key = NULL;
897 /* If channel doesn't have founder auth mode then it's impossible
898 that someone would be getting founder rights with CUMODE command.
899 In that case there already either is founder or there isn't
900 founder at all on the channel (valid only when 'client' is
902 if (client && !(channel->mode & SILC_CHANNEL_MODE_FOUNDER_AUTH)) {
903 /* Force the mode to not have founder mode */
904 chl->mode = mode &= ~SILC_CHANNEL_UMODE_CHANFO;
905 silc_server_force_cumode_change(server, sock, channel, chl, mode);
910 /* Get the founder of the channel and if found then this client
911 cannot be the founder since there already is one. */
912 silc_hash_table_list(channel->user_list, &htl);
913 while (silc_hash_table_get(&htl, NULL, (void *)&chl2))
914 if (chl2->mode & SILC_CHANNEL_UMODE_CHANFO) {
915 SILC_LOG_DEBUG(("Founder already on channel"));
916 chl->mode = mode &= ~SILC_CHANNEL_UMODE_CHANFO;
917 silc_server_force_cumode_change(server, sock, channel,
922 silc_hash_table_list_reset(&htl);
923 if (!(mode & SILC_CHANNEL_UMODE_CHANFO))
926 /* Founder not found on the channel. Since the founder auth mode
927 is set on the channel now check whether this is the client that
928 originally set the mode. */
930 if (channel->founder_key) {
931 /* Get public key that must be present in notify */
932 tmp = silc_argument_get_arg_type(args, 4, &tmp_len);
933 if (!tmp || !silc_public_key_payload_decode(tmp, tmp_len,
935 chl->mode = mode &= ~SILC_CHANNEL_UMODE_CHANFO;
936 SILC_LOG_DEBUG(("Founder public key not present"));
937 silc_server_force_cumode_change(server, sock, channel, chl, mode);
942 /* Now match the public key we have cached and public key sent.
944 if (!silc_pkcs_public_key_compare(channel->founder_key,
946 chl->mode = mode &= ~SILC_CHANNEL_UMODE_CHANFO;
947 SILC_LOG_DEBUG(("Founder public key mismatch"));
948 silc_server_force_cumode_change(server, sock, channel, chl, mode);
954 /* There cannot be anyone else as founder on the channel now. This
955 client is definitely the founder due to this 'authentication'.
956 We trust the server did the actual signature verification
957 earlier (bad, yes). */
958 silc_hash_table_list(channel->user_list, &htl);
959 while (silc_hash_table_get(&htl, NULL, (void *)&chl2))
960 if (chl2->mode & SILC_CHANNEL_UMODE_CHANFO) {
961 chl2->mode &= ~SILC_CHANNEL_UMODE_CHANFO;
962 SILC_LOG_DEBUG(("Removing old founder rights, new authenticated"));
963 silc_server_force_cumode_change(server, NULL, channel, chl2,
967 silc_hash_table_list_reset(&htl);
970 silc_pkcs_public_key_free(founder_key);
973 if (server->server_type != SILC_SERVER && chl->mode == mode) {
974 SILC_LOG_DEBUG(("Mode is changed already"));
978 SILC_LOG_DEBUG(("Changing %s channel user mode",
979 chl->client->nickname ? chl->client->nickname :
980 (unsigned char *)""));
982 /* Change the mode */
985 /* Send the same notify to the channel */
987 silc_server_packet_send_to_channel(server, sock, channel,
989 FALSE, TRUE, buffer->data,
990 silc_buffer_len(buffer));
995 case SILC_NOTIFY_TYPE_INVITE:
997 if (packet->dst_id_type == SILC_ID_CLIENT)
1000 SILC_LOG_DEBUG(("INVITE notify"));
1002 /* Get Channel ID */
1003 if (!silc_argument_get_decoded(args, 1, SILC_ARGUMENT_ID, &id, NULL))
1006 /* Get channel entry */
1007 channel = silc_idlist_find_channel_by_id(server->global_list,
1008 SILC_ID_GET_ID(id), NULL);
1010 channel = silc_idlist_find_channel_by_id(server->local_list,
1011 SILC_ID_GET_ID(id), NULL);
1013 SILC_LOG_DEBUG(("Notify for unknown channel"));
1018 /* Get the invite action */
1019 tmp = silc_argument_get_arg_type(args, 4, &tmp_len);
1020 if (tmp && tmp_len == 1) {
1021 SilcUInt8 action = (SilcUInt8)tmp[0];
1022 SilcUInt16 iargc = 0;
1023 SilcArgumentPayload iargs;
1025 /* Get invite list */
1026 tmp = silc_argument_get_arg_type(args, 5, &tmp_len);
1027 if (!tmp || tmp_len < 2)
1030 /* Parse the arguments to see they are constructed correctly */
1031 SILC_GET16_MSB(iargc, tmp);
1032 iargs = silc_argument_payload_parse(tmp + 2, tmp_len - 2, iargc);
1036 if (!channel->invite_list)
1037 channel->invite_list =
1038 silc_hash_table_alloc(0, silc_hash_ptr,
1040 silc_server_inviteban_destruct, channel, TRUE);
1042 /* Proces the invite action */
1043 if (!silc_server_inviteban_process(server, channel->invite_list, action,
1046 silc_argument_payload_free(iargs);
1048 /* If we are router we must send this notify to our local servers on
1049 the channel. Normal server does nothing. The notify is not
1051 if (server->server_type == SILC_ROUTER)
1052 silc_server_packet_send_to_channel(server, sock, channel,
1053 packet->type, FALSE, FALSE,
1055 silc_buffer_len(buffer));
1060 case SILC_NOTIFY_TYPE_CHANNEL_CHANGE:
1062 * Distribute to the local clients on the channel and change the
1066 SILC_LOG_DEBUG(("CHANNEL CHANGE"));
1068 if (idata->conn_type != SILC_CONN_ROUTER)
1071 /* Get the old Channel ID */
1072 if (!silc_argument_get_decoded(args, 1, SILC_ARGUMENT_ID, &id, NULL))
1075 /* Get the channel entry */
1076 channel = silc_idlist_find_channel_by_id(server->local_list,
1077 SILC_ID_GET_ID(id), NULL);
1079 channel = silc_idlist_find_channel_by_id(server->global_list,
1080 SILC_ID_GET_ID(id), NULL);
1082 SILC_LOG_DEBUG(("Notify for unknown channel"));
1087 /* Send the notify to the channel */
1088 silc_server_packet_send_to_channel(server, sock, channel, packet->type,
1089 FALSE, TRUE, buffer->data,
1090 silc_buffer_len(buffer));
1092 /* Get the new Channel ID */
1093 if (!silc_argument_get_decoded(args, 2, SILC_ARGUMENT_ID, &id2, NULL))
1096 SILC_LOG_DEBUG(("Old Channel ID id(%s)",
1097 silc_id_render(SILC_ID_GET_ID(id), SILC_ID_CHANNEL)));
1098 SILC_LOG_DEBUG(("New Channel ID id(%s)",
1099 silc_id_render(SILC_ID_GET_ID(id2), SILC_ID_CHANNEL)));
1101 /* Replace the Channel ID */
1103 if (!silc_idlist_replace_channel_id(server->local_list,
1105 SILC_ID_GET_ID(id2)))
1106 if (!silc_idlist_replace_channel_id(server->global_list,
1108 SILC_ID_GET_ID(id2)))
1112 SilcBuffer modes = NULL, users = NULL, users_modes = NULL;
1114 /* Re-announce this channel which ID was changed. */
1115 silc_server_send_new_channel(server, sock, FALSE, channel->channel_name,
1117 silc_id_get_len(channel->id,
1121 /* Re-announce our clients on the channel as the ID has changed now */
1122 silc_server_announce_get_channel_users(server, channel, &modes, &users,
1125 silc_buffer_push(users, users->data - users->head);
1126 silc_server_packet_send(server, sock,
1127 SILC_PACKET_NOTIFY, SILC_PACKET_FLAG_LIST,
1128 users->data, silc_buffer_len(users));
1129 silc_buffer_free(users);
1132 silc_buffer_push(modes, modes->data - modes->head);
1133 silc_server_packet_send_dest(server, sock,
1134 SILC_PACKET_NOTIFY, SILC_PACKET_FLAG_LIST,
1135 channel->id, SILC_ID_CHANNEL,
1136 modes->data, silc_buffer_len(modes));
1137 silc_buffer_free(modes);
1140 silc_buffer_push(users_modes, users_modes->data - users_modes->head);
1141 silc_server_packet_send_dest(server, sock,
1142 SILC_PACKET_NOTIFY, SILC_PACKET_FLAG_LIST,
1143 channel->id, SILC_ID_CHANNEL,
1145 silc_buffer_len(users_modes));
1146 silc_buffer_free(users_modes);
1149 /* Re-announce channel's topic */
1150 if (channel->topic) {
1151 silc_server_send_notify_topic_set(server, sock,
1152 server->server_type == SILC_ROUTER ?
1153 TRUE : FALSE, channel,
1154 server->id, SILC_ID_SERVER,
1161 case SILC_NOTIFY_TYPE_SERVER_SIGNOFF:
1163 * Remove the server entry and all clients that this server owns.
1166 SILC_LOG_DEBUG(("SERVER SIGNOFF notify"));
1168 /* Backup router shouldn't accept SERVER_SIGNOFF's from normal routers
1169 when the backup isn't acting as primary router. */
1170 if (idata->conn_type == SILC_CONN_SERVER &&
1171 server->backup_router && server->server_type == SILC_BACKUP_ROUTER)
1175 if (!silc_argument_get_decoded(args, 1, SILC_ARGUMENT_ID, &id, NULL))
1178 /* If the ID is mine, this notify is not allowed. */
1179 if (SILC_ID_SERVER_COMPARE(SILC_ID_GET_ID(id), server->id)) {
1180 SILC_LOG_DEBUG(("Ignoring my own ID for SERVER_SIGNOFF"));
1184 /* Get server entry */
1185 server_entry = silc_idlist_find_server_by_id(server->global_list,
1189 if (!server_entry) {
1190 server_entry = silc_idlist_find_server_by_id(server->local_list,
1194 if (!server_entry) {
1195 /* If we are normal server then we might not have the server. Check
1196 whether router was kind enough to send the list of all clients
1197 that actually was to be removed. Remove them if the list is
1199 if (server->server_type != SILC_ROUTER &&
1200 silc_argument_get_arg_num(args) > 1) {
1203 for (i = 1; i < silc_argument_get_arg_num(args); i++) {
1205 if (!silc_argument_get_decoded(args, i + 1, SILC_ARGUMENT_ID,
1209 /* Get client entry */
1210 client = silc_idlist_find_client_by_id(server->global_list,
1215 client = silc_idlist_find_client_by_id(server->local_list,
1223 /* Update statistics */
1224 server->stat.clients--;
1225 if (server->stat.cell_clients)
1226 server->stat.cell_clients--;
1227 SILC_OPER_STATS_UPDATE(client, server, SILC_UMODE_SERVER_OPERATOR);
1228 SILC_OPER_STATS_UPDATE(client, router, SILC_UMODE_ROUTER_OPERATOR);
1230 /* Remove the client from all channels. */
1231 silc_server_remove_from_channels(server, NULL, client,
1232 TRUE, NULL, FALSE, FALSE);
1234 /* Check if anyone is watching this nickname */
1235 if (server->server_type == SILC_ROUTER)
1236 silc_server_check_watcher_list(server, client, NULL,
1237 SILC_NOTIFY_TYPE_SERVER_SIGNOFF);
1239 /* Remove this client from watcher list if it is */
1241 silc_server_del_from_watcher_list(server, client);
1243 /* Remove the client */
1244 silc_idlist_del_data(client);
1245 silc_idlist_del_client(local ? server->local_list :
1246 server->global_list, client);
1254 /* For local entrys SERVER_SIGNOFF is processed only on backup router.
1255 It is possible that router sends server signoff for a server. If
1256 backup router has it as local connection it will be closed. */
1257 if (SILC_IS_LOCAL(server_entry)) {
1258 if (server->server_type == SILC_BACKUP_ROUTER) {
1259 sock = server_entry->connection;
1260 SILC_LOG_DEBUG(("Closing connection after SERVER_SIGNOFF"));
1261 silc_server_free_sock_user_data(server, sock, NULL);
1262 silc_server_close_connection(server, sock);
1268 /* Remove all servers that are originated from this server, and
1269 remove the clients of those servers too. */
1270 silc_server_remove_servers_by_server(server, server_entry, TRUE);
1272 /* Remove the clients that this server owns as they will become
1274 silc_server_remove_clients_by_server(server, server_entry->router,
1275 server_entry, TRUE);
1276 silc_server_backup_del(server, server_entry);
1278 /* Remove the server entry */
1279 silc_idlist_del_server(local ? server->local_list :
1280 server->global_list, server_entry);
1282 /* Update statistics */
1283 if (server->server_type == SILC_ROUTER)
1284 server->stat.servers--;
1288 case SILC_NOTIFY_TYPE_KICKED:
1290 * Distribute the notify to local clients on the channel
1293 SILC_LOG_DEBUG(("KICKED notify"));
1295 if (!silc_id_str2id(packet->dst_id, packet->dst_id_len,
1296 packet->dst_id_type, &channel_id,
1297 sizeof(channel_id)))
1300 /* Get channel entry */
1301 channel = silc_idlist_find_channel_by_id(server->global_list,
1304 channel = silc_idlist_find_channel_by_id(server->local_list,
1307 SILC_LOG_DEBUG(("Notify for unknown channel"));
1313 if (!silc_argument_get_decoded(args, 1, SILC_ARGUMENT_ID, &id, NULL))
1316 /* If the the client is not in local list we check global list */
1317 client = silc_idlist_find_client_by_id(server->global_list,
1318 SILC_ID_GET_ID(id), TRUE, NULL);
1320 client = silc_idlist_find_client_by_id(server->local_list,
1321 SILC_ID_GET_ID(id), TRUE, NULL);
1326 /* If target is founder they cannot be kicked */
1327 if (!silc_server_client_on_channel(client, channel, &chl))
1329 if (chl->mode & SILC_CHANNEL_UMODE_CHANFO)
1332 /* Get the kicker's Client ID */
1333 if (!silc_argument_get_decoded(args, 3, SILC_ARGUMENT_ID, &id, NULL))
1336 /* If the the client is not in local list we check global list */
1337 client2 = silc_idlist_find_client_by_id(server->global_list,
1338 SILC_ID_GET_ID(id), TRUE, NULL);
1340 client2 = silc_idlist_find_client_by_id(server->local_list,
1341 SILC_ID_GET_ID(id), TRUE, NULL);
1346 /* Kicker must be operator on channel */
1347 if (!silc_server_client_on_channel(client2, channel, &chl))
1349 if (!(chl->mode & SILC_CHANNEL_UMODE_CHANOP) &&
1350 !(chl->mode & SILC_CHANNEL_UMODE_CHANFO)) {
1351 SILC_LOG_DEBUG(("Kicking is not allowed"));
1355 /* Send to channel */
1356 silc_server_packet_send_to_channel(server, sock, channel, packet->type,
1357 FALSE, TRUE, buffer->data,
1358 silc_buffer_len(buffer));
1360 /* Remove the client from channel's invite list */
1361 if (channel->invite_list && silc_hash_table_count(channel->invite_list)) {
1363 SilcArgumentPayload iargs;
1364 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
1365 ab = silc_argument_payload_encode_one(NULL, tmp, tmp_len, 3);
1366 iargs = silc_argument_payload_parse(ab->data, silc_buffer_len(ab), 1);
1367 silc_server_inviteban_process(server, channel->invite_list, 1, iargs);
1368 silc_buffer_free(ab);
1369 silc_argument_payload_free(iargs);
1372 /* Remove the client from channel */
1373 silc_server_remove_from_one_channel(server, sock, channel, client, FALSE);
1377 case SILC_NOTIFY_TYPE_KILLED:
1380 * Distribute the notify to local clients on channels
1382 unsigned char *comment;
1383 SilcUInt32 comment_len;
1385 SILC_LOG_DEBUG(("KILLED notify"));
1388 if (!silc_argument_get_decoded(args, 1, SILC_ARGUMENT_ID, &id, NULL))
1391 /* If the the client is not in local list we check global list */
1392 client = silc_idlist_find_client_by_id(server->global_list,
1393 SILC_ID_GET_ID(id), TRUE, &cache);
1395 client = silc_idlist_find_client_by_id(server->local_list,
1402 /* If the client is one of ours, then close the connection to the
1403 client now. This removes the client from all channels as well. */
1404 if (packet->dst_id_type == SILC_ID_CLIENT && client->connection) {
1405 sock = client->connection;
1406 silc_server_free_client_data(server, NULL, client, FALSE, NULL);
1407 silc_server_close_connection(server, sock);
1412 comment = silc_argument_get_arg_type(args, 2, &comment_len);
1413 if (comment_len > 128)
1416 /* Get the killer's Client ID */
1417 if (!silc_argument_get_decoded(args, 3, SILC_ARGUMENT_ID, &id, NULL))
1420 if (id.type == SILC_ID_CLIENT) {
1421 /* If the the client is not in local list we check global list */
1422 client2 = silc_idlist_find_client_by_id(server->global_list,
1426 client2 = silc_idlist_find_client_by_id(server->local_list,
1433 /* Killer must be router operator */
1434 if (server->server_type != SILC_SERVER &&
1435 !(client2->mode & SILC_UMODE_ROUTER_OPERATOR)) {
1436 SILC_LOG_DEBUG(("Killing is not allowed"));
1441 /* Send the notify to local clients on the channels except to the
1442 client who is killed. */
1443 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
1444 tmp2 = silc_argument_get_arg_type(args, 3, &tmp2_len);
1445 silc_server_send_notify_on_channels(server, client, client,
1446 SILC_NOTIFY_TYPE_KILLED, 3,
1447 tmp, tmp_len, comment, comment_len,
1450 /* Remove the client from all channels */
1451 silc_server_remove_from_channels(server, NULL, client, FALSE, NULL,
1454 /* Check if anyone is watching this nickname */
1455 if (server->server_type == SILC_ROUTER)
1456 silc_server_check_watcher_list(server, client, NULL,
1457 SILC_NOTIFY_TYPE_KILLED);
1459 /* Remove client's public key from repository, this will free it too. */
1460 if (client->data.public_key) {
1461 silc_skr_del_public_key(server->repository, client->data.public_key,
1463 client->data.public_key = NULL;
1466 /* Update statistics */
1467 server->stat.clients--;
1468 if (server->stat.cell_clients)
1469 server->stat.cell_clients--;
1470 SILC_OPER_STATS_UPDATE(client, server, SILC_UMODE_SERVER_OPERATOR);
1471 SILC_OPER_STATS_UPDATE(client, router, SILC_UMODE_ROUTER_OPERATOR);
1473 if (SILC_IS_LOCAL(client)) {
1474 server->stat.my_clients--;
1475 silc_schedule_task_del_by_context(server->schedule, client);
1476 silc_idlist_del_data(client);
1480 client->data.status &= ~SILC_IDLIST_STATUS_REGISTERED;
1482 client->router = NULL;
1483 client->connection = NULL;
1484 silc_dlist_add(server->expired_clients, client);
1488 case SILC_NOTIFY_TYPE_UMODE_CHANGE:
1490 * Save the mode of the client.
1493 SILC_LOG_DEBUG(("UMODE_CHANGE notify"));
1496 if (!silc_argument_get_decoded(args, 1, SILC_ARGUMENT_ID, &id, NULL))
1499 /* Get client entry */
1500 client = silc_idlist_find_client_by_id(server->global_list,
1501 SILC_ID_GET_ID(id), TRUE, NULL);
1503 client = silc_idlist_find_client_by_id(server->local_list,
1504 SILC_ID_GET_ID(id), TRUE, NULL);
1510 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
1513 SILC_GET32_MSB(mode, tmp);
1515 /* Remove internal resumed flag if client is marked detached now */
1516 if (mode & SILC_UMODE_DETACHED)
1517 client->data.status &= ~SILC_IDLIST_STATUS_RESUMED;
1519 /* Update statistics */
1520 if (server->server_type == SILC_ROUTER) {
1521 if (mode & SILC_UMODE_GONE) {
1522 if (!(client->mode & SILC_UMODE_GONE))
1523 server->stat.aways++;
1525 if (client->mode & SILC_UMODE_GONE)
1526 server->stat.aways--;
1528 if (mode & SILC_UMODE_DETACHED) {
1529 if (!(client->mode & SILC_UMODE_DETACHED))
1530 server->stat.detached++;
1532 if (client->mode & SILC_UMODE_DETACHED)
1533 server->stat.detached--;
1536 SILC_UMODE_STATS_UPDATE(server, SILC_UMODE_SERVER_OPERATOR);
1537 SILC_UMODE_STATS_UPDATE(router, SILC_UMODE_ROUTER_OPERATOR);
1539 /* Change the mode */
1540 client->mode = mode;
1542 /* Check if anyone is watching this nickname */
1543 if (server->server_type == SILC_ROUTER)
1544 silc_server_check_watcher_list(server, client, NULL,
1545 SILC_NOTIFY_TYPE_UMODE_CHANGE);
1549 case SILC_NOTIFY_TYPE_BAN:
1554 SILC_LOG_DEBUG(("BAN notify"));
1556 /* Get Channel ID */
1557 if (!silc_argument_get_decoded(args, 1, SILC_ARGUMENT_ID, &id, NULL))
1560 /* Get channel entry */
1561 channel = silc_idlist_find_channel_by_id(server->global_list,
1562 SILC_ID_GET_ID(id), NULL);
1564 channel = silc_idlist_find_channel_by_id(server->local_list,
1565 SILC_ID_GET_ID(id), NULL);
1567 SILC_LOG_DEBUG(("Notify for unknown channel"));
1572 /* Get the ban action */
1573 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
1574 if (tmp && tmp_len == 1) {
1575 SilcUInt8 action = (SilcUInt8)tmp[0];
1576 SilcUInt16 iargc = 0;
1577 SilcArgumentPayload iargs;
1580 tmp = silc_argument_get_arg_type(args, 3, &tmp_len);
1581 if (!tmp || tmp_len < 2)
1584 /* Parse the arguments to see they are constructed correctly */
1585 SILC_GET16_MSB(iargc, tmp);
1586 iargs = silc_argument_payload_parse(tmp + 2, tmp_len - 2, iargc);
1590 if (!channel->ban_list)
1592 silc_hash_table_alloc(0, silc_hash_ptr,
1594 silc_server_inviteban_destruct, channel, TRUE);
1596 /* Proces the ban action */
1597 if (!silc_server_inviteban_process(server, channel->ban_list, action,
1600 silc_argument_payload_free(iargs);
1602 /* If we are router we must send this notify to our local servers on
1603 the channel. Normal server does nothing. The notify is not
1605 if (server->server_type == SILC_ROUTER)
1606 silc_server_packet_send_to_channel(server, sock, channel,
1607 packet->type, FALSE, FALSE,
1609 silc_buffer_len(buffer));
1613 case SILC_NOTIFY_TYPE_ERROR:
1620 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
1621 if (!tmp && tmp_len != 1)
1623 error = (SilcStatus)tmp[0];
1625 SILC_LOG_DEBUG(("ERROR notify (%d)", error));
1627 if (error == SILC_STATUS_ERR_NO_SUCH_CLIENT_ID &&
1628 idata->conn_type == SILC_CONN_ROUTER) {
1629 if (!silc_argument_get_decoded(args, 2, SILC_ARGUMENT_ID, &id, NULL))
1632 SILC_LOG_DEBUG(("Received invalid client ID notification, deleting "
1633 "the entry from cache"));
1635 client = silc_idlist_find_client_by_id(server->global_list,
1641 silc_server_remove_from_channels(server, NULL, client, TRUE,
1643 silc_idlist_del_data(client);
1644 silc_idlist_del_client(server->global_list, client);
1649 /* Ignore rest of the notify types for now */
1650 case SILC_NOTIFY_TYPE_NONE:
1651 case SILC_NOTIFY_TYPE_MOTD:
1659 silc_notify_payload_free(payload);
1662 void silc_server_notify(SilcServer server,
1663 SilcPacketStream sock,
1666 silc_server_notify_process(server, sock, packet, &packet->buffer);
1667 silc_packet_free(packet);
1670 void silc_server_notify_list(SilcServer server,
1671 SilcPacketStream sock,
1674 SilcIDListData idata = silc_packet_get_context(sock);
1678 SILC_LOG_DEBUG(("Processing Notify List"));
1680 if (idata->conn_type == SILC_CONN_CLIENT ||
1681 packet->src_id_type != SILC_ID_SERVER)
1684 buffer = silc_buffer_alloc(1024);
1688 while (silc_buffer_len(&packet->buffer)) {
1689 SILC_GET16_MSB(len, packet->buffer.data + 2);
1690 if (len > silc_buffer_len(&packet->buffer))
1693 if (len > silc_buffer_truelen(buffer)) {
1694 silc_buffer_free(buffer);
1695 buffer = silc_buffer_alloc(1024 + len);
1698 silc_buffer_pull_tail(buffer, len);
1699 silc_buffer_put(buffer, packet->buffer.data, len);
1701 /* Process the Notify */
1702 silc_server_notify_process(server, sock, packet, buffer);
1704 silc_buffer_push_tail(buffer, len);
1705 silc_buffer_pull(&packet->buffer, len);
1708 silc_packet_free(packet);
1709 silc_buffer_free(buffer);
1712 /* Received private message. This resolves the destination of the message
1713 and sends the packet. This is used by both server and router. If the
1714 destination is our locally connected client this sends the packet to
1715 the client. This may also send the message for further routing if
1716 the destination is not in our server (or router). */
1718 void silc_server_private_message(SilcServer server,
1719 SilcPacketStream sock,
1722 SilcPacketStream dst_sock;
1723 SilcIDListData idata;
1724 SilcClientEntry client;
1725 SilcClientID client_id;
1727 SILC_LOG_DEBUG(("Start"));
1729 if (packet->src_id_type != SILC_ID_CLIENT ||
1730 packet->dst_id_type != SILC_ID_CLIENT || !packet->dst_id)
1733 /* Get the route to the client */
1734 dst_sock = silc_server_get_client_route(server, packet->dst_id,
1735 packet->dst_id_len, NULL,
1739 unsigned char error;
1741 if (client && client->mode & SILC_UMODE_DETACHED) {
1742 SILC_LOG_DEBUG(("Client is detached, discarding packet"));
1746 /* Send SILC_NOTIFY_TYPE_ERROR to indicate that such destination ID
1747 does not exist or is invalid. */
1748 idp = silc_id_payload_encode_data(packet->dst_id,
1750 packet->dst_id_type);
1754 error = SILC_STATUS_ERR_NO_SUCH_CLIENT_ID;
1755 if (packet->src_id_type == SILC_ID_CLIENT) {
1756 silc_id_str2id(packet->src_id, packet->src_id_len,
1757 packet->src_id_type, &client_id, sizeof(client_id));
1758 silc_server_send_notify_dest(server, sock, FALSE,
1759 &client_id, SILC_ID_CLIENT,
1760 SILC_NOTIFY_TYPE_ERROR, 2,
1762 idp->data, silc_buffer_len(idp));
1764 silc_server_send_notify(server, sock, FALSE,
1765 SILC_NOTIFY_TYPE_ERROR, 2,
1767 idp->data, silc_buffer_len(idp));
1770 silc_buffer_free(idp);
1774 /* Check whether destination client wishes to receive private messages */
1775 if (client && !(packet->flags & SILC_PACKET_FLAG_PRIVMSG_KEY) &&
1776 client->mode & SILC_UMODE_BLOCK_PRIVMSG) {
1777 SILC_LOG_DEBUG(("Client blocks private messages, discarding packet"));
1781 /* Send the private message */
1782 silc_server_packet_route(server, dst_sock, packet);
1785 silc_packet_free(packet);
1788 /* Received private message key packet.. This packet is never for us. It is to
1789 the client in the packet's destination ID. Sending of this sort of packet
1790 equals sending private message, ie. it is sent point to point from
1791 one client to another. */
1793 void silc_server_private_message_key(SilcServer server,
1794 SilcPacketStream sock,
1797 SilcPacketStream dst_sock;
1798 SilcIDListData idata;
1800 SILC_LOG_DEBUG(("Start"));
1802 if (packet->src_id_type != SILC_ID_CLIENT ||
1803 packet->dst_id_type != SILC_ID_CLIENT || !packet->dst_id) {
1804 silc_packet_free(packet);
1808 /* Get the route to the client */
1809 dst_sock = silc_server_get_client_route(server, packet->dst_id,
1810 packet->dst_id_len, NULL,
1813 silc_packet_free(packet);
1817 /* Relay the packet */
1818 silc_server_packet_route(server, dst_sock, packet);
1820 silc_packet_free(packet);
1823 /* Processes incoming command reply packet. The command reply packet may
1824 be destined to one of our clients or it may directly for us. We will
1825 call the command reply routine after processing the packet. */
1827 void silc_server_command_reply(SilcServer server,
1828 SilcPacketStream sock,
1831 SilcBuffer buffer = &packet->buffer;
1832 SilcClientEntry client = NULL;
1835 SILC_LOG_DEBUG(("Start"));
1837 if (packet->dst_id_type == SILC_ID_CHANNEL) {
1838 silc_packet_free(packet);
1842 if (packet->dst_id_type == SILC_ID_CLIENT) {
1843 /* Destination must be one of ours */
1844 if (!silc_id_str2id(packet->dst_id, packet->dst_id_len, SILC_ID_CLIENT,
1846 silc_packet_free(packet);
1849 client = silc_idlist_find_client_by_id(server->local_list, &id,
1852 SILC_LOG_ERROR(("Cannot process command reply to unknown client"));
1853 silc_packet_free(packet);
1858 if (packet->dst_id_type == SILC_ID_SERVER) {
1859 /* For now this must be for us */
1860 if (memcmp(packet->dst_id, server->id_string, server->id_string_len)) {
1861 SILC_LOG_ERROR(("Cannot process command reply to unknown server"));
1862 silc_packet_free(packet);
1867 /* Execute command reply locally for the command */
1868 silc_server_command_reply_process(server, sock, buffer);
1870 /* Relay the packet to the client */
1871 if (packet->dst_id_type == SILC_ID_CLIENT && client)
1872 silc_server_packet_route(server, client->connection, packet);
1874 silc_packet_free(packet);
1877 /* Process received channel message. The message can be originated from
1878 client or server. */
1880 void silc_server_channel_message(SilcServer server,
1881 SilcPacketStream sock,
1884 SilcChannelEntry channel = NULL;
1888 SilcClientEntry sender_entry = NULL;
1889 SilcIDListData idata;
1890 SilcChannelClientEntry chl;
1891 SilcBool local = TRUE;
1893 SILC_LOG_DEBUG(("Processing channel message"));
1896 if (packet->dst_id_type != SILC_ID_CHANNEL) {
1897 SILC_LOG_DEBUG(("Received bad message for channel, dropped"));
1901 /* Find channel entry */
1902 if (!silc_id_str2id(packet->dst_id, packet->dst_id_len, SILC_ID_CHANNEL,
1905 channel = silc_idlist_find_channel_by_id(server->local_list, &id, NULL);
1907 channel = silc_idlist_find_channel_by_id(server->global_list, &id, NULL);
1910 unsigned char error;
1912 /* Send SILC_NOTIFY_TYPE_ERROR to indicate that such destination ID
1913 does not exist or is invalid. */
1914 idp = silc_id_payload_encode_data(packet->dst_id,
1916 packet->dst_id_type);
1920 error = SILC_STATUS_ERR_NO_SUCH_CHANNEL_ID;
1921 if (packet->src_id_type == SILC_ID_CLIENT) {
1922 silc_id_str2id(packet->src_id, packet->src_id_len,
1923 packet->src_id_type, &cid, sizeof(cid));
1924 silc_server_send_notify_dest(server, sock, FALSE,
1925 &cid, SILC_ID_CLIENT,
1926 SILC_NOTIFY_TYPE_ERROR, 2,
1927 &error, 1, idp->data,
1928 silc_buffer_len(idp));
1930 silc_server_send_notify(server, sock, FALSE,
1931 SILC_NOTIFY_TYPE_ERROR, 2,
1932 &error, 1, idp->data, silc_buffer_len(idp));
1935 silc_buffer_free(idp);
1940 /* See that this client is on the channel. If the original sender is
1941 not client (as it can be server as well) we don't do the check. */
1942 if (!silc_id_str2id2(packet->src_id, packet->src_id_len,
1943 packet->src_id_type, &sid))
1945 if (sid.type == SILC_ID_CLIENT) {
1946 sender_entry = silc_idlist_find_client_by_id(server->local_list,
1947 SILC_ID_GET_ID(sid),
1949 if (!sender_entry) {
1951 sender_entry = silc_idlist_find_client_by_id(server->global_list,
1952 SILC_ID_GET_ID(sid),
1955 if (!sender_entry || !silc_server_client_on_channel(sender_entry,
1957 SILC_LOG_DEBUG(("Client not on channel"));
1961 /* If channel is moderated check that client is allowed to send
1963 if (channel->mode & SILC_CHANNEL_MODE_SILENCE_USERS &&
1964 !(chl->mode & SILC_CHANNEL_UMODE_CHANOP) &&
1965 !(chl->mode & SILC_CHANNEL_UMODE_CHANFO)) {
1966 SILC_LOG_DEBUG(("Channel is silenced from normal users"));
1969 if (channel->mode & SILC_CHANNEL_MODE_SILENCE_OPERS &&
1970 chl->mode & SILC_CHANNEL_UMODE_CHANOP &&
1971 !(chl->mode & SILC_CHANNEL_UMODE_CHANFO)) {
1972 SILC_LOG_DEBUG(("Channel is silenced from operators"));
1975 if (chl->mode & SILC_CHANNEL_UMODE_QUIET) {
1976 SILC_LOG_DEBUG(("Sender is quieted on the channel"));
1980 /* If the packet is coming from router, but the client entry is local
1981 entry to us then some router is rerouting this to us and it is not
1982 allowed. When the client is local to us it means that we've routed
1983 this packet to network, and now someone is routing it back to us. */
1984 idata = silc_packet_get_context(sock);
1985 if (server->server_type == SILC_ROUTER &&
1986 idata->conn_type == SILC_CONN_ROUTER && local) {
1987 SILC_LOG_DEBUG(("Channel message rerouted to the sender, drop it"));
1992 /* Distribute the packet to our local clients. This will send the
1993 packet for further routing as well, if needed. */
1994 silc_server_packet_relay_to_channel(server, sock, channel,
1995 SILC_ID_GET_ID(sid), sid.type,
1996 sender_entry, packet->buffer.data,
1997 silc_buffer_len(&packet->buffer));
2000 silc_packet_free(packet);
2003 /* Received channel key packet. We distribute the key to all of our locally
2004 connected clients on the channel. */
2006 void silc_server_channel_key(SilcServer server,
2007 SilcPacketStream sock,
2010 SilcBuffer buffer = &packet->buffer;
2011 SilcIDListData idata = silc_packet_get_context(sock);
2012 SilcChannelEntry channel;
2014 if (packet->src_id_type != SILC_ID_SERVER ||
2015 (server->server_type == SILC_ROUTER && !server->backup_router &&
2016 idata->conn_type == SILC_CONN_ROUTER)) {
2017 silc_packet_free(packet);
2021 /* Save the channel key */
2022 channel = silc_server_save_channel_key(server, buffer, NULL);
2024 SILC_LOG_ERROR(("Bad channel key from %s", idata->sconn->remote_host));
2025 silc_packet_free(packet);
2029 /* Distribute the key to everybody who is on the channel. If we are router
2030 we will also send it to locally connected servers. */
2031 silc_server_send_channel_key(server, sock, channel, FALSE);
2033 if (server->server_type != SILC_BACKUP_ROUTER)
2034 /* Distribute to local cell backup routers. */
2035 silc_server_backup_send(server, (SilcServerEntry)idata,
2036 SILC_PACKET_CHANNEL_KEY, 0,
2037 buffer->data, silc_buffer_len(buffer),
2040 silc_packet_free(packet);
2043 /* Received New Client packet and processes it. Creates Client ID for the
2044 client. Client becomes registered after calling this functions. */
2046 SilcClientEntry silc_server_new_client(SilcServer server,
2047 SilcPacketStream sock,
2050 SilcBuffer buffer = &packet->buffer;
2051 SilcIDListData idata = silc_packet_get_context(sock);
2052 SilcClientEntry client;
2053 SilcClientID *client_id;
2054 char *username = NULL, *realname = NULL;
2055 SilcUInt16 username_len, nickname_len;
2056 SilcUInt32 id_len, tmp_len;
2058 char *host, *nickname = NULL, *nicknamec;
2059 const char *hostname, *ip;
2061 SILC_LOG_DEBUG(("Creating new client"));
2063 if (idata->conn_type != SILC_CONN_CLIENT) {
2064 silc_packet_free(packet);
2068 /* Take client entry */
2069 client = (SilcClientEntry)idata;
2070 silc_socket_stream_get_info(silc_packet_stream_get_stream(sock),
2071 NULL, &hostname, &ip, NULL);
2073 SILC_LOG_DEBUG(("%s %s", ip, hostname));
2075 /* Make sure this client hasn't registered already */
2076 if (idata->status & SILC_IDLIST_STATUS_REGISTERED) {
2077 silc_packet_free(packet);
2081 /* Parse incoming packet */
2082 ret = silc_buffer_unformat(buffer,
2084 SILC_STR_UI16_NSTRING_ALLOC(&username,
2086 SILC_STR_UI16_STRING_ALLOC(&realname),
2089 silc_free(username);
2090 silc_free(realname);
2091 SILC_LOG_ERROR(("Client %s (%s) sent incomplete information, closing "
2092 "connection", hostname, ip));
2093 silc_server_disconnect_remote(server, sock,
2094 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
2096 silc_server_free_sock_user_data(server, sock, NULL);
2097 silc_packet_free(packet);
2102 silc_free(username);
2103 silc_free(realname);
2104 SILC_LOG_ERROR(("Client %s (%s) did not send its username, closing "
2105 "connection", hostname, ip));
2106 silc_server_disconnect_remote(server, sock,
2107 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
2109 silc_server_free_sock_user_data(server, sock, NULL);
2110 silc_packet_free(packet);
2114 if (username_len > 128) {
2116 username[username_len - 1] = '\0';
2119 /* Take nickname from NEW_CLIENT packet, if present */
2120 if (silc_buffer_unformat(buffer,
2121 SILC_STR_UI16_NSTRING_ALLOC(&nickname,
2124 if (nickname_len > 128) {
2126 nickname[nickname_len - 1] = '\0';
2130 /* Nickname is initially same as username, if not present in NEW_CLIENT */
2132 nickname = strdup(username);
2133 nickname_len = strlen(nickname);
2136 /* Check for valid username string */
2137 nicknamec = silc_identifier_check(nickname, nickname_len,
2138 SILC_STRING_UTF8, 128, &tmp_len);
2140 silc_free(username);
2141 silc_free(realname);
2142 silc_free(nickname);
2143 SILC_LOG_ERROR(("Client %s (%s) sent bad username string '%s', closing "
2144 "connection", hostname, ip, username));
2145 silc_server_disconnect_remote(server, sock,
2146 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
2148 silc_server_free_sock_user_data(server, sock, NULL);
2149 silc_packet_free(packet);
2153 /* Make sanity checks for the hostname of the client. If the hostname
2154 is provided in the `username' check that it is the same than the
2155 resolved hostname, or if not resolved the hostname that appears in
2156 the client's public key. If the hostname is not present then put
2157 it from the resolved name or from the public key. */
2158 if (strchr(username, '@')) {
2159 SilcSILCPublicKey silc_pubkey;
2160 int tlen = strcspn(username, "@");
2161 char *phostname = NULL;
2163 host = silc_memdup(username + tlen + 1, strlen(username) - tlen - 1);
2165 if (strcmp(hostname, ip) && strcmp(hostname, host)) {
2166 silc_free(nickname);
2167 silc_free(username);
2169 silc_free(realname);
2170 SILC_LOG_ERROR(("Client %s (%s) sent incomplete information, closing "
2171 "connection", hostname, ip));
2172 silc_server_disconnect_remote(server, sock,
2173 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
2175 silc_server_free_sock_user_data(server, sock, NULL);
2176 silc_packet_free(packet);
2180 silc_pubkey = silc_pkcs_get_context(SILC_PKCS_SILC,
2181 client->data.public_key);
2182 phostname = strdup(silc_pubkey->identifier.host);
2183 if (!strcmp(hostname, ip) && phostname && strcmp(phostname, host)) {
2184 silc_free(nickname);
2185 silc_free(username);
2187 silc_free(phostname);
2188 silc_free(realname);
2189 SILC_LOG_ERROR(("Client %s (%s) sent incomplete information, closing "
2190 "connection", hostname, ip));
2191 silc_server_disconnect_remote(server, sock,
2192 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
2194 silc_server_free_sock_user_data(server, sock, NULL);
2195 silc_packet_free(packet);
2199 silc_free(phostname);
2201 /* The hostname is not present, add it. */
2203 newusername = silc_calloc(strlen(username) + strlen(hostname) + 2,
2204 sizeof(*newusername));
2205 strncat(newusername, username, strlen(username));
2206 strncat(newusername, "@", 1);
2207 strncat(newusername, hostname, strlen(hostname));
2208 silc_free(username);
2209 username = newusername;
2212 SILC_LOG_DEBUG(("%s %s", ip, hostname));
2214 /* Create Client ID */
2215 if (!silc_id_create_client_id(server, server->id, server->rng,
2216 server->md5hash, nicknamec,
2217 strlen(nicknamec), &client_id)) {
2218 silc_server_disconnect_remote(server, sock,
2219 SILC_STATUS_ERR_BAD_NICKNAME, NULL);
2220 silc_server_free_sock_user_data(server, sock, NULL);
2221 silc_packet_free(packet);
2225 /* If client marked as anonymous, scramble the username and hostname */
2226 if (client->mode & SILC_UMODE_ANONYMOUS) {
2229 if (strlen(username) >= 2) {
2230 username[0] = silc_rng_get_byte_fast(server->rng);
2231 username[1] = silc_rng_get_byte_fast(server->rng);
2234 scramble = silc_hash_babbleprint(server->sha1hash, username,
2238 memcpy(&scramble[16], ".silc", 5);
2239 scramble[21] = '\0';
2240 silc_free(username);
2241 username = scramble;
2244 /* Update client entry */
2245 idata->status |= SILC_IDLIST_STATUS_REGISTERED;
2246 client->nickname = nickname;
2247 client->username = username;
2248 client->userinfo = realname ? realname : strdup(username);
2249 client->id = client_id;
2250 id_len = silc_id_get_len(client_id, SILC_ID_CLIENT);
2251 silc_idcache_update_by_context(server->local_list->clients, client,
2252 client_id, nicknamec, TRUE);
2254 /* Notify our router about new client on the SILC network */
2255 silc_server_send_new_id(server, SILC_PRIMARY_ROUTE(server),
2256 SILC_BROADCAST(server), client->id,
2257 SILC_ID_CLIENT, id_len);
2259 /* Distribute to backup routers */
2260 if (server->server_type == SILC_ROUTER) {
2261 SilcBuffer idp = silc_id_payload_encode(client->id, SILC_ID_CLIENT);
2262 silc_server_backup_send(server, NULL, SILC_PACKET_NEW_ID, 0,
2263 idp->data, silc_buffer_len(idp), FALSE, TRUE);
2264 silc_buffer_free(idp);
2267 /* Send the new client ID to the client. */
2268 silc_server_send_new_id(server, sock, FALSE, client->id, SILC_ID_CLIENT,
2269 silc_id_get_len(client->id, SILC_ID_CLIENT));
2271 /* Send some nice info to the client */
2272 silc_server_send_connect_notifys(server, sock, client);
2274 /* Check if anyone is watching this nickname */
2275 if (server->server_type == SILC_ROUTER)
2276 silc_server_check_watcher_list(server, client, NULL, 0);
2278 silc_packet_free(packet);
2282 /* Create new server. This processes received New Server packet and
2283 saves the received Server ID. The server is our locally connected
2284 server thus we save all the information and save it to local list.
2285 This funtion can be used by both normal server and router server.
2286 If normal server uses this it means that its router has connected
2287 to the server. If router uses this it means that one of the cell's
2288 servers is connected to the router. */
2290 SilcServerEntry silc_server_new_server(SilcServer server,
2291 SilcPacketStream sock,
2294 SilcBuffer buffer = &packet->buffer;
2295 SilcIDListData idata = silc_packet_get_context(sock);
2296 SilcServerEntry new_server, server_entry;
2297 SilcServerID server_id;
2298 unsigned char *server_name, *server_namec, *id_string;
2299 SilcUInt16 id_len, name_len;
2301 SilcBool local = TRUE;
2302 const char *hostname, *ip;
2304 SILC_LOG_DEBUG(("Creating new server"));
2306 if (idata->conn_type != SILC_CONN_SERVER &&
2307 idata->conn_type != SILC_CONN_ROUTER) {
2308 silc_packet_free(packet);
2312 /* Take server entry */
2313 new_server = (SilcServerEntry)idata;
2314 silc_socket_stream_get_info(silc_packet_stream_get_stream(sock),
2315 NULL, &hostname, &ip, NULL);
2318 if (server->server_type == SILC_ROUTER)
2319 server->stat.cell_servers++;
2321 /* Remove the old cache entry */
2322 if (!silc_idcache_del_by_context(server->local_list->servers, new_server,
2324 if (!silc_idcache_del_by_context(server->global_list->servers,
2325 new_server, NULL)) {
2326 SILC_LOG_INFO(("Unauthenticated %s attempted to register to "
2327 "network", (idata->conn_type == SILC_CONN_SERVER ?
2328 "server" : "router")));
2329 silc_server_disconnect_remote(server, sock,
2330 SILC_STATUS_ERR_NOT_AUTHENTICATED, NULL);
2331 silc_server_free_sock_user_data(server, sock, NULL);
2337 /* Make sure this server hasn't registered already */
2338 if (idata->status & SILC_IDLIST_STATUS_REGISTERED) {
2339 silc_server_disconnect_remote(server, sock,
2340 SILC_STATUS_ERR_OPERATION_ALLOWED,
2341 "Too many registrations");
2342 silc_server_free_sock_user_data(server, sock, NULL);
2346 /* Parse the incoming packet */
2347 ret = silc_buffer_unformat(buffer,
2348 SILC_STR_UI16_NSTRING_ALLOC(&id_string, &id_len),
2349 SILC_STR_UI16_NSTRING_ALLOC(&server_name,
2353 silc_free(id_string);
2354 silc_free(server_name);
2355 silc_server_disconnect_remote(server, sock,
2356 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
2358 silc_server_free_sock_user_data(server, sock, NULL);
2362 if (id_len > silc_buffer_len(buffer)) {
2363 silc_free(id_string);
2364 silc_free(server_name);
2365 silc_server_disconnect_remote(server, sock,
2366 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
2368 silc_server_free_sock_user_data(server, sock, NULL);
2372 if (name_len > 256) {
2373 server_name[256] = '\0';
2378 if (!silc_id_str2id(id_string, id_len, SILC_ID_SERVER, &server_id,
2379 sizeof(server_id))) {
2380 silc_free(id_string);
2381 silc_free(server_name);
2382 silc_server_disconnect_remote(server, sock,
2383 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
2385 silc_server_free_sock_user_data(server, sock, NULL);
2388 silc_free(id_string);
2390 /* Check for valid server ID */
2391 if (!silc_id_is_valid_server_id(server, &server_id, sock)) {
2392 SILC_LOG_INFO(("Invalid server ID sent by %s (%s)",
2394 silc_server_disconnect_remote(server, sock,
2395 SILC_STATUS_ERR_BAD_SERVER_ID, NULL);
2396 silc_server_free_sock_user_data(server, sock, NULL);
2397 silc_free(server_name);
2401 /* Check that we do not have this ID already */
2402 server_entry = silc_idlist_find_server_by_id(server->local_list,
2403 &server_id, TRUE, NULL);
2405 if (SILC_IS_LOCAL(server_entry)) {
2406 SILC_LOG_ERROR(("Too many registrations from %s (%s)",
2408 silc_server_disconnect_remote(server, sock,
2409 SILC_STATUS_ERR_OPERATION_ALLOWED,
2410 "Too many registrations");
2411 silc_server_free_sock_user_data(server, sock, NULL);
2414 silc_idcache_del_by_context(server->local_list->servers, server_entry,
2418 server_entry = silc_idlist_find_server_by_id(server->global_list,
2419 &server_id, TRUE, NULL);
2421 if (SILC_IS_LOCAL(server_entry)) {
2422 SILC_LOG_ERROR(("Too many registrations from %s (%s)",
2424 silc_server_disconnect_remote(server, sock,
2425 SILC_STATUS_ERR_OPERATION_ALLOWED,
2426 "Too many registrations");
2427 silc_server_free_sock_user_data(server, sock, NULL);
2430 silc_idcache_del_by_context(server->global_list->servers,
2431 server_entry, NULL);
2436 /* Check server name */
2437 server_namec = silc_identifier_check(server_name, strlen(server_name),
2438 SILC_STRING_UTF8, 256, NULL);
2439 if (!server_namec) {
2440 SILC_LOG_ERROR(("Malformed server name from %s (%s)",
2442 silc_server_disconnect_remote(server, sock,
2443 SILC_STATUS_ERR_OPERATION_ALLOWED,
2444 "Malfromed server name");
2445 silc_server_free_sock_user_data(server, sock, NULL);
2449 /* Update server entry */
2450 idata->status |= SILC_IDLIST_STATUS_REGISTERED;
2451 new_server->server_name = server_name;
2452 new_server->id = silc_id_dup(&server_id, SILC_ID_SERVER);
2454 SILC_LOG_DEBUG(("New server id(%s)",
2455 silc_id_render(&server_id, SILC_ID_SERVER)));
2457 /* Add again the entry to the ID cache. */
2458 silc_idcache_add(local ? server->local_list->servers :
2459 server->global_list->servers, server_namec,
2460 new_server->id, new_server);
2462 /* Distribute the information about new server in the SILC network
2463 to our router. If we are normal server we won't send anything
2464 since this connection must be our router connection. */
2465 if (server->server_type == SILC_ROUTER && !server->standalone &&
2466 SILC_PRIMARY_ROUTE(server) != sock)
2467 silc_server_send_new_id(server, SILC_PRIMARY_ROUTE(server),
2468 TRUE, new_server->id, SILC_ID_SERVER,
2469 silc_id_get_len(&server_id, SILC_ID_SERVER));
2471 if (server->server_type == SILC_ROUTER) {
2472 /* Distribute to backup routers */
2473 SilcBuffer idp = silc_id_payload_encode(new_server->id, SILC_ID_SERVER);
2474 silc_server_backup_send(server, NULL, SILC_PACKET_NEW_ID, 0, idp->data,
2475 silc_buffer_len(idp), FALSE, TRUE);
2476 silc_buffer_free(idp);
2479 /* Check whether this router connection has been replaced by an
2480 backup router. If it has been then we'll disable the server and will
2481 ignore everything it will send until the backup router resuming
2482 protocol has been completed. */
2483 if (idata->conn_type == SILC_CONN_ROUTER &&
2484 silc_server_backup_replaced_get(server, &server_id, NULL)) {
2485 /* Send packet to the router indicating that it cannot use this
2486 connection as it has been replaced by backup router. */
2487 SILC_LOG_DEBUG(("Remote router has been replaced by backup router, "
2488 "disabling its connection"));
2490 silc_server_backup_send_replaced(server, sock);
2492 /* Mark the router disabled. The data sent earlier will go but nothing
2493 after this goes to this connection. */
2494 idata->status |= SILC_IDLIST_STATUS_DISABLED;
2496 /* If it is router announce our stuff to it. */
2497 if (idata->conn_type == SILC_CONN_ROUTER &&
2498 server->server_type == SILC_ROUTER) {
2499 silc_server_announce_servers(server, FALSE, 0, sock);
2500 silc_server_announce_clients(server, 0, sock);
2501 silc_server_announce_channels(server, 0, sock);
2504 /* Announce our information to backup router */
2505 if (new_server->server_type == SILC_BACKUP_ROUTER &&
2506 idata->conn_type == SILC_CONN_SERVER &&
2507 server->server_type == SILC_ROUTER) {
2508 silc_server_announce_servers(server, TRUE, 0, sock);
2509 silc_server_announce_clients(server, 0, sock);
2510 silc_server_announce_channels(server, 0, sock);
2513 /* If backup router, mark it as one of ours. This server is considered
2514 to be backup router after this setting. */
2515 if (new_server->server_type == SILC_BACKUP_ROUTER) {
2516 SilcServerConfigRouter *backup;
2517 backup = silc_server_config_find_backup_conn(server, (char *)ip);
2519 backup = silc_server_config_find_backup_conn(server, (char *)hostname);
2521 /* Add as our backup router */
2522 silc_server_backup_add(server, new_server, backup->backup_replace_ip,
2523 backup->backup_replace_port,
2524 backup->backup_local);
2528 /* By default the servers connected to backup router are disabled
2529 until backup router has become the primary */
2530 if (server->server_type == SILC_BACKUP_ROUTER &&
2531 idata->conn_type == SILC_CONN_SERVER)
2532 idata->status |= SILC_IDLIST_STATUS_DISABLED;
2538 /* Processes incoming New ID packet. New ID Payload is used to distribute
2539 information about newly registered clients and servers. */
2541 static void silc_server_new_id_real(SilcServer server,
2542 SilcPacketStream sock,
2547 SilcIDListData idata = silc_packet_get_context(sock);
2549 SilcServerEntry router, server_entry;
2550 SilcPacketStream router_sock;
2553 SilcServerID sender_id;
2554 const char *hostname, *ip;
2556 SILC_LOG_DEBUG(("Processing new ID"));
2558 if (idata->conn_type == SILC_CONN_CLIENT ||
2559 server->server_type == SILC_SERVER ||
2560 packet->src_id_type != SILC_ID_SERVER)
2563 idp = silc_id_payload_parse(buffer->data, silc_buffer_len(buffer));
2567 id_type = silc_id_payload_get_type(idp);
2569 silc_socket_stream_get_info(silc_packet_stream_get_stream(sock),
2570 NULL, &hostname, &ip, NULL);
2572 /* Normal server cannot have other normal server connections */
2573 server_entry = (SilcServerEntry)idata;
2574 if (id_type == SILC_ID_SERVER && idata->conn_type == SILC_CONN_SERVER &&
2575 server_entry->server_type == SILC_SERVER)
2578 /* If the packet is coming from server then use the sender as the
2579 origin of the the packet. If it came from router then check the real
2580 sender of the packet and use that as the origin. */
2581 if (idata->conn_type == SILC_CONN_SERVER) {
2582 id_list = server->local_list;
2584 router = server_entry;
2586 /* If the sender is backup router and ID is server (and we are not
2587 backup router) then switch the entry to global list. */
2588 if (server_entry->server_type == SILC_BACKUP_ROUTER &&
2589 id_type == SILC_ID_SERVER &&
2590 server->id_entry->server_type != SILC_BACKUP_ROUTER) {
2591 id_list = server->global_list;
2592 router_sock = server->router ? SILC_PRIMARY_ROUTE(server) : sock;
2595 silc_id_str2id(packet->src_id, packet->src_id_len,
2596 packet->src_id_type, &sender_id, sizeof(sender_id));
2597 router = silc_idlist_find_server_by_id(server->global_list,
2598 &sender_id, TRUE, NULL);
2600 router = silc_idlist_find_server_by_id(server->local_list,
2601 &sender_id, TRUE, NULL);
2603 id_list = server->global_list;
2610 case SILC_ID_CLIENT:
2612 SilcClientEntry entry;
2615 if (!silc_id_payload_get_id(idp, &id, sizeof(id)))
2618 /* Check that we do not have this client already */
2619 entry = silc_idlist_find_client_by_id(server->global_list,
2620 &id, server->server_type,
2623 entry = silc_idlist_find_client_by_id(server->local_list,
2624 &id, server->server_type,
2627 SILC_LOG_DEBUG(("Ignoring client that we already have"));
2631 SILC_LOG_DEBUG(("New client id(%s) from [%s] %s",
2632 silc_id_render(&id, SILC_ID_CLIENT),
2633 idata->conn_type == SILC_CONN_SERVER ?
2634 "Server" : "Router", hostname));
2636 /* As a router we keep information of all global information in our
2637 global list. Cell wide information however is kept in the local
2639 entry = silc_idlist_add_client(id_list, NULL, NULL, NULL,
2642 SILC_LOG_ERROR(("Could not add new client to the ID Cache"));
2644 /* Inform the sender that the ID is not usable */
2645 silc_server_send_notify_signoff(server, sock, FALSE, &id, NULL);
2648 entry->nickname = NULL;
2649 entry->data.status |= SILC_IDLIST_STATUS_REGISTERED;
2651 if (idata->conn_type == SILC_CONN_SERVER)
2652 server->stat.cell_clients++;
2653 server->stat.clients++;
2655 /* Check if anyone is watching this nickname */
2656 if (server->server_type == SILC_ROUTER && id_list == server->local_list)
2657 silc_server_check_watcher_list(server, entry, NULL, 0);
2659 if (server->server_type == SILC_ROUTER) {
2660 /* Add the client's public key to repository or get the key with
2662 if (entry->data.public_key) {
2663 if (!silc_server_get_public_key_by_client(server, entry, NULL))
2664 silc_skr_add_public_key_simple(server->repository,
2665 entry->data.public_key,
2666 SILC_SKR_USAGE_IDENTIFICATION,
2669 silc_server_send_command(server, router_sock,
2670 SILC_COMMAND_GETKEY, ++server->cmd_ident,
2672 silc_buffer_len(buffer));
2678 case SILC_ID_SERVER:
2680 SilcServerEntry entry;
2683 if (!silc_id_payload_get_id(idp, &id, sizeof(id)))
2686 /* If the ID is mine, ignore it. */
2687 if (SILC_ID_SERVER_COMPARE(&id, server->id)) {
2688 SILC_LOG_DEBUG(("Ignoring my own ID as new ID"));
2692 /* If the ID is the sender's ID, ignore it (we have it already) */
2693 if (SILC_ID_SERVER_COMPARE(&id, router->id)) {
2694 SILC_LOG_DEBUG(("Ignoring sender's own ID"));
2698 /* Check that we do not have this server already */
2699 entry = silc_idlist_find_server_by_id(server->global_list,
2700 &id, server->server_type,
2703 entry = silc_idlist_find_server_by_id(server->local_list,
2704 &id, server->server_type,
2707 SILC_LOG_DEBUG(("Ignoring server that we already have"));
2711 SILC_LOG_DEBUG(("New server id(%s) from [%s] %s",
2712 silc_id_render(&id, SILC_ID_SERVER),
2713 idata->conn_type == SILC_CONN_SERVER ?
2714 "Server" : "Router", hostname));
2716 /* As a router we keep information of all global information in our
2717 global list. Cell wide information however is kept in the local
2719 entry = silc_idlist_add_server(id_list, NULL, 0, &id, router,
2722 SILC_LOG_ERROR(("Could not add new server to the ID Cache"));
2725 entry->data.status |= SILC_IDLIST_STATUS_REGISTERED;
2727 if (idata->conn_type == SILC_CONN_SERVER)
2728 server->stat.cell_servers++;
2729 server->stat.servers++;
2733 case SILC_ID_CHANNEL:
2734 SILC_LOG_ERROR(("Channel cannot be registered with NEW_ID packet"));
2743 /* If the sender of this packet is server and we are router we need to
2744 broadcast this packet to other routers in the network. */
2745 if (broadcast && server->server_type == SILC_ROUTER &&
2746 idata->conn_type == SILC_CONN_SERVER &&
2747 !(packet->flags & SILC_PACKET_FLAG_BROADCAST)) {
2748 SILC_LOG_DEBUG(("Broadcasting received New ID packet"));
2749 silc_server_packet_send(server, SILC_PRIMARY_ROUTE(server),
2751 packet->flags | SILC_PACKET_FLAG_BROADCAST,
2752 buffer->data, silc_buffer_len(buffer));
2753 silc_server_backup_send(server, (SilcServerEntry)idata,
2754 packet->type, packet->flags,
2755 packet->buffer.data,
2756 silc_buffer_len(&packet->buffer),
2761 silc_id_payload_free(idp);
2765 /* Processes incoming New ID packet. New ID Payload is used to distribute
2766 information about newly registered clients and servers. */
2768 void silc_server_new_id(SilcServer server, SilcPacketStream sock,
2771 silc_server_new_id_real(server, sock, packet, &packet->buffer, TRUE);
2772 silc_packet_free(packet);
2775 /* Receoved New Id List packet, list of New ID payloads inside one
2776 packet. Process the New ID payloads one by one. */
2778 void silc_server_new_id_list(SilcServer server, SilcPacketStream sock,
2781 SilcIDListData idata = silc_packet_get_context(sock);
2785 SILC_LOG_DEBUG(("Processing New ID List"));
2787 if (idata->conn_type == SILC_CONN_CLIENT ||
2788 packet->src_id_type != SILC_ID_SERVER) {
2789 silc_packet_free(packet);
2793 /* If the sender of this packet is server and we are router we need to
2794 broadcast this packet to other routers in the network. Broadcast
2795 this list packet instead of multiple New ID packets. */
2796 if (server->server_type == SILC_ROUTER &&
2797 idata->conn_type == SILC_CONN_SERVER &&
2798 !(packet->flags & SILC_PACKET_FLAG_BROADCAST)) {
2799 SILC_LOG_DEBUG(("Broadcasting received New ID List packet"));
2800 silc_server_packet_send(server, SILC_PRIMARY_ROUTE(server),
2802 packet->flags | SILC_PACKET_FLAG_BROADCAST,
2803 packet->buffer.data,
2804 silc_buffer_len(&packet->buffer));
2805 silc_server_backup_send(server, (SilcServerEntry)idata,
2806 packet->type, packet->flags,
2807 packet->buffer.data,
2808 silc_buffer_len(&packet->buffer),
2812 idp = silc_buffer_alloc(256);
2814 silc_packet_free(packet);
2818 while (silc_buffer_len(&packet->buffer)) {
2819 SILC_GET16_MSB(id_len, packet->buffer.data + 2);
2820 if ((id_len > silc_buffer_len(&packet->buffer)) ||
2821 (id_len > silc_buffer_truelen(idp)))
2824 silc_buffer_pull_tail(idp, 4 + id_len);
2825 silc_buffer_put(idp, packet->buffer.data, 4 + id_len);
2827 /* Process the New ID */
2828 silc_server_new_id_real(server, sock, packet, idp, FALSE);
2830 silc_buffer_push_tail(idp, 4 + id_len);
2831 silc_buffer_pull(&packet->buffer, 4 + id_len);
2834 silc_buffer_free(idp);
2835 silc_packet_free(packet);
2838 /* Received New Channel packet. Information about new channels in the
2839 network are distributed using this packet. Save the information about
2840 the new channel. This usually comes from router but also normal server
2841 can send this to notify channels it has when it connects to us. */
2843 static void silc_server_new_channel_process(SilcServer server,
2844 SilcPacketStream sock,
2848 SilcIDListData idata = silc_packet_get_context(sock);
2849 SilcChannelPayload payload;
2850 SilcChannelID channel_id;
2851 char *channel_name, *channel_namec = NULL;
2852 SilcUInt32 name_len;
2853 unsigned char *id, cid[32];
2854 SilcUInt32 id_len, cipher_len;
2855 SilcServerEntry server_entry;
2856 SilcChannelEntry channel;
2859 if (idata->conn_type == SILC_CONN_CLIENT ||
2860 packet->src_id_type != SILC_ID_SERVER ||
2861 server->server_type == SILC_SERVER)
2864 /* Parse the channel payload */
2865 payload = silc_channel_payload_parse(buffer->data, silc_buffer_len(buffer));
2869 /* Get the channel ID */
2870 if (!silc_channel_get_id_parse(payload, &channel_id)) {
2871 silc_channel_payload_free(payload);
2875 channel_name = silc_channel_get_name(payload, &name_len);
2876 if (name_len > 256) {
2877 channel_name[256] = '\0';
2881 /* Check channel name */
2882 channel_namec = silc_channel_name_check(channel_name, strlen(channel_name),
2883 SILC_STRING_UTF8, 256, NULL);
2887 id = silc_channel_get_id(payload, &id_len);
2889 server_entry = (SilcServerEntry)idata;
2891 if (idata->conn_type == SILC_CONN_ROUTER) {
2892 /* Add the channel to global list as it is coming from router. It
2893 cannot be our own channel as it is coming from router. */
2895 /* Check that we don't already have this channel */
2896 channel = silc_idlist_find_channel_by_name(server->local_list,
2897 channel_namec, NULL);
2899 channel = silc_idlist_find_channel_by_name(server->global_list,
2900 channel_namec, NULL);
2902 SILC_LOG_DEBUG(("New channel id(%s) from [Router] %s",
2903 silc_id_render(&channel_id, SILC_ID_CHANNEL),
2904 idata->sconn->remote_host));
2907 silc_idlist_add_channel(server->global_list, strdup(channel_name),
2908 0, silc_id_dup(&channel_id, SILC_ID_CHANNEL),
2909 (SilcServerEntry)idata, NULL, NULL, NULL);
2911 silc_channel_payload_free(payload);
2914 channel->disabled = TRUE; /* Disabled until someone JOINs */
2916 server->stat.channels++;
2917 if (server->server_type == SILC_ROUTER)
2918 channel->users_resolved = TRUE;
2921 /* The channel is coming from our server, thus it is in our cell
2922 we will add it to our local list. */
2925 SILC_LOG_DEBUG(("Channel id(%s) from [Server] %s",
2926 silc_id_render(&channel_id, SILC_ID_CHANNEL),
2927 idata->sconn->remote_host));
2929 /* Check that we don't already have this channel */
2930 channel = silc_idlist_find_channel_by_name(server->local_list,
2931 channel_namec, NULL);
2933 channel = silc_idlist_find_channel_by_name(server->global_list,
2934 channel_namec, NULL);
2936 /* If the channel does not exist, then create it. This creates a new
2937 key to the channel as well that we will send to the server. */
2939 SILC_LOG_DEBUG(("Channel is new to us"));
2941 /* The protocol says that the Channel ID's IP address must be based
2942 on the router's IP address. Check whether the ID is based in our
2943 IP and if it is not then create a new ID and enforce the server
2944 to switch the ID. */
2945 if (server_entry->server_type != SILC_BACKUP_ROUTER &&
2946 !SILC_ID_COMPARE(&channel_id, server->id, server->id->ip.data_len)) {
2948 SILC_LOG_DEBUG(("Forcing the server to change Channel ID"));
2949 if (silc_id_create_channel_id(server, server->id, server->rng, &tmp)) {
2950 silc_server_send_notify_channel_change(server, sock, FALSE,
2952 silc_channel_payload_free(payload);
2956 /* Wait that server re-announces this channel */
2960 /* Create the channel with the provided Channel ID */
2961 channel = silc_server_create_new_channel_with_id(server, NULL, NULL,
2963 &channel_id, FALSE);
2965 silc_channel_payload_free(payload);
2968 channel->disabled = TRUE; /* Disabled until someone JOINs */
2970 #if 0 /* We assume that CMODE_CHANGE notify is sent to us after this. */
2972 /* XXX Dunno if this is supposed to be set in any server type. If set
2973 here the CMODE_CHANGE that may follow sets mode that we already
2974 have, and we may loose data from the CMODE_CHANGE notify. */
2975 if (server_entry->server_type != SILC_BACKUP_ROUTER)
2976 channel->mode = silc_channel_get_mode(payload);
2979 /* Send the new channel key to the server */
2980 silc_id_id2str(channel->id, SILC_ID_CHANNEL, cid, sizeof(cid),
2982 cipher = silc_cipher_get_name(channel->send_key);
2983 cipher_len = strlen(cipher);
2984 chk = silc_channel_key_payload_encode(id_len, cid,
2986 channel->key_len / 8,
2988 silc_server_packet_send(server, sock, SILC_PACKET_CHANNEL_KEY, 0,
2989 chk->data, silc_buffer_len(chk));
2990 silc_buffer_free(chk);
2992 /* The channel exist by that name, check whether the ID's match.
2993 If they don't then we'll force the server to use the ID we have.
2994 We also create a new key for the channel. */
2995 SilcBuffer modes = NULL, users = NULL, users_modes = NULL;
2997 SILC_LOG_DEBUG(("Channel already exists"));
2999 if (!SILC_ID_CHANNEL_COMPARE(&channel_id, channel->id)) {
3000 /* They don't match, send CHANNEL_CHANGE notify to the server to
3001 force the ID change. */
3002 SILC_LOG_DEBUG(("Forcing the server to change Channel ID"));
3003 silc_server_send_notify_channel_change(server, sock, FALSE,
3004 &channel_id, channel->id);
3005 silc_channel_payload_free(payload);
3007 /* Wait that server re-announces this channel */
3011 #if 0 /* We will announce our CMODE anyway for this channel, so no need
3012 to check it (implicit enforce). */
3014 /* If the mode is different from what we have then enforce the
3016 mode = silc_channel_get_mode(payload);
3017 if (channel->mode != mode) {
3018 SILC_LOG_DEBUG(("Forcing the server to change channel mode"));
3019 silc_server_send_notify_cmode(server, sock, FALSE, channel,
3020 channel->mode, server->id,
3021 SILC_ID_SERVER, channel->cipher,
3023 channel->passphrase,
3024 channel->founder_key);
3028 /* Create new key for the channel and send it to the server and
3029 everybody else possibly on the channel. */
3030 if (!(channel->mode & SILC_CHANNEL_MODE_PRIVKEY)) {
3032 if (silc_hash_table_count(channel->user_list)) {
3033 if (!silc_server_create_channel_key(server, channel, 0)) {
3034 silc_channel_payload_free(payload);
3038 /* Send to the channel */
3039 silc_server_send_channel_key(server, sock, channel, FALSE);
3042 /* Send to the server */
3043 silc_id_id2str(channel->id, SILC_ID_CHANNEL, cid, sizeof(cid),
3045 cipher = silc_cipher_get_name(channel->send_key);
3046 cipher_len = strlen(cipher);
3047 chk = silc_channel_key_payload_encode(id_len, cid,
3049 channel->key_len / 8,
3051 silc_server_packet_send(server, sock, SILC_PACKET_CHANNEL_KEY, 0,
3052 chk->data, silc_buffer_len(chk));
3053 silc_buffer_free(chk);
3056 /* Since the channel is coming from server and we also know about it
3057 then send the JOIN notify to the server so that it see's our
3058 users on the channel "joining" the channel. */
3059 silc_server_announce_get_channel_users(server, channel, &modes, &users,
3062 silc_buffer_push(users, users->data - users->head);
3063 silc_server_packet_send(server, sock,
3064 SILC_PACKET_NOTIFY, SILC_PACKET_FLAG_LIST,
3065 users->data, silc_buffer_len(users));
3066 silc_buffer_free(users);
3069 silc_buffer_push(modes, modes->data - modes->head);
3070 silc_server_packet_send_dest(server, sock,
3071 SILC_PACKET_NOTIFY, SILC_PACKET_FLAG_LIST,
3072 channel->id, SILC_ID_CHANNEL,
3073 modes->data, silc_buffer_len(modes));
3074 silc_buffer_free(modes);
3077 silc_buffer_push(users_modes, users_modes->data - users_modes->head);
3078 silc_server_packet_send_dest(server, sock,
3079 SILC_PACKET_NOTIFY, SILC_PACKET_FLAG_LIST,
3080 channel->id, SILC_ID_CHANNEL,
3082 silc_buffer_len(users_modes));
3083 silc_buffer_free(users_modes);
3085 if (channel->topic) {
3086 silc_server_send_notify_topic_set(server, sock,
3087 server->server_type == SILC_ROUTER ?
3088 TRUE : FALSE, channel,
3089 server->id, SILC_ID_SERVER,
3095 /* If the sender of this packet is server and we are router we need to
3096 broadcast this packet to other routers in the network. Broadcast
3097 this list packet instead of multiple New Channel packets. */
3098 if (server->server_type == SILC_ROUTER &&
3099 idata->conn_type == SILC_CONN_SERVER &&
3100 !(packet->flags & SILC_PACKET_FLAG_BROADCAST)) {
3101 SILC_LOG_DEBUG(("Broadcasting received New Channel packet"));
3102 silc_server_packet_send(server, SILC_PRIMARY_ROUTE(server),
3104 packet->flags | SILC_PACKET_FLAG_BROADCAST,
3105 buffer->data, silc_buffer_len(buffer));
3106 silc_server_backup_send(server, (SilcServerEntry)idata,
3107 packet->type, packet->flags,
3108 buffer->data, silc_buffer_len(buffer),
3112 silc_free(channel_namec);
3113 silc_channel_payload_free(payload);
3116 /* Received New Channel packet. Information about new channels in the
3117 network are distributed using this packet. Save the information about
3118 the new channel. This usually comes from router but also normal server
3119 can send this to notify channels it has when it connects to us. */
3121 void silc_server_new_channel(SilcServer server,
3122 SilcPacketStream sock,
3125 silc_server_new_channel_process(server, sock, packet, &packet->buffer);
3126 silc_packet_free(packet);
3129 /* Received New Channel List packet, list of New Channel List payloads inside
3130 one packet. Process the New Channel payloads one by one. */
3132 void silc_server_new_channel_list(SilcServer server,
3133 SilcPacketStream sock,
3136 SilcIDListData idata = silc_packet_get_context(sock);
3138 SilcUInt16 len1, len2;
3140 SILC_LOG_DEBUG(("Processing New Channel List"));
3142 if (idata->conn_type == SILC_CONN_CLIENT ||
3143 packet->src_id_type != SILC_ID_SERVER ||
3144 server->server_type == SILC_SERVER) {
3145 silc_packet_free(packet);
3149 buffer = silc_buffer_alloc(512);
3151 silc_packet_free(packet);
3155 while (silc_buffer_len(&packet->buffer)) {
3156 SILC_GET16_MSB(len1, packet->buffer.data);
3157 if ((len1 > silc_buffer_len(&packet->buffer)) ||
3158 (len1 > silc_buffer_truelen(buffer)))
3161 SILC_GET16_MSB(len2, packet->buffer.data + 2 + len1);
3162 if ((len2 > silc_buffer_len(&packet->buffer)) ||
3163 (len2 > silc_buffer_truelen(buffer)))
3166 silc_buffer_pull_tail(buffer, 8 + len1 + len2);
3167 silc_buffer_put(buffer, packet->buffer.data, 8 + len1 + len2);
3169 /* Process the New Channel */
3170 silc_server_new_channel_process(server, sock, packet, buffer);
3172 silc_buffer_push_tail(buffer, 8 + len1 + len2);
3173 silc_buffer_pull(&packet->buffer, 8 + len1 + len2);
3176 silc_buffer_free(buffer);
3177 silc_packet_free(packet);
3180 /* Received key agreement packet. This packet is never for us. It is to
3181 the client in the packet's destination ID. Sending of this sort of packet
3182 equals sending private message, ie. it is sent point to point from
3183 one client to another. */
3185 void silc_server_key_agreement(SilcServer server,
3186 SilcPacketStream sock,
3189 SilcPacketStream dst_sock;
3190 SilcIDListData idata;
3192 SILC_LOG_DEBUG(("Start"));
3194 if (packet->src_id_type != SILC_ID_CLIENT ||
3195 packet->dst_id_type != SILC_ID_CLIENT || !packet->dst_id) {
3196 silc_packet_free(packet);
3200 /* Get the route to the client */
3201 dst_sock = silc_server_get_client_route(server, packet->dst_id,
3202 packet->dst_id_len, NULL,
3205 silc_packet_free(packet);
3209 /* Relay the packet */
3210 silc_server_packet_route(server, dst_sock, packet);
3211 silc_packet_free(packet);
3214 /* Received connection auth request packet that is used during connection
3215 phase to resolve the mandatory authentication method. This packet can
3216 actually be received at anytime but usually it is used only during
3217 the connection authentication phase. Now, protocol says that this packet
3218 can come from client or server, however, we support only this coming
3219 from client and expect that server always knows what authentication
3222 void silc_server_connection_auth_request(SilcServer server,
3223 SilcPacketStream sock,
3226 SilcServerConfigClient *client = NULL;
3227 SilcUInt16 conn_type;
3229 SilcAuthMethod auth_meth = SILC_AUTH_NONE;
3230 const char *hostname, *ip;
3232 if (packet->src_id_type && packet->src_id_type != SILC_ID_CLIENT) {
3233 SILC_LOG_DEBUG(("Request not from client"));
3234 silc_packet_free(packet);
3238 silc_socket_stream_get_info(silc_packet_stream_get_stream(sock),
3239 NULL, &hostname, &ip, NULL);
3241 /* Parse the payload */
3242 ret = silc_buffer_unformat(&packet->buffer,
3243 SILC_STR_UI_SHORT(&conn_type),
3244 SILC_STR_UI_SHORT(NULL),
3246 if (ret == -1 || conn_type != SILC_CONN_CLIENT) {
3247 silc_packet_free(packet);
3251 /* Get the authentication method for the client */
3252 auth_meth = SILC_AUTH_NONE;
3253 client = silc_server_config_find_client(server, (char *)ip);
3255 client = silc_server_config_find_client(server, (char *)hostname);
3257 if (client->passphrase) {
3258 if (client->publickeys && !server->config->prefer_passphrase_auth)
3259 auth_meth = SILC_AUTH_PUBLIC_KEY;
3261 auth_meth = SILC_AUTH_PASSWORD;
3262 } else if (client->publickeys)
3263 auth_meth = SILC_AUTH_PUBLIC_KEY;
3266 SILC_LOG_DEBUG(("Authentication method is [%s]",
3267 (auth_meth == SILC_AUTH_NONE ? "None" :
3268 auth_meth == SILC_AUTH_PASSWORD ? "Passphrase" :
3269 "Digital signatures")));
3271 /* Send it back to the client */
3272 silc_server_send_connection_auth_request(server, sock, conn_type, auth_meth);
3273 silc_packet_free(packet);
3276 /* Received file transger packet. This packet is never for us. It is to
3277 the client in the packet's destination ID. Sending of this sort of packet
3278 equals sending private message, ie. it is sent point to point from
3279 one client to another. */
3281 void silc_server_ftp(SilcServer server,
3282 SilcPacketStream sock,
3285 SilcPacketStream dst_sock;
3286 SilcIDListData idata;
3288 SILC_LOG_DEBUG(("Start"));
3290 if (packet->src_id_type != SILC_ID_CLIENT ||
3291 packet->dst_id_type != SILC_ID_CLIENT || !packet->dst_id) {
3292 silc_packet_free(packet);
3296 /* Get the route to the client */
3297 dst_sock = silc_server_get_client_route(server, packet->dst_id,
3298 packet->dst_id_len, NULL,
3301 silc_packet_free(packet);
3305 /* Relay the packet */
3306 silc_server_packet_route(server, dst_sock, packet);
3307 silc_packet_free(packet);
3312 SilcPacketStream sock;
3314 SilcClientID client_id;
3315 } *SilcServerResumeResolve;
3317 SILC_SERVER_CMD_FUNC(resume_resolve)
3319 SilcServerResumeResolve r = (SilcServerResumeResolve)context;
3320 SilcServer server = r->server;
3321 SilcPacketStream sock = r->sock;
3322 SilcServerCommandReplyContext reply = context2;
3323 SilcClientEntry client;
3324 const char *hostname, *ip;
3326 SILC_LOG_DEBUG(("Start"));
3328 silc_socket_stream_get_info(silc_packet_stream_get_stream(sock),
3329 NULL, &hostname, &ip, NULL);
3331 if (!reply || !silc_command_get_status(reply->payload, NULL, NULL)) {
3332 SILC_LOG_ERROR(("Client %s (%s) tried to resume unknown client, "
3333 "closing connection", hostname, ip));
3334 silc_server_disconnect_remote(server, sock,
3335 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
3336 "Resuming not possible");
3337 silc_server_free_sock_user_data(server, sock, NULL);
3341 if (reply && silc_command_get(reply->payload) == SILC_COMMAND_WHOIS) {
3342 /* Get entry to the client, and resolve it if we don't have it. */
3343 client = silc_idlist_find_client_by_id(server->local_list,
3344 &r->client_id, TRUE, NULL);
3346 client = silc_idlist_find_client_by_id(server->global_list,
3347 &r->client_id, TRUE, NULL);
3349 SILC_LOG_ERROR(("Client %s (%s) tried to resume unknown client, "
3350 "closing connection", hostname, ip));
3351 silc_server_disconnect_remote(server, sock,
3352 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
3353 "Resuming not possible");
3354 silc_server_free_sock_user_data(server, sock, NULL);
3359 if (!(client->mode & SILC_UMODE_DETACHED)) {
3360 SILC_LOG_ERROR(("Client %s (%s) tried to resume un-detached client, "
3361 "closing connection", hostname, ip));
3362 silc_server_disconnect_remote(server, sock,
3363 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
3364 "Resuming not possible");
3365 silc_server_free_sock_user_data(server, sock, NULL);
3369 client->data.status |= SILC_IDLIST_STATUS_RESUME_RES;
3372 /* Reprocess the packet */
3373 silc_server_resume_client(server, sock, r->packet);
3376 silc_packet_stream_unref(r->sock);
3380 /* Received client resuming packet. This is used to resume detached
3381 client session. It can be sent by the client who wishes to resume
3382 but this is also sent by servers and routers to notify other routers
3383 that the client is not detached anymore. */
3385 void silc_server_resume_client(SilcServer server,
3386 SilcPacketStream sock,
3389 SilcBuffer buffer = &packet->buffer, buf;
3390 SilcIDListData idata = silc_packet_get_context(sock);
3391 SilcIDCacheEntry id_cache = NULL;
3392 SilcClientEntry detached_client;
3393 SilcClientID client_id;
3394 unsigned char *id_string, *auth = NULL, *nicknamec = NULL;
3395 unsigned char cid[32];
3397 SilcUInt16 id_len, auth_len = 0;
3398 SilcBool resolved, local, nick_change = FALSE, resolve = FALSE;
3399 SilcChannelEntry channel;
3400 SilcHashTableList htl;
3401 SilcChannelClientEntry chl;
3402 SilcServerResumeResolve r;
3403 SilcPublicKey public_key;
3404 const char *cipher, *hostname, *ip;
3406 silc_socket_stream_get_info(silc_packet_stream_get_stream(sock),
3407 NULL, &hostname, &ip, NULL);
3409 if (silc_buffer_unformat(buffer,
3410 SILC_STR_UI16_NSTRING(&id_string, &id_len),
3411 SILC_STR_END) < 0) {
3412 if (idata->conn_type == SILC_CONN_CLIENT) {
3413 SILC_LOG_ERROR(("Client %s (%s) sent incomplete resume information, "
3414 "closing connection", hostname, ip));
3415 silc_server_disconnect_remote(server, sock,
3416 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
3417 "Resuming not possible");
3418 silc_server_free_sock_user_data(server, sock, NULL);
3423 silc_id_str2id(id_string, id_len, SILC_ID_CLIENT, &client_id,
3426 if (idata->conn_type == SILC_CONN_CLIENT) {
3427 /* Client send this and is attempting to resume to old client session */
3428 SilcClientEntry client;
3431 silc_buffer_pull(buffer, 2 + id_len);
3432 auth = buffer->data;
3433 auth_len = silc_buffer_len(buffer);
3434 silc_buffer_push(buffer, 2 + id_len);
3436 if (auth_len < 128) {
3437 SILC_LOG_ERROR(("Client %s (%s) sent incomplete resume information, "
3438 "closing connection", hostname, ip));
3439 silc_server_disconnect_remote(server, sock,
3440 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
3441 "Resuming not possible");
3442 silc_server_free_sock_user_data(server, sock, NULL);
3446 /* Take client entry of this connection */
3447 client = (SilcClientEntry)idata;
3449 /* Get entry to the client, and resolve it if we don't have it. */
3450 detached_client = silc_server_query_client(server, &client_id, FALSE,
3452 if (!detached_client) {
3454 /* The client info is being resolved. Reprocess this packet after
3455 receiving the reply to the query. */
3456 SILC_LOG_DEBUG(("Resolving client"));
3457 r = silc_calloc(1, sizeof(*r));
3460 silc_packet_stream_ref(sock);
3464 r->client_id = client_id;
3465 silc_server_command_pending(server, SILC_COMMAND_WHOIS,
3467 silc_server_command_resume_resolve, r);
3470 SILC_LOG_ERROR(("Client %s (%s) tried to resume unknown client, "
3471 "closing connection", hostname, ip));
3472 silc_server_disconnect_remote(server, sock,
3473 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
3474 "Resuming not possible");
3475 silc_server_free_sock_user_data(server, sock, NULL);
3480 if (detached_client->data.status & SILC_IDLIST_STATUS_RESUMED) {
3481 SILC_LOG_ERROR(("Client %s (%s) tried to attach more than once, "
3482 "closing connection", hostname, ip));
3483 silc_server_disconnect_remote(server, sock,
3484 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
3485 "Resuming not possible");
3486 silc_server_free_sock_user_data(server, sock, NULL);
3490 if (detached_client->resuming_client &&
3491 detached_client->resuming_client != client) {
3492 SILC_LOG_ERROR(("Client %s (%s) tried to attach more than once, "
3493 "closing connection", hostname, ip));
3494 silc_server_disconnect_remote(server, sock,
3495 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
3496 "Resuming not possible");
3497 silc_server_free_sock_user_data(server, sock, NULL);
3501 if (!detached_client->resuming_client)
3502 detached_client->resuming_client = client;
3504 if (!(detached_client->mode & SILC_UMODE_DETACHED))
3506 if (!silc_hash_table_count(detached_client->channels) &&
3507 detached_client->router)
3509 if (!detached_client->nickname)
3511 if (detached_client->data.status & SILC_IDLIST_STATUS_RESUME_RES)
3515 if (server->server_type == SILC_SERVER && !server->standalone) {
3516 /* The client info is being resolved. Reprocess this packet after
3517 receiving the reply to the query. */
3518 SILC_LOG_DEBUG(("Resolving client info"));
3519 silc_server_query_client(server, &client_id, TRUE, NULL);
3520 r = silc_calloc(1, sizeof(*r));
3523 silc_packet_stream_ref(sock);
3527 r->client_id = client_id;
3528 silc_server_command_pending(server, SILC_COMMAND_WHOIS,
3530 silc_server_command_resume_resolve, r);
3533 if (server->server_type == SILC_SERVER) {
3534 SILC_LOG_ERROR(("Client %s (%s) tried to resume un-detached client, "
3535 "closing connection", hostname, ip));
3536 silc_server_disconnect_remote(server, sock,
3537 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
3538 "Resuming not possible");
3539 silc_server_free_sock_user_data(server, sock, NULL);
3544 /* Check that we have the public key of the client, if not then we must
3545 resolve it first. */
3546 if (!detached_client->data.public_key) {
3547 if (server->server_type == SILC_SERVER && server->standalone) {
3548 SILC_LOG_ERROR(("Detached client's public key not present, "
3549 "closing connection"));
3550 silc_server_disconnect_remote(server, sock,
3551 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
3552 "Resuming not possible");
3553 silc_server_free_sock_user_data(server, sock, NULL);
3556 /* We must retrieve the detached client's public key by sending
3557 GETKEY command. Reprocess this packet after receiving the key */
3558 SilcBuffer idp = silc_id_payload_encode(&client_id, SILC_ID_CLIENT);
3559 SilcPacketStream dest_sock =
3560 silc_server_get_client_route(server, NULL, 0, &client_id,
3563 SILC_LOG_DEBUG(("Resolving client public key"));
3565 silc_server_send_command(server, dest_sock ? dest_sock :
3566 SILC_PRIMARY_ROUTE(server),
3567 SILC_COMMAND_GETKEY, ++server->cmd_ident,
3568 1, 1, idp->data, silc_buffer_len(idp));
3570 r = silc_calloc(1, sizeof(*r));
3573 silc_packet_stream_ref(sock);
3577 r->client_id = client_id;
3578 silc_server_command_pending(server, SILC_COMMAND_GETKEY,
3580 silc_server_command_resume_resolve, r);
3582 silc_buffer_free(idp);
3585 } else if (!silc_pkcs_public_key_compare(detached_client->data.public_key,
3586 idata->public_key)) {
3587 /* We require that the connection and resuming authentication data
3588 must be using same key pair. */
3589 SILC_LOG_ERROR(("Resuming attempted with wrong public key, "
3590 "closing connection"));
3591 silc_server_disconnect_remote(server, sock,
3592 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
3593 "Resuming not possible");
3594 silc_server_free_sock_user_data(server, sock, NULL);
3598 /* Verify the authentication payload. This has to be successful in
3599 order to allow the resuming */
3601 !silc_auth_verify_data(auth, auth_len, SILC_AUTH_PUBLIC_KEY,
3602 detached_client->data.public_key, 0,
3603 idata->hash, detached_client->id,
3605 SILC_LOG_ERROR(("Client %s (%s) resume authentication failed, "
3606 "closing connection", hostname, ip));
3607 silc_server_disconnect_remote(server, sock,
3608 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
3609 "Resuming not possible");
3610 silc_server_free_sock_user_data(server, sock, NULL);
3614 /* Check nickname */
3615 nicknamec = silc_identifier_check(detached_client->nickname,
3616 strlen(detached_client->nickname),
3617 SILC_STRING_UTF8, 128, NULL);
3619 silc_server_disconnect_remote(server, sock,
3620 SILC_STATUS_ERR_BAD_NICKNAME,
3621 "Malformed nickname, cannot resume");
3622 silc_server_free_sock_user_data(server, sock, NULL);
3626 /* If the ID is not based in our ID then change it */
3627 if (!SILC_ID_COMPARE(detached_client->id, server->id,
3628 server->id->ip.data_len)) {
3629 SilcClientID *new_id;
3630 if (!silc_id_create_client_id(server, server->id, server->rng,
3631 server->md5hash, nicknamec,
3632 strlen(nicknamec), &new_id)) {
3633 silc_server_disconnect_remote(server, sock,
3634 SILC_STATUS_ERR_BAD_NICKNAME,
3635 "Resuming not possible");
3636 silc_server_free_sock_user_data(server, sock, NULL);
3640 client_id = *new_id;
3644 /* Now resume the client to the network */
3646 silc_schedule_task_del_by_context(server->schedule, detached_client);
3647 silc_packet_set_context(sock, detached_client);
3648 detached_client->connection = sock;
3650 if (detached_client->data.public_key) {
3651 /* Delete the detached client's public key from repository */
3652 silc_skr_del_public_key(server->repository,
3653 detached_client->data.public_key,
3655 detached_client->data.public_key = NULL;
3658 if (idata->public_key) {
3659 /* Delete the resuming client's public key from repository. It will
3660 be added later again. */
3661 public_key = silc_pkcs_public_key_copy(idata->public_key);
3662 silc_skr_del_public_key(server->repository, idata->public_key, idata);
3663 idata->public_key = public_key;
3666 /* Take new keys and stuff into use in the old entry */
3667 silc_idlist_del_data(detached_client);
3668 silc_idlist_add_data(detached_client, idata);
3670 if (detached_client->data.public_key) {
3671 /* Add the resumed client's public key back to repository. */
3672 if (!silc_server_get_public_key_by_client(server, detached_client, NULL))
3673 silc_skr_add_public_key_simple(server->repository,
3674 detached_client->data.public_key,
3675 SILC_SKR_USAGE_IDENTIFICATION,
3676 detached_client, NULL);
3679 detached_client->data.status |= SILC_IDLIST_STATUS_REGISTERED;
3680 detached_client->data.status |= SILC_IDLIST_STATUS_RESUMED;
3681 detached_client->data.status |= SILC_IDLIST_STATUS_LOCAL;
3682 detached_client->data.status &= ~SILC_IDLIST_STATUS_RESUME_RES;
3683 detached_client->mode &= ~SILC_UMODE_DETACHED;
3684 server->stat.my_detached--;
3686 /* We are finished - reset resuming client */
3687 detached_client->resuming_client = NULL;
3689 /* Check if anyone is watching this client */
3690 if (server->server_type == SILC_ROUTER)
3691 silc_server_check_watcher_list(server, detached_client, NULL,
3692 SILC_NOTIFY_TYPE_UMODE_CHANGE);
3694 /* Delete this current client entry since we're resuming to old one. */
3695 server->stat.my_clients--;
3696 server->stat.clients--;
3697 if (server->stat.cell_clients)
3698 server->stat.cell_clients--;
3699 silc_server_remove_from_channels(server, NULL, client, FALSE,
3700 NULL, FALSE, FALSE);
3701 silc_server_del_from_watcher_list(server, client);
3702 if (!silc_idlist_del_client(server->local_list, client))
3703 silc_idlist_del_client(server->global_list, client);
3704 client = detached_client;
3705 silc_free(client->servername);
3706 client->servername = strdup(server->server_name);
3708 /* Send the RESUME_CLIENT packet to our primary router so that others
3709 know this client isn't detached anymore. */
3710 buf = silc_buffer_alloc_size(2 + id_len);
3711 silc_buffer_format(buf,
3712 SILC_STR_UI_SHORT(id_len),
3713 SILC_STR_UI_XNSTRING(id_string, id_len),
3716 /* Send to primary router */
3717 silc_server_packet_send(server, SILC_PRIMARY_ROUTE(server),
3718 SILC_PACKET_RESUME_CLIENT, 0,
3719 buf->data, silc_buffer_len(buf));
3720 silc_server_backup_send(server, client->router,
3721 SILC_PACKET_RESUME_CLIENT, 0,
3722 buf->data, silc_buffer_len(buf), TRUE, TRUE);
3724 /* As router we must deliver this packet directly to the original
3725 server whom this client was earlier. */
3726 if (server->server_type == SILC_ROUTER && client->router &&
3727 client->router->server_type != SILC_ROUTER)
3728 silc_server_packet_send(server, client->router->connection,
3729 SILC_PACKET_RESUME_CLIENT, 0,
3730 buf->data, silc_buffer_len(buf));
3731 silc_buffer_free(buf);
3732 client->router = NULL;
3735 /* Notify about Client ID change, nickname doesn't actually change. */
3736 silc_server_send_notify_nick_change(server, SILC_PRIMARY_ROUTE(server),
3737 SILC_BROADCAST(server),
3738 client->id, &client_id,
3742 /* Resolve users on those channels that client has joined but we
3743 haven't resolved user list yet. */
3744 if (server->server_type == SILC_SERVER && !server->standalone) {
3745 silc_hash_table_list(client->channels, &htl);
3746 while (silc_hash_table_get(&htl, NULL, (void *)&chl)) {
3747 channel = chl->channel;
3748 SILC_LOG_DEBUG(("Resolving users for %s channel",
3749 channel->channel_name));
3750 if (channel->disabled || !channel->users_resolved) {
3751 silc_server_send_command(server, SILC_PRIMARY_ROUTE(server),
3752 SILC_COMMAND_USERS, ++server->cmd_ident,
3753 1, 2, channel->channel_name,
3754 strlen(channel->channel_name));
3757 silc_hash_table_list_reset(&htl);
3760 /* Send the new client ID to the client. After this client may start
3761 receiving other packets, and may start sending packets too. */
3762 silc_server_send_new_id(server, sock, FALSE, &client_id, SILC_ID_CLIENT,
3763 silc_id_get_len(&client_id, SILC_ID_CLIENT));
3766 /* Send NICK change notify to channels as well. */
3767 SilcBuffer oidp, nidp;
3768 oidp = silc_id_payload_encode(client->id, SILC_ID_CLIENT);
3769 nidp = silc_id_payload_encode(&client_id, SILC_ID_CLIENT);
3770 silc_server_send_notify_on_channels(server, NULL, client,
3771 SILC_NOTIFY_TYPE_NICK_CHANGE, 3,
3772 oidp->data, silc_buffer_len(oidp),
3773 nidp->data, silc_buffer_len(nidp),
3775 strlen(client->nickname));
3776 silc_buffer_free(oidp);
3777 silc_buffer_free(nidp);
3780 /* Add the client again to the ID cache to get it to correct list */
3781 if (!silc_idcache_del_by_context(server->local_list->clients, client,
3783 silc_idcache_del_by_context(server->global_list->clients, client, NULL);
3784 silc_free(client->id);
3785 *client->id = client_id;
3786 silc_idcache_add(server->local_list->clients, nicknamec,
3787 client->id, client);
3789 /* Send some nice info to the client */
3790 silc_server_send_connect_notifys(server, sock, client);
3792 /* Send all channel keys of channels the client has joined */
3793 silc_hash_table_list(client->channels, &htl);
3794 while (silc_hash_table_get(&htl, NULL, (void *)&chl)) {
3795 SilcBool created = FALSE;
3796 channel = chl->channel;
3798 if (channel->mode & SILC_CHANNEL_MODE_PRIVKEY)
3801 /* If we don't have channel key, then create one */
3802 if (!channel->send_key) {
3803 if (!silc_server_create_channel_key(server, channel, 0))
3808 silc_id_id2str(channel->id, SILC_ID_CHANNEL, cid, sizeof(cid),
3810 cipher = silc_cipher_get_name(channel->send_key);
3812 silc_channel_key_payload_encode(cid_len, cid,
3813 strlen(cipher), cipher,
3814 channel->key_len / 8, channel->key);
3816 /* Send the channel key to the client */
3817 silc_server_packet_send(server, sock, SILC_PACKET_CHANNEL_KEY, 0,
3818 keyp->data, silc_buffer_len(keyp));
3820 /* Distribute the channel key to channel */
3822 silc_server_send_channel_key(server, NULL, channel,
3823 server->server_type == SILC_ROUTER ?
3824 FALSE : !server->standalone);
3825 silc_server_backup_send(server, NULL, SILC_PACKET_CHANNEL_KEY, 0,
3826 keyp->data, silc_buffer_len(keyp),
3830 silc_buffer_free(keyp);
3832 silc_hash_table_list_reset(&htl);
3834 } else if (idata->conn_type != SILC_CONN_CLIENT) {
3835 /* Server or router sent this to us to notify that that a client has
3837 SilcServerEntry server_entry;
3838 SilcServerID server_id;
3840 /* Get entry to the client, and resolve it if we don't have it. */
3841 detached_client = silc_idlist_find_client_by_id(server->local_list,
3844 if (!detached_client) {
3845 detached_client = silc_idlist_find_client_by_id(server->global_list,
3848 if (!detached_client) {
3849 SILC_LOG_DEBUG(("Resuming client is unknown"));
3854 /* Check that the client has not been resumed already because it is
3855 protocol error to attempt to resume more than once. The client
3856 will be killed if this protocol error occurs. */
3857 if (detached_client->data.status & SILC_IDLIST_STATUS_RESUMED &&
3858 !(detached_client->mode & SILC_UMODE_DETACHED)) {
3859 /* The client is clearly attempting to resume more than once and
3860 perhaps playing around by resuming from several different places
3861 at the same time. */
3862 SILC_LOG_DEBUG(("Attempting to re-resume client, killing both"));
3863 silc_server_kill_client(server, detached_client, NULL,
3864 server->id, SILC_ID_SERVER);
3868 /* Check whether client is detached at all */
3869 if (!(detached_client->mode & SILC_UMODE_DETACHED)) {
3870 SILC_LOG_DEBUG(("Client is not detached"));
3874 /* Check nickname */
3875 if (detached_client->nickname) {
3876 nicknamec = silc_identifier_check(detached_client->nickname,
3877 strlen(detached_client->nickname),
3878 SILC_STRING_UTF8, 128, NULL);
3883 SILC_LOG_DEBUG(("Resuming detached client"));
3885 /* If the sender of this packet is server and we are router we need to
3886 broadcast this packet to other routers in the network. */
3887 if (server->server_type == SILC_ROUTER &&
3888 idata->conn_type == SILC_CONN_SERVER &&
3889 !(packet->flags & SILC_PACKET_FLAG_BROADCAST)) {
3890 SILC_LOG_DEBUG(("Broadcasting received Resume Client packet"));
3891 silc_server_packet_send(server, SILC_PRIMARY_ROUTE(server),
3893 packet->flags | SILC_PACKET_FLAG_BROADCAST,
3894 buffer->data, silc_buffer_len(buffer));
3895 silc_server_backup_send(server, (SilcServerEntry)idata,
3896 packet->type, packet->flags,
3897 packet->buffer.data,
3898 silc_buffer_len(&packet->buffer),
3902 /* Client is detached, and now it is resumed. Remove the detached
3903 mode and mark that it is resumed. */
3905 if (detached_client->data.public_key) {
3906 /* Delete the detached client's public key from repository */
3907 silc_skr_del_public_key(server->repository,
3908 detached_client->data.public_key,
3910 detached_client->data.public_key = NULL;
3913 silc_idlist_del_data(detached_client);
3914 detached_client->mode &= ~SILC_UMODE_DETACHED;
3915 detached_client->data.status |= SILC_IDLIST_STATUS_RESUMED;
3916 detached_client->data.status &= ~SILC_IDLIST_STATUS_LOCAL;
3917 silc_dlist_del(server->expired_clients, detached_client);
3919 /* Check if anyone is watching this client */
3920 if (server->server_type == SILC_ROUTER)
3921 silc_server_check_watcher_list(server, detached_client, NULL,
3922 SILC_NOTIFY_TYPE_UMODE_CHANGE);
3924 silc_schedule_task_del_by_context(server->schedule, detached_client);
3926 /* Get the new owner of the resumed client */
3927 if (!silc_id_str2id(packet->src_id, packet->src_id_len,
3928 packet->src_id_type, &server_id, sizeof(server_id)))
3931 /* Get server entry */
3932 server_entry = silc_idlist_find_server_by_id(server->global_list,
3933 &server_id, TRUE, NULL);
3935 if (!server_entry) {
3936 server_entry = silc_idlist_find_server_by_id(server->local_list,
3937 &server_id, TRUE, NULL);
3943 if (server->server_type == SILC_ROUTER &&
3944 idata->conn_type == SILC_CONN_ROUTER &&
3945 server_entry->server_type == SILC_ROUTER)
3948 /* Change the client to correct list. */
3949 if (!silc_idcache_del_by_context(server->local_list->clients,
3950 detached_client, NULL))
3951 silc_idcache_del_by_context(server->global_list->clients,
3952 detached_client, NULL);
3953 silc_idcache_add(local && server->server_type == SILC_ROUTER ?
3954 server->local_list->clients :
3955 server->global_list->clients, nicknamec,
3956 detached_client->id, detached_client);
3958 /* Change the owner of the client */
3959 detached_client->router = server_entry;
3961 /* Update channel information regarding global clients on channel. */
3962 if (server->server_type != SILC_ROUTER) {
3963 silc_hash_table_list(detached_client->channels, &htl);
3964 while (silc_hash_table_get(&htl, NULL, (void *)&chl))
3965 chl->channel->global_users =
3966 silc_server_channel_has_global(chl->channel);
3967 silc_hash_table_list_reset(&htl);
3972 silc_packet_free(packet);