5 Author: Pekka Riikonen <priikone@silcnet.org>
7 Copyright (C) 1997 - 2005 Pekka Riikonen
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
21 * Server packet routines to handle received packets.
25 #include "serverincludes.h"
26 #include "server_internal.h"
28 /* Received notify packet. Server can receive notify packets from router.
29 Server then relays the notify messages to clients if needed. */
31 void silc_server_notify(SilcServer server,
32 SilcSocketConnection sock,
33 SilcPacketContext *packet)
35 SilcNotifyPayload payload;
37 SilcArgumentPayload args;
38 SilcChannelID *channel_id = NULL, *channel_id2;
39 SilcClientID *client_id, *client_id2;
40 SilcServerID *server_id;
42 SilcChannelEntry channel = NULL;
43 SilcClientEntry client = NULL, client2 = NULL;
44 SilcServerEntry server_entry = NULL;
45 SilcChannelClientEntry chl;
46 SilcIDCacheEntry cache = NULL;
47 SilcHashTableList htl;
53 if (sock->type == SILC_SOCKET_TYPE_CLIENT ||
54 packet->src_id_type != SILC_ID_SERVER || !packet->dst_id) {
55 SILC_LOG_DEBUG(("Bad notify packet received"));
59 /* If the packet is destined directly to a client then relay the packet
60 before processing it. */
61 if (packet->dst_id_type == SILC_ID_CLIENT) {
63 SilcSocketConnection dst_sock;
65 /* Get the route to the client */
66 dst_sock = silc_server_get_client_route(server, packet->dst_id,
67 packet->dst_id_len, NULL,
70 /* Relay the packet */
71 silc_server_relay_packet(server, dst_sock, idata->send_key,
72 idata->hmac_send, idata->psn_send++,
76 /* Parse the Notify Payload */
77 payload = silc_notify_payload_parse(packet->buffer->data,
82 /* If we are router and this packet is not already broadcast packet
83 we will broadcast it. The sending socket really cannot be router or
84 the router is buggy. If this packet is coming from router then it must
85 have the broadcast flag set already and we won't do anything. */
86 if (server->server_type == SILC_ROUTER &&
87 sock->type == SILC_SOCKET_TYPE_SERVER &&
88 !(packet->flags & SILC_PACKET_FLAG_BROADCAST)) {
89 SILC_LOG_DEBUG(("Broadcasting received Notify packet"));
90 if (packet->dst_id_type == SILC_ID_CHANNEL) {
91 /* Packet is destined to channel */
92 channel_id = silc_id_str2id(packet->dst_id, packet->dst_id_len,
97 silc_server_packet_send_dest(server, SILC_PRIMARY_ROUTE(server),
98 packet->type, packet->flags |
99 SILC_PACKET_FLAG_BROADCAST,
100 channel_id, SILC_ID_CHANNEL,
101 packet->buffer->data,
102 packet->buffer->len, FALSE);
103 silc_server_backup_send_dest(server, sock->user_data,
104 packet->type, packet->flags,
105 channel_id, SILC_ID_CHANNEL,
106 packet->buffer->data, packet->buffer->len,
109 /* Packet is destined to client or server */
110 silc_server_packet_send(server, SILC_PRIMARY_ROUTE(server),
112 packet->flags | SILC_PACKET_FLAG_BROADCAST,
113 packet->buffer->data, packet->buffer->len,
115 silc_server_backup_send(server, sock->user_data,
116 packet->type, packet->flags,
117 packet->buffer->data, packet->buffer->len,
122 type = silc_notify_get_type(payload);
123 args = silc_notify_get_args(payload);
128 case SILC_NOTIFY_TYPE_JOIN:
130 * Distribute the notify to local clients on the channel
132 SILC_LOG_DEBUG(("JOIN notify"));
135 silc_free(channel_id);
138 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
141 channel_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
145 /* Get channel entry */
146 channel = silc_idlist_find_channel_by_id(server->global_list,
149 channel = silc_idlist_find_channel_by_id(server->local_list,
152 SILC_LOG_DEBUG(("Notify for unknown channel"));
153 silc_free(channel_id);
157 silc_free(channel_id);
160 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
163 client_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
167 /* If the the client is not in local list we check global list (ie. the
168 channel will be global channel) and if it does not exist then create
169 entry for the client. */
170 client = silc_idlist_find_client_by_id(server->global_list,
171 client_id, server->server_type,
174 client = silc_idlist_find_client_by_id(server->local_list,
175 client_id, server->server_type,
178 /* If router did not find the client the it is bogus */
179 if (server->server_type != SILC_SERVER) {
180 silc_free(client_id);
185 silc_idlist_add_client(server->global_list, NULL, NULL, NULL,
186 silc_id_dup(client_id, SILC_ID_CLIENT),
187 sock->user_data, NULL, 0);
189 SILC_LOG_ERROR(("Could not add new client to the ID Cache"));
190 silc_free(client_id);
194 client->data.status |= SILC_IDLIST_STATUS_REGISTERED;
197 silc_free(client_id);
199 /* Do not process the notify if the client is not registered */
200 if (!(client->data.status & SILC_IDLIST_STATUS_REGISTERED))
203 /* Do not add client to channel if it is there already */
204 if (silc_server_client_on_channel(client, channel, NULL)) {
205 SILC_LOG_DEBUG(("Client already on channel %s",
206 channel->channel_name));
210 /* Send to channel */
211 silc_server_packet_send_to_channel(server, sock, channel, packet->type,
212 FALSE, TRUE, packet->buffer->data,
213 packet->buffer->len, FALSE);
215 if (server->server_type != SILC_ROUTER &&
216 sock->type == SILC_SOCKET_TYPE_ROUTER)
217 /* The channel is global now */
218 channel->global_users = TRUE;
220 SILC_LOG_DEBUG(("Joining to channel %s", channel->channel_name));
222 /* JOIN the global client to the channel (local clients (if router
223 created the channel) is joined in the pending JOIN command). */
224 chl = silc_calloc(1, sizeof(*chl));
225 chl->client = client;
226 chl->channel = channel;
228 if (server->server_type != SILC_ROUTER ||
229 sock->type == SILC_SOCKET_TYPE_ROUTER) {
230 /* If this is the first one on the channel then it is the founder of
231 the channel. This is done on normal server and on router if this
232 notify is coming from router */
233 if (!silc_hash_table_count(channel->user_list)) {
234 SILC_LOG_DEBUG(("Client %s is founder on channel",
235 silc_id_render(chl->client->id, SILC_ID_CLIENT)));
236 chl->mode = (SILC_CHANNEL_UMODE_CHANOP | SILC_CHANNEL_UMODE_CHANFO);
240 silc_hash_table_add(channel->user_list, client, chl);
241 silc_hash_table_add(client->channels, channel, chl);
242 channel->user_count++;
243 channel->disabled = FALSE;
245 /* Make sure we don't expire clients that are on channel */
249 /* Update statistics */
250 if (server->server_type == SILC_ROUTER) {
251 if (sock->type != SILC_SOCKET_TYPE_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"));
265 channel_id = silc_id_str2id(packet->dst_id, packet->dst_id_len,
266 packet->dst_id_type);
271 /* Get channel entry */
272 channel = silc_idlist_find_channel_by_id(server->global_list,
275 channel = silc_idlist_find_channel_by_id(server->local_list,
278 SILC_LOG_DEBUG(("Notify for unknown channel"));
279 silc_free(channel_id);
285 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
287 silc_free(channel_id);
290 client_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
292 silc_free(channel_id);
296 /* Get client entry */
297 client = silc_idlist_find_client_by_id(server->global_list,
298 client_id, TRUE, NULL);
300 client = silc_idlist_find_client_by_id(server->local_list,
301 client_id, TRUE, NULL);
303 silc_free(client_id);
304 silc_free(channel_id);
308 silc_free(client_id);
309 silc_free(channel_id);
311 /* Check if on channel */
312 if (!silc_server_client_on_channel(client, channel, NULL))
315 /* Send the leave notify to channel */
316 silc_server_packet_send_to_channel(server, sock, channel, packet->type,
317 FALSE, TRUE, packet->buffer->data,
318 packet->buffer->len, FALSE);
320 /* Remove the user from channel */
321 silc_server_remove_from_one_channel(server, sock, channel, client, FALSE);
324 case SILC_NOTIFY_TYPE_SIGNOFF:
326 * Distribute the notify to local clients on the channel
328 SILC_LOG_DEBUG(("SIGNOFF notify"));
331 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
334 client_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
338 /* Get client entry */
339 client = silc_idlist_find_client_by_id(server->global_list,
340 client_id, TRUE, &cache);
342 client = silc_idlist_find_client_by_id(server->local_list,
343 client_id, TRUE, &cache);
345 silc_free(client_id);
349 silc_free(client_id);
351 /* Get signoff message */
352 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
356 /* Update statistics */
357 server->stat.clients--;
358 if (server->stat.cell_clients)
359 server->stat.cell_clients--;
360 SILC_OPER_STATS_UPDATE(client, server, SILC_UMODE_SERVER_OPERATOR);
361 SILC_OPER_STATS_UPDATE(client, router, SILC_UMODE_ROUTER_OPERATOR);
362 silc_schedule_task_del_by_context(server->schedule, client);
364 /* Remove from public key hash table. */
365 if (client->data.public_key)
366 silc_hash_table_del_by_context(server->pk_hash, client->data.public_key,
369 /* Remove the client from all channels. */
370 silc_server_remove_from_channels(server, NULL, client, TRUE,
373 /* Check if anyone is watching this nickname */
374 if (server->server_type == SILC_ROUTER)
375 silc_server_check_watcher_list(server, client, NULL,
376 SILC_NOTIFY_TYPE_SIGNOFF);
378 /* Remove this client from watcher list if it is */
379 silc_server_del_from_watcher_list(server, client);
381 client->data.status &= ~SILC_IDLIST_STATUS_REGISTERED;
383 client->router = NULL;
384 client->connection = NULL;
385 cache->expire = SILC_ID_CACHE_EXPIRE_DEF;
388 case SILC_NOTIFY_TYPE_TOPIC_SET:
390 * Distribute the notify to local clients on the channel
393 SILC_LOG_DEBUG(("TOPIC SET notify"));
396 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
399 client_id = silc_id_payload_parse_id(tmp, tmp_len, &id_type);
403 /* Get client entry */
404 if (id_type == SILC_ID_CLIENT) {
405 client = silc_idlist_find_client_by_id(server->global_list,
406 client_id, TRUE, &cache);
408 client = silc_idlist_find_client_by_id(server->local_list,
409 client_id, TRUE, &cache);
411 silc_free(client_id);
415 silc_free(client_id);
419 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
421 silc_free(channel_id);
426 channel_id = silc_id_str2id(packet->dst_id, packet->dst_id_len,
427 packet->dst_id_type);
432 /* Get channel entry */
433 channel = silc_idlist_find_channel_by_id(server->global_list,
436 channel = silc_idlist_find_channel_by_id(server->local_list,
439 SILC_LOG_DEBUG(("Notify for unknown channel"));
440 silc_free(channel_id);
444 silc_free(channel_id);
446 if (channel->topic && !strcmp(channel->topic, tmp)) {
447 SILC_LOG_DEBUG(("Topic is already set and same"));
452 /* Get user's channel entry and check that topic set is allowed. */
453 if (!silc_server_client_on_channel(client, channel, &chl))
455 if (channel->mode & SILC_CHANNEL_MODE_TOPIC &&
456 !(chl->mode & SILC_CHANNEL_UMODE_CHANOP) &&
457 !(chl->mode & SILC_CHANNEL_UMODE_CHANFO)) {
458 SILC_LOG_DEBUG(("Topic change is not allowed"));
463 /* Change the topic */
464 silc_free(channel->topic);
465 channel->topic = strdup(tmp);
467 /* Send the same notify to the channel */
468 silc_server_packet_send_to_channel(server, NULL, channel, packet->type,
469 FALSE, TRUE, packet->buffer->data,
470 packet->buffer->len, FALSE);
473 case SILC_NOTIFY_TYPE_NICK_CHANGE:
476 * Distribute the notify to local clients on the channel
478 unsigned char *id, *id2;
480 SilcUInt32 nickname_len;
482 SILC_LOG_DEBUG(("NICK CHANGE notify"));
484 /* Get old client ID */
485 id = silc_argument_get_arg_type(args, 1, &tmp_len);
488 client_id = silc_id_payload_parse_id(id, tmp_len, NULL);
492 /* Get new client ID */
493 id2 = silc_argument_get_arg_type(args, 2, &tmp_len);
496 client_id2 = silc_id_payload_parse_id(id2, tmp_len, NULL);
498 silc_free(client_id);
502 SILC_LOG_DEBUG(("Old Client ID id(%s)",
503 silc_id_render(client_id, SILC_ID_CLIENT)));
504 SILC_LOG_DEBUG(("New Client ID id(%s)",
505 silc_id_render(client_id2, SILC_ID_CLIENT)));
507 /* From protocol version 1.1 we also get the new nickname */
508 nickname = silc_argument_get_arg_type(args, 3, &nickname_len);;
510 /* Replace the Client ID */
511 client = silc_idlist_replace_client_id(server,
512 server->global_list, client_id,
513 client_id2, nickname);
515 client = silc_idlist_replace_client_id(server,
516 server->local_list, client_id,
517 client_id2, nickname);
520 /* Send the NICK_CHANGE notify type to local clients on the channels
521 this client is joined to. */
522 silc_server_send_notify_on_channels(server, client, client,
523 SILC_NOTIFY_TYPE_NICK_CHANGE, 3,
524 id, tmp_len, id2, tmp_len,
529 silc_free(client_id);
531 silc_free(client_id2);
535 case SILC_NOTIFY_TYPE_CMODE_CHANGE:
537 * Distribute the notify to local clients on the channel
540 SILC_LOG_DEBUG(("CMODE CHANGE notify"));
543 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
546 client_id = silc_id_payload_parse_id(tmp, tmp_len, &id_type);
550 /* Get client entry */
551 if (id_type == SILC_ID_CLIENT) {
552 client = silc_idlist_find_client_by_id(server->global_list,
553 client_id, TRUE, &cache);
555 client = silc_idlist_find_client_by_id(server->local_list,
556 client_id, TRUE, &cache);
558 silc_free(client_id);
563 silc_free(client_id);
566 channel_id = silc_id_str2id(packet->dst_id, packet->dst_id_len,
567 packet->dst_id_type);
572 /* Get channel entry */
573 channel = silc_idlist_find_channel_by_id(server->global_list,
576 channel = silc_idlist_find_channel_by_id(server->local_list,
579 SILC_LOG_DEBUG(("Notify for unknown channel"));
580 silc_free(channel_id);
584 silc_free(channel_id);
587 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
590 SILC_GET32_MSB(mode, tmp);
592 /* Check if mode changed */
593 if (channel->mode == mode) {
594 SILC_LOG_DEBUG(("Mode is changed already"));
596 /* If this mode change has founder mode then we'll enforce the
597 change so that the server gets the real founder public key */
598 if (server->server_type != SILC_SERVER &&
599 sock != SILC_PRIMARY_ROUTE(server) &&
600 mode & SILC_CHANNEL_MODE_FOUNDER_AUTH && channel->founder_key) {
601 SILC_LOG_DEBUG(("Sending founder public key to server"));
602 silc_server_send_notify_cmode(server, sock, FALSE, channel,
603 channel->mode, server->id,
604 SILC_ID_SERVER, channel->cipher,
607 channel->founder_key, NULL);
610 /* If we received same mode from our primary check whether founder
611 mode and key in the notify is set. We update the founder key
612 here since we may have wrong one */
613 if (server->server_type == SILC_SERVER &&
614 sock == SILC_PRIMARY_ROUTE(server) &&
615 mode & SILC_CHANNEL_MODE_FOUNDER_AUTH) {
616 SILC_LOG_DEBUG(("Founder public key received from router"));
617 tmp = silc_argument_get_arg_type(args, 6, &tmp_len);
621 if (channel->founder_key)
622 silc_pkcs_public_key_free(channel->founder_key);
623 channel->founder_key = NULL;
624 silc_pkcs_public_key_payload_decode(tmp, tmp_len,
625 &channel->founder_key);
628 /* Check also for channel public key list */
629 if (server->server_type == SILC_SERVER &&
630 sock == SILC_PRIMARY_ROUTE(server) &&
631 mode & SILC_CHANNEL_MODE_CHANNEL_AUTH) {
634 unsigned char mask[4], ulimit[4];
636 SILC_LOG_DEBUG(("Channel public key list received from router"));
637 tmp = silc_argument_get_arg_type(args, 7, &tmp_len);
641 /* Set the router's list, and send the notify to channel too so that
642 channel gets the list */
643 silc_server_set_channel_pk_list(server, sock, channel, tmp, tmp_len);
644 chpklist = silc_server_get_channel_pk_list(server, channel,
648 sidp = silc_id_payload_encode(server->router->id, SILC_ID_SERVER);
649 SILC_PUT32_MSB(channel->mode, mask);
650 if (channel->mode & SILC_CHANNEL_MODE_ULIMIT)
651 SILC_PUT32_MSB(channel->user_limit, ulimit);
652 silc_server_send_notify_to_channel(server, NULL, channel, FALSE, TRUE,
653 SILC_NOTIFY_TYPE_CMODE_CHANGE, 8,
654 sidp->data, sidp->len,
658 strlen(channel->cipher) : 0,
661 strlen(channel->hmac_name) : 0,
663 channel->passphrase ?
664 strlen(channel->passphrase) : 0,
666 chpklist->data, chpklist->len,
668 SILC_CHANNEL_MODE_ULIMIT ?
671 SILC_CHANNEL_MODE_ULIMIT ?
672 sizeof(ulimit) : 0));
673 silc_buffer_free(sidp);
674 silc_buffer_free(chpklist);
681 /* Get user's channel entry and check that mode change is allowed */
683 if (!silc_server_client_on_channel(client, channel, &chl))
685 if (!silc_server_check_cmode_rights(server, channel, chl, mode)) {
686 SILC_LOG_DEBUG(("CMODE change is not allowed"));
687 silc_server_send_notify_cmode(server, sock, FALSE, channel,
688 channel->mode, server->id,
689 SILC_ID_SERVER, channel->cipher,
692 channel->founder_key, NULL);
696 /* Assure that server is not removing founder mode from us */
697 if (server->server_type == SILC_ROUTER &&
698 sock != SILC_PRIMARY_ROUTE(server) &&
699 channel->mode & SILC_CHANNEL_MODE_FOUNDER_AUTH &&
700 !(mode & SILC_CHANNEL_MODE_FOUNDER_AUTH)) {
701 SILC_LOG_DEBUG(("Enforcing sender to change channel mode"));
702 silc_server_send_notify_cmode(server, sock, FALSE, channel,
703 channel->mode, server->id,
704 SILC_ID_SERVER, channel->cipher,
707 channel->founder_key, NULL);
711 /* If server is adding founder mode, check whether there is founder
712 on channel already and is not from this server */
713 if (server->server_type == SILC_ROUTER &&
714 sock != SILC_PRIMARY_ROUTE(server) &&
715 mode & SILC_CHANNEL_MODE_FOUNDER_AUTH) {
716 silc_hash_table_list(channel->user_list, &htl);
717 while (silc_hash_table_get(&htl, NULL, (void *)&chl))
718 if (chl->mode & SILC_CHANNEL_UMODE_CHANFO &&
719 chl->client->router != sock->user_data) {
720 SILC_LOG_DEBUG(("Enforcing sender to change channel mode"));
721 silc_server_send_notify_cmode(server, sock, FALSE, channel,
722 channel->mode, server->id,
723 SILC_ID_SERVER, channel->cipher,
726 channel->founder_key, NULL);
727 silc_hash_table_list_reset(&htl);
730 silc_hash_table_list_reset(&htl);
734 /* If the channel had private keys set and the mode was removed then
735 we must re-generate and re-distribute a new channel key */
736 if (channel->mode & SILC_CHANNEL_MODE_PRIVKEY &&
737 !(mode & SILC_CHANNEL_MODE_PRIVKEY)) {
738 /* Re-generate channel key */
739 if (!silc_server_create_channel_key(server, channel, 0))
742 /* Send the channel key. This sends it to our local clients and if
743 we are normal server to our router as well. */
744 silc_server_send_channel_key(server, NULL, channel,
745 server->server_type == SILC_ROUTER ?
746 FALSE : !server->standalone);
750 tmp = silc_argument_get_arg_type(args, 4, &tmp_len);
752 unsigned char hash[32];
755 silc_hmac_free(channel->hmac);
756 if (!silc_hmac_alloc(tmp, NULL, &channel->hmac))
759 /* Set the HMAC key out of current channel key. The client must do
761 silc_hash_make(silc_hmac_get_hash(channel->hmac), channel->key,
762 channel->key_len / 8, hash);
763 silc_hmac_set_key(channel->hmac, hash,
764 silc_hash_len(silc_hmac_get_hash(channel->hmac)));
765 memset(hash, 0, sizeof(hash));
768 /* Get the passphrase */
769 tmp = silc_argument_get_arg_type(args, 5, &tmp_len);
771 silc_free(channel->passphrase);
772 channel->passphrase = silc_memdup(tmp, tmp_len);
775 /* Get founder public key */
776 tmp = silc_argument_get_arg_type(args, 6, &tmp_len);
777 if (tmp && mode & SILC_CHANNEL_MODE_FOUNDER_AUTH) {
778 if (channel->founder_key)
779 silc_pkcs_public_key_free(channel->founder_key);
780 channel->founder_key = NULL;
781 if (!silc_pkcs_public_key_payload_decode(tmp, tmp_len,
782 &channel->founder_key)) {
783 SILC_LOG_DEBUG(("Enforcing sender to change channel mode"));
784 mode &= ~SILC_CHANNEL_MODE_FOUNDER_AUTH;
785 silc_server_send_notify_cmode(server, sock, FALSE, channel,
786 mode, server->id, SILC_ID_SERVER,
789 channel->passphrase, NULL, NULL);
790 if (channel->founder_key)
791 silc_pkcs_public_key_free(channel->founder_key);
792 channel->founder_key = NULL;
796 if (mode & SILC_CHANNEL_MODE_FOUNDER_AUTH && !channel->founder_key &&
797 server->server_type == SILC_ROUTER) {
798 SILC_LOG_DEBUG(("Enforcing sender to change channel mode"));
799 mode &= ~SILC_CHANNEL_MODE_FOUNDER_AUTH;
800 silc_server_send_notify_cmode(server, sock, FALSE, channel,
801 mode, server->id, SILC_ID_SERVER,
804 channel->passphrase, NULL, NULL);
807 /* Process channel public key(s). */
808 tmp = silc_argument_get_arg_type(args, 7, &tmp_len);
809 if (tmp && mode & SILC_CHANNEL_MODE_CHANNEL_AUTH) {
811 silc_server_set_channel_pk_list(server, sock, channel, tmp, tmp_len);
813 /* If list was set already we will enforce the same list to server. */
814 if (ret == SILC_STATUS_ERR_OPERATION_ALLOWED) {
815 SilcBuffer chpklist = silc_server_get_channel_pk_list(server, channel,
817 silc_server_send_notify_cmode(server, sock, FALSE, channel,
818 mode, server->id, SILC_ID_SERVER,
821 channel->passphrase, NULL,
823 silc_buffer_free(chpklist);
827 /* Get the user limit */
828 tmp = silc_argument_get_arg_type(args, 8, &tmp_len);
829 if (tmp && tmp_len == 4 && mode & SILC_CHANNEL_MODE_ULIMIT)
830 SILC_GET32_MSB(channel->user_limit, tmp);
832 /* Send the same notify to the channel */
833 silc_server_packet_send_to_channel(server, NULL, channel, packet->type,
834 FALSE, TRUE, packet->buffer->data,
835 packet->buffer->len, FALSE);
838 channel->mode = mode;
840 /* Cleanup if some modes are removed */
842 if (!(channel->mode & SILC_CHANNEL_MODE_FOUNDER_AUTH) &&
843 channel->founder_key) {
844 silc_pkcs_public_key_free(channel->founder_key);
845 channel->founder_key = NULL;
848 if (!(channel->mode & SILC_CHANNEL_MODE_CHANNEL_AUTH) &&
849 channel->channel_pubkeys) {
850 silc_hash_table_free(channel->channel_pubkeys);
851 channel->channel_pubkeys = NULL;
856 case SILC_NOTIFY_TYPE_CUMODE_CHANGE:
859 * Distribute the notify to local clients on the channel
861 SilcChannelClientEntry chl2 = NULL;
862 bool notify_sent = FALSE;
864 SILC_LOG_DEBUG(("CUMODE CHANGE notify"));
867 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
870 client_id = silc_id_payload_parse_id(tmp, tmp_len, &id_type);
874 /* Get client entry */
875 if (id_type == SILC_ID_CLIENT) {
876 client = silc_idlist_find_client_by_id(server->global_list,
877 client_id, TRUE, &cache);
879 client = silc_idlist_find_client_by_id(server->local_list,
880 client_id, TRUE, &cache);
882 silc_free(client_id);
887 silc_free(client_id);
890 channel_id = silc_id_str2id(packet->dst_id, packet->dst_id_len,
891 packet->dst_id_type);
896 /* Get channel entry */
897 channel = silc_idlist_find_channel_by_id(server->global_list,
900 channel = silc_idlist_find_channel_by_id(server->local_list,
903 SILC_LOG_DEBUG(("Notify for unknown channel"));
904 silc_free(channel_id);
910 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
912 silc_free(channel_id);
916 SILC_GET32_MSB(mode, tmp);
918 /* Get target client */
919 tmp = silc_argument_get_arg_type(args, 3, &tmp_len);
922 client_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
926 /* Get client entry */
927 client2 = silc_idlist_find_client_by_id(server->global_list,
928 client_id, TRUE, NULL);
930 client2 = silc_idlist_find_client_by_id(server->local_list,
931 client_id, TRUE, NULL);
933 silc_free(client_id);
937 silc_free(client_id);
940 /* Check that sender is on channel */
941 if (!silc_server_client_on_channel(client, channel, &chl))
944 if (client != client2 && server->server_type == SILC_ROUTER) {
945 /* Sender must be operator */
946 if (!(chl->mode & SILC_CHANNEL_UMODE_CHANOP) &&
947 !(chl->mode & SILC_CHANNEL_UMODE_CHANFO)) {
948 SILC_LOG_DEBUG(("CUMODE change is not allowed"));
952 if (!silc_server_client_on_channel(client2, channel, &chl))
955 /* If target is founder mode change is not allowed. */
956 if (chl->mode & SILC_CHANNEL_UMODE_CHANFO) {
957 SILC_LOG_DEBUG(("CUMODE change is not allowed"));
963 /* Get target channel user entry */
964 if (!silc_server_client_on_channel(client2, channel, &chl))
967 if (server->server_type == SILC_SERVER && chl->mode == mode) {
968 SILC_LOG_DEBUG(("Mode is changed already"));
972 /* Check whether to give founder rights to this user or not. The
973 problem here is that we get only the public key of the client,
974 but no authentication data. We must assume that server has
975 already authenticated the user (and thus we must trust the
977 if (mode & SILC_CHANNEL_UMODE_CHANFO &&
978 !(chl->mode & SILC_CHANNEL_UMODE_CHANFO) &&
979 server->server_type == SILC_ROUTER &&
980 sock != SILC_PRIMARY_ROUTE(server)) {
981 SilcPublicKey founder_key = NULL;
983 /* If channel doesn't have founder auth mode then it's impossible
984 that someone would be getting founder rights with CUMODE command.
985 In that case there already either is founder or there isn't
986 founder at all on the channel (valid only when 'client' is
988 if (client && !(channel->mode & SILC_CHANNEL_MODE_FOUNDER_AUTH)) {
989 /* Force the mode to not have founder mode */
990 chl->mode = mode &= ~SILC_CHANNEL_UMODE_CHANFO;
991 silc_server_force_cumode_change(server, sock, channel, chl, mode);
996 /* Get the founder of the channel and if found then this client
997 cannot be the founder since there already is one. */
998 silc_hash_table_list(channel->user_list, &htl);
999 while (silc_hash_table_get(&htl, NULL, (void *)&chl2))
1000 if (chl2->mode & SILC_CHANNEL_UMODE_CHANFO) {
1001 SILC_LOG_DEBUG(("Founder already on channel"));
1002 chl->mode = mode &= ~SILC_CHANNEL_UMODE_CHANFO;
1003 silc_server_force_cumode_change(server, sock, channel,
1008 silc_hash_table_list_reset(&htl);
1009 if (!(mode & SILC_CHANNEL_UMODE_CHANFO))
1012 /* Founder not found on the channel. Since the founder auth mode
1013 is set on the channel now check whether this is the client that
1014 originally set the mode. */
1016 if (channel->founder_key) {
1017 /* Get public key that must be present in notify */
1018 tmp = silc_argument_get_arg_type(args, 4, &tmp_len);
1019 if (!tmp || !silc_pkcs_public_key_payload_decode(tmp, tmp_len,
1021 chl->mode = mode &= ~SILC_CHANNEL_UMODE_CHANFO;
1022 SILC_LOG_DEBUG(("Founder public key not present"));
1023 silc_server_force_cumode_change(server, sock, channel, chl, mode);
1028 /* Now match the public key we have cached and public key sent.
1030 if (!silc_pkcs_public_key_compare(channel->founder_key,
1032 chl->mode = mode &= ~SILC_CHANNEL_UMODE_CHANFO;
1033 SILC_LOG_DEBUG(("Founder public key mismatch"));
1034 silc_server_force_cumode_change(server, sock, channel, chl, mode);
1040 /* There cannot be anyone else as founder on the channel now. This
1041 client is definitely the founder due to this 'authentication'.
1042 We trust the server did the actual signature verification
1043 earlier (bad, yes). */
1044 silc_hash_table_list(channel->user_list, &htl);
1045 while (silc_hash_table_get(&htl, NULL, (void *)&chl2))
1046 if (chl2->mode & SILC_CHANNEL_UMODE_CHANFO) {
1047 chl2->mode &= ~SILC_CHANNEL_UMODE_CHANFO;
1048 SILC_LOG_DEBUG(("Removing old founder rights, new authenticated"));
1049 silc_server_force_cumode_change(server, NULL, channel, chl2,
1053 silc_hash_table_list_reset(&htl);
1056 silc_pkcs_public_key_free(founder_key);
1059 if (server->server_type != SILC_SERVER && chl->mode == mode) {
1060 SILC_LOG_DEBUG(("Mode is changed already"));
1064 SILC_LOG_DEBUG(("Changing %s channel user mode",
1065 chl->client->nickname ? chl->client->nickname :
1066 (unsigned char *)""));
1068 /* Change the mode */
1071 /* Send the same notify to the channel */
1073 silc_server_packet_send_to_channel(server, NULL, channel,
1075 FALSE, TRUE, packet->buffer->data,
1076 packet->buffer->len, FALSE);
1078 silc_free(channel_id);
1082 case SILC_NOTIFY_TYPE_INVITE:
1084 if (packet->dst_id_type == SILC_ID_CLIENT)
1087 SILC_LOG_DEBUG(("INVITE notify"));
1089 /* Get Channel ID */
1090 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
1093 channel_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
1097 /* Get channel entry */
1098 channel = silc_idlist_find_channel_by_id(server->global_list,
1101 channel = silc_idlist_find_channel_by_id(server->local_list,
1104 SILC_LOG_DEBUG(("Notify for unknown channel"));
1105 silc_free(channel_id);
1109 silc_free(channel_id);
1111 #if 0 /* These aren't actually used anywhere or needed, since this
1112 notify is for handling the invite list (direct invite
1113 goes to client and is not handled here at all). */
1116 tmp = silc_argument_get_arg_type(args, 3, &tmp_len);
1119 client_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
1123 /* Get client entry */
1124 client = silc_idlist_find_client_by_id(server->global_list,
1125 client_id, TRUE, &cache);
1127 client = silc_idlist_find_client_by_id(server->local_list,
1128 client_id, TRUE, &cache);
1130 silc_free(client_id);
1134 silc_free(client_id);
1136 /* Get user's channel entry and check that inviting is allowed. */
1137 if (server->server_type == SILC_ROUTER) {
1138 if (!silc_server_client_on_channel(client, channel, &chl))
1140 if (channel->mode & SILC_CHANNEL_MODE_INVITE &&
1141 !(chl->mode & SILC_CHANNEL_UMODE_CHANOP) &&
1142 !(chl->mode & SILC_CHANNEL_UMODE_CHANFO)) {
1143 SILC_LOG_DEBUG(("Inviting is not allowed"));
1149 /* Get the invite action */
1150 tmp = silc_argument_get_arg_type(args, 4, &tmp_len);
1151 if (tmp && tmp_len == 1) {
1152 SilcUInt8 action = (SilcUInt8)tmp[0];
1153 SilcUInt16 iargc = 0;
1154 SilcArgumentPayload iargs;
1156 /* Get invite list */
1157 tmp = silc_argument_get_arg_type(args, 5, &tmp_len);
1158 if (!tmp || tmp_len < 2)
1161 /* Parse the arguments to see they are constructed correctly */
1162 SILC_GET16_MSB(iargc, tmp);
1163 iargs = silc_argument_payload_parse(tmp + 2, tmp_len - 2, iargc);
1167 if (action != 0x01 && !channel->invite_list)
1168 channel->invite_list =
1169 silc_hash_table_alloc(0, silc_hash_ptr,
1171 silc_server_inviteban_destruct, channel, TRUE);
1173 /* Proces the invite action */
1174 silc_server_inviteban_process(server, channel->invite_list, action,
1176 silc_argument_payload_free(iargs);
1178 /* If we are router we must send this notify to our local servers on
1179 the channel. Normal server does nothing. The notify is not
1181 if (server->server_type == SILC_ROUTER)
1182 silc_server_packet_send_to_channel(server, sock, channel,
1183 packet->type, FALSE, FALSE,
1184 packet->buffer->data,
1185 packet->buffer->len, FALSE);
1190 case SILC_NOTIFY_TYPE_CHANNEL_CHANGE:
1192 * Distribute to the local clients on the channel and change the
1196 SILC_LOG_DEBUG(("CHANNEL CHANGE"));
1198 if (sock->type != SILC_SOCKET_TYPE_ROUTER)
1201 /* Get the old Channel ID */
1202 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
1205 channel_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
1209 /* Get the channel entry */
1210 channel = silc_idlist_find_channel_by_id(server->local_list,
1213 channel = silc_idlist_find_channel_by_id(server->global_list,
1216 SILC_LOG_DEBUG(("Notify for unknown channel"));
1217 silc_free(channel_id);
1222 /* Send the notify to the channel */
1223 silc_server_packet_send_to_channel(server, sock, channel, packet->type,
1224 FALSE, TRUE, packet->buffer->data,
1225 packet->buffer->len, FALSE);
1227 /* Get the new Channel ID */
1228 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
1231 channel_id2 = silc_id_payload_parse_id(tmp, tmp_len, NULL);
1235 SILC_LOG_DEBUG(("Old Channel ID id(%s)",
1236 silc_id_render(channel_id, SILC_ID_CHANNEL)));
1237 SILC_LOG_DEBUG(("New Channel ID id(%s)",
1238 silc_id_render(channel_id2, SILC_ID_CHANNEL)));
1240 /* Replace the Channel ID */
1241 if (!silc_idlist_replace_channel_id(server->local_list, channel_id,
1243 if (!silc_idlist_replace_channel_id(server->global_list, channel_id,
1245 silc_free(channel_id2);
1250 SilcBuffer modes = NULL, users = NULL, users_modes = NULL;
1252 /* Re-announce this channel which ID was changed. */
1253 silc_server_send_new_channel(server, sock, FALSE, channel->channel_name,
1255 silc_id_get_len(channel->id,
1259 /* Re-announce our clients on the channel as the ID has changed now */
1260 silc_server_announce_get_channel_users(server, channel, &modes, &users,
1263 silc_buffer_push(users, users->data - users->head);
1264 silc_server_packet_send(server, sock,
1265 SILC_PACKET_NOTIFY, SILC_PACKET_FLAG_LIST,
1266 users->data, users->len, FALSE);
1267 silc_buffer_free(users);
1270 silc_buffer_push(modes, modes->data - modes->head);
1271 silc_server_packet_send_dest(server, sock,
1272 SILC_PACKET_NOTIFY, SILC_PACKET_FLAG_LIST,
1273 channel->id, SILC_ID_CHANNEL,
1274 modes->data, modes->len, FALSE);
1275 silc_buffer_free(modes);
1278 silc_buffer_push(users_modes, users_modes->data - users_modes->head);
1279 silc_server_packet_send_dest(server, sock,
1280 SILC_PACKET_NOTIFY, SILC_PACKET_FLAG_LIST,
1281 channel->id, SILC_ID_CHANNEL,
1283 users_modes->len, FALSE);
1284 silc_buffer_free(users_modes);
1287 /* Re-announce channel's topic */
1288 if (channel->topic) {
1289 silc_server_send_notify_topic_set(server, sock,
1290 server->server_type == SILC_ROUTER ?
1291 TRUE : FALSE, channel,
1292 server->id, SILC_ID_SERVER,
1297 silc_free(channel_id);
1301 case SILC_NOTIFY_TYPE_SERVER_SIGNOFF:
1303 * Remove the server entry and all clients that this server owns.
1306 SILC_LOG_DEBUG(("SERVER SIGNOFF notify"));
1308 /* Backup router shouldn't accept SERVER_SIGNOFF's from normal routers
1309 when the backup isn't acting as primary router. */
1310 if (sock->type == SILC_SOCKET_TYPE_SERVER &&
1311 server->backup_router && server->server_type == SILC_BACKUP_ROUTER)
1315 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
1318 server_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
1322 /* If the ID is mine, this notify is not allowed. */
1323 if (SILC_ID_SERVER_COMPARE(server_id, server->id)) {
1324 SILC_LOG_DEBUG(("Ignoring my own ID for SERVER_SIGNOFF"));
1328 /* Get server entry */
1329 server_entry = silc_idlist_find_server_by_id(server->global_list,
1330 server_id, TRUE, NULL);
1332 if (!server_entry) {
1333 server_entry = silc_idlist_find_server_by_id(server->local_list,
1334 server_id, TRUE, NULL);
1336 if (!server_entry) {
1337 /* If we are normal server then we might not have the server. Check
1338 whether router was kind enough to send the list of all clients
1339 that actually was to be removed. Remove them if the list is
1341 if (server->server_type != SILC_ROUTER &&
1342 silc_argument_get_arg_num(args) > 1) {
1345 for (i = 1; i < silc_argument_get_arg_num(args); i++) {
1347 tmp = silc_argument_get_arg_type(args, i + 1, &tmp_len);
1350 client_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
1354 /* Get client entry */
1355 client = silc_idlist_find_client_by_id(server->global_list,
1356 client_id, TRUE, &cache);
1359 client = silc_idlist_find_client_by_id(server->local_list,
1360 client_id, TRUE, &cache);
1363 silc_free(client_id);
1367 silc_free(client_id);
1369 /* Update statistics */
1370 server->stat.clients--;
1371 if (server->stat.cell_clients)
1372 server->stat.cell_clients--;
1373 SILC_OPER_STATS_UPDATE(client, server, SILC_UMODE_SERVER_OPERATOR);
1374 SILC_OPER_STATS_UPDATE(client, router, SILC_UMODE_ROUTER_OPERATOR);
1376 /* Remove the client from all channels. */
1377 silc_server_remove_from_channels(server, NULL, client,
1378 TRUE, NULL, FALSE, FALSE);
1380 /* Check if anyone is watching this nickname */
1381 if (server->server_type == SILC_ROUTER)
1382 silc_server_check_watcher_list(server, client, NULL,
1383 SILC_NOTIFY_TYPE_SERVER_SIGNOFF);
1385 /* Remove this client from watcher list if it is */
1387 silc_server_del_from_watcher_list(server, client);
1389 /* Remove from public key hash table. */
1390 if (client->data.public_key)
1391 silc_hash_table_del_by_context(server->pk_hash,
1392 client->data.public_key,
1395 /* Remove the client */
1396 silc_idlist_del_data(client);
1397 silc_idlist_del_client(local ? server->local_list :
1398 server->global_list, client);
1402 silc_free(server_id);
1406 silc_free(server_id);
1408 /* For local entrys SERVER_SIGNOFF is processed only on backup router.
1409 It is possible that router sends server signoff for a server. If
1410 backup router has it as local connection it will be closed. */
1411 if (SILC_IS_LOCAL(server_entry)) {
1412 if (server->server_type == SILC_BACKUP_ROUTER) {
1413 sock = server_entry->connection;
1414 SILC_LOG_DEBUG(("Closing connection %s after SERVER_SIGNOFF",
1416 if (sock->user_data)
1417 silc_server_free_sock_user_data(server, sock, NULL);
1418 SILC_SET_DISCONNECTING(sock);
1419 silc_server_close_connection(server, sock);
1425 /* Remove all servers that are originated from this server, and
1426 remove the clients of those servers too. */
1427 silc_server_remove_servers_by_server(server, server_entry, TRUE);
1429 /* Remove the clients that this server owns as they will become
1431 silc_server_remove_clients_by_server(server, server_entry->router,
1432 server_entry, TRUE);
1433 silc_server_backup_del(server, server_entry);
1435 /* Remove the server entry */
1436 silc_idlist_del_server(local ? server->local_list :
1437 server->global_list, server_entry);
1439 /* Update statistics */
1440 if (server->server_type == SILC_ROUTER)
1441 server->stat.servers--;
1445 case SILC_NOTIFY_TYPE_KICKED:
1447 * Distribute the notify to local clients on the channel
1450 SILC_LOG_DEBUG(("KICKED notify"));
1453 channel_id = silc_id_str2id(packet->dst_id, packet->dst_id_len,
1454 packet->dst_id_type);
1459 /* Get channel entry */
1460 channel = silc_idlist_find_channel_by_id(server->global_list,
1463 channel = silc_idlist_find_channel_by_id(server->local_list,
1466 SILC_LOG_DEBUG(("Notify for unknown channel"));
1467 silc_free(channel_id);
1471 silc_free(channel_id);
1474 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
1477 client_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
1481 /* If the the client is not in local list we check global list */
1482 client = silc_idlist_find_client_by_id(server->global_list,
1483 client_id, TRUE, NULL);
1485 client = silc_idlist_find_client_by_id(server->local_list,
1486 client_id, TRUE, NULL);
1488 silc_free(client_id);
1492 silc_free(client_id);
1494 /* If target is founder they cannot be kicked */
1495 if (!silc_server_client_on_channel(client, channel, &chl))
1497 if (chl->mode & SILC_CHANNEL_UMODE_CHANFO)
1500 /* Get the kicker's Client ID */
1501 tmp = silc_argument_get_arg_type(args, 3, &tmp_len);
1504 client_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
1508 /* If the the client is not in local list we check global list */
1509 client2 = silc_idlist_find_client_by_id(server->global_list,
1510 client_id, TRUE, NULL);
1512 client2 = silc_idlist_find_client_by_id(server->local_list,
1513 client_id, TRUE, NULL);
1515 silc_free(client_id);
1519 silc_free(client_id);
1521 /* Kicker must be operator on channel */
1522 if (!silc_server_client_on_channel(client2, channel, &chl))
1524 if (!(chl->mode & SILC_CHANNEL_UMODE_CHANOP) &&
1525 !(chl->mode & SILC_CHANNEL_UMODE_CHANFO)) {
1526 SILC_LOG_DEBUG(("Kicking is not allowed"));
1530 /* Send to channel */
1531 silc_server_packet_send_to_channel(server, sock, channel, packet->type,
1532 FALSE, TRUE, packet->buffer->data,
1533 packet->buffer->len, FALSE);
1535 /* Remove the client from channel's invite list */
1536 if (channel->invite_list && silc_hash_table_count(channel->invite_list)) {
1538 SilcArgumentPayload iargs;
1539 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
1540 ab = silc_argument_payload_encode_one(NULL, tmp, tmp_len, 3);
1541 iargs = silc_argument_payload_parse(ab->data, ab->len, 1);
1542 silc_server_inviteban_process(server, channel->invite_list, 1, iargs);
1543 silc_buffer_free(ab);
1544 silc_argument_payload_free(iargs);
1547 /* Remove the client from channel */
1548 silc_server_remove_from_one_channel(server, sock, channel, client, FALSE);
1552 case SILC_NOTIFY_TYPE_KILLED:
1555 * Distribute the notify to local clients on channels
1557 unsigned char *id, *comment;
1558 SilcUInt32 id_len, comment_len;
1560 SILC_LOG_DEBUG(("KILLED notify"));
1563 id = silc_argument_get_arg_type(args, 1, &id_len);
1566 client_id = silc_id_payload_parse_id(id, id_len, NULL);
1570 /* If the the client is not in local list we check global list */
1571 client = silc_idlist_find_client_by_id(server->global_list,
1572 client_id, TRUE, &cache);
1574 client = silc_idlist_find_client_by_id(server->local_list,
1575 client_id, TRUE, &cache);
1577 silc_free(client_id);
1581 silc_free(client_id);
1583 /* If the client is one of ours, then close the connection to the
1584 client now. This removes the client from all channels as well. */
1585 if (packet->dst_id_type == SILC_ID_CLIENT && client->connection) {
1586 sock = client->connection;
1587 silc_server_free_client_data(server, NULL, client, FALSE, NULL);
1588 silc_server_close_connection(server, sock);
1593 comment = silc_argument_get_arg_type(args, 2, &comment_len);
1594 if (comment_len > 128)
1597 /* Get the killer's Client ID */
1598 tmp = silc_argument_get_arg_type(args, 3, &tmp_len);
1601 client_id = silc_id_payload_parse_id(tmp, tmp_len, &id_type);
1605 if (id_type == SILC_ID_CLIENT) {
1606 /* If the the client is not in local list we check global list */
1607 client2 = silc_idlist_find_client_by_id(server->global_list,
1608 client_id, TRUE, NULL);
1610 client2 = silc_idlist_find_client_by_id(server->local_list,
1611 client_id, TRUE, NULL);
1613 silc_free(client_id);
1617 silc_free(client_id);
1619 /* Killer must be router operator */
1620 if (server->server_type != SILC_SERVER &&
1621 !(client2->mode & SILC_UMODE_ROUTER_OPERATOR)) {
1622 SILC_LOG_DEBUG(("Killing is not allowed"));
1627 /* Send the notify to local clients on the channels except to the
1628 client who is killed. */
1629 silc_server_send_notify_on_channels(server, client, client,
1630 SILC_NOTIFY_TYPE_KILLED, 3,
1631 id, id_len, comment, comment_len,
1634 /* Remove the client from all channels */
1635 silc_server_remove_from_channels(server, NULL, client, FALSE, NULL,
1638 /* Check if anyone is watching this nickname */
1639 silc_server_check_watcher_list(server, client, NULL,
1640 SILC_NOTIFY_TYPE_KILLED);
1642 /* Remove from public key hash table. */
1643 if (client->data.public_key)
1644 silc_hash_table_del_by_context(server->pk_hash,
1645 client->data.public_key,
1648 /* Update statistics */
1649 server->stat.clients--;
1650 if (server->stat.cell_clients)
1651 server->stat.cell_clients--;
1652 SILC_OPER_STATS_UPDATE(client, server, SILC_UMODE_SERVER_OPERATOR);
1653 SILC_OPER_STATS_UPDATE(client, router, SILC_UMODE_ROUTER_OPERATOR);
1655 if (SILC_IS_LOCAL(client)) {
1656 server->stat.my_clients--;
1657 silc_schedule_task_del_by_context(server->schedule, client);
1658 silc_idlist_del_data(client);
1662 client->data.status &= ~SILC_IDLIST_STATUS_REGISTERED;
1664 client->router = NULL;
1665 client->connection = NULL;
1666 cache->expire = SILC_ID_CACHE_EXPIRE_DEF;
1670 case SILC_NOTIFY_TYPE_UMODE_CHANGE:
1672 * Save the mode of the client.
1675 SILC_LOG_DEBUG(("UMODE_CHANGE notify"));
1678 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
1681 client_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
1685 /* Get client entry */
1686 client = silc_idlist_find_client_by_id(server->global_list,
1687 client_id, TRUE, NULL);
1689 client = silc_idlist_find_client_by_id(server->local_list,
1690 client_id, TRUE, NULL);
1692 silc_free(client_id);
1696 silc_free(client_id);
1699 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
1702 SILC_GET32_MSB(mode, tmp);
1704 /* Remove internal resumed flag if client is marked detached now */
1705 if (mode & SILC_UMODE_DETACHED)
1706 client->data.status &= ~SILC_IDLIST_STATUS_RESUMED;
1708 /* Update statistics */
1709 if (server->server_type == SILC_ROUTER) {
1710 if (mode & SILC_UMODE_GONE) {
1711 if (!(client->mode & SILC_UMODE_GONE))
1712 server->stat.aways++;
1714 if (client->mode & SILC_UMODE_GONE)
1715 server->stat.aways--;
1717 if (mode & SILC_UMODE_DETACHED) {
1718 if (!(client->mode & SILC_UMODE_DETACHED))
1719 server->stat.detached++;
1721 if (client->mode & SILC_UMODE_DETACHED)
1722 server->stat.detached--;
1725 SILC_UMODE_STATS_UPDATE(server, SILC_UMODE_SERVER_OPERATOR);
1726 SILC_UMODE_STATS_UPDATE(router, SILC_UMODE_ROUTER_OPERATOR);
1728 /* Change the mode */
1729 client->mode = mode;
1731 /* Check if anyone is watching this nickname */
1732 if (server->server_type == SILC_ROUTER)
1733 silc_server_check_watcher_list(server, client, NULL,
1734 SILC_NOTIFY_TYPE_UMODE_CHANGE);
1738 case SILC_NOTIFY_TYPE_BAN:
1743 SILC_LOG_DEBUG(("BAN notify"));
1745 /* Get Channel ID */
1746 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
1749 channel_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
1753 /* Get channel entry */
1754 channel = silc_idlist_find_channel_by_id(server->global_list,
1757 channel = silc_idlist_find_channel_by_id(server->local_list,
1760 SILC_LOG_DEBUG(("Notify for unknown channel"));
1761 silc_free(channel_id);
1765 silc_free(channel_id);
1767 /* Get the ban action */
1768 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
1769 if (tmp && tmp_len == 1) {
1770 SilcUInt8 action = (SilcUInt8)tmp[0];
1771 SilcUInt16 iargc = 0;
1772 SilcArgumentPayload iargs;
1775 tmp = silc_argument_get_arg_type(args, 3, &tmp_len);
1776 if (!tmp || tmp_len < 2)
1779 /* Parse the arguments to see they are constructed correctly */
1780 SILC_GET16_MSB(iargc, tmp);
1781 iargs = silc_argument_payload_parse(tmp + 2, tmp_len - 2, iargc);
1785 if (action != 0x01 && !channel->ban_list)
1787 silc_hash_table_alloc(0, silc_hash_ptr,
1789 silc_server_inviteban_destruct, channel, TRUE);
1791 /* Proces the ban action */
1792 silc_server_inviteban_process(server, channel->ban_list, action,
1794 silc_argument_payload_free(iargs);
1796 /* If we are router we must send this notify to our local servers on
1797 the channel. Normal server does nothing. The notify is not
1799 if (server->server_type == SILC_ROUTER)
1800 silc_server_packet_send_to_channel(server, sock, channel,
1801 packet->type, FALSE, FALSE,
1802 packet->buffer->data,
1803 packet->buffer->len, FALSE);
1807 case SILC_NOTIFY_TYPE_ERROR:
1814 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
1815 if (!tmp && tmp_len != 1)
1817 error = (SilcStatus)tmp[0];
1819 SILC_LOG_DEBUG(("ERROR notify (%d)", error));
1821 if (error == SILC_STATUS_ERR_NO_SUCH_CLIENT_ID &&
1822 sock->type == SILC_SOCKET_TYPE_ROUTER) {
1823 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
1825 SILC_LOG_DEBUG(("Received invalid client ID notification, deleting "
1826 "the entry from cache"));
1827 client_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
1830 client = silc_idlist_find_client_by_id(server->global_list,
1831 client_id, FALSE, NULL);
1833 if (client->data.public_key)
1834 silc_hash_table_del_by_context(server->pk_hash,
1835 client->data.public_key,
1838 silc_server_remove_from_channels(server, NULL, client, TRUE,
1840 silc_idlist_del_data(client);
1841 silc_idlist_del_client(server->global_list, client);
1843 silc_free(client_id);
1849 /* Ignore rest of the notify types for now */
1850 case SILC_NOTIFY_TYPE_NONE:
1851 case SILC_NOTIFY_TYPE_MOTD:
1858 silc_notify_payload_free(payload);
1861 void silc_server_notify_list(SilcServer server,
1862 SilcSocketConnection sock,
1863 SilcPacketContext *packet)
1865 SilcPacketContext *new;
1869 SILC_LOG_DEBUG(("Processing Notify List"));
1871 if (sock->type == SILC_SOCKET_TYPE_CLIENT ||
1872 packet->src_id_type != SILC_ID_SERVER)
1875 /* Make copy of the original packet context, except for the actual
1876 data buffer, which we will here now fetch from the original buffer. */
1877 new = silc_packet_context_alloc();
1878 new->type = SILC_PACKET_NOTIFY;
1879 new->flags = packet->flags;
1880 new->src_id = packet->src_id;
1881 new->src_id_len = packet->src_id_len;
1882 new->src_id_type = packet->src_id_type;
1883 new->dst_id = packet->dst_id;
1884 new->dst_id_len = packet->dst_id_len;
1885 new->dst_id_type = packet->dst_id_type;
1887 buffer = silc_buffer_alloc(1024);
1888 new->buffer = buffer;
1890 while (packet->buffer->len) {
1891 SILC_GET16_MSB(len, packet->buffer->data + 2);
1892 if (len > packet->buffer->len)
1895 if (len > buffer->truelen) {
1896 silc_buffer_free(buffer);
1897 buffer = silc_buffer_alloc(1024 + len);
1900 silc_buffer_pull_tail(buffer, len);
1901 silc_buffer_put(buffer, packet->buffer->data, len);
1903 /* Process the Notify */
1904 silc_server_notify(server, sock, new);
1906 silc_buffer_push_tail(buffer, len);
1907 silc_buffer_pull(packet->buffer, len);
1910 silc_buffer_free(buffer);
1914 /* Received private message. This resolves the destination of the message
1915 and sends the packet. This is used by both server and router. If the
1916 destination is our locally connected client this sends the packet to
1917 the client. This may also send the message for further routing if
1918 the destination is not in our server (or router). */
1920 void silc_server_private_message(SilcServer server,
1921 SilcSocketConnection sock,
1922 SilcPacketContext *packet)
1924 SilcSocketConnection dst_sock;
1925 SilcIDListData idata;
1926 SilcClientEntry client;
1928 SILC_LOG_DEBUG(("Start"));
1930 if (packet->src_id_type != SILC_ID_CLIENT ||
1931 packet->dst_id_type != SILC_ID_CLIENT || !packet->dst_id)
1934 /* Get the route to the client */
1935 dst_sock = silc_server_get_client_route(server, packet->dst_id,
1936 packet->dst_id_len, NULL,
1940 unsigned char error;
1942 if (client && client->mode & SILC_UMODE_DETACHED) {
1943 SILC_LOG_DEBUG(("Client is detached, discarding packet"));
1947 /* Send SILC_NOTIFY_TYPE_ERROR to indicate that such destination ID
1948 does not exist or is invalid. */
1949 idp = silc_id_payload_encode_data(packet->dst_id,
1951 packet->dst_id_type);
1955 error = SILC_STATUS_ERR_NO_SUCH_CLIENT_ID;
1956 if (packet->src_id_type == SILC_ID_CLIENT) {
1957 SilcClientID *client_id = silc_id_str2id(packet->src_id,
1959 packet->src_id_type);
1960 silc_server_send_notify_dest(server, sock, FALSE,
1961 client_id, SILC_ID_CLIENT,
1962 SILC_NOTIFY_TYPE_ERROR, 2,
1964 idp->data, idp->len);
1965 silc_free(client_id);
1967 silc_server_send_notify(server, sock, FALSE,
1968 SILC_NOTIFY_TYPE_ERROR, 2,
1970 idp->data, idp->len);
1973 silc_buffer_free(idp);
1977 /* Check whether destination client wishes to receive private messages */
1978 if (client && !(packet->flags & SILC_PACKET_FLAG_PRIVMSG_KEY) &&
1979 client->mode & SILC_UMODE_BLOCK_PRIVMSG) {
1980 SILC_LOG_DEBUG(("Client blocks private messages, discarding packet"));
1984 /* Send the private message */
1985 silc_server_send_private_message(server, dst_sock, idata->send_key,
1986 idata->hmac_send, idata->psn_send++,
1990 /* Received private message key packet.. This packet is never for us. It is to
1991 the client in the packet's destination ID. Sending of this sort of packet
1992 equals sending private message, ie. it is sent point to point from
1993 one client to another. */
1995 void silc_server_private_message_key(SilcServer server,
1996 SilcSocketConnection sock,
1997 SilcPacketContext *packet)
1999 SilcSocketConnection dst_sock;
2000 SilcIDListData idata;
2002 SILC_LOG_DEBUG(("Start"));
2004 if (packet->src_id_type != SILC_ID_CLIENT ||
2005 packet->dst_id_type != SILC_ID_CLIENT)
2008 if (!packet->dst_id)
2011 /* Get the route to the client */
2012 dst_sock = silc_server_get_client_route(server, packet->dst_id,
2013 packet->dst_id_len, NULL,
2018 /* Relay the packet */
2019 silc_server_relay_packet(server, dst_sock, idata->send_key,
2020 idata->hmac_send, idata->psn_send++, packet, FALSE);
2023 /* Processes incoming command reply packet. The command reply packet may
2024 be destined to one of our clients or it may directly for us. We will
2025 call the command reply routine after processing the packet. */
2027 void silc_server_command_reply(SilcServer server,
2028 SilcSocketConnection sock,
2029 SilcPacketContext *packet)
2031 SilcBuffer buffer = packet->buffer;
2032 SilcClientEntry client = NULL;
2033 SilcSocketConnection dst_sock;
2034 SilcIDListData idata;
2035 SilcClientID *id = NULL;
2037 SILC_LOG_DEBUG(("Start"));
2039 if (packet->dst_id_type == SILC_ID_CHANNEL)
2042 if (packet->dst_id_type == SILC_ID_CLIENT) {
2043 /* Destination must be one of ours */
2044 id = silc_id_str2id(packet->dst_id, packet->dst_id_len, SILC_ID_CLIENT);
2047 client = silc_idlist_find_client_by_id(server->local_list, id, TRUE, NULL);
2049 SILC_LOG_ERROR(("Cannot process command reply to unknown client"));
2055 if (packet->dst_id_type == SILC_ID_SERVER) {
2056 /* For now this must be for us */
2057 if (memcmp(packet->dst_id, server->id_string, server->id_string_len)) {
2058 SILC_LOG_ERROR(("Cannot process command reply to unknown server"));
2063 /* Execute command reply locally for the command */
2064 silc_server_command_reply_process(server, sock, buffer);
2066 if (packet->dst_id_type == SILC_ID_CLIENT && client && id) {
2067 /* Relay the packet to the client */
2068 const SilcBufferStruct p;
2070 dst_sock = (SilcSocketConnection)client->connection;
2071 idata = (SilcIDListData)client;
2073 silc_buffer_push(buffer, SILC_PACKET_HEADER_LEN + packet->src_id_len
2074 + packet->dst_id_len + packet->padlen);
2075 if (!silc_packet_send_prepare(dst_sock, 0, 0, buffer->len,
2076 idata->hmac_send, (const SilcBuffer)&p)) {
2077 SILC_LOG_ERROR(("Cannot send packet"));
2080 silc_buffer_put((SilcBuffer)&p, buffer->data, buffer->len);
2082 /* Encrypt packet */
2083 silc_packet_encrypt(idata->send_key, idata->hmac_send, idata->psn_send++,
2084 (SilcBuffer)&p, buffer->len);
2086 /* Send the packet */
2087 silc_server_packet_send_real(server, dst_sock, TRUE);
2093 /* Process received channel message. The message can be originated from
2094 client or server. */
2096 void silc_server_channel_message(SilcServer server,
2097 SilcSocketConnection sock,
2098 SilcPacketContext *packet)
2100 SilcChannelEntry channel = NULL;
2101 SilcChannelID *id = NULL;
2102 void *sender_id = NULL;
2103 SilcClientEntry sender_entry = NULL;
2104 SilcChannelClientEntry chl;
2107 SILC_LOG_DEBUG(("Processing channel message"));
2110 if (packet->dst_id_type != SILC_ID_CHANNEL) {
2111 SILC_LOG_DEBUG(("Received bad message for channel, dropped"));
2115 /* Find channel entry */
2116 id = silc_id_str2id(packet->dst_id, packet->dst_id_len, SILC_ID_CHANNEL);
2119 channel = silc_idlist_find_channel_by_id(server->local_list, id, NULL);
2121 channel = silc_idlist_find_channel_by_id(server->global_list, id, NULL);
2124 unsigned char error;
2126 /* Send SILC_NOTIFY_TYPE_ERROR to indicate that such destination ID
2127 does not exist or is invalid. */
2128 idp = silc_id_payload_encode_data(packet->dst_id,
2130 packet->dst_id_type);
2134 error = SILC_STATUS_ERR_NO_SUCH_CHANNEL_ID;
2135 if (packet->src_id_type == SILC_ID_CLIENT) {
2136 SilcClientID *client_id = silc_id_str2id(packet->src_id,
2138 packet->src_id_type);
2139 silc_server_send_notify_dest(server, sock, FALSE,
2140 client_id, SILC_ID_CLIENT,
2141 SILC_NOTIFY_TYPE_ERROR, 2,
2142 &error, 1, idp->data, idp->len);
2143 silc_free(client_id);
2145 silc_server_send_notify(server, sock, FALSE,
2146 SILC_NOTIFY_TYPE_ERROR, 2,
2147 &error, 1, idp->data, idp->len);
2150 silc_buffer_free(idp);
2155 /* See that this client is on the channel. If the original sender is
2156 not client (as it can be server as well) we don't do the check. */
2157 sender_id = silc_id_str2id(packet->src_id, packet->src_id_len,
2158 packet->src_id_type);
2161 if (packet->src_id_type == SILC_ID_CLIENT) {
2162 sender_entry = silc_idlist_find_client_by_id(server->local_list,
2163 sender_id, TRUE, NULL);
2164 if (!sender_entry) {
2166 sender_entry = silc_idlist_find_client_by_id(server->global_list,
2167 sender_id, TRUE, NULL);
2169 if (!sender_entry || !silc_server_client_on_channel(sender_entry,
2171 SILC_LOG_DEBUG(("Client not on channel"));
2175 /* If channel is moderated check that client is allowed to send
2177 if (channel->mode & SILC_CHANNEL_MODE_SILENCE_USERS &&
2178 !(chl->mode & SILC_CHANNEL_UMODE_CHANOP) &&
2179 !(chl->mode & SILC_CHANNEL_UMODE_CHANFO)) {
2180 SILC_LOG_DEBUG(("Channel is silenced from normal users"));
2183 if (channel->mode & SILC_CHANNEL_MODE_SILENCE_OPERS &&
2184 chl->mode & SILC_CHANNEL_UMODE_CHANOP &&
2185 !(chl->mode & SILC_CHANNEL_UMODE_CHANFO)) {
2186 SILC_LOG_DEBUG(("Channel is silenced from operators"));
2189 if (chl->mode & SILC_CHANNEL_UMODE_QUIET) {
2190 SILC_LOG_DEBUG(("Sender is quieted on the channel"));
2194 /* If the packet is coming from router, but the client entry is local
2195 entry to us then some router is rerouting this to us and it is not
2196 allowed. When the client is local to us it means that we've routed
2197 this packet to network, and now someone is routing it back to us. */
2198 if (server->server_type == SILC_ROUTER &&
2199 sock->type == SILC_SOCKET_TYPE_ROUTER && local) {
2200 SILC_LOG_DEBUG(("Channel message rerouted to the sender, drop it"));
2205 /* Distribute the packet to our local clients. This will send the
2206 packet for further routing as well, if needed. */
2207 silc_server_packet_relay_to_channel(server, sock, channel, sender_id,
2208 packet->src_id_type, sender_entry,
2209 packet->buffer->data,
2210 packet->buffer->len, FALSE);
2213 silc_free(sender_id);
2217 /* Received channel key packet. We distribute the key to all of our locally
2218 connected clients on the channel. */
2220 void silc_server_channel_key(SilcServer server,
2221 SilcSocketConnection sock,
2222 SilcPacketContext *packet)
2224 SilcBuffer buffer = packet->buffer;
2225 SilcChannelEntry channel;
2227 if (packet->src_id_type != SILC_ID_SERVER ||
2228 (server->server_type == SILC_ROUTER && !server->backup_router &&
2229 sock->type == SILC_SOCKET_TYPE_ROUTER))
2232 /* Save the channel key */
2233 channel = silc_server_save_channel_key(server, buffer, NULL);
2235 SILC_LOG_ERROR(("Bad channel key from %s (%s)",
2236 sock->hostname, sock->ip));
2240 /* Distribute the key to everybody who is on the channel. If we are router
2241 we will also send it to locally connected servers. */
2242 silc_server_send_channel_key(server, sock, channel, FALSE);
2244 if (server->server_type != SILC_BACKUP_ROUTER) {
2245 /* Distribute to local cell backup routers. */
2246 silc_server_backup_send(server, sock->user_data,
2247 SILC_PACKET_CHANNEL_KEY, 0,
2248 buffer->data, buffer->len, FALSE, TRUE);
2252 /* Received New Client packet and processes it. Creates Client ID for the
2253 client. Client becomes registered after calling this functions. */
2255 SilcClientEntry silc_server_new_client(SilcServer server,
2256 SilcSocketConnection sock,
2257 SilcPacketContext *packet)
2259 SilcBuffer buffer = packet->buffer;
2260 SilcClientEntry client;
2261 SilcClientID *client_id;
2262 SilcIDListData idata;
2263 char *username = NULL, *realname = NULL;
2264 SilcUInt16 username_len;
2265 SilcUInt32 id_len, tmp_len;
2267 char *hostname, *nickname, *nicknamec;
2269 SILC_LOG_DEBUG(("Creating new client"));
2271 if (sock->type != SILC_SOCKET_TYPE_CLIENT)
2274 /* Take client entry */
2275 client = (SilcClientEntry)sock->user_data;
2276 idata = (SilcIDListData)client;
2278 /* Remove the old cache entry. */
2279 if (!silc_idcache_del_by_context(server->local_list->clients, client)) {
2280 SILC_LOG_INFO(("Unauthenticated client attempted to register to network"));
2281 silc_server_disconnect_remote(server, sock,
2282 SILC_STATUS_ERR_NOT_AUTHENTICATED, NULL);
2283 if (sock->user_data)
2284 silc_server_free_sock_user_data(server, sock, NULL);
2288 /* Make sure this client hasn't registered already */
2289 if (idata->status & SILC_IDLIST_STATUS_REGISTERED) {
2290 silc_server_disconnect_remote(server, sock,
2291 SILC_STATUS_ERR_OPERATION_ALLOWED,
2292 "Too many registrations");
2293 if (sock->user_data)
2294 silc_server_free_sock_user_data(server, sock, NULL);
2298 /* Parse incoming packet */
2299 ret = silc_buffer_unformat(buffer,
2300 SILC_STR_UI16_NSTRING_ALLOC(&username,
2302 SILC_STR_UI16_STRING_ALLOC(&realname),
2305 silc_free(username);
2306 silc_free(realname);
2307 SILC_LOG_ERROR(("Client %s (%s) sent incomplete information, closing "
2308 "connection", sock->hostname, sock->ip));
2309 silc_server_disconnect_remote(server, sock,
2310 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
2312 if (sock->user_data)
2313 silc_server_free_sock_user_data(server, sock, NULL);
2318 silc_free(username);
2319 silc_free(realname);
2320 SILC_LOG_ERROR(("Client %s (%s) did not send its username, closing "
2321 "connection", sock->hostname, sock->ip));
2322 silc_server_disconnect_remote(server, sock,
2323 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
2325 if (sock->user_data)
2326 silc_server_free_sock_user_data(server, sock, NULL);
2330 if (username_len > 128) {
2331 username[128] = '\0';
2335 /* Check for valid username string */
2336 nicknamec = silc_identifier_check(username, username_len,
2337 SILC_STRING_UTF8, 128, &tmp_len);
2339 silc_free(username);
2340 silc_free(realname);
2341 SILC_LOG_ERROR(("Client %s (%s) sent bad username string, closing "
2342 "connection", sock->hostname, sock->ip));
2343 silc_server_disconnect_remote(server, sock,
2344 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
2346 if (sock->user_data)
2347 silc_server_free_sock_user_data(server, sock, NULL);
2351 /* Nickname is initially same as username */
2352 nickname = strdup(username);
2354 /* Make sanity checks for the hostname of the client. If the hostname
2355 is provided in the `username' check that it is the same than the
2356 resolved hostname, or if not resolved the hostname that appears in
2357 the client's public key. If the hostname is not present then put
2358 it from the resolved name or from the public key. */
2359 if (strchr(username, '@')) {
2360 SilcPublicKeyIdentifier pident;
2361 int tlen = strcspn(username, "@");
2362 char *phostname = NULL;
2364 hostname = silc_memdup(username + tlen + 1, strlen(username) - tlen - 1);
2366 if (strcmp(sock->hostname, sock->ip) &&
2367 strcmp(sock->hostname, hostname)) {
2368 silc_free(username);
2369 silc_free(hostname);
2370 silc_free(realname);
2371 SILC_LOG_ERROR(("Client %s (%s) sent incomplete information, closing "
2372 "connection", sock->hostname, sock->ip));
2373 silc_server_disconnect_remote(server, sock,
2374 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
2376 if (sock->user_data)
2377 silc_server_free_sock_user_data(server, sock, NULL);
2381 pident = silc_pkcs_decode_identifier(client->data.public_key->identifier);
2383 phostname = strdup(pident->host);
2384 silc_pkcs_free_identifier(pident);
2387 if (!strcmp(sock->hostname, sock->ip) &&
2388 phostname && strcmp(phostname, hostname)) {
2389 silc_free(username);
2390 silc_free(hostname);
2391 silc_free(phostname);
2392 silc_free(realname);
2393 SILC_LOG_ERROR(("Client %s (%s) sent incomplete information, closing "
2394 "connection", sock->hostname, sock->ip));
2395 silc_server_disconnect_remote(server, sock,
2396 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
2398 if (sock->user_data)
2399 silc_server_free_sock_user_data(server, sock, NULL);
2403 silc_free(phostname);
2405 /* The hostname is not present, add it. */
2407 /* XXX For now we cannot take the host name from the public key since
2408 they are not trusted or we cannot verify them as trusted. Just take
2409 what the resolved name or address is. */
2411 if (strcmp(sock->hostname, sock->ip)) {
2413 newusername = silc_calloc(strlen(username) +
2414 strlen(sock->hostname) + 2,
2415 sizeof(*newusername));
2416 strncat(newusername, username, strlen(username));
2417 strncat(newusername, "@", 1);
2418 strncat(newusername, sock->hostname, strlen(sock->hostname));
2419 silc_free(username);
2420 username = newusername;
2423 SilcPublicKeyIdentifier pident =
2424 silc_pkcs_decode_identifier(client->data.public_key->identifier);
2427 newusername = silc_calloc(strlen(username) +
2428 strlen(pident->host) + 2,
2429 sizeof(*newusername));
2430 strncat(newusername, username, strlen(username));
2431 strncat(newusername, "@", 1);
2432 strncat(newusername, pident->host, strlen(pident->host));
2433 silc_free(username);
2434 username = newusername;
2435 silc_pkcs_free_identifier(pident);
2441 /* Create Client ID */
2442 if (!silc_id_create_client_id(server, server->id, server->rng,
2443 server->md5hash, nicknamec,
2444 strlen(nicknamec), &client_id)) {
2445 silc_server_disconnect_remote(server, sock,
2446 SILC_STATUS_ERR_BAD_NICKNAME, NULL);
2447 if (sock->user_data)
2448 silc_server_free_sock_user_data(server, sock, NULL);
2452 /* If client marked as anonymous, scramble the username and hostname */
2453 if (client->mode & SILC_UMODE_ANONYMOUS) {
2456 if (strlen(username) >= 2) {
2457 username[0] = silc_rng_get_byte_fast(server->rng);
2458 username[1] = silc_rng_get_byte_fast(server->rng);
2461 scramble = silc_hash_babbleprint(server->sha1hash, username,
2465 memcpy(&scramble[16], ".silc", 5);
2466 scramble[21] = '\0';
2467 silc_free(username);
2468 username = scramble;
2471 /* Update client entry */
2472 idata->status |= SILC_IDLIST_STATUS_REGISTERED;
2473 client->nickname = nickname;
2474 client->username = username;
2475 client->userinfo = realname ? realname : strdup(username);
2476 client->id = client_id;
2477 id_len = silc_id_get_len(client_id, SILC_ID_CLIENT);
2479 /* Add the client again to the ID cache */
2480 silc_idcache_add(server->local_list->clients, nicknamec,
2481 client_id, client, 0, NULL);
2483 /* Notify our router about new client on the SILC network */
2484 silc_server_send_new_id(server, SILC_PRIMARY_ROUTE(server),
2485 SILC_BROADCAST(server), client->id,
2486 SILC_ID_CLIENT, id_len);
2488 /* Distribute to backup routers */
2489 if (server->server_type == SILC_ROUTER) {
2490 SilcBuffer idp = silc_id_payload_encode(client->id, SILC_ID_CLIENT);
2491 silc_server_backup_send(server, sock->user_data, SILC_PACKET_NEW_ID, 0,
2492 idp->data, idp->len, FALSE, TRUE);
2493 silc_buffer_free(idp);
2496 /* Send the new client ID to the client. */
2497 silc_server_send_new_id(server, sock, FALSE, client->id, SILC_ID_CLIENT,
2498 silc_id_get_len(client->id, SILC_ID_CLIENT));
2500 /* Send some nice info to the client */
2501 silc_server_send_connect_notifys(server, sock, client);
2503 /* Check if anyone is watching this nickname */
2504 if (server->server_type == SILC_ROUTER)
2505 silc_server_check_watcher_list(server, client, NULL, 0);
2510 /* Create new server. This processes received New Server packet and
2511 saves the received Server ID. The server is our locally connected
2512 server thus we save all the information and save it to local list.
2513 This funtion can be used by both normal server and router server.
2514 If normal server uses this it means that its router has connected
2515 to the server. If router uses this it means that one of the cell's
2516 servers is connected to the router. */
2518 SilcServerEntry silc_server_new_server(SilcServer server,
2519 SilcSocketConnection sock,
2520 SilcPacketContext *packet)
2522 SilcBuffer buffer = packet->buffer;
2523 SilcServerEntry new_server, server_entry;
2524 SilcServerID *server_id;
2525 SilcIDListData idata;
2526 unsigned char *server_name, *server_namec, *id_string;
2527 SilcUInt16 id_len, name_len;
2531 SILC_LOG_DEBUG(("Creating new server"));
2533 if (sock->type != SILC_SOCKET_TYPE_SERVER &&
2534 sock->type != SILC_SOCKET_TYPE_ROUTER)
2537 /* Take server entry */
2538 new_server = (SilcServerEntry)sock->user_data;
2539 idata = (SilcIDListData)new_server;
2542 if (server->server_type == SILC_ROUTER)
2543 server->stat.cell_servers++;
2545 /* Remove the old cache entry */
2546 if (!silc_idcache_del_by_context(server->local_list->servers, new_server)) {
2547 if (!silc_idcache_del_by_context(server->global_list->servers,
2549 SILC_LOG_INFO(("Unauthenticated %s attempted to register to "
2550 "network", (sock->type == SILC_SOCKET_TYPE_SERVER ?
2551 "server" : "router")));
2552 silc_server_disconnect_remote(server, sock,
2553 SILC_STATUS_ERR_NOT_AUTHENTICATED, NULL);
2554 if (sock->user_data)
2555 silc_server_free_sock_user_data(server, sock, NULL);
2561 /* Make sure this server hasn't registered already */
2562 if (idata->status & SILC_IDLIST_STATUS_REGISTERED) {
2563 silc_server_disconnect_remote(server, sock,
2564 SILC_STATUS_ERR_OPERATION_ALLOWED,
2565 "Too many registrations");
2566 if (sock->user_data)
2567 silc_server_free_sock_user_data(server, sock, NULL);
2571 /* Parse the incoming packet */
2572 ret = silc_buffer_unformat(buffer,
2573 SILC_STR_UI16_NSTRING_ALLOC(&id_string, &id_len),
2574 SILC_STR_UI16_NSTRING_ALLOC(&server_name,
2578 silc_free(id_string);
2579 silc_free(server_name);
2580 silc_server_disconnect_remote(server, sock,
2581 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
2583 if (sock->user_data)
2584 silc_server_free_sock_user_data(server, sock, NULL);
2588 if (id_len > buffer->len) {
2589 silc_free(id_string);
2590 silc_free(server_name);
2591 silc_server_disconnect_remote(server, sock,
2592 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
2594 if (sock->user_data)
2595 silc_server_free_sock_user_data(server, sock, NULL);
2599 if (name_len > 256) {
2600 server_name[256] = '\0';
2605 server_id = silc_id_str2id(id_string, id_len, SILC_ID_SERVER);
2607 silc_free(id_string);
2608 silc_free(server_name);
2609 silc_server_disconnect_remote(server, sock,
2610 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
2612 if (sock->user_data)
2613 silc_server_free_sock_user_data(server, sock, NULL);
2616 silc_free(id_string);
2618 /* Check for valid server ID */
2619 if (!silc_id_is_valid_server_id(server, server_id, sock)) {
2620 SILC_LOG_INFO(("Invalid server ID sent by %s (%s)",
2621 sock->ip, sock->hostname));
2622 silc_server_disconnect_remote(server, sock,
2623 SILC_STATUS_ERR_BAD_SERVER_ID, NULL);
2624 if (sock->user_data)
2625 silc_server_free_sock_user_data(server, sock, NULL);
2626 silc_free(server_name);
2630 /* Check that we do not have this ID already */
2631 server_entry = silc_idlist_find_server_by_id(server->local_list,
2632 server_id, TRUE, NULL);
2634 if (SILC_IS_LOCAL(server_entry)) {
2635 SILC_LOG_ERROR(("Too many registrations from %s (%s)",
2636 sock->ip, sock->hostname));
2637 silc_server_disconnect_remote(server, sock,
2638 SILC_STATUS_ERR_OPERATION_ALLOWED,
2639 "Too many registrations");
2640 if (sock->user_data)
2641 silc_server_free_sock_user_data(server, sock, NULL);
2644 silc_idcache_del_by_context(server->local_list->servers, server_entry);
2647 server_entry = silc_idlist_find_server_by_id(server->global_list,
2648 server_id, TRUE, NULL);
2650 if (SILC_IS_LOCAL(server_entry)) {
2651 SILC_LOG_ERROR(("Too many registrations from %s (%s)",
2652 sock->ip, sock->hostname));
2653 silc_server_disconnect_remote(server, sock,
2654 SILC_STATUS_ERR_OPERATION_ALLOWED,
2655 "Too many registrations");
2656 if (sock->user_data)
2657 silc_server_free_sock_user_data(server, sock, NULL);
2660 silc_idcache_del_by_context(server->global_list->servers,
2666 /* Check server name */
2667 server_namec = silc_identifier_check(server_name, strlen(server_name),
2668 SILC_STRING_UTF8, 256, NULL);
2669 if (!server_namec) {
2670 SILC_LOG_ERROR(("Malformed server name from %s (%s)",
2671 sock->ip, sock->hostname));
2672 silc_server_disconnect_remote(server, sock,
2673 SILC_STATUS_ERR_OPERATION_ALLOWED,
2674 "Malfromed server name");
2675 if (sock->user_data)
2676 silc_server_free_sock_user_data(server, sock, NULL);
2680 /* Update server entry */
2681 idata->status |= SILC_IDLIST_STATUS_REGISTERED;
2682 new_server->server_name = server_name;
2683 new_server->id = server_id;
2685 SILC_LOG_DEBUG(("New server id(%s)",
2686 silc_id_render(server_id, SILC_ID_SERVER)));
2688 /* Add again the entry to the ID cache. */
2689 silc_idcache_add(local ? server->local_list->servers :
2690 server->global_list->servers, server_namec, server_id,
2691 new_server, 0, NULL);
2693 /* Distribute the information about new server in the SILC network
2694 to our router. If we are normal server we won't send anything
2695 since this connection must be our router connection. */
2696 if (server->server_type == SILC_ROUTER && !server->standalone &&
2697 SILC_PRIMARY_ROUTE(server) != sock)
2698 silc_server_send_new_id(server, SILC_PRIMARY_ROUTE(server),
2699 TRUE, new_server->id, SILC_ID_SERVER,
2700 silc_id_get_len(server_id, SILC_ID_SERVER));
2702 if (server->server_type == SILC_ROUTER) {
2703 /* Distribute to backup routers */
2704 SilcBuffer idp = silc_id_payload_encode(new_server->id, SILC_ID_SERVER);
2705 silc_server_backup_send(server, sock->user_data, SILC_PACKET_NEW_ID, 0,
2706 idp->data, idp->len, FALSE, TRUE);
2707 silc_buffer_free(idp);
2710 /* Check whether this router connection has been replaced by an
2711 backup router. If it has been then we'll disable the server and will
2712 ignore everything it will send until the backup router resuming
2713 protocol has been completed. */
2714 if (sock->type == SILC_SOCKET_TYPE_ROUTER &&
2715 silc_server_backup_replaced_get(server, server_id, NULL)) {
2716 /* Send packet to the router indicating that it cannot use this
2717 connection as it has been replaced by backup router. */
2718 SILC_LOG_DEBUG(("Remote router has been replaced by backup router, "
2719 "disabling its connection"));
2721 silc_server_backup_send_replaced(server, sock);
2723 /* Mark the router disabled. The data sent earlier will go but nothing
2724 after this goes to this connection. */
2725 idata->status |= SILC_IDLIST_STATUS_DISABLED;
2727 /* If it is router announce our stuff to it. */
2728 if (sock->type == SILC_SOCKET_TYPE_ROUTER &&
2729 server->server_type == SILC_ROUTER) {
2730 silc_server_announce_servers(server, FALSE, 0, sock);
2731 silc_server_announce_clients(server, 0, sock);
2732 silc_server_announce_channels(server, 0, sock);
2735 /* Announce our information to backup router */
2736 if (new_server->server_type == SILC_BACKUP_ROUTER &&
2737 sock->type == SILC_SOCKET_TYPE_SERVER &&
2738 server->server_type == SILC_ROUTER) {
2739 silc_server_announce_servers(server, TRUE, 0, sock);
2740 silc_server_announce_clients(server, 0, sock);
2741 silc_server_announce_channels(server, 0, sock);
2744 /* If backup router, mark it as one of ours. This server is considered
2745 to be backup router after this setting. */
2746 if (new_server->server_type == SILC_BACKUP_ROUTER) {
2747 SilcServerConfigRouter *backup;
2748 backup = silc_server_config_find_backup_conn(server, sock->ip);
2750 backup = silc_server_config_find_backup_conn(server, sock->hostname);
2752 /* Add as our backup router */
2753 silc_server_backup_add(server, new_server, backup->backup_replace_ip,
2754 backup->backup_replace_port,
2755 backup->backup_local);
2759 /* By default the servers connected to backup router are disabled
2760 until backup router has become the primary */
2761 if (server->server_type == SILC_BACKUP_ROUTER &&
2762 sock->type == SILC_SOCKET_TYPE_SERVER)
2763 idata->status |= SILC_IDLIST_STATUS_DISABLED;
2769 /* Processes incoming New ID packet. New ID Payload is used to distribute
2770 information about newly registered clients and servers. */
2772 static void silc_server_new_id_real(SilcServer server,
2773 SilcSocketConnection sock,
2774 SilcPacketContext *packet,
2777 SilcBuffer buffer = packet->buffer;
2779 SilcServerEntry router, server_entry;
2780 SilcSocketConnection router_sock;
2785 SILC_LOG_DEBUG(("Processing new ID"));
2787 if (sock->type == SILC_SOCKET_TYPE_CLIENT ||
2788 server->server_type == SILC_SERVER ||
2789 packet->src_id_type != SILC_ID_SERVER)
2792 idp = silc_id_payload_parse(buffer->data, buffer->len);
2796 id_type = silc_id_payload_get_type(idp);
2798 /* Normal server cannot have other normal server connections */
2799 server_entry = (SilcServerEntry)sock->user_data;
2800 if (id_type == SILC_ID_SERVER && sock->type == SILC_SOCKET_TYPE_SERVER &&
2801 server_entry->server_type == SILC_SERVER)
2804 id = silc_id_payload_get_id(idp);
2808 /* If the packet is coming from server then use the sender as the
2809 origin of the the packet. If it came from router then check the real
2810 sender of the packet and use that as the origin. */
2811 if (sock->type == SILC_SOCKET_TYPE_SERVER) {
2812 id_list = server->local_list;
2814 router = sock->user_data;
2816 /* If the sender is backup router and ID is server (and we are not
2817 backup router) then switch the entry to global list. */
2818 if (server_entry->server_type == SILC_BACKUP_ROUTER &&
2819 id_type == SILC_ID_SERVER &&
2820 server->id_entry->server_type != SILC_BACKUP_ROUTER) {
2821 id_list = server->global_list;
2822 router_sock = server->router ? SILC_PRIMARY_ROUTE(server) : sock;
2825 void *sender_id = silc_id_str2id(packet->src_id, packet->src_id_len,
2826 packet->src_id_type);
2827 router = silc_idlist_find_server_by_id(server->global_list,
2828 sender_id, TRUE, NULL);
2830 router = silc_idlist_find_server_by_id(server->local_list,
2831 sender_id, TRUE, NULL);
2832 silc_free(sender_id);
2834 id_list = server->global_list;
2841 case SILC_ID_CLIENT:
2843 SilcClientEntry entry;
2845 /* Check that we do not have this client already */
2846 entry = silc_idlist_find_client_by_id(server->global_list,
2847 id, server->server_type,
2850 entry = silc_idlist_find_client_by_id(server->local_list,
2851 id, server->server_type,
2854 SILC_LOG_DEBUG(("Ignoring client that we already have"));
2858 SILC_LOG_DEBUG(("New client id(%s) from [%s] %s",
2859 silc_id_render(id, SILC_ID_CLIENT),
2860 sock->type == SILC_SOCKET_TYPE_SERVER ?
2861 "Server" : "Router", sock->hostname));
2863 /* As a router we keep information of all global information in our
2864 global list. Cell wide information however is kept in the local
2866 entry = silc_idlist_add_client(id_list, NULL, NULL, NULL,
2867 id, router, NULL, 0);
2869 SILC_LOG_ERROR(("Could not add new client to the ID Cache"));
2871 /* Inform the sender that the ID is not usable */
2872 silc_server_send_notify_signoff(server, sock, FALSE, id, NULL);
2875 entry->nickname = NULL;
2876 entry->data.status |= SILC_IDLIST_STATUS_REGISTERED;
2878 if (sock->type == SILC_SOCKET_TYPE_SERVER)
2879 server->stat.cell_clients++;
2880 server->stat.clients++;
2882 /* Check if anyone is watching this nickname */
2883 if (server->server_type == SILC_ROUTER && id_list == server->local_list)
2884 silc_server_check_watcher_list(server, entry, NULL, 0);
2886 if (server->server_type == SILC_ROUTER) {
2887 /* Add the client's public key to hash table or get the key with
2889 if (entry->data.public_key)
2890 silc_hash_table_add(server->pk_hash, entry->data.public_key, entry);
2892 silc_server_send_command(server, router_sock,
2893 SILC_COMMAND_GETKEY, ++server->cmd_ident,
2894 1, 1, buffer->data, buffer->len);
2899 case SILC_ID_SERVER:
2901 SilcServerEntry entry;
2903 /* If the ID is mine, ignore it. */
2904 if (SILC_ID_SERVER_COMPARE(id, server->id)) {
2905 SILC_LOG_DEBUG(("Ignoring my own ID as new ID"));
2909 /* If the ID is the sender's ID, ignore it (we have it already) */
2910 if (SILC_ID_SERVER_COMPARE(id, router->id)) {
2911 SILC_LOG_DEBUG(("Ignoring sender's own ID"));
2915 /* Check that we do not have this server already */
2916 entry = silc_idlist_find_server_by_id(server->global_list,
2917 id, server->server_type,
2920 entry = silc_idlist_find_server_by_id(server->local_list,
2921 id, server->server_type,
2924 SILC_LOG_DEBUG(("Ignoring server that we already have"));
2928 SILC_LOG_DEBUG(("New server id(%s) from [%s] %s",
2929 silc_id_render(id, SILC_ID_SERVER),
2930 sock->type == SILC_SOCKET_TYPE_SERVER ?
2931 "Server" : "Router", sock->hostname));
2933 /* As a router we keep information of all global information in our
2934 global list. Cell wide information however is kept in the local
2936 entry = silc_idlist_add_server(id_list, NULL, 0, id, router,
2939 SILC_LOG_ERROR(("Could not add new server to the ID Cache"));
2942 entry->data.status |= SILC_IDLIST_STATUS_REGISTERED;
2944 if (sock->type == SILC_SOCKET_TYPE_SERVER)
2945 server->stat.cell_servers++;
2946 server->stat.servers++;
2950 case SILC_ID_CHANNEL:
2951 SILC_LOG_ERROR(("Channel cannot be registered with NEW_ID packet"));
2960 /* If the sender of this packet is server and we are router we need to
2961 broadcast this packet to other routers in the network. */
2962 if (broadcast && server->server_type == SILC_ROUTER &&
2963 sock->type == SILC_SOCKET_TYPE_SERVER &&
2964 !(packet->flags & SILC_PACKET_FLAG_BROADCAST)) {
2965 SILC_LOG_DEBUG(("Broadcasting received New ID packet"));
2966 silc_server_packet_send(server, SILC_PRIMARY_ROUTE(server),
2968 packet->flags | SILC_PACKET_FLAG_BROADCAST,
2969 buffer->data, buffer->len, FALSE);
2970 silc_server_backup_send(server, sock->user_data,
2971 packet->type, packet->flags,
2972 packet->buffer->data, packet->buffer->len,
2977 silc_id_payload_free(idp);
2981 /* Processes incoming New ID packet. New ID Payload is used to distribute
2982 information about newly registered clients and servers. */
2984 void silc_server_new_id(SilcServer server, SilcSocketConnection sock,
2985 SilcPacketContext *packet)
2987 silc_server_new_id_real(server, sock, packet, TRUE);
2990 /* Receoved New Id List packet, list of New ID payloads inside one
2991 packet. Process the New ID payloads one by one. */
2993 void silc_server_new_id_list(SilcServer server, SilcSocketConnection sock,
2994 SilcPacketContext *packet)
2996 SilcPacketContext *new_id;
3000 SILC_LOG_DEBUG(("Processing New ID List"));
3002 if (sock->type == SILC_SOCKET_TYPE_CLIENT ||
3003 packet->src_id_type != SILC_ID_SERVER)
3006 /* If the sender of this packet is server and we are router we need to
3007 broadcast this packet to other routers in the network. Broadcast
3008 this list packet instead of multiple New ID packets. */
3009 if (server->server_type == SILC_ROUTER &&
3010 sock->type == SILC_SOCKET_TYPE_SERVER &&
3011 !(packet->flags & SILC_PACKET_FLAG_BROADCAST)) {
3012 SILC_LOG_DEBUG(("Broadcasting received New ID List packet"));
3013 silc_server_packet_send(server, SILC_PRIMARY_ROUTE(server),
3015 packet->flags | SILC_PACKET_FLAG_BROADCAST,
3016 packet->buffer->data,
3017 packet->buffer->len, FALSE);
3018 silc_server_backup_send(server, sock->user_data,
3019 packet->type, packet->flags,
3020 packet->buffer->data, packet->buffer->len,
3024 /* Make copy of the original packet context, except for the actual
3025 data buffer, which we will here now fetch from the original buffer. */
3026 new_id = silc_packet_context_alloc();
3027 new_id->type = SILC_PACKET_NEW_ID;
3028 new_id->flags = packet->flags & (~SILC_PACKET_FLAG_LIST);
3029 new_id->src_id = packet->src_id;
3030 new_id->src_id_len = packet->src_id_len;
3031 new_id->src_id_type = packet->src_id_type;
3032 new_id->dst_id = packet->dst_id;
3033 new_id->dst_id_len = packet->dst_id_len;
3034 new_id->dst_id_type = packet->dst_id_type;
3036 idp = silc_buffer_alloc(256);
3037 new_id->buffer = idp;
3039 while (packet->buffer->len) {
3040 SILC_GET16_MSB(id_len, packet->buffer->data + 2);
3041 if ((id_len > packet->buffer->len) ||
3042 (id_len > idp->truelen))
3045 silc_buffer_pull_tail(idp, 4 + id_len);
3046 silc_buffer_put(idp, packet->buffer->data, 4 + id_len);
3048 /* Process the New ID */
3049 silc_server_new_id_real(server, sock, new_id, FALSE);
3051 silc_buffer_push_tail(idp, 4 + id_len);
3052 silc_buffer_pull(packet->buffer, 4 + id_len);
3055 silc_buffer_free(idp);
3059 /* Received New Channel packet. Information about new channels in the
3060 network are distributed using this packet. Save the information about
3061 the new channel. This usually comes from router but also normal server
3062 can send this to notify channels it has when it connects to us. */
3064 void silc_server_new_channel(SilcServer server,
3065 SilcSocketConnection sock,
3066 SilcPacketContext *packet)
3068 SilcChannelPayload payload;
3069 SilcChannelID *channel_id;
3070 char *channel_name, *channel_namec = NULL;
3071 SilcUInt32 name_len;
3073 SilcUInt32 id_len, cipher_len;
3074 SilcServerEntry server_entry;
3075 SilcChannelEntry channel;
3078 if (sock->type == SILC_SOCKET_TYPE_CLIENT ||
3079 packet->src_id_type != SILC_ID_SERVER ||
3080 server->server_type == SILC_SERVER)
3083 /* Parse the channel payload */
3084 payload = silc_channel_payload_parse(packet->buffer->data,
3085 packet->buffer->len);
3089 /* Get the channel ID */
3090 channel_id = silc_channel_get_id_parse(payload);
3092 silc_channel_payload_free(payload);
3096 channel_name = silc_channel_get_name(payload, &name_len);
3097 if (name_len > 256) {
3098 channel_name[256] = '\0';
3102 /* Check channel name */
3103 channel_namec = silc_identifier_check(channel_name, strlen(channel_name),
3104 SILC_STRING_UTF8, 256, NULL);
3108 id = silc_channel_get_id(payload, &id_len);
3110 server_entry = (SilcServerEntry)sock->user_data;
3112 if (sock->type == SILC_SOCKET_TYPE_ROUTER) {
3113 /* Add the channel to global list as it is coming from router. It
3114 cannot be our own channel as it is coming from router. */
3116 /* Check that we don't already have this channel */
3117 channel = silc_idlist_find_channel_by_name(server->local_list,
3118 channel_namec, NULL);
3120 channel = silc_idlist_find_channel_by_name(server->global_list,
3121 channel_namec, NULL);
3123 SILC_LOG_DEBUG(("New channel id(%s) from [Router] %s",
3124 silc_id_render(channel_id, SILC_ID_CHANNEL),
3128 silc_idlist_add_channel(server->global_list, strdup(channel_name),
3129 0, channel_id, sock->user_data, NULL, NULL, 0);
3131 silc_channel_payload_free(payload);
3132 silc_free(channel_id);
3135 channel->disabled = TRUE; /* Disabled until someone JOINs */
3137 server->stat.channels++;
3138 if (server->server_type == SILC_ROUTER)
3139 channel->users_resolved = TRUE;
3142 /* The channel is coming from our server, thus it is in our cell
3143 we will add it to our local list. */
3146 SILC_LOG_DEBUG(("Channel id(%s) from [Server] %s",
3147 silc_id_render(channel_id, SILC_ID_CHANNEL),
3150 /* Check that we don't already have this channel */
3151 channel = silc_idlist_find_channel_by_name(server->local_list,
3152 channel_namec, NULL);
3154 channel = silc_idlist_find_channel_by_name(server->global_list,
3155 channel_namec, NULL);
3157 /* If the channel does not exist, then create it. This creates a new
3158 key to the channel as well that we will send to the server. */
3160 SILC_LOG_DEBUG(("Channel is new to us"));
3162 /* The protocol says that the Channel ID's IP address must be based
3163 on the router's IP address. Check whether the ID is based in our
3164 IP and if it is not then create a new ID and enforce the server
3165 to switch the ID. */
3166 if (server_entry->server_type != SILC_BACKUP_ROUTER &&
3167 !SILC_ID_COMPARE(channel_id, server->id, server->id->ip.data_len)) {
3169 SILC_LOG_DEBUG(("Forcing the server to change Channel ID"));
3170 if (silc_id_create_channel_id(server, server->id, server->rng, &tmp)) {
3171 silc_server_send_notify_channel_change(server, sock, FALSE,
3173 silc_channel_payload_free(payload);
3174 silc_free(channel_id);
3178 /* Wait that server re-announces this channel */
3182 /* Create the channel with the provided Channel ID */
3183 channel = silc_server_create_new_channel_with_id(server, NULL, NULL,
3187 silc_channel_payload_free(payload);
3188 silc_free(channel_id);
3191 channel->disabled = TRUE; /* Disabled until someone JOINs */
3193 #if 0 /* We assume that CMODE_CHANGE notify is sent to us after this. */
3195 /* XXX Dunno if this is supposed to be set in any server type. If set
3196 here the CMODE_CHANGE that may follow sets mode that we already
3197 have, and we may loose data from the CMODE_CHANGE notify. */
3198 if (server_entry->server_type != SILC_BACKUP_ROUTER)
3199 channel->mode = silc_channel_get_mode(payload);
3202 /* Send the new channel key to the server */
3203 id = silc_id_id2str(channel->id, SILC_ID_CHANNEL);
3204 id_len = silc_id_get_len(channel->id, SILC_ID_CHANNEL);
3205 cipher = silc_cipher_get_name(channel->channel_key);
3206 cipher_len = strlen(cipher);
3207 chk = silc_channel_key_payload_encode(id_len, id,
3209 channel->key_len / 8,
3211 silc_server_packet_send(server, sock, SILC_PACKET_CHANNEL_KEY, 0,
3212 chk->data, chk->len, FALSE);
3213 silc_buffer_free(chk);
3216 /* The channel exist by that name, check whether the ID's match.
3217 If they don't then we'll force the server to use the ID we have.
3218 We also create a new key for the channel. */
3219 SilcBuffer modes = NULL, users = NULL, users_modes = NULL;
3221 SILC_LOG_DEBUG(("Channel already exists"));
3223 if (!SILC_ID_CHANNEL_COMPARE(channel_id, channel->id)) {
3224 /* They don't match, send CHANNEL_CHANGE notify to the server to
3225 force the ID change. */
3226 SILC_LOG_DEBUG(("Forcing the server to change Channel ID"));
3227 silc_server_send_notify_channel_change(server, sock, FALSE,
3228 channel_id, channel->id);
3229 silc_channel_payload_free(payload);
3230 silc_free(channel_id);
3232 /* Wait that server re-announces this channel */
3236 #if 0 /* We will announce our CMODE anyway for this channel, so no need
3237 to check it (implicit enforce). */
3239 /* If the mode is different from what we have then enforce the
3241 mode = silc_channel_get_mode(payload);
3242 if (channel->mode != mode) {
3243 SILC_LOG_DEBUG(("Forcing the server to change channel mode"));
3244 silc_server_send_notify_cmode(server, sock, FALSE, channel,
3245 channel->mode, server->id,
3246 SILC_ID_SERVER, channel->cipher,
3248 channel->passphrase,
3249 channel->founder_key);
3253 /* Create new key for the channel and send it to the server and
3254 everybody else possibly on the channel. */
3255 if (!(channel->mode & SILC_CHANNEL_MODE_PRIVKEY)) {
3257 if (silc_hash_table_count(channel->user_list)) {
3258 if (!silc_server_create_channel_key(server, channel, 0)) {
3259 silc_channel_payload_free(payload);
3260 silc_free(channel_id);
3264 /* Send to the channel */
3265 silc_server_send_channel_key(server, sock, channel, FALSE);
3268 /* Send to the server */
3269 id = silc_id_id2str(channel->id, SILC_ID_CHANNEL);
3270 id_len = silc_id_get_len(channel->id, SILC_ID_CHANNEL);
3271 cipher = silc_cipher_get_name(channel->channel_key);
3272 cipher_len = strlen(cipher);
3273 chk = silc_channel_key_payload_encode(id_len, id,
3275 channel->key_len / 8,
3277 silc_server_packet_send(server, sock, SILC_PACKET_CHANNEL_KEY, 0,
3278 chk->data, chk->len, FALSE);
3279 silc_buffer_free(chk);
3283 silc_free(channel_id);
3285 /* Since the channel is coming from server and we also know about it
3286 then send the JOIN notify to the server so that it see's our
3287 users on the channel "joining" the channel. */
3288 silc_server_announce_get_channel_users(server, channel, &modes, &users,
3291 silc_buffer_push(users, users->data - users->head);
3292 silc_server_packet_send(server, sock,
3293 SILC_PACKET_NOTIFY, SILC_PACKET_FLAG_LIST,
3294 users->data, users->len, FALSE);
3295 silc_buffer_free(users);
3298 silc_buffer_push(modes, modes->data - modes->head);
3299 silc_server_packet_send_dest(server, sock,
3300 SILC_PACKET_NOTIFY, SILC_PACKET_FLAG_LIST,
3301 channel->id, SILC_ID_CHANNEL,
3302 modes->data, modes->len, FALSE);
3303 silc_buffer_free(modes);
3306 silc_buffer_push(users_modes, users_modes->data - users_modes->head);
3307 silc_server_packet_send_dest(server, sock,
3308 SILC_PACKET_NOTIFY, SILC_PACKET_FLAG_LIST,
3309 channel->id, SILC_ID_CHANNEL,
3311 users_modes->len, FALSE);
3312 silc_buffer_free(users_modes);
3314 if (channel->topic) {
3315 silc_server_send_notify_topic_set(server, sock,
3316 server->server_type == SILC_ROUTER ?
3317 TRUE : FALSE, channel,
3318 server->id, SILC_ID_SERVER,
3324 /* If the sender of this packet is server and we are router we need to
3325 broadcast this packet to other routers in the network. Broadcast
3326 this list packet instead of multiple New Channel packets. */
3327 if (server->server_type == SILC_ROUTER &&
3328 sock->type == SILC_SOCKET_TYPE_SERVER &&
3329 !(packet->flags & SILC_PACKET_FLAG_BROADCAST)) {
3330 SILC_LOG_DEBUG(("Broadcasting received New Channel packet"));
3331 silc_server_packet_send(server, SILC_PRIMARY_ROUTE(server),
3333 packet->flags | SILC_PACKET_FLAG_BROADCAST,
3334 packet->buffer->data,
3335 packet->buffer->len, FALSE);
3336 silc_server_backup_send(server, sock->user_data,
3337 packet->type, packet->flags,
3338 packet->buffer->data, packet->buffer->len,
3342 silc_free(channel_namec);
3343 silc_channel_payload_free(payload);
3346 /* Received New Channel List packet, list of New Channel List payloads inside
3347 one packet. Process the New Channel payloads one by one. */
3349 void silc_server_new_channel_list(SilcServer server,
3350 SilcSocketConnection sock,
3351 SilcPacketContext *packet)
3353 SilcPacketContext *new;
3355 SilcUInt16 len1, len2;
3357 SILC_LOG_DEBUG(("Processing New Channel List"));
3359 if (sock->type == SILC_SOCKET_TYPE_CLIENT ||
3360 packet->src_id_type != SILC_ID_SERVER ||
3361 server->server_type == SILC_SERVER)
3364 /* Make copy of the original packet context, except for the actual
3365 data buffer, which we will here now fetch from the original buffer. */
3366 new = silc_packet_context_alloc();
3367 new->type = SILC_PACKET_NEW_CHANNEL;
3368 new->flags = packet->flags & (~SILC_PACKET_FLAG_LIST);
3369 new->src_id = packet->src_id;
3370 new->src_id_len = packet->src_id_len;
3371 new->src_id_type = packet->src_id_type;
3372 new->dst_id = packet->dst_id;
3373 new->dst_id_len = packet->dst_id_len;
3374 new->dst_id_type = packet->dst_id_type;
3376 buffer = silc_buffer_alloc(512);
3377 new->buffer = buffer;
3379 while (packet->buffer->len) {
3380 SILC_GET16_MSB(len1, packet->buffer->data);
3381 if ((len1 > packet->buffer->len) ||
3382 (len1 > buffer->truelen))
3385 SILC_GET16_MSB(len2, packet->buffer->data + 2 + len1);
3386 if ((len2 > packet->buffer->len) ||
3387 (len2 > buffer->truelen))
3390 silc_buffer_pull_tail(buffer, 8 + len1 + len2);
3391 silc_buffer_put(buffer, packet->buffer->data, 8 + len1 + len2);
3393 /* Process the New Channel */
3394 silc_server_new_channel(server, sock, new);
3396 silc_buffer_push_tail(buffer, 8 + len1 + len2);
3397 silc_buffer_pull(packet->buffer, 8 + len1 + len2);
3400 silc_buffer_free(buffer);
3404 /* Received key agreement packet. This packet is never for us. It is to
3405 the client in the packet's destination ID. Sending of this sort of packet
3406 equals sending private message, ie. it is sent point to point from
3407 one client to another. */
3409 void silc_server_key_agreement(SilcServer server,
3410 SilcSocketConnection sock,
3411 SilcPacketContext *packet)
3413 SilcSocketConnection dst_sock;
3414 SilcIDListData idata;
3416 SILC_LOG_DEBUG(("Start"));
3418 if (packet->src_id_type != SILC_ID_CLIENT ||
3419 packet->dst_id_type != SILC_ID_CLIENT)
3422 if (!packet->dst_id)
3425 /* Get the route to the client */
3426 dst_sock = silc_server_get_client_route(server, packet->dst_id,
3427 packet->dst_id_len, NULL,
3432 /* Relay the packet */
3433 silc_server_relay_packet(server, dst_sock, idata->send_key,
3434 idata->hmac_send, idata->psn_send++,
3438 /* Received connection auth request packet that is used during connection
3439 phase to resolve the mandatory authentication method. This packet can
3440 actually be received at anytime but usually it is used only during
3441 the connection authentication phase. Now, protocol says that this packet
3442 can come from client or server, however, we support only this coming
3443 from client and expect that server always knows what authentication
3446 void silc_server_connection_auth_request(SilcServer server,
3447 SilcSocketConnection sock,
3448 SilcPacketContext *packet)
3450 SilcServerConfigClient *client = NULL;
3451 SilcUInt16 conn_type;
3453 SilcAuthMethod auth_meth = SILC_AUTH_NONE;
3455 if (packet->src_id_type && packet->src_id_type != SILC_ID_CLIENT) {
3456 SILC_LOG_DEBUG(("Request not from client"));
3460 /* Parse the payload */
3461 ret = silc_buffer_unformat(packet->buffer,
3462 SILC_STR_UI_SHORT(&conn_type),
3463 SILC_STR_UI_SHORT(NULL),
3468 if (conn_type != SILC_SOCKET_TYPE_CLIENT)
3471 /* Get the authentication method for the client */
3472 auth_meth = SILC_AUTH_NONE;
3473 client = silc_server_config_find_client(server, sock->ip);
3475 client = silc_server_config_find_client(server, sock->hostname);
3477 if (client->passphrase) {
3478 if (client->publickeys && !server->config->prefer_passphrase_auth)
3479 auth_meth = SILC_AUTH_PUBLIC_KEY;
3481 auth_meth = SILC_AUTH_PASSWORD;
3482 } else if (client->publickeys)
3483 auth_meth = SILC_AUTH_PUBLIC_KEY;
3486 SILC_LOG_DEBUG(("Authentication method is [%s]",
3487 (auth_meth == SILC_AUTH_NONE ? "None" :
3488 auth_meth == SILC_AUTH_PASSWORD ? "Passphrase" :
3489 "Digital signatures")));
3491 /* Send it back to the client */
3492 silc_server_send_connection_auth_request(server, sock, conn_type, auth_meth);
3495 /* Received REKEY packet. The sender of the packet wants to regenerate
3496 its session keys. This starts the REKEY protocol. */
3498 void silc_server_rekey(SilcServer server,
3499 SilcSocketConnection sock,
3500 SilcPacketContext *packet)
3502 SilcProtocol protocol;
3503 SilcServerRekeyInternalContext *proto_ctx;
3504 SilcIDListData idata = (SilcIDListData)sock->user_data;
3506 SILC_LOG_DEBUG(("Received rekey request"));
3508 /* If we have other protocol executing we have no other choice but to
3509 not execute rekey. XXX This is very bad thing. Let's hope this
3510 doesn't happen often. */
3511 if (sock->protocol) {
3512 SILC_LOG_WARNING(("Cannot execute REKEY protocol because other protocol "
3513 "is executing at the same time"));
3517 /* Allocate internal protocol context. This is sent as context
3519 proto_ctx = silc_calloc(1, sizeof(*proto_ctx));
3520 proto_ctx->server = (void *)server;
3521 proto_ctx->sock = silc_socket_dup(sock);
3522 proto_ctx->responder = TRUE;
3523 proto_ctx->pfs = idata->rekey->pfs;
3525 /* Perform rekey protocol. Will call the final callback after the
3526 protocol is over. */
3527 silc_protocol_alloc(SILC_PROTOCOL_SERVER_REKEY,
3528 &protocol, proto_ctx, silc_server_rekey_final);
3529 sock->protocol = protocol;
3531 if (proto_ctx->pfs == FALSE)
3532 /* Run the protocol */
3533 silc_protocol_execute(protocol, server->schedule, 0, 0);
3536 /* Received file transger packet. This packet is never for us. It is to
3537 the client in the packet's destination ID. Sending of this sort of packet
3538 equals sending private message, ie. it is sent point to point from
3539 one client to another. */
3541 void silc_server_ftp(SilcServer server,
3542 SilcSocketConnection sock,
3543 SilcPacketContext *packet)
3545 SilcSocketConnection dst_sock;
3546 SilcIDListData idata;
3548 SILC_LOG_DEBUG(("Start"));
3550 if (packet->src_id_type != SILC_ID_CLIENT ||
3551 packet->dst_id_type != SILC_ID_CLIENT)
3554 if (!packet->dst_id)
3557 /* Get the route to the client */
3558 dst_sock = silc_server_get_client_route(server, packet->dst_id,
3559 packet->dst_id_len, NULL,
3564 /* Relay the packet */
3565 silc_server_relay_packet(server, dst_sock, idata->send_key,
3566 idata->hmac_send, idata->psn_send++,
3572 SilcSocketConnection sock;
3573 SilcPacketContext *packet;
3575 } *SilcServerResumeResolve;
3577 SILC_SERVER_CMD_FUNC(resume_resolve)
3579 SilcServerResumeResolve r = (SilcServerResumeResolve)context;
3580 SilcServer server = r->server;
3581 SilcSocketConnection sock = r->sock;
3582 SilcServerCommandReplyContext reply = context2;
3583 SilcClientEntry client;
3585 SILC_LOG_DEBUG(("Start"));
3587 if (!reply || !silc_command_get_status(reply->payload, NULL, NULL)) {
3588 SILC_LOG_ERROR(("Client %s (%s) tried to resume unknown client, "
3589 "closing connection", sock->hostname, sock->ip));
3590 silc_server_disconnect_remote(server, sock,
3591 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
3592 "Resuming not possible");
3593 if (sock->user_data)
3594 silc_server_free_sock_user_data(server, sock, NULL);
3598 if (reply && silc_command_get(reply->payload) == SILC_COMMAND_WHOIS) {
3599 /* Get entry to the client, and resolve it if we don't have it. */
3600 client = silc_idlist_find_client_by_id(server->local_list,
3601 r->data, TRUE, NULL);
3603 client = silc_idlist_find_client_by_id(server->global_list,
3604 r->data, TRUE, NULL);
3606 SILC_LOG_ERROR(("Client %s (%s) tried to resume unknown client, "
3607 "closing connection", sock->hostname, sock->ip));
3608 silc_server_disconnect_remote(server, sock,
3609 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
3610 "Resuming not possible");
3611 if (sock->user_data)
3612 silc_server_free_sock_user_data(server, sock, NULL);
3617 if (!(client->mode & SILC_UMODE_DETACHED)) {
3618 SILC_LOG_ERROR(("Client %s (%s) tried to resume un-detached client, "
3619 "closing connection", sock->hostname, sock->ip));
3620 silc_server_disconnect_remote(server, sock,
3621 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
3622 "Resuming not possible");
3623 if (sock->user_data)
3624 silc_server_free_sock_user_data(server, sock, NULL);
3628 client->data.status |= SILC_IDLIST_STATUS_RESUME_RES;
3631 /* Reprocess the packet */
3632 silc_server_resume_client(server, sock, r->packet);
3635 silc_socket_free(r->sock);
3636 silc_packet_context_free(r->packet);
3641 /* Received client resuming packet. This is used to resume detached
3642 client session. It can be sent by the client who wishes to resume
3643 but this is also sent by servers and routers to notify other routers
3644 that the client is not detached anymore. */
3646 void silc_server_resume_client(SilcServer server,
3647 SilcSocketConnection sock,
3648 SilcPacketContext *packet)
3650 SilcBuffer buffer = packet->buffer, buf;
3651 SilcIDListData idata;
3652 SilcIDCacheEntry id_cache = NULL;
3653 SilcClientEntry detached_client;
3654 SilcClientID *client_id = NULL;
3655 unsigned char *id_string, *auth = NULL, *nicknamec = NULL;
3656 SilcUInt16 id_len, auth_len = 0;
3658 bool resolved, local, nick_change = FALSE, resolve = FALSE;
3659 SilcChannelEntry channel;
3660 SilcHashTableList htl;
3661 SilcChannelClientEntry chl;
3662 SilcServerResumeResolve r;
3665 ret = silc_buffer_unformat(buffer,
3666 SILC_STR_UI16_NSTRING(&id_string, &id_len),
3669 client_id = silc_id_str2id(id_string, id_len, SILC_ID_CLIENT);
3671 if (sock->type == SILC_SOCKET_TYPE_CLIENT) {
3672 /* Client send this and is attempting to resume to old client session */
3673 SilcClientEntry client;
3677 silc_buffer_pull(buffer, 2 + id_len);
3678 auth = buffer->data;
3679 auth_len = buffer->len;
3680 silc_buffer_push(buffer, 2 + id_len);
3683 if (!client_id || auth_len < 128) {
3684 SILC_LOG_ERROR(("Client %s (%s) sent incomplete resume information, "
3685 "closing connection", sock->hostname, sock->ip));
3686 silc_server_disconnect_remote(server, sock,
3687 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
3688 "Resuming not possible");
3689 if (sock->user_data)
3690 silc_server_free_sock_user_data(server, sock, NULL);
3691 silc_free(client_id);
3695 /* Take client entry of this connection */
3696 client = (SilcClientEntry)sock->user_data;
3697 idata = (SilcIDListData)client;
3699 /* Get entry to the client, and resolve it if we don't have it. */
3700 detached_client = silc_server_query_client(server, client_id, FALSE,
3702 if (!detached_client) {
3704 /* The client info is being resolved. Reprocess this packet after
3705 receiving the reply to the query. */
3706 SILC_LOG_DEBUG(("Resolving client"));
3707 r = silc_calloc(1, sizeof(*r));
3711 r->sock = silc_socket_dup(sock);
3712 r->packet = silc_packet_context_dup(packet);
3713 r->data = client_id;
3714 silc_server_command_pending(server, SILC_COMMAND_WHOIS,
3716 silc_server_command_resume_resolve, r);
3718 SILC_LOG_ERROR(("Client %s (%s) tried to resume unknown client, "
3719 "closing connection", sock->hostname, sock->ip));
3720 silc_server_disconnect_remote(server, sock,
3721 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
3722 "Resuming not possible");
3723 if (sock->user_data)
3724 silc_server_free_sock_user_data(server, sock, NULL);
3725 silc_free(client_id);
3730 if (detached_client->data.status & SILC_IDLIST_STATUS_RESUMED) {
3731 SILC_LOG_ERROR(("Client %s (%s) tried to attach more than once, "
3732 "closing connection", sock->hostname, sock->ip));
3733 silc_server_disconnect_remote(server, sock,
3734 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
3735 "Resuming not possible");
3736 if (sock->user_data)
3737 silc_server_free_sock_user_data(server, sock, NULL);
3738 silc_free(client_id);
3743 if (detached_client->resuming_client &&
3744 detached_client->resuming_client != client) {
3745 SILC_LOG_ERROR(("Client %s (%s) tried to attach more than once, "
3746 "closing connection", sock->hostname, sock->ip));
3747 silc_server_disconnect_remote(server, sock,
3748 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
3749 "Resuming not possible");
3750 if (sock->user_data)
3751 silc_server_free_sock_user_data(server, sock, NULL);
3752 silc_free(client_id);
3757 if (!detached_client->resuming_client)
3758 detached_client->resuming_client = client;
3760 if (!(detached_client->mode & SILC_UMODE_DETACHED))
3762 if (!silc_hash_table_count(detached_client->channels) &&
3763 detached_client->router)
3765 if (!detached_client->nickname)
3767 if (detached_client->data.status & SILC_IDLIST_STATUS_RESUME_RES)
3771 if (server->server_type == SILC_SERVER && !server->standalone) {
3772 /* The client info is being resolved. Reprocess this packet after
3773 receiving the reply to the query. */
3774 SILC_LOG_DEBUG(("Resolving client info"));
3775 silc_server_query_client(server, client_id, TRUE, NULL);
3776 r = silc_calloc(1, sizeof(*r));
3780 r->sock = silc_socket_dup(sock);
3781 r->packet = silc_packet_context_dup(packet);
3782 r->data = client_id;
3783 silc_server_command_pending(server, SILC_COMMAND_WHOIS,
3785 silc_server_command_resume_resolve, r);
3788 if (server->server_type == SILC_SERVER) {
3789 SILC_LOG_ERROR(("Client %s (%s) tried to resume un-detached client, "
3790 "closing connection", sock->hostname, sock->ip));
3791 silc_server_disconnect_remote(server, sock,
3792 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
3793 "Resuming not possible");
3794 if (sock->user_data)
3795 silc_server_free_sock_user_data(server, sock, NULL);
3796 silc_free(client_id);
3801 /* Check that we have the public key of the client, if not then we must
3802 resolve it first. */
3803 if (!detached_client->data.public_key) {
3804 if (server->server_type == SILC_SERVER && server->standalone) {
3805 SILC_LOG_ERROR(("Detached client's public key not present, "
3806 "closing connection"));
3807 silc_server_disconnect_remote(server, sock,
3808 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
3809 "Resuming not possible");
3810 if (sock->user_data)
3811 silc_server_free_sock_user_data(server, sock, NULL);
3812 silc_free(client_id);
3814 /* We must retrieve the detached client's public key by sending
3815 GETKEY command. Reprocess this packet after receiving the key */
3816 SilcBuffer idp = silc_id_payload_encode(client_id, SILC_ID_CLIENT);
3817 SilcSocketConnection dest_sock =
3818 silc_server_get_client_route(server, NULL, 0, client_id, NULL, NULL);
3820 SILC_LOG_DEBUG(("Resolving client public key"));
3822 silc_server_send_command(server, dest_sock ? dest_sock :
3823 SILC_PRIMARY_ROUTE(server),
3824 SILC_COMMAND_GETKEY, ++server->cmd_ident,
3825 1, 1, idp->data, idp->len);
3827 r = silc_calloc(1, sizeof(*r));
3829 silc_free(client_id);
3834 r->sock = silc_socket_dup(sock);
3835 r->packet = silc_packet_context_dup(packet);
3836 silc_server_command_pending(server, SILC_COMMAND_GETKEY,
3838 silc_server_command_resume_resolve, r);
3840 silc_buffer_free(idp);
3842 silc_free(client_id);
3844 } else if (!silc_pkcs_public_key_compare(detached_client->data.public_key,
3845 idata->public_key)) {
3846 /* We require that the connection and resuming authentication data
3847 must be using same key pair. */
3848 SILC_LOG_ERROR(("Resuming attempted with wrong public key, "
3849 "closing connection"));
3850 silc_server_disconnect_remote(server, sock,
3851 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
3852 "Resuming not possible");
3853 if (sock->user_data)
3854 silc_server_free_sock_user_data(server, sock, NULL);
3855 silc_free(client_id);
3859 /* Verify the authentication payload. This has to be successful in
3860 order to allow the resuming */
3862 !silc_auth_verify_data(auth, auth_len, SILC_AUTH_PUBLIC_KEY,
3863 detached_client->data.public_key, 0,
3864 idata->hash, detached_client->id,
3866 SILC_LOG_ERROR(("Client %s (%s) resume authentication failed, "
3867 "closing connection", sock->hostname, sock->ip));
3868 silc_server_disconnect_remote(server, sock,
3869 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
3870 "Resuming not possible");
3871 if (sock->user_data)
3872 silc_server_free_sock_user_data(server, sock, NULL);
3873 silc_free(client_id);
3877 /* Check nickname */
3878 nicknamec = silc_identifier_check(detached_client->nickname,
3879 strlen(detached_client->nickname),
3880 SILC_STRING_UTF8, 128, NULL);
3882 silc_server_disconnect_remote(server, sock,
3883 SILC_STATUS_ERR_BAD_NICKNAME,
3884 "Malformed nickname, cannot resume");
3885 if (sock->user_data)
3886 silc_server_free_sock_user_data(server, sock, NULL);
3890 /* If the ID is not based in our ID then change it */
3891 if (!SILC_ID_COMPARE(detached_client->id, server->id,
3892 server->id->ip.data_len)) {
3893 silc_free(client_id);
3894 if (!silc_id_create_client_id(server, server->id, server->rng,
3895 server->md5hash, nicknamec,
3896 strlen(nicknamec), &client_id)) {
3897 silc_server_disconnect_remote(server, sock,
3898 SILC_STATUS_ERR_BAD_NICKNAME,
3899 "Resuming not possible");
3900 if (sock->user_data)
3901 silc_server_free_sock_user_data(server, sock, NULL);
3907 /* Now resume the client to the network */
3909 silc_schedule_task_del_by_context(server->schedule, detached_client);
3910 sock->user_data = detached_client;
3911 detached_client->connection = sock;
3913 if (detached_client->data.public_key)
3914 silc_hash_table_del_by_context(server->pk_hash,
3915 detached_client->data.public_key,
3917 if (idata->public_key)
3918 silc_hash_table_del_by_context(server->pk_hash,
3919 idata->public_key, idata);
3921 /* Take new keys and stuff into use in the old entry */
3922 silc_idlist_del_data(detached_client);
3923 silc_idlist_add_data(detached_client, idata);
3925 if (detached_client->data.public_key)
3926 silc_hash_table_add(server->pk_hash,
3927 detached_client->data.public_key, detached_client);
3929 detached_client->data.status |= SILC_IDLIST_STATUS_REGISTERED;
3930 detached_client->data.status |= SILC_IDLIST_STATUS_RESUMED;
3931 detached_client->data.status |= SILC_IDLIST_STATUS_LOCAL;
3932 detached_client->data.status &= ~SILC_IDLIST_STATUS_RESUME_RES;
3933 detached_client->mode &= ~SILC_UMODE_DETACHED;
3934 server->stat.my_detached--;
3936 /* We are finished - reset resuming client */
3937 detached_client->resuming_client = NULL;
3939 /* Check if anyone is watching this client */
3940 if (server->server_type == SILC_ROUTER)
3941 silc_server_check_watcher_list(server, detached_client, NULL,
3942 SILC_NOTIFY_TYPE_UMODE_CHANGE);
3944 /* Delete this current client entry since we're resuming to old one. */
3945 server->stat.my_clients--;
3946 server->stat.clients--;
3947 if (server->stat.cell_clients)
3948 server->stat.cell_clients--;
3949 silc_server_remove_from_channels(server, NULL, client, FALSE,
3950 NULL, FALSE, FALSE);
3951 silc_server_del_from_watcher_list(server, client);
3952 if (!silc_idlist_del_client(server->local_list, client))
3953 silc_idlist_del_client(server->global_list, client);
3954 client = detached_client;
3955 silc_free(client->servername);
3956 client->servername = strdup(server->server_name);
3958 /* Send the RESUME_CLIENT packet to our primary router so that others
3959 know this client isn't detached anymore. */
3960 buf = silc_buffer_alloc_size(2 + id_len);
3961 silc_buffer_format(buf,
3962 SILC_STR_UI_SHORT(id_len),
3963 SILC_STR_UI_XNSTRING(id_string, id_len),
3966 /* Send to primary router */
3967 silc_server_packet_send(server, SILC_PRIMARY_ROUTE(server),
3968 SILC_PACKET_RESUME_CLIENT, 0,
3969 buf->data, buf->len, TRUE);
3970 silc_server_backup_send(server, client->router,
3971 SILC_PACKET_RESUME_CLIENT, 0,
3972 buf->data, buf->len, TRUE, TRUE);
3974 /* As router we must deliver this packet directly to the original
3975 server whom this client was earlier. */
3976 if (server->server_type == SILC_ROUTER && client->router &&
3977 client->router->server_type != SILC_ROUTER)
3978 silc_server_packet_send(server, client->router->connection,
3979 SILC_PACKET_RESUME_CLIENT, 0,
3980 buf->data, buf->len, TRUE);
3981 silc_buffer_free(buf);
3982 client->router = NULL;
3985 /* Notify about Client ID change, nickname doesn't actually change. */
3986 silc_server_send_notify_nick_change(server, SILC_PRIMARY_ROUTE(server),
3987 SILC_BROADCAST(server),
3988 client->id, client_id,
3992 /* Resolve users on those channels that client has joined but we
3993 haven't resolved user list yet. */
3994 if (server->server_type == SILC_SERVER && !server->standalone) {
3995 silc_hash_table_list(client->channels, &htl);
3996 while (silc_hash_table_get(&htl, NULL, (void *)&chl)) {
3997 channel = chl->channel;
3998 SILC_LOG_DEBUG(("Resolving users for %s channel",
3999 channel->channel_name));
4000 if (channel->disabled || !channel->users_resolved) {
4001 silc_server_send_command(server, SILC_PRIMARY_ROUTE(server),
4002 SILC_COMMAND_USERS, ++server->cmd_ident,
4003 1, 2, channel->channel_name,
4004 strlen(channel->channel_name));
4007 silc_hash_table_list_reset(&htl);
4010 /* Send the new client ID to the client. After this client may start
4011 receiving other packets, and may start sending packets too. */
4012 silc_server_send_new_id(server, sock, FALSE, client_id, SILC_ID_CLIENT,
4013 silc_id_get_len(client_id, SILC_ID_CLIENT));
4016 /* Send NICK change notify to channels as well. */
4017 SilcBuffer oidp, nidp;
4018 oidp = silc_id_payload_encode(client->id, SILC_ID_CLIENT);
4019 nidp = silc_id_payload_encode(client_id, SILC_ID_CLIENT);
4020 silc_server_send_notify_on_channels(server, NULL, client,
4021 SILC_NOTIFY_TYPE_NICK_CHANGE, 3,
4022 oidp->data, oidp->len,
4023 nidp->data, nidp->len,
4025 strlen(client->nickname));
4026 silc_buffer_free(oidp);
4027 silc_buffer_free(nidp);
4030 /* Add the client again to the ID cache to get it to correct list */
4031 if (!silc_idcache_del_by_context(server->local_list->clients, client))
4032 silc_idcache_del_by_context(server->global_list->clients, client);
4033 silc_free(client->id);
4034 client->id = client_id;
4036 silc_idcache_add(server->local_list->clients, nicknamec,
4037 client->id, client, 0, NULL);
4039 /* Send some nice info to the client */
4040 silc_server_send_connect_notifys(server, sock, client);
4042 /* Send all channel keys of channels the client has joined */
4043 silc_hash_table_list(client->channels, &htl);
4044 while (silc_hash_table_get(&htl, NULL, (void *)&chl)) {
4045 bool created = FALSE;
4046 channel = chl->channel;
4048 if (channel->mode & SILC_CHANNEL_MODE_PRIVKEY)
4051 /* If we don't have channel key, then create one */
4052 if (!channel->channel_key) {
4053 if (!silc_server_create_channel_key(server, channel, 0))
4058 id_string = silc_id_id2str(channel->id, SILC_ID_CHANNEL);
4059 cipher = silc_cipher_get_name(channel->channel_key);
4061 silc_channel_key_payload_encode(silc_id_get_len(channel->id,
4064 strlen(cipher), cipher,
4065 channel->key_len / 8, channel->key);
4066 silc_free(id_string);
4068 /* Send the channel key to the client */
4069 silc_server_packet_send(server, sock, SILC_PACKET_CHANNEL_KEY, 0,
4070 keyp->data, keyp->len, FALSE);
4072 /* Distribute the channel key to channel */
4074 silc_server_send_channel_key(server, NULL, channel,
4075 server->server_type == SILC_ROUTER ?
4076 FALSE : !server->standalone);
4077 silc_server_backup_send(server, NULL, SILC_PACKET_CHANNEL_KEY, 0,
4078 keyp->data, keyp->len, FALSE, TRUE);
4081 silc_buffer_free(keyp);
4083 silc_hash_table_list_reset(&htl);
4085 } else if (sock->type != SILC_SOCKET_TYPE_CLIENT) {
4086 /* Server or router sent this to us to notify that that a client has
4088 SilcServerEntry server_entry;
4089 SilcServerID *server_id;
4092 SILC_LOG_DEBUG(("Malformed resuming packet"));
4096 /* Get entry to the client, and resolve it if we don't have it. */
4097 detached_client = silc_idlist_find_client_by_id(server->local_list,
4100 if (!detached_client) {
4101 detached_client = silc_idlist_find_client_by_id(server->global_list,
4104 if (!detached_client) {
4105 SILC_LOG_DEBUG(("Resuming client is unknown"));
4106 silc_free(client_id);
4111 /* Check that the client has not been resumed already because it is
4112 protocol error to attempt to resume more than once. The client
4113 will be killed if this protocol error occurs. */
4114 if (detached_client->data.status & SILC_IDLIST_STATUS_RESUMED &&
4115 !(detached_client->mode & SILC_UMODE_DETACHED)) {
4116 /* The client is clearly attempting to resume more than once and
4117 perhaps playing around by resuming from several different places
4118 at the same time. */
4119 SILC_LOG_DEBUG(("Attempting to re-resume client, killing both"));
4120 silc_server_kill_client(server, detached_client, NULL,
4121 server->id, SILC_ID_SERVER);
4122 silc_free(client_id);
4126 /* Check whether client is detached at all */
4127 if (!(detached_client->mode & SILC_UMODE_DETACHED)) {
4128 SILC_LOG_DEBUG(("Client is not detached"));
4129 silc_free(client_id);
4133 /* Check nickname */
4134 if (detached_client->nickname) {
4135 nicknamec = silc_identifier_check(detached_client->nickname,
4136 strlen(detached_client->nickname),
4137 SILC_STRING_UTF8, 128, NULL);
4139 silc_free(client_id);
4144 SILC_LOG_DEBUG(("Resuming detached client"));
4146 /* If the sender of this packet is server and we are router we need to
4147 broadcast this packet to other routers in the network. */
4148 if (server->server_type == SILC_ROUTER &&
4149 sock->type == SILC_SOCKET_TYPE_SERVER &&
4150 !(packet->flags & SILC_PACKET_FLAG_BROADCAST)) {
4151 SILC_LOG_DEBUG(("Broadcasting received Resume Client packet"));
4152 silc_server_packet_send(server, SILC_PRIMARY_ROUTE(server),
4154 packet->flags | SILC_PACKET_FLAG_BROADCAST,
4155 buffer->data, buffer->len, FALSE);
4156 silc_server_backup_send(server, sock->user_data,
4157 packet->type, packet->flags,
4158 packet->buffer->data, packet->buffer->len,
4162 /* Client is detached, and now it is resumed. Remove the detached
4163 mode and mark that it is resumed. */
4165 if (detached_client->data.public_key)
4166 silc_hash_table_del_by_context(server->pk_hash,
4167 detached_client->data.public_key,
4170 silc_idlist_del_data(detached_client);
4171 detached_client->mode &= ~SILC_UMODE_DETACHED;
4172 detached_client->data.status |= SILC_IDLIST_STATUS_RESUMED;
4173 detached_client->data.status &= ~SILC_IDLIST_STATUS_LOCAL;
4174 id_cache->expire = 0;
4176 /* Check if anyone is watching this client */
4177 if (server->server_type == SILC_ROUTER)
4178 silc_server_check_watcher_list(server, detached_client, NULL,
4179 SILC_NOTIFY_TYPE_UMODE_CHANGE);
4181 silc_schedule_task_del_by_context(server->schedule, detached_client);
4183 /* Get the new owner of the resumed client */
4184 server_id = silc_id_str2id(packet->src_id, packet->src_id_len,
4185 packet->src_id_type);
4187 silc_free(client_id);
4191 /* Get server entry */
4192 server_entry = silc_idlist_find_server_by_id(server->global_list,
4193 server_id, TRUE, NULL);
4195 if (!server_entry) {
4196 server_entry = silc_idlist_find_server_by_id(server->local_list,
4197 server_id, TRUE, NULL);
4199 if (!server_entry) {
4200 silc_free(server_id);
4201 silc_free(client_id);
4206 if (server->server_type == SILC_ROUTER &&
4207 sock->type == SILC_SOCKET_TYPE_ROUTER &&
4208 server_entry->server_type == SILC_ROUTER)
4211 /* Change the client to correct list. */
4212 if (!silc_idcache_del_by_context(server->local_list->clients,
4214 silc_idcache_del_by_context(server->global_list->clients,
4216 silc_idcache_add(local && server->server_type == SILC_ROUTER ?
4217 server->local_list->clients :
4218 server->global_list->clients, nicknamec,
4219 detached_client->id, detached_client, FALSE, NULL);
4221 /* Change the owner of the client */
4222 detached_client->router = server_entry;
4224 /* Update channel information regarding global clients on channel. */
4225 if (server->server_type != SILC_ROUTER) {
4226 silc_hash_table_list(detached_client->channels, &htl);
4227 while (silc_hash_table_get(&htl, NULL, (void *)&chl))
4228 chl->channel->global_users =
4229 silc_server_channel_has_global(chl->channel);
4230 silc_hash_table_list_reset(&htl);
4233 silc_free(server_id);
4236 silc_free(client_id);