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->connection) {
873 sock = client->connection;
874 silc_server_free_client_data(server, NULL, client, FALSE, NULL);
875 silc_server_close_connection(server, sock);
880 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
884 /* Send the notify to local clients on the channels except to the
885 client who is killed. */
886 silc_server_send_notify_on_channels(server, client, client,
887 SILC_NOTIFY_TYPE_KILLED,
892 /* Remove the client from all channels */
893 silc_server_remove_from_channels(server, NULL, client, FALSE, NULL,
899 case SILC_NOTIFY_TYPE_UMODE_CHANGE:
901 * Save the mode of the client.
904 SILC_LOG_DEBUG(("UMODE_CHANGE notify"));
907 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
910 client_id = silc_id_payload_parse_id(tmp, tmp_len);
914 /* Get client entry */
915 client = silc_idlist_find_client_by_id(server->global_list,
916 client_id, TRUE, NULL);
918 client = silc_idlist_find_client_by_id(server->local_list,
919 client_id, TRUE, NULL);
921 silc_free(client_id);
925 silc_free(client_id);
928 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
933 SILC_GET32_MSB(client->mode, tmp);
937 case SILC_NOTIFY_TYPE_BAN:
942 SILC_LOG_DEBUG(("BAN notify"));
945 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
948 channel_id = silc_id_payload_parse_id(tmp, tmp_len);
952 /* Get channel entry */
953 channel = silc_idlist_find_channel_by_id(server->global_list,
956 channel = silc_idlist_find_channel_by_id(server->local_list,
959 silc_free(channel_id);
963 silc_free(channel_id);
965 /* Get the new ban and add it to the ban list */
966 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
968 if (!channel->ban_list)
969 channel->ban_list = silc_calloc(tmp_len + 2,
970 sizeof(*channel->ban_list));
972 channel->ban_list = silc_realloc(channel->ban_list,
973 sizeof(*channel->ban_list) *
975 strlen(channel->ban_list) + 2));
976 strncat(channel->ban_list, tmp, tmp_len);
977 strncat(channel->ban_list, ",", 1);
980 /* Get the ban to be removed and remove it from the list */
981 tmp = silc_argument_get_arg_type(args, 3, &tmp_len);
982 if (tmp && channel->ban_list) {
983 char *start, *end, *n;
985 if (!strcmp(channel->ban_list, tmp)) {
986 silc_free(channel->ban_list);
987 channel->ban_list = NULL;
989 start = strstr(channel->ban_list, tmp);
990 if (start && strlen(start) >= tmp_len) {
991 end = start + tmp_len;
992 n = silc_calloc(strlen(channel->ban_list) - tmp_len, sizeof(*n));
993 strncat(n, channel->ban_list, start - channel->ban_list);
994 strncat(n, end + 1, ((channel->ban_list +
995 strlen(channel->ban_list)) - end) - 1);
996 silc_free(channel->ban_list);
997 channel->ban_list = n;
1004 /* Ignore rest of the notify types for now */
1005 case SILC_NOTIFY_TYPE_NONE:
1006 case SILC_NOTIFY_TYPE_MOTD:
1013 silc_notify_payload_free(payload);
1016 void silc_server_notify_list(SilcServer server,
1017 SilcSocketConnection sock,
1018 SilcPacketContext *packet)
1020 SilcPacketContext *new;
1024 SILC_LOG_DEBUG(("Processing Notify List"));
1026 if (sock->type == SILC_SOCKET_TYPE_CLIENT ||
1027 packet->src_id_type != SILC_ID_SERVER)
1030 /* Make copy of the original packet context, except for the actual
1031 data buffer, which we will here now fetch from the original buffer. */
1032 new = silc_packet_context_alloc();
1033 new->type = SILC_PACKET_NOTIFY;
1034 new->flags = packet->flags;
1035 new->src_id = packet->src_id;
1036 new->src_id_len = packet->src_id_len;
1037 new->src_id_type = packet->src_id_type;
1038 new->dst_id = packet->dst_id;
1039 new->dst_id_len = packet->dst_id_len;
1040 new->dst_id_type = packet->dst_id_type;
1042 buffer = silc_buffer_alloc(1024);
1043 new->buffer = buffer;
1045 while (packet->buffer->len) {
1046 SILC_GET16_MSB(len, packet->buffer->data + 2);
1047 if (len > packet->buffer->len)
1050 if (len > buffer->truelen) {
1051 silc_buffer_free(buffer);
1052 buffer = silc_buffer_alloc(1024 + len);
1055 silc_buffer_pull_tail(buffer, len);
1056 silc_buffer_put(buffer, packet->buffer->data, len);
1058 /* Process the Notify */
1059 silc_server_notify(server, sock, new);
1061 silc_buffer_push_tail(buffer, len);
1062 silc_buffer_pull(packet->buffer, len);
1065 silc_buffer_free(buffer);
1069 /* Received private message. This resolves the destination of the message
1070 and sends the packet. This is used by both server and router. If the
1071 destination is our locally connected client this sends the packet to
1072 the client. This may also send the message for further routing if
1073 the destination is not in our server (or router). */
1075 void silc_server_private_message(SilcServer server,
1076 SilcSocketConnection sock,
1077 SilcPacketContext *packet)
1079 SilcSocketConnection dst_sock;
1080 SilcIDListData idata;
1082 SILC_LOG_DEBUG(("Start"));
1084 if (packet->src_id_type != SILC_ID_CLIENT ||
1085 packet->dst_id_type != SILC_ID_CLIENT)
1088 if (!packet->dst_id)
1091 /* Get the route to the client */
1092 dst_sock = silc_server_get_client_route(server, packet->dst_id,
1093 packet->dst_id_len, NULL, &idata);
1097 /* Send the private message */
1098 silc_server_send_private_message(server, dst_sock, idata->send_key,
1099 idata->hmac_send, packet);
1102 /* Received private message key packet.. This packet is never for us. It is to
1103 the client in the packet's destination ID. Sending of this sort of packet
1104 equals sending private message, ie. it is sent point to point from
1105 one client to another. */
1107 void silc_server_private_message_key(SilcServer server,
1108 SilcSocketConnection sock,
1109 SilcPacketContext *packet)
1111 SilcSocketConnection dst_sock;
1112 SilcIDListData idata;
1114 SILC_LOG_DEBUG(("Start"));
1116 if (packet->src_id_type != SILC_ID_CLIENT ||
1117 packet->dst_id_type != SILC_ID_CLIENT)
1120 if (!packet->dst_id)
1123 /* Get the route to the client */
1124 dst_sock = silc_server_get_client_route(server, packet->dst_id,
1125 packet->dst_id_len, NULL, &idata);
1129 /* Relay the packet */
1130 silc_server_relay_packet(server, dst_sock, idata->send_key,
1131 idata->hmac_send, packet, FALSE);
1134 /* Processes incoming command reply packet. The command reply packet may
1135 be destined to one of our clients or it may directly for us. We will
1136 call the command reply routine after processing the packet. */
1138 void silc_server_command_reply(SilcServer server,
1139 SilcSocketConnection sock,
1140 SilcPacketContext *packet)
1142 SilcBuffer buffer = packet->buffer;
1143 SilcClientEntry client = NULL;
1144 SilcSocketConnection dst_sock;
1145 SilcIDListData idata;
1146 SilcClientID *id = NULL;
1148 SILC_LOG_DEBUG(("Start"));
1150 /* Source must be server or router */
1151 if (packet->src_id_type != SILC_ID_SERVER &&
1152 sock->type != SILC_SOCKET_TYPE_ROUTER)
1155 if (packet->dst_id_type == SILC_ID_CHANNEL)
1158 if (packet->dst_id_type == SILC_ID_CLIENT) {
1159 /* Destination must be one of ours */
1160 id = silc_id_str2id(packet->dst_id, packet->dst_id_len, SILC_ID_CLIENT);
1163 client = silc_idlist_find_client_by_id(server->local_list, id, TRUE, NULL);
1165 SILC_LOG_ERROR(("Cannot process command reply to unknown client"));
1171 if (packet->dst_id_type == SILC_ID_SERVER) {
1172 /* For now this must be for us */
1173 if (memcmp(packet->dst_id, server->id_string, packet->dst_id_len)) {
1174 SILC_LOG_ERROR(("Cannot process command reply to unknown server"));
1179 /* Execute command reply locally for the command */
1180 silc_server_command_reply_process(server, sock, buffer);
1182 if (packet->dst_id_type == SILC_ID_CLIENT && client && id) {
1183 /* Relay the packet to the client */
1185 dst_sock = (SilcSocketConnection)client->connection;
1186 silc_buffer_push(buffer, SILC_PACKET_HEADER_LEN + packet->src_id_len
1187 + packet->dst_id_len + packet->padlen);
1189 silc_packet_send_prepare(dst_sock, 0, 0, buffer->len);
1190 silc_buffer_put(dst_sock->outbuf, buffer->data, buffer->len);
1192 idata = (SilcIDListData)client;
1194 /* Encrypt packet */
1195 silc_packet_encrypt(idata->send_key, idata->hmac_send, dst_sock->outbuf,
1198 /* Send the packet */
1199 silc_server_packet_send_real(server, dst_sock, TRUE);
1205 /* Process received channel message. The message can be originated from
1206 client or server. */
1208 void silc_server_channel_message(SilcServer server,
1209 SilcSocketConnection sock,
1210 SilcPacketContext *packet)
1212 SilcChannelEntry channel = NULL;
1213 SilcChannelID *id = NULL;
1214 void *sender = NULL;
1215 void *sender_entry = NULL;
1217 SILC_LOG_DEBUG(("Processing channel message"));
1220 if (packet->dst_id_type != SILC_ID_CHANNEL) {
1221 SILC_LOG_DEBUG(("Received bad message for channel, dropped"));
1225 /* Find channel entry */
1226 id = silc_id_str2id(packet->dst_id, packet->dst_id_len, SILC_ID_CHANNEL);
1229 channel = silc_idlist_find_channel_by_id(server->local_list, id, NULL);
1231 channel = silc_idlist_find_channel_by_id(server->global_list, id, NULL);
1233 SILC_LOG_DEBUG(("Could not find channel"));
1238 /* See that this client is on the channel. If the original sender is
1239 not client (as it can be server as well) we don't do the check. */
1240 sender = silc_id_str2id(packet->src_id, packet->src_id_len,
1241 packet->src_id_type);
1244 if (packet->src_id_type == SILC_ID_CLIENT) {
1245 sender_entry = silc_idlist_find_client_by_id(server->local_list,
1246 sender, TRUE, NULL);
1248 sender_entry = silc_idlist_find_client_by_id(server->global_list,
1249 sender, TRUE, NULL);
1250 if (!sender_entry || !silc_server_client_on_channel(sender_entry,
1252 SILC_LOG_DEBUG(("Client not on channel"));
1257 /* Distribute the packet to our local clients. This will send the
1258 packet for further routing as well, if needed. */
1259 silc_server_packet_relay_to_channel(server, sock, channel, sender,
1260 packet->src_id_type, sender_entry,
1261 packet->buffer->data,
1262 packet->buffer->len, FALSE);
1271 /* Received channel key packet. We distribute the key to all of our locally
1272 connected clients on the channel. */
1274 void silc_server_channel_key(SilcServer server,
1275 SilcSocketConnection sock,
1276 SilcPacketContext *packet)
1278 SilcBuffer buffer = packet->buffer;
1279 SilcChannelEntry channel;
1281 if (packet->src_id_type != SILC_ID_SERVER ||
1282 (server->server_type == SILC_ROUTER &&
1283 sock->type == SILC_SOCKET_TYPE_ROUTER))
1286 /* Save the channel key */
1287 channel = silc_server_save_channel_key(server, buffer, NULL);
1291 /* Distribute the key to everybody who is on the channel. If we are router
1292 we will also send it to locally connected servers. */
1293 silc_server_send_channel_key(server, sock, channel, FALSE);
1296 /* Received New Client packet and processes it. Creates Client ID for the
1297 client. Client becomes registered after calling this functions. */
1299 SilcClientEntry silc_server_new_client(SilcServer server,
1300 SilcSocketConnection sock,
1301 SilcPacketContext *packet)
1303 SilcBuffer buffer = packet->buffer;
1304 SilcClientEntry client;
1305 SilcClientID *client_id;
1307 SilcIDListData idata;
1308 char *username = NULL, *realname = NULL, *id_string;
1311 char *hostname, *nickname;
1313 SILC_LOG_DEBUG(("Creating new client"));
1315 if (sock->type != SILC_SOCKET_TYPE_CLIENT)
1318 /* Take client entry */
1319 client = (SilcClientEntry)sock->user_data;
1320 idata = (SilcIDListData)client;
1322 /* Remove the old cache entry */
1323 if (!silc_idcache_del_by_context(server->local_list->clients, client)) {
1324 SILC_LOG_ERROR(("Lost client's cache entry - bad thing"));
1325 silc_server_disconnect_remote(server, sock, "Server closed connection: "
1330 /* Parse incoming packet */
1331 ret = silc_buffer_unformat(buffer,
1332 SILC_STR_UI16_STRING_ALLOC(&username),
1333 SILC_STR_UI16_STRING_ALLOC(&realname),
1337 silc_free(username);
1339 silc_free(realname);
1340 silc_server_disconnect_remote(server, sock, "Server closed connection: "
1341 "Incomplete client information");
1346 silc_free(username);
1348 silc_free(realname);
1349 silc_server_disconnect_remote(server, sock, "Server closed connection: "
1350 "Incomplete client information");
1354 if (strlen(username) > 128)
1355 username[127] = '\0';
1357 nickname = strdup(username);
1359 /* Make sanity checks for the hostname of the client. If the hostname
1360 is provided in the `username' check that it is the same than the
1361 resolved hostname, or if not resolved the hostname that appears in
1362 the client's public key. If the hostname is not present then put
1363 it from the resolved name or from the public key. */
1364 if (strchr(username, '@')) {
1365 SilcPublicKeyIdentifier pident;
1366 int tlen = strcspn(username, "@");
1367 char *phostname = NULL;
1369 hostname = silc_calloc((strlen(username) - tlen) + 1, sizeof(char));
1370 memcpy(hostname, username + tlen + 1, strlen(username) - tlen - 1);
1372 if (strcmp(sock->hostname, sock->ip) &&
1373 strcmp(sock->hostname, hostname)) {
1374 silc_free(username);
1375 silc_free(hostname);
1377 silc_free(realname);
1378 silc_server_disconnect_remote(server, sock,
1379 "Server closed connection: "
1380 "Incomplete client information");
1384 pident = silc_pkcs_decode_identifier(client->data.public_key->identifier);
1386 phostname = strdup(pident->host);
1387 silc_pkcs_free_identifier(pident);
1390 if (!strcmp(sock->hostname, sock->ip) &&
1391 phostname && strcmp(phostname, hostname)) {
1392 silc_free(username);
1393 silc_free(hostname);
1395 silc_free(phostname);
1397 silc_free(realname);
1398 silc_server_disconnect_remote(server, sock,
1399 "Server closed connection: "
1400 "Incomplete client information");
1405 silc_free(phostname);
1407 /* The hostname is not present, add it. */
1409 /* XXX For now we cannot take the host name from the public key since
1410 they are not trusted or we cannot verify them as trusted. Just take
1411 what the resolved name or address is. */
1413 if (strcmp(sock->hostname, sock->ip)) {
1415 newusername = silc_calloc(strlen(username) +
1416 strlen(sock->hostname) + 2,
1417 sizeof(*newusername));
1418 strncat(newusername, username, strlen(username));
1419 strncat(newusername, "@", 1);
1420 strncat(newusername, sock->hostname, strlen(sock->hostname));
1421 silc_free(username);
1422 username = newusername;
1425 SilcPublicKeyIdentifier pident =
1426 silc_pkcs_decode_identifier(client->data.public_key->identifier);
1429 newusername = silc_calloc(strlen(username) +
1430 strlen(pident->host) + 2,
1431 sizeof(*newusername));
1432 strncat(newusername, username, strlen(username));
1433 strncat(newusername, "@", 1);
1434 strncat(newusername, pident->host, strlen(pident->host));
1435 silc_free(username);
1436 username = newusername;
1437 silc_pkcs_free_identifier(pident);
1443 /* Create Client ID */
1444 silc_id_create_client_id(server->id, server->rng, server->md5hash,
1445 nickname, &client_id);
1447 /* Update client entry */
1448 idata->registered = TRUE;
1449 client->nickname = nickname;
1450 client->username = username;
1451 client->userinfo = realname ? realname : strdup(" ");
1452 client->id = client_id;
1453 id_len = silc_id_get_len(client_id, SILC_ID_CLIENT);
1455 /* Add the client again to the ID cache */
1456 silc_idcache_add(server->local_list->clients, client->nickname,
1457 client_id, client, FALSE);
1459 /* Notify our router about new client on the SILC network */
1460 if (!server->standalone)
1461 silc_server_send_new_id(server, (SilcSocketConnection)
1462 server->router->connection,
1463 server->server_type == SILC_ROUTER ? TRUE : FALSE,
1464 client->id, SILC_ID_CLIENT, id_len);
1466 /* Send the new client ID to the client. */
1467 id_string = silc_id_id2str(client->id, SILC_ID_CLIENT);
1468 reply = silc_buffer_alloc(2 + 2 + id_len);
1469 silc_buffer_pull_tail(reply, SILC_BUFFER_END(reply));
1470 silc_buffer_format(reply,
1471 SILC_STR_UI_SHORT(SILC_ID_CLIENT),
1472 SILC_STR_UI_SHORT(id_len),
1473 SILC_STR_UI_XNSTRING(id_string, id_len),
1475 silc_server_packet_send(server, sock, SILC_PACKET_NEW_ID, 0,
1476 reply->data, reply->len, FALSE);
1477 silc_free(id_string);
1478 silc_buffer_free(reply);
1480 /* Send some nice info to the client */
1481 SILC_SERVER_SEND_NOTIFY(server, sock, SILC_NOTIFY_TYPE_NONE,
1482 ("Welcome to the SILC Network %s",
1484 SILC_SERVER_SEND_NOTIFY(server, sock, SILC_NOTIFY_TYPE_NONE,
1485 ("Your host is %s, running version %s",
1486 server->config->server_info->server_name,
1488 if (server->server_type == SILC_ROUTER) {
1489 SILC_SERVER_SEND_NOTIFY(server, sock, SILC_NOTIFY_TYPE_NONE,
1490 ("There are %d clients on %d servers in SILC "
1491 "Network", server->stat.clients,
1492 server->stat.servers + 1));
1493 SILC_SERVER_SEND_NOTIFY(server, sock, SILC_NOTIFY_TYPE_NONE,
1494 ("There are %d clients on %d server in our cell",
1495 server->stat.cell_clients,
1496 server->stat.cell_servers + 1));
1497 SILC_SERVER_SEND_NOTIFY(server, sock, SILC_NOTIFY_TYPE_NONE,
1498 ("I have %d clients, %d channels, %d servers and "
1500 server->stat.my_clients,
1501 server->stat.my_channels,
1502 server->stat.my_servers,
1503 server->stat.my_routers));
1504 SILC_SERVER_SEND_NOTIFY(server, sock, SILC_NOTIFY_TYPE_NONE,
1505 ("%d server operators and %d router operators "
1507 server->stat.my_server_ops,
1508 server->stat.my_router_ops));
1510 SILC_SERVER_SEND_NOTIFY(server, sock, SILC_NOTIFY_TYPE_NONE,
1511 ("I have %d clients and %d channels formed",
1512 server->stat.my_clients,
1513 server->stat.my_channels));
1514 SILC_SERVER_SEND_NOTIFY(server, sock, SILC_NOTIFY_TYPE_NONE,
1515 ("%d operators online",
1516 server->stat.my_server_ops));
1518 SILC_SERVER_SEND_NOTIFY(server, sock, SILC_NOTIFY_TYPE_NONE,
1519 ("Your connection is secured with %s cipher, "
1520 "key length %d bits",
1521 idata->send_key->cipher->name,
1522 idata->send_key->cipher->key_len));
1523 SILC_SERVER_SEND_NOTIFY(server, sock, SILC_NOTIFY_TYPE_NONE,
1524 ("Your current nickname is %s",
1528 silc_server_send_motd(server, sock);
1533 /* Create new server. This processes received New Server packet and
1534 saves the received Server ID. The server is our locally connected
1535 server thus we save all the information and save it to local list.
1536 This funtion can be used by both normal server and router server.
1537 If normal server uses this it means that its router has connected
1538 to the server. If router uses this it means that one of the cell's
1539 servers is connected to the router. */
1541 SilcServerEntry silc_server_new_server(SilcServer server,
1542 SilcSocketConnection sock,
1543 SilcPacketContext *packet)
1545 SilcBuffer buffer = packet->buffer;
1546 SilcServerEntry new_server;
1547 SilcServerID *server_id;
1548 SilcIDListData idata;
1549 unsigned char *server_name, *id_string;
1550 uint16 id_len, name_len;
1553 SILC_LOG_DEBUG(("Creating new server"));
1555 if (sock->type != SILC_SOCKET_TYPE_SERVER &&
1556 sock->type != SILC_SOCKET_TYPE_ROUTER)
1559 /* Take server entry */
1560 new_server = (SilcServerEntry)sock->user_data;
1561 idata = (SilcIDListData)new_server;
1563 /* Remove the old cache entry */
1564 silc_idcache_del_by_context(server->local_list->servers, new_server);
1566 /* Parse the incoming packet */
1567 ret = silc_buffer_unformat(buffer,
1568 SILC_STR_UI16_NSTRING_ALLOC(&id_string, &id_len),
1569 SILC_STR_UI16_NSTRING_ALLOC(&server_name,
1574 silc_free(id_string);
1576 silc_free(server_name);
1580 if (id_len > buffer->len) {
1581 silc_free(id_string);
1582 silc_free(server_name);
1587 server_name[255] = '\0';
1590 server_id = silc_id_str2id(id_string, id_len, SILC_ID_SERVER);
1592 silc_free(id_string);
1593 silc_free(server_name);
1596 silc_free(id_string);
1598 /* Update server entry */
1599 idata->registered = TRUE;
1600 new_server->server_name = server_name;
1601 new_server->id = server_id;
1603 /* Add again the entry to the ID cache. */
1604 silc_idcache_add(server->local_list->servers, server_name, server_id,
1607 /* Distribute the information about new server in the SILC network
1608 to our router. If we are normal server we won't send anything
1609 since this connection must be our router connection. */
1610 if (server->server_type == SILC_ROUTER && !server->standalone &&
1611 server->router->connection != sock)
1612 silc_server_send_new_id(server, server->router->connection,
1613 TRUE, new_server->id, SILC_ID_SERVER,
1614 silc_id_get_len(server_id, SILC_ID_SERVER));
1616 if (server->server_type == SILC_ROUTER)
1617 server->stat.cell_servers++;
1622 /* Processes incoming New ID packet. New ID Payload is used to distribute
1623 information about newly registered clients and servers. */
1625 static void silc_server_new_id_real(SilcServer server,
1626 SilcSocketConnection sock,
1627 SilcPacketContext *packet,
1630 SilcBuffer buffer = packet->buffer;
1632 SilcServerEntry router;
1633 SilcSocketConnection router_sock;
1638 SILC_LOG_DEBUG(("Processing new ID"));
1640 if (sock->type == SILC_SOCKET_TYPE_CLIENT ||
1641 server->server_type == SILC_SERVER ||
1642 packet->src_id_type != SILC_ID_SERVER)
1645 idp = silc_id_payload_parse(buffer);
1649 id_type = silc_id_payload_get_type(idp);
1651 /* Normal server cannot have other normal server connections */
1652 if (id_type == SILC_ID_SERVER && sock->type == SILC_SOCKET_TYPE_SERVER)
1655 id = silc_id_payload_get_id(idp);
1659 /* If the sender of this packet is server and we are router we need to
1660 broadcast this packet to other routers in the network. */
1661 if (broadcast && !server->standalone && server->server_type == SILC_ROUTER &&
1662 sock->type == SILC_SOCKET_TYPE_SERVER &&
1663 !(packet->flags & SILC_PACKET_FLAG_BROADCAST)) {
1664 SILC_LOG_DEBUG(("Broadcasting received New ID packet"));
1665 silc_server_packet_send(server, server->router->connection,
1667 packet->flags | SILC_PACKET_FLAG_BROADCAST,
1668 buffer->data, buffer->len, FALSE);
1671 if (sock->type == SILC_SOCKET_TYPE_SERVER)
1672 id_list = server->local_list;
1674 id_list = server->global_list;
1676 /* If the packet is coming from server then use the sender as the
1677 origin of the the packet. If it came from router then check the real
1678 sender of the packet and use that as the origin. */
1679 if (sock->type == SILC_SOCKET_TYPE_SERVER) {
1681 router = sock->user_data;
1683 void *sender_id = silc_id_str2id(packet->src_id, packet->src_id_len,
1684 packet->src_id_type);
1685 router = silc_idlist_find_server_by_id(server->global_list,
1686 sender_id, TRUE, NULL);
1688 router = silc_idlist_find_server_by_id(server->local_list,
1689 sender_id, TRUE, NULL);
1690 silc_free(sender_id);
1697 case SILC_ID_CLIENT:
1699 SilcClientEntry entry;
1701 SILC_LOG_DEBUG(("New client id(%s) from [%s] %s",
1702 silc_id_render(id, SILC_ID_CLIENT),
1703 sock->type == SILC_SOCKET_TYPE_SERVER ?
1704 "Server" : "Router", sock->hostname));
1706 /* As a router we keep information of all global information in our
1707 global list. Cell wide information however is kept in the local
1709 entry = silc_idlist_add_client(id_list, NULL, NULL, NULL,
1712 SILC_LOG_ERROR(("Could not add new client to the ID Cache"));
1715 entry->nickname = NULL;
1716 entry->data.registered = TRUE;
1718 if (sock->type == SILC_SOCKET_TYPE_SERVER)
1719 server->stat.cell_clients++;
1720 server->stat.clients++;
1724 case SILC_ID_SERVER:
1726 SilcServerEntry entry;
1728 /* If the ID is mine, ignore it. */
1729 if (SILC_ID_SERVER_COMPARE(id, server->id)) {
1730 SILC_LOG_DEBUG(("Ignoring my own ID as new ID"));
1734 SILC_LOG_DEBUG(("New server id(%s) from [%s] %s",
1735 silc_id_render(id, SILC_ID_SERVER),
1736 sock->type == SILC_SOCKET_TYPE_SERVER ?
1737 "Server" : "Router", sock->hostname));
1739 /* As a router we keep information of all global information in our
1740 global list. Cell wide information however is kept in the local
1742 entry = silc_idlist_add_server(id_list, NULL, 0, id, router,
1745 SILC_LOG_ERROR(("Could not add new server to the ID Cache"));
1748 entry->data.registered = TRUE;
1750 if (sock->type == SILC_SOCKET_TYPE_SERVER)
1751 server->stat.cell_servers++;
1752 server->stat.servers++;
1756 case SILC_ID_CHANNEL:
1757 SILC_LOG_ERROR(("Channel cannot be registered with NEW_ID packet"));
1765 silc_id_payload_free(idp);
1769 /* Processes incoming New ID packet. New ID Payload is used to distribute
1770 information about newly registered clients and servers. */
1772 void silc_server_new_id(SilcServer server, SilcSocketConnection sock,
1773 SilcPacketContext *packet)
1775 silc_server_new_id_real(server, sock, packet, TRUE);
1778 /* Receoved New Id List packet, list of New ID payloads inside one
1779 packet. Process the New ID payloads one by one. */
1781 void silc_server_new_id_list(SilcServer server, SilcSocketConnection sock,
1782 SilcPacketContext *packet)
1784 SilcPacketContext *new_id;
1788 SILC_LOG_DEBUG(("Processing New ID List"));
1790 if (sock->type == SILC_SOCKET_TYPE_CLIENT ||
1791 packet->src_id_type != SILC_ID_SERVER)
1794 /* If the sender of this packet is server and we are router we need to
1795 broadcast this packet to other routers in the network. Broadcast
1796 this list packet instead of multiple New ID packets. */
1797 if (!server->standalone && server->server_type == SILC_ROUTER &&
1798 sock->type == SILC_SOCKET_TYPE_SERVER &&
1799 !(packet->flags & SILC_PACKET_FLAG_BROADCAST)) {
1800 SILC_LOG_DEBUG(("Broadcasting received New ID List packet"));
1801 silc_server_packet_send(server, server->router->connection,
1803 packet->flags | SILC_PACKET_FLAG_BROADCAST,
1804 packet->buffer->data, packet->buffer->len, FALSE);
1807 /* Make copy of the original packet context, except for the actual
1808 data buffer, which we will here now fetch from the original buffer. */
1809 new_id = silc_packet_context_alloc();
1810 new_id->type = SILC_PACKET_NEW_ID;
1811 new_id->flags = packet->flags;
1812 new_id->src_id = packet->src_id;
1813 new_id->src_id_len = packet->src_id_len;
1814 new_id->src_id_type = packet->src_id_type;
1815 new_id->dst_id = packet->dst_id;
1816 new_id->dst_id_len = packet->dst_id_len;
1817 new_id->dst_id_type = packet->dst_id_type;
1819 idp = silc_buffer_alloc(256);
1820 new_id->buffer = idp;
1822 while (packet->buffer->len) {
1823 SILC_GET16_MSB(id_len, packet->buffer->data + 2);
1824 if ((id_len > packet->buffer->len) ||
1825 (id_len > idp->truelen))
1828 silc_buffer_pull_tail(idp, 4 + id_len);
1829 silc_buffer_put(idp, packet->buffer->data, 4 + id_len);
1831 /* Process the New ID */
1832 silc_server_new_id_real(server, sock, new_id, FALSE);
1834 silc_buffer_push_tail(idp, 4 + id_len);
1835 silc_buffer_pull(packet->buffer, 4 + id_len);
1838 silc_buffer_free(idp);
1842 /* Received New Channel packet. Information about new channels in the
1843 network are distributed using this packet. Save the information about
1844 the new channel. This usually comes from router but also normal server
1845 can send this to notify channels it has when it connects to us. */
1847 void silc_server_new_channel(SilcServer server,
1848 SilcSocketConnection sock,
1849 SilcPacketContext *packet)
1851 SilcChannelPayload payload;
1852 SilcChannelID *channel_id;
1859 SILC_LOG_DEBUG(("Processing New Channel"));
1861 if (sock->type == SILC_SOCKET_TYPE_CLIENT ||
1862 packet->src_id_type != SILC_ID_SERVER ||
1863 server->server_type == SILC_SERVER)
1866 /* Parse the channel payload */
1867 payload = silc_channel_payload_parse(packet->buffer);
1871 /* Get the channel ID */
1872 channel_id = silc_channel_get_id_parse(payload);
1874 silc_channel_payload_free(payload);
1878 channel_name = silc_channel_get_name(payload, &name_len);
1880 channel_name[255] = '\0';
1882 id = silc_channel_get_id(payload, &id_len);
1884 if (sock->type == SILC_SOCKET_TYPE_ROUTER) {
1885 /* Add the channel to global list as it is coming from router. It
1886 cannot be our own channel as it is coming from router. */
1888 SILC_LOG_DEBUG(("New channel id(%s) from [Router] %s",
1889 silc_id_render(channel_id, SILC_ID_CHANNEL),
1892 silc_idlist_add_channel(server->global_list, strdup(channel_name),
1893 0, channel_id, sock->user_data, NULL, NULL);
1895 server->stat.channels++;
1897 /* The channel is coming from our server, thus it is in our cell
1898 we will add it to our local list. */
1899 SilcChannelEntry channel;
1902 SILC_LOG_DEBUG(("New channel id(%s) from [Server] %s",
1903 silc_id_render(channel_id, SILC_ID_CHANNEL),
1906 /* Check that we don't already have this channel */
1907 channel = silc_idlist_find_channel_by_name(server->local_list,
1908 channel_name, NULL);
1910 channel = silc_idlist_find_channel_by_name(server->global_list,
1911 channel_name, NULL);
1913 /* If the channel does not exist, then create it. We create the channel
1914 with the channel ID provided by the server. This creates a new
1915 key to the channel as well that we will send to the server. */
1917 channel = silc_server_create_new_channel_with_id(server, NULL, NULL,
1921 silc_channel_payload_free(payload);
1922 silc_free(channel_id);
1926 /* Get the mode and set it to the channel */
1927 channel->mode = silc_channel_get_mode(payload);
1929 /* Send the new channel key to the server */
1930 chk = silc_channel_key_payload_encode(id_len, id,
1931 strlen(channel->channel_key->
1933 channel->channel_key->cipher->name,
1934 channel->key_len / 8,
1936 silc_server_packet_send(server, sock, SILC_PACKET_CHANNEL_KEY, 0,
1937 chk->data, chk->len, FALSE);
1938 silc_buffer_free(chk);
1941 /* The channel exist by that name, check whether the ID's match.
1942 If they don't then we'll force the server to use the ID we have.
1943 We also create a new key for the channel. */
1944 SilcBuffer users = NULL, users_modes = NULL;
1947 channel_id = silc_id_dup(channel_id, SILC_ID_CHANNEL);
1949 if (!SILC_ID_CHANNEL_COMPARE(channel_id, channel->id)) {
1950 /* They don't match, send CHANNEL_CHANGE notify to the server to
1951 force the ID change. */
1952 SILC_LOG_DEBUG(("Forcing the server to change Channel ID"));
1953 silc_server_send_notify_channel_change(server, sock, FALSE,
1954 channel_id, channel->id);
1957 /* If the mode is different from what we have then enforce the
1959 mode = silc_channel_get_mode(payload);
1960 if (channel->mode != mode) {
1961 SILC_LOG_DEBUG(("Forcing the server to change channel mode"));
1962 silc_server_send_notify_cmode(server, sock, FALSE, channel,
1963 channel->mode, server->id,
1965 channel->cipher, channel->hmac_name);
1968 /* Create new key for the channel and send it to the server and
1969 everybody else possibly on the channel. */
1971 if (!(channel->mode & SILC_CHANNEL_MODE_PRIVKEY)) {
1972 if (!silc_server_create_channel_key(server, channel, 0))
1975 /* Send to the channel */
1976 silc_server_send_channel_key(server, sock, channel, FALSE);
1977 id = silc_id_id2str(channel->id, SILC_ID_CHANNEL);
1978 id_len = SILC_ID_CHANNEL_LEN;
1980 /* Send to the server */
1981 chk = silc_channel_key_payload_encode(id_len, id,
1982 strlen(channel->channel_key->
1984 channel->channel_key->
1986 channel->key_len / 8,
1988 silc_server_packet_send(server, sock, SILC_PACKET_CHANNEL_KEY, 0,
1989 chk->data, chk->len, FALSE);
1990 silc_buffer_free(chk);
1994 silc_free(channel_id);
1996 /* Since the channel is coming from server and we also know about it
1997 then send the JOIN notify to the server so that it see's our
1998 users on the channel "joining" the channel. */
1999 silc_server_announce_get_channel_users(server, channel, &users,
2002 silc_buffer_push(users, users->data - users->head);
2003 silc_server_packet_send(server, sock,
2004 SILC_PACKET_NOTIFY, SILC_PACKET_FLAG_LIST,
2005 users->data, users->len, FALSE);
2006 silc_buffer_free(users);
2009 silc_buffer_push(users_modes, users_modes->data - users_modes->head);
2010 silc_server_packet_send_dest(server, sock,
2011 SILC_PACKET_NOTIFY, SILC_PACKET_FLAG_LIST,
2012 channel->id, SILC_ID_CHANNEL,
2014 users_modes->len, FALSE);
2015 silc_buffer_free(users_modes);
2020 silc_channel_payload_free(payload);
2023 /* Received New Channel List packet, list of New Channel List payloads inside
2024 one packet. Process the New Channel payloads one by one. */
2026 void silc_server_new_channel_list(SilcServer server,
2027 SilcSocketConnection sock,
2028 SilcPacketContext *packet)
2030 SilcPacketContext *new;
2034 SILC_LOG_DEBUG(("Processing New Channel List"));
2036 if (sock->type == SILC_SOCKET_TYPE_CLIENT ||
2037 packet->src_id_type != SILC_ID_SERVER ||
2038 server->server_type == SILC_SERVER)
2041 /* If the sender of this packet is server and we are router we need to
2042 broadcast this packet to other routers in the network. Broadcast
2043 this list packet instead of multiple New Channel packets. */
2044 if (!server->standalone && server->server_type == SILC_ROUTER &&
2045 sock->type == SILC_SOCKET_TYPE_SERVER &&
2046 !(packet->flags & SILC_PACKET_FLAG_BROADCAST)) {
2047 SILC_LOG_DEBUG(("Broadcasting received New Channel List packet"));
2048 silc_server_packet_send(server, server->router->connection,
2050 packet->flags | SILC_PACKET_FLAG_BROADCAST,
2051 packet->buffer->data, packet->buffer->len, FALSE);
2054 /* Make copy of the original packet context, except for the actual
2055 data buffer, which we will here now fetch from the original buffer. */
2056 new = silc_packet_context_alloc();
2057 new->type = SILC_PACKET_NEW_CHANNEL;
2058 new->flags = packet->flags;
2059 new->src_id = packet->src_id;
2060 new->src_id_len = packet->src_id_len;
2061 new->src_id_type = packet->src_id_type;
2062 new->dst_id = packet->dst_id;
2063 new->dst_id_len = packet->dst_id_len;
2064 new->dst_id_type = packet->dst_id_type;
2066 buffer = silc_buffer_alloc(512);
2067 new->buffer = buffer;
2069 while (packet->buffer->len) {
2070 SILC_GET16_MSB(len1, packet->buffer->data);
2071 if ((len1 > packet->buffer->len) ||
2072 (len1 > buffer->truelen))
2075 SILC_GET16_MSB(len2, packet->buffer->data + 2 + len1);
2076 if ((len2 > packet->buffer->len) ||
2077 (len2 > buffer->truelen))
2080 silc_buffer_pull_tail(buffer, 8 + len1 + len2);
2081 silc_buffer_put(buffer, packet->buffer->data, 8 + len1 + len2);
2083 /* Process the New Channel */
2084 silc_server_new_channel(server, sock, new);
2086 silc_buffer_push_tail(buffer, 8 + len1 + len2);
2087 silc_buffer_pull(packet->buffer, 8 + len1 + len2);
2090 silc_buffer_free(buffer);
2094 /* Received key agreement packet. This packet is never for us. It is to
2095 the client in the packet's destination ID. Sending of this sort of packet
2096 equals sending private message, ie. it is sent point to point from
2097 one client to another. */
2099 void silc_server_key_agreement(SilcServer server,
2100 SilcSocketConnection sock,
2101 SilcPacketContext *packet)
2103 SilcSocketConnection dst_sock;
2104 SilcIDListData idata;
2106 SILC_LOG_DEBUG(("Start"));
2108 if (packet->src_id_type != SILC_ID_CLIENT ||
2109 packet->dst_id_type != SILC_ID_CLIENT)
2112 if (!packet->dst_id)
2115 /* Get the route to the client */
2116 dst_sock = silc_server_get_client_route(server, packet->dst_id,
2117 packet->dst_id_len, NULL, &idata);
2121 /* Relay the packet */
2122 silc_server_relay_packet(server, dst_sock, idata->send_key,
2123 idata->hmac_send, packet, FALSE);
2126 /* Received connection auth request packet that is used during connection
2127 phase to resolve the mandatory authentication method. This packet can
2128 actually be received at anytime but usually it is used only during
2129 the connection authentication phase. Now, protocol says that this packet
2130 can come from client or server, however, we support only this coming
2131 from client and expect that server's always knows what authentication
2134 void silc_server_connection_auth_request(SilcServer server,
2135 SilcSocketConnection sock,
2136 SilcPacketContext *packet)
2138 SilcServerConfigSectionClientConnection *client = NULL;
2141 SilcAuthMethod auth_meth;
2143 SILC_LOG_DEBUG(("Start"));
2145 if (packet->src_id_type && packet->src_id_type != SILC_ID_CLIENT)
2148 /* Parse the payload */
2149 ret = silc_buffer_unformat(packet->buffer,
2150 SILC_STR_UI_SHORT(&conn_type),
2151 SILC_STR_UI_SHORT(NULL),
2156 if (conn_type != SILC_SOCKET_TYPE_CLIENT)
2159 /* Get the authentication method for the client */
2160 auth_meth = SILC_AUTH_NONE;
2161 client = silc_server_config_find_client_conn(server->config,
2165 client = silc_server_config_find_client_conn(server->config,
2169 auth_meth = client->auth_meth;
2171 /* Send it back to the client */
2172 silc_server_send_connection_auth_request(server, sock,
2177 /* Received REKEY packet. The sender of the packet wants to regenerate
2178 its session keys. This starts the REKEY protocol. */
2180 void silc_server_rekey(SilcServer server,
2181 SilcSocketConnection sock,
2182 SilcPacketContext *packet)
2184 SilcProtocol protocol;
2185 SilcServerRekeyInternalContext *proto_ctx;
2186 SilcIDListData idata = (SilcIDListData)sock->user_data;
2188 SILC_LOG_DEBUG(("Start"));
2190 /* Allocate internal protocol context. This is sent as context
2192 proto_ctx = silc_calloc(1, sizeof(*proto_ctx));
2193 proto_ctx->server = (void *)server;
2194 proto_ctx->sock = sock;
2195 proto_ctx->responder = TRUE;
2196 proto_ctx->pfs = idata->rekey->pfs;
2198 /* Perform rekey protocol. Will call the final callback after the
2199 protocol is over. */
2200 silc_protocol_alloc(SILC_PROTOCOL_SERVER_REKEY,
2201 &protocol, proto_ctx, silc_server_rekey_final);
2202 sock->protocol = protocol;
2204 if (proto_ctx->pfs == FALSE)
2205 /* Run the protocol */
2206 silc_protocol_execute(protocol, server->schedule, 0, 0);