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;
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 while (!silc_id_create_client_id(server, server->id, server->rng,
1446 server->md5hash, nickname, &client_id)) {
1448 snprintf(&nickname[strlen(nickname) - 1], 1, "%d", nickfail);
1451 /* Update client entry */
1452 idata->registered = TRUE;
1453 client->nickname = nickname;
1454 client->username = username;
1455 client->userinfo = realname ? realname : strdup(" ");
1456 client->id = client_id;
1457 id_len = silc_id_get_len(client_id, SILC_ID_CLIENT);
1459 /* Add the client again to the ID cache */
1460 silc_idcache_add(server->local_list->clients, client->nickname,
1461 client_id, client, FALSE);
1463 /* Notify our router about new client on the SILC network */
1464 if (!server->standalone)
1465 silc_server_send_new_id(server, (SilcSocketConnection)
1466 server->router->connection,
1467 server->server_type == SILC_ROUTER ? TRUE : FALSE,
1468 client->id, SILC_ID_CLIENT, id_len);
1470 /* Send the new client ID to the client. */
1471 id_string = silc_id_id2str(client->id, SILC_ID_CLIENT);
1472 reply = silc_buffer_alloc(2 + 2 + id_len);
1473 silc_buffer_pull_tail(reply, SILC_BUFFER_END(reply));
1474 silc_buffer_format(reply,
1475 SILC_STR_UI_SHORT(SILC_ID_CLIENT),
1476 SILC_STR_UI_SHORT(id_len),
1477 SILC_STR_UI_XNSTRING(id_string, id_len),
1479 silc_server_packet_send(server, sock, SILC_PACKET_NEW_ID, 0,
1480 reply->data, reply->len, FALSE);
1481 silc_free(id_string);
1482 silc_buffer_free(reply);
1484 /* Send some nice info to the client */
1485 SILC_SERVER_SEND_NOTIFY(server, sock, SILC_NOTIFY_TYPE_NONE,
1486 ("Welcome to the SILC Network %s",
1488 SILC_SERVER_SEND_NOTIFY(server, sock, SILC_NOTIFY_TYPE_NONE,
1489 ("Your host is %s, running version %s",
1490 server->config->server_info->server_name,
1492 if (server->server_type == SILC_ROUTER) {
1493 SILC_SERVER_SEND_NOTIFY(server, sock, SILC_NOTIFY_TYPE_NONE,
1494 ("There are %d clients on %d servers in SILC "
1495 "Network", server->stat.clients,
1496 server->stat.servers + 1));
1497 SILC_SERVER_SEND_NOTIFY(server, sock, SILC_NOTIFY_TYPE_NONE,
1498 ("There are %d clients on %d server in our cell",
1499 server->stat.cell_clients,
1500 server->stat.cell_servers + 1));
1501 SILC_SERVER_SEND_NOTIFY(server, sock, SILC_NOTIFY_TYPE_NONE,
1502 ("I have %d clients, %d channels, %d servers and "
1504 server->stat.my_clients,
1505 server->stat.my_channels,
1506 server->stat.my_servers,
1507 server->stat.my_routers));
1508 SILC_SERVER_SEND_NOTIFY(server, sock, SILC_NOTIFY_TYPE_NONE,
1509 ("%d server operators and %d router operators "
1511 server->stat.my_server_ops,
1512 server->stat.my_router_ops));
1514 SILC_SERVER_SEND_NOTIFY(server, sock, SILC_NOTIFY_TYPE_NONE,
1515 ("I have %d clients and %d channels formed",
1516 server->stat.my_clients,
1517 server->stat.my_channels));
1518 SILC_SERVER_SEND_NOTIFY(server, sock, SILC_NOTIFY_TYPE_NONE,
1519 ("%d operators online",
1520 server->stat.my_server_ops));
1522 SILC_SERVER_SEND_NOTIFY(server, sock, SILC_NOTIFY_TYPE_NONE,
1523 ("Your connection is secured with %s cipher, "
1524 "key length %d bits",
1525 idata->send_key->cipher->name,
1526 idata->send_key->cipher->key_len));
1527 SILC_SERVER_SEND_NOTIFY(server, sock, SILC_NOTIFY_TYPE_NONE,
1528 ("Your current nickname is %s",
1532 silc_server_send_motd(server, sock);
1537 /* Create new server. This processes received New Server packet and
1538 saves the received Server ID. The server is our locally connected
1539 server thus we save all the information and save it to local list.
1540 This funtion can be used by both normal server and router server.
1541 If normal server uses this it means that its router has connected
1542 to the server. If router uses this it means that one of the cell's
1543 servers is connected to the router. */
1545 SilcServerEntry silc_server_new_server(SilcServer server,
1546 SilcSocketConnection sock,
1547 SilcPacketContext *packet)
1549 SilcBuffer buffer = packet->buffer;
1550 SilcServerEntry new_server;
1551 SilcServerID *server_id;
1552 SilcIDListData idata;
1553 unsigned char *server_name, *id_string;
1554 uint16 id_len, name_len;
1557 SILC_LOG_DEBUG(("Creating new server"));
1559 if (sock->type != SILC_SOCKET_TYPE_SERVER &&
1560 sock->type != SILC_SOCKET_TYPE_ROUTER)
1563 /* Take server entry */
1564 new_server = (SilcServerEntry)sock->user_data;
1565 idata = (SilcIDListData)new_server;
1567 /* Remove the old cache entry */
1568 silc_idcache_del_by_context(server->local_list->servers, new_server);
1570 /* Parse the incoming packet */
1571 ret = silc_buffer_unformat(buffer,
1572 SILC_STR_UI16_NSTRING_ALLOC(&id_string, &id_len),
1573 SILC_STR_UI16_NSTRING_ALLOC(&server_name,
1578 silc_free(id_string);
1580 silc_free(server_name);
1584 if (id_len > buffer->len) {
1585 silc_free(id_string);
1586 silc_free(server_name);
1591 server_name[255] = '\0';
1594 server_id = silc_id_str2id(id_string, id_len, SILC_ID_SERVER);
1596 silc_free(id_string);
1597 silc_free(server_name);
1600 silc_free(id_string);
1602 /* Update server entry */
1603 idata->registered = TRUE;
1604 new_server->server_name = server_name;
1605 new_server->id = server_id;
1607 /* Add again the entry to the ID cache. */
1608 silc_idcache_add(server->local_list->servers, server_name, server_id,
1611 /* Distribute the information about new server in the SILC network
1612 to our router. If we are normal server we won't send anything
1613 since this connection must be our router connection. */
1614 if (server->server_type == SILC_ROUTER && !server->standalone &&
1615 server->router->connection != sock)
1616 silc_server_send_new_id(server, server->router->connection,
1617 TRUE, new_server->id, SILC_ID_SERVER,
1618 silc_id_get_len(server_id, SILC_ID_SERVER));
1620 if (server->server_type == SILC_ROUTER)
1621 server->stat.cell_servers++;
1626 /* Processes incoming New ID packet. New ID Payload is used to distribute
1627 information about newly registered clients and servers. */
1629 static void silc_server_new_id_real(SilcServer server,
1630 SilcSocketConnection sock,
1631 SilcPacketContext *packet,
1634 SilcBuffer buffer = packet->buffer;
1636 SilcServerEntry router;
1637 SilcSocketConnection router_sock;
1642 SILC_LOG_DEBUG(("Processing new ID"));
1644 if (sock->type == SILC_SOCKET_TYPE_CLIENT ||
1645 server->server_type == SILC_SERVER ||
1646 packet->src_id_type != SILC_ID_SERVER)
1649 idp = silc_id_payload_parse(buffer);
1653 id_type = silc_id_payload_get_type(idp);
1655 /* Normal server cannot have other normal server connections */
1656 if (id_type == SILC_ID_SERVER && sock->type == SILC_SOCKET_TYPE_SERVER)
1659 id = silc_id_payload_get_id(idp);
1663 /* If the sender of this packet is server and we are router we need to
1664 broadcast this packet to other routers in the network. */
1665 if (broadcast && !server->standalone && server->server_type == SILC_ROUTER &&
1666 sock->type == SILC_SOCKET_TYPE_SERVER &&
1667 !(packet->flags & SILC_PACKET_FLAG_BROADCAST)) {
1668 SILC_LOG_DEBUG(("Broadcasting received New ID packet"));
1669 silc_server_packet_send(server, server->router->connection,
1671 packet->flags | SILC_PACKET_FLAG_BROADCAST,
1672 buffer->data, buffer->len, FALSE);
1675 if (sock->type == SILC_SOCKET_TYPE_SERVER)
1676 id_list = server->local_list;
1678 id_list = server->global_list;
1680 /* If the packet is coming from server then use the sender as the
1681 origin of the the packet. If it came from router then check the real
1682 sender of the packet and use that as the origin. */
1683 if (sock->type == SILC_SOCKET_TYPE_SERVER) {
1685 router = sock->user_data;
1687 void *sender_id = silc_id_str2id(packet->src_id, packet->src_id_len,
1688 packet->src_id_type);
1689 router = silc_idlist_find_server_by_id(server->global_list,
1690 sender_id, TRUE, NULL);
1692 router = silc_idlist_find_server_by_id(server->local_list,
1693 sender_id, TRUE, NULL);
1694 silc_free(sender_id);
1701 case SILC_ID_CLIENT:
1703 SilcClientEntry entry;
1705 SILC_LOG_DEBUG(("New client id(%s) from [%s] %s",
1706 silc_id_render(id, SILC_ID_CLIENT),
1707 sock->type == SILC_SOCKET_TYPE_SERVER ?
1708 "Server" : "Router", sock->hostname));
1710 /* As a router we keep information of all global information in our
1711 global list. Cell wide information however is kept in the local
1713 entry = silc_idlist_add_client(id_list, NULL, NULL, NULL,
1716 SILC_LOG_ERROR(("Could not add new client to the ID Cache"));
1718 /* Inform the sender that the ID is not usable */
1719 silc_server_send_notify_signoff(server, sock, FALSE, id, NULL);
1722 entry->nickname = NULL;
1723 entry->data.registered = TRUE;
1725 if (sock->type == SILC_SOCKET_TYPE_SERVER)
1726 server->stat.cell_clients++;
1727 server->stat.clients++;
1731 case SILC_ID_SERVER:
1733 SilcServerEntry entry;
1735 /* If the ID is mine, ignore it. */
1736 if (SILC_ID_SERVER_COMPARE(id, server->id)) {
1737 SILC_LOG_DEBUG(("Ignoring my own ID as new ID"));
1741 SILC_LOG_DEBUG(("New server id(%s) from [%s] %s",
1742 silc_id_render(id, SILC_ID_SERVER),
1743 sock->type == SILC_SOCKET_TYPE_SERVER ?
1744 "Server" : "Router", sock->hostname));
1746 /* As a router we keep information of all global information in our
1747 global list. Cell wide information however is kept in the local
1749 entry = silc_idlist_add_server(id_list, NULL, 0, id, router,
1752 SILC_LOG_ERROR(("Could not add new server to the ID Cache"));
1755 entry->data.registered = TRUE;
1757 if (sock->type == SILC_SOCKET_TYPE_SERVER)
1758 server->stat.cell_servers++;
1759 server->stat.servers++;
1763 case SILC_ID_CHANNEL:
1764 SILC_LOG_ERROR(("Channel cannot be registered with NEW_ID packet"));
1772 silc_id_payload_free(idp);
1776 /* Processes incoming New ID packet. New ID Payload is used to distribute
1777 information about newly registered clients and servers. */
1779 void silc_server_new_id(SilcServer server, SilcSocketConnection sock,
1780 SilcPacketContext *packet)
1782 silc_server_new_id_real(server, sock, packet, TRUE);
1785 /* Receoved New Id List packet, list of New ID payloads inside one
1786 packet. Process the New ID payloads one by one. */
1788 void silc_server_new_id_list(SilcServer server, SilcSocketConnection sock,
1789 SilcPacketContext *packet)
1791 SilcPacketContext *new_id;
1795 SILC_LOG_DEBUG(("Processing New ID List"));
1797 if (sock->type == SILC_SOCKET_TYPE_CLIENT ||
1798 packet->src_id_type != SILC_ID_SERVER)
1801 /* If the sender of this packet is server and we are router we need to
1802 broadcast this packet to other routers in the network. Broadcast
1803 this list packet instead of multiple New ID packets. */
1804 if (!server->standalone && server->server_type == SILC_ROUTER &&
1805 sock->type == SILC_SOCKET_TYPE_SERVER &&
1806 !(packet->flags & SILC_PACKET_FLAG_BROADCAST)) {
1807 SILC_LOG_DEBUG(("Broadcasting received New ID List packet"));
1808 silc_server_packet_send(server, server->router->connection,
1810 packet->flags | SILC_PACKET_FLAG_BROADCAST,
1811 packet->buffer->data, packet->buffer->len, FALSE);
1814 /* Make copy of the original packet context, except for the actual
1815 data buffer, which we will here now fetch from the original buffer. */
1816 new_id = silc_packet_context_alloc();
1817 new_id->type = SILC_PACKET_NEW_ID;
1818 new_id->flags = packet->flags;
1819 new_id->src_id = packet->src_id;
1820 new_id->src_id_len = packet->src_id_len;
1821 new_id->src_id_type = packet->src_id_type;
1822 new_id->dst_id = packet->dst_id;
1823 new_id->dst_id_len = packet->dst_id_len;
1824 new_id->dst_id_type = packet->dst_id_type;
1826 idp = silc_buffer_alloc(256);
1827 new_id->buffer = idp;
1829 while (packet->buffer->len) {
1830 SILC_GET16_MSB(id_len, packet->buffer->data + 2);
1831 if ((id_len > packet->buffer->len) ||
1832 (id_len > idp->truelen))
1835 silc_buffer_pull_tail(idp, 4 + id_len);
1836 silc_buffer_put(idp, packet->buffer->data, 4 + id_len);
1838 /* Process the New ID */
1839 silc_server_new_id_real(server, sock, new_id, FALSE);
1841 silc_buffer_push_tail(idp, 4 + id_len);
1842 silc_buffer_pull(packet->buffer, 4 + id_len);
1845 silc_buffer_free(idp);
1849 /* Received New Channel packet. Information about new channels in the
1850 network are distributed using this packet. Save the information about
1851 the new channel. This usually comes from router but also normal server
1852 can send this to notify channels it has when it connects to us. */
1854 void silc_server_new_channel(SilcServer server,
1855 SilcSocketConnection sock,
1856 SilcPacketContext *packet)
1858 SilcChannelPayload payload;
1859 SilcChannelID *channel_id;
1866 SILC_LOG_DEBUG(("Processing New Channel"));
1868 if (sock->type == SILC_SOCKET_TYPE_CLIENT ||
1869 packet->src_id_type != SILC_ID_SERVER ||
1870 server->server_type == SILC_SERVER)
1873 /* Parse the channel payload */
1874 payload = silc_channel_payload_parse(packet->buffer);
1878 /* Get the channel ID */
1879 channel_id = silc_channel_get_id_parse(payload);
1881 silc_channel_payload_free(payload);
1885 channel_name = silc_channel_get_name(payload, &name_len);
1887 channel_name[255] = '\0';
1889 id = silc_channel_get_id(payload, &id_len);
1891 if (sock->type == SILC_SOCKET_TYPE_ROUTER) {
1892 /* Add the channel to global list as it is coming from router. It
1893 cannot be our own channel as it is coming from router. */
1895 SILC_LOG_DEBUG(("New channel id(%s) from [Router] %s",
1896 silc_id_render(channel_id, SILC_ID_CHANNEL),
1899 silc_idlist_add_channel(server->global_list, strdup(channel_name),
1900 0, channel_id, sock->user_data, NULL, NULL);
1902 server->stat.channels++;
1904 /* The channel is coming from our server, thus it is in our cell
1905 we will add it to our local list. */
1906 SilcChannelEntry channel;
1909 SILC_LOG_DEBUG(("New channel id(%s) from [Server] %s",
1910 silc_id_render(channel_id, SILC_ID_CHANNEL),
1913 /* Check that we don't already have this channel */
1914 channel = silc_idlist_find_channel_by_name(server->local_list,
1915 channel_name, NULL);
1917 channel = silc_idlist_find_channel_by_name(server->global_list,
1918 channel_name, NULL);
1920 /* If the channel does not exist, then create it. We create the channel
1921 with the channel ID provided by the server. This creates a new
1922 key to the channel as well that we will send to the server. */
1924 channel = silc_server_create_new_channel_with_id(server, NULL, NULL,
1928 silc_channel_payload_free(payload);
1929 silc_free(channel_id);
1933 /* Get the mode and set it to the channel */
1934 channel->mode = silc_channel_get_mode(payload);
1936 /* Send the new channel key to the server */
1937 chk = silc_channel_key_payload_encode(id_len, id,
1938 strlen(channel->channel_key->
1940 channel->channel_key->cipher->name,
1941 channel->key_len / 8,
1943 silc_server_packet_send(server, sock, SILC_PACKET_CHANNEL_KEY, 0,
1944 chk->data, chk->len, FALSE);
1945 silc_buffer_free(chk);
1948 /* The channel exist by that name, check whether the ID's match.
1949 If they don't then we'll force the server to use the ID we have.
1950 We also create a new key for the channel. */
1951 SilcBuffer users = NULL, users_modes = NULL;
1954 channel_id = silc_id_dup(channel_id, SILC_ID_CHANNEL);
1956 if (!SILC_ID_CHANNEL_COMPARE(channel_id, channel->id)) {
1957 /* They don't match, send CHANNEL_CHANGE notify to the server to
1958 force the ID change. */
1959 SILC_LOG_DEBUG(("Forcing the server to change Channel ID"));
1960 silc_server_send_notify_channel_change(server, sock, FALSE,
1961 channel_id, channel->id);
1964 /* If the mode is different from what we have then enforce the
1966 mode = silc_channel_get_mode(payload);
1967 if (channel->mode != mode) {
1968 SILC_LOG_DEBUG(("Forcing the server to change channel mode"));
1969 silc_server_send_notify_cmode(server, sock, FALSE, channel,
1970 channel->mode, server->id,
1972 channel->cipher, channel->hmac_name);
1975 /* Create new key for the channel and send it to the server and
1976 everybody else possibly on the channel. */
1978 if (!(channel->mode & SILC_CHANNEL_MODE_PRIVKEY)) {
1979 if (!silc_server_create_channel_key(server, channel, 0))
1982 /* Send to the channel */
1983 silc_server_send_channel_key(server, sock, channel, FALSE);
1984 id = silc_id_id2str(channel->id, SILC_ID_CHANNEL);
1985 id_len = SILC_ID_CHANNEL_LEN;
1987 /* Send to the server */
1988 chk = silc_channel_key_payload_encode(id_len, id,
1989 strlen(channel->channel_key->
1991 channel->channel_key->
1993 channel->key_len / 8,
1995 silc_server_packet_send(server, sock, SILC_PACKET_CHANNEL_KEY, 0,
1996 chk->data, chk->len, FALSE);
1997 silc_buffer_free(chk);
2001 silc_free(channel_id);
2003 /* Since the channel is coming from server and we also know about it
2004 then send the JOIN notify to the server so that it see's our
2005 users on the channel "joining" the channel. */
2006 silc_server_announce_get_channel_users(server, channel, &users,
2009 silc_buffer_push(users, users->data - users->head);
2010 silc_server_packet_send(server, sock,
2011 SILC_PACKET_NOTIFY, SILC_PACKET_FLAG_LIST,
2012 users->data, users->len, FALSE);
2013 silc_buffer_free(users);
2016 silc_buffer_push(users_modes, users_modes->data - users_modes->head);
2017 silc_server_packet_send_dest(server, sock,
2018 SILC_PACKET_NOTIFY, SILC_PACKET_FLAG_LIST,
2019 channel->id, SILC_ID_CHANNEL,
2021 users_modes->len, FALSE);
2022 silc_buffer_free(users_modes);
2027 silc_channel_payload_free(payload);
2030 /* Received New Channel List packet, list of New Channel List payloads inside
2031 one packet. Process the New Channel payloads one by one. */
2033 void silc_server_new_channel_list(SilcServer server,
2034 SilcSocketConnection sock,
2035 SilcPacketContext *packet)
2037 SilcPacketContext *new;
2041 SILC_LOG_DEBUG(("Processing New Channel List"));
2043 if (sock->type == SILC_SOCKET_TYPE_CLIENT ||
2044 packet->src_id_type != SILC_ID_SERVER ||
2045 server->server_type == SILC_SERVER)
2048 /* If the sender of this packet is server and we are router we need to
2049 broadcast this packet to other routers in the network. Broadcast
2050 this list packet instead of multiple New Channel packets. */
2051 if (!server->standalone && server->server_type == SILC_ROUTER &&
2052 sock->type == SILC_SOCKET_TYPE_SERVER &&
2053 !(packet->flags & SILC_PACKET_FLAG_BROADCAST)) {
2054 SILC_LOG_DEBUG(("Broadcasting received New Channel List packet"));
2055 silc_server_packet_send(server, server->router->connection,
2057 packet->flags | SILC_PACKET_FLAG_BROADCAST,
2058 packet->buffer->data, packet->buffer->len, FALSE);
2061 /* Make copy of the original packet context, except for the actual
2062 data buffer, which we will here now fetch from the original buffer. */
2063 new = silc_packet_context_alloc();
2064 new->type = SILC_PACKET_NEW_CHANNEL;
2065 new->flags = packet->flags;
2066 new->src_id = packet->src_id;
2067 new->src_id_len = packet->src_id_len;
2068 new->src_id_type = packet->src_id_type;
2069 new->dst_id = packet->dst_id;
2070 new->dst_id_len = packet->dst_id_len;
2071 new->dst_id_type = packet->dst_id_type;
2073 buffer = silc_buffer_alloc(512);
2074 new->buffer = buffer;
2076 while (packet->buffer->len) {
2077 SILC_GET16_MSB(len1, packet->buffer->data);
2078 if ((len1 > packet->buffer->len) ||
2079 (len1 > buffer->truelen))
2082 SILC_GET16_MSB(len2, packet->buffer->data + 2 + len1);
2083 if ((len2 > packet->buffer->len) ||
2084 (len2 > buffer->truelen))
2087 silc_buffer_pull_tail(buffer, 8 + len1 + len2);
2088 silc_buffer_put(buffer, packet->buffer->data, 8 + len1 + len2);
2090 /* Process the New Channel */
2091 silc_server_new_channel(server, sock, new);
2093 silc_buffer_push_tail(buffer, 8 + len1 + len2);
2094 silc_buffer_pull(packet->buffer, 8 + len1 + len2);
2097 silc_buffer_free(buffer);
2101 /* Received key agreement packet. This packet is never for us. It is to
2102 the client in the packet's destination ID. Sending of this sort of packet
2103 equals sending private message, ie. it is sent point to point from
2104 one client to another. */
2106 void silc_server_key_agreement(SilcServer server,
2107 SilcSocketConnection sock,
2108 SilcPacketContext *packet)
2110 SilcSocketConnection dst_sock;
2111 SilcIDListData idata;
2113 SILC_LOG_DEBUG(("Start"));
2115 if (packet->src_id_type != SILC_ID_CLIENT ||
2116 packet->dst_id_type != SILC_ID_CLIENT)
2119 if (!packet->dst_id)
2122 /* Get the route to the client */
2123 dst_sock = silc_server_get_client_route(server, packet->dst_id,
2124 packet->dst_id_len, NULL, &idata);
2128 /* Relay the packet */
2129 silc_server_relay_packet(server, dst_sock, idata->send_key,
2130 idata->hmac_send, packet, FALSE);
2133 /* Received connection auth request packet that is used during connection
2134 phase to resolve the mandatory authentication method. This packet can
2135 actually be received at anytime but usually it is used only during
2136 the connection authentication phase. Now, protocol says that this packet
2137 can come from client or server, however, we support only this coming
2138 from client and expect that server's always knows what authentication
2141 void silc_server_connection_auth_request(SilcServer server,
2142 SilcSocketConnection sock,
2143 SilcPacketContext *packet)
2145 SilcServerConfigSectionClientConnection *client = NULL;
2148 SilcAuthMethod auth_meth;
2150 SILC_LOG_DEBUG(("Start"));
2152 if (packet->src_id_type && packet->src_id_type != SILC_ID_CLIENT)
2155 /* Parse the payload */
2156 ret = silc_buffer_unformat(packet->buffer,
2157 SILC_STR_UI_SHORT(&conn_type),
2158 SILC_STR_UI_SHORT(NULL),
2163 if (conn_type != SILC_SOCKET_TYPE_CLIENT)
2166 /* Get the authentication method for the client */
2167 auth_meth = SILC_AUTH_NONE;
2168 client = silc_server_config_find_client_conn(server->config,
2172 client = silc_server_config_find_client_conn(server->config,
2176 auth_meth = client->auth_meth;
2178 /* Send it back to the client */
2179 silc_server_send_connection_auth_request(server, sock,
2184 /* Received REKEY packet. The sender of the packet wants to regenerate
2185 its session keys. This starts the REKEY protocol. */
2187 void silc_server_rekey(SilcServer server,
2188 SilcSocketConnection sock,
2189 SilcPacketContext *packet)
2191 SilcProtocol protocol;
2192 SilcServerRekeyInternalContext *proto_ctx;
2193 SilcIDListData idata = (SilcIDListData)sock->user_data;
2195 SILC_LOG_DEBUG(("Start"));
2197 /* Allocate internal protocol context. This is sent as context
2199 proto_ctx = silc_calloc(1, sizeof(*proto_ctx));
2200 proto_ctx->server = (void *)server;
2201 proto_ctx->sock = sock;
2202 proto_ctx->responder = TRUE;
2203 proto_ctx->pfs = idata->rekey->pfs;
2205 /* Perform rekey protocol. Will call the final callback after the
2206 protocol is over. */
2207 silc_protocol_alloc(SILC_PROTOCOL_SERVER_REKEY,
2208 &protocol, proto_ctx, silc_server_rekey_final);
2209 sock->protocol = protocol;
2211 if (proto_ctx->pfs == FALSE)
2212 /* Run the protocol */
2213 silc_protocol_execute(protocol, server->schedule, 0, 0);