5 Author: Pekka Riikonen <priikone@silcnet.org>
7 Copyright (C) 1997 - 2007 Pekka Riikonen
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
21 #include "serverincludes.h"
22 #include "server_internal.h"
24 /* Received notify packet. Server can receive notify packets from router.
25 Server then relays the notify messages to clients if needed. */
27 static void silc_server_notify_process(SilcServer server,
28 SilcPacketStream sock,
32 SilcIDListData idata = silc_packet_get_context(sock);
33 SilcNotifyPayload payload;
35 SilcArgumentPayload args;
36 SilcChannelID channel_id;
38 SilcChannelEntry channel = NULL;
39 SilcClientEntry client = NULL, client2 = NULL;
40 SilcServerEntry server_entry = NULL;
41 SilcChannelClientEntry chl;
42 SilcIDCacheEntry cache = NULL;
43 SilcHashTableList htl;
45 unsigned char *tmp, *tmp2;
46 SilcUInt32 tmp_len, tmp2_len;
49 if (idata->conn_type == SILC_CONN_CLIENT ||
50 packet->src_id_type != SILC_ID_SERVER || !packet->dst_id) {
51 SILC_LOG_DEBUG(("Bad notify packet received"));
55 /* If the packet is destined directly to a client then relay the packet
56 before processing it. */
57 if (packet->dst_id_type == SILC_ID_CLIENT) {
59 SilcPacketStream dst_sock;
61 /* Get the route to the client */
62 dst_sock = silc_server_get_client_route(server, packet->dst_id,
63 packet->dst_id_len, NULL,
66 /* Relay the packet */
67 silc_server_packet_route(server, dst_sock, packet);
70 /* Parse the Notify Payload */
71 payload = silc_notify_payload_parse(buffer->data, silc_buffer_len(buffer));
75 /* If we are router and this packet is not already broadcast packet
76 we will broadcast it. The sending socket really cannot be router or
77 the router is buggy. If this packet is coming from router then it must
78 have the broadcast flag set already and we won't do anything. */
79 if (server->server_type == SILC_ROUTER &&
80 idata->conn_type == SILC_CONN_SERVER &&
81 !(packet->flags & SILC_PACKET_FLAG_BROADCAST)) {
82 SILC_LOG_DEBUG(("Broadcasting received Notify packet"));
83 if (packet->dst_id_type == SILC_ID_CHANNEL) {
84 /* Packet is destined to channel */
85 if (!silc_id_str2id(packet->dst_id, packet->dst_id_len,
86 packet->dst_id_type, &channel_id,
90 silc_server_packet_send_dest(server, SILC_PRIMARY_ROUTE(server),
91 packet->type, packet->flags |
92 SILC_PACKET_FLAG_BROADCAST,
93 &channel_id, SILC_ID_CHANNEL,
94 buffer->data, silc_buffer_len(buffer));
95 silc_server_backup_send_dest(server, (SilcServerEntry)idata,
96 packet->type, packet->flags,
97 &channel_id, SILC_ID_CHANNEL,
98 buffer->data, silc_buffer_len(buffer),
101 /* Packet is destined to client or server */
102 silc_server_packet_send(server, SILC_PRIMARY_ROUTE(server),
104 packet->flags | SILC_PACKET_FLAG_BROADCAST,
105 buffer->data, silc_buffer_len(buffer));
106 silc_server_backup_send(server, (SilcServerEntry)idata,
107 packet->type, packet->flags,
108 buffer->data, silc_buffer_len(buffer),
113 type = silc_notify_get_type(payload);
114 args = silc_notify_get_args(payload);
119 case SILC_NOTIFY_TYPE_JOIN:
121 * Distribute the notify to local clients on the channel
123 SILC_LOG_DEBUG(("JOIN notify"));
126 if (!silc_argument_get_decoded(args, 2, SILC_ARGUMENT_ID, &id, NULL))
129 /* Get channel entry */
130 channel = silc_idlist_find_channel_by_id(server->global_list,
131 SILC_ID_GET_ID(id), NULL);
133 channel = silc_idlist_find_channel_by_id(server->local_list,
134 SILC_ID_GET_ID(id), NULL);
136 SILC_LOG_DEBUG(("Notify for unknown channel"));
142 if (!silc_argument_get_decoded(args, 1, SILC_ARGUMENT_ID, &id, NULL))
145 /* If the the client is not in local list we check global list (ie. the
146 channel will be global channel) and if it does not exist then create
147 entry for the client. */
148 client = silc_idlist_find_client_by_id(server->global_list,
153 client = silc_idlist_find_client_by_id(server->local_list,
158 /* If router did not find the client the it is bogus */
159 if (server->server_type != SILC_SERVER)
163 silc_idlist_add_client(server->global_list, NULL, NULL, NULL,
164 silc_id_dup(SILC_ID_GET_ID(id),
166 (SilcServerEntry)idata, NULL);
168 SILC_LOG_ERROR(("Could not add new client to the ID Cache"));
172 client->data.status |= SILC_IDLIST_STATUS_REGISTERED;
176 /* Do not process the notify if the client is not registered */
177 if (!(client->data.status & SILC_IDLIST_STATUS_REGISTERED))
180 /* Do not add client to channel if it is there already */
181 if (silc_server_client_on_channel(client, channel, NULL)) {
182 SILC_LOG_DEBUG(("Client already on channel %s",
183 channel->channel_name));
187 /* Send to channel */
188 silc_server_packet_send_to_channel(server, sock, channel, packet->type,
189 FALSE, TRUE, buffer->data,
190 silc_buffer_len(buffer));
192 if (server->server_type != SILC_ROUTER &&
193 idata->conn_type == SILC_CONN_ROUTER)
194 /* The channel is global now */
195 channel->global_users = TRUE;
197 SILC_LOG_DEBUG(("Joining to channel %s", channel->channel_name));
199 /* JOIN the global client to the channel (local clients (if router
200 created the channel) is joined in the pending JOIN command). */
201 chl = silc_calloc(1, sizeof(*chl));
204 chl->client = client;
205 chl->channel = channel;
207 if (server->server_type != SILC_ROUTER ||
208 idata->conn_type == SILC_CONN_ROUTER) {
209 /* If founder auth is set, first client is not automatically founder. */
210 if (!(channel->mode & SILC_CHANNEL_MODE_FOUNDER_AUTH)) {
211 /* If this is the first one on the channel then it is the founder of
212 the channel. This is done on normal server and on router if this
213 notify is coming from router */
214 if (!silc_hash_table_count(channel->user_list)) {
215 SILC_LOG_DEBUG(("Client %s is founder on channel",
216 silc_id_render(chl->client->id, SILC_ID_CLIENT)));
217 chl->mode = (SILC_CHANNEL_UMODE_CHANOP | SILC_CHANNEL_UMODE_CHANFO);
222 silc_hash_table_add(channel->user_list, client, chl);
223 silc_hash_table_add(client->channels, channel, chl);
224 channel->user_count++;
225 channel->disabled = FALSE;
227 /* Update statistics */
228 if (server->server_type == SILC_ROUTER) {
229 if (idata->conn_type != SILC_CONN_ROUTER)
230 server->stat.cell_chanclients++;
231 server->stat.chanclients++;
236 case SILC_NOTIFY_TYPE_LEAVE:
238 * Distribute the notify to local clients on the channel
240 SILC_LOG_DEBUG(("LEAVE notify"));
242 if (!silc_id_str2id(packet->dst_id, packet->dst_id_len,
243 packet->dst_id_type, &channel_id,
247 /* Get channel entry */
248 channel = silc_idlist_find_channel_by_id(server->global_list,
251 channel = silc_idlist_find_channel_by_id(server->local_list,
254 SILC_LOG_DEBUG(("Notify for unknown channel"));
260 if (!silc_argument_get_decoded(args, 1, SILC_ARGUMENT_ID, &id, NULL))
263 /* Get client entry */
264 client = silc_idlist_find_client_by_id(server->global_list,
265 SILC_ID_GET_ID(id), TRUE, NULL);
267 client = silc_idlist_find_client_by_id(server->local_list,
268 SILC_ID_GET_ID(id), TRUE, NULL);
273 /* Check if on channel */
274 if (!silc_server_client_on_channel(client, channel, NULL))
277 /* Send the leave notify to channel */
278 silc_server_packet_send_to_channel(server, sock, channel, packet->type,
279 FALSE, TRUE, buffer->data,
280 silc_buffer_len(buffer));
282 /* Remove the user from channel */
283 silc_server_remove_from_one_channel(server, sock, channel, client, FALSE);
286 case SILC_NOTIFY_TYPE_SIGNOFF:
288 * Distribute the notify to local clients on the channel
290 SILC_LOG_DEBUG(("SIGNOFF notify"));
293 if (!silc_argument_get_decoded(args, 1, SILC_ARGUMENT_ID, &id, NULL))
296 /* Get client entry */
297 client = silc_idlist_find_client_by_id(server->global_list,
298 SILC_ID_GET_ID(id), TRUE, &cache);
300 client = silc_idlist_find_client_by_id(server->local_list,
301 SILC_ID_GET_ID(id), TRUE, &cache);
306 /* Get signoff message */
307 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
311 /* Update statistics */
312 server->stat.clients--;
313 if (server->stat.cell_clients)
314 server->stat.cell_clients--;
315 SILC_OPER_STATS_UPDATE(client, server, SILC_UMODE_SERVER_OPERATOR);
316 SILC_OPER_STATS_UPDATE(client, router, SILC_UMODE_ROUTER_OPERATOR);
317 silc_schedule_task_del_by_context(server->schedule, client);
319 /* Remove from public key hash table. */
320 if (client->data.public_key)
321 silc_hash_table_del_by_context(server->pk_hash, client->data.public_key,
324 /* Remove the client from all channels. */
325 silc_server_remove_from_channels(server, NULL, client, TRUE,
328 /* Check if anyone is watching this nickname */
329 if (server->server_type == SILC_ROUTER)
330 silc_server_check_watcher_list(server, client, NULL,
331 SILC_NOTIFY_TYPE_SIGNOFF);
333 /* Remove this client from watcher list if it is */
334 silc_server_del_from_watcher_list(server, client);
336 client->data.status &= ~SILC_IDLIST_STATUS_REGISTERED;
338 client->router = NULL;
339 client->connection = NULL;
342 case SILC_NOTIFY_TYPE_TOPIC_SET:
344 * Distribute the notify to local clients on the channel
347 SILC_LOG_DEBUG(("TOPIC SET notify"));
350 if (!silc_argument_get_decoded(args, 1, SILC_ARGUMENT_ID, &id, NULL))
353 /* Get client entry */
354 if (id.type == SILC_ID_CLIENT) {
355 client = silc_idlist_find_client_by_id(server->global_list,
356 SILC_ID_GET_ID(id), TRUE, &cache);
358 client = silc_idlist_find_client_by_id(server->local_list,
367 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
371 if (!silc_id_str2id(packet->dst_id, packet->dst_id_len,
372 packet->dst_id_type, &channel_id,
376 /* Get channel entry */
377 channel = silc_idlist_find_channel_by_id(server->global_list,
380 channel = silc_idlist_find_channel_by_id(server->local_list,
383 SILC_LOG_DEBUG(("Notify for unknown channel"));
388 if (channel->topic && !strcmp(channel->topic, tmp)) {
389 SILC_LOG_DEBUG(("Topic is already set and same"));
394 /* Get user's channel entry and check that topic set is allowed. */
395 if (!silc_server_client_on_channel(client, channel, &chl))
397 if (channel->mode & SILC_CHANNEL_MODE_TOPIC &&
398 !(chl->mode & SILC_CHANNEL_UMODE_CHANOP) &&
399 !(chl->mode & SILC_CHANNEL_UMODE_CHANFO)) {
400 SILC_LOG_DEBUG(("Topic change is not allowed"));
405 /* Change the topic */
406 silc_free(channel->topic);
407 channel->topic = strdup(tmp);
409 /* Send the same notify to the channel */
410 silc_server_packet_send_to_channel(server, NULL, channel, packet->type,
411 FALSE, TRUE, buffer->data,
412 silc_buffer_len(buffer));
415 case SILC_NOTIFY_TYPE_NICK_CHANGE:
418 * Distribute the notify to local clients on the channel
421 SilcUInt32 nickname_len;
423 SILC_LOG_DEBUG(("NICK CHANGE notify"));
425 /* Get old client ID */
426 if (!silc_argument_get_decoded(args, 1, SILC_ARGUMENT_ID, &id, NULL))
429 /* Get new client ID */
430 if (!silc_argument_get_decoded(args, 2, SILC_ARGUMENT_ID, &id2, NULL))
433 SILC_LOG_DEBUG(("Old Client ID id(%s)",
434 silc_id_render(SILC_ID_GET_ID(id), SILC_ID_CLIENT)));
435 SILC_LOG_DEBUG(("New Client ID id(%s)",
436 silc_id_render(SILC_ID_GET_ID(id2), SILC_ID_CLIENT)));
438 /* From protocol version 1.1 we also get the new nickname */
439 nickname = silc_argument_get_arg_type(args, 3, &nickname_len);;
441 /* Replace the Client ID */
442 client = silc_idlist_replace_client_id(server,
445 SILC_ID_GET_ID(id2), nickname);
447 client = silc_idlist_replace_client_id(server,
450 SILC_ID_GET_ID(id2), nickname);
453 /* Send the NICK_CHANGE notify type to local clients on the channels
454 this client is joined to. */
455 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
456 tmp2 = silc_argument_get_arg_type(args, 2, &tmp2_len);
457 silc_server_send_notify_on_channels(server, client, client,
458 SILC_NOTIFY_TYPE_NICK_CHANGE, 3,
459 tmp, tmp_len, tmp2, tmp2_len,
467 case SILC_NOTIFY_TYPE_CMODE_CHANGE:
469 * Distribute the notify to local clients on the channel
472 SILC_LOG_DEBUG(("CMODE CHANGE notify"));
475 if (!silc_argument_get_decoded(args, 1, SILC_ARGUMENT_ID, &id, NULL))
478 /* Get client entry */
479 if (id.type == SILC_ID_CLIENT) {
480 client = silc_idlist_find_client_by_id(server->global_list,
481 SILC_ID_GET_ID(id), TRUE, &cache);
483 client = silc_idlist_find_client_by_id(server->local_list,
491 if (!silc_id_str2id(packet->dst_id, packet->dst_id_len,
492 packet->dst_id_type, &channel_id,
496 /* Get channel entry */
497 channel = silc_idlist_find_channel_by_id(server->global_list,
500 channel = silc_idlist_find_channel_by_id(server->local_list,
503 SILC_LOG_DEBUG(("Notify for unknown channel"));
509 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
512 SILC_GET32_MSB(mode, tmp);
514 /* Check if mode changed */
515 if (channel->mode == mode) {
516 SILC_LOG_DEBUG(("Mode is changed already"));
518 /* If this mode change has founder mode then we'll enforce the
519 change so that the server gets the real founder public key */
520 if (server->server_type != SILC_SERVER &&
521 sock != SILC_PRIMARY_ROUTE(server) &&
522 mode & SILC_CHANNEL_MODE_FOUNDER_AUTH && channel->founder_key) {
523 SILC_LOG_DEBUG(("Sending founder public key to server"));
524 silc_server_send_notify_cmode(server, sock, FALSE, channel,
525 channel->mode, server->id,
526 SILC_ID_SERVER, channel->cipher,
529 channel->founder_key, NULL);
532 /* If we received same mode from our primary check whether founder
533 mode and key in the notify is set. We update the founder key
534 here since we may have wrong one */
535 if (server->server_type != SILC_ROUTER &&
536 sock == SILC_PRIMARY_ROUTE(server) &&
537 mode & SILC_CHANNEL_MODE_FOUNDER_AUTH) {
538 SILC_LOG_DEBUG(("Founder public key received from router"));
539 tmp = silc_argument_get_arg_type(args, 6, &tmp_len);
543 if (channel->founder_key)
544 silc_pkcs_public_key_free(channel->founder_key);
545 channel->founder_key = NULL;
546 silc_public_key_payload_decode(tmp, tmp_len,
547 &channel->founder_key);
550 /* Check also for channel public key list */
551 if (server->server_type == SILC_SERVER &&
552 sock == SILC_PRIMARY_ROUTE(server) &&
553 mode & SILC_CHANNEL_MODE_CHANNEL_AUTH) {
556 unsigned char mask[4], ulimit[4];
558 SILC_LOG_DEBUG(("Channel public key list received from router"));
559 tmp = silc_argument_get_arg_type(args, 7, &tmp_len);
563 /* Set the router's list, and send the notify to channel too so that
564 channel gets the list */
565 silc_server_set_channel_pk_list(server, sock, channel, tmp, tmp_len);
566 chpklist = silc_server_get_channel_pk_list(server, channel,
570 sidp = silc_id_payload_encode(server->router->id, SILC_ID_SERVER);
571 SILC_PUT32_MSB(channel->mode, mask);
572 if (channel->mode & SILC_CHANNEL_MODE_ULIMIT)
573 SILC_PUT32_MSB(channel->user_limit, ulimit);
574 silc_server_send_notify_to_channel(server, NULL, channel, FALSE, TRUE,
575 SILC_NOTIFY_TYPE_CMODE_CHANGE, 8,
576 sidp->data, silc_buffer_len(sidp),
580 strlen(channel->cipher) : 0,
583 strlen(channel->hmac_name) : 0,
585 channel->passphrase ?
586 strlen(channel->passphrase) : 0,
589 silc_buffer_len(chpklist),
591 SILC_CHANNEL_MODE_ULIMIT ?
594 SILC_CHANNEL_MODE_ULIMIT ?
595 sizeof(ulimit) : 0));
596 silc_buffer_free(sidp);
597 silc_buffer_free(chpklist);
604 /* Get user's channel entry and check that mode change is allowed */
606 if (!silc_server_client_on_channel(client, channel, &chl))
608 if (!silc_server_check_cmode_rights(server, channel, chl, mode)) {
609 SILC_LOG_DEBUG(("CMODE change is not allowed"));
610 silc_server_send_notify_cmode(server, sock, FALSE, channel,
611 channel->mode, server->id,
612 SILC_ID_SERVER, channel->cipher,
615 channel->founder_key, NULL);
619 /* Assure that server is not removing founder mode from us */
620 if (server->server_type == SILC_ROUTER &&
621 sock != SILC_PRIMARY_ROUTE(server) &&
622 channel->mode & SILC_CHANNEL_MODE_FOUNDER_AUTH &&
623 !(mode & SILC_CHANNEL_MODE_FOUNDER_AUTH)) {
624 SILC_LOG_DEBUG(("Enforcing sender to change channel mode"));
625 silc_server_send_notify_cmode(server, sock, FALSE, channel,
626 channel->mode, server->id,
627 SILC_ID_SERVER, channel->cipher,
630 channel->founder_key, NULL);
634 /* If server is adding founder mode, check whether there is founder
635 on channel already and is not from this server */
636 if (server->server_type == SILC_ROUTER &&
637 sock != SILC_PRIMARY_ROUTE(server) &&
638 mode & SILC_CHANNEL_MODE_FOUNDER_AUTH) {
639 silc_hash_table_list(channel->user_list, &htl);
640 while (silc_hash_table_get(&htl, NULL, (void *)&chl))
641 if (chl->mode & SILC_CHANNEL_UMODE_CHANFO &&
642 chl->client->router != (SilcServerEntry)idata) {
643 SILC_LOG_DEBUG(("Enforcing sender to change channel mode"));
644 silc_server_send_notify_cmode(server, sock, FALSE, channel,
645 channel->mode, server->id,
646 SILC_ID_SERVER, channel->cipher,
649 channel->founder_key, NULL);
650 silc_hash_table_list_reset(&htl);
653 silc_hash_table_list_reset(&htl);
657 /* If the channel had private keys set and the mode was removed then
658 we must re-generate and re-distribute a new channel key */
659 if (channel->mode & SILC_CHANNEL_MODE_PRIVKEY &&
660 !(mode & SILC_CHANNEL_MODE_PRIVKEY)) {
661 /* Re-generate channel key */
662 if (!silc_server_create_channel_key(server, channel, 0))
665 /* Send the channel key. This sends it to our local clients and if
666 we are normal server to our router as well. */
667 silc_server_send_channel_key(server, NULL, channel,
668 server->server_type == SILC_ROUTER ?
669 FALSE : !server->standalone);
673 tmp = silc_argument_get_arg_type(args, 4, &tmp_len);
675 unsigned char hash[SILC_HASH_MAXLEN];
678 silc_hmac_free(channel->hmac);
679 if (!silc_hmac_alloc(tmp, NULL, &channel->hmac))
682 /* Set the HMAC key out of current channel key. The client must do
684 silc_hash_make(silc_hmac_get_hash(channel->hmac), channel->key,
685 channel->key_len / 8, hash);
686 silc_hmac_set_key(channel->hmac, hash,
687 silc_hash_len(silc_hmac_get_hash(channel->hmac)));
688 memset(hash, 0, sizeof(hash));
691 /* Get the passphrase */
692 tmp = silc_argument_get_arg_type(args, 5, &tmp_len);
694 silc_free(channel->passphrase);
695 channel->passphrase = silc_memdup(tmp, tmp_len);
698 /* Get founder public key */
699 tmp = silc_argument_get_arg_type(args, 6, &tmp_len);
700 if (tmp && mode & SILC_CHANNEL_MODE_FOUNDER_AUTH) {
701 if (channel->founder_key)
702 silc_pkcs_public_key_free(channel->founder_key);
703 channel->founder_key = NULL;
704 SILC_LOG_DEBUG(("Founder public key received"));
705 if (!silc_public_key_payload_decode(tmp, tmp_len,
706 &channel->founder_key)) {
707 SILC_LOG_DEBUG(("Enforcing sender to change channel mode"));
708 mode &= ~SILC_CHANNEL_MODE_FOUNDER_AUTH;
709 silc_server_send_notify_cmode(server, sock, FALSE, channel,
710 mode, server->id, SILC_ID_SERVER,
713 channel->passphrase, NULL, NULL);
714 if (channel->founder_key)
715 silc_pkcs_public_key_free(channel->founder_key);
716 channel->founder_key = NULL;
720 if (mode & SILC_CHANNEL_MODE_FOUNDER_AUTH && !channel->founder_key &&
721 server->server_type == SILC_ROUTER) {
722 SILC_LOG_DEBUG(("Enforcing sender to change channel mode"));
723 mode &= ~SILC_CHANNEL_MODE_FOUNDER_AUTH;
724 silc_server_send_notify_cmode(server, sock, FALSE, channel,
725 mode, server->id, SILC_ID_SERVER,
728 channel->passphrase, NULL, NULL);
731 /* Process channel public key(s). */
732 tmp = silc_argument_get_arg_type(args, 7, &tmp_len);
733 if (tmp && mode & SILC_CHANNEL_MODE_CHANNEL_AUTH) {
735 SILC_LOG_DEBUG(("Channel public key list received from router"));
738 silc_server_set_channel_pk_list(server, sock, channel, tmp, tmp_len);
740 /* If list was set already we will enforce the same list to server. */
741 if (ret == SILC_STATUS_ERR_OPERATION_ALLOWED) {
742 SilcBuffer chpklist = silc_server_get_channel_pk_list(server, channel,
744 silc_server_send_notify_cmode(server, sock, FALSE, channel,
745 mode, server->id, SILC_ID_SERVER,
748 channel->passphrase, NULL,
750 silc_buffer_free(chpklist);
754 /* Get the user limit */
755 tmp = silc_argument_get_arg_type(args, 8, &tmp_len);
756 if (tmp && tmp_len == 4 && mode & SILC_CHANNEL_MODE_ULIMIT)
757 SILC_GET32_MSB(channel->user_limit, tmp);
759 /* Send the same notify to the channel */
760 silc_server_packet_send_to_channel(server, NULL, channel, packet->type,
761 FALSE, TRUE, buffer->data,
762 silc_buffer_len(buffer));
765 channel->mode = mode;
767 /* Cleanup if some modes are removed */
769 if (!(channel->mode & SILC_CHANNEL_MODE_FOUNDER_AUTH) &&
770 channel->founder_key) {
771 silc_pkcs_public_key_free(channel->founder_key);
772 channel->founder_key = NULL;
775 if (!(channel->mode & SILC_CHANNEL_MODE_CHANNEL_AUTH) &&
776 channel->channel_pubkeys) {
777 silc_hash_table_free(channel->channel_pubkeys);
778 channel->channel_pubkeys = NULL;
783 case SILC_NOTIFY_TYPE_CUMODE_CHANGE:
786 * Distribute the notify to local clients on the channel
788 SilcChannelClientEntry chl2 = NULL;
789 SilcBool notify_sent = FALSE;
791 SILC_LOG_DEBUG(("CUMODE CHANGE notify"));
794 if (!silc_argument_get_decoded(args, 1, SILC_ARGUMENT_ID, &id, NULL))
797 /* Get client entry */
798 if (id.type == SILC_ID_CLIENT) {
799 client = silc_idlist_find_client_by_id(server->global_list,
803 client = silc_idlist_find_client_by_id(server->local_list,
811 if (!silc_id_str2id(packet->dst_id, packet->dst_id_len,
812 packet->dst_id_type, &channel_id,
816 /* Get channel entry */
817 channel = silc_idlist_find_channel_by_id(server->global_list,
820 channel = silc_idlist_find_channel_by_id(server->local_list,
823 SILC_LOG_DEBUG(("Notify for unknown channel"));
829 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
833 SILC_GET32_MSB(mode, tmp);
835 /* Get target client */
836 if (!silc_argument_get_decoded(args, 3, SILC_ARGUMENT_ID, &id, NULL))
839 /* Get client entry */
840 client2 = silc_idlist_find_client_by_id(server->global_list,
841 SILC_ID_GET_ID(id), TRUE, NULL);
843 client2 = silc_idlist_find_client_by_id(server->local_list,
851 /* Check that sender is on channel */
852 if (!silc_server_client_on_channel(client, channel, &chl))
855 if (client != client2 && server->server_type == SILC_ROUTER) {
856 /* Sender must be operator */
857 if (!(chl->mode & SILC_CHANNEL_UMODE_CHANOP) &&
858 !(chl->mode & SILC_CHANNEL_UMODE_CHANFO)) {
859 SILC_LOG_DEBUG(("CUMODE change is not allowed"));
863 if (!silc_server_client_on_channel(client2, channel, &chl))
866 /* If target is founder mode change is not allowed. */
867 if (chl->mode & SILC_CHANNEL_UMODE_CHANFO) {
868 SILC_LOG_DEBUG(("CUMODE change is not allowed"));
874 /* Get target channel user entry */
875 if (!silc_server_client_on_channel(client2, channel, &chl))
878 if (server->server_type == SILC_SERVER && chl->mode == mode) {
879 SILC_LOG_DEBUG(("Mode is changed already"));
883 /* Check whether to give founder rights to this user or not. The
884 problem here is that we get only the public key of the client,
885 but no authentication data. We must assume that server has
886 already authenticated the user (and thus we must trust the
888 if (mode & SILC_CHANNEL_UMODE_CHANFO &&
889 !(chl->mode & SILC_CHANNEL_UMODE_CHANFO) &&
890 server->server_type == SILC_ROUTER &&
891 sock != SILC_PRIMARY_ROUTE(server)) {
892 SilcPublicKey founder_key = NULL;
894 /* If channel doesn't have founder auth mode then it's impossible
895 that someone would be getting founder rights with CUMODE command.
896 In that case there already either is founder or there isn't
897 founder at all on the channel (valid only when 'client' is
899 if (client && !(channel->mode & SILC_CHANNEL_MODE_FOUNDER_AUTH)) {
900 /* Force the mode to not have founder mode */
901 chl->mode = mode &= ~SILC_CHANNEL_UMODE_CHANFO;
902 silc_server_force_cumode_change(server, sock, channel, chl, mode);
907 /* Get the founder of the channel and if found then this client
908 cannot be the founder since there already is one. */
909 silc_hash_table_list(channel->user_list, &htl);
910 while (silc_hash_table_get(&htl, NULL, (void *)&chl2))
911 if (chl2->mode & SILC_CHANNEL_UMODE_CHANFO) {
912 SILC_LOG_DEBUG(("Founder already on channel"));
913 chl->mode = mode &= ~SILC_CHANNEL_UMODE_CHANFO;
914 silc_server_force_cumode_change(server, sock, channel,
919 silc_hash_table_list_reset(&htl);
920 if (!(mode & SILC_CHANNEL_UMODE_CHANFO))
923 /* Founder not found on the channel. Since the founder auth mode
924 is set on the channel now check whether this is the client that
925 originally set the mode. */
927 if (channel->founder_key) {
928 /* Get public key that must be present in notify */
929 tmp = silc_argument_get_arg_type(args, 4, &tmp_len);
930 if (!tmp || !silc_public_key_payload_decode(tmp, tmp_len,
932 chl->mode = mode &= ~SILC_CHANNEL_UMODE_CHANFO;
933 SILC_LOG_DEBUG(("Founder public key not present"));
934 silc_server_force_cumode_change(server, sock, channel, chl, mode);
939 /* Now match the public key we have cached and public key sent.
941 if (!silc_pkcs_public_key_compare(channel->founder_key,
943 chl->mode = mode &= ~SILC_CHANNEL_UMODE_CHANFO;
944 SILC_LOG_DEBUG(("Founder public key mismatch"));
945 silc_server_force_cumode_change(server, sock, channel, chl, mode);
951 /* There cannot be anyone else as founder on the channel now. This
952 client is definitely the founder due to this 'authentication'.
953 We trust the server did the actual signature verification
954 earlier (bad, yes). */
955 silc_hash_table_list(channel->user_list, &htl);
956 while (silc_hash_table_get(&htl, NULL, (void *)&chl2))
957 if (chl2->mode & SILC_CHANNEL_UMODE_CHANFO) {
958 chl2->mode &= ~SILC_CHANNEL_UMODE_CHANFO;
959 SILC_LOG_DEBUG(("Removing old founder rights, new authenticated"));
960 silc_server_force_cumode_change(server, NULL, channel, chl2,
964 silc_hash_table_list_reset(&htl);
967 silc_pkcs_public_key_free(founder_key);
970 if (server->server_type != SILC_SERVER && chl->mode == mode) {
971 SILC_LOG_DEBUG(("Mode is changed already"));
975 SILC_LOG_DEBUG(("Changing %s channel user mode",
976 chl->client->nickname ? chl->client->nickname :
977 (unsigned char *)""));
979 /* Change the mode */
982 /* Send the same notify to the channel */
984 silc_server_packet_send_to_channel(server, sock, channel,
986 FALSE, TRUE, buffer->data,
987 silc_buffer_len(buffer));
992 case SILC_NOTIFY_TYPE_INVITE:
994 if (packet->dst_id_type == SILC_ID_CLIENT)
997 SILC_LOG_DEBUG(("INVITE notify"));
1000 if (!silc_argument_get_decoded(args, 1, SILC_ARGUMENT_ID, &id, NULL))
1003 /* Get channel entry */
1004 channel = silc_idlist_find_channel_by_id(server->global_list,
1005 SILC_ID_GET_ID(id), NULL);
1007 channel = silc_idlist_find_channel_by_id(server->local_list,
1008 SILC_ID_GET_ID(id), NULL);
1010 SILC_LOG_DEBUG(("Notify for unknown channel"));
1015 /* Get the invite action */
1016 tmp = silc_argument_get_arg_type(args, 4, &tmp_len);
1017 if (tmp && tmp_len == 1) {
1018 SilcUInt8 action = (SilcUInt8)tmp[0];
1019 SilcUInt16 iargc = 0;
1020 SilcArgumentPayload iargs;
1022 /* Get invite list */
1023 tmp = silc_argument_get_arg_type(args, 5, &tmp_len);
1024 if (!tmp || tmp_len < 2)
1027 /* Parse the arguments to see they are constructed correctly */
1028 SILC_GET16_MSB(iargc, tmp);
1029 iargs = silc_argument_payload_parse(tmp + 2, tmp_len - 2, iargc);
1033 if (!channel->invite_list)
1034 channel->invite_list =
1035 silc_hash_table_alloc(0, silc_hash_ptr,
1037 silc_server_inviteban_destruct, channel, TRUE);
1039 /* Proces the invite action */
1040 if (!silc_server_inviteban_process(server, channel->invite_list, action,
1043 silc_argument_payload_free(iargs);
1045 /* If we are router we must send this notify to our local servers on
1046 the channel. Normal server does nothing. The notify is not
1048 if (server->server_type == SILC_ROUTER)
1049 silc_server_packet_send_to_channel(server, sock, channel,
1050 packet->type, FALSE, FALSE,
1052 silc_buffer_len(buffer));
1057 case SILC_NOTIFY_TYPE_CHANNEL_CHANGE:
1059 * Distribute to the local clients on the channel and change the
1063 SILC_LOG_DEBUG(("CHANNEL CHANGE"));
1065 if (idata->conn_type != SILC_CONN_ROUTER)
1068 /* Get the old Channel ID */
1069 if (!silc_argument_get_decoded(args, 1, SILC_ARGUMENT_ID, &id, NULL))
1072 /* Get the channel entry */
1073 channel = silc_idlist_find_channel_by_id(server->local_list,
1074 SILC_ID_GET_ID(id), NULL);
1076 channel = silc_idlist_find_channel_by_id(server->global_list,
1077 SILC_ID_GET_ID(id), NULL);
1079 SILC_LOG_DEBUG(("Notify for unknown channel"));
1084 /* Send the notify to the channel */
1085 silc_server_packet_send_to_channel(server, sock, channel, packet->type,
1086 FALSE, TRUE, buffer->data,
1087 silc_buffer_len(buffer));
1089 /* Get the new Channel ID */
1090 if (!silc_argument_get_decoded(args, 2, SILC_ARGUMENT_ID, &id2, NULL))
1093 SILC_LOG_DEBUG(("Old Channel ID id(%s)",
1094 silc_id_render(SILC_ID_GET_ID(id), SILC_ID_CHANNEL)));
1095 SILC_LOG_DEBUG(("New Channel ID id(%s)",
1096 silc_id_render(SILC_ID_GET_ID(id2), SILC_ID_CHANNEL)));
1098 /* Replace the Channel ID */
1100 if (!silc_idlist_replace_channel_id(server->local_list,
1102 SILC_ID_GET_ID(id2)))
1103 if (!silc_idlist_replace_channel_id(server->global_list,
1105 SILC_ID_GET_ID(id2)))
1109 SilcBuffer modes = NULL, users = NULL, users_modes = NULL;
1111 /* Re-announce this channel which ID was changed. */
1112 silc_server_send_new_channel(server, sock, FALSE, channel->channel_name,
1114 silc_id_get_len(channel->id,
1118 /* Re-announce our clients on the channel as the ID has changed now */
1119 silc_server_announce_get_channel_users(server, channel, &modes, &users,
1122 silc_buffer_push(users, users->data - users->head);
1123 silc_server_packet_send(server, sock,
1124 SILC_PACKET_NOTIFY, SILC_PACKET_FLAG_LIST,
1125 users->data, silc_buffer_len(users));
1126 silc_buffer_free(users);
1129 silc_buffer_push(modes, modes->data - modes->head);
1130 silc_server_packet_send_dest(server, sock,
1131 SILC_PACKET_NOTIFY, SILC_PACKET_FLAG_LIST,
1132 channel->id, SILC_ID_CHANNEL,
1133 modes->data, silc_buffer_len(modes));
1134 silc_buffer_free(modes);
1137 silc_buffer_push(users_modes, users_modes->data - users_modes->head);
1138 silc_server_packet_send_dest(server, sock,
1139 SILC_PACKET_NOTIFY, SILC_PACKET_FLAG_LIST,
1140 channel->id, SILC_ID_CHANNEL,
1142 silc_buffer_len(users_modes));
1143 silc_buffer_free(users_modes);
1146 /* Re-announce channel's topic */
1147 if (channel->topic) {
1148 silc_server_send_notify_topic_set(server, sock,
1149 server->server_type == SILC_ROUTER ?
1150 TRUE : FALSE, channel,
1151 server->id, SILC_ID_SERVER,
1158 case SILC_NOTIFY_TYPE_SERVER_SIGNOFF:
1160 * Remove the server entry and all clients that this server owns.
1163 SILC_LOG_DEBUG(("SERVER SIGNOFF notify"));
1165 /* Backup router shouldn't accept SERVER_SIGNOFF's from normal routers
1166 when the backup isn't acting as primary router. */
1167 if (idata->conn_type == SILC_CONN_SERVER &&
1168 server->backup_router && server->server_type == SILC_BACKUP_ROUTER)
1172 if (!silc_argument_get_decoded(args, 1, SILC_ARGUMENT_ID, &id, NULL))
1175 /* If the ID is mine, this notify is not allowed. */
1176 if (SILC_ID_SERVER_COMPARE(SILC_ID_GET_ID(id), server->id)) {
1177 SILC_LOG_DEBUG(("Ignoring my own ID for SERVER_SIGNOFF"));
1181 /* Get server entry */
1182 server_entry = silc_idlist_find_server_by_id(server->global_list,
1186 if (!server_entry) {
1187 server_entry = silc_idlist_find_server_by_id(server->local_list,
1191 if (!server_entry) {
1192 /* If we are normal server then we might not have the server. Check
1193 whether router was kind enough to send the list of all clients
1194 that actually was to be removed. Remove them if the list is
1196 if (server->server_type != SILC_ROUTER &&
1197 silc_argument_get_arg_num(args) > 1) {
1200 for (i = 1; i < silc_argument_get_arg_num(args); i++) {
1202 if (!silc_argument_get_decoded(args, i + 1, SILC_ARGUMENT_ID,
1206 /* Get client entry */
1207 client = silc_idlist_find_client_by_id(server->global_list,
1212 client = silc_idlist_find_client_by_id(server->local_list,
1220 /* Update statistics */
1221 server->stat.clients--;
1222 if (server->stat.cell_clients)
1223 server->stat.cell_clients--;
1224 SILC_OPER_STATS_UPDATE(client, server, SILC_UMODE_SERVER_OPERATOR);
1225 SILC_OPER_STATS_UPDATE(client, router, SILC_UMODE_ROUTER_OPERATOR);
1227 /* Remove the client from all channels. */
1228 silc_server_remove_from_channels(server, NULL, client,
1229 TRUE, NULL, FALSE, FALSE);
1231 /* Check if anyone is watching this nickname */
1232 if (server->server_type == SILC_ROUTER)
1233 silc_server_check_watcher_list(server, client, NULL,
1234 SILC_NOTIFY_TYPE_SERVER_SIGNOFF);
1236 /* Remove this client from watcher list if it is */
1238 silc_server_del_from_watcher_list(server, client);
1240 /* Remove from public key hash table. */
1241 if (client->data.public_key)
1242 silc_hash_table_del_by_context(server->pk_hash,
1243 client->data.public_key,
1246 /* Remove the client */
1247 silc_idlist_del_data(client);
1248 silc_idlist_del_client(local ? server->local_list :
1249 server->global_list, client);
1257 /* For local entrys SERVER_SIGNOFF is processed only on backup router.
1258 It is possible that router sends server signoff for a server. If
1259 backup router has it as local connection it will be closed. */
1260 if (SILC_IS_LOCAL(server_entry)) {
1261 if (server->server_type == SILC_BACKUP_ROUTER) {
1262 sock = server_entry->connection;
1263 SILC_LOG_DEBUG(("Closing connection after SERVER_SIGNOFF"));
1264 silc_server_free_sock_user_data(server, sock, NULL);
1265 silc_server_close_connection(server, sock);
1271 /* Remove all servers that are originated from this server, and
1272 remove the clients of those servers too. */
1273 silc_server_remove_servers_by_server(server, server_entry, TRUE);
1275 /* Remove the clients that this server owns as they will become
1277 silc_server_remove_clients_by_server(server, server_entry->router,
1278 server_entry, TRUE);
1279 silc_server_backup_del(server, server_entry);
1281 /* Remove the server entry */
1282 silc_idlist_del_server(local ? server->local_list :
1283 server->global_list, server_entry);
1285 /* Update statistics */
1286 if (server->server_type == SILC_ROUTER)
1287 server->stat.servers--;
1291 case SILC_NOTIFY_TYPE_KICKED:
1293 * Distribute the notify to local clients on the channel
1296 SILC_LOG_DEBUG(("KICKED notify"));
1298 if (!silc_id_str2id(packet->dst_id, packet->dst_id_len,
1299 packet->dst_id_type, &channel_id,
1300 sizeof(channel_id)))
1303 /* Get channel entry */
1304 channel = silc_idlist_find_channel_by_id(server->global_list,
1307 channel = silc_idlist_find_channel_by_id(server->local_list,
1310 SILC_LOG_DEBUG(("Notify for unknown channel"));
1316 if (!silc_argument_get_decoded(args, 1, SILC_ARGUMENT_ID, &id, NULL))
1319 /* If the the client is not in local list we check global list */
1320 client = silc_idlist_find_client_by_id(server->global_list,
1321 SILC_ID_GET_ID(id), TRUE, NULL);
1323 client = silc_idlist_find_client_by_id(server->local_list,
1324 SILC_ID_GET_ID(id), TRUE, NULL);
1329 /* If target is founder they cannot be kicked */
1330 if (!silc_server_client_on_channel(client, channel, &chl))
1332 if (chl->mode & SILC_CHANNEL_UMODE_CHANFO)
1335 /* Get the kicker's Client ID */
1336 if (!silc_argument_get_decoded(args, 3, SILC_ARGUMENT_ID, &id, NULL))
1339 /* If the the client is not in local list we check global list */
1340 client2 = silc_idlist_find_client_by_id(server->global_list,
1341 SILC_ID_GET_ID(id), TRUE, NULL);
1343 client2 = silc_idlist_find_client_by_id(server->local_list,
1344 SILC_ID_GET_ID(id), TRUE, NULL);
1349 /* Kicker must be operator on channel */
1350 if (!silc_server_client_on_channel(client2, channel, &chl))
1352 if (!(chl->mode & SILC_CHANNEL_UMODE_CHANOP) &&
1353 !(chl->mode & SILC_CHANNEL_UMODE_CHANFO)) {
1354 SILC_LOG_DEBUG(("Kicking is not allowed"));
1358 /* Send to channel */
1359 silc_server_packet_send_to_channel(server, sock, channel, packet->type,
1360 FALSE, TRUE, buffer->data,
1361 silc_buffer_len(buffer));
1363 /* Remove the client from channel's invite list */
1364 if (channel->invite_list && silc_hash_table_count(channel->invite_list)) {
1366 SilcArgumentPayload iargs;
1367 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
1368 ab = silc_argument_payload_encode_one(NULL, tmp, tmp_len, 3);
1369 iargs = silc_argument_payload_parse(ab->data, silc_buffer_len(ab), 1);
1370 silc_server_inviteban_process(server, channel->invite_list, 1, iargs);
1371 silc_buffer_free(ab);
1372 silc_argument_payload_free(iargs);
1375 /* Remove the client from channel */
1376 silc_server_remove_from_one_channel(server, sock, channel, client, FALSE);
1380 case SILC_NOTIFY_TYPE_KILLED:
1383 * Distribute the notify to local clients on channels
1385 unsigned char *comment;
1386 SilcUInt32 comment_len;
1388 SILC_LOG_DEBUG(("KILLED notify"));
1391 if (!silc_argument_get_decoded(args, 1, SILC_ARGUMENT_ID, &id, NULL))
1394 /* If the the client is not in local list we check global list */
1395 client = silc_idlist_find_client_by_id(server->global_list,
1396 SILC_ID_GET_ID(id), TRUE, &cache);
1398 client = silc_idlist_find_client_by_id(server->local_list,
1405 /* If the client is one of ours, then close the connection to the
1406 client now. This removes the client from all channels as well. */
1407 if (packet->dst_id_type == SILC_ID_CLIENT && client->connection) {
1408 sock = client->connection;
1409 silc_server_free_client_data(server, NULL, client, FALSE, NULL);
1410 silc_server_close_connection(server, sock);
1415 comment = silc_argument_get_arg_type(args, 2, &comment_len);
1416 if (comment_len > 128)
1419 /* Get the killer's Client ID */
1420 if (!silc_argument_get_decoded(args, 3, SILC_ARGUMENT_ID, &id, NULL))
1423 if (id.type == SILC_ID_CLIENT) {
1424 /* If the the client is not in local list we check global list */
1425 client2 = silc_idlist_find_client_by_id(server->global_list,
1429 client2 = silc_idlist_find_client_by_id(server->local_list,
1436 /* Killer must be router operator */
1437 if (server->server_type != SILC_SERVER &&
1438 !(client2->mode & SILC_UMODE_ROUTER_OPERATOR)) {
1439 SILC_LOG_DEBUG(("Killing is not allowed"));
1444 /* Send the notify to local clients on the channels except to the
1445 client who is killed. */
1446 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
1447 tmp2 = silc_argument_get_arg_type(args, 3, &tmp2_len);
1448 silc_server_send_notify_on_channels(server, client, client,
1449 SILC_NOTIFY_TYPE_KILLED, 3,
1450 tmp, tmp_len, comment, comment_len,
1453 /* Remove the client from all channels */
1454 silc_server_remove_from_channels(server, NULL, client, FALSE, NULL,
1457 /* Check if anyone is watching this nickname */
1458 if (server->server_type == SILC_ROUTER)
1459 silc_server_check_watcher_list(server, client, NULL,
1460 SILC_NOTIFY_TYPE_KILLED);
1462 /* Remove from public key hash table. */
1463 if (client->data.public_key)
1464 silc_hash_table_del_by_context(server->pk_hash,
1465 client->data.public_key,
1468 /* Update statistics */
1469 server->stat.clients--;
1470 if (server->stat.cell_clients)
1471 server->stat.cell_clients--;
1472 SILC_OPER_STATS_UPDATE(client, server, SILC_UMODE_SERVER_OPERATOR);
1473 SILC_OPER_STATS_UPDATE(client, router, SILC_UMODE_ROUTER_OPERATOR);
1475 if (SILC_IS_LOCAL(client)) {
1476 server->stat.my_clients--;
1477 silc_schedule_task_del_by_context(server->schedule, client);
1478 silc_idlist_del_data(client);
1482 client->data.status &= ~SILC_IDLIST_STATUS_REGISTERED;
1484 client->router = NULL;
1485 client->connection = NULL;
1489 case SILC_NOTIFY_TYPE_UMODE_CHANGE:
1491 * Save the mode of the client.
1494 SILC_LOG_DEBUG(("UMODE_CHANGE notify"));
1497 if (!silc_argument_get_decoded(args, 1, SILC_ARGUMENT_ID, &id, NULL))
1500 /* Get client entry */
1501 client = silc_idlist_find_client_by_id(server->global_list,
1502 SILC_ID_GET_ID(id), TRUE, NULL);
1504 client = silc_idlist_find_client_by_id(server->local_list,
1505 SILC_ID_GET_ID(id), TRUE, NULL);
1511 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
1514 SILC_GET32_MSB(mode, tmp);
1516 /* Remove internal resumed flag if client is marked detached now */
1517 if (mode & SILC_UMODE_DETACHED)
1518 client->data.status &= ~SILC_IDLIST_STATUS_RESUMED;
1520 /* Update statistics */
1521 if (server->server_type == SILC_ROUTER) {
1522 if (mode & SILC_UMODE_GONE) {
1523 if (!(client->mode & SILC_UMODE_GONE))
1524 server->stat.aways++;
1526 if (client->mode & SILC_UMODE_GONE)
1527 server->stat.aways--;
1529 if (mode & SILC_UMODE_DETACHED) {
1530 if (!(client->mode & SILC_UMODE_DETACHED))
1531 server->stat.detached++;
1533 if (client->mode & SILC_UMODE_DETACHED)
1534 server->stat.detached--;
1537 SILC_UMODE_STATS_UPDATE(server, SILC_UMODE_SERVER_OPERATOR);
1538 SILC_UMODE_STATS_UPDATE(router, SILC_UMODE_ROUTER_OPERATOR);
1540 /* Change the mode */
1541 client->mode = mode;
1543 /* Check if anyone is watching this nickname */
1544 if (server->server_type == SILC_ROUTER)
1545 silc_server_check_watcher_list(server, client, NULL,
1546 SILC_NOTIFY_TYPE_UMODE_CHANGE);
1550 case SILC_NOTIFY_TYPE_BAN:
1555 SILC_LOG_DEBUG(("BAN notify"));
1557 /* Get Channel ID */
1558 if (!silc_argument_get_decoded(args, 1, SILC_ARGUMENT_ID, &id, NULL))
1561 /* Get channel entry */
1562 channel = silc_idlist_find_channel_by_id(server->global_list,
1563 SILC_ID_GET_ID(id), NULL);
1565 channel = silc_idlist_find_channel_by_id(server->local_list,
1566 SILC_ID_GET_ID(id), NULL);
1568 SILC_LOG_DEBUG(("Notify for unknown channel"));
1573 /* Get the ban action */
1574 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
1575 if (tmp && tmp_len == 1) {
1576 SilcUInt8 action = (SilcUInt8)tmp[0];
1577 SilcUInt16 iargc = 0;
1578 SilcArgumentPayload iargs;
1581 tmp = silc_argument_get_arg_type(args, 3, &tmp_len);
1582 if (!tmp || tmp_len < 2)
1585 /* Parse the arguments to see they are constructed correctly */
1586 SILC_GET16_MSB(iargc, tmp);
1587 iargs = silc_argument_payload_parse(tmp + 2, tmp_len - 2, iargc);
1591 if (!channel->ban_list)
1593 silc_hash_table_alloc(0, silc_hash_ptr,
1595 silc_server_inviteban_destruct, channel, TRUE);
1597 /* Proces the ban action */
1598 if (!silc_server_inviteban_process(server, channel->ban_list, action,
1601 silc_argument_payload_free(iargs);
1603 /* If we are router we must send this notify to our local servers on
1604 the channel. Normal server does nothing. The notify is not
1606 if (server->server_type == SILC_ROUTER)
1607 silc_server_packet_send_to_channel(server, sock, channel,
1608 packet->type, FALSE, FALSE,
1610 silc_buffer_len(buffer));
1614 case SILC_NOTIFY_TYPE_ERROR:
1621 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
1622 if (!tmp && tmp_len != 1)
1624 error = (SilcStatus)tmp[0];
1626 SILC_LOG_DEBUG(("ERROR notify (%d)", error));
1628 if (error == SILC_STATUS_ERR_NO_SUCH_CLIENT_ID &&
1629 idata->conn_type == SILC_CONN_ROUTER) {
1630 if (!silc_argument_get_decoded(args, 2, SILC_ARGUMENT_ID, &id, NULL))
1633 SILC_LOG_DEBUG(("Received invalid client ID notification, deleting "
1634 "the entry from cache"));
1636 client = silc_idlist_find_client_by_id(server->global_list,
1642 if (client->data.public_key)
1643 silc_hash_table_del_by_context(server->pk_hash,
1644 client->data.public_key,
1647 silc_server_remove_from_channels(server, NULL, client, TRUE,
1649 silc_idlist_del_data(client);
1650 silc_idlist_del_client(server->global_list, client);
1655 /* Ignore rest of the notify types for now */
1656 case SILC_NOTIFY_TYPE_NONE:
1657 case SILC_NOTIFY_TYPE_MOTD:
1665 silc_notify_payload_free(payload);
1668 void silc_server_notify(SilcServer server,
1669 SilcPacketStream sock,
1672 silc_server_notify_process(server, sock, packet, &packet->buffer);
1675 void silc_server_notify_list(SilcServer server,
1676 SilcPacketStream sock,
1679 SilcIDListData idata = silc_packet_get_context(sock);
1683 SILC_LOG_DEBUG(("Processing Notify List"));
1685 if (idata->conn_type == SILC_CONN_CLIENT ||
1686 packet->src_id_type != SILC_ID_SERVER)
1689 buffer = silc_buffer_alloc(1024);
1693 while (silc_buffer_len(&packet->buffer)) {
1694 SILC_GET16_MSB(len, packet->buffer.data + 2);
1695 if (len > silc_buffer_len(&packet->buffer))
1698 if (len > silc_buffer_truelen(buffer)) {
1699 silc_buffer_free(buffer);
1700 buffer = silc_buffer_alloc(1024 + len);
1703 silc_buffer_pull_tail(buffer, len);
1704 silc_buffer_put(buffer, packet->buffer.data, len);
1706 /* Process the Notify */
1707 silc_server_notify_process(server, sock, packet, buffer);
1709 silc_buffer_push_tail(buffer, len);
1710 silc_buffer_pull(&packet->buffer, len);
1713 silc_buffer_free(buffer);
1716 /* Received private message. This resolves the destination of the message
1717 and sends the packet. This is used by both server and router. If the
1718 destination is our locally connected client this sends the packet to
1719 the client. This may also send the message for further routing if
1720 the destination is not in our server (or router). */
1722 void silc_server_private_message(SilcServer server,
1723 SilcPacketStream sock,
1726 SilcPacketStream dst_sock;
1727 SilcIDListData idata;
1728 SilcClientEntry client;
1729 SilcClientID client_id;
1731 SILC_LOG_DEBUG(("Start"));
1733 if (packet->src_id_type != SILC_ID_CLIENT ||
1734 packet->dst_id_type != SILC_ID_CLIENT || !packet->dst_id)
1737 /* Get the route to the client */
1738 dst_sock = silc_server_get_client_route(server, packet->dst_id,
1739 packet->dst_id_len, NULL,
1743 unsigned char error;
1745 if (client && client->mode & SILC_UMODE_DETACHED) {
1746 SILC_LOG_DEBUG(("Client is detached, discarding packet"));
1750 /* Send SILC_NOTIFY_TYPE_ERROR to indicate that such destination ID
1751 does not exist or is invalid. */
1752 idp = silc_id_payload_encode_data(packet->dst_id,
1754 packet->dst_id_type);
1758 error = SILC_STATUS_ERR_NO_SUCH_CLIENT_ID;
1759 if (packet->src_id_type == SILC_ID_CLIENT) {
1760 silc_id_str2id(packet->src_id, packet->src_id_len,
1761 packet->src_id_type, &client_id, sizeof(client_id));
1762 silc_server_send_notify_dest(server, sock, FALSE,
1763 &client_id, SILC_ID_CLIENT,
1764 SILC_NOTIFY_TYPE_ERROR, 2,
1766 idp->data, silc_buffer_len(idp));
1768 silc_server_send_notify(server, sock, FALSE,
1769 SILC_NOTIFY_TYPE_ERROR, 2,
1771 idp->data, silc_buffer_len(idp));
1774 silc_buffer_free(idp);
1778 /* Check whether destination client wishes to receive private messages */
1779 if (client && !(packet->flags & SILC_PACKET_FLAG_PRIVMSG_KEY) &&
1780 client->mode & SILC_UMODE_BLOCK_PRIVMSG) {
1781 SILC_LOG_DEBUG(("Client blocks private messages, discarding packet"));
1785 /* Send the private message */
1786 silc_server_packet_route(server, dst_sock, packet);
1789 /* Received private message key packet.. This packet is never for us. It is to
1790 the client in the packet's destination ID. Sending of this sort of packet
1791 equals sending private message, ie. it is sent point to point from
1792 one client to another. */
1794 void silc_server_private_message_key(SilcServer server,
1795 SilcPacketStream sock,
1798 SilcPacketStream dst_sock;
1799 SilcIDListData idata;
1801 SILC_LOG_DEBUG(("Start"));
1803 if (packet->src_id_type != SILC_ID_CLIENT ||
1804 packet->dst_id_type != SILC_ID_CLIENT)
1807 if (!packet->dst_id)
1810 /* Get the route to the client */
1811 dst_sock = silc_server_get_client_route(server, packet->dst_id,
1812 packet->dst_id_len, NULL,
1817 /* Relay the packet */
1818 silc_server_packet_route(server, dst_sock, packet);
1821 /* Processes incoming command reply packet. The command reply packet may
1822 be destined to one of our clients or it may directly for us. We will
1823 call the command reply routine after processing the packet. */
1825 void silc_server_command_reply(SilcServer server,
1826 SilcPacketStream sock,
1829 SilcBuffer buffer = &packet->buffer;
1830 SilcClientEntry client = NULL;
1833 SILC_LOG_DEBUG(("Start"));
1835 if (packet->dst_id_type == SILC_ID_CHANNEL)
1838 if (packet->dst_id_type == SILC_ID_CLIENT) {
1839 /* Destination must be one of ours */
1840 if (!silc_id_str2id(packet->dst_id, packet->dst_id_len, SILC_ID_CLIENT,
1843 client = silc_idlist_find_client_by_id(server->local_list, &id,
1846 SILC_LOG_ERROR(("Cannot process command reply to unknown client"));
1851 if (packet->dst_id_type == SILC_ID_SERVER) {
1852 /* For now this must be for us */
1853 if (memcmp(packet->dst_id, server->id_string, server->id_string_len)) {
1854 SILC_LOG_ERROR(("Cannot process command reply to unknown server"));
1859 /* Execute command reply locally for the command */
1860 silc_server_command_reply_process(server, sock, buffer);
1862 /* Relay the packet to the client */
1863 if (packet->dst_id_type == SILC_ID_CLIENT && client)
1864 silc_server_packet_route(server, client->connection, packet);
1867 /* Process received channel message. The message can be originated from
1868 client or server. */
1870 void silc_server_channel_message(SilcServer server,
1871 SilcPacketStream sock,
1874 SilcChannelEntry channel = NULL;
1878 void *sender_id = NULL;
1879 SilcClientEntry sender_entry = NULL;
1880 SilcIDListData idata;
1881 SilcChannelClientEntry chl;
1882 SilcBool local = TRUE;
1884 SILC_LOG_DEBUG(("Processing channel message"));
1887 if (packet->dst_id_type != SILC_ID_CHANNEL) {
1888 SILC_LOG_DEBUG(("Received bad message for channel, dropped"));
1892 /* Find channel entry */
1893 if (!silc_id_str2id(packet->dst_id, packet->dst_id_len, SILC_ID_CHANNEL,
1896 channel = silc_idlist_find_channel_by_id(server->local_list, &id, NULL);
1898 channel = silc_idlist_find_channel_by_id(server->global_list, &id, NULL);
1901 unsigned char error;
1903 /* Send SILC_NOTIFY_TYPE_ERROR to indicate that such destination ID
1904 does not exist or is invalid. */
1905 idp = silc_id_payload_encode_data(packet->dst_id,
1907 packet->dst_id_type);
1911 error = SILC_STATUS_ERR_NO_SUCH_CHANNEL_ID;
1912 if (packet->src_id_type == SILC_ID_CLIENT) {
1913 silc_id_str2id(packet->src_id, packet->src_id_len,
1914 packet->src_id_type, &cid, sizeof(cid));
1915 silc_server_send_notify_dest(server, sock, FALSE,
1916 &cid, SILC_ID_CLIENT,
1917 SILC_NOTIFY_TYPE_ERROR, 2,
1918 &error, 1, idp->data,
1919 silc_buffer_len(idp));
1921 silc_server_send_notify(server, sock, FALSE,
1922 SILC_NOTIFY_TYPE_ERROR, 2,
1923 &error, 1, idp->data, silc_buffer_len(idp));
1926 silc_buffer_free(idp);
1931 /* See that this client is on the channel. If the original sender is
1932 not client (as it can be server as well) we don't do the check. */
1933 if (!silc_id_str2id2(packet->src_id, packet->src_id_len,
1934 packet->src_id_type, &sid))
1936 if (sid.type == SILC_ID_CLIENT) {
1937 sender_entry = silc_idlist_find_client_by_id(server->local_list,
1938 SILC_ID_GET_ID(sid),
1940 if (!sender_entry) {
1942 sender_entry = silc_idlist_find_client_by_id(server->global_list,
1943 SILC_ID_GET_ID(sid),
1946 if (!sender_entry || !silc_server_client_on_channel(sender_entry,
1948 SILC_LOG_DEBUG(("Client not on channel"));
1952 /* If channel is moderated check that client is allowed to send
1954 if (channel->mode & SILC_CHANNEL_MODE_SILENCE_USERS &&
1955 !(chl->mode & SILC_CHANNEL_UMODE_CHANOP) &&
1956 !(chl->mode & SILC_CHANNEL_UMODE_CHANFO)) {
1957 SILC_LOG_DEBUG(("Channel is silenced from normal users"));
1960 if (channel->mode & SILC_CHANNEL_MODE_SILENCE_OPERS &&
1961 chl->mode & SILC_CHANNEL_UMODE_CHANOP &&
1962 !(chl->mode & SILC_CHANNEL_UMODE_CHANFO)) {
1963 SILC_LOG_DEBUG(("Channel is silenced from operators"));
1966 if (chl->mode & SILC_CHANNEL_UMODE_QUIET) {
1967 SILC_LOG_DEBUG(("Sender is quieted on the channel"));
1971 /* If the packet is coming from router, but the client entry is local
1972 entry to us then some router is rerouting this to us and it is not
1973 allowed. When the client is local to us it means that we've routed
1974 this packet to network, and now someone is routing it back to us. */
1975 idata = silc_packet_get_context(sock);
1976 if (server->server_type == SILC_ROUTER &&
1977 idata->conn_type == SILC_CONN_ROUTER && local) {
1978 SILC_LOG_DEBUG(("Channel message rerouted to the sender, drop it"));
1983 /* Distribute the packet to our local clients. This will send the
1984 packet for further routing as well, if needed. */
1985 silc_server_packet_relay_to_channel(server, sock, channel, sender_id,
1986 packet->src_id_type, sender_entry,
1987 packet->buffer.data,
1988 silc_buffer_len(&packet->buffer));
1991 /* Received channel key packet. We distribute the key to all of our locally
1992 connected clients on the channel. */
1994 void silc_server_channel_key(SilcServer server,
1995 SilcPacketStream sock,
1998 SilcBuffer buffer = &packet->buffer;
1999 SilcIDListData idata = silc_packet_get_context(sock);
2000 SilcChannelEntry channel;
2002 if (packet->src_id_type != SILC_ID_SERVER ||
2003 (server->server_type == SILC_ROUTER && !server->backup_router &&
2004 idata->conn_type == SILC_CONN_ROUTER))
2007 /* Save the channel key */
2008 channel = silc_server_save_channel_key(server, buffer, NULL);
2010 SILC_LOG_ERROR(("Bad channel key from %s", idata->sconn->remote_host));
2014 /* Distribute the key to everybody who is on the channel. If we are router
2015 we will also send it to locally connected servers. */
2016 silc_server_send_channel_key(server, sock, channel, FALSE);
2018 if (server->server_type != SILC_BACKUP_ROUTER) {
2019 /* Distribute to local cell backup routers. */
2020 silc_server_backup_send(server, (SilcServerEntry)idata,
2021 SILC_PACKET_CHANNEL_KEY, 0,
2022 buffer->data, silc_buffer_len(buffer),
2027 /* Received New Client packet and processes it. Creates Client ID for the
2028 client. Client becomes registered after calling this functions. */
2030 SilcClientEntry silc_server_new_client(SilcServer server,
2031 SilcPacketStream sock,
2034 SilcBuffer buffer = &packet->buffer;
2035 SilcIDListData idata = silc_packet_get_context(sock);
2036 SilcClientEntry client;
2037 SilcClientID *client_id;
2038 char *username = NULL, *realname = NULL;
2039 SilcUInt16 username_len;
2040 SilcUInt32 id_len, tmp_len;
2042 char *host, *nickname, *nicknamec;
2043 const char *hostname, *ip;
2045 SILC_LOG_DEBUG(("Creating new client"));
2047 if (idata->conn_type != SILC_CONN_CLIENT)
2050 /* Take client entry */
2051 client = (SilcClientEntry)idata;
2052 silc_socket_stream_get_info(sock, NULL, &hostname, &ip, NULL);
2054 /* Remove the old cache entry. */
2055 if (!silc_idcache_del_by_context(server->local_list->clients, client,
2057 SILC_LOG_INFO(("Unauthenticated client attempted to register to network"));
2058 silc_server_disconnect_remote(server, sock,
2059 SILC_STATUS_ERR_NOT_AUTHENTICATED, NULL);
2060 silc_server_free_sock_user_data(server, sock, NULL);
2064 /* Make sure this client hasn't registered already */
2065 if (idata->status & SILC_IDLIST_STATUS_REGISTERED) {
2066 silc_server_disconnect_remote(server, sock,
2067 SILC_STATUS_ERR_OPERATION_ALLOWED,
2068 "Too many registrations");
2069 silc_server_free_sock_user_data(server, sock, NULL);
2073 /* Parse incoming packet */
2074 ret = silc_buffer_unformat(buffer,
2075 SILC_STR_UI16_NSTRING_ALLOC(&username,
2077 SILC_STR_UI16_STRING_ALLOC(&realname),
2080 silc_free(username);
2081 silc_free(realname);
2082 SILC_LOG_ERROR(("Client %s (%s) sent incomplete information, closing "
2083 "connection", hostname, ip));
2084 silc_server_disconnect_remote(server, sock,
2085 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
2087 silc_server_free_sock_user_data(server, sock, NULL);
2092 silc_free(username);
2093 silc_free(realname);
2094 SILC_LOG_ERROR(("Client %s (%s) did not send its username, closing "
2095 "connection", hostname, ip));
2096 silc_server_disconnect_remote(server, sock,
2097 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
2099 silc_server_free_sock_user_data(server, sock, NULL);
2103 if (username_len > 128) {
2104 username[128] = '\0';
2108 /* Check for valid username string */
2109 nicknamec = silc_identifier_check(username, username_len,
2110 SILC_STRING_UTF8, 128, &tmp_len);
2112 silc_free(username);
2113 silc_free(realname);
2114 SILC_LOG_ERROR(("Client %s (%s) sent bad username string '%s', closing "
2115 "connection", hostname, ip, username));
2116 silc_server_disconnect_remote(server, sock,
2117 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
2119 silc_server_free_sock_user_data(server, sock, NULL);
2123 /* Nickname is initially same as username */
2124 nickname = strdup(username);
2126 /* Make sanity checks for the hostname of the client. If the hostname
2127 is provided in the `username' check that it is the same than the
2128 resolved hostname, or if not resolved the hostname that appears in
2129 the client's public key. If the hostname is not present then put
2130 it from the resolved name or from the public key. */
2131 if (strchr(username, '@')) {
2132 SilcSILCPublicKey silc_pubkey;
2133 int tlen = strcspn(username, "@");
2134 char *phostname = NULL;
2136 host = silc_memdup(username + tlen + 1, strlen(username) - tlen - 1);
2138 if (strcmp(hostname, ip) && strcmp(hostname, host)) {
2139 silc_free(username);
2141 silc_free(realname);
2142 SILC_LOG_ERROR(("Client %s (%s) sent incomplete information, closing "
2143 "connection", hostname, ip));
2144 silc_server_disconnect_remote(server, sock,
2145 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
2147 silc_server_free_sock_user_data(server, sock, NULL);
2151 silc_pubkey = silc_pkcs_get_context(SILC_PKCS_SILC,
2152 client->data.public_key);
2153 phostname = strdup(silc_pubkey->identifier.host);
2154 if (!strcmp(hostname, ip) && phostname && strcmp(phostname, host)) {
2155 silc_free(username);
2157 silc_free(phostname);
2158 silc_free(realname);
2159 SILC_LOG_ERROR(("Client %s (%s) sent incomplete information, closing "
2160 "connection", hostname, ip));
2161 silc_server_disconnect_remote(server, sock,
2162 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
2164 silc_server_free_sock_user_data(server, sock, NULL);
2168 silc_free(phostname);
2170 /* The hostname is not present, add it. */
2172 newusername = silc_calloc(strlen(username) + strlen(hostname) + 2,
2173 sizeof(*newusername));
2174 strncat(newusername, username, strlen(username));
2175 strncat(newusername, "@", 1);
2176 strncat(newusername, hostname, strlen(hostname));
2177 silc_free(username);
2178 username = newusername;
2181 /* Create Client ID */
2182 if (!silc_id_create_client_id(server, server->id, server->rng,
2183 server->md5hash, nicknamec,
2184 strlen(nicknamec), &client_id)) {
2185 silc_server_disconnect_remote(server, sock,
2186 SILC_STATUS_ERR_BAD_NICKNAME, NULL);
2187 silc_server_free_sock_user_data(server, sock, NULL);
2191 /* If client marked as anonymous, scramble the username and hostname */
2192 if (client->mode & SILC_UMODE_ANONYMOUS) {
2195 if (strlen(username) >= 2) {
2196 username[0] = silc_rng_get_byte_fast(server->rng);
2197 username[1] = silc_rng_get_byte_fast(server->rng);
2200 scramble = silc_hash_babbleprint(server->sha1hash, username,
2204 memcpy(&scramble[16], ".silc", 5);
2205 scramble[21] = '\0';
2206 silc_free(username);
2207 username = scramble;
2210 /* Update client entry */
2211 idata->status |= SILC_IDLIST_STATUS_REGISTERED;
2212 client->nickname = nickname;
2213 client->username = username;
2214 client->userinfo = realname ? realname : strdup(username);
2215 client->id = client_id;
2216 id_len = silc_id_get_len(client_id, SILC_ID_CLIENT);
2218 /* Add the client again to the ID cache */
2219 silc_idcache_add(server->local_list->clients, nicknamec,
2222 /* Notify our router about new client on the SILC network */
2223 silc_server_send_new_id(server, SILC_PRIMARY_ROUTE(server),
2224 SILC_BROADCAST(server), client->id,
2225 SILC_ID_CLIENT, id_len);
2227 /* Distribute to backup routers */
2228 if (server->server_type == SILC_ROUTER) {
2229 SilcBuffer idp = silc_id_payload_encode(client->id, SILC_ID_CLIENT);
2230 silc_server_backup_send(server, NULL, SILC_PACKET_NEW_ID, 0,
2231 idp->data, silc_buffer_len(idp), FALSE, TRUE);
2232 silc_buffer_free(idp);
2235 /* Send the new client ID to the client. */
2236 silc_server_send_new_id(server, sock, FALSE, client->id, SILC_ID_CLIENT,
2237 silc_id_get_len(client->id, SILC_ID_CLIENT));
2239 /* Send some nice info to the client */
2240 silc_server_send_connect_notifys(server, sock, client);
2242 /* Check if anyone is watching this nickname */
2243 if (server->server_type == SILC_ROUTER)
2244 silc_server_check_watcher_list(server, client, NULL, 0);
2249 /* Create new server. This processes received New Server packet and
2250 saves the received Server ID. The server is our locally connected
2251 server thus we save all the information and save it to local list.
2252 This funtion can be used by both normal server and router server.
2253 If normal server uses this it means that its router has connected
2254 to the server. If router uses this it means that one of the cell's
2255 servers is connected to the router. */
2257 SilcServerEntry silc_server_new_server(SilcServer server,
2258 SilcPacketStream sock,
2261 SilcBuffer buffer = &packet->buffer;
2262 SilcIDListData idata = silc_packet_get_context(sock);
2263 SilcServerEntry new_server, server_entry;
2264 SilcServerID server_id;
2265 unsigned char *server_name, *server_namec, *id_string;
2266 SilcUInt16 id_len, name_len;
2268 SilcBool local = TRUE;
2269 const char *hostname, *ip;
2271 SILC_LOG_DEBUG(("Creating new server"));
2273 if (idata->conn_type != SILC_CONN_SERVER &&
2274 idata->conn_type != SILC_CONN_ROUTER)
2277 /* Take server entry */
2278 new_server = (SilcServerEntry)idata;
2279 silc_socket_stream_get_info(sock, NULL, &hostname, &ip, NULL);
2282 if (server->server_type == SILC_ROUTER)
2283 server->stat.cell_servers++;
2285 /* Remove the old cache entry */
2286 if (!silc_idcache_del_by_context(server->local_list->servers, new_server,
2288 if (!silc_idcache_del_by_context(server->global_list->servers,
2289 new_server, NULL)) {
2290 SILC_LOG_INFO(("Unauthenticated %s attempted to register to "
2291 "network", (idata->conn_type == SILC_CONN_SERVER ?
2292 "server" : "router")));
2293 silc_server_disconnect_remote(server, sock,
2294 SILC_STATUS_ERR_NOT_AUTHENTICATED, NULL);
2295 silc_server_free_sock_user_data(server, sock, NULL);
2301 /* Make sure this server hasn't registered already */
2302 if (idata->status & SILC_IDLIST_STATUS_REGISTERED) {
2303 silc_server_disconnect_remote(server, sock,
2304 SILC_STATUS_ERR_OPERATION_ALLOWED,
2305 "Too many registrations");
2306 silc_server_free_sock_user_data(server, sock, NULL);
2310 /* Parse the incoming packet */
2311 ret = silc_buffer_unformat(buffer,
2312 SILC_STR_UI16_NSTRING_ALLOC(&id_string, &id_len),
2313 SILC_STR_UI16_NSTRING_ALLOC(&server_name,
2317 silc_free(id_string);
2318 silc_free(server_name);
2319 silc_server_disconnect_remote(server, sock,
2320 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
2322 silc_server_free_sock_user_data(server, sock, NULL);
2326 if (id_len > silc_buffer_len(buffer)) {
2327 silc_free(id_string);
2328 silc_free(server_name);
2329 silc_server_disconnect_remote(server, sock,
2330 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
2332 silc_server_free_sock_user_data(server, sock, NULL);
2336 if (name_len > 256) {
2337 server_name[256] = '\0';
2342 if (!silc_id_str2id(id_string, id_len, SILC_ID_SERVER, &server_id,
2343 sizeof(server_id))) {
2344 silc_free(id_string);
2345 silc_free(server_name);
2346 silc_server_disconnect_remote(server, sock,
2347 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
2349 silc_server_free_sock_user_data(server, sock, NULL);
2352 silc_free(id_string);
2354 /* Check for valid server ID */
2355 if (!silc_id_is_valid_server_id(server, &server_id, sock)) {
2356 SILC_LOG_INFO(("Invalid server ID sent by %s (%s)",
2358 silc_server_disconnect_remote(server, sock,
2359 SILC_STATUS_ERR_BAD_SERVER_ID, NULL);
2360 silc_server_free_sock_user_data(server, sock, NULL);
2361 silc_free(server_name);
2365 /* Check that we do not have this ID already */
2366 server_entry = silc_idlist_find_server_by_id(server->local_list,
2367 &server_id, TRUE, NULL);
2369 if (SILC_IS_LOCAL(server_entry)) {
2370 SILC_LOG_ERROR(("Too many registrations from %s (%s)",
2372 silc_server_disconnect_remote(server, sock,
2373 SILC_STATUS_ERR_OPERATION_ALLOWED,
2374 "Too many registrations");
2375 silc_server_free_sock_user_data(server, sock, NULL);
2378 silc_idcache_del_by_context(server->local_list->servers, server_entry,
2382 server_entry = silc_idlist_find_server_by_id(server->global_list,
2383 &server_id, TRUE, NULL);
2385 if (SILC_IS_LOCAL(server_entry)) {
2386 SILC_LOG_ERROR(("Too many registrations from %s (%s)",
2388 silc_server_disconnect_remote(server, sock,
2389 SILC_STATUS_ERR_OPERATION_ALLOWED,
2390 "Too many registrations");
2391 silc_server_free_sock_user_data(server, sock, NULL);
2394 silc_idcache_del_by_context(server->global_list->servers,
2395 server_entry, NULL);
2400 /* Check server name */
2401 server_namec = silc_identifier_check(server_name, strlen(server_name),
2402 SILC_STRING_UTF8, 256, NULL);
2403 if (!server_namec) {
2404 SILC_LOG_ERROR(("Malformed server name from %s (%s)",
2406 silc_server_disconnect_remote(server, sock,
2407 SILC_STATUS_ERR_OPERATION_ALLOWED,
2408 "Malfromed server name");
2409 silc_server_free_sock_user_data(server, sock, NULL);
2413 /* Update server entry */
2414 idata->status |= SILC_IDLIST_STATUS_REGISTERED;
2415 new_server->server_name = server_name;
2416 new_server->id = silc_id_dup(&server_id, SILC_ID_SERVER);
2418 SILC_LOG_DEBUG(("New server id(%s)",
2419 silc_id_render(&server_id, SILC_ID_SERVER)));
2421 /* Add again the entry to the ID cache. */
2422 silc_idcache_add(local ? server->local_list->servers :
2423 server->global_list->servers, server_namec,
2424 new_server->id, new_server);
2426 /* Distribute the information about new server in the SILC network
2427 to our router. If we are normal server we won't send anything
2428 since this connection must be our router connection. */
2429 if (server->server_type == SILC_ROUTER && !server->standalone &&
2430 SILC_PRIMARY_ROUTE(server) != sock)
2431 silc_server_send_new_id(server, SILC_PRIMARY_ROUTE(server),
2432 TRUE, new_server->id, SILC_ID_SERVER,
2433 silc_id_get_len(&server_id, SILC_ID_SERVER));
2435 if (server->server_type == SILC_ROUTER) {
2436 /* Distribute to backup routers */
2437 SilcBuffer idp = silc_id_payload_encode(new_server->id, SILC_ID_SERVER);
2438 silc_server_backup_send(server, NULL, SILC_PACKET_NEW_ID, 0, idp->data,
2439 silc_buffer_len(idp), FALSE, TRUE);
2440 silc_buffer_free(idp);
2443 /* Check whether this router connection has been replaced by an
2444 backup router. If it has been then we'll disable the server and will
2445 ignore everything it will send until the backup router resuming
2446 protocol has been completed. */
2447 if (idata->conn_type == SILC_CONN_ROUTER &&
2448 silc_server_backup_replaced_get(server, &server_id, NULL)) {
2449 /* Send packet to the router indicating that it cannot use this
2450 connection as it has been replaced by backup router. */
2451 SILC_LOG_DEBUG(("Remote router has been replaced by backup router, "
2452 "disabling its connection"));
2454 silc_server_backup_send_replaced(server, sock);
2456 /* Mark the router disabled. The data sent earlier will go but nothing
2457 after this goes to this connection. */
2458 idata->status |= SILC_IDLIST_STATUS_DISABLED;
2460 /* If it is router announce our stuff to it. */
2461 if (idata->conn_type == SILC_CONN_ROUTER &&
2462 server->server_type == SILC_ROUTER) {
2463 silc_server_announce_servers(server, FALSE, 0, sock);
2464 silc_server_announce_clients(server, 0, sock);
2465 silc_server_announce_channels(server, 0, sock);
2468 /* Announce our information to backup router */
2469 if (new_server->server_type == SILC_BACKUP_ROUTER &&
2470 idata->conn_type == SILC_CONN_SERVER &&
2471 server->server_type == SILC_ROUTER) {
2472 silc_server_announce_servers(server, TRUE, 0, sock);
2473 silc_server_announce_clients(server, 0, sock);
2474 silc_server_announce_channels(server, 0, sock);
2477 /* If backup router, mark it as one of ours. This server is considered
2478 to be backup router after this setting. */
2479 if (new_server->server_type == SILC_BACKUP_ROUTER) {
2480 SilcServerConfigRouter *backup;
2481 backup = silc_server_config_find_backup_conn(server, (char *)ip);
2483 backup = silc_server_config_find_backup_conn(server, (char *)hostname);
2485 /* Add as our backup router */
2486 silc_server_backup_add(server, new_server, backup->backup_replace_ip,
2487 backup->backup_replace_port,
2488 backup->backup_local);
2492 /* By default the servers connected to backup router are disabled
2493 until backup router has become the primary */
2494 if (server->server_type == SILC_BACKUP_ROUTER &&
2495 idata->conn_type == SILC_CONN_SERVER)
2496 idata->status |= SILC_IDLIST_STATUS_DISABLED;
2502 /* Processes incoming New ID packet. New ID Payload is used to distribute
2503 information about newly registered clients and servers. */
2505 static void silc_server_new_id_real(SilcServer server,
2506 SilcPacketStream sock,
2511 SilcIDListData idata = silc_packet_get_context(sock);
2513 SilcServerEntry router, server_entry;
2514 SilcPacketStream router_sock;
2517 SilcServerID sender_id;
2518 const char *hostname, *ip;
2520 SILC_LOG_DEBUG(("Processing new ID"));
2522 if (idata->conn_type == SILC_CONN_CLIENT ||
2523 server->server_type == SILC_SERVER ||
2524 packet->src_id_type != SILC_ID_SERVER)
2527 idp = silc_id_payload_parse(buffer->data, silc_buffer_len(buffer));
2531 id_type = silc_id_payload_get_type(idp);
2533 silc_socket_stream_get_info(sock, NULL, &hostname, &ip, NULL);
2535 /* Normal server cannot have other normal server connections */
2536 server_entry = (SilcServerEntry)idata;
2537 if (id_type == SILC_ID_SERVER && idata->conn_type == SILC_CONN_SERVER &&
2538 server_entry->server_type == SILC_SERVER)
2541 /* If the packet is coming from server then use the sender as the
2542 origin of the the packet. If it came from router then check the real
2543 sender of the packet and use that as the origin. */
2544 if (idata->conn_type == SILC_CONN_SERVER) {
2545 id_list = server->local_list;
2547 router = server_entry;
2549 /* If the sender is backup router and ID is server (and we are not
2550 backup router) then switch the entry to global list. */
2551 if (server_entry->server_type == SILC_BACKUP_ROUTER &&
2552 id_type == SILC_ID_SERVER &&
2553 server->id_entry->server_type != SILC_BACKUP_ROUTER) {
2554 id_list = server->global_list;
2555 router_sock = server->router ? SILC_PRIMARY_ROUTE(server) : sock;
2558 silc_id_str2id(packet->src_id, packet->src_id_len,
2559 packet->src_id_type, &sender_id, sizeof(sender_id));
2560 router = silc_idlist_find_server_by_id(server->global_list,
2561 &sender_id, TRUE, NULL);
2563 router = silc_idlist_find_server_by_id(server->local_list,
2564 &sender_id, TRUE, NULL);
2566 id_list = server->global_list;
2573 case SILC_ID_CLIENT:
2575 SilcClientEntry entry;
2578 if (!silc_id_payload_get_id(idp, &id, sizeof(id)))
2581 /* Check that we do not have this client already */
2582 entry = silc_idlist_find_client_by_id(server->global_list,
2583 &id, server->server_type,
2586 entry = silc_idlist_find_client_by_id(server->local_list,
2587 &id, server->server_type,
2590 SILC_LOG_DEBUG(("Ignoring client that we already have"));
2594 SILC_LOG_DEBUG(("New client id(%s) from [%s] %s",
2595 silc_id_render(&id, SILC_ID_CLIENT),
2596 idata->conn_type == SILC_CONN_SERVER ?
2597 "Server" : "Router", hostname));
2599 /* As a router we keep information of all global information in our
2600 global list. Cell wide information however is kept in the local
2602 entry = silc_idlist_add_client(id_list, NULL, NULL, NULL,
2605 SILC_LOG_ERROR(("Could not add new client to the ID Cache"));
2607 /* Inform the sender that the ID is not usable */
2608 silc_server_send_notify_signoff(server, sock, FALSE, &id, NULL);
2611 entry->nickname = NULL;
2612 entry->data.status |= SILC_IDLIST_STATUS_REGISTERED;
2614 if (idata->conn_type == SILC_CONN_SERVER)
2615 server->stat.cell_clients++;
2616 server->stat.clients++;
2618 /* Check if anyone is watching this nickname */
2619 if (server->server_type == SILC_ROUTER && id_list == server->local_list)
2620 silc_server_check_watcher_list(server, entry, NULL, 0);
2622 if (server->server_type == SILC_ROUTER) {
2623 /* Add the client's public key to hash table or get the key with
2625 if (entry->data.public_key) {
2626 if (!silc_hash_table_find_by_context(server->pk_hash,
2627 entry->data.public_key,
2629 silc_hash_table_add(server->pk_hash, entry->data.public_key,
2632 silc_server_send_command(server, router_sock,
2633 SILC_COMMAND_GETKEY, ++server->cmd_ident,
2635 silc_buffer_len(buffer));
2640 case SILC_ID_SERVER:
2642 SilcServerEntry entry;
2645 if (!silc_id_payload_get_id(idp, &id, sizeof(id)))
2648 /* If the ID is mine, ignore it. */
2649 if (SILC_ID_SERVER_COMPARE(&id, server->id)) {
2650 SILC_LOG_DEBUG(("Ignoring my own ID as new ID"));
2654 /* If the ID is the sender's ID, ignore it (we have it already) */
2655 if (SILC_ID_SERVER_COMPARE(&id, router->id)) {
2656 SILC_LOG_DEBUG(("Ignoring sender's own ID"));
2660 /* Check that we do not have this server already */
2661 entry = silc_idlist_find_server_by_id(server->global_list,
2662 &id, server->server_type,
2665 entry = silc_idlist_find_server_by_id(server->local_list,
2666 &id, server->server_type,
2669 SILC_LOG_DEBUG(("Ignoring server that we already have"));
2673 SILC_LOG_DEBUG(("New server id(%s) from [%s] %s",
2674 silc_id_render(&id, SILC_ID_SERVER),
2675 idata->conn_type == SILC_CONN_SERVER ?
2676 "Server" : "Router", hostname));
2678 /* As a router we keep information of all global information in our
2679 global list. Cell wide information however is kept in the local
2681 entry = silc_idlist_add_server(id_list, NULL, 0, &id, router,
2684 SILC_LOG_ERROR(("Could not add new server to the ID Cache"));
2687 entry->data.status |= SILC_IDLIST_STATUS_REGISTERED;
2689 if (idata->conn_type == SILC_CONN_SERVER)
2690 server->stat.cell_servers++;
2691 server->stat.servers++;
2695 case SILC_ID_CHANNEL:
2696 SILC_LOG_ERROR(("Channel cannot be registered with NEW_ID packet"));
2705 /* If the sender of this packet is server and we are router we need to
2706 broadcast this packet to other routers in the network. */
2707 if (broadcast && server->server_type == SILC_ROUTER &&
2708 idata->conn_type == SILC_CONN_SERVER &&
2709 !(packet->flags & SILC_PACKET_FLAG_BROADCAST)) {
2710 SILC_LOG_DEBUG(("Broadcasting received New ID packet"));
2711 silc_server_packet_send(server, SILC_PRIMARY_ROUTE(server),
2713 packet->flags | SILC_PACKET_FLAG_BROADCAST,
2714 buffer->data, silc_buffer_len(buffer));
2715 silc_server_backup_send(server, (SilcServerEntry)idata,
2716 packet->type, packet->flags,
2717 packet->buffer.data,
2718 silc_buffer_len(&packet->buffer),
2723 silc_id_payload_free(idp);
2727 /* Processes incoming New ID packet. New ID Payload is used to distribute
2728 information about newly registered clients and servers. */
2730 void silc_server_new_id(SilcServer server, SilcPacketStream sock,
2733 silc_server_new_id_real(server, sock, packet, &packet->buffer, TRUE);
2736 /* Receoved New Id List packet, list of New ID payloads inside one
2737 packet. Process the New ID payloads one by one. */
2739 void silc_server_new_id_list(SilcServer server, SilcPacketStream sock,
2742 SilcIDListData idata = silc_packet_get_context(sock);
2746 SILC_LOG_DEBUG(("Processing New ID List"));
2748 if (idata->conn_type == SILC_CONN_CLIENT ||
2749 packet->src_id_type != SILC_ID_SERVER)
2752 /* If the sender of this packet is server and we are router we need to
2753 broadcast this packet to other routers in the network. Broadcast
2754 this list packet instead of multiple New ID packets. */
2755 if (server->server_type == SILC_ROUTER &&
2756 idata->conn_type == SILC_CONN_SERVER &&
2757 !(packet->flags & SILC_PACKET_FLAG_BROADCAST)) {
2758 SILC_LOG_DEBUG(("Broadcasting received New ID List packet"));
2759 silc_server_packet_send(server, SILC_PRIMARY_ROUTE(server),
2761 packet->flags | SILC_PACKET_FLAG_BROADCAST,
2762 packet->buffer.data,
2763 silc_buffer_len(&packet->buffer));
2764 silc_server_backup_send(server, (SilcServerEntry)idata,
2765 packet->type, packet->flags,
2766 packet->buffer.data,
2767 silc_buffer_len(&packet->buffer),
2771 idp = silc_buffer_alloc(256);
2775 while (silc_buffer_len(&packet->buffer)) {
2776 SILC_GET16_MSB(id_len, packet->buffer.data + 2);
2777 if ((id_len > silc_buffer_len(&packet->buffer)) ||
2778 (id_len > silc_buffer_truelen(idp)))
2781 silc_buffer_pull_tail(idp, 4 + id_len);
2782 silc_buffer_put(idp, packet->buffer.data, 4 + id_len);
2784 /* Process the New ID */
2785 silc_server_new_id_real(server, sock, packet, idp, FALSE);
2787 silc_buffer_push_tail(idp, 4 + id_len);
2788 silc_buffer_pull(&packet->buffer, 4 + id_len);
2791 silc_buffer_free(idp);
2794 /* Received New Channel packet. Information about new channels in the
2795 network are distributed using this packet. Save the information about
2796 the new channel. This usually comes from router but also normal server
2797 can send this to notify channels it has when it connects to us. */
2799 static void silc_server_new_channel_process(SilcServer server,
2800 SilcPacketStream sock,
2804 SilcIDListData idata = silc_packet_get_context(sock);
2805 SilcChannelPayload payload;
2806 SilcChannelID channel_id;
2807 char *channel_name, *channel_namec = NULL;
2808 SilcUInt32 name_len;
2809 unsigned char *id, cid[32];
2810 SilcUInt32 id_len, cipher_len;
2811 SilcServerEntry server_entry;
2812 SilcChannelEntry channel;
2815 if (idata->conn_type == SILC_CONN_CLIENT ||
2816 packet->src_id_type != SILC_ID_SERVER ||
2817 server->server_type == SILC_SERVER)
2820 /* Parse the channel payload */
2821 payload = silc_channel_payload_parse(buffer->data, silc_buffer_len(buffer));
2825 /* Get the channel ID */
2826 if (!silc_channel_get_id_parse(payload, &channel_id)) {
2827 silc_channel_payload_free(payload);
2831 channel_name = silc_channel_get_name(payload, &name_len);
2832 if (name_len > 256) {
2833 channel_name[256] = '\0';
2837 /* Check channel name */
2838 channel_namec = silc_channel_name_check(channel_name, strlen(channel_name),
2839 SILC_STRING_UTF8, 256, NULL);
2843 id = silc_channel_get_id(payload, &id_len);
2845 server_entry = (SilcServerEntry)idata;
2847 if (idata->conn_type == SILC_CONN_ROUTER) {
2848 /* Add the channel to global list as it is coming from router. It
2849 cannot be our own channel as it is coming from router. */
2851 /* Check that we don't already have this channel */
2852 channel = silc_idlist_find_channel_by_name(server->local_list,
2853 channel_namec, NULL);
2855 channel = silc_idlist_find_channel_by_name(server->global_list,
2856 channel_namec, NULL);
2858 SILC_LOG_DEBUG(("New channel id(%s) from [Router] %s",
2859 silc_id_render(&channel_id, SILC_ID_CHANNEL),
2860 idata->sconn->remote_host));
2863 silc_idlist_add_channel(server->global_list, strdup(channel_name),
2864 0, silc_id_dup(&channel_id, SILC_ID_CHANNEL),
2865 (SilcServerEntry)idata, NULL, NULL, NULL);
2867 silc_channel_payload_free(payload);
2870 channel->disabled = TRUE; /* Disabled until someone JOINs */
2872 server->stat.channels++;
2873 if (server->server_type == SILC_ROUTER)
2874 channel->users_resolved = TRUE;
2877 /* The channel is coming from our server, thus it is in our cell
2878 we will add it to our local list. */
2881 SILC_LOG_DEBUG(("Channel id(%s) from [Server] %s",
2882 silc_id_render(&channel_id, SILC_ID_CHANNEL),
2883 idata->sconn->remote_host));
2885 /* Check that we don't already have this channel */
2886 channel = silc_idlist_find_channel_by_name(server->local_list,
2887 channel_namec, NULL);
2889 channel = silc_idlist_find_channel_by_name(server->global_list,
2890 channel_namec, NULL);
2892 /* If the channel does not exist, then create it. This creates a new
2893 key to the channel as well that we will send to the server. */
2895 SILC_LOG_DEBUG(("Channel is new to us"));
2897 /* The protocol says that the Channel ID's IP address must be based
2898 on the router's IP address. Check whether the ID is based in our
2899 IP and if it is not then create a new ID and enforce the server
2900 to switch the ID. */
2901 if (server_entry->server_type != SILC_BACKUP_ROUTER &&
2902 !SILC_ID_COMPARE(&channel_id, server->id, server->id->ip.data_len)) {
2904 SILC_LOG_DEBUG(("Forcing the server to change Channel ID"));
2905 if (silc_id_create_channel_id(server, server->id, server->rng, &tmp)) {
2906 silc_server_send_notify_channel_change(server, sock, FALSE,
2908 silc_channel_payload_free(payload);
2912 /* Wait that server re-announces this channel */
2916 /* Create the channel with the provided Channel ID */
2917 channel = silc_server_create_new_channel_with_id(server, NULL, NULL,
2919 &channel_id, FALSE);
2921 silc_channel_payload_free(payload);
2924 channel->disabled = TRUE; /* Disabled until someone JOINs */
2926 #if 0 /* We assume that CMODE_CHANGE notify is sent to us after this. */
2928 /* XXX Dunno if this is supposed to be set in any server type. If set
2929 here the CMODE_CHANGE that may follow sets mode that we already
2930 have, and we may loose data from the CMODE_CHANGE notify. */
2931 if (server_entry->server_type != SILC_BACKUP_ROUTER)
2932 channel->mode = silc_channel_get_mode(payload);
2935 /* Send the new channel key to the server */
2936 silc_id_id2str(channel->id, SILC_ID_CHANNEL, cid, sizeof(cid),
2938 cipher = silc_cipher_get_name(channel->send_key);
2939 cipher_len = strlen(cipher);
2940 chk = silc_channel_key_payload_encode(id_len, cid,
2942 channel->key_len / 8,
2944 silc_server_packet_send(server, sock, SILC_PACKET_CHANNEL_KEY, 0,
2945 chk->data, silc_buffer_len(chk));
2946 silc_buffer_free(chk);
2948 /* The channel exist by that name, check whether the ID's match.
2949 If they don't then we'll force the server to use the ID we have.
2950 We also create a new key for the channel. */
2951 SilcBuffer modes = NULL, users = NULL, users_modes = NULL;
2953 SILC_LOG_DEBUG(("Channel already exists"));
2955 if (!SILC_ID_CHANNEL_COMPARE(&channel_id, channel->id)) {
2956 /* They don't match, send CHANNEL_CHANGE notify to the server to
2957 force the ID change. */
2958 SILC_LOG_DEBUG(("Forcing the server to change Channel ID"));
2959 silc_server_send_notify_channel_change(server, sock, FALSE,
2960 &channel_id, channel->id);
2961 silc_channel_payload_free(payload);
2963 /* Wait that server re-announces this channel */
2967 #if 0 /* We will announce our CMODE anyway for this channel, so no need
2968 to check it (implicit enforce). */
2970 /* If the mode is different from what we have then enforce the
2972 mode = silc_channel_get_mode(payload);
2973 if (channel->mode != mode) {
2974 SILC_LOG_DEBUG(("Forcing the server to change channel mode"));
2975 silc_server_send_notify_cmode(server, sock, FALSE, channel,
2976 channel->mode, server->id,
2977 SILC_ID_SERVER, channel->cipher,
2979 channel->passphrase,
2980 channel->founder_key);
2984 /* Create new key for the channel and send it to the server and
2985 everybody else possibly on the channel. */
2986 if (!(channel->mode & SILC_CHANNEL_MODE_PRIVKEY)) {
2988 if (silc_hash_table_count(channel->user_list)) {
2989 if (!silc_server_create_channel_key(server, channel, 0)) {
2990 silc_channel_payload_free(payload);
2994 /* Send to the channel */
2995 silc_server_send_channel_key(server, sock, channel, FALSE);
2998 /* Send to the server */
2999 silc_id_id2str(channel->id, SILC_ID_CHANNEL, cid, sizeof(cid),
3001 cipher = silc_cipher_get_name(channel->send_key);
3002 cipher_len = strlen(cipher);
3003 chk = silc_channel_key_payload_encode(id_len, cid,
3005 channel->key_len / 8,
3007 silc_server_packet_send(server, sock, SILC_PACKET_CHANNEL_KEY, 0,
3008 chk->data, silc_buffer_len(chk));
3009 silc_buffer_free(chk);
3012 /* Since the channel is coming from server and we also know about it
3013 then send the JOIN notify to the server so that it see's our
3014 users on the channel "joining" the channel. */
3015 silc_server_announce_get_channel_users(server, channel, &modes, &users,
3018 silc_buffer_push(users, users->data - users->head);
3019 silc_server_packet_send(server, sock,
3020 SILC_PACKET_NOTIFY, SILC_PACKET_FLAG_LIST,
3021 users->data, silc_buffer_len(users));
3022 silc_buffer_free(users);
3025 silc_buffer_push(modes, modes->data - modes->head);
3026 silc_server_packet_send_dest(server, sock,
3027 SILC_PACKET_NOTIFY, SILC_PACKET_FLAG_LIST,
3028 channel->id, SILC_ID_CHANNEL,
3029 modes->data, silc_buffer_len(modes));
3030 silc_buffer_free(modes);
3033 silc_buffer_push(users_modes, users_modes->data - users_modes->head);
3034 silc_server_packet_send_dest(server, sock,
3035 SILC_PACKET_NOTIFY, SILC_PACKET_FLAG_LIST,
3036 channel->id, SILC_ID_CHANNEL,
3038 silc_buffer_len(users_modes));
3039 silc_buffer_free(users_modes);
3041 if (channel->topic) {
3042 silc_server_send_notify_topic_set(server, sock,
3043 server->server_type == SILC_ROUTER ?
3044 TRUE : FALSE, channel,
3045 server->id, SILC_ID_SERVER,
3051 /* If the sender of this packet is server and we are router we need to
3052 broadcast this packet to other routers in the network. Broadcast
3053 this list packet instead of multiple New Channel packets. */
3054 if (server->server_type == SILC_ROUTER &&
3055 idata->conn_type == SILC_CONN_SERVER &&
3056 !(packet->flags & SILC_PACKET_FLAG_BROADCAST)) {
3057 SILC_LOG_DEBUG(("Broadcasting received New Channel packet"));
3058 silc_server_packet_send(server, SILC_PRIMARY_ROUTE(server),
3060 packet->flags | SILC_PACKET_FLAG_BROADCAST,
3061 buffer->data, silc_buffer_len(buffer));
3062 silc_server_backup_send(server, (SilcServerEntry)idata,
3063 packet->type, packet->flags,
3064 buffer->data, silc_buffer_len(buffer),
3068 silc_free(channel_namec);
3069 silc_channel_payload_free(payload);
3072 /* Received New Channel packet. Information about new channels in the
3073 network are distributed using this packet. Save the information about
3074 the new channel. This usually comes from router but also normal server
3075 can send this to notify channels it has when it connects to us. */
3077 void silc_server_new_channel(SilcServer server,
3078 SilcPacketStream sock,
3081 silc_server_new_channel_process(server, sock, packet, &packet->buffer);
3084 /* Received New Channel List packet, list of New Channel List payloads inside
3085 one packet. Process the New Channel payloads one by one. */
3087 void silc_server_new_channel_list(SilcServer server,
3088 SilcPacketStream sock,
3091 SilcIDListData idata = silc_packet_get_context(sock);
3093 SilcUInt16 len1, len2;
3095 SILC_LOG_DEBUG(("Processing New Channel List"));
3097 if (idata->conn_type == SILC_CONN_CLIENT ||
3098 packet->src_id_type != SILC_ID_SERVER ||
3099 server->server_type == SILC_SERVER)
3102 buffer = silc_buffer_alloc(512);
3106 while (silc_buffer_len(&packet->buffer)) {
3107 SILC_GET16_MSB(len1, packet->buffer.data);
3108 if ((len1 > silc_buffer_len(&packet->buffer)) ||
3109 (len1 > silc_buffer_truelen(buffer)))
3112 SILC_GET16_MSB(len2, packet->buffer.data + 2 + len1);
3113 if ((len2 > silc_buffer_len(&packet->buffer)) ||
3114 (len2 > silc_buffer_truelen(buffer)))
3117 silc_buffer_pull_tail(buffer, 8 + len1 + len2);
3118 silc_buffer_put(buffer, packet->buffer.data, 8 + len1 + len2);
3120 /* Process the New Channel */
3121 silc_server_new_channel_process(server, sock, packet, buffer);
3123 silc_buffer_push_tail(buffer, 8 + len1 + len2);
3124 silc_buffer_pull(&packet->buffer, 8 + len1 + len2);
3127 silc_buffer_free(buffer);
3130 /* Received key agreement packet. This packet is never for us. It is to
3131 the client in the packet's destination ID. Sending of this sort of packet
3132 equals sending private message, ie. it is sent point to point from
3133 one client to another. */
3135 void silc_server_key_agreement(SilcServer server,
3136 SilcPacketStream sock,
3139 SilcPacketStream dst_sock;
3140 SilcIDListData idata;
3142 SILC_LOG_DEBUG(("Start"));
3144 if (packet->src_id_type != SILC_ID_CLIENT ||
3145 packet->dst_id_type != SILC_ID_CLIENT)
3148 if (!packet->dst_id)
3151 /* Get the route to the client */
3152 dst_sock = silc_server_get_client_route(server, packet->dst_id,
3153 packet->dst_id_len, NULL,
3158 /* Relay the packet */
3159 silc_server_packet_route(server, dst_sock, packet);
3162 /* Received connection auth request packet that is used during connection
3163 phase to resolve the mandatory authentication method. This packet can
3164 actually be received at anytime but usually it is used only during
3165 the connection authentication phase. Now, protocol says that this packet
3166 can come from client or server, however, we support only this coming
3167 from client and expect that server always knows what authentication
3170 void silc_server_connection_auth_request(SilcServer server,
3171 SilcPacketStream sock,
3174 SilcServerConfigClient *client = NULL;
3175 SilcUInt16 conn_type;
3177 SilcAuthMethod auth_meth = SILC_AUTH_NONE;
3178 const char *hostname, *ip;
3180 if (packet->src_id_type && packet->src_id_type != SILC_ID_CLIENT) {
3181 SILC_LOG_DEBUG(("Request not from client"));
3185 silc_socket_stream_get_info(sock, NULL, &hostname, &ip, NULL);
3187 /* Parse the payload */
3188 ret = silc_buffer_unformat(&packet->buffer,
3189 SILC_STR_UI_SHORT(&conn_type),
3190 SILC_STR_UI_SHORT(NULL),
3195 if (conn_type != SILC_CONN_CLIENT)
3198 /* Get the authentication method for the client */
3199 auth_meth = SILC_AUTH_NONE;
3200 client = silc_server_config_find_client(server, (char *)ip);
3202 client = silc_server_config_find_client(server, (char *)hostname);
3204 if (client->passphrase) {
3205 if (client->publickeys && !server->config->prefer_passphrase_auth)
3206 auth_meth = SILC_AUTH_PUBLIC_KEY;
3208 auth_meth = SILC_AUTH_PASSWORD;
3209 } else if (client->publickeys)
3210 auth_meth = SILC_AUTH_PUBLIC_KEY;
3213 SILC_LOG_DEBUG(("Authentication method is [%s]",
3214 (auth_meth == SILC_AUTH_NONE ? "None" :
3215 auth_meth == SILC_AUTH_PASSWORD ? "Passphrase" :
3216 "Digital signatures")));
3218 /* Send it back to the client */
3219 silc_server_send_connection_auth_request(server, sock, conn_type, auth_meth);
3222 /* Received file transger packet. This packet is never for us. It is to
3223 the client in the packet's destination ID. Sending of this sort of packet
3224 equals sending private message, ie. it is sent point to point from
3225 one client to another. */
3227 void silc_server_ftp(SilcServer server,
3228 SilcPacketStream sock,
3231 SilcPacketStream dst_sock;
3232 SilcIDListData idata;
3234 SILC_LOG_DEBUG(("Start"));
3236 if (packet->src_id_type != SILC_ID_CLIENT ||
3237 packet->dst_id_type != SILC_ID_CLIENT)
3240 if (!packet->dst_id)
3243 /* Get the route to the client */
3244 dst_sock = silc_server_get_client_route(server, packet->dst_id,
3245 packet->dst_id_len, NULL,
3250 /* Relay the packet */
3251 silc_server_packet_route(server, dst_sock, packet);
3256 SilcPacketStream sock;
3258 SilcClientID client_id;
3259 } *SilcServerResumeResolve;
3261 SILC_SERVER_CMD_FUNC(resume_resolve)
3263 SilcServerResumeResolve r = (SilcServerResumeResolve)context;
3264 SilcServer server = r->server;
3265 SilcPacketStream sock = r->sock;
3266 SilcServerCommandReplyContext reply = context2;
3267 SilcClientEntry client;
3268 const char *hostname, *ip;
3270 SILC_LOG_DEBUG(("Start"));
3272 silc_socket_stream_get_info(sock, NULL, &hostname, &ip, NULL);
3274 if (!reply || !silc_command_get_status(reply->payload, NULL, NULL)) {
3275 SILC_LOG_ERROR(("Client %s (%s) tried to resume unknown client, "
3276 "closing connection", hostname, ip));
3277 silc_server_disconnect_remote(server, sock,
3278 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
3279 "Resuming not possible");
3280 silc_server_free_sock_user_data(server, sock, NULL);
3284 if (reply && silc_command_get(reply->payload) == SILC_COMMAND_WHOIS) {
3285 /* Get entry to the client, and resolve it if we don't have it. */
3286 client = silc_idlist_find_client_by_id(server->local_list,
3287 &r->client_id, TRUE, NULL);
3289 client = silc_idlist_find_client_by_id(server->global_list,
3290 &r->client_id, TRUE, NULL);
3292 SILC_LOG_ERROR(("Client %s (%s) tried to resume unknown client, "
3293 "closing connection", hostname, ip));
3294 silc_server_disconnect_remote(server, sock,
3295 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
3296 "Resuming not possible");
3297 silc_server_free_sock_user_data(server, sock, NULL);
3302 if (!(client->mode & SILC_UMODE_DETACHED)) {
3303 SILC_LOG_ERROR(("Client %s (%s) tried to resume un-detached client, "
3304 "closing connection", hostname, ip));
3305 silc_server_disconnect_remote(server, sock,
3306 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
3307 "Resuming not possible");
3308 silc_server_free_sock_user_data(server, sock, NULL);
3312 client->data.status |= SILC_IDLIST_STATUS_RESUME_RES;
3315 /* Reprocess the packet */
3316 silc_server_resume_client(server, sock, r->packet);
3319 silc_packet_stream_unref(r->sock);
3323 /* Received client resuming packet. This is used to resume detached
3324 client session. It can be sent by the client who wishes to resume
3325 but this is also sent by servers and routers to notify other routers
3326 that the client is not detached anymore. */
3328 void silc_server_resume_client(SilcServer server,
3329 SilcPacketStream sock,
3332 SilcBuffer buffer = &packet->buffer, buf;
3333 SilcIDListData idata = silc_packet_get_context(sock);
3334 SilcIDCacheEntry id_cache = NULL;
3335 SilcClientEntry detached_client;
3336 SilcClientID client_id;
3337 unsigned char *id_string, *auth = NULL, *nicknamec = NULL;
3338 unsigned char cid[32];
3340 SilcUInt16 id_len, auth_len = 0;
3341 SilcBool resolved, local, nick_change = FALSE, resolve = FALSE;
3342 SilcChannelEntry channel;
3343 SilcHashTableList htl;
3344 SilcChannelClientEntry chl;
3345 SilcServerResumeResolve r;
3346 const char *cipher, *hostname, *ip;
3348 silc_socket_stream_get_info(sock, NULL, &hostname, &ip, NULL);
3350 if (silc_buffer_unformat(buffer,
3351 SILC_STR_UI16_NSTRING(&id_string, &id_len),
3352 SILC_STR_END) < 0) {
3353 if (idata->conn_type == SILC_CONN_CLIENT) {
3354 SILC_LOG_ERROR(("Client %s (%s) sent incomplete resume information, "
3355 "closing connection", hostname, ip));
3356 silc_server_disconnect_remote(server, sock,
3357 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
3358 "Resuming not possible");
3359 silc_server_free_sock_user_data(server, sock, NULL);
3364 silc_id_str2id(id_string, id_len, SILC_ID_CLIENT, &client_id,
3367 if (idata->conn_type == SILC_CONN_CLIENT) {
3368 /* Client send this and is attempting to resume to old client session */
3369 SilcClientEntry client;
3372 silc_buffer_pull(buffer, 2 + id_len);
3373 auth = buffer->data;
3374 auth_len = silc_buffer_len(buffer);
3375 silc_buffer_push(buffer, 2 + id_len);
3377 if (auth_len < 128) {
3378 SILC_LOG_ERROR(("Client %s (%s) sent incomplete resume information, "
3379 "closing connection", hostname, ip));
3380 silc_server_disconnect_remote(server, sock,
3381 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
3382 "Resuming not possible");
3383 silc_server_free_sock_user_data(server, sock, NULL);
3387 /* Take client entry of this connection */
3388 client = (SilcClientEntry)idata;
3390 /* Get entry to the client, and resolve it if we don't have it. */
3391 detached_client = silc_server_query_client(server, &client_id, FALSE,
3393 if (!detached_client) {
3395 /* The client info is being resolved. Reprocess this packet after
3396 receiving the reply to the query. */
3397 SILC_LOG_DEBUG(("Resolving client"));
3398 r = silc_calloc(1, sizeof(*r));
3401 silc_packet_stream_ref(sock);
3405 r->client_id = client_id;
3406 silc_server_command_pending(server, SILC_COMMAND_WHOIS,
3408 silc_server_command_resume_resolve, r);
3411 SILC_LOG_ERROR(("Client %s (%s) tried to resume unknown client, "
3412 "closing connection", hostname, ip));
3413 silc_server_disconnect_remote(server, sock,
3414 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
3415 "Resuming not possible");
3416 silc_server_free_sock_user_data(server, sock, NULL);
3421 if (detached_client->data.status & SILC_IDLIST_STATUS_RESUMED) {
3422 SILC_LOG_ERROR(("Client %s (%s) tried to attach more than once, "
3423 "closing connection", hostname, ip));
3424 silc_server_disconnect_remote(server, sock,
3425 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
3426 "Resuming not possible");
3427 silc_server_free_sock_user_data(server, sock, NULL);
3431 if (detached_client->resuming_client &&
3432 detached_client->resuming_client != client) {
3433 SILC_LOG_ERROR(("Client %s (%s) tried to attach more than once, "
3434 "closing connection", hostname, ip));
3435 silc_server_disconnect_remote(server, sock,
3436 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
3437 "Resuming not possible");
3438 silc_server_free_sock_user_data(server, sock, NULL);
3442 if (!detached_client->resuming_client)
3443 detached_client->resuming_client = client;
3445 if (!(detached_client->mode & SILC_UMODE_DETACHED))
3447 if (!silc_hash_table_count(detached_client->channels) &&
3448 detached_client->router)
3450 if (!detached_client->nickname)
3452 if (detached_client->data.status & SILC_IDLIST_STATUS_RESUME_RES)
3456 if (server->server_type == SILC_SERVER && !server->standalone) {
3457 /* The client info is being resolved. Reprocess this packet after
3458 receiving the reply to the query. */
3459 SILC_LOG_DEBUG(("Resolving client info"));
3460 silc_server_query_client(server, &client_id, TRUE, NULL);
3461 r = silc_calloc(1, sizeof(*r));
3464 silc_packet_stream_ref(sock);
3468 r->client_id = client_id;
3469 silc_server_command_pending(server, SILC_COMMAND_WHOIS,
3471 silc_server_command_resume_resolve, r);
3474 if (server->server_type == SILC_SERVER) {
3475 SILC_LOG_ERROR(("Client %s (%s) tried to resume un-detached client, "
3476 "closing connection", hostname, ip));
3477 silc_server_disconnect_remote(server, sock,
3478 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
3479 "Resuming not possible");
3480 silc_server_free_sock_user_data(server, sock, NULL);
3485 /* Check that we have the public key of the client, if not then we must
3486 resolve it first. */
3487 if (!detached_client->data.public_key) {
3488 if (server->server_type == SILC_SERVER && server->standalone) {
3489 SILC_LOG_ERROR(("Detached client's public key not present, "
3490 "closing connection"));
3491 silc_server_disconnect_remote(server, sock,
3492 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
3493 "Resuming not possible");
3494 silc_server_free_sock_user_data(server, sock, NULL);
3497 /* We must retrieve the detached client's public key by sending
3498 GETKEY command. Reprocess this packet after receiving the key */
3499 SilcBuffer idp = silc_id_payload_encode(&client_id, SILC_ID_CLIENT);
3500 SilcPacketStream dest_sock =
3501 silc_server_get_client_route(server, NULL, 0, &client_id,
3504 SILC_LOG_DEBUG(("Resolving client public key"));
3506 silc_server_send_command(server, dest_sock ? dest_sock :
3507 SILC_PRIMARY_ROUTE(server),
3508 SILC_COMMAND_GETKEY, ++server->cmd_ident,
3509 1, 1, idp->data, silc_buffer_len(idp));
3511 r = silc_calloc(1, sizeof(*r));
3514 silc_packet_stream_ref(sock);
3518 r->client_id = client_id;
3519 silc_server_command_pending(server, SILC_COMMAND_GETKEY,
3521 silc_server_command_resume_resolve, r);
3523 silc_buffer_free(idp);
3526 } else if (!silc_pkcs_public_key_compare(detached_client->data.public_key,
3527 idata->public_key)) {
3528 /* We require that the connection and resuming authentication data
3529 must be using same key pair. */
3530 SILC_LOG_ERROR(("Resuming attempted with wrong public key, "
3531 "closing connection"));
3532 silc_server_disconnect_remote(server, sock,
3533 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
3534 "Resuming not possible");
3535 silc_server_free_sock_user_data(server, sock, NULL);
3539 /* Verify the authentication payload. This has to be successful in
3540 order to allow the resuming */
3542 !silc_auth_verify_data(auth, auth_len, SILC_AUTH_PUBLIC_KEY,
3543 detached_client->data.public_key, 0,
3544 idata->hash, detached_client->id,
3546 SILC_LOG_ERROR(("Client %s (%s) resume authentication failed, "
3547 "closing connection", hostname, ip));
3548 silc_server_disconnect_remote(server, sock,
3549 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
3550 "Resuming not possible");
3551 silc_server_free_sock_user_data(server, sock, NULL);
3555 /* Check nickname */
3556 nicknamec = silc_identifier_check(detached_client->nickname,
3557 strlen(detached_client->nickname),
3558 SILC_STRING_UTF8, 128, NULL);
3560 silc_server_disconnect_remote(server, sock,
3561 SILC_STATUS_ERR_BAD_NICKNAME,
3562 "Malformed nickname, cannot resume");
3563 silc_server_free_sock_user_data(server, sock, NULL);
3567 /* If the ID is not based in our ID then change it */
3568 if (!SILC_ID_COMPARE(detached_client->id, server->id,
3569 server->id->ip.data_len)) {
3570 SilcClientID *new_id;
3571 if (!silc_id_create_client_id(server, server->id, server->rng,
3572 server->md5hash, nicknamec,
3573 strlen(nicknamec), &new_id)) {
3574 silc_server_disconnect_remote(server, sock,
3575 SILC_STATUS_ERR_BAD_NICKNAME,
3576 "Resuming not possible");
3577 silc_server_free_sock_user_data(server, sock, NULL);
3581 client_id = *new_id;
3585 /* Now resume the client to the network */
3587 silc_schedule_task_del_by_context(server->schedule, detached_client);
3588 silc_packet_set_context(sock, detached_client);
3589 detached_client->connection = sock;
3591 if (detached_client->data.public_key)
3592 silc_hash_table_del_by_context(server->pk_hash,
3593 detached_client->data.public_key,
3595 if (idata->public_key)
3596 silc_hash_table_del_by_context(server->pk_hash,
3597 idata->public_key, idata);
3599 /* Take new keys and stuff into use in the old entry */
3600 silc_idlist_del_data(detached_client);
3601 silc_idlist_add_data(detached_client, idata);
3603 if (detached_client->data.public_key)
3604 silc_hash_table_add(server->pk_hash,
3605 detached_client->data.public_key, detached_client);
3607 detached_client->data.status |= SILC_IDLIST_STATUS_REGISTERED;
3608 detached_client->data.status |= SILC_IDLIST_STATUS_RESUMED;
3609 detached_client->data.status |= SILC_IDLIST_STATUS_LOCAL;
3610 detached_client->data.status &= ~SILC_IDLIST_STATUS_RESUME_RES;
3611 detached_client->mode &= ~SILC_UMODE_DETACHED;
3612 server->stat.my_detached--;
3614 /* We are finished - reset resuming client */
3615 detached_client->resuming_client = NULL;
3617 /* Check if anyone is watching this client */
3618 if (server->server_type == SILC_ROUTER)
3619 silc_server_check_watcher_list(server, detached_client, NULL,
3620 SILC_NOTIFY_TYPE_UMODE_CHANGE);
3622 /* Delete this current client entry since we're resuming to old one. */
3623 server->stat.my_clients--;
3624 server->stat.clients--;
3625 if (server->stat.cell_clients)
3626 server->stat.cell_clients--;
3627 silc_server_remove_from_channels(server, NULL, client, FALSE,
3628 NULL, FALSE, FALSE);
3629 silc_server_del_from_watcher_list(server, client);
3630 if (!silc_idlist_del_client(server->local_list, client))
3631 silc_idlist_del_client(server->global_list, client);
3632 client = detached_client;
3633 silc_free(client->servername);
3634 client->servername = strdup(server->server_name);
3636 /* Send the RESUME_CLIENT packet to our primary router so that others
3637 know this client isn't detached anymore. */
3638 buf = silc_buffer_alloc_size(2 + id_len);
3639 silc_buffer_format(buf,
3640 SILC_STR_UI_SHORT(id_len),
3641 SILC_STR_UI_XNSTRING(id_string, id_len),
3644 /* Send to primary router */
3645 silc_server_packet_send(server, SILC_PRIMARY_ROUTE(server),
3646 SILC_PACKET_RESUME_CLIENT, 0,
3647 buf->data, silc_buffer_len(buf));
3648 silc_server_backup_send(server, client->router,
3649 SILC_PACKET_RESUME_CLIENT, 0,
3650 buf->data, silc_buffer_len(buf), TRUE, TRUE);
3652 /* As router we must deliver this packet directly to the original
3653 server whom this client was earlier. */
3654 if (server->server_type == SILC_ROUTER && client->router &&
3655 client->router->server_type != SILC_ROUTER)
3656 silc_server_packet_send(server, client->router->connection,
3657 SILC_PACKET_RESUME_CLIENT, 0,
3658 buf->data, silc_buffer_len(buf));
3659 silc_buffer_free(buf);
3660 client->router = NULL;
3663 /* Notify about Client ID change, nickname doesn't actually change. */
3664 silc_server_send_notify_nick_change(server, SILC_PRIMARY_ROUTE(server),
3665 SILC_BROADCAST(server),
3666 client->id, &client_id,
3670 /* Resolve users on those channels that client has joined but we
3671 haven't resolved user list yet. */
3672 if (server->server_type == SILC_SERVER && !server->standalone) {
3673 silc_hash_table_list(client->channels, &htl);
3674 while (silc_hash_table_get(&htl, NULL, (void *)&chl)) {
3675 channel = chl->channel;
3676 SILC_LOG_DEBUG(("Resolving users for %s channel",
3677 channel->channel_name));
3678 if (channel->disabled || !channel->users_resolved) {
3679 silc_server_send_command(server, SILC_PRIMARY_ROUTE(server),
3680 SILC_COMMAND_USERS, ++server->cmd_ident,
3681 1, 2, channel->channel_name,
3682 strlen(channel->channel_name));
3685 silc_hash_table_list_reset(&htl);
3688 /* Send the new client ID to the client. After this client may start
3689 receiving other packets, and may start sending packets too. */
3690 silc_server_send_new_id(server, sock, FALSE, &client_id, SILC_ID_CLIENT,
3691 silc_id_get_len(&client_id, SILC_ID_CLIENT));
3694 /* Send NICK change notify to channels as well. */
3695 SilcBuffer oidp, nidp;
3696 oidp = silc_id_payload_encode(client->id, SILC_ID_CLIENT);
3697 nidp = silc_id_payload_encode(&client_id, SILC_ID_CLIENT);
3698 silc_server_send_notify_on_channels(server, NULL, client,
3699 SILC_NOTIFY_TYPE_NICK_CHANGE, 3,
3700 oidp->data, silc_buffer_len(oidp),
3701 nidp->data, silc_buffer_len(nidp),
3703 strlen(client->nickname));
3704 silc_buffer_free(oidp);
3705 silc_buffer_free(nidp);
3708 /* Add the client again to the ID cache to get it to correct list */
3709 if (!silc_idcache_del_by_context(server->local_list->clients, client,
3711 silc_idcache_del_by_context(server->global_list->clients, client, NULL);
3712 silc_free(client->id);
3713 *client->id = client_id;
3714 silc_idcache_add(server->local_list->clients, nicknamec,
3715 client->id, client);
3717 /* Send some nice info to the client */
3718 silc_server_send_connect_notifys(server, sock, client);
3720 /* Send all channel keys of channels the client has joined */
3721 silc_hash_table_list(client->channels, &htl);
3722 while (silc_hash_table_get(&htl, NULL, (void *)&chl)) {
3723 SilcBool created = FALSE;
3724 channel = chl->channel;
3726 if (channel->mode & SILC_CHANNEL_MODE_PRIVKEY)
3729 /* If we don't have channel key, then create one */
3730 if (!channel->send_key) {
3731 if (!silc_server_create_channel_key(server, channel, 0))
3736 silc_id_id2str(channel->id, SILC_ID_CHANNEL, cid, sizeof(cid),
3738 cipher = silc_cipher_get_name(channel->send_key);
3740 silc_channel_key_payload_encode(cid_len, cid,
3741 strlen(cipher), cipher,
3742 channel->key_len / 8, channel->key);
3744 /* Send the channel key to the client */
3745 silc_server_packet_send(server, sock, SILC_PACKET_CHANNEL_KEY, 0,
3746 keyp->data, silc_buffer_len(keyp));
3748 /* Distribute the channel key to channel */
3750 silc_server_send_channel_key(server, NULL, channel,
3751 server->server_type == SILC_ROUTER ?
3752 FALSE : !server->standalone);
3753 silc_server_backup_send(server, NULL, SILC_PACKET_CHANNEL_KEY, 0,
3754 keyp->data, silc_buffer_len(keyp),
3758 silc_buffer_free(keyp);
3760 silc_hash_table_list_reset(&htl);
3762 } else if (idata->conn_type != SILC_CONN_CLIENT) {
3763 /* Server or router sent this to us to notify that that a client has
3765 SilcServerEntry server_entry;
3766 SilcServerID server_id;
3768 /* Get entry to the client, and resolve it if we don't have it. */
3769 detached_client = silc_idlist_find_client_by_id(server->local_list,
3772 if (!detached_client) {
3773 detached_client = silc_idlist_find_client_by_id(server->global_list,
3776 if (!detached_client) {
3777 SILC_LOG_DEBUG(("Resuming client is unknown"));
3782 /* Check that the client has not been resumed already because it is
3783 protocol error to attempt to resume more than once. The client
3784 will be killed if this protocol error occurs. */
3785 if (detached_client->data.status & SILC_IDLIST_STATUS_RESUMED &&
3786 !(detached_client->mode & SILC_UMODE_DETACHED)) {
3787 /* The client is clearly attempting to resume more than once and
3788 perhaps playing around by resuming from several different places
3789 at the same time. */
3790 SILC_LOG_DEBUG(("Attempting to re-resume client, killing both"));
3791 silc_server_kill_client(server, detached_client, NULL,
3792 server->id, SILC_ID_SERVER);
3796 /* Check whether client is detached at all */
3797 if (!(detached_client->mode & SILC_UMODE_DETACHED)) {
3798 SILC_LOG_DEBUG(("Client is not detached"));
3802 /* Check nickname */
3803 if (detached_client->nickname) {
3804 nicknamec = silc_identifier_check(detached_client->nickname,
3805 strlen(detached_client->nickname),
3806 SILC_STRING_UTF8, 128, NULL);
3811 SILC_LOG_DEBUG(("Resuming detached client"));
3813 /* If the sender of this packet is server and we are router we need to
3814 broadcast this packet to other routers in the network. */
3815 if (server->server_type == SILC_ROUTER &&
3816 idata->conn_type == SILC_CONN_SERVER &&
3817 !(packet->flags & SILC_PACKET_FLAG_BROADCAST)) {
3818 SILC_LOG_DEBUG(("Broadcasting received Resume Client packet"));
3819 silc_server_packet_send(server, SILC_PRIMARY_ROUTE(server),
3821 packet->flags | SILC_PACKET_FLAG_BROADCAST,
3822 buffer->data, silc_buffer_len(buffer));
3823 silc_server_backup_send(server, (SilcServerEntry)idata,
3824 packet->type, packet->flags,
3825 packet->buffer.data,
3826 silc_buffer_len(&packet->buffer),
3830 /* Client is detached, and now it is resumed. Remove the detached
3831 mode and mark that it is resumed. */
3833 if (detached_client->data.public_key)
3834 silc_hash_table_del_by_context(server->pk_hash,
3835 detached_client->data.public_key,
3838 silc_idlist_del_data(detached_client);
3839 detached_client->mode &= ~SILC_UMODE_DETACHED;
3840 detached_client->data.status |= SILC_IDLIST_STATUS_RESUMED;
3841 detached_client->data.status &= ~SILC_IDLIST_STATUS_LOCAL;
3843 /* Check if anyone is watching this client */
3844 if (server->server_type == SILC_ROUTER)
3845 silc_server_check_watcher_list(server, detached_client, NULL,
3846 SILC_NOTIFY_TYPE_UMODE_CHANGE);
3848 silc_schedule_task_del_by_context(server->schedule, detached_client);
3850 /* Get the new owner of the resumed client */
3851 if (!silc_id_str2id(packet->src_id, packet->src_id_len,
3852 packet->src_id_type, &server_id, sizeof(server_id)))
3855 /* Get server entry */
3856 server_entry = silc_idlist_find_server_by_id(server->global_list,
3857 &server_id, TRUE, NULL);
3859 if (!server_entry) {
3860 server_entry = silc_idlist_find_server_by_id(server->local_list,
3861 &server_id, TRUE, NULL);
3867 if (server->server_type == SILC_ROUTER &&
3868 idata->conn_type == SILC_CONN_ROUTER &&
3869 server_entry->server_type == SILC_ROUTER)
3872 /* Change the client to correct list. */
3873 if (!silc_idcache_del_by_context(server->local_list->clients,
3874 detached_client, NULL))
3875 silc_idcache_del_by_context(server->global_list->clients,
3876 detached_client, NULL);
3877 silc_idcache_add(local && server->server_type == SILC_ROUTER ?
3878 server->local_list->clients :
3879 server->global_list->clients, nicknamec,
3880 detached_client->id, detached_client);
3882 /* Change the owner of the client */
3883 detached_client->router = server_entry;
3885 /* Update channel information regarding global clients on channel. */
3886 if (server->server_type != SILC_ROUTER) {
3887 silc_hash_table_list(detached_client->channels, &htl);
3888 while (silc_hash_table_get(&htl, NULL, (void *)&chl))
3889 chl->channel->global_users =
3890 silc_server_channel_has_global(chl->channel);
3891 silc_hash_table_list_reset(&htl);
3896 silc_packet_free(packet);