5 Author: Pekka Riikonen <priikone@silcnet.org>
7 Copyright (C) 1997 - 2009 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 SILC_LOG_DEBUG(("Notify received from client, drop it"));
54 if (packet->src_id_type != SILC_ID_SERVER){
55 SILC_LOG_DEBUG(("Bad notify packet received"));
59 if (!packet->dst_id) {
60 SILC_LOG_DEBUG(("Bad notify packet received"));
64 /* If the packet is destined directly to a client then relay the packet
65 before processing it. */
66 if (packet->dst_id_type == SILC_ID_CLIENT) {
68 SilcPacketStream dst_sock;
70 /* Get the route to the client */
71 dst_sock = silc_server_get_client_route(server, packet->dst_id,
72 packet->dst_id_len, NULL,
75 /* Relay the packet */
76 silc_server_packet_route(server, dst_sock, packet);
79 /* Parse the Notify Payload */
80 payload = silc_notify_payload_parse(buffer->data, silc_buffer_len(buffer));
82 SILC_LOG_DEBUG(("Marlformed notify payload"));
86 /* If we are router and this packet is not already broadcast packet
87 we will broadcast it. The sending socket really cannot be router or
88 the router is buggy. If this packet is coming from router then it must
89 have the broadcast flag set already and we won't do anything. */
90 if (server->server_type == SILC_ROUTER &&
91 idata->conn_type == SILC_CONN_SERVER &&
92 !(packet->flags & SILC_PACKET_FLAG_BROADCAST)) {
93 SILC_LOG_DEBUG(("Broadcasting received Notify packet"));
94 if (packet->dst_id_type == SILC_ID_CHANNEL) {
95 /* Packet is destined to channel */
96 if (!silc_id_str2id(packet->dst_id, packet->dst_id_len,
97 packet->dst_id_type, &channel_id,
98 sizeof(channel_id))) {
99 SILC_LOG_DEBUG(("Malformed destination ID in notify packet"));
103 silc_server_packet_send_dest(server, SILC_PRIMARY_ROUTE(server),
104 packet->type, packet->flags |
105 SILC_PACKET_FLAG_BROADCAST,
106 &channel_id, SILC_ID_CHANNEL,
107 buffer->data, silc_buffer_len(buffer));
108 silc_server_backup_send_dest(server, (SilcServerEntry)idata,
109 packet->type, packet->flags,
110 &channel_id, SILC_ID_CHANNEL,
111 buffer->data, silc_buffer_len(buffer),
114 /* Packet is destined to client or server */
115 silc_server_packet_send(server, SILC_PRIMARY_ROUTE(server),
117 packet->flags | SILC_PACKET_FLAG_BROADCAST,
118 buffer->data, silc_buffer_len(buffer));
119 silc_server_backup_send(server, (SilcServerEntry)idata,
120 packet->type, packet->flags,
121 buffer->data, silc_buffer_len(buffer),
126 type = silc_notify_get_type(payload);
127 args = silc_notify_get_args(payload);
129 SILC_LOG_DEBUG(("Notify doesn't have any arguments, drop it"));
134 case SILC_NOTIFY_TYPE_JOIN:
136 * Distribute the notify to local clients on the channel
138 SILC_LOG_DEBUG(("JOIN notify"));
141 if (!silc_argument_get_decoded(args, 2, SILC_ARGUMENT_ID, &id, NULL))
144 /* Get channel entry */
145 channel = silc_idlist_find_channel_by_id(server->global_list,
146 SILC_ID_GET_ID(id), NULL);
148 channel = silc_idlist_find_channel_by_id(server->local_list,
149 SILC_ID_GET_ID(id), NULL);
151 SILC_LOG_DEBUG(("Notify for unknown channel %s",
152 silc_id_render(SILC_ID_GET_ID(id), SILC_ID_CHANNEL)));
158 if (!silc_argument_get_decoded(args, 1, SILC_ARGUMENT_ID, &id, NULL))
161 /* If the the client is not in local list we check global list (ie. the
162 channel will be global channel) and if it does not exist then create
163 entry for the client. */
164 client = silc_idlist_find_client_by_id(server->global_list,
169 client = silc_idlist_find_client_by_id(server->local_list,
174 /* If router did not find the client the it is bogus */
175 if (server->server_type != SILC_SERVER)
179 silc_idlist_add_client(server->global_list, NULL, NULL, NULL,
180 silc_id_dup(SILC_ID_GET_ID(id),
182 (SilcServerEntry)idata, NULL);
184 SILC_LOG_ERROR(("Could not add new client to the ID Cache"));
188 client->data.status |= SILC_IDLIST_STATUS_REGISTERED;
190 if (idata->conn_type == SILC_CONN_SERVER)
191 server->stat.cell_clients++;
192 SILC_LOG_DEBUG(("stat.clients %d->%d", server->stat.clients,
193 server->stat.clients + 1));
194 server->stat.clients++;
198 /* Do not process the notify if the client is not registered */
199 if (!(client->data.status & SILC_IDLIST_STATUS_REGISTERED))
202 /* Do not add client to channel if it is there already */
203 if (silc_server_client_on_channel(client, channel, NULL)) {
204 SILC_LOG_DEBUG(("Client already on channel %s",
205 channel->channel_name));
209 /* Send to channel */
210 silc_server_packet_send_to_channel(server, sock, channel, packet->type,
211 FALSE, TRUE, buffer->data,
212 silc_buffer_len(buffer));
214 if (server->server_type != SILC_ROUTER &&
215 idata->conn_type == SILC_CONN_ROUTER)
216 /* The channel is global now */
217 channel->global_users = TRUE;
219 SILC_LOG_DEBUG(("Joining to channel %s", channel->channel_name));
221 /* JOIN the global client to the channel (local clients (if router
222 created the channel) is joined in the pending JOIN command). */
223 chl = silc_calloc(1, sizeof(*chl));
226 chl->client = client;
227 chl->channel = channel;
229 if (server->server_type != SILC_ROUTER ||
230 idata->conn_type == SILC_CONN_ROUTER) {
231 /* If founder auth is set, first client is not automatically founder. */
232 if (!(channel->mode & SILC_CHANNEL_MODE_FOUNDER_AUTH)) {
233 /* If this is the first one on the channel then it is the founder of
234 the channel. This is done on normal server and on router if this
235 notify is coming from router */
236 if (!silc_hash_table_count(channel->user_list)) {
237 SILC_LOG_DEBUG(("Client %s is founder on channel",
238 silc_id_render(chl->client->id, SILC_ID_CLIENT)));
239 chl->mode = (SILC_CHANNEL_UMODE_CHANOP | SILC_CHANNEL_UMODE_CHANFO);
244 silc_hash_table_add(channel->user_list, client, chl);
245 silc_hash_table_add(client->channels, channel, chl);
246 channel->user_count++;
247 channel->disabled = FALSE;
249 /* Update statistics */
250 if (server->server_type == SILC_ROUTER) {
251 if (idata->conn_type != SILC_CONN_ROUTER)
252 server->stat.cell_chanclients++;
253 server->stat.chanclients++;
258 case SILC_NOTIFY_TYPE_LEAVE:
260 * Distribute the notify to local clients on the channel
262 SILC_LOG_DEBUG(("LEAVE notify"));
264 if (!silc_id_str2id(packet->dst_id, packet->dst_id_len,
265 packet->dst_id_type, &channel_id,
269 /* Get channel entry */
270 channel = silc_idlist_find_channel_by_id(server->global_list,
273 channel = silc_idlist_find_channel_by_id(server->local_list,
276 SILC_LOG_DEBUG(("Notify for unknown channel %s",
277 silc_id_render(&channel_id, SILC_ID_CHANNEL)));
283 if (!silc_argument_get_decoded(args, 1, SILC_ARGUMENT_ID, &id, NULL))
286 /* Get client entry */
287 client = silc_idlist_find_client_by_id(server->global_list,
288 SILC_ID_GET_ID(id), TRUE, NULL);
290 client = silc_idlist_find_client_by_id(server->local_list,
291 SILC_ID_GET_ID(id), TRUE, NULL);
296 /* Check if on channel */
297 if (!silc_server_client_on_channel(client, channel, NULL))
300 /* Send the leave notify to channel */
301 silc_server_packet_send_to_channel(server, sock, channel, packet->type,
302 FALSE, TRUE, buffer->data,
303 silc_buffer_len(buffer));
305 /* Remove the user from channel */
306 silc_server_remove_from_one_channel(server, sock, channel, client, FALSE);
309 case SILC_NOTIFY_TYPE_SIGNOFF:
311 * Distribute the notify to local clients on the channel
313 SILC_LOG_DEBUG(("SIGNOFF notify"));
316 if (!silc_argument_get_decoded(args, 1, SILC_ARGUMENT_ID, &id, NULL))
319 /* Get client entry */
320 client = silc_idlist_find_client_by_id(server->global_list,
321 SILC_ID_GET_ID(id), TRUE, &cache);
323 client = silc_idlist_find_client_by_id(server->local_list,
324 SILC_ID_GET_ID(id), TRUE, &cache);
329 /* Get signoff message */
330 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
334 /* Update statistics */
335 SILC_LOG_DEBUG(("stat.clients %d->%d", server->stat.clients,
336 server->stat.clients - 1));
337 SILC_VERIFY(server->stat.clients > 0);
338 server->stat.clients--;
339 if (server->stat.cell_clients)
340 server->stat.cell_clients--;
341 SILC_OPER_STATS_UPDATE(client, server, SILC_UMODE_SERVER_OPERATOR);
342 SILC_OPER_STATS_UPDATE(client, router, SILC_UMODE_ROUTER_OPERATOR);
343 silc_schedule_task_del_by_context(server->schedule, client);
345 /* Remove client's public key from repository, this will free it too. */
346 if (client->data.public_key) {
347 silc_skr_del_public_key(server->repository, client->data.public_key,
349 client->data.public_key = NULL;
352 /* Remove the client from all channels. */
353 silc_server_remove_from_channels(server, NULL, client, TRUE,
356 /* Check if anyone is watching this nickname */
357 if (server->server_type == SILC_ROUTER)
358 silc_server_check_watcher_list(server, client, NULL,
359 SILC_NOTIFY_TYPE_SIGNOFF);
361 /* Remove this client from watcher list if it is */
362 silc_server_del_from_watcher_list(server, client);
364 client->data.status &= ~SILC_IDLIST_STATUS_REGISTERED;
366 client->router = NULL;
367 client->connection = NULL;
368 client->data.created = silc_time();
369 silc_dlist_del(server->expired_clients, client);
370 silc_dlist_add(server->expired_clients, client);
373 case SILC_NOTIFY_TYPE_TOPIC_SET:
375 * Distribute the notify to local clients on the channel
378 SILC_LOG_DEBUG(("TOPIC SET notify"));
381 if (!silc_argument_get_decoded(args, 1, SILC_ARGUMENT_ID, &id, NULL))
384 /* Get client entry */
385 if (id.type == SILC_ID_CLIENT) {
386 client = silc_idlist_find_client_by_id(server->global_list,
387 SILC_ID_GET_ID(id), TRUE, &cache);
389 client = silc_idlist_find_client_by_id(server->local_list,
398 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
402 if (!silc_id_str2id(packet->dst_id, packet->dst_id_len,
403 packet->dst_id_type, &channel_id,
407 /* Get channel entry */
408 channel = silc_idlist_find_channel_by_id(server->global_list,
411 channel = silc_idlist_find_channel_by_id(server->local_list,
414 SILC_LOG_DEBUG(("Notify for unknown channel %s",
415 silc_id_render(&channel_id, SILC_ID_CHANNEL)));
420 if (channel->topic && !strcmp(channel->topic, tmp)) {
421 SILC_LOG_DEBUG(("Topic is already set and same"));
426 /* Get user's channel entry and check that topic set is allowed. */
427 if (!silc_server_client_on_channel(client, channel, &chl))
429 if (channel->mode & SILC_CHANNEL_MODE_TOPIC &&
430 !(chl->mode & SILC_CHANNEL_UMODE_CHANOP) &&
431 !(chl->mode & SILC_CHANNEL_UMODE_CHANFO)) {
432 SILC_LOG_DEBUG(("Topic change is not allowed"));
437 /* Change the topic */
438 silc_free(channel->topic);
439 channel->topic = strdup(tmp);
441 /* Send the same notify to the channel */
442 silc_server_packet_send_to_channel(server, NULL, channel, packet->type,
443 FALSE, TRUE, buffer->data,
444 silc_buffer_len(buffer));
447 case SILC_NOTIFY_TYPE_NICK_CHANGE:
450 * Distribute the notify to local clients on the channel
453 SilcUInt32 nickname_len;
455 SILC_LOG_DEBUG(("NICK CHANGE notify"));
457 /* Get old client ID */
458 if (!silc_argument_get_decoded(args, 1, SILC_ARGUMENT_ID, &id, NULL))
461 /* Get new client ID */
462 if (!silc_argument_get_decoded(args, 2, SILC_ARGUMENT_ID, &id2, NULL))
465 SILC_LOG_DEBUG(("Old Client ID id(%s)",
466 silc_id_render(SILC_ID_GET_ID(id), SILC_ID_CLIENT)));
467 SILC_LOG_DEBUG(("New Client ID id(%s)",
468 silc_id_render(SILC_ID_GET_ID(id2), SILC_ID_CLIENT)));
470 /* From protocol version 1.1 we also get the new nickname */
471 nickname = silc_argument_get_arg_type(args, 3, &nickname_len);;
473 /* Replace the Client ID */
474 client = silc_idlist_replace_client_id(server,
477 SILC_ID_GET_ID(id2), nickname);
479 client = silc_idlist_replace_client_id(server,
482 SILC_ID_GET_ID(id2), nickname);
485 /* Send the NICK_CHANGE notify type to local clients on the channels
486 this client is joined to. */
487 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
488 tmp2 = silc_argument_get_arg_type(args, 2, &tmp2_len);
489 silc_server_send_notify_on_channels(server, client, client,
490 SILC_NOTIFY_TYPE_NICK_CHANGE, 3,
491 tmp, tmp_len, tmp2, tmp2_len,
499 case SILC_NOTIFY_TYPE_CMODE_CHANGE:
501 * Distribute the notify to local clients on the channel
504 SILC_LOG_DEBUG(("CMODE CHANGE notify"));
507 if (!silc_argument_get_decoded(args, 1, SILC_ARGUMENT_ID, &id, NULL))
510 /* Get client entry */
511 if (id.type == SILC_ID_CLIENT) {
512 client = silc_idlist_find_client_by_id(server->global_list,
513 SILC_ID_GET_ID(id), TRUE, &cache);
515 client = silc_idlist_find_client_by_id(server->local_list,
523 if (!silc_id_str2id(packet->dst_id, packet->dst_id_len,
524 packet->dst_id_type, &channel_id,
528 /* Get channel entry */
529 channel = silc_idlist_find_channel_by_id(server->global_list,
532 channel = silc_idlist_find_channel_by_id(server->local_list,
535 SILC_LOG_DEBUG(("Notify for unknown channel %s",
536 silc_id_render(&channel_id, SILC_ID_CHANNEL)));
542 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
545 SILC_GET32_MSB(mode, tmp);
547 /* Check if mode changed */
548 if (channel->mode == mode) {
549 SILC_LOG_DEBUG(("Mode is changed already"));
551 /* If this mode change has founder mode then we'll enforce the
552 change so that the server gets the real founder public key */
553 if (server->server_type != SILC_SERVER &&
554 sock != SILC_PRIMARY_ROUTE(server) &&
555 mode & SILC_CHANNEL_MODE_FOUNDER_AUTH && channel->founder_key) {
556 SILC_LOG_DEBUG(("Sending founder public key to server"));
557 silc_server_send_notify_cmode(server, sock, FALSE, channel,
558 channel->mode, server->id,
559 SILC_ID_SERVER, channel->cipher,
562 channel->founder_key, NULL);
565 /* If we received same mode from our primary check whether founder
566 mode and key in the notify is set. We update the founder key
567 here since we may have wrong one */
568 if (server->server_type != SILC_ROUTER &&
569 sock == SILC_PRIMARY_ROUTE(server) &&
570 mode & SILC_CHANNEL_MODE_FOUNDER_AUTH) {
571 SILC_LOG_DEBUG(("Founder public key received from router"));
572 tmp = silc_argument_get_arg_type(args, 6, &tmp_len);
576 if (channel->founder_key)
577 silc_pkcs_public_key_free(channel->founder_key);
578 channel->founder_key = NULL;
579 silc_public_key_payload_decode(tmp, tmp_len,
580 &channel->founder_key);
583 /* Check also for channel public key list */
584 if (server->server_type == SILC_SERVER &&
585 sock == SILC_PRIMARY_ROUTE(server) &&
586 mode & SILC_CHANNEL_MODE_CHANNEL_AUTH) {
589 unsigned char mask[4], ulimit[4];
591 SILC_LOG_DEBUG(("Channel public key list received from router"));
592 tmp = silc_argument_get_arg_type(args, 7, &tmp_len);
596 /* Set the router's list, and send the notify to channel too so that
597 channel gets the list */
598 silc_server_set_channel_pk_list(server, sock, channel, tmp, tmp_len);
599 chpklist = silc_server_get_channel_pk_list(server, channel,
603 sidp = silc_id_payload_encode(server->router->id, SILC_ID_SERVER);
604 SILC_PUT32_MSB(channel->mode, mask);
605 if (channel->mode & SILC_CHANNEL_MODE_ULIMIT)
606 SILC_PUT32_MSB(channel->user_limit, ulimit);
607 silc_server_send_notify_to_channel(server, NULL, channel, FALSE, TRUE,
608 SILC_NOTIFY_TYPE_CMODE_CHANGE, 8,
609 sidp->data, silc_buffer_len(sidp),
613 strlen(channel->cipher) : 0,
616 strlen(channel->hmac_name) : 0,
618 channel->passphrase ?
619 strlen(channel->passphrase) : 0,
622 silc_buffer_len(chpklist),
624 SILC_CHANNEL_MODE_ULIMIT ?
627 SILC_CHANNEL_MODE_ULIMIT ?
628 sizeof(ulimit) : 0));
629 silc_buffer_free(sidp);
630 silc_buffer_free(chpklist);
637 /* Get user's channel entry and check that mode change is allowed */
639 if (!silc_server_client_on_channel(client, channel, &chl))
641 if (!silc_server_check_cmode_rights(server, channel, chl, mode)) {
642 SILC_LOG_DEBUG(("CMODE change is not allowed"));
643 silc_server_send_notify_cmode(server, sock, FALSE, channel,
644 channel->mode, server->id,
645 SILC_ID_SERVER, channel->cipher,
648 channel->founder_key, NULL);
652 /* Assure that server is not removing founder mode from us */
653 if (server->server_type == SILC_ROUTER &&
654 sock != SILC_PRIMARY_ROUTE(server) &&
655 channel->mode & SILC_CHANNEL_MODE_FOUNDER_AUTH &&
656 !(mode & SILC_CHANNEL_MODE_FOUNDER_AUTH)) {
657 SILC_LOG_DEBUG(("Enforcing sender to change channel mode"));
658 silc_server_send_notify_cmode(server, sock, FALSE, channel,
659 channel->mode, server->id,
660 SILC_ID_SERVER, channel->cipher,
663 channel->founder_key, NULL);
667 /* If server is adding founder mode, check whether there is founder
668 on channel already and is not from this server */
669 if (server->server_type == SILC_ROUTER &&
670 sock != SILC_PRIMARY_ROUTE(server) &&
671 mode & SILC_CHANNEL_MODE_FOUNDER_AUTH) {
672 silc_hash_table_list(channel->user_list, &htl);
673 while (silc_hash_table_get(&htl, NULL, (void *)&chl))
674 if (chl->mode & SILC_CHANNEL_UMODE_CHANFO &&
675 chl->client->router != (SilcServerEntry)idata) {
676 SILC_LOG_DEBUG(("Enforcing sender to change channel mode"));
677 silc_server_send_notify_cmode(server, sock, FALSE, channel,
678 channel->mode, server->id,
679 SILC_ID_SERVER, channel->cipher,
682 channel->founder_key, NULL);
683 silc_hash_table_list_reset(&htl);
686 silc_hash_table_list_reset(&htl);
690 /* If the channel had private keys set and the mode was removed then
691 we must re-generate and re-distribute a new channel key */
692 if (channel->mode & SILC_CHANNEL_MODE_PRIVKEY &&
693 !(mode & SILC_CHANNEL_MODE_PRIVKEY)) {
694 /* Re-generate channel key */
695 if (!silc_server_create_channel_key(server, channel, 0))
698 /* Send the channel key. This sends it to our local clients and if
699 we are normal server to our router as well. */
700 silc_server_send_channel_key(server, NULL, channel,
701 server->server_type == SILC_ROUTER ?
702 FALSE : !server->standalone);
706 tmp = silc_argument_get_arg_type(args, 4, &tmp_len);
708 unsigned char hash[SILC_HASH_MAXLEN];
711 silc_hmac_free(channel->hmac);
712 if (!silc_hmac_alloc(tmp, NULL, &channel->hmac))
715 /* Set the HMAC key out of current channel key. The client must do
717 silc_hash_make(silc_hmac_get_hash(channel->hmac), channel->key,
718 channel->key_len / 8, hash);
719 silc_hmac_set_key(channel->hmac, hash,
720 silc_hash_len(silc_hmac_get_hash(channel->hmac)));
721 memset(hash, 0, sizeof(hash));
724 /* Get the passphrase */
725 tmp = silc_argument_get_arg_type(args, 5, &tmp_len);
727 silc_free(channel->passphrase);
728 channel->passphrase = silc_memdup(tmp, tmp_len);
731 /* Get founder public key */
732 tmp = silc_argument_get_arg_type(args, 6, &tmp_len);
733 if (tmp && mode & SILC_CHANNEL_MODE_FOUNDER_AUTH) {
734 if (channel->founder_key)
735 silc_pkcs_public_key_free(channel->founder_key);
736 channel->founder_key = NULL;
737 SILC_LOG_DEBUG(("Founder public key received"));
738 if (!silc_public_key_payload_decode(tmp, tmp_len,
739 &channel->founder_key)) {
740 SILC_LOG_DEBUG(("Enforcing sender to change channel mode"));
741 mode &= ~SILC_CHANNEL_MODE_FOUNDER_AUTH;
742 silc_server_send_notify_cmode(server, sock, FALSE, channel,
743 mode, server->id, SILC_ID_SERVER,
746 channel->passphrase, NULL, NULL);
747 if (channel->founder_key)
748 silc_pkcs_public_key_free(channel->founder_key);
749 channel->founder_key = NULL;
753 if (mode & SILC_CHANNEL_MODE_FOUNDER_AUTH && !channel->founder_key &&
754 server->server_type == SILC_ROUTER) {
755 SILC_LOG_DEBUG(("Enforcing sender to change channel mode"));
756 mode &= ~SILC_CHANNEL_MODE_FOUNDER_AUTH;
757 silc_server_send_notify_cmode(server, sock, FALSE, channel,
758 mode, server->id, SILC_ID_SERVER,
761 channel->passphrase, NULL, NULL);
764 /* Process channel public key(s). */
765 tmp = silc_argument_get_arg_type(args, 7, &tmp_len);
766 if (tmp && mode & SILC_CHANNEL_MODE_CHANNEL_AUTH) {
768 SILC_LOG_DEBUG(("Channel public key list received from router"));
771 silc_server_set_channel_pk_list(server, sock, channel, tmp, tmp_len);
773 /* If list was set already we will enforce the same list to server. */
774 if (ret == SILC_STATUS_ERR_OPERATION_ALLOWED) {
775 SilcBuffer chpklist = silc_server_get_channel_pk_list(server, channel,
777 silc_server_send_notify_cmode(server, sock, FALSE, channel,
778 mode, server->id, SILC_ID_SERVER,
781 channel->passphrase, NULL,
783 silc_buffer_free(chpklist);
787 /* Get the user limit */
788 tmp = silc_argument_get_arg_type(args, 8, &tmp_len);
789 if (tmp && tmp_len == 4 && mode & SILC_CHANNEL_MODE_ULIMIT)
790 SILC_GET32_MSB(channel->user_limit, tmp);
792 /* Send the same notify to the channel */
793 silc_server_packet_send_to_channel(server, NULL, channel, packet->type,
794 FALSE, TRUE, buffer->data,
795 silc_buffer_len(buffer));
798 channel->mode = mode;
800 /* Cleanup if some modes are removed */
802 if (!(channel->mode & SILC_CHANNEL_MODE_FOUNDER_AUTH) &&
803 channel->founder_key) {
804 silc_pkcs_public_key_free(channel->founder_key);
805 channel->founder_key = NULL;
808 if (!(channel->mode & SILC_CHANNEL_MODE_CHANNEL_AUTH) &&
809 channel->channel_pubkeys) {
810 silc_hash_table_free(channel->channel_pubkeys);
811 channel->channel_pubkeys = NULL;
816 case SILC_NOTIFY_TYPE_CUMODE_CHANGE:
819 * Distribute the notify to local clients on the channel
821 SilcChannelClientEntry chl2 = NULL;
822 SilcBool notify_sent = FALSE;
824 SILC_LOG_DEBUG(("CUMODE CHANGE notify"));
827 if (!silc_argument_get_decoded(args, 1, SILC_ARGUMENT_ID, &id, NULL))
830 /* Get client entry */
831 if (id.type == SILC_ID_CLIENT) {
832 client = silc_idlist_find_client_by_id(server->global_list,
836 client = silc_idlist_find_client_by_id(server->local_list,
844 if (!silc_id_str2id(packet->dst_id, packet->dst_id_len,
845 packet->dst_id_type, &channel_id,
849 /* Get channel entry */
850 channel = silc_idlist_find_channel_by_id(server->global_list,
853 channel = silc_idlist_find_channel_by_id(server->local_list,
856 SILC_LOG_DEBUG(("Notify for unknown channel %s",
857 silc_id_render(&channel_id, SILC_ID_CHANNEL)));
863 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
867 SILC_GET32_MSB(mode, tmp);
869 /* Get target client */
870 if (!silc_argument_get_decoded(args, 3, SILC_ARGUMENT_ID, &id, NULL))
873 /* Get client entry */
874 client2 = silc_idlist_find_client_by_id(server->global_list,
875 SILC_ID_GET_ID(id), TRUE, NULL);
877 client2 = silc_idlist_find_client_by_id(server->local_list,
885 /* Check that sender is on channel */
886 if (!silc_server_client_on_channel(client, channel, &chl))
889 if (client != client2 && server->server_type == SILC_ROUTER) {
890 /* Sender must be operator */
891 if (!(chl->mode & SILC_CHANNEL_UMODE_CHANOP) &&
892 !(chl->mode & SILC_CHANNEL_UMODE_CHANFO)) {
893 SILC_LOG_DEBUG(("CUMODE change is not allowed"));
897 if (!silc_server_client_on_channel(client2, channel, &chl))
900 /* If target is founder mode change is not allowed. */
901 if (chl->mode & SILC_CHANNEL_UMODE_CHANFO) {
902 SILC_LOG_DEBUG(("CUMODE change is not allowed"));
908 /* Get target channel user entry */
909 if (!silc_server_client_on_channel(client2, channel, &chl))
912 if (server->server_type == SILC_SERVER && chl->mode == mode) {
913 SILC_LOG_DEBUG(("Mode is changed already"));
917 /* Check whether to give founder rights to this user or not. The
918 problem here is that we get only the public key of the client,
919 but no authentication data. We must assume that server has
920 already authenticated the user (and thus we must trust the
922 if (mode & SILC_CHANNEL_UMODE_CHANFO &&
923 !(chl->mode & SILC_CHANNEL_UMODE_CHANFO) &&
924 server->server_type == SILC_ROUTER &&
925 sock != SILC_PRIMARY_ROUTE(server)) {
926 SilcPublicKey founder_key = NULL;
928 /* If channel doesn't have founder auth mode then it's impossible
929 that someone would be getting founder rights with CUMODE command.
930 In that case there already either is founder or there isn't
931 founder at all on the channel (valid only when 'client' is
933 if (client && !(channel->mode & SILC_CHANNEL_MODE_FOUNDER_AUTH)) {
934 /* Force the mode to not have founder mode */
935 chl->mode = mode &= ~SILC_CHANNEL_UMODE_CHANFO;
936 silc_server_force_cumode_change(server, sock, channel, chl, mode);
941 /* Get the founder of the channel and if found then this client
942 cannot be the founder since there already is one. */
943 silc_hash_table_list(channel->user_list, &htl);
944 while (silc_hash_table_get(&htl, NULL, (void *)&chl2))
945 if (chl2->mode & SILC_CHANNEL_UMODE_CHANFO) {
946 SILC_LOG_DEBUG(("Founder already on channel"));
947 chl->mode = mode &= ~SILC_CHANNEL_UMODE_CHANFO;
948 silc_server_force_cumode_change(server, sock, channel,
953 silc_hash_table_list_reset(&htl);
954 if (!(mode & SILC_CHANNEL_UMODE_CHANFO))
957 /* Founder not found on the channel. Since the founder auth mode
958 is set on the channel now check whether this is the client that
959 originally set the mode. */
961 if (channel->founder_key) {
962 /* Get public key that must be present in notify */
963 tmp = silc_argument_get_arg_type(args, 4, &tmp_len);
964 if (!tmp || !silc_public_key_payload_decode(tmp, tmp_len,
966 chl->mode = mode &= ~SILC_CHANNEL_UMODE_CHANFO;
967 SILC_LOG_DEBUG(("Founder public key not present"));
968 silc_server_force_cumode_change(server, sock, channel, chl, mode);
973 /* Now match the public key we have cached and public key sent.
975 if (!silc_pkcs_public_key_compare(channel->founder_key,
977 chl->mode = mode &= ~SILC_CHANNEL_UMODE_CHANFO;
978 SILC_LOG_DEBUG(("Founder public key mismatch"));
979 silc_server_force_cumode_change(server, sock, channel, chl, mode);
985 /* There cannot be anyone else as founder on the channel now. This
986 client is definitely the founder due to this 'authentication'.
987 We trust the server did the actual signature verification
988 earlier (bad, yes). */
989 silc_hash_table_list(channel->user_list, &htl);
990 while (silc_hash_table_get(&htl, NULL, (void *)&chl2))
991 if (chl2->mode & SILC_CHANNEL_UMODE_CHANFO) {
992 chl2->mode &= ~SILC_CHANNEL_UMODE_CHANFO;
993 SILC_LOG_DEBUG(("Removing old founder rights, new authenticated"));
994 silc_server_force_cumode_change(server, NULL, channel, chl2,
998 silc_hash_table_list_reset(&htl);
1001 silc_pkcs_public_key_free(founder_key);
1004 if (server->server_type != SILC_SERVER && chl->mode == mode) {
1005 SILC_LOG_DEBUG(("Mode is changed already"));
1009 SILC_LOG_DEBUG(("Changing %s channel user mode",
1010 chl->client->nickname ? chl->client->nickname :
1011 (unsigned char *)""));
1013 /* Change the mode */
1016 /* Send the same notify to the channel */
1018 silc_server_packet_send_to_channel(server, sock, channel,
1020 FALSE, TRUE, buffer->data,
1021 silc_buffer_len(buffer));
1026 case SILC_NOTIFY_TYPE_INVITE:
1028 if (packet->dst_id_type == SILC_ID_CLIENT)
1031 SILC_LOG_DEBUG(("INVITE notify"));
1033 /* Get Channel ID */
1034 if (!silc_argument_get_decoded(args, 1, SILC_ARGUMENT_ID, &id, NULL))
1037 /* Get channel entry */
1038 channel = silc_idlist_find_channel_by_id(server->global_list,
1039 SILC_ID_GET_ID(id), NULL);
1041 channel = silc_idlist_find_channel_by_id(server->local_list,
1042 SILC_ID_GET_ID(id), NULL);
1044 SILC_LOG_DEBUG(("Notify for unknown channel %s",
1045 silc_id_render(SILC_ID_GET_ID(id), SILC_ID_CHANNEL)));
1050 /* Get the invite action */
1051 tmp = silc_argument_get_arg_type(args, 4, &tmp_len);
1052 if (tmp && tmp_len == 1) {
1053 SilcUInt8 action = (SilcUInt8)tmp[0];
1054 SilcUInt16 iargc = 0;
1055 SilcArgumentPayload iargs;
1057 /* Get invite list */
1058 tmp = silc_argument_get_arg_type(args, 5, &tmp_len);
1059 if (!tmp || tmp_len < 2)
1062 /* Parse the arguments to see they are constructed correctly */
1063 SILC_GET16_MSB(iargc, tmp);
1064 iargs = silc_argument_payload_parse(tmp + 2, tmp_len - 2, iargc);
1068 if (!channel->invite_list)
1069 channel->invite_list =
1070 silc_hash_table_alloc(0, silc_hash_ptr,
1072 silc_server_inviteban_destruct, channel, TRUE);
1074 /* Proces the invite action */
1075 if (!silc_server_inviteban_process(server, channel->invite_list, action,
1078 silc_argument_payload_free(iargs);
1080 /* If we are router we must send this notify to our local servers on
1081 the channel. Normal server does nothing. The notify is not
1083 if (server->server_type == SILC_ROUTER)
1084 silc_server_packet_send_to_channel(server, sock, channel,
1085 packet->type, FALSE, FALSE,
1087 silc_buffer_len(buffer));
1092 case SILC_NOTIFY_TYPE_CHANNEL_CHANGE:
1094 * Distribute to the local clients on the channel and change the
1098 SILC_LOG_DEBUG(("CHANNEL CHANGE"));
1100 if (idata->conn_type != SILC_CONN_ROUTER)
1103 /* Get the old Channel ID */
1104 if (!silc_argument_get_decoded(args, 1, SILC_ARGUMENT_ID, &id, NULL))
1107 /* Get the channel entry */
1108 channel = silc_idlist_find_channel_by_id(server->local_list,
1109 SILC_ID_GET_ID(id), NULL);
1111 channel = silc_idlist_find_channel_by_id(server->global_list,
1112 SILC_ID_GET_ID(id), NULL);
1114 SILC_LOG_DEBUG(("Notify for unknown channel %s",
1115 silc_id_render(SILC_ID_GET_ID(id), SILC_ID_CHANNEL)));
1120 /* Send the notify to the channel */
1121 silc_server_packet_send_to_channel(server, sock, channel, packet->type,
1122 FALSE, TRUE, buffer->data,
1123 silc_buffer_len(buffer));
1125 /* Get the new Channel ID */
1126 if (!silc_argument_get_decoded(args, 2, SILC_ARGUMENT_ID, &id2, NULL))
1129 SILC_LOG_DEBUG(("Old Channel ID id(%s)",
1130 silc_id_render(SILC_ID_GET_ID(id), SILC_ID_CHANNEL)));
1131 SILC_LOG_DEBUG(("New Channel ID id(%s)",
1132 silc_id_render(SILC_ID_GET_ID(id2), SILC_ID_CHANNEL)));
1134 /* Replace the Channel ID */
1136 if (!silc_idlist_replace_channel_id(server->local_list,
1138 SILC_ID_GET_ID(id2)))
1139 if (!silc_idlist_replace_channel_id(server->global_list,
1141 SILC_ID_GET_ID(id2)))
1145 SilcBuffer modes = NULL, users = NULL, users_modes = NULL;
1147 /* Re-announce this channel which ID was changed. */
1148 silc_server_send_new_channel(server, sock, FALSE, channel->channel_name,
1150 silc_id_get_len(channel->id,
1154 /* Re-announce our clients on the channel as the ID has changed now */
1155 silc_server_announce_get_channel_users(server, channel, &modes, &users,
1158 silc_buffer_push(users, users->data - users->head);
1159 silc_server_packet_send(server, sock,
1160 SILC_PACKET_NOTIFY, SILC_PACKET_FLAG_LIST,
1161 users->data, silc_buffer_len(users));
1162 silc_buffer_free(users);
1165 silc_buffer_push(modes, modes->data - modes->head);
1166 silc_server_packet_send_dest(server, sock,
1167 SILC_PACKET_NOTIFY, SILC_PACKET_FLAG_LIST,
1168 channel->id, SILC_ID_CHANNEL,
1169 modes->data, silc_buffer_len(modes));
1170 silc_buffer_free(modes);
1173 silc_buffer_push(users_modes, users_modes->data - users_modes->head);
1174 silc_server_packet_send_dest(server, sock,
1175 SILC_PACKET_NOTIFY, SILC_PACKET_FLAG_LIST,
1176 channel->id, SILC_ID_CHANNEL,
1178 silc_buffer_len(users_modes));
1179 silc_buffer_free(users_modes);
1182 /* Re-announce channel's topic */
1183 if (channel->topic) {
1184 silc_server_send_notify_topic_set(server, sock,
1185 server->server_type == SILC_ROUTER ?
1186 TRUE : FALSE, channel,
1187 server->id, SILC_ID_SERVER,
1194 case SILC_NOTIFY_TYPE_SERVER_SIGNOFF:
1196 * Remove the server entry and all clients that this server owns.
1199 SILC_LOG_DEBUG(("SERVER SIGNOFF notify"));
1201 /* Backup router shouldn't accept SERVER_SIGNOFF's from normal routers
1202 when the backup isn't acting as primary router. */
1203 if (idata->conn_type == SILC_CONN_SERVER &&
1204 server->backup_router && server->server_type == SILC_BACKUP_ROUTER)
1208 if (!silc_argument_get_decoded(args, 1, SILC_ARGUMENT_ID, &id, NULL))
1211 /* If the ID is mine, this notify is not allowed. */
1212 if (SILC_ID_SERVER_COMPARE(SILC_ID_GET_ID(id), server->id)) {
1213 SILC_LOG_DEBUG(("Ignoring my own ID for SERVER_SIGNOFF"));
1217 /* Get server entry */
1218 server_entry = silc_idlist_find_server_by_id(server->global_list,
1222 if (!server_entry) {
1223 server_entry = silc_idlist_find_server_by_id(server->local_list,
1227 if (!server_entry) {
1228 /* If we are normal server then we might not have the server. Check
1229 whether router was kind enough to send the list of all clients
1230 that actually was to be removed. Remove them if the list is
1232 if (server->server_type != SILC_ROUTER &&
1233 silc_argument_get_arg_num(args) > 1) {
1236 for (i = 1; i < silc_argument_get_arg_num(args); i++) {
1238 if (!silc_argument_get_decoded(args, i + 1, SILC_ARGUMENT_ID,
1242 /* Get client entry */
1243 client = silc_idlist_find_client_by_id(server->global_list,
1244 SILC_ID_GET_ID(id2),
1248 client = silc_idlist_find_client_by_id(server->local_list,
1249 SILC_ID_GET_ID(id2),
1256 /* Update statistics */
1257 SILC_LOG_DEBUG(("stat.clients %d->%d", server->stat.clients,
1258 server->stat.clients - 1));
1259 SILC_VERIFY(server->stat.clients > 0);
1260 server->stat.clients--;
1261 if (server->stat.cell_clients)
1262 server->stat.cell_clients--;
1263 SILC_OPER_STATS_UPDATE(client, server, SILC_UMODE_SERVER_OPERATOR);
1264 SILC_OPER_STATS_UPDATE(client, router, SILC_UMODE_ROUTER_OPERATOR);
1266 /* Remove the client from all channels. */
1267 silc_server_remove_from_channels(server, NULL, client,
1268 TRUE, NULL, FALSE, FALSE);
1270 /* Check if anyone is watching this nickname */
1271 if (server->server_type == SILC_ROUTER)
1272 silc_server_check_watcher_list(server, client, NULL,
1273 SILC_NOTIFY_TYPE_SERVER_SIGNOFF);
1275 /* Remove this client from watcher list if it is */
1277 silc_server_del_from_watcher_list(server, client);
1279 /* Remove the client */
1280 silc_dlist_del(server->expired_clients, client);
1281 silc_idlist_del_data(client);
1282 silc_idlist_del_client(local ? server->local_list :
1283 server->global_list, client);
1291 /* For local entrys SERVER_SIGNOFF is processed only on backup router.
1292 It is possible that router sends server signoff for a server. If
1293 backup router has it as local connection it will be closed. */
1294 if (SILC_IS_LOCAL(server_entry)) {
1295 if (server->server_type == SILC_BACKUP_ROUTER) {
1296 sock = server_entry->connection;
1297 SILC_LOG_DEBUG(("Closing connection after SERVER_SIGNOFF"));
1298 silc_server_free_sock_user_data(server, sock, NULL);
1299 silc_server_close_connection(server, sock);
1305 /* Remove all servers that are originated from this server, and
1306 remove the clients of those servers too. */
1307 silc_server_remove_servers_by_server(server, server_entry, TRUE);
1309 /* Remove the clients that this server owns as they will become
1311 silc_server_remove_clients_by_server(server, server_entry->router,
1312 server_entry, TRUE);
1313 silc_server_backup_del(server, server_entry);
1315 /* Remove the server entry */
1316 silc_idlist_del_server(local ? server->local_list :
1317 server->global_list, server_entry);
1319 /* Update statistics */
1320 if (server->server_type == SILC_ROUTER)
1321 server->stat.servers--;
1325 case SILC_NOTIFY_TYPE_KICKED:
1327 * Distribute the notify to local clients on the channel
1330 SILC_LOG_DEBUG(("KICKED notify"));
1332 if (!silc_id_str2id(packet->dst_id, packet->dst_id_len,
1333 packet->dst_id_type, &channel_id,
1334 sizeof(channel_id)))
1337 /* Get channel entry */
1338 channel = silc_idlist_find_channel_by_id(server->global_list,
1341 channel = silc_idlist_find_channel_by_id(server->local_list,
1344 SILC_LOG_DEBUG(("Notify for unknown channel %s",
1345 silc_id_render(SILC_ID_GET_ID(id), SILC_ID_CHANNEL)));
1351 if (!silc_argument_get_decoded(args, 1, SILC_ARGUMENT_ID, &id, NULL))
1354 /* If the the client is not in local list we check global list */
1355 client = silc_idlist_find_client_by_id(server->global_list,
1356 SILC_ID_GET_ID(id), TRUE, NULL);
1358 client = silc_idlist_find_client_by_id(server->local_list,
1359 SILC_ID_GET_ID(id), TRUE, NULL);
1364 /* If target is founder they cannot be kicked */
1365 if (!silc_server_client_on_channel(client, channel, &chl))
1367 if (chl->mode & SILC_CHANNEL_UMODE_CHANFO)
1370 /* Get the kicker's Client ID */
1371 if (!silc_argument_get_decoded(args, 3, SILC_ARGUMENT_ID, &id, NULL))
1374 /* If the the client is not in local list we check global list */
1375 client2 = silc_idlist_find_client_by_id(server->global_list,
1376 SILC_ID_GET_ID(id), TRUE, NULL);
1378 client2 = silc_idlist_find_client_by_id(server->local_list,
1379 SILC_ID_GET_ID(id), TRUE, NULL);
1384 /* Kicker must be operator on channel */
1385 if (!silc_server_client_on_channel(client2, channel, &chl))
1387 if (!(chl->mode & SILC_CHANNEL_UMODE_CHANOP) &&
1388 !(chl->mode & SILC_CHANNEL_UMODE_CHANFO)) {
1389 SILC_LOG_DEBUG(("Kicking is not allowed"));
1393 /* Send to channel */
1394 silc_server_packet_send_to_channel(server, sock, channel, packet->type,
1395 FALSE, TRUE, buffer->data,
1396 silc_buffer_len(buffer));
1398 /* Remove the client from channel's invite list */
1399 if (channel->invite_list && silc_hash_table_count(channel->invite_list)) {
1401 SilcArgumentPayload iargs;
1402 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
1403 ab = silc_argument_payload_encode_one(NULL, tmp, tmp_len, 3);
1404 iargs = silc_argument_payload_parse(ab->data, silc_buffer_len(ab), 1);
1405 silc_server_inviteban_process(server, channel->invite_list, 1, iargs);
1406 silc_buffer_free(ab);
1407 silc_argument_payload_free(iargs);
1410 /* Remove the client from channel */
1411 silc_server_remove_from_one_channel(server, sock, channel, client, FALSE);
1415 case SILC_NOTIFY_TYPE_KILLED:
1418 * Distribute the notify to local clients on channels
1420 unsigned char *comment;
1421 SilcUInt32 comment_len;
1423 SILC_LOG_DEBUG(("KILLED notify"));
1426 if (!silc_argument_get_decoded(args, 1, SILC_ARGUMENT_ID, &id, NULL))
1429 /* If the the client is not in local list we check global list */
1430 client = silc_idlist_find_client_by_id(server->global_list,
1431 SILC_ID_GET_ID(id), TRUE, &cache);
1433 client = silc_idlist_find_client_by_id(server->local_list,
1440 /* If the client is one of ours, then close the connection to the
1441 client now. This removes the client from all channels as well. */
1442 if (packet->dst_id_type == SILC_ID_CLIENT && client->connection) {
1443 sock = client->connection;
1444 silc_server_free_client_data(server, NULL, client, FALSE, NULL);
1445 silc_server_close_connection(server, sock);
1450 comment = silc_argument_get_arg_type(args, 2, &comment_len);
1451 if (comment_len > 128)
1454 /* Get the killer's Client ID */
1455 if (!silc_argument_get_decoded(args, 3, SILC_ARGUMENT_ID, &id, NULL))
1458 if (id.type == SILC_ID_CLIENT) {
1459 /* If the the client is not in local list we check global list */
1460 client2 = silc_idlist_find_client_by_id(server->global_list,
1464 client2 = silc_idlist_find_client_by_id(server->local_list,
1471 /* Killer must be router operator */
1472 if (server->server_type != SILC_SERVER &&
1473 !(client2->mode & SILC_UMODE_ROUTER_OPERATOR)) {
1474 SILC_LOG_DEBUG(("Killing is not allowed"));
1479 /* Send the notify to local clients on the channels except to the
1480 client who is killed. */
1481 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
1482 tmp2 = silc_argument_get_arg_type(args, 3, &tmp2_len);
1483 silc_server_send_notify_on_channels(server, client, client,
1484 SILC_NOTIFY_TYPE_KILLED, 3,
1485 tmp, tmp_len, comment, comment_len,
1488 /* Remove the client from all channels */
1489 silc_server_remove_from_channels(server, NULL, client, FALSE, NULL,
1492 /* Check if anyone is watching this nickname */
1493 if (server->server_type == SILC_ROUTER)
1494 silc_server_check_watcher_list(server, client, NULL,
1495 SILC_NOTIFY_TYPE_KILLED);
1497 /* Remove client's public key from repository, this will free it too. */
1498 if (client->data.public_key) {
1499 silc_skr_del_public_key(server->repository, client->data.public_key,
1501 client->data.public_key = NULL;
1504 /* Update statistics */
1505 SILC_LOG_DEBUG(("stat.clients %d->%d", server->stat.clients,
1506 server->stat.clients - 1));
1507 SILC_VERIFY(server->stat.clients > 0);
1508 server->stat.clients--;
1509 if (server->stat.cell_clients)
1510 server->stat.cell_clients--;
1511 SILC_OPER_STATS_UPDATE(client, server, SILC_UMODE_SERVER_OPERATOR);
1512 SILC_OPER_STATS_UPDATE(client, router, SILC_UMODE_ROUTER_OPERATOR);
1514 if (SILC_IS_LOCAL(client)) {
1515 if (!client->local_detached)
1516 server->stat.my_clients--;
1517 silc_schedule_task_del_by_context(server->schedule, client);
1518 silc_idlist_del_data(client);
1522 client->data.status &= ~SILC_IDLIST_STATUS_REGISTERED;
1524 client->router = NULL;
1525 client->connection = NULL;
1526 client->data.created = silc_time();
1527 silc_dlist_del(server->expired_clients, client);
1528 silc_dlist_add(server->expired_clients, client);
1532 case SILC_NOTIFY_TYPE_UMODE_CHANGE:
1534 * Save the mode of the client.
1537 SILC_LOG_DEBUG(("UMODE_CHANGE notify"));
1540 if (!silc_argument_get_decoded(args, 1, SILC_ARGUMENT_ID, &id, NULL))
1543 /* Get client entry */
1544 client = silc_idlist_find_client_by_id(server->global_list,
1545 SILC_ID_GET_ID(id), TRUE, NULL);
1547 client = silc_idlist_find_client_by_id(server->local_list,
1548 SILC_ID_GET_ID(id), TRUE, NULL);
1554 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
1557 SILC_GET32_MSB(mode, tmp);
1559 /* Remove internal resumed flag if client is marked detached now */
1560 if (mode & SILC_UMODE_DETACHED)
1561 client->data.status &= ~SILC_IDLIST_STATUS_RESUMED;
1563 /* Update statistics */
1564 if (server->server_type == SILC_ROUTER) {
1565 if (mode & SILC_UMODE_GONE) {
1566 if (!(client->mode & SILC_UMODE_GONE))
1567 server->stat.aways++;
1569 if (client->mode & SILC_UMODE_GONE)
1570 server->stat.aways--;
1572 if (mode & SILC_UMODE_DETACHED) {
1573 if (!(client->mode & SILC_UMODE_DETACHED))
1574 server->stat.detached++;
1576 if (client->mode & SILC_UMODE_DETACHED)
1577 server->stat.detached--;
1580 SILC_UMODE_STATS_UPDATE(server, SILC_UMODE_SERVER_OPERATOR);
1581 SILC_UMODE_STATS_UPDATE(router, SILC_UMODE_ROUTER_OPERATOR);
1583 /* Change the mode */
1584 client->mode = mode;
1586 /* Check if anyone is watching this nickname */
1587 if (server->server_type == SILC_ROUTER)
1588 silc_server_check_watcher_list(server, client, NULL,
1589 SILC_NOTIFY_TYPE_UMODE_CHANGE);
1593 case SILC_NOTIFY_TYPE_BAN:
1598 SILC_LOG_DEBUG(("BAN notify"));
1600 /* Get Channel ID */
1601 if (!silc_argument_get_decoded(args, 1, SILC_ARGUMENT_ID, &id, NULL))
1604 /* Get channel entry */
1605 channel = silc_idlist_find_channel_by_id(server->global_list,
1606 SILC_ID_GET_ID(id), NULL);
1608 channel = silc_idlist_find_channel_by_id(server->local_list,
1609 SILC_ID_GET_ID(id), NULL);
1611 SILC_LOG_DEBUG(("Notify for unknown channel %s",
1612 silc_id_render(SILC_ID_GET_ID(id), SILC_ID_CHANNEL)));
1617 /* Get the ban action */
1618 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
1619 if (tmp && tmp_len == 1) {
1620 SilcUInt8 action = (SilcUInt8)tmp[0];
1621 SilcUInt16 iargc = 0;
1622 SilcArgumentPayload iargs;
1625 tmp = silc_argument_get_arg_type(args, 3, &tmp_len);
1626 if (!tmp || tmp_len < 2)
1629 /* Parse the arguments to see they are constructed correctly */
1630 SILC_GET16_MSB(iargc, tmp);
1631 iargs = silc_argument_payload_parse(tmp + 2, tmp_len - 2, iargc);
1635 if (!channel->ban_list)
1637 silc_hash_table_alloc(0, silc_hash_ptr,
1639 silc_server_inviteban_destruct, channel, TRUE);
1641 /* Proces the ban action */
1642 if (!silc_server_inviteban_process(server, channel->ban_list, action,
1645 silc_argument_payload_free(iargs);
1647 /* If we are router we must send this notify to our local servers on
1648 the channel. Normal server does nothing. The notify is not
1650 if (server->server_type == SILC_ROUTER)
1651 silc_server_packet_send_to_channel(server, sock, channel,
1652 packet->type, FALSE, FALSE,
1654 silc_buffer_len(buffer));
1658 case SILC_NOTIFY_TYPE_ERROR:
1665 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
1666 if (!tmp && tmp_len != 1)
1668 error = (SilcStatus)tmp[0];
1670 SILC_LOG_DEBUG(("ERROR notify (%d)", error));
1672 if (error == SILC_STATUS_ERR_NO_SUCH_CLIENT_ID &&
1673 idata->conn_type == SILC_CONN_ROUTER) {
1674 if (!silc_argument_get_decoded(args, 2, SILC_ARGUMENT_ID, &id, NULL))
1677 SILC_LOG_DEBUG(("Received invalid client ID notification, deleting "
1678 "the entry from cache"));
1680 client = silc_idlist_find_client_by_id(server->global_list,
1686 silc_server_remove_from_channels(server, NULL, client, TRUE,
1688 silc_dlist_del(server->expired_clients, client);
1689 silc_idlist_del_data(client);
1690 silc_idlist_del_client(server->global_list, client);
1695 /* Ignore rest of the notify types for now */
1696 case SILC_NOTIFY_TYPE_NONE:
1697 case SILC_NOTIFY_TYPE_MOTD:
1701 SILC_LOG_DEBUG(("Unsupported notify %d", type));
1706 silc_notify_payload_free(payload);
1709 void silc_server_notify(SilcServer server,
1710 SilcPacketStream sock,
1713 silc_server_notify_process(server, sock, packet, &packet->buffer);
1714 silc_packet_free(packet);
1717 void silc_server_notify_list(SilcServer server,
1718 SilcPacketStream sock,
1721 SilcIDListData idata = silc_packet_get_context(sock);
1725 SILC_LOG_DEBUG(("Processing Notify List"));
1727 if (idata->conn_type == SILC_CONN_CLIENT ||
1728 packet->src_id_type != SILC_ID_SERVER)
1731 buffer = silc_buffer_alloc(1024);
1735 while (silc_buffer_len(&packet->buffer)) {
1736 SILC_GET16_MSB(len, packet->buffer.data + 2);
1737 if (len > silc_buffer_len(&packet->buffer))
1740 if (len > silc_buffer_truelen(buffer)) {
1741 silc_buffer_free(buffer);
1742 buffer = silc_buffer_alloc(1024 + len);
1745 silc_buffer_pull_tail(buffer, len);
1746 silc_buffer_put(buffer, packet->buffer.data, len);
1748 /* Process the Notify */
1749 silc_server_notify_process(server, sock, packet, buffer);
1751 silc_buffer_push_tail(buffer, len);
1752 silc_buffer_pull(&packet->buffer, len);
1755 silc_packet_free(packet);
1756 silc_buffer_free(buffer);
1759 /* Received private message. This resolves the destination of the message
1760 and sends the packet. This is used by both server and router. If the
1761 destination is our locally connected client this sends the packet to
1762 the client. This may also send the message for further routing if
1763 the destination is not in our server (or router). */
1765 void silc_server_private_message(SilcServer server,
1766 SilcPacketStream sock,
1769 SilcPacketStream dst_sock;
1770 SilcIDListData idata;
1771 SilcClientEntry client;
1772 SilcClientID client_id;
1774 SILC_LOG_DEBUG(("Start"));
1776 if (packet->src_id_type != SILC_ID_CLIENT ||
1777 packet->dst_id_type != SILC_ID_CLIENT || !packet->dst_id)
1780 /* Get the route to the client */
1781 dst_sock = silc_server_get_client_route(server, packet->dst_id,
1782 packet->dst_id_len, NULL,
1786 unsigned char error;
1788 if (client && client->mode & SILC_UMODE_DETACHED) {
1789 SILC_LOG_DEBUG(("Client is detached, discarding packet"));
1793 /* Send SILC_NOTIFY_TYPE_ERROR to indicate that such destination ID
1794 does not exist or is invalid. */
1795 idp = silc_id_payload_encode_data(packet->dst_id,
1797 packet->dst_id_type);
1801 error = SILC_STATUS_ERR_NO_SUCH_CLIENT_ID;
1802 if (packet->src_id_type == SILC_ID_CLIENT) {
1803 silc_id_str2id(packet->src_id, packet->src_id_len,
1804 packet->src_id_type, &client_id, sizeof(client_id));
1805 silc_server_send_notify_dest(server, sock, FALSE,
1806 &client_id, SILC_ID_CLIENT,
1807 SILC_NOTIFY_TYPE_ERROR, 2,
1809 idp->data, silc_buffer_len(idp));
1811 silc_server_send_notify(server, sock, FALSE,
1812 SILC_NOTIFY_TYPE_ERROR, 2,
1814 idp->data, silc_buffer_len(idp));
1817 silc_buffer_free(idp);
1821 /* Check whether destination client wishes to receive private messages */
1822 if (client && !(packet->flags & SILC_PACKET_FLAG_PRIVMSG_KEY) &&
1823 client->mode & SILC_UMODE_BLOCK_PRIVMSG) {
1824 SILC_LOG_DEBUG(("Client blocks private messages, discarding packet"));
1828 /* Send the private message */
1829 silc_server_packet_route(server, dst_sock, packet);
1832 silc_packet_free(packet);
1835 /* Received private message key packet.. This packet is never for us. It is to
1836 the client in the packet's destination ID. Sending of this sort of packet
1837 equals sending private message, ie. it is sent point to point from
1838 one client to another. */
1840 void silc_server_private_message_key(SilcServer server,
1841 SilcPacketStream sock,
1844 SilcPacketStream dst_sock;
1845 SilcIDListData idata;
1847 SILC_LOG_DEBUG(("Start"));
1849 if (packet->src_id_type != SILC_ID_CLIENT ||
1850 packet->dst_id_type != SILC_ID_CLIENT || !packet->dst_id) {
1851 silc_packet_free(packet);
1855 /* Get the route to the client */
1856 dst_sock = silc_server_get_client_route(server, packet->dst_id,
1857 packet->dst_id_len, NULL,
1860 silc_packet_free(packet);
1864 /* Relay the packet */
1865 silc_server_packet_route(server, dst_sock, packet);
1867 silc_packet_free(packet);
1870 /* Processes incoming command reply packet. The command reply packet may
1871 be destined to one of our clients or it may directly for us. We will
1872 call the command reply routine after processing the packet. */
1874 void silc_server_command_reply(SilcServer server,
1875 SilcPacketStream sock,
1878 SilcBuffer buffer = &packet->buffer;
1879 SilcClientEntry client = NULL;
1882 SILC_LOG_DEBUG(("Start"));
1884 if (packet->dst_id_type == SILC_ID_CHANNEL) {
1885 silc_packet_free(packet);
1889 if (packet->dst_id_type == SILC_ID_CLIENT) {
1890 /* Destination must be one of ours */
1891 if (!silc_id_str2id(packet->dst_id, packet->dst_id_len, SILC_ID_CLIENT,
1893 silc_packet_free(packet);
1896 client = silc_idlist_find_client_by_id(server->local_list, &id,
1899 SILC_LOG_ERROR(("Cannot process command reply to unknown client"));
1900 silc_packet_free(packet);
1905 if (packet->dst_id_type == SILC_ID_SERVER) {
1906 /* For now this must be for us */
1907 if (memcmp(packet->dst_id, server->id_string, server->id_string_len)) {
1908 SILC_LOG_ERROR(("Cannot process command reply to unknown server"));
1909 silc_packet_free(packet);
1914 /* Execute command reply locally for the command */
1915 silc_server_command_reply_process(server, sock, buffer);
1917 /* Relay the packet to the client */
1918 if (packet->dst_id_type == SILC_ID_CLIENT && client)
1919 silc_server_packet_route(server, client->connection, packet);
1921 silc_packet_free(packet);
1924 /* Process received channel message. The message can be originated from
1925 client or server. */
1927 void silc_server_channel_message(SilcServer server,
1928 SilcPacketStream sock,
1931 SilcChannelEntry channel = NULL;
1935 SilcClientEntry sender_entry = NULL;
1936 SilcIDListData idata;
1937 SilcChannelClientEntry chl;
1938 SilcBool local = TRUE;
1940 SILC_LOG_DEBUG(("Processing channel message"));
1943 if (packet->dst_id_type != SILC_ID_CHANNEL) {
1944 SILC_LOG_DEBUG(("Received bad message for channel, dropped"));
1948 /* Find channel entry */
1949 if (!silc_id_str2id(packet->dst_id, packet->dst_id_len, SILC_ID_CHANNEL,
1952 channel = silc_idlist_find_channel_by_id(server->local_list, &id, NULL);
1954 channel = silc_idlist_find_channel_by_id(server->global_list, &id, NULL);
1957 unsigned char error;
1959 /* Send SILC_NOTIFY_TYPE_ERROR to indicate that such destination ID
1960 does not exist or is invalid. */
1961 idp = silc_id_payload_encode_data(packet->dst_id,
1963 packet->dst_id_type);
1967 error = SILC_STATUS_ERR_NO_SUCH_CHANNEL_ID;
1968 if (packet->src_id_type == SILC_ID_CLIENT) {
1969 silc_id_str2id(packet->src_id, packet->src_id_len,
1970 packet->src_id_type, &cid, sizeof(cid));
1971 silc_server_send_notify_dest(server, sock, FALSE,
1972 &cid, SILC_ID_CLIENT,
1973 SILC_NOTIFY_TYPE_ERROR, 2,
1974 &error, 1, idp->data,
1975 silc_buffer_len(idp));
1977 silc_server_send_notify(server, sock, FALSE,
1978 SILC_NOTIFY_TYPE_ERROR, 2,
1979 &error, 1, idp->data, silc_buffer_len(idp));
1982 silc_buffer_free(idp);
1987 /* See that this client is on the channel. If the original sender is
1988 not client (as it can be server as well) we don't do the check. */
1989 if (!silc_id_str2id2(packet->src_id, packet->src_id_len,
1990 packet->src_id_type, &sid))
1992 if (sid.type == SILC_ID_CLIENT) {
1993 sender_entry = silc_idlist_find_client_by_id(server->local_list,
1994 SILC_ID_GET_ID(sid),
1996 if (!sender_entry) {
1998 sender_entry = silc_idlist_find_client_by_id(server->global_list,
1999 SILC_ID_GET_ID(sid),
2002 if (!sender_entry || !silc_server_client_on_channel(sender_entry,
2004 SILC_LOG_DEBUG(("Client not on channel"));
2008 /* If channel is moderated check that client is allowed to send
2010 if (channel->mode & SILC_CHANNEL_MODE_SILENCE_USERS &&
2011 !(chl->mode & SILC_CHANNEL_UMODE_CHANOP) &&
2012 !(chl->mode & SILC_CHANNEL_UMODE_CHANFO)) {
2013 SILC_LOG_DEBUG(("Channel is silenced from normal users"));
2016 if (channel->mode & SILC_CHANNEL_MODE_SILENCE_OPERS &&
2017 chl->mode & SILC_CHANNEL_UMODE_CHANOP &&
2018 !(chl->mode & SILC_CHANNEL_UMODE_CHANFO)) {
2019 SILC_LOG_DEBUG(("Channel is silenced from operators"));
2022 if (chl->mode & SILC_CHANNEL_UMODE_QUIET) {
2023 SILC_LOG_DEBUG(("Sender is quieted on the channel"));
2027 /* If the packet is coming from router, but the client entry is local
2028 entry to us then some router is rerouting this to us and it is not
2029 allowed. When the client is local to us it means that we've routed
2030 this packet to network, and now someone is routing it back to us. */
2031 idata = silc_packet_get_context(sock);
2032 if (server->server_type == SILC_ROUTER &&
2033 idata->conn_type == SILC_CONN_ROUTER && local) {
2034 SILC_LOG_DEBUG(("Channel message rerouted to the sender, drop it"));
2039 /* Distribute the packet to our local clients. This will send the
2040 packet for further routing as well, if needed. */
2041 silc_server_packet_relay_to_channel(server, sock, channel,
2042 SILC_ID_GET_ID(sid), sid.type,
2043 sender_entry, packet->buffer.data,
2044 silc_buffer_len(&packet->buffer));
2047 silc_packet_free(packet);
2050 /* Received channel key packet. We distribute the key to all of our locally
2051 connected clients on the channel. */
2053 void silc_server_channel_key(SilcServer server,
2054 SilcPacketStream sock,
2057 SilcBuffer buffer = &packet->buffer;
2058 SilcIDListData idata = silc_packet_get_context(sock);
2059 SilcChannelEntry channel;
2061 if (packet->src_id_type != SILC_ID_SERVER ||
2062 (server->server_type == SILC_ROUTER && !server->backup_router &&
2063 idata->conn_type == SILC_CONN_ROUTER)) {
2064 silc_packet_free(packet);
2068 /* Save the channel key */
2069 channel = silc_server_save_channel_key(server, buffer, NULL);
2071 SILC_LOG_ERROR(("Bad channel key from %s", idata->sconn->remote_host));
2072 silc_packet_free(packet);
2076 /* Distribute the key to everybody who is on the channel. If we are router
2077 we will also send it to locally connected servers. */
2078 silc_server_send_channel_key(server, sock, channel, FALSE);
2080 if (server->server_type != SILC_BACKUP_ROUTER)
2081 /* Distribute to local cell backup routers. */
2082 silc_server_backup_send(server, (SilcServerEntry)idata,
2083 SILC_PACKET_CHANNEL_KEY, 0,
2084 buffer->data, silc_buffer_len(buffer),
2087 silc_packet_free(packet);
2090 /* Received New Client packet and processes it. Creates Client ID for the
2091 client. Client becomes registered after calling this functions. */
2093 SilcClientEntry silc_server_new_client(SilcServer server,
2094 SilcPacketStream sock,
2097 SilcBuffer buffer = &packet->buffer;
2098 SilcIDListData idata = silc_packet_get_context(sock);
2099 SilcClientEntry client;
2100 SilcClientID *client_id;
2101 char *username = NULL, *realname = NULL;
2102 SilcUInt16 username_len, nickname_len;
2103 SilcUInt32 id_len, tmp_len;
2105 char *host, *nickname = NULL, *nicknamec;
2106 const char *hostname, *ip;
2108 SILC_LOG_DEBUG(("Creating new client"));
2110 if (idata->conn_type != SILC_CONN_CLIENT) {
2111 silc_packet_free(packet);
2115 /* Take client entry */
2116 client = (SilcClientEntry)idata;
2117 silc_socket_stream_get_info(silc_packet_stream_get_stream(sock),
2118 NULL, &hostname, &ip, NULL);
2120 SILC_LOG_DEBUG(("%s %s", ip, hostname));
2122 /* Make sure this client hasn't registered already */
2123 if (idata->status & SILC_IDLIST_STATUS_REGISTERED) {
2124 silc_packet_free(packet);
2128 /* Parse incoming packet */
2129 ret = silc_buffer_unformat(buffer,
2131 SILC_STR_UI16_NSTRING_ALLOC(&username,
2133 SILC_STR_UI16_STRING_ALLOC(&realname),
2136 silc_free(username);
2137 silc_free(realname);
2138 SILC_LOG_ERROR(("Client %s (%s) sent incomplete information, closing "
2139 "connection", hostname, ip));
2140 silc_server_disconnect_remote(server, sock,
2141 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
2143 silc_server_free_sock_user_data(server, sock, NULL);
2144 silc_packet_free(packet);
2149 silc_free(username);
2150 silc_free(realname);
2151 SILC_LOG_ERROR(("Client %s (%s) did not send its username, closing "
2152 "connection", hostname, ip));
2153 silc_server_disconnect_remote(server, sock,
2154 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
2156 silc_server_free_sock_user_data(server, sock, NULL);
2157 silc_packet_free(packet);
2161 if (username_len > 128) {
2163 username[username_len - 1] = '\0';
2166 /* Take nickname from NEW_CLIENT packet, if present */
2167 if (silc_buffer_unformat(buffer,
2168 SILC_STR_UI16_NSTRING_ALLOC(&nickname,
2170 SILC_STR_END) >= 0) {
2171 if (nickname_len > 128) {
2173 nickname[nickname_len - 1] = '\0';
2177 /* Nickname is initially same as username, if not present in NEW_CLIENT */
2179 nickname = strdup(username);
2180 nickname_len = strlen(nickname);
2183 /* Check for valid username string */
2184 nicknamec = silc_identifier_check(nickname, nickname_len,
2185 SILC_STRING_UTF8, 128, &tmp_len);
2187 silc_free(username);
2188 silc_free(realname);
2189 silc_free(nickname);
2190 SILC_LOG_ERROR(("Client %s (%s) sent bad username string '%s', closing "
2191 "connection", hostname, ip, username));
2192 silc_server_disconnect_remote(server, sock,
2193 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
2195 silc_server_free_sock_user_data(server, sock, NULL);
2196 silc_packet_free(packet);
2200 /* Make sanity checks for the hostname of the client. If the hostname
2201 is provided in the `username' check that it is the same than the
2202 resolved hostname, or if not resolved the hostname that appears in
2203 the client's public key. If the hostname is not present then put
2204 it from the resolved name or from the public key. */
2205 if (strchr(username, '@')) {
2206 SilcSILCPublicKey silc_pubkey;
2207 int tlen = strcspn(username, "@");
2208 char *phostname = NULL;
2210 host = silc_memdup(username + tlen + 1, strlen(username) - tlen - 1);
2212 if (strcmp(hostname, ip) && strcmp(hostname, host)) {
2213 silc_free(nickname);
2214 silc_free(username);
2216 silc_free(realname);
2217 SILC_LOG_ERROR(("Client %s (%s) sent incomplete information, closing "
2218 "connection", hostname, ip));
2219 silc_server_disconnect_remote(server, sock,
2220 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
2222 silc_server_free_sock_user_data(server, sock, NULL);
2223 silc_packet_free(packet);
2227 silc_pubkey = silc_pkcs_get_context(SILC_PKCS_SILC,
2228 client->data.public_key);
2229 phostname = strdup(silc_pubkey->identifier.host);
2230 if (!strcmp(hostname, ip) && phostname && strcmp(phostname, host)) {
2231 silc_free(nickname);
2232 silc_free(username);
2234 silc_free(phostname);
2235 silc_free(realname);
2236 SILC_LOG_ERROR(("Client %s (%s) sent incomplete information, closing "
2237 "connection", hostname, ip));
2238 silc_server_disconnect_remote(server, sock,
2239 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
2241 silc_server_free_sock_user_data(server, sock, NULL);
2242 silc_packet_free(packet);
2246 silc_free(phostname);
2248 /* The hostname is not present, add it. */
2250 newusername = silc_calloc(strlen(username) + strlen(hostname) + 2,
2251 sizeof(*newusername));
2252 strncat(newusername, username, strlen(username));
2253 strncat(newusername, "@", 1);
2254 strncat(newusername, hostname, strlen(hostname));
2255 silc_free(username);
2256 username = newusername;
2259 SILC_LOG_DEBUG(("%s %s", ip, hostname));
2261 /* Create Client ID */
2262 if (!silc_id_create_client_id(server, server->id, server->rng,
2263 server->md5hash, nicknamec,
2264 strlen(nicknamec), &client_id)) {
2265 silc_server_disconnect_remote(server, sock,
2266 SILC_STATUS_ERR_BAD_NICKNAME, NULL);
2267 silc_server_free_sock_user_data(server, sock, NULL);
2268 silc_packet_free(packet);
2272 /* If client marked as anonymous, scramble the username and hostname */
2273 if (client->mode & SILC_UMODE_ANONYMOUS) {
2276 if (strlen(username) >= 2) {
2277 username[0] = silc_rng_get_byte_fast(server->rng);
2278 username[1] = silc_rng_get_byte_fast(server->rng);
2281 scramble = silc_hash_babbleprint(server->sha1hash, username,
2285 memcpy(&scramble[16], ".silc", 5);
2286 scramble[21] = '\0';
2287 silc_free(username);
2288 username = scramble;
2291 /* Update client entry */
2292 idata->status |= SILC_IDLIST_STATUS_REGISTERED;
2293 client->nickname = nickname;
2294 client->username = username;
2295 client->userinfo = realname ? realname : strdup(username);
2296 client->id = client_id;
2297 id_len = silc_id_get_len(client_id, SILC_ID_CLIENT);
2298 silc_idcache_update_by_context(server->local_list->clients, client,
2299 client_id, nicknamec, TRUE);
2301 /* Notify our router about new client on the SILC network */
2302 silc_server_send_new_id(server, SILC_PRIMARY_ROUTE(server),
2303 SILC_BROADCAST(server), client->id,
2304 SILC_ID_CLIENT, id_len);
2306 /* Distribute to backup routers */
2307 if (server->server_type == SILC_ROUTER) {
2308 SilcBuffer idp = silc_id_payload_encode(client->id, SILC_ID_CLIENT);
2309 silc_server_backup_send(server, NULL, SILC_PACKET_NEW_ID, 0,
2310 idp->data, silc_buffer_len(idp), FALSE, TRUE);
2311 silc_buffer_free(idp);
2314 /* Send the new client ID to the client. */
2315 silc_server_send_new_id(server, sock, FALSE, client->id, SILC_ID_CLIENT,
2316 silc_id_get_len(client->id, SILC_ID_CLIENT));
2318 /* Send some nice info to the client */
2319 silc_server_send_connect_notifys(server, sock, client);
2321 /* Check if anyone is watching this nickname */
2322 if (server->server_type == SILC_ROUTER)
2323 silc_server_check_watcher_list(server, client, NULL, 0);
2325 silc_packet_free(packet);
2329 /* Create new server. This processes received New Server packet and
2330 saves the received Server ID. The server is our locally connected
2331 server thus we save all the information and save it to local list.
2332 This funtion can be used by both normal server and router server.
2333 If normal server uses this it means that its router has connected
2334 to the server. If router uses this it means that one of the cell's
2335 servers is connected to the router. */
2337 SilcServerEntry silc_server_new_server(SilcServer server,
2338 SilcPacketStream sock,
2341 SilcBuffer buffer = &packet->buffer;
2342 SilcIDListData idata = silc_packet_get_context(sock);
2343 SilcServerEntry new_server, server_entry;
2344 SilcServerID server_id;
2345 unsigned char *server_name, *server_namec, *id_string;
2346 SilcUInt16 id_len, name_len;
2348 SilcBool local = TRUE;
2349 const char *hostname, *ip;
2351 SILC_LOG_DEBUG(("Creating new server"));
2353 if (idata->conn_type != SILC_CONN_SERVER &&
2354 idata->conn_type != SILC_CONN_ROUTER) {
2355 silc_packet_free(packet);
2359 /* Take server entry */
2360 new_server = (SilcServerEntry)idata;
2361 silc_socket_stream_get_info(silc_packet_stream_get_stream(sock),
2362 NULL, &hostname, &ip, NULL);
2365 if (server->server_type == SILC_ROUTER)
2366 server->stat.cell_servers++;
2368 /* Remove the old cache entry */
2369 if (!silc_idcache_del_by_context(server->local_list->servers, new_server,
2371 if (!silc_idcache_del_by_context(server->global_list->servers,
2372 new_server, NULL)) {
2373 SILC_LOG_INFO(("Unauthenticated %s attempted to register to "
2374 "network", (idata->conn_type == SILC_CONN_SERVER ?
2375 "server" : "router")));
2376 silc_server_disconnect_remote(server, sock,
2377 SILC_STATUS_ERR_NOT_AUTHENTICATED, NULL);
2378 silc_server_free_sock_user_data(server, sock, NULL);
2384 /* Make sure this server hasn't registered already */
2385 if (idata->status & SILC_IDLIST_STATUS_REGISTERED) {
2386 silc_server_disconnect_remote(server, sock,
2387 SILC_STATUS_ERR_OPERATION_ALLOWED,
2388 "Too many registrations");
2389 silc_server_free_sock_user_data(server, sock, NULL);
2393 /* Parse the incoming packet */
2394 ret = silc_buffer_unformat(buffer,
2395 SILC_STR_UI16_NSTRING_ALLOC(&id_string, &id_len),
2396 SILC_STR_UI16_NSTRING_ALLOC(&server_name,
2400 silc_free(id_string);
2401 silc_free(server_name);
2402 silc_server_disconnect_remote(server, sock,
2403 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
2405 silc_server_free_sock_user_data(server, sock, NULL);
2409 if (id_len > silc_buffer_len(buffer)) {
2410 silc_free(id_string);
2411 silc_free(server_name);
2412 silc_server_disconnect_remote(server, sock,
2413 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
2415 silc_server_free_sock_user_data(server, sock, NULL);
2419 if (name_len > 256) {
2420 server_name[256] = '\0';
2425 if (!silc_id_str2id(id_string, id_len, SILC_ID_SERVER, &server_id,
2426 sizeof(server_id))) {
2427 silc_free(id_string);
2428 silc_free(server_name);
2429 silc_server_disconnect_remote(server, sock,
2430 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
2432 silc_server_free_sock_user_data(server, sock, NULL);
2435 silc_free(id_string);
2437 /* Check for valid server ID */
2438 if (!silc_id_is_valid_server_id(server, &server_id, sock)) {
2439 SILC_LOG_INFO(("Invalid server ID sent by %s (%s)",
2441 silc_server_disconnect_remote(server, sock,
2442 SILC_STATUS_ERR_BAD_SERVER_ID, NULL);
2443 silc_server_free_sock_user_data(server, sock, NULL);
2444 silc_free(server_name);
2448 /* Check that we do not have this ID already */
2449 server_entry = silc_idlist_find_server_by_id(server->local_list,
2450 &server_id, TRUE, NULL);
2452 if (SILC_IS_LOCAL(server_entry)) {
2453 SILC_LOG_ERROR(("Too many registrations from %s (%s)",
2455 silc_server_disconnect_remote(server, sock,
2456 SILC_STATUS_ERR_OPERATION_ALLOWED,
2457 "Too many registrations");
2458 silc_server_free_sock_user_data(server, sock, NULL);
2461 silc_idcache_del_by_context(server->local_list->servers, server_entry,
2465 server_entry = silc_idlist_find_server_by_id(server->global_list,
2466 &server_id, TRUE, NULL);
2468 if (SILC_IS_LOCAL(server_entry)) {
2469 SILC_LOG_ERROR(("Too many registrations from %s (%s)",
2471 silc_server_disconnect_remote(server, sock,
2472 SILC_STATUS_ERR_OPERATION_ALLOWED,
2473 "Too many registrations");
2474 silc_server_free_sock_user_data(server, sock, NULL);
2477 silc_idcache_del_by_context(server->global_list->servers,
2478 server_entry, NULL);
2483 /* Check server name */
2484 server_namec = silc_identifier_check(server_name, strlen(server_name),
2485 SILC_STRING_UTF8, 256, NULL);
2486 if (!server_namec) {
2487 SILC_LOG_ERROR(("Malformed server name from %s (%s)",
2489 silc_server_disconnect_remote(server, sock,
2490 SILC_STATUS_ERR_OPERATION_ALLOWED,
2491 "Malfromed server name");
2492 silc_server_free_sock_user_data(server, sock, NULL);
2496 /* Update server entry */
2497 idata->status |= SILC_IDLIST_STATUS_REGISTERED;
2498 new_server->server_name = server_name;
2499 new_server->id = silc_id_dup(&server_id, SILC_ID_SERVER);
2501 SILC_LOG_DEBUG(("New server id(%s)",
2502 silc_id_render(&server_id, SILC_ID_SERVER)));
2504 /* Add again the entry to the ID cache. */
2505 silc_idcache_add(local ? server->local_list->servers :
2506 server->global_list->servers, server_namec,
2507 new_server->id, new_server);
2509 /* Distribute the information about new server in the SILC network
2510 to our router. If we are normal server we won't send anything
2511 since this connection must be our router connection. */
2512 if (server->server_type == SILC_ROUTER && !server->standalone &&
2513 SILC_PRIMARY_ROUTE(server) != sock)
2514 silc_server_send_new_id(server, SILC_PRIMARY_ROUTE(server),
2515 TRUE, new_server->id, SILC_ID_SERVER,
2516 silc_id_get_len(&server_id, SILC_ID_SERVER));
2518 if (server->server_type == SILC_ROUTER) {
2519 /* Distribute to backup routers */
2520 SilcBuffer idp = silc_id_payload_encode(new_server->id, SILC_ID_SERVER);
2521 silc_server_backup_send(server, NULL, SILC_PACKET_NEW_ID, 0, idp->data,
2522 silc_buffer_len(idp), FALSE, TRUE);
2523 silc_buffer_free(idp);
2526 /* Check whether this router connection has been replaced by an
2527 backup router. If it has been then we'll disable the server and will
2528 ignore everything it will send until the backup router resuming
2529 protocol has been completed. */
2530 if (idata->conn_type == SILC_CONN_ROUTER &&
2531 silc_server_backup_replaced_get(server, &server_id, NULL)) {
2532 /* Send packet to the router indicating that it cannot use this
2533 connection as it has been replaced by backup router. */
2534 SILC_LOG_DEBUG(("Remote router has been replaced by backup router, "
2535 "disabling its connection"));
2537 silc_server_backup_send_replaced(server, sock);
2539 /* Mark the router disabled. The data sent earlier will go but nothing
2540 after this goes to this connection. */
2541 idata->status |= SILC_IDLIST_STATUS_DISABLED;
2543 /* If it is router announce our stuff to it. */
2544 if (idata->conn_type == SILC_CONN_ROUTER &&
2545 server->server_type == SILC_ROUTER) {
2546 silc_server_announce_servers(server, FALSE, 0, sock);
2547 silc_server_announce_clients(server, 0, sock);
2548 silc_server_announce_channels(server, 0, sock);
2551 /* Announce our information to backup router */
2552 if (new_server->server_type == SILC_BACKUP_ROUTER &&
2553 idata->conn_type == SILC_CONN_SERVER &&
2554 server->server_type == SILC_ROUTER) {
2555 silc_server_announce_servers(server, TRUE, 0, sock);
2556 silc_server_announce_clients(server, 0, sock);
2557 silc_server_announce_channels(server, 0, sock);
2560 /* If backup router, mark it as one of ours. This server is considered
2561 to be backup router after this setting. */
2562 if (new_server->server_type == SILC_BACKUP_ROUTER) {
2563 SilcServerConfigRouter *backup;
2564 backup = silc_server_config_find_backup_conn(server, (char *)ip);
2566 backup = silc_server_config_find_backup_conn(server, (char *)hostname);
2568 /* Add as our backup router */
2569 silc_server_backup_add(server, new_server, backup->backup_replace_ip,
2570 backup->backup_replace_port,
2571 backup->backup_local);
2575 /* By default the servers connected to backup router are disabled
2576 until backup router has become the primary */
2577 if (server->server_type == SILC_BACKUP_ROUTER &&
2578 idata->conn_type == SILC_CONN_SERVER)
2579 idata->status |= SILC_IDLIST_STATUS_DISABLED;
2585 /* Processes incoming New ID packet. New ID Payload is used to distribute
2586 information about newly registered clients and servers. */
2588 static void silc_server_new_id_real(SilcServer server,
2589 SilcPacketStream sock,
2594 SilcIDListData idata = silc_packet_get_context(sock);
2596 SilcServerEntry router, server_entry;
2597 SilcPacketStream router_sock;
2600 SilcServerID sender_id;
2601 const char *hostname, *ip;
2603 SILC_LOG_DEBUG(("Processing new ID"));
2605 if (idata->conn_type == SILC_CONN_CLIENT ||
2606 server->server_type == SILC_SERVER ||
2607 packet->src_id_type != SILC_ID_SERVER)
2610 idp = silc_id_payload_parse(buffer->data, silc_buffer_len(buffer));
2614 id_type = silc_id_payload_get_type(idp);
2616 silc_socket_stream_get_info(silc_packet_stream_get_stream(sock),
2617 NULL, &hostname, &ip, NULL);
2619 /* Normal server cannot have other normal server connections */
2620 server_entry = (SilcServerEntry)idata;
2621 if (id_type == SILC_ID_SERVER && idata->conn_type == SILC_CONN_SERVER &&
2622 server_entry->server_type == SILC_SERVER)
2625 /* If the packet is coming from server then use the sender as the
2626 origin of the the packet. If it came from router then check the real
2627 sender of the packet and use that as the origin. */
2628 if (idata->conn_type == SILC_CONN_SERVER) {
2629 id_list = server->local_list;
2631 router = server_entry;
2633 /* If the sender is backup router and ID is server (and we are not
2634 backup router) then switch the entry to global list. */
2635 if (server_entry->server_type == SILC_BACKUP_ROUTER &&
2636 id_type == SILC_ID_SERVER &&
2637 server->id_entry->server_type != SILC_BACKUP_ROUTER) {
2638 id_list = server->global_list;
2639 router_sock = server->router ? SILC_PRIMARY_ROUTE(server) : sock;
2642 silc_id_str2id(packet->src_id, packet->src_id_len,
2643 packet->src_id_type, &sender_id, sizeof(sender_id));
2644 router = silc_idlist_find_server_by_id(server->global_list,
2645 &sender_id, TRUE, NULL);
2647 router = silc_idlist_find_server_by_id(server->local_list,
2648 &sender_id, TRUE, NULL);
2650 id_list = server->global_list;
2657 case SILC_ID_CLIENT:
2659 SilcClientEntry entry;
2662 if (!silc_id_payload_get_id(idp, &id, sizeof(id)))
2665 /* Check that we do not have this client already */
2666 entry = silc_idlist_find_client_by_id(server->global_list,
2667 &id, server->server_type,
2670 entry = silc_idlist_find_client_by_id(server->local_list,
2671 &id, server->server_type,
2674 SILC_LOG_DEBUG(("Ignoring client that we already have"));
2678 SILC_LOG_DEBUG(("New client id(%s) from [%s] %s",
2679 silc_id_render(&id, SILC_ID_CLIENT),
2680 idata->conn_type == SILC_CONN_SERVER ?
2681 "Server" : "Router", hostname));
2683 /* As a router we keep information of all global information in our
2684 global list. Cell wide information however is kept in the local
2686 entry = silc_idlist_add_client(id_list, NULL, NULL, NULL,
2687 silc_id_dup(&id, SILC_ID_CLIENT),
2690 SILC_LOG_ERROR(("Could not add new client to the ID Cache"));
2692 /* Inform the sender that the ID is not usable */
2693 silc_server_send_notify_signoff(server, sock, FALSE, &id, NULL);
2696 entry->nickname = NULL;
2697 entry->data.status |= SILC_IDLIST_STATUS_REGISTERED;
2699 if (idata->conn_type == SILC_CONN_SERVER)
2700 server->stat.cell_clients++;
2701 SILC_LOG_DEBUG(("stat.clients %d->%d", server->stat.clients,
2702 server->stat.clients + 1));
2703 server->stat.clients++;
2705 /* Check if anyone is watching this nickname */
2706 if (server->server_type == SILC_ROUTER && id_list == server->local_list)
2707 silc_server_check_watcher_list(server, entry, NULL, 0);
2709 if (server->server_type == SILC_ROUTER) {
2710 /* Add the client's public key to repository or get the key with
2712 if (entry->data.public_key) {
2713 if (!silc_server_get_public_key_by_client(server, entry, NULL))
2714 silc_skr_add_public_key_simple(server->repository,
2715 entry->data.public_key,
2716 SILC_SKR_USAGE_IDENTIFICATION,
2719 silc_server_send_command(server, router_sock,
2720 SILC_COMMAND_GETKEY, ++server->cmd_ident,
2722 silc_buffer_len(buffer));
2728 case SILC_ID_SERVER:
2730 SilcServerEntry entry;
2733 if (!silc_id_payload_get_id(idp, &id, sizeof(id)))
2736 /* If the ID is mine, ignore it. */
2737 if (SILC_ID_SERVER_COMPARE(&id, server->id)) {
2738 SILC_LOG_DEBUG(("Ignoring my own ID as new ID"));
2742 /* If the ID is the sender's ID, ignore it (we have it already) */
2743 if (SILC_ID_SERVER_COMPARE(&id, router->id)) {
2744 SILC_LOG_DEBUG(("Ignoring sender's own ID"));
2748 /* Check that we do not have this server already */
2749 entry = silc_idlist_find_server_by_id(server->global_list,
2750 &id, server->server_type,
2753 entry = silc_idlist_find_server_by_id(server->local_list,
2754 &id, server->server_type,
2757 SILC_LOG_DEBUG(("Ignoring server that we already have"));
2761 SILC_LOG_DEBUG(("New server id(%s) from [%s] %s",
2762 silc_id_render(&id, SILC_ID_SERVER),
2763 idata->conn_type == SILC_CONN_SERVER ?
2764 "Server" : "Router", hostname));
2766 /* As a router we keep information of all global information in our
2767 global list. Cell wide information however is kept in the local
2769 entry = silc_idlist_add_server(id_list, NULL, 0,
2770 silc_id_dup(&id, SILC_ID_SERVER), router,
2773 SILC_LOG_ERROR(("Could not add new server to the ID Cache"));
2776 entry->data.status |= SILC_IDLIST_STATUS_REGISTERED;
2778 if (idata->conn_type == SILC_CONN_SERVER)
2779 server->stat.cell_servers++;
2780 server->stat.servers++;
2784 case SILC_ID_CHANNEL:
2785 SILC_LOG_ERROR(("Channel cannot be registered with NEW_ID packet"));
2794 /* If the sender of this packet is server and we are router we need to
2795 broadcast this packet to other routers in the network. */
2796 if (broadcast && 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 packet"));
2800 silc_server_packet_send(server, SILC_PRIMARY_ROUTE(server),
2802 packet->flags | SILC_PACKET_FLAG_BROADCAST,
2803 buffer->data, silc_buffer_len(buffer));
2804 silc_server_backup_send(server, (SilcServerEntry)idata,
2805 packet->type, packet->flags,
2806 packet->buffer.data,
2807 silc_buffer_len(&packet->buffer),
2812 silc_id_payload_free(idp);
2816 /* Processes incoming New ID packet. New ID Payload is used to distribute
2817 information about newly registered clients and servers. */
2819 void silc_server_new_id(SilcServer server, SilcPacketStream sock,
2822 silc_server_new_id_real(server, sock, packet, &packet->buffer, TRUE);
2823 silc_packet_free(packet);
2826 /* Receoved New Id List packet, list of New ID payloads inside one
2827 packet. Process the New ID payloads one by one. */
2829 void silc_server_new_id_list(SilcServer server, SilcPacketStream sock,
2832 SilcIDListData idata = silc_packet_get_context(sock);
2836 SILC_LOG_DEBUG(("Processing New ID List"));
2838 if (idata->conn_type == SILC_CONN_CLIENT ||
2839 packet->src_id_type != SILC_ID_SERVER) {
2840 silc_packet_free(packet);
2844 /* If the sender of this packet is server and we are router we need to
2845 broadcast this packet to other routers in the network. Broadcast
2846 this list packet instead of multiple New ID packets. */
2847 if (server->server_type == SILC_ROUTER &&
2848 idata->conn_type == SILC_CONN_SERVER &&
2849 !(packet->flags & SILC_PACKET_FLAG_BROADCAST)) {
2850 SILC_LOG_DEBUG(("Broadcasting received New ID List packet"));
2851 silc_server_packet_send(server, SILC_PRIMARY_ROUTE(server),
2853 packet->flags | SILC_PACKET_FLAG_BROADCAST,
2854 packet->buffer.data,
2855 silc_buffer_len(&packet->buffer));
2856 silc_server_backup_send(server, (SilcServerEntry)idata,
2857 packet->type, packet->flags,
2858 packet->buffer.data,
2859 silc_buffer_len(&packet->buffer),
2863 idp = silc_buffer_alloc(256);
2865 silc_packet_free(packet);
2869 while (silc_buffer_len(&packet->buffer)) {
2870 SILC_GET16_MSB(id_len, packet->buffer.data + 2);
2871 if ((id_len > silc_buffer_len(&packet->buffer)) ||
2872 (id_len > silc_buffer_truelen(idp)))
2875 silc_buffer_pull_tail(idp, 4 + id_len);
2876 silc_buffer_put(idp, packet->buffer.data, 4 + id_len);
2878 /* Process the New ID */
2879 silc_server_new_id_real(server, sock, packet, idp, FALSE);
2881 silc_buffer_push_tail(idp, 4 + id_len);
2882 silc_buffer_pull(&packet->buffer, 4 + id_len);
2885 silc_buffer_free(idp);
2886 silc_packet_free(packet);
2889 /* Received New Channel packet. Information about new channels in the
2890 network are distributed using this packet. Save the information about
2891 the new channel. This usually comes from router but also normal server
2892 can send this to notify channels it has when it connects to us. */
2894 static void silc_server_new_channel_process(SilcServer server,
2895 SilcPacketStream sock,
2899 SilcIDListData idata = silc_packet_get_context(sock);
2900 SilcChannelPayload payload;
2901 SilcChannelID channel_id;
2902 char *channel_name, *channel_namec = NULL;
2903 SilcUInt32 name_len;
2904 unsigned char *id, cid[32];
2905 SilcUInt32 id_len, cipher_len;
2906 SilcServerEntry server_entry;
2907 SilcChannelEntry channel;
2910 if (idata->conn_type == SILC_CONN_CLIENT ||
2911 packet->src_id_type != SILC_ID_SERVER ||
2912 server->server_type == SILC_SERVER)
2915 /* Parse the channel payload */
2916 payload = silc_channel_payload_parse(buffer->data, silc_buffer_len(buffer));
2920 /* Get the channel ID */
2921 if (!silc_channel_get_id_parse(payload, &channel_id)) {
2922 silc_channel_payload_free(payload);
2926 channel_name = silc_channel_get_name(payload, &name_len);
2927 if (name_len > 256) {
2928 channel_name[256] = '\0';
2932 /* Check channel name */
2933 channel_namec = silc_channel_name_check(channel_name, strlen(channel_name),
2934 SILC_STRING_UTF8, 256, NULL);
2938 id = silc_channel_get_id(payload, &id_len);
2940 server_entry = (SilcServerEntry)idata;
2942 if (idata->conn_type == SILC_CONN_ROUTER) {
2943 /* Add the channel to global list as it is coming from router. It
2944 cannot be our own channel as it is coming from router. */
2946 /* Check that we don't already have this channel */
2947 channel = silc_idlist_find_channel_by_name(server->local_list,
2948 channel_namec, NULL);
2950 channel = silc_idlist_find_channel_by_name(server->global_list,
2951 channel_namec, NULL);
2953 SILC_LOG_DEBUG(("New channel id(%s) from [Router] %s",
2954 silc_id_render(&channel_id, SILC_ID_CHANNEL),
2955 idata->sconn->remote_host));
2958 silc_idlist_add_channel(server->global_list, strdup(channel_name),
2959 0, silc_id_dup(&channel_id, SILC_ID_CHANNEL),
2960 (SilcServerEntry)idata, NULL, NULL, NULL);
2962 silc_channel_payload_free(payload);
2965 channel->disabled = TRUE; /* Disabled until someone JOINs */
2967 server->stat.channels++;
2968 if (server->server_type == SILC_ROUTER)
2969 channel->users_resolved = TRUE;
2972 /* The channel is coming from our server, thus it is in our cell
2973 we will add it to our local list. */
2976 SILC_LOG_DEBUG(("Channel id(%s) from [Server] %s",
2977 silc_id_render(&channel_id, SILC_ID_CHANNEL),
2978 idata->sconn->remote_host));
2980 /* Check that we don't already have this channel */
2981 channel = silc_idlist_find_channel_by_name(server->local_list,
2982 channel_namec, NULL);
2984 channel = silc_idlist_find_channel_by_name(server->global_list,
2985 channel_namec, NULL);
2987 /* If the channel does not exist, then create it. This creates a new
2988 key to the channel as well that we will send to the server. */
2990 SILC_LOG_DEBUG(("Channel is new to us"));
2992 /* The protocol says that the Channel ID's IP address must be based
2993 on the router's IP address. Check whether the ID is based in our
2994 IP and if it is not then create a new ID and enforce the server
2995 to switch the ID. */
2996 if (server_entry->server_type != SILC_BACKUP_ROUTER &&
2997 !SILC_ID_COMPARE(&channel_id, server->id, server->id->ip.data_len)) {
2999 SILC_LOG_DEBUG(("Forcing the server to change Channel ID"));
3000 if (silc_id_create_channel_id(server, server->id, server->rng, &tmp)) {
3001 silc_server_send_notify_channel_change(server, sock, FALSE,
3003 silc_channel_payload_free(payload);
3007 /* Wait that server re-announces this channel */
3011 /* Create the channel with the provided Channel ID */
3013 silc_server_create_new_channel_with_id(
3016 silc_id_dup(&channel_id, SILC_ID_CHANNEL),
3019 silc_channel_payload_free(payload);
3022 channel->disabled = TRUE; /* Disabled until someone JOINs */
3024 #if 0 /* We assume that CMODE_CHANGE notify is sent to us after this. */
3026 /* XXX Dunno if this is supposed to be set in any server type. If set
3027 here the CMODE_CHANGE that may follow sets mode that we already
3028 have, and we may loose data from the CMODE_CHANGE notify. */
3029 if (server_entry->server_type != SILC_BACKUP_ROUTER)
3030 channel->mode = silc_channel_get_mode(payload);
3033 /* Send the new channel key to the server */
3034 silc_id_id2str(channel->id, SILC_ID_CHANNEL, cid, sizeof(cid),
3036 cipher = silc_cipher_get_name(channel->send_key);
3037 cipher_len = strlen(cipher);
3038 chk = silc_channel_key_payload_encode(id_len, cid,
3040 channel->key_len / 8,
3042 silc_server_packet_send(server, sock, SILC_PACKET_CHANNEL_KEY, 0,
3043 chk->data, silc_buffer_len(chk));
3044 silc_buffer_free(chk);
3046 /* The channel exist by that name, check whether the ID's match.
3047 If they don't then we'll force the server to use the ID we have.
3048 We also create a new key for the channel. */
3049 SilcBuffer modes = NULL, users = NULL, users_modes = NULL;
3051 SILC_LOG_DEBUG(("Channel already exists"));
3053 if (!SILC_ID_CHANNEL_COMPARE(&channel_id, channel->id)) {
3054 /* They don't match, send CHANNEL_CHANGE notify to the server to
3055 force the ID change. */
3056 SILC_LOG_DEBUG(("Forcing the server to change Channel ID"));
3057 silc_server_send_notify_channel_change(server, sock, FALSE,
3058 &channel_id, channel->id);
3059 silc_channel_payload_free(payload);
3061 /* Wait that server re-announces this channel */
3065 #if 0 /* We will announce our CMODE anyway for this channel, so no need
3066 to check it (implicit enforce). */
3068 /* If the mode is different from what we have then enforce the
3070 mode = silc_channel_get_mode(payload);
3071 if (channel->mode != mode) {
3072 SILC_LOG_DEBUG(("Forcing the server to change channel mode"));
3073 silc_server_send_notify_cmode(server, sock, FALSE, channel,
3074 channel->mode, server->id,
3075 SILC_ID_SERVER, channel->cipher,
3077 channel->passphrase,
3078 channel->founder_key);
3082 /* Create new key for the channel and send it to the server and
3083 everybody else possibly on the channel. */
3084 if (!(channel->mode & SILC_CHANNEL_MODE_PRIVKEY)) {
3086 if (silc_hash_table_count(channel->user_list)) {
3087 if (!silc_server_create_channel_key(server, channel, 0)) {
3088 silc_channel_payload_free(payload);
3092 /* Send to the channel */
3093 silc_server_send_channel_key(server, sock, channel, FALSE);
3096 /* Send to the server */
3097 silc_id_id2str(channel->id, SILC_ID_CHANNEL, cid, sizeof(cid),
3099 cipher = silc_cipher_get_name(channel->send_key);
3100 cipher_len = strlen(cipher);
3101 chk = silc_channel_key_payload_encode(id_len, cid,
3103 channel->key_len / 8,
3105 silc_server_packet_send(server, sock, SILC_PACKET_CHANNEL_KEY, 0,
3106 chk->data, silc_buffer_len(chk));
3107 silc_buffer_free(chk);
3110 /* Since the channel is coming from server and we also know about it
3111 then send the JOIN notify to the server so that it see's our
3112 users on the channel "joining" the channel. */
3113 silc_server_announce_get_channel_users(server, channel, &modes, &users,
3116 silc_buffer_push(users, users->data - users->head);
3117 silc_server_packet_send(server, sock,
3118 SILC_PACKET_NOTIFY, SILC_PACKET_FLAG_LIST,
3119 users->data, silc_buffer_len(users));
3120 silc_buffer_free(users);
3123 silc_buffer_push(modes, modes->data - modes->head);
3124 silc_server_packet_send_dest(server, sock,
3125 SILC_PACKET_NOTIFY, SILC_PACKET_FLAG_LIST,
3126 channel->id, SILC_ID_CHANNEL,
3127 modes->data, silc_buffer_len(modes));
3128 silc_buffer_free(modes);
3131 silc_buffer_push(users_modes, users_modes->data - users_modes->head);
3132 silc_server_packet_send_dest(server, sock,
3133 SILC_PACKET_NOTIFY, SILC_PACKET_FLAG_LIST,
3134 channel->id, SILC_ID_CHANNEL,
3136 silc_buffer_len(users_modes));
3137 silc_buffer_free(users_modes);
3139 if (channel->topic) {
3140 silc_server_send_notify_topic_set(server, sock,
3141 server->server_type == SILC_ROUTER ?
3142 TRUE : FALSE, channel,
3143 server->id, SILC_ID_SERVER,
3149 /* If the sender of this packet is server and we are router we need to
3150 broadcast this packet to other routers in the network. Broadcast
3151 this list packet instead of multiple New Channel packets. */
3152 if (server->server_type == SILC_ROUTER &&
3153 idata->conn_type == SILC_CONN_SERVER &&
3154 !(packet->flags & SILC_PACKET_FLAG_BROADCAST)) {
3155 SILC_LOG_DEBUG(("Broadcasting received New Channel packet"));
3156 silc_server_packet_send(server, SILC_PRIMARY_ROUTE(server),
3158 packet->flags | SILC_PACKET_FLAG_BROADCAST,
3159 buffer->data, silc_buffer_len(buffer));
3160 silc_server_backup_send(server, (SilcServerEntry)idata,
3161 packet->type, packet->flags,
3162 buffer->data, silc_buffer_len(buffer),
3166 silc_free(channel_namec);
3167 silc_channel_payload_free(payload);
3170 /* Received New Channel packet. Information about new channels in the
3171 network are distributed using this packet. Save the information about
3172 the new channel. This usually comes from router but also normal server
3173 can send this to notify channels it has when it connects to us. */
3175 void silc_server_new_channel(SilcServer server,
3176 SilcPacketStream sock,
3179 silc_server_new_channel_process(server, sock, packet, &packet->buffer);
3180 silc_packet_free(packet);
3183 /* Received New Channel List packet, list of New Channel List payloads inside
3184 one packet. Process the New Channel payloads one by one. */
3186 void silc_server_new_channel_list(SilcServer server,
3187 SilcPacketStream sock,
3190 SilcIDListData idata = silc_packet_get_context(sock);
3192 SilcUInt16 len1, len2;
3194 SILC_LOG_DEBUG(("Processing New Channel List"));
3196 if (idata->conn_type == SILC_CONN_CLIENT ||
3197 packet->src_id_type != SILC_ID_SERVER ||
3198 server->server_type == SILC_SERVER) {
3199 silc_packet_free(packet);
3203 buffer = silc_buffer_alloc(512);
3205 silc_packet_free(packet);
3209 while (silc_buffer_len(&packet->buffer)) {
3210 SILC_GET16_MSB(len1, packet->buffer.data);
3211 if ((len1 > silc_buffer_len(&packet->buffer)) ||
3212 (len1 > silc_buffer_truelen(buffer)))
3215 SILC_GET16_MSB(len2, packet->buffer.data + 2 + len1);
3216 if ((len2 > silc_buffer_len(&packet->buffer)) ||
3217 (len2 > silc_buffer_truelen(buffer)))
3220 silc_buffer_pull_tail(buffer, 8 + len1 + len2);
3221 silc_buffer_put(buffer, packet->buffer.data, 8 + len1 + len2);
3223 /* Process the New Channel */
3224 silc_server_new_channel_process(server, sock, packet, buffer);
3226 silc_buffer_push_tail(buffer, 8 + len1 + len2);
3227 silc_buffer_pull(&packet->buffer, 8 + len1 + len2);
3230 silc_buffer_free(buffer);
3231 silc_packet_free(packet);
3234 /* Received key agreement packet. This packet is never for us. It is to
3235 the client in the packet's destination ID. Sending of this sort of packet
3236 equals sending private message, ie. it is sent point to point from
3237 one client to another. */
3239 void silc_server_key_agreement(SilcServer server,
3240 SilcPacketStream sock,
3243 SilcPacketStream dst_sock;
3244 SilcIDListData idata;
3246 SILC_LOG_DEBUG(("Start"));
3248 if (packet->src_id_type != SILC_ID_CLIENT ||
3249 packet->dst_id_type != SILC_ID_CLIENT || !packet->dst_id) {
3250 silc_packet_free(packet);
3254 /* Get the route to the client */
3255 dst_sock = silc_server_get_client_route(server, packet->dst_id,
3256 packet->dst_id_len, NULL,
3259 silc_packet_free(packet);
3263 /* Relay the packet */
3264 silc_server_packet_route(server, dst_sock, packet);
3265 silc_packet_free(packet);
3268 /* Received connection auth request packet that is used during connection
3269 phase to resolve the mandatory authentication method. This packet can
3270 actually be received at anytime but usually it is used only during
3271 the connection authentication phase. Now, protocol says that this packet
3272 can come from client or server, however, we support only this coming
3273 from client and expect that server always knows what authentication
3276 void silc_server_connection_auth_request(SilcServer server,
3277 SilcPacketStream sock,
3280 SilcServerConfigClient *client = NULL;
3281 SilcUInt16 conn_type;
3283 SilcAuthMethod auth_meth = SILC_AUTH_NONE;
3284 const char *hostname, *ip;
3286 if (packet->src_id_type && packet->src_id_type != SILC_ID_CLIENT) {
3287 SILC_LOG_DEBUG(("Request not from client"));
3288 silc_packet_free(packet);
3292 silc_socket_stream_get_info(silc_packet_stream_get_stream(sock),
3293 NULL, &hostname, &ip, NULL);
3295 /* Parse the payload */
3296 ret = silc_buffer_unformat(&packet->buffer,
3297 SILC_STR_UI_SHORT(&conn_type),
3298 SILC_STR_UI_SHORT(NULL),
3300 if (ret == -1 || conn_type != SILC_CONN_CLIENT) {
3301 silc_packet_free(packet);
3305 /* Get the authentication method for the client */
3306 auth_meth = SILC_AUTH_NONE;
3307 client = silc_server_config_find_client(server, (char *)ip);
3309 client = silc_server_config_find_client(server, (char *)hostname);
3311 if (client->passphrase) {
3312 if (client->publickeys && !server->config->prefer_passphrase_auth)
3313 auth_meth = SILC_AUTH_PUBLIC_KEY;
3315 auth_meth = SILC_AUTH_PASSWORD;
3316 } else if (client->publickeys)
3317 auth_meth = SILC_AUTH_PUBLIC_KEY;
3320 SILC_LOG_DEBUG(("Authentication method is [%s]",
3321 (auth_meth == SILC_AUTH_NONE ? "None" :
3322 auth_meth == SILC_AUTH_PASSWORD ? "Passphrase" :
3323 "Digital signatures")));
3325 /* Send it back to the client */
3326 silc_server_send_connection_auth_request(server, sock, conn_type, auth_meth);
3327 silc_packet_free(packet);
3330 /* Received file transger packet. This packet is never for us. It is to
3331 the client in the packet's destination ID. Sending of this sort of packet
3332 equals sending private message, ie. it is sent point to point from
3333 one client to another. */
3335 void silc_server_ftp(SilcServer server,
3336 SilcPacketStream sock,
3339 SilcPacketStream dst_sock;
3340 SilcIDListData idata;
3342 SILC_LOG_DEBUG(("Start"));
3344 if (packet->src_id_type != SILC_ID_CLIENT ||
3345 packet->dst_id_type != SILC_ID_CLIENT || !packet->dst_id) {
3346 silc_packet_free(packet);
3350 /* Get the route to the client */
3351 dst_sock = silc_server_get_client_route(server, packet->dst_id,
3352 packet->dst_id_len, NULL,
3355 silc_packet_free(packet);
3359 /* Relay the packet */
3360 silc_server_packet_route(server, dst_sock, packet);
3361 silc_packet_free(packet);
3366 SilcPacketStream sock;
3368 SilcClientID client_id;
3369 } *SilcServerResumeResolve;
3371 SILC_SERVER_CMD_FUNC(resume_resolve)
3373 SilcServerResumeResolve r = (SilcServerResumeResolve)context;
3374 SilcServer server = r->server;
3375 SilcPacketStream sock = r->sock;
3376 SilcServerCommandReplyContext reply = context2;
3377 SilcClientEntry client;
3378 const char *hostname, *ip;
3380 SILC_LOG_DEBUG(("Start"));
3382 silc_socket_stream_get_info(silc_packet_stream_get_stream(sock),
3383 NULL, &hostname, &ip, NULL);
3385 if (!reply || !silc_command_get_status(reply->payload, NULL, NULL)) {
3386 SILC_LOG_ERROR(("Client %s (%s) tried to resume unknown client, "
3387 "closing connection", hostname, ip));
3388 silc_server_disconnect_remote(server, sock,
3389 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
3390 "Resuming not possible");
3391 silc_server_free_sock_user_data(server, sock, NULL);
3395 if (reply && silc_command_get(reply->payload) == SILC_COMMAND_WHOIS) {
3396 /* Get entry to the client, and resolve it if we don't have it. */
3397 client = silc_idlist_find_client_by_id(server->local_list,
3398 &r->client_id, TRUE, NULL);
3400 client = silc_idlist_find_client_by_id(server->global_list,
3401 &r->client_id, TRUE, NULL);
3403 SILC_LOG_ERROR(("Client %s (%s) tried to resume unknown client, "
3404 "closing connection", hostname, ip));
3405 silc_server_disconnect_remote(server, sock,
3406 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
3407 "Resuming not possible");
3408 silc_server_free_sock_user_data(server, sock, NULL);
3413 if (!(client->mode & SILC_UMODE_DETACHED)) {
3414 SILC_LOG_ERROR(("Client %s (%s) tried to resume un-detached client, "
3415 "closing connection", hostname, ip));
3416 silc_server_disconnect_remote(server, sock,
3417 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
3418 "Resuming not possible");
3419 silc_server_free_sock_user_data(server, sock, NULL);
3423 client->data.status |= SILC_IDLIST_STATUS_RESUME_RES;
3426 /* Reprocess the packet */
3427 silc_server_resume_client(server, sock, r->packet);
3430 silc_packet_stream_unref(r->sock);
3434 /* Received client resuming packet. This is used to resume detached
3435 client session. It can be sent by the client who wishes to resume
3436 but this is also sent by servers and routers to notify other routers
3437 that the client is not detached anymore. */
3439 void silc_server_resume_client(SilcServer server,
3440 SilcPacketStream sock,
3443 SilcBuffer buffer = &packet->buffer, buf;
3444 SilcIDListData idata = silc_packet_get_context(sock);
3445 SilcIDCacheEntry id_cache = NULL;
3446 SilcClientEntry detached_client;
3447 SilcClientID client_id;
3448 unsigned char *id_string, *auth = NULL, *nicknamec = NULL;
3449 unsigned char cid[32];
3451 SilcUInt16 id_len, auth_len = 0;
3452 SilcBool resolved, local, nick_change = FALSE, resolve = FALSE;
3453 SilcChannelEntry channel;
3454 SilcHashTableList htl;
3455 SilcChannelClientEntry chl;
3456 SilcServerResumeResolve r;
3457 SilcPublicKey public_key;
3458 const char *cipher, *hostname, *ip;
3460 SILC_LOG_DEBUG(("Resuming client"));
3462 silc_socket_stream_get_info(silc_packet_stream_get_stream(sock),
3463 NULL, &hostname, &ip, NULL);
3465 if (silc_buffer_unformat(buffer,
3466 SILC_STR_UI16_NSTRING(&id_string, &id_len),
3467 SILC_STR_END) < 0) {
3468 if (idata->conn_type == SILC_CONN_CLIENT) {
3469 SILC_LOG_ERROR(("Client %s (%s) sent incomplete resume information, "
3470 "closing connection", hostname, ip));
3471 silc_server_disconnect_remote(server, sock,
3472 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
3473 "Resuming not possible");
3474 silc_server_free_sock_user_data(server, sock, NULL);
3479 silc_id_str2id(id_string, id_len, SILC_ID_CLIENT, &client_id,
3482 if (idata->conn_type == SILC_CONN_CLIENT) {
3483 /* Client send this and is attempting to resume to old client session */
3484 SilcClientEntry client;
3487 silc_buffer_pull(buffer, 2 + id_len);
3488 auth = buffer->data;
3489 auth_len = silc_buffer_len(buffer);
3490 silc_buffer_push(buffer, 2 + id_len);
3492 if (auth_len < 128) {
3493 SILC_LOG_ERROR(("Client %s (%s) sent incomplete resume information, "
3494 "closing connection", hostname, ip));
3495 silc_server_disconnect_remote(server, sock,
3496 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
3497 "Resuming not possible");
3498 silc_server_free_sock_user_data(server, sock, NULL);
3502 /* Take client entry of this connection */
3503 client = (SilcClientEntry)idata;
3505 /* Get entry to the client, and resolve it if we don't have it. */
3506 detached_client = silc_server_query_client(server, &client_id, FALSE,
3508 if (!detached_client) {
3510 /* The client info is being resolved. Reprocess this packet after
3511 receiving the reply to the query. */
3512 SILC_LOG_DEBUG(("Resolving client"));
3513 r = silc_calloc(1, sizeof(*r));
3516 silc_packet_stream_ref(sock);
3520 r->client_id = client_id;
3521 silc_server_command_pending(server, SILC_COMMAND_WHOIS,
3523 silc_server_command_resume_resolve, r);
3526 SILC_LOG_ERROR(("Client %s (%s) tried to resume unknown client, "
3527 "closing connection", hostname, ip));
3528 silc_server_disconnect_remote(server, sock,
3529 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
3530 "Resuming not possible");
3531 silc_server_free_sock_user_data(server, sock, NULL);
3536 if (detached_client->data.status & SILC_IDLIST_STATUS_RESUMED) {
3537 SILC_LOG_ERROR(("Client %s (%s) tried to attach more than once, "
3538 "closing connection", hostname, ip));
3539 silc_server_disconnect_remote(server, sock,
3540 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
3541 "Resuming not possible");
3542 silc_server_free_sock_user_data(server, sock, NULL);
3546 if (detached_client->resuming_client &&
3547 detached_client->resuming_client != client) {
3548 SILC_LOG_ERROR(("Client %s (%s) tried to attach more than once, "
3549 "closing connection", hostname, ip));
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);
3557 if (!detached_client->resuming_client)
3558 detached_client->resuming_client = client;
3560 if (!(detached_client->mode & SILC_UMODE_DETACHED))
3562 if (!silc_hash_table_count(detached_client->channels) &&
3563 detached_client->router)
3565 if (!detached_client->nickname)
3567 if (detached_client->data.status & SILC_IDLIST_STATUS_RESUME_RES)
3571 if (server->server_type == SILC_SERVER && !server->standalone) {
3572 /* The client info is being resolved. Reprocess this packet after
3573 receiving the reply to the query. */
3574 SILC_LOG_DEBUG(("Resolving client info"));
3575 silc_server_query_client(server, &client_id, TRUE, NULL);
3576 r = silc_calloc(1, sizeof(*r));
3579 silc_packet_stream_ref(sock);
3583 r->client_id = client_id;
3584 silc_server_command_pending(server, SILC_COMMAND_WHOIS,
3586 silc_server_command_resume_resolve, r);
3589 if (server->server_type == SILC_SERVER) {
3590 SILC_LOG_ERROR(("Client %s (%s) tried to resume un-detached client, "
3591 "closing connection", hostname, ip));
3592 silc_server_disconnect_remote(server, sock,
3593 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
3594 "Resuming not possible");
3595 silc_server_free_sock_user_data(server, sock, NULL);
3600 /* Check that we have the public key of the client, if not then we must
3601 resolve it first. */
3602 if (!detached_client->data.public_key) {
3603 if (server->server_type == SILC_SERVER && server->standalone) {
3604 SILC_LOG_ERROR(("Detached client's public key not present, "
3605 "closing connection"));
3606 silc_server_disconnect_remote(server, sock,
3607 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
3608 "Resuming not possible");
3609 silc_server_free_sock_user_data(server, sock, NULL);
3612 /* We must retrieve the detached client's public key by sending
3613 GETKEY command. Reprocess this packet after receiving the key */
3614 SilcBuffer idp = silc_id_payload_encode(&client_id, SILC_ID_CLIENT);
3615 SilcPacketStream dest_sock =
3616 silc_server_get_client_route(server, NULL, 0, &client_id,
3619 SILC_LOG_DEBUG(("Resolving client public key"));
3621 silc_server_send_command(server, dest_sock ? dest_sock :
3622 SILC_PRIMARY_ROUTE(server),
3623 SILC_COMMAND_GETKEY, ++server->cmd_ident,
3624 1, 1, idp->data, silc_buffer_len(idp));
3626 r = silc_calloc(1, sizeof(*r));
3629 silc_packet_stream_ref(sock);
3633 r->client_id = client_id;
3634 silc_server_command_pending(server, SILC_COMMAND_GETKEY,
3636 silc_server_command_resume_resolve, r);
3638 silc_buffer_free(idp);
3641 } else if (!silc_pkcs_public_key_compare(detached_client->data.public_key,
3642 idata->public_key)) {
3643 /* We require that the connection and resuming authentication data
3644 must be using same key pair. */
3645 SILC_LOG_ERROR(("Resuming attempted with wrong public key, "
3646 "closing connection"));
3647 silc_server_disconnect_remote(server, sock,
3648 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
3649 "Resuming not possible");
3650 silc_server_free_sock_user_data(server, sock, NULL);
3654 /* Verify the authentication payload. This has to be successful in
3655 order to allow the resuming */
3657 !silc_auth_verify_data(auth, auth_len, SILC_AUTH_PUBLIC_KEY,
3658 detached_client->data.public_key, 0,
3659 idata->hash, detached_client->id,
3661 SILC_LOG_ERROR(("Client %s (%s) resume authentication failed, "
3662 "closing connection", hostname, ip));
3663 silc_server_disconnect_remote(server, sock,
3664 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
3665 "Resuming not possible");
3666 silc_server_free_sock_user_data(server, sock, NULL);
3670 /* Check nickname */
3671 nicknamec = silc_identifier_check(detached_client->nickname,
3672 strlen(detached_client->nickname),
3673 SILC_STRING_UTF8, 128, NULL);
3675 silc_server_disconnect_remote(server, sock,
3676 SILC_STATUS_ERR_BAD_NICKNAME,
3677 "Malformed nickname, cannot resume");
3678 silc_server_free_sock_user_data(server, sock, NULL);
3682 /* If the ID is not based in our ID then change it */
3683 if (!SILC_ID_COMPARE(detached_client->id, server->id,
3684 server->id->ip.data_len)) {
3685 SilcClientID *new_id;
3686 if (!silc_id_create_client_id(server, server->id, server->rng,
3687 server->md5hash, nicknamec,
3688 strlen(nicknamec), &new_id)) {
3689 silc_server_disconnect_remote(server, sock,
3690 SILC_STATUS_ERR_BAD_NICKNAME,
3691 "Resuming not possible");
3692 silc_server_free_sock_user_data(server, sock, NULL);
3696 client_id = *new_id;
3700 /* Now resume the client to the network */
3702 silc_schedule_task_del_by_context(server->schedule, detached_client);
3703 silc_packet_set_context(sock, detached_client);
3704 detached_client->connection = sock;
3706 if (detached_client->data.public_key) {
3707 /* Delete the detached client's public key from repository */
3708 silc_skr_del_public_key(server->repository,
3709 detached_client->data.public_key,
3711 detached_client->data.public_key = NULL;
3714 if (idata->public_key) {
3715 /* Delete the resuming client's public key from repository. It will
3716 be added later again. */
3717 public_key = silc_pkcs_public_key_copy(idata->public_key);
3718 silc_skr_del_public_key(server->repository, idata->public_key, idata);
3719 idata->public_key = public_key;
3722 /* Take new keys and stuff into use in the old entry */
3723 silc_idlist_del_data(detached_client);
3724 silc_idlist_add_data(detached_client, idata);
3725 idata->public_key = NULL;
3727 if (detached_client->data.public_key) {
3728 /* Add the resumed client's public key back to repository. */
3729 if (!silc_server_get_public_key_by_client(server, detached_client, NULL))
3730 silc_skr_add_public_key_simple(server->repository,
3731 detached_client->data.public_key,
3732 SILC_SKR_USAGE_IDENTIFICATION,
3733 detached_client, NULL);
3736 detached_client->data.status |= SILC_IDLIST_STATUS_REGISTERED;
3737 detached_client->data.status |= SILC_IDLIST_STATUS_RESUMED;
3738 detached_client->data.status |= SILC_IDLIST_STATUS_LOCAL;
3739 detached_client->data.status &= ~SILC_IDLIST_STATUS_RESUME_RES;
3740 detached_client->mode &= ~SILC_UMODE_DETACHED;
3741 server->stat.my_detached--;
3742 silc_dlist_del(server->expired_clients, detached_client);
3744 /* We are finished - reset resuming client */
3745 detached_client->resuming_client = NULL;
3747 /* Check if anyone is watching this client */
3748 if (server->server_type == SILC_ROUTER)
3749 silc_server_check_watcher_list(server, detached_client, NULL,
3750 SILC_NOTIFY_TYPE_UMODE_CHANGE);
3752 /* Delete this current client entry since we're resuming to old one.
3753 We decrement clients/cell_clients as we are getting rid of the
3754 current client and replacing it with the detached one. We keep the
3755 server user count as-is (incremented by the current client entry) as
3756 we decremented the count already during detach, thus we'd be undoing
3758 detached_client->local_detached = FALSE;
3759 SILC_LOG_DEBUG(("stat.clients %d->%d", server->stat.clients,
3760 server->stat.clients - 1));
3761 SILC_VERIFY(server->stat.clients > 0);
3762 server->stat.clients--;
3763 if (server->stat.cell_clients)
3764 server->stat.cell_clients--;
3765 silc_server_remove_from_channels(server, NULL, client, FALSE,
3766 NULL, FALSE, FALSE);
3767 silc_server_del_from_watcher_list(server, client);
3768 silc_dlist_del(server->expired_clients, client);
3769 if (!silc_idlist_del_client(server->local_list, client))
3770 silc_idlist_del_client(server->global_list, client);
3771 client = detached_client;
3772 silc_free(client->servername);
3773 client->servername = strdup(server->server_name);
3775 /* Send the RESUME_CLIENT packet to our primary router so that others
3776 know this client isn't detached anymore. */
3777 buf = silc_buffer_alloc_size(2 + id_len);
3778 silc_buffer_format(buf,
3779 SILC_STR_UI_SHORT(id_len),
3780 SILC_STR_UI_XNSTRING(id_string, id_len),
3783 /* Send to primary router */
3784 silc_server_packet_send(server, SILC_PRIMARY_ROUTE(server),
3785 SILC_PACKET_RESUME_CLIENT, 0,
3786 buf->data, silc_buffer_len(buf));
3787 silc_server_backup_send(server, client->router,
3788 SILC_PACKET_RESUME_CLIENT, 0,
3789 buf->data, silc_buffer_len(buf), TRUE, TRUE);
3791 /* As router we must deliver this packet directly to the original
3792 server whom this client was earlier. */
3793 if (server->server_type == SILC_ROUTER && client->router &&
3794 client->router->server_type != SILC_ROUTER)
3795 silc_server_packet_send(server, client->router->connection,
3796 SILC_PACKET_RESUME_CLIENT, 0,
3797 buf->data, silc_buffer_len(buf));
3798 silc_buffer_free(buf);
3799 client->router = NULL;
3802 /* Notify about Client ID change, nickname doesn't actually change. */
3803 silc_server_send_notify_nick_change(server, SILC_PRIMARY_ROUTE(server),
3804 SILC_BROADCAST(server),
3805 client->id, &client_id,
3809 /* Resolve users on those channels that client has joined but we
3810 haven't resolved user list yet. */
3811 if (server->server_type == SILC_SERVER && !server->standalone) {
3812 silc_hash_table_list(client->channels, &htl);
3813 while (silc_hash_table_get(&htl, NULL, (void *)&chl)) {
3814 channel = chl->channel;
3815 SILC_LOG_DEBUG(("Resolving users for %s channel",
3816 channel->channel_name));
3817 if (channel->disabled || !channel->users_resolved) {
3818 silc_server_send_command(server, SILC_PRIMARY_ROUTE(server),
3819 SILC_COMMAND_USERS, ++server->cmd_ident,
3820 1, 2, channel->channel_name,
3821 strlen(channel->channel_name));
3824 silc_hash_table_list_reset(&htl);
3827 /* Send the new client ID to the client. After this client may start
3828 receiving other packets, and may start sending packets too. */
3829 silc_server_send_new_id(server, sock, FALSE, &client_id, SILC_ID_CLIENT,
3830 silc_id_get_len(&client_id, SILC_ID_CLIENT));
3833 /* Send NICK change notify to channels as well. */
3834 SilcBuffer oidp, nidp;
3835 oidp = silc_id_payload_encode(client->id, SILC_ID_CLIENT);
3836 nidp = silc_id_payload_encode(&client_id, SILC_ID_CLIENT);
3837 silc_server_send_notify_on_channels(server, NULL, client,
3838 SILC_NOTIFY_TYPE_NICK_CHANGE, 3,
3839 oidp->data, silc_buffer_len(oidp),
3840 nidp->data, silc_buffer_len(nidp),
3842 strlen(client->nickname));
3843 silc_buffer_free(oidp);
3844 silc_buffer_free(nidp);
3848 if (!silc_idcache_update_by_context(server->local_list->clients, client,
3849 &client_id, NULL, FALSE))
3850 silc_idcache_update_by_context(server->global_list->clients, client,
3851 &client_id, NULL, FALSE);
3853 /* Move entry to local list if it is in global list */
3854 if (silc_idcache_find_by_context(server->global_list->clients, client,
3856 silc_idcache_move(server->global_list->clients,
3857 server->local_list->clients, id_cache);
3859 /* Send some nice info to the client */
3860 silc_server_send_connect_notifys(server, sock, client);
3862 /* Send all channel keys of channels the client has joined */
3863 silc_hash_table_list(client->channels, &htl);
3864 while (silc_hash_table_get(&htl, NULL, (void *)&chl)) {
3865 SilcBool created = FALSE;
3866 channel = chl->channel;
3868 if (channel->mode & SILC_CHANNEL_MODE_PRIVKEY)
3871 /* If we don't have channel key, then create one */
3872 if (!channel->send_key) {
3873 if (!silc_server_create_channel_key(server, channel, 0))
3878 silc_id_id2str(channel->id, SILC_ID_CHANNEL, cid, sizeof(cid),
3880 cipher = silc_cipher_get_name(channel->send_key);
3882 silc_channel_key_payload_encode(cid_len, cid,
3883 strlen(cipher), cipher,
3884 channel->key_len / 8, channel->key);
3886 /* Send the channel key to the client */
3887 silc_server_packet_send(server, sock, SILC_PACKET_CHANNEL_KEY, 0,
3888 keyp->data, silc_buffer_len(keyp));
3890 /* Distribute the channel key to channel */
3892 silc_server_send_channel_key(server, NULL, channel,
3893 server->server_type == SILC_ROUTER ?
3894 FALSE : !server->standalone);
3895 silc_server_backup_send(server, NULL, SILC_PACKET_CHANNEL_KEY, 0,
3896 keyp->data, silc_buffer_len(keyp),
3900 silc_buffer_free(keyp);
3902 silc_hash_table_list_reset(&htl);
3904 } else if (idata->conn_type != SILC_CONN_CLIENT) {
3905 /* Server or router sent this to us to notify that that a client has
3907 SilcServerEntry server_entry;
3908 SilcServerID server_id;
3910 /* Get entry to the client, and resolve it if we don't have it. */
3911 detached_client = silc_idlist_find_client_by_id(server->local_list,
3914 if (!detached_client) {
3915 detached_client = silc_idlist_find_client_by_id(server->global_list,
3918 if (!detached_client) {
3919 SILC_LOG_DEBUG(("Resuming client is unknown"));
3924 /* Check that the client has not been resumed already because it is
3925 protocol error to attempt to resume more than once. The client
3926 will be killed if this protocol error occurs. */
3927 if (detached_client->data.status & SILC_IDLIST_STATUS_RESUMED &&
3928 !(detached_client->mode & SILC_UMODE_DETACHED)) {
3929 /* The client is clearly attempting to resume more than once and
3930 perhaps playing around by resuming from several different places
3931 at the same time. */
3932 SILC_LOG_DEBUG(("Attempting to re-resume client, killing both"));
3933 silc_server_kill_client(server, detached_client, NULL,
3934 server->id, SILC_ID_SERVER);
3938 /* Check whether client is detached at all */
3939 if (!(detached_client->mode & SILC_UMODE_DETACHED)) {
3940 SILC_LOG_DEBUG(("Client is not detached"));
3944 /* Check nickname */
3945 if (detached_client->nickname) {
3946 nicknamec = silc_identifier_check(detached_client->nickname,
3947 strlen(detached_client->nickname),
3948 SILC_STRING_UTF8, 128, NULL);
3953 SILC_LOG_DEBUG(("Resuming detached client"));
3955 /* If the sender of this packet is server and we are router we need to
3956 broadcast this packet to other routers in the network. */
3957 if (server->server_type == SILC_ROUTER &&
3958 idata->conn_type == SILC_CONN_SERVER &&
3959 !(packet->flags & SILC_PACKET_FLAG_BROADCAST)) {
3960 SILC_LOG_DEBUG(("Broadcasting received Resume Client packet"));
3961 silc_server_packet_send(server, SILC_PRIMARY_ROUTE(server),
3963 packet->flags | SILC_PACKET_FLAG_BROADCAST,
3964 buffer->data, silc_buffer_len(buffer));
3965 silc_server_backup_send(server, (SilcServerEntry)idata,
3966 packet->type, packet->flags,
3967 packet->buffer.data,
3968 silc_buffer_len(&packet->buffer),
3972 /* If the client has a locally-connected previous owner, then we
3973 will need to notify them that the resume has completed. Note
3974 that if the previous owner was a router, this case is already
3975 handled above by the broadcast, so we shouldn't attempt to
3976 send another notification in that case. Additionally, if
3977 the previous owner was the server that sent the packet, then
3978 we'll not send the notification as it will have already done
3979 the necessary work locally. */
3980 if (server->server_type == SILC_ROUTER &&
3981 idata->conn_type == SILC_CONN_SERVER &&
3982 detached_client->router &&
3983 SILC_IS_LOCAL(detached_client->router) &&
3984 detached_client->router->server_type != SILC_ROUTER)
3985 silc_server_packet_send(server, detached_client->router->connection,
3986 SILC_PACKET_RESUME_CLIENT, 0,
3987 buffer->data, silc_buffer_len(buffer));
3989 /* Client is detached, and now it is resumed. Remove the detached
3990 mode and mark that it is resumed. */
3992 if (detached_client->data.public_key) {
3993 /* Delete the detached client's public key from repository */
3994 silc_skr_del_public_key(server->repository,
3995 detached_client->data.public_key,
3997 detached_client->data.public_key = NULL;
4000 silc_idlist_del_data(detached_client);
4001 detached_client->mode &= ~SILC_UMODE_DETACHED;
4002 detached_client->data.status |= SILC_IDLIST_STATUS_RESUMED;
4003 detached_client->data.status &= ~SILC_IDLIST_STATUS_LOCAL;
4004 silc_dlist_del(server->expired_clients, detached_client);
4006 /* Check if anyone is watching this client */
4007 if (server->server_type == SILC_ROUTER)
4008 silc_server_check_watcher_list(server, detached_client, NULL,
4009 SILC_NOTIFY_TYPE_UMODE_CHANGE);
4011 silc_schedule_task_del_by_context(server->schedule, detached_client);
4013 /* Get the new owner of the resumed client */
4014 if (!silc_id_str2id(packet->src_id, packet->src_id_len,
4015 packet->src_id_type, &server_id, sizeof(server_id)))
4018 /* Get server entry */
4019 server_entry = silc_idlist_find_server_by_id(server->global_list,
4020 &server_id, TRUE, NULL);
4022 if (!server_entry) {
4023 server_entry = silc_idlist_find_server_by_id(server->local_list,
4024 &server_id, TRUE, NULL);
4030 if (server->server_type == SILC_ROUTER &&
4031 idata->conn_type == SILC_CONN_ROUTER &&
4032 server_entry->server_type == SILC_ROUTER)
4035 /* Move entry to correct list */
4036 if (local && server->server_type == SILC_ROUTER) {
4037 if (silc_idcache_find_by_context(server->global_list->clients,
4038 detached_client, &id_cache))
4039 silc_idcache_move(server->global_list->clients,
4040 server->local_list->clients, id_cache);
4042 if (silc_idcache_find_by_context(server->local_list->clients,
4043 detached_client, &id_cache))
4044 silc_idcache_move(server->local_list->clients,
4045 server->global_list->clients, id_cache);
4048 /* We don't own this client anymore, if we ever did, as we were just
4049 * told that someone else resumed it. Thus, it is most definitely no
4050 * a detached client.*/
4051 detached_client->local_detached = FALSE;
4052 /* Change the owner of the client */
4053 detached_client->router = server_entry;
4055 /* Update channel information regarding global clients on channel. */
4056 if (server->server_type != SILC_ROUTER) {
4057 silc_hash_table_list(detached_client->channels, &htl);
4058 while (silc_hash_table_get(&htl, NULL, (void *)&chl))
4059 chl->channel->global_users =
4060 silc_server_channel_has_global(chl->channel);
4061 silc_hash_table_list_reset(&htl);
4066 silc_packet_free(packet);