5 Author: Pekka Riikonen <priikone@poseidon.pspt.fi>
7 Copyright (C) 1997 - 2001 Pekka Riikonen
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
21 * Server packet routines to handle received packets.
25 #include "serverincludes.h"
26 #include "server_internal.h"
28 extern char *server_version;
30 /* Received notify packet. Server can receive notify packets from router.
31 Server then relays the notify messages to clients if needed. */
33 void silc_server_notify(SilcServer server,
34 SilcSocketConnection sock,
35 SilcPacketContext *packet)
37 SilcNotifyPayload payload;
39 SilcArgumentPayload args;
40 SilcChannelID *channel_id = NULL, *channel_id2;
41 SilcClientID *client_id, *client_id2;
42 SilcServerID *server_id;
43 SilcChannelEntry channel;
44 SilcClientEntry client;
45 SilcServerEntry server_entry;
46 SilcChannelClientEntry chl;
47 SilcIDCacheEntry cache;
48 SilcHashTableList htl;
54 SILC_LOG_DEBUG(("Start"));
56 if (sock->type == SILC_SOCKET_TYPE_CLIENT ||
57 packet->src_id_type != SILC_ID_SERVER)
63 /* If the packet is destined directly to a client then relay the packet
64 before processing it. */
65 if (packet->dst_id_type == SILC_ID_CLIENT) {
67 SilcSocketConnection dst_sock;
69 /* Get the route to the client */
70 dst_sock = silc_server_get_client_route(server, packet->dst_id,
71 packet->dst_id_len, NULL, &idata);
73 /* Relay the packet */
74 silc_server_relay_packet(server, dst_sock, idata->send_key,
75 idata->hmac_receive, idata->psn_send++,
79 /* Parse the Notify Payload */
80 payload = silc_notify_payload_parse(packet->buffer->data,
85 /* If we are router and this packet is not already broadcast packet
86 we will broadcast it. The sending socket really cannot be router or
87 the router is buggy. If this packet is coming from router then it must
88 have the broadcast flag set already and we won't do anything. */
89 if (!server->standalone && server->server_type == SILC_ROUTER &&
90 sock->type == SILC_SOCKET_TYPE_SERVER &&
91 !(packet->flags & SILC_PACKET_FLAG_BROADCAST)) {
92 SILC_LOG_DEBUG(("Broadcasting received Notify packet"));
93 if (packet->dst_id_type == SILC_ID_CHANNEL) {
94 /* Packet is destined to channel */
95 channel_id = silc_id_str2id(packet->dst_id, packet->dst_id_len,
100 silc_server_packet_send_dest(server, server->router->connection,
102 packet->flags | SILC_PACKET_FLAG_BROADCAST,
103 channel_id, SILC_ID_CHANNEL,
104 packet->buffer->data, packet->buffer->len,
106 silc_server_backup_send_dest(server, (SilcServerEntry)sock->user_data,
107 packet->type, packet->flags,
108 channel_id, SILC_ID_CHANNEL,
109 packet->buffer->data, packet->buffer->len,
112 /* Packet is destined to client or server */
113 silc_server_packet_send(server, server->router->connection,
115 packet->flags | SILC_PACKET_FLAG_BROADCAST,
116 packet->buffer->data, packet->buffer->len,
118 silc_server_backup_send(server, (SilcServerEntry)sock->user_data,
119 packet->type, packet->flags,
120 packet->buffer->data, packet->buffer->len,
125 type = silc_notify_get_type(payload);
126 args = silc_notify_get_args(payload);
131 case SILC_NOTIFY_TYPE_JOIN:
133 * Distribute the notify to local clients on the channel
135 SILC_LOG_DEBUG(("JOIN notify"));
138 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
141 channel_id = silc_id_payload_parse_id(tmp, tmp_len);
145 /* Get channel entry */
146 channel = silc_idlist_find_channel_by_id(server->global_list,
149 channel = silc_idlist_find_channel_by_id(server->local_list,
152 silc_free(channel_id);
156 silc_free(channel_id);
159 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
162 client_id = silc_id_payload_parse_id(tmp, tmp_len);
166 /* If the the client is not in local list we check global list (ie. the
167 channel will be global channel) and if it does not exist then create
168 entry for the client. */
169 client = silc_idlist_find_client_by_id(server->global_list,
170 client_id, server->server_type,
173 client = silc_idlist_find_client_by_id(server->local_list,
174 client_id, server->server_type,
177 /* If router did not find the client the it is bogus */
178 if (server->server_type != SILC_SERVER)
182 silc_idlist_add_client(server->global_list, NULL, NULL, NULL,
183 silc_id_dup(client_id, SILC_ID_CLIENT),
184 sock->user_data, NULL, 0);
186 SILC_LOG_ERROR(("Could not add new client to the ID Cache"));
187 silc_free(client_id);
191 client->data.status |= SILC_IDLIST_STATUS_REGISTERED;
195 /* Do not process the notify if the client is not registered */
196 if (!(client->data.status & SILC_IDLIST_STATUS_REGISTERED))
199 /* Do not add client to channel if it is there already */
200 if (silc_server_client_on_channel(client, channel)) {
201 SILC_LOG_DEBUG(("Client already on channel"));
205 /* Send to channel */
206 silc_server_packet_send_to_channel(server, sock, channel, packet->type,
207 FALSE, packet->buffer->data,
208 packet->buffer->len, FALSE);
210 if (server->server_type != SILC_ROUTER &&
211 sock->type == SILC_SOCKET_TYPE_ROUTER)
212 /* The channel is global now */
213 channel->global_users = TRUE;
215 /* JOIN the global client to the channel (local clients (if router
216 created the channel) is joined in the pending JOIN command). */
217 chl = silc_calloc(1, sizeof(*chl));
218 chl->client = client;
219 chl->channel = channel;
221 /* If this is the first one on the channel then it is the founder of
223 if (!silc_hash_table_count(channel->user_list))
224 chl->mode = (SILC_CHANNEL_UMODE_CHANOP | SILC_CHANNEL_UMODE_CHANFO);
226 silc_hash_table_add(channel->user_list, client, chl);
227 silc_hash_table_add(client->channels, channel, chl);
228 silc_free(client_id);
232 case SILC_NOTIFY_TYPE_LEAVE:
234 * Distribute the notify to local clients on the channel
236 SILC_LOG_DEBUG(("LEAVE notify"));
239 channel_id = silc_id_str2id(packet->dst_id, packet->dst_id_len,
240 packet->dst_id_type);
245 /* Get channel entry */
246 channel = silc_idlist_find_channel_by_id(server->global_list,
249 channel = silc_idlist_find_channel_by_id(server->local_list,
252 silc_free(channel_id);
258 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
260 silc_free(channel_id);
263 client_id = silc_id_payload_parse_id(tmp, tmp_len);
265 silc_free(channel_id);
269 /* Get client entry */
270 client = silc_idlist_find_client_by_id(server->global_list,
271 client_id, TRUE, NULL);
273 client = silc_idlist_find_client_by_id(server->local_list,
274 client_id, TRUE, NULL);
276 silc_free(client_id);
277 silc_free(channel_id);
281 silc_free(client_id);
283 /* Check if on channel */
284 if (!silc_server_client_on_channel(client, channel))
287 /* Send the leave notify to channel */
288 silc_server_packet_send_to_channel(server, sock, channel, packet->type,
289 FALSE, packet->buffer->data,
290 packet->buffer->len, FALSE);
292 /* Remove the user from channel */
293 silc_server_remove_from_one_channel(server, sock, channel, client, FALSE);
296 case SILC_NOTIFY_TYPE_SIGNOFF:
298 * Distribute the notify to local clients on the channel
300 SILC_LOG_DEBUG(("SIGNOFF notify"));
303 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
306 client_id = silc_id_payload_parse_id(tmp, tmp_len);
310 /* Get client entry */
311 client = silc_idlist_find_client_by_id(server->global_list,
312 client_id, TRUE, &cache);
314 client = silc_idlist_find_client_by_id(server->local_list,
315 client_id, TRUE, &cache);
317 silc_free(client_id);
321 silc_free(client_id);
323 /* Get signoff message */
324 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
328 /* Update statistics */
329 server->stat.clients--;
330 if (server->server_type == SILC_ROUTER)
331 server->stat.cell_clients--;
332 SILC_OPER_STATS_UPDATE(client, server, SILC_UMODE_SERVER_OPERATOR);
333 SILC_OPER_STATS_UPDATE(client, router, SILC_UMODE_ROUTER_OPERATOR);
335 /* Remove the client from all channels. */
336 silc_server_remove_from_channels(server, NULL, client, TRUE, tmp, FALSE);
338 client->data.status &= ~SILC_IDLIST_STATUS_REGISTERED;
339 cache->expire = SILC_ID_CACHE_EXPIRE_DEF;
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 channel_id = silc_id_str2id(packet->dst_id, packet->dst_id_len,
351 packet->dst_id_type);
356 /* Get channel entry */
357 channel = silc_idlist_find_channel_by_id(server->global_list,
360 channel = silc_idlist_find_channel_by_id(server->local_list,
363 silc_free(channel_id);
369 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
371 silc_free(channel_id);
375 silc_free(channel->topic);
376 channel->topic = strdup(tmp);
378 /* Send the same notify to the channel */
379 silc_server_packet_send_to_channel(server, sock, channel, packet->type,
380 FALSE, packet->buffer->data,
381 packet->buffer->len, FALSE);
382 silc_free(channel_id);
385 case SILC_NOTIFY_TYPE_NICK_CHANGE:
388 * Distribute the notify to local clients on the channel
390 unsigned char *id, *id2;
392 SILC_LOG_DEBUG(("NICK CHANGE notify"));
394 /* Get old client ID */
395 id = silc_argument_get_arg_type(args, 1, &tmp_len);
398 client_id = silc_id_payload_parse_id(id, tmp_len);
402 /* Get new client ID */
403 id2 = silc_argument_get_arg_type(args, 2, &tmp_len);
406 client_id2 = silc_id_payload_parse_id(id2, tmp_len);
410 SILC_LOG_DEBUG(("Old Client ID id(%s)",
411 silc_id_render(client_id, SILC_ID_CLIENT)));
412 SILC_LOG_DEBUG(("New Client ID id(%s)",
413 silc_id_render(client_id2, SILC_ID_CLIENT)));
415 /* Replace the Client ID */
416 client = silc_idlist_replace_client_id(server->global_list, client_id,
419 client = silc_idlist_replace_client_id(server->local_list, client_id,
423 /* The nickname is not valid anymore, set it NULL. This causes that
424 the nickname will be queried if someone wants to know it. */
425 if (client->nickname)
426 silc_free(client->nickname);
427 client->nickname = NULL;
429 /* Send the NICK_CHANGE notify type to local clients on the channels
430 this client is joined to. */
431 silc_server_send_notify_on_channels(server, NULL, client,
432 SILC_NOTIFY_TYPE_NICK_CHANGE, 2,
437 silc_free(client_id);
439 silc_free(client_id2);
443 case SILC_NOTIFY_TYPE_CMODE_CHANGE:
445 * Distribute the notify to local clients on the channel
448 SILC_LOG_DEBUG(("CMODE CHANGE notify"));
451 channel_id = silc_id_str2id(packet->dst_id, packet->dst_id_len,
452 packet->dst_id_type);
457 /* Get channel entry */
458 channel = silc_idlist_find_channel_by_id(server->global_list,
461 channel = silc_idlist_find_channel_by_id(server->local_list,
464 silc_free(channel_id);
470 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
472 silc_free(channel_id);
476 SILC_GET32_MSB(mode, tmp);
478 /* Check if mode changed */
479 if (channel->mode == mode)
482 /* Send the same notify to the channel */
483 silc_server_packet_send_to_channel(server, sock, channel, packet->type,
484 FALSE, packet->buffer->data,
485 packet->buffer->len, FALSE);
487 /* If the channel had private keys set and the mode was removed then
488 we must re-generate and re-distribute a new channel key */
489 if (channel->mode & SILC_CHANNEL_MODE_PRIVKEY &&
490 !(mode & SILC_CHANNEL_MODE_PRIVKEY)) {
491 /* Re-generate channel key */
492 if (!silc_server_create_channel_key(server, channel, 0))
495 /* Send the channel key. This sends it to our local clients and if
496 we are normal server to our router as well. */
497 silc_server_send_channel_key(server, NULL, channel,
498 server->server_type == SILC_ROUTER ?
499 FALSE : !server->standalone);
503 channel->mode = mode;
504 silc_free(channel_id);
507 tmp = silc_argument_get_arg_type(args, 4, &tmp_len);
509 unsigned char hash[32];
512 silc_hmac_free(channel->hmac);
513 if (!silc_hmac_alloc(tmp, NULL, &channel->hmac))
516 /* Set the HMAC key out of current channel key. The client must do
518 silc_hash_make(silc_hmac_get_hash(channel->hmac), channel->key,
519 channel->key_len / 8,
521 silc_hmac_set_key(channel->hmac, hash,
522 silc_hash_len(silc_hmac_get_hash(channel->hmac)));
523 memset(hash, 0, sizeof(hash));
526 /* Get the passphrase */
527 tmp = silc_argument_get_arg_type(args, 5, &tmp_len);
529 silc_free(channel->passphrase);
530 channel->passphrase = strdup(tmp);
535 case SILC_NOTIFY_TYPE_CUMODE_CHANGE:
538 * Distribute the notify to local clients on the channel
540 SilcChannelClientEntry chl2 = NULL;
541 bool notify_sent = FALSE;
543 SILC_LOG_DEBUG(("CUMODE CHANGE notify"));
546 channel_id = silc_id_str2id(packet->dst_id, packet->dst_id_len,
547 packet->dst_id_type);
552 /* Get channel entry */
553 channel = silc_idlist_find_channel_by_id(server->global_list,
556 channel = silc_idlist_find_channel_by_id(server->local_list,
559 silc_free(channel_id);
565 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
567 silc_free(channel_id);
571 SILC_GET32_MSB(mode, tmp);
573 /* Get target client */
574 tmp = silc_argument_get_arg_type(args, 3, &tmp_len);
577 client_id = silc_id_payload_parse_id(tmp, tmp_len);
581 /* Get client entry */
582 client = silc_idlist_find_client_by_id(server->global_list,
583 client_id, TRUE, NULL);
585 client = silc_idlist_find_client_by_id(server->local_list,
586 client_id, TRUE, NULL);
588 silc_free(client_id);
592 silc_free(client_id);
594 /* Get entry to the channel user list */
595 silc_hash_table_list(channel->user_list, &htl);
596 while (silc_hash_table_get(&htl, NULL, (void *)&chl)) {
597 /* If the mode is channel founder and we already find a client
598 to have that mode on the channel we will enforce the sender
599 to change the channel founder mode away. There can be only one
600 channel founder on the channel. */
601 if (server->server_type == SILC_ROUTER &&
602 mode & SILC_CHANNEL_UMODE_CHANFO &&
603 chl->mode & SILC_CHANNEL_UMODE_CHANFO) {
605 unsigned char cumode[4];
607 if (chl->client == client && chl->mode == mode) {
612 mode &= ~SILC_CHANNEL_UMODE_CHANFO;
613 silc_server_send_notify_cumode(server, sock, FALSE, channel, mode,
614 client->id, SILC_ID_CLIENT,
617 idp = silc_id_payload_encode(client->id, SILC_ID_CLIENT);
618 SILC_PUT32_MSB(mode, cumode);
619 silc_server_send_notify_to_channel(server, sock, channel, FALSE,
620 SILC_NOTIFY_TYPE_CUMODE_CHANGE,
621 3, idp->data, idp->len,
623 idp->data, idp->len);
624 silc_buffer_free(idp);
627 /* Force the mode change if we alredy set the mode */
630 silc_free(channel_id);
635 if (chl->client == client) {
636 if (chl->mode == mode) {
641 /* Change the mode */
643 if (!(mode & SILC_CHANNEL_UMODE_CHANFO))
650 /* Send the same notify to the channel */
652 silc_server_packet_send_to_channel(server, sock, channel,
654 FALSE, packet->buffer->data,
655 packet->buffer->len, FALSE);
657 silc_free(channel_id);
661 case SILC_NOTIFY_TYPE_INVITE:
663 if (packet->dst_id_type == SILC_ID_CLIENT)
666 SILC_LOG_DEBUG(("INVITE notify"));
669 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
672 channel_id = silc_id_payload_parse_id(tmp, tmp_len);
676 /* Get channel entry */
677 channel = silc_idlist_find_channel_by_id(server->global_list,
680 channel = silc_idlist_find_channel_by_id(server->local_list,
683 silc_free(channel_id);
687 silc_free(channel_id);
689 /* Get the added invite */
690 tmp = silc_argument_get_arg_type(args, 3, &tmp_len);
692 if (!channel->invite_list)
693 channel->invite_list = silc_calloc(tmp_len + 2,
694 sizeof(*channel->invite_list));
696 channel->invite_list = silc_realloc(channel->invite_list,
697 sizeof(*channel->invite_list) *
699 strlen(channel->invite_list) +
701 if (tmp[tmp_len - 1] == ',')
702 tmp[tmp_len - 1] = '\0';
704 strncat(channel->invite_list, tmp, tmp_len);
705 strncat(channel->invite_list, ",", 1);
708 /* Get the deleted invite */
709 tmp = silc_argument_get_arg_type(args, 4, &tmp_len);
710 if (tmp && channel->invite_list) {
711 char *start, *end, *n;
713 if (!strncmp(channel->invite_list, tmp,
714 strlen(channel->invite_list) - 1)) {
715 silc_free(channel->invite_list);
716 channel->invite_list = NULL;
718 start = strstr(channel->invite_list, tmp);
719 if (start && strlen(start) >= tmp_len) {
720 end = start + tmp_len;
721 n = silc_calloc(strlen(channel->invite_list) - tmp_len, sizeof(*n));
722 strncat(n, channel->invite_list, start - channel->invite_list);
723 strncat(n, end + 1, ((channel->invite_list +
724 strlen(channel->invite_list)) - end) - 1);
725 silc_free(channel->invite_list);
726 channel->invite_list = n;
733 case SILC_NOTIFY_TYPE_CHANNEL_CHANGE:
735 * Distribute to the local clients on the channel and change the
739 SILC_LOG_DEBUG(("CHANNEL CHANGE"));
741 if (sock->type != SILC_SOCKET_TYPE_ROUTER)
744 /* Get the old Channel ID */
745 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
748 channel_id = silc_id_payload_parse_id(tmp, tmp_len);
752 /* Get the channel entry */
753 channel = silc_idlist_find_channel_by_id(server->global_list,
756 channel = silc_idlist_find_channel_by_id(server->local_list,
759 silc_free(channel_id);
764 /* Send the notify to the channel */
765 silc_server_packet_send_to_channel(server, sock, channel, packet->type,
766 FALSE, packet->buffer->data,
767 packet->buffer->len, FALSE);
769 /* Get the new Channel ID */
770 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
773 channel_id2 = silc_id_payload_parse_id(tmp, tmp_len);
777 SILC_LOG_DEBUG(("Old Channel ID id(%s)",
778 silc_id_render(channel_id, SILC_ID_CHANNEL)));
779 SILC_LOG_DEBUG(("New Channel ID id(%s)",
780 silc_id_render(channel_id2, SILC_ID_CHANNEL)));
782 /* Replace the Channel ID */
783 if (!silc_idlist_replace_channel_id(server->global_list, channel_id,
785 if (!silc_idlist_replace_channel_id(server->local_list, channel_id,
787 silc_free(channel_id2);
792 SilcBuffer users = NULL, users_modes = NULL;
794 /* Re-announce our clients on the channel as the ID has changed now */
795 silc_server_announce_get_channel_users(server, channel, &users,
798 silc_buffer_push(users, users->data - users->head);
799 silc_server_packet_send(server, sock,
800 SILC_PACKET_NOTIFY, SILC_PACKET_FLAG_LIST,
801 users->data, users->len, FALSE);
802 silc_buffer_free(users);
805 silc_buffer_push(users_modes, users_modes->data - users_modes->head);
806 silc_server_packet_send_dest(server, sock,
807 SILC_PACKET_NOTIFY, SILC_PACKET_FLAG_LIST,
808 channel->id, SILC_ID_CHANNEL,
810 users_modes->len, FALSE);
811 silc_buffer_free(users_modes);
814 /* Re-announce channel's topic */
815 if (channel->topic) {
816 silc_server_send_notify_topic_set(server, sock,
817 server->server_type == SILC_ROUTER ?
818 TRUE : FALSE, channel,
819 channel->id, SILC_ID_CHANNEL,
824 silc_free(channel_id);
828 case SILC_NOTIFY_TYPE_SERVER_SIGNOFF:
830 * Remove the server entry and all clients that this server owns.
833 SILC_LOG_DEBUG(("SERVER SIGNOFF notify"));
836 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
839 server_id = silc_id_payload_parse_id(tmp, tmp_len);
843 /* Get server entry */
844 server_entry = silc_idlist_find_server_by_id(server->global_list,
845 server_id, TRUE, NULL);
848 server_entry = silc_idlist_find_server_by_id(server->local_list,
849 server_id, TRUE, NULL);
852 /* If we are normal server then we might not have the server. Check
853 whether router was kind enough to send the list of all clients
854 that actually was to be removed. Remove them if the list is
856 if (server->server_type != SILC_ROUTER &&
857 silc_argument_get_arg_num(args) > 1) {
860 for (i = 1; i < silc_argument_get_arg_num(args); i++) {
862 tmp = silc_argument_get_arg_type(args, i + 1, &tmp_len);
865 client_id = silc_id_payload_parse_id(tmp, tmp_len);
869 /* Get client entry */
870 client = silc_idlist_find_client_by_id(server->global_list,
871 client_id, TRUE, &cache);
874 client = silc_idlist_find_client_by_id(server->local_list,
875 client_id, TRUE, &cache);
878 silc_free(client_id);
882 silc_free(client_id);
884 /* Update statistics */
885 server->stat.clients--;
886 if (server->server_type == SILC_ROUTER)
887 server->stat.cell_clients--;
888 SILC_OPER_STATS_UPDATE(client, server, SILC_UMODE_SERVER_OPERATOR);
889 SILC_OPER_STATS_UPDATE(client, router, SILC_UMODE_ROUTER_OPERATOR);
891 /* Remove the client from all channels. */
892 silc_server_remove_from_channels(server, NULL, client,
895 /* Remove the client */
896 silc_idlist_del_client(local ? server->local_list :
897 server->global_list, client);
901 silc_free(server_id);
905 silc_free(server_id);
907 /* Free all client entries that this server owns as they will
908 become invalid now as well. */
909 silc_server_remove_clients_by_server(server, server_entry, TRUE);
911 /* Remove the server entry */
912 silc_idlist_del_server(local ? server->local_list :
913 server->global_list, server_entry);
915 /* XXX update statistics */
919 case SILC_NOTIFY_TYPE_KICKED:
921 * Distribute the notify to local clients on the channel
924 SILC_LOG_DEBUG(("KICKED notify"));
927 channel_id = silc_id_str2id(packet->dst_id, packet->dst_id_len,
928 packet->dst_id_type);
933 /* Get channel entry */
934 channel = silc_idlist_find_channel_by_id(server->global_list,
937 channel = silc_idlist_find_channel_by_id(server->local_list,
940 silc_free(channel_id);
944 silc_free(channel_id);
947 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
950 client_id = silc_id_payload_parse_id(tmp, tmp_len);
954 /* If the the client is not in local list we check global list */
955 client = silc_idlist_find_client_by_id(server->global_list,
956 client_id, TRUE, NULL);
958 client = silc_idlist_find_client_by_id(server->local_list,
959 client_id, TRUE, NULL);
961 silc_free(client_id);
966 /* Send to channel */
967 silc_server_packet_send_to_channel(server, sock, channel, packet->type,
968 FALSE, packet->buffer->data,
969 packet->buffer->len, FALSE);
971 /* Remove the client from channel */
972 silc_server_remove_from_one_channel(server, sock, channel, client, FALSE);
976 case SILC_NOTIFY_TYPE_KILLED:
979 * Distribute the notify to local clients on channels
984 SILC_LOG_DEBUG(("KILLED notify"));
987 id = silc_argument_get_arg_type(args, 1, &id_len);
990 client_id = silc_id_payload_parse_id(id, id_len);
994 /* If the the client is not in local list we check global list */
995 client = silc_idlist_find_client_by_id(server->global_list,
996 client_id, TRUE, NULL);
998 client = silc_idlist_find_client_by_id(server->local_list,
999 client_id, TRUE, NULL);
1001 silc_free(client_id);
1005 silc_free(client_id);
1007 /* If the client is one of ours, then close the connection to the
1008 client now. This removes the client from all channels as well. */
1009 if (packet->dst_id_type == SILC_ID_CLIENT && client->connection) {
1010 sock = client->connection;
1011 silc_server_free_client_data(server, NULL, client, FALSE, NULL);
1012 silc_server_close_connection(server, sock);
1017 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
1021 /* Send the notify to local clients on the channels except to the
1022 client who is killed. */
1023 silc_server_send_notify_on_channels(server, client, client,
1024 SILC_NOTIFY_TYPE_KILLED,
1029 /* Remove the client from all channels */
1030 silc_server_remove_from_channels(server, NULL, client, FALSE, NULL,
1036 case SILC_NOTIFY_TYPE_UMODE_CHANGE:
1038 * Save the mode of the client.
1041 SILC_LOG_DEBUG(("UMODE_CHANGE notify"));
1044 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
1047 client_id = silc_id_payload_parse_id(tmp, tmp_len);
1051 /* Get client entry */
1052 client = silc_idlist_find_client_by_id(server->global_list,
1053 client_id, TRUE, NULL);
1055 client = silc_idlist_find_client_by_id(server->local_list,
1056 client_id, TRUE, NULL);
1058 silc_free(client_id);
1062 silc_free(client_id);
1065 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
1068 SILC_GET32_MSB(mode, tmp);
1070 #define SILC_UMODE_STATS_UPDATE(oper, mod) \
1072 if (client->mode & (mod)) { \
1073 if (!(mode & (mod))) { \
1074 if (client->connection) \
1075 server->stat.my_ ## oper ## _ops--; \
1076 if (server->server_type == SILC_ROUTER) \
1077 server->stat. oper ## _ops--; \
1080 if (mode & (mod)) { \
1081 if (client->connection) \
1082 server->stat.my_ ## oper ## _ops++; \
1083 if (server->server_type == SILC_ROUTER) \
1084 server->stat. oper ## _ops++; \
1089 /* Update statistics */
1090 SILC_UMODE_STATS_UPDATE(server, SILC_UMODE_SERVER_OPERATOR);
1091 SILC_UMODE_STATS_UPDATE(router, SILC_UMODE_ROUTER_OPERATOR);
1094 client->mode = mode;
1098 case SILC_NOTIFY_TYPE_BAN:
1103 SILC_LOG_DEBUG(("BAN notify"));
1105 /* Get Channel ID */
1106 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
1109 channel_id = silc_id_payload_parse_id(tmp, tmp_len);
1113 /* Get channel entry */
1114 channel = silc_idlist_find_channel_by_id(server->global_list,
1117 channel = silc_idlist_find_channel_by_id(server->local_list,
1120 silc_free(channel_id);
1124 silc_free(channel_id);
1126 /* Get the new ban and add it to the ban list */
1127 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
1129 if (!channel->ban_list)
1130 channel->ban_list = silc_calloc(tmp_len + 2,
1131 sizeof(*channel->ban_list));
1133 channel->ban_list = silc_realloc(channel->ban_list,
1134 sizeof(*channel->ban_list) *
1136 strlen(channel->ban_list) + 2));
1137 strncat(channel->ban_list, tmp, tmp_len);
1138 strncat(channel->ban_list, ",", 1);
1141 /* Get the ban to be removed and remove it from the list */
1142 tmp = silc_argument_get_arg_type(args, 3, &tmp_len);
1143 if (tmp && channel->ban_list) {
1144 char *start, *end, *n;
1146 if (!strncmp(channel->ban_list, tmp, strlen(channel->ban_list) - 1)) {
1147 silc_free(channel->ban_list);
1148 channel->ban_list = NULL;
1150 start = strstr(channel->ban_list, tmp);
1151 if (start && strlen(start) >= tmp_len) {
1152 end = start + tmp_len;
1153 n = silc_calloc(strlen(channel->ban_list) - tmp_len, sizeof(*n));
1154 strncat(n, channel->ban_list, start - channel->ban_list);
1155 strncat(n, end + 1, ((channel->ban_list +
1156 strlen(channel->ban_list)) - end) - 1);
1157 silc_free(channel->ban_list);
1158 channel->ban_list = n;
1164 /* Ignore rest of the notify types for now */
1165 case SILC_NOTIFY_TYPE_NONE:
1166 case SILC_NOTIFY_TYPE_MOTD:
1173 silc_notify_payload_free(payload);
1176 void silc_server_notify_list(SilcServer server,
1177 SilcSocketConnection sock,
1178 SilcPacketContext *packet)
1180 SilcPacketContext *new;
1184 SILC_LOG_DEBUG(("Processing Notify List"));
1186 if (sock->type == SILC_SOCKET_TYPE_CLIENT ||
1187 packet->src_id_type != SILC_ID_SERVER)
1190 /* Make copy of the original packet context, except for the actual
1191 data buffer, which we will here now fetch from the original buffer. */
1192 new = silc_packet_context_alloc();
1193 new->type = SILC_PACKET_NOTIFY;
1194 new->flags = packet->flags;
1195 new->src_id = packet->src_id;
1196 new->src_id_len = packet->src_id_len;
1197 new->src_id_type = packet->src_id_type;
1198 new->dst_id = packet->dst_id;
1199 new->dst_id_len = packet->dst_id_len;
1200 new->dst_id_type = packet->dst_id_type;
1202 buffer = silc_buffer_alloc(1024);
1203 new->buffer = buffer;
1205 while (packet->buffer->len) {
1206 SILC_GET16_MSB(len, packet->buffer->data + 2);
1207 if (len > packet->buffer->len)
1210 if (len > buffer->truelen) {
1211 silc_buffer_free(buffer);
1212 buffer = silc_buffer_alloc(1024 + len);
1215 silc_buffer_pull_tail(buffer, len);
1216 silc_buffer_put(buffer, packet->buffer->data, len);
1218 /* Process the Notify */
1219 silc_server_notify(server, sock, new);
1221 silc_buffer_push_tail(buffer, len);
1222 silc_buffer_pull(packet->buffer, len);
1225 silc_buffer_free(buffer);
1229 /* Received private message. This resolves the destination of the message
1230 and sends the packet. This is used by both server and router. If the
1231 destination is our locally connected client this sends the packet to
1232 the client. This may also send the message for further routing if
1233 the destination is not in our server (or router). */
1235 void silc_server_private_message(SilcServer server,
1236 SilcSocketConnection sock,
1237 SilcPacketContext *packet)
1239 SilcSocketConnection dst_sock;
1240 SilcIDListData idata;
1242 SILC_LOG_DEBUG(("Start"));
1244 if (packet->src_id_type != SILC_ID_CLIENT ||
1245 packet->dst_id_type != SILC_ID_CLIENT || !packet->dst_id)
1248 /* Get the route to the client */
1249 dst_sock = silc_server_get_client_route(server, packet->dst_id,
1250 packet->dst_id_len, NULL, &idata);
1252 /* Send IDENTIFY command reply with error status to indicate that
1253 such destination ID does not exist or is invalid */
1254 SilcBuffer idp = silc_id_payload_encode_data(packet->dst_id,
1256 packet->dst_id_type);
1260 if (packet->src_id_type == SILC_ID_CLIENT) {
1261 SilcClientID *client_id = silc_id_str2id(packet->src_id,
1263 packet->src_id_type);
1264 silc_server_send_dest_command_reply(server, sock,
1265 client_id, SILC_ID_CLIENT,
1266 SILC_COMMAND_IDENTIFY,
1267 SILC_STATUS_ERR_NO_SUCH_CLIENT_ID,
1268 0, 1, 2, idp->data, idp->len);
1269 silc_free(client_id);
1271 silc_server_send_command_reply(server, sock, SILC_COMMAND_IDENTIFY,
1272 SILC_STATUS_ERR_NO_SUCH_CLIENT_ID,
1273 0, 1, 2, idp->data, idp->len);
1276 silc_buffer_free(idp);
1280 /* Send the private message */
1281 silc_server_send_private_message(server, dst_sock, idata->send_key,
1282 idata->hmac_send, idata->psn_send++,
1286 /* Received private message key packet.. This packet is never for us. It is to
1287 the client in the packet's destination ID. Sending of this sort of packet
1288 equals sending private message, ie. it is sent point to point from
1289 one client to another. */
1291 void silc_server_private_message_key(SilcServer server,
1292 SilcSocketConnection sock,
1293 SilcPacketContext *packet)
1295 SilcSocketConnection dst_sock;
1296 SilcIDListData idata;
1298 SILC_LOG_DEBUG(("Start"));
1300 if (packet->src_id_type != SILC_ID_CLIENT ||
1301 packet->dst_id_type != SILC_ID_CLIENT)
1304 if (!packet->dst_id)
1307 /* Get the route to the client */
1308 dst_sock = silc_server_get_client_route(server, packet->dst_id,
1309 packet->dst_id_len, NULL, &idata);
1313 /* Relay the packet */
1314 silc_server_relay_packet(server, dst_sock, idata->send_key,
1315 idata->hmac_send, idata->psn_send++, packet, FALSE);
1318 /* Processes incoming command reply packet. The command reply packet may
1319 be destined to one of our clients or it may directly for us. We will
1320 call the command reply routine after processing the packet. */
1322 void silc_server_command_reply(SilcServer server,
1323 SilcSocketConnection sock,
1324 SilcPacketContext *packet)
1326 SilcBuffer buffer = packet->buffer;
1327 SilcClientEntry client = NULL;
1328 SilcSocketConnection dst_sock;
1329 SilcIDListData idata;
1330 SilcClientID *id = NULL;
1332 SILC_LOG_DEBUG(("Start"));
1334 /* Source must be server or router */
1335 if (packet->src_id_type != SILC_ID_SERVER &&
1336 sock->type != SILC_SOCKET_TYPE_ROUTER)
1339 if (packet->dst_id_type == SILC_ID_CHANNEL)
1342 if (packet->dst_id_type == SILC_ID_CLIENT) {
1343 /* Destination must be one of ours */
1344 id = silc_id_str2id(packet->dst_id, packet->dst_id_len, SILC_ID_CLIENT);
1347 client = silc_idlist_find_client_by_id(server->local_list, id, TRUE, NULL);
1349 SILC_LOG_ERROR(("Cannot process command reply to unknown client"));
1355 if (packet->dst_id_type == SILC_ID_SERVER) {
1356 /* For now this must be for us */
1357 if (memcmp(packet->dst_id, server->id_string, server->id_string_len)) {
1358 SILC_LOG_ERROR(("Cannot process command reply to unknown server"));
1363 /* Execute command reply locally for the command */
1364 silc_server_command_reply_process(server, sock, buffer);
1366 if (packet->dst_id_type == SILC_ID_CLIENT && client && id) {
1367 /* Relay the packet to the client */
1369 dst_sock = (SilcSocketConnection)client->connection;
1370 silc_buffer_push(buffer, SILC_PACKET_HEADER_LEN + packet->src_id_len
1371 + packet->dst_id_len + packet->padlen);
1373 silc_packet_send_prepare(dst_sock, 0, 0, buffer->len);
1374 silc_buffer_put(dst_sock->outbuf, buffer->data, buffer->len);
1376 idata = (SilcIDListData)client;
1378 /* Encrypt packet */
1379 silc_packet_encrypt(idata->send_key, idata->hmac_send, idata->psn_send++,
1380 dst_sock->outbuf, buffer->len);
1382 /* Send the packet */
1383 silc_server_packet_send_real(server, dst_sock, TRUE);
1389 /* Process received channel message. The message can be originated from
1390 client or server. */
1392 void silc_server_channel_message(SilcServer server,
1393 SilcSocketConnection sock,
1394 SilcPacketContext *packet)
1396 SilcChannelEntry channel = NULL;
1397 SilcChannelID *id = NULL;
1398 void *sender = NULL;
1399 void *sender_entry = NULL;
1402 SILC_LOG_DEBUG(("Processing channel message"));
1405 if (packet->dst_id_type != SILC_ID_CHANNEL) {
1406 SILC_LOG_DEBUG(("Received bad message for channel, dropped"));
1410 /* Find channel entry */
1411 id = silc_id_str2id(packet->dst_id, packet->dst_id_len, SILC_ID_CHANNEL);
1414 channel = silc_idlist_find_channel_by_id(server->local_list, id, NULL);
1416 channel = silc_idlist_find_channel_by_id(server->global_list, id, NULL);
1418 SILC_LOG_DEBUG(("Could not find channel"));
1423 /* See that this client is on the channel. If the original sender is
1424 not client (as it can be server as well) we don't do the check. */
1425 sender = silc_id_str2id(packet->src_id, packet->src_id_len,
1426 packet->src_id_type);
1429 if (packet->src_id_type == SILC_ID_CLIENT) {
1430 sender_entry = silc_idlist_find_client_by_id(server->local_list,
1431 sender, TRUE, NULL);
1432 if (!sender_entry) {
1434 sender_entry = silc_idlist_find_client_by_id(server->global_list,
1435 sender, TRUE, NULL);
1437 if (!sender_entry || !silc_server_client_on_channel(sender_entry,
1439 SILC_LOG_DEBUG(("Client not on channel"));
1443 /* If the packet is coming from router, but the client entry is
1444 local entry to us then some router is rerouting this to us and it is
1446 if (server->server_type == SILC_ROUTER &&
1447 sock->type == SILC_SOCKET_TYPE_ROUTER && local) {
1448 SILC_LOG_DEBUG(("Channel message rerouted to the sender, drop it"));
1453 /* Distribute the packet to our local clients. This will send the
1454 packet for further routing as well, if needed. */
1455 silc_server_packet_relay_to_channel(server, sock, channel, sender,
1456 packet->src_id_type, sender_entry,
1457 packet->buffer->data,
1458 packet->buffer->len, FALSE);
1467 /* Received channel key packet. We distribute the key to all of our locally
1468 connected clients on the channel. */
1470 void silc_server_channel_key(SilcServer server,
1471 SilcSocketConnection sock,
1472 SilcPacketContext *packet)
1474 SilcBuffer buffer = packet->buffer;
1475 SilcChannelEntry channel;
1477 if (packet->src_id_type != SILC_ID_SERVER ||
1478 (server->server_type == SILC_ROUTER &&
1479 sock->type == SILC_SOCKET_TYPE_ROUTER))
1482 /* Save the channel key */
1483 channel = silc_server_save_channel_key(server, buffer, NULL);
1487 /* Distribute the key to everybody who is on the channel. If we are router
1488 we will also send it to locally connected servers. */
1489 silc_server_send_channel_key(server, sock, channel, FALSE);
1491 if (server->server_type != SILC_BACKUP_ROUTER) {
1492 /* Distribute to local cell backup routers. */
1493 silc_server_backup_send(server, (SilcServerEntry)sock->user_data,
1494 SILC_PACKET_CHANNEL_KEY, 0,
1495 buffer->data, buffer->len, FALSE, TRUE);
1499 /* Received New Client packet and processes it. Creates Client ID for the
1500 client. Client becomes registered after calling this functions. */
1502 SilcClientEntry silc_server_new_client(SilcServer server,
1503 SilcSocketConnection sock,
1504 SilcPacketContext *packet)
1506 SilcBuffer buffer = packet->buffer;
1507 SilcClientEntry client;
1508 SilcClientID *client_id;
1510 SilcIDListData idata;
1511 char *username = NULL, *realname = NULL, *id_string;
1512 uint16 username_len;
1515 char *hostname, *nickname;
1518 SILC_LOG_DEBUG(("Creating new client"));
1520 if (sock->type != SILC_SOCKET_TYPE_CLIENT)
1523 /* Take client entry */
1524 client = (SilcClientEntry)sock->user_data;
1525 idata = (SilcIDListData)client;
1527 /* Remove the old cache entry. */
1528 if (!silc_idcache_del_by_context(server->local_list->clients, client)) {
1529 SILC_LOG_ERROR(("Lost client's cache entry - bad thing"));
1530 silc_server_disconnect_remote(server, sock, "Server closed connection: "
1535 /* Parse incoming packet */
1536 ret = silc_buffer_unformat(buffer,
1537 SILC_STR_UI16_NSTRING_ALLOC(&username,
1539 SILC_STR_UI16_STRING_ALLOC(&realname),
1542 silc_free(username);
1543 silc_free(realname);
1544 silc_server_disconnect_remote(server, sock, "Server closed connection: "
1545 "Incomplete client information");
1550 silc_free(username);
1551 silc_free(realname);
1552 silc_server_disconnect_remote(server, sock, "Server closed connection: "
1553 "Incomplete client information");
1557 if (username_len > 128)
1558 username[128] = '\0';
1560 /* Check for bad characters for nickname, and modify the nickname if
1561 it includes those. */
1562 if (silc_server_name_bad_chars(username, username_len)) {
1563 nickname = silc_server_name_modify_bad(username, username_len);
1565 nickname = strdup(username);
1568 /* Make sanity checks for the hostname of the client. If the hostname
1569 is provided in the `username' check that it is the same than the
1570 resolved hostname, or if not resolved the hostname that appears in
1571 the client's public key. If the hostname is not present then put
1572 it from the resolved name or from the public key. */
1573 if (strchr(username, '@')) {
1574 SilcPublicKeyIdentifier pident;
1575 int tlen = strcspn(username, "@");
1576 char *phostname = NULL;
1578 hostname = silc_calloc((strlen(username) - tlen) + 1, sizeof(char));
1579 memcpy(hostname, username + tlen + 1, strlen(username) - tlen - 1);
1581 if (strcmp(sock->hostname, sock->ip) &&
1582 strcmp(sock->hostname, hostname)) {
1583 silc_free(username);
1584 silc_free(hostname);
1585 silc_free(realname);
1586 silc_server_disconnect_remote(server, sock,
1587 "Server closed connection: "
1588 "Incomplete client information");
1592 pident = silc_pkcs_decode_identifier(client->data.public_key->identifier);
1594 phostname = strdup(pident->host);
1595 silc_pkcs_free_identifier(pident);
1598 if (!strcmp(sock->hostname, sock->ip) &&
1599 phostname && strcmp(phostname, hostname)) {
1600 silc_free(username);
1601 silc_free(hostname);
1602 silc_free(phostname);
1603 silc_free(realname);
1604 silc_server_disconnect_remote(server, sock,
1605 "Server closed connection: "
1606 "Incomplete client information");
1610 silc_free(phostname);
1612 /* The hostname is not present, add it. */
1614 /* XXX For now we cannot take the host name from the public key since
1615 they are not trusted or we cannot verify them as trusted. Just take
1616 what the resolved name or address is. */
1618 if (strcmp(sock->hostname, sock->ip)) {
1620 newusername = silc_calloc(strlen(username) +
1621 strlen(sock->hostname) + 2,
1622 sizeof(*newusername));
1623 strncat(newusername, username, strlen(username));
1624 strncat(newusername, "@", 1);
1625 strncat(newusername, sock->hostname, strlen(sock->hostname));
1626 silc_free(username);
1627 username = newusername;
1630 SilcPublicKeyIdentifier pident =
1631 silc_pkcs_decode_identifier(client->data.public_key->identifier);
1634 newusername = silc_calloc(strlen(username) +
1635 strlen(pident->host) + 2,
1636 sizeof(*newusername));
1637 strncat(newusername, username, strlen(username));
1638 strncat(newusername, "@", 1);
1639 strncat(newusername, pident->host, strlen(pident->host));
1640 silc_free(username);
1641 username = newusername;
1642 silc_pkcs_free_identifier(pident);
1648 /* Create Client ID */
1649 while (!silc_id_create_client_id(server, server->id, server->rng,
1650 server->md5hash, nickname, &client_id)) {
1652 snprintf(&nickname[strlen(nickname) - 1], 1, "%d", nickfail);
1655 /* Update client entry */
1656 idata->status |= SILC_IDLIST_STATUS_REGISTERED;
1657 client->nickname = nickname;
1658 client->username = username;
1659 client->userinfo = realname ? realname : strdup(" ");
1660 client->id = client_id;
1661 id_len = silc_id_get_len(client_id, SILC_ID_CLIENT);
1663 /* Add the client again to the ID cache */
1664 silc_idcache_add(server->local_list->clients, client->nickname,
1665 client_id, client, 0, NULL);
1667 /* Notify our router about new client on the SILC network */
1668 if (!server->standalone)
1669 silc_server_send_new_id(server, (SilcSocketConnection)
1670 server->router->connection,
1671 server->server_type == SILC_ROUTER ? TRUE : FALSE,
1672 client->id, SILC_ID_CLIENT, id_len);
1674 /* Send the new client ID to the client. */
1675 id_string = silc_id_id2str(client->id, SILC_ID_CLIENT);
1676 reply = silc_buffer_alloc(2 + 2 + id_len);
1677 silc_buffer_pull_tail(reply, SILC_BUFFER_END(reply));
1678 silc_buffer_format(reply,
1679 SILC_STR_UI_SHORT(SILC_ID_CLIENT),
1680 SILC_STR_UI_SHORT(id_len),
1681 SILC_STR_UI_XNSTRING(id_string, id_len),
1683 silc_server_packet_send(server, sock, SILC_PACKET_NEW_ID, 0,
1684 reply->data, reply->len, FALSE);
1685 silc_free(id_string);
1686 silc_buffer_free(reply);
1688 /* Send some nice info to the client */
1689 SILC_SERVER_SEND_NOTIFY(server, sock, SILC_NOTIFY_TYPE_NONE,
1690 ("Welcome to the SILC Network %s",
1692 SILC_SERVER_SEND_NOTIFY(server, sock, SILC_NOTIFY_TYPE_NONE,
1693 ("Your host is %s, running version %s",
1694 server->config->server_info->server_name,
1696 if (server->server_type == SILC_ROUTER) {
1697 SILC_SERVER_SEND_NOTIFY(server, sock, SILC_NOTIFY_TYPE_NONE,
1698 ("There are %d clients on %d servers in SILC "
1699 "Network", server->stat.clients,
1700 server->stat.servers + 1));
1701 SILC_SERVER_SEND_NOTIFY(server, sock, SILC_NOTIFY_TYPE_NONE,
1702 ("There are %d clients on %d server in our cell",
1703 server->stat.cell_clients,
1704 server->stat.cell_servers + 1));
1705 SILC_SERVER_SEND_NOTIFY(server, sock, SILC_NOTIFY_TYPE_NONE,
1706 ("I have %d clients, %d channels, %d servers and "
1708 server->stat.my_clients,
1709 server->stat.my_channels,
1710 server->stat.my_servers,
1711 server->stat.my_routers));
1712 SILC_SERVER_SEND_NOTIFY(server, sock, SILC_NOTIFY_TYPE_NONE,
1713 ("There are %d server operators and %d router "
1715 server->stat.server_ops,
1716 server->stat.router_ops));
1717 SILC_SERVER_SEND_NOTIFY(server, sock, SILC_NOTIFY_TYPE_NONE,
1718 ("I have %d operators online",
1719 server->stat.my_router_ops +
1720 server->stat.my_server_ops));
1722 SILC_SERVER_SEND_NOTIFY(server, sock, SILC_NOTIFY_TYPE_NONE,
1723 ("I have %d clients and %d channels formed",
1724 server->stat.my_clients,
1725 server->stat.my_channels));
1726 SILC_SERVER_SEND_NOTIFY(server, sock, SILC_NOTIFY_TYPE_NONE,
1727 ("%d operators online",
1728 server->stat.my_server_ops));
1730 SILC_SERVER_SEND_NOTIFY(server, sock, SILC_NOTIFY_TYPE_NONE,
1731 ("Your connection is secured with %s cipher, "
1732 "key length %d bits",
1733 idata->send_key->cipher->name,
1734 idata->send_key->cipher->key_len));
1735 SILC_SERVER_SEND_NOTIFY(server, sock, SILC_NOTIFY_TYPE_NONE,
1736 ("Your current nickname is %s",
1740 silc_server_send_motd(server, sock);
1745 /* Create new server. This processes received New Server packet and
1746 saves the received Server ID. The server is our locally connected
1747 server thus we save all the information and save it to local list.
1748 This funtion can be used by both normal server and router server.
1749 If normal server uses this it means that its router has connected
1750 to the server. If router uses this it means that one of the cell's
1751 servers is connected to the router. */
1753 SilcServerEntry silc_server_new_server(SilcServer server,
1754 SilcSocketConnection sock,
1755 SilcPacketContext *packet)
1757 SilcBuffer buffer = packet->buffer;
1758 SilcServerEntry new_server, server_entry;
1759 SilcServerID *server_id;
1760 SilcIDListData idata;
1761 unsigned char *server_name, *id_string;
1762 uint16 id_len, name_len;
1766 SILC_LOG_DEBUG(("Creating new server"));
1768 if (sock->type != SILC_SOCKET_TYPE_SERVER &&
1769 sock->type != SILC_SOCKET_TYPE_ROUTER)
1772 /* Take server entry */
1773 new_server = (SilcServerEntry)sock->user_data;
1774 idata = (SilcIDListData)new_server;
1776 /* Remove the old cache entry */
1777 if (!silc_idcache_del_by_context(server->local_list->servers, new_server)) {
1778 silc_idcache_del_by_context(server->global_list->servers, new_server);
1782 /* Parse the incoming packet */
1783 ret = silc_buffer_unformat(buffer,
1784 SILC_STR_UI16_NSTRING_ALLOC(&id_string, &id_len),
1785 SILC_STR_UI16_NSTRING_ALLOC(&server_name,
1790 silc_free(id_string);
1792 silc_free(server_name);
1796 if (id_len > buffer->len) {
1797 silc_free(id_string);
1798 silc_free(server_name);
1803 server_name[255] = '\0';
1806 server_id = silc_id_str2id(id_string, id_len, SILC_ID_SERVER);
1808 silc_free(id_string);
1809 silc_free(server_name);
1812 silc_free(id_string);
1814 /* Check that we do not have this ID already */
1815 server_entry = silc_idlist_find_server_by_id(server->local_list,
1816 server_id, TRUE, NULL);
1818 silc_idcache_del_by_context(server->local_list->servers, server_entry);
1820 server_entry = silc_idlist_find_server_by_id(server->global_list,
1821 server_id, TRUE, NULL);
1823 silc_idcache_del_by_context(server->global_list->servers, server_entry);
1826 /* Update server entry */
1827 idata->status |= SILC_IDLIST_STATUS_REGISTERED;
1828 new_server->server_name = server_name;
1829 new_server->id = server_id;
1831 SILC_LOG_DEBUG(("New server id(%s)",
1832 silc_id_render(server_id, SILC_ID_SERVER)));
1834 /* Add again the entry to the ID cache. */
1835 silc_idcache_add(local ? server->local_list->servers :
1836 server->global_list->servers, server_name, server_id,
1837 new_server, 0, NULL);
1839 /* Distribute the information about new server in the SILC network
1840 to our router. If we are normal server we won't send anything
1841 since this connection must be our router connection. */
1842 if (server->server_type == SILC_ROUTER && !server->standalone &&
1843 server->router->connection != sock)
1844 silc_server_send_new_id(server, server->router->connection,
1845 TRUE, new_server->id, SILC_ID_SERVER,
1846 silc_id_get_len(server_id, SILC_ID_SERVER));
1848 if (server->server_type == SILC_ROUTER)
1849 server->stat.cell_servers++;
1851 /* Check whether this router connection has been replaced by an
1852 backup router. If it has been then we'll disable the server and will
1853 ignore everything it will send until the backup router resuming
1854 protocol has been completed. */
1855 if (sock->type == SILC_SOCKET_TYPE_ROUTER &&
1856 silc_server_backup_replaced_get(server, server_id, NULL)) {
1857 /* Send packet to the server indicating that it cannot use this
1858 connection as it has been replaced by backup router. */
1859 SilcBuffer packet = silc_buffer_alloc(2);
1860 silc_buffer_pull_tail(packet, SILC_BUFFER_END(packet));
1861 silc_buffer_format(packet,
1862 SILC_STR_UI_CHAR(SILC_SERVER_BACKUP_REPLACED),
1863 SILC_STR_UI_CHAR(0),
1865 silc_server_packet_send(server, sock,
1866 SILC_PACKET_RESUME_ROUTER, 0,
1867 packet->data, packet->len, TRUE);
1868 silc_buffer_free(packet);
1870 /* Mark the router disabled. The data sent earlier will go but nothing
1871 after this does not go to this connection. */
1872 idata->status |= SILC_IDLIST_STATUS_DISABLED;
1874 /* If it is router announce our stuff to it. */
1875 if (sock->type == SILC_SOCKET_TYPE_ROUTER &&
1876 server->server_type == SILC_ROUTER) {
1877 silc_server_announce_servers(server, FALSE, 0, sock);
1878 silc_server_announce_clients(server, 0, sock);
1879 silc_server_announce_channels(server, 0, sock);
1886 /* Processes incoming New ID packet. New ID Payload is used to distribute
1887 information about newly registered clients and servers. */
1889 static void silc_server_new_id_real(SilcServer server,
1890 SilcSocketConnection sock,
1891 SilcPacketContext *packet,
1894 SilcBuffer buffer = packet->buffer;
1896 SilcServerEntry router, server_entry;
1897 SilcSocketConnection router_sock;
1902 SILC_LOG_DEBUG(("Processing new ID"));
1904 if (sock->type == SILC_SOCKET_TYPE_CLIENT ||
1905 server->server_type == SILC_SERVER ||
1906 packet->src_id_type != SILC_ID_SERVER)
1909 idp = silc_id_payload_parse(buffer->data, buffer->len);
1913 id_type = silc_id_payload_get_type(idp);
1915 /* Normal server cannot have other normal server connections */
1916 server_entry = (SilcServerEntry)sock->user_data;
1917 if (id_type == SILC_ID_SERVER && sock->type == SILC_SOCKET_TYPE_SERVER &&
1918 server_entry->server_type == SILC_SERVER)
1921 id = silc_id_payload_get_id(idp);
1925 /* If the packet is coming from server then use the sender as the
1926 origin of the the packet. If it came from router then check the real
1927 sender of the packet and use that as the origin. */
1928 if (sock->type == SILC_SOCKET_TYPE_SERVER) {
1929 id_list = server->local_list;
1931 router = sock->user_data;
1933 /* If the sender is backup router and ID is server (and we are not
1934 backup router) then switch the entry to global list. */
1935 if (server_entry->server_type == SILC_BACKUP_ROUTER &&
1936 id_type == SILC_ID_SERVER &&
1937 server->id_entry->server_type != SILC_BACKUP_ROUTER) {
1938 id_list = server->global_list;
1939 router_sock = server->router ? server->router->connection : sock;
1942 void *sender_id = silc_id_str2id(packet->src_id, packet->src_id_len,
1943 packet->src_id_type);
1944 router = silc_idlist_find_server_by_id(server->global_list,
1945 sender_id, TRUE, NULL);
1947 router = silc_idlist_find_server_by_id(server->local_list,
1948 sender_id, TRUE, NULL);
1949 silc_free(sender_id);
1951 id_list = server->global_list;
1958 case SILC_ID_CLIENT:
1960 SilcClientEntry entry;
1962 /* Check that we do not have this client already */
1963 entry = silc_idlist_find_client_by_id(server->global_list,
1964 id, server->server_type,
1967 entry = silc_idlist_find_client_by_id(server->local_list,
1968 id, server->server_type,
1971 SILC_LOG_DEBUG(("Ignoring client that we already have"));
1975 SILC_LOG_DEBUG(("New client id(%s) from [%s] %s",
1976 silc_id_render(id, SILC_ID_CLIENT),
1977 sock->type == SILC_SOCKET_TYPE_SERVER ?
1978 "Server" : "Router", sock->hostname));
1980 /* As a router we keep information of all global information in our
1981 global list. Cell wide information however is kept in the local
1983 entry = silc_idlist_add_client(id_list, NULL, NULL, NULL,
1984 id, router, NULL, 0);
1986 SILC_LOG_ERROR(("Could not add new client to the ID Cache"));
1988 /* Inform the sender that the ID is not usable */
1989 silc_server_send_notify_signoff(server, sock, FALSE, id, NULL);
1992 entry->nickname = NULL;
1993 entry->data.status |= SILC_IDLIST_STATUS_REGISTERED;
1995 if (sock->type == SILC_SOCKET_TYPE_SERVER)
1996 server->stat.cell_clients++;
1997 server->stat.clients++;
2001 case SILC_ID_SERVER:
2003 SilcServerEntry entry;
2005 /* If the ID is mine, ignore it. */
2006 if (SILC_ID_SERVER_COMPARE(id, server->id)) {
2007 SILC_LOG_DEBUG(("Ignoring my own ID as new ID"));
2011 /* If the ID is the sender's ID, ignore it (we have it already) */
2012 if (SILC_ID_SERVER_COMPARE(id, router->id)) {
2013 SILC_LOG_DEBUG(("Ignoring sender's own ID"));
2017 /* Check that we do not have this server already */
2018 entry = silc_idlist_find_server_by_id(server->global_list,
2019 id, server->server_type,
2022 entry = silc_idlist_find_server_by_id(server->local_list,
2023 id, server->server_type,
2026 SILC_LOG_DEBUG(("Ignoring server that we already have"));
2030 SILC_LOG_DEBUG(("New server id(%s) from [%s] %s",
2031 silc_id_render(id, SILC_ID_SERVER),
2032 sock->type == SILC_SOCKET_TYPE_SERVER ?
2033 "Server" : "Router", sock->hostname));
2035 /* As a router we keep information of all global information in our
2036 global list. Cell wide information however is kept in the local
2038 entry = silc_idlist_add_server(id_list, NULL, 0, id, router,
2041 SILC_LOG_ERROR(("Could not add new server to the ID Cache"));
2044 entry->data.status |= SILC_IDLIST_STATUS_REGISTERED;
2046 if (sock->type == SILC_SOCKET_TYPE_SERVER)
2047 server->stat.cell_servers++;
2048 server->stat.servers++;
2052 case SILC_ID_CHANNEL:
2053 SILC_LOG_ERROR(("Channel cannot be registered with NEW_ID packet"));
2062 /* If the sender of this packet is server and we are router we need to
2063 broadcast this packet to other routers in the network. */
2064 if (broadcast && !server->standalone && server->server_type == SILC_ROUTER &&
2065 sock->type == SILC_SOCKET_TYPE_SERVER &&
2066 !(packet->flags & SILC_PACKET_FLAG_BROADCAST)) {
2067 SILC_LOG_DEBUG(("Broadcasting received New ID packet"));
2068 silc_server_packet_send(server, server->router->connection,
2070 packet->flags | SILC_PACKET_FLAG_BROADCAST,
2071 buffer->data, buffer->len, FALSE);
2072 silc_server_backup_send(server, (SilcServerEntry)sock->user_data,
2073 packet->type, packet->flags,
2074 packet->buffer->data, packet->buffer->len,
2079 silc_id_payload_free(idp);
2083 /* Processes incoming New ID packet. New ID Payload is used to distribute
2084 information about newly registered clients and servers. */
2086 void silc_server_new_id(SilcServer server, SilcSocketConnection sock,
2087 SilcPacketContext *packet)
2089 silc_server_new_id_real(server, sock, packet, TRUE);
2092 /* Receoved New Id List packet, list of New ID payloads inside one
2093 packet. Process the New ID payloads one by one. */
2095 void silc_server_new_id_list(SilcServer server, SilcSocketConnection sock,
2096 SilcPacketContext *packet)
2098 SilcPacketContext *new_id;
2102 SILC_LOG_DEBUG(("Processing New ID List"));
2104 if (sock->type == SILC_SOCKET_TYPE_CLIENT ||
2105 packet->src_id_type != SILC_ID_SERVER)
2108 /* If the sender of this packet is server and we are router we need to
2109 broadcast this packet to other routers in the network. Broadcast
2110 this list packet instead of multiple New ID packets. */
2111 if (!server->standalone && server->server_type == SILC_ROUTER &&
2112 sock->type == SILC_SOCKET_TYPE_SERVER &&
2113 !(packet->flags & SILC_PACKET_FLAG_BROADCAST)) {
2114 SILC_LOG_DEBUG(("Broadcasting received New ID List packet"));
2115 silc_server_packet_send(server, server->router->connection,
2117 packet->flags | SILC_PACKET_FLAG_BROADCAST,
2118 packet->buffer->data, packet->buffer->len, FALSE);
2119 silc_server_backup_send(server, (SilcServerEntry)sock->user_data,
2120 packet->type, packet->flags,
2121 packet->buffer->data, packet->buffer->len,
2125 /* Make copy of the original packet context, except for the actual
2126 data buffer, which we will here now fetch from the original buffer. */
2127 new_id = silc_packet_context_alloc();
2128 new_id->type = SILC_PACKET_NEW_ID;
2129 new_id->flags = packet->flags;
2130 new_id->src_id = packet->src_id;
2131 new_id->src_id_len = packet->src_id_len;
2132 new_id->src_id_type = packet->src_id_type;
2133 new_id->dst_id = packet->dst_id;
2134 new_id->dst_id_len = packet->dst_id_len;
2135 new_id->dst_id_type = packet->dst_id_type;
2137 idp = silc_buffer_alloc(256);
2138 new_id->buffer = idp;
2140 while (packet->buffer->len) {
2141 SILC_GET16_MSB(id_len, packet->buffer->data + 2);
2142 if ((id_len > packet->buffer->len) ||
2143 (id_len > idp->truelen))
2146 silc_buffer_pull_tail(idp, 4 + id_len);
2147 silc_buffer_put(idp, packet->buffer->data, 4 + id_len);
2149 /* Process the New ID */
2150 silc_server_new_id_real(server, sock, new_id, FALSE);
2152 silc_buffer_push_tail(idp, 4 + id_len);
2153 silc_buffer_pull(packet->buffer, 4 + id_len);
2156 silc_buffer_free(idp);
2160 /* Received New Channel packet. Information about new channels in the
2161 network are distributed using this packet. Save the information about
2162 the new channel. This usually comes from router but also normal server
2163 can send this to notify channels it has when it connects to us. */
2165 void silc_server_new_channel(SilcServer server,
2166 SilcSocketConnection sock,
2167 SilcPacketContext *packet)
2169 SilcChannelPayload payload;
2170 SilcChannelID *channel_id;
2176 SilcServerEntry server_entry;
2177 SilcChannelEntry channel;
2179 SILC_LOG_DEBUG(("Processing New Channel"));
2181 if (sock->type == SILC_SOCKET_TYPE_CLIENT ||
2182 packet->src_id_type != SILC_ID_SERVER ||
2183 server->server_type == SILC_SERVER)
2186 /* Parse the channel payload */
2187 payload = silc_channel_payload_parse(packet->buffer->data,
2188 packet->buffer->len);
2192 /* Get the channel ID */
2193 channel_id = silc_channel_get_id_parse(payload);
2195 silc_channel_payload_free(payload);
2199 channel_name = silc_channel_get_name(payload, &name_len);
2201 channel_name[255] = '\0';
2203 id = silc_channel_get_id(payload, &id_len);
2205 server_entry = (SilcServerEntry)sock->user_data;
2207 if (sock->type == SILC_SOCKET_TYPE_ROUTER) {
2208 /* Add the channel to global list as it is coming from router. It
2209 cannot be our own channel as it is coming from router. */
2211 /* Check that we don't already have this channel */
2212 channel = silc_idlist_find_channel_by_name(server->local_list,
2213 channel_name, NULL);
2215 channel = silc_idlist_find_channel_by_name(server->global_list,
2216 channel_name, NULL);
2218 SILC_LOG_DEBUG(("New channel id(%s) from [Router] %s",
2219 silc_id_render(channel_id, SILC_ID_CHANNEL),
2222 silc_idlist_add_channel(server->global_list, strdup(channel_name),
2223 0, channel_id, sock->user_data, NULL, NULL, 0);
2224 server->stat.channels++;
2227 /* The channel is coming from our server, thus it is in our cell
2228 we will add it to our local list. */
2231 SILC_LOG_DEBUG(("Channel id(%s) from [Server] %s",
2232 silc_id_render(channel_id, SILC_ID_CHANNEL),
2235 /* Check that we don't already have this channel */
2236 channel = silc_idlist_find_channel_by_name(server->local_list,
2237 channel_name, NULL);
2239 channel = silc_idlist_find_channel_by_name(server->global_list,
2240 channel_name, NULL);
2242 /* If the channel does not exist, then create it. This creates a new
2243 key to the channel as well that we will send to the server. */
2245 /* The protocol says that the Channel ID's IP address must be based
2246 on the router's IP address. Check whether the ID is based in our
2247 IP and if it is not then create a new ID and enforce the server
2248 to switch the ID. */
2249 if (server_entry->server_type != SILC_BACKUP_ROUTER &&
2250 !SILC_ID_COMPARE(channel_id, server->id, server->id->ip.data_len)) {
2252 SILC_LOG_DEBUG(("Forcing the server to change Channel ID"));
2254 if (silc_id_create_channel_id(server, server->id, server->rng, &tmp)) {
2255 silc_server_send_notify_channel_change(server, sock, FALSE,
2257 silc_free(channel_id);
2262 /* Create the channel with the provided Channel ID */
2263 channel = silc_server_create_new_channel_with_id(server, NULL, NULL,
2267 silc_channel_payload_free(payload);
2268 silc_free(channel_id);
2272 /* Get the mode and set it to the channel */
2273 channel->mode = silc_channel_get_mode(payload);
2275 /* Send the new channel key to the server */
2276 id = silc_id_id2str(channel->id, SILC_ID_CHANNEL);
2277 id_len = silc_id_get_len(channel->id, SILC_ID_CHANNEL);
2278 chk = silc_channel_key_payload_encode(id_len, id,
2279 strlen(channel->channel_key->
2281 channel->channel_key->cipher->name,
2282 channel->key_len / 8,
2284 silc_server_packet_send(server, sock, SILC_PACKET_CHANNEL_KEY, 0,
2285 chk->data, chk->len, FALSE);
2286 silc_buffer_free(chk);
2289 /* The channel exist by that name, check whether the ID's match.
2290 If they don't then we'll force the server to use the ID we have.
2291 We also create a new key for the channel. */
2292 SilcBuffer users = NULL, users_modes = NULL;
2294 if (!SILC_ID_CHANNEL_COMPARE(channel_id, channel->id)) {
2295 /* They don't match, send CHANNEL_CHANGE notify to the server to
2296 force the ID change. */
2297 SILC_LOG_DEBUG(("Forcing the server to change Channel ID"));
2298 silc_server_send_notify_channel_change(server, sock, FALSE,
2299 channel_id, channel->id);
2302 /* If the mode is different from what we have then enforce the
2304 mode = silc_channel_get_mode(payload);
2305 if (channel->mode != mode) {
2306 SILC_LOG_DEBUG(("Forcing the server to change channel mode"));
2307 silc_server_send_notify_cmode(server, sock, FALSE, channel,
2308 channel->mode, server->id,
2310 channel->cipher, channel->hmac_name,
2311 channel->passphrase);
2314 /* Create new key for the channel and send it to the server and
2315 everybody else possibly on the channel. */
2317 if (!(channel->mode & SILC_CHANNEL_MODE_PRIVKEY)) {
2318 if (!silc_server_create_channel_key(server, channel, 0))
2321 /* Send to the channel */
2322 silc_server_send_channel_key(server, sock, channel, FALSE);
2323 id = silc_id_id2str(channel->id, SILC_ID_CHANNEL);
2324 id_len = silc_id_get_len(channel->id, SILC_ID_CHANNEL);
2326 /* Send to the server */
2327 chk = silc_channel_key_payload_encode(id_len, id,
2328 strlen(channel->channel_key->
2330 channel->channel_key->
2332 channel->key_len / 8,
2334 silc_server_packet_send(server, sock, SILC_PACKET_CHANNEL_KEY, 0,
2335 chk->data, chk->len, FALSE);
2336 silc_buffer_free(chk);
2340 silc_free(channel_id);
2342 /* Since the channel is coming from server and we also know about it
2343 then send the JOIN notify to the server so that it see's our
2344 users on the channel "joining" the channel. */
2345 silc_server_announce_get_channel_users(server, channel, &users,
2348 silc_buffer_push(users, users->data - users->head);
2349 silc_server_packet_send(server, sock,
2350 SILC_PACKET_NOTIFY, SILC_PACKET_FLAG_LIST,
2351 users->data, users->len, FALSE);
2352 silc_buffer_free(users);
2355 silc_buffer_push(users_modes, users_modes->data - users_modes->head);
2356 silc_server_packet_send_dest(server, sock,
2357 SILC_PACKET_NOTIFY, SILC_PACKET_FLAG_LIST,
2358 channel->id, SILC_ID_CHANNEL,
2360 users_modes->len, FALSE);
2361 silc_buffer_free(users_modes);
2366 silc_channel_payload_free(payload);
2369 /* Received New Channel List packet, list of New Channel List payloads inside
2370 one packet. Process the New Channel payloads one by one. */
2372 void silc_server_new_channel_list(SilcServer server,
2373 SilcSocketConnection sock,
2374 SilcPacketContext *packet)
2376 SilcPacketContext *new;
2380 SILC_LOG_DEBUG(("Processing New Channel List"));
2382 if (sock->type == SILC_SOCKET_TYPE_CLIENT ||
2383 packet->src_id_type != SILC_ID_SERVER ||
2384 server->server_type == SILC_SERVER)
2387 /* If the sender of this packet is server and we are router we need to
2388 broadcast this packet to other routers in the network. Broadcast
2389 this list packet instead of multiple New Channel packets. */
2390 if (!server->standalone && server->server_type == SILC_ROUTER &&
2391 sock->type == SILC_SOCKET_TYPE_SERVER &&
2392 !(packet->flags & SILC_PACKET_FLAG_BROADCAST)) {
2393 SILC_LOG_DEBUG(("Broadcasting received New Channel List packet"));
2394 silc_server_packet_send(server, server->router->connection,
2396 packet->flags | SILC_PACKET_FLAG_BROADCAST,
2397 packet->buffer->data, packet->buffer->len, FALSE);
2398 silc_server_backup_send(server, (SilcServerEntry)sock->user_data,
2399 packet->type, packet->flags,
2400 packet->buffer->data, packet->buffer->len,
2404 /* Make copy of the original packet context, except for the actual
2405 data buffer, which we will here now fetch from the original buffer. */
2406 new = silc_packet_context_alloc();
2407 new->type = SILC_PACKET_NEW_CHANNEL;
2408 new->flags = packet->flags;
2409 new->src_id = packet->src_id;
2410 new->src_id_len = packet->src_id_len;
2411 new->src_id_type = packet->src_id_type;
2412 new->dst_id = packet->dst_id;
2413 new->dst_id_len = packet->dst_id_len;
2414 new->dst_id_type = packet->dst_id_type;
2416 buffer = silc_buffer_alloc(512);
2417 new->buffer = buffer;
2419 while (packet->buffer->len) {
2420 SILC_GET16_MSB(len1, packet->buffer->data);
2421 if ((len1 > packet->buffer->len) ||
2422 (len1 > buffer->truelen))
2425 SILC_GET16_MSB(len2, packet->buffer->data + 2 + len1);
2426 if ((len2 > packet->buffer->len) ||
2427 (len2 > buffer->truelen))
2430 silc_buffer_pull_tail(buffer, 8 + len1 + len2);
2431 silc_buffer_put(buffer, packet->buffer->data, 8 + len1 + len2);
2433 /* Process the New Channel */
2434 silc_server_new_channel(server, sock, new);
2436 silc_buffer_push_tail(buffer, 8 + len1 + len2);
2437 silc_buffer_pull(packet->buffer, 8 + len1 + len2);
2440 silc_buffer_free(buffer);
2444 /* Received key agreement packet. This packet is never for us. It is to
2445 the client in the packet's destination ID. Sending of this sort of packet
2446 equals sending private message, ie. it is sent point to point from
2447 one client to another. */
2449 void silc_server_key_agreement(SilcServer server,
2450 SilcSocketConnection sock,
2451 SilcPacketContext *packet)
2453 SilcSocketConnection dst_sock;
2454 SilcIDListData idata;
2456 SILC_LOG_DEBUG(("Start"));
2458 if (packet->src_id_type != SILC_ID_CLIENT ||
2459 packet->dst_id_type != SILC_ID_CLIENT)
2462 if (!packet->dst_id)
2465 /* Get the route to the client */
2466 dst_sock = silc_server_get_client_route(server, packet->dst_id,
2467 packet->dst_id_len, NULL, &idata);
2471 /* Relay the packet */
2472 silc_server_relay_packet(server, dst_sock, idata->send_key,
2473 idata->hmac_send, idata->psn_send++,
2477 /* Received connection auth request packet that is used during connection
2478 phase to resolve the mandatory authentication method. This packet can
2479 actually be received at anytime but usually it is used only during
2480 the connection authentication phase. Now, protocol says that this packet
2481 can come from client or server, however, we support only this coming
2482 from client and expect that server always knows what authentication
2485 void silc_server_connection_auth_request(SilcServer server,
2486 SilcSocketConnection sock,
2487 SilcPacketContext *packet)
2489 SilcServerConfigSectionClientConnection *client = NULL;
2492 SilcAuthMethod auth_meth;
2494 SILC_LOG_DEBUG(("Start"));
2496 if (packet->src_id_type && packet->src_id_type != SILC_ID_CLIENT)
2499 /* Parse the payload */
2500 ret = silc_buffer_unformat(packet->buffer,
2501 SILC_STR_UI_SHORT(&conn_type),
2502 SILC_STR_UI_SHORT(NULL),
2507 if (conn_type != SILC_SOCKET_TYPE_CLIENT)
2510 /* Get the authentication method for the client */
2511 auth_meth = SILC_AUTH_NONE;
2512 port = server->sockets[server->sock]->port; /* Listenning port */
2513 client = silc_server_config_find_client_conn(server->config,
2517 client = silc_server_config_find_client_conn(server->config,
2521 auth_meth = client->auth_meth;
2523 /* Send it back to the client */
2524 silc_server_send_connection_auth_request(server, sock,
2529 /* Received REKEY packet. The sender of the packet wants to regenerate
2530 its session keys. This starts the REKEY protocol. */
2532 void silc_server_rekey(SilcServer server,
2533 SilcSocketConnection sock,
2534 SilcPacketContext *packet)
2536 SilcProtocol protocol;
2537 SilcServerRekeyInternalContext *proto_ctx;
2538 SilcIDListData idata = (SilcIDListData)sock->user_data;
2540 SILC_LOG_DEBUG(("Start"));
2542 /* Allocate internal protocol context. This is sent as context
2544 proto_ctx = silc_calloc(1, sizeof(*proto_ctx));
2545 proto_ctx->server = (void *)server;
2546 proto_ctx->sock = sock;
2547 proto_ctx->responder = TRUE;
2548 proto_ctx->pfs = idata->rekey->pfs;
2550 /* Perform rekey protocol. Will call the final callback after the
2551 protocol is over. */
2552 silc_protocol_alloc(SILC_PROTOCOL_SERVER_REKEY,
2553 &protocol, proto_ctx, silc_server_rekey_final);
2554 sock->protocol = protocol;
2556 if (proto_ctx->pfs == FALSE)
2557 /* Run the protocol */
2558 silc_protocol_execute(protocol, server->schedule, 0, 0);
2561 /* Received file transger packet. This packet is never for us. It is to
2562 the client in the packet's destination ID. Sending of this sort of packet
2563 equals sending private message, ie. it is sent point to point from
2564 one client to another. */
2566 void silc_server_ftp(SilcServer server,
2567 SilcSocketConnection sock,
2568 SilcPacketContext *packet)
2570 SilcSocketConnection dst_sock;
2571 SilcIDListData idata;
2573 SILC_LOG_DEBUG(("Start"));
2575 if (packet->src_id_type != SILC_ID_CLIENT ||
2576 packet->dst_id_type != SILC_ID_CLIENT)
2579 if (!packet->dst_id)
2582 /* Get the route to the client */
2583 dst_sock = silc_server_get_client_route(server, packet->dst_id,
2584 packet->dst_id_len, NULL, &idata);
2588 /* Relay the packet */
2589 silc_server_relay_packet(server, dst_sock, idata->send_key,
2590 idata->hmac_send, idata->psn_send++,