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);
1673 silc_packet_free(packet);
1676 void silc_server_notify_list(SilcServer server,
1677 SilcPacketStream sock,
1680 SilcIDListData idata = silc_packet_get_context(sock);
1684 SILC_LOG_DEBUG(("Processing Notify List"));
1686 if (idata->conn_type == SILC_CONN_CLIENT ||
1687 packet->src_id_type != SILC_ID_SERVER)
1690 buffer = silc_buffer_alloc(1024);
1694 while (silc_buffer_len(&packet->buffer)) {
1695 SILC_GET16_MSB(len, packet->buffer.data + 2);
1696 if (len > silc_buffer_len(&packet->buffer))
1699 if (len > silc_buffer_truelen(buffer)) {
1700 silc_buffer_free(buffer);
1701 buffer = silc_buffer_alloc(1024 + len);
1704 silc_buffer_pull_tail(buffer, len);
1705 silc_buffer_put(buffer, packet->buffer.data, len);
1707 /* Process the Notify */
1708 silc_server_notify_process(server, sock, packet, buffer);
1710 silc_buffer_push_tail(buffer, len);
1711 silc_buffer_pull(&packet->buffer, len);
1714 silc_packet_free(packet);
1715 silc_buffer_free(buffer);
1718 /* Received private message. This resolves the destination of the message
1719 and sends the packet. This is used by both server and router. If the
1720 destination is our locally connected client this sends the packet to
1721 the client. This may also send the message for further routing if
1722 the destination is not in our server (or router). */
1724 void silc_server_private_message(SilcServer server,
1725 SilcPacketStream sock,
1728 SilcPacketStream dst_sock;
1729 SilcIDListData idata;
1730 SilcClientEntry client;
1731 SilcClientID client_id;
1733 SILC_LOG_DEBUG(("Start"));
1735 if (packet->src_id_type != SILC_ID_CLIENT ||
1736 packet->dst_id_type != SILC_ID_CLIENT || !packet->dst_id)
1739 /* Get the route to the client */
1740 dst_sock = silc_server_get_client_route(server, packet->dst_id,
1741 packet->dst_id_len, NULL,
1745 unsigned char error;
1747 if (client && client->mode & SILC_UMODE_DETACHED) {
1748 SILC_LOG_DEBUG(("Client is detached, discarding packet"));
1752 /* Send SILC_NOTIFY_TYPE_ERROR to indicate that such destination ID
1753 does not exist or is invalid. */
1754 idp = silc_id_payload_encode_data(packet->dst_id,
1756 packet->dst_id_type);
1760 error = SILC_STATUS_ERR_NO_SUCH_CLIENT_ID;
1761 if (packet->src_id_type == SILC_ID_CLIENT) {
1762 silc_id_str2id(packet->src_id, packet->src_id_len,
1763 packet->src_id_type, &client_id, sizeof(client_id));
1764 silc_server_send_notify_dest(server, sock, FALSE,
1765 &client_id, SILC_ID_CLIENT,
1766 SILC_NOTIFY_TYPE_ERROR, 2,
1768 idp->data, silc_buffer_len(idp));
1770 silc_server_send_notify(server, sock, FALSE,
1771 SILC_NOTIFY_TYPE_ERROR, 2,
1773 idp->data, silc_buffer_len(idp));
1776 silc_buffer_free(idp);
1780 /* Check whether destination client wishes to receive private messages */
1781 if (client && !(packet->flags & SILC_PACKET_FLAG_PRIVMSG_KEY) &&
1782 client->mode & SILC_UMODE_BLOCK_PRIVMSG) {
1783 SILC_LOG_DEBUG(("Client blocks private messages, discarding packet"));
1787 /* Send the private message */
1788 silc_server_packet_route(server, dst_sock, packet);
1791 silc_packet_free(packet);
1794 /* Received private message key packet.. This packet is never for us. It is to
1795 the client in the packet's destination ID. Sending of this sort of packet
1796 equals sending private message, ie. it is sent point to point from
1797 one client to another. */
1799 void silc_server_private_message_key(SilcServer server,
1800 SilcPacketStream sock,
1803 SilcPacketStream dst_sock;
1804 SilcIDListData idata;
1806 SILC_LOG_DEBUG(("Start"));
1808 if (packet->src_id_type != SILC_ID_CLIENT ||
1809 packet->dst_id_type != SILC_ID_CLIENT || !packet->dst_id) {
1810 silc_packet_free(packet);
1814 /* Get the route to the client */
1815 dst_sock = silc_server_get_client_route(server, packet->dst_id,
1816 packet->dst_id_len, NULL,
1819 silc_packet_free(packet);
1823 /* Relay the packet */
1824 silc_server_packet_route(server, dst_sock, packet);
1826 silc_packet_free(packet);
1829 /* Processes incoming command reply packet. The command reply packet may
1830 be destined to one of our clients or it may directly for us. We will
1831 call the command reply routine after processing the packet. */
1833 void silc_server_command_reply(SilcServer server,
1834 SilcPacketStream sock,
1837 SilcBuffer buffer = &packet->buffer;
1838 SilcClientEntry client = NULL;
1841 SILC_LOG_DEBUG(("Start"));
1843 if (packet->dst_id_type == SILC_ID_CHANNEL) {
1844 silc_packet_free(packet);
1848 if (packet->dst_id_type == SILC_ID_CLIENT) {
1849 /* Destination must be one of ours */
1850 if (!silc_id_str2id(packet->dst_id, packet->dst_id_len, SILC_ID_CLIENT,
1852 silc_packet_free(packet);
1855 client = silc_idlist_find_client_by_id(server->local_list, &id,
1858 SILC_LOG_ERROR(("Cannot process command reply to unknown client"));
1859 silc_packet_free(packet);
1864 if (packet->dst_id_type == SILC_ID_SERVER) {
1865 /* For now this must be for us */
1866 if (memcmp(packet->dst_id, server->id_string, server->id_string_len)) {
1867 SILC_LOG_ERROR(("Cannot process command reply to unknown server"));
1868 silc_packet_free(packet);
1873 /* Execute command reply locally for the command */
1874 silc_server_command_reply_process(server, sock, buffer);
1876 /* Relay the packet to the client */
1877 if (packet->dst_id_type == SILC_ID_CLIENT && client)
1878 silc_server_packet_route(server, client->connection, packet);
1880 silc_packet_free(packet);
1883 /* Process received channel message. The message can be originated from
1884 client or server. */
1886 void silc_server_channel_message(SilcServer server,
1887 SilcPacketStream sock,
1890 SilcChannelEntry channel = NULL;
1894 void *sender_id = NULL;
1895 SilcClientEntry sender_entry = NULL;
1896 SilcIDListData idata;
1897 SilcChannelClientEntry chl;
1898 SilcBool local = TRUE;
1900 SILC_LOG_DEBUG(("Processing channel message"));
1903 if (packet->dst_id_type != SILC_ID_CHANNEL) {
1904 SILC_LOG_DEBUG(("Received bad message for channel, dropped"));
1908 /* Find channel entry */
1909 if (!silc_id_str2id(packet->dst_id, packet->dst_id_len, SILC_ID_CHANNEL,
1912 channel = silc_idlist_find_channel_by_id(server->local_list, &id, NULL);
1914 channel = silc_idlist_find_channel_by_id(server->global_list, &id, NULL);
1917 unsigned char error;
1919 /* Send SILC_NOTIFY_TYPE_ERROR to indicate that such destination ID
1920 does not exist or is invalid. */
1921 idp = silc_id_payload_encode_data(packet->dst_id,
1923 packet->dst_id_type);
1927 error = SILC_STATUS_ERR_NO_SUCH_CHANNEL_ID;
1928 if (packet->src_id_type == SILC_ID_CLIENT) {
1929 silc_id_str2id(packet->src_id, packet->src_id_len,
1930 packet->src_id_type, &cid, sizeof(cid));
1931 silc_server_send_notify_dest(server, sock, FALSE,
1932 &cid, SILC_ID_CLIENT,
1933 SILC_NOTIFY_TYPE_ERROR, 2,
1934 &error, 1, idp->data,
1935 silc_buffer_len(idp));
1937 silc_server_send_notify(server, sock, FALSE,
1938 SILC_NOTIFY_TYPE_ERROR, 2,
1939 &error, 1, idp->data, silc_buffer_len(idp));
1942 silc_buffer_free(idp);
1947 /* See that this client is on the channel. If the original sender is
1948 not client (as it can be server as well) we don't do the check. */
1949 if (!silc_id_str2id2(packet->src_id, packet->src_id_len,
1950 packet->src_id_type, &sid))
1952 if (sid.type == SILC_ID_CLIENT) {
1953 sender_entry = silc_idlist_find_client_by_id(server->local_list,
1954 SILC_ID_GET_ID(sid),
1956 if (!sender_entry) {
1958 sender_entry = silc_idlist_find_client_by_id(server->global_list,
1959 SILC_ID_GET_ID(sid),
1962 if (!sender_entry || !silc_server_client_on_channel(sender_entry,
1964 SILC_LOG_DEBUG(("Client not on channel"));
1968 /* If channel is moderated check that client is allowed to send
1970 if (channel->mode & SILC_CHANNEL_MODE_SILENCE_USERS &&
1971 !(chl->mode & SILC_CHANNEL_UMODE_CHANOP) &&
1972 !(chl->mode & SILC_CHANNEL_UMODE_CHANFO)) {
1973 SILC_LOG_DEBUG(("Channel is silenced from normal users"));
1976 if (channel->mode & SILC_CHANNEL_MODE_SILENCE_OPERS &&
1977 chl->mode & SILC_CHANNEL_UMODE_CHANOP &&
1978 !(chl->mode & SILC_CHANNEL_UMODE_CHANFO)) {
1979 SILC_LOG_DEBUG(("Channel is silenced from operators"));
1982 if (chl->mode & SILC_CHANNEL_UMODE_QUIET) {
1983 SILC_LOG_DEBUG(("Sender is quieted on the channel"));
1987 /* If the packet is coming from router, but the client entry is local
1988 entry to us then some router is rerouting this to us and it is not
1989 allowed. When the client is local to us it means that we've routed
1990 this packet to network, and now someone is routing it back to us. */
1991 idata = silc_packet_get_context(sock);
1992 if (server->server_type == SILC_ROUTER &&
1993 idata->conn_type == SILC_CONN_ROUTER && local) {
1994 SILC_LOG_DEBUG(("Channel message rerouted to the sender, drop it"));
1999 /* Distribute the packet to our local clients. This will send the
2000 packet for further routing as well, if needed. */
2001 silc_server_packet_relay_to_channel(server, sock, channel, sender_id,
2002 packet->src_id_type, sender_entry,
2003 packet->buffer.data,
2004 silc_buffer_len(&packet->buffer));
2007 silc_packet_free(packet);
2010 /* Received channel key packet. We distribute the key to all of our locally
2011 connected clients on the channel. */
2013 void silc_server_channel_key(SilcServer server,
2014 SilcPacketStream sock,
2017 SilcBuffer buffer = &packet->buffer;
2018 SilcIDListData idata = silc_packet_get_context(sock);
2019 SilcChannelEntry channel;
2021 if (packet->src_id_type != SILC_ID_SERVER ||
2022 (server->server_type == SILC_ROUTER && !server->backup_router &&
2023 idata->conn_type == SILC_CONN_ROUTER)) {
2024 silc_packet_free(packet);
2028 /* Save the channel key */
2029 channel = silc_server_save_channel_key(server, buffer, NULL);
2031 SILC_LOG_ERROR(("Bad channel key from %s", idata->sconn->remote_host));
2032 silc_packet_free(packet);
2036 /* Distribute the key to everybody who is on the channel. If we are router
2037 we will also send it to locally connected servers. */
2038 silc_server_send_channel_key(server, sock, channel, FALSE);
2040 if (server->server_type != SILC_BACKUP_ROUTER)
2041 /* Distribute to local cell backup routers. */
2042 silc_server_backup_send(server, (SilcServerEntry)idata,
2043 SILC_PACKET_CHANNEL_KEY, 0,
2044 buffer->data, silc_buffer_len(buffer),
2047 silc_packet_free(packet);
2050 /* Received New Client packet and processes it. Creates Client ID for the
2051 client. Client becomes registered after calling this functions. */
2053 SilcClientEntry silc_server_new_client(SilcServer server,
2054 SilcPacketStream sock,
2057 SilcBuffer buffer = &packet->buffer;
2058 SilcIDListData idata = silc_packet_get_context(sock);
2059 SilcClientEntry client;
2060 SilcClientID *client_id;
2061 char *username = NULL, *realname = NULL;
2062 SilcUInt16 username_len, nickname_len;
2063 SilcUInt32 id_len, tmp_len;
2065 char *host, *nickname = NULL, *nicknamec;
2066 const char *hostname, *ip;
2068 SILC_LOG_DEBUG(("Creating new client"));
2070 if (idata->conn_type != SILC_CONN_CLIENT) {
2071 silc_packet_free(packet);
2075 /* Take client entry */
2076 client = (SilcClientEntry)idata;
2077 silc_socket_stream_get_info(silc_packet_stream_get_stream(sock),
2078 NULL, &hostname, &ip, NULL);
2080 SILC_LOG_DEBUG(("%s %s", ip, hostname));
2082 /* Make sure this client hasn't registered already */
2083 if (idata->status & SILC_IDLIST_STATUS_REGISTERED) {
2084 silc_packet_free(packet);
2088 /* Parse incoming packet */
2089 ret = silc_buffer_unformat(buffer,
2091 SILC_STR_UI16_NSTRING_ALLOC(&username,
2093 SILC_STR_UI16_STRING_ALLOC(&realname),
2096 silc_free(username);
2097 silc_free(realname);
2098 SILC_LOG_ERROR(("Client %s (%s) sent incomplete information, closing "
2099 "connection", hostname, ip));
2100 silc_server_disconnect_remote(server, sock,
2101 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
2103 silc_server_free_sock_user_data(server, sock, NULL);
2104 silc_packet_free(packet);
2109 silc_free(username);
2110 silc_free(realname);
2111 SILC_LOG_ERROR(("Client %s (%s) did not send its username, closing "
2112 "connection", hostname, ip));
2113 silc_server_disconnect_remote(server, sock,
2114 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
2116 silc_server_free_sock_user_data(server, sock, NULL);
2117 silc_packet_free(packet);
2121 if (username_len > 128) {
2123 username[username_len - 1] = '\0';
2126 /* Check for valid username string */
2127 nicknamec = silc_identifier_check(username, username_len,
2128 SILC_STRING_UTF8, 128, &tmp_len);
2130 silc_free(username);
2131 silc_free(realname);
2132 SILC_LOG_ERROR(("Client %s (%s) sent bad username string '%s', closing "
2133 "connection", hostname, ip, username));
2134 silc_server_disconnect_remote(server, sock,
2135 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
2137 silc_server_free_sock_user_data(server, sock, NULL);
2138 silc_packet_free(packet);
2142 /* Take nickname from NEW_CLIENT packet, if present */
2143 if (silc_buffer_unformat(buffer,
2144 SILC_STR_UI16_NSTRING_ALLOC(&nickname,
2147 if (nickname_len > 128) {
2149 nickname[nickname_len - 1] = '\0';
2153 /* Nickname is initially same as username, if not present in NEW_CLIENT */
2155 nickname = strdup(username);
2157 /* Make sanity checks for the hostname of the client. If the hostname
2158 is provided in the `username' check that it is the same than the
2159 resolved hostname, or if not resolved the hostname that appears in
2160 the client's public key. If the hostname is not present then put
2161 it from the resolved name or from the public key. */
2162 if (strchr(username, '@')) {
2163 SilcSILCPublicKey silc_pubkey;
2164 int tlen = strcspn(username, "@");
2165 char *phostname = NULL;
2167 host = silc_memdup(username + tlen + 1, strlen(username) - tlen - 1);
2169 if (strcmp(hostname, ip) && strcmp(hostname, host)) {
2170 silc_free(username);
2172 silc_free(realname);
2173 SILC_LOG_ERROR(("Client %s (%s) sent incomplete information, closing "
2174 "connection", hostname, ip));
2175 silc_server_disconnect_remote(server, sock,
2176 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
2178 silc_server_free_sock_user_data(server, sock, NULL);
2179 silc_packet_free(packet);
2183 silc_pubkey = silc_pkcs_get_context(SILC_PKCS_SILC,
2184 client->data.public_key);
2185 phostname = strdup(silc_pubkey->identifier.host);
2186 if (!strcmp(hostname, ip) && phostname && strcmp(phostname, host)) {
2187 silc_free(username);
2189 silc_free(phostname);
2190 silc_free(realname);
2191 SILC_LOG_ERROR(("Client %s (%s) sent incomplete information, closing "
2192 "connection", hostname, ip));
2193 silc_server_disconnect_remote(server, sock,
2194 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
2196 silc_server_free_sock_user_data(server, sock, NULL);
2197 silc_packet_free(packet);
2201 silc_free(phostname);
2203 /* The hostname is not present, add it. */
2205 newusername = silc_calloc(strlen(username) + strlen(hostname) + 2,
2206 sizeof(*newusername));
2207 strncat(newusername, username, strlen(username));
2208 strncat(newusername, "@", 1);
2209 strncat(newusername, hostname, strlen(hostname));
2210 silc_free(username);
2211 username = newusername;
2214 SILC_LOG_DEBUG(("%s %s", ip, hostname));
2216 /* Create Client ID */
2217 if (!silc_id_create_client_id(server, server->id, server->rng,
2218 server->md5hash, nicknamec,
2219 strlen(nicknamec), &client_id)) {
2220 silc_server_disconnect_remote(server, sock,
2221 SILC_STATUS_ERR_BAD_NICKNAME, NULL);
2222 silc_server_free_sock_user_data(server, sock, NULL);
2223 silc_packet_free(packet);
2227 /* If client marked as anonymous, scramble the username and hostname */
2228 if (client->mode & SILC_UMODE_ANONYMOUS) {
2231 if (strlen(username) >= 2) {
2232 username[0] = silc_rng_get_byte_fast(server->rng);
2233 username[1] = silc_rng_get_byte_fast(server->rng);
2236 scramble = silc_hash_babbleprint(server->sha1hash, username,
2240 memcpy(&scramble[16], ".silc", 5);
2241 scramble[21] = '\0';
2242 silc_free(username);
2243 username = scramble;
2246 /* Update client entry */
2247 idata->status |= SILC_IDLIST_STATUS_REGISTERED;
2248 client->nickname = nickname;
2249 client->username = username;
2250 client->userinfo = realname ? realname : strdup(username);
2251 client->id = client_id;
2252 id_len = silc_id_get_len(client_id, SILC_ID_CLIENT);
2253 silc_idcache_update_by_context(server->local_list->clients, client,
2254 client_id, nicknamec, TRUE);
2256 /* Notify our router about new client on the SILC network */
2257 silc_server_send_new_id(server, SILC_PRIMARY_ROUTE(server),
2258 SILC_BROADCAST(server), client->id,
2259 SILC_ID_CLIENT, id_len);
2261 /* Distribute to backup routers */
2262 if (server->server_type == SILC_ROUTER) {
2263 SilcBuffer idp = silc_id_payload_encode(client->id, SILC_ID_CLIENT);
2264 silc_server_backup_send(server, NULL, SILC_PACKET_NEW_ID, 0,
2265 idp->data, silc_buffer_len(idp), FALSE, TRUE);
2266 silc_buffer_free(idp);
2269 /* Send the new client ID to the client. */
2270 silc_server_send_new_id(server, sock, FALSE, client->id, SILC_ID_CLIENT,
2271 silc_id_get_len(client->id, SILC_ID_CLIENT));
2273 /* Send some nice info to the client */
2274 silc_server_send_connect_notifys(server, sock, client);
2276 /* Check if anyone is watching this nickname */
2277 if (server->server_type == SILC_ROUTER)
2278 silc_server_check_watcher_list(server, client, NULL, 0);
2280 silc_packet_free(packet);
2284 /* Create new server. This processes received New Server packet and
2285 saves the received Server ID. The server is our locally connected
2286 server thus we save all the information and save it to local list.
2287 This funtion can be used by both normal server and router server.
2288 If normal server uses this it means that its router has connected
2289 to the server. If router uses this it means that one of the cell's
2290 servers is connected to the router. */
2292 SilcServerEntry silc_server_new_server(SilcServer server,
2293 SilcPacketStream sock,
2296 SilcBuffer buffer = &packet->buffer;
2297 SilcIDListData idata = silc_packet_get_context(sock);
2298 SilcServerEntry new_server, server_entry;
2299 SilcServerID server_id;
2300 unsigned char *server_name, *server_namec, *id_string;
2301 SilcUInt16 id_len, name_len;
2303 SilcBool local = TRUE;
2304 const char *hostname, *ip;
2306 SILC_LOG_DEBUG(("Creating new server"));
2308 if (idata->conn_type != SILC_CONN_SERVER &&
2309 idata->conn_type != SILC_CONN_ROUTER) {
2310 silc_packet_free(packet);
2314 /* Take server entry */
2315 new_server = (SilcServerEntry)idata;
2316 silc_socket_stream_get_info(silc_packet_stream_get_stream(sock),
2317 NULL, &hostname, &ip, NULL);
2320 if (server->server_type == SILC_ROUTER)
2321 server->stat.cell_servers++;
2323 /* Remove the old cache entry */
2324 if (!silc_idcache_del_by_context(server->local_list->servers, new_server,
2326 if (!silc_idcache_del_by_context(server->global_list->servers,
2327 new_server, NULL)) {
2328 SILC_LOG_INFO(("Unauthenticated %s attempted to register to "
2329 "network", (idata->conn_type == SILC_CONN_SERVER ?
2330 "server" : "router")));
2331 silc_server_disconnect_remote(server, sock,
2332 SILC_STATUS_ERR_NOT_AUTHENTICATED, NULL);
2333 silc_server_free_sock_user_data(server, sock, NULL);
2339 /* Make sure this server hasn't registered already */
2340 if (idata->status & SILC_IDLIST_STATUS_REGISTERED) {
2341 silc_server_disconnect_remote(server, sock,
2342 SILC_STATUS_ERR_OPERATION_ALLOWED,
2343 "Too many registrations");
2344 silc_server_free_sock_user_data(server, sock, NULL);
2348 /* Parse the incoming packet */
2349 ret = silc_buffer_unformat(buffer,
2350 SILC_STR_UI16_NSTRING_ALLOC(&id_string, &id_len),
2351 SILC_STR_UI16_NSTRING_ALLOC(&server_name,
2355 silc_free(id_string);
2356 silc_free(server_name);
2357 silc_server_disconnect_remote(server, sock,
2358 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
2360 silc_server_free_sock_user_data(server, sock, NULL);
2364 if (id_len > silc_buffer_len(buffer)) {
2365 silc_free(id_string);
2366 silc_free(server_name);
2367 silc_server_disconnect_remote(server, sock,
2368 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
2370 silc_server_free_sock_user_data(server, sock, NULL);
2374 if (name_len > 256) {
2375 server_name[256] = '\0';
2380 if (!silc_id_str2id(id_string, id_len, SILC_ID_SERVER, &server_id,
2381 sizeof(server_id))) {
2382 silc_free(id_string);
2383 silc_free(server_name);
2384 silc_server_disconnect_remote(server, sock,
2385 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
2387 silc_server_free_sock_user_data(server, sock, NULL);
2390 silc_free(id_string);
2392 /* Check for valid server ID */
2393 if (!silc_id_is_valid_server_id(server, &server_id, sock)) {
2394 SILC_LOG_INFO(("Invalid server ID sent by %s (%s)",
2396 silc_server_disconnect_remote(server, sock,
2397 SILC_STATUS_ERR_BAD_SERVER_ID, NULL);
2398 silc_server_free_sock_user_data(server, sock, NULL);
2399 silc_free(server_name);
2403 /* Check that we do not have this ID already */
2404 server_entry = silc_idlist_find_server_by_id(server->local_list,
2405 &server_id, TRUE, NULL);
2407 if (SILC_IS_LOCAL(server_entry)) {
2408 SILC_LOG_ERROR(("Too many registrations from %s (%s)",
2410 silc_server_disconnect_remote(server, sock,
2411 SILC_STATUS_ERR_OPERATION_ALLOWED,
2412 "Too many registrations");
2413 silc_server_free_sock_user_data(server, sock, NULL);
2416 silc_idcache_del_by_context(server->local_list->servers, server_entry,
2420 server_entry = silc_idlist_find_server_by_id(server->global_list,
2421 &server_id, TRUE, NULL);
2423 if (SILC_IS_LOCAL(server_entry)) {
2424 SILC_LOG_ERROR(("Too many registrations from %s (%s)",
2426 silc_server_disconnect_remote(server, sock,
2427 SILC_STATUS_ERR_OPERATION_ALLOWED,
2428 "Too many registrations");
2429 silc_server_free_sock_user_data(server, sock, NULL);
2432 silc_idcache_del_by_context(server->global_list->servers,
2433 server_entry, NULL);
2438 /* Check server name */
2439 server_namec = silc_identifier_check(server_name, strlen(server_name),
2440 SILC_STRING_UTF8, 256, NULL);
2441 if (!server_namec) {
2442 SILC_LOG_ERROR(("Malformed server name from %s (%s)",
2444 silc_server_disconnect_remote(server, sock,
2445 SILC_STATUS_ERR_OPERATION_ALLOWED,
2446 "Malfromed server name");
2447 silc_server_free_sock_user_data(server, sock, NULL);
2451 /* Update server entry */
2452 idata->status |= SILC_IDLIST_STATUS_REGISTERED;
2453 new_server->server_name = server_name;
2454 new_server->id = silc_id_dup(&server_id, SILC_ID_SERVER);
2456 SILC_LOG_DEBUG(("New server id(%s)",
2457 silc_id_render(&server_id, SILC_ID_SERVER)));
2459 /* Add again the entry to the ID cache. */
2460 silc_idcache_add(local ? server->local_list->servers :
2461 server->global_list->servers, server_namec,
2462 new_server->id, new_server);
2464 /* Distribute the information about new server in the SILC network
2465 to our router. If we are normal server we won't send anything
2466 since this connection must be our router connection. */
2467 if (server->server_type == SILC_ROUTER && !server->standalone &&
2468 SILC_PRIMARY_ROUTE(server) != sock)
2469 silc_server_send_new_id(server, SILC_PRIMARY_ROUTE(server),
2470 TRUE, new_server->id, SILC_ID_SERVER,
2471 silc_id_get_len(&server_id, SILC_ID_SERVER));
2473 if (server->server_type == SILC_ROUTER) {
2474 /* Distribute to backup routers */
2475 SilcBuffer idp = silc_id_payload_encode(new_server->id, SILC_ID_SERVER);
2476 silc_server_backup_send(server, NULL, SILC_PACKET_NEW_ID, 0, idp->data,
2477 silc_buffer_len(idp), FALSE, TRUE);
2478 silc_buffer_free(idp);
2481 /* Check whether this router connection has been replaced by an
2482 backup router. If it has been then we'll disable the server and will
2483 ignore everything it will send until the backup router resuming
2484 protocol has been completed. */
2485 if (idata->conn_type == SILC_CONN_ROUTER &&
2486 silc_server_backup_replaced_get(server, &server_id, NULL)) {
2487 /* Send packet to the router indicating that it cannot use this
2488 connection as it has been replaced by backup router. */
2489 SILC_LOG_DEBUG(("Remote router has been replaced by backup router, "
2490 "disabling its connection"));
2492 silc_server_backup_send_replaced(server, sock);
2494 /* Mark the router disabled. The data sent earlier will go but nothing
2495 after this goes to this connection. */
2496 idata->status |= SILC_IDLIST_STATUS_DISABLED;
2498 /* If it is router announce our stuff to it. */
2499 if (idata->conn_type == SILC_CONN_ROUTER &&
2500 server->server_type == SILC_ROUTER) {
2501 silc_server_announce_servers(server, FALSE, 0, sock);
2502 silc_server_announce_clients(server, 0, sock);
2503 silc_server_announce_channels(server, 0, sock);
2506 /* Announce our information to backup router */
2507 if (new_server->server_type == SILC_BACKUP_ROUTER &&
2508 idata->conn_type == SILC_CONN_SERVER &&
2509 server->server_type == SILC_ROUTER) {
2510 silc_server_announce_servers(server, TRUE, 0, sock);
2511 silc_server_announce_clients(server, 0, sock);
2512 silc_server_announce_channels(server, 0, sock);
2515 /* If backup router, mark it as one of ours. This server is considered
2516 to be backup router after this setting. */
2517 if (new_server->server_type == SILC_BACKUP_ROUTER) {
2518 SilcServerConfigRouter *backup;
2519 backup = silc_server_config_find_backup_conn(server, (char *)ip);
2521 backup = silc_server_config_find_backup_conn(server, (char *)hostname);
2523 /* Add as our backup router */
2524 silc_server_backup_add(server, new_server, backup->backup_replace_ip,
2525 backup->backup_replace_port,
2526 backup->backup_local);
2530 /* By default the servers connected to backup router are disabled
2531 until backup router has become the primary */
2532 if (server->server_type == SILC_BACKUP_ROUTER &&
2533 idata->conn_type == SILC_CONN_SERVER)
2534 idata->status |= SILC_IDLIST_STATUS_DISABLED;
2540 /* Processes incoming New ID packet. New ID Payload is used to distribute
2541 information about newly registered clients and servers. */
2543 static void silc_server_new_id_real(SilcServer server,
2544 SilcPacketStream sock,
2549 SilcIDListData idata = silc_packet_get_context(sock);
2551 SilcServerEntry router, server_entry;
2552 SilcPacketStream router_sock;
2555 SilcServerID sender_id;
2556 const char *hostname, *ip;
2558 SILC_LOG_DEBUG(("Processing new ID"));
2560 if (idata->conn_type == SILC_CONN_CLIENT ||
2561 server->server_type == SILC_SERVER ||
2562 packet->src_id_type != SILC_ID_SERVER)
2565 idp = silc_id_payload_parse(buffer->data, silc_buffer_len(buffer));
2569 id_type = silc_id_payload_get_type(idp);
2571 silc_socket_stream_get_info(silc_packet_stream_get_stream(sock),
2572 NULL, &hostname, &ip, NULL);
2574 /* Normal server cannot have other normal server connections */
2575 server_entry = (SilcServerEntry)idata;
2576 if (id_type == SILC_ID_SERVER && idata->conn_type == SILC_CONN_SERVER &&
2577 server_entry->server_type == SILC_SERVER)
2580 /* If the packet is coming from server then use the sender as the
2581 origin of the the packet. If it came from router then check the real
2582 sender of the packet and use that as the origin. */
2583 if (idata->conn_type == SILC_CONN_SERVER) {
2584 id_list = server->local_list;
2586 router = server_entry;
2588 /* If the sender is backup router and ID is server (and we are not
2589 backup router) then switch the entry to global list. */
2590 if (server_entry->server_type == SILC_BACKUP_ROUTER &&
2591 id_type == SILC_ID_SERVER &&
2592 server->id_entry->server_type != SILC_BACKUP_ROUTER) {
2593 id_list = server->global_list;
2594 router_sock = server->router ? SILC_PRIMARY_ROUTE(server) : sock;
2597 silc_id_str2id(packet->src_id, packet->src_id_len,
2598 packet->src_id_type, &sender_id, sizeof(sender_id));
2599 router = silc_idlist_find_server_by_id(server->global_list,
2600 &sender_id, TRUE, NULL);
2602 router = silc_idlist_find_server_by_id(server->local_list,
2603 &sender_id, TRUE, NULL);
2605 id_list = server->global_list;
2612 case SILC_ID_CLIENT:
2614 SilcClientEntry entry;
2617 if (!silc_id_payload_get_id(idp, &id, sizeof(id)))
2620 /* Check that we do not have this client already */
2621 entry = silc_idlist_find_client_by_id(server->global_list,
2622 &id, server->server_type,
2625 entry = silc_idlist_find_client_by_id(server->local_list,
2626 &id, server->server_type,
2629 SILC_LOG_DEBUG(("Ignoring client that we already have"));
2633 SILC_LOG_DEBUG(("New client id(%s) from [%s] %s",
2634 silc_id_render(&id, SILC_ID_CLIENT),
2635 idata->conn_type == SILC_CONN_SERVER ?
2636 "Server" : "Router", hostname));
2638 /* As a router we keep information of all global information in our
2639 global list. Cell wide information however is kept in the local
2641 entry = silc_idlist_add_client(id_list, NULL, NULL, NULL,
2644 SILC_LOG_ERROR(("Could not add new client to the ID Cache"));
2646 /* Inform the sender that the ID is not usable */
2647 silc_server_send_notify_signoff(server, sock, FALSE, &id, NULL);
2650 entry->nickname = NULL;
2651 entry->data.status |= SILC_IDLIST_STATUS_REGISTERED;
2653 if (idata->conn_type == SILC_CONN_SERVER)
2654 server->stat.cell_clients++;
2655 server->stat.clients++;
2657 /* Check if anyone is watching this nickname */
2658 if (server->server_type == SILC_ROUTER && id_list == server->local_list)
2659 silc_server_check_watcher_list(server, entry, NULL, 0);
2661 if (server->server_type == SILC_ROUTER) {
2662 /* Add the client's public key to hash table or get the key with
2664 if (entry->data.public_key) {
2665 if (!silc_hash_table_find_by_context(server->pk_hash,
2666 entry->data.public_key,
2668 silc_hash_table_add(server->pk_hash, entry->data.public_key,
2671 silc_server_send_command(server, router_sock,
2672 SILC_COMMAND_GETKEY, ++server->cmd_ident,
2674 silc_buffer_len(buffer));
2679 case SILC_ID_SERVER:
2681 SilcServerEntry entry;
2684 if (!silc_id_payload_get_id(idp, &id, sizeof(id)))
2687 /* If the ID is mine, ignore it. */
2688 if (SILC_ID_SERVER_COMPARE(&id, server->id)) {
2689 SILC_LOG_DEBUG(("Ignoring my own ID as new ID"));
2693 /* If the ID is the sender's ID, ignore it (we have it already) */
2694 if (SILC_ID_SERVER_COMPARE(&id, router->id)) {
2695 SILC_LOG_DEBUG(("Ignoring sender's own ID"));
2699 /* Check that we do not have this server already */
2700 entry = silc_idlist_find_server_by_id(server->global_list,
2701 &id, server->server_type,
2704 entry = silc_idlist_find_server_by_id(server->local_list,
2705 &id, server->server_type,
2708 SILC_LOG_DEBUG(("Ignoring server that we already have"));
2712 SILC_LOG_DEBUG(("New server id(%s) from [%s] %s",
2713 silc_id_render(&id, SILC_ID_SERVER),
2714 idata->conn_type == SILC_CONN_SERVER ?
2715 "Server" : "Router", hostname));
2717 /* As a router we keep information of all global information in our
2718 global list. Cell wide information however is kept in the local
2720 entry = silc_idlist_add_server(id_list, NULL, 0, &id, router,
2723 SILC_LOG_ERROR(("Could not add new server to the ID Cache"));
2726 entry->data.status |= SILC_IDLIST_STATUS_REGISTERED;
2728 if (idata->conn_type == SILC_CONN_SERVER)
2729 server->stat.cell_servers++;
2730 server->stat.servers++;
2734 case SILC_ID_CHANNEL:
2735 SILC_LOG_ERROR(("Channel cannot be registered with NEW_ID packet"));
2744 /* If the sender of this packet is server and we are router we need to
2745 broadcast this packet to other routers in the network. */
2746 if (broadcast && server->server_type == SILC_ROUTER &&
2747 idata->conn_type == SILC_CONN_SERVER &&
2748 !(packet->flags & SILC_PACKET_FLAG_BROADCAST)) {
2749 SILC_LOG_DEBUG(("Broadcasting received New ID packet"));
2750 silc_server_packet_send(server, SILC_PRIMARY_ROUTE(server),
2752 packet->flags | SILC_PACKET_FLAG_BROADCAST,
2753 buffer->data, silc_buffer_len(buffer));
2754 silc_server_backup_send(server, (SilcServerEntry)idata,
2755 packet->type, packet->flags,
2756 packet->buffer.data,
2757 silc_buffer_len(&packet->buffer),
2762 silc_id_payload_free(idp);
2766 /* Processes incoming New ID packet. New ID Payload is used to distribute
2767 information about newly registered clients and servers. */
2769 void silc_server_new_id(SilcServer server, SilcPacketStream sock,
2772 silc_server_new_id_real(server, sock, packet, &packet->buffer, TRUE);
2773 silc_packet_free(packet);
2776 /* Receoved New Id List packet, list of New ID payloads inside one
2777 packet. Process the New ID payloads one by one. */
2779 void silc_server_new_id_list(SilcServer server, SilcPacketStream sock,
2782 SilcIDListData idata = silc_packet_get_context(sock);
2786 SILC_LOG_DEBUG(("Processing New ID List"));
2788 if (idata->conn_type == SILC_CONN_CLIENT ||
2789 packet->src_id_type != SILC_ID_SERVER) {
2790 silc_packet_free(packet);
2794 /* If the sender of this packet is server and we are router we need to
2795 broadcast this packet to other routers in the network. Broadcast
2796 this list packet instead of multiple New ID packets. */
2797 if (server->server_type == SILC_ROUTER &&
2798 idata->conn_type == SILC_CONN_SERVER &&
2799 !(packet->flags & SILC_PACKET_FLAG_BROADCAST)) {
2800 SILC_LOG_DEBUG(("Broadcasting received New ID List packet"));
2801 silc_server_packet_send(server, SILC_PRIMARY_ROUTE(server),
2803 packet->flags | SILC_PACKET_FLAG_BROADCAST,
2804 packet->buffer.data,
2805 silc_buffer_len(&packet->buffer));
2806 silc_server_backup_send(server, (SilcServerEntry)idata,
2807 packet->type, packet->flags,
2808 packet->buffer.data,
2809 silc_buffer_len(&packet->buffer),
2813 idp = silc_buffer_alloc(256);
2815 silc_packet_free(packet);
2819 while (silc_buffer_len(&packet->buffer)) {
2820 SILC_GET16_MSB(id_len, packet->buffer.data + 2);
2821 if ((id_len > silc_buffer_len(&packet->buffer)) ||
2822 (id_len > silc_buffer_truelen(idp)))
2825 silc_buffer_pull_tail(idp, 4 + id_len);
2826 silc_buffer_put(idp, packet->buffer.data, 4 + id_len);
2828 /* Process the New ID */
2829 silc_server_new_id_real(server, sock, packet, idp, FALSE);
2831 silc_buffer_push_tail(idp, 4 + id_len);
2832 silc_buffer_pull(&packet->buffer, 4 + id_len);
2835 silc_buffer_free(idp);
2836 silc_packet_free(packet);
2839 /* Received New Channel packet. Information about new channels in the
2840 network are distributed using this packet. Save the information about
2841 the new channel. This usually comes from router but also normal server
2842 can send this to notify channels it has when it connects to us. */
2844 static void silc_server_new_channel_process(SilcServer server,
2845 SilcPacketStream sock,
2849 SilcIDListData idata = silc_packet_get_context(sock);
2850 SilcChannelPayload payload;
2851 SilcChannelID channel_id;
2852 char *channel_name, *channel_namec = NULL;
2853 SilcUInt32 name_len;
2854 unsigned char *id, cid[32];
2855 SilcUInt32 id_len, cipher_len;
2856 SilcServerEntry server_entry;
2857 SilcChannelEntry channel;
2860 if (idata->conn_type == SILC_CONN_CLIENT ||
2861 packet->src_id_type != SILC_ID_SERVER ||
2862 server->server_type == SILC_SERVER)
2865 /* Parse the channel payload */
2866 payload = silc_channel_payload_parse(buffer->data, silc_buffer_len(buffer));
2870 /* Get the channel ID */
2871 if (!silc_channel_get_id_parse(payload, &channel_id)) {
2872 silc_channel_payload_free(payload);
2876 channel_name = silc_channel_get_name(payload, &name_len);
2877 if (name_len > 256) {
2878 channel_name[256] = '\0';
2882 /* Check channel name */
2883 channel_namec = silc_channel_name_check(channel_name, strlen(channel_name),
2884 SILC_STRING_UTF8, 256, NULL);
2888 id = silc_channel_get_id(payload, &id_len);
2890 server_entry = (SilcServerEntry)idata;
2892 if (idata->conn_type == SILC_CONN_ROUTER) {
2893 /* Add the channel to global list as it is coming from router. It
2894 cannot be our own channel as it is coming from router. */
2896 /* Check that we don't already have this channel */
2897 channel = silc_idlist_find_channel_by_name(server->local_list,
2898 channel_namec, NULL);
2900 channel = silc_idlist_find_channel_by_name(server->global_list,
2901 channel_namec, NULL);
2903 SILC_LOG_DEBUG(("New channel id(%s) from [Router] %s",
2904 silc_id_render(&channel_id, SILC_ID_CHANNEL),
2905 idata->sconn->remote_host));
2908 silc_idlist_add_channel(server->global_list, strdup(channel_name),
2909 0, silc_id_dup(&channel_id, SILC_ID_CHANNEL),
2910 (SilcServerEntry)idata, NULL, NULL, NULL);
2912 silc_channel_payload_free(payload);
2915 channel->disabled = TRUE; /* Disabled until someone JOINs */
2917 server->stat.channels++;
2918 if (server->server_type == SILC_ROUTER)
2919 channel->users_resolved = TRUE;
2922 /* The channel is coming from our server, thus it is in our cell
2923 we will add it to our local list. */
2926 SILC_LOG_DEBUG(("Channel id(%s) from [Server] %s",
2927 silc_id_render(&channel_id, SILC_ID_CHANNEL),
2928 idata->sconn->remote_host));
2930 /* Check that we don't already have this channel */
2931 channel = silc_idlist_find_channel_by_name(server->local_list,
2932 channel_namec, NULL);
2934 channel = silc_idlist_find_channel_by_name(server->global_list,
2935 channel_namec, NULL);
2937 /* If the channel does not exist, then create it. This creates a new
2938 key to the channel as well that we will send to the server. */
2940 SILC_LOG_DEBUG(("Channel is new to us"));
2942 /* The protocol says that the Channel ID's IP address must be based
2943 on the router's IP address. Check whether the ID is based in our
2944 IP and if it is not then create a new ID and enforce the server
2945 to switch the ID. */
2946 if (server_entry->server_type != SILC_BACKUP_ROUTER &&
2947 !SILC_ID_COMPARE(&channel_id, server->id, server->id->ip.data_len)) {
2949 SILC_LOG_DEBUG(("Forcing the server to change Channel ID"));
2950 if (silc_id_create_channel_id(server, server->id, server->rng, &tmp)) {
2951 silc_server_send_notify_channel_change(server, sock, FALSE,
2953 silc_channel_payload_free(payload);
2957 /* Wait that server re-announces this channel */
2961 /* Create the channel with the provided Channel ID */
2962 channel = silc_server_create_new_channel_with_id(server, NULL, NULL,
2964 &channel_id, FALSE);
2966 silc_channel_payload_free(payload);
2969 channel->disabled = TRUE; /* Disabled until someone JOINs */
2971 #if 0 /* We assume that CMODE_CHANGE notify is sent to us after this. */
2973 /* XXX Dunno if this is supposed to be set in any server type. If set
2974 here the CMODE_CHANGE that may follow sets mode that we already
2975 have, and we may loose data from the CMODE_CHANGE notify. */
2976 if (server_entry->server_type != SILC_BACKUP_ROUTER)
2977 channel->mode = silc_channel_get_mode(payload);
2980 /* Send the new channel key to the server */
2981 silc_id_id2str(channel->id, SILC_ID_CHANNEL, cid, sizeof(cid),
2983 cipher = silc_cipher_get_name(channel->send_key);
2984 cipher_len = strlen(cipher);
2985 chk = silc_channel_key_payload_encode(id_len, cid,
2987 channel->key_len / 8,
2989 silc_server_packet_send(server, sock, SILC_PACKET_CHANNEL_KEY, 0,
2990 chk->data, silc_buffer_len(chk));
2991 silc_buffer_free(chk);
2993 /* The channel exist by that name, check whether the ID's match.
2994 If they don't then we'll force the server to use the ID we have.
2995 We also create a new key for the channel. */
2996 SilcBuffer modes = NULL, users = NULL, users_modes = NULL;
2998 SILC_LOG_DEBUG(("Channel already exists"));
3000 if (!SILC_ID_CHANNEL_COMPARE(&channel_id, channel->id)) {
3001 /* They don't match, send CHANNEL_CHANGE notify to the server to
3002 force the ID change. */
3003 SILC_LOG_DEBUG(("Forcing the server to change Channel ID"));
3004 silc_server_send_notify_channel_change(server, sock, FALSE,
3005 &channel_id, channel->id);
3006 silc_channel_payload_free(payload);
3008 /* Wait that server re-announces this channel */
3012 #if 0 /* We will announce our CMODE anyway for this channel, so no need
3013 to check it (implicit enforce). */
3015 /* If the mode is different from what we have then enforce the
3017 mode = silc_channel_get_mode(payload);
3018 if (channel->mode != mode) {
3019 SILC_LOG_DEBUG(("Forcing the server to change channel mode"));
3020 silc_server_send_notify_cmode(server, sock, FALSE, channel,
3021 channel->mode, server->id,
3022 SILC_ID_SERVER, channel->cipher,
3024 channel->passphrase,
3025 channel->founder_key);
3029 /* Create new key for the channel and send it to the server and
3030 everybody else possibly on the channel. */
3031 if (!(channel->mode & SILC_CHANNEL_MODE_PRIVKEY)) {
3033 if (silc_hash_table_count(channel->user_list)) {
3034 if (!silc_server_create_channel_key(server, channel, 0)) {
3035 silc_channel_payload_free(payload);
3039 /* Send to the channel */
3040 silc_server_send_channel_key(server, sock, channel, FALSE);
3043 /* Send to the server */
3044 silc_id_id2str(channel->id, SILC_ID_CHANNEL, cid, sizeof(cid),
3046 cipher = silc_cipher_get_name(channel->send_key);
3047 cipher_len = strlen(cipher);
3048 chk = silc_channel_key_payload_encode(id_len, cid,
3050 channel->key_len / 8,
3052 silc_server_packet_send(server, sock, SILC_PACKET_CHANNEL_KEY, 0,
3053 chk->data, silc_buffer_len(chk));
3054 silc_buffer_free(chk);
3057 /* Since the channel is coming from server and we also know about it
3058 then send the JOIN notify to the server so that it see's our
3059 users on the channel "joining" the channel. */
3060 silc_server_announce_get_channel_users(server, channel, &modes, &users,
3063 silc_buffer_push(users, users->data - users->head);
3064 silc_server_packet_send(server, sock,
3065 SILC_PACKET_NOTIFY, SILC_PACKET_FLAG_LIST,
3066 users->data, silc_buffer_len(users));
3067 silc_buffer_free(users);
3070 silc_buffer_push(modes, modes->data - modes->head);
3071 silc_server_packet_send_dest(server, sock,
3072 SILC_PACKET_NOTIFY, SILC_PACKET_FLAG_LIST,
3073 channel->id, SILC_ID_CHANNEL,
3074 modes->data, silc_buffer_len(modes));
3075 silc_buffer_free(modes);
3078 silc_buffer_push(users_modes, users_modes->data - users_modes->head);
3079 silc_server_packet_send_dest(server, sock,
3080 SILC_PACKET_NOTIFY, SILC_PACKET_FLAG_LIST,
3081 channel->id, SILC_ID_CHANNEL,
3083 silc_buffer_len(users_modes));
3084 silc_buffer_free(users_modes);
3086 if (channel->topic) {
3087 silc_server_send_notify_topic_set(server, sock,
3088 server->server_type == SILC_ROUTER ?
3089 TRUE : FALSE, channel,
3090 server->id, SILC_ID_SERVER,
3096 /* If the sender of this packet is server and we are router we need to
3097 broadcast this packet to other routers in the network. Broadcast
3098 this list packet instead of multiple New Channel packets. */
3099 if (server->server_type == SILC_ROUTER &&
3100 idata->conn_type == SILC_CONN_SERVER &&
3101 !(packet->flags & SILC_PACKET_FLAG_BROADCAST)) {
3102 SILC_LOG_DEBUG(("Broadcasting received New Channel packet"));
3103 silc_server_packet_send(server, SILC_PRIMARY_ROUTE(server),
3105 packet->flags | SILC_PACKET_FLAG_BROADCAST,
3106 buffer->data, silc_buffer_len(buffer));
3107 silc_server_backup_send(server, (SilcServerEntry)idata,
3108 packet->type, packet->flags,
3109 buffer->data, silc_buffer_len(buffer),
3113 silc_free(channel_namec);
3114 silc_channel_payload_free(payload);
3117 /* Received New Channel packet. Information about new channels in the
3118 network are distributed using this packet. Save the information about
3119 the new channel. This usually comes from router but also normal server
3120 can send this to notify channels it has when it connects to us. */
3122 void silc_server_new_channel(SilcServer server,
3123 SilcPacketStream sock,
3126 silc_server_new_channel_process(server, sock, packet, &packet->buffer);
3127 silc_packet_free(packet);
3130 /* Received New Channel List packet, list of New Channel List payloads inside
3131 one packet. Process the New Channel payloads one by one. */
3133 void silc_server_new_channel_list(SilcServer server,
3134 SilcPacketStream sock,
3137 SilcIDListData idata = silc_packet_get_context(sock);
3139 SilcUInt16 len1, len2;
3141 SILC_LOG_DEBUG(("Processing New Channel List"));
3143 if (idata->conn_type == SILC_CONN_CLIENT ||
3144 packet->src_id_type != SILC_ID_SERVER ||
3145 server->server_type == SILC_SERVER) {
3146 silc_packet_free(packet);
3150 buffer = silc_buffer_alloc(512);
3152 silc_packet_free(packet);
3156 while (silc_buffer_len(&packet->buffer)) {
3157 SILC_GET16_MSB(len1, packet->buffer.data);
3158 if ((len1 > silc_buffer_len(&packet->buffer)) ||
3159 (len1 > silc_buffer_truelen(buffer)))
3162 SILC_GET16_MSB(len2, packet->buffer.data + 2 + len1);
3163 if ((len2 > silc_buffer_len(&packet->buffer)) ||
3164 (len2 > silc_buffer_truelen(buffer)))
3167 silc_buffer_pull_tail(buffer, 8 + len1 + len2);
3168 silc_buffer_put(buffer, packet->buffer.data, 8 + len1 + len2);
3170 /* Process the New Channel */
3171 silc_server_new_channel_process(server, sock, packet, buffer);
3173 silc_buffer_push_tail(buffer, 8 + len1 + len2);
3174 silc_buffer_pull(&packet->buffer, 8 + len1 + len2);
3177 silc_buffer_free(buffer);
3178 silc_packet_free(packet);
3181 /* Received key agreement packet. This packet is never for us. It is to
3182 the client in the packet's destination ID. Sending of this sort of packet
3183 equals sending private message, ie. it is sent point to point from
3184 one client to another. */
3186 void silc_server_key_agreement(SilcServer server,
3187 SilcPacketStream sock,
3190 SilcPacketStream dst_sock;
3191 SilcIDListData idata;
3193 SILC_LOG_DEBUG(("Start"));
3195 if (packet->src_id_type != SILC_ID_CLIENT ||
3196 packet->dst_id_type != SILC_ID_CLIENT || !packet->dst_id) {
3197 silc_packet_free(packet);
3201 /* Get the route to the client */
3202 dst_sock = silc_server_get_client_route(server, packet->dst_id,
3203 packet->dst_id_len, NULL,
3206 silc_packet_free(packet);
3210 /* Relay the packet */
3211 silc_server_packet_route(server, dst_sock, packet);
3212 silc_packet_free(packet);
3215 /* Received connection auth request packet that is used during connection
3216 phase to resolve the mandatory authentication method. This packet can
3217 actually be received at anytime but usually it is used only during
3218 the connection authentication phase. Now, protocol says that this packet
3219 can come from client or server, however, we support only this coming
3220 from client and expect that server always knows what authentication
3223 void silc_server_connection_auth_request(SilcServer server,
3224 SilcPacketStream sock,
3227 SilcServerConfigClient *client = NULL;
3228 SilcUInt16 conn_type;
3230 SilcAuthMethod auth_meth = SILC_AUTH_NONE;
3231 const char *hostname, *ip;
3233 if (packet->src_id_type && packet->src_id_type != SILC_ID_CLIENT) {
3234 SILC_LOG_DEBUG(("Request not from client"));
3235 silc_packet_free(packet);
3239 silc_socket_stream_get_info(silc_packet_stream_get_stream(sock),
3240 NULL, &hostname, &ip, NULL);
3242 /* Parse the payload */
3243 ret = silc_buffer_unformat(&packet->buffer,
3244 SILC_STR_UI_SHORT(&conn_type),
3245 SILC_STR_UI_SHORT(NULL),
3247 if (ret == -1 || conn_type != SILC_CONN_CLIENT) {
3248 silc_packet_free(packet);
3252 /* Get the authentication method for the client */
3253 auth_meth = SILC_AUTH_NONE;
3254 client = silc_server_config_find_client(server, (char *)ip);
3256 client = silc_server_config_find_client(server, (char *)hostname);
3258 if (client->passphrase) {
3259 if (client->publickeys && !server->config->prefer_passphrase_auth)
3260 auth_meth = SILC_AUTH_PUBLIC_KEY;
3262 auth_meth = SILC_AUTH_PASSWORD;
3263 } else if (client->publickeys)
3264 auth_meth = SILC_AUTH_PUBLIC_KEY;
3267 SILC_LOG_DEBUG(("Authentication method is [%s]",
3268 (auth_meth == SILC_AUTH_NONE ? "None" :
3269 auth_meth == SILC_AUTH_PASSWORD ? "Passphrase" :
3270 "Digital signatures")));
3272 /* Send it back to the client */
3273 silc_server_send_connection_auth_request(server, sock, conn_type, auth_meth);
3274 silc_packet_free(packet);
3277 /* Received file transger packet. This packet is never for us. It is to
3278 the client in the packet's destination ID. Sending of this sort of packet
3279 equals sending private message, ie. it is sent point to point from
3280 one client to another. */
3282 void silc_server_ftp(SilcServer server,
3283 SilcPacketStream sock,
3286 SilcPacketStream dst_sock;
3287 SilcIDListData idata;
3289 SILC_LOG_DEBUG(("Start"));
3291 if (packet->src_id_type != SILC_ID_CLIENT ||
3292 packet->dst_id_type != SILC_ID_CLIENT || !packet->dst_id) {
3293 silc_packet_free(packet);
3297 /* Get the route to the client */
3298 dst_sock = silc_server_get_client_route(server, packet->dst_id,
3299 packet->dst_id_len, NULL,
3302 silc_packet_free(packet);
3306 /* Relay the packet */
3307 silc_server_packet_route(server, dst_sock, packet);
3308 silc_packet_free(packet);
3313 SilcPacketStream sock;
3315 SilcClientID client_id;
3316 } *SilcServerResumeResolve;
3318 SILC_SERVER_CMD_FUNC(resume_resolve)
3320 SilcServerResumeResolve r = (SilcServerResumeResolve)context;
3321 SilcServer server = r->server;
3322 SilcPacketStream sock = r->sock;
3323 SilcServerCommandReplyContext reply = context2;
3324 SilcClientEntry client;
3325 const char *hostname, *ip;
3327 SILC_LOG_DEBUG(("Start"));
3329 silc_socket_stream_get_info(silc_packet_stream_get_stream(sock),
3330 NULL, &hostname, &ip, NULL);
3332 if (!reply || !silc_command_get_status(reply->payload, NULL, NULL)) {
3333 SILC_LOG_ERROR(("Client %s (%s) tried to resume unknown client, "
3334 "closing connection", hostname, ip));
3335 silc_server_disconnect_remote(server, sock,
3336 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
3337 "Resuming not possible");
3338 silc_server_free_sock_user_data(server, sock, NULL);
3342 if (reply && silc_command_get(reply->payload) == SILC_COMMAND_WHOIS) {
3343 /* Get entry to the client, and resolve it if we don't have it. */
3344 client = silc_idlist_find_client_by_id(server->local_list,
3345 &r->client_id, TRUE, NULL);
3347 client = silc_idlist_find_client_by_id(server->global_list,
3348 &r->client_id, TRUE, NULL);
3350 SILC_LOG_ERROR(("Client %s (%s) tried to resume unknown client, "
3351 "closing connection", hostname, ip));
3352 silc_server_disconnect_remote(server, sock,
3353 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
3354 "Resuming not possible");
3355 silc_server_free_sock_user_data(server, sock, NULL);
3360 if (!(client->mode & SILC_UMODE_DETACHED)) {
3361 SILC_LOG_ERROR(("Client %s (%s) tried to resume un-detached client, "
3362 "closing connection", hostname, ip));
3363 silc_server_disconnect_remote(server, sock,
3364 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
3365 "Resuming not possible");
3366 silc_server_free_sock_user_data(server, sock, NULL);
3370 client->data.status |= SILC_IDLIST_STATUS_RESUME_RES;
3373 /* Reprocess the packet */
3374 silc_server_resume_client(server, sock, r->packet);
3377 silc_packet_stream_unref(r->sock);
3381 /* Received client resuming packet. This is used to resume detached
3382 client session. It can be sent by the client who wishes to resume
3383 but this is also sent by servers and routers to notify other routers
3384 that the client is not detached anymore. */
3386 void silc_server_resume_client(SilcServer server,
3387 SilcPacketStream sock,
3390 SilcBuffer buffer = &packet->buffer, buf;
3391 SilcIDListData idata = silc_packet_get_context(sock);
3392 SilcIDCacheEntry id_cache = NULL;
3393 SilcClientEntry detached_client;
3394 SilcClientID client_id;
3395 unsigned char *id_string, *auth = NULL, *nicknamec = NULL;
3396 unsigned char cid[32];
3398 SilcUInt16 id_len, auth_len = 0;
3399 SilcBool resolved, local, nick_change = FALSE, resolve = FALSE;
3400 SilcChannelEntry channel;
3401 SilcHashTableList htl;
3402 SilcChannelClientEntry chl;
3403 SilcServerResumeResolve r;
3404 const char *cipher, *hostname, *ip;
3406 silc_socket_stream_get_info(silc_packet_stream_get_stream(sock),
3407 NULL, &hostname, &ip, NULL);
3409 if (silc_buffer_unformat(buffer,
3410 SILC_STR_UI16_NSTRING(&id_string, &id_len),
3411 SILC_STR_END) < 0) {
3412 if (idata->conn_type == SILC_CONN_CLIENT) {
3413 SILC_LOG_ERROR(("Client %s (%s) sent incomplete resume information, "
3414 "closing connection", hostname, ip));
3415 silc_server_disconnect_remote(server, sock,
3416 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
3417 "Resuming not possible");
3418 silc_server_free_sock_user_data(server, sock, NULL);
3423 silc_id_str2id(id_string, id_len, SILC_ID_CLIENT, &client_id,
3426 if (idata->conn_type == SILC_CONN_CLIENT) {
3427 /* Client send this and is attempting to resume to old client session */
3428 SilcClientEntry client;
3431 silc_buffer_pull(buffer, 2 + id_len);
3432 auth = buffer->data;
3433 auth_len = silc_buffer_len(buffer);
3434 silc_buffer_push(buffer, 2 + id_len);
3436 if (auth_len < 128) {
3437 SILC_LOG_ERROR(("Client %s (%s) sent incomplete resume information, "
3438 "closing connection", hostname, ip));
3439 silc_server_disconnect_remote(server, sock,
3440 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
3441 "Resuming not possible");
3442 silc_server_free_sock_user_data(server, sock, NULL);
3446 /* Take client entry of this connection */
3447 client = (SilcClientEntry)idata;
3449 /* Get entry to the client, and resolve it if we don't have it. */
3450 detached_client = silc_server_query_client(server, &client_id, FALSE,
3452 if (!detached_client) {
3454 /* The client info is being resolved. Reprocess this packet after
3455 receiving the reply to the query. */
3456 SILC_LOG_DEBUG(("Resolving client"));
3457 r = silc_calloc(1, sizeof(*r));
3460 silc_packet_stream_ref(sock);
3464 r->client_id = client_id;
3465 silc_server_command_pending(server, SILC_COMMAND_WHOIS,
3467 silc_server_command_resume_resolve, r);
3470 SILC_LOG_ERROR(("Client %s (%s) tried to resume unknown client, "
3471 "closing connection", hostname, ip));
3472 silc_server_disconnect_remote(server, sock,
3473 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
3474 "Resuming not possible");
3475 silc_server_free_sock_user_data(server, sock, NULL);
3480 if (detached_client->data.status & SILC_IDLIST_STATUS_RESUMED) {
3481 SILC_LOG_ERROR(("Client %s (%s) tried to attach more than once, "
3482 "closing connection", hostname, ip));
3483 silc_server_disconnect_remote(server, sock,
3484 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
3485 "Resuming not possible");
3486 silc_server_free_sock_user_data(server, sock, NULL);
3490 if (detached_client->resuming_client &&
3491 detached_client->resuming_client != client) {
3492 SILC_LOG_ERROR(("Client %s (%s) tried to attach more than once, "
3493 "closing connection", hostname, ip));
3494 silc_server_disconnect_remote(server, sock,
3495 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
3496 "Resuming not possible");
3497 silc_server_free_sock_user_data(server, sock, NULL);
3501 if (!detached_client->resuming_client)
3502 detached_client->resuming_client = client;
3504 if (!(detached_client->mode & SILC_UMODE_DETACHED))
3506 if (!silc_hash_table_count(detached_client->channels) &&
3507 detached_client->router)
3509 if (!detached_client->nickname)
3511 if (detached_client->data.status & SILC_IDLIST_STATUS_RESUME_RES)
3515 if (server->server_type == SILC_SERVER && !server->standalone) {
3516 /* The client info is being resolved. Reprocess this packet after
3517 receiving the reply to the query. */
3518 SILC_LOG_DEBUG(("Resolving client info"));
3519 silc_server_query_client(server, &client_id, TRUE, NULL);
3520 r = silc_calloc(1, sizeof(*r));
3523 silc_packet_stream_ref(sock);
3527 r->client_id = client_id;
3528 silc_server_command_pending(server, SILC_COMMAND_WHOIS,
3530 silc_server_command_resume_resolve, r);
3533 if (server->server_type == SILC_SERVER) {
3534 SILC_LOG_ERROR(("Client %s (%s) tried to resume un-detached client, "
3535 "closing connection", hostname, ip));
3536 silc_server_disconnect_remote(server, sock,
3537 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
3538 "Resuming not possible");
3539 silc_server_free_sock_user_data(server, sock, NULL);
3544 /* Check that we have the public key of the client, if not then we must
3545 resolve it first. */
3546 if (!detached_client->data.public_key) {
3547 if (server->server_type == SILC_SERVER && server->standalone) {
3548 SILC_LOG_ERROR(("Detached client's public key not present, "
3549 "closing connection"));
3550 silc_server_disconnect_remote(server, sock,
3551 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
3552 "Resuming not possible");
3553 silc_server_free_sock_user_data(server, sock, NULL);
3556 /* We must retrieve the detached client's public key by sending
3557 GETKEY command. Reprocess this packet after receiving the key */
3558 SilcBuffer idp = silc_id_payload_encode(&client_id, SILC_ID_CLIENT);
3559 SilcPacketStream dest_sock =
3560 silc_server_get_client_route(server, NULL, 0, &client_id,
3563 SILC_LOG_DEBUG(("Resolving client public key"));
3565 silc_server_send_command(server, dest_sock ? dest_sock :
3566 SILC_PRIMARY_ROUTE(server),
3567 SILC_COMMAND_GETKEY, ++server->cmd_ident,
3568 1, 1, idp->data, silc_buffer_len(idp));
3570 r = silc_calloc(1, sizeof(*r));
3573 silc_packet_stream_ref(sock);
3577 r->client_id = client_id;
3578 silc_server_command_pending(server, SILC_COMMAND_GETKEY,
3580 silc_server_command_resume_resolve, r);
3582 silc_buffer_free(idp);
3585 } else if (!silc_pkcs_public_key_compare(detached_client->data.public_key,
3586 idata->public_key)) {
3587 /* We require that the connection and resuming authentication data
3588 must be using same key pair. */
3589 SILC_LOG_ERROR(("Resuming attempted with wrong public key, "
3590 "closing connection"));
3591 silc_server_disconnect_remote(server, sock,
3592 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
3593 "Resuming not possible");
3594 silc_server_free_sock_user_data(server, sock, NULL);
3598 /* Verify the authentication payload. This has to be successful in
3599 order to allow the resuming */
3601 !silc_auth_verify_data(auth, auth_len, SILC_AUTH_PUBLIC_KEY,
3602 detached_client->data.public_key, 0,
3603 idata->hash, detached_client->id,
3605 SILC_LOG_ERROR(("Client %s (%s) resume authentication failed, "
3606 "closing connection", hostname, ip));
3607 silc_server_disconnect_remote(server, sock,
3608 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
3609 "Resuming not possible");
3610 silc_server_free_sock_user_data(server, sock, NULL);
3614 /* Check nickname */
3615 nicknamec = silc_identifier_check(detached_client->nickname,
3616 strlen(detached_client->nickname),
3617 SILC_STRING_UTF8, 128, NULL);
3619 silc_server_disconnect_remote(server, sock,
3620 SILC_STATUS_ERR_BAD_NICKNAME,
3621 "Malformed nickname, cannot resume");
3622 silc_server_free_sock_user_data(server, sock, NULL);
3626 /* If the ID is not based in our ID then change it */
3627 if (!SILC_ID_COMPARE(detached_client->id, server->id,
3628 server->id->ip.data_len)) {
3629 SilcClientID *new_id;
3630 if (!silc_id_create_client_id(server, server->id, server->rng,
3631 server->md5hash, nicknamec,
3632 strlen(nicknamec), &new_id)) {
3633 silc_server_disconnect_remote(server, sock,
3634 SILC_STATUS_ERR_BAD_NICKNAME,
3635 "Resuming not possible");
3636 silc_server_free_sock_user_data(server, sock, NULL);
3640 client_id = *new_id;
3644 /* Now resume the client to the network */
3646 silc_schedule_task_del_by_context(server->schedule, detached_client);
3647 silc_packet_set_context(sock, detached_client);
3648 detached_client->connection = sock;
3650 if (detached_client->data.public_key)
3651 silc_hash_table_del_by_context(server->pk_hash,
3652 detached_client->data.public_key,
3654 if (idata->public_key)
3655 silc_hash_table_del_by_context(server->pk_hash,
3656 idata->public_key, idata);
3658 /* Take new keys and stuff into use in the old entry */
3659 silc_idlist_del_data(detached_client);
3660 silc_idlist_add_data(detached_client, idata);
3662 if (detached_client->data.public_key)
3663 silc_hash_table_add(server->pk_hash,
3664 detached_client->data.public_key, detached_client);
3666 detached_client->data.status |= SILC_IDLIST_STATUS_REGISTERED;
3667 detached_client->data.status |= SILC_IDLIST_STATUS_RESUMED;
3668 detached_client->data.status |= SILC_IDLIST_STATUS_LOCAL;
3669 detached_client->data.status &= ~SILC_IDLIST_STATUS_RESUME_RES;
3670 detached_client->mode &= ~SILC_UMODE_DETACHED;
3671 server->stat.my_detached--;
3673 /* We are finished - reset resuming client */
3674 detached_client->resuming_client = NULL;
3676 /* Check if anyone is watching this client */
3677 if (server->server_type == SILC_ROUTER)
3678 silc_server_check_watcher_list(server, detached_client, NULL,
3679 SILC_NOTIFY_TYPE_UMODE_CHANGE);
3681 /* Delete this current client entry since we're resuming to old one. */
3682 server->stat.my_clients--;
3683 server->stat.clients--;
3684 if (server->stat.cell_clients)
3685 server->stat.cell_clients--;
3686 silc_server_remove_from_channels(server, NULL, client, FALSE,
3687 NULL, FALSE, FALSE);
3688 silc_server_del_from_watcher_list(server, client);
3689 if (!silc_idlist_del_client(server->local_list, client))
3690 silc_idlist_del_client(server->global_list, client);
3691 client = detached_client;
3692 silc_free(client->servername);
3693 client->servername = strdup(server->server_name);
3695 /* Send the RESUME_CLIENT packet to our primary router so that others
3696 know this client isn't detached anymore. */
3697 buf = silc_buffer_alloc_size(2 + id_len);
3698 silc_buffer_format(buf,
3699 SILC_STR_UI_SHORT(id_len),
3700 SILC_STR_UI_XNSTRING(id_string, id_len),
3703 /* Send to primary router */
3704 silc_server_packet_send(server, SILC_PRIMARY_ROUTE(server),
3705 SILC_PACKET_RESUME_CLIENT, 0,
3706 buf->data, silc_buffer_len(buf));
3707 silc_server_backup_send(server, client->router,
3708 SILC_PACKET_RESUME_CLIENT, 0,
3709 buf->data, silc_buffer_len(buf), TRUE, TRUE);
3711 /* As router we must deliver this packet directly to the original
3712 server whom this client was earlier. */
3713 if (server->server_type == SILC_ROUTER && client->router &&
3714 client->router->server_type != SILC_ROUTER)
3715 silc_server_packet_send(server, client->router->connection,
3716 SILC_PACKET_RESUME_CLIENT, 0,
3717 buf->data, silc_buffer_len(buf));
3718 silc_buffer_free(buf);
3719 client->router = NULL;
3722 /* Notify about Client ID change, nickname doesn't actually change. */
3723 silc_server_send_notify_nick_change(server, SILC_PRIMARY_ROUTE(server),
3724 SILC_BROADCAST(server),
3725 client->id, &client_id,
3729 /* Resolve users on those channels that client has joined but we
3730 haven't resolved user list yet. */
3731 if (server->server_type == SILC_SERVER && !server->standalone) {
3732 silc_hash_table_list(client->channels, &htl);
3733 while (silc_hash_table_get(&htl, NULL, (void *)&chl)) {
3734 channel = chl->channel;
3735 SILC_LOG_DEBUG(("Resolving users for %s channel",
3736 channel->channel_name));
3737 if (channel->disabled || !channel->users_resolved) {
3738 silc_server_send_command(server, SILC_PRIMARY_ROUTE(server),
3739 SILC_COMMAND_USERS, ++server->cmd_ident,
3740 1, 2, channel->channel_name,
3741 strlen(channel->channel_name));
3744 silc_hash_table_list_reset(&htl);
3747 /* Send the new client ID to the client. After this client may start
3748 receiving other packets, and may start sending packets too. */
3749 silc_server_send_new_id(server, sock, FALSE, &client_id, SILC_ID_CLIENT,
3750 silc_id_get_len(&client_id, SILC_ID_CLIENT));
3753 /* Send NICK change notify to channels as well. */
3754 SilcBuffer oidp, nidp;
3755 oidp = silc_id_payload_encode(client->id, SILC_ID_CLIENT);
3756 nidp = silc_id_payload_encode(&client_id, SILC_ID_CLIENT);
3757 silc_server_send_notify_on_channels(server, NULL, client,
3758 SILC_NOTIFY_TYPE_NICK_CHANGE, 3,
3759 oidp->data, silc_buffer_len(oidp),
3760 nidp->data, silc_buffer_len(nidp),
3762 strlen(client->nickname));
3763 silc_buffer_free(oidp);
3764 silc_buffer_free(nidp);
3767 /* Add the client again to the ID cache to get it to correct list */
3768 if (!silc_idcache_del_by_context(server->local_list->clients, client,
3770 silc_idcache_del_by_context(server->global_list->clients, client, NULL);
3771 silc_free(client->id);
3772 *client->id = client_id;
3773 silc_idcache_add(server->local_list->clients, nicknamec,
3774 client->id, client);
3776 /* Send some nice info to the client */
3777 silc_server_send_connect_notifys(server, sock, client);
3779 /* Send all channel keys of channels the client has joined */
3780 silc_hash_table_list(client->channels, &htl);
3781 while (silc_hash_table_get(&htl, NULL, (void *)&chl)) {
3782 SilcBool created = FALSE;
3783 channel = chl->channel;
3785 if (channel->mode & SILC_CHANNEL_MODE_PRIVKEY)
3788 /* If we don't have channel key, then create one */
3789 if (!channel->send_key) {
3790 if (!silc_server_create_channel_key(server, channel, 0))
3795 silc_id_id2str(channel->id, SILC_ID_CHANNEL, cid, sizeof(cid),
3797 cipher = silc_cipher_get_name(channel->send_key);
3799 silc_channel_key_payload_encode(cid_len, cid,
3800 strlen(cipher), cipher,
3801 channel->key_len / 8, channel->key);
3803 /* Send the channel key to the client */
3804 silc_server_packet_send(server, sock, SILC_PACKET_CHANNEL_KEY, 0,
3805 keyp->data, silc_buffer_len(keyp));
3807 /* Distribute the channel key to channel */
3809 silc_server_send_channel_key(server, NULL, channel,
3810 server->server_type == SILC_ROUTER ?
3811 FALSE : !server->standalone);
3812 silc_server_backup_send(server, NULL, SILC_PACKET_CHANNEL_KEY, 0,
3813 keyp->data, silc_buffer_len(keyp),
3817 silc_buffer_free(keyp);
3819 silc_hash_table_list_reset(&htl);
3821 } else if (idata->conn_type != SILC_CONN_CLIENT) {
3822 /* Server or router sent this to us to notify that that a client has
3824 SilcServerEntry server_entry;
3825 SilcServerID server_id;
3827 /* Get entry to the client, and resolve it if we don't have it. */
3828 detached_client = silc_idlist_find_client_by_id(server->local_list,
3831 if (!detached_client) {
3832 detached_client = silc_idlist_find_client_by_id(server->global_list,
3835 if (!detached_client) {
3836 SILC_LOG_DEBUG(("Resuming client is unknown"));
3841 /* Check that the client has not been resumed already because it is
3842 protocol error to attempt to resume more than once. The client
3843 will be killed if this protocol error occurs. */
3844 if (detached_client->data.status & SILC_IDLIST_STATUS_RESUMED &&
3845 !(detached_client->mode & SILC_UMODE_DETACHED)) {
3846 /* The client is clearly attempting to resume more than once and
3847 perhaps playing around by resuming from several different places
3848 at the same time. */
3849 SILC_LOG_DEBUG(("Attempting to re-resume client, killing both"));
3850 silc_server_kill_client(server, detached_client, NULL,
3851 server->id, SILC_ID_SERVER);
3855 /* Check whether client is detached at all */
3856 if (!(detached_client->mode & SILC_UMODE_DETACHED)) {
3857 SILC_LOG_DEBUG(("Client is not detached"));
3861 /* Check nickname */
3862 if (detached_client->nickname) {
3863 nicknamec = silc_identifier_check(detached_client->nickname,
3864 strlen(detached_client->nickname),
3865 SILC_STRING_UTF8, 128, NULL);
3870 SILC_LOG_DEBUG(("Resuming detached client"));
3872 /* If the sender of this packet is server and we are router we need to
3873 broadcast this packet to other routers in the network. */
3874 if (server->server_type == SILC_ROUTER &&
3875 idata->conn_type == SILC_CONN_SERVER &&
3876 !(packet->flags & SILC_PACKET_FLAG_BROADCAST)) {
3877 SILC_LOG_DEBUG(("Broadcasting received Resume Client packet"));
3878 silc_server_packet_send(server, SILC_PRIMARY_ROUTE(server),
3880 packet->flags | SILC_PACKET_FLAG_BROADCAST,
3881 buffer->data, silc_buffer_len(buffer));
3882 silc_server_backup_send(server, (SilcServerEntry)idata,
3883 packet->type, packet->flags,
3884 packet->buffer.data,
3885 silc_buffer_len(&packet->buffer),
3889 /* Client is detached, and now it is resumed. Remove the detached
3890 mode and mark that it is resumed. */
3892 if (detached_client->data.public_key)
3893 silc_hash_table_del_by_context(server->pk_hash,
3894 detached_client->data.public_key,
3897 silc_idlist_del_data(detached_client);
3898 detached_client->mode &= ~SILC_UMODE_DETACHED;
3899 detached_client->data.status |= SILC_IDLIST_STATUS_RESUMED;
3900 detached_client->data.status &= ~SILC_IDLIST_STATUS_LOCAL;
3902 /* Check if anyone is watching this client */
3903 if (server->server_type == SILC_ROUTER)
3904 silc_server_check_watcher_list(server, detached_client, NULL,
3905 SILC_NOTIFY_TYPE_UMODE_CHANGE);
3907 silc_schedule_task_del_by_context(server->schedule, detached_client);
3909 /* Get the new owner of the resumed client */
3910 if (!silc_id_str2id(packet->src_id, packet->src_id_len,
3911 packet->src_id_type, &server_id, sizeof(server_id)))
3914 /* Get server entry */
3915 server_entry = silc_idlist_find_server_by_id(server->global_list,
3916 &server_id, TRUE, NULL);
3918 if (!server_entry) {
3919 server_entry = silc_idlist_find_server_by_id(server->local_list,
3920 &server_id, TRUE, NULL);
3926 if (server->server_type == SILC_ROUTER &&
3927 idata->conn_type == SILC_CONN_ROUTER &&
3928 server_entry->server_type == SILC_ROUTER)
3931 /* Change the client to correct list. */
3932 if (!silc_idcache_del_by_context(server->local_list->clients,
3933 detached_client, NULL))
3934 silc_idcache_del_by_context(server->global_list->clients,
3935 detached_client, NULL);
3936 silc_idcache_add(local && server->server_type == SILC_ROUTER ?
3937 server->local_list->clients :
3938 server->global_list->clients, nicknamec,
3939 detached_client->id, detached_client);
3941 /* Change the owner of the client */
3942 detached_client->router = server_entry;
3944 /* Update channel information regarding global clients on channel. */
3945 if (server->server_type != SILC_ROUTER) {
3946 silc_hash_table_list(detached_client->channels, &htl);
3947 while (silc_hash_table_get(&htl, NULL, (void *)&chl))
3948 chl->channel->global_users =
3949 silc_server_channel_has_global(chl->channel);
3950 silc_hash_table_list_reset(&htl);
3955 silc_packet_free(packet);