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 /* It's possible router doesn't accept our local client in the network
365 and sends SIGNOFF to our local client */
366 if (SILC_IS_LOCAL(client)) {
367 SILC_LOG_DEBUG(("SIGNOFF from router to local client, disconnect"));
368 if (client->data.sconn) {
369 silc_server_connection_free(client->data.sconn);
370 client->data.sconn = NULL;
372 silc_packet_set_context(client->connection, NULL);
373 silc_server_disconnect_remote(server, client->connection,
374 SILC_STATUS_ERR_RESOURCE_LIMIT,
375 "Router prevented registration");
378 client->data.status &= ~SILC_IDLIST_STATUS_REGISTERED;
380 client->router = NULL;
381 client->connection = NULL;
382 client->data.created = silc_time();
383 silc_dlist_del(server->expired_clients, client);
384 silc_dlist_add(server->expired_clients, client);
387 case SILC_NOTIFY_TYPE_TOPIC_SET:
389 * Distribute the notify to local clients on the channel
392 SILC_LOG_DEBUG(("TOPIC SET notify"));
395 if (!silc_argument_get_decoded(args, 1, SILC_ARGUMENT_ID, &id, NULL))
398 /* Get client entry */
399 if (id.type == SILC_ID_CLIENT) {
400 client = silc_idlist_find_client_by_id(server->global_list,
401 SILC_ID_GET_ID(id), TRUE, &cache);
403 client = silc_idlist_find_client_by_id(server->local_list,
412 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
416 if (!silc_id_str2id(packet->dst_id, packet->dst_id_len,
417 packet->dst_id_type, &channel_id,
421 /* Get channel entry */
422 channel = silc_idlist_find_channel_by_id(server->global_list,
425 channel = silc_idlist_find_channel_by_id(server->local_list,
428 SILC_LOG_DEBUG(("Notify for unknown channel %s",
429 silc_id_render(&channel_id, SILC_ID_CHANNEL)));
434 if (channel->topic && !strcmp(channel->topic, tmp)) {
435 SILC_LOG_DEBUG(("Topic is already set and same"));
440 /* Get user's channel entry and check that topic set is allowed. */
441 if (!silc_server_client_on_channel(client, channel, &chl))
443 if (channel->mode & SILC_CHANNEL_MODE_TOPIC &&
444 !(chl->mode & SILC_CHANNEL_UMODE_CHANOP) &&
445 !(chl->mode & SILC_CHANNEL_UMODE_CHANFO)) {
446 SILC_LOG_DEBUG(("Topic change is not allowed"));
451 /* Change the topic */
452 silc_free(channel->topic);
453 channel->topic = strdup(tmp);
455 /* Send the same notify to the channel */
456 silc_server_packet_send_to_channel(server, NULL, channel, packet->type,
457 FALSE, TRUE, buffer->data,
458 silc_buffer_len(buffer));
461 case SILC_NOTIFY_TYPE_NICK_CHANGE:
464 * Distribute the notify to local clients on the channel
467 SilcUInt32 nickname_len;
469 SILC_LOG_DEBUG(("NICK CHANGE notify"));
471 /* Get old client ID */
472 if (!silc_argument_get_decoded(args, 1, SILC_ARGUMENT_ID, &id, NULL))
475 /* Get new client ID */
476 if (!silc_argument_get_decoded(args, 2, SILC_ARGUMENT_ID, &id2, NULL))
479 SILC_LOG_DEBUG(("Old Client ID id(%s)",
480 silc_id_render(SILC_ID_GET_ID(id), SILC_ID_CLIENT)));
481 SILC_LOG_DEBUG(("New Client ID id(%s)",
482 silc_id_render(SILC_ID_GET_ID(id2), SILC_ID_CLIENT)));
484 /* From protocol version 1.1 we also get the new nickname */
485 nickname = silc_argument_get_arg_type(args, 3, &nickname_len);;
487 /* Replace the Client ID */
488 client = silc_idlist_replace_client_id(server,
491 SILC_ID_GET_ID(id2), nickname);
493 client = silc_idlist_replace_client_id(server,
496 SILC_ID_GET_ID(id2), nickname);
499 /* Send the NICK_CHANGE notify type to local clients on the channels
500 this client is joined to. */
501 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
502 tmp2 = silc_argument_get_arg_type(args, 2, &tmp2_len);
503 silc_server_send_notify_on_channels(server, client, client,
504 SILC_NOTIFY_TYPE_NICK_CHANGE, 3,
505 tmp, tmp_len, tmp2, tmp2_len,
513 case SILC_NOTIFY_TYPE_CMODE_CHANGE:
515 * Distribute the notify to local clients on the channel
518 SILC_LOG_DEBUG(("CMODE CHANGE notify"));
521 if (!silc_argument_get_decoded(args, 1, SILC_ARGUMENT_ID, &id, NULL))
524 /* Get client entry */
525 if (id.type == SILC_ID_CLIENT) {
526 client = silc_idlist_find_client_by_id(server->global_list,
527 SILC_ID_GET_ID(id), TRUE, &cache);
529 client = silc_idlist_find_client_by_id(server->local_list,
537 if (!silc_id_str2id(packet->dst_id, packet->dst_id_len,
538 packet->dst_id_type, &channel_id,
542 /* Get channel entry */
543 channel = silc_idlist_find_channel_by_id(server->global_list,
546 channel = silc_idlist_find_channel_by_id(server->local_list,
549 SILC_LOG_DEBUG(("Notify for unknown channel %s",
550 silc_id_render(&channel_id, SILC_ID_CHANNEL)));
556 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
559 SILC_GET32_MSB(mode, tmp);
561 /* Check if mode changed */
562 if (channel->mode == mode) {
563 SILC_LOG_DEBUG(("Mode is changed already"));
565 /* If this mode change has founder mode then we'll enforce the
566 change so that the server gets the real founder public key */
567 if (server->server_type != SILC_SERVER &&
568 sock != SILC_PRIMARY_ROUTE(server) &&
569 mode & SILC_CHANNEL_MODE_FOUNDER_AUTH && channel->founder_key) {
570 SILC_LOG_DEBUG(("Sending founder public key to server"));
571 silc_server_send_notify_cmode(server, sock, FALSE, channel,
572 channel->mode, server->id,
573 SILC_ID_SERVER, channel->cipher,
576 channel->founder_key, NULL);
579 /* If we received same mode from our primary check whether founder
580 mode and key in the notify is set. We update the founder key
581 here since we may have wrong one */
582 if (server->server_type != SILC_ROUTER &&
583 sock == SILC_PRIMARY_ROUTE(server) &&
584 mode & SILC_CHANNEL_MODE_FOUNDER_AUTH) {
585 SILC_LOG_DEBUG(("Founder public key received from router"));
586 tmp = silc_argument_get_arg_type(args, 6, &tmp_len);
590 if (channel->founder_key)
591 silc_pkcs_public_key_free(channel->founder_key);
592 channel->founder_key = NULL;
593 silc_public_key_payload_decode(tmp, tmp_len,
594 &channel->founder_key);
597 /* Check also for channel public key list */
598 if (server->server_type == SILC_SERVER &&
599 sock == SILC_PRIMARY_ROUTE(server) &&
600 mode & SILC_CHANNEL_MODE_CHANNEL_AUTH) {
603 unsigned char mask[4], ulimit[4];
605 SILC_LOG_DEBUG(("Channel public key list received from router"));
606 tmp = silc_argument_get_arg_type(args, 7, &tmp_len);
610 /* Set the router's list, and send the notify to channel too so that
611 channel gets the list */
612 silc_server_set_channel_pk_list(server, sock, channel, tmp, tmp_len);
613 chpklist = silc_server_get_channel_pk_list(server, channel,
617 sidp = silc_id_payload_encode(server->router->id, SILC_ID_SERVER);
618 SILC_PUT32_MSB(channel->mode, mask);
619 if (channel->mode & SILC_CHANNEL_MODE_ULIMIT)
620 SILC_PUT32_MSB(channel->user_limit, ulimit);
621 silc_server_send_notify_to_channel(server, NULL, channel, FALSE, TRUE,
622 SILC_NOTIFY_TYPE_CMODE_CHANGE, 8,
623 sidp->data, silc_buffer_len(sidp),
627 strlen(channel->cipher) : 0,
630 strlen(channel->hmac_name) : 0,
632 channel->passphrase ?
633 strlen(channel->passphrase) : 0,
636 silc_buffer_len(chpklist),
638 SILC_CHANNEL_MODE_ULIMIT ?
641 SILC_CHANNEL_MODE_ULIMIT ?
642 sizeof(ulimit) : 0));
643 silc_buffer_free(sidp);
644 silc_buffer_free(chpklist);
651 /* Get user's channel entry and check that mode change is allowed */
653 if (!silc_server_client_on_channel(client, channel, &chl))
655 if (!silc_server_check_cmode_rights(server, channel, chl, mode)) {
656 SILC_LOG_DEBUG(("CMODE change is not allowed"));
657 silc_server_send_notify_cmode(server, sock, FALSE, channel,
658 channel->mode, server->id,
659 SILC_ID_SERVER, channel->cipher,
662 channel->founder_key, NULL);
666 /* Assure that server is not removing founder mode from us */
667 if (server->server_type == SILC_ROUTER &&
668 sock != SILC_PRIMARY_ROUTE(server) &&
669 channel->mode & SILC_CHANNEL_MODE_FOUNDER_AUTH &&
670 !(mode & SILC_CHANNEL_MODE_FOUNDER_AUTH)) {
671 SILC_LOG_DEBUG(("Enforcing sender to change channel mode"));
672 silc_server_send_notify_cmode(server, sock, FALSE, channel,
673 channel->mode, server->id,
674 SILC_ID_SERVER, channel->cipher,
677 channel->founder_key, NULL);
681 /* If server is adding founder mode, check whether there is founder
682 on channel already and is not from this server */
683 if (server->server_type == SILC_ROUTER &&
684 sock != SILC_PRIMARY_ROUTE(server) &&
685 mode & SILC_CHANNEL_MODE_FOUNDER_AUTH) {
686 silc_hash_table_list(channel->user_list, &htl);
687 while (silc_hash_table_get(&htl, NULL, (void *)&chl))
688 if (chl->mode & SILC_CHANNEL_UMODE_CHANFO &&
689 chl->client->router != (SilcServerEntry)idata) {
690 SILC_LOG_DEBUG(("Enforcing sender to change channel mode"));
691 silc_server_send_notify_cmode(server, sock, FALSE, channel,
692 channel->mode, server->id,
693 SILC_ID_SERVER, channel->cipher,
696 channel->founder_key, NULL);
697 silc_hash_table_list_reset(&htl);
700 silc_hash_table_list_reset(&htl);
704 /* If the channel had private keys set and the mode was removed then
705 we must re-generate and re-distribute a new channel key */
706 if (channel->mode & SILC_CHANNEL_MODE_PRIVKEY &&
707 !(mode & SILC_CHANNEL_MODE_PRIVKEY)) {
708 /* Re-generate channel key */
709 if (!silc_server_create_channel_key(server, channel, 0))
712 /* Send the channel key. This sends it to our local clients and if
713 we are normal server to our router as well. */
714 silc_server_send_channel_key(server, NULL, channel,
715 server->server_type == SILC_ROUTER ?
716 FALSE : !server->standalone);
720 tmp = silc_argument_get_arg_type(args, 4, &tmp_len);
722 unsigned char hash[SILC_HASH_MAXLEN];
725 silc_hmac_free(channel->hmac);
726 if (!silc_hmac_alloc(tmp, NULL, &channel->hmac))
729 /* Set the HMAC key out of current channel key. The client must do
731 silc_hash_make(silc_hmac_get_hash(channel->hmac), channel->key,
732 channel->key_len / 8, hash);
733 silc_hmac_set_key(channel->hmac, hash,
734 silc_hash_len(silc_hmac_get_hash(channel->hmac)));
735 memset(hash, 0, sizeof(hash));
738 /* Get the passphrase */
739 tmp = silc_argument_get_arg_type(args, 5, &tmp_len);
741 silc_free(channel->passphrase);
742 channel->passphrase = silc_memdup(tmp, tmp_len);
745 /* Get founder public key */
746 tmp = silc_argument_get_arg_type(args, 6, &tmp_len);
747 if (tmp && mode & SILC_CHANNEL_MODE_FOUNDER_AUTH) {
748 if (channel->founder_key)
749 silc_pkcs_public_key_free(channel->founder_key);
750 channel->founder_key = NULL;
751 SILC_LOG_DEBUG(("Founder public key received"));
752 if (!silc_public_key_payload_decode(tmp, tmp_len,
753 &channel->founder_key)) {
754 SILC_LOG_DEBUG(("Enforcing sender to change channel mode"));
755 mode &= ~SILC_CHANNEL_MODE_FOUNDER_AUTH;
756 silc_server_send_notify_cmode(server, sock, FALSE, channel,
757 mode, server->id, SILC_ID_SERVER,
760 channel->passphrase, NULL, NULL);
761 if (channel->founder_key)
762 silc_pkcs_public_key_free(channel->founder_key);
763 channel->founder_key = NULL;
767 if (mode & SILC_CHANNEL_MODE_FOUNDER_AUTH && !channel->founder_key &&
768 server->server_type == SILC_ROUTER) {
769 SILC_LOG_DEBUG(("Enforcing sender to change channel mode"));
770 mode &= ~SILC_CHANNEL_MODE_FOUNDER_AUTH;
771 silc_server_send_notify_cmode(server, sock, FALSE, channel,
772 mode, server->id, SILC_ID_SERVER,
775 channel->passphrase, NULL, NULL);
778 /* Process channel public key(s). */
779 tmp = silc_argument_get_arg_type(args, 7, &tmp_len);
780 if (tmp && mode & SILC_CHANNEL_MODE_CHANNEL_AUTH) {
782 SILC_LOG_DEBUG(("Channel public key list received from router"));
785 silc_server_set_channel_pk_list(server, sock, channel, tmp, tmp_len);
787 /* If list was set already we will enforce the same list to server. */
788 if (ret == SILC_STATUS_ERR_OPERATION_ALLOWED) {
789 SilcBuffer chpklist = silc_server_get_channel_pk_list(server, channel,
791 silc_server_send_notify_cmode(server, sock, FALSE, channel,
792 mode, server->id, SILC_ID_SERVER,
795 channel->passphrase, NULL,
797 silc_buffer_free(chpklist);
801 /* Get the user limit */
802 tmp = silc_argument_get_arg_type(args, 8, &tmp_len);
803 if (tmp && tmp_len == 4 && mode & SILC_CHANNEL_MODE_ULIMIT)
804 SILC_GET32_MSB(channel->user_limit, tmp);
806 /* Send the same notify to the channel */
807 silc_server_packet_send_to_channel(server, NULL, channel, packet->type,
808 FALSE, TRUE, buffer->data,
809 silc_buffer_len(buffer));
812 channel->mode = mode;
814 /* Cleanup if some modes are removed */
816 if (!(channel->mode & SILC_CHANNEL_MODE_FOUNDER_AUTH) &&
817 channel->founder_key) {
818 silc_pkcs_public_key_free(channel->founder_key);
819 channel->founder_key = NULL;
822 if (!(channel->mode & SILC_CHANNEL_MODE_CHANNEL_AUTH) &&
823 channel->channel_pubkeys) {
824 silc_hash_table_free(channel->channel_pubkeys);
825 channel->channel_pubkeys = NULL;
830 case SILC_NOTIFY_TYPE_CUMODE_CHANGE:
833 * Distribute the notify to local clients on the channel
835 SilcChannelClientEntry chl2 = NULL;
836 SilcBool notify_sent = FALSE;
838 SILC_LOG_DEBUG(("CUMODE CHANGE notify"));
841 if (!silc_argument_get_decoded(args, 1, SILC_ARGUMENT_ID, &id, NULL))
844 /* Get client entry */
845 if (id.type == SILC_ID_CLIENT) {
846 client = silc_idlist_find_client_by_id(server->global_list,
850 client = silc_idlist_find_client_by_id(server->local_list,
858 if (!silc_id_str2id(packet->dst_id, packet->dst_id_len,
859 packet->dst_id_type, &channel_id,
863 /* Get channel entry */
864 channel = silc_idlist_find_channel_by_id(server->global_list,
867 channel = silc_idlist_find_channel_by_id(server->local_list,
870 SILC_LOG_DEBUG(("Notify for unknown channel %s",
871 silc_id_render(&channel_id, SILC_ID_CHANNEL)));
877 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
881 SILC_GET32_MSB(mode, tmp);
883 /* Get target client */
884 if (!silc_argument_get_decoded(args, 3, SILC_ARGUMENT_ID, &id, NULL))
887 /* Get client entry */
888 client2 = silc_idlist_find_client_by_id(server->global_list,
889 SILC_ID_GET_ID(id), TRUE, NULL);
891 client2 = silc_idlist_find_client_by_id(server->local_list,
899 /* Check that sender is on channel */
900 if (!silc_server_client_on_channel(client, channel, &chl))
903 if (client != client2 && server->server_type == SILC_ROUTER) {
904 /* Sender must be operator */
905 if (!(chl->mode & SILC_CHANNEL_UMODE_CHANOP) &&
906 !(chl->mode & SILC_CHANNEL_UMODE_CHANFO)) {
907 SILC_LOG_DEBUG(("CUMODE change is not allowed"));
911 if (!silc_server_client_on_channel(client2, channel, &chl))
914 /* If target is founder mode change is not allowed. */
915 if (chl->mode & SILC_CHANNEL_UMODE_CHANFO) {
916 SILC_LOG_DEBUG(("CUMODE change is not allowed"));
922 /* Get target channel user entry */
923 if (!silc_server_client_on_channel(client2, channel, &chl))
926 if (server->server_type == SILC_SERVER && chl->mode == mode) {
927 SILC_LOG_DEBUG(("Mode is changed already"));
931 /* Check whether to give founder rights to this user or not. The
932 problem here is that we get only the public key of the client,
933 but no authentication data. We must assume that server has
934 already authenticated the user (and thus we must trust the
936 if (mode & SILC_CHANNEL_UMODE_CHANFO &&
937 !(chl->mode & SILC_CHANNEL_UMODE_CHANFO) &&
938 server->server_type == SILC_ROUTER &&
939 sock != SILC_PRIMARY_ROUTE(server)) {
940 SilcPublicKey founder_key = NULL;
942 /* If channel doesn't have founder auth mode then it's impossible
943 that someone would be getting founder rights with CUMODE command.
944 In that case there already either is founder or there isn't
945 founder at all on the channel (valid only when 'client' is
947 if (client && !(channel->mode & SILC_CHANNEL_MODE_FOUNDER_AUTH)) {
948 /* Force the mode to not have founder mode */
949 chl->mode = mode &= ~SILC_CHANNEL_UMODE_CHANFO;
950 silc_server_force_cumode_change(server, sock, channel, chl, mode);
955 /* Get the founder of the channel and if found then this client
956 cannot be the founder since there already is one. */
957 silc_hash_table_list(channel->user_list, &htl);
958 while (silc_hash_table_get(&htl, NULL, (void *)&chl2))
959 if (chl2->mode & SILC_CHANNEL_UMODE_CHANFO) {
960 SILC_LOG_DEBUG(("Founder already on channel"));
961 chl->mode = mode &= ~SILC_CHANNEL_UMODE_CHANFO;
962 silc_server_force_cumode_change(server, sock, channel,
967 silc_hash_table_list_reset(&htl);
968 if (!(mode & SILC_CHANNEL_UMODE_CHANFO))
971 /* Founder not found on the channel. Since the founder auth mode
972 is set on the channel now check whether this is the client that
973 originally set the mode. */
975 if (channel->founder_key) {
976 /* Get public key that must be present in notify */
977 tmp = silc_argument_get_arg_type(args, 4, &tmp_len);
978 if (!tmp || !silc_public_key_payload_decode(tmp, tmp_len,
980 chl->mode = mode &= ~SILC_CHANNEL_UMODE_CHANFO;
981 SILC_LOG_DEBUG(("Founder public key not present"));
982 silc_server_force_cumode_change(server, sock, channel, chl, mode);
987 /* Now match the public key we have cached and public key sent.
989 if (!silc_pkcs_public_key_compare(channel->founder_key,
991 chl->mode = mode &= ~SILC_CHANNEL_UMODE_CHANFO;
992 SILC_LOG_DEBUG(("Founder public key mismatch"));
993 silc_server_force_cumode_change(server, sock, channel, chl, mode);
999 /* There cannot be anyone else as founder on the channel now. This
1000 client is definitely the founder due to this 'authentication'.
1001 We trust the server did the actual signature verification
1002 earlier (bad, yes). */
1003 silc_hash_table_list(channel->user_list, &htl);
1004 while (silc_hash_table_get(&htl, NULL, (void *)&chl2))
1005 if (chl2->mode & SILC_CHANNEL_UMODE_CHANFO) {
1006 chl2->mode &= ~SILC_CHANNEL_UMODE_CHANFO;
1007 SILC_LOG_DEBUG(("Removing old founder rights, new authenticated"));
1008 silc_server_force_cumode_change(server, NULL, channel, chl2,
1012 silc_hash_table_list_reset(&htl);
1015 silc_pkcs_public_key_free(founder_key);
1018 if (server->server_type != SILC_SERVER && chl->mode == mode) {
1019 SILC_LOG_DEBUG(("Mode is changed already"));
1023 SILC_LOG_DEBUG(("Changing %s channel user mode",
1024 chl->client->nickname ? chl->client->nickname :
1025 (unsigned char *)""));
1027 /* Change the mode */
1030 /* Send the same notify to the channel */
1032 silc_server_packet_send_to_channel(server, sock, channel,
1034 FALSE, TRUE, buffer->data,
1035 silc_buffer_len(buffer));
1040 case SILC_NOTIFY_TYPE_INVITE:
1042 if (packet->dst_id_type == SILC_ID_CLIENT)
1045 SILC_LOG_DEBUG(("INVITE notify"));
1047 /* Get Channel ID */
1048 if (!silc_argument_get_decoded(args, 1, SILC_ARGUMENT_ID, &id, NULL))
1051 /* Get channel entry */
1052 channel = silc_idlist_find_channel_by_id(server->global_list,
1053 SILC_ID_GET_ID(id), NULL);
1055 channel = silc_idlist_find_channel_by_id(server->local_list,
1056 SILC_ID_GET_ID(id), NULL);
1058 SILC_LOG_DEBUG(("Notify for unknown channel %s",
1059 silc_id_render(SILC_ID_GET_ID(id), SILC_ID_CHANNEL)));
1064 /* Get the invite action */
1065 tmp = silc_argument_get_arg_type(args, 4, &tmp_len);
1066 if (tmp && tmp_len == 1) {
1067 SilcUInt8 action = (SilcUInt8)tmp[0];
1068 SilcUInt16 iargc = 0;
1069 SilcArgumentPayload iargs;
1071 /* Get invite list */
1072 tmp = silc_argument_get_arg_type(args, 5, &tmp_len);
1073 if (!tmp || tmp_len < 2)
1076 /* Parse the arguments to see they are constructed correctly */
1077 SILC_GET16_MSB(iargc, tmp);
1078 iargs = silc_argument_payload_parse(tmp + 2, tmp_len - 2, iargc);
1082 if (!channel->invite_list)
1083 channel->invite_list =
1084 silc_hash_table_alloc(0, silc_hash_ptr,
1086 silc_server_inviteban_destruct, channel, TRUE);
1088 /* Proces the invite action */
1089 if (!silc_server_inviteban_process(server, channel->invite_list, action,
1092 silc_argument_payload_free(iargs);
1094 /* If we are router we must send this notify to our local servers on
1095 the channel. Normal server does nothing. The notify is not
1097 if (server->server_type == SILC_ROUTER)
1098 silc_server_packet_send_to_channel(server, sock, channel,
1099 packet->type, FALSE, FALSE,
1101 silc_buffer_len(buffer));
1106 case SILC_NOTIFY_TYPE_CHANNEL_CHANGE:
1108 * Distribute to the local clients on the channel and change the
1112 SILC_LOG_DEBUG(("CHANNEL CHANGE"));
1114 if (idata->conn_type != SILC_CONN_ROUTER)
1117 /* Get the old Channel ID */
1118 if (!silc_argument_get_decoded(args, 1, SILC_ARGUMENT_ID, &id, NULL))
1121 /* Get the channel entry */
1122 channel = silc_idlist_find_channel_by_id(server->local_list,
1123 SILC_ID_GET_ID(id), NULL);
1125 channel = silc_idlist_find_channel_by_id(server->global_list,
1126 SILC_ID_GET_ID(id), NULL);
1128 SILC_LOG_DEBUG(("Notify for unknown channel %s",
1129 silc_id_render(SILC_ID_GET_ID(id), SILC_ID_CHANNEL)));
1134 /* Send the notify to the channel */
1135 silc_server_packet_send_to_channel(server, sock, channel, packet->type,
1136 FALSE, TRUE, buffer->data,
1137 silc_buffer_len(buffer));
1139 /* Get the new Channel ID */
1140 if (!silc_argument_get_decoded(args, 2, SILC_ARGUMENT_ID, &id2, NULL))
1143 SILC_LOG_DEBUG(("Old Channel ID id(%s)",
1144 silc_id_render(SILC_ID_GET_ID(id), SILC_ID_CHANNEL)));
1145 SILC_LOG_DEBUG(("New Channel ID id(%s)",
1146 silc_id_render(SILC_ID_GET_ID(id2), SILC_ID_CHANNEL)));
1148 /* Replace the Channel ID */
1150 if (!silc_idlist_replace_channel_id(server->local_list,
1152 SILC_ID_GET_ID(id2)))
1153 if (!silc_idlist_replace_channel_id(server->global_list,
1155 SILC_ID_GET_ID(id2)))
1159 SilcBuffer modes = NULL, users = NULL, users_modes = NULL;
1161 /* Re-announce this channel which ID was changed. */
1162 silc_server_send_new_channel(server, sock, FALSE, channel->channel_name,
1164 silc_id_get_len(channel->id,
1168 /* Re-announce our clients on the channel as the ID has changed now */
1169 silc_server_announce_get_channel_users(server, channel, &modes, &users,
1172 silc_buffer_push(users, users->data - users->head);
1173 silc_server_packet_send(server, sock,
1174 SILC_PACKET_NOTIFY, SILC_PACKET_FLAG_LIST,
1175 users->data, silc_buffer_len(users));
1176 silc_buffer_free(users);
1179 silc_buffer_push(modes, modes->data - modes->head);
1180 silc_server_packet_send_dest(server, sock,
1181 SILC_PACKET_NOTIFY, SILC_PACKET_FLAG_LIST,
1182 channel->id, SILC_ID_CHANNEL,
1183 modes->data, silc_buffer_len(modes));
1184 silc_buffer_free(modes);
1187 silc_buffer_push(users_modes, users_modes->data - users_modes->head);
1188 silc_server_packet_send_dest(server, sock,
1189 SILC_PACKET_NOTIFY, SILC_PACKET_FLAG_LIST,
1190 channel->id, SILC_ID_CHANNEL,
1192 silc_buffer_len(users_modes));
1193 silc_buffer_free(users_modes);
1196 /* Re-announce channel's topic */
1197 if (channel->topic) {
1198 silc_server_send_notify_topic_set(server, sock,
1199 server->server_type == SILC_ROUTER ?
1200 TRUE : FALSE, channel,
1201 server->id, SILC_ID_SERVER,
1208 case SILC_NOTIFY_TYPE_SERVER_SIGNOFF:
1210 * Remove the server entry and all clients that this server owns.
1213 SILC_LOG_DEBUG(("SERVER SIGNOFF notify"));
1215 /* Backup router shouldn't accept SERVER_SIGNOFF's from normal routers
1216 when the backup isn't acting as primary router. */
1217 if (idata->conn_type == SILC_CONN_SERVER &&
1218 server->backup_router && server->server_type == SILC_BACKUP_ROUTER)
1222 if (!silc_argument_get_decoded(args, 1, SILC_ARGUMENT_ID, &id, NULL))
1225 /* If the ID is mine, this notify is not allowed. */
1226 if (SILC_ID_SERVER_COMPARE(SILC_ID_GET_ID(id), server->id)) {
1227 SILC_LOG_DEBUG(("Ignoring my own ID for SERVER_SIGNOFF"));
1231 /* Get server entry */
1232 server_entry = silc_idlist_find_server_by_id(server->global_list,
1236 if (!server_entry) {
1237 server_entry = silc_idlist_find_server_by_id(server->local_list,
1241 if (!server_entry) {
1242 /* If we are normal server then we might not have the server. Check
1243 whether router was kind enough to send the list of all clients
1244 that actually was to be removed. Remove them if the list is
1246 if (server->server_type != SILC_ROUTER &&
1247 silc_argument_get_arg_num(args) > 1) {
1250 for (i = 1; i < silc_argument_get_arg_num(args); i++) {
1252 if (!silc_argument_get_decoded(args, i + 1, SILC_ARGUMENT_ID,
1256 /* Get client entry */
1257 client = silc_idlist_find_client_by_id(server->global_list,
1258 SILC_ID_GET_ID(id2),
1262 client = silc_idlist_find_client_by_id(server->local_list,
1263 SILC_ID_GET_ID(id2),
1270 /* Update statistics */
1271 SILC_LOG_DEBUG(("stat.clients %d->%d", server->stat.clients,
1272 server->stat.clients - 1));
1273 SILC_VERIFY(server->stat.clients > 0);
1274 server->stat.clients--;
1275 if (server->stat.cell_clients)
1276 server->stat.cell_clients--;
1277 SILC_OPER_STATS_UPDATE(client, server, SILC_UMODE_SERVER_OPERATOR);
1278 SILC_OPER_STATS_UPDATE(client, router, SILC_UMODE_ROUTER_OPERATOR);
1280 /* Remove the client from all channels. */
1281 silc_server_remove_from_channels(server, NULL, client,
1282 TRUE, NULL, FALSE, FALSE);
1284 /* Check if anyone is watching this nickname */
1285 if (server->server_type == SILC_ROUTER)
1286 silc_server_check_watcher_list(server, client, NULL,
1287 SILC_NOTIFY_TYPE_SERVER_SIGNOFF);
1289 /* Remove this client from watcher list if it is */
1291 silc_server_del_from_watcher_list(server, client);
1293 /* Remove the client */
1294 silc_dlist_del(server->expired_clients, client);
1295 silc_idlist_del_data(client);
1296 silc_idlist_del_client(local ? server->local_list :
1297 server->global_list, client);
1305 /* For local entrys SERVER_SIGNOFF is processed only on backup router.
1306 It is possible that router sends server signoff for a server. If
1307 backup router has it as local connection it will be closed. */
1308 if (SILC_IS_LOCAL(server_entry)) {
1309 if (server->server_type == SILC_BACKUP_ROUTER) {
1310 sock = server_entry->connection;
1311 SILC_LOG_DEBUG(("Closing connection after SERVER_SIGNOFF"));
1312 silc_server_free_sock_user_data(server, sock, NULL);
1313 silc_server_close_connection(server, sock);
1319 /* Remove all servers that are originated from this server, and
1320 remove the clients of those servers too. */
1321 silc_server_remove_servers_by_server(server, server_entry, TRUE);
1323 /* Remove the clients that this server owns as they will become
1325 silc_server_remove_clients_by_server(server, server_entry->router,
1326 server_entry, TRUE);
1327 silc_server_backup_del(server, server_entry);
1329 /* Remove the server entry */
1330 silc_idlist_del_server(local ? server->local_list :
1331 server->global_list, server_entry);
1333 /* Update statistics */
1334 if (server->server_type == SILC_ROUTER)
1335 server->stat.servers--;
1339 case SILC_NOTIFY_TYPE_KICKED:
1341 * Distribute the notify to local clients on the channel
1344 SILC_LOG_DEBUG(("KICKED notify"));
1346 if (!silc_id_str2id(packet->dst_id, packet->dst_id_len,
1347 packet->dst_id_type, &channel_id,
1348 sizeof(channel_id)))
1351 /* Get channel entry */
1352 channel = silc_idlist_find_channel_by_id(server->global_list,
1355 channel = silc_idlist_find_channel_by_id(server->local_list,
1358 SILC_LOG_DEBUG(("Notify for unknown channel %s",
1359 silc_id_render(SILC_ID_GET_ID(id), SILC_ID_CHANNEL)));
1365 if (!silc_argument_get_decoded(args, 1, SILC_ARGUMENT_ID, &id, NULL))
1368 /* If the the client is not in local list we check global list */
1369 client = silc_idlist_find_client_by_id(server->global_list,
1370 SILC_ID_GET_ID(id), TRUE, NULL);
1372 client = silc_idlist_find_client_by_id(server->local_list,
1373 SILC_ID_GET_ID(id), TRUE, NULL);
1378 /* If target is founder they cannot be kicked */
1379 if (!silc_server_client_on_channel(client, channel, &chl))
1381 if (chl->mode & SILC_CHANNEL_UMODE_CHANFO)
1384 /* Get the kicker's Client ID */
1385 if (!silc_argument_get_decoded(args, 3, SILC_ARGUMENT_ID, &id, NULL))
1388 /* If the the client is not in local list we check global list */
1389 client2 = silc_idlist_find_client_by_id(server->global_list,
1390 SILC_ID_GET_ID(id), TRUE, NULL);
1392 client2 = silc_idlist_find_client_by_id(server->local_list,
1393 SILC_ID_GET_ID(id), TRUE, NULL);
1398 /* Kicker must be operator on channel */
1399 if (!silc_server_client_on_channel(client2, channel, &chl))
1401 if (!(chl->mode & SILC_CHANNEL_UMODE_CHANOP) &&
1402 !(chl->mode & SILC_CHANNEL_UMODE_CHANFO)) {
1403 SILC_LOG_DEBUG(("Kicking is not allowed"));
1407 /* Send to channel */
1408 silc_server_packet_send_to_channel(server, sock, channel, packet->type,
1409 FALSE, TRUE, buffer->data,
1410 silc_buffer_len(buffer));
1412 /* Remove the client from channel's invite list */
1413 if (channel->invite_list && silc_hash_table_count(channel->invite_list)) {
1415 SilcArgumentPayload iargs;
1416 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
1417 ab = silc_argument_payload_encode_one(NULL, tmp, tmp_len, 3);
1418 iargs = silc_argument_payload_parse(ab->data, silc_buffer_len(ab), 1);
1419 silc_server_inviteban_process(server, channel->invite_list, 1, iargs);
1420 silc_buffer_free(ab);
1421 silc_argument_payload_free(iargs);
1424 /* Remove the client from channel */
1425 silc_server_remove_from_one_channel(server, sock, channel, client, FALSE);
1429 case SILC_NOTIFY_TYPE_KILLED:
1432 * Distribute the notify to local clients on channels
1434 unsigned char *comment;
1435 SilcUInt32 comment_len;
1437 SILC_LOG_DEBUG(("KILLED notify"));
1440 if (!silc_argument_get_decoded(args, 1, SILC_ARGUMENT_ID, &id, NULL))
1443 /* If the the client is not in local list we check global list */
1444 client = silc_idlist_find_client_by_id(server->global_list,
1445 SILC_ID_GET_ID(id), TRUE, &cache);
1447 client = silc_idlist_find_client_by_id(server->local_list,
1454 /* If the client is one of ours, then close the connection to the
1455 client now. This removes the client from all channels as well. */
1456 if (packet->dst_id_type == SILC_ID_CLIENT && client->connection) {
1457 sock = client->connection;
1458 silc_server_free_client_data(server, NULL, client, FALSE, NULL);
1459 silc_server_close_connection(server, sock);
1464 comment = silc_argument_get_arg_type(args, 2, &comment_len);
1465 if (comment_len > 128)
1468 /* Get the killer's Client ID */
1469 if (!silc_argument_get_decoded(args, 3, SILC_ARGUMENT_ID, &id, NULL))
1472 if (id.type == SILC_ID_CLIENT) {
1473 /* If the the client is not in local list we check global list */
1474 client2 = silc_idlist_find_client_by_id(server->global_list,
1478 client2 = silc_idlist_find_client_by_id(server->local_list,
1485 /* Killer must be router operator */
1486 if (server->server_type != SILC_SERVER &&
1487 !(client2->mode & SILC_UMODE_ROUTER_OPERATOR)) {
1488 SILC_LOG_DEBUG(("Killing is not allowed"));
1493 /* Send the notify to local clients on the channels except to the
1494 client who is killed. */
1495 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
1496 tmp2 = silc_argument_get_arg_type(args, 3, &tmp2_len);
1497 silc_server_send_notify_on_channels(server, client, client,
1498 SILC_NOTIFY_TYPE_KILLED, 3,
1499 tmp, tmp_len, comment, comment_len,
1502 /* Remove the client from all channels */
1503 silc_server_remove_from_channels(server, NULL, client, FALSE, NULL,
1506 /* Check if anyone is watching this nickname */
1507 if (server->server_type == SILC_ROUTER)
1508 silc_server_check_watcher_list(server, client, NULL,
1509 SILC_NOTIFY_TYPE_KILLED);
1511 /* Remove client's public key from repository, this will free it too. */
1512 if (client->data.public_key) {
1513 silc_skr_del_public_key(server->repository, client->data.public_key,
1515 client->data.public_key = NULL;
1518 /* Update statistics */
1519 SILC_LOG_DEBUG(("stat.clients %d->%d", server->stat.clients,
1520 server->stat.clients - 1));
1521 SILC_VERIFY(server->stat.clients > 0);
1522 server->stat.clients--;
1523 if (server->stat.cell_clients)
1524 server->stat.cell_clients--;
1525 SILC_OPER_STATS_UPDATE(client, server, SILC_UMODE_SERVER_OPERATOR);
1526 SILC_OPER_STATS_UPDATE(client, router, SILC_UMODE_ROUTER_OPERATOR);
1528 if (SILC_IS_LOCAL(client)) {
1529 if (!client->local_detached)
1530 server->stat.my_clients--;
1531 silc_schedule_task_del_by_context(server->schedule, client);
1532 silc_idlist_del_data(client);
1536 client->data.status &= ~SILC_IDLIST_STATUS_REGISTERED;
1538 client->router = NULL;
1539 client->connection = NULL;
1540 client->data.created = silc_time();
1541 silc_dlist_del(server->expired_clients, client);
1542 silc_dlist_add(server->expired_clients, client);
1546 case SILC_NOTIFY_TYPE_UMODE_CHANGE:
1548 * Save the mode of the client.
1551 SILC_LOG_DEBUG(("UMODE_CHANGE notify"));
1554 if (!silc_argument_get_decoded(args, 1, SILC_ARGUMENT_ID, &id, NULL))
1557 /* Get client entry */
1558 client = silc_idlist_find_client_by_id(server->global_list,
1559 SILC_ID_GET_ID(id), TRUE, NULL);
1561 client = silc_idlist_find_client_by_id(server->local_list,
1562 SILC_ID_GET_ID(id), TRUE, NULL);
1568 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
1571 SILC_GET32_MSB(mode, tmp);
1573 /* Remove internal resumed flag if client is marked detached now */
1574 if (mode & SILC_UMODE_DETACHED)
1575 client->data.status &= ~SILC_IDLIST_STATUS_RESUMED;
1577 /* Update statistics */
1578 if (server->server_type == SILC_ROUTER) {
1579 if (mode & SILC_UMODE_GONE) {
1580 if (!(client->mode & SILC_UMODE_GONE))
1581 server->stat.aways++;
1583 if (client->mode & SILC_UMODE_GONE)
1584 server->stat.aways--;
1586 if (mode & SILC_UMODE_DETACHED) {
1587 if (!(client->mode & SILC_UMODE_DETACHED))
1588 server->stat.detached++;
1590 if (client->mode & SILC_UMODE_DETACHED)
1591 server->stat.detached--;
1594 SILC_UMODE_STATS_UPDATE(server, SILC_UMODE_SERVER_OPERATOR);
1595 SILC_UMODE_STATS_UPDATE(router, SILC_UMODE_ROUTER_OPERATOR);
1597 /* Change the mode */
1598 client->mode = mode;
1600 /* Check if anyone is watching this nickname */
1601 if (server->server_type == SILC_ROUTER)
1602 silc_server_check_watcher_list(server, client, NULL,
1603 SILC_NOTIFY_TYPE_UMODE_CHANGE);
1607 case SILC_NOTIFY_TYPE_BAN:
1612 SILC_LOG_DEBUG(("BAN notify"));
1614 /* Get Channel ID */
1615 if (!silc_argument_get_decoded(args, 1, SILC_ARGUMENT_ID, &id, NULL))
1618 /* Get channel entry */
1619 channel = silc_idlist_find_channel_by_id(server->global_list,
1620 SILC_ID_GET_ID(id), NULL);
1622 channel = silc_idlist_find_channel_by_id(server->local_list,
1623 SILC_ID_GET_ID(id), NULL);
1625 SILC_LOG_DEBUG(("Notify for unknown channel %s",
1626 silc_id_render(SILC_ID_GET_ID(id), SILC_ID_CHANNEL)));
1631 /* Get the ban action */
1632 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
1633 if (tmp && tmp_len == 1) {
1634 SilcUInt8 action = (SilcUInt8)tmp[0];
1635 SilcUInt16 iargc = 0;
1636 SilcArgumentPayload iargs;
1639 tmp = silc_argument_get_arg_type(args, 3, &tmp_len);
1640 if (!tmp || tmp_len < 2)
1643 /* Parse the arguments to see they are constructed correctly */
1644 SILC_GET16_MSB(iargc, tmp);
1645 iargs = silc_argument_payload_parse(tmp + 2, tmp_len - 2, iargc);
1649 if (!channel->ban_list)
1651 silc_hash_table_alloc(0, silc_hash_ptr,
1653 silc_server_inviteban_destruct, channel, TRUE);
1655 /* Proces the ban action */
1656 if (!silc_server_inviteban_process(server, channel->ban_list, action,
1659 silc_argument_payload_free(iargs);
1661 /* If we are router we must send this notify to our local servers on
1662 the channel. Normal server does nothing. The notify is not
1664 if (server->server_type == SILC_ROUTER)
1665 silc_server_packet_send_to_channel(server, sock, channel,
1666 packet->type, FALSE, FALSE,
1668 silc_buffer_len(buffer));
1672 case SILC_NOTIFY_TYPE_ERROR:
1679 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
1680 if (!tmp && tmp_len != 1)
1682 error = (SilcStatus)tmp[0];
1684 SILC_LOG_DEBUG(("ERROR notify (%d)", error));
1686 if (error == SILC_STATUS_ERR_NO_SUCH_CLIENT_ID &&
1687 idata->conn_type == SILC_CONN_ROUTER) {
1688 if (!silc_argument_get_decoded(args, 2, SILC_ARGUMENT_ID, &id, NULL))
1691 SILC_LOG_DEBUG(("Received invalid client ID notification, deleting "
1692 "the entry from cache"));
1694 client = silc_idlist_find_client_by_id(server->global_list,
1700 silc_server_remove_from_channels(server, NULL, client, TRUE,
1702 silc_dlist_del(server->expired_clients, client);
1703 silc_idlist_del_data(client);
1704 silc_idlist_del_client(server->global_list, client);
1709 /* Ignore rest of the notify types for now */
1710 case SILC_NOTIFY_TYPE_NONE:
1711 case SILC_NOTIFY_TYPE_MOTD:
1715 SILC_LOG_DEBUG(("Unsupported notify %d", type));
1720 silc_notify_payload_free(payload);
1723 void silc_server_notify(SilcServer server,
1724 SilcPacketStream sock,
1727 silc_server_notify_process(server, sock, packet, &packet->buffer);
1728 silc_packet_free(packet);
1731 void silc_server_notify_list(SilcServer server,
1732 SilcPacketStream sock,
1735 SilcIDListData idata = silc_packet_get_context(sock);
1739 SILC_LOG_DEBUG(("Processing Notify List"));
1741 if (idata->conn_type == SILC_CONN_CLIENT ||
1742 packet->src_id_type != SILC_ID_SERVER)
1745 buffer = silc_buffer_alloc(1024);
1749 while (silc_buffer_len(&packet->buffer)) {
1750 SILC_GET16_MSB(len, packet->buffer.data + 2);
1751 if (len > silc_buffer_len(&packet->buffer))
1754 if (len > silc_buffer_truelen(buffer)) {
1755 silc_buffer_free(buffer);
1756 buffer = silc_buffer_alloc(1024 + len);
1759 silc_buffer_pull_tail(buffer, len);
1760 silc_buffer_put(buffer, packet->buffer.data, len);
1762 /* Process the Notify */
1763 silc_server_notify_process(server, sock, packet, buffer);
1765 silc_buffer_push_tail(buffer, len);
1766 silc_buffer_pull(&packet->buffer, len);
1769 silc_packet_free(packet);
1770 silc_buffer_free(buffer);
1773 /* Received private message. This resolves the destination of the message
1774 and sends the packet. This is used by both server and router. If the
1775 destination is our locally connected client this sends the packet to
1776 the client. This may also send the message for further routing if
1777 the destination is not in our server (or router). */
1779 void silc_server_private_message(SilcServer server,
1780 SilcPacketStream sock,
1783 SilcPacketStream dst_sock;
1784 SilcIDListData idata;
1785 SilcClientEntry client;
1786 SilcClientID client_id;
1788 SILC_LOG_DEBUG(("Start"));
1790 if (packet->src_id_type != SILC_ID_CLIENT ||
1791 packet->dst_id_type != SILC_ID_CLIENT || !packet->dst_id)
1794 /* Get the route to the client */
1795 dst_sock = silc_server_get_client_route(server, packet->dst_id,
1796 packet->dst_id_len, NULL,
1800 unsigned char error;
1802 if (client && client->mode & SILC_UMODE_DETACHED) {
1803 SILC_LOG_DEBUG(("Client is detached, discarding packet"));
1807 /* Send SILC_NOTIFY_TYPE_ERROR to indicate that such destination ID
1808 does not exist or is invalid. */
1809 idp = silc_id_payload_encode_data(packet->dst_id,
1811 packet->dst_id_type);
1815 error = SILC_STATUS_ERR_NO_SUCH_CLIENT_ID;
1816 if (packet->src_id_type == SILC_ID_CLIENT) {
1817 silc_id_str2id(packet->src_id, packet->src_id_len,
1818 packet->src_id_type, &client_id, sizeof(client_id));
1819 silc_server_send_notify_dest(server, sock, FALSE,
1820 &client_id, SILC_ID_CLIENT,
1821 SILC_NOTIFY_TYPE_ERROR, 2,
1823 idp->data, silc_buffer_len(idp));
1825 silc_server_send_notify(server, sock, FALSE,
1826 SILC_NOTIFY_TYPE_ERROR, 2,
1828 idp->data, silc_buffer_len(idp));
1831 silc_buffer_free(idp);
1835 /* Check whether destination client wishes to receive private messages */
1836 if (client && !(packet->flags & SILC_PACKET_FLAG_PRIVMSG_KEY) &&
1837 client->mode & SILC_UMODE_BLOCK_PRIVMSG) {
1838 SILC_LOG_DEBUG(("Client blocks private messages, discarding packet"));
1842 /* Send the private message */
1843 silc_server_packet_route(server, dst_sock, packet);
1846 silc_packet_free(packet);
1849 /* Received private message key packet.. This packet is never for us. It is to
1850 the client in the packet's destination ID. Sending of this sort of packet
1851 equals sending private message, ie. it is sent point to point from
1852 one client to another. */
1854 void silc_server_private_message_key(SilcServer server,
1855 SilcPacketStream sock,
1858 SilcPacketStream dst_sock;
1859 SilcIDListData idata;
1861 SILC_LOG_DEBUG(("Start"));
1863 if (packet->src_id_type != SILC_ID_CLIENT ||
1864 packet->dst_id_type != SILC_ID_CLIENT || !packet->dst_id) {
1865 silc_packet_free(packet);
1869 /* Get the route to the client */
1870 dst_sock = silc_server_get_client_route(server, packet->dst_id,
1871 packet->dst_id_len, NULL,
1874 silc_packet_free(packet);
1878 /* Relay the packet */
1879 silc_server_packet_route(server, dst_sock, packet);
1881 silc_packet_free(packet);
1884 /* Processes incoming command reply packet. The command reply packet may
1885 be destined to one of our clients or it may directly for us. We will
1886 call the command reply routine after processing the packet. */
1888 void silc_server_command_reply(SilcServer server,
1889 SilcPacketStream sock,
1892 SilcBuffer buffer = &packet->buffer;
1893 SilcClientEntry client = NULL;
1896 SILC_LOG_DEBUG(("Start"));
1898 if (packet->dst_id_type == SILC_ID_CHANNEL) {
1899 silc_packet_free(packet);
1903 if (packet->dst_id_type == SILC_ID_CLIENT) {
1904 /* Destination must be one of ours */
1905 if (!silc_id_str2id(packet->dst_id, packet->dst_id_len, SILC_ID_CLIENT,
1907 silc_packet_free(packet);
1910 client = silc_idlist_find_client_by_id(server->local_list, &id,
1913 SILC_LOG_ERROR(("Cannot process command reply to unknown client"));
1914 silc_packet_free(packet);
1919 if (packet->dst_id_type == SILC_ID_SERVER) {
1920 /* For now this must be for us */
1921 if (memcmp(packet->dst_id, server->id_string, server->id_string_len)) {
1922 SILC_LOG_ERROR(("Cannot process command reply to unknown server"));
1923 silc_packet_free(packet);
1928 /* Execute command reply locally for the command */
1929 silc_server_command_reply_process(server, sock, buffer);
1931 /* Relay the packet to the client */
1932 if (packet->dst_id_type == SILC_ID_CLIENT && client)
1933 silc_server_packet_route(server, client->connection, packet);
1935 silc_packet_free(packet);
1938 /* Process received channel message. The message can be originated from
1939 client or server. */
1941 void silc_server_channel_message(SilcServer server,
1942 SilcPacketStream sock,
1945 SilcChannelEntry channel = NULL;
1949 SilcClientEntry sender_entry = NULL;
1950 SilcIDListData idata;
1951 SilcChannelClientEntry chl;
1952 SilcBool local = TRUE;
1954 SILC_LOG_DEBUG(("Processing channel message"));
1957 if (packet->dst_id_type != SILC_ID_CHANNEL) {
1958 SILC_LOG_DEBUG(("Received bad message for channel, dropped"));
1962 /* Find channel entry */
1963 if (!silc_id_str2id(packet->dst_id, packet->dst_id_len, SILC_ID_CHANNEL,
1966 channel = silc_idlist_find_channel_by_id(server->local_list, &id, NULL);
1968 channel = silc_idlist_find_channel_by_id(server->global_list, &id, NULL);
1971 unsigned char error;
1973 /* Send SILC_NOTIFY_TYPE_ERROR to indicate that such destination ID
1974 does not exist or is invalid. */
1975 idp = silc_id_payload_encode_data(packet->dst_id,
1977 packet->dst_id_type);
1981 error = SILC_STATUS_ERR_NO_SUCH_CHANNEL_ID;
1982 if (packet->src_id_type == SILC_ID_CLIENT) {
1983 silc_id_str2id(packet->src_id, packet->src_id_len,
1984 packet->src_id_type, &cid, sizeof(cid));
1985 silc_server_send_notify_dest(server, sock, FALSE,
1986 &cid, SILC_ID_CLIENT,
1987 SILC_NOTIFY_TYPE_ERROR, 2,
1988 &error, 1, idp->data,
1989 silc_buffer_len(idp));
1991 silc_server_send_notify(server, sock, FALSE,
1992 SILC_NOTIFY_TYPE_ERROR, 2,
1993 &error, 1, idp->data, silc_buffer_len(idp));
1996 silc_buffer_free(idp);
2001 /* See that this client is on the channel. If the original sender is
2002 not client (as it can be server as well) we don't do the check. */
2003 if (!silc_id_str2id2(packet->src_id, packet->src_id_len,
2004 packet->src_id_type, &sid))
2006 if (sid.type == SILC_ID_CLIENT) {
2007 sender_entry = silc_idlist_find_client_by_id(server->local_list,
2008 SILC_ID_GET_ID(sid),
2010 if (!sender_entry) {
2012 sender_entry = silc_idlist_find_client_by_id(server->global_list,
2013 SILC_ID_GET_ID(sid),
2016 if (!sender_entry || !silc_server_client_on_channel(sender_entry,
2018 SILC_LOG_DEBUG(("Client not on channel"));
2022 /* If channel is moderated check that client is allowed to send
2024 if (channel->mode & SILC_CHANNEL_MODE_SILENCE_USERS &&
2025 !(chl->mode & SILC_CHANNEL_UMODE_CHANOP) &&
2026 !(chl->mode & SILC_CHANNEL_UMODE_CHANFO)) {
2027 SILC_LOG_DEBUG(("Channel is silenced from normal users"));
2030 if (channel->mode & SILC_CHANNEL_MODE_SILENCE_OPERS &&
2031 chl->mode & SILC_CHANNEL_UMODE_CHANOP &&
2032 !(chl->mode & SILC_CHANNEL_UMODE_CHANFO)) {
2033 SILC_LOG_DEBUG(("Channel is silenced from operators"));
2036 if (chl->mode & SILC_CHANNEL_UMODE_QUIET) {
2037 SILC_LOG_DEBUG(("Sender is quieted on the channel"));
2041 /* If the packet is coming from router, but the client entry is local
2042 entry to us then some router is rerouting this to us and it is not
2043 allowed. When the client is local to us it means that we've routed
2044 this packet to network, and now someone is routing it back to us. */
2045 idata = silc_packet_get_context(sock);
2046 if (server->server_type == SILC_ROUTER &&
2047 idata->conn_type == SILC_CONN_ROUTER && local) {
2048 SILC_LOG_DEBUG(("Channel message rerouted to the sender, drop it"));
2053 /* Distribute the packet to our local clients. This will send the
2054 packet for further routing as well, if needed. */
2055 silc_server_packet_relay_to_channel(server, sock, channel,
2056 SILC_ID_GET_ID(sid), sid.type,
2057 sender_entry, packet->buffer.data,
2058 silc_buffer_len(&packet->buffer));
2061 silc_packet_free(packet);
2064 /* Received channel key packet. We distribute the key to all of our locally
2065 connected clients on the channel. */
2067 void silc_server_channel_key(SilcServer server,
2068 SilcPacketStream sock,
2071 SilcBuffer buffer = &packet->buffer;
2072 SilcIDListData idata = silc_packet_get_context(sock);
2073 SilcChannelEntry channel;
2075 if (packet->src_id_type != SILC_ID_SERVER ||
2076 (server->server_type == SILC_ROUTER && !server->backup_router &&
2077 idata->conn_type == SILC_CONN_ROUTER)) {
2078 silc_packet_free(packet);
2082 /* Save the channel key */
2083 channel = silc_server_save_channel_key(server, buffer, NULL);
2085 SILC_LOG_ERROR(("Bad channel key from %s", idata->sconn->remote_host));
2086 silc_packet_free(packet);
2090 /* Distribute the key to everybody who is on the channel. If we are router
2091 we will also send it to locally connected servers. */
2092 silc_server_send_channel_key(server, sock, channel, FALSE);
2094 if (server->server_type != SILC_BACKUP_ROUTER)
2095 /* Distribute to local cell backup routers. */
2096 silc_server_backup_send(server, (SilcServerEntry)idata,
2097 SILC_PACKET_CHANNEL_KEY, 0,
2098 buffer->data, silc_buffer_len(buffer),
2101 silc_packet_free(packet);
2104 /* Received New Client packet and processes it. Creates Client ID for the
2105 client. Client becomes registered after calling this functions. */
2107 SilcClientEntry silc_server_new_client(SilcServer server,
2108 SilcPacketStream sock,
2111 SilcBuffer buffer = &packet->buffer;
2112 SilcIDListData idata = silc_packet_get_context(sock);
2113 SilcClientEntry client;
2114 SilcClientID *client_id;
2115 char *username = NULL, *realname = NULL;
2116 SilcUInt16 username_len, nickname_len;
2117 SilcUInt32 id_len, tmp_len;
2119 char *host, *nickname = NULL, *nicknamec;
2120 const char *hostname, *ip;
2122 SILC_LOG_DEBUG(("Creating new client"));
2124 if (idata->conn_type != SILC_CONN_CLIENT) {
2125 silc_packet_free(packet);
2129 /* Take client entry */
2130 client = (SilcClientEntry)idata;
2131 silc_socket_stream_get_info(silc_packet_stream_get_stream(sock),
2132 NULL, &hostname, &ip, NULL);
2134 SILC_LOG_DEBUG(("%s %s", ip, hostname));
2136 /* Make sure this client hasn't registered already */
2137 if (idata->status & SILC_IDLIST_STATUS_REGISTERED) {
2138 silc_packet_free(packet);
2142 /* Parse incoming packet */
2143 ret = silc_buffer_unformat(buffer,
2145 SILC_STR_UI16_NSTRING_ALLOC(&username,
2147 SILC_STR_UI16_STRING_ALLOC(&realname),
2150 silc_free(username);
2151 silc_free(realname);
2152 SILC_LOG_ERROR(("Client %s (%s) sent incomplete information, closing "
2153 "connection", hostname, ip));
2154 silc_server_disconnect_remote(server, sock,
2155 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
2157 silc_server_free_sock_user_data(server, sock, NULL);
2158 silc_packet_free(packet);
2163 silc_free(username);
2164 silc_free(realname);
2165 SILC_LOG_ERROR(("Client %s (%s) did not send its username, closing "
2166 "connection", hostname, ip));
2167 silc_server_disconnect_remote(server, sock,
2168 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
2170 silc_server_free_sock_user_data(server, sock, NULL);
2171 silc_packet_free(packet);
2175 if (username_len > 128) {
2177 username[username_len - 1] = '\0';
2180 /* Take nickname from NEW_CLIENT packet, if present */
2181 if (silc_buffer_unformat(buffer,
2182 SILC_STR_UI16_NSTRING_ALLOC(&nickname,
2184 SILC_STR_END) >= 0) {
2185 if (nickname_len > 128) {
2187 nickname[nickname_len - 1] = '\0';
2191 /* Nickname is initially same as username, if not present in NEW_CLIENT */
2193 nickname = strdup(username);
2194 nickname_len = strlen(nickname);
2197 /* Check for valid username string */
2198 nicknamec = silc_identifier_check(nickname, nickname_len,
2199 SILC_STRING_UTF8, 128, &tmp_len);
2201 silc_free(username);
2202 silc_free(realname);
2203 silc_free(nickname);
2204 SILC_LOG_ERROR(("Client %s (%s) sent bad username string '%s', closing "
2205 "connection", hostname, ip, username));
2206 silc_server_disconnect_remote(server, sock,
2207 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
2209 silc_server_free_sock_user_data(server, sock, NULL);
2210 silc_packet_free(packet);
2214 /* Make sanity checks for the hostname of the client. If the hostname
2215 is provided in the `username' check that it is the same than the
2216 resolved hostname, or if not resolved the hostname that appears in
2217 the client's public key. If the hostname is not present then put
2218 it from the resolved name or from the public key. */
2219 if (strchr(username, '@')) {
2220 SilcSILCPublicKey silc_pubkey;
2221 int tlen = strcspn(username, "@");
2222 char *phostname = NULL;
2224 host = silc_memdup(username + tlen + 1, strlen(username) - tlen - 1);
2226 if (strcmp(hostname, ip) && strcmp(hostname, host)) {
2227 silc_free(nickname);
2228 silc_free(username);
2230 silc_free(realname);
2231 SILC_LOG_ERROR(("Client %s (%s) sent incomplete information, closing "
2232 "connection", hostname, ip));
2233 silc_server_disconnect_remote(server, sock,
2234 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
2236 silc_server_free_sock_user_data(server, sock, NULL);
2237 silc_packet_free(packet);
2241 silc_pubkey = silc_pkcs_get_context(SILC_PKCS_SILC,
2242 client->data.public_key);
2243 phostname = strdup(silc_pubkey->identifier.host);
2244 if (!strcmp(hostname, ip) && phostname && strcmp(phostname, host)) {
2245 silc_free(nickname);
2246 silc_free(username);
2248 silc_free(phostname);
2249 silc_free(realname);
2250 SILC_LOG_ERROR(("Client %s (%s) sent incomplete information, closing "
2251 "connection", hostname, ip));
2252 silc_server_disconnect_remote(server, sock,
2253 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
2255 silc_server_free_sock_user_data(server, sock, NULL);
2256 silc_packet_free(packet);
2260 silc_free(phostname);
2262 /* The hostname is not present, add it. */
2264 newusername = silc_calloc(strlen(username) + strlen(hostname) + 2,
2265 sizeof(*newusername));
2266 strncat(newusername, username, strlen(username));
2267 strncat(newusername, "@", 1);
2268 strncat(newusername, hostname, strlen(hostname));
2269 silc_free(username);
2270 username = newusername;
2273 SILC_LOG_DEBUG(("%s %s", ip, hostname));
2275 /* Create Client ID */
2276 if (!silc_id_create_client_id(server, server->id, server->rng,
2277 server->md5hash, nicknamec,
2278 strlen(nicknamec), &client_id)) {
2279 silc_server_disconnect_remote(server, sock,
2280 SILC_STATUS_ERR_BAD_NICKNAME, NULL);
2281 silc_server_free_sock_user_data(server, sock, NULL);
2282 silc_packet_free(packet);
2286 /* If client marked as anonymous, scramble the username and hostname */
2287 if (client->mode & SILC_UMODE_ANONYMOUS) {
2290 if (strlen(username) >= 2) {
2291 username[0] = silc_rng_get_byte_fast(server->rng);
2292 username[1] = silc_rng_get_byte_fast(server->rng);
2295 scramble = silc_hash_babbleprint(server->sha1hash, username,
2299 memcpy(&scramble[16], ".silc", 5);
2300 scramble[21] = '\0';
2301 silc_free(username);
2302 username = scramble;
2305 /* Update client entry */
2306 idata->status |= SILC_IDLIST_STATUS_REGISTERED;
2307 client->nickname = nickname;
2308 client->username = username;
2309 client->userinfo = realname ? realname : strdup(username);
2310 client->id = client_id;
2311 id_len = silc_id_get_len(client_id, SILC_ID_CLIENT);
2312 silc_idcache_update_by_context(server->local_list->clients, client,
2313 client_id, nicknamec, TRUE);
2315 /* Notify our router about new client on the SILC network */
2316 silc_server_send_new_id(server, SILC_PRIMARY_ROUTE(server),
2317 SILC_BROADCAST(server), client->id,
2318 SILC_ID_CLIENT, id_len);
2320 /* Distribute to backup routers */
2321 if (server->server_type == SILC_ROUTER) {
2322 SilcBuffer idp = silc_id_payload_encode(client->id, SILC_ID_CLIENT);
2323 silc_server_backup_send(server, NULL, SILC_PACKET_NEW_ID, 0,
2324 idp->data, silc_buffer_len(idp), FALSE, TRUE);
2325 silc_buffer_free(idp);
2328 /* Send the new client ID to the client. */
2329 silc_server_send_new_id(server, sock, FALSE, client->id, SILC_ID_CLIENT,
2330 silc_id_get_len(client->id, SILC_ID_CLIENT));
2332 /* Send some nice info to the client */
2333 silc_server_send_connect_notifys(server, sock, client);
2335 /* Check if anyone is watching this nickname */
2336 if (server->server_type == SILC_ROUTER)
2337 silc_server_check_watcher_list(server, client, NULL, 0);
2339 silc_packet_free(packet);
2343 /* Create new server. This processes received New Server packet and
2344 saves the received Server ID. The server is our locally connected
2345 server thus we save all the information and save it to local list.
2346 This funtion can be used by both normal server and router server.
2347 If normal server uses this it means that its router has connected
2348 to the server. If router uses this it means that one of the cell's
2349 servers is connected to the router. */
2351 SilcServerEntry silc_server_new_server(SilcServer server,
2352 SilcPacketStream sock,
2355 SilcBuffer buffer = &packet->buffer;
2356 SilcIDListData idata = silc_packet_get_context(sock);
2357 SilcServerEntry new_server, server_entry;
2358 SilcServerID server_id;
2359 unsigned char *server_name, *server_namec, *id_string;
2360 SilcUInt16 id_len, name_len;
2362 SilcBool local = TRUE;
2363 const char *hostname, *ip;
2365 SILC_LOG_DEBUG(("Creating new server"));
2367 if (idata->conn_type != SILC_CONN_SERVER &&
2368 idata->conn_type != SILC_CONN_ROUTER) {
2369 silc_packet_free(packet);
2373 /* Take server entry */
2374 new_server = (SilcServerEntry)idata;
2375 silc_socket_stream_get_info(silc_packet_stream_get_stream(sock),
2376 NULL, &hostname, &ip, NULL);
2379 if (server->server_type == SILC_ROUTER)
2380 server->stat.cell_servers++;
2382 /* Remove the old cache entry */
2383 if (!silc_idcache_del_by_context(server->local_list->servers, new_server,
2385 if (!silc_idcache_del_by_context(server->global_list->servers,
2386 new_server, NULL)) {
2387 SILC_LOG_INFO(("Unauthenticated %s attempted to register to "
2388 "network", (idata->conn_type == SILC_CONN_SERVER ?
2389 "server" : "router")));
2390 silc_server_disconnect_remote(server, sock,
2391 SILC_STATUS_ERR_NOT_AUTHENTICATED, NULL);
2392 silc_server_free_sock_user_data(server, sock, NULL);
2398 /* Make sure this server hasn't registered already */
2399 if (idata->status & SILC_IDLIST_STATUS_REGISTERED) {
2400 silc_server_disconnect_remote(server, sock,
2401 SILC_STATUS_ERR_OPERATION_ALLOWED,
2402 "Too many registrations");
2403 silc_server_free_sock_user_data(server, sock, NULL);
2407 /* Parse the incoming packet */
2408 ret = silc_buffer_unformat(buffer,
2409 SILC_STR_UI16_NSTRING_ALLOC(&id_string, &id_len),
2410 SILC_STR_UI16_NSTRING_ALLOC(&server_name,
2414 silc_free(id_string);
2415 silc_free(server_name);
2416 silc_server_disconnect_remote(server, sock,
2417 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
2419 silc_server_free_sock_user_data(server, sock, NULL);
2423 if (id_len > silc_buffer_len(buffer)) {
2424 silc_free(id_string);
2425 silc_free(server_name);
2426 silc_server_disconnect_remote(server, sock,
2427 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
2429 silc_server_free_sock_user_data(server, sock, NULL);
2433 if (name_len > 256) {
2434 server_name[256] = '\0';
2439 if (!silc_id_str2id(id_string, id_len, SILC_ID_SERVER, &server_id,
2440 sizeof(server_id))) {
2441 silc_free(id_string);
2442 silc_free(server_name);
2443 silc_server_disconnect_remote(server, sock,
2444 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
2446 silc_server_free_sock_user_data(server, sock, NULL);
2449 silc_free(id_string);
2451 /* Check for valid server ID */
2452 if (!silc_id_is_valid_server_id(server, &server_id, sock)) {
2453 SILC_LOG_INFO(("Invalid server ID sent by %s (%s)",
2455 silc_server_disconnect_remote(server, sock,
2456 SILC_STATUS_ERR_BAD_SERVER_ID, NULL);
2457 silc_server_free_sock_user_data(server, sock, NULL);
2458 silc_free(server_name);
2462 /* Check that we do not have this ID already */
2463 server_entry = silc_idlist_find_server_by_id(server->local_list,
2464 &server_id, TRUE, NULL);
2466 if (SILC_IS_LOCAL(server_entry)) {
2467 SILC_LOG_ERROR(("Too many registrations from %s (%s)",
2469 silc_server_disconnect_remote(server, sock,
2470 SILC_STATUS_ERR_OPERATION_ALLOWED,
2471 "Too many registrations");
2472 silc_server_free_sock_user_data(server, sock, NULL);
2475 silc_idcache_del_by_context(server->local_list->servers, server_entry,
2479 server_entry = silc_idlist_find_server_by_id(server->global_list,
2480 &server_id, TRUE, NULL);
2482 if (SILC_IS_LOCAL(server_entry)) {
2483 SILC_LOG_ERROR(("Too many registrations from %s (%s)",
2485 silc_server_disconnect_remote(server, sock,
2486 SILC_STATUS_ERR_OPERATION_ALLOWED,
2487 "Too many registrations");
2488 silc_server_free_sock_user_data(server, sock, NULL);
2491 silc_idcache_del_by_context(server->global_list->servers,
2492 server_entry, NULL);
2497 /* Check server name */
2498 server_namec = silc_identifier_check(server_name, strlen(server_name),
2499 SILC_STRING_UTF8, 256, NULL);
2500 if (!server_namec) {
2501 SILC_LOG_ERROR(("Malformed server name from %s (%s)",
2503 silc_server_disconnect_remote(server, sock,
2504 SILC_STATUS_ERR_OPERATION_ALLOWED,
2505 "Malfromed server name");
2506 silc_server_free_sock_user_data(server, sock, NULL);
2510 /* Update server entry */
2511 idata->status |= SILC_IDLIST_STATUS_REGISTERED;
2512 new_server->server_name = server_name;
2513 new_server->id = silc_id_dup(&server_id, SILC_ID_SERVER);
2515 SILC_LOG_DEBUG(("New server id(%s)",
2516 silc_id_render(&server_id, SILC_ID_SERVER)));
2518 /* Add again the entry to the ID cache. */
2519 silc_idcache_add(local ? server->local_list->servers :
2520 server->global_list->servers, server_namec,
2521 new_server->id, new_server);
2523 /* Distribute the information about new server in the SILC network
2524 to our router. If we are normal server we won't send anything
2525 since this connection must be our router connection. */
2526 if (server->server_type == SILC_ROUTER && !server->standalone &&
2527 SILC_PRIMARY_ROUTE(server) != sock)
2528 silc_server_send_new_id(server, SILC_PRIMARY_ROUTE(server),
2529 TRUE, new_server->id, SILC_ID_SERVER,
2530 silc_id_get_len(&server_id, SILC_ID_SERVER));
2532 if (server->server_type == SILC_ROUTER) {
2533 /* Distribute to backup routers */
2534 SilcBuffer idp = silc_id_payload_encode(new_server->id, SILC_ID_SERVER);
2535 silc_server_backup_send(server, NULL, SILC_PACKET_NEW_ID, 0, idp->data,
2536 silc_buffer_len(idp), FALSE, TRUE);
2537 silc_buffer_free(idp);
2540 /* Check whether this router connection has been replaced by an
2541 backup router. If it has been then we'll disable the server and will
2542 ignore everything it will send until the backup router resuming
2543 protocol has been completed. */
2544 if (idata->conn_type == SILC_CONN_ROUTER &&
2545 silc_server_backup_replaced_get(server, &server_id, NULL)) {
2546 /* Send packet to the router indicating that it cannot use this
2547 connection as it has been replaced by backup router. */
2548 SILC_LOG_DEBUG(("Remote router has been replaced by backup router, "
2549 "disabling its connection"));
2551 silc_server_backup_send_replaced(server, sock);
2553 /* Mark the router disabled. The data sent earlier will go but nothing
2554 after this goes to this connection. */
2555 idata->status |= SILC_IDLIST_STATUS_DISABLED;
2557 /* If it is router announce our stuff to it. */
2558 if (idata->conn_type == SILC_CONN_ROUTER &&
2559 server->server_type == SILC_ROUTER) {
2560 silc_server_announce_servers(server, FALSE, 0, sock);
2561 silc_server_announce_clients(server, 0, sock);
2562 silc_server_announce_channels(server, 0, sock);
2565 /* Announce our information to backup router */
2566 if (new_server->server_type == SILC_BACKUP_ROUTER &&
2567 idata->conn_type == SILC_CONN_SERVER &&
2568 server->server_type == SILC_ROUTER) {
2569 silc_server_announce_servers(server, TRUE, 0, sock);
2570 silc_server_announce_clients(server, 0, sock);
2571 silc_server_announce_channels(server, 0, sock);
2574 /* If backup router, mark it as one of ours. This server is considered
2575 to be backup router after this setting. */
2576 if (new_server->server_type == SILC_BACKUP_ROUTER) {
2577 SilcServerConfigRouter *backup;
2578 backup = silc_server_config_find_backup_conn(server, (char *)ip);
2580 backup = silc_server_config_find_backup_conn(server, (char *)hostname);
2582 /* Add as our backup router */
2583 silc_server_backup_add(server, new_server, backup->backup_replace_ip,
2584 backup->backup_replace_port,
2585 backup->backup_local);
2589 /* By default the servers connected to backup router are disabled
2590 until backup router has become the primary */
2591 if (server->server_type == SILC_BACKUP_ROUTER &&
2592 idata->conn_type == SILC_CONN_SERVER)
2593 idata->status |= SILC_IDLIST_STATUS_DISABLED;
2599 /* Processes incoming New ID packet. New ID Payload is used to distribute
2600 information about newly registered clients and servers. */
2602 static void silc_server_new_id_real(SilcServer server,
2603 SilcPacketStream sock,
2608 SilcIDListData idata = silc_packet_get_context(sock);
2610 SilcServerEntry router, server_entry;
2611 SilcPacketStream router_sock;
2614 SilcServerID sender_id;
2615 const char *hostname, *ip;
2617 SILC_LOG_DEBUG(("Processing new ID"));
2619 if (idata->conn_type == SILC_CONN_CLIENT ||
2620 server->server_type == SILC_SERVER ||
2621 packet->src_id_type != SILC_ID_SERVER)
2624 idp = silc_id_payload_parse(buffer->data, silc_buffer_len(buffer));
2628 id_type = silc_id_payload_get_type(idp);
2630 silc_socket_stream_get_info(silc_packet_stream_get_stream(sock),
2631 NULL, &hostname, &ip, NULL);
2633 /* Normal server cannot have other normal server connections */
2634 server_entry = (SilcServerEntry)idata;
2635 if (id_type == SILC_ID_SERVER && idata->conn_type == SILC_CONN_SERVER &&
2636 server_entry->server_type == SILC_SERVER)
2639 /* If the packet is coming from server then use the sender as the
2640 origin of the the packet. If it came from router then check the real
2641 sender of the packet and use that as the origin. */
2642 if (idata->conn_type == SILC_CONN_SERVER) {
2643 id_list = server->local_list;
2645 router = server_entry;
2647 /* If the sender is backup router and ID is server (and we are not
2648 backup router) then switch the entry to global list. */
2649 if (server_entry->server_type == SILC_BACKUP_ROUTER &&
2650 id_type == SILC_ID_SERVER &&
2651 server->id_entry->server_type != SILC_BACKUP_ROUTER) {
2652 id_list = server->global_list;
2653 router_sock = server->router ? SILC_PRIMARY_ROUTE(server) : sock;
2656 silc_id_str2id(packet->src_id, packet->src_id_len,
2657 packet->src_id_type, &sender_id, sizeof(sender_id));
2658 router = silc_idlist_find_server_by_id(server->global_list,
2659 &sender_id, TRUE, NULL);
2661 router = silc_idlist_find_server_by_id(server->local_list,
2662 &sender_id, TRUE, NULL);
2664 id_list = server->global_list;
2671 case SILC_ID_CLIENT:
2673 SilcClientEntry entry;
2676 if (!silc_id_payload_get_id(idp, &id, sizeof(id)))
2679 /* Check that we do not have this client already */
2680 entry = silc_idlist_find_client_by_id(server->global_list,
2681 &id, server->server_type,
2684 entry = silc_idlist_find_client_by_id(server->local_list,
2685 &id, server->server_type,
2688 SILC_LOG_DEBUG(("Ignoring client that we already have"));
2692 SILC_LOG_DEBUG(("New client id(%s) from [%s] %s",
2693 silc_id_render(&id, SILC_ID_CLIENT),
2694 idata->conn_type == SILC_CONN_SERVER ?
2695 "Server" : "Router", hostname));
2697 /* As a router we keep information of all global information in our
2698 global list. Cell wide information however is kept in the local
2700 entry = silc_idlist_add_client(id_list, NULL, NULL, NULL,
2701 silc_id_dup(&id, SILC_ID_CLIENT),
2704 SILC_LOG_ERROR(("Could not add new client to the ID Cache"));
2706 /* Inform the sender that the ID is not usable */
2707 silc_server_send_notify_signoff(server, sock, FALSE, &id, NULL);
2710 entry->nickname = NULL;
2711 entry->data.status |= SILC_IDLIST_STATUS_REGISTERED;
2713 if (idata->conn_type == SILC_CONN_SERVER)
2714 server->stat.cell_clients++;
2715 SILC_LOG_DEBUG(("stat.clients %d->%d", server->stat.clients,
2716 server->stat.clients + 1));
2717 server->stat.clients++;
2719 /* Check if anyone is watching this nickname */
2720 if (server->server_type == SILC_ROUTER && id_list == server->local_list)
2721 silc_server_check_watcher_list(server, entry, NULL, 0);
2723 if (server->server_type == SILC_ROUTER) {
2724 /* Add the client's public key to repository or get the key with
2726 if (entry->data.public_key) {
2727 if (!silc_server_get_public_key_by_client(server, entry, NULL))
2728 silc_skr_add_public_key_simple(server->repository,
2729 entry->data.public_key,
2730 SILC_SKR_USAGE_IDENTIFICATION,
2733 silc_server_send_command(server, router_sock,
2734 SILC_COMMAND_GETKEY, ++server->cmd_ident,
2736 silc_buffer_len(buffer));
2742 case SILC_ID_SERVER:
2744 SilcServerEntry entry;
2747 if (!silc_id_payload_get_id(idp, &id, sizeof(id)))
2750 /* If the ID is mine, ignore it. */
2751 if (SILC_ID_SERVER_COMPARE(&id, server->id)) {
2752 SILC_LOG_DEBUG(("Ignoring my own ID as new ID"));
2756 /* If the ID is the sender's ID, ignore it (we have it already) */
2757 if (SILC_ID_SERVER_COMPARE(&id, router->id)) {
2758 SILC_LOG_DEBUG(("Ignoring sender's own ID"));
2762 /* Check that we do not have this server already */
2763 entry = silc_idlist_find_server_by_id(server->global_list,
2764 &id, server->server_type,
2767 entry = silc_idlist_find_server_by_id(server->local_list,
2768 &id, server->server_type,
2771 SILC_LOG_DEBUG(("Ignoring server that we already have"));
2775 SILC_LOG_DEBUG(("New server id(%s) from [%s] %s",
2776 silc_id_render(&id, SILC_ID_SERVER),
2777 idata->conn_type == SILC_CONN_SERVER ?
2778 "Server" : "Router", hostname));
2780 /* As a router we keep information of all global information in our
2781 global list. Cell wide information however is kept in the local
2783 entry = silc_idlist_add_server(id_list, NULL, 0,
2784 silc_id_dup(&id, SILC_ID_SERVER), router,
2787 SILC_LOG_ERROR(("Could not add new server to the ID Cache"));
2790 entry->data.status |= SILC_IDLIST_STATUS_REGISTERED;
2792 if (idata->conn_type == SILC_CONN_SERVER)
2793 server->stat.cell_servers++;
2794 server->stat.servers++;
2798 case SILC_ID_CHANNEL:
2799 SILC_LOG_ERROR(("Channel cannot be registered with NEW_ID packet"));
2808 /* If the sender of this packet is server and we are router we need to
2809 broadcast this packet to other routers in the network. */
2810 if (broadcast && server->server_type == SILC_ROUTER &&
2811 idata->conn_type == SILC_CONN_SERVER &&
2812 !(packet->flags & SILC_PACKET_FLAG_BROADCAST)) {
2813 SILC_LOG_DEBUG(("Broadcasting received New ID packet"));
2814 silc_server_packet_send(server, SILC_PRIMARY_ROUTE(server),
2816 packet->flags | SILC_PACKET_FLAG_BROADCAST,
2817 buffer->data, silc_buffer_len(buffer));
2818 silc_server_backup_send(server, (SilcServerEntry)idata,
2819 packet->type, packet->flags,
2820 packet->buffer.data,
2821 silc_buffer_len(&packet->buffer),
2826 silc_id_payload_free(idp);
2830 /* Processes incoming New ID packet. New ID Payload is used to distribute
2831 information about newly registered clients and servers. */
2833 void silc_server_new_id(SilcServer server, SilcPacketStream sock,
2836 silc_server_new_id_real(server, sock, packet, &packet->buffer, TRUE);
2837 silc_packet_free(packet);
2840 /* Receoved New Id List packet, list of New ID payloads inside one
2841 packet. Process the New ID payloads one by one. */
2843 void silc_server_new_id_list(SilcServer server, SilcPacketStream sock,
2846 SilcIDListData idata = silc_packet_get_context(sock);
2850 SILC_LOG_DEBUG(("Processing New ID List"));
2852 if (idata->conn_type == SILC_CONN_CLIENT ||
2853 packet->src_id_type != SILC_ID_SERVER) {
2854 silc_packet_free(packet);
2858 /* If the sender of this packet is server and we are router we need to
2859 broadcast this packet to other routers in the network. Broadcast
2860 this list packet instead of multiple New ID packets. */
2861 if (server->server_type == SILC_ROUTER &&
2862 idata->conn_type == SILC_CONN_SERVER &&
2863 !(packet->flags & SILC_PACKET_FLAG_BROADCAST)) {
2864 SILC_LOG_DEBUG(("Broadcasting received New ID List packet"));
2865 silc_server_packet_send(server, SILC_PRIMARY_ROUTE(server),
2867 packet->flags | SILC_PACKET_FLAG_BROADCAST,
2868 packet->buffer.data,
2869 silc_buffer_len(&packet->buffer));
2870 silc_server_backup_send(server, (SilcServerEntry)idata,
2871 packet->type, packet->flags,
2872 packet->buffer.data,
2873 silc_buffer_len(&packet->buffer),
2877 idp = silc_buffer_alloc(256);
2879 silc_packet_free(packet);
2883 while (silc_buffer_len(&packet->buffer)) {
2884 SILC_GET16_MSB(id_len, packet->buffer.data + 2);
2885 if ((id_len > silc_buffer_len(&packet->buffer)) ||
2886 (id_len > silc_buffer_truelen(idp)))
2889 silc_buffer_pull_tail(idp, 4 + id_len);
2890 silc_buffer_put(idp, packet->buffer.data, 4 + id_len);
2892 /* Process the New ID */
2893 silc_server_new_id_real(server, sock, packet, idp, FALSE);
2895 silc_buffer_push_tail(idp, 4 + id_len);
2896 silc_buffer_pull(&packet->buffer, 4 + id_len);
2899 silc_buffer_free(idp);
2900 silc_packet_free(packet);
2903 /* Received New Channel packet. Information about new channels in the
2904 network are distributed using this packet. Save the information about
2905 the new channel. This usually comes from router but also normal server
2906 can send this to notify channels it has when it connects to us. */
2908 static void silc_server_new_channel_process(SilcServer server,
2909 SilcPacketStream sock,
2913 SilcIDListData idata = silc_packet_get_context(sock);
2914 SilcChannelPayload payload;
2915 SilcChannelID channel_id;
2916 char *channel_name, *channel_namec = NULL;
2917 SilcUInt32 name_len;
2918 unsigned char *id, cid[32];
2919 SilcUInt32 id_len, cipher_len;
2920 SilcServerEntry server_entry;
2921 SilcChannelEntry channel;
2924 if (idata->conn_type == SILC_CONN_CLIENT ||
2925 packet->src_id_type != SILC_ID_SERVER ||
2926 server->server_type == SILC_SERVER)
2929 /* Parse the channel payload */
2930 payload = silc_channel_payload_parse(buffer->data, silc_buffer_len(buffer));
2934 /* Get the channel ID */
2935 if (!silc_channel_get_id_parse(payload, &channel_id)) {
2936 silc_channel_payload_free(payload);
2940 channel_name = silc_channel_get_name(payload, &name_len);
2941 if (name_len > 256) {
2942 channel_name[256] = '\0';
2946 /* Check channel name */
2947 channel_namec = silc_channel_name_check(channel_name, strlen(channel_name),
2948 SILC_STRING_UTF8, 256, NULL);
2952 id = silc_channel_get_id(payload, &id_len);
2954 server_entry = (SilcServerEntry)idata;
2956 if (idata->conn_type == SILC_CONN_ROUTER) {
2957 /* Add the channel to global list as it is coming from router. It
2958 cannot be our own channel as it is coming from router. */
2960 /* Check that we don't already have this channel */
2961 channel = silc_idlist_find_channel_by_name(server->local_list,
2962 channel_namec, NULL);
2964 channel = silc_idlist_find_channel_by_name(server->global_list,
2965 channel_namec, NULL);
2967 SILC_LOG_DEBUG(("New channel id(%s) from [Router] %s",
2968 silc_id_render(&channel_id, SILC_ID_CHANNEL),
2969 idata->sconn->remote_host));
2972 silc_idlist_add_channel(server->global_list, strdup(channel_name),
2973 0, silc_id_dup(&channel_id, SILC_ID_CHANNEL),
2974 (SilcServerEntry)idata, NULL, NULL, NULL);
2976 silc_channel_payload_free(payload);
2979 channel->disabled = TRUE; /* Disabled until someone JOINs */
2981 server->stat.channels++;
2982 if (server->server_type == SILC_ROUTER)
2983 channel->users_resolved = TRUE;
2986 /* The channel is coming from our server, thus it is in our cell
2987 we will add it to our local list. */
2990 SILC_LOG_DEBUG(("Channel id(%s) from [Server] %s",
2991 silc_id_render(&channel_id, SILC_ID_CHANNEL),
2992 idata->sconn->remote_host));
2994 /* Check that we don't already have this channel */
2995 channel = silc_idlist_find_channel_by_name(server->local_list,
2996 channel_namec, NULL);
2998 channel = silc_idlist_find_channel_by_name(server->global_list,
2999 channel_namec, NULL);
3001 /* If the channel does not exist, then create it. This creates a new
3002 key to the channel as well that we will send to the server. */
3004 SILC_LOG_DEBUG(("Channel is new to us"));
3006 /* The protocol says that the Channel ID's IP address must be based
3007 on the router's IP address. Check whether the ID is based in our
3008 IP and if it is not then create a new ID and enforce the server
3009 to switch the ID. */
3010 if (server_entry->server_type != SILC_BACKUP_ROUTER &&
3011 !SILC_ID_COMPARE(&channel_id, server->id, server->id->ip.data_len)) {
3013 SILC_LOG_DEBUG(("Forcing the server to change Channel ID"));
3014 if (silc_id_create_channel_id(server, server->id, server->rng, &tmp)) {
3015 silc_server_send_notify_channel_change(server, sock, FALSE,
3017 silc_channel_payload_free(payload);
3021 /* Wait that server re-announces this channel */
3025 /* Create the channel with the provided Channel ID */
3027 silc_server_create_new_channel_with_id(
3030 silc_id_dup(&channel_id, SILC_ID_CHANNEL),
3033 silc_channel_payload_free(payload);
3036 channel->disabled = TRUE; /* Disabled until someone JOINs */
3038 #if 0 /* We assume that CMODE_CHANGE notify is sent to us after this. */
3040 /* XXX Dunno if this is supposed to be set in any server type. If set
3041 here the CMODE_CHANGE that may follow sets mode that we already
3042 have, and we may loose data from the CMODE_CHANGE notify. */
3043 if (server_entry->server_type != SILC_BACKUP_ROUTER)
3044 channel->mode = silc_channel_get_mode(payload);
3047 /* Send the new channel key to the server */
3048 silc_id_id2str(channel->id, SILC_ID_CHANNEL, cid, sizeof(cid),
3050 cipher = silc_cipher_get_name(channel->send_key);
3051 cipher_len = strlen(cipher);
3052 chk = silc_channel_key_payload_encode(id_len, cid,
3054 channel->key_len / 8,
3056 silc_server_packet_send(server, sock, SILC_PACKET_CHANNEL_KEY, 0,
3057 chk->data, silc_buffer_len(chk));
3058 silc_buffer_free(chk);
3060 /* The channel exist by that name, check whether the ID's match.
3061 If they don't then we'll force the server to use the ID we have.
3062 We also create a new key for the channel. */
3063 SilcBuffer modes = NULL, users = NULL, users_modes = NULL;
3065 SILC_LOG_DEBUG(("Channel already exists"));
3067 if (!SILC_ID_CHANNEL_COMPARE(&channel_id, channel->id)) {
3068 /* They don't match, send CHANNEL_CHANGE notify to the server to
3069 force the ID change. */
3070 SILC_LOG_DEBUG(("Forcing the server to change Channel ID"));
3071 silc_server_send_notify_channel_change(server, sock, FALSE,
3072 &channel_id, channel->id);
3073 silc_channel_payload_free(payload);
3075 /* Wait that server re-announces this channel */
3079 #if 0 /* We will announce our CMODE anyway for this channel, so no need
3080 to check it (implicit enforce). */
3082 /* If the mode is different from what we have then enforce the
3084 mode = silc_channel_get_mode(payload);
3085 if (channel->mode != mode) {
3086 SILC_LOG_DEBUG(("Forcing the server to change channel mode"));
3087 silc_server_send_notify_cmode(server, sock, FALSE, channel,
3088 channel->mode, server->id,
3089 SILC_ID_SERVER, channel->cipher,
3091 channel->passphrase,
3092 channel->founder_key);
3096 /* Create new key for the channel and send it to the server and
3097 everybody else possibly on the channel. */
3098 if (!(channel->mode & SILC_CHANNEL_MODE_PRIVKEY)) {
3100 if (silc_hash_table_count(channel->user_list)) {
3101 if (!silc_server_create_channel_key(server, channel, 0)) {
3102 silc_channel_payload_free(payload);
3106 /* Send to the channel */
3107 silc_server_send_channel_key(server, sock, channel, FALSE);
3110 /* Send to the server */
3111 silc_id_id2str(channel->id, SILC_ID_CHANNEL, cid, sizeof(cid),
3113 cipher = silc_cipher_get_name(channel->send_key);
3114 cipher_len = strlen(cipher);
3115 chk = silc_channel_key_payload_encode(id_len, cid,
3117 channel->key_len / 8,
3119 silc_server_packet_send(server, sock, SILC_PACKET_CHANNEL_KEY, 0,
3120 chk->data, silc_buffer_len(chk));
3121 silc_buffer_free(chk);
3124 /* Since the channel is coming from server and we also know about it
3125 then send the JOIN notify to the server so that it see's our
3126 users on the channel "joining" the channel. */
3127 silc_server_announce_get_channel_users(server, channel, &modes, &users,
3130 silc_buffer_push(users, users->data - users->head);
3131 silc_server_packet_send(server, sock,
3132 SILC_PACKET_NOTIFY, SILC_PACKET_FLAG_LIST,
3133 users->data, silc_buffer_len(users));
3134 silc_buffer_free(users);
3137 silc_buffer_push(modes, modes->data - modes->head);
3138 silc_server_packet_send_dest(server, sock,
3139 SILC_PACKET_NOTIFY, SILC_PACKET_FLAG_LIST,
3140 channel->id, SILC_ID_CHANNEL,
3141 modes->data, silc_buffer_len(modes));
3142 silc_buffer_free(modes);
3145 silc_buffer_push(users_modes, users_modes->data - users_modes->head);
3146 silc_server_packet_send_dest(server, sock,
3147 SILC_PACKET_NOTIFY, SILC_PACKET_FLAG_LIST,
3148 channel->id, SILC_ID_CHANNEL,
3150 silc_buffer_len(users_modes));
3151 silc_buffer_free(users_modes);
3153 if (channel->topic) {
3154 silc_server_send_notify_topic_set(server, sock,
3155 server->server_type == SILC_ROUTER ?
3156 TRUE : FALSE, channel,
3157 server->id, SILC_ID_SERVER,
3163 /* If the sender of this packet is server and we are router we need to
3164 broadcast this packet to other routers in the network. Broadcast
3165 this list packet instead of multiple New Channel packets. */
3166 if (server->server_type == SILC_ROUTER &&
3167 idata->conn_type == SILC_CONN_SERVER &&
3168 !(packet->flags & SILC_PACKET_FLAG_BROADCAST)) {
3169 SILC_LOG_DEBUG(("Broadcasting received New Channel packet"));
3170 silc_server_packet_send(server, SILC_PRIMARY_ROUTE(server),
3172 packet->flags | SILC_PACKET_FLAG_BROADCAST,
3173 buffer->data, silc_buffer_len(buffer));
3174 silc_server_backup_send(server, (SilcServerEntry)idata,
3175 packet->type, packet->flags,
3176 buffer->data, silc_buffer_len(buffer),
3180 silc_free(channel_namec);
3181 silc_channel_payload_free(payload);
3184 /* Received New Channel packet. Information about new channels in the
3185 network are distributed using this packet. Save the information about
3186 the new channel. This usually comes from router but also normal server
3187 can send this to notify channels it has when it connects to us. */
3189 void silc_server_new_channel(SilcServer server,
3190 SilcPacketStream sock,
3193 silc_server_new_channel_process(server, sock, packet, &packet->buffer);
3194 silc_packet_free(packet);
3197 /* Received New Channel List packet, list of New Channel List payloads inside
3198 one packet. Process the New Channel payloads one by one. */
3200 void silc_server_new_channel_list(SilcServer server,
3201 SilcPacketStream sock,
3204 SilcIDListData idata = silc_packet_get_context(sock);
3206 SilcUInt16 len1, len2;
3208 SILC_LOG_DEBUG(("Processing New Channel List"));
3210 if (idata->conn_type == SILC_CONN_CLIENT ||
3211 packet->src_id_type != SILC_ID_SERVER ||
3212 server->server_type == SILC_SERVER) {
3213 silc_packet_free(packet);
3217 buffer = silc_buffer_alloc(512);
3219 silc_packet_free(packet);
3223 while (silc_buffer_len(&packet->buffer)) {
3224 SILC_GET16_MSB(len1, packet->buffer.data);
3225 if ((len1 > silc_buffer_len(&packet->buffer)) ||
3226 (len1 > silc_buffer_truelen(buffer)))
3229 SILC_GET16_MSB(len2, packet->buffer.data + 2 + len1);
3230 if ((len2 > silc_buffer_len(&packet->buffer)) ||
3231 (len2 > silc_buffer_truelen(buffer)))
3234 silc_buffer_pull_tail(buffer, 8 + len1 + len2);
3235 silc_buffer_put(buffer, packet->buffer.data, 8 + len1 + len2);
3237 /* Process the New Channel */
3238 silc_server_new_channel_process(server, sock, packet, buffer);
3240 silc_buffer_push_tail(buffer, 8 + len1 + len2);
3241 silc_buffer_pull(&packet->buffer, 8 + len1 + len2);
3244 silc_buffer_free(buffer);
3245 silc_packet_free(packet);
3248 /* Received key agreement packet. This packet is never for us. It is to
3249 the client in the packet's destination ID. Sending of this sort of packet
3250 equals sending private message, ie. it is sent point to point from
3251 one client to another. */
3253 void silc_server_key_agreement(SilcServer server,
3254 SilcPacketStream sock,
3257 SilcPacketStream dst_sock;
3258 SilcIDListData idata;
3260 SILC_LOG_DEBUG(("Start"));
3262 if (packet->src_id_type != SILC_ID_CLIENT ||
3263 packet->dst_id_type != SILC_ID_CLIENT || !packet->dst_id) {
3264 silc_packet_free(packet);
3268 /* Get the route to the client */
3269 dst_sock = silc_server_get_client_route(server, packet->dst_id,
3270 packet->dst_id_len, NULL,
3273 silc_packet_free(packet);
3277 /* Relay the packet */
3278 silc_server_packet_route(server, dst_sock, packet);
3279 silc_packet_free(packet);
3282 /* Received connection auth request packet that is used during connection
3283 phase to resolve the mandatory authentication method. This packet can
3284 actually be received at anytime but usually it is used only during
3285 the connection authentication phase. Now, protocol says that this packet
3286 can come from client or server, however, we support only this coming
3287 from client and expect that server always knows what authentication
3290 void silc_server_connection_auth_request(SilcServer server,
3291 SilcPacketStream sock,
3294 SilcServerConfigClient *client = NULL;
3295 SilcUInt16 conn_type;
3297 SilcAuthMethod auth_meth = SILC_AUTH_NONE;
3298 const char *hostname, *ip;
3300 if (packet->src_id_type && packet->src_id_type != SILC_ID_CLIENT) {
3301 SILC_LOG_DEBUG(("Request not from client"));
3302 silc_packet_free(packet);
3306 silc_socket_stream_get_info(silc_packet_stream_get_stream(sock),
3307 NULL, &hostname, &ip, NULL);
3309 /* Parse the payload */
3310 ret = silc_buffer_unformat(&packet->buffer,
3311 SILC_STR_UI_SHORT(&conn_type),
3312 SILC_STR_UI_SHORT(NULL),
3314 if (ret == -1 || conn_type != SILC_CONN_CLIENT) {
3315 silc_packet_free(packet);
3319 /* Get the authentication method for the client */
3320 auth_meth = SILC_AUTH_NONE;
3321 client = silc_server_config_find_client(server, (char *)ip);
3323 client = silc_server_config_find_client(server, (char *)hostname);
3325 if (client->passphrase) {
3326 if (client->publickeys && !server->config->prefer_passphrase_auth)
3327 auth_meth = SILC_AUTH_PUBLIC_KEY;
3329 auth_meth = SILC_AUTH_PASSWORD;
3330 } else if (client->publickeys)
3331 auth_meth = SILC_AUTH_PUBLIC_KEY;
3334 SILC_LOG_DEBUG(("Authentication method is [%s]",
3335 (auth_meth == SILC_AUTH_NONE ? "None" :
3336 auth_meth == SILC_AUTH_PASSWORD ? "Passphrase" :
3337 "Digital signatures")));
3339 /* Send it back to the client */
3340 silc_server_send_connection_auth_request(server, sock, conn_type, auth_meth);
3341 silc_packet_free(packet);
3344 /* Received file transger packet. This packet is never for us. It is to
3345 the client in the packet's destination ID. Sending of this sort of packet
3346 equals sending private message, ie. it is sent point to point from
3347 one client to another. */
3349 void silc_server_ftp(SilcServer server,
3350 SilcPacketStream sock,
3353 SilcPacketStream dst_sock;
3354 SilcIDListData idata;
3356 SILC_LOG_DEBUG(("Start"));
3358 if (packet->src_id_type != SILC_ID_CLIENT ||
3359 packet->dst_id_type != SILC_ID_CLIENT || !packet->dst_id) {
3360 silc_packet_free(packet);
3364 /* Get the route to the client */
3365 dst_sock = silc_server_get_client_route(server, packet->dst_id,
3366 packet->dst_id_len, NULL,
3369 silc_packet_free(packet);
3373 /* Relay the packet */
3374 silc_server_packet_route(server, dst_sock, packet);
3375 silc_packet_free(packet);
3380 SilcPacketStream sock;
3382 SilcClientID client_id;
3383 } *SilcServerResumeResolve;
3385 SILC_SERVER_CMD_FUNC(resume_resolve)
3387 SilcServerResumeResolve r = (SilcServerResumeResolve)context;
3388 SilcServer server = r->server;
3389 SilcPacketStream sock = r->sock;
3390 SilcServerCommandReplyContext reply = context2;
3391 SilcClientEntry client;
3392 const char *hostname, *ip;
3394 SILC_LOG_DEBUG(("Start"));
3396 silc_socket_stream_get_info(silc_packet_stream_get_stream(sock),
3397 NULL, &hostname, &ip, NULL);
3399 if (!reply || !silc_command_get_status(reply->payload, NULL, NULL)) {
3400 SILC_LOG_ERROR(("Client %s (%s) tried to resume unknown client, "
3401 "closing connection", hostname, ip));
3402 silc_server_disconnect_remote(server, sock,
3403 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
3404 "Resuming not possible");
3405 silc_server_free_sock_user_data(server, sock, NULL);
3409 if (reply && silc_command_get(reply->payload) == SILC_COMMAND_WHOIS) {
3410 /* Get entry to the client, and resolve it if we don't have it. */
3411 client = silc_idlist_find_client_by_id(server->local_list,
3412 &r->client_id, TRUE, NULL);
3414 client = silc_idlist_find_client_by_id(server->global_list,
3415 &r->client_id, TRUE, NULL);
3417 SILC_LOG_ERROR(("Client %s (%s) tried to resume unknown client, "
3418 "closing connection", hostname, ip));
3419 silc_server_disconnect_remote(server, sock,
3420 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
3421 "Resuming not possible");
3422 silc_server_free_sock_user_data(server, sock, NULL);
3427 if (!(client->mode & SILC_UMODE_DETACHED)) {
3428 SILC_LOG_ERROR(("Client %s (%s) tried to resume un-detached client, "
3429 "closing connection", hostname, ip));
3430 silc_server_disconnect_remote(server, sock,
3431 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
3432 "Resuming not possible");
3433 silc_server_free_sock_user_data(server, sock, NULL);
3437 client->data.status |= SILC_IDLIST_STATUS_RESUME_RES;
3440 /* Reprocess the packet */
3441 silc_server_resume_client(server, sock, r->packet);
3444 silc_packet_stream_unref(r->sock);
3448 /* Received client resuming packet. This is used to resume detached
3449 client session. It can be sent by the client who wishes to resume
3450 but this is also sent by servers and routers to notify other routers
3451 that the client is not detached anymore. */
3453 void silc_server_resume_client(SilcServer server,
3454 SilcPacketStream sock,
3457 SilcBuffer buffer = &packet->buffer, buf;
3458 SilcIDListData idata = silc_packet_get_context(sock);
3459 SilcIDCacheEntry id_cache = NULL;
3460 SilcClientEntry detached_client;
3461 SilcClientID client_id;
3462 unsigned char *id_string, *auth = NULL, *nicknamec = NULL;
3463 unsigned char cid[32];
3465 SilcUInt16 id_len, auth_len = 0;
3466 SilcBool resolved, local, nick_change = FALSE, resolve = FALSE;
3467 SilcChannelEntry channel;
3468 SilcHashTableList htl;
3469 SilcChannelClientEntry chl;
3470 SilcServerResumeResolve r;
3471 SilcPublicKey public_key;
3472 const char *cipher, *hostname, *ip;
3474 SILC_LOG_DEBUG(("Resuming client"));
3476 silc_socket_stream_get_info(silc_packet_stream_get_stream(sock),
3477 NULL, &hostname, &ip, NULL);
3479 if (silc_buffer_unformat(buffer,
3480 SILC_STR_UI16_NSTRING(&id_string, &id_len),
3481 SILC_STR_END) < 0) {
3482 if (idata->conn_type == SILC_CONN_CLIENT) {
3483 SILC_LOG_ERROR(("Client %s (%s) sent incomplete resume information, "
3484 "closing connection", hostname, ip));
3485 silc_server_disconnect_remote(server, sock,
3486 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
3487 "Resuming not possible");
3488 silc_server_free_sock_user_data(server, sock, NULL);
3493 silc_id_str2id(id_string, id_len, SILC_ID_CLIENT, &client_id,
3496 if (idata->conn_type == SILC_CONN_CLIENT) {
3497 /* Client send this and is attempting to resume to old client session */
3498 SilcClientEntry client;
3501 silc_buffer_pull(buffer, 2 + id_len);
3502 auth = buffer->data;
3503 auth_len = silc_buffer_len(buffer);
3504 silc_buffer_push(buffer, 2 + id_len);
3506 if (auth_len < 128) {
3507 SILC_LOG_ERROR(("Client %s (%s) sent incomplete resume information, "
3508 "closing connection", hostname, ip));
3509 silc_server_disconnect_remote(server, sock,
3510 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
3511 "Resuming not possible");
3512 silc_server_free_sock_user_data(server, sock, NULL);
3516 /* Take client entry of this connection */
3517 client = (SilcClientEntry)idata;
3519 /* Get entry to the client, and resolve it if we don't have it. */
3520 detached_client = silc_server_query_client(server, &client_id, FALSE,
3522 if (!detached_client) {
3524 /* The client info is being resolved. Reprocess this packet after
3525 receiving the reply to the query. */
3526 SILC_LOG_DEBUG(("Resolving client"));
3527 r = silc_calloc(1, sizeof(*r));
3530 silc_packet_stream_ref(sock);
3534 r->client_id = client_id;
3535 silc_server_command_pending(server, SILC_COMMAND_WHOIS,
3537 silc_server_command_resume_resolve, r);
3540 SILC_LOG_ERROR(("Client %s (%s) tried to resume unknown client, "
3541 "closing connection", hostname, ip));
3542 silc_server_disconnect_remote(server, sock,
3543 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
3544 "Resuming not possible");
3545 silc_server_free_sock_user_data(server, sock, NULL);
3550 if (detached_client->data.status & SILC_IDLIST_STATUS_RESUMED) {
3551 SILC_LOG_ERROR(("Client %s (%s) tried to attach more than once, "
3552 "closing connection", hostname, ip));
3553 silc_server_disconnect_remote(server, sock,
3554 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
3555 "Resuming not possible");
3556 silc_server_free_sock_user_data(server, sock, NULL);
3560 if (detached_client->resuming_client &&
3561 detached_client->resuming_client != client) {
3562 SILC_LOG_ERROR(("Client %s (%s) tried to attach more than once, "
3563 "closing connection", hostname, ip));
3564 silc_server_disconnect_remote(server, sock,
3565 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
3566 "Resuming not possible");
3567 silc_server_free_sock_user_data(server, sock, NULL);
3571 if (!detached_client->resuming_client)
3572 detached_client->resuming_client = client;
3574 if (!(detached_client->mode & SILC_UMODE_DETACHED))
3576 if (!silc_hash_table_count(detached_client->channels) &&
3577 detached_client->router)
3579 if (!detached_client->nickname)
3581 if (detached_client->data.status & SILC_IDLIST_STATUS_RESUME_RES)
3585 if (server->server_type == SILC_SERVER && !server->standalone) {
3586 /* The client info is being resolved. Reprocess this packet after
3587 receiving the reply to the query. */
3588 SILC_LOG_DEBUG(("Resolving client info"));
3589 silc_server_query_client(server, &client_id, TRUE, NULL);
3590 r = silc_calloc(1, sizeof(*r));
3593 silc_packet_stream_ref(sock);
3597 r->client_id = client_id;
3598 silc_server_command_pending(server, SILC_COMMAND_WHOIS,
3600 silc_server_command_resume_resolve, r);
3603 if (server->server_type == SILC_SERVER) {
3604 SILC_LOG_ERROR(("Client %s (%s) tried to resume un-detached client, "
3605 "closing connection", hostname, ip));
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);
3614 /* Check that we have the public key of the client, if not then we must
3615 resolve it first. */
3616 if (!detached_client->data.public_key) {
3617 if (server->server_type == SILC_SERVER && server->standalone) {
3618 SILC_LOG_ERROR(("Detached client's public key not present, "
3619 "closing connection"));
3620 silc_server_disconnect_remote(server, sock,
3621 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
3622 "Resuming not possible");
3623 silc_server_free_sock_user_data(server, sock, NULL);
3626 /* We must retrieve the detached client's public key by sending
3627 GETKEY command. Reprocess this packet after receiving the key */
3628 SilcBuffer idp = silc_id_payload_encode(&client_id, SILC_ID_CLIENT);
3629 SilcPacketStream dest_sock =
3630 silc_server_get_client_route(server, NULL, 0, &client_id,
3633 SILC_LOG_DEBUG(("Resolving client public key"));
3635 silc_server_send_command(server, dest_sock ? dest_sock :
3636 SILC_PRIMARY_ROUTE(server),
3637 SILC_COMMAND_GETKEY, ++server->cmd_ident,
3638 1, 1, idp->data, silc_buffer_len(idp));
3640 r = silc_calloc(1, sizeof(*r));
3643 silc_packet_stream_ref(sock);
3647 r->client_id = client_id;
3648 silc_server_command_pending(server, SILC_COMMAND_GETKEY,
3650 silc_server_command_resume_resolve, r);
3652 silc_buffer_free(idp);
3655 } else if (!silc_pkcs_public_key_compare(detached_client->data.public_key,
3656 idata->public_key)) {
3657 /* We require that the connection and resuming authentication data
3658 must be using same key pair. */
3659 SILC_LOG_ERROR(("Resuming attempted with wrong public key, "
3660 "closing connection"));
3661 silc_server_disconnect_remote(server, sock,
3662 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
3663 "Resuming not possible");
3664 silc_server_free_sock_user_data(server, sock, NULL);
3668 /* Verify the authentication payload. This has to be successful in
3669 order to allow the resuming */
3671 !silc_auth_verify_data(auth, auth_len, SILC_AUTH_PUBLIC_KEY,
3672 detached_client->data.public_key, 0,
3673 idata->hash, detached_client->id,
3675 SILC_LOG_ERROR(("Client %s (%s) resume authentication failed, "
3676 "closing connection", hostname, ip));
3677 silc_server_disconnect_remote(server, sock,
3678 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
3679 "Resuming not possible");
3680 silc_server_free_sock_user_data(server, sock, NULL);
3684 /* Check nickname */
3685 nicknamec = silc_identifier_check(detached_client->nickname,
3686 strlen(detached_client->nickname),
3687 SILC_STRING_UTF8, 128, NULL);
3689 silc_server_disconnect_remote(server, sock,
3690 SILC_STATUS_ERR_BAD_NICKNAME,
3691 "Malformed nickname, cannot resume");
3692 silc_server_free_sock_user_data(server, sock, NULL);
3696 /* If the ID is not based in our ID then change it */
3697 if (!SILC_ID_COMPARE(detached_client->id, server->id,
3698 server->id->ip.data_len)) {
3699 SilcClientID *new_id;
3700 if (!silc_id_create_client_id(server, server->id, server->rng,
3701 server->md5hash, nicknamec,
3702 strlen(nicknamec), &new_id)) {
3703 silc_server_disconnect_remote(server, sock,
3704 SILC_STATUS_ERR_BAD_NICKNAME,
3705 "Resuming not possible");
3706 silc_server_free_sock_user_data(server, sock, NULL);
3710 client_id = *new_id;
3714 /* Now resume the client to the network */
3716 silc_schedule_task_del_by_context(server->schedule, detached_client);
3717 silc_packet_set_context(sock, detached_client);
3718 detached_client->connection = sock;
3720 if (detached_client->data.public_key) {
3721 /* Delete the detached client's public key from repository */
3722 silc_skr_del_public_key(server->repository,
3723 detached_client->data.public_key,
3725 detached_client->data.public_key = NULL;
3728 if (idata->public_key) {
3729 /* Delete the resuming client's public key from repository. It will
3730 be added later again. */
3731 public_key = silc_pkcs_public_key_copy(idata->public_key);
3732 silc_skr_del_public_key(server->repository, idata->public_key, idata);
3733 idata->public_key = public_key;
3736 /* Take new keys and stuff into use in the old entry */
3737 silc_idlist_del_data(detached_client);
3738 silc_idlist_add_data(detached_client, idata);
3739 idata->public_key = NULL;
3741 if (detached_client->data.public_key) {
3742 /* Add the resumed client's public key back to repository. */
3743 if (!silc_server_get_public_key_by_client(server, detached_client, NULL))
3744 silc_skr_add_public_key_simple(server->repository,
3745 detached_client->data.public_key,
3746 SILC_SKR_USAGE_IDENTIFICATION,
3747 detached_client, NULL);
3750 detached_client->data.status |= SILC_IDLIST_STATUS_REGISTERED;
3751 detached_client->data.status |= SILC_IDLIST_STATUS_RESUMED;
3752 detached_client->data.status |= SILC_IDLIST_STATUS_LOCAL;
3753 detached_client->data.status &= ~SILC_IDLIST_STATUS_RESUME_RES;
3754 detached_client->mode &= ~SILC_UMODE_DETACHED;
3755 server->stat.my_detached--;
3756 silc_dlist_del(server->expired_clients, detached_client);
3758 /* We are finished - reset resuming client */
3759 detached_client->resuming_client = NULL;
3761 /* Check if anyone is watching this client */
3762 if (server->server_type == SILC_ROUTER)
3763 silc_server_check_watcher_list(server, detached_client, NULL,
3764 SILC_NOTIFY_TYPE_UMODE_CHANGE);
3766 /* Delete this current client entry since we're resuming to old one.
3767 We decrement clients/cell_clients as we are getting rid of the
3768 current client and replacing it with the detached one. We keep the
3769 server user count as-is (incremented by the current client entry) as
3770 we decremented the count already during detach, thus we'd be undoing
3772 detached_client->local_detached = FALSE;
3773 SILC_LOG_DEBUG(("stat.clients %d->%d", server->stat.clients,
3774 server->stat.clients - 1));
3775 SILC_VERIFY(server->stat.clients > 0);
3776 server->stat.clients--;
3777 if (server->stat.cell_clients)
3778 server->stat.cell_clients--;
3779 silc_server_remove_from_channels(server, NULL, client, FALSE,
3780 NULL, FALSE, FALSE);
3781 silc_server_del_from_watcher_list(server, client);
3782 silc_dlist_del(server->expired_clients, client);
3783 if (!silc_idlist_del_client(server->local_list, client))
3784 silc_idlist_del_client(server->global_list, client);
3785 client = detached_client;
3786 silc_free(client->servername);
3787 client->servername = strdup(server->server_name);
3789 /* Send the RESUME_CLIENT packet to our primary router so that others
3790 know this client isn't detached anymore. */
3791 buf = silc_buffer_alloc_size(2 + id_len);
3792 silc_buffer_format(buf,
3793 SILC_STR_UI_SHORT(id_len),
3794 SILC_STR_UI_XNSTRING(id_string, id_len),
3797 /* Send to primary router */
3798 silc_server_packet_send(server, SILC_PRIMARY_ROUTE(server),
3799 SILC_PACKET_RESUME_CLIENT, 0,
3800 buf->data, silc_buffer_len(buf));
3801 silc_server_backup_send(server, client->router,
3802 SILC_PACKET_RESUME_CLIENT, 0,
3803 buf->data, silc_buffer_len(buf), TRUE, TRUE);
3805 /* As router we must deliver this packet directly to the original
3806 server whom this client was earlier. */
3807 if (server->server_type == SILC_ROUTER && client->router &&
3808 client->router->server_type != SILC_ROUTER)
3809 silc_server_packet_send(server, client->router->connection,
3810 SILC_PACKET_RESUME_CLIENT, 0,
3811 buf->data, silc_buffer_len(buf));
3812 silc_buffer_free(buf);
3813 client->router = NULL;
3816 /* Notify about Client ID change, nickname doesn't actually change. */
3817 silc_server_send_notify_nick_change(server, SILC_PRIMARY_ROUTE(server),
3818 SILC_BROADCAST(server),
3819 client->id, &client_id,
3823 /* Resolve users on those channels that client has joined but we
3824 haven't resolved user list yet. */
3825 if (server->server_type == SILC_SERVER && !server->standalone) {
3826 silc_hash_table_list(client->channels, &htl);
3827 while (silc_hash_table_get(&htl, NULL, (void *)&chl)) {
3828 channel = chl->channel;
3829 SILC_LOG_DEBUG(("Resolving users for %s channel",
3830 channel->channel_name));
3831 if (channel->disabled || !channel->users_resolved) {
3832 silc_server_send_command(server, SILC_PRIMARY_ROUTE(server),
3833 SILC_COMMAND_USERS, ++server->cmd_ident,
3834 1, 2, channel->channel_name,
3835 strlen(channel->channel_name));
3838 silc_hash_table_list_reset(&htl);
3841 /* Send the new client ID to the client. After this client may start
3842 receiving other packets, and may start sending packets too. */
3843 silc_server_send_new_id(server, sock, FALSE, &client_id, SILC_ID_CLIENT,
3844 silc_id_get_len(&client_id, SILC_ID_CLIENT));
3847 /* Send NICK change notify to channels as well. */
3848 SilcBuffer oidp, nidp;
3849 oidp = silc_id_payload_encode(client->id, SILC_ID_CLIENT);
3850 nidp = silc_id_payload_encode(&client_id, SILC_ID_CLIENT);
3851 silc_server_send_notify_on_channels(server, NULL, client,
3852 SILC_NOTIFY_TYPE_NICK_CHANGE, 3,
3853 oidp->data, silc_buffer_len(oidp),
3854 nidp->data, silc_buffer_len(nidp),
3856 strlen(client->nickname));
3857 silc_buffer_free(oidp);
3858 silc_buffer_free(nidp);
3862 if (!silc_idcache_update_by_context(server->local_list->clients, client,
3863 &client_id, NULL, FALSE))
3864 silc_idcache_update_by_context(server->global_list->clients, client,
3865 &client_id, NULL, FALSE);
3867 /* Move entry to local list if it is in global list */
3868 if (silc_idcache_find_by_context(server->global_list->clients, client,
3870 silc_idcache_move(server->global_list->clients,
3871 server->local_list->clients, id_cache);
3873 /* Send some nice info to the client */
3874 silc_server_send_connect_notifys(server, sock, client);
3876 /* Send all channel keys of channels the client has joined */
3877 silc_hash_table_list(client->channels, &htl);
3878 while (silc_hash_table_get(&htl, NULL, (void *)&chl)) {
3879 SilcBool created = FALSE;
3880 channel = chl->channel;
3882 if (channel->mode & SILC_CHANNEL_MODE_PRIVKEY)
3885 /* If we don't have channel key, then create one */
3886 if (!channel->send_key) {
3887 if (!silc_server_create_channel_key(server, channel, 0))
3892 silc_id_id2str(channel->id, SILC_ID_CHANNEL, cid, sizeof(cid),
3894 cipher = silc_cipher_get_name(channel->send_key);
3896 silc_channel_key_payload_encode(cid_len, cid,
3897 strlen(cipher), cipher,
3898 channel->key_len / 8, channel->key);
3900 /* Send the channel key to the client */
3901 silc_server_packet_send(server, sock, SILC_PACKET_CHANNEL_KEY, 0,
3902 keyp->data, silc_buffer_len(keyp));
3904 /* Distribute the channel key to channel */
3906 silc_server_send_channel_key(server, NULL, channel,
3907 server->server_type == SILC_ROUTER ?
3908 FALSE : !server->standalone);
3909 silc_server_backup_send(server, NULL, SILC_PACKET_CHANNEL_KEY, 0,
3910 keyp->data, silc_buffer_len(keyp),
3914 silc_buffer_free(keyp);
3916 silc_hash_table_list_reset(&htl);
3918 } else if (idata->conn_type != SILC_CONN_CLIENT) {
3919 /* Server or router sent this to us to notify that that a client has
3921 SilcServerEntry server_entry;
3922 SilcServerID server_id;
3924 /* Get entry to the client, and resolve it if we don't have it. */
3925 detached_client = silc_idlist_find_client_by_id(server->local_list,
3928 if (!detached_client) {
3929 detached_client = silc_idlist_find_client_by_id(server->global_list,
3932 if (!detached_client) {
3933 SILC_LOG_DEBUG(("Resuming client is unknown"));
3938 /* Check that the client has not been resumed already because it is
3939 protocol error to attempt to resume more than once. The client
3940 will be killed if this protocol error occurs. */
3941 if (detached_client->data.status & SILC_IDLIST_STATUS_RESUMED &&
3942 !(detached_client->mode & SILC_UMODE_DETACHED)) {
3943 /* The client is clearly attempting to resume more than once and
3944 perhaps playing around by resuming from several different places
3945 at the same time. */
3946 SILC_LOG_DEBUG(("Attempting to re-resume client, killing both"));
3947 silc_server_kill_client(server, detached_client, NULL,
3948 server->id, SILC_ID_SERVER);
3952 /* Check whether client is detached at all */
3953 if (!(detached_client->mode & SILC_UMODE_DETACHED)) {
3954 SILC_LOG_DEBUG(("Client is not detached"));
3958 /* Check nickname */
3959 if (detached_client->nickname) {
3960 nicknamec = silc_identifier_check(detached_client->nickname,
3961 strlen(detached_client->nickname),
3962 SILC_STRING_UTF8, 128, NULL);
3967 SILC_LOG_DEBUG(("Resuming detached client"));
3969 /* If the sender of this packet is server and we are router we need to
3970 broadcast this packet to other routers in the network. */
3971 if (server->server_type == SILC_ROUTER &&
3972 idata->conn_type == SILC_CONN_SERVER &&
3973 !(packet->flags & SILC_PACKET_FLAG_BROADCAST)) {
3974 SILC_LOG_DEBUG(("Broadcasting received Resume Client packet"));
3975 silc_server_packet_send(server, SILC_PRIMARY_ROUTE(server),
3977 packet->flags | SILC_PACKET_FLAG_BROADCAST,
3978 buffer->data, silc_buffer_len(buffer));
3979 silc_server_backup_send(server, (SilcServerEntry)idata,
3980 packet->type, packet->flags,
3981 packet->buffer.data,
3982 silc_buffer_len(&packet->buffer),
3986 /* If the client has a locally-connected previous owner, then we
3987 will need to notify them that the resume has completed. Note
3988 that if the previous owner was a router, this case is already
3989 handled above by the broadcast, so we shouldn't attempt to
3990 send another notification in that case. Additionally, if
3991 the previous owner was the server that sent the packet, then
3992 we'll not send the notification as it will have already done
3993 the necessary work locally. */
3994 if (server->server_type == SILC_ROUTER &&
3995 idata->conn_type == SILC_CONN_SERVER &&
3996 detached_client->router &&
3997 SILC_IS_LOCAL(detached_client->router) &&
3998 detached_client->router->server_type != SILC_ROUTER)
3999 silc_server_packet_send(server, detached_client->router->connection,
4000 SILC_PACKET_RESUME_CLIENT, 0,
4001 buffer->data, silc_buffer_len(buffer));
4003 /* Client is detached, and now it is resumed. Remove the detached
4004 mode and mark that it is resumed. */
4006 if (detached_client->data.public_key) {
4007 /* Delete the detached client's public key from repository */
4008 silc_skr_del_public_key(server->repository,
4009 detached_client->data.public_key,
4011 detached_client->data.public_key = NULL;
4014 silc_idlist_del_data(detached_client);
4015 detached_client->mode &= ~SILC_UMODE_DETACHED;
4016 detached_client->data.status |= SILC_IDLIST_STATUS_RESUMED;
4017 detached_client->data.status &= ~SILC_IDLIST_STATUS_LOCAL;
4018 silc_dlist_del(server->expired_clients, detached_client);
4020 /* Check if anyone is watching this client */
4021 if (server->server_type == SILC_ROUTER)
4022 silc_server_check_watcher_list(server, detached_client, NULL,
4023 SILC_NOTIFY_TYPE_UMODE_CHANGE);
4025 silc_schedule_task_del_by_context(server->schedule, detached_client);
4027 /* Get the new owner of the resumed client */
4028 if (!silc_id_str2id(packet->src_id, packet->src_id_len,
4029 packet->src_id_type, &server_id, sizeof(server_id)))
4032 /* Get server entry */
4033 server_entry = silc_idlist_find_server_by_id(server->global_list,
4034 &server_id, TRUE, NULL);
4036 if (!server_entry) {
4037 server_entry = silc_idlist_find_server_by_id(server->local_list,
4038 &server_id, TRUE, NULL);
4044 if (server->server_type == SILC_ROUTER &&
4045 idata->conn_type == SILC_CONN_ROUTER &&
4046 server_entry->server_type == SILC_ROUTER)
4049 /* Move entry to correct list */
4050 if (local && server->server_type == SILC_ROUTER) {
4051 if (silc_idcache_find_by_context(server->global_list->clients,
4052 detached_client, &id_cache))
4053 silc_idcache_move(server->global_list->clients,
4054 server->local_list->clients, id_cache);
4056 if (silc_idcache_find_by_context(server->local_list->clients,
4057 detached_client, &id_cache))
4058 silc_idcache_move(server->local_list->clients,
4059 server->global_list->clients, id_cache);
4062 /* We don't own this client anymore, if we ever did, as we were just
4063 * told that someone else resumed it. Thus, it is most definitely no
4064 * a detached client.*/
4065 detached_client->local_detached = FALSE;
4066 /* Change the owner of the client */
4067 detached_client->router = server_entry;
4069 /* Update channel information regarding global clients on channel. */
4070 if (server->server_type != SILC_ROUTER) {
4071 silc_hash_table_list(detached_client->channels, &htl);
4072 while (silc_hash_table_get(&htl, NULL, (void *)&chl))
4073 chl->channel->global_users =
4074 silc_server_channel_has_global(chl->channel);
4075 silc_hash_table_list_reset(&htl);
4080 silc_packet_free(packet);