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, *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;
53 SILC_LOG_DEBUG(("Start"));
55 if (sock->type == SILC_SOCKET_TYPE_CLIENT ||
56 packet->src_id_type != SILC_ID_SERVER)
62 /* If the packet is destined directly to a client then relay the packet
63 before processing it. */
64 if (packet->dst_id_type == SILC_ID_CLIENT) {
66 SilcSocketConnection dst_sock;
68 /* Get the route to the client */
69 dst_sock = silc_server_get_client_route(server, packet->dst_id,
70 packet->dst_id_len, NULL, &idata);
72 /* Relay the packet */
73 silc_server_relay_packet(server, dst_sock, idata->send_key,
74 idata->hmac_receive, packet, TRUE);
77 /* If we are router and this packet is not already broadcast packet
78 we will broadcast it. The sending socket really cannot be router or
79 the router is buggy. If this packet is coming from router then it must
80 have the broadcast flag set already and we won't do anything. */
81 if (!server->standalone && server->server_type == SILC_ROUTER &&
82 sock->type == SILC_SOCKET_TYPE_SERVER &&
83 !(packet->flags & SILC_PACKET_FLAG_BROADCAST)) {
84 SILC_LOG_DEBUG(("Broadcasting received Notify packet"));
85 silc_server_packet_send(server, server->router->connection, packet->type,
86 packet->flags | SILC_PACKET_FLAG_BROADCAST,
87 packet->buffer->data, packet->buffer->len, FALSE);
90 payload = silc_notify_payload_parse(packet->buffer);
94 type = silc_notify_get_type(payload);
95 args = silc_notify_get_args(payload);
100 case SILC_NOTIFY_TYPE_JOIN:
102 * Distribute the notify to local clients on the channel
104 SILC_LOG_DEBUG(("JOIN notify"));
107 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
110 channel_id = silc_id_payload_parse_id(tmp, tmp_len);
114 /* Get channel entry */
115 channel = silc_idlist_find_channel_by_id(server->global_list,
118 channel = silc_idlist_find_channel_by_id(server->local_list,
121 silc_free(channel_id);
125 silc_free(channel_id);
128 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
131 client_id = silc_id_payload_parse_id(tmp, tmp_len);
135 /* Send to channel */
136 silc_server_packet_send_to_channel(server, sock, channel, packet->type,
137 FALSE, packet->buffer->data,
138 packet->buffer->len, FALSE);
140 /* If the the client is not in local list we check global list (ie. the
141 channel will be global channel) and if it does not exist then create
142 entry for the client. */
143 client = silc_idlist_find_client_by_id(server->global_list,
144 client_id, server->server_type,
147 client = silc_idlist_find_client_by_id(server->local_list,
148 client_id, server->server_type,
151 /* If router did not find the client the it is bogus */
152 if (server->server_type == SILC_ROUTER)
156 silc_idlist_add_client(server->global_list, NULL, NULL, NULL,
157 silc_id_dup(client_id, SILC_ID_CLIENT),
158 sock->user_data, NULL);
160 SILC_LOG_ERROR(("Could not add new client to the ID Cache"));
161 silc_free(client_id);
165 client->data.registered = TRUE;
169 /* Do not process the notify if the client is not registered */
170 if (client->data.registered == FALSE)
173 /* Do not add client to channel if it is there already */
174 if (silc_server_client_on_channel(client, channel))
177 if (server->server_type == SILC_SERVER &&
178 sock->type == SILC_SOCKET_TYPE_ROUTER)
179 /* The channel is global now */
180 channel->global_users = TRUE;
182 /* JOIN the global client to the channel (local clients (if router
183 created the channel) is joined in the pending JOIN command). */
184 chl = silc_calloc(1, sizeof(*chl));
185 chl->client = client;
186 chl->channel = channel;
187 silc_hash_table_add(channel->user_list, client, chl);
188 silc_hash_table_add(client->channels, channel, chl);
189 silc_free(client_id);
193 case SILC_NOTIFY_TYPE_LEAVE:
195 * Distribute the notify to local clients on the channel
197 SILC_LOG_DEBUG(("LEAVE notify"));
199 channel_id = silc_id_str2id(packet->dst_id, packet->dst_id_len,
200 packet->dst_id_type);
204 /* Get channel entry */
205 channel = silc_idlist_find_channel_by_id(server->global_list,
208 channel = silc_idlist_find_channel_by_id(server->local_list,
211 silc_free(channel_id);
217 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
219 silc_free(channel_id);
222 client_id = silc_id_payload_parse_id(tmp, tmp_len);
224 silc_free(channel_id);
228 /* Send to channel */
229 silc_server_packet_send_to_channel(server, sock, channel, packet->type,
230 FALSE, packet->buffer->data,
231 packet->buffer->len, FALSE);
233 /* Get client entry */
234 client = silc_idlist_find_client_by_id(server->global_list,
235 client_id, TRUE, NULL);
237 client = silc_idlist_find_client_by_id(server->local_list,
238 client_id, TRUE, NULL);
240 silc_free(client_id);
241 silc_free(channel_id);
245 silc_free(client_id);
247 /* Remove the user from channel */
248 silc_server_remove_from_one_channel(server, sock, channel, client, FALSE);
251 case SILC_NOTIFY_TYPE_SIGNOFF:
253 * Distribute the notify to local clients on the channel
255 SILC_LOG_DEBUG(("SIGNOFF notify"));
258 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
261 client_id = silc_id_payload_parse_id(tmp, tmp_len);
265 /* Get client entry */
266 client = silc_idlist_find_client_by_id(server->global_list,
267 client_id, TRUE, &cache);
269 client = silc_idlist_find_client_by_id(server->local_list,
270 client_id, TRUE, &cache);
272 silc_free(client_id);
276 silc_free(client_id);
278 /* Get signoff message */
279 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
283 /* Remove the client from all channels. */
284 silc_server_remove_from_channels(server, NULL, client, TRUE, tmp, FALSE);
286 client->data.registered = FALSE;
287 cache->expire = SILC_ID_CACHE_EXPIRE_DEF;
288 server->stat.clients--;
289 if (server->server_type == SILC_ROUTER)
290 server->stat.cell_clients--;
293 case SILC_NOTIFY_TYPE_TOPIC_SET:
295 * Distribute the notify to local clients on the channel
298 SILC_LOG_DEBUG(("TOPIC SET notify"));
300 channel_id = silc_id_str2id(packet->dst_id, packet->dst_id_len,
301 packet->dst_id_type);
305 /* Get channel entry */
306 channel = silc_idlist_find_channel_by_id(server->global_list,
309 channel = silc_idlist_find_channel_by_id(server->local_list,
312 silc_free(channel_id);
318 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
320 silc_free(channel_id);
325 silc_free(channel->topic);
326 channel->topic = silc_calloc(tmp_len + 1, sizeof(*channel->topic));
327 memcpy(channel->topic, tmp, tmp_len);
329 /* Send the same notify to the channel */
330 silc_server_packet_send_to_channel(server, sock, channel, packet->type,
331 FALSE, packet->buffer->data,
332 packet->buffer->len, FALSE);
333 silc_free(channel_id);
336 case SILC_NOTIFY_TYPE_NICK_CHANGE:
339 * Distribute the notify to local clients on the channel
341 unsigned char *id, *id2;
343 SILC_LOG_DEBUG(("NICK CHANGE notify"));
345 /* Get old client ID */
346 id = silc_argument_get_arg_type(args, 1, &tmp_len);
349 client_id = silc_id_payload_parse_id(id, tmp_len);
353 /* Get new client ID */
354 id2 = silc_argument_get_arg_type(args, 2, &tmp_len);
357 client_id2 = silc_id_payload_parse_id(id2, tmp_len);
361 SILC_LOG_DEBUG(("Old Client ID id(%s)",
362 silc_id_render(client_id, SILC_ID_CLIENT)));
363 SILC_LOG_DEBUG(("New Client ID id(%s)",
364 silc_id_render(client_id2, SILC_ID_CLIENT)));
366 /* Replace the Client ID */
367 client = silc_idlist_replace_client_id(server->global_list, client_id,
370 client = silc_idlist_replace_client_id(server->local_list, client_id,
374 /* The nickname is not valid anymore, set it NULL. This causes that
375 the nickname will be queried if someone wants to know it. */
376 if (client->nickname)
377 silc_free(client->nickname);
378 client->nickname = NULL;
380 /* Send the NICK_CHANGE notify type to local clients on the channels
381 this client is joined to. */
382 silc_server_send_notify_on_channels(server, NULL, client,
383 SILC_NOTIFY_TYPE_NICK_CHANGE, 2,
388 silc_free(client_id);
390 silc_free(client_id2);
394 case SILC_NOTIFY_TYPE_CMODE_CHANGE:
396 * Distribute the notify to local clients on the channel
399 SILC_LOG_DEBUG(("CMODE CHANGE notify"));
401 channel_id = silc_id_str2id(packet->dst_id, packet->dst_id_len,
402 packet->dst_id_type);
406 /* Get channel entry */
407 channel = silc_idlist_find_channel_by_id(server->global_list,
410 channel = silc_idlist_find_channel_by_id(server->local_list,
413 silc_free(channel_id);
418 /* Send the same notify to the channel */
419 silc_server_packet_send_to_channel(server, sock, channel, packet->type,
420 FALSE, packet->buffer->data,
421 packet->buffer->len, FALSE);
424 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
426 silc_free(channel_id);
430 SILC_GET32_MSB(mode, tmp);
432 /* If the channel had private keys set and the mode was removed then
433 we must re-generate and re-distribute a new channel key */
434 if (channel->mode & SILC_CHANNEL_MODE_PRIVKEY &&
435 !(mode & SILC_CHANNEL_MODE_PRIVKEY)) {
436 /* Re-generate channel key */
437 if (!silc_server_create_channel_key(server, channel, 0))
440 /* Send the channel key. This sends it to our local clients and if
441 we are normal server to our router as well. */
442 silc_server_send_channel_key(server, NULL, channel,
443 server->server_type == SILC_ROUTER ?
444 FALSE : !server->standalone);
448 channel->mode = mode;
449 silc_free(channel_id);
452 tmp = silc_argument_get_arg_type(args, 4, &tmp_len);
454 unsigned char hash[32];
457 silc_hmac_free(channel->hmac);
458 if (!silc_hmac_alloc(tmp, NULL, &channel->hmac))
461 /* Set the HMAC key out of current channel key. The client must do
463 silc_hash_make(channel->hmac->hash, channel->key, channel->key_len / 8,
465 silc_hmac_set_key(channel->hmac, hash,
466 silc_hash_len(channel->hmac->hash));
467 memset(hash, 0, sizeof(hash));
472 case SILC_NOTIFY_TYPE_CUMODE_CHANGE:
475 * Distribute the notify to local clients on the channel
477 SilcChannelClientEntry chl2 = NULL;
478 bool notify_sent = FALSE;
480 SILC_LOG_DEBUG(("CUMODE CHANGE notify"));
482 channel_id = silc_id_str2id(packet->dst_id, packet->dst_id_len,
483 packet->dst_id_type);
487 /* Get channel entry */
488 channel = silc_idlist_find_channel_by_id(server->global_list,
491 channel = silc_idlist_find_channel_by_id(server->local_list,
494 silc_free(channel_id);
500 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
502 silc_free(channel_id);
506 SILC_GET32_MSB(mode, tmp);
508 /* Get target client */
509 tmp = silc_argument_get_arg_type(args, 3, &tmp_len);
512 client_id = silc_id_payload_parse_id(tmp, tmp_len);
516 /* Get client entry */
517 client = silc_idlist_find_client_by_id(server->global_list,
518 client_id, TRUE, NULL);
520 client = silc_idlist_find_client_by_id(server->local_list,
521 client_id, TRUE, NULL);
523 silc_free(client_id);
527 silc_free(client_id);
529 /* Get entry to the channel user list */
530 silc_hash_table_list(channel->user_list, &htl);
531 while (silc_hash_table_get(&htl, NULL, (void *)&chl)) {
532 /* If the mode is channel founder and we already find a client
533 to have that mode on the channel we will enforce the sender
534 to change the channel founder mode away. There can be only one
535 channel founder on the channel. */
536 if (server->server_type == SILC_ROUTER &&
537 mode & SILC_CHANNEL_UMODE_CHANFO &&
538 chl->mode & SILC_CHANNEL_UMODE_CHANFO) {
540 unsigned char cumode[4];
542 mode &= ~SILC_CHANNEL_UMODE_CHANFO;
543 silc_server_send_notify_cumode(server, sock, FALSE, channel, mode,
544 client->id, SILC_ID_CLIENT,
547 idp = silc_id_payload_encode(client->id, SILC_ID_CLIENT);
548 SILC_PUT32_MSB(mode, cumode);
549 silc_server_send_notify_to_channel(server, sock, channel, FALSE,
550 SILC_NOTIFY_TYPE_CUMODE_CHANGE,
551 3, idp->data, idp->len,
553 idp->data, idp->len);
554 silc_buffer_free(idp);
557 /* Force the mode change if we alredy set the mode */
560 silc_free(channel_id);
565 if (chl->client == client) {
566 /* Change the mode */
568 if (!(mode & SILC_CHANNEL_UMODE_CHANFO))
575 /* Send the same notify to the channel */
577 silc_server_packet_send_to_channel(server, sock, channel,
579 FALSE, packet->buffer->data,
580 packet->buffer->len, FALSE);
582 silc_free(channel_id);
586 case SILC_NOTIFY_TYPE_INVITE:
588 if (packet->dst_id_type == SILC_ID_CLIENT)
591 SILC_LOG_DEBUG(("INVITE notify"));
594 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
597 channel_id = silc_id_payload_parse_id(tmp, tmp_len);
601 /* Get channel entry */
602 channel = silc_idlist_find_channel_by_id(server->global_list,
605 channel = silc_idlist_find_channel_by_id(server->local_list,
608 silc_free(channel_id);
612 silc_free(channel_id);
614 /* Get the added invite */
615 tmp = silc_argument_get_arg_type(args, 3, &tmp_len);
617 if (!channel->invite_list)
618 channel->invite_list = silc_calloc(tmp_len + 2,
619 sizeof(*channel->invite_list));
621 channel->invite_list = silc_realloc(channel->invite_list,
622 sizeof(*channel->invite_list) *
624 strlen(channel->invite_list) +
626 if (tmp[tmp_len - 1] == ',')
627 tmp[tmp_len - 1] = '\0';
629 strncat(channel->invite_list, tmp, tmp_len);
630 strncat(channel->invite_list, ",", 1);
633 /* Get the deleted invite */
634 tmp = silc_argument_get_arg_type(args, 4, &tmp_len);
635 if (tmp && channel->invite_list) {
636 char *start, *end, *n;
638 if (!strncmp(channel->invite_list, tmp,
639 strlen(channel->invite_list) - 1)) {
640 silc_free(channel->invite_list);
641 channel->invite_list = NULL;
643 start = strstr(channel->invite_list, tmp);
644 if (start && strlen(start) >= tmp_len) {
645 end = start + tmp_len;
646 n = silc_calloc(strlen(channel->invite_list) - tmp_len, sizeof(*n));
647 strncat(n, channel->invite_list, start - channel->invite_list);
648 strncat(n, end + 1, ((channel->invite_list +
649 strlen(channel->invite_list)) - end) - 1);
650 silc_free(channel->invite_list);
651 channel->invite_list = n;
658 case SILC_NOTIFY_TYPE_CHANNEL_CHANGE:
660 * Distribute to the local clients on the channel and change the
664 SILC_LOG_DEBUG(("CHANNEL CHANGE"));
666 if (sock->type != SILC_SOCKET_TYPE_ROUTER)
669 /* Get the old Channel ID */
670 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
673 channel_id = silc_id_payload_parse_id(tmp, tmp_len);
677 /* Get the channel entry */
678 channel = silc_idlist_find_channel_by_id(server->global_list,
681 channel = silc_idlist_find_channel_by_id(server->local_list,
684 silc_free(channel_id);
689 /* Send the notify to the channel */
690 silc_server_packet_send_to_channel(server, sock, channel, packet->type,
691 FALSE, packet->buffer->data,
692 packet->buffer->len, FALSE);
694 /* Get the new Channel ID */
695 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
698 channel_id2 = silc_id_payload_parse_id(tmp, tmp_len);
702 SILC_LOG_DEBUG(("Old Channel ID id(%s)",
703 silc_id_render(channel_id, SILC_ID_CHANNEL)));
704 SILC_LOG_DEBUG(("New Channel ID id(%s)",
705 silc_id_render(channel_id2, SILC_ID_CHANNEL)));
707 /* Replace the Channel ID */
708 if (!silc_idlist_replace_channel_id(server->global_list, channel_id,
710 if (!silc_idlist_replace_channel_id(server->local_list, channel_id,
712 silc_free(channel_id2);
717 SilcBuffer users = NULL, users_modes = NULL;
719 /* Re-announce our clients on the channel as the ID has changed now */
720 silc_server_announce_get_channel_users(server, channel, &users,
723 silc_buffer_push(users, users->data - users->head);
724 silc_server_packet_send(server, sock,
725 SILC_PACKET_NOTIFY, SILC_PACKET_FLAG_LIST,
726 users->data, users->len, FALSE);
727 silc_buffer_free(users);
730 silc_buffer_push(users_modes, users_modes->data - users_modes->head);
731 silc_server_packet_send_dest(server, sock,
732 SILC_PACKET_NOTIFY, SILC_PACKET_FLAG_LIST,
733 channel->id, SILC_ID_CHANNEL,
735 users_modes->len, FALSE);
736 silc_buffer_free(users_modes);
740 silc_free(channel_id);
744 case SILC_NOTIFY_TYPE_SERVER_SIGNOFF:
746 * Remove the server entry and all clients that this server owns.
749 SILC_LOG_DEBUG(("SERVER SIGNOFF notify"));
752 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
755 server_id = silc_id_payload_parse_id(tmp, tmp_len);
759 /* Get server entry */
760 server_entry = silc_idlist_find_server_by_id(server->global_list,
761 server_id, TRUE, NULL);
763 server_entry = silc_idlist_find_server_by_id(server->local_list,
764 server_id, TRUE, NULL);
766 silc_free(server_id);
770 silc_free(server_id);
772 /* Free all client entries that this server owns as they will
773 become invalid now as well. */
774 silc_server_remove_clients_by_server(server, server_entry, TRUE);
776 /* Remove the server entry */
777 if (!silc_idlist_del_server(server->global_list, server_entry))
778 silc_idlist_del_server(server->local_list, server_entry);
780 /* XXX update statistics */
784 case SILC_NOTIFY_TYPE_KICKED:
786 * Distribute the notify to local clients on the channel
789 SILC_LOG_DEBUG(("KICKED notify"));
791 channel_id = silc_id_str2id(packet->dst_id, packet->dst_id_len,
792 packet->dst_id_type);
796 /* Get channel entry */
797 channel = silc_idlist_find_channel_by_id(server->global_list,
800 channel = silc_idlist_find_channel_by_id(server->local_list,
803 silc_free(channel_id);
807 silc_free(channel_id);
810 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
813 client_id = silc_id_payload_parse_id(tmp, tmp_len);
817 /* Send to channel */
818 silc_server_packet_send_to_channel(server, sock, channel, packet->type,
819 FALSE, packet->buffer->data,
820 packet->buffer->len, FALSE);
822 /* If the the client is not in local list we check global list */
823 client = silc_idlist_find_client_by_id(server->global_list,
824 client_id, TRUE, NULL);
826 client = silc_idlist_find_client_by_id(server->local_list,
827 client_id, TRUE, NULL);
829 silc_free(client_id);
834 /* Remove the client from channel */
835 silc_server_remove_from_one_channel(server, sock, channel, client, FALSE);
839 case SILC_NOTIFY_TYPE_KILLED:
842 * Distribute the notify to local clients on channels
847 SILC_LOG_DEBUG(("KILLED notify"));
850 id = silc_argument_get_arg_type(args, 1, &id_len);
853 client_id = silc_id_payload_parse_id(id, id_len);
857 /* If the the client is not in local list we check global list */
858 client = silc_idlist_find_client_by_id(server->global_list,
859 client_id, TRUE, NULL);
861 client = silc_idlist_find_client_by_id(server->local_list,
862 client_id, TRUE, NULL);
864 silc_free(client_id);
868 silc_free(client_id);
870 /* If the client is one of ours, then close the connection to the
871 client now. This removes the client from all channels as well. */
872 if (packet->dst_id_type == SILC_ID_CLIENT && client->data.registered &&
873 client->connection) {
874 sock = client->connection;
875 silc_server_free_client_data(server, NULL, client, FALSE, NULL);
876 silc_server_close_connection(server, sock);
881 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
885 /* Send the notify to local clients on the channels except to the
886 client who is killed. */
887 silc_server_send_notify_on_channels(server, client, client,
888 SILC_NOTIFY_TYPE_KILLED,
893 /* Remove the client from all channels */
894 silc_server_remove_from_channels(server, NULL, client, FALSE, NULL,
900 case SILC_NOTIFY_TYPE_UMODE_CHANGE:
902 * Save the mode of the client.
905 SILC_LOG_DEBUG(("UMODE_CHANGE notify"));
908 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
911 client_id = silc_id_payload_parse_id(tmp, tmp_len);
915 /* Get client entry */
916 client = silc_idlist_find_client_by_id(server->global_list,
917 client_id, TRUE, NULL);
919 client = silc_idlist_find_client_by_id(server->local_list,
920 client_id, TRUE, NULL);
922 silc_free(client_id);
926 silc_free(client_id);
929 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
934 SILC_GET32_MSB(client->mode, tmp);
938 case SILC_NOTIFY_TYPE_BAN:
943 SILC_LOG_DEBUG(("BAN notify"));
946 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
949 channel_id = silc_id_payload_parse_id(tmp, tmp_len);
953 /* Get channel entry */
954 channel = silc_idlist_find_channel_by_id(server->global_list,
957 channel = silc_idlist_find_channel_by_id(server->local_list,
960 silc_free(channel_id);
964 silc_free(channel_id);
966 /* Get the new ban and add it to the ban list */
967 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
969 if (!channel->ban_list)
970 channel->ban_list = silc_calloc(tmp_len + 2,
971 sizeof(*channel->ban_list));
973 channel->ban_list = silc_realloc(channel->ban_list,
974 sizeof(*channel->ban_list) *
976 strlen(channel->ban_list) + 2));
977 strncat(channel->ban_list, tmp, tmp_len);
978 strncat(channel->ban_list, ",", 1);
981 /* Get the ban to be removed and remove it from the list */
982 tmp = silc_argument_get_arg_type(args, 3, &tmp_len);
983 if (tmp && channel->ban_list) {
984 char *start, *end, *n;
986 if (!strcmp(channel->ban_list, tmp)) {
987 silc_free(channel->ban_list);
988 channel->ban_list = NULL;
990 start = strstr(channel->ban_list, tmp);
991 if (start && strlen(start) >= tmp_len) {
992 end = start + tmp_len;
993 n = silc_calloc(strlen(channel->ban_list) - tmp_len, sizeof(*n));
994 strncat(n, channel->ban_list, start - channel->ban_list);
995 strncat(n, end + 1, ((channel->ban_list +
996 strlen(channel->ban_list)) - end) - 1);
997 silc_free(channel->ban_list);
998 channel->ban_list = n;
1005 /* Ignore rest of the notify types for now */
1006 case SILC_NOTIFY_TYPE_NONE:
1007 case SILC_NOTIFY_TYPE_MOTD:
1014 silc_notify_payload_free(payload);
1017 void silc_server_notify_list(SilcServer server,
1018 SilcSocketConnection sock,
1019 SilcPacketContext *packet)
1021 SilcPacketContext *new;
1025 SILC_LOG_DEBUG(("Processing Notify List"));
1027 if (sock->type == SILC_SOCKET_TYPE_CLIENT ||
1028 packet->src_id_type != SILC_ID_SERVER)
1031 /* Make copy of the original packet context, except for the actual
1032 data buffer, which we will here now fetch from the original buffer. */
1033 new = silc_packet_context_alloc();
1034 new->type = SILC_PACKET_NOTIFY;
1035 new->flags = packet->flags;
1036 new->src_id = packet->src_id;
1037 new->src_id_len = packet->src_id_len;
1038 new->src_id_type = packet->src_id_type;
1039 new->dst_id = packet->dst_id;
1040 new->dst_id_len = packet->dst_id_len;
1041 new->dst_id_type = packet->dst_id_type;
1043 buffer = silc_buffer_alloc(1024);
1044 new->buffer = buffer;
1046 while (packet->buffer->len) {
1047 SILC_GET16_MSB(len, packet->buffer->data + 2);
1048 if (len > packet->buffer->len)
1051 if (len > buffer->truelen) {
1052 silc_buffer_free(buffer);
1053 buffer = silc_buffer_alloc(1024 + len);
1056 silc_buffer_pull_tail(buffer, len);
1057 silc_buffer_put(buffer, packet->buffer->data, len);
1059 /* Process the Notify */
1060 silc_server_notify(server, sock, new);
1062 silc_buffer_push_tail(buffer, len);
1063 silc_buffer_pull(packet->buffer, len);
1066 silc_buffer_free(buffer);
1070 /* Received private message. This resolves the destination of the message
1071 and sends the packet. This is used by both server and router. If the
1072 destination is our locally connected client this sends the packet to
1073 the client. This may also send the message for further routing if
1074 the destination is not in our server (or router). */
1076 void silc_server_private_message(SilcServer server,
1077 SilcSocketConnection sock,
1078 SilcPacketContext *packet)
1080 SilcSocketConnection dst_sock;
1081 SilcIDListData idata;
1083 SILC_LOG_DEBUG(("Start"));
1085 if (packet->src_id_type != SILC_ID_CLIENT ||
1086 packet->dst_id_type != SILC_ID_CLIENT)
1089 if (!packet->dst_id)
1092 /* Get the route to the client */
1093 dst_sock = silc_server_get_client_route(server, packet->dst_id,
1094 packet->dst_id_len, NULL, &idata);
1098 /* Send the private message */
1099 silc_server_send_private_message(server, dst_sock, idata->send_key,
1100 idata->hmac_send, packet);
1103 /* Received private message key packet.. This packet is never for us. It is to
1104 the client in the packet's destination ID. Sending of this sort of packet
1105 equals sending private message, ie. it is sent point to point from
1106 one client to another. */
1108 void silc_server_private_message_key(SilcServer server,
1109 SilcSocketConnection sock,
1110 SilcPacketContext *packet)
1112 SilcSocketConnection dst_sock;
1113 SilcIDListData idata;
1115 SILC_LOG_DEBUG(("Start"));
1117 if (packet->src_id_type != SILC_ID_CLIENT ||
1118 packet->dst_id_type != SILC_ID_CLIENT)
1121 if (!packet->dst_id)
1124 /* Get the route to the client */
1125 dst_sock = silc_server_get_client_route(server, packet->dst_id,
1126 packet->dst_id_len, NULL, &idata);
1130 /* Relay the packet */
1131 silc_server_relay_packet(server, dst_sock, idata->send_key,
1132 idata->hmac_send, packet, FALSE);
1135 /* Processes incoming command reply packet. The command reply packet may
1136 be destined to one of our clients or it may directly for us. We will
1137 call the command reply routine after processing the packet. */
1139 void silc_server_command_reply(SilcServer server,
1140 SilcSocketConnection sock,
1141 SilcPacketContext *packet)
1143 SilcBuffer buffer = packet->buffer;
1144 SilcClientEntry client = NULL;
1145 SilcSocketConnection dst_sock;
1146 SilcIDListData idata;
1147 SilcClientID *id = NULL;
1149 SILC_LOG_DEBUG(("Start"));
1151 /* Source must be server or router */
1152 if (packet->src_id_type != SILC_ID_SERVER &&
1153 sock->type != SILC_SOCKET_TYPE_ROUTER)
1156 if (packet->dst_id_type == SILC_ID_CHANNEL)
1159 if (packet->dst_id_type == SILC_ID_CLIENT) {
1160 /* Destination must be one of ours */
1161 id = silc_id_str2id(packet->dst_id, packet->dst_id_len, SILC_ID_CLIENT);
1164 client = silc_idlist_find_client_by_id(server->local_list, id, TRUE, NULL);
1166 SILC_LOG_ERROR(("Cannot process command reply to unknown client"));
1172 if (packet->dst_id_type == SILC_ID_SERVER) {
1173 /* For now this must be for us */
1174 if (memcmp(packet->dst_id, server->id_string, packet->dst_id_len)) {
1175 SILC_LOG_ERROR(("Cannot process command reply to unknown server"));
1180 /* Execute command reply locally for the command */
1181 silc_server_command_reply_process(server, sock, buffer);
1183 if (packet->dst_id_type == SILC_ID_CLIENT && client && id) {
1184 /* Relay the packet to the client */
1186 dst_sock = (SilcSocketConnection)client->connection;
1187 silc_buffer_push(buffer, SILC_PACKET_HEADER_LEN + packet->src_id_len
1188 + packet->dst_id_len + packet->padlen);
1190 silc_packet_send_prepare(dst_sock, 0, 0, buffer->len);
1191 silc_buffer_put(dst_sock->outbuf, buffer->data, buffer->len);
1193 idata = (SilcIDListData)client;
1195 /* Encrypt packet */
1196 silc_packet_encrypt(idata->send_key, idata->hmac_send, dst_sock->outbuf,
1199 /* Send the packet */
1200 silc_server_packet_send_real(server, dst_sock, TRUE);
1206 /* Process received channel message. The message can be originated from
1207 client or server. */
1209 void silc_server_channel_message(SilcServer server,
1210 SilcSocketConnection sock,
1211 SilcPacketContext *packet)
1213 SilcChannelEntry channel = NULL;
1214 SilcChannelID *id = NULL;
1215 void *sender = NULL;
1216 void *sender_entry = NULL;
1218 SILC_LOG_DEBUG(("Processing channel message"));
1221 if (packet->dst_id_type != SILC_ID_CHANNEL) {
1222 SILC_LOG_DEBUG(("Received bad message for channel, dropped"));
1226 /* Find channel entry */
1227 id = silc_id_str2id(packet->dst_id, packet->dst_id_len, SILC_ID_CHANNEL);
1230 channel = silc_idlist_find_channel_by_id(server->local_list, id, NULL);
1232 channel = silc_idlist_find_channel_by_id(server->global_list, id, NULL);
1234 SILC_LOG_DEBUG(("Could not find channel"));
1239 /* See that this client is on the channel. If the original sender is
1240 not client (as it can be server as well) we don't do the check. */
1241 sender = silc_id_str2id(packet->src_id, packet->src_id_len,
1242 packet->src_id_type);
1245 if (packet->src_id_type == SILC_ID_CLIENT) {
1246 sender_entry = silc_idlist_find_client_by_id(server->local_list,
1247 sender, TRUE, NULL);
1249 sender_entry = silc_idlist_find_client_by_id(server->global_list,
1250 sender, TRUE, NULL);
1251 if (!sender_entry || !silc_server_client_on_channel(sender_entry,
1253 SILC_LOG_DEBUG(("Client not on channel"));
1258 /* Distribute the packet to our local clients. This will send the
1259 packet for further routing as well, if needed. */
1260 silc_server_packet_relay_to_channel(server, sock, channel, sender,
1261 packet->src_id_type, sender_entry,
1262 packet->buffer->data,
1263 packet->buffer->len, FALSE);
1272 /* Received channel key packet. We distribute the key to all of our locally
1273 connected clients on the channel. */
1275 void silc_server_channel_key(SilcServer server,
1276 SilcSocketConnection sock,
1277 SilcPacketContext *packet)
1279 SilcBuffer buffer = packet->buffer;
1280 SilcChannelEntry channel;
1282 if (packet->src_id_type != SILC_ID_SERVER ||
1283 (server->server_type == SILC_ROUTER &&
1284 sock->type == SILC_SOCKET_TYPE_ROUTER))
1287 /* Save the channel key */
1288 channel = silc_server_save_channel_key(server, buffer, NULL);
1292 /* Distribute the key to everybody who is on the channel. If we are router
1293 we will also send it to locally connected servers. */
1294 silc_server_send_channel_key(server, sock, channel, FALSE);
1297 /* Received New Client packet and processes it. Creates Client ID for the
1298 client. Client becomes registered after calling this functions. */
1300 SilcClientEntry silc_server_new_client(SilcServer server,
1301 SilcSocketConnection sock,
1302 SilcPacketContext *packet)
1304 SilcBuffer buffer = packet->buffer;
1305 SilcClientEntry client;
1306 SilcClientID *client_id;
1308 SilcIDListData idata;
1309 char *username = NULL, *realname = NULL, *id_string;
1312 char *hostname, *nickname;
1314 SILC_LOG_DEBUG(("Creating new client"));
1316 if (sock->type != SILC_SOCKET_TYPE_CLIENT)
1319 /* Take client entry */
1320 client = (SilcClientEntry)sock->user_data;
1321 idata = (SilcIDListData)client;
1323 /* Remove the old cache entry */
1324 if (!silc_idcache_del_by_context(server->local_list->clients, client)) {
1325 SILC_LOG_ERROR(("Lost client's cache entry - bad thing"));
1326 silc_server_disconnect_remote(server, sock, "Server closed connection: "
1331 /* Parse incoming packet */
1332 ret = silc_buffer_unformat(buffer,
1333 SILC_STR_UI16_STRING_ALLOC(&username),
1334 SILC_STR_UI16_STRING_ALLOC(&realname),
1338 silc_free(username);
1340 silc_free(realname);
1341 silc_server_disconnect_remote(server, sock, "Server closed connection: "
1342 "Incomplete client information");
1347 silc_free(username);
1349 silc_free(realname);
1350 silc_server_disconnect_remote(server, sock, "Server closed connection: "
1351 "Incomplete client information");
1355 if (strlen(username) > 128)
1356 username[127] = '\0';
1358 nickname = strdup(username);
1360 /* Make sanity checks for the hostname of the client. If the hostname
1361 is provided in the `username' check that it is the same than the
1362 resolved hostname, or if not resolved the hostname that appears in
1363 the client's public key. If the hostname is not present then put
1364 it from the resolved name or from the public key. */
1365 if (strchr(username, '@')) {
1366 SilcPublicKeyIdentifier pident;
1367 int tlen = strcspn(username, "@");
1368 char *phostname = NULL;
1370 hostname = silc_calloc((strlen(username) - tlen) + 1, sizeof(char));
1371 memcpy(hostname, username + tlen + 1, strlen(username) - tlen - 1);
1373 if (strcmp(sock->hostname, sock->ip) &&
1374 strcmp(sock->hostname, hostname)) {
1375 silc_free(username);
1376 silc_free(hostname);
1378 silc_free(realname);
1379 silc_server_disconnect_remote(server, sock,
1380 "Server closed connection: "
1381 "Incomplete client information");
1385 pident = silc_pkcs_decode_identifier(client->data.public_key->identifier);
1387 phostname = strdup(pident->host);
1388 silc_pkcs_free_identifier(pident);
1391 if (!strcmp(sock->hostname, sock->ip) &&
1392 phostname && strcmp(phostname, hostname)) {
1393 silc_free(username);
1394 silc_free(hostname);
1396 silc_free(phostname);
1398 silc_free(realname);
1399 silc_server_disconnect_remote(server, sock,
1400 "Server closed connection: "
1401 "Incomplete client information");
1406 silc_free(phostname);
1408 /* The hostname is not present, add it. */
1410 /* XXX For now we cannot take the host name from the public key since
1411 they are not trusted or we cannot verify them as trusted. Just take
1412 what the resolved name or address is. */
1414 if (strcmp(sock->hostname, sock->ip)) {
1416 newusername = silc_calloc(strlen(username) +
1417 strlen(sock->hostname) + 2,
1418 sizeof(*newusername));
1419 strncat(newusername, username, strlen(username));
1420 strncat(newusername, "@", 1);
1421 strncat(newusername, sock->hostname, strlen(sock->hostname));
1422 silc_free(username);
1423 username = newusername;
1426 SilcPublicKeyIdentifier pident =
1427 silc_pkcs_decode_identifier(client->data.public_key->identifier);
1430 newusername = silc_calloc(strlen(username) +
1431 strlen(pident->host) + 2,
1432 sizeof(*newusername));
1433 strncat(newusername, username, strlen(username));
1434 strncat(newusername, "@", 1);
1435 strncat(newusername, pident->host, strlen(pident->host));
1436 silc_free(username);
1437 username = newusername;
1438 silc_pkcs_free_identifier(pident);
1444 /* Create Client ID */
1445 silc_id_create_client_id(server->id, server->rng, server->md5hash,
1446 nickname, &client_id);
1448 /* Update client entry */
1449 idata->registered = TRUE;
1450 client->nickname = nickname;
1451 client->username = username;
1452 client->userinfo = realname ? realname : strdup(" ");
1453 client->id = client_id;
1454 id_len = silc_id_get_len(client_id, SILC_ID_CLIENT);
1456 /* Add the client again to the ID cache */
1457 silc_idcache_add(server->local_list->clients, client->nickname,
1458 client_id, client, FALSE);
1460 /* Notify our router about new client on the SILC network */
1461 if (!server->standalone)
1462 silc_server_send_new_id(server, (SilcSocketConnection)
1463 server->router->connection,
1464 server->server_type == SILC_ROUTER ? TRUE : FALSE,
1465 client->id, SILC_ID_CLIENT, id_len);
1467 /* Send the new client ID to the client. */
1468 id_string = silc_id_id2str(client->id, SILC_ID_CLIENT);
1469 reply = silc_buffer_alloc(2 + 2 + id_len);
1470 silc_buffer_pull_tail(reply, SILC_BUFFER_END(reply));
1471 silc_buffer_format(reply,
1472 SILC_STR_UI_SHORT(SILC_ID_CLIENT),
1473 SILC_STR_UI_SHORT(id_len),
1474 SILC_STR_UI_XNSTRING(id_string, id_len),
1476 silc_server_packet_send(server, sock, SILC_PACKET_NEW_ID, 0,
1477 reply->data, reply->len, FALSE);
1478 silc_free(id_string);
1479 silc_buffer_free(reply);
1481 /* Send some nice info to the client */
1482 SILC_SERVER_SEND_NOTIFY(server, sock, SILC_NOTIFY_TYPE_NONE,
1483 ("Welcome to the SILC Network %s",
1485 SILC_SERVER_SEND_NOTIFY(server, sock, SILC_NOTIFY_TYPE_NONE,
1486 ("Your host is %s, running version %s",
1487 server->config->server_info->server_name,
1489 if (server->server_type == SILC_ROUTER) {
1490 SILC_SERVER_SEND_NOTIFY(server, sock, SILC_NOTIFY_TYPE_NONE,
1491 ("There are %d clients on %d servers in SILC "
1492 "Network", server->stat.clients,
1493 server->stat.servers + 1));
1494 SILC_SERVER_SEND_NOTIFY(server, sock, SILC_NOTIFY_TYPE_NONE,
1495 ("There are %d clients on %d server in our cell",
1496 server->stat.cell_clients,
1497 server->stat.cell_servers + 1));
1498 SILC_SERVER_SEND_NOTIFY(server, sock, SILC_NOTIFY_TYPE_NONE,
1499 ("I have %d clients, %d channels, %d servers and "
1501 server->stat.my_clients,
1502 server->stat.my_channels,
1503 server->stat.my_servers,
1504 server->stat.my_routers));
1505 SILC_SERVER_SEND_NOTIFY(server, sock, SILC_NOTIFY_TYPE_NONE,
1506 ("%d server operators and %d router operators "
1508 server->stat.my_server_ops,
1509 server->stat.my_router_ops));
1511 SILC_SERVER_SEND_NOTIFY(server, sock, SILC_NOTIFY_TYPE_NONE,
1512 ("I have %d clients and %d channels formed",
1513 server->stat.my_clients,
1514 server->stat.my_channels));
1515 SILC_SERVER_SEND_NOTIFY(server, sock, SILC_NOTIFY_TYPE_NONE,
1516 ("%d operators online",
1517 server->stat.my_server_ops));
1519 SILC_SERVER_SEND_NOTIFY(server, sock, SILC_NOTIFY_TYPE_NONE,
1520 ("Your connection is secured with %s cipher, "
1521 "key length %d bits",
1522 idata->send_key->cipher->name,
1523 idata->send_key->cipher->key_len));
1524 SILC_SERVER_SEND_NOTIFY(server, sock, SILC_NOTIFY_TYPE_NONE,
1525 ("Your current nickname is %s",
1529 silc_server_send_motd(server, sock);
1534 /* Create new server. This processes received New Server packet and
1535 saves the received Server ID. The server is our locally connected
1536 server thus we save all the information and save it to local list.
1537 This funtion can be used by both normal server and router server.
1538 If normal server uses this it means that its router has connected
1539 to the server. If router uses this it means that one of the cell's
1540 servers is connected to the router. */
1542 SilcServerEntry silc_server_new_server(SilcServer server,
1543 SilcSocketConnection sock,
1544 SilcPacketContext *packet)
1546 SilcBuffer buffer = packet->buffer;
1547 SilcServerEntry new_server;
1548 SilcServerID *server_id;
1549 SilcIDListData idata;
1550 unsigned char *server_name, *id_string;
1551 uint16 id_len, name_len;
1554 SILC_LOG_DEBUG(("Creating new server"));
1556 if (sock->type != SILC_SOCKET_TYPE_SERVER &&
1557 sock->type != SILC_SOCKET_TYPE_ROUTER)
1560 /* Take server entry */
1561 new_server = (SilcServerEntry)sock->user_data;
1562 idata = (SilcIDListData)new_server;
1564 /* Remove the old cache entry */
1565 silc_idcache_del_by_context(server->local_list->servers, new_server);
1567 /* Parse the incoming packet */
1568 ret = silc_buffer_unformat(buffer,
1569 SILC_STR_UI16_NSTRING_ALLOC(&id_string, &id_len),
1570 SILC_STR_UI16_NSTRING_ALLOC(&server_name,
1575 silc_free(id_string);
1577 silc_free(server_name);
1581 if (id_len > buffer->len) {
1582 silc_free(id_string);
1583 silc_free(server_name);
1588 server_name[255] = '\0';
1591 server_id = silc_id_str2id(id_string, id_len, SILC_ID_SERVER);
1593 silc_free(id_string);
1594 silc_free(server_name);
1597 silc_free(id_string);
1599 /* Update server entry */
1600 idata->registered = TRUE;
1601 new_server->server_name = server_name;
1602 new_server->id = server_id;
1604 /* Add again the entry to the ID cache. */
1605 silc_idcache_add(server->local_list->servers, server_name, server_id,
1608 /* Distribute the information about new server in the SILC network
1609 to our router. If we are normal server we won't send anything
1610 since this connection must be our router connection. */
1611 if (server->server_type == SILC_ROUTER && !server->standalone &&
1612 server->router->connection != sock)
1613 silc_server_send_new_id(server, server->router->connection,
1614 TRUE, new_server->id, SILC_ID_SERVER,
1615 silc_id_get_len(server_id, SILC_ID_SERVER));
1617 if (server->server_type == SILC_ROUTER)
1618 server->stat.cell_servers++;
1623 /* Processes incoming New ID packet. New ID Payload is used to distribute
1624 information about newly registered clients and servers. */
1626 static void silc_server_new_id_real(SilcServer server,
1627 SilcSocketConnection sock,
1628 SilcPacketContext *packet,
1631 SilcBuffer buffer = packet->buffer;
1633 SilcServerEntry router;
1634 SilcSocketConnection router_sock;
1639 SILC_LOG_DEBUG(("Processing new ID"));
1641 if (sock->type == SILC_SOCKET_TYPE_CLIENT ||
1642 server->server_type == SILC_SERVER ||
1643 packet->src_id_type != SILC_ID_SERVER)
1646 idp = silc_id_payload_parse(buffer);
1650 id_type = silc_id_payload_get_type(idp);
1652 /* Normal server cannot have other normal server connections */
1653 if (id_type == SILC_ID_SERVER && sock->type == SILC_SOCKET_TYPE_SERVER)
1656 id = silc_id_payload_get_id(idp);
1660 /* If the sender of this packet is server and we are router we need to
1661 broadcast this packet to other routers in the network. */
1662 if (broadcast && !server->standalone && server->server_type == SILC_ROUTER &&
1663 sock->type == SILC_SOCKET_TYPE_SERVER &&
1664 !(packet->flags & SILC_PACKET_FLAG_BROADCAST)) {
1665 SILC_LOG_DEBUG(("Broadcasting received New ID packet"));
1666 silc_server_packet_send(server, server->router->connection,
1668 packet->flags | SILC_PACKET_FLAG_BROADCAST,
1669 buffer->data, buffer->len, FALSE);
1672 if (sock->type == SILC_SOCKET_TYPE_SERVER)
1673 id_list = server->local_list;
1675 id_list = server->global_list;
1677 /* If the packet is coming from server then use the sender as the
1678 origin of the the packet. If it came from router then check the real
1679 sender of the packet and use that as the origin. */
1680 if (sock->type == SILC_SOCKET_TYPE_SERVER) {
1682 router = sock->user_data;
1684 void *sender_id = silc_id_str2id(packet->src_id, packet->src_id_len,
1685 packet->src_id_type);
1686 router = silc_idlist_find_server_by_id(server->global_list,
1687 sender_id, TRUE, NULL);
1689 router = silc_idlist_find_server_by_id(server->local_list,
1690 sender_id, TRUE, NULL);
1691 silc_free(sender_id);
1698 case SILC_ID_CLIENT:
1700 SilcClientEntry entry;
1702 SILC_LOG_DEBUG(("New client id(%s) from [%s] %s",
1703 silc_id_render(id, SILC_ID_CLIENT),
1704 sock->type == SILC_SOCKET_TYPE_SERVER ?
1705 "Server" : "Router", sock->hostname));
1707 /* As a router we keep information of all global information in our
1708 global list. Cell wide information however is kept in the local
1710 entry = silc_idlist_add_client(id_list, NULL, NULL, NULL,
1713 SILC_LOG_ERROR(("Could not add new client to the ID Cache"));
1716 entry->nickname = NULL;
1717 entry->data.registered = TRUE;
1719 if (sock->type == SILC_SOCKET_TYPE_SERVER)
1720 server->stat.cell_clients++;
1721 server->stat.clients++;
1725 case SILC_ID_SERVER:
1727 SilcServerEntry entry;
1729 /* If the ID is mine, ignore it. */
1730 if (SILC_ID_SERVER_COMPARE(id, server->id)) {
1731 SILC_LOG_DEBUG(("Ignoring my own ID as new ID"));
1735 SILC_LOG_DEBUG(("New server id(%s) from [%s] %s",
1736 silc_id_render(id, SILC_ID_SERVER),
1737 sock->type == SILC_SOCKET_TYPE_SERVER ?
1738 "Server" : "Router", sock->hostname));
1740 /* As a router we keep information of all global information in our
1741 global list. Cell wide information however is kept in the local
1743 entry = silc_idlist_add_server(id_list, NULL, 0, id, router,
1746 SILC_LOG_ERROR(("Could not add new server to the ID Cache"));
1749 entry->data.registered = TRUE;
1751 if (sock->type == SILC_SOCKET_TYPE_SERVER)
1752 server->stat.cell_servers++;
1753 server->stat.servers++;
1757 case SILC_ID_CHANNEL:
1758 SILC_LOG_ERROR(("Channel cannot be registered with NEW_ID packet"));
1766 silc_id_payload_free(idp);
1770 /* Processes incoming New ID packet. New ID Payload is used to distribute
1771 information about newly registered clients and servers. */
1773 void silc_server_new_id(SilcServer server, SilcSocketConnection sock,
1774 SilcPacketContext *packet)
1776 silc_server_new_id_real(server, sock, packet, TRUE);
1779 /* Receoved New Id List packet, list of New ID payloads inside one
1780 packet. Process the New ID payloads one by one. */
1782 void silc_server_new_id_list(SilcServer server, SilcSocketConnection sock,
1783 SilcPacketContext *packet)
1785 SilcPacketContext *new_id;
1789 SILC_LOG_DEBUG(("Processing New ID List"));
1791 if (sock->type == SILC_SOCKET_TYPE_CLIENT ||
1792 packet->src_id_type != SILC_ID_SERVER)
1795 /* If the sender of this packet is server and we are router we need to
1796 broadcast this packet to other routers in the network. Broadcast
1797 this list packet instead of multiple New ID packets. */
1798 if (!server->standalone && server->server_type == SILC_ROUTER &&
1799 sock->type == SILC_SOCKET_TYPE_SERVER &&
1800 !(packet->flags & SILC_PACKET_FLAG_BROADCAST)) {
1801 SILC_LOG_DEBUG(("Broadcasting received New ID List packet"));
1802 silc_server_packet_send(server, server->router->connection,
1804 packet->flags | SILC_PACKET_FLAG_BROADCAST,
1805 packet->buffer->data, packet->buffer->len, FALSE);
1808 /* Make copy of the original packet context, except for the actual
1809 data buffer, which we will here now fetch from the original buffer. */
1810 new_id = silc_packet_context_alloc();
1811 new_id->type = SILC_PACKET_NEW_ID;
1812 new_id->flags = packet->flags;
1813 new_id->src_id = packet->src_id;
1814 new_id->src_id_len = packet->src_id_len;
1815 new_id->src_id_type = packet->src_id_type;
1816 new_id->dst_id = packet->dst_id;
1817 new_id->dst_id_len = packet->dst_id_len;
1818 new_id->dst_id_type = packet->dst_id_type;
1820 idp = silc_buffer_alloc(256);
1821 new_id->buffer = idp;
1823 while (packet->buffer->len) {
1824 SILC_GET16_MSB(id_len, packet->buffer->data + 2);
1825 if ((id_len > packet->buffer->len) ||
1826 (id_len > idp->truelen))
1829 silc_buffer_pull_tail(idp, 4 + id_len);
1830 silc_buffer_put(idp, packet->buffer->data, 4 + id_len);
1832 /* Process the New ID */
1833 silc_server_new_id_real(server, sock, new_id, FALSE);
1835 silc_buffer_push_tail(idp, 4 + id_len);
1836 silc_buffer_pull(packet->buffer, 4 + id_len);
1839 silc_buffer_free(idp);
1843 /* Received New Channel packet. Information about new channels in the
1844 network are distributed using this packet. Save the information about
1845 the new channel. This usually comes from router but also normal server
1846 can send this to notify channels it has when it connects to us. */
1848 void silc_server_new_channel(SilcServer server,
1849 SilcSocketConnection sock,
1850 SilcPacketContext *packet)
1852 SilcChannelPayload payload;
1853 SilcChannelID *channel_id;
1860 SILC_LOG_DEBUG(("Processing New Channel"));
1862 if (sock->type == SILC_SOCKET_TYPE_CLIENT ||
1863 packet->src_id_type != SILC_ID_SERVER ||
1864 server->server_type == SILC_SERVER)
1867 /* Parse the channel payload */
1868 payload = silc_channel_payload_parse(packet->buffer);
1872 /* Get the channel ID */
1873 channel_id = silc_channel_get_id_parse(payload);
1875 silc_channel_payload_free(payload);
1879 channel_name = silc_channel_get_name(payload, &name_len);
1881 channel_name[255] = '\0';
1883 id = silc_channel_get_id(payload, &id_len);
1885 if (sock->type == SILC_SOCKET_TYPE_ROUTER) {
1886 /* Add the channel to global list as it is coming from router. It
1887 cannot be our own channel as it is coming from router. */
1889 SILC_LOG_DEBUG(("New channel id(%s) from [Router] %s",
1890 silc_id_render(channel_id, SILC_ID_CHANNEL),
1893 silc_idlist_add_channel(server->global_list, strdup(channel_name),
1894 0, channel_id, sock->user_data, NULL, NULL);
1896 server->stat.channels++;
1898 /* The channel is coming from our server, thus it is in our cell
1899 we will add it to our local list. */
1900 SilcChannelEntry channel;
1903 SILC_LOG_DEBUG(("New channel id(%s) from [Server] %s",
1904 silc_id_render(channel_id, SILC_ID_CHANNEL),
1907 /* Check that we don't already have this channel */
1908 channel = silc_idlist_find_channel_by_name(server->local_list,
1909 channel_name, NULL);
1911 channel = silc_idlist_find_channel_by_name(server->global_list,
1912 channel_name, NULL);
1914 /* If the channel does not exist, then create it. We create the channel
1915 with the channel ID provided by the server. This creates a new
1916 key to the channel as well that we will send to the server. */
1918 channel = silc_server_create_new_channel_with_id(server, NULL, NULL,
1922 silc_channel_payload_free(payload);
1923 silc_free(channel_id);
1927 /* Get the mode and set it to the channel */
1928 channel->mode = silc_channel_get_mode(payload);
1930 /* Send the new channel key to the server */
1931 chk = silc_channel_key_payload_encode(id_len, id,
1932 strlen(channel->channel_key->
1934 channel->channel_key->cipher->name,
1935 channel->key_len / 8,
1937 silc_server_packet_send(server, sock, SILC_PACKET_CHANNEL_KEY, 0,
1938 chk->data, chk->len, FALSE);
1939 silc_buffer_free(chk);
1942 /* The channel exist by that name, check whether the ID's match.
1943 If they don't then we'll force the server to use the ID we have.
1944 We also create a new key for the channel. */
1945 SilcBuffer users = NULL, users_modes = NULL;
1948 channel_id = silc_id_dup(channel_id, SILC_ID_CHANNEL);
1950 if (!SILC_ID_CHANNEL_COMPARE(channel_id, channel->id)) {
1951 /* They don't match, send CHANNEL_CHANGE notify to the server to
1952 force the ID change. */
1953 SILC_LOG_DEBUG(("Forcing the server to change Channel ID"));
1954 silc_server_send_notify_channel_change(server, sock, FALSE,
1955 channel_id, channel->id);
1958 /* If the mode is different from what we have then enforce the
1960 mode = silc_channel_get_mode(payload);
1961 if (channel->mode != mode) {
1962 SILC_LOG_DEBUG(("Forcing the server to change channel mode"));
1963 silc_server_send_notify_cmode(server, sock, FALSE, channel,
1964 channel->mode, server->id,
1966 channel->cipher, channel->hmac_name);
1969 /* Create new key for the channel and send it to the server and
1970 everybody else possibly on the channel. */
1972 if (!(channel->mode & SILC_CHANNEL_MODE_PRIVKEY)) {
1973 if (!silc_server_create_channel_key(server, channel, 0))
1976 /* Send to the channel */
1977 silc_server_send_channel_key(server, sock, channel, FALSE);
1978 id = silc_id_id2str(channel->id, SILC_ID_CHANNEL);
1979 id_len = SILC_ID_CHANNEL_LEN;
1981 /* Send to the server */
1982 chk = silc_channel_key_payload_encode(id_len, id,
1983 strlen(channel->channel_key->
1985 channel->channel_key->
1987 channel->key_len / 8,
1989 silc_server_packet_send(server, sock, SILC_PACKET_CHANNEL_KEY, 0,
1990 chk->data, chk->len, FALSE);
1991 silc_buffer_free(chk);
1995 silc_free(channel_id);
1997 /* Since the channel is coming from server and we also know about it
1998 then send the JOIN notify to the server so that it see's our
1999 users on the channel "joining" the channel. */
2000 silc_server_announce_get_channel_users(server, channel, &users,
2003 silc_buffer_push(users, users->data - users->head);
2004 silc_server_packet_send(server, sock,
2005 SILC_PACKET_NOTIFY, SILC_PACKET_FLAG_LIST,
2006 users->data, users->len, FALSE);
2007 silc_buffer_free(users);
2010 silc_buffer_push(users_modes, users_modes->data - users_modes->head);
2011 silc_server_packet_send_dest(server, sock,
2012 SILC_PACKET_NOTIFY, SILC_PACKET_FLAG_LIST,
2013 channel->id, SILC_ID_CHANNEL,
2015 users_modes->len, FALSE);
2016 silc_buffer_free(users_modes);
2021 silc_channel_payload_free(payload);
2024 /* Received New Channel List packet, list of New Channel List payloads inside
2025 one packet. Process the New Channel payloads one by one. */
2027 void silc_server_new_channel_list(SilcServer server,
2028 SilcSocketConnection sock,
2029 SilcPacketContext *packet)
2031 SilcPacketContext *new;
2035 SILC_LOG_DEBUG(("Processing New Channel List"));
2037 if (sock->type == SILC_SOCKET_TYPE_CLIENT ||
2038 packet->src_id_type != SILC_ID_SERVER ||
2039 server->server_type == SILC_SERVER)
2042 /* If the sender of this packet is server and we are router we need to
2043 broadcast this packet to other routers in the network. Broadcast
2044 this list packet instead of multiple New Channel packets. */
2045 if (!server->standalone && server->server_type == SILC_ROUTER &&
2046 sock->type == SILC_SOCKET_TYPE_SERVER &&
2047 !(packet->flags & SILC_PACKET_FLAG_BROADCAST)) {
2048 SILC_LOG_DEBUG(("Broadcasting received New Channel List packet"));
2049 silc_server_packet_send(server, server->router->connection,
2051 packet->flags | SILC_PACKET_FLAG_BROADCAST,
2052 packet->buffer->data, packet->buffer->len, FALSE);
2055 /* Make copy of the original packet context, except for the actual
2056 data buffer, which we will here now fetch from the original buffer. */
2057 new = silc_packet_context_alloc();
2058 new->type = SILC_PACKET_NEW_CHANNEL;
2059 new->flags = packet->flags;
2060 new->src_id = packet->src_id;
2061 new->src_id_len = packet->src_id_len;
2062 new->src_id_type = packet->src_id_type;
2063 new->dst_id = packet->dst_id;
2064 new->dst_id_len = packet->dst_id_len;
2065 new->dst_id_type = packet->dst_id_type;
2067 buffer = silc_buffer_alloc(512);
2068 new->buffer = buffer;
2070 while (packet->buffer->len) {
2071 SILC_GET16_MSB(len1, packet->buffer->data);
2072 if ((len1 > packet->buffer->len) ||
2073 (len1 > buffer->truelen))
2076 SILC_GET16_MSB(len2, packet->buffer->data + 2 + len1);
2077 if ((len2 > packet->buffer->len) ||
2078 (len2 > buffer->truelen))
2081 silc_buffer_pull_tail(buffer, 8 + len1 + len2);
2082 silc_buffer_put(buffer, packet->buffer->data, 8 + len1 + len2);
2084 /* Process the New Channel */
2085 silc_server_new_channel(server, sock, new);
2087 silc_buffer_push_tail(buffer, 8 + len1 + len2);
2088 silc_buffer_pull(packet->buffer, 8 + len1 + len2);
2091 silc_buffer_free(buffer);
2095 /* Received key agreement packet. This packet is never for us. It is to
2096 the client in the packet's destination ID. Sending of this sort of packet
2097 equals sending private message, ie. it is sent point to point from
2098 one client to another. */
2100 void silc_server_key_agreement(SilcServer server,
2101 SilcSocketConnection sock,
2102 SilcPacketContext *packet)
2104 SilcSocketConnection dst_sock;
2105 SilcIDListData idata;
2107 SILC_LOG_DEBUG(("Start"));
2109 if (packet->src_id_type != SILC_ID_CLIENT ||
2110 packet->dst_id_type != SILC_ID_CLIENT)
2113 if (!packet->dst_id)
2116 /* Get the route to the client */
2117 dst_sock = silc_server_get_client_route(server, packet->dst_id,
2118 packet->dst_id_len, NULL, &idata);
2122 /* Relay the packet */
2123 silc_server_relay_packet(server, dst_sock, idata->send_key,
2124 idata->hmac_send, packet, FALSE);
2127 /* Received connection auth request packet that is used during connection
2128 phase to resolve the mandatory authentication method. This packet can
2129 actually be received at anytime but usually it is used only during
2130 the connection authentication phase. Now, protocol says that this packet
2131 can come from client or server, however, we support only this coming
2132 from client and expect that server's always knows what authentication
2135 void silc_server_connection_auth_request(SilcServer server,
2136 SilcSocketConnection sock,
2137 SilcPacketContext *packet)
2139 SilcServerConfigSectionClientConnection *client = NULL;
2142 SilcAuthMethod auth_meth;
2144 SILC_LOG_DEBUG(("Start"));
2146 if (packet->src_id_type && packet->src_id_type != SILC_ID_CLIENT)
2149 /* Parse the payload */
2150 ret = silc_buffer_unformat(packet->buffer,
2151 SILC_STR_UI_SHORT(&conn_type),
2152 SILC_STR_UI_SHORT(NULL),
2157 if (conn_type != SILC_SOCKET_TYPE_CLIENT)
2160 /* Get the authentication method for the client */
2161 auth_meth = SILC_AUTH_NONE;
2162 client = silc_server_config_find_client_conn(server->config,
2166 client = silc_server_config_find_client_conn(server->config,
2170 auth_meth = client->auth_meth;
2172 /* Send it back to the client */
2173 silc_server_send_connection_auth_request(server, sock,
2178 /* Received REKEY packet. The sender of the packet wants to regenerate
2179 its session keys. This starts the REKEY protocol. */
2181 void silc_server_rekey(SilcServer server,
2182 SilcSocketConnection sock,
2183 SilcPacketContext *packet)
2185 SilcProtocol protocol;
2186 SilcServerRekeyInternalContext *proto_ctx;
2187 SilcIDListData idata = (SilcIDListData)sock->user_data;
2189 SILC_LOG_DEBUG(("Start"));
2191 /* Allocate internal protocol context. This is sent as context
2193 proto_ctx = silc_calloc(1, sizeof(*proto_ctx));
2194 proto_ctx->server = (void *)server;
2195 proto_ctx->sock = sock;
2196 proto_ctx->responder = TRUE;
2197 proto_ctx->pfs = idata->rekey->pfs;
2199 /* Perform rekey protocol. Will call the final callback after the
2200 protocol is over. */
2201 silc_protocol_alloc(SILC_PROTOCOL_SERVER_REKEY,
2202 &protocol, proto_ctx, silc_server_rekey_final);
2203 sock->protocol = protocol;
2205 if (proto_ctx->pfs == FALSE)
2206 /* Run the protocol */
2207 silc_protocol_execute(protocol, server->schedule, 0, 0);