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 SILC_LOG_DEBUG(("Joining to channel %s", channel->channel_name));
217 /* JOIN the global client to the channel (local clients (if router
218 created the channel) is joined in the pending JOIN command). */
219 chl = silc_calloc(1, sizeof(*chl));
220 chl->client = client;
221 chl->channel = channel;
223 /* If this is the first one on the channel then it is the founder of
225 if (!silc_hash_table_count(channel->user_list))
226 chl->mode = (SILC_CHANNEL_UMODE_CHANOP | SILC_CHANNEL_UMODE_CHANFO);
228 silc_hash_table_add(channel->user_list, client, chl);
229 silc_hash_table_add(client->channels, channel, chl);
230 silc_free(client_id);
234 case SILC_NOTIFY_TYPE_LEAVE:
236 * Distribute the notify to local clients on the channel
238 SILC_LOG_DEBUG(("LEAVE notify"));
241 channel_id = silc_id_str2id(packet->dst_id, packet->dst_id_len,
242 packet->dst_id_type);
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_free(channel_id);
260 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
262 silc_free(channel_id);
265 client_id = silc_id_payload_parse_id(tmp, tmp_len);
267 silc_free(channel_id);
271 /* Get client entry */
272 client = silc_idlist_find_client_by_id(server->global_list,
273 client_id, TRUE, NULL);
275 client = silc_idlist_find_client_by_id(server->local_list,
276 client_id, TRUE, NULL);
278 silc_free(client_id);
279 silc_free(channel_id);
283 silc_free(client_id);
285 /* Check if on channel */
286 if (!silc_server_client_on_channel(client, channel))
289 /* Send the leave notify to channel */
290 silc_server_packet_send_to_channel(server, sock, channel, packet->type,
291 FALSE, packet->buffer->data,
292 packet->buffer->len, FALSE);
294 /* Remove the user from channel */
295 silc_server_remove_from_one_channel(server, sock, channel, client, FALSE);
298 case SILC_NOTIFY_TYPE_SIGNOFF:
300 * Distribute the notify to local clients on the channel
302 SILC_LOG_DEBUG(("SIGNOFF notify"));
305 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
308 client_id = silc_id_payload_parse_id(tmp, tmp_len);
312 /* Get client entry */
313 client = silc_idlist_find_client_by_id(server->global_list,
314 client_id, TRUE, &cache);
316 client = silc_idlist_find_client_by_id(server->local_list,
317 client_id, TRUE, &cache);
319 silc_free(client_id);
323 silc_free(client_id);
325 /* Get signoff message */
326 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
330 /* Update statistics */
331 server->stat.clients--;
332 if (server->server_type == SILC_ROUTER)
333 server->stat.cell_clients--;
334 SILC_OPER_STATS_UPDATE(client, server, SILC_UMODE_SERVER_OPERATOR);
335 SILC_OPER_STATS_UPDATE(client, router, SILC_UMODE_ROUTER_OPERATOR);
337 /* Remove the client from all channels. */
338 silc_server_remove_from_channels(server, NULL, client, TRUE, tmp, FALSE);
340 client->data.status &= ~SILC_IDLIST_STATUS_REGISTERED;
341 cache->expire = SILC_ID_CACHE_EXPIRE_DEF;
344 case SILC_NOTIFY_TYPE_TOPIC_SET:
346 * Distribute the notify to local clients on the channel
349 SILC_LOG_DEBUG(("TOPIC SET notify"));
352 channel_id = silc_id_str2id(packet->dst_id, packet->dst_id_len,
353 packet->dst_id_type);
358 /* Get channel entry */
359 channel = silc_idlist_find_channel_by_id(server->global_list,
362 channel = silc_idlist_find_channel_by_id(server->local_list,
365 silc_free(channel_id);
371 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
373 silc_free(channel_id);
377 silc_free(channel->topic);
378 channel->topic = strdup(tmp);
380 /* Send the same notify to the channel */
381 silc_server_packet_send_to_channel(server, sock, channel, packet->type,
382 FALSE, packet->buffer->data,
383 packet->buffer->len, FALSE);
384 silc_free(channel_id);
387 case SILC_NOTIFY_TYPE_NICK_CHANGE:
390 * Distribute the notify to local clients on the channel
392 unsigned char *id, *id2;
394 SILC_LOG_DEBUG(("NICK CHANGE notify"));
396 /* Get old client ID */
397 id = silc_argument_get_arg_type(args, 1, &tmp_len);
400 client_id = silc_id_payload_parse_id(id, tmp_len);
404 /* Get new client ID */
405 id2 = silc_argument_get_arg_type(args, 2, &tmp_len);
408 client_id2 = silc_id_payload_parse_id(id2, tmp_len);
412 SILC_LOG_DEBUG(("Old Client ID id(%s)",
413 silc_id_render(client_id, SILC_ID_CLIENT)));
414 SILC_LOG_DEBUG(("New Client ID id(%s)",
415 silc_id_render(client_id2, SILC_ID_CLIENT)));
417 /* Replace the Client ID */
418 client = silc_idlist_replace_client_id(server->global_list, client_id,
421 client = silc_idlist_replace_client_id(server->local_list, client_id,
425 /* The nickname is not valid anymore, set it NULL. This causes that
426 the nickname will be queried if someone wants to know it. */
427 if (client->nickname)
428 silc_free(client->nickname);
429 client->nickname = NULL;
431 /* Send the NICK_CHANGE notify type to local clients on the channels
432 this client is joined to. */
433 silc_server_send_notify_on_channels(server, NULL, client,
434 SILC_NOTIFY_TYPE_NICK_CHANGE, 2,
439 silc_free(client_id);
441 silc_free(client_id2);
445 case SILC_NOTIFY_TYPE_CMODE_CHANGE:
447 * Distribute the notify to local clients on the channel
450 SILC_LOG_DEBUG(("CMODE CHANGE notify"));
453 channel_id = silc_id_str2id(packet->dst_id, packet->dst_id_len,
454 packet->dst_id_type);
459 /* Get channel entry */
460 channel = silc_idlist_find_channel_by_id(server->global_list,
463 channel = silc_idlist_find_channel_by_id(server->local_list,
466 silc_free(channel_id);
472 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
474 silc_free(channel_id);
478 SILC_GET32_MSB(mode, tmp);
480 /* Check if mode changed */
481 if (channel->mode == mode)
484 /* Send the same notify to the channel */
485 silc_server_packet_send_to_channel(server, sock, channel, packet->type,
486 FALSE, packet->buffer->data,
487 packet->buffer->len, FALSE);
489 /* If the channel had private keys set and the mode was removed then
490 we must re-generate and re-distribute a new channel key */
491 if (channel->mode & SILC_CHANNEL_MODE_PRIVKEY &&
492 !(mode & SILC_CHANNEL_MODE_PRIVKEY)) {
493 /* Re-generate channel key */
494 if (!silc_server_create_channel_key(server, channel, 0))
497 /* Send the channel key. This sends it to our local clients and if
498 we are normal server to our router as well. */
499 silc_server_send_channel_key(server, NULL, channel,
500 server->server_type == SILC_ROUTER ?
501 FALSE : !server->standalone);
505 channel->mode = mode;
506 silc_free(channel_id);
509 tmp = silc_argument_get_arg_type(args, 4, &tmp_len);
511 unsigned char hash[32];
514 silc_hmac_free(channel->hmac);
515 if (!silc_hmac_alloc(tmp, NULL, &channel->hmac))
518 /* Set the HMAC key out of current channel key. The client must do
520 silc_hash_make(silc_hmac_get_hash(channel->hmac), channel->key,
521 channel->key_len / 8,
523 silc_hmac_set_key(channel->hmac, hash,
524 silc_hash_len(silc_hmac_get_hash(channel->hmac)));
525 memset(hash, 0, sizeof(hash));
528 /* Get the passphrase */
529 tmp = silc_argument_get_arg_type(args, 5, &tmp_len);
531 silc_free(channel->passphrase);
532 channel->passphrase = strdup(tmp);
537 case SILC_NOTIFY_TYPE_CUMODE_CHANGE:
540 * Distribute the notify to local clients on the channel
542 SilcChannelClientEntry chl2 = NULL;
543 bool notify_sent = FALSE;
545 SILC_LOG_DEBUG(("CUMODE CHANGE notify"));
548 channel_id = silc_id_str2id(packet->dst_id, packet->dst_id_len,
549 packet->dst_id_type);
554 /* Get channel entry */
555 channel = silc_idlist_find_channel_by_id(server->global_list,
558 channel = silc_idlist_find_channel_by_id(server->local_list,
561 silc_free(channel_id);
567 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
569 silc_free(channel_id);
573 SILC_GET32_MSB(mode, tmp);
575 /* Get target client */
576 tmp = silc_argument_get_arg_type(args, 3, &tmp_len);
579 client_id = silc_id_payload_parse_id(tmp, tmp_len);
583 /* Get client entry */
584 client = silc_idlist_find_client_by_id(server->global_list,
585 client_id, TRUE, NULL);
587 client = silc_idlist_find_client_by_id(server->local_list,
588 client_id, TRUE, NULL);
590 silc_free(client_id);
594 silc_free(client_id);
596 /* Get entry to the channel user list */
597 silc_hash_table_list(channel->user_list, &htl);
598 while (silc_hash_table_get(&htl, NULL, (void *)&chl)) {
599 /* If the mode is channel founder and we already find a client
600 to have that mode on the channel we will enforce the sender
601 to change the channel founder mode away. There can be only one
602 channel founder on the channel. */
603 if (server->server_type == SILC_ROUTER &&
604 mode & SILC_CHANNEL_UMODE_CHANFO &&
605 chl->mode & SILC_CHANNEL_UMODE_CHANFO) {
607 unsigned char cumode[4];
609 if (chl->client == client && chl->mode == mode) {
614 mode &= ~SILC_CHANNEL_UMODE_CHANFO;
615 silc_server_send_notify_cumode(server, sock, FALSE, channel, mode,
616 client->id, SILC_ID_CLIENT,
619 idp = silc_id_payload_encode(client->id, SILC_ID_CLIENT);
620 SILC_PUT32_MSB(mode, cumode);
621 silc_server_send_notify_to_channel(server, sock, channel, FALSE,
622 SILC_NOTIFY_TYPE_CUMODE_CHANGE,
623 3, idp->data, idp->len,
625 idp->data, idp->len);
626 silc_buffer_free(idp);
629 /* Force the mode change if we alredy set the mode */
632 silc_free(channel_id);
637 if (chl->client == client) {
638 if (chl->mode == mode) {
643 SILC_LOG_DEBUG(("Changing the channel user mode"));
645 /* Change the mode */
647 if (!(mode & SILC_CHANNEL_UMODE_CHANFO))
654 /* Send the same notify to the channel */
656 silc_server_packet_send_to_channel(server, sock, channel,
658 FALSE, packet->buffer->data,
659 packet->buffer->len, FALSE);
661 silc_free(channel_id);
665 case SILC_NOTIFY_TYPE_INVITE:
667 if (packet->dst_id_type == SILC_ID_CLIENT)
670 SILC_LOG_DEBUG(("INVITE notify"));
673 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
676 channel_id = silc_id_payload_parse_id(tmp, tmp_len);
680 /* Get channel entry */
681 channel = silc_idlist_find_channel_by_id(server->global_list,
684 channel = silc_idlist_find_channel_by_id(server->local_list,
687 silc_free(channel_id);
691 silc_free(channel_id);
693 /* Get the added invite */
694 tmp = silc_argument_get_arg_type(args, 3, &tmp_len);
696 if (!channel->invite_list)
697 channel->invite_list = silc_calloc(tmp_len + 2,
698 sizeof(*channel->invite_list));
700 channel->invite_list = silc_realloc(channel->invite_list,
701 sizeof(*channel->invite_list) *
703 strlen(channel->invite_list) +
705 if (tmp[tmp_len - 1] == ',')
706 tmp[tmp_len - 1] = '\0';
708 strncat(channel->invite_list, tmp, tmp_len);
709 strncat(channel->invite_list, ",", 1);
712 /* Get the deleted invite */
713 tmp = silc_argument_get_arg_type(args, 4, &tmp_len);
714 if (tmp && channel->invite_list) {
715 char *start, *end, *n;
717 if (!strncmp(channel->invite_list, tmp,
718 strlen(channel->invite_list) - 1)) {
719 silc_free(channel->invite_list);
720 channel->invite_list = NULL;
722 start = strstr(channel->invite_list, tmp);
723 if (start && strlen(start) >= tmp_len) {
724 end = start + tmp_len;
725 n = silc_calloc(strlen(channel->invite_list) - tmp_len, sizeof(*n));
726 strncat(n, channel->invite_list, start - channel->invite_list);
727 strncat(n, end + 1, ((channel->invite_list +
728 strlen(channel->invite_list)) - end) - 1);
729 silc_free(channel->invite_list);
730 channel->invite_list = n;
737 case SILC_NOTIFY_TYPE_CHANNEL_CHANGE:
739 * Distribute to the local clients on the channel and change the
743 SILC_LOG_DEBUG(("CHANNEL CHANGE"));
745 if (sock->type != SILC_SOCKET_TYPE_ROUTER)
748 /* Get the old Channel ID */
749 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
752 channel_id = silc_id_payload_parse_id(tmp, tmp_len);
756 /* Get the channel entry */
757 channel = silc_idlist_find_channel_by_id(server->local_list,
760 channel = silc_idlist_find_channel_by_id(server->global_list,
763 silc_free(channel_id);
768 /* Send the notify to the channel */
769 silc_server_packet_send_to_channel(server, sock, channel, packet->type,
770 FALSE, packet->buffer->data,
771 packet->buffer->len, FALSE);
773 /* Get the new Channel ID */
774 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
777 channel_id2 = silc_id_payload_parse_id(tmp, tmp_len);
781 SILC_LOG_DEBUG(("Old Channel ID id(%s)",
782 silc_id_render(channel_id, SILC_ID_CHANNEL)));
783 SILC_LOG_DEBUG(("New Channel ID id(%s)",
784 silc_id_render(channel_id2, SILC_ID_CHANNEL)));
786 /* Replace the Channel ID */
787 if (!silc_idlist_replace_channel_id(server->local_list, channel_id,
789 if (!silc_idlist_replace_channel_id(server->global_list, channel_id,
791 silc_free(channel_id2);
796 SilcBuffer users = NULL, users_modes = NULL;
798 /* Re-announce this channel which ID was changed. */
799 silc_server_send_new_channel(server, sock, FALSE, channel->channel_name,
801 silc_id_get_len(channel->id,
805 /* Re-announce our clients on the channel as the ID has changed now */
806 silc_server_announce_get_channel_users(server, channel, &users,
809 silc_buffer_push(users, users->data - users->head);
810 silc_server_packet_send(server, sock,
811 SILC_PACKET_NOTIFY, SILC_PACKET_FLAG_LIST,
812 users->data, users->len, FALSE);
813 silc_buffer_free(users);
816 silc_buffer_push(users_modes, users_modes->data - users_modes->head);
817 silc_server_packet_send_dest(server, sock,
818 SILC_PACKET_NOTIFY, SILC_PACKET_FLAG_LIST,
819 channel->id, SILC_ID_CHANNEL,
821 users_modes->len, FALSE);
822 silc_buffer_free(users_modes);
825 /* Re-announce channel's topic */
826 if (channel->topic) {
827 silc_server_send_notify_topic_set(server, sock,
828 server->server_type == SILC_ROUTER ?
829 TRUE : FALSE, channel,
830 channel->id, SILC_ID_CHANNEL,
835 silc_free(channel_id);
839 case SILC_NOTIFY_TYPE_SERVER_SIGNOFF:
841 * Remove the server entry and all clients that this server owns.
844 SILC_LOG_DEBUG(("SERVER SIGNOFF notify"));
847 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
850 server_id = silc_id_payload_parse_id(tmp, tmp_len);
854 /* Get server entry */
855 server_entry = silc_idlist_find_server_by_id(server->global_list,
856 server_id, TRUE, NULL);
859 server_entry = silc_idlist_find_server_by_id(server->local_list,
860 server_id, TRUE, NULL);
863 /* If we are normal server then we might not have the server. Check
864 whether router was kind enough to send the list of all clients
865 that actually was to be removed. Remove them if the list is
867 if (server->server_type != SILC_ROUTER &&
868 silc_argument_get_arg_num(args) > 1) {
871 for (i = 1; i < silc_argument_get_arg_num(args); i++) {
873 tmp = silc_argument_get_arg_type(args, i + 1, &tmp_len);
876 client_id = silc_id_payload_parse_id(tmp, tmp_len);
880 /* Get client entry */
881 client = silc_idlist_find_client_by_id(server->global_list,
882 client_id, TRUE, &cache);
885 client = silc_idlist_find_client_by_id(server->local_list,
886 client_id, TRUE, &cache);
889 silc_free(client_id);
893 silc_free(client_id);
895 /* Update statistics */
896 server->stat.clients--;
897 if (server->server_type == SILC_ROUTER)
898 server->stat.cell_clients--;
899 SILC_OPER_STATS_UPDATE(client, server, SILC_UMODE_SERVER_OPERATOR);
900 SILC_OPER_STATS_UPDATE(client, router, SILC_UMODE_ROUTER_OPERATOR);
902 /* Remove the client from all channels. */
903 silc_server_remove_from_channels(server, NULL, client,
906 /* Remove the client */
907 silc_idlist_del_client(local ? server->local_list :
908 server->global_list, client);
912 silc_free(server_id);
916 silc_free(server_id);
918 /* Free all client entries that this server owns as they will
919 become invalid now as well. */
920 silc_server_remove_clients_by_server(server, server_entry, TRUE);
922 /* Remove the server entry */
923 silc_idlist_del_server(local ? server->local_list :
924 server->global_list, server_entry);
926 /* XXX update statistics */
930 case SILC_NOTIFY_TYPE_KICKED:
932 * Distribute the notify to local clients on the channel
935 SILC_LOG_DEBUG(("KICKED notify"));
938 channel_id = silc_id_str2id(packet->dst_id, packet->dst_id_len,
939 packet->dst_id_type);
944 /* Get channel entry */
945 channel = silc_idlist_find_channel_by_id(server->global_list,
948 channel = silc_idlist_find_channel_by_id(server->local_list,
951 silc_free(channel_id);
955 silc_free(channel_id);
958 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
961 client_id = silc_id_payload_parse_id(tmp, tmp_len);
965 /* If the the client is not in local list we check global list */
966 client = silc_idlist_find_client_by_id(server->global_list,
967 client_id, TRUE, NULL);
969 client = silc_idlist_find_client_by_id(server->local_list,
970 client_id, TRUE, NULL);
972 silc_free(client_id);
977 /* Send to channel */
978 silc_server_packet_send_to_channel(server, sock, channel, packet->type,
979 FALSE, packet->buffer->data,
980 packet->buffer->len, FALSE);
982 /* Remove the client from channel */
983 silc_server_remove_from_one_channel(server, sock, channel, client, FALSE);
987 case SILC_NOTIFY_TYPE_KILLED:
990 * Distribute the notify to local clients on channels
995 SILC_LOG_DEBUG(("KILLED notify"));
998 id = silc_argument_get_arg_type(args, 1, &id_len);
1001 client_id = silc_id_payload_parse_id(id, id_len);
1005 /* If the the client is not in local list we check global list */
1006 client = silc_idlist_find_client_by_id(server->global_list,
1007 client_id, TRUE, NULL);
1009 client = silc_idlist_find_client_by_id(server->local_list,
1010 client_id, TRUE, NULL);
1012 silc_free(client_id);
1016 silc_free(client_id);
1018 /* If the client is one of ours, then close the connection to the
1019 client now. This removes the client from all channels as well. */
1020 if (packet->dst_id_type == SILC_ID_CLIENT && client->connection) {
1021 sock = client->connection;
1022 silc_server_free_client_data(server, NULL, client, FALSE, NULL);
1023 silc_server_close_connection(server, sock);
1028 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
1032 /* Send the notify to local clients on the channels except to the
1033 client who is killed. */
1034 silc_server_send_notify_on_channels(server, client, client,
1035 SILC_NOTIFY_TYPE_KILLED,
1040 /* Remove the client from all channels */
1041 silc_server_remove_from_channels(server, NULL, client, FALSE, NULL,
1047 case SILC_NOTIFY_TYPE_UMODE_CHANGE:
1049 * Save the mode of the client.
1052 SILC_LOG_DEBUG(("UMODE_CHANGE notify"));
1055 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
1058 client_id = silc_id_payload_parse_id(tmp, tmp_len);
1062 /* Get client entry */
1063 client = silc_idlist_find_client_by_id(server->global_list,
1064 client_id, TRUE, NULL);
1066 client = silc_idlist_find_client_by_id(server->local_list,
1067 client_id, TRUE, NULL);
1069 silc_free(client_id);
1073 silc_free(client_id);
1076 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
1079 SILC_GET32_MSB(mode, tmp);
1081 #define SILC_UMODE_STATS_UPDATE(oper, mod) \
1083 if (client->mode & (mod)) { \
1084 if (!(mode & (mod))) { \
1085 if (client->connection) \
1086 server->stat.my_ ## oper ## _ops--; \
1087 if (server->server_type == SILC_ROUTER) \
1088 server->stat. oper ## _ops--; \
1091 if (mode & (mod)) { \
1092 if (client->connection) \
1093 server->stat.my_ ## oper ## _ops++; \
1094 if (server->server_type == SILC_ROUTER) \
1095 server->stat. oper ## _ops++; \
1100 /* Update statistics */
1101 SILC_UMODE_STATS_UPDATE(server, SILC_UMODE_SERVER_OPERATOR);
1102 SILC_UMODE_STATS_UPDATE(router, SILC_UMODE_ROUTER_OPERATOR);
1105 client->mode = mode;
1109 case SILC_NOTIFY_TYPE_BAN:
1114 SILC_LOG_DEBUG(("BAN notify"));
1116 /* Get Channel ID */
1117 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
1120 channel_id = silc_id_payload_parse_id(tmp, tmp_len);
1124 /* Get channel entry */
1125 channel = silc_idlist_find_channel_by_id(server->global_list,
1128 channel = silc_idlist_find_channel_by_id(server->local_list,
1131 silc_free(channel_id);
1135 silc_free(channel_id);
1137 /* Get the new ban and add it to the ban list */
1138 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
1140 if (!channel->ban_list)
1141 channel->ban_list = silc_calloc(tmp_len + 2,
1142 sizeof(*channel->ban_list));
1144 channel->ban_list = silc_realloc(channel->ban_list,
1145 sizeof(*channel->ban_list) *
1147 strlen(channel->ban_list) + 2));
1148 strncat(channel->ban_list, tmp, tmp_len);
1149 strncat(channel->ban_list, ",", 1);
1152 /* Get the ban to be removed and remove it from the list */
1153 tmp = silc_argument_get_arg_type(args, 3, &tmp_len);
1154 if (tmp && channel->ban_list) {
1155 char *start, *end, *n;
1157 if (!strncmp(channel->ban_list, tmp, strlen(channel->ban_list) - 1)) {
1158 silc_free(channel->ban_list);
1159 channel->ban_list = NULL;
1161 start = strstr(channel->ban_list, tmp);
1162 if (start && strlen(start) >= tmp_len) {
1163 end = start + tmp_len;
1164 n = silc_calloc(strlen(channel->ban_list) - tmp_len, sizeof(*n));
1165 strncat(n, channel->ban_list, start - channel->ban_list);
1166 strncat(n, end + 1, ((channel->ban_list +
1167 strlen(channel->ban_list)) - end) - 1);
1168 silc_free(channel->ban_list);
1169 channel->ban_list = n;
1175 /* Ignore rest of the notify types for now */
1176 case SILC_NOTIFY_TYPE_NONE:
1177 case SILC_NOTIFY_TYPE_MOTD:
1184 silc_notify_payload_free(payload);
1187 void silc_server_notify_list(SilcServer server,
1188 SilcSocketConnection sock,
1189 SilcPacketContext *packet)
1191 SilcPacketContext *new;
1195 SILC_LOG_DEBUG(("Processing Notify List"));
1197 if (sock->type == SILC_SOCKET_TYPE_CLIENT ||
1198 packet->src_id_type != SILC_ID_SERVER)
1201 /* Make copy of the original packet context, except for the actual
1202 data buffer, which we will here now fetch from the original buffer. */
1203 new = silc_packet_context_alloc();
1204 new->type = SILC_PACKET_NOTIFY;
1205 new->flags = packet->flags;
1206 new->src_id = packet->src_id;
1207 new->src_id_len = packet->src_id_len;
1208 new->src_id_type = packet->src_id_type;
1209 new->dst_id = packet->dst_id;
1210 new->dst_id_len = packet->dst_id_len;
1211 new->dst_id_type = packet->dst_id_type;
1213 buffer = silc_buffer_alloc(1024);
1214 new->buffer = buffer;
1216 while (packet->buffer->len) {
1217 SILC_GET16_MSB(len, packet->buffer->data + 2);
1218 if (len > packet->buffer->len)
1221 if (len > buffer->truelen) {
1222 silc_buffer_free(buffer);
1223 buffer = silc_buffer_alloc(1024 + len);
1226 silc_buffer_pull_tail(buffer, len);
1227 silc_buffer_put(buffer, packet->buffer->data, len);
1229 /* Process the Notify */
1230 silc_server_notify(server, sock, new);
1232 silc_buffer_push_tail(buffer, len);
1233 silc_buffer_pull(packet->buffer, len);
1236 silc_buffer_free(buffer);
1240 /* Received private message. This resolves the destination of the message
1241 and sends the packet. This is used by both server and router. If the
1242 destination is our locally connected client this sends the packet to
1243 the client. This may also send the message for further routing if
1244 the destination is not in our server (or router). */
1246 void silc_server_private_message(SilcServer server,
1247 SilcSocketConnection sock,
1248 SilcPacketContext *packet)
1250 SilcSocketConnection dst_sock;
1251 SilcIDListData idata;
1253 SILC_LOG_DEBUG(("Start"));
1255 if (packet->src_id_type != SILC_ID_CLIENT ||
1256 packet->dst_id_type != SILC_ID_CLIENT || !packet->dst_id)
1259 /* Get the route to the client */
1260 dst_sock = silc_server_get_client_route(server, packet->dst_id,
1261 packet->dst_id_len, NULL, &idata);
1263 /* Send IDENTIFY command reply with error status to indicate that
1264 such destination ID does not exist or is invalid */
1265 SilcBuffer idp = silc_id_payload_encode_data(packet->dst_id,
1267 packet->dst_id_type);
1271 if (packet->src_id_type == SILC_ID_CLIENT) {
1272 SilcClientID *client_id = silc_id_str2id(packet->src_id,
1274 packet->src_id_type);
1275 silc_server_send_dest_command_reply(server, sock,
1276 client_id, SILC_ID_CLIENT,
1277 SILC_COMMAND_IDENTIFY,
1278 SILC_STATUS_ERR_NO_SUCH_CLIENT_ID,
1279 0, 1, 2, idp->data, idp->len);
1280 silc_free(client_id);
1282 silc_server_send_command_reply(server, sock, SILC_COMMAND_IDENTIFY,
1283 SILC_STATUS_ERR_NO_SUCH_CLIENT_ID,
1284 0, 1, 2, idp->data, idp->len);
1287 silc_buffer_free(idp);
1291 /* Send the private message */
1292 silc_server_send_private_message(server, dst_sock, idata->send_key,
1293 idata->hmac_send, idata->psn_send++,
1297 /* Received private message key packet.. This packet is never for us. It is to
1298 the client in the packet's destination ID. Sending of this sort of packet
1299 equals sending private message, ie. it is sent point to point from
1300 one client to another. */
1302 void silc_server_private_message_key(SilcServer server,
1303 SilcSocketConnection sock,
1304 SilcPacketContext *packet)
1306 SilcSocketConnection dst_sock;
1307 SilcIDListData idata;
1309 SILC_LOG_DEBUG(("Start"));
1311 if (packet->src_id_type != SILC_ID_CLIENT ||
1312 packet->dst_id_type != SILC_ID_CLIENT)
1315 if (!packet->dst_id)
1318 /* Get the route to the client */
1319 dst_sock = silc_server_get_client_route(server, packet->dst_id,
1320 packet->dst_id_len, NULL, &idata);
1324 /* Relay the packet */
1325 silc_server_relay_packet(server, dst_sock, idata->send_key,
1326 idata->hmac_send, idata->psn_send++, packet, FALSE);
1329 /* Processes incoming command reply packet. The command reply packet may
1330 be destined to one of our clients or it may directly for us. We will
1331 call the command reply routine after processing the packet. */
1333 void silc_server_command_reply(SilcServer server,
1334 SilcSocketConnection sock,
1335 SilcPacketContext *packet)
1337 SilcBuffer buffer = packet->buffer;
1338 SilcClientEntry client = NULL;
1339 SilcSocketConnection dst_sock;
1340 SilcIDListData idata;
1341 SilcClientID *id = NULL;
1343 SILC_LOG_DEBUG(("Start"));
1345 /* Source must be server or router */
1346 if (packet->src_id_type != SILC_ID_SERVER &&
1347 sock->type != SILC_SOCKET_TYPE_ROUTER)
1350 if (packet->dst_id_type == SILC_ID_CHANNEL)
1353 if (packet->dst_id_type == SILC_ID_CLIENT) {
1354 /* Destination must be one of ours */
1355 id = silc_id_str2id(packet->dst_id, packet->dst_id_len, SILC_ID_CLIENT);
1358 client = silc_idlist_find_client_by_id(server->local_list, id, TRUE, NULL);
1360 SILC_LOG_ERROR(("Cannot process command reply to unknown client"));
1366 if (packet->dst_id_type == SILC_ID_SERVER) {
1367 /* For now this must be for us */
1368 if (memcmp(packet->dst_id, server->id_string, server->id_string_len)) {
1369 SILC_LOG_ERROR(("Cannot process command reply to unknown server"));
1374 /* Execute command reply locally for the command */
1375 silc_server_command_reply_process(server, sock, buffer);
1377 if (packet->dst_id_type == SILC_ID_CLIENT && client && id) {
1378 /* Relay the packet to the client */
1380 dst_sock = (SilcSocketConnection)client->connection;
1381 silc_buffer_push(buffer, SILC_PACKET_HEADER_LEN + packet->src_id_len
1382 + packet->dst_id_len + packet->padlen);
1384 silc_packet_send_prepare(dst_sock, 0, 0, buffer->len);
1385 silc_buffer_put(dst_sock->outbuf, buffer->data, buffer->len);
1387 idata = (SilcIDListData)client;
1389 /* Encrypt packet */
1390 silc_packet_encrypt(idata->send_key, idata->hmac_send, idata->psn_send++,
1391 dst_sock->outbuf, buffer->len);
1393 /* Send the packet */
1394 silc_server_packet_send_real(server, dst_sock, TRUE);
1400 /* Process received channel message. The message can be originated from
1401 client or server. */
1403 void silc_server_channel_message(SilcServer server,
1404 SilcSocketConnection sock,
1405 SilcPacketContext *packet)
1407 SilcChannelEntry channel = NULL;
1408 SilcChannelID *id = NULL;
1409 void *sender = NULL;
1410 void *sender_entry = NULL;
1413 SILC_LOG_DEBUG(("Processing channel message"));
1416 if (packet->dst_id_type != SILC_ID_CHANNEL) {
1417 SILC_LOG_DEBUG(("Received bad message for channel, dropped"));
1421 /* Find channel entry */
1422 id = silc_id_str2id(packet->dst_id, packet->dst_id_len, SILC_ID_CHANNEL);
1425 channel = silc_idlist_find_channel_by_id(server->local_list, id, NULL);
1427 channel = silc_idlist_find_channel_by_id(server->global_list, id, NULL);
1429 SILC_LOG_DEBUG(("Could not find channel"));
1434 /* See that this client is on the channel. If the original sender is
1435 not client (as it can be server as well) we don't do the check. */
1436 sender = silc_id_str2id(packet->src_id, packet->src_id_len,
1437 packet->src_id_type);
1440 if (packet->src_id_type == SILC_ID_CLIENT) {
1441 sender_entry = silc_idlist_find_client_by_id(server->local_list,
1442 sender, TRUE, NULL);
1443 if (!sender_entry) {
1445 sender_entry = silc_idlist_find_client_by_id(server->global_list,
1446 sender, TRUE, NULL);
1448 if (!sender_entry || !silc_server_client_on_channel(sender_entry,
1450 SILC_LOG_DEBUG(("Client not on channel"));
1454 /* If the packet is coming from router, but the client entry is
1455 local entry to us then some router is rerouting this to us and it is
1457 if (server->server_type == SILC_ROUTER &&
1458 sock->type == SILC_SOCKET_TYPE_ROUTER && local) {
1459 SILC_LOG_DEBUG(("Channel message rerouted to the sender, drop it"));
1464 /* Distribute the packet to our local clients. This will send the
1465 packet for further routing as well, if needed. */
1466 silc_server_packet_relay_to_channel(server, sock, channel, sender,
1467 packet->src_id_type, sender_entry,
1468 packet->buffer->data,
1469 packet->buffer->len, FALSE);
1478 /* Received channel key packet. We distribute the key to all of our locally
1479 connected clients on the channel. */
1481 void silc_server_channel_key(SilcServer server,
1482 SilcSocketConnection sock,
1483 SilcPacketContext *packet)
1485 SilcBuffer buffer = packet->buffer;
1486 SilcChannelEntry channel;
1488 if (packet->src_id_type != SILC_ID_SERVER ||
1489 (server->server_type == SILC_ROUTER &&
1490 sock->type == SILC_SOCKET_TYPE_ROUTER))
1493 /* Save the channel key */
1494 channel = silc_server_save_channel_key(server, buffer, NULL);
1498 /* Distribute the key to everybody who is on the channel. If we are router
1499 we will also send it to locally connected servers. */
1500 silc_server_send_channel_key(server, sock, channel, FALSE);
1502 if (server->server_type != SILC_BACKUP_ROUTER) {
1503 /* Distribute to local cell backup routers. */
1504 silc_server_backup_send(server, (SilcServerEntry)sock->user_data,
1505 SILC_PACKET_CHANNEL_KEY, 0,
1506 buffer->data, buffer->len, FALSE, TRUE);
1510 /* Received New Client packet and processes it. Creates Client ID for the
1511 client. Client becomes registered after calling this functions. */
1513 SilcClientEntry silc_server_new_client(SilcServer server,
1514 SilcSocketConnection sock,
1515 SilcPacketContext *packet)
1517 SilcBuffer buffer = packet->buffer;
1518 SilcClientEntry client;
1519 SilcClientID *client_id;
1521 SilcIDListData idata;
1522 char *username = NULL, *realname = NULL, *id_string;
1523 uint16 username_len;
1526 char *hostname, *nickname;
1529 SILC_LOG_DEBUG(("Creating new client"));
1531 if (sock->type != SILC_SOCKET_TYPE_CLIENT)
1534 /* Take client entry */
1535 client = (SilcClientEntry)sock->user_data;
1536 idata = (SilcIDListData)client;
1538 /* Remove the old cache entry. */
1539 if (!silc_idcache_del_by_context(server->local_list->clients, client)) {
1540 SILC_LOG_ERROR(("Lost client's cache entry - bad thing"));
1541 silc_server_disconnect_remote(server, sock, "Server closed connection: "
1546 /* Parse incoming packet */
1547 ret = silc_buffer_unformat(buffer,
1548 SILC_STR_UI16_NSTRING_ALLOC(&username,
1550 SILC_STR_UI16_STRING_ALLOC(&realname),
1553 silc_free(username);
1554 silc_free(realname);
1555 silc_server_disconnect_remote(server, sock, "Server closed connection: "
1556 "Incomplete client information");
1561 silc_free(username);
1562 silc_free(realname);
1563 silc_server_disconnect_remote(server, sock, "Server closed connection: "
1564 "Incomplete client information");
1568 if (username_len > 128)
1569 username[128] = '\0';
1571 /* Check for bad characters for nickname, and modify the nickname if
1572 it includes those. */
1573 if (silc_server_name_bad_chars(username, username_len)) {
1574 nickname = silc_server_name_modify_bad(username, username_len);
1576 nickname = strdup(username);
1579 /* Make sanity checks for the hostname of the client. If the hostname
1580 is provided in the `username' check that it is the same than the
1581 resolved hostname, or if not resolved the hostname that appears in
1582 the client's public key. If the hostname is not present then put
1583 it from the resolved name or from the public key. */
1584 if (strchr(username, '@')) {
1585 SilcPublicKeyIdentifier pident;
1586 int tlen = strcspn(username, "@");
1587 char *phostname = NULL;
1589 hostname = silc_calloc((strlen(username) - tlen) + 1, sizeof(char));
1590 memcpy(hostname, username + tlen + 1, strlen(username) - tlen - 1);
1592 if (strcmp(sock->hostname, sock->ip) &&
1593 strcmp(sock->hostname, hostname)) {
1594 silc_free(username);
1595 silc_free(hostname);
1596 silc_free(realname);
1597 silc_server_disconnect_remote(server, sock,
1598 "Server closed connection: "
1599 "Incomplete client information");
1603 pident = silc_pkcs_decode_identifier(client->data.public_key->identifier);
1605 phostname = strdup(pident->host);
1606 silc_pkcs_free_identifier(pident);
1609 if (!strcmp(sock->hostname, sock->ip) &&
1610 phostname && strcmp(phostname, hostname)) {
1611 silc_free(username);
1612 silc_free(hostname);
1613 silc_free(phostname);
1614 silc_free(realname);
1615 silc_server_disconnect_remote(server, sock,
1616 "Server closed connection: "
1617 "Incomplete client information");
1621 silc_free(phostname);
1623 /* The hostname is not present, add it. */
1625 /* XXX For now we cannot take the host name from the public key since
1626 they are not trusted or we cannot verify them as trusted. Just take
1627 what the resolved name or address is. */
1629 if (strcmp(sock->hostname, sock->ip)) {
1631 newusername = silc_calloc(strlen(username) +
1632 strlen(sock->hostname) + 2,
1633 sizeof(*newusername));
1634 strncat(newusername, username, strlen(username));
1635 strncat(newusername, "@", 1);
1636 strncat(newusername, sock->hostname, strlen(sock->hostname));
1637 silc_free(username);
1638 username = newusername;
1641 SilcPublicKeyIdentifier pident =
1642 silc_pkcs_decode_identifier(client->data.public_key->identifier);
1645 newusername = silc_calloc(strlen(username) +
1646 strlen(pident->host) + 2,
1647 sizeof(*newusername));
1648 strncat(newusername, username, strlen(username));
1649 strncat(newusername, "@", 1);
1650 strncat(newusername, pident->host, strlen(pident->host));
1651 silc_free(username);
1652 username = newusername;
1653 silc_pkcs_free_identifier(pident);
1659 /* Create Client ID */
1660 while (!silc_id_create_client_id(server, server->id, server->rng,
1661 server->md5hash, nickname, &client_id)) {
1663 snprintf(&nickname[strlen(nickname) - 1], 1, "%d", nickfail);
1666 /* Update client entry */
1667 idata->status |= SILC_IDLIST_STATUS_REGISTERED;
1668 client->nickname = nickname;
1669 client->username = username;
1670 client->userinfo = realname ? realname : strdup(" ");
1671 client->id = client_id;
1672 id_len = silc_id_get_len(client_id, SILC_ID_CLIENT);
1674 /* Add the client again to the ID cache */
1675 silc_idcache_add(server->local_list->clients, client->nickname,
1676 client_id, client, 0, NULL);
1678 /* Notify our router about new client on the SILC network */
1679 if (!server->standalone)
1680 silc_server_send_new_id(server, (SilcSocketConnection)
1681 server->router->connection,
1682 server->server_type == SILC_ROUTER ? TRUE : FALSE,
1683 client->id, SILC_ID_CLIENT, id_len);
1685 /* Send the new client ID to the client. */
1686 id_string = silc_id_id2str(client->id, SILC_ID_CLIENT);
1687 reply = silc_buffer_alloc(2 + 2 + id_len);
1688 silc_buffer_pull_tail(reply, SILC_BUFFER_END(reply));
1689 silc_buffer_format(reply,
1690 SILC_STR_UI_SHORT(SILC_ID_CLIENT),
1691 SILC_STR_UI_SHORT(id_len),
1692 SILC_STR_UI_XNSTRING(id_string, id_len),
1694 silc_server_packet_send(server, sock, SILC_PACKET_NEW_ID, 0,
1695 reply->data, reply->len, FALSE);
1696 silc_free(id_string);
1697 silc_buffer_free(reply);
1699 /* Send some nice info to the client */
1700 SILC_SERVER_SEND_NOTIFY(server, sock, SILC_NOTIFY_TYPE_NONE,
1701 ("Welcome to the SILC Network %s",
1703 SILC_SERVER_SEND_NOTIFY(server, sock, SILC_NOTIFY_TYPE_NONE,
1704 ("Your host is %s, running version %s",
1705 server->config->server_info->server_name,
1707 if (server->server_type == SILC_ROUTER) {
1708 SILC_SERVER_SEND_NOTIFY(server, sock, SILC_NOTIFY_TYPE_NONE,
1709 ("There are %d clients on %d servers in SILC "
1710 "Network", server->stat.clients,
1711 server->stat.servers + 1));
1712 SILC_SERVER_SEND_NOTIFY(server, sock, SILC_NOTIFY_TYPE_NONE,
1713 ("There are %d clients on %d server in our cell",
1714 server->stat.cell_clients,
1715 server->stat.cell_servers + 1));
1716 SILC_SERVER_SEND_NOTIFY(server, sock, SILC_NOTIFY_TYPE_NONE,
1717 ("I have %d clients, %d channels, %d servers and "
1719 server->stat.my_clients,
1720 server->stat.my_channels,
1721 server->stat.my_servers,
1722 server->stat.my_routers));
1723 SILC_SERVER_SEND_NOTIFY(server, sock, SILC_NOTIFY_TYPE_NONE,
1724 ("There are %d server operators and %d router "
1726 server->stat.server_ops,
1727 server->stat.router_ops));
1728 SILC_SERVER_SEND_NOTIFY(server, sock, SILC_NOTIFY_TYPE_NONE,
1729 ("I have %d operators online",
1730 server->stat.my_router_ops +
1731 server->stat.my_server_ops));
1733 SILC_SERVER_SEND_NOTIFY(server, sock, SILC_NOTIFY_TYPE_NONE,
1734 ("I have %d clients and %d channels formed",
1735 server->stat.my_clients,
1736 server->stat.my_channels));
1737 SILC_SERVER_SEND_NOTIFY(server, sock, SILC_NOTIFY_TYPE_NONE,
1738 ("%d operators online",
1739 server->stat.my_server_ops));
1741 SILC_SERVER_SEND_NOTIFY(server, sock, SILC_NOTIFY_TYPE_NONE,
1742 ("Your connection is secured with %s cipher, "
1743 "key length %d bits",
1744 idata->send_key->cipher->name,
1745 idata->send_key->cipher->key_len));
1746 SILC_SERVER_SEND_NOTIFY(server, sock, SILC_NOTIFY_TYPE_NONE,
1747 ("Your current nickname is %s",
1751 silc_server_send_motd(server, sock);
1756 /* Create new server. This processes received New Server packet and
1757 saves the received Server ID. The server is our locally connected
1758 server thus we save all the information and save it to local list.
1759 This funtion can be used by both normal server and router server.
1760 If normal server uses this it means that its router has connected
1761 to the server. If router uses this it means that one of the cell's
1762 servers is connected to the router. */
1764 SilcServerEntry silc_server_new_server(SilcServer server,
1765 SilcSocketConnection sock,
1766 SilcPacketContext *packet)
1768 SilcBuffer buffer = packet->buffer;
1769 SilcServerEntry new_server, server_entry;
1770 SilcServerID *server_id;
1771 SilcIDListData idata;
1772 unsigned char *server_name, *id_string;
1773 uint16 id_len, name_len;
1777 SILC_LOG_DEBUG(("Creating new server"));
1779 if (sock->type != SILC_SOCKET_TYPE_SERVER &&
1780 sock->type != SILC_SOCKET_TYPE_ROUTER)
1783 /* Take server entry */
1784 new_server = (SilcServerEntry)sock->user_data;
1785 idata = (SilcIDListData)new_server;
1787 /* Remove the old cache entry */
1788 if (!silc_idcache_del_by_context(server->local_list->servers, new_server)) {
1789 silc_idcache_del_by_context(server->global_list->servers, new_server);
1793 /* Parse the incoming packet */
1794 ret = silc_buffer_unformat(buffer,
1795 SILC_STR_UI16_NSTRING_ALLOC(&id_string, &id_len),
1796 SILC_STR_UI16_NSTRING_ALLOC(&server_name,
1801 silc_free(id_string);
1803 silc_free(server_name);
1807 if (id_len > buffer->len) {
1808 silc_free(id_string);
1809 silc_free(server_name);
1814 server_name[255] = '\0';
1817 server_id = silc_id_str2id(id_string, id_len, SILC_ID_SERVER);
1819 silc_free(id_string);
1820 silc_free(server_name);
1823 silc_free(id_string);
1825 /* Check that we do not have this ID already */
1826 server_entry = silc_idlist_find_server_by_id(server->local_list,
1827 server_id, TRUE, NULL);
1829 silc_idcache_del_by_context(server->local_list->servers, server_entry);
1831 server_entry = silc_idlist_find_server_by_id(server->global_list,
1832 server_id, TRUE, NULL);
1834 silc_idcache_del_by_context(server->global_list->servers, server_entry);
1837 /* Update server entry */
1838 idata->status |= SILC_IDLIST_STATUS_REGISTERED;
1839 new_server->server_name = server_name;
1840 new_server->id = server_id;
1842 SILC_LOG_DEBUG(("New server id(%s)",
1843 silc_id_render(server_id, SILC_ID_SERVER)));
1845 /* Add again the entry to the ID cache. */
1846 silc_idcache_add(local ? server->local_list->servers :
1847 server->global_list->servers, server_name, server_id,
1848 new_server, 0, NULL);
1850 /* Distribute the information about new server in the SILC network
1851 to our router. If we are normal server we won't send anything
1852 since this connection must be our router connection. */
1853 if (server->server_type == SILC_ROUTER && !server->standalone &&
1854 server->router->connection != sock)
1855 silc_server_send_new_id(server, server->router->connection,
1856 TRUE, new_server->id, SILC_ID_SERVER,
1857 silc_id_get_len(server_id, SILC_ID_SERVER));
1859 if (server->server_type == SILC_ROUTER)
1860 server->stat.cell_servers++;
1862 /* Check whether this router connection has been replaced by an
1863 backup router. If it has been then we'll disable the server and will
1864 ignore everything it will send until the backup router resuming
1865 protocol has been completed. */
1866 if (sock->type == SILC_SOCKET_TYPE_ROUTER &&
1867 silc_server_backup_replaced_get(server, server_id, NULL)) {
1868 /* Send packet to the server indicating that it cannot use this
1869 connection as it has been replaced by backup router. */
1870 SilcBuffer packet = silc_buffer_alloc(2);
1871 silc_buffer_pull_tail(packet, SILC_BUFFER_END(packet));
1872 silc_buffer_format(packet,
1873 SILC_STR_UI_CHAR(SILC_SERVER_BACKUP_REPLACED),
1874 SILC_STR_UI_CHAR(0),
1876 silc_server_packet_send(server, sock,
1877 SILC_PACKET_RESUME_ROUTER, 0,
1878 packet->data, packet->len, TRUE);
1879 silc_buffer_free(packet);
1881 /* Mark the router disabled. The data sent earlier will go but nothing
1882 after this does not go to this connection. */
1883 idata->status |= SILC_IDLIST_STATUS_DISABLED;
1885 /* If it is router announce our stuff to it. */
1886 if (sock->type == SILC_SOCKET_TYPE_ROUTER &&
1887 server->server_type == SILC_ROUTER) {
1888 silc_server_announce_servers(server, FALSE, 0, sock);
1889 silc_server_announce_clients(server, 0, sock);
1890 silc_server_announce_channels(server, 0, sock);
1897 /* Processes incoming New ID packet. New ID Payload is used to distribute
1898 information about newly registered clients and servers. */
1900 static void silc_server_new_id_real(SilcServer server,
1901 SilcSocketConnection sock,
1902 SilcPacketContext *packet,
1905 SilcBuffer buffer = packet->buffer;
1907 SilcServerEntry router, server_entry;
1908 SilcSocketConnection router_sock;
1913 SILC_LOG_DEBUG(("Processing new ID"));
1915 if (sock->type == SILC_SOCKET_TYPE_CLIENT ||
1916 server->server_type == SILC_SERVER ||
1917 packet->src_id_type != SILC_ID_SERVER)
1920 idp = silc_id_payload_parse(buffer->data, buffer->len);
1924 id_type = silc_id_payload_get_type(idp);
1926 /* Normal server cannot have other normal server connections */
1927 server_entry = (SilcServerEntry)sock->user_data;
1928 if (id_type == SILC_ID_SERVER && sock->type == SILC_SOCKET_TYPE_SERVER &&
1929 server_entry->server_type == SILC_SERVER)
1932 id = silc_id_payload_get_id(idp);
1936 /* If the packet is coming from server then use the sender as the
1937 origin of the the packet. If it came from router then check the real
1938 sender of the packet and use that as the origin. */
1939 if (sock->type == SILC_SOCKET_TYPE_SERVER) {
1940 id_list = server->local_list;
1942 router = sock->user_data;
1944 /* If the sender is backup router and ID is server (and we are not
1945 backup router) then switch the entry to global list. */
1946 if (server_entry->server_type == SILC_BACKUP_ROUTER &&
1947 id_type == SILC_ID_SERVER &&
1948 server->id_entry->server_type != SILC_BACKUP_ROUTER) {
1949 id_list = server->global_list;
1950 router_sock = server->router ? server->router->connection : sock;
1953 void *sender_id = silc_id_str2id(packet->src_id, packet->src_id_len,
1954 packet->src_id_type);
1955 router = silc_idlist_find_server_by_id(server->global_list,
1956 sender_id, TRUE, NULL);
1958 router = silc_idlist_find_server_by_id(server->local_list,
1959 sender_id, TRUE, NULL);
1960 silc_free(sender_id);
1962 id_list = server->global_list;
1969 case SILC_ID_CLIENT:
1971 SilcClientEntry entry;
1973 /* Check that we do not have this client already */
1974 entry = silc_idlist_find_client_by_id(server->global_list,
1975 id, server->server_type,
1978 entry = silc_idlist_find_client_by_id(server->local_list,
1979 id, server->server_type,
1982 SILC_LOG_DEBUG(("Ignoring client that we already have"));
1986 SILC_LOG_DEBUG(("New client id(%s) from [%s] %s",
1987 silc_id_render(id, SILC_ID_CLIENT),
1988 sock->type == SILC_SOCKET_TYPE_SERVER ?
1989 "Server" : "Router", sock->hostname));
1991 /* As a router we keep information of all global information in our
1992 global list. Cell wide information however is kept in the local
1994 entry = silc_idlist_add_client(id_list, NULL, NULL, NULL,
1995 id, router, NULL, 0);
1997 SILC_LOG_ERROR(("Could not add new client to the ID Cache"));
1999 /* Inform the sender that the ID is not usable */
2000 silc_server_send_notify_signoff(server, sock, FALSE, id, NULL);
2003 entry->nickname = NULL;
2004 entry->data.status |= SILC_IDLIST_STATUS_REGISTERED;
2006 if (sock->type == SILC_SOCKET_TYPE_SERVER)
2007 server->stat.cell_clients++;
2008 server->stat.clients++;
2012 case SILC_ID_SERVER:
2014 SilcServerEntry entry;
2016 /* If the ID is mine, ignore it. */
2017 if (SILC_ID_SERVER_COMPARE(id, server->id)) {
2018 SILC_LOG_DEBUG(("Ignoring my own ID as new ID"));
2022 /* If the ID is the sender's ID, ignore it (we have it already) */
2023 if (SILC_ID_SERVER_COMPARE(id, router->id)) {
2024 SILC_LOG_DEBUG(("Ignoring sender's own ID"));
2028 /* Check that we do not have this server already */
2029 entry = silc_idlist_find_server_by_id(server->global_list,
2030 id, server->server_type,
2033 entry = silc_idlist_find_server_by_id(server->local_list,
2034 id, server->server_type,
2037 SILC_LOG_DEBUG(("Ignoring server that we already have"));
2041 SILC_LOG_DEBUG(("New server id(%s) from [%s] %s",
2042 silc_id_render(id, SILC_ID_SERVER),
2043 sock->type == SILC_SOCKET_TYPE_SERVER ?
2044 "Server" : "Router", sock->hostname));
2046 /* As a router we keep information of all global information in our
2047 global list. Cell wide information however is kept in the local
2049 entry = silc_idlist_add_server(id_list, NULL, 0, id, router,
2052 SILC_LOG_ERROR(("Could not add new server to the ID Cache"));
2055 entry->data.status |= SILC_IDLIST_STATUS_REGISTERED;
2057 if (sock->type == SILC_SOCKET_TYPE_SERVER)
2058 server->stat.cell_servers++;
2059 server->stat.servers++;
2063 case SILC_ID_CHANNEL:
2064 SILC_LOG_ERROR(("Channel cannot be registered with NEW_ID packet"));
2073 /* If the sender of this packet is server and we are router we need to
2074 broadcast this packet to other routers in the network. */
2075 if (broadcast && !server->standalone && server->server_type == SILC_ROUTER &&
2076 sock->type == SILC_SOCKET_TYPE_SERVER &&
2077 !(packet->flags & SILC_PACKET_FLAG_BROADCAST)) {
2078 SILC_LOG_DEBUG(("Broadcasting received New ID packet"));
2079 silc_server_packet_send(server, server->router->connection,
2081 packet->flags | SILC_PACKET_FLAG_BROADCAST,
2082 buffer->data, buffer->len, FALSE);
2083 silc_server_backup_send(server, (SilcServerEntry)sock->user_data,
2084 packet->type, packet->flags,
2085 packet->buffer->data, packet->buffer->len,
2090 silc_id_payload_free(idp);
2094 /* Processes incoming New ID packet. New ID Payload is used to distribute
2095 information about newly registered clients and servers. */
2097 void silc_server_new_id(SilcServer server, SilcSocketConnection sock,
2098 SilcPacketContext *packet)
2100 silc_server_new_id_real(server, sock, packet, TRUE);
2103 /* Receoved New Id List packet, list of New ID payloads inside one
2104 packet. Process the New ID payloads one by one. */
2106 void silc_server_new_id_list(SilcServer server, SilcSocketConnection sock,
2107 SilcPacketContext *packet)
2109 SilcPacketContext *new_id;
2113 SILC_LOG_DEBUG(("Processing New ID List"));
2115 if (sock->type == SILC_SOCKET_TYPE_CLIENT ||
2116 packet->src_id_type != SILC_ID_SERVER)
2119 /* If the sender of this packet is server and we are router we need to
2120 broadcast this packet to other routers in the network. Broadcast
2121 this list packet instead of multiple New ID packets. */
2122 if (!server->standalone && server->server_type == SILC_ROUTER &&
2123 sock->type == SILC_SOCKET_TYPE_SERVER &&
2124 !(packet->flags & SILC_PACKET_FLAG_BROADCAST)) {
2125 SILC_LOG_DEBUG(("Broadcasting received New ID List packet"));
2126 silc_server_packet_send(server, server->router->connection,
2128 packet->flags | SILC_PACKET_FLAG_BROADCAST,
2129 packet->buffer->data, packet->buffer->len, FALSE);
2130 silc_server_backup_send(server, (SilcServerEntry)sock->user_data,
2131 packet->type, packet->flags,
2132 packet->buffer->data, packet->buffer->len,
2136 /* Make copy of the original packet context, except for the actual
2137 data buffer, which we will here now fetch from the original buffer. */
2138 new_id = silc_packet_context_alloc();
2139 new_id->type = SILC_PACKET_NEW_ID;
2140 new_id->flags = packet->flags;
2141 new_id->src_id = packet->src_id;
2142 new_id->src_id_len = packet->src_id_len;
2143 new_id->src_id_type = packet->src_id_type;
2144 new_id->dst_id = packet->dst_id;
2145 new_id->dst_id_len = packet->dst_id_len;
2146 new_id->dst_id_type = packet->dst_id_type;
2148 idp = silc_buffer_alloc(256);
2149 new_id->buffer = idp;
2151 while (packet->buffer->len) {
2152 SILC_GET16_MSB(id_len, packet->buffer->data + 2);
2153 if ((id_len > packet->buffer->len) ||
2154 (id_len > idp->truelen))
2157 silc_buffer_pull_tail(idp, 4 + id_len);
2158 silc_buffer_put(idp, packet->buffer->data, 4 + id_len);
2160 /* Process the New ID */
2161 silc_server_new_id_real(server, sock, new_id, FALSE);
2163 silc_buffer_push_tail(idp, 4 + id_len);
2164 silc_buffer_pull(packet->buffer, 4 + id_len);
2167 silc_buffer_free(idp);
2171 /* Received New Channel packet. Information about new channels in the
2172 network are distributed using this packet. Save the information about
2173 the new channel. This usually comes from router but also normal server
2174 can send this to notify channels it has when it connects to us. */
2176 void silc_server_new_channel(SilcServer server,
2177 SilcSocketConnection sock,
2178 SilcPacketContext *packet)
2180 SilcChannelPayload payload;
2181 SilcChannelID *channel_id;
2187 SilcServerEntry server_entry;
2188 SilcChannelEntry channel;
2190 SILC_LOG_DEBUG(("Processing New Channel"));
2192 if (sock->type == SILC_SOCKET_TYPE_CLIENT ||
2193 packet->src_id_type != SILC_ID_SERVER ||
2194 server->server_type == SILC_SERVER)
2197 /* Parse the channel payload */
2198 payload = silc_channel_payload_parse(packet->buffer->data,
2199 packet->buffer->len);
2203 /* Get the channel ID */
2204 channel_id = silc_channel_get_id_parse(payload);
2206 silc_channel_payload_free(payload);
2210 channel_name = silc_channel_get_name(payload, &name_len);
2212 channel_name[255] = '\0';
2214 id = silc_channel_get_id(payload, &id_len);
2216 server_entry = (SilcServerEntry)sock->user_data;
2218 if (sock->type == SILC_SOCKET_TYPE_ROUTER) {
2219 /* Add the channel to global list as it is coming from router. It
2220 cannot be our own channel as it is coming from router. */
2222 /* Check that we don't already have this channel */
2223 channel = silc_idlist_find_channel_by_name(server->local_list,
2224 channel_name, NULL);
2226 channel = silc_idlist_find_channel_by_name(server->global_list,
2227 channel_name, NULL);
2229 SILC_LOG_DEBUG(("New channel id(%s) from [Router] %s",
2230 silc_id_render(channel_id, SILC_ID_CHANNEL),
2233 silc_idlist_add_channel(server->global_list, strdup(channel_name),
2234 0, channel_id, sock->user_data, NULL, NULL, 0);
2235 server->stat.channels++;
2238 /* The channel is coming from our server, thus it is in our cell
2239 we will add it to our local list. */
2242 SILC_LOG_DEBUG(("Channel id(%s) from [Server] %s",
2243 silc_id_render(channel_id, SILC_ID_CHANNEL),
2246 /* Check that we don't already have this channel */
2247 channel = silc_idlist_find_channel_by_name(server->local_list,
2248 channel_name, NULL);
2250 channel = silc_idlist_find_channel_by_name(server->global_list,
2251 channel_name, NULL);
2253 /* If the channel does not exist, then create it. This creates a new
2254 key to the channel as well that we will send to the server. */
2256 /* The protocol says that the Channel ID's IP address must be based
2257 on the router's IP address. Check whether the ID is based in our
2258 IP and if it is not then create a new ID and enforce the server
2259 to switch the ID. */
2260 if (server_entry->server_type != SILC_BACKUP_ROUTER &&
2261 !SILC_ID_COMPARE(channel_id, server->id, server->id->ip.data_len)) {
2263 SILC_LOG_DEBUG(("Forcing the server to change Channel ID"));
2265 if (silc_id_create_channel_id(server, server->id, server->rng, &tmp)) {
2266 silc_server_send_notify_channel_change(server, sock, FALSE,
2268 silc_free(channel_id);
2273 /* Create the channel with the provided Channel ID */
2274 channel = silc_server_create_new_channel_with_id(server, NULL, NULL,
2278 silc_channel_payload_free(payload);
2279 silc_free(channel_id);
2283 /* Get the mode and set it to the channel */
2284 channel->mode = silc_channel_get_mode(payload);
2286 /* Send the new channel key to the server */
2287 id = silc_id_id2str(channel->id, SILC_ID_CHANNEL);
2288 id_len = silc_id_get_len(channel->id, SILC_ID_CHANNEL);
2289 chk = silc_channel_key_payload_encode(id_len, id,
2290 strlen(channel->channel_key->
2292 channel->channel_key->cipher->name,
2293 channel->key_len / 8,
2295 silc_server_packet_send(server, sock, SILC_PACKET_CHANNEL_KEY, 0,
2296 chk->data, chk->len, FALSE);
2297 silc_buffer_free(chk);
2300 /* The channel exist by that name, check whether the ID's match.
2301 If they don't then we'll force the server to use the ID we have.
2302 We also create a new key for the channel. */
2303 SilcBuffer users = NULL, users_modes = NULL;
2305 if (!SILC_ID_CHANNEL_COMPARE(channel_id, channel->id)) {
2306 /* They don't match, send CHANNEL_CHANGE notify to the server to
2307 force the ID change. */
2308 SILC_LOG_DEBUG(("Forcing the server to change Channel ID"));
2309 silc_server_send_notify_channel_change(server, sock, FALSE,
2310 channel_id, channel->id);
2313 /* If the mode is different from what we have then enforce the
2315 mode = silc_channel_get_mode(payload);
2316 if (channel->mode != mode) {
2317 SILC_LOG_DEBUG(("Forcing the server to change channel mode"));
2318 silc_server_send_notify_cmode(server, sock, FALSE, channel,
2319 channel->mode, server->id,
2321 channel->cipher, channel->hmac_name,
2322 channel->passphrase);
2325 /* Create new key for the channel and send it to the server and
2326 everybody else possibly on the channel. */
2328 if (!(channel->mode & SILC_CHANNEL_MODE_PRIVKEY)) {
2329 if (!silc_server_create_channel_key(server, channel, 0))
2332 /* Send to the channel */
2333 silc_server_send_channel_key(server, sock, channel, FALSE);
2334 id = silc_id_id2str(channel->id, SILC_ID_CHANNEL);
2335 id_len = silc_id_get_len(channel->id, SILC_ID_CHANNEL);
2337 /* Send to the server */
2338 chk = silc_channel_key_payload_encode(id_len, id,
2339 strlen(channel->channel_key->
2341 channel->channel_key->
2343 channel->key_len / 8,
2345 silc_server_packet_send(server, sock, SILC_PACKET_CHANNEL_KEY, 0,
2346 chk->data, chk->len, FALSE);
2347 silc_buffer_free(chk);
2351 silc_free(channel_id);
2353 /* Since the channel is coming from server and we also know about it
2354 then send the JOIN notify to the server so that it see's our
2355 users on the channel "joining" the channel. */
2356 silc_server_announce_get_channel_users(server, channel, &users,
2359 silc_buffer_push(users, users->data - users->head);
2360 silc_server_packet_send(server, sock,
2361 SILC_PACKET_NOTIFY, SILC_PACKET_FLAG_LIST,
2362 users->data, users->len, FALSE);
2363 silc_buffer_free(users);
2366 silc_buffer_push(users_modes, users_modes->data - users_modes->head);
2367 silc_server_packet_send_dest(server, sock,
2368 SILC_PACKET_NOTIFY, SILC_PACKET_FLAG_LIST,
2369 channel->id, SILC_ID_CHANNEL,
2371 users_modes->len, FALSE);
2372 silc_buffer_free(users_modes);
2377 silc_channel_payload_free(payload);
2380 /* Received New Channel List packet, list of New Channel List payloads inside
2381 one packet. Process the New Channel payloads one by one. */
2383 void silc_server_new_channel_list(SilcServer server,
2384 SilcSocketConnection sock,
2385 SilcPacketContext *packet)
2387 SilcPacketContext *new;
2391 SILC_LOG_DEBUG(("Processing New Channel List"));
2393 if (sock->type == SILC_SOCKET_TYPE_CLIENT ||
2394 packet->src_id_type != SILC_ID_SERVER ||
2395 server->server_type == SILC_SERVER)
2398 /* If the sender of this packet is server and we are router we need to
2399 broadcast this packet to other routers in the network. Broadcast
2400 this list packet instead of multiple New Channel packets. */
2401 if (!server->standalone && server->server_type == SILC_ROUTER &&
2402 sock->type == SILC_SOCKET_TYPE_SERVER &&
2403 !(packet->flags & SILC_PACKET_FLAG_BROADCAST)) {
2404 SILC_LOG_DEBUG(("Broadcasting received New Channel List packet"));
2405 silc_server_packet_send(server, server->router->connection,
2407 packet->flags | SILC_PACKET_FLAG_BROADCAST,
2408 packet->buffer->data, packet->buffer->len, FALSE);
2409 silc_server_backup_send(server, (SilcServerEntry)sock->user_data,
2410 packet->type, packet->flags,
2411 packet->buffer->data, packet->buffer->len,
2415 /* Make copy of the original packet context, except for the actual
2416 data buffer, which we will here now fetch from the original buffer. */
2417 new = silc_packet_context_alloc();
2418 new->type = SILC_PACKET_NEW_CHANNEL;
2419 new->flags = packet->flags;
2420 new->src_id = packet->src_id;
2421 new->src_id_len = packet->src_id_len;
2422 new->src_id_type = packet->src_id_type;
2423 new->dst_id = packet->dst_id;
2424 new->dst_id_len = packet->dst_id_len;
2425 new->dst_id_type = packet->dst_id_type;
2427 buffer = silc_buffer_alloc(512);
2428 new->buffer = buffer;
2430 while (packet->buffer->len) {
2431 SILC_GET16_MSB(len1, packet->buffer->data);
2432 if ((len1 > packet->buffer->len) ||
2433 (len1 > buffer->truelen))
2436 SILC_GET16_MSB(len2, packet->buffer->data + 2 + len1);
2437 if ((len2 > packet->buffer->len) ||
2438 (len2 > buffer->truelen))
2441 silc_buffer_pull_tail(buffer, 8 + len1 + len2);
2442 silc_buffer_put(buffer, packet->buffer->data, 8 + len1 + len2);
2444 /* Process the New Channel */
2445 silc_server_new_channel(server, sock, new);
2447 silc_buffer_push_tail(buffer, 8 + len1 + len2);
2448 silc_buffer_pull(packet->buffer, 8 + len1 + len2);
2451 silc_buffer_free(buffer);
2455 /* Received key agreement packet. This packet is never for us. It is to
2456 the client in the packet's destination ID. Sending of this sort of packet
2457 equals sending private message, ie. it is sent point to point from
2458 one client to another. */
2460 void silc_server_key_agreement(SilcServer server,
2461 SilcSocketConnection sock,
2462 SilcPacketContext *packet)
2464 SilcSocketConnection dst_sock;
2465 SilcIDListData idata;
2467 SILC_LOG_DEBUG(("Start"));
2469 if (packet->src_id_type != SILC_ID_CLIENT ||
2470 packet->dst_id_type != SILC_ID_CLIENT)
2473 if (!packet->dst_id)
2476 /* Get the route to the client */
2477 dst_sock = silc_server_get_client_route(server, packet->dst_id,
2478 packet->dst_id_len, NULL, &idata);
2482 /* Relay the packet */
2483 silc_server_relay_packet(server, dst_sock, idata->send_key,
2484 idata->hmac_send, idata->psn_send++,
2488 /* Received connection auth request packet that is used during connection
2489 phase to resolve the mandatory authentication method. This packet can
2490 actually be received at anytime but usually it is used only during
2491 the connection authentication phase. Now, protocol says that this packet
2492 can come from client or server, however, we support only this coming
2493 from client and expect that server always knows what authentication
2496 void silc_server_connection_auth_request(SilcServer server,
2497 SilcSocketConnection sock,
2498 SilcPacketContext *packet)
2500 SilcServerConfigSectionClientConnection *client = NULL;
2503 SilcAuthMethod auth_meth;
2505 SILC_LOG_DEBUG(("Start"));
2507 if (packet->src_id_type && packet->src_id_type != SILC_ID_CLIENT)
2510 /* Parse the payload */
2511 ret = silc_buffer_unformat(packet->buffer,
2512 SILC_STR_UI_SHORT(&conn_type),
2513 SILC_STR_UI_SHORT(NULL),
2518 if (conn_type != SILC_SOCKET_TYPE_CLIENT)
2521 /* Get the authentication method for the client */
2522 auth_meth = SILC_AUTH_NONE;
2523 port = server->sockets[server->sock]->port; /* Listenning port */
2524 client = silc_server_config_find_client_conn(server->config,
2528 client = silc_server_config_find_client_conn(server->config,
2532 auth_meth = client->auth_meth;
2534 /* Send it back to the client */
2535 silc_server_send_connection_auth_request(server, sock,
2540 /* Received REKEY packet. The sender of the packet wants to regenerate
2541 its session keys. This starts the REKEY protocol. */
2543 void silc_server_rekey(SilcServer server,
2544 SilcSocketConnection sock,
2545 SilcPacketContext *packet)
2547 SilcProtocol protocol;
2548 SilcServerRekeyInternalContext *proto_ctx;
2549 SilcIDListData idata = (SilcIDListData)sock->user_data;
2551 SILC_LOG_DEBUG(("Start"));
2553 /* Allocate internal protocol context. This is sent as context
2555 proto_ctx = silc_calloc(1, sizeof(*proto_ctx));
2556 proto_ctx->server = (void *)server;
2557 proto_ctx->sock = sock;
2558 proto_ctx->responder = TRUE;
2559 proto_ctx->pfs = idata->rekey->pfs;
2561 /* Perform rekey protocol. Will call the final callback after the
2562 protocol is over. */
2563 silc_protocol_alloc(SILC_PROTOCOL_SERVER_REKEY,
2564 &protocol, proto_ctx, silc_server_rekey_final);
2565 sock->protocol = protocol;
2567 if (proto_ctx->pfs == FALSE)
2568 /* Run the protocol */
2569 silc_protocol_execute(protocol, server->schedule, 0, 0);
2572 /* Received file transger packet. This packet is never for us. It is to
2573 the client in the packet's destination ID. Sending of this sort of packet
2574 equals sending private message, ie. it is sent point to point from
2575 one client to another. */
2577 void silc_server_ftp(SilcServer server,
2578 SilcSocketConnection sock,
2579 SilcPacketContext *packet)
2581 SilcSocketConnection dst_sock;
2582 SilcIDListData idata;
2584 SILC_LOG_DEBUG(("Start"));
2586 if (packet->src_id_type != SILC_ID_CLIENT ||
2587 packet->dst_id_type != SILC_ID_CLIENT)
2590 if (!packet->dst_id)
2593 /* Get the route to the client */
2594 dst_sock = silc_server_get_client_route(server, packet->dst_id,
2595 packet->dst_id_len, NULL, &idata);
2599 /* Relay the packet */
2600 silc_server_relay_packet(server, dst_sock, idata->send_key,
2601 idata->hmac_send, idata->psn_send++,